Skip to content

Commit ad78c01

Browse files
[nrf fromlist] drivers: timer: nrf_grtc: Add support for clock outputs
Add support for GRTC clock output pins. Upstream PR #: 82788 Signed-off-by: Adam Kondraciuk <[email protected]>
1 parent e4608ef commit ad78c01

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

drivers/timer/nrf_grtc_timer.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010
#if defined(CONFIG_CLOCK_CONTROL_NRF)
1111
#include <zephyr/drivers/clock_control/nrf_clock_control.h>
1212
#endif
13+
#include <zephyr/drivers/pinctrl.h>
1314
#include <zephyr/drivers/timer/system_timer.h>
1415
#include <zephyr/drivers/timer/nrf_grtc_timer.h>
1516
#include <nrfx_grtc.h>
1617
#include <zephyr/sys/math_extras.h>
1718

1819
#define GRTC_NODE DT_NODELABEL(grtc)
20+
#define HFCLK_NODE DT_PHANDLE_BY_NAME(GRTC_NODE, clocks, hfclock)
21+
#define LFCLK_NODE DT_PHANDLE_BY_NAME(GRTC_NODE, clocks, lfclock)
1922

2023
/* Ensure that GRTC properties in devicetree are defined correctly. */
2124
#if !DT_NODE_HAS_PROP(GRTC_NODE, owned_channels)
@@ -49,7 +52,7 @@
4952

5053
#define MAX_CYCLES (MAX_TICKS * CYC_PER_TICK)
5154

52-
#define LFCLK_FREQUENCY_HZ 32768
55+
#define LFCLK_FREQUENCY_HZ DT_PROP(LFCLK_NODE, clock_frequency)
5356

5457
#if defined(CONFIG_TEST)
5558
const int32_t z_sys_timer_irq_for_test = DT_IRQN(GRTC_NODE);
@@ -518,7 +521,32 @@ static int sys_clock_driver_init(void)
518521
#if defined(CONFIG_NRF_GRTC_ALWAYS_ON)
519522
nrfx_grtc_active_request_set(true);
520523
#endif
524+
525+
#if DT_PROP(GRTC_NODE, clkout_32k)
526+
nrfy_grtc_clkout_set(NRF_GRTC, NRF_GRTC_CLKOUT_32K, true);
527+
#endif
528+
529+
#if DT_NODE_HAS_PROP(GRTC_NODE, clkout_fast_frequency)
530+
#if DT_NODE_HAS_PROP(HFCLK_NODE, clock_frequency) && \
531+
DT_PROP(GRTC_NODE, clkout_fast_frequency) > (DT_PROP(HFCLK_NODE, clock_frequency) / 2)
532+
#error "Invalid frequency value for fast clock output."
533+
#endif
534+
uint32_t base_frequency = DT_PROP(HFCLK_NODE, clock_frequency);
535+
uint32_t requested_frequency = DT_PROP(GRTC_NODE, clkout_fast_frequency);
536+
uint32_t grtc_div = base_frequency / (requested_frequency * 2);
537+
538+
nrfy_grtc_clkout_set(NRF_GRTC, NRF_GRTC_CLKOUT_FAST, true);
539+
nrfy_grtc_clkout_divider_set(NRF_GRTC, (uint8_t)grtc_div);
540+
#endif
541+
542+
#if DT_PROP(GRTC_NODE, clkout_32k) || DT_NODE_HAS_PROP(GRTC_NODE, clkout_fast_frequency)
543+
PINCTRL_DT_DEFINE(GRTC_NODE);
544+
const struct pinctrl_dev_config *pcfg = PINCTRL_DT_DEV_CONFIG_GET(GRTC_NODE);
545+
546+
return pinctrl_apply_state(pcfg, PINCTRL_STATE_DEFAULT);
547+
#else
521548
return 0;
549+
#endif
522550
}
523551

524552
void sys_clock_set_timeout(int32_t ticks, bool idle)

0 commit comments

Comments
 (0)