2 * omap-mcpdm.c -- OMAP ALSA SoC DAI driver using McPDM port
4 * Copyright (C) 2009 - 2011 Texas Instruments
6 * Author: Misael Lopez Cruz <misael.lopez@ti.com>
7 * Contact: Jorge Eduardo Candelaria <x0107209@ti.com>
8 * Margarita Olaya <magi.olaya@ti.com>
9 * Peter Ujfalusi <peter.ujfalusi@ti.com>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * version 2 as published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/platform_device.h>
30 #include <linux/interrupt.h>
31 #include <linux/err.h>
33 #include <linux/irq.h>
34 #include <linux/slab.h>
35 #include <linux/pm_runtime.h>
37 #include <sound/core.h>
38 #include <sound/pcm.h>
39 #include <sound/pcm_params.h>
40 #include <sound/soc.h>
43 #include <plat/omap_hwmod.h>
44 #include "omap-mcpdm.h"
49 unsigned long phys_base;
50 void __iomem *io_base;
59 /* McPDM FIFO thresholds */
63 /* McPDM dn offsets for rx1, and 2 channels */
68 * Stream DMA parameters
70 static struct omap_pcm_dma_data omap_mcpdm_dai_dma_params[] = {
72 .name = "Audio playback",
73 .dma_req = OMAP44XX_DMA_MCPDM_DL,
74 .data_type = OMAP_DMA_DATA_TYPE_S32,
75 .sync_mode = OMAP_DMA_SYNC_PACKET,
76 .port_addr = OMAP44XX_MCPDM_L3_BASE + MCPDM_REG_DN_DATA,
79 .name = "Audio capture",
80 .dma_req = OMAP44XX_DMA_MCPDM_UP,
81 .data_type = OMAP_DMA_DATA_TYPE_S32,
82 .sync_mode = OMAP_DMA_SYNC_PACKET,
83 .port_addr = OMAP44XX_MCPDM_L3_BASE + MCPDM_REG_UP_DATA,
87 static inline void omap_mcpdm_write(struct omap_mcpdm *mcpdm, u16 reg, u32 val)
89 __raw_writel(val, mcpdm->io_base + reg);
92 static inline int omap_mcpdm_read(struct omap_mcpdm *mcpdm, u16 reg)
94 return __raw_readl(mcpdm->io_base + reg);
98 static void omap_mcpdm_reg_dump(struct omap_mcpdm *mcpdm)
100 dev_dbg(mcpdm->dev, "***********************\n");
101 dev_dbg(mcpdm->dev, "IRQSTATUS_RAW: 0x%04x\n",
102 omap_mcpdm_read(mcpdm, MCPDM_REG_IRQSTATUS_RAW));
103 dev_dbg(mcpdm->dev, "IRQSTATUS: 0x%04x\n",
104 omap_mcpdm_read(mcpdm, MCPDM_REG_IRQSTATUS));
105 dev_dbg(mcpdm->dev, "IRQENABLE_SET: 0x%04x\n",
106 omap_mcpdm_read(mcpdm, MCPDM_REG_IRQENABLE_SET));
107 dev_dbg(mcpdm->dev, "IRQENABLE_CLR: 0x%04x\n",
108 omap_mcpdm_read(mcpdm, MCPDM_REG_IRQENABLE_CLR));
109 dev_dbg(mcpdm->dev, "IRQWAKE_EN: 0x%04x\n",
110 omap_mcpdm_read(mcpdm, MCPDM_REG_IRQWAKE_EN));
111 dev_dbg(mcpdm->dev, "DMAENABLE_SET: 0x%04x\n",
112 omap_mcpdm_read(mcpdm, MCPDM_REG_DMAENABLE_SET));
113 dev_dbg(mcpdm->dev, "DMAENABLE_CLR: 0x%04x\n",
114 omap_mcpdm_read(mcpdm, MCPDM_REG_DMAENABLE_CLR));
115 dev_dbg(mcpdm->dev, "DMAWAKEEN: 0x%04x\n",
116 omap_mcpdm_read(mcpdm, MCPDM_REG_DMAWAKEEN));
117 dev_dbg(mcpdm->dev, "CTRL: 0x%04x\n",
118 omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL));
119 dev_dbg(mcpdm->dev, "DN_DATA: 0x%04x\n",
120 omap_mcpdm_read(mcpdm, MCPDM_REG_DN_DATA));
121 dev_dbg(mcpdm->dev, "UP_DATA: 0x%04x\n",
122 omap_mcpdm_read(mcpdm, MCPDM_REG_UP_DATA));
123 dev_dbg(mcpdm->dev, "FIFO_CTRL_DN: 0x%04x\n",
124 omap_mcpdm_read(mcpdm, MCPDM_REG_FIFO_CTRL_DN));
125 dev_dbg(mcpdm->dev, "FIFO_CTRL_UP: 0x%04x\n",
126 omap_mcpdm_read(mcpdm, MCPDM_REG_FIFO_CTRL_UP));
127 dev_dbg(mcpdm->dev, "***********************\n");
130 static void omap_mcpdm_reg_dump(struct omap_mcpdm *mcpdm) {}
134 * Enables the transfer through the PDM interface to/from the Phoenix
135 * codec by enabling the corresponding UP or DN channels.
137 static void omap_mcpdm_start(struct omap_mcpdm *mcpdm)
139 u32 ctrl = omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL);
141 ctrl |= (MCPDM_SW_DN_RST | MCPDM_SW_UP_RST);
142 omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl);
144 ctrl |= mcpdm->dn_channels | mcpdm->up_channels;
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);
152 * Disables the transfer through the PDM interface to/from the Phoenix
153 * codec by disabling the corresponding UP or DN channels.
155 static void omap_mcpdm_stop(struct omap_mcpdm *mcpdm)
157 u32 ctrl = omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL);
159 ctrl |= (MCPDM_SW_DN_RST | MCPDM_SW_UP_RST);
160 omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl);
162 ctrl &= ~(mcpdm->dn_channels | mcpdm->up_channels);
163 omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl);
165 ctrl &= ~(MCPDM_SW_DN_RST | MCPDM_SW_UP_RST);
166 omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, ctrl);
171 * Is the physical McPDM interface active.
173 static inline int omap_mcpdm_active(struct omap_mcpdm *mcpdm)
175 return omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL) &
176 (MCPDM_PDM_DN_MASK | MCPDM_PDM_UP_MASK);
180 * Configures McPDM uplink, and downlink for audio.
181 * This function should be called before omap_mcpdm_start.
183 static void omap_mcpdm_open_streams(struct omap_mcpdm *mcpdm)
185 omap_mcpdm_write(mcpdm, MCPDM_REG_IRQENABLE_SET,
186 MCPDM_DN_IRQ_EMPTY | MCPDM_DN_IRQ_FULL |
187 MCPDM_UP_IRQ_EMPTY | MCPDM_UP_IRQ_FULL);
189 /* Enable DN RX1/2 offset cancellation feature, if configured */
190 if (mcpdm->dn_rx_offset) {
191 u32 dn_offset = mcpdm->dn_rx_offset;
193 omap_mcpdm_write(mcpdm, MCPDM_REG_DN_OFFSET, dn_offset);
194 dn_offset |= (MCPDM_DN_OFST_RX1_EN | MCPDM_DN_OFST_RX2_EN);
195 omap_mcpdm_write(mcpdm, MCPDM_REG_DN_OFFSET, dn_offset);
198 omap_mcpdm_write(mcpdm, MCPDM_REG_FIFO_CTRL_DN, mcpdm->dn_threshold);
199 omap_mcpdm_write(mcpdm, MCPDM_REG_FIFO_CTRL_UP, mcpdm->up_threshold);
201 omap_mcpdm_write(mcpdm, MCPDM_REG_DMAENABLE_SET,
202 MCPDM_DMA_DN_ENABLE | MCPDM_DMA_UP_ENABLE);
206 * Cleans McPDM uplink, and downlink configuration.
207 * This function should be called when the stream is closed.
209 static void omap_mcpdm_close_streams(struct omap_mcpdm *mcpdm)
211 /* Disable irq request generation for downlink */
212 omap_mcpdm_write(mcpdm, MCPDM_REG_IRQENABLE_CLR,
213 MCPDM_DN_IRQ_EMPTY | MCPDM_DN_IRQ_FULL);
215 /* Disable DMA request generation for downlink */
216 omap_mcpdm_write(mcpdm, MCPDM_REG_DMAENABLE_CLR, MCPDM_DMA_DN_ENABLE);
218 /* Disable irq request generation for uplink */
219 omap_mcpdm_write(mcpdm, MCPDM_REG_IRQENABLE_CLR,
220 MCPDM_UP_IRQ_EMPTY | MCPDM_UP_IRQ_FULL);
222 /* Disable DMA request generation for uplink */
223 omap_mcpdm_write(mcpdm, MCPDM_REG_DMAENABLE_CLR, MCPDM_DMA_UP_ENABLE);
225 /* Disable RX1/2 offset cancellation */
226 if (mcpdm->dn_rx_offset)
227 omap_mcpdm_write(mcpdm, MCPDM_REG_DN_OFFSET, 0);
230 static irqreturn_t omap_mcpdm_irq_handler(int irq, void *dev_id)
232 struct omap_mcpdm *mcpdm = dev_id;
235 irq_status = omap_mcpdm_read(mcpdm, MCPDM_REG_IRQSTATUS);
237 /* Acknowledge irq event */
238 omap_mcpdm_write(mcpdm, MCPDM_REG_IRQSTATUS, irq_status);
240 if (irq_status & MCPDM_DN_IRQ_FULL)
241 dev_dbg(mcpdm->dev, "DN (playback) FIFO Full\n");
243 if (irq_status & MCPDM_DN_IRQ_EMPTY)
244 dev_dbg(mcpdm->dev, "DN (playback) FIFO Empty\n");
246 if (irq_status & MCPDM_DN_IRQ)
247 dev_dbg(mcpdm->dev, "DN (playback) write request\n");
249 if (irq_status & MCPDM_UP_IRQ_FULL)
250 dev_dbg(mcpdm->dev, "UP (capture) FIFO Full\n");
252 if (irq_status & MCPDM_UP_IRQ_EMPTY)
253 dev_dbg(mcpdm->dev, "UP (capture) FIFO Empty\n");
255 if (irq_status & MCPDM_UP_IRQ)
256 dev_dbg(mcpdm->dev, "UP (capture) write request\n");
261 static int omap_mcpdm_dai_startup(struct snd_pcm_substream *substream,
262 struct snd_soc_dai *dai)
264 struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai);
266 mutex_lock(&mcpdm->mutex);
269 pm_runtime_get_sync(mcpdm->dev);
271 /* Enable watch dog for ES above ES 1.0 to avoid saturation */
272 if (omap_rev() != OMAP4430_REV_ES1_0) {
273 u32 ctrl = omap_mcpdm_read(mcpdm, MCPDM_REG_CTRL);
275 omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL,
278 omap_mcpdm_open_streams(mcpdm);
281 mutex_unlock(&mcpdm->mutex);
286 static void omap_mcpdm_dai_shutdown(struct snd_pcm_substream *substream,
287 struct snd_soc_dai *dai)
289 struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai);
291 mutex_lock(&mcpdm->mutex);
294 if (omap_mcpdm_active(mcpdm)) {
295 omap_mcpdm_stop(mcpdm);
296 omap_mcpdm_close_streams(mcpdm);
299 if (!omap_mcpdm_active(mcpdm))
300 pm_runtime_put_sync(mcpdm->dev);
303 mutex_unlock(&mcpdm->mutex);
306 static int omap_mcpdm_dai_hw_params(struct snd_pcm_substream *substream,
307 struct snd_pcm_hw_params *params,
308 struct snd_soc_dai *dai)
310 struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai);
311 int stream = substream->stream;
312 struct omap_pcm_dma_data *dma_data;
316 channels = params_channels(params);
319 if (stream == SNDRV_PCM_STREAM_CAPTURE)
320 /* up to 3 channels for capture */
324 if (stream == SNDRV_PCM_STREAM_CAPTURE)
325 /* up to 3 channels for capture */
336 /* unsupported number of channels */
340 dma_data = &omap_mcpdm_dai_dma_params[stream];
342 /* Configure McPDM channels, and DMA packet size */
343 if (stream == SNDRV_PCM_STREAM_PLAYBACK) {
344 mcpdm->dn_channels = link_mask << 3;
345 dma_data->packet_size =
346 (MCPDM_DN_THRES_MAX - mcpdm->dn_threshold) * channels;
348 mcpdm->up_channels = link_mask << 0;
349 dma_data->packet_size = mcpdm->up_threshold * channels;
352 snd_soc_dai_set_dma_data(dai, substream, dma_data);
357 static int omap_mcpdm_prepare(struct snd_pcm_substream *substream,
358 struct snd_soc_dai *dai)
360 struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai);
362 if (!omap_mcpdm_active(mcpdm)) {
363 omap_mcpdm_start(mcpdm);
364 omap_mcpdm_reg_dump(mcpdm);
370 static struct snd_soc_dai_ops omap_mcpdm_dai_ops = {
371 .startup = omap_mcpdm_dai_startup,
372 .shutdown = omap_mcpdm_dai_shutdown,
373 .hw_params = omap_mcpdm_dai_hw_params,
374 .prepare = omap_mcpdm_prepare,
377 static int omap_mcpdm_probe(struct snd_soc_dai *dai)
379 struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai);
382 pm_runtime_enable(mcpdm->dev);
384 /* Disable lines while request is ongoing */
385 pm_runtime_get_sync(mcpdm->dev);
386 omap_mcpdm_write(mcpdm, MCPDM_REG_CTRL, 0x00);
388 ret = request_irq(mcpdm->irq, omap_mcpdm_irq_handler,
389 0, "McPDM", (void *)mcpdm);
391 pm_runtime_put_sync(mcpdm->dev);
394 dev_err(mcpdm->dev, "Request for IRQ failed\n");
395 pm_runtime_disable(mcpdm->dev);
398 /* Configure McPDM threshold values */
399 mcpdm->dn_threshold = 2;
400 mcpdm->up_threshold = MCPDM_UP_THRES_MAX - 3;
404 static int omap_mcpdm_remove(struct snd_soc_dai *dai)
406 struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(dai);
408 free_irq(mcpdm->irq, (void *)mcpdm);
409 pm_runtime_disable(mcpdm->dev);
414 #define OMAP_MCPDM_RATES (SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000)
415 #define OMAP_MCPDM_FORMATS SNDRV_PCM_FMTBIT_S32_LE
417 static struct snd_soc_dai_driver omap_mcpdm_dai = {
418 .probe = omap_mcpdm_probe,
419 .remove = omap_mcpdm_remove,
420 .probe_order = SND_SOC_COMP_ORDER_LATE,
421 .remove_order = SND_SOC_COMP_ORDER_EARLY,
425 .rates = OMAP_MCPDM_RATES,
426 .formats = OMAP_MCPDM_FORMATS,
431 .rates = OMAP_MCPDM_RATES,
432 .formats = OMAP_MCPDM_FORMATS,
434 .ops = &omap_mcpdm_dai_ops,
437 void omap_mcpdm_configure_dn_offsets(struct snd_soc_pcm_runtime *rtd,
440 struct omap_mcpdm *mcpdm = snd_soc_dai_get_drvdata(rtd->cpu_dai);
442 mcpdm->dn_rx_offset = MCPDM_DNOFST_RX1(rx1) | MCPDM_DNOFST_RX2(rx2);
444 EXPORT_SYMBOL_GPL(omap_mcpdm_configure_dn_offsets);
446 static __devinit int asoc_mcpdm_probe(struct platform_device *pdev)
448 struct omap_mcpdm *mcpdm;
449 struct resource *res;
452 mcpdm = kzalloc(sizeof(struct omap_mcpdm), GFP_KERNEL);
456 platform_set_drvdata(pdev, mcpdm);
458 mutex_init(&mcpdm->mutex);
460 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
462 dev_err(&pdev->dev, "no resource\n");
466 if (!request_mem_region(res->start, resource_size(res), "McPDM")) {
471 mcpdm->io_base = ioremap(res->start, resource_size(res));
472 if (!mcpdm->io_base) {
477 mcpdm->irq = platform_get_irq(pdev, 0);
478 if (mcpdm->irq < 0) {
483 mcpdm->dev = &pdev->dev;
485 ret = snd_soc_register_dai(&pdev->dev, &omap_mcpdm_dai);
490 iounmap(mcpdm->io_base);
492 release_mem_region(res->start, resource_size(res));
498 static int __devexit asoc_mcpdm_remove(struct platform_device *pdev)
500 struct omap_mcpdm *mcpdm = platform_get_drvdata(pdev);
501 struct resource *res;
503 snd_soc_unregister_dai(&pdev->dev);
505 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
506 iounmap(mcpdm->io_base);
507 release_mem_region(res->start, resource_size(res));
513 static struct platform_driver asoc_mcpdm_driver = {
515 .name = "omap-mcpdm",
516 .owner = THIS_MODULE,
519 .probe = asoc_mcpdm_probe,
520 .remove = __devexit_p(asoc_mcpdm_remove),
523 static int __init snd_omap_mcpdm_init(void)
525 return platform_driver_register(&asoc_mcpdm_driver);
527 module_init(snd_omap_mcpdm_init);
529 static void __exit snd_omap_mcpdm_exit(void)
531 platform_driver_unregister(&asoc_mcpdm_driver);
533 module_exit(snd_omap_mcpdm_exit);
535 MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
536 MODULE_DESCRIPTION("OMAP PDM SoC Interface");
537 MODULE_LICENSE("GPL");