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 retval = sst_free_stream(ctx, str_id);
242 stream->compr_cb_param = NULL;
243 stream->compr_cb = NULL;
246 dev_err(dev, "free stream returned err %d\n", retval);
248 dev_dbg(dev, "End\n");
252 static int sst_cdev_ack(struct device *dev, unsigned int str_id,
255 struct stream_info *stream;
256 struct snd_sst_tstamp fw_tstamp = {0,};
259 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
261 stream = get_stream_info(ctx, str_id);
265 /* update bytes sent */
266 stream->cumm_bytes += bytes;
267 dev_dbg(dev, "bytes copied %d inc by %ld\n", stream->cumm_bytes, bytes);
269 addr = ((void __iomem *)(ctx->mailbox + ctx->tstamp)) +
270 (str_id * sizeof(fw_tstamp));
272 memcpy_fromio(&fw_tstamp, addr, sizeof(fw_tstamp));
274 fw_tstamp.bytes_copied = stream->cumm_bytes;
275 dev_dbg(dev, "bytes sent to fw %llu inc by %ld\n",
276 fw_tstamp.bytes_copied, bytes);
278 offset = offsetof(struct snd_sst_tstamp, bytes_copied);
279 sst_shim_write(addr, offset, fw_tstamp.bytes_copied);
283 static int sst_cdev_set_metadata(struct device *dev,
284 unsigned int str_id, struct snd_compr_metadata *metadata)
287 struct stream_info *str_info;
288 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
290 dev_dbg(dev, "set metadata for stream %d\n", str_id);
292 str_info = get_stream_info(ctx, str_id);
296 dev_dbg(dev, "pipe id = %d\n", str_info->pipe_id);
297 retval = sst_prepare_and_post_msg(ctx, str_info->task_id, IPC_CMD,
298 IPC_IA_SET_STREAM_PARAMS_MRFLD, str_info->pipe_id,
299 sizeof(*metadata), metadata, NULL,
300 true, true, true, false);
305 static int sst_cdev_stream_pause(struct device *dev, unsigned int str_id)
307 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
309 return sst_pause_stream(ctx, str_id);
312 static int sst_cdev_stream_pause_release(struct device *dev,
315 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
317 return sst_resume_stream(ctx, str_id);
320 static int sst_cdev_stream_start(struct device *dev, unsigned int str_id)
322 struct stream_info *str_info;
323 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
325 str_info = get_stream_info(ctx, str_id);
328 str_info->prev = str_info->status;
329 str_info->status = STREAM_RUNNING;
330 return sst_start_stream(ctx, str_id);
333 static int sst_cdev_stream_drop(struct device *dev, unsigned int str_id)
335 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
337 return sst_drop_stream(ctx, str_id);
340 static int sst_cdev_stream_drain(struct device *dev, unsigned int str_id)
342 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
344 return sst_drain_stream(ctx, str_id, false);
347 static int sst_cdev_stream_partial_drain(struct device *dev,
350 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
352 return sst_drain_stream(ctx, str_id, true);
355 static int sst_cdev_tstamp(struct device *dev, unsigned int str_id,
356 struct snd_compr_tstamp *tstamp)
358 struct snd_sst_tstamp fw_tstamp = {0,};
359 struct stream_info *stream;
360 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
363 addr = (void __iomem *)(ctx->mailbox + ctx->tstamp) +
364 (str_id * sizeof(fw_tstamp));
366 memcpy_fromio(&fw_tstamp, addr, sizeof(fw_tstamp));
368 stream = get_stream_info(ctx, str_id);
371 dev_dbg(dev, "rb_counter %llu in bytes\n", fw_tstamp.ring_buffer_counter);
373 tstamp->copied_total = fw_tstamp.ring_buffer_counter;
374 tstamp->pcm_frames = fw_tstamp.frames_decoded;
375 tstamp->pcm_io_frames = div_u64(fw_tstamp.hardware_counter,
376 (u64)stream->num_ch * SST_GET_BYTES_PER_SAMPLE(24));
377 tstamp->sampling_rate = fw_tstamp.sampling_frequency;
379 dev_dbg(dev, "PCM = %u\n", tstamp->pcm_io_frames);
380 dev_dbg(dev, "Ptr Query on strid = %d copied_total %d, decodec %d\n",
381 str_id, tstamp->copied_total, tstamp->pcm_frames);
382 dev_dbg(dev, "rendered %d\n", tstamp->pcm_io_frames);
387 static int sst_cdev_caps(struct snd_compr_caps *caps)
389 caps->num_codecs = NUM_CODEC;
390 caps->min_fragment_size = MIN_FRAGMENT_SIZE; /* 50KB */
391 caps->max_fragment_size = MAX_FRAGMENT_SIZE; /* 1024KB */
392 caps->min_fragments = MIN_FRAGMENT;
393 caps->max_fragments = MAX_FRAGMENT;
394 caps->codecs[0] = SND_AUDIOCODEC_MP3;
395 caps->codecs[1] = SND_AUDIOCODEC_AAC;
399 static const struct snd_compr_codec_caps caps_mp3 = {
400 .num_descriptors = 1,
401 .descriptor[0].max_ch = 2,
402 .descriptor[0].sample_rates[0] = 48000,
403 .descriptor[0].sample_rates[1] = 44100,
404 .descriptor[0].sample_rates[2] = 32000,
405 .descriptor[0].sample_rates[3] = 16000,
406 .descriptor[0].sample_rates[4] = 8000,
407 .descriptor[0].num_sample_rates = 5,
408 .descriptor[0].bit_rate[0] = 320,
409 .descriptor[0].bit_rate[1] = 192,
410 .descriptor[0].num_bitrates = 2,
411 .descriptor[0].profiles = 0,
412 .descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO,
413 .descriptor[0].formats = 0,
416 static const struct snd_compr_codec_caps caps_aac = {
417 .num_descriptors = 2,
418 .descriptor[1].max_ch = 2,
419 .descriptor[0].sample_rates[0] = 48000,
420 .descriptor[0].sample_rates[1] = 44100,
421 .descriptor[0].sample_rates[2] = 32000,
422 .descriptor[0].sample_rates[3] = 16000,
423 .descriptor[0].sample_rates[4] = 8000,
424 .descriptor[0].num_sample_rates = 5,
425 .descriptor[1].bit_rate[0] = 320,
426 .descriptor[1].bit_rate[1] = 192,
427 .descriptor[1].num_bitrates = 2,
428 .descriptor[1].profiles = 0,
429 .descriptor[1].modes = 0,
430 .descriptor[1].formats =
431 (SND_AUDIOSTREAMFORMAT_MP4ADTS |
432 SND_AUDIOSTREAMFORMAT_RAW),
435 static int sst_cdev_codec_caps(struct snd_compr_codec_caps *codec)
437 if (codec->codec == SND_AUDIOCODEC_MP3)
439 else if (codec->codec == SND_AUDIOCODEC_AAC)
447 void sst_cdev_fragment_elapsed(struct intel_sst_drv *ctx, int str_id)
449 struct stream_info *stream;
451 dev_dbg(ctx->dev, "fragment elapsed from firmware for str_id %d\n",
453 stream = &ctx->streams[str_id];
454 if (stream->compr_cb)
455 stream->compr_cb(stream->compr_cb_param);
459 * sst_close_pcm_stream - Close PCM interface
461 * @str_id: stream id to be closed
463 * This function is called by MID sound card driver to close
464 * an existing pcm interface
466 static int sst_close_pcm_stream(struct device *dev, unsigned int str_id)
468 struct stream_info *stream;
470 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
472 stream = get_stream_info(ctx, str_id);
474 dev_err(ctx->dev, "stream info is NULL for str %d!!!\n", str_id);
478 retval = free_stream_context(ctx, str_id);
479 stream->pcm_substream = NULL;
480 stream->status = STREAM_UN_INIT;
481 stream->period_elapsed = NULL;
485 dev_err(ctx->dev, "free stream returned err %d\n", retval);
487 dev_dbg(ctx->dev, "Exit\n");
491 static inline int sst_calc_tstamp(struct intel_sst_drv *ctx,
492 struct pcm_stream_info *info,
493 struct snd_pcm_substream *substream,
494 struct snd_sst_tstamp *fw_tstamp)
496 size_t delay_bytes, delay_frames;
498 u32 pointer_bytes, pointer_samples;
500 dev_dbg(ctx->dev, "mrfld ring_buffer_counter %llu in bytes\n",
501 fw_tstamp->ring_buffer_counter);
502 dev_dbg(ctx->dev, "mrfld hardware_counter %llu in bytes\n",
503 fw_tstamp->hardware_counter);
504 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
505 delay_bytes = (size_t) (fw_tstamp->ring_buffer_counter -
506 fw_tstamp->hardware_counter);
508 delay_bytes = (size_t) (fw_tstamp->hardware_counter -
509 fw_tstamp->ring_buffer_counter);
510 delay_frames = bytes_to_frames(substream->runtime, delay_bytes);
511 buffer_sz = snd_pcm_lib_buffer_bytes(substream);
512 div_u64_rem(fw_tstamp->ring_buffer_counter, buffer_sz, &pointer_bytes);
513 pointer_samples = bytes_to_samples(substream->runtime, pointer_bytes);
515 dev_dbg(ctx->dev, "pcm delay %zu in bytes\n", delay_bytes);
517 info->buffer_ptr = pointer_samples / substream->runtime->channels;
519 info->pcm_delay = delay_frames;
520 dev_dbg(ctx->dev, "buffer ptr %llu pcm_delay rep: %llu\n",
521 info->buffer_ptr, info->pcm_delay);
525 static int sst_read_timestamp(struct device *dev, struct pcm_stream_info *info)
527 struct stream_info *stream;
528 struct snd_pcm_substream *substream;
529 struct snd_sst_tstamp fw_tstamp;
531 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
534 str_id = info->str_id;
535 stream = get_stream_info(ctx, str_id);
539 if (!stream->pcm_substream)
541 substream = stream->pcm_substream;
543 addr = (void __iomem *)(ctx->mailbox + ctx->tstamp) +
544 (str_id * sizeof(fw_tstamp));
546 memcpy_fromio(&fw_tstamp, addr, sizeof(fw_tstamp));
548 return sst_calc_tstamp(ctx, info, substream, &fw_tstamp);
551 static int sst_stream_start(struct device *dev, int str_id)
553 struct stream_info *str_info;
554 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
556 if (ctx->sst_state != SST_FW_RUNNING)
558 str_info = get_stream_info(ctx, str_id);
561 str_info->prev = str_info->status;
562 str_info->status = STREAM_RUNNING;
563 sst_start_stream(ctx, str_id);
568 static int sst_stream_drop(struct device *dev, int str_id)
570 struct stream_info *str_info;
571 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
573 if (ctx->sst_state != SST_FW_RUNNING)
576 str_info = get_stream_info(ctx, str_id);
579 str_info->prev = STREAM_UN_INIT;
580 str_info->status = STREAM_INIT;
581 return sst_drop_stream(ctx, str_id);
584 static int sst_stream_pause(struct device *dev, int str_id)
586 struct stream_info *str_info;
587 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
589 if (ctx->sst_state != SST_FW_RUNNING)
592 str_info = get_stream_info(ctx, str_id);
596 return sst_pause_stream(ctx, str_id);
599 static int sst_stream_resume(struct device *dev, int str_id)
601 struct stream_info *str_info;
602 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
604 if (ctx->sst_state != SST_FW_RUNNING)
607 str_info = get_stream_info(ctx, str_id);
610 return sst_resume_stream(ctx, str_id);
613 static int sst_stream_init(struct device *dev, struct pcm_stream_info *str_info)
616 struct stream_info *stream;
617 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
619 str_id = str_info->str_id;
621 if (ctx->sst_state != SST_FW_RUNNING)
624 stream = get_stream_info(ctx, str_id);
628 dev_dbg(ctx->dev, "setting the period ptrs\n");
629 stream->pcm_substream = str_info->arg;
630 stream->period_elapsed = str_info->period_elapsed;
631 stream->sfreq = str_info->sfreq;
632 stream->prev = stream->status;
633 stream->status = STREAM_INIT;
635 "pcm_substream %p, period_elapsed %p, sfreq %d, status %d\n",
636 stream->pcm_substream, stream->period_elapsed,
637 stream->sfreq, stream->status);
643 * sst_set_byte_stream - Set generic params
645 * @cmd: control cmd to be set
646 * @arg: command argument
648 * This function is called by MID sound card driver to configure
649 * SST runtime params.
651 static int sst_send_byte_stream(struct device *dev,
652 struct snd_sst_bytes_v2 *bytes)
655 struct intel_sst_drv *ctx = dev_get_drvdata(dev);
659 ret_val = pm_runtime_get_sync(ctx->dev);
661 pm_runtime_put_sync(ctx->dev);
665 ret_val = sst_send_byte_stream_mrfld(ctx, bytes);
666 sst_pm_runtime_put(ctx);
671 static struct sst_ops pcm_ops = {
672 .open = sst_open_pcm_stream,
673 .stream_init = sst_stream_init,
674 .stream_start = sst_stream_start,
675 .stream_drop = sst_stream_drop,
676 .stream_pause = sst_stream_pause,
677 .stream_pause_release = sst_stream_resume,
678 .stream_read_tstamp = sst_read_timestamp,
679 .send_byte_stream = sst_send_byte_stream,
680 .close = sst_close_pcm_stream,
681 .power = sst_power_control,
684 static struct compress_sst_ops compr_ops = {
685 .open = sst_cdev_open,
686 .close = sst_cdev_close,
687 .stream_pause = sst_cdev_stream_pause,
688 .stream_pause_release = sst_cdev_stream_pause_release,
689 .stream_start = sst_cdev_stream_start,
690 .stream_drop = sst_cdev_stream_drop,
691 .stream_drain = sst_cdev_stream_drain,
692 .stream_partial_drain = sst_cdev_stream_partial_drain,
693 .tstamp = sst_cdev_tstamp,
695 .get_caps = sst_cdev_caps,
696 .get_codec_caps = sst_cdev_codec_caps,
697 .set_metadata = sst_cdev_set_metadata,
698 .power = sst_power_control,
701 static struct sst_device sst_dsp_device = {
702 .name = "Intel(R) SST LPE",
705 .compr_ops = &compr_ops,
709 * sst_register - function to register DSP
711 * This functions registers DSP with the platform driver
713 int sst_register(struct device *dev)
717 sst_dsp_device.dev = dev;
718 ret_val = sst_register_dsp(&sst_dsp_device);
720 dev_err(dev, "Unable to register DSP with platform driver\n");
725 int sst_unregister(struct device *dev)
727 return sst_unregister_dsp(&sst_dsp_device);