2 * sst_platform.c - Intel MID Platform driver
4 * Copyright (C) 2010-2013 Intel Corp
5 * Author: Vinod Koul <vinod.koul@intel.com>
6 * Author: Harsha Priya <priya.harsha@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 #include <linux/slab.h>
30 #include <linux/module.h>
31 #include <sound/core.h>
32 #include <sound/pcm.h>
33 #include <sound/pcm_params.h>
34 #include <sound/soc.h>
35 #include <sound/compress_driver.h>
36 #include "sst_platform.h"
38 static struct sst_device *sst;
39 static DEFINE_MUTEX(sst_lock);
41 int sst_register_dsp(struct sst_device *dev)
45 if (!try_module_get(dev->dev->driver->owner))
47 mutex_lock(&sst_lock);
49 pr_err("we already have a device %s\n", sst->name);
50 module_put(dev->dev->driver->owner);
51 mutex_unlock(&sst_lock);
54 pr_debug("registering device %s\n", dev->name);
56 mutex_unlock(&sst_lock);
59 EXPORT_SYMBOL_GPL(sst_register_dsp);
61 int sst_unregister_dsp(struct sst_device *dev)
68 mutex_lock(&sst_lock);
71 mutex_unlock(&sst_lock);
75 module_put(sst->dev->driver->owner);
76 pr_debug("unreg %s\n", sst->name);
78 mutex_unlock(&sst_lock);
81 EXPORT_SYMBOL_GPL(sst_unregister_dsp);
83 static struct snd_pcm_hardware sst_platform_pcm_hw = {
84 .info = (SNDRV_PCM_INFO_INTERLEAVED |
85 SNDRV_PCM_INFO_DOUBLE |
86 SNDRV_PCM_INFO_PAUSE |
87 SNDRV_PCM_INFO_RESUME |
89 SNDRV_PCM_INFO_MMAP_VALID |
90 SNDRV_PCM_INFO_BLOCK_TRANSFER |
91 SNDRV_PCM_INFO_SYNC_START),
92 .formats = (SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_U16 |
93 SNDRV_PCM_FMTBIT_S24 | SNDRV_PCM_FMTBIT_U24 |
94 SNDRV_PCM_FMTBIT_S32 | SNDRV_PCM_FMTBIT_U32),
95 .rates = (SNDRV_PCM_RATE_8000|
96 SNDRV_PCM_RATE_44100 |
97 SNDRV_PCM_RATE_48000),
98 .rate_min = SST_MIN_RATE,
99 .rate_max = SST_MAX_RATE,
100 .channels_min = SST_MIN_CHANNEL,
101 .channels_max = SST_MAX_CHANNEL,
102 .buffer_bytes_max = SST_MAX_BUFFER,
103 .period_bytes_min = SST_MIN_PERIOD_BYTES,
104 .period_bytes_max = SST_MAX_PERIOD_BYTES,
105 .periods_min = SST_MIN_PERIODS,
106 .periods_max = SST_MAX_PERIODS,
107 .fifo_size = SST_FIFO_SIZE,
111 static struct snd_soc_dai_driver sst_platform_dai[] = {
113 .name = "Headset-cpu-dai",
116 .channels_min = SST_STEREO,
117 .channels_max = SST_STEREO,
118 .rates = SNDRV_PCM_RATE_48000,
119 .formats = SNDRV_PCM_FMTBIT_S24_LE,
124 .rates = SNDRV_PCM_RATE_48000,
125 .formats = SNDRV_PCM_FMTBIT_S24_LE,
129 .name = "Speaker-cpu-dai",
132 .channels_min = SST_MONO,
133 .channels_max = SST_STEREO,
134 .rates = SNDRV_PCM_RATE_48000,
135 .formats = SNDRV_PCM_FMTBIT_S24_LE,
139 .name = "Vibra1-cpu-dai",
142 .channels_min = SST_MONO,
143 .channels_max = SST_MONO,
144 .rates = SNDRV_PCM_RATE_48000,
145 .formats = SNDRV_PCM_FMTBIT_S24_LE,
149 .name = "Vibra2-cpu-dai",
152 .channels_min = SST_MONO,
153 .channels_max = SST_STEREO,
154 .rates = SNDRV_PCM_RATE_48000,
155 .formats = SNDRV_PCM_FMTBIT_S24_LE,
159 .name = "Compress-cpu-dai",
162 .channels_min = SST_STEREO,
163 .channels_max = SST_STEREO,
164 .rates = SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000,
165 .formats = SNDRV_PCM_FMTBIT_S16_LE,
170 static const struct snd_soc_component_driver sst_component = {
174 /* helper functions */
175 static inline void sst_set_stream_status(struct sst_runtime_stream *stream,
179 spin_lock_irqsave(&stream->status_lock, flags);
180 stream->stream_status = state;
181 spin_unlock_irqrestore(&stream->status_lock, flags);
184 static inline int sst_get_stream_status(struct sst_runtime_stream *stream)
189 spin_lock_irqsave(&stream->status_lock, flags);
190 state = stream->stream_status;
191 spin_unlock_irqrestore(&stream->status_lock, flags);
195 static void sst_fill_pcm_params(struct snd_pcm_substream *substream,
196 struct sst_pcm_params *param)
199 param->codec = SST_CODEC_TYPE_PCM;
200 param->num_chan = (u8) substream->runtime->channels;
201 param->pcm_wd_sz = substream->runtime->sample_bits;
203 param->sfreq = substream->runtime->rate;
204 param->ring_buffer_size = snd_pcm_lib_buffer_bytes(substream);
205 param->period_count = substream->runtime->period_size;
206 param->ring_buffer_addr = virt_to_phys(substream->dma_buffer.area);
207 pr_debug("period_cnt = %d\n", param->period_count);
208 pr_debug("sfreq= %d, wd_sz = %d\n", param->sfreq, param->pcm_wd_sz);
211 static int sst_platform_alloc_stream(struct snd_pcm_substream *substream)
213 struct sst_runtime_stream *stream =
214 substream->runtime->private_data;
215 struct sst_pcm_params param = {0};
216 struct sst_stream_params str_params = {0};
219 /* set codec params and inform SST driver the same */
220 sst_fill_pcm_params(substream, ¶m);
221 substream->runtime->dma_area = substream->dma_buffer.area;
222 str_params.sparams = param;
223 str_params.codec = param.codec;
224 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
225 str_params.ops = STREAM_OPS_PLAYBACK;
226 str_params.device_type = substream->pcm->device + 1;
227 pr_debug("Playbck stream,Device %d\n",
228 substream->pcm->device);
230 str_params.ops = STREAM_OPS_CAPTURE;
231 str_params.device_type = SND_SST_DEVICE_CAPTURE;
232 pr_debug("Capture stream,Device %d\n",
233 substream->pcm->device);
235 ret_val = stream->ops->open(&str_params);
236 pr_debug("SST_SND_PLAY/CAPTURE ret_val = %x\n", ret_val);
240 stream->stream_info.str_id = ret_val;
241 pr_debug("str id : %d\n", stream->stream_info.str_id);
245 static void sst_period_elapsed(void *mad_substream)
247 struct snd_pcm_substream *substream = mad_substream;
248 struct sst_runtime_stream *stream;
251 if (!substream || !substream->runtime)
253 stream = substream->runtime->private_data;
256 status = sst_get_stream_status(stream);
257 if (status != SST_PLATFORM_RUNNING)
259 snd_pcm_period_elapsed(substream);
262 static int sst_platform_init_stream(struct snd_pcm_substream *substream)
264 struct sst_runtime_stream *stream =
265 substream->runtime->private_data;
268 pr_debug("setting buffer ptr param\n");
269 sst_set_stream_status(stream, SST_PLATFORM_INIT);
270 stream->stream_info.period_elapsed = sst_period_elapsed;
271 stream->stream_info.mad_substream = substream;
272 stream->stream_info.buffer_ptr = 0;
273 stream->stream_info.sfreq = substream->runtime->rate;
274 ret_val = stream->ops->device_control(
275 SST_SND_STREAM_INIT, &stream->stream_info);
277 pr_err("control_set ret error %d\n", ret_val);
281 /* end -- helper functions */
283 static int sst_platform_open(struct snd_pcm_substream *substream)
285 struct snd_pcm_runtime *runtime = substream->runtime;
286 struct sst_runtime_stream *stream;
289 pr_debug("sst_platform_open called\n");
291 snd_soc_set_runtime_hwparams(substream, &sst_platform_pcm_hw);
292 ret_val = snd_pcm_hw_constraint_integer(runtime,
293 SNDRV_PCM_HW_PARAM_PERIODS);
297 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
300 spin_lock_init(&stream->status_lock);
302 /* get the sst ops */
303 mutex_lock(&sst_lock);
305 pr_err("no device available to run\n");
306 mutex_unlock(&sst_lock);
310 if (!try_module_get(sst->dev->driver->owner)) {
311 mutex_unlock(&sst_lock);
315 stream->ops = sst->ops;
316 mutex_unlock(&sst_lock);
318 stream->stream_info.str_id = 0;
319 sst_set_stream_status(stream, SST_PLATFORM_INIT);
320 stream->stream_info.mad_substream = substream;
321 /* allocate memory for SST API set */
322 runtime->private_data = stream;
327 static int sst_platform_close(struct snd_pcm_substream *substream)
329 struct sst_runtime_stream *stream;
330 int ret_val = 0, str_id;
332 pr_debug("sst_platform_close called\n");
333 stream = substream->runtime->private_data;
334 str_id = stream->stream_info.str_id;
336 ret_val = stream->ops->close(str_id);
337 module_put(sst->dev->driver->owner);
342 static int sst_platform_pcm_prepare(struct snd_pcm_substream *substream)
344 struct sst_runtime_stream *stream;
345 int ret_val = 0, str_id;
347 pr_debug("sst_platform_pcm_prepare called\n");
348 stream = substream->runtime->private_data;
349 str_id = stream->stream_info.str_id;
350 if (stream->stream_info.str_id) {
351 ret_val = stream->ops->device_control(
352 SST_SND_DROP, &str_id);
356 ret_val = sst_platform_alloc_stream(substream);
359 snprintf(substream->pcm->id, sizeof(substream->pcm->id),
360 "%d", stream->stream_info.str_id);
362 ret_val = sst_platform_init_stream(substream);
365 substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
369 static int sst_platform_pcm_trigger(struct snd_pcm_substream *substream,
372 int ret_val = 0, str_id;
373 struct sst_runtime_stream *stream;
376 pr_debug("sst_platform_pcm_trigger called\n");
377 stream = substream->runtime->private_data;
378 str_id = stream->stream_info.str_id;
380 case SNDRV_PCM_TRIGGER_START:
381 pr_debug("sst: Trigger Start\n");
382 str_cmd = SST_SND_START;
383 status = SST_PLATFORM_RUNNING;
384 stream->stream_info.mad_substream = substream;
386 case SNDRV_PCM_TRIGGER_STOP:
387 pr_debug("sst: in stop\n");
388 str_cmd = SST_SND_DROP;
389 status = SST_PLATFORM_DROPPED;
391 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
392 pr_debug("sst: in pause\n");
393 str_cmd = SST_SND_PAUSE;
394 status = SST_PLATFORM_PAUSED;
396 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
397 pr_debug("sst: in pause release\n");
398 str_cmd = SST_SND_RESUME;
399 status = SST_PLATFORM_RUNNING;
404 ret_val = stream->ops->device_control(str_cmd, &str_id);
406 sst_set_stream_status(stream, status);
412 static snd_pcm_uframes_t sst_platform_pcm_pointer
413 (struct snd_pcm_substream *substream)
415 struct sst_runtime_stream *stream;
417 struct pcm_stream_info *str_info;
419 stream = substream->runtime->private_data;
420 status = sst_get_stream_status(stream);
421 if (status == SST_PLATFORM_INIT)
423 str_info = &stream->stream_info;
424 ret_val = stream->ops->device_control(
425 SST_SND_BUFFER_POINTER, str_info);
427 pr_err("sst: error code = %d\n", ret_val);
430 return stream->stream_info.buffer_ptr;
433 static int sst_platform_pcm_hw_params(struct snd_pcm_substream *substream,
434 struct snd_pcm_hw_params *params)
436 snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
437 memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
442 static int sst_platform_pcm_hw_free(struct snd_pcm_substream *substream)
444 return snd_pcm_lib_free_pages(substream);
447 static struct snd_pcm_ops sst_platform_ops = {
448 .open = sst_platform_open,
449 .close = sst_platform_close,
450 .ioctl = snd_pcm_lib_ioctl,
451 .prepare = sst_platform_pcm_prepare,
452 .trigger = sst_platform_pcm_trigger,
453 .pointer = sst_platform_pcm_pointer,
454 .hw_params = sst_platform_pcm_hw_params,
455 .hw_free = sst_platform_pcm_hw_free,
458 static void sst_pcm_free(struct snd_pcm *pcm)
460 pr_debug("sst_pcm_free called\n");
461 snd_pcm_lib_preallocate_free_for_all(pcm);
464 static int sst_pcm_new(struct snd_soc_pcm_runtime *rtd)
466 struct snd_pcm *pcm = rtd->pcm;
469 pr_debug("sst_pcm_new called\n");
470 if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
471 pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
472 retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
473 SNDRV_DMA_TYPE_CONTINUOUS,
474 snd_dma_continuous_data(GFP_KERNEL),
475 SST_MIN_BUFFER, SST_MAX_BUFFER);
477 pr_err("dma buffer allocationf fail\n");
484 /* compress stream operations */
485 static void sst_compr_fragment_elapsed(void *arg)
487 struct snd_compr_stream *cstream = (struct snd_compr_stream *)arg;
489 pr_debug("fragment elapsed by driver\n");
491 snd_compr_fragment_elapsed(cstream);
494 static int sst_platform_compr_open(struct snd_compr_stream *cstream)
498 struct snd_compr_runtime *runtime = cstream->runtime;
499 struct sst_runtime_stream *stream;
501 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
505 spin_lock_init(&stream->status_lock);
507 /* get the sst ops */
508 if (!sst || !try_module_get(sst->dev->driver->owner)) {
509 pr_err("no device available to run\n");
513 stream->compr_ops = sst->compr_ops;
516 sst_set_stream_status(stream, SST_PLATFORM_INIT);
517 runtime->private_data = stream;
524 static int sst_platform_compr_free(struct snd_compr_stream *cstream)
526 struct sst_runtime_stream *stream;
527 int ret_val = 0, str_id;
529 stream = cstream->runtime->private_data;
533 ret_val = stream->compr_ops->close(str_id);
534 module_put(sst->dev->driver->owner);
536 pr_debug("%s: %d\n", __func__, ret_val);
540 static int sst_platform_compr_set_params(struct snd_compr_stream *cstream,
541 struct snd_compr_params *params)
543 struct sst_runtime_stream *stream;
545 struct snd_sst_params str_params;
546 struct sst_compress_cb cb;
548 stream = cstream->runtime->private_data;
549 /* construct fw structure for this*/
550 memset(&str_params, 0, sizeof(str_params));
552 str_params.ops = STREAM_OPS_PLAYBACK;
553 str_params.stream_type = SST_STREAM_TYPE_MUSIC;
554 str_params.device_type = SND_SST_DEVICE_COMPRESS;
556 switch (params->codec.id) {
557 case SND_AUDIOCODEC_MP3: {
558 str_params.codec = SST_CODEC_TYPE_MP3;
559 str_params.sparams.uc.mp3_params.codec = SST_CODEC_TYPE_MP3;
560 str_params.sparams.uc.mp3_params.num_chan = params->codec.ch_in;
561 str_params.sparams.uc.mp3_params.pcm_wd_sz = 16;
565 case SND_AUDIOCODEC_AAC: {
566 str_params.codec = SST_CODEC_TYPE_AAC;
567 str_params.sparams.uc.aac_params.codec = SST_CODEC_TYPE_AAC;
568 str_params.sparams.uc.aac_params.num_chan = params->codec.ch_in;
569 str_params.sparams.uc.aac_params.pcm_wd_sz = 16;
570 if (params->codec.format == SND_AUDIOSTREAMFORMAT_MP4ADTS)
571 str_params.sparams.uc.aac_params.bs_format =
573 else if (params->codec.format == SND_AUDIOSTREAMFORMAT_RAW)
574 str_params.sparams.uc.aac_params.bs_format =
577 pr_err("Undefined format%d\n", params->codec.format);
580 str_params.sparams.uc.aac_params.externalsr =
581 params->codec.sample_rate;
586 pr_err("codec not supported, id =%d\n", params->codec.id);
590 str_params.aparams.ring_buf_info[0].addr =
591 virt_to_phys(cstream->runtime->buffer);
592 str_params.aparams.ring_buf_info[0].size =
593 cstream->runtime->buffer_size;
594 str_params.aparams.sg_count = 1;
595 str_params.aparams.frag_size = cstream->runtime->fragment_size;
598 cb.compr_cb = sst_compr_fragment_elapsed;
600 retval = stream->compr_ops->open(&str_params, &cb);
602 pr_err("stream allocation failed %d\n", retval);
610 static int sst_platform_compr_trigger(struct snd_compr_stream *cstream, int cmd)
612 struct sst_runtime_stream *stream =
613 cstream->runtime->private_data;
615 return stream->compr_ops->control(cmd, stream->id);
618 static int sst_platform_compr_pointer(struct snd_compr_stream *cstream,
619 struct snd_compr_tstamp *tstamp)
621 struct sst_runtime_stream *stream;
623 stream = cstream->runtime->private_data;
624 stream->compr_ops->tstamp(stream->id, tstamp);
625 tstamp->byte_offset = tstamp->copied_total %
626 (u32)cstream->runtime->buffer_size;
627 pr_debug("calc bytes offset/copied bytes as %d\n", tstamp->byte_offset);
631 static int sst_platform_compr_ack(struct snd_compr_stream *cstream,
634 struct sst_runtime_stream *stream;
636 stream = cstream->runtime->private_data;
637 stream->compr_ops->ack(stream->id, (unsigned long)bytes);
638 stream->bytes_written += bytes;
643 static int sst_platform_compr_get_caps(struct snd_compr_stream *cstream,
644 struct snd_compr_caps *caps)
646 struct sst_runtime_stream *stream =
647 cstream->runtime->private_data;
649 return stream->compr_ops->get_caps(caps);
652 static int sst_platform_compr_get_codec_caps(struct snd_compr_stream *cstream,
653 struct snd_compr_codec_caps *codec)
655 struct sst_runtime_stream *stream =
656 cstream->runtime->private_data;
658 return stream->compr_ops->get_codec_caps(codec);
661 static int sst_platform_compr_set_metadata(struct snd_compr_stream *cstream,
662 struct snd_compr_metadata *metadata)
664 struct sst_runtime_stream *stream =
665 cstream->runtime->private_data;
667 return stream->compr_ops->set_metadata(stream->id, metadata);
670 static struct snd_compr_ops sst_platform_compr_ops = {
672 .open = sst_platform_compr_open,
673 .free = sst_platform_compr_free,
674 .set_params = sst_platform_compr_set_params,
675 .set_metadata = sst_platform_compr_set_metadata,
676 .trigger = sst_platform_compr_trigger,
677 .pointer = sst_platform_compr_pointer,
678 .ack = sst_platform_compr_ack,
679 .get_caps = sst_platform_compr_get_caps,
680 .get_codec_caps = sst_platform_compr_get_codec_caps,
683 static struct snd_soc_platform_driver sst_soc_platform_drv = {
684 .ops = &sst_platform_ops,
685 .compr_ops = &sst_platform_compr_ops,
686 .pcm_new = sst_pcm_new,
687 .pcm_free = sst_pcm_free,
690 static int sst_platform_probe(struct platform_device *pdev)
694 pr_debug("sst_platform_probe called\n");
696 ret = snd_soc_register_platform(&pdev->dev, &sst_soc_platform_drv);
698 pr_err("registering soc platform failed\n");
702 ret = snd_soc_register_component(&pdev->dev, &sst_component,
703 sst_platform_dai, ARRAY_SIZE(sst_platform_dai));
705 pr_err("registering cpu dais failed\n");
706 snd_soc_unregister_platform(&pdev->dev);
711 static int sst_platform_remove(struct platform_device *pdev)
714 snd_soc_unregister_component(&pdev->dev);
715 snd_soc_unregister_platform(&pdev->dev);
716 pr_debug("sst_platform_remove success\n");
720 static struct platform_driver sst_platform_driver = {
722 .name = "sst-platform",
723 .owner = THIS_MODULE,
725 .probe = sst_platform_probe,
726 .remove = sst_platform_remove,
729 module_platform_driver(sst_platform_driver);
731 MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");
732 MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
733 MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
734 MODULE_LICENSE("GPL v2");
735 MODULE_ALIAS("platform:sst-platform");