2 * sst_drv_interface.c - Intel SST Driver for audio engine
4 * Copyright (C) 2008-14 Intel Corp
5 * Authors: Vinod Koul <vinod.koul@intel.com>
6 * Harsha Priya <priya.harsha@intel.com>
7 * Dharageswari R <dharageswari.r@intel.com)
8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21 #include <linux/delay.h>
22 #include <linux/pci.h>
24 #include <linux/firmware.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/pm_qos.h>
27 #include <linux/math64.h>
28 #include <sound/core.h>
29 #include <sound/pcm.h>
30 #include <sound/soc.h>
31 #include <sound/compress_driver.h>
32 #include <asm/platform_sst_audio.h>
33 #include "../sst-mfld-platform.h"
35 #include "../../common/sst-dsp.h"
40 #define MIN_FRAGMENT 2
41 #define MAX_FRAGMENT 4
42 #define MIN_FRAGMENT_SIZE (50 * 1024)
43 #define MAX_FRAGMENT_SIZE (1024 * 1024)
44 #define SST_GET_BYTES_PER_SAMPLE(pcm_wd_sz) (((pcm_wd_sz + 15) >> 4) << 1)
46 #define GET_USAGE_COUNT(dev) (atomic_read(&dev->power.usage_count))
48 #define GET_USAGE_COUNT(dev) 1
51 int free_stream_context(struct intel_sst_drv *ctx, unsigned int str_id)
53 struct stream_info *stream;
56 stream = get_stream_info(ctx, str_id);
58 /* str_id is valid, so stream is alloacted */
59 ret = sst_free_stream(ctx, str_id);
61 sst_clean_stream(&ctx->streams[str_id]);
64 dev_err(ctx->dev, "we tried to free stream context %d which was freed!!!\n", str_id);
69 int sst_get_stream_allocated(struct intel_sst_drv *ctx,
70 struct snd_sst_params *str_param,
71 struct snd_sst_lib_download **lib_dnld)
75 retval = ctx->ops->alloc_stream(ctx, str_param);
77 dev_dbg(ctx->dev, "Stream allocated %d\n", retval);
83 * sst_get_sfreq - this function returns the frequency of the stream
85 * @str_param : stream params
87 int sst_get_sfreq(struct snd_sst_params *str_param)
89 switch (str_param->codec) {
90 case SST_CODEC_TYPE_PCM:
91 return str_param->sparams.uc.pcm_params.sfreq;
92 case SST_CODEC_TYPE_AAC:
93 return str_param->sparams.uc.aac_params.externalsr;
94 case SST_CODEC_TYPE_MP3:
102 * sst_get_num_channel - get number of channels for the stream
104 * @str_param : stream params
106 int sst_get_num_channel(struct snd_sst_params *str_param)
108 switch (str_param->codec) {
109 case SST_CODEC_TYPE_PCM:
110 return str_param->sparams.uc.pcm_params.num_chan;
111 case SST_CODEC_TYPE_MP3:
112 return str_param->sparams.uc.mp3_params.num_chan;
113 case SST_CODEC_TYPE_AAC:
114 return str_param->sparams.uc.aac_params.num_chan;
121 * sst_get_stream - this function prepares for stream allocation
123 * @str_param : stream param
125 int sst_get_stream(struct intel_sst_drv *ctx,
126 struct snd_sst_params *str_param)
129 struct stream_info *str_info;
131 /* stream is not allocated, we are allocating */
132 retval = ctx->ops->alloc_stream(ctx, str_param);
136 /* store sampling freq */
137 str_info = &ctx->streams[retval];
138 str_info->sfreq = sst_get_sfreq(str_param);
143 static int sst_power_control(struct device *dev, bool state)
145 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
150 ret = pm_runtime_get_sync(dev);
151 usage_count = GET_USAGE_COUNT(dev);
152 dev_dbg(ctx->dev, "Enable: pm usage count: %d\n", usage_count);
154 pm_runtime_put_sync(dev);
155 dev_err(ctx->dev, "Runtime get failed with err: %d\n", ret);
158 if ((ctx->sst_state == SST_RESET) && (usage_count == 1)) {
159 ret = sst_load_fw(ctx);
161 dev_err(dev, "FW download fail %d\n", ret);
162 sst_set_fw_state_locked(ctx, SST_RESET);
163 ret = sst_pm_runtime_put(ctx);
167 usage_count = GET_USAGE_COUNT(dev);
168 dev_dbg(ctx->dev, "Disable: pm usage count: %d\n", usage_count);
169 return sst_pm_runtime_put(ctx);
175 * sst_open_pcm_stream - Open PCM interface
177 * @str_param: parameters of pcm stream
179 * This function is called by MID sound card driver to open
180 * a new pcm interface
182 static int sst_open_pcm_stream(struct device *dev,
183 struct snd_sst_params *str_param)
186 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
191 retval = sst_get_stream(ctx, str_param);
195 dev_err(ctx->dev, "sst_get_stream returned err %d\n", retval);
200 static int sst_cdev_open(struct device *dev,
201 struct snd_sst_params *str_params, struct sst_compress_cb *cb)
204 struct stream_info *stream;
205 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
207 retval = pm_runtime_get_sync(ctx->dev);
209 pm_runtime_put_sync(ctx->dev);
213 str_id = sst_get_stream(ctx, str_params);
215 dev_dbg(dev, "stream allocated in sst_cdev_open %d\n", str_id);
216 stream = &ctx->streams[str_id];
217 stream->compr_cb = cb->compr_cb;
218 stream->compr_cb_param = cb->param;
219 stream->drain_notify = cb->drain_notify;
220 stream->drain_cb_param = cb->drain_cb_param;
222 dev_err(dev, "stream encountered error during alloc %d\n", str_id);
224 sst_pm_runtime_put(ctx);
229 static int sst_cdev_close(struct device *dev, unsigned int str_id)
232 struct stream_info *stream;
233 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
235 stream = get_stream_info(ctx, str_id);
237 dev_err(dev, "stream info is NULL for str %d!!!\n", str_id);
241 if (stream->status == STREAM_RESET) {
242 dev_dbg(dev, "stream in reset state...\n");
243 stream->status = STREAM_UN_INIT;
249 retval = sst_free_stream(ctx, str_id);
251 stream->compr_cb_param = NULL;
252 stream->compr_cb = NULL;
255 dev_err(dev, "free stream returned err %d\n", retval);
257 dev_dbg(dev, "End\n");
262 static int sst_cdev_ack(struct device *dev, unsigned int str_id,
265 struct stream_info *stream;
266 struct snd_sst_tstamp fw_tstamp = {0,};
269 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
271 stream = get_stream_info(ctx, str_id);
275 /* update bytes sent */
276 stream->cumm_bytes += bytes;
277 dev_dbg(dev, "bytes copied %d inc by %ld\n", stream->cumm_bytes, bytes);
279 memcpy_fromio(&fw_tstamp,
280 ((void *)(ctx->mailbox + ctx->tstamp)
281 +(str_id * sizeof(fw_tstamp))),
284 fw_tstamp.bytes_copied = stream->cumm_bytes;
285 dev_dbg(dev, "bytes sent to fw %llu inc by %ld\n",
286 fw_tstamp.bytes_copied, bytes);
288 addr = ((void *)(ctx->mailbox + ctx->tstamp)) +
289 (str_id * sizeof(fw_tstamp));
290 offset = offsetof(struct snd_sst_tstamp, bytes_copied);
291 sst_shim_write(addr, offset, fw_tstamp.bytes_copied);
295 static int sst_cdev_set_metadata(struct device *dev,
296 unsigned int str_id, struct snd_compr_metadata *metadata)
299 struct stream_info *str_info;
300 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
302 dev_dbg(dev, "set metadata for stream %d\n", str_id);
304 str_info = get_stream_info(ctx, str_id);
308 dev_dbg(dev, "pipe id = %d\n", str_info->pipe_id);
309 retval = sst_prepare_and_post_msg(ctx, str_info->task_id, IPC_CMD,
310 IPC_IA_SET_STREAM_PARAMS_MRFLD, str_info->pipe_id,
311 sizeof(*metadata), metadata, NULL,
312 true, true, true, false);
317 static int sst_cdev_stream_pause(struct device *dev, unsigned int str_id)
319 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
321 return sst_pause_stream(ctx, str_id);
324 static int sst_cdev_stream_pause_release(struct device *dev,
327 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
329 return sst_resume_stream(ctx, str_id);
332 static int sst_cdev_stream_start(struct device *dev, unsigned int str_id)
334 struct stream_info *str_info;
335 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
337 str_info = get_stream_info(ctx, str_id);
340 str_info->prev = str_info->status;
341 str_info->status = STREAM_RUNNING;
342 return sst_start_stream(ctx, str_id);
345 static int sst_cdev_stream_drop(struct device *dev, unsigned int str_id)
347 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
349 return sst_drop_stream(ctx, str_id);
352 static int sst_cdev_stream_drain(struct device *dev, unsigned int str_id)
354 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
356 return sst_drain_stream(ctx, str_id, false);
359 static int sst_cdev_stream_partial_drain(struct device *dev,
362 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
364 return sst_drain_stream(ctx, str_id, true);
367 static int sst_cdev_tstamp(struct device *dev, unsigned int str_id,
368 struct snd_compr_tstamp *tstamp)
370 struct snd_sst_tstamp fw_tstamp = {0,};
371 struct stream_info *stream;
372 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
374 memcpy_fromio(&fw_tstamp,
375 ((void *)(ctx->mailbox + ctx->tstamp)
376 +(str_id * sizeof(fw_tstamp))),
379 stream = get_stream_info(ctx, str_id);
382 dev_dbg(dev, "rb_counter %llu in bytes\n", fw_tstamp.ring_buffer_counter);
384 tstamp->copied_total = fw_tstamp.ring_buffer_counter;
385 tstamp->pcm_frames = fw_tstamp.frames_decoded;
386 tstamp->pcm_io_frames = div_u64(fw_tstamp.hardware_counter,
387 (u64)stream->num_ch * SST_GET_BYTES_PER_SAMPLE(24));
388 tstamp->sampling_rate = fw_tstamp.sampling_frequency;
390 dev_dbg(dev, "PCM = %u\n", tstamp->pcm_io_frames);
391 dev_dbg(dev, "Ptr Query on strid = %d copied_total %d, decodec %d\n",
392 str_id, tstamp->copied_total, tstamp->pcm_frames);
393 dev_dbg(dev, "rendered %d\n", tstamp->pcm_io_frames);
398 static int sst_cdev_caps(struct snd_compr_caps *caps)
400 caps->num_codecs = NUM_CODEC;
401 caps->min_fragment_size = MIN_FRAGMENT_SIZE; /* 50KB */
402 caps->max_fragment_size = MAX_FRAGMENT_SIZE; /* 1024KB */
403 caps->min_fragments = MIN_FRAGMENT;
404 caps->max_fragments = MAX_FRAGMENT;
405 caps->codecs[0] = SND_AUDIOCODEC_MP3;
406 caps->codecs[1] = SND_AUDIOCODEC_AAC;
410 static struct snd_compr_codec_caps caps_mp3 = {
411 .num_descriptors = 1,
412 .descriptor[0].max_ch = 2,
413 .descriptor[0].sample_rates[0] = 48000,
414 .descriptor[0].sample_rates[1] = 44100,
415 .descriptor[0].sample_rates[2] = 32000,
416 .descriptor[0].sample_rates[3] = 16000,
417 .descriptor[0].sample_rates[4] = 8000,
418 .descriptor[0].num_sample_rates = 5,
419 .descriptor[0].bit_rate[0] = 320,
420 .descriptor[0].bit_rate[1] = 192,
421 .descriptor[0].num_bitrates = 2,
422 .descriptor[0].profiles = 0,
423 .descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO,
424 .descriptor[0].formats = 0,
427 static struct snd_compr_codec_caps caps_aac = {
428 .num_descriptors = 2,
429 .descriptor[1].max_ch = 2,
430 .descriptor[0].sample_rates[0] = 48000,
431 .descriptor[0].sample_rates[1] = 44100,
432 .descriptor[0].sample_rates[2] = 32000,
433 .descriptor[0].sample_rates[3] = 16000,
434 .descriptor[0].sample_rates[4] = 8000,
435 .descriptor[0].num_sample_rates = 5,
436 .descriptor[1].bit_rate[0] = 320,
437 .descriptor[1].bit_rate[1] = 192,
438 .descriptor[1].num_bitrates = 2,
439 .descriptor[1].profiles = 0,
440 .descriptor[1].modes = 0,
441 .descriptor[1].formats =
442 (SND_AUDIOSTREAMFORMAT_MP4ADTS |
443 SND_AUDIOSTREAMFORMAT_RAW),
446 static int sst_cdev_codec_caps(struct snd_compr_codec_caps *codec)
448 if (codec->codec == SND_AUDIOCODEC_MP3)
450 else if (codec->codec == SND_AUDIOCODEC_AAC)
458 void sst_cdev_fragment_elapsed(struct intel_sst_drv *ctx, int str_id)
460 struct stream_info *stream;
462 dev_dbg(ctx->dev, "fragment elapsed from firmware for str_id %d\n",
464 stream = &ctx->streams[str_id];
465 if (stream->compr_cb)
466 stream->compr_cb(stream->compr_cb_param);
470 * sst_close_pcm_stream - Close PCM interface
472 * @str_id: stream id to be closed
474 * This function is called by MID sound card driver to close
475 * an existing pcm interface
477 static int sst_close_pcm_stream(struct device *dev, unsigned int str_id)
479 struct stream_info *stream;
481 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
483 stream = get_stream_info(ctx, str_id);
485 dev_err(ctx->dev, "stream info is NULL for str %d!!!\n", str_id);
489 if (stream->status == STREAM_RESET) {
490 /* silently fail here as we have cleaned the stream earlier */
491 dev_dbg(ctx->dev, "stream in reset state...\n");
497 retval = free_stream_context(ctx, str_id);
499 stream->pcm_substream = NULL;
500 stream->status = STREAM_UN_INIT;
501 stream->period_elapsed = NULL;
505 dev_err(ctx->dev, "free stream returned err %d\n", retval);
507 dev_dbg(ctx->dev, "Exit\n");
511 static inline int sst_calc_tstamp(struct intel_sst_drv *ctx,
512 struct pcm_stream_info *info,
513 struct snd_pcm_substream *substream,
514 struct snd_sst_tstamp *fw_tstamp)
516 size_t delay_bytes, delay_frames;
518 u32 pointer_bytes, pointer_samples;
520 dev_dbg(ctx->dev, "mrfld ring_buffer_counter %llu in bytes\n",
521 fw_tstamp->ring_buffer_counter);
522 dev_dbg(ctx->dev, "mrfld hardware_counter %llu in bytes\n",
523 fw_tstamp->hardware_counter);
524 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
525 delay_bytes = (size_t) (fw_tstamp->ring_buffer_counter -
526 fw_tstamp->hardware_counter);
528 delay_bytes = (size_t) (fw_tstamp->hardware_counter -
529 fw_tstamp->ring_buffer_counter);
530 delay_frames = bytes_to_frames(substream->runtime, delay_bytes);
531 buffer_sz = snd_pcm_lib_buffer_bytes(substream);
532 div_u64_rem(fw_tstamp->ring_buffer_counter, buffer_sz, &pointer_bytes);
533 pointer_samples = bytes_to_samples(substream->runtime, pointer_bytes);
535 dev_dbg(ctx->dev, "pcm delay %zu in bytes\n", delay_bytes);
537 info->buffer_ptr = pointer_samples / substream->runtime->channels;
539 info->pcm_delay = delay_frames;
540 dev_dbg(ctx->dev, "buffer ptr %llu pcm_delay rep: %llu\n",
541 info->buffer_ptr, info->pcm_delay);
545 static int sst_read_timestamp(struct device *dev, struct pcm_stream_info *info)
547 struct stream_info *stream;
548 struct snd_pcm_substream *substream;
549 struct snd_sst_tstamp fw_tstamp;
551 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
553 str_id = info->str_id;
554 stream = get_stream_info(ctx, str_id);
558 if (!stream->pcm_substream)
560 substream = stream->pcm_substream;
562 memcpy_fromio(&fw_tstamp,
563 ((void *)(ctx->mailbox + ctx->tstamp)
564 + (str_id * sizeof(fw_tstamp))),
566 return sst_calc_tstamp(ctx, info, substream, &fw_tstamp);
569 static int sst_stream_start(struct device *dev, int str_id)
571 struct stream_info *str_info;
572 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
574 if (ctx->sst_state != SST_FW_RUNNING)
576 str_info = get_stream_info(ctx, str_id);
579 str_info->prev = str_info->status;
580 str_info->status = STREAM_RUNNING;
581 sst_start_stream(ctx, str_id);
586 static int sst_stream_drop(struct device *dev, int str_id)
588 struct stream_info *str_info;
589 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
591 if (ctx->sst_state != SST_FW_RUNNING)
594 str_info = get_stream_info(ctx, str_id);
597 str_info->prev = STREAM_UN_INIT;
598 str_info->status = STREAM_INIT;
599 return sst_drop_stream(ctx, str_id);
602 static int sst_stream_pause(struct device *dev, int str_id)
604 struct stream_info *str_info;
605 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
607 if (ctx->sst_state != SST_FW_RUNNING)
610 str_info = get_stream_info(ctx, str_id);
614 return sst_pause_stream(ctx, str_id);
617 static int sst_stream_resume(struct device *dev, int str_id)
619 struct stream_info *str_info;
620 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
622 if (ctx->sst_state != SST_FW_RUNNING)
625 str_info = get_stream_info(ctx, str_id);
628 return sst_resume_stream(ctx, str_id);
631 static int sst_stream_init(struct device *dev, struct pcm_stream_info *str_info)
634 struct stream_info *stream;
635 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
637 str_id = str_info->str_id;
639 if (ctx->sst_state != SST_FW_RUNNING)
642 stream = get_stream_info(ctx, str_id);
646 dev_dbg(ctx->dev, "setting the period ptrs\n");
647 stream->pcm_substream = str_info->arg;
648 stream->period_elapsed = str_info->period_elapsed;
649 stream->sfreq = str_info->sfreq;
650 stream->prev = stream->status;
651 stream->status = STREAM_INIT;
653 "pcm_substream %p, period_elapsed %p, sfreq %d, status %d\n",
654 stream->pcm_substream, stream->period_elapsed,
655 stream->sfreq, stream->status);
661 * sst_set_byte_stream - Set generic params
663 * @cmd: control cmd to be set
664 * @arg: command argument
666 * This function is called by MID sound card driver to configure
667 * SST runtime params.
669 static int sst_send_byte_stream(struct device *dev,
670 struct snd_sst_bytes_v2 *bytes)
673 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
677 ret_val = pm_runtime_get_sync(ctx->dev);
679 pm_runtime_put_sync(ctx->dev);
683 ret_val = sst_send_byte_stream_mrfld(ctx, bytes);
684 sst_pm_runtime_put(ctx);
689 static struct sst_ops pcm_ops = {
690 .open = sst_open_pcm_stream,
691 .stream_init = sst_stream_init,
692 .stream_start = sst_stream_start,
693 .stream_drop = sst_stream_drop,
694 .stream_pause = sst_stream_pause,
695 .stream_pause_release = sst_stream_resume,
696 .stream_read_tstamp = sst_read_timestamp,
697 .send_byte_stream = sst_send_byte_stream,
698 .close = sst_close_pcm_stream,
699 .power = sst_power_control,
702 static struct compress_sst_ops compr_ops = {
703 .open = sst_cdev_open,
704 .close = sst_cdev_close,
705 .stream_pause = sst_cdev_stream_pause,
706 .stream_pause_release = sst_cdev_stream_pause_release,
707 .stream_start = sst_cdev_stream_start,
708 .stream_drop = sst_cdev_stream_drop,
709 .stream_drain = sst_cdev_stream_drain,
710 .stream_partial_drain = sst_cdev_stream_partial_drain,
711 .tstamp = sst_cdev_tstamp,
713 .get_caps = sst_cdev_caps,
714 .get_codec_caps = sst_cdev_codec_caps,
715 .set_metadata = sst_cdev_set_metadata,
716 .power = sst_power_control,
719 static struct sst_device sst_dsp_device = {
720 .name = "Intel(R) SST LPE",
723 .compr_ops = &compr_ops,
727 * sst_register - function to register DSP
729 * This functions registers DSP with the platform driver
731 int sst_register(struct device *dev)
735 sst_dsp_device.dev = dev;
736 ret_val = sst_register_dsp(&sst_dsp_device);
738 dev_err(dev, "Unable to register DSP with platform driver\n");
743 int sst_unregister(struct device *dev)
745 return sst_unregister_dsp(&sst_dsp_device);