Skip to contentSkip to conversation

Raspberry Pi 5 RTC driver

PATCH Superseded Sander Speetjens 3 participants 8 messages Sep 23

Subsystems (4)

v4 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v4 cover Raspberry Pi 5 RTC driver

The Raspberry Pi 5 has a battery-backed RTC located
on the DA9091 PMIC accessed via the I2C bus[1].
Access to the RTC is provided exclusively through the VideoCore firmware mailbox interface (not via a direct I2C register interface).

As stated by jamesh on the Rasperry Pi forum [2],
"There are no plans to release a public datasheet for the DA9091." So we are stuck with the firmware interface for now.
PhilE also states that "The firmware needs full control of the DA9091, sometimes with extremely low latency,
otherwise there's a risk the supply could just cut the power."

This series adds:
- a DT property for the charging voltage of the RTC battery
- the RTC driver that talks to the firmware

The driver and binding are based on (and largely taken from) the downstream Raspberry Pi kernel. The main changes for upstream are the addition of a dt binding property for setting the charging voltage and binding the driver from the raspberrypi-firmware driver instead of from the DT.

The driver supports time read/write, alarm, and optional trickle charging voltage configuration via the device-tree.

Testing
- Hardware: Raspberry Pi 5 Model B revC
- Kernel: v7.2
- dt_binding_check: passed
- Basic RTC operations (read/write time, alarm) verified

[1] https://www.raspberrypi.com/news/introducing-raspberry-pi-5/
[2] https://forums.raspberrypi.com/viewtopic.php?t=359073

Sander Speetjens (2):
dt-bindings: rtc: Add property for Raspberry Pi 5 RTC
rtc: Add Raspberry Pi 5 RTC driver

 .../arm/bcm/raspberrypi,bcm2835-firmware.yaml |   8 +
 drivers/firmware/raspberrypi.c                |  25 ++
 drivers/rtc/Kconfig                           |  12 +
 drivers/rtc/Makefile                          |   1 +
 drivers/rtc/rtc-raspberrypi.c                 | 242 ++++++++++++++++++
 5 files changed, 288 insertions(+)
 create mode 100644 drivers/rtc/rtc-raspberrypi.c

base-commit: 93f51579e7df248780214094418f205253383cc5

Signature · 1 line
2.55.0
#1 lore raw

PATCH v4 1/2 dt-bindings: rtc: Add property for Raspberry Pi 5 RTC

The Raspberry Pi 5 has a built-in RTC that can be battery-backed via the VBAT pin. When a rechargeable coin cell is used, the hardware requires a trickle-charge voltage to keep the battery topped up.

Expose the trickle-voltage-millivolt DT property so the driver can program the correct charge voltage for board specific configurations with the attached battery.

---
V3 -> V4: Add a reff to uint32

V2 -> V3: Fixed the firmware property naming

V1 -> V2: As discussed with Krzysztof Kozlowski, this property is folded into the existing raspberrypi,bcm2835-firmware properties.

 .../bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml    | 8 ++++++++
 1 file changed, 8 insertions(+)
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml +8 -0
index 983ea80eaec9..9349fc8908a5 100644
@@ -133,6 +133,13 @@ properties:
     required:
       - compatible
 
+  trickle-voltage-millivolt:
+    allOf:
+      - $ref: /schemas/rtc/rtc.yaml#/properties/trickle-voltage-millivolt
+      - $ref: /schemas/types.yaml#/definitions/uint32
+      - minimum: 1300
+        maximum: 5000
+
 required:
   - compatible
   - mboxes
@@ -144,6 +151,7 @@ examples:
     firmware {
         compatible = "raspberrypi,bcm2835-firmware", "simple-mfd";
         mboxes = <&mailbox>;
+        trickle-voltage-millivolt = <3300>;
 
         firmware_clocks: clocks {
             compatible = "raspberrypi,firmware-clocks";
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v4 1/2] dt-bindings: rtc: Add property for Raspberry Pi 5 RTC

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] dt-bindings: minimum and maximum constraints are ignored due to array isolation --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] dt-bindings: minimum and maximum constraints are ignored due to array isolation --

commit ff7cdbdeb8fcd609ef36f409f92bd2d94dc107d7
Author: Sander Speetjens <[email protected]>

dt-bindings: rtc: Add property for Raspberry Pi 5 RTC

The Raspberry Pi 5 has a built-in RTC that can be battery-backed via the VBAT pin. Expose the trickle-voltage-millivolt DT property so the driver can program the correct charge voltage for board specific configurations with the attached battery.

Quoted 5 lines
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml +0 -0
index 983ea80eaec97..9349fc8908a50 100644

Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml properties: · line 141

