Skip to content

Commit a58588e

Browse files
adamkondraciukkartben
authored andcommitted
drivers: timer: nrf_grtc: Add support for clock outputs
Add support for GRTC clock output pins. Signed-off-by: Adam Kondraciuk <[email protected]>
1 parent 865b245 commit a58588e

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

drivers/timer/nrf_grtc_timer.c

Lines changed: 32 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,35 @@ 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_hz)
530+
#if !DT_NODE_HAS_PROP(HFCLK_NODE, clock_frequency)
531+
#error "hfclock reference required when fast clock output is enabled."
532+
#endif
533+
534+
#if DT_PROP(GRTC_NODE, clkout_fast_frequency_hz) > (DT_PROP(HFCLK_NODE, clock_frequency) / 2)
535+
#error "Invalid frequency value for fast clock output."
536+
#endif
537+
uint32_t base_frequency = DT_PROP(HFCLK_NODE, clock_frequency);
538+
uint32_t requested_frequency = DT_PROP(GRTC_NODE, clkout_fast_frequency_hz);
539+
uint32_t grtc_div = base_frequency / (requested_frequency * 2);
540+
541+
nrfy_grtc_clkout_divider_set(NRF_GRTC, (uint8_t)grtc_div);
542+
nrfy_grtc_clkout_set(NRF_GRTC, NRF_GRTC_CLKOUT_FAST, true);
543+
#endif
544+
545+
#if DT_PROP(GRTC_NODE, clkout_32k) || DT_NODE_HAS_PROP(GRTC_NODE, clkout_fast_frequency_hz)
546+
PINCTRL_DT_DEFINE(GRTC_NODE);
547+
const struct pinctrl_dev_config *pcfg = PINCTRL_DT_DEV_CONFIG_GET(GRTC_NODE);
548+
549+
return pinctrl_apply_state(pcfg, PINCTRL_STATE_DEFAULT);
550+
#else
521551
return 0;
552+
#endif
522553
}
523554

524555
void sys_clock_set_timeout(int32_t ticks, bool idle)

0 commit comments

Comments
 (0)