Skip to content

Commit 0654fb4

Browse files
lidt-coderdanieldegrasse
authored andcommitted
counter: cmsdk_apb_timer: Use clock freq from DT clocks
Previously, the CMSDK APB timer driver hardcoded the counter clock frequency to 24 MHz, which limits reuse across SoCs and boards with different timer clock sources. This patch replaces the hardcoded frequency with a value derived from the device tree's `clocks` phandle, using the `clock-frequency` property of the referenced clock controller node. If the property is missing, it falls back to a default 24 MHz and issues a build-time warning. Signed-off-by: Lidor T <lidor@exibit-iot.com>
1 parent de001f7 commit 0654fb4

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

drivers/counter/timer_tmr_cmsdk_apb.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@
1616

1717
#include "timer_cmsdk_apb.h"
1818

19+
#define TIMER_NODE DT_INST(0, arm_cmsdk_timer)
20+
#define CLOCK_NODE DT_PHANDLE(TIMER_NODE, clocks)
21+
22+
#define HAS_TIMER_CLOCK DT_NODE_HAS_PROP(TIMER_NODE, clocks)
23+
#define HAS_CLOCK_FREQUENCY DT_NODE_HAS_PROP(CLOCK_NODE, clock_frequency)
24+
25+
#if HAS_TIMER_CLOCK && HAS_CLOCK_FREQUENCY
26+
#define TIMER_CMSDK_FREQ(inst) \
27+
DT_INST_PROP_BY_PHANDLE(inst, clocks, clock_frequency)
28+
#else
29+
#define TIMER_CMSDK_FREQ(inst) \
30+
24000000U /* fallback default */
31+
#endif /* HAS_TIMER_CLOCK && HAS_CLOCK_FREQUENCY */
32+
1933
typedef void (*timer_config_func_t)(const struct device *dev);
2034

2135
struct tmr_cmsdk_apb_cfg {
@@ -172,7 +186,7 @@ static int tmr_cmsdk_apb_init(const struct device *dev)
172186
static const struct tmr_cmsdk_apb_cfg tmr_cmsdk_apb_cfg_##inst = { \
173187
.info = { \
174188
.max_top_value = UINT32_MAX, \
175-
.freq = 24000000U, \
189+
.freq = TIMER_CMSDK_FREQ(inst), \
176190
.flags = COUNTER_CONFIG_INFO_COUNT_UP, \
177191
.channels = 0U, \
178192
}, \

0 commit comments

Comments
 (0)