Quoted 9 lines
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml +6 -0
@@ -133,6 +133,13 @@ properties:
     required:
       - compatible
 
+  trickle-voltage-millivolt:
+    allOf:
+      - $ref: /schemas/rtc/rtc.yaml#/properties/trickle-voltage-millivolt
+      - $ref: /schemas/types.yaml#/definitions/uint32
+      - minimum: 1300
+        maximum: 5000

[Severity: Low]
Are the minimum and maximum constraints ignored here due to being placed in a separate dictionary under allOf?

In dt-schema, properties using scalar types like uint32 are parsed as arrays. The JSON Schema minimum and maximum keywords only apply to numeric instances, so when applied to an array, they are silently ignored. The dt-schema preprocessor fixes this by pushing constraints into the items schema, but only if they reside in the exact same dictionary as the reference to /schemas/types.yaml#/definitions/uint32.

Because minimum and maximum are placed in a separate dictionary within the allOf list, the preprocessor misses them, leaving them ineffective. Out-of-bounds trickle-voltage-millivolt values could silently pass dt_binding_check validation.

Quoted 4 lines
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml +1 -0
+
 required:
   - compatible
   - mboxes
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH v4 1/2] dt-bindings: rtc: Add property for Raspberry Pi 5 RTC

In reply to Sander Speetjens
Quoted 26 lines · Sander Speetjens

The Raspberry Pi 5 has a built-in RTC that can be battery-backed via the VBAT pin. When a rechargeable coin cell is used, the hardware requires a trickle-charge voltage to keep the battery topped up.

Expose the trickle-voltage-millivolt DT property so the driver can program the correct charge voltage for board specific configurations with the attached battery.

---
V3 -> V4: Add a reff to uint32

V2 -> V3: Fixed the firmware property naming

V1 -> V2: As discussed with Krzysztof Kozlowski, this property is folded into the existing raspberrypi,bcm2835-firmware properties.

 .../bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml    | 8 ++++++++
 1 file changed, 8 insertions(+)
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml +0 -0
index 983ea80eaec9..9349fc8908a5 100644

Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml properties: · line 139

Quoted 7 lines
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml +4 -0
@@ -133,6 +133,13 @@ properties:
     required:
       - compatible
 
+  trickle-voltage-millivolt:
+    allOf:
+      - $ref: /schemas/rtc/rtc.yaml#/properties/trickle-voltage-millivolt
+      - $ref: /schemas/types.yaml#/definitions/uint32

Is this actually needed? Shouldn't the type of uint32 come from the same source that the type in rtc.yaml does? Or is this missing in dt-schema?

Quoted 17 lines
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml +4 -0
+      - minimum: 1300
+        maximum: 5000
+
 required:
   - compatible
   - mboxes
