2 * sst_mfld_platform.c - Intel MID Platform driver
4 * Copyright (C) 2010-2014 Intel Corp
5 * Author: Vinod Koul <vinod.koul@intel.com>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <sound/core.h>
25 #include <sound/pcm.h>
26 #include <sound/pcm_params.h>
27 #include <sound/soc.h>
28 #include <sound/compress_driver.h>
29 #include "sst-mfld-platform.h"
31 /* compress stream operations */
32 static void sst_compr_fragment_elapsed(void *arg)
34 struct snd_compr_stream *cstream = (struct snd_compr_stream *)arg;
36 pr_debug("fragment elapsed by driver\n");
38 snd_compr_fragment_elapsed(cstream);
41 static int sst_platform_compr_open(struct snd_compr_stream *cstream)
45 struct snd_compr_runtime *runtime = cstream->runtime;
46 struct sst_runtime_stream *stream;
48 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
52 spin_lock_init(&stream->status_lock);
55 if (!sst || !try_module_get(sst->dev->driver->owner)) {
56 pr_err("no device available to run\n");
60 stream->compr_ops = sst->compr_ops;
63 sst_set_stream_status(stream, SST_PLATFORM_INIT);
64 runtime->private_data = stream;
71 static int sst_platform_compr_free(struct snd_compr_stream *cstream)
73 struct sst_runtime_stream *stream;
74 int ret_val = 0, str_id;
76 stream = cstream->runtime->private_data;
80 ret_val = stream->compr_ops->close(str_id);
81 module_put(sst->dev->driver->owner);
83 pr_debug("%s: %d\n", __func__, ret_val);
87 static int sst_platform_compr_set_params(struct snd_compr_stream *cstream,
88 struct snd_compr_params *params)
90 struct sst_runtime_stream *stream;
92 struct snd_sst_params str_params;
93 struct sst_compress_cb cb;
95 stream = cstream->runtime->private_data;
96 /* construct fw structure for this*/
97 memset(&str_params, 0, sizeof(str_params));
99 str_params.ops = STREAM_OPS_PLAYBACK;
100 str_params.stream_type = SST_STREAM_TYPE_MUSIC;
101 str_params.device_type = SND_SST_DEVICE_COMPRESS;
103 switch (params->codec.id) {
104 case SND_AUDIOCODEC_MP3: {
105 str_params.codec = SST_CODEC_TYPE_MP3;
106 str_params.sparams.uc.mp3_params.codec = SST_CODEC_TYPE_MP3;
107 str_params.sparams.uc.mp3_params.num_chan = params->codec.ch_in;
108 str_params.sparams.uc.mp3_params.pcm_wd_sz = 16;
112 case SND_AUDIOCODEC_AAC: {
113 str_params.codec = SST_CODEC_TYPE_AAC;
114 str_params.sparams.uc.aac_params.codec = SST_CODEC_TYPE_AAC;
115 str_params.sparams.uc.aac_params.num_chan = params->codec.ch_in;
116 str_params.sparams.uc.aac_params.pcm_wd_sz = 16;
117 if (params->codec.format == SND_AUDIOSTREAMFORMAT_MP4ADTS)
118 str_params.sparams.uc.aac_params.bs_format =
120 else if (params->codec.format == SND_AUDIOSTREAMFORMAT_RAW)
121 str_params.sparams.uc.aac_params.bs_format =
124 pr_err("Undefined format%d\n", params->codec.format);
127 str_params.sparams.uc.aac_params.externalsr =
128 params->codec.sample_rate;
133 pr_err("codec not supported, id =%d\n", params->codec.id);
137 str_params.aparams.ring_buf_info[0].addr =
138 virt_to_phys(cstream->runtime->buffer);
139 str_params.aparams.ring_buf_info[0].size =
140 cstream->runtime->buffer_size;
141 str_params.aparams.sg_count = 1;
142 str_params.aparams.frag_size = cstream->runtime->fragment_size;
145 cb.compr_cb = sst_compr_fragment_elapsed;
147 retval = stream->compr_ops->open(&str_params, &cb);
149 pr_err("stream allocation failed %d\n", retval);
157 static int sst_platform_compr_trigger(struct snd_compr_stream *cstream, int cmd)
159 struct sst_runtime_stream *stream =
160 cstream->runtime->private_data;
162 return stream->compr_ops->control(cmd, stream->id);
165 static int sst_platform_compr_pointer(struct snd_compr_stream *cstream,
166 struct snd_compr_tstamp *tstamp)
168 struct sst_runtime_stream *stream;
170 stream = cstream->runtime->private_data;
171 stream->compr_ops->tstamp(stream->id, tstamp);
172 tstamp->byte_offset = tstamp->copied_total %
173 (u32)cstream->runtime->buffer_size;
174 pr_debug("calc bytes offset/copied bytes as %d\n", tstamp->byte_offset);
178 static int sst_platform_compr_ack(struct snd_compr_stream *cstream,
181 struct sst_runtime_stream *stream;
183 stream = cstream->runtime->private_data;
184 stream->compr_ops->ack(stream->id, (unsigned long)bytes);
185 stream->bytes_written += bytes;
190 static int sst_platform_compr_get_caps(struct snd_compr_stream *cstream,
191 struct snd_compr_caps *caps)
193 struct sst_runtime_stream *stream =
194 cstream->runtime->private_data;
196 return stream->compr_ops->get_caps(caps);
199 static int sst_platform_compr_get_codec_caps(struct snd_compr_stream *cstream,
200 struct snd_compr_codec_caps *codec)
202 struct sst_runtime_stream *stream =
203 cstream->runtime->private_data;
205 return stream->compr_ops->get_codec_caps(codec);
208 static int sst_platform_compr_set_metadata(struct snd_compr_stream *cstream,
209 struct snd_compr_metadata *metadata)
211 struct sst_runtime_stream *stream =
212 cstream->runtime->private_data;
214 return stream->compr_ops->set_metadata(stream->id, metadata);
217 struct snd_compr_ops sst_platform_compr_ops = {
219 .open = sst_platform_compr_open,
220 .free = sst_platform_compr_free,
221 .set_params = sst_platform_compr_set_params,
222 .set_metadata = sst_platform_compr_set_metadata,
223 .trigger = sst_platform_compr_trigger,
224 .pointer = sst_platform_compr_pointer,
225 .ack = sst_platform_compr_ack,
226 .get_caps = sst_platform_compr_get_caps,
227 .get_codec_caps = sst_platform_compr_get_codec_caps,