xref: /linux/sound/soc/fsl/imx-pcm-dma.c (revision 312bb4f626328fdc246c8d13082ab00e26e7d048)
1 /*
2  * imx-pcm-dma-mx2.c  --  ALSA Soc Audio Layer
3  *
4  * Copyright 2009 Sascha Hauer <s.hauer@pengutronix.de>
5  *
6  * This code is based on code copyrighted by Freescale,
7  * Liam Girdwood, Javier Martin and probably others.
8  *
9  *  This program is free software; you can redistribute  it and/or modify it
10  *  under  the terms of  the GNU General  Public License as published by the
11  *  Free Software Foundation;  either version 2 of the  License, or (at your
12  *  option) any later version.
13  */
14 #include <linux/clk.h>
15 #include <linux/delay.h>
16 #include <linux/device.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
20 #include <linux/module.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.h>
23 #include <linux/dmaengine.h>
24 #include <linux/types.h>
25 
26 #include <sound/core.h>
27 #include <sound/initval.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/dmaengine_pcm.h>
32 
33 #include "imx-pcm.h"
34 
35 static bool filter(struct dma_chan *chan, void *param)
36 {
37 	if (!imx_dma_is_general_purpose(chan))
38 		return false;
39 
40 	chan->private = param;
41 
42 	return true;
43 }
44 
45 static int snd_imx_pcm_hw_params(struct snd_pcm_substream *substream,
46 				struct snd_pcm_hw_params *params)
47 {
48 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
49 	struct dma_chan *chan = snd_dmaengine_pcm_get_chan(substream);
50 	struct imx_pcm_dma_params *dma_params;
51 	struct dma_slave_config slave_config;
52 	int ret;
53 
54 	dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
55 
56 	ret = snd_hwparams_to_dma_slave_config(substream, params, &slave_config);
57 	if (ret)
58 		return ret;
59 
60 	slave_config.device_fc = false;
61 
62 	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
63 		slave_config.dst_addr = dma_params->dma_addr;
64 		slave_config.dst_maxburst = dma_params->burstsize;
65 	} else {
66 		slave_config.src_addr = dma_params->dma_addr;
67 		slave_config.src_maxburst = dma_params->burstsize;
68 	}
69 
70 	ret = dmaengine_slave_config(chan, &slave_config);
71 	if (ret)
72 		return ret;
73 
74 	snd_pcm_set_runtime_buffer(substream, &substream->dma_buffer);
75 
76 	return 0;
77 }
78 
79 static struct snd_pcm_hardware snd_imx_hardware = {
80 	.info = SNDRV_PCM_INFO_INTERLEAVED |
81 		SNDRV_PCM_INFO_BLOCK_TRANSFER |
82 		SNDRV_PCM_INFO_MMAP |
83 		SNDRV_PCM_INFO_MMAP_VALID |
84 		SNDRV_PCM_INFO_PAUSE |
85 		SNDRV_PCM_INFO_RESUME,
86 	.formats = SNDRV_PCM_FMTBIT_S16_LE,
87 	.rate_min = 8000,
88 	.channels_min = 2,
89 	.channels_max = 2,
90 	.buffer_bytes_max = IMX_SSI_DMABUF_SIZE,
91 	.period_bytes_min = 128,
92 	.period_bytes_max = 65535, /* Limited by SDMA engine */
93 	.periods_min = 2,
94 	.periods_max = 255,
95 	.fifo_size = 0,
96 };
97 
98 static int snd_imx_open(struct snd_pcm_substream *substream)
99 {
100 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
101 	struct imx_pcm_dma_params *dma_params;
102 
103 	snd_soc_set_runtime_hwparams(substream, &snd_imx_hardware);
104 
105 	dma_params = snd_soc_dai_get_dma_data(rtd->cpu_dai, substream);
106 
107 	return snd_dmaengine_pcm_open(substream, filter, &dma_params->dma_data);
108 }
109 
110 static struct snd_pcm_ops imx_pcm_ops = {
111 	.open		= snd_imx_open,
112 	.close		= snd_dmaengine_pcm_close,
113 	.ioctl		= snd_pcm_lib_ioctl,
114 	.hw_params	= snd_imx_pcm_hw_params,
115 	.trigger	= snd_dmaengine_pcm_trigger,
116 	.pointer	= snd_dmaengine_pcm_pointer_no_residue,
117 	.mmap		= snd_imx_pcm_mmap,
118 };
119 
120 static struct snd_soc_platform_driver imx_soc_platform_mx2 = {
121 	.ops		= &imx_pcm_ops,
122 	.pcm_new	= imx_pcm_new,
123 	.pcm_free	= imx_pcm_free,
124 };
125 
126 int imx_pcm_dma_init(struct platform_device *pdev)
127 {
128 	return snd_soc_register_platform(&pdev->dev, &imx_soc_platform_mx2);
129 }
130