@@ -144,6 +151,7 @@ examples:
     firmware {
         compatible = "raspberrypi,bcm2835-firmware", "simple-mfd";
         mboxes = <&mailbox>;
+        trickle-voltage-millivolt = <3300>;
 
         firmware_clocks: clocks {
             compatible = "raspberrypi,firmware-clocks";
Signature · 1 line
2.55.0
#4 lore raw

Re: [PATCH v4 1/2] dt-bindings: rtc: Add property for Raspberry Pi 5 RTC

Quoted 2 lines

Is this actually needed? Shouldn't the type of uint32 come from the same source that the type in rtc.yaml does? Or is this missing in dt-schema?

The rtc.yaml does only provide the description not the type, I also expected the type to be there.

#5 lore raw

Re: [PATCH v4 1/2] dt-bindings: rtc: Add property for Raspberry Pi 5 RTC

Quoted 6 lines · Sander Speetjens
Quoted 2 lines

Is this actually needed? Shouldn't the type of uint32 come from the same source that the type in rtc.yaml does? Or is this missing in dt-schema?

The rtc.yaml does only provide the description not the type, I also expected the type to be there.

I think this should be dropped and the type added to property-units.yaml, I don't know where the one in rtc.yaml is getting a type from.

#6 lore raw

PATCH v4 2/2 rtc: Add Raspberry Pi 5 RTC driver

In reply to Sander Speetjens

Upstreaming the downstream Raspberry Pi 5 RTC driver. This driver supports the custom DA9091,
which is accessed through the firmware mailbox.

---
V3 -> V4:
- Fix Kconfig depends that disallows having Raspberrypi
firmware built as a module while the rtc driver is built in
- Remove sysfs
- Add limits from firmware properties and check in rpi_rtc_set_charge_voltage

V2 -> V3:
- Move platform check to firmware and abort before registering a platform device if on another platform
- Add a dependency on the Raspberry Pi firmware to Kconfig
- Check return value of devm_device_init_wakeup
- Fix property name

V1 -> V2:
Instead of the original driver, which was directly bound to the device tree. This driver is bound to the Raspberry Pi firmware device by creating a child device in the firmware driver probe function.
The child device is then bound to this driver,
which uses the firmware mailbox to access the RTC.

A couple of minor changes have been made to the driver since it was originally written, including:
- Checking if the model is a Raspberry Pi 5, as the RTC is only present on that model.
- Using the new devm_rpi_firmware_get() and devm_init_wakeup() helper to get the firmware device and avoid leaking memory.
- Using millivolts instead of microvolts for the trickle charge voltage, to match the RTC standard.
- Instead of setting the trickle charge voltage to 0 to disable trickle charging, the property is now optional. If the property is not present, trickle charging is disabled, per the RTC standard.
- Renaming the driver dt match compatible to "raspberrypi,firmware-rtc" to match the other firmware bindings.
- Renaming the driver name to "raspberrypi-rtc" to match the other firmware drivers.

 drivers/firmware/raspberrypi.c |  25 ++++
 drivers/rtc/Kconfig            |  12 ++
 drivers/rtc/Makefile           |   1 +
 drivers/rtc/rtc-raspberrypi.c  | 242 +++++++++++++++++++++++++++++++++
 4 files changed, 280 insertions(+)
 create mode 100644 drivers/rtc/rtc-raspberrypi.c
drivers/firmware/raspberrypi.c +25 -0
index 0aa322e9a2e7..a35f00c6fb2c 100644
@@ -24,6 +24,7 @@
 
 static struct platform_device *rpi_hwmon;
 static struct platform_device *rpi_clk;
+static struct platform_device *rpi_rtc;
 
 struct rpi_firmware {
 	struct mbox_client cl;
@@ -231,6 +232,27 @@ static void rpi_register_clk_driver(struct device *dev)
 						-1, NULL, 0);
 }
 
+static void rpi_register_rtc_driver(struct device *dev)
+{
+	struct device_node *firmware;
+
+	// Check if our model is a Raspberry Pi 5, as the RTC is only present on that model.
+	const char *model = of_get_property(of_root, "model", NULL);
+
+	if (!model || strncmp(model, "Raspberry Pi 5", 14) != 0)
+		return;
+
+	firmware = of_get_compatible_child(dev->of_node,
+					   "raspberrypi,firmware-rtc");
+	if (firmware) {
+		of_node_put(firmware);
+		return;
+	}
+
+	rpi_rtc = platform_device_register_data(dev, "raspberrypi-rtc",
+						-1, NULL, 0);
+}
+
 unsigned int rpi_firmware_clk_get_max_rate(struct rpi_firmware *fw, unsigned int id)
 {
 	struct rpi_firmware_clk_rate_request msg =
@@ -305,6 +327,7 @@ static int rpi_firmware_probe(struct platform_device *pdev)
 	rpi_firmware_print_firmware_revision(fw);
 	rpi_register_hwmon_driver(dev, fw);
 	rpi_register_clk_driver(dev);
+	rpi_register_rtc_driver(dev);
 
 	return 0;
 }
@@ -327,6 +350,8 @@ static void rpi_firmware_remove(struct platform_device *pdev)
 	rpi_hwmon = NULL;
 	platform_device_unregister(rpi_clk);
 	rpi_clk = NULL;
+	platform_device_unregister(rpi_rtc);
+	rpi_rtc = NULL;
 
 	rpi_firmware_put(fw);
 }
drivers/rtc/Kconfig +12 -0
index 05b9233b9418..88006f83d7bc 100644
@@ -1999,6 +1999,18 @@ config RTC_DRV_R7301
 	   This driver can also be built as a module. If so, the module
 	   will be called rtc-r7301.
 
+config RTC_DRV_RPI
+        tristate "Raspberry Pi RTC"
+	depends on RASPBERRYPI_FIRMWARE || (COMPILE_TEST && !RASPBERRYPI_FIRMWARE)
+        depends on ARCH_BRCMSTB || COMPILE_TEST
+        default ARCH_BRCMSTB
+        help
+          If you say yes here you get support for the RTC found on
+          Raspberry Pi devices.
+
+          This driver can also be built as a module. If so, the module
+          will be called rtc-raspberrypi.
+
 config RTC_DRV_STM32
 	tristate "STM32 RTC"
 	select REGMAP_MMIO
drivers/rtc/Makefile +1 -0
index 0347645b021f..46f1a0fc2416 100644
@@ -144,6 +144,7 @@ obj-$(CONFIG_RTC_DRV_PS3)	+= rtc-ps3.o
 obj-$(CONFIG_RTC_DRV_PXA)	+= rtc-pxa.o
 obj-$(CONFIG_RTC_DRV_R7301)	+= rtc-r7301.o
 obj-$(CONFIG_RTC_DRV_R9701)	+= rtc-r9701.o
