Skip to content

drivers: counter: add calibration API and Xilinx ZynqMP RTC driver#107684

Open
Harini-T wants to merge 3 commits intozephyrproject-rtos:mainfrom
Harini-T:counter_calibration_api
Open

drivers: counter: add calibration API and Xilinx ZynqMP RTC driver#107684
Harini-T wants to merge 3 commits intozephyrproject-rtos:mainfrom
Harini-T:counter_calibration_api

Conversation

@Harini-T
Copy link
Copy Markdown

@Harini-T Harini-T commented Apr 21, 2026

Summary

Add optional set_calibration / get_calibration operations to the counter driver API, enabling counter drivers to expose hardware clock calibration through a generic interface, and implement them in a new Xilinx ZynqMP RTC driver.

Context

The rtc_counter wrapper (zephyr,rtc-counter) bridges the counter API to the RTC API, allowing a hardware counter to be used as an RTC device. The RTC API already supports calibration via rtc_set_calibration() / rtc_get_calibration() (in ppb), but the counter API has no equivalent operations. As a result, the wrapper currently stubs out calibration with -ENOTSUP, and any hardware calibration capability is lost. This was identified during the Xilinx ZynqMP RTC upstreaming effort (#96720), where the maintainer directed that the hardware be implemented as a counter driver with the rtc_counter wrapper, and that calibration support be added to the counter API first:

"Implement calibration API for counters, then update wrapper."
@bjarki-andreasen

This PR adds the counter-side calibration API and includes the Xilinx ZynqMP RTC driver as the first in-tree implementation.

Commits

Commit 1: drivers: counter: add calibration API

  • Add counter_api_set_calibration / counter_api_get_calibration typedefs and fields to counter_driver_api (gated by
    CONFIG_COUNTER_CALIBRATION)
  • Add syscall functions and userspace verification handlers
  • Add CONFIG_COUNTER_CALIBRATION Kconfig option

Commit 2: dts: bindings: counter: add Xilinx ZynqMP RTC binding

  • New binding xlnx,zynqmp-rtc under dts/bindings/counter/
  • Describes reg, interrupts (alarm + sec), and clock-frequency

Commit 3: drivers: counter: add Xilinx ZynqMP RTC driver

  • New counter driver drivers/counter/counter_xlnx_zynqmp_rtc.c
  • Implements get/set_value, alarm, calibration (gated by CONFIG_COUNTER_CALIBRATION)
  • Always-on oscillator: start()/stop() return -ENOTSUP
  • Uses DEVICE_MMIO_NAMED for portable MMIO access
  • New Kconfig COUNTER_XLNX_ZYNQMP_RTC

Design Decisions

  • Unit: ppb (parts per billion) — matches the existing RTC calibration API, so the rtc_counter wrapper can be a trivial passthrough with zero unit conversion.
  • Optional — most counters don't have calibration hardware. The ops default to NULL and the syscalls return -ENOSYS.
  • Kconfig gatedCONFIG_COUNTER_CALIBRATION controls inclusion of the struct fields, matching the pattern used by CONFIG_RTC_CALIBRATION and CONFIG_COUNTER_64BITS_TICKS.

Testing

  • Build and run tests/drivers/counter/counter_basic_api/ on native_sim/native/64 with CONFIG_COUNTER_CALIBRATION=n (default) — verifies no regression to existing counter functionality.
  • Build and run the same test with CONFIG_COUNTER_CALIBRATION=y — verifies the new API compiles and existing tests pass with the extended struct.
  • Build tests/drivers/rtc/rtc_api/ on native_sim/native/64 — verifies rtc_counter.c (which includes counter.h) still compiles with the header changes.
  • checkpatch.pl and clang-format pass with no warnings.
  • tests/drivers/rtc/rtc_api/ on kv260_r5 hardware — 6/6 PASS (alarm, alarm callback, calibration, set/get time, time counting, Y2K)

Related

Copy link
Copy Markdown
Contributor

@bjarki-andreasen bjarki-andreasen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty spot on. Please implement this API in a driver or two as part of this PR to provide a good foundation for its inclusion.

@Harini-T
Copy link
Copy Markdown
Author

Looks pretty spot on. Please implement this API in a driver or two as part of this PR to provide a good foundation for its inclusion.

@bjarki-andreasen
The Xilinx ZynqMP RTC counter driver (PR: #107705 ) implements this calibration API — see xlnx_rtc_set_calibration() / xlnx_rtc_get_calibration() in drivers/counter/counter_xlnx_zynqmp_rtc.c. The rtc_counter wrapper passthrough is in
PR: #107697 .

Would referencing these stacked PRs work, or would you prefer the driver folded into this PR?

@nordic-krch
Copy link
Copy Markdown
Contributor

Doesn't it belong to clock_control API? Counter is just a client of a certain clock source and it seems to me that this is a calibration of that clock source.

@bjarki-andreasen
Copy link
Copy Markdown
Contributor

bjarki-andreasen commented Apr 22, 2026

Doesn't it belong to clock_control API? Counter is just a client of a certain clock source and it seems to me that this is a calibration of that clock source.

The calibration is not of the clock itself, its part of the RTC (a low power counter in this case) which applies calibration to its internal tick value. There is usually some hardware which can take the 1Hz output from the RTC and use that as an input, but the RTC is an actual counter with capture compare registers etc.

@bjarki-andreasen
Copy link
Copy Markdown
Contributor

Looks pretty spot on. Please implement this API in a driver or two as part of this PR to provide a good foundation for its inclusion.

@bjarki-andreasen The Xilinx ZynqMP RTC counter driver (PR: #107705 ) implements this calibration API — see xlnx_rtc_set_calibration() / xlnx_rtc_get_calibration() in drivers/counter/counter_xlnx_zynqmp_rtc.c. The rtc_counter wrapper passthrough is in PR: #107697 .

Would referencing these stacked PRs work, or would you prefer the driver folded into this PR?

If you could include this commit 34615be#diff-ebb7d630d71df3f66af55e47f382a70d29179d45fb00f33e10ba7c2626950306 in this PR, that would be really helpful, APIs require atleast one in tree user to be approved.

Harini T added 3 commits April 23, 2026 10:49
Add optional set_calibration/get_calibration operations to the counter
driver API. Calibration is specified in parts per billion (ppb). The
operations are gated behind CONFIG_COUNTER_CALIBRATION and return
-ENOSYS if not implemented by the driver.

Signed-off-by: Harini T <harini.t@amd.com>
Add devicetree binding for the Xilinx Zynq Ultrascale+ MPSoC Real Time
Clock (xlnx,zynqmp-rtc) as a counter device.
The binding describes the RTC hardware block which provides a seconds
counter with alarm and seconds-tick interrupts.

Signed-off-by: Harini T <harini.t@amd.com>
Add a counter driver for the Xilinx Zynq Ultrascale+ MPSoC Real Time
Clock. The hardware provides a 32-bit seconds counter driven by an
external crystal oscillator, one alarm channel, and a calibration
register with integer and fractional tick adjustment.

The driver implements the Zephyr counter API and is intended to be used
with the rtc_counter wrapper to expose the standard RTC API.

The RTC oscillator runs continuously so start() and stop() return
-ENOTSUP. Calibration support is gated by CONFIG_COUNTER_CALIBRATION.

Signed-off-by: Harini T <harini.t@amd.com>
@sonarqubecloud
Copy link
Copy Markdown

@Harini-T Harini-T changed the title drivers: counter: add calibration API drivers: counter: add calibration API and Xilinx ZynqMP RTC driver Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: Counter area: Devicetree Binding PR modifies or adds a Device Tree binding platform: Xilinx AMD Xilinx

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants