blob: 0e994c16e7ab2da4181170c99ff4c5ede506d326 [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};
108/* helper functions */
109static int sst_platform_alloc_stream(struct snd_pcm_substream *substream)
110{
111 struct sst_runtime_stream *stream =
112 substream->runtime->private_data;
113 struct snd_sst_stream_params param = {{{0,},},};
114 struct snd_sst_params str_params = {0};
115 int ret_val;
116
117 /* set codec params and inform SST driver the same */
118
119 param.uc.pcm_params.codec = SST_CODEC_TYPE_PCM;
120 param.uc.pcm_params.num_chan = (u8) substream->runtime->channels;
121 param.uc.pcm_params.pcm_wd_sz = substream->runtime->sample_bits;
122 param.uc.pcm_params.reserved = 0;
123 param.uc.pcm_params.sfreq = substream->runtime->rate;
124 param.uc.pcm_params.ring_buffer_size =
125 snd_pcm_lib_buffer_bytes(substream);
126 param.uc.pcm_params.period_count = substream->runtime->period_size;
127 param.uc.pcm_params.ring_buffer_addr =
128 virt_to_phys(substream->dma_buffer.area);
129 substream->runtime->dma_area = substream->dma_buffer.area;
130
131 pr_debug("period_cnt = %d\n", param.uc.pcm_params.period_count);
132 pr_debug("sfreq= %d, wd_sz = %d\n",
133 param.uc.pcm_params.sfreq, param.uc.pcm_params.pcm_wd_sz);
134
135 str_params.sparams = param;
136 str_params.codec = SST_CODEC_TYPE_PCM;
137
138 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
139 str_params.ops = STREAM_OPS_PLAYBACK;
140 str_params.device_type = substream->pcm->device + 1;
141 pr_debug("Playbck stream,Device %d\n",
142 substream->pcm->device);
143 } else {
144 str_params.ops = STREAM_OPS_CAPTURE;
145 str_params.device_type = SND_SST_DEVICE_CAPTURE;
146 pr_debug("Capture stream,Device %d\n",
147 substream->pcm->device);
148 }
149 ret_val = stream->sstdrv_ops->control_set(SST_SND_ALLOC, &str_params);
150 pr_debug("SST_SND_PLAY/CAPTURE ret_val = %x\n", ret_val);
151 if (ret_val < 0)
152 return ret_val;
153
154 stream->stream_info.str_id = ret_val;
155 pr_debug("str id : %d\n", stream->stream_info.str_id);
156
157 return ret_val;
158}
159
160
161static void sst_period_elapsed(void *mad_substream)
162{
163 struct snd_pcm_substream *substream = mad_substream;
164 struct sst_runtime_stream *stream;
165
166 if (!substream || !substream->runtime)
167 return;
168 stream = substream->runtime->private_data;
169 if (!stream)
170 return;
171
172 spin_lock(&stream->status_lock);
173 if (stream->stream_status != SST_PLATFORM_RUNNING) {
174 spin_unlock(&stream->status_lock);
175 return;
176 }
177 spin_unlock(&stream->status_lock);
178 snd_pcm_period_elapsed(substream);
Vinod Kould927fda2011-01-04 20:16:32 +0530179}
180
181static int sst_platform_init_stream(struct snd_pcm_substream *substream)
182{
183 struct sst_runtime_stream *stream =
184 substream->runtime->private_data;
185 int ret_val;
186
187 pr_debug("setting buffer ptr param\n");
188 spin_lock(&stream->status_lock);
189 stream->stream_status = SST_PLATFORM_INIT;
190 spin_unlock(&stream->status_lock);
191 stream->stream_info.period_elapsed = sst_period_elapsed;
192 stream->stream_info.mad_substream = substream;
193 stream->stream_info.buffer_ptr = 0;
194 stream->stream_info.sfreq = substream->runtime->rate;
195 ret_val = stream->sstdrv_ops->control_set(SST_SND_STREAM_INIT,
196 &stream->stream_info);
197 if (ret_val)
198 pr_err("control_set ret error %d\n", ret_val);
199 return ret_val;
200
201}
202/* end -- helper functions */
203
204static int sst_platform_open(struct snd_pcm_substream *substream)
205{
206 struct snd_pcm_runtime *runtime;
207 struct sst_runtime_stream *stream;
208 int ret_val = 0;
209
210 pr_debug("sst_platform_open called\n");
211
212 runtime = substream->runtime;
213 runtime->hw = sst_platform_pcm_hw;
214
215 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
216 if (!stream)
217 return -ENOMEM;
218
219 spin_lock_init(&stream->status_lock);
220 stream->stream_info.str_id = 0;
221
222 spin_lock(&stream->status_lock);
223 stream->stream_status = SST_PLATFORM_INIT;
224 spin_unlock(&stream->status_lock);
225
226 stream->stream_info.mad_substream = substream;
227 /* allocate memory for SST API set */
228 stream->sstdrv_ops = kzalloc(sizeof(*stream->sstdrv_ops),
229 GFP_KERNEL);
230 if (!stream->sstdrv_ops) {
231 pr_err("sst: mem allocation for ops fail\n");
232 kfree(stream);
233 return -ENOMEM;
234 }
235 stream->sstdrv_ops->vendor_id = MSIC_VENDOR_ID;
236
237 /* registering with SST driver to get access to SST APIs to use */
238 ret_val = register_sst_card(stream->sstdrv_ops);
239 if (ret_val) {
240 pr_err("sst: sst card registration failed\n");
241 return ret_val;
242 }
243 runtime->private_data = stream;
244 return snd_pcm_hw_constraint_integer(runtime,
245 SNDRV_PCM_HW_PARAM_PERIODS);
246}
247
248static int sst_platform_close(struct snd_pcm_substream *substream)
249{
250 struct sst_runtime_stream *stream;
251 int ret_val = 0, str_id;
252
253 pr_debug("sst_platform_close called\n");
254
255 stream = substream->runtime->private_data;
256 str_id = stream->stream_info.str_id;
257
258 if (str_id)
259 ret_val = stream->sstdrv_ops->control_set(
260 SST_SND_FREE, &str_id);
261
262 kfree(stream->sstdrv_ops);
263 kfree(stream);
264 return ret_val;
265}
266
267static int sst_platform_pcm_prepare(struct snd_pcm_substream *substream)
268{
269 struct sst_runtime_stream *stream;
270 int ret_val = 0, str_id;
271
272 pr_debug("sst_platform_pcm_prepare called\n");
273
274 stream = substream->runtime->private_data;
275 str_id = stream->stream_info.str_id;
276 if (stream->stream_info.str_id) {
277 ret_val = stream->sstdrv_ops->control_set(
278 SST_SND_DROP, &str_id);
279 return ret_val;
280 }
281
282 ret_val = sst_platform_alloc_stream(substream);
283 if (ret_val < 0)
284 return ret_val;
285 snprintf(substream->pcm->id, sizeof(substream->pcm->id),
286 "%d", stream->stream_info.str_id);
287
288 ret_val = sst_platform_init_stream(substream);
289 if (ret_val)
290 return ret_val;
291 substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
292 return ret_val;
293}
294
295static int sst_platform_pcm_trigger(struct snd_pcm_substream *substream,
296 int cmd)
297{
298 int ret_val = 0, str_id;
299 struct sst_runtime_stream *stream;
300
301 pr_debug("sst_platform_pcm_trigger called\n");
302
303 stream = substream->runtime->private_data;
304
305 str_id = stream->stream_info.str_id;
306
307 switch (cmd) {
308 case SNDRV_PCM_TRIGGER_START:
309 pr_debug("sst: Trigger Start\n");
310 ret_val = stream->sstdrv_ops->control_set(
311 SST_SND_START, &str_id);
312 if (ret_val)
313 break;
314 spin_lock(&stream->status_lock);
315 stream->stream_status = SST_PLATFORM_RUNNING;
316 spin_unlock(&stream->status_lock);
317 stream->stream_info.mad_substream = substream;
318 break;
319 case SNDRV_PCM_TRIGGER_STOP:
320 pr_debug("sst: in stop\n");
321 ret_val = stream->sstdrv_ops->control_set(
322 SST_SND_DROP, &str_id);
323 if (ret_val)
324 break;
325 spin_lock(&stream->status_lock);
326 stream->stream_status = SST_PLATFORM_DROPPED;
327 spin_unlock(&stream->status_lock);
328 break;
329 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
330 pr_debug("sst: in pause\n");
331 ret_val = stream->sstdrv_ops->control_set(
332 SST_SND_PAUSE, &str_id);
333 if (ret_val)
334 break;
335 spin_lock(&stream->status_lock);
336 stream->stream_status = SST_PLATFORM_PAUSED;
337 spin_unlock(&stream->status_lock);
338 break;
339 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
340 pr_debug("sst: in pause release\n");
341 ret_val = stream->sstdrv_ops->control_set(
342 SST_SND_RESUME, &str_id);
343 if (ret_val)
344 break;
345 spin_lock(&stream->status_lock);
346 stream->stream_status = SST_PLATFORM_RUNNING;
347 spin_unlock(&stream->status_lock);
348 break;
349 default:
350 ret_val = -EINVAL;
351 }
352 return ret_val;
353}
354
355
356static snd_pcm_uframes_t sst_platform_pcm_pointer
357 (struct snd_pcm_substream *substream)
358{
359 struct sst_runtime_stream *stream;
360 int ret_val;
361 struct pcm_stream_info *str_info;
362
363
364 stream = substream->runtime->private_data;
365 spin_lock(&stream->status_lock);
366 if (stream->stream_status == SST_PLATFORM_INIT) {
367 spin_unlock(&stream->status_lock);
368 return 0;
369 }
370 spin_unlock(&stream->status_lock);
371
372 str_info = &stream->stream_info;
373 ret_val = stream->sstdrv_ops->control_set(
374 SST_SND_BUFFER_POINTER, str_info);
375 if (ret_val) {
376 pr_err("sst: error code = %d\n", ret_val);
377 return ret_val;
378 }
379
380 return stream->stream_info.buffer_ptr;
381}
382
383
384static struct snd_pcm_ops sst_platform_ops = {
385 .open = sst_platform_open,
386 .close = sst_platform_close,
387 .ioctl = snd_pcm_lib_ioctl,
388 .prepare = sst_platform_pcm_prepare,
389 .trigger = sst_platform_pcm_trigger,
390 .pointer = sst_platform_pcm_pointer,
391};
392
393static void sst_pcm_free(struct snd_pcm *pcm)
394{
395 pr_debug("sst_pcm_free called\n");
396 snd_pcm_lib_preallocate_free_for_all(pcm);
397}
398
399int sst_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
400 struct snd_pcm *pcm)
401{
402 int retval = 0;
403
404 pr_debug("sst_pcm_new called\n");
405
406 if (dai->driver->playback.channels_min ||
407 dai->driver->capture.channels_min) {
408 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
409 SNDRV_DMA_TYPE_CONTINUOUS,
410 snd_dma_continuous_data(GFP_KERNEL),
411 SST_MIN_BUFFER, SST_MAX_BUFFER);
412 if (retval) {
413 pr_err("dma buffer allocationf fail\n");
414 return retval;
415 }
416 }
417
418 return retval;
419}
420struct snd_soc_platform_driver sst_soc_platform_drv = {
421 .ops = &sst_platform_ops,
422 .pcm_new = sst_pcm_new,
423 .pcm_free = sst_pcm_free,
424};
425
426static int sst_platform_probe(struct platform_device *pdev)
427{
428 int ret;
429
430 pr_debug("sst_platform_probe called\n");
431 ret = snd_soc_register_platform(&pdev->dev, &sst_soc_platform_drv);
432 if (ret) {
433 pr_err("registering soc platform failed\n");
434 return ret;
435 }
436 ret = snd_soc_register_dais(&pdev->dev,
437 sst_platform_dai, ARRAY_SIZE(sst_platform_dai));
438 if (ret) {
439 pr_err("registering cpu dais failed\n");
440 snd_soc_unregister_platform(&pdev->dev);
441 }
442 return ret;
443}
444
445static int sst_platform_remove(struct platform_device *pdev)
446{
447
448 snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(sst_platform_dai));
449
450 snd_soc_unregister_platform(&pdev->dev);
451 pr_debug("sst_platform_remove sucess\n");
452
453 return 0;
454}
455
456static struct platform_driver sst_platform_driver = {
457 .driver = {
458 .name = "sst-platform",
459 .owner = THIS_MODULE,
460 },
461 .probe = sst_platform_probe,
462 .remove = sst_platform_remove,
463};
464
465static int __init sst_soc_platform_init(void)
466{
467 pr_debug("sst_soc_platform_init called\n");
468 return platform_driver_register(&sst_platform_driver);
469}
470module_init(sst_soc_platform_init);
471
472static void __exit sst_soc_platform_exit(void)
473{
474 platform_driver_unregister(&sst_platform_driver);
475 pr_debug("sst_soc_platform_exit sucess\n");
476}
477module_exit(sst_soc_platform_exit);
478
479MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");
480MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
481MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
482MODULE_LICENSE("GPL v2");
Vinod Koulf5c6b2a2011-01-07 16:20:56 +0530483MODULE_ALIAS("platform:sst-platform");