09b5364e5095f03af167f2564f59405f8bea6662
[linux-2.6-block.git] / sound / soc / ux500 / ux500_pcm.c
1 /*
2  * Copyright (C) ST-Ericsson SA 2012
3  *
4  * Author: Ola Lilja <ola.o.lilja@stericsson.com>,
5  *         Roger Nilsson <roger.xr.nilsson@stericsson.com>
6  *         for ST-Ericsson.
7  *
8  * License terms:
9  *
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.
13  */
14
15 #include <asm/page.h>
16
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>
22
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/dmaengine_pcm.h>
27
28 #include "ux500_msp_i2s.h"
29 #include "ux500_pcm.h"
30
31 #define UX500_PLATFORM_MIN_RATE 8000
32 #define UX500_PLATFORM_MAX_RATE 48000
33
34 #define UX500_PLATFORM_MIN_CHANNELS 1
35 #define UX500_PLATFORM_MAX_CHANNELS 8
36
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)
42
43 static struct snd_pcm_hardware ux500_pcm_hw = {
44         .info = SNDRV_PCM_INFO_INTERLEAVED |
45                 SNDRV_PCM_INFO_MMAP |
46                 SNDRV_PCM_INFO_RESUME |
47                 SNDRV_PCM_INFO_PAUSE,
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,
62 };
63
64 static void ux500_pcm_dma_hw_free(struct device *dev,
65                                 struct snd_pcm_substream *substream)
66 {
67         struct snd_pcm_runtime *runtime = substream->runtime;
68         struct snd_dma_buffer *buf = runtime->dma_buffer_p;
69
70         if (runtime->dma_area == NULL)
71                 return;
72
73         if (buf != &substream->dma_buffer) {
74                 dma_free_coherent(buf->dev.dev, buf->bytes, buf->area,
75                                 buf->addr);
76                 kfree(runtime->dma_buffer_p);
77         }
78
79         snd_pcm_set_runtime_buffer(substream, NULL);
80 }
81
82 static int ux500_pcm_open(struct snd_pcm_substream *substream)
83 {
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;
87         int ret;
88         struct ux500_msp_dma_params *dma_params;
89         u16 per_data_width, mem_data_width;
90         struct stedma40_chan_cfg *dma_cfg;
91
92         dev_dbg(dev, "%s: MSP %d (%s): Enter.\n", __func__, dai->id,
93                 snd_pcm_stream_str(substream));
94
95         dev_dbg(dev, "%s: Set runtime hwparams.\n", __func__);
96         snd_soc_set_runtime_hwparams(substream, &ux500_pcm_hw);
97
98         mem_data_width = STEDMA40_HALFWORD_WIDTH;
99
100         dma_params = snd_soc_dai_get_dma_data(dai, substream);
101         switch (dma_params->data_size) {
102         case 32:
103                 per_data_width = STEDMA40_WORD_WIDTH;
104                 break;
105         case 16:
106                 per_data_width = STEDMA40_HALFWORD_WIDTH;
107                 break;
108         case 8:
109                 per_data_width = STEDMA40_BYTE_WIDTH;
110                 break;
111         default:
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);
116         }
117
118         dma_cfg = dma_params->dma_cfg;
119
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;
123         } else {
124                 dma_cfg->src_info.data_width = per_data_width;
125                 dma_cfg->dst_info.data_width = mem_data_width;
126         }
127
128
129         ret = snd_dmaengine_pcm_open(substream, stedma40_filter, dma_cfg);
130         if (ret) {
131                 dev_dbg(dai->dev,
132                         "%s: ERROR: snd_dmaengine_pcm_open failed (%d)!\n",
133                         __func__, ret);
134                 return ret;
135         }
136
137         return 0;
138 }
139
140 static int ux500_pcm_hw_params(struct snd_pcm_substream *substream,
141                         struct snd_pcm_hw_params *hw_params)
142 {
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;
146         int ret = 0;
147         int size;
148
149         dev_dbg(rtd->platform->dev, "%s: Enter\n", __func__);
150
151         size = params_buffer_bytes(hw_params);
152
153         if (buf) {
154                 if (buf->bytes >= size)
155                         goto out;
156                 ux500_pcm_dma_hw_free(NULL, substream);
157         }
158
159         if (substream->dma_buffer.area != NULL &&
160                 substream->dma_buffer.bytes >= size) {
161                 buf = &substream->dma_buffer;
162         } else {
163                 buf = kmalloc(sizeof(struct snd_dma_buffer), GFP_KERNEL);
164                 if (!buf)
165                         goto nomem;
166
167                 buf->dev.type = SNDRV_DMA_TYPE_DEV;
168                 buf->dev.dev = NULL;
169                 buf->area = dma_alloc_coherent(NULL, size, &buf->addr,
170                                         GFP_KERNEL);
171                 buf->bytes = size;
172                 buf->private_data = NULL;
173
174                 if (!buf->area)
175                         goto free;
176         }
177         snd_pcm_set_runtime_buffer(substream, buf);
178         ret = 1;
179  out:
180         runtime->dma_bytes = size;
181         return ret;
182
183  free:
184         kfree(buf);
185  nomem:
186         return -ENOMEM;
187 }
188
189 static int ux500_pcm_hw_free(struct snd_pcm_substream *substream)
190 {
191         struct snd_soc_pcm_runtime *rtd = substream->private_data;
192
193         dev_dbg(rtd->platform->dev, "%s: Enter\n", __func__);
194
195         ux500_pcm_dma_hw_free(NULL, substream);
196
197         return 0;
198 }
199
200 static int ux500_pcm_mmap(struct snd_pcm_substream *substream,
201                         struct vm_area_struct *vma)
202 {
203         struct snd_pcm_runtime *runtime = substream->runtime;
204         struct snd_soc_pcm_runtime *rtd = substream->private_data;
205
206         dev_dbg(rtd->platform->dev, "%s: Enter.\n", __func__);
207
208         return dma_mmap_coherent(NULL, vma, runtime->dma_area,
209                                 runtime->dma_addr, runtime->dma_bytes);
210 }
211
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
221 };
222
223 int ux500_pcm_new(struct snd_soc_pcm_runtime *rtd)
224 {
225         struct snd_pcm *pcm = rtd->pcm;
226
227         dev_dbg(rtd->platform->dev, "%s: Enter (id = '%s').\n", __func__,
228                 pcm->id);
229
230         pcm->info_flags = 0;
231
232         return 0;
233 }
234
235 static struct snd_soc_platform_driver ux500_pcm_soc_drv = {
236         .ops            = &ux500_pcm_ops,
237         .pcm_new        = ux500_pcm_new,
238 };
239
240 int ux500_pcm_register_platform(struct platform_device *pdev)
241 {
242         int ret;
243
244         ret = snd_soc_register_platform(&pdev->dev, &ux500_pcm_soc_drv);
245         if (ret < 0) {
246                 dev_err(&pdev->dev,
247                         "%s: ERROR: Failed to register platform '%s' (%d)!\n",
248                         __func__, pdev->name, ret);
249                 return ret;
250         }
251
252         return 0;
253 }
254 EXPORT_SYMBOL_GPL(ux500_pcm_register_platform);
255
256 int ux500_pcm_unregister_platform(struct platform_device *pdev)
257 {
258         snd_soc_unregister_platform(&pdev->dev);
259
260         return 0;
261 }
262 EXPORT_SYMBOL_GPL(ux500_pcm_unregister_platform);