Raspberry Pi 5 RTC driver
Subsystems (4)
v3 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 | 6 + drivers/firmware/raspberrypi.c | 25 ++ drivers/rtc/Kconfig | 11 + drivers/rtc/Makefile | 1 + drivers/rtc/rtc-raspberrypi.c | 272 ++++++++++++++++++ 5 files changed, 315 insertions(+) create mode 100644 drivers/rtc/rtc-raspberrypi.c
base-commit: fd73f4a6659897191fa0d40695fe370925dd3780
Signature · 1 line
2.55.0
PATCH v3 1/2 dt-bindings: rtc: Add property for Raspberry Pi 5 RTC
Add trickle-voltage-millivolt property for the Raspberry Pi 5 RTC.
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 | 6 ++++++ 1 file changed, 6 insertions(+)
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml +6 -0
@@ -133,6 +133,11 @@ properties: required: - compatible + trickle-voltage-millivolt: + $ref: /schemas/rtc/rtc.yaml#/properties/trickle-voltage-millivolt + minimum: 1300 + maximum: 5000 + required: - compatible - mboxes
@@ -144,6 +149,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
[email protected]
AI reviewRe: [PATCH v3 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: - [Medium] dt-bindings: constraint bypass due to missing scalar type on -millivolt property --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] dt-bindings: constraint bypass due to missing scalar type on -millivolt property --
commit a0ac03cb8b207820432d22540ca183fbaafbeb38
Author: Sander Speetjens <[email protected]>
dt-bindings: rtc: Add property for Raspberry Pi 5 RTC
This commit adds the trickle-voltage-millivolt property to the raspberrypi,bcm2835-firmware binding for the Raspberry Pi 5 RTC. It also introduces minimum and maximum voltage constraints for this property.
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml properties: · line 139
Quoted 12 lines
Documentation/devicetree/bindings/arm/bcm/raspberrypi,bcm2835-firmware.yaml +4
-0@@ -133,6 +133,11 @@ properties: required: - compatible + trickle-voltage-millivolt: + $ref: /schemas/rtc/rtc.yaml#/properties/trickle-voltage-millivolt + minimum: 1300 + maximum: 5000
[Severity: Medium]
Are these constraints applied effectively here?
Since dtschema's property-units.yaml assigns a default array type to properties ending in -millivolt, applying minimum and maximum constraints here without explicitly redefining the property as a scalar type (e.g., via $ref: /schemas/types.yaml#/definitions/uint32) can cause JSON Schema to silently bypass these constraints.
Could device trees with out-of-bounds voltages pass validation when dt_binding_check evaluates the constraints against the default array type?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v3 1/2] dt-bindings: rtc: Add property for Raspberry Pi 5 RTC
In reply to Sander SpeetjensQuoted 1 line · Sander Speetjens
Add trickle-voltage-millivolt property for the Raspberry Pi 5 RTC.
Why? Describe the hardware, the need, not the diff.
Quoted 2 lines
V2 -> V3: Fixed the firmware property naming
Changelog goes to the changelog area (under ---). Please look at other patches how they do it if you are uncertain.
Best regards,
Krzysztof
Re: [PATCH v3 1/2] dt-bindings: rtc: Add property for Raspberry Pi 5 RTC
Am 18.09.26 um 13:44 schrieb Krzysztof Kozlowski:
Quoted 3 lines · Sander Speetjens
Quoted 1 line
Add trickle-voltage-millivolt property for the Raspberry Pi 5 RTC.
Why? Describe the hardware, the need, not the diff.
Is this something we can avoid by making this configurable via userspace / hw_clock --param-set=parameter=value ?
Re: [PATCH v3 1/2] dt-bindings: rtc: Add property for Raspberry Pi 5 RTC
Quoted 6 lines · Stefan Wahren
Quoted 5 lines
Am 18.09.26 um 13:44 schrieb Krzysztof Kozlowski:
Quoted 3 lines · Sander Speetjens
Quoted 1 line
Add trickle-voltage-millivolt property for the Raspberry Pi 5 RTC.
Why? Describe the hardware, the need, not the diff.
Is this something we can avoid by making this configurable via userspace /
hw_clock--param-set=parameter=value ?
I think it would be handy to have both.
A cm5 board designer could choose to have a super-cap or lithium ion cell and for that a device-tree property would be handy for it is always the same. But for a user attachable battery on the pi 5b via headers or soldering the userspace version would be easier.
Alexandre Belloni
maintainerRe: [PATCH v3 1/2] dt-bindings: rtc: Add property for Raspberry Pi 5 RTC
In reply to Stefan WahrenQuoted 6 lines · Stefan Wahren
Am 18.09.26 um 13:44 schrieb Krzysztof Kozlowski:
Quoted 3 lines · Sander Speetjens
Quoted 1 line
Add trickle-voltage-millivolt property for the Raspberry Pi 5 RTC.
Why? Describe the hardware, the need, not the diff.
Is this something we can avoid by making this configurable via userspace /
hw_clock--param-set=parameter=value ?
I don't think so, the battery, once select is unlikely to change and so having it is the device tree is describing the HW properly.
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
PATCH v3 2/2 rtc: Add Raspberry Pi 5 RTC driver
In reply to Sander SpeetjensUpstreaming the downstream Raspberry Pi 5 RTC driver.
This driver supports the custom DA9091, which is accessed through the firmware mailbox.
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 | 11 ++ drivers/rtc/Makefile | 1 + drivers/rtc/rtc-raspberrypi.c | 272 +++++++++++++++++++++++++++++++++ 4 files changed, 309 insertions(+) create mode 100644 drivers/rtc/rtc-raspberrypi.c
drivers/firmware/raspberrypi.c +25 -0
@@ -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 +11 -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 || RASPBERRYPI_FIRMWARE || 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
@@ -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 +272 -0
272 lines not on this page · raw message
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH v3 2/2] rtc: Add Raspberry Pi 5 RTC driver
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Endianness mismatch in Raspberry Pi firmware mailbox payloads - [Low] Kconfig dependency allows…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Endianness mismatch in Raspberry Pi firmware mailbox payloads
- [Low] Kconfig dependency allows built-in RTC driver with modular firmware --
commit b1ad8443ceac920191fe490d4fe9a93bc6d9205a
Author: Sander Speetjens <[email protected]>
rtc: Add Raspberry Pi 5 RTC driver
This commit upstreams the downstream Raspberry Pi 5 RTC driver for the custom DA9091. The driver binds as a child device in the firmware driver probe function and uses the firmware mailbox to access the RTC.
[ ... ]
drivers/rtc/Kconfig config RTC_DRV_R7301 · line 2004
Quoted 11 lines
drivers/rtc/Kconfig +3
-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 || RASPBERRYPI_FIRMWARE || COMPILE_TEST
[Severity: Low]
Does this dependency allow the RTC driver to be compiled as built-in (=y) while RASPBERRYPI_FIRMWARE is compiled as a loadable module (=m)?
If this driver is built-in and the firmware is modular, it looks like the unconditional calls to devm_rpi_firmware_get() and rpi_firmware_property() in rpi_rtc_probe() will result in a linker error.
Quoted 4 lines
drivers/rtc/Kconfig +4
-0+ default ARCH_BRCMSTB + help + If you say yes here you get support for the RTC found on + Raspberry Pi devices.
[ ... ]
Quoted 5 lines
drivers/rtc/rtc-raspberrypi.c new file +0
-0
[ ... ]
drivers/rtc/rtc-raspberrypi.c line 54
Quoted 4 lines
drivers/rtc/rtc-raspberrypi.c new file +4
-0+static int rpi_rtc_set_time (struct device *dev,struct rtc_time *tm) +{ +struct rpi_rtc_data *vrtc =dev_get_drvdata (dev); +u32 data[2 ] = {RTC_TIME ,rtc_tm_to_time64 (tm)};
[Severity: High]
Can this payload format cause communication issues on big-endian architectures?
The Raspberry Pi firmware mailbox expects little-endian data. By passing a host-endian u32 array directly to rpi_firmware_property(), both the register ID (RTC_TIME) and the timestamp payload will be byte-swapped on big-endian machines.
Quoted 4 lines
drivers/rtc/rtc-raspberrypi.c new file +4
-0+ +return rpi_firmware_property (vrtc->fw,RPI_FIRMWARE_SET_RTC_REG , + &data,sizeof (data)); +}
[ ... ]
drivers/rtc/rtc-raspberrypi.c line 131
Quoted 4 lines
drivers/rtc/rtc-raspberrypi.c new file +4
-0+static int rpi_rtc_set_charge_voltage (struct device *dev) +{ +struct rpi_rtc_data *vrtc =dev_get_drvdata (dev); +u32 data[2 ] = {RTC_BBAT_CHG_VOLTS , vrtc->bbat_vchg_millivolts *1000U };
[Severity: High]
Will this conversion to a host-endian integer cause out-of-spec PMIC voltages to be sent on big-endian machines?
Similar to rpi_rtc_set_time(), passing host-endian values to the mailbox here will byte-swap the charge voltage target on big-endian systems, potentially resulting in hardware misconfiguration.
Quoted 4 lines
drivers/rtc/rtc-raspberrypi.c new file +4
-0+int err; + + err =rpi_firmware_property (vrtc->fw,RPI_FIRMWARE_SET_RTC_REG , + &data,sizeof (data));
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v3 2/2] rtc: Add Raspberry Pi 5 RTC driver
In reply to Sander Speetjensdrivers/rtc/rtc-raspberrypi.c line 256
Quoted 5 lines · Sander Speetjens
drivers/rtc/rtc-raspberrypi.c new file +5
-0+return devm_rtc_register_device (vrtc->rtc); +} + +static const struct of_device_id rpi_rtc_dt_match[] = { + { .compatible ="raspberrypi,firmware-rtc" },
Please fixup white space issues, see existing drivers (missing space before }
drivers/rtc/rtc-raspberrypi.c line 257
Quoted 1 line
drivers/rtc/rtc-raspberrypi.c new file +1
-0+ {},
and unnecessary ,.
Just take existing latest code as starting point, otherwise you reintroduce discouraged patterns.
More important: undocumented ABI.
Please run scripts/checkpatch.pl on the patches and fix reported warnings. After that, run also 'scripts/checkpatch.pl --strict' on the patches and (probably) fix more warnings. Some warnings can be ignored, especially from --strict run, but the code here looks like it needs a fix. Feel free to get in touch if the warning is not clear.
Best regards,
Krzysztof
Alexandre Belloni
maintainerRe: [PATCH v3 2/2] rtc: Add Raspberry Pi 5 RTC driver
Quoted 10 lines · Krzysztof Kozlowski
drivers/rtc/rtc-raspberrypi.c line 256
Quoted 5 lines · Sander Speetjens
drivers/rtc/rtc-raspberrypi.c new file +5
-0+return devm_rtc_register_device (vrtc->rtc); +} + +static const struct of_device_id rpi_rtc_dt_match[] = { + { .compatible ="raspberrypi,firmware-rtc" },Please fixup white space issues, see existing drivers (missing space before }
Quoted 9 lines
drivers/rtc/rtc-raspberrypi.c line 257
Quoted 1 line
drivers/rtc/rtc-raspberrypi.c new file +1
-0+ {},and unnecessary ,.
Just take existing latest code as starting point, otherwise you reintroduce discouraged patterns.
More important: undocumented ABI.
This was my comment on the previous revision, I'm not sure why it was ignored.
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin Embedded Linux and Kernel engineering https://bootlin.com
Re: [PATCH v3 2/2] rtc: Add Raspberry Pi 5 RTC driver
Quoted 2 lines
This was my comment on the previous revision, I'm not sure why it was ignored.
Looks like that email got lost on my end, I found it in the archive.
Quoted 1 line
This undocumented sysfs interface has to be removed.
Just to make sure I understand correctly: does "This undocumented sysfs interface has to be removed" mean that
a) the attributes themselves are not wanted and should be dropped, or
b) they are acceptable but must be properly documented under Documentation/ABI/ before they can stay?
Kind regards
Sander Speetjens
Quoted 22 lines · Alexandre Belloni
Quoted 9 lines · Krzysztof Kozlowski
drivers/rtc/rtc-raspberrypi.c line 256
Quoted 5 lines · Sander Speetjens
drivers/rtc/rtc-raspberrypi.c new file +5
-0+return devm_rtc_register_device (vrtc->rtc); +} + +static const struct of_device_id rpi_rtc_dt_match[] = { + { .compatible ="raspberrypi,firmware-rtc" },Please fixup white space issues, see existing drivers (missing space before }
Quoted 8 lines
drivers/rtc/rtc-raspberrypi.c line 257
Quoted 1 line
drivers/rtc/rtc-raspberrypi.c new file +1
-0+ {},and unnecessary ,.
Just take existing latest code as starting point, otherwise you reintroduce discouraged patterns.
More important: undocumented ABI.
This was my comment on the previous revision, I'm not sure why it was ignored.
Alexandre Belloni
maintainerRe: [PATCH v3 2/2] rtc: Add Raspberry Pi 5 RTC driver
Quoted 11 lines · Sander Speetjens
Quoted 2 lines
This was my comment on the previous revision, I'm not sure why it was ignored.
Looks like that email got lost on my end, I found it in the archive.
Quoted 1 line
This undocumented sysfs interface has to be removed.
Just to make sure I understand correctly: does "This undocumented sysfs interface has to be removed" mean that
a) the attributes themselves are not wanted and should be dropped, or
b) they are acceptable but must be properly documented underDocumentation/ABI/before they can stay?
This won't get in unless the ABI is proven to be useful to multiple RTCs. As-is, this can go to debugfs if you think you really need it but I doubt this is going to be used ever.
Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin Embedded Linux and Kernel engineering https://bootlin.com