blob: 1d1f5447b338bd23ced10845c7103ca024e917b6 [file] [log] [blame]
Vinod Kould927fda2011-01-04 20:16:32 +05301/*
2 * sst_platform.c - Intel MID Platform driver
3 *
4 * Copyright (C) 2010 Intel Corp
5 * 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>
30#include <sound/core.h>
31#include <sound/pcm.h>
32#include <sound/pcm_params.h>
33#include <sound/soc.h>
34#include "../../../drivers/staging/intel_sst/intel_sst_ioctl.h"
35#include "../../../drivers/staging/intel_sst/intel_sst.h"
36#include "sst_platform.h"
37
38static struct snd_pcm_hardware sst_platform_pcm_hw = {
39 .info = (SNDRV_PCM_INFO_INTERLEAVED |
40 SNDRV_PCM_INFO_DOUBLE |
41 SNDRV_PCM_INFO_PAUSE |
42 SNDRV_PCM_INFO_RESUME |
43 SNDRV_PCM_INFO_MMAP|
44 SNDRV_PCM_INFO_MMAP_VALID |
45 SNDRV_PCM_INFO_BLOCK_TRANSFER |
46 SNDRV_PCM_INFO_SYNC_START),
47 .formats = (SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_U16 |
48 SNDRV_PCM_FMTBIT_S24 | SNDRV_PCM_FMTBIT_U24 |
49 SNDRV_PCM_FMTBIT_S32 | SNDRV_PCM_FMTBIT_U32),
50 .rates = (SNDRV_PCM_RATE_8000|
51 SNDRV_PCM_RATE_44100 |
52 SNDRV_PCM_RATE_48000),
53 .rate_min = SST_MIN_RATE,
54 .rate_max = SST_MAX_RATE,
55 .channels_min = SST_MIN_CHANNEL,
56 .channels_max = SST_MAX_CHANNEL,
57 .buffer_bytes_max = SST_MAX_BUFFER,
58 .period_bytes_min = SST_MIN_PERIOD_BYTES,
59 .period_bytes_max = SST_MAX_PERIOD_BYTES,
60 .periods_min = SST_MIN_PERIODS,
61 .periods_max = SST_MAX_PERIODS,
62 .fifo_size = SST_FIFO_SIZE,
63};
64
65/* MFLD - MSIC */
66struct snd_soc_dai_driver sst_platform_dai[] = {
67{
68 .name = "Headset-cpu-dai",
69 .id = 0,
70 .playback = {
71 .channels_min = SST_STEREO,
72 .channels_max = SST_STEREO,
73 .rates = SNDRV_PCM_RATE_48000,
74 .formats = SNDRV_PCM_FMTBIT_S24_LE,
75 },
76},
77{
78 .name = "Speaker-cpu-dai",
79 .id = 1,
80 .playback = {
81 .channels_min = SST_MONO,
82 .channels_max = SST_STEREO,
83 .rates = SNDRV_PCM_RATE_48000,
84 .formats = SNDRV_PCM_FMTBIT_S24_LE,
85 },
86},
87{
88 .name = "Vibra1-cpu-dai",
89 .id = 2,
90 .playback = {
91 .channels_min = SST_MONO,
92 .channels_max = SST_MONO,
93 .rates = SNDRV_PCM_RATE_48000,
94 .formats = SNDRV_PCM_FMTBIT_S24_LE,
95 },
96},
97{
98 .name = "Vibra2-cpu-dai",
99 .id = 3,
100 .playback = {
101 .channels_min = SST_MONO,
102 .channels_max = SST_STEREO,
103 .rates = SNDRV_PCM_RATE_48000,
104 .formats = SNDRV_PCM_FMTBIT_S24_LE,
105 },
106},
107};
Harsha Priya5c685362011-01-11 14:50:35 +0530108
Vinod Kould927fda2011-01-04 20:16:32 +0530109/* helper functions */
Harsha Priya5c685362011-01-11 14:50:35 +0530110static inline void sst_set_stream_status(struct sst_runtime_stream *stream,
111 int state)
112{
113 spin_lock(&stream->status_lock);
114 stream->stream_status = state;
115 spin_unlock(&stream->status_lock);
116}
117
118static inline int sst_get_stream_status(struct sst_runtime_stream *stream)
119{
120 int state;
121
122 spin_lock(&stream->status_lock);
123 state = stream->stream_status;
124 spin_unlock(&stream->status_lock);
125 return state;
126}
127
128static void sst_fill_pcm_params(struct snd_pcm_substream *substream,
129 struct snd_sst_stream_params *param)
130{
131
132 param->uc.pcm_params.codec = SST_CODEC_TYPE_PCM;
133 param->uc.pcm_params.num_chan = (u8) substream->runtime->channels;
134 param->uc.pcm_params.pcm_wd_sz = substream->runtime->sample_bits;
135 param->uc.pcm_params.reserved = 0;
136 param->uc.pcm_params.sfreq = substream->runtime->rate;
137 param->uc.pcm_params.ring_buffer_size =
138 snd_pcm_lib_buffer_bytes(substream);
139 param->uc.pcm_params.period_count = substream->runtime->period_size;
140 param->uc.pcm_params.ring_buffer_addr =
141 virt_to_phys(substream->dma_buffer.area);
142 pr_debug("period_cnt = %d\n", param->uc.pcm_params.period_count);
143 pr_debug("sfreq= %d, wd_sz = %d\n",
144 param->uc.pcm_params.sfreq, param->uc.pcm_params.pcm_wd_sz);
145}
146
Vinod Kould927fda2011-01-04 20:16:32 +0530147static int sst_platform_alloc_stream(struct snd_pcm_substream *substream)
148{
149 struct sst_runtime_stream *stream =
150 substream->runtime->private_data;
151 struct snd_sst_stream_params param = {{{0,},},};
152 struct snd_sst_params str_params = {0};
153 int ret_val;
154
155 /* set codec params and inform SST driver the same */
Harsha Priya5c685362011-01-11 14:50:35 +0530156 sst_fill_pcm_params(substream, &param);
Vinod Kould927fda2011-01-04 20:16:32 +0530157 substream->runtime->dma_area = substream->dma_buffer.area;
Vinod Kould927fda2011-01-04 20:16:32 +0530158 str_params.sparams = param;
Harsha Priya5c685362011-01-11 14:50:35 +0530159 str_params.codec = param.uc.pcm_params.codec;
Vinod Kould927fda2011-01-04 20:16:32 +0530160 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
161 str_params.ops = STREAM_OPS_PLAYBACK;
162 str_params.device_type = substream->pcm->device + 1;
163 pr_debug("Playbck stream,Device %d\n",
164 substream->pcm->device);
165 } else {
166 str_params.ops = STREAM_OPS_CAPTURE;
167 str_params.device_type = SND_SST_DEVICE_CAPTURE;
168 pr_debug("Capture stream,Device %d\n",
169 substream->pcm->device);
170 }
Harsha Priyaa2117012011-01-11 14:50:59 +0530171 ret_val = stream->sstdrv_ops->pcm_control->open(&str_params);
Vinod Kould927fda2011-01-04 20:16:32 +0530172 pr_debug("SST_SND_PLAY/CAPTURE ret_val = %x\n", ret_val);
173 if (ret_val < 0)
174 return ret_val;
175
176 stream->stream_info.str_id = ret_val;
177 pr_debug("str id : %d\n", stream->stream_info.str_id);
Vinod Kould927fda2011-01-04 20:16:32 +0530178 return ret_val;
179}
180
Vinod Kould927fda2011-01-04 20:16:32 +0530181static void sst_period_elapsed(void *mad_substream)
182{
183 struct snd_pcm_substream *substream = mad_substream;
184 struct sst_runtime_stream *stream;
Harsha Priya5c685362011-01-11 14:50:35 +0530185 int status;
Vinod Kould927fda2011-01-04 20:16:32 +0530186
187 if (!substream || !substream->runtime)
188 return;
189 stream = substream->runtime->private_data;
190 if (!stream)
191 return;
Harsha Priya5c685362011-01-11 14:50:35 +0530192 status = sst_get_stream_status(stream);
193 if (status != SST_PLATFORM_RUNNING)
Vinod Kould927fda2011-01-04 20:16:32 +0530194 return;
Vinod Kould927fda2011-01-04 20:16:32 +0530195 snd_pcm_period_elapsed(substream);
Vinod Kould927fda2011-01-04 20:16:32 +0530196}
197
198static int sst_platform_init_stream(struct snd_pcm_substream *substream)
199{
200 struct sst_runtime_stream *stream =
201 substream->runtime->private_data;
202 int ret_val;
203
204 pr_debug("setting buffer ptr param\n");
Harsha Priya5c685362011-01-11 14:50:35 +0530205 sst_set_stream_status(stream, SST_PLATFORM_INIT);
Vinod Kould927fda2011-01-04 20:16:32 +0530206 stream->stream_info.period_elapsed = sst_period_elapsed;
207 stream->stream_info.mad_substream = substream;
208 stream->stream_info.buffer_ptr = 0;
209 stream->stream_info.sfreq = substream->runtime->rate;
Harsha Priyaa2117012011-01-11 14:50:59 +0530210 ret_val = stream->sstdrv_ops->pcm_control->device_control(
211 SST_SND_STREAM_INIT, &stream->stream_info);
Vinod Kould927fda2011-01-04 20:16:32 +0530212 if (ret_val)
213 pr_err("control_set ret error %d\n", ret_val);
214 return ret_val;
215
216}
217/* end -- helper functions */
218
219static int sst_platform_open(struct snd_pcm_substream *substream)
220{
221 struct snd_pcm_runtime *runtime;
222 struct sst_runtime_stream *stream;
223 int ret_val = 0;
224
225 pr_debug("sst_platform_open called\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530226 runtime = substream->runtime;
227 runtime->hw = sst_platform_pcm_hw;
Vinod Kould927fda2011-01-04 20:16:32 +0530228 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
229 if (!stream)
230 return -ENOMEM;
Vinod Kould927fda2011-01-04 20:16:32 +0530231 spin_lock_init(&stream->status_lock);
232 stream->stream_info.str_id = 0;
Harsha Priya5c685362011-01-11 14:50:35 +0530233 sst_set_stream_status(stream, SST_PLATFORM_INIT);
Vinod Kould927fda2011-01-04 20:16:32 +0530234 stream->stream_info.mad_substream = substream;
235 /* allocate memory for SST API set */
236 stream->sstdrv_ops = kzalloc(sizeof(*stream->sstdrv_ops),
237 GFP_KERNEL);
238 if (!stream->sstdrv_ops) {
239 pr_err("sst: mem allocation for ops fail\n");
240 kfree(stream);
241 return -ENOMEM;
242 }
243 stream->sstdrv_ops->vendor_id = MSIC_VENDOR_ID;
Vinod Kould927fda2011-01-04 20:16:32 +0530244 /* registering with SST driver to get access to SST APIs to use */
245 ret_val = register_sst_card(stream->sstdrv_ops);
246 if (ret_val) {
247 pr_err("sst: sst card registration failed\n");
248 return ret_val;
249 }
250 runtime->private_data = stream;
251 return snd_pcm_hw_constraint_integer(runtime,
252 SNDRV_PCM_HW_PARAM_PERIODS);
253}
254
255static int sst_platform_close(struct snd_pcm_substream *substream)
256{
257 struct sst_runtime_stream *stream;
258 int ret_val = 0, str_id;
259
260 pr_debug("sst_platform_close called\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530261 stream = substream->runtime->private_data;
262 str_id = stream->stream_info.str_id;
Vinod Kould927fda2011-01-04 20:16:32 +0530263 if (str_id)
Harsha Priyaa2117012011-01-11 14:50:59 +0530264 ret_val = stream->sstdrv_ops->pcm_control->close(str_id);
Vinod Kould927fda2011-01-04 20:16:32 +0530265 kfree(stream->sstdrv_ops);
266 kfree(stream);
267 return ret_val;
268}
269
270static int sst_platform_pcm_prepare(struct snd_pcm_substream *substream)
271{
272 struct sst_runtime_stream *stream;
273 int ret_val = 0, str_id;
274
275 pr_debug("sst_platform_pcm_prepare called\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530276 stream = substream->runtime->private_data;
277 str_id = stream->stream_info.str_id;
278 if (stream->stream_info.str_id) {
Harsha Priyaa2117012011-01-11 14:50:59 +0530279 ret_val = stream->sstdrv_ops->pcm_control->device_control(
Vinod Kould927fda2011-01-04 20:16:32 +0530280 SST_SND_DROP, &str_id);
281 return ret_val;
282 }
283
284 ret_val = sst_platform_alloc_stream(substream);
285 if (ret_val < 0)
286 return ret_val;
287 snprintf(substream->pcm->id, sizeof(substream->pcm->id),
288 "%d", stream->stream_info.str_id);
289
290 ret_val = sst_platform_init_stream(substream);
291 if (ret_val)
292 return ret_val;
293 substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
294 return ret_val;
295}
296
297static int sst_platform_pcm_trigger(struct snd_pcm_substream *substream,
298 int cmd)
299{
300 int ret_val = 0, str_id;
301 struct sst_runtime_stream *stream;
Harsha Priyaa2117012011-01-11 14:50:59 +0530302 int str_cmd, status;
Vinod Kould927fda2011-01-04 20:16:32 +0530303
304 pr_debug("sst_platform_pcm_trigger called\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530305 stream = substream->runtime->private_data;
Vinod Kould927fda2011-01-04 20:16:32 +0530306 str_id = stream->stream_info.str_id;
Vinod Kould927fda2011-01-04 20:16:32 +0530307 switch (cmd) {
308 case SNDRV_PCM_TRIGGER_START:
309 pr_debug("sst: Trigger Start\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530310 str_cmd = SST_SND_START;
311 status = SST_PLATFORM_RUNNING;
Vinod Kould927fda2011-01-04 20:16:32 +0530312 stream->stream_info.mad_substream = substream;
313 break;
314 case SNDRV_PCM_TRIGGER_STOP:
315 pr_debug("sst: in stop\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530316 str_cmd = SST_SND_DROP;
317 status = SST_PLATFORM_DROPPED;
Vinod Kould927fda2011-01-04 20:16:32 +0530318 break;
319 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
320 pr_debug("sst: in pause\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530321 str_cmd = SST_SND_PAUSE;
322 status = SST_PLATFORM_PAUSED;
Vinod Kould927fda2011-01-04 20:16:32 +0530323 break;
324 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
325 pr_debug("sst: in pause release\n");
Harsha Priyaa2117012011-01-11 14:50:59 +0530326 str_cmd = SST_SND_RESUME;
327 status = SST_PLATFORM_RUNNING;
Vinod Kould927fda2011-01-04 20:16:32 +0530328 break;
329 default:
Harsha Priyaa2117012011-01-11 14:50:59 +0530330 return -EINVAL;
Vinod Kould927fda2011-01-04 20:16:32 +0530331 }
Harsha Priyaa2117012011-01-11 14:50:59 +0530332 ret_val = stream->sstdrv_ops->pcm_control->device_control(str_cmd,
333 &str_id);
334 if (!ret_val)
335 sst_set_stream_status(stream, status);
336
Vinod Kould927fda2011-01-04 20:16:32 +0530337 return ret_val;
338}
339
340
341static snd_pcm_uframes_t sst_platform_pcm_pointer
342 (struct snd_pcm_substream *substream)
343{
344 struct sst_runtime_stream *stream;
Harsha Priya5c685362011-01-11 14:50:35 +0530345 int ret_val, status;
Vinod Kould927fda2011-01-04 20:16:32 +0530346 struct pcm_stream_info *str_info;
347
Vinod Kould927fda2011-01-04 20:16:32 +0530348 stream = substream->runtime->private_data;
Harsha Priya5c685362011-01-11 14:50:35 +0530349 status = sst_get_stream_status(stream);
350 if (status == SST_PLATFORM_INIT)
Vinod Kould927fda2011-01-04 20:16:32 +0530351 return 0;
Vinod Kould927fda2011-01-04 20:16:32 +0530352 str_info = &stream->stream_info;
Harsha Priyaa2117012011-01-11 14:50:59 +0530353 ret_val = stream->sstdrv_ops->pcm_control->device_control(
Vinod Kould927fda2011-01-04 20:16:32 +0530354 SST_SND_BUFFER_POINTER, str_info);
355 if (ret_val) {
356 pr_err("sst: error code = %d\n", ret_val);
357 return ret_val;
358 }
Vinod Kould927fda2011-01-04 20:16:32 +0530359 return stream->stream_info.buffer_ptr;
360}
361
362
363static struct snd_pcm_ops sst_platform_ops = {
364 .open = sst_platform_open,
365 .close = sst_platform_close,
366 .ioctl = snd_pcm_lib_ioctl,
367 .prepare = sst_platform_pcm_prepare,
368 .trigger = sst_platform_pcm_trigger,
369 .pointer = sst_platform_pcm_pointer,
370};
371
372static void sst_pcm_free(struct snd_pcm *pcm)
373{
374 pr_debug("sst_pcm_free called\n");
375 snd_pcm_lib_preallocate_free_for_all(pcm);
376}
377
378int sst_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
379 struct snd_pcm *pcm)
380{
381 int retval = 0;
382
383 pr_debug("sst_pcm_new called\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530384 if (dai->driver->playback.channels_min ||
385 dai->driver->capture.channels_min) {
386 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
387 SNDRV_DMA_TYPE_CONTINUOUS,
388 snd_dma_continuous_data(GFP_KERNEL),
389 SST_MIN_BUFFER, SST_MAX_BUFFER);
390 if (retval) {
391 pr_err("dma buffer allocationf fail\n");
392 return retval;
393 }
394 }
Vinod Kould927fda2011-01-04 20:16:32 +0530395 return retval;
396}
397struct snd_soc_platform_driver sst_soc_platform_drv = {
398 .ops = &sst_platform_ops,
399 .pcm_new = sst_pcm_new,
400 .pcm_free = sst_pcm_free,
401};
402
403static int sst_platform_probe(struct platform_device *pdev)
404{
405 int ret;
406
407 pr_debug("sst_platform_probe called\n");
408 ret = snd_soc_register_platform(&pdev->dev, &sst_soc_platform_drv);
409 if (ret) {
410 pr_err("registering soc platform failed\n");
411 return ret;
412 }
Harsha Priya5c685362011-01-11 14:50:35 +0530413
Vinod Kould927fda2011-01-04 20:16:32 +0530414 ret = snd_soc_register_dais(&pdev->dev,
415 sst_platform_dai, ARRAY_SIZE(sst_platform_dai));
416 if (ret) {
417 pr_err("registering cpu dais failed\n");
418 snd_soc_unregister_platform(&pdev->dev);
419 }
420 return ret;
421}
422
423static int sst_platform_remove(struct platform_device *pdev)
424{
425
426 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(sst_platform_dai));
Vinod Kould927fda2011-01-04 20:16:32 +0530427 snd_soc_unregister_platform(&pdev->dev);
428 pr_debug("sst_platform_remove sucess\n");
Vinod Kould927fda2011-01-04 20:16:32 +0530429 return 0;
430}
431
432static struct platform_driver sst_platform_driver = {
433 .driver = {
434 .name = "sst-platform",
435 .owner = THIS_MODULE,
436 },
437 .probe = sst_platform_probe,
438 .remove = sst_platform_remove,
439};
440
441static int __init sst_soc_platform_init(void)
442{
443 pr_debug("sst_soc_platform_init called\n");
444 return platform_driver_register(&sst_platform_driver);
445}
446module_init(sst_soc_platform_init);
447
448static void __exit sst_soc_platform_exit(void)
449{
450 platform_driver_unregister(&sst_platform_driver);
451 pr_debug("sst_soc_platform_exit sucess\n");
452}
453module_exit(sst_soc_platform_exit);
454
455MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");
456MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
457MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
458MODULE_LICENSE("GPL v2");
Vinod Koulf5c6b2a2011-01-07 16:20:56 +0530459MODULE_ALIAS("platform:sst-platform");