Skip to contentSkip to conversation

Raspberry Pi 5 RTC driver

PATCH Superseded Sander Speetjens 2 participants 5 messages Sep 17

Subsystems (4)

v2 of 14 Superseded Latest is v14 →

lore
Share & tools
lore
#0 lore raw

v2 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 |   7 +
 drivers/firmware/raspberrypi.c                |  19 ++
 drivers/rtc/Kconfig                           |  11 +
 drivers/rtc/Makefile                          |   1 +
 drivers/rtc/rtc-raspberrypi.c                 | 277 ++++++++++++++++++
 5 files changed, 315 insertions(+)
 create mode 100644 drivers/rtc/rtc-raspberrypi.c

base-commit: fd73f4a6659897191fa0d40695fe370925dd3780

Signature · 1 line
2.55.0
#1 lore raw

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

Add charging-voltage-millivolt property for the Raspberry Pi 5 RTC.

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     | 7 +++++++
 1 file changed, 7 insertions(+)
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml +7 -0
index 983ea80eaec9..1a0432510bee 100644
@@ -133,6 +133,11 @@ properties:
     required:
       - compatible
 
+  charging-voltage-millivolt:
+    $ref: /schemas/rtc/rtc.yaml#/properties/charging-voltage-millivolt
+    minimum: 1300
+    maximum: 5000
+
 required:
   - compatible
   - mboxes
@@ -171,5 +176,7 @@ examples:
             touchscreen-size-x = <800>;
             touchscreen-size-y = <480>;
         };
+
+        trickle-charge-millivolt = <3300>;
     };
 ...
Signature · 1 line
2.55.0
#2 lore raw

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

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] dt-bindings: invalid $ref to non-existent property charging-voltage-millivolt - [Medium]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] dt-bindings: invalid $ref to non-existent property charging-voltage-millivolt
- [Medium] dt-bindings: undeclared property trickle-charge-millivolt in example --

commit 387f082415e97ddf81bc4779459443584085928f
Author: Sander Speetjens <[email protected]>

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

This commit adds a new property to configure the charging voltage for the Raspberry Pi 5 RTC within the bcm2835-firmware binding.

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

Quoted 10 lines
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml +2 -0
index 983ea80eaec97..1a0432510beeb 100644
@@ -133,6 +133,11 @@ properties:
     required:
       - compatible
 
+  charging-voltage-millivolt:
+    $ref: /schemas/rtc/rtc.yaml#/properties/charging-voltage-millivolt

[Severity: Medium]
Does the standard rtc.yaml define charging-voltage-millivolt?

Looking at the standard RTC binding (rtc.yaml), it does not define this property. The correct standard name appears to be trickle-voltage-millivolt. This invalid schema reference will cause make dt_binding_check to fail to resolve the path and import constraints, breaking schema validation.

Quoted 5 lines
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml +3 -0
+    minimum: 1300
+    maximum: 5000
+
 required:
   - compatible

[ ... ]

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

Quoted 6 lines
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml +2 -0
@@ -171,5 +176,7 @@ examples:
             touchscreen-size-x = <800>;
             touchscreen-size-y = <480>;
         };
+
+        trickle-charge-millivolt = <3300>;

[Severity: Medium]
Should this example property name match the one declared above?

The patch defines charging-voltage-millivolt in the properties block, but the example uses trickle-charge-millivolt. Because additional properties are restricted by the schema rules, the use of an undeclared property in the example will trigger a strict validation failure during make dt_binding_check.

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

PATCH v2 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.

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 |  19 +++
 drivers/rtc/Kconfig            |  11 ++
 drivers/rtc/Makefile           |   1 +
 drivers/rtc/rtc-raspberrypi.c  | 277 +++++++++++++++++++++++++++++++++
 4 files changed, 308 insertions(+)
 create mode 100644 drivers/rtc/rtc-raspberrypi.c
drivers/firmware/raspberrypi.c +19 -0
index 0aa322e9a2e7..b284c689f782 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,21 @@ 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;
+
+	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 +321,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 +344,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 +11 -0
index 05b9233b9418..382973d46e06 100644
@@ -1999,6 +1999,17 @@ 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 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-rpi.
+
 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 +277 -0

277 lines not on this page · raw message

