2 * Copyright (C) ST-Ericsson SA 2012
4 * Author: Ola Lilja <ola.o.lilja@stericsson.com>,
5 * Roger Nilsson <roger.xr.nilsson@stericsson.com>
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.
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 #define UX500_PLATFORM_MIN_RATE 8000
32 #define UX500_PLATFORM_MAX_RATE 48000
34 #define UX500_PLATFORM_MIN_CHANNELS 1
35 #define UX500_PLATFORM_MAX_CHANNELS 8
37 #define UX500_PLATFORM_PERIODS_BYTES_MIN 128
38 #define UX500_PLATFORM_PERIODS_BYTES_MAX (64 * PAGE_SIZE)
39 #define UX500_PLATFORM_PERIODS_MIN 2
40 #define UX500_PLATFORM_PERIODS_MAX 48
41 #define UX500_PLATFORM_BUFFER_BYTES_MAX (2048 * PAGE_SIZE)
43 static struct snd_pcm_hardware ux500_pcm_hw = {
44 .info = SNDRV_PCM_INFO_INTERLEAVED |
46 SNDRV_PCM_INFO_RESUME |
48 .formats = SNDRV_PCM_FMTBIT_S16_LE |
49 SNDRV_PCM_FMTBIT_U16_LE |
50 SNDRV_PCM_FMTBIT_S16_BE |
51 SNDRV_PCM_FMTBIT_U16_BE,
52 .rates = SNDRV_PCM_RATE_KNOT,
53 .rate_min = UX500_PLATFORM_MIN_RATE,
54 .rate_max = UX500_PLATFORM_MAX_RATE,
55 .channels_min = UX500_PLATFORM_MIN_CHANNELS,
56 .channels_max = UX500_PLATFORM_MAX_CHANNELS,
57 .buffer_bytes_max = UX500_PLATFORM_BUFFER_BYTES_MAX,
58 .period_bytes_min = UX500_PLATFORM_PERIODS_BYTES_MIN,
59 .period_bytes_max = UX500_PLATFORM_PERIODS_BYTES_MAX,
60 .periods_min = UX500_PLATFORM_PERIODS_MIN,
61 .periods_max = UX500_PLATFORM_PERIODS_MAX,
64 static void ux500_pcm_dma_hw_free(struct device *dev,
65 struct snd_pcm_substream *substream)
67 struct snd_pcm_runtime *runtime = substream->runtime;
68 struct snd_dma_buffer *buf = runtime->dma_buffer_p;
70 if (runtime->dma_area == NULL)
73 if (buf != &substream->dma_buffer) {
74 dma_free_coherent(buf->dev.dev, buf->bytes, buf->area,
76 kfree(runtime->dma_buffer_p);
79 snd_pcm_set_runtime_buffer(substream, NULL);
82 static int ux500_pcm_open(struct snd_pcm_substream *substream)
84 struct snd_soc_pcm_runtime *rtd = substream->private_data;
85 struct snd_soc_dai *dai = rtd->cpu_dai;
86 struct device *dev = dai->dev;
88 struct ux500_msp_dma_params *dma_params;
89 u16 per_data_width, mem_data_width;
90 struct stedma40_chan_cfg *dma_cfg;
92 dev_dbg(dev, "%s: MSP %d (%s): Enter.\n", __func__, dai->id,
93 snd_pcm_stream_str(substream));
95 dev_dbg(dev, "%s: Set runtime hwparams.\n", __func__);
96 snd_soc_set_runtime_hwparams(substream, &ux500_pcm_hw);
98 mem_data_width = STEDMA40_HALFWORD_WIDTH;
100 dma_params = snd_soc_dai_get_dma_data(dai, substream);
101 switch (dma_params->data_size) {
103 per_data_width = STEDMA40_WORD_WIDTH;
106 per_data_width = STEDMA40_HALFWORD_WIDTH;
109 per_data_width = STEDMA40_BYTE_WIDTH;
112 per_data_width = STEDMA40_WORD_WIDTH;
113 dev_warn(rtd->platform->dev,
114 "%s: Unknown data-size (%d)! Assuming 32 bits.\n",
115 __func__, dma_params->data_size);
118 dma_cfg = dma_params->dma_cfg;
120 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
121 dma_cfg->src_info.data_width = mem_data_width;
122 dma_cfg->dst_info.data_width = per_data_width;
124 dma_cfg->src_info.data_width = per_data_width;
125 dma_cfg->dst_info.data_width = mem_data_width;
129 ret = snd_dmaengine_pcm_open(substream, stedma40_filter, dma_cfg);
132 "%s: ERROR: snd_dmaengine_pcm_open failed (%d)!\n",
140 static int ux500_pcm_hw_params(struct snd_pcm_substream *substream,
141 struct snd_pcm_hw_params *hw_params)
143 struct snd_pcm_runtime *runtime = substream->runtime;
144 struct snd_dma_buffer *buf = runtime->dma_buffer_p;
145 struct snd_soc_pcm_runtime *rtd = substream->private_data;
149 dev_dbg(rtd->platform->dev, "%s: Enter\n", __func__);
151 size = params_buffer_bytes(hw_params);
154 if (buf->bytes >= size)
156 ux500_pcm_dma_hw_free(NULL, substream);
159 if (substream->dma_buffer.area != NULL &&
160 substream->dma_buffer.bytes >= size) {
161 buf = &substream->dma_buffer;
163 buf = kmalloc(sizeof(struct snd_dma_buffer), GFP_KERNEL);
167 buf->dev.type = SNDRV_DMA_TYPE_DEV;
169 buf->area = dma_alloc_coherent(NULL, size, &buf->addr,
172 buf->private_data = NULL;
177 snd_pcm_set_runtime_buffer(substream, buf);
180 runtime->dma_bytes = size;
189 static int ux500_pcm_hw_free(struct snd_pcm_substream *substream)
191 struct snd_soc_pcm_runtime *rtd = substream->private_data;
193 dev_dbg(rtd->platform->dev, "%s: Enter\n", __func__);
195 ux500_pcm_dma_hw_free(NULL, substream);
200 static int ux500_pcm_mmap(struct snd_pcm_substream *substream,
201 struct vm_area_struct *vma)
203 struct snd_pcm_runtime *runtime = substream->runtime;
204 struct snd_soc_pcm_runtime *rtd = substream->private_data;
206 dev_dbg(rtd->platform->dev, "%s: Enter.\n", __func__);
208 return dma_mmap_coherent(NULL, vma, runtime->dma_area,
209 runtime->dma_addr, runtime->dma_bytes);
212 static struct snd_pcm_ops ux500_pcm_ops = {
213 .open = ux500_pcm_open,
214 .close = snd_dmaengine_pcm_close,
215 .ioctl = snd_pcm_lib_ioctl,
216 .hw_params = ux500_pcm_hw_params,
217 .hw_free = ux500_pcm_hw_free,
218 .trigger = snd_dmaengine_pcm_trigger,
219 .pointer = snd_dmaengine_pcm_pointer_no_residue,
220 .mmap = ux500_pcm_mmap
223 int ux500_pcm_new(struct snd_soc_pcm_runtime *rtd)
225 struct snd_pcm *pcm = rtd->pcm;
227 dev_dbg(rtd->platform->dev, "%s: Enter (id = '%s').\n", __func__,
235 static struct snd_soc_platform_driver ux500_pcm_soc_drv = {
236 .ops = &ux500_pcm_ops,
237 .pcm_new = ux500_pcm_new,
240 int ux500_pcm_register_platform(struct platform_device *pdev)
244 ret = snd_soc_register_platform(&pdev->dev, &ux500_pcm_soc_drv);
247 "%s: ERROR: Failed to register platform '%s' (%d)!\n",
248 __func__, pdev->name, ret);
254 EXPORT_SYMBOL_GPL(ux500_pcm_register_platform);
256 int ux500_pcm_unregister_platform(struct platform_device *pdev)
258 snd_soc_unregister_platform(&pdev->dev);
262 EXPORT_SYMBOL_GPL(ux500_pcm_unregister_platform);