blob: 968656ceb39e0f8e93f497f856bfd5947ad92cf4 [file] [log] [blame]
Vinod Kould927fda2011-01-04 20:16:32 +05301/*
2 * sst_platform.c - Intel MID Platform driver
3 *
Vinod Koul0cd57512013-03-29 23:41:42 +05304 * Copyright (C) 2010-2013 Intel Corp
Vinod Kould927fda2011-01-04 20:16:32 +05305 * Author: Vinod Koul <vinod.koul@intel.com>
6 * Author: Harsha Priya <priya.harsha@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 *
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 *
24 *
25 */
26#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
28#include <linux/slab.h>
29#include <linux/io.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040030#include <linux/module.h>
Vinod Kould927fda2011-01-04 20:16:32 +053031#include <sound/core.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/soc.h>
Vinod Koulc514a912012-08-16 17:10:42 +053035#include <sound/compress_driver.h>
Vinod Kould927fda2011-01-04 20:16:32 +053036#include "sst_platform.h"
37
Vinod Koul03c33042011-12-05 19:13:41 +053038static struct sst_device *sst;
39static DEFINE_MUTEX(sst_lock);
40
41int sst_register_dsp(struct sst_device *dev)
42{
43 BUG_ON(!dev);
44 if (!try_module_get(dev->dev->driver->owner))
45 return -ENODEV;
46 mutex_lock(&sst_lock);
47 if (sst) {
48 pr_err("we already have a device %s\n", sst->name);
49 module_put(dev->dev->driver->owner);
50 mutex_unlock(&sst_lock);
51 return -EEXIST;
52 }
53 pr_debug("registering device %s\n", dev->name);
54 sst = dev;
55 mutex_unlock(&sst_lock);
56 return 0;
57}
58EXPORT_SYMBOL_GPL(sst_register_dsp);
59
60int sst_unregister_dsp(struct sst_device *dev)
61{
62 BUG_ON(!dev);
63 if (dev != sst)
64 return -EINVAL;
65
66 mutex_lock(&sst_lock);
67
68 if (!sst) {
69 mutex_unlock(&sst_lock);
70 return -EIO;
71 }
72
73 module_put(sst->dev->driver->owner);
74 pr_debug("unreg %s\n", sst->name);
75 sst = NULL;
76 mutex_unlock(&sst_lock);
77 return 0;
78}
79EXPORT_SYMBOL_GPL(sst_unregister_dsp);
80
Vinod Kould927fda2011-01-04 20:16:32 +053081static struct snd_pcm_hardware sst_platform_pcm_hw = {
82 .info = (SNDRV_PCM_INFO_INTERLEAVED |
83 SNDRV_PCM_INFO_DOUBLE |
84 SNDRV_PCM_INFO_PAUSE |
85 SNDRV_PCM_INFO_RESUME |
86 SNDRV_PCM_INFO_MMAP|
87 SNDRV_PCM_INFO_MMAP_VALID |
88 SNDRV_PCM_INFO_BLOCK_TRANSFER |
89 SNDRV_PCM_INFO_SYNC_START),
90 .formats = (SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_U16 |
91 SNDRV_PCM_FMTBIT_S24 | SNDRV_PCM_FMTBIT_U24 |
92 SNDRV_PCM_FMTBIT_S32 | SNDRV_PCM_FMTBIT_U32),
93 .rates = (SNDRV_PCM_RATE_8000|
94 SNDRV_PCM_RATE_44100 |
95 SNDRV_PCM_RATE_48000),
96 .rate_min = SST_MIN_RATE,
97 .rate_max = SST_MAX_RATE,
98 .channels_min = SST_MIN_CHANNEL,
99 .channels_max = SST_MAX_CHANNEL,
100 .buffer_bytes_max = SST_MAX_BUFFER,
101 .period_bytes_min = SST_MIN_PERIOD_BYTES,
102 .period_bytes_max = SST_MAX_PERIOD_BYTES,
103 .periods_min = SST_MIN_PERIODS,
104 .periods_max = SST_MAX_PERIODS,
105 .fifo_size = SST_FIFO_SIZE,
106};
107
108/* MFLD - MSIC */
Axel Lind1b73282011-09-27 10:38:50 +0800109static struct snd_soc_dai_driver sst_platform_dai[] = {
Vinod Kould927fda2011-01-04 20:16:32 +0530110{
111 .name = "Headset-cpu-dai",
112 .id = 0,
113 .playback = {
114 .channels_min = SST_STEREO,
115 .channels_max = SST_STEREO,
116 .rates = SNDRV_PCM_RATE_48000,
117 .formats = SNDRV_PCM_FMTBIT_S24_LE,
118 },
Harsha Priyaa7bffdf2011-01-28 22:27:26 +0530119 .capture = {
120 .channels_min = 1,
121 .channels_max = 5,
122 .rates = SNDRV_PCM_RATE_48000,
123 .formats = SNDRV_PCM_FMTBIT_S24_LE,
124 },
Vinod Kould927fda2011-01-04 20:16:32 +0530125},
126{
127 .name = "Speaker-cpu-dai",
128 .id = 1,
129 .playback = {
130 .channels_min = SST_MONO,
131 .channels_max = SST_STEREO,
132 .rates = SNDRV_PCM_RATE_48000,
133 .formats = SNDRV_PCM_FMTBIT_S24_LE,
134 },
135},
136{
137 .name = "Vibra1-cpu-dai",
138 .id = 2,
139 .playback = {
140 .channels_min = SST_MONO,
141 .channels_max = SST_MONO,
142 .rates = SNDRV_PCM_RATE_48000,
143 .formats = SNDRV_PCM_FMTBIT_S24_LE,
144 },
145},
146{
147 .name = "Vibra2-cpu-dai",
148 .id = 3,
149 .playback = {
150 .channels_min = SST_MONO,
151 .channels_max = SST_STEREO,
152 .rates = SNDRV_PCM_RATE_48000,
153 .formats = SNDRV_PCM_FMTBIT_S24_LE,
154 },
155},
Vinod Koulc514a912012-08-16 17:10:42 +0530156{
157 .name = "Compress-cpu-dai",
158 .compress_dai = 1,
159 .playback = {
160 .channels_min = SST_STEREO,
161 .channels_max = SST_STEREO,
162 .rates = SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000,
163 .formats = SNDRV_PCM_FMTBIT_S16_LE,
164 },
165},
Vinod Kould927fda2011-01-04 20:16:32 +0530166};
Harsha Priya5c685362011-01-11 14:50:35 +0530167
Vinod Kould927fda2011-01-04 20:16:32 +0530168/* helper functions */
Harsha Priya5c685362011-01-11 14:50:35 +0530169static inline void sst_set_stream_status(struct sst_runtime_stream *stream,
170 int state)
171{
Lu Guanqund89b0a12011-04-08 15:38:48 +0800172 unsigned long flags;
173 spin_lock_irqsave(&stream->status_lock, flags);
Harsha Priya5c685362011-01-11 14:50:35 +0530174 stream->stream_status = state;
Lu Guanqund89b0a12011-04-08 15:38:48 +0800175 spin_unlock_irqrestore(&stream->status_lock, flags);
Harsha Priya5c685362011-01-11 14:50:35 +0530176}
177
178static inline int sst_get_stream_status(struct sst_runtime_stream *stream)
179{
180 int state;
Lu Guanqund89b0a12011-04-08 15:38:48 +0800181 unsigned long flags;
Harsha Priya5c685362011-01-11 14:50:35 +0530182
Lu Guanqund89b0a12011-04-08 15:38:48 +0800183 spin_lock_irqsave(&stream->status_lock, flags);
Harsha Priya5c685362011-01-11 14:50:35 +0530184 state = stream->stream_status;
Lu Guanqund89b0a12011-04-08 15:38:48 +0800185 spin_unlock_irqrestore(&stream->status_lock, flags);
Harsha Priya5c685362011-01-11 14:50:35 +0530186 return state;
187}
188
189static void sst_fill_pcm_params(struct snd_pcm_substream *substream,
Vinod Koul03c33042011-12-05 19:13:41 +0530190 struct sst_pcm_params *param)
Harsha Priya5c685362011-01-11 14:50:35 +0530191{
192
Vinod Koul03c33042011-12-05 19:13:41 +0530193 param->codec = SST_CODEC_TYPE_PCM;
194 param->num_chan = (u8) substream->runtime->channels;
195 param->pcm_wd_sz = substream->runtime->sample_bits;
196 param->reserved = 0;
197 param->sfreq = substream->runtime->rate;
198 param->ring_buffer_size = snd_pcm_lib_buffer_bytes(substream);
199 param->period_count = substream->runtime->period_size;
200 param->ring_buffer_addr = virt_to_phys(substream->dma_buffer.area);
201 pr_debug("period_cnt = %d\n", param->period_count);
202 pr_debug("sfreq= %d, wd_sz = %d\n", param->sfreq, param->pcm_wd_sz);
Harsha Priya5c685362011-01-11 14:50:35 +0530203}
204
Vinod Kould927fda2011-01-04 20:16:32 +0530205static int sst_platform_alloc_stream(struct snd_pcm_substream *substream)
206{
207 struct sst_runtime_stream *stream =
208 substream->runtime->private_data;
Vinod Koul03c33042011-12-05 19:13:41 +0530209 struct sst_pcm_params param = {0};
210 struct sst_stream_params str_params = {0};
Vinod Kould927fda2011-01-04 20:16:32 +0530211 int ret_val;
212
213 /* set codec params and inform SST driver the same */
Harsha Priya5c685362011-01-11 14:50:35 +0530214 sst_fill_pcm_params(substream, &param);
Vinod Kould927fda2011-01-04 20:16:32 +0530215 substream->runtime->dma_area = substream->dma_buffer.area;
Vinod Kould927fda2011-01-04 20:16:32 +0530216 str_params.sparams = param;
Vinod Koul03c33042011-12-05 19:13:41 +0530217 str_params.codec = param.codec;
Vinod Kould927fda2011-01-04 20:16:32 +0530218 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
219 str_params.ops = STREAM_OPS_PLAYBACK;
220 str_params.device_type = substream->pcm->device + 1;
221 pr_debug("Playbck stream,Device %d\n",
222 substream->pcm->device);
223 } else {
224 str_params.ops = STREAM_OPS_CAPTURE;
225 str_params.device_type = SND_SST_DEVICE_CAPTURE;
226 pr_debug("Capture stream,Device %d\n",
227 substream->pcm->device);
228 }
Vinod Koul03c33042011-12-05 19:13:41 +0530229 ret_val = stream->ops->open(&str_params);
Vinod Kould927fda2011-01-04 20:16:32 +0530230 pr_debug("SST_SND_PLAY/CAPTURE ret_val = %x\n", ret_val);
231 if (ret_val < 0)
232 return ret_val;
233
234 stream->stream_info.str_id = ret_val;
235 pr_debug("str id : %d\n", stream->stream_info.str_id);
Vinod Kould927fda2011-01-04 20:16:32 +0530236 return ret_val;
237}
238
Vinod Kould927fda2011-01-04 20:16:32 +0530239static void sst_period_elapsed(void *mad_substream)
240{
241 struct snd_pcm_substream *substream = mad_substream;
242 struct sst_runtime_stream *stream;
Harsha Priya5c685362011-01-11 14:50:35 +0530243 int status;
Vinod Kould927fda2011-01-04 20:16:32 +0530244
245 if (!substream || !substream->runtime)
246 return;
247 stream = substream->runtime->private_data;
248 if (!stream)
249 return;
Harsha Priya5c685362011-01-11 14:50:35 +0530250 status = sst_get_stream_status(stream);
251 if (status != SST_PLATFORM_RUNNING)
Vinod Kould927fda2011-01-04 20:16:32 +0530252 return;
Vinod Kould927fda2011-01-04 20:16:32 +0530253 snd_pcm_period_elapsed(substream);
Vinod Kould927fda2011-01-04 20:16:32 +0530254}
255
256static int sst_platform_init_stream(struct snd_pcm_substream *substream)
257{
258 struct sst_runtime_stream *stream =
259 substream->runtime->private_data;
260 int ret_val;
261
262 pr_debug("setting buffer ptr param\n");
Harsha Priya5c685362011-01-11 14:50:35 +0530263 sst_set_stream_status(stream, SST_PLATFORM_INIT);
Vinod Kould927fda2011-01-04 20:16:32 +0530264 stream->stream_info.period_elapsed = sst_period_elapsed;
265 stream->stream_info.mad_substream = substream;
266 stream->stream_info.buffer_ptr = 0;
267 stream->stream_info.sfreq = substream->runtime->rate;
Vinod Koul03c33042011-12-05 19:13:41 +0530268 ret_val = stream->ops->device_control(
Harsha Priyaa2117012011-01-11 14:50:59 +0530269 SST_SND_STREAM_INIT, &stream->stream_info);
Vinod Kould927fda2011-01-04 20:16:32 +0530270 if (ret_val)
271 pr_err("control_set ret error %d\n", ret_val);
272 return ret_val;
273
274}
275/* end -- helper functions */
276
277static int sst_platform_open(struct snd_pcm_substream *substream)
278{
Lu Guanqun22be5042011-09-06 15:21:38 +0800279 struct snd_pcm_runtime *runtime = substream->runtime;
Vinod Kould927fda2011-01-04 20:16:32 +0530280 struct sst_runtime_stream *stream;
Joerg Roedelc45471ea2011-12-15 18:24:54 +0100281 int ret_val;
Vinod Kould927fda2011-01-04 20:16:32 +0530282
283 pr_debug("sst_platform_open called\n");
Lu Guanqun22be5042011-09-06 15:21:38 +0800284
285 snd_soc_set_runtime_hwparams(substream, &sst_platform_pcm_hw);
Lu Guanqun283e42e2011-09-06 15:21:43 +0800286 ret_val = snd_pcm_hw_constraint_integer(runtime,
287 SNDRV_PCM_HW_PARAM_PERIODS);
288 if (ret_val < 0)
289 return ret_val;
Lu Guanqun22be5042011-09-06 15:21:38 +0800290
Vinod Kould927fda2011-01-04 20:16:32 +0530291 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
292 if (!stream)
293 return -ENOMEM;
Vinod Kould927fda2011-01-04 20:16:32 +0530294 spin_lock_init(&stream->status_lock);
Vinod Koul03c33042011-12-05 19:13:41 +0530295
296 /* get the sst ops */
297 mutex_lock(&sst_lock);
298 if (!sst) {
299 pr_err("no device available to run\n");
300 mutex_unlock(&sst_lock);
301 kfree(stream);
302 return -ENODEV;
303 }
304 if (!try_module_get(sst->dev->driver->owner)) {
305 mutex_unlock(&sst_lock);
306 kfree(stream);
307 return -ENODEV;
308 }
309 stream->ops = sst->ops;
310 mutex_unlock(&sst_lock);
311
Vinod Kould927fda2011-01-04 20:16:32 +0530312 stream->stream_info.str_id = 0;
Harsha Priya5c685362011-01-11 14:50:35 +0530313 sst_set_stream_status(stream, SST_PLATFORM_INIT);
Vinod Kould927fda2011-01-04 20:16:32 +0530314 stream->stream_info.mad_substream = substream;
315 /* allocate memory for SST API set */
Vinod Kould927fda2011-01-04 20:16:32 +0530316 runtime->private_data = stream;
Lu Guanqun283e42e2011-09-06 15:21:43 +0800317
318 return 0;
Vinod Kould927fda2011-01-04 20:16:32 +0530319}
320
321static int sst_platform_close(struct snd_pcm_substream *substream)
322{
323 struct sst_runtime_stream *stream;
324 int ret_val = 0, str_id;
325
326 pr_debug("sst_platform_close called\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530327 stream = substream->runtime->private_data;
328 str_id = stream->stream_info.str_id;
Vinod Kould927fda2011-01-04 20:16:32 +0530329 if (str_id)
Vinod Koul03c33042011-12-05 19:13:41 +0530330 ret_val = stream->ops->close(str_id);
331 module_put(sst->dev->driver->owner);
Vinod Kould927fda2011-01-04 20:16:32 +0530332 kfree(stream);
333 return ret_val;
334}
335
336static int sst_platform_pcm_prepare(struct snd_pcm_substream *substream)
337{
338 struct sst_runtime_stream *stream;
339 int ret_val = 0, str_id;
340
341 pr_debug("sst_platform_pcm_prepare called\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530342 stream = substream->runtime->private_data;
343 str_id = stream->stream_info.str_id;
344 if (stream->stream_info.str_id) {
Vinod Koul03c33042011-12-05 19:13:41 +0530345 ret_val = stream->ops->device_control(
346 SST_SND_DROP, &str_id);
Vinod Kould927fda2011-01-04 20:16:32 +0530347 return ret_val;
348 }
349
350 ret_val = sst_platform_alloc_stream(substream);
351 if (ret_val < 0)
352 return ret_val;
353 snprintf(substream->pcm->id, sizeof(substream->pcm->id),
354 "%d", stream->stream_info.str_id);
355
356 ret_val = sst_platform_init_stream(substream);
357 if (ret_val)
358 return ret_val;
359 substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
360 return ret_val;
361}
362
363static int sst_platform_pcm_trigger(struct snd_pcm_substream *substream,
364 int cmd)
365{
366 int ret_val = 0, str_id;
367 struct sst_runtime_stream *stream;
Harsha Priyaa2117012011-01-11 14:50:59 +0530368 int str_cmd, status;
Vinod Kould927fda2011-01-04 20:16:32 +0530369
370 pr_debug("sst_platform_pcm_trigger called\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530371 stream = substream->runtime->private_data;
Vinod Kould927fda2011-01-04 20:16:32 +0530372 str_id = stream->stream_info.str_id;
Vinod Kould927fda2011-01-04 20:16:32 +0530373 switch (cmd) {
374 case SNDRV_PCM_TRIGGER_START:
375 pr_debug("sst: Trigger Start\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530376 str_cmd = SST_SND_START;
377 status = SST_PLATFORM_RUNNING;
Vinod Kould927fda2011-01-04 20:16:32 +0530378 stream->stream_info.mad_substream = substream;
379 break;
380 case SNDRV_PCM_TRIGGER_STOP:
381 pr_debug("sst: in stop\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530382 str_cmd = SST_SND_DROP;
383 status = SST_PLATFORM_DROPPED;
Vinod Kould927fda2011-01-04 20:16:32 +0530384 break;
385 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
386 pr_debug("sst: in pause\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530387 str_cmd = SST_SND_PAUSE;
388 status = SST_PLATFORM_PAUSED;
Vinod Kould927fda2011-01-04 20:16:32 +0530389 break;
390 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
391 pr_debug("sst: in pause release\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530392 str_cmd = SST_SND_RESUME;
393 status = SST_PLATFORM_RUNNING;
Vinod Kould927fda2011-01-04 20:16:32 +0530394 break;
395 default:
Harsha Priyaa2117012011-01-11 14:50:59 +0530396 return -EINVAL;
Vinod Kould927fda2011-01-04 20:16:32 +0530397 }
Vinod Koul03c33042011-12-05 19:13:41 +0530398 ret_val = stream->ops->device_control(str_cmd, &str_id);
Harsha Priyaa2117012011-01-11 14:50:59 +0530399 if (!ret_val)
400 sst_set_stream_status(stream, status);
401
Vinod Kould927fda2011-01-04 20:16:32 +0530402 return ret_val;
403}
404
405
406static snd_pcm_uframes_t sst_platform_pcm_pointer
407 (struct snd_pcm_substream *substream)
408{
409 struct sst_runtime_stream *stream;
Harsha Priya5c685362011-01-11 14:50:35 +0530410 int ret_val, status;
Vinod Kould927fda2011-01-04 20:16:32 +0530411 struct pcm_stream_info *str_info;
412
Vinod Kould927fda2011-01-04 20:16:32 +0530413 stream = substream->runtime->private_data;
Harsha Priya5c685362011-01-11 14:50:35 +0530414 status = sst_get_stream_status(stream);
415 if (status == SST_PLATFORM_INIT)
Vinod Kould927fda2011-01-04 20:16:32 +0530416 return 0;
Vinod Kould927fda2011-01-04 20:16:32 +0530417 str_info = &stream->stream_info;
Vinod Koul03c33042011-12-05 19:13:41 +0530418 ret_val = stream->ops->device_control(
Vinod Kould927fda2011-01-04 20:16:32 +0530419 SST_SND_BUFFER_POINTER, str_info);
420 if (ret_val) {
421 pr_err("sst: error code = %d\n", ret_val);
422 return ret_val;
423 }
Vinod Kould927fda2011-01-04 20:16:32 +0530424 return stream->stream_info.buffer_ptr;
425}
426
Vinod Koul5b499f82011-02-15 18:28:54 +0530427static int sst_platform_pcm_hw_params(struct snd_pcm_substream *substream,
428 struct snd_pcm_hw_params *params)
429{
430 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
431 memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
432
433 return 0;
434}
Vinod Kould927fda2011-01-04 20:16:32 +0530435
xingchao9ab88432011-04-27 16:58:54 -0400436static int sst_platform_pcm_hw_free(struct snd_pcm_substream *substream)
437{
438 return snd_pcm_lib_free_pages(substream);
439}
440
Vinod Kould927fda2011-01-04 20:16:32 +0530441static struct snd_pcm_ops sst_platform_ops = {
442 .open = sst_platform_open,
443 .close = sst_platform_close,
444 .ioctl = snd_pcm_lib_ioctl,
445 .prepare = sst_platform_pcm_prepare,
446 .trigger = sst_platform_pcm_trigger,
447 .pointer = sst_platform_pcm_pointer,
Vinod Koul5b499f82011-02-15 18:28:54 +0530448 .hw_params = sst_platform_pcm_hw_params,
xingchao9ab88432011-04-27 16:58:54 -0400449 .hw_free = sst_platform_pcm_hw_free,
Vinod Kould927fda2011-01-04 20:16:32 +0530450};
451
452static void sst_pcm_free(struct snd_pcm *pcm)
453{
454 pr_debug("sst_pcm_free called\n");
455 snd_pcm_lib_preallocate_free_for_all(pcm);
456}
457
Axel Lin8faa8c12011-12-13 17:13:45 +0800458static int sst_pcm_new(struct snd_soc_pcm_runtime *rtd)
Vinod Kould927fda2011-01-04 20:16:32 +0530459{
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100460 struct snd_pcm *pcm = rtd->pcm;
Vinod Kould927fda2011-01-04 20:16:32 +0530461 int retval = 0;
462
463 pr_debug("sst_pcm_new called\n");
Joachim Eastwood25e9e752012-01-01 01:58:44 +0100464 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
465 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
Vinod Kould927fda2011-01-04 20:16:32 +0530466 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
467 SNDRV_DMA_TYPE_CONTINUOUS,
468 snd_dma_continuous_data(GFP_KERNEL),
469 SST_MIN_BUFFER, SST_MAX_BUFFER);
470 if (retval) {
471 pr_err("dma buffer allocationf fail\n");
472 return retval;
473 }
474 }
Vinod Kould927fda2011-01-04 20:16:32 +0530475 return retval;
476}
Vinod Koulc514a912012-08-16 17:10:42 +0530477
478/* compress stream operations */
479static void sst_compr_fragment_elapsed(void *arg)
480{
481 struct snd_compr_stream *cstream = (struct snd_compr_stream *)arg;
482
483 pr_debug("fragment elapsed by driver\n");
484 if (cstream)
485 snd_compr_fragment_elapsed(cstream);
486}
487
488static int sst_platform_compr_open(struct snd_compr_stream *cstream)
489{
490
491 int ret_val = 0;
492 struct snd_compr_runtime *runtime = cstream->runtime;
493 struct sst_runtime_stream *stream;
494
495 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
496 if (!stream)
497 return -ENOMEM;
498
499 spin_lock_init(&stream->status_lock);
500
501 /* get the sst ops */
502 if (!sst || !try_module_get(sst->dev->driver->owner)) {
503 pr_err("no device available to run\n");
504 ret_val = -ENODEV;
505 goto out_ops;
506 }
507 stream->compr_ops = sst->compr_ops;
508
509 stream->id = 0;
510 sst_set_stream_status(stream, SST_PLATFORM_INIT);
511 runtime->private_data = stream;
512 return 0;
513out_ops:
514 kfree(stream);
515 return ret_val;
516}
517
518static int sst_platform_compr_free(struct snd_compr_stream *cstream)
519{
520 struct sst_runtime_stream *stream;
521 int ret_val = 0, str_id;
522
523 stream = cstream->runtime->private_data;
524 /*need to check*/
525 str_id = stream->id;
526 if (str_id)
527 ret_val = stream->compr_ops->close(str_id);
528 module_put(sst->dev->driver->owner);
529 kfree(stream);
530 pr_debug("%s: %d\n", __func__, ret_val);
531 return 0;
532}
533
534static int sst_platform_compr_set_params(struct snd_compr_stream *cstream,
535 struct snd_compr_params *params)
536{
537 struct sst_runtime_stream *stream;
538 int retval;
539 struct snd_sst_params str_params;
540 struct sst_compress_cb cb;
541
542 stream = cstream->runtime->private_data;
543 /* construct fw structure for this*/
544 memset(&str_params, 0, sizeof(str_params));
545
546 str_params.ops = STREAM_OPS_PLAYBACK;
547 str_params.stream_type = SST_STREAM_TYPE_MUSIC;
548 str_params.device_type = SND_SST_DEVICE_COMPRESS;
549
550 switch (params->codec.id) {
551 case SND_AUDIOCODEC_MP3: {
552 str_params.codec = SST_CODEC_TYPE_MP3;
553 str_params.sparams.uc.mp3_params.codec = SST_CODEC_TYPE_MP3;
554 str_params.sparams.uc.mp3_params.num_chan = params->codec.ch_in;
555 str_params.sparams.uc.mp3_params.pcm_wd_sz = 16;
556 break;
557 }
558
559 case SND_AUDIOCODEC_AAC: {
560 str_params.codec = SST_CODEC_TYPE_AAC;
561 str_params.sparams.uc.aac_params.codec = SST_CODEC_TYPE_AAC;
562 str_params.sparams.uc.aac_params.num_chan = params->codec.ch_in;
563 str_params.sparams.uc.aac_params.pcm_wd_sz = 16;
564 if (params->codec.format == SND_AUDIOSTREAMFORMAT_MP4ADTS)
565 str_params.sparams.uc.aac_params.bs_format =
566 AAC_BIT_STREAM_ADTS;
567 else if (params->codec.format == SND_AUDIOSTREAMFORMAT_RAW)
568 str_params.sparams.uc.aac_params.bs_format =
569 AAC_BIT_STREAM_RAW;
570 else {
571 pr_err("Undefined format%d\n", params->codec.format);
572 return -EINVAL;
573 }
574 str_params.sparams.uc.aac_params.externalsr =
575 params->codec.sample_rate;
576 break;
577 }
578
579 default:
580 pr_err("codec not supported, id =%d\n", params->codec.id);
581 return -EINVAL;
582 }
583
584 str_params.aparams.ring_buf_info[0].addr =
585 virt_to_phys(cstream->runtime->buffer);
586 str_params.aparams.ring_buf_info[0].size =
587 cstream->runtime->buffer_size;
588 str_params.aparams.sg_count = 1;
589 str_params.aparams.frag_size = cstream->runtime->fragment_size;
590
591 cb.param = cstream;
592 cb.compr_cb = sst_compr_fragment_elapsed;
593
594 retval = stream->compr_ops->open(&str_params, &cb);
595 if (retval < 0) {
596 pr_err("stream allocation failed %d\n", retval);
597 return retval;
598 }
599
600 stream->id = retval;
601 return 0;
602}
603
604static int sst_platform_compr_trigger(struct snd_compr_stream *cstream, int cmd)
605{
606 struct sst_runtime_stream *stream =
607 cstream->runtime->private_data;
608
609 return stream->compr_ops->control(cmd, stream->id);
610}
611
612static int sst_platform_compr_pointer(struct snd_compr_stream *cstream,
613 struct snd_compr_tstamp *tstamp)
614{
615 struct sst_runtime_stream *stream;
616
617 stream = cstream->runtime->private_data;
618 stream->compr_ops->tstamp(stream->id, tstamp);
619 tstamp->byte_offset = tstamp->copied_total %
620 (u32)cstream->runtime->buffer_size;
621 pr_debug("calc bytes offset/copied bytes as %d\n", tstamp->byte_offset);
622 return 0;
623}
624
625static int sst_platform_compr_ack(struct snd_compr_stream *cstream,
626 size_t bytes)
627{
628 struct sst_runtime_stream *stream;
629
630 stream = cstream->runtime->private_data;
631 stream->compr_ops->ack(stream->id, (unsigned long)bytes);
632 stream->bytes_written += bytes;
633
634 return 0;
635}
636
637static int sst_platform_compr_get_caps(struct snd_compr_stream *cstream,
638 struct snd_compr_caps *caps)
639{
640 struct sst_runtime_stream *stream =
641 cstream->runtime->private_data;
642
643 return stream->compr_ops->get_caps(caps);
644}
645
646static int sst_platform_compr_get_codec_caps(struct snd_compr_stream *cstream,
647 struct snd_compr_codec_caps *codec)
648{
649 struct sst_runtime_stream *stream =
650 cstream->runtime->private_data;
651
652 return stream->compr_ops->get_codec_caps(codec);
653}
654
Vinod Koul0cd57512013-03-29 23:41:42 +0530655static int sst_platform_compr_set_metadata(struct snd_compr_stream *cstream,
656 struct snd_compr_metadata *metadata)
657{
658 struct sst_runtime_stream *stream =
659 cstream->runtime->private_data;
660
661 return stream->compr_ops->set_metadata(stream->id, metadata);
662}
663
Vinod Koulc514a912012-08-16 17:10:42 +0530664static struct snd_compr_ops sst_platform_compr_ops = {
665
666 .open = sst_platform_compr_open,
667 .free = sst_platform_compr_free,
668 .set_params = sst_platform_compr_set_params,
Vinod Koul0cd57512013-03-29 23:41:42 +0530669 .set_metadata = sst_platform_compr_set_metadata,
Vinod Koulc514a912012-08-16 17:10:42 +0530670 .trigger = sst_platform_compr_trigger,
671 .pointer = sst_platform_compr_pointer,
672 .ack = sst_platform_compr_ack,
673 .get_caps = sst_platform_compr_get_caps,
674 .get_codec_caps = sst_platform_compr_get_codec_caps,
675};
676
Axel Lin8faa8c12011-12-13 17:13:45 +0800677static struct snd_soc_platform_driver sst_soc_platform_drv = {
Vinod Kould927fda2011-01-04 20:16:32 +0530678 .ops = &sst_platform_ops,
Vinod Koulc514a912012-08-16 17:10:42 +0530679 .compr_ops = &sst_platform_compr_ops,
Vinod Kould927fda2011-01-04 20:16:32 +0530680 .pcm_new = sst_pcm_new,
681 .pcm_free = sst_pcm_free,
682};
683
684static int sst_platform_probe(struct platform_device *pdev)
685{
686 int ret;
687
688 pr_debug("sst_platform_probe called\n");
Vinod Koul03c33042011-12-05 19:13:41 +0530689 sst = NULL;
Vinod Kould927fda2011-01-04 20:16:32 +0530690 ret = snd_soc_register_platform(&pdev->dev, &sst_soc_platform_drv);
691 if (ret) {
692 pr_err("registering soc platform failed\n");
693 return ret;
694 }
Harsha Priya5c685362011-01-11 14:50:35 +0530695
Vinod Kould927fda2011-01-04 20:16:32 +0530696 ret = snd_soc_register_dais(&pdev->dev,
697 sst_platform_dai, ARRAY_SIZE(sst_platform_dai));
698 if (ret) {
699 pr_err("registering cpu dais failed\n");
700 snd_soc_unregister_platform(&pdev->dev);
701 }
702 return ret;
703}
704
705static int sst_platform_remove(struct platform_device *pdev)
706{
707
708 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(sst_platform_dai));
Vinod Kould927fda2011-01-04 20:16:32 +0530709 snd_soc_unregister_platform(&pdev->dev);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300710 pr_debug("sst_platform_remove success\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530711 return 0;
712}
713
714static struct platform_driver sst_platform_driver = {
715 .driver = {
716 .name = "sst-platform",
717 .owner = THIS_MODULE,
718 },
719 .probe = sst_platform_probe,
720 .remove = sst_platform_remove,
721};
722
Axel Lin29515d62011-11-24 11:51:56 +0800723module_platform_driver(sst_platform_driver);
Vinod Kould927fda2011-01-04 20:16:32 +0530724
725MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");
726MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
727MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
728MODULE_LICENSE("GPL v2");
Vinod Koulf5c6b2a2011-01-07 16:20:56 +0530729MODULE_ALIAS("platform:sst-platform");