Signature · 1 line
2.55.0
#4 lore raw

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

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Medium] rtc: unconditional dev_err on unsupported platforms - [Medium] rtc: dt property name mismatch…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Medium] rtc: unconditional dev_err on unsupported platforms
- [Medium] rtc: dt property name mismatch with schema and standard
- [Medium] rtc: ignored devm_device_init_wakeup return value
- [Low] kconfig: missing dependency on RASPBERRYPI_FIRMWARE
--

commit 63b49baa5affb33b58f7190d1f915c0156fa6365
Author: Sander Speetjens <[email protected]>

rtc: Add Raspberry Pi 5 RTC driver

This commit upstream supports the Raspberry Pi 5 RTC by interacting with the custom DA9091 over the firmware mailbox. It binds as a child of the firmware device and configures trickle charging based on device tree settings.

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

[ ... ]

Quoted 7 lines
drivers/firmware/raspberrypi.c +3 -0
@@ -231,6 +232,21 @@ 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;

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

Quoted 10 lines
drivers/firmware/raspberrypi.c +10 -0
+
+	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);

[Severity: Medium]
Will this unconditionally register the rtc platform device on legacy Raspberry Pi models?

If rpi_rtc is registered on all models, the probe function will run universally. When rpi_rtc_probe() executes on a model other than the Raspberry Pi 5, it prints an error message via dev_err() and aborts:

    if (!model || strncmp(model, "Raspberry Pi 5", 14) != 0) {
            dev_err(dev, "RTC is only available on Raspberry Pi 5\n");
            return -ENODEV;
    }

This seems like it would produce a spurious error message in the kernel log on every boot for all older models where the hardware doesn't exist.

Quoted 4 lines
drivers/rtc/Kconfig +0 -0
index 05b9233b94189..382973d46e06d 100644

[ ... ]

drivers/rtc/Kconfig config RTC_DRV_R7301 · line 2005

Quoted 8 lines
drivers/rtc/Kconfig +4 -0
@@ -1999,6 +1999,17 @@ 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 ARCH_BRCMSTB || COMPILE_TEST
+        default ARCH_BRCMSTB

[Severity: Low]
Does this configuration allow RTC_DRV_RPI to be built-in when RASPBERRYPI_FIRMWARE is built as a module?

The driver calls devm_rpi_firmware_get() and rpi_firmware_property(), which are exported by the raspberrypi-firmware module. If the RTC driver is built-in and the firmware driver is a module, this will result in an undefined reference linker error. Should there be a dependency on RASPBERRYPI_FIRMWARE?

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

[ ... ]

Quoted 6 lines
drivers/rtc/rtc-raspberrypi.c new file +6 -0
+static int rpi_rtc_probe(struct platform_device *pdev)
+{
+	struct rpi_rtc_data *vrtc;
+	struct device *dev = &pdev->dev;
+	struct rpi_firmware *firmware;
+	int ret;

[ ... ]

drivers/rtc/rtc-raspberrypi.c line 231

Quoted 7 lines
drivers/rtc/rtc-raspberrypi.c new file +7 -0
+	vrtc = devm_kzalloc(dev, sizeof(*vrtc), GFP_KERNEL);
+	if (!vrtc)
+		return -ENOMEM;
+
+	vrtc->fw = firmware;
+
+	devm_device_init_wakeup(dev);

[Severity: Medium]
Should the return value of devm_device_init_wakeup() be checked here?

This function registers a devres action and can fail, for instance, due to an out of memory condition. By ignoring the integer error code, the driver might continue with an uninitialized or broken wakeup state on failure.

Quoted 2 lines
drivers/rtc/rtc-raspberrypi.c new file +2 -0
+
+	platform_set_drvdata(pdev, vrtc);

[ ... ]

drivers/rtc/rtc-raspberrypi.c line 253

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-charge-millivolt",
+			&vrtc->bbat_vchg_millivolts);

[Severity: Medium]
Does this device tree property name match the binding schemas?

The standard RTC bindings typically use trickle-voltage-millivolt, and the driver's own schema might define charging-voltage-millivolt, but this attempts to read trickle-charge-millivolt. If a device tree follows the standard schema, this property lookup will fail and leave trickle charging silently disabled.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2