Skip to content

Commit

Permalink
drivers: timer: nrf_grtc: Add support for clock outputs
Browse files Browse the repository at this point in the history
Add support for GRTC clock output pins.

Signed-off-by: Adam Kondraciuk <[email protected]>
  • Loading branch information
adamkondraciuk committed Jan 31, 2025
1 parent 2a57efe commit 7108545
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion drivers/timer/nrf_grtc_timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
#if defined(CONFIG_CLOCK_CONTROL_NRF)
#include <zephyr/drivers/clock_control/nrf_clock_control.h>
#endif
#include <zephyr/drivers/pinctrl.h>
#include <zephyr/drivers/timer/system_timer.h>
#include <zephyr/drivers/timer/nrf_grtc_timer.h>
#include <nrfx_grtc.h>
#include <zephyr/sys/math_extras.h>

#define GRTC_NODE DT_NODELABEL(grtc)
#define HFCLK_NODE DT_PHANDLE_BY_NAME(GRTC_NODE, clocks, hfclock)
#define LFCLK_NODE DT_PHANDLE_BY_NAME(GRTC_NODE, clocks, lfclock)

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

#define MAX_CYCLES (MAX_TICKS * CYC_PER_TICK)

#define LFCLK_FREQUENCY_HZ 32768
#define LFCLK_FREQUENCY_HZ DT_PROP(LFCLK_NODE, clock_frequency)

#if defined(CONFIG_TEST)
const int32_t z_sys_timer_irq_for_test = DT_IRQN(GRTC_NODE);
Expand Down Expand Up @@ -518,7 +521,35 @@ static int sys_clock_driver_init(void)
#if defined(CONFIG_NRF_GRTC_ALWAYS_ON)
nrfx_grtc_active_request_set(true);
#endif

#if DT_PROP(GRTC_NODE, clkout_32k)
nrfy_grtc_clkout_set(NRF_GRTC, NRF_GRTC_CLKOUT_32K, true);
#endif

#if DT_NODE_HAS_PROP(GRTC_NODE, clkout_fast_frequency)
#if !DT_NODE_HAS_PROP(HFCLK_NODE, clock_frequency)
#error "hfclock reference required when fast clock output is enabled."
#endif

#if DT_PROP(GRTC_NODE, clkout_fast_frequency) > (DT_PROP(HFCLK_NODE, clock_frequency) / 2)
#error "Invalid frequency value for fast clock output."
#endif
uint32_t base_frequency = DT_PROP(HFCLK_NODE, clock_frequency);
uint32_t requested_frequency = DT_PROP(GRTC_NODE, clkout_fast_frequency);
uint32_t grtc_div = base_frequency / (requested_frequency * 2);

nrfy_grtc_clkout_divider_set(NRF_GRTC, (uint8_t)grtc_div);
nrfy_grtc_clkout_set(NRF_GRTC, NRF_GRTC_CLKOUT_FAST, true);
#endif

#if DT_PROP(GRTC_NODE, clkout_32k) || DT_NODE_HAS_PROP(GRTC_NODE, clkout_fast_frequency)
PINCTRL_DT_DEFINE(GRTC_NODE);
const struct pinctrl_dev_config *pcfg = PINCTRL_DT_DEV_CONFIG_GET(GRTC_NODE);

return pinctrl_apply_state(pcfg, PINCTRL_STATE_DEFAULT);
#else
return 0;
#endif
}

void sys_clock_set_timeout(int32_t ticks, bool idle)
Expand Down

0 comments on commit 7108545

Please sign in to comment.