1 // SPDX-License-Identifier: GPL-2.0-only
3 * omap-mcpdm.c -- OMAP ALSA SoC DAI driver using McPDM port
5 * Copyright (C) 2009 - 2011 Texas Instruments
7 * Author: Misael Lopez Cruz <misael.lopez@ti.com>
8 * Contact: Jorge Eduardo Candelaria <x0107209@ti.com>
9 * Margarita Olaya <magi.olaya@ti.com>
10 * Peter Ujfalusi <peter.ujfalusi@ti.com>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/interrupt.h>
17 #include <linux/err.h>
19 #include <linux/irq.h>
20 #include <linux/slab.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/of_device.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/dmaengine_pcm.h>
30 #include "omap-mcpdm.h"
33 struct mcpdm_link_config
{
34 u32 link_mask
; /* channel mask for the direction */
35 u32 threshold
; /* FIFO threshold */
40 unsigned long phys_base
;
41 void __iomem
*io_base
;
43 struct pm_qos_request pm_qos_req
;
48 /* Playback/Capture configuration */
49 struct mcpdm_link_config config
[2];
51 /* McPDM dn offsets for rx1, and 2 channels */
54 /* McPDM needs to be restarted due to runtime reconfiguration */
57 /* pm state for suspend/resume handling */
60 struct snd_dmaengine_dai_dma_data dma_data
[2];
64 * Stream DMA parameters
67 static inline void omap_mcpdm_write(struct omap_mcpdm
*mcpdm
, u16 reg
, u32 val
)
69 writel_relaxed(val
, mcpdm
->io_base
+ reg
);
72 static inline int omap_mcpdm_read(struct omap_mcpdm
*mcpdm
, u16 reg
)
74 return readl_relaxed(mcpdm
->io_base
+ reg
);
78 static void omap_mcpdm_reg_dump(struct omap_mcpdm
*mcpdm
)
80 dev_dbg(mcpdm
->dev
, "***********************\n");
81 dev_dbg(mcpdm
->dev
, "IRQSTATUS_RAW: 0x%04x\n",
82 omap_mcpdm_read(mcpdm
, MCPDM_REG_IRQSTATUS_RAW
));
83 dev_dbg(mcpdm
->dev
, "IRQSTATUS: 0x%04x\n",
84 omap_mcpdm_read(mcpdm
, MCPDM_REG_IRQSTATUS
));
85 dev_dbg(mcpdm
->dev
, "IRQENABLE_SET: 0x%04x\n",
86 omap_mcpdm_read(mcpdm
, MCPDM_REG_IRQENABLE_SET
));
87 dev_dbg(mcpdm
->dev
, "IRQENABLE_CLR: 0x%04x\n",
88 omap_mcpdm_read(mcpdm
, MCPDM_REG_IRQENABLE_CLR
));
89 dev_dbg(mcpdm
->dev
, "IRQWAKE_EN: 0x%04x\n",
90 omap_mcpdm_read(mcpdm
, MCPDM_REG_IRQWAKE_EN
));
91 dev_dbg(mcpdm
->dev
, "DMAENABLE_SET: 0x%04x\n",
92 omap_mcpdm_read(mcpdm
, MCPDM_REG_DMAENABLE_SET
));
93 dev_dbg(mcpdm
->dev
, "DMAENABLE_CLR: 0x%04x\n",
94 omap_mcpdm_read(mcpdm
, MCPDM_REG_DMAENABLE_CLR
));
95 dev_dbg(mcpdm
->dev
, "DMAWAKEEN: 0x%04x\n",
96 omap_mcpdm_read(mcpdm
, MCPDM_REG_DMAWAKEEN
));
97 dev_dbg(mcpdm
->dev
, "CTRL: 0x%04x\n",
98 omap_mcpdm_read(mcpdm
, MCPDM_REG_CTRL
));
99 dev_dbg(mcpdm
->dev
, "DN_DATA: 0x%04x\n",
100 omap_mcpdm_read(mcpdm
, MCPDM_REG_DN_DATA
));
101 dev_dbg(mcpdm
->dev
, "UP_DATA: 0x%04x\n",
102 omap_mcpdm_read(mcpdm
, MCPDM_REG_UP_DATA
));
103 dev_dbg(mcpdm
->dev
, "FIFO_CTRL_DN: 0x%04x\n",
104 omap_mcpdm_read(mcpdm
, MCPDM_REG_FIFO_CTRL_DN
));
105 dev_dbg(mcpdm
->dev
, "FIFO_CTRL_UP: 0x%04x\n",
106 omap_mcpdm_read(mcpdm
, MCPDM_REG_FIFO_CTRL_UP
));
107 dev_dbg(mcpdm
->dev
, "***********************\n");
110 static void omap_mcpdm_reg_dump(struct omap_mcpdm
*mcpdm
) {}
114 * Enables the transfer through the PDM interface to/from the Phoenix
115 * codec by enabling the corresponding UP or DN channels.
117 static void omap_mcpdm_start(struct omap_mcpdm
*mcpdm
)
119 u32 ctrl
= omap_mcpdm_read(mcpdm
, MCPDM_REG_CTRL
);
120 u32 link_mask
= mcpdm
->config
[0].link_mask
| mcpdm
->config
[1].link_mask
;
122 ctrl
|= (MCPDM_SW_DN_RST
| MCPDM_SW_UP_RST
);
123 omap_mcpdm_write(mcpdm
, MCPDM_REG_CTRL
, ctrl
);
126 omap_mcpdm_write(mcpdm
, MCPDM_REG_CTRL
, ctrl
);
128 ctrl
&= ~(MCPDM_SW_DN_RST
| MCPDM_SW_UP_RST
);
129 omap_mcpdm_write(mcpdm
, MCPDM_REG_CTRL
, ctrl
);
133 * Disables the transfer through the PDM interface to/from the Phoenix
134 * codec by disabling the corresponding UP or DN channels.
136 static void omap_mcpdm_stop(struct omap_mcpdm
*mcpdm
)
138 u32 ctrl
= omap_mcpdm_read(mcpdm
, MCPDM_REG_CTRL
);
139 u32 link_mask
= MCPDM_PDM_DN_MASK
| MCPDM_PDM_UP_MASK
;
141 ctrl
|= (MCPDM_SW_DN_RST
| MCPDM_SW_UP_RST
);
142 omap_mcpdm_write(mcpdm
, MCPDM_REG_CTRL
, ctrl
);
144 ctrl
&= ~(link_mask
);
145 omap_mcpdm_write(mcpdm
, MCPDM_REG_CTRL
, ctrl
);
147 ctrl
&= ~(MCPDM_SW_DN_RST
| MCPDM_SW_UP_RST
);
148 omap_mcpdm_write(mcpdm
, MCPDM_REG_CTRL
, ctrl
);
153 * Is the physical McPDM interface active.
155 static inline int omap_mcpdm_active(struct omap_mcpdm
*mcpdm
)
157 return omap_mcpdm_read(mcpdm
, MCPDM_REG_CTRL
) &
158 (MCPDM_PDM_DN_MASK
| MCPDM_PDM_UP_MASK
);
162 * Configures McPDM uplink, and downlink for audio.
163 * This function should be called before omap_mcpdm_start.
165 static void omap_mcpdm_open_streams(struct omap_mcpdm
*mcpdm
)
167 u32 ctrl
= omap_mcpdm_read(mcpdm
, MCPDM_REG_CTRL
);
169 omap_mcpdm_write(mcpdm
, MCPDM_REG_CTRL
, ctrl
| MCPDM_WD_EN
);
171 omap_mcpdm_write(mcpdm
, MCPDM_REG_IRQENABLE_SET
,
172 MCPDM_DN_IRQ_EMPTY
| MCPDM_DN_IRQ_FULL
|
173 MCPDM_UP_IRQ_EMPTY
| MCPDM_UP_IRQ_FULL
);
175 /* Enable DN RX1/2 offset cancellation feature, if configured */
176 if (mcpdm
->dn_rx_offset
) {
177 u32 dn_offset
= mcpdm
->dn_rx_offset
;
179 omap_mcpdm_write(mcpdm
, MCPDM_REG_DN_OFFSET
, dn_offset
);
180 dn_offset
|= (MCPDM_DN_OFST_RX1_EN
| MCPDM_DN_OFST_RX2_EN
);
181 omap_mcpdm_write(mcpdm
, MCPDM_REG_DN_OFFSET
, dn_offset
);
184 omap_mcpdm_write(mcpdm
, MCPDM_REG_FIFO_CTRL_DN
,
185 mcpdm
->config
[SNDRV_PCM_STREAM_PLAYBACK
].threshold
);
186 omap_mcpdm_write(mcpdm
, MCPDM_REG_FIFO_CTRL_UP
,
187 mcpdm
->config
[SNDRV_PCM_STREAM_CAPTURE
].threshold
);
189 omap_mcpdm_write(mcpdm
, MCPDM_REG_DMAENABLE_SET
,
190 MCPDM_DMA_DN_ENABLE
| MCPDM_DMA_UP_ENABLE
);
194 * Cleans McPDM uplink, and downlink configuration.
195 * This function should be called when the stream is closed.
197 static void omap_mcpdm_close_streams(struct omap_mcpdm
*mcpdm
)
199 /* Disable irq request generation for downlink */
200 omap_mcpdm_write(mcpdm
, MCPDM_REG_IRQENABLE_CLR
,
201 MCPDM_DN_IRQ_EMPTY
| MCPDM_DN_IRQ_FULL
);
203 /* Disable DMA request generation for downlink */
204 omap_mcpdm_write(mcpdm
, MCPDM_REG_DMAENABLE_CLR
, MCPDM_DMA_DN_ENABLE
);
206 /* Disable irq request generation for uplink */
207 omap_mcpdm_write(mcpdm
, MCPDM_REG_IRQENABLE_CLR
,
208 MCPDM_UP_IRQ_EMPTY
| MCPDM_UP_IRQ_FULL
);
210 /* Disable DMA request generation for uplink */
211 omap_mcpdm_write(mcpdm
, MCPDM_REG_DMAENABLE_CLR
, MCPDM_DMA_UP_ENABLE
);
213 /* Disable RX1/2 offset cancellation */
214 if (mcpdm
->dn_rx_offset
)
215 omap_mcpdm_write(mcpdm
, MCPDM_REG_DN_OFFSET
, 0);
218 static irqreturn_t
omap_mcpdm_irq_handler(int irq
, void *dev_id
)
220 struct omap_mcpdm
*mcpdm
= dev_id
;
223 irq_status
= omap_mcpdm_read(mcpdm
, MCPDM_REG_IRQSTATUS
);
225 /* Acknowledge irq event */
226 omap_mcpdm_write(mcpdm
, MCPDM_REG_IRQSTATUS
, irq_status
);
228 if (irq_status
& MCPDM_DN_IRQ_FULL
)
229 dev_dbg(mcpdm
->dev
, "DN (playback) FIFO Full\n");
231 if (irq_status
& MCPDM_DN_IRQ_EMPTY
)
232 dev_dbg(mcpdm
->dev
, "DN (playback) FIFO Empty\n");
234 if (irq_status
& MCPDM_DN_IRQ
)
235 dev_dbg(mcpdm
->dev
, "DN (playback) write request\n");
237 if (irq_status
& MCPDM_UP_IRQ_FULL
)
238 dev_dbg(mcpdm
->dev
, "UP (capture) FIFO Full\n");
240 if (irq_status
& MCPDM_UP_IRQ_EMPTY
)
241 dev_dbg(mcpdm
->dev
, "UP (capture) FIFO Empty\n");
243 if (irq_status
& MCPDM_UP_IRQ
)
244 dev_dbg(mcpdm
->dev
, "UP (capture) write request\n");
249 static int omap_mcpdm_dai_startup(struct snd_pcm_substream
*substream
,
250 struct snd_soc_dai
*dai
)
252 struct omap_mcpdm
*mcpdm
= snd_soc_dai_get_drvdata(dai
);
254 mutex_lock(&mcpdm
->mutex
);
257 omap_mcpdm_open_streams(mcpdm
);
259 mutex_unlock(&mcpdm
->mutex
);
264 static void omap_mcpdm_dai_shutdown(struct snd_pcm_substream
*substream
,
265 struct snd_soc_dai
*dai
)
267 struct omap_mcpdm
*mcpdm
= snd_soc_dai_get_drvdata(dai
);
268 int tx
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
269 int stream1
= tx
? SNDRV_PCM_STREAM_PLAYBACK
: SNDRV_PCM_STREAM_CAPTURE
;
270 int stream2
= tx
? SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
;
272 mutex_lock(&mcpdm
->mutex
);
275 if (omap_mcpdm_active(mcpdm
)) {
276 omap_mcpdm_stop(mcpdm
);
277 omap_mcpdm_close_streams(mcpdm
);
278 mcpdm
->config
[0].link_mask
= 0;
279 mcpdm
->config
[1].link_mask
= 0;
283 if (mcpdm
->latency
[stream2
])
284 pm_qos_update_request(&mcpdm
->pm_qos_req
,
285 mcpdm
->latency
[stream2
]);
286 else if (mcpdm
->latency
[stream1
])
287 pm_qos_remove_request(&mcpdm
->pm_qos_req
);
289 mcpdm
->latency
[stream1
] = 0;
291 mutex_unlock(&mcpdm
->mutex
);
294 static int omap_mcpdm_dai_hw_params(struct snd_pcm_substream
*substream
,
295 struct snd_pcm_hw_params
*params
,
296 struct snd_soc_dai
*dai
)
298 struct omap_mcpdm
*mcpdm
= snd_soc_dai_get_drvdata(dai
);
299 int stream
= substream
->stream
;
300 struct snd_dmaengine_dai_dma_data
*dma_data
;
302 int channels
, latency
;
305 channels
= params_channels(params
);
308 if (stream
== SNDRV_PCM_STREAM_CAPTURE
)
309 /* up to 3 channels for capture */
314 if (stream
== SNDRV_PCM_STREAM_CAPTURE
)
315 /* up to 3 channels for capture */
329 /* unsupported number of channels */
333 dma_data
= snd_soc_dai_get_dma_data(dai
, substream
);
335 threshold
= mcpdm
->config
[stream
].threshold
;
336 /* Configure McPDM channels, and DMA packet size */
337 if (stream
== SNDRV_PCM_STREAM_PLAYBACK
) {
340 /* If capture is not running assume a stereo stream to come */
341 if (!mcpdm
->config
[!stream
].link_mask
)
342 mcpdm
->config
[!stream
].link_mask
= 0x3;
345 (MCPDM_DN_THRES_MAX
- threshold
) * channels
;
348 /* If playback is not running assume a stereo stream to come */
349 if (!mcpdm
->config
[!stream
].link_mask
)
350 mcpdm
->config
[!stream
].link_mask
= (0x3 << 3);
352 dma_data
->maxburst
= threshold
* channels
;
353 latency
= (MCPDM_DN_THRES_MAX
- threshold
);
357 * The DMA must act to a DMA request within latency time (usec) to avoid
360 mcpdm
->latency
[stream
] = latency
* USEC_PER_SEC
/ params_rate(params
);
362 if (!mcpdm
->latency
[stream
])
363 mcpdm
->latency
[stream
] = 10;
365 /* Check if we need to restart McPDM with this stream */
366 if (mcpdm
->config
[stream
].link_mask
&&
367 mcpdm
->config
[stream
].link_mask
!= link_mask
)
368 mcpdm
->restart
= true;
370 mcpdm
->config
[stream
].link_mask
= link_mask
;
375 static int omap_mcpdm_prepare(struct snd_pcm_substream
*substream
,
376 struct snd_soc_dai
*dai
)
378 struct omap_mcpdm
*mcpdm
= snd_soc_dai_get_drvdata(dai
);
379 struct pm_qos_request
*pm_qos_req
= &mcpdm
->pm_qos_req
;
380 int tx
= (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
);
381 int stream1
= tx
? SNDRV_PCM_STREAM_PLAYBACK
: SNDRV_PCM_STREAM_CAPTURE
;
382 int stream2
= tx
? SNDRV_PCM_STREAM_CAPTURE
: SNDRV_PCM_STREAM_PLAYBACK
;
383 int latency
= mcpdm
->latency
[stream2
];
385 /* Prevent omap hardware from hitting off between FIFO fills */
386 if (!latency
|| mcpdm
->latency
[stream1
] < latency
)
387 latency
= mcpdm
->latency
[stream1
];
389 if (pm_qos_request_active(pm_qos_req
))
390 pm_qos_update_request(pm_qos_req
, latency
);
392 pm_qos_add_request(pm_qos_req
, PM_QOS_CPU_DMA_LATENCY
, latency
);
394 if (!omap_mcpdm_active(mcpdm
)) {
395 omap_mcpdm_start(mcpdm
);
396 omap_mcpdm_reg_dump(mcpdm
);
397 } else if (mcpdm
->restart
) {
398 omap_mcpdm_stop(mcpdm
);
399 omap_mcpdm_start(mcpdm
);
400 mcpdm
->restart
= false;
401 omap_mcpdm_reg_dump(mcpdm
);
407 static const struct snd_soc_dai_ops omap_mcpdm_dai_ops
= {
408 .startup
= omap_mcpdm_dai_startup
,
409 .shutdown
= omap_mcpdm_dai_shutdown
,
410 .hw_params
= omap_mcpdm_dai_hw_params
,
411 .prepare
= omap_mcpdm_prepare
,
414 static int omap_mcpdm_probe(struct snd_soc_dai
*dai
)
416 struct omap_mcpdm
*mcpdm
= snd_soc_dai_get_drvdata(dai
);
419 pm_runtime_enable(mcpdm
->dev
);
421 /* Disable lines while request is ongoing */
422 pm_runtime_get_sync(mcpdm
->dev
);
423 omap_mcpdm_write(mcpdm
, MCPDM_REG_CTRL
, 0x00);
425 ret
= request_irq(mcpdm
->irq
, omap_mcpdm_irq_handler
, 0, "McPDM",
428 pm_runtime_put_sync(mcpdm
->dev
);
431 dev_err(mcpdm
->dev
, "Request for IRQ failed\n");
432 pm_runtime_disable(mcpdm
->dev
);
435 /* Configure McPDM threshold values */
436 mcpdm
->config
[SNDRV_PCM_STREAM_PLAYBACK
].threshold
= 2;
437 mcpdm
->config
[SNDRV_PCM_STREAM_CAPTURE
].threshold
=
438 MCPDM_UP_THRES_MAX
- 3;
440 snd_soc_dai_init_dma_data(dai
,
441 &mcpdm
->dma_data
[SNDRV_PCM_STREAM_PLAYBACK
],
442 &mcpdm
->dma_data
[SNDRV_PCM_STREAM_CAPTURE
]);
447 static int omap_mcpdm_remove(struct snd_soc_dai
*dai
)
449 struct omap_mcpdm
*mcpdm
= snd_soc_dai_get_drvdata(dai
);
451 free_irq(mcpdm
->irq
, (void *)mcpdm
);
452 pm_runtime_disable(mcpdm
->dev
);
454 if (pm_qos_request_active(&mcpdm
->pm_qos_req
))
455 pm_qos_remove_request(&mcpdm
->pm_qos_req
);
460 #ifdef CONFIG_PM_SLEEP
461 static int omap_mcpdm_suspend(struct snd_soc_component
*component
)
463 struct omap_mcpdm
*mcpdm
= snd_soc_component_get_drvdata(component
);
465 if (component
->active
) {
466 omap_mcpdm_stop(mcpdm
);
467 omap_mcpdm_close_streams(mcpdm
);
470 mcpdm
->pm_active_count
= 0;
471 while (pm_runtime_active(mcpdm
->dev
)) {
472 pm_runtime_put_sync(mcpdm
->dev
);
473 mcpdm
->pm_active_count
++;
479 static int omap_mcpdm_resume(struct snd_soc_component
*component
)
481 struct omap_mcpdm
*mcpdm
= snd_soc_component_get_drvdata(component
);
483 if (mcpdm
->pm_active_count
) {
484 while (mcpdm
->pm_active_count
--)
485 pm_runtime_get_sync(mcpdm
->dev
);
487 if (component
->active
) {
488 omap_mcpdm_open_streams(mcpdm
);
489 omap_mcpdm_start(mcpdm
);
497 #define omap_mcpdm_suspend NULL
498 #define omap_mcpdm_resume NULL
501 #define OMAP_MCPDM_RATES (SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
502 #define OMAP_MCPDM_FORMATS SNDRV_PCM_FMTBIT_S32_LE
504 static struct snd_soc_dai_driver omap_mcpdm_dai
= {
505 .probe
= omap_mcpdm_probe
,
506 .remove
= omap_mcpdm_remove
,
507 .probe_order
= SND_SOC_COMP_ORDER_LATE
,
508 .remove_order
= SND_SOC_COMP_ORDER_EARLY
,
512 .rates
= OMAP_MCPDM_RATES
,
513 .formats
= OMAP_MCPDM_FORMATS
,
519 .rates
= OMAP_MCPDM_RATES
,
520 .formats
= OMAP_MCPDM_FORMATS
,
523 .ops
= &omap_mcpdm_dai_ops
,
526 static const struct snd_soc_component_driver omap_mcpdm_component
= {
527 .name
= "omap-mcpdm",
528 .suspend
= omap_mcpdm_suspend
,
529 .resume
= omap_mcpdm_resume
,
532 void omap_mcpdm_configure_dn_offsets(struct snd_soc_pcm_runtime
*rtd
,
535 struct omap_mcpdm
*mcpdm
= snd_soc_dai_get_drvdata(rtd
->cpu_dai
);
537 mcpdm
->dn_rx_offset
= MCPDM_DNOFST_RX1(rx1
) | MCPDM_DNOFST_RX2(rx2
);
539 EXPORT_SYMBOL_GPL(omap_mcpdm_configure_dn_offsets
);
541 static int asoc_mcpdm_probe(struct platform_device
*pdev
)
543 struct omap_mcpdm
*mcpdm
;
544 struct resource
*res
;
547 mcpdm
= devm_kzalloc(&pdev
->dev
, sizeof(struct omap_mcpdm
), GFP_KERNEL
);
551 platform_set_drvdata(pdev
, mcpdm
);
553 mutex_init(&mcpdm
->mutex
);
555 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "dma");
559 mcpdm
->dma_data
[0].addr
= res
->start
+ MCPDM_REG_DN_DATA
;
560 mcpdm
->dma_data
[1].addr
= res
->start
+ MCPDM_REG_UP_DATA
;
562 mcpdm
->dma_data
[0].filter_data
= "dn_link";
563 mcpdm
->dma_data
[1].filter_data
= "up_link";
565 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "mpu");
566 mcpdm
->io_base
= devm_ioremap_resource(&pdev
->dev
, res
);
567 if (IS_ERR(mcpdm
->io_base
))
568 return PTR_ERR(mcpdm
->io_base
);
570 mcpdm
->irq
= platform_get_irq(pdev
, 0);
574 mcpdm
->dev
= &pdev
->dev
;
576 ret
= devm_snd_soc_register_component(&pdev
->dev
,
577 &omap_mcpdm_component
,
582 return sdma_pcm_platform_register(&pdev
->dev
, "dn_link", "up_link");
585 static const struct of_device_id omap_mcpdm_of_match
[] = {
586 { .compatible
= "ti,omap4-mcpdm", },
589 MODULE_DEVICE_TABLE(of
, omap_mcpdm_of_match
);
591 static struct platform_driver asoc_mcpdm_driver
= {
593 .name
= "omap-mcpdm",
594 .of_match_table
= omap_mcpdm_of_match
,
597 .probe
= asoc_mcpdm_probe
,
600 module_platform_driver(asoc_mcpdm_driver
);
602 MODULE_ALIAS("platform:omap-mcpdm");
603 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
604 MODULE_DESCRIPTION("OMAP PDM SoC Interface");
605 MODULE_LICENSE("GPL");