Write an RTC Driver Using the RTC Framework
Write a Linux 6.12 RTC driver with rtc_class_ops, read_time and set_time using 64-bit time, range limits, and test it with hwclock and sysfs.

A real-time clock keeps wall-clock time while the machine is off. Every PC has one, most boards have one on the SoC or as a small chip next to it, and the kernel reads it at boot to set the system clock and writes it back at shutdown. The RTC framework gives a driver a small set of operations to fill in, and in return the device appears as /dev/rtcN, a sysfs directory, and a source for hwclock and for the kernel's own time-keeping. This post writes a driver for a memory-mapped RTC with a 32-bit seconds counter on Linux 6.12, covers the time conversion helpers and the range limits that prevent date bugs, and shows how to test it. It follows the driver introduction.
The framework is described in the kernel's RTC documentation and in include/linux/rtc.h.
What the hardware looks like
RTC chips come in two common shapes. Some keep calendar fields, with separate registers for seconds, minutes, hours, day, month and year, usually in BCD. Others keep a single counter of elapsed seconds since a fixed origin. The second shape is much easier to drive, and the framework has helpers for both. The example uses a counter: a 32-bit register holding seconds since 1970-01-01, plus a control register with a write-enable bit.
#define REG_COUNT 0x00
#define REG_CTRL 0x04
#define CTRL_WRITE_EN BIT(0)A counter that is 32 bits wide and unsigned wraps in the year 2106. A signed 32-bit counter would wrap in 2038. Knowing the limit is the first part of the driver, because the framework needs it.
The operations
The core of an RTC driver is a struct rtc_class_ops with the functions the framework calls. The two that every driver needs are read_time and set_time. Both work with struct rtc_time, the broken-down calendar structure used by the kernel:
static int sample_rtc_read_time(struct device *dev, struct rtc_time *tm)
{
struct sample_rtc *r = dev_get_drvdata(dev);
u32 seconds = ioread32(r->regs + REG_COUNT);
rtc_time64_to_tm(seconds, tm);
return 0;
}
static int sample_rtc_set_time(struct device *dev, struct rtc_time *tm)
{
struct sample_rtc *r = dev_get_drvdata(dev);
time64_t seconds = rtc_tm_to_time64(tm);
iowrite32(CTRL_WRITE_EN, r->regs + REG_CTRL);
iowrite32(lower_32_bits(seconds), r->regs + REG_COUNT);
iowrite32(0, r->regs + REG_CTRL);
return 0;
}The two helpers do the calendar arithmetic that is easy to get wrong. rtc_time64_to_tm() converts a count of seconds since 1970 into year, month, day, hour, minute and second fields, handling leap years. rtc_tm_to_time64() goes the other way. The 64 in the name matters: they take and return time64_t, which does not overflow in 2038 on 32-bit systems. The older rtc_time_to_tm() and rtc_tm_to_time() used unsigned long and are the wrong choice on a 32-bit machine.
A few details in set_time. The write-enable bit protects the counter from accidental writes, so the driver sets it, writes the value and clears it. The cast to 32 bits is safe only because the range limits below stop the framework from passing a larger value. A value outside the range is rejected before the driver sees it.
The rtc_tm_to_time64() helper ignores the tm_wday and tm_yday fields, and rtc_time64_to_tm() fills them all in. The framework expects tm_year as years since 1900 and tm_mon as 0 to 11, which are the conventions of struct tm, and the helpers follow them.
Registration
Registration has an order that the framework enforces: allocate the device, set its properties, then register it.
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/rtc.h>
struct sample_rtc {
struct rtc_device *rtc;
void __iomem *regs;
};
static const struct rtc_class_ops sample_rtc_ops = {
.read_time = sample_rtc_read_time,
.set_time = sample_rtc_set_time,
};
static int sample_rtc_probe(struct platform_device *pdev)
{
struct device *dev = &pdev->dev;
struct sample_rtc *r;
r = devm_kzalloc(dev, sizeof(*r), GFP_KERNEL);
if (!r)
return -ENOMEM;
r->regs = devm_platform_ioremap_resource(pdev, 0);
if (IS_ERR(r->regs))
return PTR_ERR(r->regs);
platform_set_drvdata(pdev, r);
r->rtc = devm_rtc_allocate_device(dev);
if (IS_ERR(r->rtc))
return PTR_ERR(r->rtc);
r->rtc->ops = &sample_rtc_ops;
r->rtc->range_min = 0;
r->rtc->range_max = U32_MAX;
return devm_rtc_register_device(r->rtc);
}
static const struct of_device_id sample_rtc_of_match[] = {
{ .compatible = "example,counter-rtc" },
{ }
};
MODULE_DEVICE_TABLE(of, sample_rtc_of_match);
static struct platform_driver sample_rtc_driver = {
.probe = sample_rtc_probe,
.driver = {
.name = "sample-rtc",
.of_match_table = sample_rtc_of_match,
},
};
module_platform_driver(sample_rtc_driver);
MODULE_DESCRIPTION("Counter-based RTC driver");
MODULE_LICENSE("GPL");The platform_set_drvdata() call comes before registration, because read_time can be called as soon as devm_rtc_register_device() returns, and the kernel itself reads the clock during registration to set the system time if the RTC is selected for that. A callback that finds no driver data crashes at boot, which is a hard failure to debug.
range_min and range_max declare the span of times the hardware can represent, as time64_t seconds since 1970. For this counter that is 0 to U32_MAX, which ends on 2106-02-07. Setting the range is what protects the driver: the framework refuses a set_time outside it with -ERANGE, and a read_time that returns a value outside it is treated as an error. If you leave the range at its default, a hardware counter that wraps gives a time in 1970, and the system clock jumps backward at the wrap.
Testing it
After the driver binds, the RTC appears with the next free number. cat the sysfs files first:
cat /sys/class/rtc/rtc1/name
cat /sys/class/rtc/rtc1/since_epoch
cat /sys/class/rtc/rtc1/date
cat /sys/class/rtc/rtc1/timesample-rtc 10000000.rtc
1735891200
2025-01-03
08:00:00The number 1735891200 is the raw counter and the other two files are its conversion, which you can check yourself: date -u -d @1735891200 prints the third of January 2025 at eight in the morning, UTC. RTCs conventionally hold UTC, and the conversion to local time belongs to the system clock configuration, not to the hardware. A system that dual-boots with an operating system storing local time in the RTC is the classic source of a clock that is wrong by the timezone offset.
Use hwclock to read and write through the device node:
sudo hwclock --rtc=/dev/rtc1 --show --utc
sudo hwclock --rtc=/dev/rtc1 --systohc --utc2025-01-03 08:00:00.000000+00:00The second command writes the system time into the RTC and prints nothing on success. This exercises set_time, and a follow-up --show confirms the write took effect. A driver that returns success from set_time without writing is detected by exactly this round trip.
Alarms and interrupts
A wake-up alarm is optional. To support one, implement read_alarm, set_alarm and alarm_irq_enable in the operations table, and call rtc_update_irq() from the interrupt handler when the alarm fires so the framework wakes anyone waiting on the device. The alarm lets a machine schedule its own wake from suspend, which is how some systems implement timed boots. Advertise the capability by providing the interrupt in the device tree and calling device_init_wakeup(dev, true) in probe. If the hardware has no alarm, leave the callbacks out and the framework removes the alarm interface.
Which RTC the kernel uses
A machine can have several RTCs. The kernel uses one at boot to set the system time, selected by CONFIG_RTC_HCTOSYS_DEVICE, and to write it back at shutdown with CONFIG_RTC_SYSTOHC_DEVICE. By convention the one named rtc0 is the main one. If your driver is for a secondary clock, its number will be higher, and /dev/rtc is a link to the first. Most modern systems read the time from user space instead, with systemd-timesyncd or chronyd, which also keep the RTC updated.


