1 // SPDX-License-Identifier: GPL-2.0-only
3 * sst_mfld_platform.c - Intel MID Platform driver
5 * Copyright (C) 2010-2014 Intel Corp
6 * Author: Vinod Koul <vinod.koul@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <sound/core.h>
17 #include <sound/pcm.h>
18 #include <sound/pcm_params.h>
19 #include <sound/soc.h>
20 #include <sound/compress_driver.h>
21 #include "sst-mfld-platform.h"
23 /* compress stream operations */
24 static void sst_compr_fragment_elapsed(void *arg)
26 struct snd_compr_stream *cstream = (struct snd_compr_stream *)arg;
28 pr_debug("fragment elapsed by driver\n");
30 snd_compr_fragment_elapsed(cstream);
33 static void sst_drain_notify(void *arg)
35 struct snd_compr_stream *cstream = (struct snd_compr_stream *)arg;
37 pr_debug("drain notify by driver\n");
39 snd_compr_drain_notify(cstream);
42 static int sst_platform_compr_open(struct snd_soc_component *component,
43 struct snd_compr_stream *cstream)
46 struct snd_compr_runtime *runtime = cstream->runtime;
47 struct sst_runtime_stream *stream;
49 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
53 spin_lock_init(&stream->status_lock);
56 if (!sst || !try_module_get(sst->dev->driver->owner)) {
57 pr_err("no device available to run\n");
61 stream->compr_ops = sst->compr_ops;
65 sst->compr_ops->power(sst->dev, true);
67 sst_set_stream_status(stream, SST_PLATFORM_INIT);
68 runtime->private_data = stream;
75 static int sst_platform_compr_free(struct snd_soc_component *component,
76 struct snd_compr_stream *cstream)
78 struct sst_runtime_stream *stream;
79 int ret_val = 0, str_id;
81 stream = cstream->runtime->private_data;
83 sst->compr_ops->power(sst->dev, false);
88 ret_val = stream->compr_ops->close(sst->dev, str_id);
89 module_put(sst->dev->driver->owner);
91 pr_debug("%s: %d\n", __func__, ret_val);
95 static int sst_platform_compr_set_params(struct snd_soc_component *component,
96 struct snd_compr_stream *cstream,
97 struct snd_compr_params *params)
99 struct sst_runtime_stream *stream;
101 struct snd_sst_params str_params;
102 struct sst_compress_cb cb;
103 struct sst_data *ctx = snd_soc_component_get_drvdata(component);
105 stream = cstream->runtime->private_data;
106 /* construct fw structure for this*/
107 memset(&str_params, 0, sizeof(str_params));
109 /* fill the device type and stream id to pass to SST driver */
110 retval = sst_fill_stream_params(cstream, ctx, &str_params, true);
111 pr_debug("compr_set_params: fill stream params ret_val = 0x%x\n", retval);
115 switch (params->codec.id) {
116 case SND_AUDIOCODEC_MP3: {
117 str_params.codec = SST_CODEC_TYPE_MP3;
118 str_params.sparams.uc.mp3_params.num_chan = params->codec.ch_in;
119 str_params.sparams.uc.mp3_params.pcm_wd_sz = 16;
123 case SND_AUDIOCODEC_AAC: {
124 str_params.codec = SST_CODEC_TYPE_AAC;
125 str_params.sparams.uc.aac_params.num_chan = params->codec.ch_in;
126 str_params.sparams.uc.aac_params.pcm_wd_sz = 16;
127 if (params->codec.format == SND_AUDIOSTREAMFORMAT_MP4ADTS)
128 str_params.sparams.uc.aac_params.bs_format =
130 else if (params->codec.format == SND_AUDIOSTREAMFORMAT_RAW)
131 str_params.sparams.uc.aac_params.bs_format =
134 pr_err("Undefined format%d\n", params->codec.format);
137 str_params.sparams.uc.aac_params.externalsr =
138 params->codec.sample_rate;
143 pr_err("codec not supported, id =%d\n", params->codec.id);
147 str_params.aparams.ring_buf_info[0].addr =
148 virt_to_phys(cstream->runtime->buffer);
149 str_params.aparams.ring_buf_info[0].size =
150 cstream->runtime->buffer_size;
151 str_params.aparams.sg_count = 1;
152 str_params.aparams.frag_size = cstream->runtime->fragment_size;
155 cb.compr_cb = sst_compr_fragment_elapsed;
156 cb.drain_cb_param = cstream;
157 cb.drain_notify = sst_drain_notify;
159 retval = stream->compr_ops->open(sst->dev, &str_params, &cb);
161 pr_err("stream allocation failed %d\n", retval);
169 static int sst_platform_compr_trigger(struct snd_soc_component *component,
170 struct snd_compr_stream *cstream, int cmd)
172 struct sst_runtime_stream *stream = cstream->runtime->private_data;
175 case SNDRV_PCM_TRIGGER_START:
176 if (stream->compr_ops->stream_start)
177 return stream->compr_ops->stream_start(sst->dev, stream->id);
179 case SNDRV_PCM_TRIGGER_STOP:
180 if (stream->compr_ops->stream_drop)
181 return stream->compr_ops->stream_drop(sst->dev, stream->id);
183 case SND_COMPR_TRIGGER_DRAIN:
184 if (stream->compr_ops->stream_drain)
185 return stream->compr_ops->stream_drain(sst->dev, stream->id);
187 case SND_COMPR_TRIGGER_PARTIAL_DRAIN:
188 if (stream->compr_ops->stream_partial_drain)
189 return stream->compr_ops->stream_partial_drain(sst->dev, stream->id);
191 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
192 if (stream->compr_ops->stream_pause)
193 return stream->compr_ops->stream_pause(sst->dev, stream->id);
195 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
196 if (stream->compr_ops->stream_pause_release)
197 return stream->compr_ops->stream_pause_release(sst->dev, stream->id);
203 static int sst_platform_compr_pointer(struct snd_soc_component *component,
204 struct snd_compr_stream *cstream,
205 struct snd_compr_tstamp *tstamp)
207 struct sst_runtime_stream *stream;
209 stream = cstream->runtime->private_data;
210 stream->compr_ops->tstamp(sst->dev, stream->id, tstamp);
211 tstamp->byte_offset = tstamp->copied_total %
212 (u32)cstream->runtime->buffer_size;
213 pr_debug("calc bytes offset/copied bytes as %d\n", tstamp->byte_offset);
217 static int sst_platform_compr_ack(struct snd_soc_component *component,
218 struct snd_compr_stream *cstream,
221 struct sst_runtime_stream *stream;
223 stream = cstream->runtime->private_data;
224 stream->compr_ops->ack(sst->dev, stream->id, (unsigned long)bytes);
225 stream->bytes_written += bytes;
230 static int sst_platform_compr_get_caps(struct snd_soc_component *component,
231 struct snd_compr_stream *cstream,
232 struct snd_compr_caps *caps)
234 struct sst_runtime_stream *stream =
235 cstream->runtime->private_data;
237 return stream->compr_ops->get_caps(caps);
240 static int sst_platform_compr_get_codec_caps(struct snd_soc_component *component,
241 struct snd_compr_stream *cstream,
242 struct snd_compr_codec_caps *codec)
244 struct sst_runtime_stream *stream =
245 cstream->runtime->private_data;
247 return stream->compr_ops->get_codec_caps(codec);
250 static int sst_platform_compr_set_metadata(struct snd_soc_component *component,
251 struct snd_compr_stream *cstream,
252 struct snd_compr_metadata *metadata)
254 struct sst_runtime_stream *stream =
255 cstream->runtime->private_data;
257 return stream->compr_ops->set_metadata(sst->dev, stream->id, metadata);
260 const struct snd_compress_ops sst_platform_compress_ops = {
262 .open = sst_platform_compr_open,
263 .free = sst_platform_compr_free,
264 .set_params = sst_platform_compr_set_params,
265 .set_metadata = sst_platform_compr_set_metadata,
266 .trigger = sst_platform_compr_trigger,
267 .pointer = sst_platform_compr_pointer,
268 .ack = sst_platform_compr_ack,
269 .get_caps = sst_platform_compr_get_caps,
270 .get_codec_caps = sst_platform_compr_get_codec_caps,