+obj-$(CONFIG_RTC_DRV_RPI)	+= rtc-raspberrypi.o
 obj-$(CONFIG_RTC_DRV_RC5T583)	+= rtc-rc5t583.o
 obj-$(CONFIG_RTC_DRV_RC5T619)	+= rtc-rc5t619.o
 obj-$(CONFIG_RTC_DRV_RK808)	+= rtc-rk808.o
drivers/rtc/rtc-raspberrypi.c new file +244 -0

244 lines not on this page · raw message

Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH v4 2/2] rtc: Add Raspberry Pi 5 RTC driver

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] rtc: dt binding violation by reading property from parent node - [Low] firmware: global device…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] rtc: dt binding violation by reading property from parent node
- [Low] firmware: global device pointer race and leak on multiple probes
--

commit f13bdcb7308fe3b9ecc514f418f2d57fa4e0247f
Author: Sander Speetjens <[email protected]>
Subject: rtc: Add Raspberry Pi 5 RTC driver

This upstreaming patch introduces support for the downstream Raspberry Pi 5 RTC driver. The driver enables support for the custom DA9091 RTC, which is accessed through the firmware mailbox.

Quoted 4 lines
drivers/firmware/raspberrypi.c +0 -0
index 0aa322e9a2e73..a35f00c6fb2c1 100644

[ ... ]

Quoted 1 line
drivers/firmware/raspberrypi.c +0 -0
@@ -231,6 +232,27 @@ static void rpi_register_clk_driver(struct device *dev)

[ ... ]

Quoted 2 lines
drivers/firmware/raspberrypi.c +2 -0
+static void rpi_register_rtc_driver(struct device *dev)
+{

[ ... ]

drivers/firmware/raspberrypi.c static void rpi_register_clk_driver(struct device *dev) · line 254

Quoted 3 lines
drivers/firmware/raspberrypi.c +3 -0
+	rpi_rtc = platform_device_register_data(dev, "raspberrypi-rtc",
+						-1, NULL, 0);
+}

[Severity: Low]
Does locklessly overwriting the global rpi_rtc pointer here risk a resource leak? If multiple probes occur (for example, via a custom Device Tree overlay), it appears the initially registered platform device could be permanently leaked since the pointer is overwritten without synchronization.

[ ... ]

Quoted 1 line
drivers/firmware/raspberrypi.c +0 -0
@@ -327,6 +350,8 @@ static void rpi_firmware_remove(struct platform_device *pdev)

[ ... ]

drivers/firmware/raspberrypi.c static void rpi_firmware_remove(struct platform_device *pdev) · line 357

Quoted 7 lines
drivers/firmware/raspberrypi.c +2 -0
 	platform_device_unregister(rpi_clk);
 	rpi_clk = NULL;
+	platform_device_unregister(rpi_rtc);
+	rpi_rtc = NULL;
 
 	rpi_firmware_put(fw);
 }

[Severity: Low]
If a secondary probe occurred and overwrote the global pointer as noted above, could this cross-unregister the wrong device during removal in rpi_firmware_remove(), leading to a double-unregister of NULL and a leaked device?

Quoted 5 lines
drivers/rtc/rtc-raspberrypi.c new file +0 -0
new file mode 100644
index 0000000000000..494b949374064

[ ... ]

Quoted 2 lines
drivers/rtc/rtc-raspberrypi.c new file +2 -0
+static int rpi_rtc_probe(struct platform_device *pdev)
+{

[ ... ]

drivers/rtc/rtc-raspberrypi.c line 217

Quoted 5 lines
drivers/rtc/rtc-raspberrypi.c new file +5 -0
+	rpi_rtc_alarm_clear_pending(dev);
+
+	vrtc->bbat_vchg_millivolts = 0;
+	of_property_read_u32(dev->parent->of_node, "trickle-voltage-millivolt",
+			     &vrtc->bbat_vchg_millivolts);

[Severity: Medium]
Is it intentional to unconditionally read the trickle-voltage-millivolt property from the parent MFD firmware node instead of the driver's own RTC subnode?

If this property is placed correctly in the raspberrypi,firmware-rtc subnode, it seems it would be silently ignored, causing trickle charging to be disabled and breaking standard Device Tree encapsulation.

Quoted 6 lines
drivers/rtc/rtc-raspberrypi.c new file +6 -0
+
+	rpi_rtc_set_limits(dev);
+	rpi_rtc_set_charge_voltage(dev);
+
+	return devm_rtc_register_device(vrtc->rtc);
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2