Path: blob/master/sound/soc/intel/atom/sst/sst_drv_interface.c
29271 views
// SPDX-License-Identifier: GPL-2.0-only1/*2* sst_drv_interface.c - Intel SST Driver for audio engine3*4* Copyright (C) 2008-14 Intel Corp5* Authors: Vinod Koul <[email protected]>6* Harsha Priya <[email protected]>7* Dharageswari R <[email protected])8* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~9*10* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~11*/12#include <linux/delay.h>13#include <linux/pci.h>14#include <linux/fs.h>15#include <linux/firmware.h>16#include <linux/pm_runtime.h>17#include <linux/pm_qos.h>18#include <linux/math64.h>19#include <sound/core.h>20#include <sound/pcm.h>21#include <sound/soc.h>22#include <sound/compress_driver.h>23#include <asm/platform_sst_audio.h>24#include "../sst-mfld-platform.h"25#include "sst.h"2627#define NUM_CODEC 228#define MIN_FRAGMENT 229#define MAX_FRAGMENT 430#define MIN_FRAGMENT_SIZE (50 * 1024)31#define MAX_FRAGMENT_SIZE (1024 * 1024)32#define SST_GET_BYTES_PER_SAMPLE(pcm_wd_sz) (((pcm_wd_sz + 15) >> 4) << 1)33#ifdef CONFIG_PM34#define GET_USAGE_COUNT(dev) (atomic_read(&dev->power.usage_count))35#else36#define GET_USAGE_COUNT(dev) 137#endif3839int free_stream_context(struct intel_sst_drv *ctx, unsigned int str_id)40{41struct stream_info *stream;42int ret = 0;4344stream = get_stream_info(ctx, str_id);45if (stream) {46/* str_id is valid, so stream is alloacted */47ret = sst_free_stream(ctx, str_id);48if (ret)49sst_clean_stream(&ctx->streams[str_id]);50return ret;51} else {52dev_err(ctx->dev, "we tried to free stream context %d which was freed!!!\n", str_id);53}54return ret;55}5657/*58* sst_get_sfreq - this function returns the frequency of the stream59*60* @str_param : stream params61*/62int sst_get_sfreq(struct snd_sst_params *str_param)63{64switch (str_param->codec) {65case SST_CODEC_TYPE_PCM:66return str_param->sparams.uc.pcm_params.sfreq;67case SST_CODEC_TYPE_AAC:68return str_param->sparams.uc.aac_params.externalsr;69case SST_CODEC_TYPE_MP3:70return 0;71default:72return -EINVAL;73}74}7576/*77* sst_get_num_channel - get number of channels for the stream78*79* @str_param : stream params80*/81int sst_get_num_channel(struct snd_sst_params *str_param)82{83switch (str_param->codec) {84case SST_CODEC_TYPE_PCM:85return str_param->sparams.uc.pcm_params.num_chan;86case SST_CODEC_TYPE_MP3:87return str_param->sparams.uc.mp3_params.num_chan;88case SST_CODEC_TYPE_AAC:89return str_param->sparams.uc.aac_params.num_chan;90default:91return -EINVAL;92}93}9495/*96* sst_get_stream - this function prepares for stream allocation97*98* @str_param : stream param99*/100int sst_get_stream(struct intel_sst_drv *ctx,101struct snd_sst_params *str_param)102{103int retval;104struct stream_info *str_info;105106/* stream is not allocated, we are allocating */107retval = ctx->ops->alloc_stream(ctx, str_param);108if (retval <= 0) {109return -EIO;110}111/* store sampling freq */112str_info = &ctx->streams[retval];113str_info->sfreq = sst_get_sfreq(str_param);114115return retval;116}117118static int sst_power_control(struct device *dev, bool state)119{120struct intel_sst_drv *ctx = dev_get_drvdata(dev);121int ret = 0;122int usage_count = 0;123124if (state) {125ret = pm_runtime_resume_and_get(dev);126usage_count = GET_USAGE_COUNT(dev);127dev_dbg(ctx->dev, "Enable: pm usage count: %d\n", usage_count);128if (ret < 0) {129dev_err(ctx->dev, "Runtime get failed with err: %d\n", ret);130return ret;131}132if ((ctx->sst_state == SST_RESET) && (usage_count == 1)) {133ret = sst_load_fw(ctx);134if (ret) {135dev_err(dev, "FW download fail %d\n", ret);136sst_set_fw_state_locked(ctx, SST_RESET);137ret = sst_pm_runtime_put(ctx);138}139}140} else {141usage_count = GET_USAGE_COUNT(dev);142dev_dbg(ctx->dev, "Disable: pm usage count: %d\n", usage_count);143return sst_pm_runtime_put(ctx);144}145return ret;146}147148/*149* sst_open_pcm_stream - Open PCM interface150*151* @str_param: parameters of pcm stream152*153* This function is called by MID sound card driver to open154* a new pcm interface155*/156static int sst_open_pcm_stream(struct device *dev,157struct snd_sst_params *str_param)158{159int retval;160struct intel_sst_drv *ctx = dev_get_drvdata(dev);161162if (!str_param)163return -EINVAL;164165retval = sst_get_stream(ctx, str_param);166if (retval > 0)167ctx->stream_cnt++;168else169dev_err(ctx->dev, "sst_get_stream returned err %d\n", retval);170171return retval;172}173174static int sst_cdev_open(struct device *dev,175struct snd_sst_params *str_params, struct sst_compress_cb *cb)176{177int str_id, retval;178struct stream_info *stream;179struct intel_sst_drv *ctx = dev_get_drvdata(dev);180181retval = pm_runtime_resume_and_get(ctx->dev);182if (retval < 0)183return retval;184185str_id = sst_get_stream(ctx, str_params);186if (str_id > 0) {187dev_dbg(dev, "stream allocated in sst_cdev_open %d\n", str_id);188stream = &ctx->streams[str_id];189stream->compr_cb = cb->compr_cb;190stream->compr_cb_param = cb->param;191stream->drain_notify = cb->drain_notify;192stream->drain_cb_param = cb->drain_cb_param;193} else {194dev_err(dev, "stream encountered error during alloc %d\n", str_id);195str_id = -EINVAL;196sst_pm_runtime_put(ctx);197}198return str_id;199}200201static int sst_cdev_close(struct device *dev, unsigned int str_id)202{203int retval;204struct stream_info *stream;205struct intel_sst_drv *ctx = dev_get_drvdata(dev);206207stream = get_stream_info(ctx, str_id);208if (!stream) {209dev_err(dev, "stream info is NULL for str %d!!!\n", str_id);210return -EINVAL;211}212213retval = sst_free_stream(ctx, str_id);214stream->compr_cb_param = NULL;215stream->compr_cb = NULL;216217if (retval)218dev_err(dev, "free stream returned err %d\n", retval);219220dev_dbg(dev, "End\n");221return retval;222}223224static int sst_cdev_ack(struct device *dev, unsigned int str_id,225unsigned long bytes)226{227struct stream_info *stream;228struct snd_sst_tstamp fw_tstamp = {0,};229int offset;230void __iomem *addr;231struct intel_sst_drv *ctx = dev_get_drvdata(dev);232233stream = get_stream_info(ctx, str_id);234if (!stream)235return -EINVAL;236237/* update bytes sent */238stream->cumm_bytes += bytes;239dev_dbg(dev, "bytes copied %d inc by %ld\n", stream->cumm_bytes, bytes);240241addr = ((void __iomem *)(ctx->mailbox + ctx->tstamp)) +242(str_id * sizeof(fw_tstamp));243244memcpy_fromio(&fw_tstamp, addr, sizeof(fw_tstamp));245246fw_tstamp.bytes_copied = stream->cumm_bytes;247dev_dbg(dev, "bytes sent to fw %llu inc by %ld\n",248fw_tstamp.bytes_copied, bytes);249250offset = offsetof(struct snd_sst_tstamp, bytes_copied);251sst_shim_write(addr, offset, fw_tstamp.bytes_copied);252return 0;253}254255static int sst_cdev_set_metadata(struct device *dev,256unsigned int str_id, struct snd_compr_metadata *metadata)257{258int retval = 0;259struct stream_info *str_info;260struct intel_sst_drv *ctx = dev_get_drvdata(dev);261262dev_dbg(dev, "set metadata for stream %d\n", str_id);263264str_info = get_stream_info(ctx, str_id);265if (!str_info)266return -EINVAL;267268dev_dbg(dev, "pipe id = %d\n", str_info->pipe_id);269retval = sst_prepare_and_post_msg(ctx, str_info->task_id, IPC_CMD,270IPC_IA_SET_STREAM_PARAMS_MRFLD, str_info->pipe_id,271sizeof(*metadata), metadata, NULL,272true, true, true, false);273274return retval;275}276277static int sst_cdev_stream_pause(struct device *dev, unsigned int str_id)278{279struct intel_sst_drv *ctx = dev_get_drvdata(dev);280281return sst_pause_stream(ctx, str_id);282}283284static int sst_cdev_stream_pause_release(struct device *dev,285unsigned int str_id)286{287struct intel_sst_drv *ctx = dev_get_drvdata(dev);288289return sst_resume_stream(ctx, str_id);290}291292static int sst_cdev_stream_start(struct device *dev, unsigned int str_id)293{294struct stream_info *str_info;295struct intel_sst_drv *ctx = dev_get_drvdata(dev);296297str_info = get_stream_info(ctx, str_id);298if (!str_info)299return -EINVAL;300str_info->prev = str_info->status;301str_info->status = STREAM_RUNNING;302return sst_start_stream(ctx, str_id);303}304305static int sst_cdev_stream_drop(struct device *dev, unsigned int str_id)306{307struct intel_sst_drv *ctx = dev_get_drvdata(dev);308309return sst_drop_stream(ctx, str_id);310}311312static int sst_cdev_stream_drain(struct device *dev, unsigned int str_id)313{314struct intel_sst_drv *ctx = dev_get_drvdata(dev);315316return sst_drain_stream(ctx, str_id, false);317}318319static int sst_cdev_stream_partial_drain(struct device *dev,320unsigned int str_id)321{322struct intel_sst_drv *ctx = dev_get_drvdata(dev);323324return sst_drain_stream(ctx, str_id, true);325}326327static int sst_cdev_tstamp(struct device *dev, unsigned int str_id,328struct snd_compr_tstamp64 *tstamp)329{330struct snd_sst_tstamp fw_tstamp = {0,};331struct stream_info *stream;332struct intel_sst_drv *ctx = dev_get_drvdata(dev);333void __iomem *addr;334335addr = (void __iomem *)(ctx->mailbox + ctx->tstamp) +336(str_id * sizeof(fw_tstamp));337338memcpy_fromio(&fw_tstamp, addr, sizeof(fw_tstamp));339340stream = get_stream_info(ctx, str_id);341if (!stream)342return -EINVAL;343dev_dbg(dev, "rb_counter %llu in bytes\n", fw_tstamp.ring_buffer_counter);344345tstamp->copied_total = fw_tstamp.ring_buffer_counter;346tstamp->pcm_frames = fw_tstamp.frames_decoded;347tstamp->pcm_io_frames = div_u64(fw_tstamp.hardware_counter,348(u64)stream->num_ch * SST_GET_BYTES_PER_SAMPLE(24));349tstamp->sampling_rate = fw_tstamp.sampling_frequency;350351dev_dbg(dev, "PCM = %llu\n", tstamp->pcm_io_frames);352dev_dbg(dev,353"Ptr Query on strid = %d copied_total %llu, decodec %llu\n",354str_id, tstamp->copied_total, tstamp->pcm_frames);355dev_dbg(dev, "rendered %llu\n", tstamp->pcm_io_frames);356357return 0;358}359360static int sst_cdev_caps(struct snd_compr_caps *caps)361{362caps->num_codecs = NUM_CODEC;363caps->min_fragment_size = MIN_FRAGMENT_SIZE; /* 50KB */364caps->max_fragment_size = MAX_FRAGMENT_SIZE; /* 1024KB */365caps->min_fragments = MIN_FRAGMENT;366caps->max_fragments = MAX_FRAGMENT;367caps->codecs[0] = SND_AUDIOCODEC_MP3;368caps->codecs[1] = SND_AUDIOCODEC_AAC;369return 0;370}371372static const struct snd_compr_codec_caps caps_mp3 = {373.num_descriptors = 1,374.descriptor[0].max_ch = 2,375.descriptor[0].sample_rates[0] = 48000,376.descriptor[0].sample_rates[1] = 44100,377.descriptor[0].sample_rates[2] = 32000,378.descriptor[0].sample_rates[3] = 16000,379.descriptor[0].sample_rates[4] = 8000,380.descriptor[0].num_sample_rates = 5,381.descriptor[0].bit_rate[0] = 320,382.descriptor[0].bit_rate[1] = 192,383.descriptor[0].num_bitrates = 2,384.descriptor[0].profiles = 0,385.descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO,386.descriptor[0].formats = 0,387};388389static const struct snd_compr_codec_caps caps_aac = {390.num_descriptors = 2,391.descriptor[1].max_ch = 2,392.descriptor[0].sample_rates[0] = 48000,393.descriptor[0].sample_rates[1] = 44100,394.descriptor[0].sample_rates[2] = 32000,395.descriptor[0].sample_rates[3] = 16000,396.descriptor[0].sample_rates[4] = 8000,397.descriptor[0].num_sample_rates = 5,398.descriptor[1].bit_rate[0] = 320,399.descriptor[1].bit_rate[1] = 192,400.descriptor[1].num_bitrates = 2,401.descriptor[1].profiles = 0,402.descriptor[1].modes = 0,403.descriptor[1].formats =404(SND_AUDIOSTREAMFORMAT_MP4ADTS |405SND_AUDIOSTREAMFORMAT_RAW),406};407408static int sst_cdev_codec_caps(struct snd_compr_codec_caps *codec)409{410if (codec->codec == SND_AUDIOCODEC_MP3)411*codec = caps_mp3;412else if (codec->codec == SND_AUDIOCODEC_AAC)413*codec = caps_aac;414else415return -EINVAL;416417return 0;418}419420/*421* sst_close_pcm_stream - Close PCM interface422*423* @str_id: stream id to be closed424*425* This function is called by MID sound card driver to close426* an existing pcm interface427*/428static int sst_close_pcm_stream(struct device *dev, unsigned int str_id)429{430struct stream_info *stream;431int retval = 0;432struct intel_sst_drv *ctx = dev_get_drvdata(dev);433434stream = get_stream_info(ctx, str_id);435if (!stream) {436dev_err(ctx->dev, "stream info is NULL for str %d!!!\n", str_id);437return -EINVAL;438}439440retval = free_stream_context(ctx, str_id);441stream->pcm_substream = NULL;442stream->status = STREAM_UN_INIT;443stream->period_elapsed = NULL;444ctx->stream_cnt--;445446if (retval)447dev_err(ctx->dev, "free stream returned err %d\n", retval);448449dev_dbg(ctx->dev, "Exit\n");450return 0;451}452453static inline int sst_calc_tstamp(struct intel_sst_drv *ctx,454struct pcm_stream_info *info,455struct snd_pcm_substream *substream,456struct snd_sst_tstamp *fw_tstamp)457{458size_t delay_bytes, delay_frames;459size_t buffer_sz;460u32 pointer_bytes, pointer_samples;461462dev_dbg(ctx->dev, "mrfld ring_buffer_counter %llu in bytes\n",463fw_tstamp->ring_buffer_counter);464dev_dbg(ctx->dev, "mrfld hardware_counter %llu in bytes\n",465fw_tstamp->hardware_counter);466if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)467delay_bytes = (size_t) (fw_tstamp->ring_buffer_counter -468fw_tstamp->hardware_counter);469else470delay_bytes = (size_t) (fw_tstamp->hardware_counter -471fw_tstamp->ring_buffer_counter);472delay_frames = bytes_to_frames(substream->runtime, delay_bytes);473buffer_sz = snd_pcm_lib_buffer_bytes(substream);474div_u64_rem(fw_tstamp->ring_buffer_counter, buffer_sz, &pointer_bytes);475pointer_samples = bytes_to_samples(substream->runtime, pointer_bytes);476477dev_dbg(ctx->dev, "pcm delay %zu in bytes\n", delay_bytes);478479info->buffer_ptr = pointer_samples / substream->runtime->channels;480481info->pcm_delay = delay_frames;482dev_dbg(ctx->dev, "buffer ptr %llu pcm_delay rep: %llu\n",483info->buffer_ptr, info->pcm_delay);484return 0;485}486487static int sst_read_timestamp(struct device *dev, struct pcm_stream_info *info)488{489struct stream_info *stream;490struct snd_pcm_substream *substream;491struct snd_sst_tstamp fw_tstamp;492unsigned int str_id;493struct intel_sst_drv *ctx = dev_get_drvdata(dev);494void __iomem *addr;495496str_id = info->str_id;497stream = get_stream_info(ctx, str_id);498if (!stream)499return -EINVAL;500501if (!stream->pcm_substream)502return -EINVAL;503substream = stream->pcm_substream;504505addr = (void __iomem *)(ctx->mailbox + ctx->tstamp) +506(str_id * sizeof(fw_tstamp));507508memcpy_fromio(&fw_tstamp, addr, sizeof(fw_tstamp));509510return sst_calc_tstamp(ctx, info, substream, &fw_tstamp);511}512513static int sst_stream_start(struct device *dev, int str_id)514{515struct stream_info *str_info;516struct intel_sst_drv *ctx = dev_get_drvdata(dev);517518if (ctx->sst_state != SST_FW_RUNNING)519return 0;520str_info = get_stream_info(ctx, str_id);521if (!str_info)522return -EINVAL;523str_info->prev = str_info->status;524str_info->status = STREAM_RUNNING;525sst_start_stream(ctx, str_id);526527return 0;528}529530static int sst_stream_drop(struct device *dev, int str_id)531{532struct stream_info *str_info;533struct intel_sst_drv *ctx = dev_get_drvdata(dev);534535if (ctx->sst_state != SST_FW_RUNNING)536return 0;537538str_info = get_stream_info(ctx, str_id);539if (!str_info)540return -EINVAL;541str_info->prev = STREAM_UN_INIT;542str_info->status = STREAM_INIT;543return sst_drop_stream(ctx, str_id);544}545546static int sst_stream_pause(struct device *dev, int str_id)547{548struct stream_info *str_info;549struct intel_sst_drv *ctx = dev_get_drvdata(dev);550551if (ctx->sst_state != SST_FW_RUNNING)552return 0;553554str_info = get_stream_info(ctx, str_id);555if (!str_info)556return -EINVAL;557558return sst_pause_stream(ctx, str_id);559}560561static int sst_stream_resume(struct device *dev, int str_id)562{563struct stream_info *str_info;564struct intel_sst_drv *ctx = dev_get_drvdata(dev);565566if (ctx->sst_state != SST_FW_RUNNING)567return 0;568569str_info = get_stream_info(ctx, str_id);570if (!str_info)571return -EINVAL;572return sst_resume_stream(ctx, str_id);573}574575static int sst_stream_init(struct device *dev, struct pcm_stream_info *str_info)576{577int str_id = 0;578struct stream_info *stream;579struct intel_sst_drv *ctx = dev_get_drvdata(dev);580581str_id = str_info->str_id;582583if (ctx->sst_state != SST_FW_RUNNING)584return 0;585586stream = get_stream_info(ctx, str_id);587if (!stream)588return -EINVAL;589590dev_dbg(ctx->dev, "setting the period ptrs\n");591stream->pcm_substream = str_info->arg;592stream->period_elapsed = str_info->period_elapsed;593stream->sfreq = str_info->sfreq;594stream->prev = stream->status;595stream->status = STREAM_INIT;596dev_dbg(ctx->dev,597"pcm_substream %p, period_elapsed %p, sfreq %d, status %d\n",598stream->pcm_substream, stream->period_elapsed,599stream->sfreq, stream->status);600601return 0;602}603604/*605* sst_set_byte_stream - Set generic params606*607* @cmd: control cmd to be set608* @arg: command argument609*610* This function is called by MID sound card driver to configure611* SST runtime params.612*/613static int sst_send_byte_stream(struct device *dev,614struct snd_sst_bytes_v2 *bytes)615{616int ret_val = 0;617struct intel_sst_drv *ctx = dev_get_drvdata(dev);618619if (NULL == bytes)620return -EINVAL;621ret_val = pm_runtime_resume_and_get(ctx->dev);622if (ret_val < 0)623return ret_val;624625ret_val = sst_send_byte_stream_mrfld(ctx, bytes);626sst_pm_runtime_put(ctx);627628return ret_val;629}630631static struct sst_ops pcm_ops = {632.open = sst_open_pcm_stream,633.stream_init = sst_stream_init,634.stream_start = sst_stream_start,635.stream_drop = sst_stream_drop,636.stream_pause = sst_stream_pause,637.stream_pause_release = sst_stream_resume,638.stream_read_tstamp = sst_read_timestamp,639.send_byte_stream = sst_send_byte_stream,640.close = sst_close_pcm_stream,641.power = sst_power_control,642};643644static struct compress_sst_ops compr_ops = {645.open = sst_cdev_open,646.close = sst_cdev_close,647.stream_pause = sst_cdev_stream_pause,648.stream_pause_release = sst_cdev_stream_pause_release,649.stream_start = sst_cdev_stream_start,650.stream_drop = sst_cdev_stream_drop,651.stream_drain = sst_cdev_stream_drain,652.stream_partial_drain = sst_cdev_stream_partial_drain,653.tstamp = sst_cdev_tstamp,654.ack = sst_cdev_ack,655.get_caps = sst_cdev_caps,656.get_codec_caps = sst_cdev_codec_caps,657.set_metadata = sst_cdev_set_metadata,658.power = sst_power_control,659};660661static struct sst_device sst_dsp_device = {662.name = "Intel(R) SST LPE",663.dev = NULL,664.ops = &pcm_ops,665.compr_ops = &compr_ops,666};667668/*669* sst_register - function to register DSP670*671* This functions registers DSP with the platform driver672*/673int sst_register(struct device *dev)674{675int ret_val;676677sst_dsp_device.dev = dev;678ret_val = sst_register_dsp(&sst_dsp_device);679if (ret_val)680dev_err(dev, "Unable to register DSP with platform driver\n");681682return ret_val;683}684685int sst_unregister(struct device *dev)686{687return sst_unregister_dsp(&sst_dsp_device);688}689690691