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>
@@ -37,7 +39,7 @@ BUILD_ASSERT((DMIC_NRFX_AUDIO_CLOCK_FREQ == NRF_AUXPLL_FREQ_DIV_AUDIO_48K) ||
3739#endif
3840
3941struct dmic_nrfx_pdm_drv_data {
40- const nrfx_pdm_t * pdm ;
42+ nrfx_pdm_t pdm ;
4143#if CONFIG_CLOCK_CONTROL_NRFS_AUDIOPLL || DT_NODE_HAS_STATUS_OKAY (NODE_AUDIO_AUXPLL )
4244 const struct device * audiopll_dev ;
4345#elif CONFIG_CLOCK_CONTROL_NRF
@@ -75,7 +77,7 @@ static void free_buffer(struct dmic_nrfx_pdm_drv_data *drv_data, void *buffer)
7577static void stop_pdm (struct dmic_nrfx_pdm_drv_data * drv_data )
7678{
7779 drv_data -> stopping = true;
78- nrfx_pdm_stop (drv_data -> pdm );
80+ nrfx_pdm_stop (& drv_data -> pdm );
7981}
8082
8183static int request_clock (struct dmic_nrfx_pdm_drv_data * drv_data )
@@ -116,7 +118,7 @@ static void event_handler(const struct device *dev, const nrfx_pdm_evt_t *evt)
116118
117119 if (evt -> buffer_requested ) {
118120 void * buffer ;
119- nrfx_err_t err ;
121+ int err ;
120122
121123 ret = k_mem_slab_alloc (drv_data -> mem_slab , & mem_slab_buffer , K_NO_WAIT );
122124 if (ret < 0 ) {
@@ -138,9 +140,9 @@ static void event_handler(const struct device *dev, const nrfx_pdm_evt_t *evt)
138140 stop_pdm (drv_data );
139141 return ;
140142 }
141- err = nrfx_pdm_buffer_set (drv_data -> pdm , buffer , drv_data -> block_size / 2 );
142- if (err != NRFX_SUCCESS ) {
143- LOG_ERR ("Failed to set buffer: 0x%08x " , err );
143+ err = nrfx_pdm_buffer_set (& drv_data -> pdm , buffer , drv_data -> block_size / 2 );
144+ if (err != 0 ) {
145+ LOG_ERR ("Failed to set buffer: %d " , err );
144146 stop = true;
145147 }
146148 }
@@ -425,7 +427,7 @@ static int dmic_nrfx_pdm_configure(const struct device *dev,
425427 struct pcm_stream_cfg * stream = & config -> streams [0 ];
426428 uint32_t def_map , alt_map ;
427429 nrfx_pdm_config_t nrfx_cfg ;
428- nrfx_err_t err ;
430+ int err ;
429431
430432 if (drv_data -> active ) {
431433 LOG_ERR ("Cannot configure device while it is active" );
@@ -471,7 +473,7 @@ static int dmic_nrfx_pdm_configure(const struct device *dev,
471473 /* If either rate or width is 0, the stream is to be disabled. */
472474 if (stream -> pcm_rate == 0 || stream -> pcm_width == 0 ) {
473475 if (drv_data -> configured ) {
474- nrfx_pdm_uninit (drv_data -> pdm );
476+ nrfx_pdm_uninit (& drv_data -> pdm );
475477 drv_data -> configured = false;
476478 }
477479
@@ -505,13 +507,13 @@ static int dmic_nrfx_pdm_configure(const struct device *dev,
505507 }
506508
507509 if (drv_data -> configured ) {
508- nrfx_pdm_uninit (drv_data -> pdm );
510+ nrfx_pdm_uninit (& drv_data -> pdm );
509511 drv_data -> configured = false;
510512 }
511513
512- err = nrfx_pdm_init (drv_data -> pdm , & nrfx_cfg , drv_cfg -> event_handler );
513- if (err != NRFX_SUCCESS ) {
514- LOG_ERR ("Failed to initialize PDM: 0x%08x " , err );
514+ err = nrfx_pdm_init (& drv_data -> pdm , & nrfx_cfg , drv_cfg -> event_handler );
515+ if (err != 0 ) {
516+ LOG_ERR ("Failed to initialize PDM: %d " , err );
515517 return - EIO ;
516518 }
517519
@@ -532,15 +534,15 @@ static int dmic_nrfx_pdm_configure(const struct device *dev,
532534
533535static int start_transfer (struct dmic_nrfx_pdm_drv_data * drv_data )
534536{
535- nrfx_err_t err ;
537+ int err ;
536538 int ret ;
537539
538- err = nrfx_pdm_start (drv_data -> pdm );
539- if (err == NRFX_SUCCESS ) {
540+ err = nrfx_pdm_start (& drv_data -> pdm );
541+ if (err == 0 ) {
540542 return 0 ;
541543 }
542544
543- LOG_ERR ("Failed to start PDM: 0x%08x " , err );
545+ LOG_ERR ("Failed to start PDM: %d " , err );
544546 ret = - EIO ;
545547
546548 ret = release_clock (drv_data );
@@ -617,7 +619,7 @@ static int dmic_nrfx_pdm_trigger(const struct device *dev,
617619 case DMIC_TRIGGER_STOP :
618620 if (drv_data -> active ) {
619621 drv_data -> stopping = true;
620- nrfx_pdm_stop (drv_data -> pdm );
622+ nrfx_pdm_stop (& drv_data -> pdm );
621623 }
622624 break ;
623625
@@ -700,80 +702,60 @@ static const struct _dmic_ops dmic_ops = {
700702 .read = dmic_nrfx_pdm_read ,
701703};
702704
703- #define PDM (idx ) DT_NODELABEL(pdm##idx)
704- #define PDM_CLK_SRC (idx ) DT_STRING_TOKEN(PDM(idx), clock_source)
705-
706- #define PDM_NRFX_DEVICE (idx ) \
707- static void *rx_msgs##idx[DT_PROP(PDM(idx), queue_size)]; \
708- static void *mem_slab_msgs##idx[DT_PROP(PDM(idx), queue_size)]; \
709- static struct dmic_nrfx_pdm_drv_data dmic_nrfx_pdm_data##idx; \
710- static const nrfx_pdm_t dmic_nrfx_pdm##idx = NRFX_PDM_INSTANCE(idx); \
711- static int pdm_nrfx_init##idx(const struct device *dev) \
712- { \
713- IRQ_CONNECT(DT_IRQN(PDM(idx)), DT_IRQ(PDM(idx), priority), \
714- nrfx_isr, nrfx_pdm_##idx##_irq_handler, 0); \
715- const struct dmic_nrfx_pdm_drv_cfg *drv_cfg = dev->config; \
716- int err = pinctrl_apply_state(drv_cfg->pcfg, \
717- PINCTRL_STATE_DEFAULT); \
718- if (err < 0) { \
719- return err; \
720- } \
721- dmic_nrfx_pdm_data##idx.pdm = &dmic_nrfx_pdm##idx; \
722- k_msgq_init(&dmic_nrfx_pdm_data##idx.rx_queue, \
723- (char *)rx_msgs##idx, sizeof(void *), \
724- ARRAY_SIZE(rx_msgs##idx)); \
725- k_msgq_init(&dmic_nrfx_pdm_data##idx.mem_slab_queue, \
726- (char *)mem_slab_msgs##idx, sizeof(void *), \
727- ARRAY_SIZE(mem_slab_msgs##idx)); \
728- init_clock_manager(dev); \
729- return 0; \
730- } \
731- static void event_handler##idx(const nrfx_pdm_evt_t *evt) \
732- { \
733- event_handler(DEVICE_DT_GET(PDM(idx)), evt); \
734- } \
735- PINCTRL_DT_DEFINE(PDM(idx)); \
736- static const struct dmic_nrfx_pdm_drv_cfg dmic_nrfx_pdm_cfg##idx = { \
737- .event_handler = event_handler##idx, \
738- .nrfx_def_cfg = NRFX_PDM_DEFAULT_CONFIG(0, 0), \
739- .nrfx_def_cfg.skip_gpio_cfg = true, \
740- .nrfx_def_cfg.skip_psel_cfg = true, \
741- .pcfg = PINCTRL_DT_DEV_CONFIG_GET(PDM(idx)), \
742- .clk_src = PDM_CLK_SRC(idx), \
743- .mem_reg = DMM_DEV_TO_REG(PDM(idx)), \
744- }; \
745- NRF_DT_CHECK_NODE_HAS_REQUIRED_MEMORY_REGIONS(PDM(idx)); \
746- BUILD_ASSERT(PDM_CLK_SRC(idx) != ACLK || \
747- NRF_PDM_HAS_SELECTABLE_CLOCK, \
748- "Clock source ACLK is not available."); \
749- BUILD_ASSERT(PDM_CLK_SRC(idx) != ACLK || \
750- DT_NODE_HAS_PROP(DT_NODELABEL(clock), \
751- hfclkaudio_frequency) || \
752- DT_NODE_HAS_PROP(DT_NODELABEL(aclk), \
753- clock_frequency) || \
754- DT_NODE_HAS_PROP(NODE_AUDIOPLL, \
755- frequency) || \
756- DT_NODE_HAS_PROP(NODE_AUDIO_AUXPLL, \
757- nordic_frequency), \
758- "Clock source ACLK requires one following defined frequency "\
759- "properties: " \
760- "hfclkaudio-frequency in the nordic,nrf-clock node, " \
761- "clock-frequency in the aclk node, " \
762- "frequency in the audiopll node, " \
763- "nordic-frequency in the audio_auxpll node"); \
764- DEVICE_DT_DEFINE(PDM(idx), pdm_nrfx_init##idx, NULL, \
765- &dmic_nrfx_pdm_data##idx, &dmic_nrfx_pdm_cfg##idx, \
766- POST_KERNEL, CONFIG_AUDIO_DMIC_INIT_PRIORITY, \
767- &dmic_ops);
768-
769- #ifdef CONFIG_HAS_HW_NRF_PDM0
770- PDM_NRFX_DEVICE (0 );
771- #endif
772-
773- #ifdef CONFIG_HAS_HW_NRF_PDM20
774- PDM_NRFX_DEVICE (20 );
775- #endif
776-
777- #ifdef CONFIG_HAS_HW_NRF_PDM21
778- PDM_NRFX_DEVICE (21 );
779- #endif
705+ #define PDM_CLK_SRC (inst ) DT_STRING_TOKEN(DT_DRV_INST(inst), clock_source)
706+
707+ #define PDM_NRFX_DEVICE (inst ) \
708+ static void *rx_msgs##inst[DT_INST_PROP(inst, queue_size)]; \
709+ static void *mem_slab_msgs##inst[DT_INST_PROP(inst, queue_size)]; \
710+ static struct dmic_nrfx_pdm_drv_data dmic_nrfx_pdm_data##inst = { \
711+ .pdm = NRFX_PDM_INSTANCE(DT_INST_REG_ADDR(inst)), \
712+ }; \
713+ static int pdm_nrfx_init##inst(const struct device *dev) \
714+ { \
715+ IRQ_CONNECT(DT_INST_IRQN(inst), DT_INST_IRQ(inst, priority), nrfx_pdm_irq_handler, \
716+ &dmic_nrfx_pdm_data##inst.pdm, 0); \
717+ const struct dmic_nrfx_pdm_drv_cfg *drv_cfg = dev->config; \
718+ int err = pinctrl_apply_state(drv_cfg->pcfg, PINCTRL_STATE_DEFAULT); \
719+ if (err < 0) { \
720+ return err; \
721+ } \
722+ k_msgq_init(&dmic_nrfx_pdm_data##inst.rx_queue, (char *)rx_msgs##inst, \
723+ sizeof(void *), ARRAY_SIZE(rx_msgs##inst)); \
724+ k_msgq_init(&dmic_nrfx_pdm_data##inst.mem_slab_queue, (char *)mem_slab_msgs##inst, \
725+ sizeof(void *), ARRAY_SIZE(mem_slab_msgs##inst)); \
726+ init_clock_manager(dev); \
727+ return 0; \
728+ } \
729+ static void event_handler##inst(const nrfx_pdm_evt_t *evt) \
730+ { \
731+ event_handler(DEVICE_DT_INST_GET(inst), evt); \
732+ } \
733+ PINCTRL_DT_INST_DEFINE(inst); \
734+ static const struct dmic_nrfx_pdm_drv_cfg dmic_nrfx_pdm_cfg##inst = { \
735+ .event_handler = event_handler##inst, \
736+ .nrfx_def_cfg = NRFX_PDM_DEFAULT_CONFIG(0, 0), \
737+ .nrfx_def_cfg.skip_gpio_cfg = true, \
738+ .nrfx_def_cfg.skip_psel_cfg = true, \
739+ .pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(inst), \
740+ .clk_src = PDM_CLK_SRC(inst), \
741+ .mem_reg = DMM_DEV_TO_REG(DT_DRV_INST(inst)), \
742+ }; \
743+ NRF_DT_CHECK_NODE_HAS_REQUIRED_MEMORY_REGIONS(DT_DRV_INST(inst)); \
744+ BUILD_ASSERT(PDM_CLK_SRC(inst) != ACLK || NRF_PDM_HAS_SELECTABLE_CLOCK, \
745+ "Clock source ACLK is not available."); \
746+ BUILD_ASSERT(PDM_CLK_SRC(inst) != ACLK || \
747+ DT_NODE_HAS_PROP(DT_NODELABEL(clock), hfclkaudio_frequency) || \
748+ DT_NODE_HAS_PROP(DT_NODELABEL(aclk), clock_frequency) || \
749+ DT_NODE_HAS_PROP(NODE_AUDIOPLL, frequency) || \
750+ DT_NODE_HAS_PROP(NODE_AUDIO_AUXPLL, nordic_frequency), \
751+ "Clock source ACLK requires one following defined frequency " \
752+ "properties: " \
753+ "hfclkaudio-frequency in the nordic,nrf-clock node, " \
754+ "clock-frequency in the aclk node, " \
755+ "frequency in the audiopll node, " \
756+ "nordic-frequency in the audio_auxpll node"); \
757+ DEVICE_DT_INST_DEFINE(inst, pdm_nrfx_init##inst, NULL, &dmic_nrfx_pdm_data##inst, \
758+ &dmic_nrfx_pdm_cfg##inst, POST_KERNEL, \
759+ CONFIG_AUDIO_DMIC_INIT_PRIORITY, &dmic_ops);
760+
761+ DT_INST_FOREACH_STATUS_OKAY (PDM_NRFX_DEVICE )
0 commit comments