Path: blob/master/sound/soc/intel/atom/sst/sst_drv_interface.c
26516 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_tstamp *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 = %u\n", tstamp->pcm_io_frames);352dev_dbg(dev, "Ptr Query on strid = %d copied_total %d, decodec %d\n",353str_id, tstamp->copied_total, tstamp->pcm_frames);354dev_dbg(dev, "rendered %d\n", tstamp->pcm_io_frames);355356return 0;357}358359static int sst_cdev_caps(struct snd_compr_caps *caps)360{361caps->num_codecs = NUM_CODEC;362caps->min_fragment_size = MIN_FRAGMENT_SIZE; /* 50KB */363caps->max_fragment_size = MAX_FRAGMENT_SIZE; /* 1024KB */364caps->min_fragments = MIN_FRAGMENT;365caps->max_fragments = MAX_FRAGMENT;366caps->codecs[0] = SND_AUDIOCODEC_MP3;367caps->codecs[1] = SND_AUDIOCODEC_AAC;368return 0;369}370371static const struct snd_compr_codec_caps caps_mp3 = {372.num_descriptors = 1,373.descriptor[0].max_ch = 2,374.descriptor[0].sample_rates[0] = 48000,375.descriptor[0].sample_rates[1] = 44100,376.descriptor[0].sample_rates[2] = 32000,377.descriptor[0].sample_rates[3] = 16000,378.descriptor[0].sample_rates[4] = 8000,379.descriptor[0].num_sample_rates = 5,380.descriptor[0].bit_rate[0] = 320,381.descriptor[0].bit_rate[1] = 192,382.descriptor[0].num_bitrates = 2,383.descriptor[0].profiles = 0,384.descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO,385.descriptor[0].formats = 0,386};387388static const struct snd_compr_codec_caps caps_aac = {389.num_descriptors = 2,390.descriptor[1].max_ch = 2,391.descriptor[0].sample_rates[0] = 48000,392.descriptor[0].sample_rates[1] = 44100,393.descriptor[0].sample_rates[2] = 32000,394.descriptor[0].sample_rates[3] = 16000,395.descriptor[0].sample_rates[4] = 8000,396.descriptor[0].num_sample_rates = 5,397.descriptor[1].bit_rate[0] = 320,398.descriptor[1].bit_rate[1] = 192,399.descriptor[1].num_bitrates = 2,400.descriptor[1].profiles = 0,401.descriptor[1].modes = 0,402.descriptor[1].formats =403(SND_AUDIOSTREAMFORMAT_MP4ADTS |404SND_AUDIOSTREAMFORMAT_RAW),405};406407static int sst_cdev_codec_caps(struct snd_compr_codec_caps *codec)408{409if (codec->codec == SND_AUDIOCODEC_MP3)410*codec = caps_mp3;411else if (codec->codec == SND_AUDIOCODEC_AAC)412*codec = caps_aac;413else414return -EINVAL;415416return 0;417}418419/*420* sst_close_pcm_stream - Close PCM interface421*422* @str_id: stream id to be closed423*424* This function is called by MID sound card driver to close425* an existing pcm interface426*/427static int sst_close_pcm_stream(struct device *dev, unsigned int str_id)428{429struct stream_info *stream;430int retval = 0;431struct intel_sst_drv *ctx = dev_get_drvdata(dev);432433stream = get_stream_info(ctx, str_id);434if (!stream) {435dev_err(ctx->dev, "stream info is NULL for str %d!!!\n", str_id);436return -EINVAL;437}438439retval = free_stream_context(ctx, str_id);440stream->pcm_substream = NULL;441stream->status = STREAM_UN_INIT;442stream->period_elapsed = NULL;443ctx->stream_cnt--;444445if (retval)446dev_err(ctx->dev, "free stream returned err %d\n", retval);447448dev_dbg(ctx->dev, "Exit\n");449return 0;450}451452static inline int sst_calc_tstamp(struct intel_sst_drv *ctx,453struct pcm_stream_info *info,454struct snd_pcm_substream *substream,455struct snd_sst_tstamp *fw_tstamp)456{457size_t delay_bytes, delay_frames;458size_t buffer_sz;459u32 pointer_bytes, pointer_samples;460461dev_dbg(ctx->dev, "mrfld ring_buffer_counter %llu in bytes\n",462fw_tstamp->ring_buffer_counter);463dev_dbg(ctx->dev, "mrfld hardware_counter %llu in bytes\n",464fw_tstamp->hardware_counter);465if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)466delay_bytes = (size_t) (fw_tstamp->ring_buffer_counter -467fw_tstamp->hardware_counter);468else469delay_bytes = (size_t) (fw_tstamp->hardware_counter -470fw_tstamp->ring_buffer_counter);471delay_frames = bytes_to_frames(substream->runtime, delay_bytes);472buffer_sz = snd_pcm_lib_buffer_bytes(substream);473div_u64_rem(fw_tstamp->ring_buffer_counter, buffer_sz, &pointer_bytes);474pointer_samples = bytes_to_samples(substream->runtime, pointer_bytes);475476dev_dbg(ctx->dev, "pcm delay %zu in bytes\n", delay_bytes);477478info->buffer_ptr = pointer_samples / substream->runtime->channels;479480info->pcm_delay = delay_frames;481dev_dbg(ctx->dev, "buffer ptr %llu pcm_delay rep: %llu\n",482info->buffer_ptr, info->pcm_delay);483return 0;484}485486static int sst_read_timestamp(struct device *dev, struct pcm_stream_info *info)487{488struct stream_info *stream;489struct snd_pcm_substream *substream;490struct snd_sst_tstamp fw_tstamp;491unsigned int str_id;492struct intel_sst_drv *ctx = dev_get_drvdata(dev);493void __iomem *addr;494495str_id = info->str_id;496stream = get_stream_info(ctx, str_id);497if (!stream)498return -EINVAL;499500if (!stream->pcm_substream)501return -EINVAL;502substream = stream->pcm_substream;503504addr = (void __iomem *)(ctx->mailbox + ctx->tstamp) +505(str_id * sizeof(fw_tstamp));506507memcpy_fromio(&fw_tstamp, addr, sizeof(fw_tstamp));508509return sst_calc_tstamp(ctx, info, substream, &fw_tstamp);510}511512static int sst_stream_start(struct device *dev, int str_id)513{514struct stream_info *str_info;515struct intel_sst_drv *ctx = dev_get_drvdata(dev);516517if (ctx->sst_state != SST_FW_RUNNING)518return 0;519str_info = get_stream_info(ctx, str_id);520if (!str_info)521return -EINVAL;522str_info->prev = str_info->status;523str_info->status = STREAM_RUNNING;524sst_start_stream(ctx, str_id);525526return 0;527}528529static int sst_stream_drop(struct device *dev, int str_id)530{531struct stream_info *str_info;532struct intel_sst_drv *ctx = dev_get_drvdata(dev);533534if (ctx->sst_state != SST_FW_RUNNING)535return 0;536537str_info = get_stream_info(ctx, str_id);538if (!str_info)539return -EINVAL;540str_info->prev = STREAM_UN_INIT;541str_info->status = STREAM_INIT;542return sst_drop_stream(ctx, str_id);543}544545static int sst_stream_pause(struct device *dev, int str_id)546{547struct stream_info *str_info;548struct intel_sst_drv *ctx = dev_get_drvdata(dev);549550if (ctx->sst_state != SST_FW_RUNNING)551return 0;552553str_info = get_stream_info(ctx, str_id);554if (!str_info)555return -EINVAL;556557return sst_pause_stream(ctx, str_id);558}559560static int sst_stream_resume(struct device *dev, int str_id)561{562struct stream_info *str_info;563struct intel_sst_drv *ctx = dev_get_drvdata(dev);564565if (ctx->sst_state != SST_FW_RUNNING)566return 0;567568str_info = get_stream_info(ctx, str_id);569if (!str_info)570return -EINVAL;571return sst_resume_stream(ctx, str_id);572}573574static int sst_stream_init(struct device *dev, struct pcm_stream_info *str_info)575{576int str_id = 0;577struct stream_info *stream;578struct intel_sst_drv *ctx = dev_get_drvdata(dev);579580str_id = str_info->str_id;581582if (ctx->sst_state != SST_FW_RUNNING)583return 0;584585stream = get_stream_info(ctx, str_id);586if (!stream)587return -EINVAL;588589dev_dbg(ctx->dev, "setting the period ptrs\n");590stream->pcm_substream = str_info->arg;591stream->period_elapsed = str_info->period_elapsed;592stream->sfreq = str_info->sfreq;593stream->prev = stream->status;594stream->status = STREAM_INIT;595dev_dbg(ctx->dev,596"pcm_substream %p, period_elapsed %p, sfreq %d, status %d\n",597stream->pcm_substream, stream->period_elapsed,598stream->sfreq, stream->status);599600return 0;601}602603/*604* sst_set_byte_stream - Set generic params605*606* @cmd: control cmd to be set607* @arg: command argument608*609* This function is called by MID sound card driver to configure610* SST runtime params.611*/612static int sst_send_byte_stream(struct device *dev,613struct snd_sst_bytes_v2 *bytes)614{615int ret_val = 0;616struct intel_sst_drv *ctx = dev_get_drvdata(dev);617618if (NULL == bytes)619return -EINVAL;620ret_val = pm_runtime_resume_and_get(ctx->dev);621if (ret_val < 0)622return ret_val;623624ret_val = sst_send_byte_stream_mrfld(ctx, bytes);625sst_pm_runtime_put(ctx);626627return ret_val;628}629630static struct sst_ops pcm_ops = {631.open = sst_open_pcm_stream,632.stream_init = sst_stream_init,633.stream_start = sst_stream_start,634.stream_drop = sst_stream_drop,635.stream_pause = sst_stream_pause,636.stream_pause_release = sst_stream_resume,637.stream_read_tstamp = sst_read_timestamp,638.send_byte_stream = sst_send_byte_stream,639.close = sst_close_pcm_stream,640.power = sst_power_control,641};642643static struct compress_sst_ops compr_ops = {644.open = sst_cdev_open,645.close = sst_cdev_close,646.stream_pause = sst_cdev_stream_pause,647.stream_pause_release = sst_cdev_stream_pause_release,648.stream_start = sst_cdev_stream_start,649.stream_drop = sst_cdev_stream_drop,650.stream_drain = sst_cdev_stream_drain,651.stream_partial_drain = sst_cdev_stream_partial_drain,652.tstamp = sst_cdev_tstamp,653.ack = sst_cdev_ack,654.get_caps = sst_cdev_caps,655.get_codec_caps = sst_cdev_codec_caps,656.set_metadata = sst_cdev_set_metadata,657.power = sst_power_control,658};659660static struct sst_device sst_dsp_device = {661.name = "Intel(R) SST LPE",662.dev = NULL,663.ops = &pcm_ops,664.compr_ops = &compr_ops,665};666667/*668* sst_register - function to register DSP669*670* This functions registers DSP with the platform driver671*/672int sst_register(struct device *dev)673{674int ret_val;675676sst_dsp_device.dev = dev;677ret_val = sst_register_dsp(&sst_dsp_device);678if (ret_val)679dev_err(dev, "Unable to register DSP with platform driver\n");680681return ret_val;682}683684int sst_unregister(struct device *dev)685{686return sst_unregister_dsp(&sst_dsp_device);687}688689690