blob: a89ff7e18e1ae6fd4725215c3c64a7729c28a82e [file] [log] [blame]
Vinod Kould927fda2011-01-04 20:16:32 +05301/*
Mark Browna4b12992014-03-12 23:04:35 +00002 * sst_mfld_platform.c - Intel MID Platform driver
Vinod Kould927fda2011-01-04 20:16:32 +05303 *
Vinod Koul61b165c2014-06-13 18:03:56 +05304 * Copyright (C) 2010-2014 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 *
Vinod Kould927fda2011-01-04 20:16:32 +053018 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Vinod Kould927fda2011-01-04 20:16:32 +053019 */
20#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22#include <linux/slab.h>
23#include <linux/io.h>
Paul Gortmakerda155d52011-07-15 12:38:28 -040024#include <linux/module.h>
Vinod Kould927fda2011-01-04 20:16:32 +053025#include <sound/core.h>
26#include <sound/pcm.h>
27#include <sound/pcm_params.h>
28#include <sound/soc.h>
Vinod Koulc514a912012-08-16 17:10:42 +053029#include <sound/compress_driver.h>
Vinod Koul61b165c2014-06-13 18:03:56 +053030#include <asm/platform_sst_audio.h>
Mark Browna4b12992014-03-12 23:04:35 +000031#include "sst-mfld-platform.h"
Vinod Koul61b165c2014-06-13 18:03:56 +053032#include "sst-atom-controls.h"
Vinod Kould927fda2011-01-04 20:16:32 +053033
Vinod Koul4b68b4e2014-05-05 14:27:50 +053034struct sst_device *sst;
Vinod Koul03c33042011-12-05 19:13:41 +053035static DEFINE_MUTEX(sst_lock);
Vinod Koul4b68b4e2014-05-05 14:27:50 +053036extern struct snd_compr_ops sst_platform_compr_ops;
Vinod Koul03c33042011-12-05 19:13:41 +053037
38int sst_register_dsp(struct sst_device *dev)
39{
Takashi Iwai656a22a2013-11-05 18:40:01 +010040 if (WARN_ON(!dev))
41 return -EINVAL;
Vinod Koul03c33042011-12-05 19:13:41 +053042 if (!try_module_get(dev->dev->driver->owner))
43 return -ENODEV;
44 mutex_lock(&sst_lock);
45 if (sst) {
46 pr_err("we already have a device %s\n", sst->name);
47 module_put(dev->dev->driver->owner);
48 mutex_unlock(&sst_lock);
49 return -EEXIST;
50 }
51 pr_debug("registering device %s\n", dev->name);
52 sst = dev;
53 mutex_unlock(&sst_lock);
54 return 0;
55}
56EXPORT_SYMBOL_GPL(sst_register_dsp);
57
58int sst_unregister_dsp(struct sst_device *dev)
59{
Takashi Iwai656a22a2013-11-05 18:40:01 +010060 if (WARN_ON(!dev))
61 return -EINVAL;
Vinod Koul03c33042011-12-05 19:13:41 +053062 if (dev != sst)
63 return -EINVAL;
64
65 mutex_lock(&sst_lock);
66
67 if (!sst) {
68 mutex_unlock(&sst_lock);
69 return -EIO;
70 }
71
72 module_put(sst->dev->driver->owner);
73 pr_debug("unreg %s\n", sst->name);
74 sst = NULL;
75 mutex_unlock(&sst_lock);
76 return 0;
77}
78EXPORT_SYMBOL_GPL(sst_unregister_dsp);
79
Vinod Kould927fda2011-01-04 20:16:32 +053080static struct snd_pcm_hardware sst_platform_pcm_hw = {
81 .info = (SNDRV_PCM_INFO_INTERLEAVED |
82 SNDRV_PCM_INFO_DOUBLE |
83 SNDRV_PCM_INFO_PAUSE |
84 SNDRV_PCM_INFO_RESUME |
85 SNDRV_PCM_INFO_MMAP|
86 SNDRV_PCM_INFO_MMAP_VALID |
87 SNDRV_PCM_INFO_BLOCK_TRANSFER |
88 SNDRV_PCM_INFO_SYNC_START),
Vinod Kould927fda2011-01-04 20:16:32 +053089 .buffer_bytes_max = SST_MAX_BUFFER,
90 .period_bytes_min = SST_MIN_PERIOD_BYTES,
91 .period_bytes_max = SST_MAX_PERIOD_BYTES,
92 .periods_min = SST_MIN_PERIODS,
93 .periods_max = SST_MAX_PERIODS,
94 .fifo_size = SST_FIFO_SIZE,
95};
96
Vinod Koul61b165c2014-06-13 18:03:56 +053097static struct sst_dev_stream_map dpcm_strm_map[] = {
98 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, /* Reserved, not in use */
99 {MERR_DPCM_AUDIO, 0, SNDRV_PCM_STREAM_PLAYBACK, PIPE_MEDIA1_IN, SST_TASK_ID_MEDIA, 0},
100 {MERR_DPCM_COMPR, 0, SNDRV_PCM_STREAM_PLAYBACK, PIPE_MEDIA0_IN, SST_TASK_ID_MEDIA, 0},
101 {MERR_DPCM_AUDIO, 0, SNDRV_PCM_STREAM_CAPTURE, PIPE_PCM1_OUT, SST_TASK_ID_MEDIA, 0},
102};
103
Vinod Kould927fda2011-01-04 20:16:32 +0530104/* MFLD - MSIC */
Axel Lind1b73282011-09-27 10:38:50 +0800105static struct snd_soc_dai_driver sst_platform_dai[] = {
Vinod Kould927fda2011-01-04 20:16:32 +0530106{
Vinod Koul5106f5a2014-05-15 21:38:15 +0530107 .name = "Headset-cpu-dai",
Vinod Kould927fda2011-01-04 20:16:32 +0530108 .id = 0,
109 .playback = {
110 .channels_min = SST_STEREO,
111 .channels_max = SST_STEREO,
112 .rates = SNDRV_PCM_RATE_48000,
113 .formats = SNDRV_PCM_FMTBIT_S24_LE,
114 },
Harsha Priyaa7bffdf2011-01-28 22:27:26 +0530115 .capture = {
116 .channels_min = 1,
117 .channels_max = 5,
118 .rates = SNDRV_PCM_RATE_48000,
119 .formats = SNDRV_PCM_FMTBIT_S24_LE,
120 },
Vinod Kould927fda2011-01-04 20:16:32 +0530121},
122{
Vinod Koulc514a912012-08-16 17:10:42 +0530123 .name = "Compress-cpu-dai",
124 .compress_dai = 1,
125 .playback = {
126 .channels_min = SST_STEREO,
127 .channels_max = SST_STEREO,
128 .rates = SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000,
129 .formats = SNDRV_PCM_FMTBIT_S16_LE,
130 },
131},
Vinod Kould927fda2011-01-04 20:16:32 +0530132};
Harsha Priya5c685362011-01-11 14:50:35 +0530133
Vinod Kould927fda2011-01-04 20:16:32 +0530134/* helper functions */
Vinod Koul4496ffa2014-05-05 14:27:49 +0530135void sst_set_stream_status(struct sst_runtime_stream *stream,
Harsha Priya5c685362011-01-11 14:50:35 +0530136 int state)
137{
Lu Guanqund89b0a12011-04-08 15:38:48 +0800138 unsigned long flags;
139 spin_lock_irqsave(&stream->status_lock, flags);
Harsha Priya5c685362011-01-11 14:50:35 +0530140 stream->stream_status = state;
Lu Guanqund89b0a12011-04-08 15:38:48 +0800141 spin_unlock_irqrestore(&stream->status_lock, flags);
Harsha Priya5c685362011-01-11 14:50:35 +0530142}
143
144static inline int sst_get_stream_status(struct sst_runtime_stream *stream)
145{
146 int state;
Lu Guanqund89b0a12011-04-08 15:38:48 +0800147 unsigned long flags;
Harsha Priya5c685362011-01-11 14:50:35 +0530148
Lu Guanqund89b0a12011-04-08 15:38:48 +0800149 spin_lock_irqsave(&stream->status_lock, flags);
Harsha Priya5c685362011-01-11 14:50:35 +0530150 state = stream->stream_status;
Lu Guanqund89b0a12011-04-08 15:38:48 +0800151 spin_unlock_irqrestore(&stream->status_lock, flags);
Harsha Priya5c685362011-01-11 14:50:35 +0530152 return state;
153}
154
Vinod Koula870cdc2014-06-13 18:03:55 +0530155static void sst_fill_alloc_params(struct snd_pcm_substream *substream,
156 struct snd_sst_alloc_params_ext *alloc_param)
Harsha Priya5c685362011-01-11 14:50:35 +0530157{
Vinod Koula870cdc2014-06-13 18:03:55 +0530158 unsigned int channels;
159 snd_pcm_uframes_t period_size;
160 ssize_t periodbytes;
161 ssize_t buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
162 u32 buffer_addr = virt_to_phys(substream->dma_buffer.area);
Harsha Priya5c685362011-01-11 14:50:35 +0530163
Vinod Koula870cdc2014-06-13 18:03:55 +0530164 channels = substream->runtime->channels;
165 period_size = substream->runtime->period_size;
166 periodbytes = samples_to_bytes(substream->runtime, period_size);
167 alloc_param->ring_buf_info[0].addr = buffer_addr;
168 alloc_param->ring_buf_info[0].size = buffer_bytes;
169 alloc_param->sg_count = 1;
170 alloc_param->reserved = 0;
171 alloc_param->frag_size = periodbytes * channels;
172
173}
174static void sst_fill_pcm_params(struct snd_pcm_substream *substream,
175 struct snd_sst_stream_params *param)
176{
177 param->uc.pcm_params.num_chan = (u8) substream->runtime->channels;
178 param->uc.pcm_params.pcm_wd_sz = substream->runtime->sample_bits;
179 param->uc.pcm_params.sfreq = substream->runtime->rate;
180
181 /* PCM stream via ALSA interface */
182 param->uc.pcm_params.use_offload_path = 0;
183 param->uc.pcm_params.reserved2 = 0;
184 memset(param->uc.pcm_params.channel_map, 0, sizeof(u8));
185
186}
Vinod Koul61b165c2014-06-13 18:03:56 +0530187
188static int sst_get_stream_mapping(int dev, int sdev, int dir,
189 struct sst_dev_stream_map *map, int size)
Vinod Koula870cdc2014-06-13 18:03:55 +0530190{
Vinod Koul61b165c2014-06-13 18:03:56 +0530191 int i;
192
193 if (map == NULL)
194 return -EINVAL;
195
196
197 /* index 0 is not used in stream map */
198 for (i = 1; i < size; i++) {
199 if ((map[i].dev_num == dev) && (map[i].direction == dir))
200 return i;
201 }
202 return 0;
203}
204
205int sst_fill_stream_params(void *substream,
206 const struct sst_data *ctx, struct snd_sst_params *str_params, bool is_compress)
207{
208 int map_size;
209 int index;
210 struct sst_dev_stream_map *map;
Vinod Koula870cdc2014-06-13 18:03:55 +0530211 struct snd_pcm_substream *pstream = NULL;
212 struct snd_compr_stream *cstream = NULL;
213
Vinod Koul61b165c2014-06-13 18:03:56 +0530214 map = ctx->pdata->pdev_strm_map;
215 map_size = ctx->pdata->strm_map_size;
216
Vinod Koula870cdc2014-06-13 18:03:55 +0530217 if (is_compress == true)
218 cstream = (struct snd_compr_stream *)substream;
219 else
220 pstream = (struct snd_pcm_substream *)substream;
221
222 str_params->stream_type = SST_STREAM_TYPE_MUSIC;
223
224 /* For pcm streams */
Vinod Koul61b165c2014-06-13 18:03:56 +0530225 if (pstream) {
226 index = sst_get_stream_mapping(pstream->pcm->device,
227 pstream->number, pstream->stream,
228 map, map_size);
229 if (index <= 0)
230 return -EINVAL;
Vinod Koula870cdc2014-06-13 18:03:55 +0530231
Vinod Koul61b165c2014-06-13 18:03:56 +0530232 str_params->stream_id = index;
233 str_params->device_type = map[index].device_id;
234 str_params->task = map[index].task_id;
235
236 str_params->ops = (u8)pstream->stream;
237 }
238
239 if (cstream) {
240 index = sst_get_stream_mapping(cstream->device->device,
241 0, cstream->direction,
242 map, map_size);
243 if (index <= 0)
244 return -EINVAL;
245 str_params->stream_id = index;
246 str_params->device_type = map[index].device_id;
247 str_params->task = map[index].task_id;
248
249 str_params->ops = (u8)cstream->direction;
250 }
Vinod Koula870cdc2014-06-13 18:03:55 +0530251 return 0;
Harsha Priya5c685362011-01-11 14:50:35 +0530252}
253
Vinod Koula870cdc2014-06-13 18:03:55 +0530254static int sst_platform_alloc_stream(struct snd_pcm_substream *substream,
255 struct snd_soc_platform *platform)
Vinod Kould927fda2011-01-04 20:16:32 +0530256{
257 struct sst_runtime_stream *stream =
258 substream->runtime->private_data;
Vinod Koula870cdc2014-06-13 18:03:55 +0530259 struct snd_sst_stream_params param = {{{0,},},};
260 struct snd_sst_params str_params = {0};
261 struct snd_sst_alloc_params_ext alloc_params = {0};
262 int ret_val = 0;
Vinod Koul61b165c2014-06-13 18:03:56 +0530263 struct sst_data *ctx = snd_soc_platform_get_drvdata(platform);
Vinod Kould927fda2011-01-04 20:16:32 +0530264
265 /* set codec params and inform SST driver the same */
Harsha Priya5c685362011-01-11 14:50:35 +0530266 sst_fill_pcm_params(substream, &param);
Vinod Koula870cdc2014-06-13 18:03:55 +0530267 sst_fill_alloc_params(substream, &alloc_params);
Vinod Kould927fda2011-01-04 20:16:32 +0530268 substream->runtime->dma_area = substream->dma_buffer.area;
Vinod Kould927fda2011-01-04 20:16:32 +0530269 str_params.sparams = param;
Vinod Koula870cdc2014-06-13 18:03:55 +0530270 str_params.aparams = alloc_params;
271 str_params.codec = SST_CODEC_TYPE_PCM;
272
273 /* fill the device type and stream id to pass to SST driver */
Vinod Koul61b165c2014-06-13 18:03:56 +0530274 ret_val = sst_fill_stream_params(substream, ctx, &str_params, false);
Vinod Kould927fda2011-01-04 20:16:32 +0530275 if (ret_val < 0)
276 return ret_val;
277
Vinod Koula870cdc2014-06-13 18:03:55 +0530278 stream->stream_info.str_id = str_params.stream_id;
279
Subhransu S. Prustyb12b087c2014-08-04 15:04:21 +0530280 ret_val = stream->ops->open(sst->dev, &str_params);
Vinod Koula870cdc2014-06-13 18:03:55 +0530281 if (ret_val <= 0)
282 return ret_val;
283
284
Vinod Kould927fda2011-01-04 20:16:32 +0530285 return ret_val;
286}
287
Vinod Koul9daa5bd2014-06-13 18:03:52 +0530288static void sst_period_elapsed(void *arg)
Vinod Kould927fda2011-01-04 20:16:32 +0530289{
Vinod Koul9daa5bd2014-06-13 18:03:52 +0530290 struct snd_pcm_substream *substream = arg;
Vinod Kould927fda2011-01-04 20:16:32 +0530291 struct sst_runtime_stream *stream;
Harsha Priya5c685362011-01-11 14:50:35 +0530292 int status;
Vinod Kould927fda2011-01-04 20:16:32 +0530293
294 if (!substream || !substream->runtime)
295 return;
296 stream = substream->runtime->private_data;
297 if (!stream)
298 return;
Harsha Priya5c685362011-01-11 14:50:35 +0530299 status = sst_get_stream_status(stream);
300 if (status != SST_PLATFORM_RUNNING)
Vinod Kould927fda2011-01-04 20:16:32 +0530301 return;
Vinod Kould927fda2011-01-04 20:16:32 +0530302 snd_pcm_period_elapsed(substream);
Vinod Kould927fda2011-01-04 20:16:32 +0530303}
304
305static int sst_platform_init_stream(struct snd_pcm_substream *substream)
306{
307 struct sst_runtime_stream *stream =
308 substream->runtime->private_data;
309 int ret_val;
310
311 pr_debug("setting buffer ptr param\n");
Harsha Priya5c685362011-01-11 14:50:35 +0530312 sst_set_stream_status(stream, SST_PLATFORM_INIT);
Vinod Kould927fda2011-01-04 20:16:32 +0530313 stream->stream_info.period_elapsed = sst_period_elapsed;
Vinod Koul9daa5bd2014-06-13 18:03:52 +0530314 stream->stream_info.arg = substream;
Vinod Kould927fda2011-01-04 20:16:32 +0530315 stream->stream_info.buffer_ptr = 0;
316 stream->stream_info.sfreq = substream->runtime->rate;
Subhransu S. Prustyb12b087c2014-08-04 15:04:21 +0530317 ret_val = stream->ops->stream_init(sst->dev, &stream->stream_info);
Vinod Kould927fda2011-01-04 20:16:32 +0530318 if (ret_val)
319 pr_err("control_set ret error %d\n", ret_val);
320 return ret_val;
321
322}
Vinod Kould927fda2011-01-04 20:16:32 +0530323
Vinod Koul6cc0f4e2014-06-13 18:03:51 +0530324static int sst_media_open(struct snd_pcm_substream *substream,
325 struct snd_soc_dai *dai)
Vinod Kould927fda2011-01-04 20:16:32 +0530326{
Vinod Koul6cc0f4e2014-06-13 18:03:51 +0530327 int ret_val = 0;
Lu Guanqun22be5042011-09-06 15:21:38 +0800328 struct snd_pcm_runtime *runtime = substream->runtime;
Vinod Kould927fda2011-01-04 20:16:32 +0530329 struct sst_runtime_stream *stream;
Lu Guanqun22be5042011-09-06 15:21:38 +0800330
Vinod Kould927fda2011-01-04 20:16:32 +0530331 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
332 if (!stream)
333 return -ENOMEM;
Vinod Kould927fda2011-01-04 20:16:32 +0530334 spin_lock_init(&stream->status_lock);
Vinod Koul03c33042011-12-05 19:13:41 +0530335
336 /* get the sst ops */
337 mutex_lock(&sst_lock);
Vinod Koul6cc0f4e2014-06-13 18:03:51 +0530338 if (!sst ||
339 !try_module_get(sst->dev->driver->owner)) {
Vinod Koul03c33042011-12-05 19:13:41 +0530340 pr_err("no device available to run\n");
Vinod Koul6cc0f4e2014-06-13 18:03:51 +0530341 ret_val = -ENODEV;
342 goto out_ops;
Vinod Koul03c33042011-12-05 19:13:41 +0530343 }
344 stream->ops = sst->ops;
345 mutex_unlock(&sst_lock);
346
Vinod Kould927fda2011-01-04 20:16:32 +0530347 stream->stream_info.str_id = 0;
Vinod Koul6cc0f4e2014-06-13 18:03:51 +0530348
Vinod Koul9daa5bd2014-06-13 18:03:52 +0530349 stream->stream_info.arg = substream;
Vinod Kould927fda2011-01-04 20:16:32 +0530350 /* allocate memory for SST API set */
Vinod Kould927fda2011-01-04 20:16:32 +0530351 runtime->private_data = stream;
Lu Guanqun283e42e2011-09-06 15:21:43 +0800352
Vinod Koul6cc0f4e2014-06-13 18:03:51 +0530353 /* Make sure, that the period size is always even */
354 snd_pcm_hw_constraint_step(substream->runtime, 0,
355 SNDRV_PCM_HW_PARAM_PERIODS, 2);
356
357 return snd_pcm_hw_constraint_integer(runtime,
358 SNDRV_PCM_HW_PARAM_PERIODS);
359out_ops:
360 kfree(stream);
361 mutex_unlock(&sst_lock);
362 return ret_val;
Vinod Kould927fda2011-01-04 20:16:32 +0530363}
364
Vinod Koul6cc0f4e2014-06-13 18:03:51 +0530365static void sst_media_close(struct snd_pcm_substream *substream,
366 struct snd_soc_dai *dai)
Vinod Kould927fda2011-01-04 20:16:32 +0530367{
368 struct sst_runtime_stream *stream;
369 int ret_val = 0, str_id;
370
Vinod Kould927fda2011-01-04 20:16:32 +0530371 stream = substream->runtime->private_data;
372 str_id = stream->stream_info.str_id;
Vinod Kould927fda2011-01-04 20:16:32 +0530373 if (str_id)
Subhransu S. Prustyb12b087c2014-08-04 15:04:21 +0530374 ret_val = stream->ops->close(sst->dev, str_id);
Vinod Koul03c33042011-12-05 19:13:41 +0530375 module_put(sst->dev->driver->owner);
Vinod Kould927fda2011-01-04 20:16:32 +0530376 kfree(stream);
Vinod Koul61b165c2014-06-13 18:03:56 +0530377}
378
379static inline unsigned int get_current_pipe_id(struct snd_soc_platform *platform,
380 struct snd_pcm_substream *substream)
381{
382 struct sst_data *sst = snd_soc_platform_get_drvdata(platform);
383 struct sst_dev_stream_map *map = sst->pdata->pdev_strm_map;
384 struct sst_runtime_stream *stream =
385 substream->runtime->private_data;
386 u32 str_id = stream->stream_info.str_id;
387 unsigned int pipe_id;
388 pipe_id = map[str_id].device_id;
389
390 pr_debug("%s: got pipe_id = %#x for str_id = %d\n",
391 __func__, pipe_id, str_id);
392 return pipe_id;
Vinod Kould927fda2011-01-04 20:16:32 +0530393}
394
Vinod Koul6cc0f4e2014-06-13 18:03:51 +0530395static int sst_media_prepare(struct snd_pcm_substream *substream,
396 struct snd_soc_dai *dai)
Vinod Kould927fda2011-01-04 20:16:32 +0530397{
398 struct sst_runtime_stream *stream;
399 int ret_val = 0, str_id;
400
Vinod Kould927fda2011-01-04 20:16:32 +0530401 stream = substream->runtime->private_data;
402 str_id = stream->stream_info.str_id;
403 if (stream->stream_info.str_id) {
Subhransu S. Prustyb12b087c2014-08-04 15:04:21 +0530404 ret_val = stream->ops->stream_drop(sst->dev, str_id);
Vinod Kould927fda2011-01-04 20:16:32 +0530405 return ret_val;
406 }
407
Vinod Koula870cdc2014-06-13 18:03:55 +0530408 ret_val = sst_platform_alloc_stream(substream, dai->platform);
409 if (ret_val <= 0)
Vinod Kould927fda2011-01-04 20:16:32 +0530410 return ret_val;
411 snprintf(substream->pcm->id, sizeof(substream->pcm->id),
412 "%d", stream->stream_info.str_id);
413
414 ret_val = sst_platform_init_stream(substream);
415 if (ret_val)
416 return ret_val;
417 substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
418 return ret_val;
419}
420
Vinod Koul6cc0f4e2014-06-13 18:03:51 +0530421static int sst_media_hw_params(struct snd_pcm_substream *substream,
422 struct snd_pcm_hw_params *params,
423 struct snd_soc_dai *dai)
424{
425 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
426 memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
427 return 0;
428}
429
430static int sst_media_hw_free(struct snd_pcm_substream *substream,
431 struct snd_soc_dai *dai)
432{
433 return snd_pcm_lib_free_pages(substream);
434}
435
436static struct snd_soc_dai_ops sst_media_dai_ops = {
437 .startup = sst_media_open,
438 .shutdown = sst_media_close,
439 .prepare = sst_media_prepare,
440 .hw_params = sst_media_hw_params,
441 .hw_free = sst_media_hw_free,
442};
443
444static int sst_platform_open(struct snd_pcm_substream *substream)
445{
446 struct snd_pcm_runtime *runtime;
447
448 if (substream->pcm->internal)
449 return 0;
450
451 runtime = substream->runtime;
452 runtime->hw = sst_platform_pcm_hw;
453 return 0;
454}
455
Vinod Koul5106f5a2014-05-15 21:38:15 +0530456static int sst_platform_pcm_trigger(struct snd_pcm_substream *substream,
Vinod Kould927fda2011-01-04 20:16:32 +0530457 int cmd)
458{
459 int ret_val = 0, str_id;
460 struct sst_runtime_stream *stream;
Subhransu S. Prusty5981c2d2014-08-04 15:04:20 +0530461 int status;
Vinod Kould927fda2011-01-04 20:16:32 +0530462
Vinod Koul5106f5a2014-05-15 21:38:15 +0530463 pr_debug("sst_platform_pcm_trigger called\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530464 stream = substream->runtime->private_data;
Vinod Kould927fda2011-01-04 20:16:32 +0530465 str_id = stream->stream_info.str_id;
Vinod Kould927fda2011-01-04 20:16:32 +0530466 switch (cmd) {
467 case SNDRV_PCM_TRIGGER_START:
468 pr_debug("sst: Trigger Start\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530469 status = SST_PLATFORM_RUNNING;
Vinod Koul9daa5bd2014-06-13 18:03:52 +0530470 stream->stream_info.arg = substream;
Subhransu S. Prustyb12b087c2014-08-04 15:04:21 +0530471 ret_val = stream->ops->stream_start(sst->dev, str_id);
Vinod Kould927fda2011-01-04 20:16:32 +0530472 break;
473 case SNDRV_PCM_TRIGGER_STOP:
474 pr_debug("sst: in stop\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530475 status = SST_PLATFORM_DROPPED;
Subhransu S. Prustyb12b087c2014-08-04 15:04:21 +0530476 ret_val = stream->ops->stream_drop(sst->dev, str_id);
Vinod Kould927fda2011-01-04 20:16:32 +0530477 break;
478 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
479 pr_debug("sst: in pause\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530480 status = SST_PLATFORM_PAUSED;
Subhransu S. Prustyb12b087c2014-08-04 15:04:21 +0530481 ret_val = stream->ops->stream_pause(sst->dev, str_id);
Vinod Kould927fda2011-01-04 20:16:32 +0530482 break;
483 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
484 pr_debug("sst: in pause release\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530485 status = SST_PLATFORM_RUNNING;
Subhransu S. Prustyb12b087c2014-08-04 15:04:21 +0530486 ret_val = stream->ops->stream_pause_release(sst->dev, str_id);
Vinod Kould927fda2011-01-04 20:16:32 +0530487 break;
488 default:
Harsha Priyaa2117012011-01-11 14:50:59 +0530489 return -EINVAL;
Vinod Kould927fda2011-01-04 20:16:32 +0530490 }
Subhransu S. Prusty5981c2d2014-08-04 15:04:20 +0530491
Harsha Priyaa2117012011-01-11 14:50:59 +0530492 if (!ret_val)
493 sst_set_stream_status(stream, status);
494
Vinod Kould927fda2011-01-04 20:16:32 +0530495 return ret_val;
496}
497
498
Vinod Koul5106f5a2014-05-15 21:38:15 +0530499static snd_pcm_uframes_t sst_platform_pcm_pointer
Vinod Kould927fda2011-01-04 20:16:32 +0530500 (struct snd_pcm_substream *substream)
501{
502 struct sst_runtime_stream *stream;
Harsha Priya5c685362011-01-11 14:50:35 +0530503 int ret_val, status;
Vinod Kould927fda2011-01-04 20:16:32 +0530504 struct pcm_stream_info *str_info;
505
Vinod Kould927fda2011-01-04 20:16:32 +0530506 stream = substream->runtime->private_data;
Harsha Priya5c685362011-01-11 14:50:35 +0530507 status = sst_get_stream_status(stream);
508 if (status == SST_PLATFORM_INIT)
Vinod Kould927fda2011-01-04 20:16:32 +0530509 return 0;
Vinod Kould927fda2011-01-04 20:16:32 +0530510 str_info = &stream->stream_info;
Subhransu S. Prustyb12b087c2014-08-04 15:04:21 +0530511 ret_val = stream->ops->stream_read_tstamp(sst->dev, str_info);
Vinod Kould927fda2011-01-04 20:16:32 +0530512 if (ret_val) {
513 pr_err("sst: error code = %d\n", ret_val);
514 return ret_val;
515 }
Vinod Koul2a635822014-06-13 18:03:53 +0530516 substream->runtime->delay = str_info->pcm_delay;
Vinod Koul6cc0f4e2014-06-13 18:03:51 +0530517 return str_info->buffer_ptr;
xingchao9ab88432011-04-27 16:58:54 -0400518}
519
Vinod Kould927fda2011-01-04 20:16:32 +0530520static struct snd_pcm_ops sst_platform_ops = {
Vinod Koul5106f5a2014-05-15 21:38:15 +0530521 .open = sst_platform_open,
Vinod Kould927fda2011-01-04 20:16:32 +0530522 .ioctl = snd_pcm_lib_ioctl,
Vinod Koul5106f5a2014-05-15 21:38:15 +0530523 .trigger = sst_platform_pcm_trigger,
524 .pointer = sst_platform_pcm_pointer,
Vinod Kould927fda2011-01-04 20:16:32 +0530525};
526
527static void sst_pcm_free(struct snd_pcm *pcm)
528{
529 pr_debug("sst_pcm_free called\n");
530 snd_pcm_lib_preallocate_free_for_all(pcm);
531}
532
Axel Lin8faa8c12011-12-13 17:13:45 +0800533static int sst_pcm_new(struct snd_soc_pcm_runtime *rtd)
Vinod Kould927fda2011-01-04 20:16:32 +0530534{
Vinod Koul6cc0f4e2014-06-13 18:03:51 +0530535 struct snd_soc_dai *dai = rtd->cpu_dai;
Liam Girdwood552d1ef2011-06-07 16:08:33 +0100536 struct snd_pcm *pcm = rtd->pcm;
Vinod Kould927fda2011-01-04 20:16:32 +0530537 int retval = 0;
538
Vinod Koul6cc0f4e2014-06-13 18:03:51 +0530539 if (dai->driver->playback.channels_min ||
540 dai->driver->capture.channels_min) {
Vinod Kould927fda2011-01-04 20:16:32 +0530541 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
542 SNDRV_DMA_TYPE_CONTINUOUS,
Vinod Koul6cc0f4e2014-06-13 18:03:51 +0530543 snd_dma_continuous_data(GFP_DMA),
Vinod Kould927fda2011-01-04 20:16:32 +0530544 SST_MIN_BUFFER, SST_MAX_BUFFER);
545 if (retval) {
546 pr_err("dma buffer allocationf fail\n");
547 return retval;
548 }
549 }
Vinod Kould927fda2011-01-04 20:16:32 +0530550 return retval;
551}
Vinod Koulc514a912012-08-16 17:10:42 +0530552
Axel Lin8faa8c12011-12-13 17:13:45 +0800553static struct snd_soc_platform_driver sst_soc_platform_drv = {
Vinod Kould927fda2011-01-04 20:16:32 +0530554 .ops = &sst_platform_ops,
Vinod Koulc514a912012-08-16 17:10:42 +0530555 .compr_ops = &sst_platform_compr_ops,
Vinod Kould927fda2011-01-04 20:16:32 +0530556 .pcm_new = sst_pcm_new,
557 .pcm_free = sst_pcm_free,
558};
559
Vinod Koul2b4c78d2014-05-05 22:19:25 +0530560static const struct snd_soc_component_driver sst_component = {
561 .name = "sst",
562};
563
564
Vinod Kould927fda2011-01-04 20:16:32 +0530565static int sst_platform_probe(struct platform_device *pdev)
566{
Vinod Koul61b165c2014-06-13 18:03:56 +0530567 struct sst_data *drv;
Vinod Kould927fda2011-01-04 20:16:32 +0530568 int ret;
Subhransu S. Prusty2741d432014-07-30 18:39:05 +0530569 struct sst_platform_data *pdata;
Vinod Kould927fda2011-01-04 20:16:32 +0530570
Vinod Koul61b165c2014-06-13 18:03:56 +0530571 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
Subhransu S. Prusty19a23a52014-07-30 18:36:00 +0530572 if (drv == NULL) {
Vinod Koul61b165c2014-06-13 18:03:56 +0530573 pr_err("kzalloc failed\n");
574 return -ENOMEM;
575 }
576
Subhransu S. Prusty2741d432014-07-30 18:39:05 +0530577 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
578 if (pdata == NULL) {
579 pr_err("kzalloc failed for pdata\n");
580 return -ENOMEM;
581 }
582
Vinod Koul61b165c2014-06-13 18:03:56 +0530583 pdata->pdev_strm_map = dpcm_strm_map;
584 pdata->strm_map_size = ARRAY_SIZE(dpcm_strm_map);
585 drv->pdata = pdata;
586 mutex_init(&drv->lock);
587 dev_set_drvdata(&pdev->dev, drv);
588
Vinod Kould927fda2011-01-04 20:16:32 +0530589 ret = snd_soc_register_platform(&pdev->dev, &sst_soc_platform_drv);
590 if (ret) {
591 pr_err("registering soc platform failed\n");
592 return ret;
593 }
Harsha Priya5c685362011-01-11 14:50:35 +0530594
Kuninori Morimotob1c36862013-03-21 03:32:50 -0700595 ret = snd_soc_register_component(&pdev->dev, &sst_component,
Vinod Kould927fda2011-01-04 20:16:32 +0530596 sst_platform_dai, ARRAY_SIZE(sst_platform_dai));
597 if (ret) {
598 pr_err("registering cpu dais failed\n");
599 snd_soc_unregister_platform(&pdev->dev);
600 }
601 return ret;
602}
603
604static int sst_platform_remove(struct platform_device *pdev)
605{
606
Kuninori Morimotob1c36862013-03-21 03:32:50 -0700607 snd_soc_unregister_component(&pdev->dev);
Vinod Kould927fda2011-01-04 20:16:32 +0530608 snd_soc_unregister_platform(&pdev->dev);
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300609 pr_debug("sst_platform_remove success\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530610 return 0;
611}
612
613static struct platform_driver sst_platform_driver = {
614 .driver = {
Mark Browna4b12992014-03-12 23:04:35 +0000615 .name = "sst-mfld-platform",
Vinod Kould927fda2011-01-04 20:16:32 +0530616 .owner = THIS_MODULE,
617 },
618 .probe = sst_platform_probe,
619 .remove = sst_platform_remove,
620};
621
Axel Lin29515d62011-11-24 11:51:56 +0800622module_platform_driver(sst_platform_driver);
Vinod Kould927fda2011-01-04 20:16:32 +0530623
624MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");
625MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
626MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
627MODULE_LICENSE("GPL v2");
Mark Browna4b12992014-03-12 23:04:35 +0000628MODULE_ALIAS("platform:sst-mfld-platform");