1919#define HFCLOCK_TIMEOUT_MS 2
2020
2121static uint16_t const sca_ppm_lut [] = {500 , 250 , 150 , 100 , 75 , 50 , 30 , 20 };
22+ static atomic_val_t hf_refcnt ;
2223
2324#if defined(CONFIG_SOC_SERIES_NRF54HX )
2425#define CLOCK_CONTROL_NRF_K32SRC_ACCURACY 7U /* FIXME: */
@@ -63,9 +64,34 @@ int lll_clock_wait(void)
6364 return 0 ;
6465}
6566
67+ #if !defined(CONFIG_BT_CTLR_ZLI )
68+ const static struct device * clock_dev_hfxo = DEVICE_DT_GET (DT_NODELABEL (hfxo ));
69+ const static struct nrf_clock_spec clock_req_spec_hfxo = {
70+ .frequency = 0 , /* Ignore or use default */
71+ .accuracy = 1 , /* Use maximum accuracy */
72+ .precision = 1 , /* 0 for low precision, 1 for high precision */
73+ };
74+ static struct onoff_client clock_cli_hfxo ;
75+ #endif
76+
6677int lll_hfclock_on (void )
6778{
68- /* TODO: */
79+ if (atomic_inc (& hf_refcnt ) > 0 ) {
80+ return 0 ;
81+ }
82+
83+ #if defined(CONFIG_BT_CTLR_ZLI )
84+ nrf_clock_control_hfxo_request ();
85+ #else
86+ int err ;
87+
88+ sys_notify_init_spinwait (& clock_cli_hfxo .notify );
89+
90+ err = nrf_clock_control_request (clock_dev_hfxo , & clock_req_spec_hfxo , & clock_cli_hfxo );
91+ if (err ) {
92+ return err ;
93+ }
94+ #endif
6995
7096 return 0 ;
7197}
@@ -79,7 +105,24 @@ int lll_hfclock_on_wait(void)
79105
80106int lll_hfclock_off (void )
81107{
82- /* TODO: */
108+ if (atomic_get (& hf_refcnt ) < 1 ) {
109+ return - EALREADY ;
110+ }
111+
112+ if (atomic_dec (& hf_refcnt ) > 1 ) {
113+ return 0 ;
114+ }
115+
116+ #if defined(CONFIG_BT_CTLR_ZLI )
117+ nrf_clock_control_hfxo_release ();
118+ #else
119+ int err ;
120+
121+ err = nrf_clock_control_release (clock_dev_hfxo , & clock_req_spec_hfxo );
122+ if (err ) {
123+ return err ;
124+ }
125+ #endif
83126
84127 return 0 ;
85128}
@@ -89,9 +132,7 @@ struct lll_clock_state {
89132 struct onoff_client cli ;
90133 struct k_sem sem ;
91134};
92-
93135static struct onoff_client lf_cli ;
94- static atomic_val_t hf_refcnt ;
95136
96137static void clock_ready (struct onoff_manager * mgr , struct onoff_client * cli ,
97138 uint32_t state , int res )
0 commit comments