@@ -18,14 +18,23 @@ LOG_MODULE_REGISTER(dmic_nrfx_pdm, CONFIG_AUDIO_DMIC_LOG_LEVEL);
18
18
#if CONFIG_SOC_SERIES_NRF54HX
19
19
#define DMIC_NRFX_CLOCK_FREQ MHZ(16)
20
20
#define DMIC_NRFX_CLOCK_FACTOR 8192
21
+ #define DMIC_NRFX_AUDIO_CLOCK_FREQ DT_PROP_OR(DT_NODELABEL(audiopll), frequency, 0)
21
22
#else
22
23
#define DMIC_NRFX_CLOCK_FREQ MHZ(32)
23
24
#define DMIC_NRFX_CLOCK_FACTOR 4096
25
+ #define DMIC_NRFX_AUDIO_CLOCK_FREQ \
26
+ COND_CODE_1(CONFIG_SOC_NRF54L20, \
27
+ (DT_PROP_OR(DT_NODELABEL(aclk), clock_frequency, 0)), \
28
+ (DT_PROP_OR(DT_NODELABEL(clock), hfclkaudio_frequency, 0)))
24
29
#endif
25
30
26
31
struct dmic_nrfx_pdm_drv_data {
27
32
const nrfx_pdm_t * pdm ;
33
+ #if CONFIG_CLOCK_CONTROL_NRF
28
34
struct onoff_manager * clk_mgr ;
35
+ #elif CONFIG_CLOCK_CONTROL_NRF2_AUDIOPLL
36
+ const struct device * audiopll_dev ;
37
+ #endif
29
38
struct onoff_client clk_cli ;
30
39
struct k_mem_slab * mem_slab ;
31
40
uint32_t block_size ;
@@ -61,6 +70,35 @@ static void stop_pdm(struct dmic_nrfx_pdm_drv_data *drv_data)
61
70
nrfx_pdm_stop (drv_data -> pdm );
62
71
}
63
72
73
+ static int request_clock (struct dmic_nrfx_pdm_drv_data * drv_data )
74
+ {
75
+ if (!drv_data -> request_clock ) {
76
+ return 0 ;
77
+ }
78
+ #if CONFIG_CLOCK_CONTROL_NRF
79
+ return onoff_request (drv_data -> clk_mgr , & drv_data -> clk_cli );
80
+ #elif CONFIG_CLOCK_CONTROL_NRF2_AUDIOPLL
81
+ return nrf_clock_control_request (drv_data -> audiopll_dev , NULL , & drv_data -> clk_cli );
82
+ #else
83
+ return 0 ;
84
+ #endif
85
+ }
86
+
87
+ static int release_clock (struct dmic_nrfx_pdm_drv_data * drv_data )
88
+ {
89
+ if (!drv_data -> request_clock ) {
90
+ return 0 ;
91
+ }
92
+
93
+ #if CONFIG_CLOCK_CONTROL_NRF
94
+ return onoff_release (drv_data -> clk_mgr );
95
+ #elif CONFIG_CLOCK_CONTROL_NRF2_AUDIOPLL
96
+ return nrf_clock_control_release (drv_data -> audiopll_dev , NULL );
97
+ #else
98
+ return 0 ;
99
+ #endif
100
+ }
101
+
64
102
static void event_handler (const struct device * dev , const nrfx_pdm_evt_t * evt )
65
103
{
66
104
struct dmic_nrfx_pdm_drv_data * drv_data = dev -> data ;
@@ -119,8 +157,10 @@ static void event_handler(const struct device *dev, const nrfx_pdm_evt_t *evt)
119
157
120
158
if (drv_data -> active ) {
121
159
drv_data -> active = false;
122
- if (drv_data -> request_clock ) {
123
- (void )onoff_release (drv_data -> clk_mgr );
160
+ ret = release_clock (drv_data );
161
+ if (ret < 0 ) {
162
+ LOG_ERR ("Failed to release clock: %d" , ret );
163
+ return ;
124
164
}
125
165
}
126
166
} else if (evt -> buffer_released ) {
@@ -191,9 +231,11 @@ static bool check_pdm_frequencies(const struct dmic_nrfx_pdm_drv_cfg *drv_cfg,
191
231
{
192
232
uint32_t req_rate = pdm_cfg -> streams [0 ].pcm_rate ;
193
233
bool better_found = false;
194
-
234
+ const uint32_t src_freq =
235
+ (NRF_PDM_HAS_SELECTABLE_CLOCK && drv_cfg -> clk_src == ACLK )
236
+ ? DMIC_NRFX_AUDIO_CLOCK_FREQ
237
+ : DMIC_NRFX_CLOCK_FREQ ;
195
238
#if NRF_PDM_HAS_PRESCALER
196
- uint32_t src_freq = 32 * 1000 * 1000UL ;
197
239
uint32_t req_freq = req_rate * ratio ;
198
240
uint32_t prescaler = src_freq / req_freq ;
199
241
uint32_t act_freq = src_freq / prescaler ;
@@ -224,24 +266,6 @@ static bool check_pdm_frequencies(const struct dmic_nrfx_pdm_drv_cfg *drv_cfg,
224
266
}
225
267
#else
226
268
if (IS_ENABLED (CONFIG_SOC_SERIES_NRF53X ) || IS_ENABLED (CONFIG_SOC_SERIES_NRF54HX )) {
227
- const uint32_t src_freq =
228
- (NRF_PDM_HAS_MCLKCONFIG && drv_cfg -> clk_src == ACLK )
229
- /* The DMIC_NRFX_PDM_DEVICE() macro contains build
230
- * assertions that make sure that the ACLK clock
231
- * source is only used when it is available and only
232
- * with the "hfclkaudio-frequency" property defined,
233
- * but the default value of 0 here needs to be used
234
- * to prevent compilation errors when the property is
235
- * not defined (this expression will be eventually
236
- * optimized away then).
237
- */
238
- /* TODO : PS does not provide correct formula for nRF54H20 PDM_CLK.
239
- * Assume that master clock source frequency is 8 MHz. Remove once
240
- * correct formula is found.
241
- */
242
- ? DT_PROP_OR (DT_NODELABEL (clock ), hfclkaudio_frequency ,
243
- 0 )
244
- : DMIC_NRFX_CLOCK_FREQ ;
245
269
uint32_t req_freq = req_rate * ratio ;
246
270
/* As specified in the nRF5340 PS:
247
271
*
@@ -461,7 +485,7 @@ static int dmic_nrfx_pdm_configure(const struct device *dev,
461
485
nrfx_cfg .edge = NRF_PDM_EDGE_LEFTRISING ;
462
486
channel -> act_chan_map_lo = alt_map ;
463
487
}
464
- #if NRF_PDM_HAS_MCLKCONFIG
488
+ #if NRF_PDM_HAS_SELECTABLE_CLOCK
465
489
nrfx_cfg .mclksrc = drv_cfg -> clk_src == ACLK
466
490
? NRF_PDM_MCLKSRC_ACLK
467
491
: NRF_PDM_MCLKSRC_PCLK32M ;
@@ -508,8 +532,10 @@ static int start_transfer(struct dmic_nrfx_pdm_drv_data *drv_data)
508
532
LOG_ERR ("Failed to start PDM: 0x%08x" , err );
509
533
ret = - EIO ;
510
534
511
- if (drv_data -> request_clock ) {
512
- (void )onoff_release (drv_data -> clk_mgr );
535
+ ret = release_clock (drv_data );
536
+ if (ret < 0 ) {
537
+ LOG_ERR ("Failed to release clock: %d" , ret );
538
+ return ret ;
513
539
}
514
540
515
541
drv_data -> active = false;
@@ -529,7 +555,12 @@ static void clock_started_callback(struct onoff_manager *mgr,
529
555
* the actual transfer in such case.
530
556
*/
531
557
if (!drv_data -> active ) {
532
- (void )onoff_release (drv_data -> clk_mgr );
558
+ int ret = release_clock (drv_data );
559
+
560
+ if (ret < 0 ) {
561
+ LOG_ERR ("Failed to release clock: %d" , ret );
562
+ return ;
563
+ }
533
564
} else {
534
565
(void )start_transfer (drv_data );
535
566
}
@@ -548,7 +579,7 @@ static int trigger_start(const struct device *dev)
548
579
if (drv_data -> request_clock ) {
549
580
sys_notify_init_callback (& drv_data -> clk_cli .notify ,
550
581
clock_started_callback );
551
- ret = onoff_request (drv_data -> clk_mgr , & drv_data -> clk_cli );
582
+ ret = request_clock (drv_data );
552
583
if (ret < 0 ) {
553
584
drv_data -> active = false;
554
585
@@ -624,12 +655,11 @@ static int dmic_nrfx_pdm_read(const struct device *dev,
624
655
return ret ;
625
656
}
626
657
627
- #if CONFIG_CLOCK_CONTROL_NRF
628
658
static void init_clock_manager (const struct device * dev )
629
659
{
630
- struct dmic_nrfx_pdm_drv_data * drv_data = dev -> data ;
660
+ #if CONFIG_CLOCK_CONTROL_NRF
631
661
clock_control_subsys_t subsys ;
632
-
662
+ struct dmic_nrfx_pdm_drv_data * drv_data = dev -> data ;
633
663
#if NRF_CLOCK_HAS_HFCLKAUDIO
634
664
const struct dmic_nrfx_pdm_drv_cfg * drv_cfg = dev -> config ;
635
665
@@ -643,8 +673,12 @@ static void init_clock_manager(const struct device *dev)
643
673
644
674
drv_data -> clk_mgr = z_nrf_clock_control_get_onoff (subsys );
645
675
__ASSERT_NO_MSG (drv_data -> clk_mgr != NULL );
646
- }
676
+ #elif CONFIG_CLOCK_CONTROL_NRF2_AUDIOPLL
677
+ struct dmic_nrfx_pdm_drv_data * drv_data = dev -> data ;
678
+
679
+ drv_data -> audiopll_dev = DEVICE_DT_GET (DT_NODELABEL (audiopll ));
647
680
#endif
681
+ }
648
682
649
683
static const struct _dmic_ops dmic_ops = {
650
684
.configure = dmic_nrfx_pdm_configure ,
@@ -677,8 +711,7 @@ static const struct _dmic_ops dmic_ops = {
677
711
k_msgq_init(&dmic_nrfx_pdm_data##idx.mem_slab_queue, \
678
712
(char *)mem_slab_msgs##idx, sizeof(void *), \
679
713
ARRAY_SIZE(mem_slab_msgs##idx)); \
680
- IF_ENABLED(CONFIG_CLOCK_CONTROL_NRF, \
681
- (init_clock_manager(dev);)) \
714
+ init_clock_manager(dev); \
682
715
return 0; \
683
716
} \
684
717
static void event_handler##idx(const nrfx_pdm_evt_t *evt) \
@@ -695,13 +728,20 @@ static const struct _dmic_ops dmic_ops = {
695
728
.clk_src = PDM_CLK_SRC(idx), \
696
729
.mem_reg = DMM_DEV_TO_REG(PDM(idx)), \
697
730
}; \
698
- BUILD_ASSERT(PDM_CLK_SRC(idx) != ACLK || NRF_PDM_HAS_MCLKCONFIG, \
731
+ BUILD_ASSERT(PDM_CLK_SRC(idx) != ACLK || \
732
+ NRF_PDM_HAS_SELECTABLE_CLOCK, \
699
733
"Clock source ACLK is not available."); \
700
734
BUILD_ASSERT(PDM_CLK_SRC(idx) != ACLK || \
701
735
DT_NODE_HAS_PROP(DT_NODELABEL(clock), \
702
- hfclkaudio_frequency), \
736
+ hfclkaudio_frequency) || \
737
+ DT_NODE_HAS_PROP(DT_NODELABEL(aclk), \
738
+ clock_frequency) || \
739
+ DT_NODE_HAS_PROP(DT_NODELABEL(audiopll), \
740
+ frequency), \
703
741
"Clock source ACLK requires the hfclkaudio-frequency " \
704
- "property to be defined in the nordic,nrf-clock node."); \
742
+ "property to be defined in the nordic,nrf-clock node " \
743
+ "or clock-frequency property to be defined in aclk node" \
744
+ "or frequency property to be defined in audiopll node"); \
705
745
DEVICE_DT_DEFINE(PDM(idx), pdm_nrfx_init##idx, NULL, \
706
746
&dmic_nrfx_pdm_data##idx, &dmic_nrfx_pdm_cfg##idx, \
707
747
POST_KERNEL, CONFIG_AUDIO_DMIC_INIT_PRIORITY, \
0 commit comments