Path: blob/master/sound/soc/mid-x86/sst_platform.c
10819 views
/*1* sst_platform.c - Intel MID Platform driver2*3* Copyright (C) 2010 Intel Corp4* Author: Vinod Koul <[email protected]>5* Author: Harsha Priya <[email protected]>6* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~7*8* This program is free software; you can redistribute it and/or modify9* it under the terms of the GNU General Public License as published by10* the Free Software Foundation; version 2 of the License.11*12* This program is distributed in the hope that it will be useful, but13* WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU15* General Public License for more details.16*17* You should have received a copy of the GNU General Public License along18* with this program; if not, write to the Free Software Foundation, Inc.,19* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.20*21* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~22*23*24*/25#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt2627#include <linux/slab.h>28#include <linux/io.h>29#include <sound/core.h>30#include <sound/pcm.h>31#include <sound/pcm_params.h>32#include <sound/soc.h>33#include "../../../drivers/staging/intel_sst/intel_sst_ioctl.h"34#include "../../../drivers/staging/intel_sst/intel_sst.h"35#include "sst_platform.h"3637static struct snd_pcm_hardware sst_platform_pcm_hw = {38.info = (SNDRV_PCM_INFO_INTERLEAVED |39SNDRV_PCM_INFO_DOUBLE |40SNDRV_PCM_INFO_PAUSE |41SNDRV_PCM_INFO_RESUME |42SNDRV_PCM_INFO_MMAP|43SNDRV_PCM_INFO_MMAP_VALID |44SNDRV_PCM_INFO_BLOCK_TRANSFER |45SNDRV_PCM_INFO_SYNC_START),46.formats = (SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_U16 |47SNDRV_PCM_FMTBIT_S24 | SNDRV_PCM_FMTBIT_U24 |48SNDRV_PCM_FMTBIT_S32 | SNDRV_PCM_FMTBIT_U32),49.rates = (SNDRV_PCM_RATE_8000|50SNDRV_PCM_RATE_44100 |51SNDRV_PCM_RATE_48000),52.rate_min = SST_MIN_RATE,53.rate_max = SST_MAX_RATE,54.channels_min = SST_MIN_CHANNEL,55.channels_max = SST_MAX_CHANNEL,56.buffer_bytes_max = SST_MAX_BUFFER,57.period_bytes_min = SST_MIN_PERIOD_BYTES,58.period_bytes_max = SST_MAX_PERIOD_BYTES,59.periods_min = SST_MIN_PERIODS,60.periods_max = SST_MAX_PERIODS,61.fifo_size = SST_FIFO_SIZE,62};6364/* MFLD - MSIC */65struct snd_soc_dai_driver sst_platform_dai[] = {66{67.name = "Headset-cpu-dai",68.id = 0,69.playback = {70.channels_min = SST_STEREO,71.channels_max = SST_STEREO,72.rates = SNDRV_PCM_RATE_48000,73.formats = SNDRV_PCM_FMTBIT_S24_LE,74},75.capture = {76.channels_min = 1,77.channels_max = 5,78.rates = SNDRV_PCM_RATE_48000,79.formats = SNDRV_PCM_FMTBIT_S24_LE,80},81},82{83.name = "Speaker-cpu-dai",84.id = 1,85.playback = {86.channels_min = SST_MONO,87.channels_max = SST_STEREO,88.rates = SNDRV_PCM_RATE_48000,89.formats = SNDRV_PCM_FMTBIT_S24_LE,90},91},92{93.name = "Vibra1-cpu-dai",94.id = 2,95.playback = {96.channels_min = SST_MONO,97.channels_max = SST_MONO,98.rates = SNDRV_PCM_RATE_48000,99.formats = SNDRV_PCM_FMTBIT_S24_LE,100},101},102{103.name = "Vibra2-cpu-dai",104.id = 3,105.playback = {106.channels_min = SST_MONO,107.channels_max = SST_STEREO,108.rates = SNDRV_PCM_RATE_48000,109.formats = SNDRV_PCM_FMTBIT_S24_LE,110},111},112};113114/* helper functions */115static inline void sst_set_stream_status(struct sst_runtime_stream *stream,116int state)117{118unsigned long flags;119spin_lock_irqsave(&stream->status_lock, flags);120stream->stream_status = state;121spin_unlock_irqrestore(&stream->status_lock, flags);122}123124static inline int sst_get_stream_status(struct sst_runtime_stream *stream)125{126int state;127unsigned long flags;128129spin_lock_irqsave(&stream->status_lock, flags);130state = stream->stream_status;131spin_unlock_irqrestore(&stream->status_lock, flags);132return state;133}134135static void sst_fill_pcm_params(struct snd_pcm_substream *substream,136struct snd_sst_stream_params *param)137{138139param->uc.pcm_params.codec = SST_CODEC_TYPE_PCM;140param->uc.pcm_params.num_chan = (u8) substream->runtime->channels;141param->uc.pcm_params.pcm_wd_sz = substream->runtime->sample_bits;142param->uc.pcm_params.reserved = 0;143param->uc.pcm_params.sfreq = substream->runtime->rate;144param->uc.pcm_params.ring_buffer_size =145snd_pcm_lib_buffer_bytes(substream);146param->uc.pcm_params.period_count = substream->runtime->period_size;147param->uc.pcm_params.ring_buffer_addr =148virt_to_phys(substream->dma_buffer.area);149pr_debug("period_cnt = %d\n", param->uc.pcm_params.period_count);150pr_debug("sfreq= %d, wd_sz = %d\n",151param->uc.pcm_params.sfreq, param->uc.pcm_params.pcm_wd_sz);152}153154static int sst_platform_alloc_stream(struct snd_pcm_substream *substream)155{156struct sst_runtime_stream *stream =157substream->runtime->private_data;158struct snd_sst_stream_params param = {{{0,},},};159struct snd_sst_params str_params = {0};160int ret_val;161162/* set codec params and inform SST driver the same */163sst_fill_pcm_params(substream, ¶m);164substream->runtime->dma_area = substream->dma_buffer.area;165str_params.sparams = param;166str_params.codec = param.uc.pcm_params.codec;167if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {168str_params.ops = STREAM_OPS_PLAYBACK;169str_params.device_type = substream->pcm->device + 1;170pr_debug("Playbck stream,Device %d\n",171substream->pcm->device);172} else {173str_params.ops = STREAM_OPS_CAPTURE;174str_params.device_type = SND_SST_DEVICE_CAPTURE;175pr_debug("Capture stream,Device %d\n",176substream->pcm->device);177}178ret_val = stream->sstdrv_ops->pcm_control->open(&str_params);179pr_debug("SST_SND_PLAY/CAPTURE ret_val = %x\n", ret_val);180if (ret_val < 0)181return ret_val;182183stream->stream_info.str_id = ret_val;184pr_debug("str id : %d\n", stream->stream_info.str_id);185return ret_val;186}187188static void sst_period_elapsed(void *mad_substream)189{190struct snd_pcm_substream *substream = mad_substream;191struct sst_runtime_stream *stream;192int status;193194if (!substream || !substream->runtime)195return;196stream = substream->runtime->private_data;197if (!stream)198return;199status = sst_get_stream_status(stream);200if (status != SST_PLATFORM_RUNNING)201return;202snd_pcm_period_elapsed(substream);203}204205static int sst_platform_init_stream(struct snd_pcm_substream *substream)206{207struct sst_runtime_stream *stream =208substream->runtime->private_data;209int ret_val;210211pr_debug("setting buffer ptr param\n");212sst_set_stream_status(stream, SST_PLATFORM_INIT);213stream->stream_info.period_elapsed = sst_period_elapsed;214stream->stream_info.mad_substream = substream;215stream->stream_info.buffer_ptr = 0;216stream->stream_info.sfreq = substream->runtime->rate;217ret_val = stream->sstdrv_ops->pcm_control->device_control(218SST_SND_STREAM_INIT, &stream->stream_info);219if (ret_val)220pr_err("control_set ret error %d\n", ret_val);221return ret_val;222223}224/* end -- helper functions */225226static int sst_platform_open(struct snd_pcm_substream *substream)227{228struct snd_pcm_runtime *runtime;229struct sst_runtime_stream *stream;230int ret_val = 0;231232pr_debug("sst_platform_open called\n");233runtime = substream->runtime;234runtime->hw = sst_platform_pcm_hw;235stream = kzalloc(sizeof(*stream), GFP_KERNEL);236if (!stream)237return -ENOMEM;238spin_lock_init(&stream->status_lock);239stream->stream_info.str_id = 0;240sst_set_stream_status(stream, SST_PLATFORM_INIT);241stream->stream_info.mad_substream = substream;242/* allocate memory for SST API set */243stream->sstdrv_ops = kzalloc(sizeof(*stream->sstdrv_ops),244GFP_KERNEL);245if (!stream->sstdrv_ops) {246pr_err("sst: mem allocation for ops fail\n");247kfree(stream);248return -ENOMEM;249}250stream->sstdrv_ops->vendor_id = MSIC_VENDOR_ID;251stream->sstdrv_ops->module_name = SST_CARD_NAMES;252/* registering with SST driver to get access to SST APIs to use */253ret_val = register_sst_card(stream->sstdrv_ops);254if (ret_val) {255pr_err("sst: sst card registration failed\n");256kfree(stream->sstdrv_ops);257kfree(stream);258return ret_val;259}260runtime->private_data = stream;261return snd_pcm_hw_constraint_integer(runtime,262SNDRV_PCM_HW_PARAM_PERIODS);263}264265static int sst_platform_close(struct snd_pcm_substream *substream)266{267struct sst_runtime_stream *stream;268int ret_val = 0, str_id;269270pr_debug("sst_platform_close called\n");271stream = substream->runtime->private_data;272str_id = stream->stream_info.str_id;273if (str_id)274ret_val = stream->sstdrv_ops->pcm_control->close(str_id);275unregister_sst_card(stream->sstdrv_ops);276kfree(stream->sstdrv_ops);277kfree(stream);278return ret_val;279}280281static int sst_platform_pcm_prepare(struct snd_pcm_substream *substream)282{283struct sst_runtime_stream *stream;284int ret_val = 0, str_id;285286pr_debug("sst_platform_pcm_prepare called\n");287stream = substream->runtime->private_data;288str_id = stream->stream_info.str_id;289if (stream->stream_info.str_id) {290ret_val = stream->sstdrv_ops->pcm_control->device_control(291SST_SND_DROP, &str_id);292return ret_val;293}294295ret_val = sst_platform_alloc_stream(substream);296if (ret_val < 0)297return ret_val;298snprintf(substream->pcm->id, sizeof(substream->pcm->id),299"%d", stream->stream_info.str_id);300301ret_val = sst_platform_init_stream(substream);302if (ret_val)303return ret_val;304substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;305return ret_val;306}307308static int sst_platform_pcm_trigger(struct snd_pcm_substream *substream,309int cmd)310{311int ret_val = 0, str_id;312struct sst_runtime_stream *stream;313int str_cmd, status;314315pr_debug("sst_platform_pcm_trigger called\n");316stream = substream->runtime->private_data;317str_id = stream->stream_info.str_id;318switch (cmd) {319case SNDRV_PCM_TRIGGER_START:320pr_debug("sst: Trigger Start\n");321str_cmd = SST_SND_START;322status = SST_PLATFORM_RUNNING;323stream->stream_info.mad_substream = substream;324break;325case SNDRV_PCM_TRIGGER_STOP:326pr_debug("sst: in stop\n");327str_cmd = SST_SND_DROP;328status = SST_PLATFORM_DROPPED;329break;330case SNDRV_PCM_TRIGGER_PAUSE_PUSH:331pr_debug("sst: in pause\n");332str_cmd = SST_SND_PAUSE;333status = SST_PLATFORM_PAUSED;334break;335case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:336pr_debug("sst: in pause release\n");337str_cmd = SST_SND_RESUME;338status = SST_PLATFORM_RUNNING;339break;340default:341return -EINVAL;342}343ret_val = stream->sstdrv_ops->pcm_control->device_control(str_cmd,344&str_id);345if (!ret_val)346sst_set_stream_status(stream, status);347348return ret_val;349}350351352static snd_pcm_uframes_t sst_platform_pcm_pointer353(struct snd_pcm_substream *substream)354{355struct sst_runtime_stream *stream;356int ret_val, status;357struct pcm_stream_info *str_info;358359stream = substream->runtime->private_data;360status = sst_get_stream_status(stream);361if (status == SST_PLATFORM_INIT)362return 0;363str_info = &stream->stream_info;364ret_val = stream->sstdrv_ops->pcm_control->device_control(365SST_SND_BUFFER_POINTER, str_info);366if (ret_val) {367pr_err("sst: error code = %d\n", ret_val);368return ret_val;369}370return stream->stream_info.buffer_ptr;371}372373static int sst_platform_pcm_hw_params(struct snd_pcm_substream *substream,374struct snd_pcm_hw_params *params)375{376snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));377memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));378379return 0;380}381382static int sst_platform_pcm_hw_free(struct snd_pcm_substream *substream)383{384return snd_pcm_lib_free_pages(substream);385}386387static struct snd_pcm_ops sst_platform_ops = {388.open = sst_platform_open,389.close = sst_platform_close,390.ioctl = snd_pcm_lib_ioctl,391.prepare = sst_platform_pcm_prepare,392.trigger = sst_platform_pcm_trigger,393.pointer = sst_platform_pcm_pointer,394.hw_params = sst_platform_pcm_hw_params,395.hw_free = sst_platform_pcm_hw_free,396};397398static void sst_pcm_free(struct snd_pcm *pcm)399{400pr_debug("sst_pcm_free called\n");401snd_pcm_lib_preallocate_free_for_all(pcm);402}403404int sst_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,405struct snd_pcm *pcm)406{407int retval = 0;408409pr_debug("sst_pcm_new called\n");410if (dai->driver->playback.channels_min ||411dai->driver->capture.channels_min) {412retval = snd_pcm_lib_preallocate_pages_for_all(pcm,413SNDRV_DMA_TYPE_CONTINUOUS,414snd_dma_continuous_data(GFP_KERNEL),415SST_MIN_BUFFER, SST_MAX_BUFFER);416if (retval) {417pr_err("dma buffer allocationf fail\n");418return retval;419}420}421return retval;422}423struct snd_soc_platform_driver sst_soc_platform_drv = {424.ops = &sst_platform_ops,425.pcm_new = sst_pcm_new,426.pcm_free = sst_pcm_free,427};428429static int sst_platform_probe(struct platform_device *pdev)430{431int ret;432433pr_debug("sst_platform_probe called\n");434ret = snd_soc_register_platform(&pdev->dev, &sst_soc_platform_drv);435if (ret) {436pr_err("registering soc platform failed\n");437return ret;438}439440ret = snd_soc_register_dais(&pdev->dev,441sst_platform_dai, ARRAY_SIZE(sst_platform_dai));442if (ret) {443pr_err("registering cpu dais failed\n");444snd_soc_unregister_platform(&pdev->dev);445}446return ret;447}448449static int sst_platform_remove(struct platform_device *pdev)450{451452snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(sst_platform_dai));453snd_soc_unregister_platform(&pdev->dev);454pr_debug("sst_platform_remove success\n");455return 0;456}457458static struct platform_driver sst_platform_driver = {459.driver = {460.name = "sst-platform",461.owner = THIS_MODULE,462},463.probe = sst_platform_probe,464.remove = sst_platform_remove,465};466467static int __init sst_soc_platform_init(void)468{469pr_debug("sst_soc_platform_init called\n");470return platform_driver_register(&sst_platform_driver);471}472module_init(sst_soc_platform_init);473474static void __exit sst_soc_platform_exit(void)475{476platform_driver_unregister(&sst_platform_driver);477pr_debug("sst_soc_platform_exit success\n");478}479module_exit(sst_soc_platform_exit);480481MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");482MODULE_AUTHOR("Vinod Koul <[email protected]>");483MODULE_AUTHOR("Harsha Priya <[email protected]>");484MODULE_LICENSE("GPL v2");485MODULE_ALIAS("platform:sst-platform");486487488