44 * SPDX-License-Identifier: Apache-2.0
55 */
66
7+ #define DT_DRV_COMPAT nordic_nrf_pdm
8+
79#include <zephyr/audio/dmic.h>
810#include <zephyr/drivers/clock_control/nrf_clock_control.h>
911#include <zephyr/drivers/pinctrl.h>
@@ -35,7 +37,7 @@ BUILD_ASSERT((DMIC_NRFX_AUDIO_CLOCK_FREQ == NRF_AUXPLL_FREQ_DIV_AUDIO_48K) ||
3537#endif
3638
3739struct dmic_nrfx_pdm_drv_data {
38- const nrfx_pdm_t * pdm ;
40+ nrfx_pdm_t pdm ;
3941#if CONFIG_CLOCK_CONTROL_NRFS_AUDIOPLL || DT_NODE_HAS_STATUS_OKAY (NODE_AUDIO_AUXPLL )
4042 const struct device * audiopll_dev ;
4143#elif CONFIG_CLOCK_CONTROL_NRF
@@ -73,7 +75,7 @@ static void free_buffer(struct dmic_nrfx_pdm_drv_data *drv_data, void *buffer)
7375static void stop_pdm (struct dmic_nrfx_pdm_drv_data * drv_data )
7476{
7577 drv_data -> stopping = true;
76- nrfx_pdm_stop (drv_data -> pdm );
78+ nrfx_pdm_stop (& drv_data -> pdm );
7779}
7880
7981static int request_clock (struct dmic_nrfx_pdm_drv_data * drv_data )
@@ -114,7 +116,7 @@ static void event_handler(const struct device *dev, const nrfx_pdm_evt_t *evt)
114116
115117 if (evt -> buffer_requested ) {
116118 void * buffer ;
117- nrfx_err_t err ;
119+ int err ;
118120
119121 ret = k_mem_slab_alloc (drv_data -> mem_slab , & mem_slab_buffer , K_NO_WAIT );
120122 if (ret < 0 ) {
@@ -136,9 +138,9 @@ static void event_handler(const struct device *dev, const nrfx_pdm_evt_t *evt)
136138 stop_pdm (drv_data );
137139 return ;
138140 }
139- err = nrfx_pdm_buffer_set (drv_data -> pdm , buffer , drv_data -> block_size / 2 );
140- if (err != NRFX_SUCCESS ) {
141- LOG_ERR ("Failed to set buffer: 0x%08x " , err );
141+ err = nrfx_pdm_buffer_set (& drv_data -> pdm , buffer , drv_data -> block_size / 2 );
142+ if (err != 0 ) {
143+ LOG_ERR ("Failed to set buffer: %d " , err );
142144 stop = true;
143145 }
144146 }
@@ -209,7 +211,7 @@ static int dmic_nrfx_pdm_configure(const struct device *dev,
209211 struct pcm_stream_cfg * stream = & config -> streams [0 ];
210212 uint32_t def_map , alt_map ;
211213 nrfx_pdm_config_t nrfx_cfg ;
212- nrfx_err_t err ;
214+ int err ;
213215
214216 if (drv_data -> active ) {
215217 LOG_ERR ("Cannot configure device while it is active" );
@@ -255,7 +257,7 @@ static int dmic_nrfx_pdm_configure(const struct device *dev,
255257 /* If either rate or width is 0, the stream is to be disabled. */
256258 if (stream -> pcm_rate == 0 || stream -> pcm_width == 0 ) {
257259 if (drv_data -> configured ) {
258- nrfx_pdm_uninit (drv_data -> pdm );
260+ nrfx_pdm_uninit (& drv_data -> pdm );
259261 drv_data -> configured = false;
260262 }
261263
@@ -292,19 +294,19 @@ static int dmic_nrfx_pdm_configure(const struct device *dev,
292294 .output_freq_max = config -> io .max_pdm_clk_freq
293295 };
294296
295- if (nrfx_pdm_prescalers_calc (& output_config , & nrfx_cfg .prescalers ) != NRFX_SUCCESS ) {
297+ if (nrfx_pdm_prescalers_calc (& output_config , & nrfx_cfg .prescalers ) != 0 ) {
296298 LOG_ERR ("Cannot find suitable PDM clock configuration." );
297299 return - EINVAL ;
298300 }
299301
300302 if (drv_data -> configured ) {
301- nrfx_pdm_uninit (drv_data -> pdm );
303+ nrfx_pdm_uninit (& drv_data -> pdm );
302304 drv_data -> configured = false;
303305 }
304306
305- err = nrfx_pdm_init (drv_data -> pdm , & nrfx_cfg , drv_cfg -> event_handler );
306- if (err != NRFX_SUCCESS ) {
307- LOG_ERR ("Failed to initialize PDM: 0x%08x " , err );
307+ err = nrfx_pdm_init (& drv_data -> pdm , & nrfx_cfg , drv_cfg -> event_handler );
308+ if (err != 0 ) {
309+ LOG_ERR ("Failed to initialize PDM: %d " , err );
308310 return - EIO ;
309311 }
310312
@@ -325,15 +327,15 @@ static int dmic_nrfx_pdm_configure(const struct device *dev,
325327
326328static int start_transfer (struct dmic_nrfx_pdm_drv_data * drv_data )
327329{
328- nrfx_err_t err ;
330+ int err ;
329331 int ret ;
330332
331- err = nrfx_pdm_start (drv_data -> pdm );
332- if (err == NRFX_SUCCESS ) {
333+ err = nrfx_pdm_start (& drv_data -> pdm );
334+ if (err == 0 ) {
333335 return 0 ;
334336 }
335337
336- LOG_ERR ("Failed to start PDM: 0x%08x " , err );
338+ LOG_ERR ("Failed to start PDM: %d " , err );
337339 ret = - EIO ;
338340
339341 ret = release_clock (drv_data );
@@ -410,7 +412,7 @@ static int dmic_nrfx_pdm_trigger(const struct device *dev,
410412 case DMIC_TRIGGER_STOP :
411413 if (drv_data -> active ) {
412414 drv_data -> stopping = true;
413- nrfx_pdm_stop (drv_data -> pdm );
415+ nrfx_pdm_stop (& drv_data -> pdm );
414416 }
415417 break ;
416418
@@ -493,80 +495,60 @@ static const struct _dmic_ops dmic_ops = {
493495 .read = dmic_nrfx_pdm_read ,
494496};
495497
496- #define PDM (idx ) DT_NODELABEL(pdm##idx)
497- #define PDM_CLK_SRC (idx ) DT_STRING_TOKEN(PDM(idx), clock_source)
498-
499- #define PDM_NRFX_DEVICE (idx ) \
500- static void *rx_msgs##idx[DT_PROP(PDM(idx), queue_size)]; \
501- static void *mem_slab_msgs##idx[DT_PROP(PDM(idx), queue_size)]; \
502- static struct dmic_nrfx_pdm_drv_data dmic_nrfx_pdm_data##idx; \
503- static const nrfx_pdm_t dmic_nrfx_pdm##idx = NRFX_PDM_INSTANCE(idx); \
504- static int pdm_nrfx_init##idx(const struct device *dev) \
505- { \
506- IRQ_CONNECT(DT_IRQN(PDM(idx)), DT_IRQ(PDM(idx), priority), \
507- nrfx_isr, nrfx_pdm_##idx##_irq_handler, 0); \
508- const struct dmic_nrfx_pdm_drv_cfg *drv_cfg = dev->config; \
509- int err = pinctrl_apply_state(drv_cfg->pcfg, \
510- PINCTRL_STATE_DEFAULT); \
511- if (err < 0) { \
512- return err; \
513- } \
514- dmic_nrfx_pdm_data##idx.pdm = &dmic_nrfx_pdm##idx; \
515- k_msgq_init(&dmic_nrfx_pdm_data##idx.rx_queue, \
516- (char *)rx_msgs##idx, sizeof(void *), \
517- ARRAY_SIZE(rx_msgs##idx)); \
518- k_msgq_init(&dmic_nrfx_pdm_data##idx.mem_slab_queue, \
519- (char *)mem_slab_msgs##idx, sizeof(void *), \
520- ARRAY_SIZE(mem_slab_msgs##idx)); \
521- init_clock_manager(dev); \
522- return 0; \
523- } \
524- static void event_handler##idx(const nrfx_pdm_evt_t *evt) \
525- { \
526- event_handler(DEVICE_DT_GET(PDM(idx)), evt); \
527- } \
528- PINCTRL_DT_DEFINE(PDM(idx)); \
529- static const struct dmic_nrfx_pdm_drv_cfg dmic_nrfx_pdm_cfg##idx = { \
530- .event_handler = event_handler##idx, \
531- .nrfx_def_cfg = NRFX_PDM_DEFAULT_CONFIG(0, 0), \
532- .nrfx_def_cfg.skip_gpio_cfg = true, \
533- .nrfx_def_cfg.skip_psel_cfg = true, \
534- .pcfg = PINCTRL_DT_DEV_CONFIG_GET(PDM(idx)), \
535- .clk_src = PDM_CLK_SRC(idx), \
536- .mem_reg = DMM_DEV_TO_REG(PDM(idx)), \
537- }; \
538- NRF_DT_CHECK_NODE_HAS_REQUIRED_MEMORY_REGIONS(PDM(idx)); \
539- BUILD_ASSERT(PDM_CLK_SRC(idx) != ACLK || \
540- NRF_PDM_HAS_SELECTABLE_CLOCK, \
541- "Clock source ACLK is not available."); \
542- BUILD_ASSERT(PDM_CLK_SRC(idx) != ACLK || \
543- DT_NODE_HAS_PROP(DT_NODELABEL(clock), \
544- hfclkaudio_frequency) || \
545- DT_NODE_HAS_PROP(DT_NODELABEL(aclk), \
546- clock_frequency) || \
547- DT_NODE_HAS_PROP(NODE_AUDIOPLL, \
548- frequency) || \
549- DT_NODE_HAS_PROP(NODE_AUDIO_AUXPLL, \
550- nordic_frequency), \
551- "Clock source ACLK requires one following defined frequency "\
552- "properties: " \
553- "hfclkaudio-frequency in the nordic,nrf-clock node, " \
554- "clock-frequency in the aclk node, " \
555- "frequency in the audiopll node, " \
556- "nordic-frequency in the audio_auxpll node"); \
557- DEVICE_DT_DEFINE(PDM(idx), pdm_nrfx_init##idx, NULL, \
558- &dmic_nrfx_pdm_data##idx, &dmic_nrfx_pdm_cfg##idx, \
559- POST_KERNEL, CONFIG_AUDIO_DMIC_INIT_PRIORITY, \
560- &dmic_ops);
561-
562- #ifdef CONFIG_HAS_HW_NRF_PDM0
563- PDM_NRFX_DEVICE (0 );
564- #endif
565-
566- #ifdef CONFIG_HAS_HW_NRF_PDM20
567- PDM_NRFX_DEVICE (20 );
568- #endif
569-
570- #ifdef CONFIG_HAS_HW_NRF_PDM21
571- PDM_NRFX_DEVICE (21 );
572- #endif
498+ #define PDM_CLK_SRC (inst ) DT_STRING_TOKEN(DT_DRV_INST(inst), clock_source)
499+
500+ #define PDM_NRFX_DEVICE (inst ) \
501+ static void *rx_msgs##inst[DT_INST_PROP(inst, queue_size)]; \
502+ static void *mem_slab_msgs##inst[DT_INST_PROP(inst, queue_size)]; \
503+ static struct dmic_nrfx_pdm_drv_data dmic_nrfx_pdm_data##inst = { \
504+ .pdm = NRFX_PDM_INSTANCE(DT_INST_REG_ADDR(inst)), \
505+ }; \
506+ static int pdm_nrfx_init##inst(const struct device *dev) \
507+ { \
508+ IRQ_CONNECT(DT_INST_IRQN(inst), DT_INST_IRQ(inst, priority), nrfx_pdm_irq_handler, \
509+ &dmic_nrfx_pdm_data##inst.pdm, 0); \
510+ const struct dmic_nrfx_pdm_drv_cfg *drv_cfg = dev->config; \
511+ int err = pinctrl_apply_state(drv_cfg->pcfg, PINCTRL_STATE_DEFAULT); \
512+ if (err < 0) { \
513+ return err; \
514+ } \
515+ k_msgq_init(&dmic_nrfx_pdm_data##inst.rx_queue, (char *)rx_msgs##inst, \
516+ sizeof(void *), ARRAY_SIZE(rx_msgs##inst)); \
517+ k_msgq_init(&dmic_nrfx_pdm_data##inst.mem_slab_queue, (char *)mem_slab_msgs##inst, \
518+ sizeof(void *), ARRAY_SIZE(mem_slab_msgs##inst)); \
519+ init_clock_manager(dev); \
520+ return 0; \
521+ } \
522+ static void event_handler##inst(const nrfx_pdm_evt_t *evt) \
523+ { \
524+ event_handler(DEVICE_DT_INST_GET(inst), evt); \
525+ } \
526+ PINCTRL_DT_INST_DEFINE(inst); \
527+ static const struct dmic_nrfx_pdm_drv_cfg dmic_nrfx_pdm_cfg##inst = { \
528+ .event_handler = event_handler##inst, \
529+ .nrfx_def_cfg = NRFX_PDM_DEFAULT_CONFIG(0, 0), \
530+ .nrfx_def_cfg.skip_gpio_cfg = true, \
531+ .nrfx_def_cfg.skip_psel_cfg = true, \
532+ .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \
533+ .clk_src = PDM_CLK_SRC(inst), \
534+ .mem_reg = DMM_DEV_TO_REG(DT_DRV_INST(inst)), \
535+ }; \
536+ NRF_DT_CHECK_NODE_HAS_REQUIRED_MEMORY_REGIONS(DT_DRV_INST(inst)); \
537+ BUILD_ASSERT(PDM_CLK_SRC(inst) != ACLK || NRF_PDM_HAS_SELECTABLE_CLOCK, \
538+ "Clock source ACLK is not available."); \
539+ BUILD_ASSERT(PDM_CLK_SRC(inst) != ACLK || \
540+ DT_NODE_HAS_PROP(DT_NODELABEL(clock), hfclkaudio_frequency) || \
541+ DT_NODE_HAS_PROP(DT_NODELABEL(aclk), clock_frequency) || \
542+ DT_NODE_HAS_PROP(NODE_AUDIOPLL, frequency) || \
543+ DT_NODE_HAS_PROP(NODE_AUDIO_AUXPLL, nordic_frequency), \
544+ "Clock source ACLK requires one following defined frequency " \
545+ "properties: " \
546+ "hfclkaudio-frequency in the nordic,nrf-clock node, " \
547+ "clock-frequency in the aclk node, " \
548+ "frequency in the audiopll node, " \
549+ "nordic-frequency in the audio_auxpll node"); \
550+ DEVICE_DT_INST_DEFINE(inst, pdm_nrfx_init##inst, NULL, &dmic_nrfx_pdm_data##inst, \
551+ &dmic_nrfx_pdm_cfg##inst, POST_KERNEL, \
552+ CONFIG_AUDIO_DMIC_INIT_PRIORITY, &dmic_ops);
553+
554+ DT_INST_FOREACH_STATUS_OKAY (PDM_NRFX_DEVICE )
0 commit comments