Linux 3.8-rc7
[cris-mirror.git] / sound / soc / ux500 / ux500_pcm.c
blob846fa82a58d013bbb04980fdfeab9aa44d4e505d
1 /*
2 * Copyright (C) ST-Ericsson SA 2012
4 * Author: Ola Lilja <ola.o.lilja@stericsson.com>,
5 * Roger Nilsson <roger.xr.nilsson@stericsson.com>
6 * for ST-Ericsson.
8 * License terms:
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
15 #include <asm/page.h>
17 #include <linux/module.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/dmaengine.h>
20 #include <linux/slab.h>
21 #include <linux/platform_data/dma-ste-dma40.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/dmaengine_pcm.h>
28 #include "ux500_msp_i2s.h"
29 #include "ux500_pcm.h"
31 static struct snd_pcm_hardware ux500_pcm_hw_playback = {
32 .info = SNDRV_PCM_INFO_INTERLEAVED |
33 SNDRV_PCM_INFO_MMAP |
34 SNDRV_PCM_INFO_RESUME |
35 SNDRV_PCM_INFO_PAUSE,
36 .formats = SNDRV_PCM_FMTBIT_S16_LE |
37 SNDRV_PCM_FMTBIT_U16_LE |
38 SNDRV_PCM_FMTBIT_S16_BE |
39 SNDRV_PCM_FMTBIT_U16_BE,
40 .rates = SNDRV_PCM_RATE_KNOT,
41 .rate_min = UX500_PLATFORM_MIN_RATE_PLAYBACK,
42 .rate_max = UX500_PLATFORM_MAX_RATE_PLAYBACK,
43 .channels_min = UX500_PLATFORM_MIN_CHANNELS,
44 .channels_max = UX500_PLATFORM_MAX_CHANNELS,
45 .buffer_bytes_max = UX500_PLATFORM_BUFFER_BYTES_MAX,
46 .period_bytes_min = UX500_PLATFORM_PERIODS_BYTES_MIN,
47 .period_bytes_max = UX500_PLATFORM_PERIODS_BYTES_MAX,
48 .periods_min = UX500_PLATFORM_PERIODS_MIN,
49 .periods_max = UX500_PLATFORM_PERIODS_MAX,
52 static struct snd_pcm_hardware ux500_pcm_hw_capture = {
53 .info = SNDRV_PCM_INFO_INTERLEAVED |
54 SNDRV_PCM_INFO_MMAP |
55 SNDRV_PCM_INFO_RESUME |
56 SNDRV_PCM_INFO_PAUSE,
57 .formats = SNDRV_PCM_FMTBIT_S16_LE |
58 SNDRV_PCM_FMTBIT_U16_LE |
59 SNDRV_PCM_FMTBIT_S16_BE |
60 SNDRV_PCM_FMTBIT_U16_BE,
61 .rates = SNDRV_PCM_RATE_KNOT,
62 .rate_min = UX500_PLATFORM_MIN_RATE_CAPTURE,
63 .rate_max = UX500_PLATFORM_MAX_RATE_CAPTURE,
64 .channels_min = UX500_PLATFORM_MIN_CHANNELS,
65 .channels_max = UX500_PLATFORM_MAX_CHANNELS,
66 .buffer_bytes_max = UX500_PLATFORM_BUFFER_BYTES_MAX,
67 .period_bytes_min = UX500_PLATFORM_PERIODS_BYTES_MIN,
68 .period_bytes_max = UX500_PLATFORM_PERIODS_BYTES_MAX,
69 .periods_min = UX500_PLATFORM_PERIODS_MIN,
70 .periods_max = UX500_PLATFORM_PERIODS_MAX,
73 static void ux500_pcm_dma_hw_free(struct device *dev,
74 struct snd_pcm_substream *substream)
76 struct snd_pcm_runtime *runtime = substream->runtime;
77 struct snd_dma_buffer *buf = runtime->dma_buffer_p;
79 if (runtime->dma_area == NULL)
80 return;
82 if (buf != &substream->dma_buffer) {
83 dma_free_coherent(buf->dev.dev, buf->bytes, buf->area,
84 buf->addr);
85 kfree(runtime->dma_buffer_p);
88 snd_pcm_set_runtime_buffer(substream, NULL);
91 static int ux500_pcm_open(struct snd_pcm_substream *substream)
93 int stream_id = substream->pstr->stream;
94 struct snd_pcm_runtime *runtime = substream->runtime;
95 struct snd_soc_pcm_runtime *rtd = substream->private_data;
96 struct snd_soc_dai *dai = rtd->cpu_dai;
97 struct device *dev = dai->dev;
98 int ret;
99 struct ux500_msp_dma_params *dma_params;
100 u16 per_data_width, mem_data_width;
101 struct stedma40_chan_cfg *dma_cfg;
103 dev_dbg(dev, "%s: MSP %d (%s): Enter.\n", __func__, dai->id,
104 snd_pcm_stream_str(substream));
106 dev_dbg(dev, "%s: Set runtime hwparams.\n", __func__);
107 if (stream_id == SNDRV_PCM_STREAM_PLAYBACK)
108 snd_soc_set_runtime_hwparams(substream,
109 &ux500_pcm_hw_playback);
110 else
111 snd_soc_set_runtime_hwparams(substream,
112 &ux500_pcm_hw_capture);
114 /* ensure that buffer size is a multiple of period size */
115 ret = snd_pcm_hw_constraint_integer(runtime,
116 SNDRV_PCM_HW_PARAM_PERIODS);
117 if (ret < 0) {
118 dev_err(dev, "%s: Error: snd_pcm_hw_constraints failed (%d)\n",
119 __func__, ret);
120 return ret;
123 dev_dbg(dev, "%s: Set hw-struct for %s.\n", __func__,
124 snd_pcm_stream_str(substream));
125 runtime->hw = (stream_id == SNDRV_PCM_STREAM_PLAYBACK) ?
126 ux500_pcm_hw_playback : ux500_pcm_hw_capture;
128 mem_data_width = STEDMA40_HALFWORD_WIDTH;
130 dma_params = snd_soc_dai_get_dma_data(dai, substream);
131 switch (dma_params->data_size) {
132 case 32:
133 per_data_width = STEDMA40_WORD_WIDTH;
134 break;
135 case 16:
136 per_data_width = STEDMA40_HALFWORD_WIDTH;
137 break;
138 case 8:
139 per_data_width = STEDMA40_BYTE_WIDTH;
140 break;
141 default:
142 per_data_width = STEDMA40_WORD_WIDTH;
143 dev_warn(rtd->platform->dev,
144 "%s: Unknown data-size (%d)! Assuming 32 bits.\n",
145 __func__, dma_params->data_size);
148 dma_cfg = dma_params->dma_cfg;
150 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
151 dma_cfg->src_info.data_width = mem_data_width;
152 dma_cfg->dst_info.data_width = per_data_width;
153 } else {
154 dma_cfg->src_info.data_width = per_data_width;
155 dma_cfg->dst_info.data_width = mem_data_width;
159 ret = snd_dmaengine_pcm_open(substream, stedma40_filter, dma_cfg);
160 if (ret) {
161 dev_dbg(dai->dev,
162 "%s: ERROR: snd_dmaengine_pcm_open failed (%d)!\n",
163 __func__, ret);
164 return ret;
167 snd_dmaengine_pcm_set_data(substream, dma_cfg);
169 return 0;
172 static int ux500_pcm_close(struct snd_pcm_substream *substream)
174 struct snd_soc_pcm_runtime *rtd = substream->private_data;
175 struct snd_soc_dai *dai = rtd->cpu_dai;
177 dev_dbg(dai->dev, "%s: Enter\n", __func__);
179 snd_dmaengine_pcm_close(substream);
181 return 0;
184 static int ux500_pcm_hw_params(struct snd_pcm_substream *substream,
185 struct snd_pcm_hw_params *hw_params)
187 struct snd_pcm_runtime *runtime = substream->runtime;
188 struct snd_dma_buffer *buf = runtime->dma_buffer_p;
189 struct snd_soc_pcm_runtime *rtd = substream->private_data;
190 int ret = 0;
191 int size;
193 dev_dbg(rtd->platform->dev, "%s: Enter\n", __func__);
195 size = params_buffer_bytes(hw_params);
197 if (buf) {
198 if (buf->bytes >= size)
199 goto out;
200 ux500_pcm_dma_hw_free(NULL, substream);
203 if (substream->dma_buffer.area != NULL &&
204 substream->dma_buffer.bytes >= size) {
205 buf = &substream->dma_buffer;
206 } else {
207 buf = kmalloc(sizeof(struct snd_dma_buffer), GFP_KERNEL);
208 if (!buf)
209 goto nomem;
211 buf->dev.type = SNDRV_DMA_TYPE_DEV;
212 buf->dev.dev = NULL;
213 buf->area = dma_alloc_coherent(NULL, size, &buf->addr,
214 GFP_KERNEL);
215 buf->bytes = size;
216 buf->private_data = NULL;
218 if (!buf->area)
219 goto free;
221 snd_pcm_set_runtime_buffer(substream, buf);
222 ret = 1;
223 out:
224 runtime->dma_bytes = size;
225 return ret;
227 free:
228 kfree(buf);
229 nomem:
230 return -ENOMEM;
233 static int ux500_pcm_hw_free(struct snd_pcm_substream *substream)
235 struct snd_soc_pcm_runtime *rtd = substream->private_data;
237 dev_dbg(rtd->platform->dev, "%s: Enter\n", __func__);
239 ux500_pcm_dma_hw_free(NULL, substream);
241 return 0;
244 static int ux500_pcm_mmap(struct snd_pcm_substream *substream,
245 struct vm_area_struct *vma)
247 struct snd_pcm_runtime *runtime = substream->runtime;
248 struct snd_soc_pcm_runtime *rtd = substream->private_data;
250 dev_dbg(rtd->platform->dev, "%s: Enter.\n", __func__);
252 return dma_mmap_coherent(NULL, vma, runtime->dma_area,
253 runtime->dma_addr, runtime->dma_bytes);
256 static struct snd_pcm_ops ux500_pcm_ops = {
257 .open = ux500_pcm_open,
258 .close = ux500_pcm_close,
259 .ioctl = snd_pcm_lib_ioctl,
260 .hw_params = ux500_pcm_hw_params,
261 .hw_free = ux500_pcm_hw_free,
262 .trigger = snd_dmaengine_pcm_trigger,
263 .pointer = snd_dmaengine_pcm_pointer_no_residue,
264 .mmap = ux500_pcm_mmap
267 int ux500_pcm_new(struct snd_soc_pcm_runtime *rtd)
269 struct snd_pcm *pcm = rtd->pcm;
271 dev_dbg(rtd->platform->dev, "%s: Enter (id = '%s').\n", __func__,
272 pcm->id);
274 pcm->info_flags = 0;
276 return 0;
279 static struct snd_soc_platform_driver ux500_pcm_soc_drv = {
280 .ops = &ux500_pcm_ops,
281 .pcm_new = ux500_pcm_new,
284 int ux500_pcm_register_platform(struct platform_device *pdev)
286 int ret;
288 ret = snd_soc_register_platform(&pdev->dev, &ux500_pcm_soc_drv);
289 if (ret < 0) {
290 dev_err(&pdev->dev,
291 "%s: ERROR: Failed to register platform '%s' (%d)!\n",
292 __func__, pdev->name, ret);
293 return ret;
296 return 0;
298 EXPORT_SYMBOL_GPL(ux500_pcm_register_platform);
300 int ux500_pcm_unregister_platform(struct platform_device *pdev)
302 snd_soc_unregister_platform(&pdev->dev);
304 return 0;
306 EXPORT_SYMBOL_GPL(ux500_pcm_unregister_platform);