blob: 1595e01a7e126c77c8874af2c8b2537edcdcd47a [file] [log] [blame]
Thomas Gleixner8e8e69d2019-05-29 07:17:59 -07001// SPDX-License-Identifier: GPL-2.0-only
Vinod Koul4b68b4e2014-05-05 14:27:50 +05302/*
3 * sst_mfld_platform.c - Intel MID Platform driver
4 *
5 * Copyright (C) 2010-2014 Intel Corp
6 * Author: Vinod Koul <vinod.koul@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
Vinod Koul4b68b4e2014-05-05 14:27:50 +05309 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 */
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/slab.h>
14#include <linux/io.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"
22
23/* compress stream operations */
24static void sst_compr_fragment_elapsed(void *arg)
25{
26 struct snd_compr_stream *cstream = (struct snd_compr_stream *)arg;
27
28 pr_debug("fragment elapsed by driver\n");
29 if (cstream)
30 snd_compr_fragment_elapsed(cstream);
31}
32
Vinod Koulbd17aa42014-05-15 21:38:16 +053033static void sst_drain_notify(void *arg)
34{
35 struct snd_compr_stream *cstream = (struct snd_compr_stream *)arg;
36
37 pr_debug("drain notify by driver\n");
38 if (cstream)
39 snd_compr_drain_notify(cstream);
40}
41
Kuninori Morimotoc60e4452020-04-20 16:09:09 +090042static int sst_platform_compr_open(struct snd_soc_component *component,
43 struct snd_compr_stream *cstream)
Vinod Koul4b68b4e2014-05-05 14:27:50 +053044{
45
46 int ret_val = 0;
47 struct snd_compr_runtime *runtime = cstream->runtime;
48 struct sst_runtime_stream *stream;
49
50 stream = kzalloc(sizeof(*stream), GFP_KERNEL);
51 if (!stream)
52 return -ENOMEM;
53
54 spin_lock_init(&stream->status_lock);
55
56 /* get the sst ops */
57 if (!sst || !try_module_get(sst->dev->driver->owner)) {
58 pr_err("no device available to run\n");
59 ret_val = -ENODEV;
60 goto out_ops;
61 }
62 stream->compr_ops = sst->compr_ops;
Vinod Koul4b68b4e2014-05-05 14:27:50 +053063 stream->id = 0;
Subhransu S. Prustyfdcc4a02014-10-30 16:20:58 +053064
65 /* Turn on LPE */
66 sst->compr_ops->power(sst->dev, true);
67
Vinod Koul4b68b4e2014-05-05 14:27:50 +053068 sst_set_stream_status(stream, SST_PLATFORM_INIT);
69 runtime->private_data = stream;
70 return 0;
71out_ops:
72 kfree(stream);
73 return ret_val;
74}
75
Kuninori Morimotoc60e4452020-04-20 16:09:09 +090076static int sst_platform_compr_free(struct snd_soc_component *component,
77 struct snd_compr_stream *cstream)
Vinod Koul4b68b4e2014-05-05 14:27:50 +053078{
79 struct sst_runtime_stream *stream;
80 int ret_val = 0, str_id;
81
82 stream = cstream->runtime->private_data;
Subhransu S. Prustyfdcc4a02014-10-30 16:20:58 +053083 /* Turn off LPE */
84 sst->compr_ops->power(sst->dev, false);
85
Vinod Koul4b68b4e2014-05-05 14:27:50 +053086 /*need to check*/
87 str_id = stream->id;
88 if (str_id)
Subhransu S. Prusty06cb1eb2014-08-21 18:20:49 +053089 ret_val = stream->compr_ops->close(sst->dev, str_id);
Vinod Koul4b68b4e2014-05-05 14:27:50 +053090 module_put(sst->dev->driver->owner);
91 kfree(stream);
92 pr_debug("%s: %d\n", __func__, ret_val);
93 return 0;
94}
95
Kuninori Morimotoc60e4452020-04-20 16:09:09 +090096static int sst_platform_compr_set_params(struct snd_soc_component *component,
97 struct snd_compr_stream *cstream,
98 struct snd_compr_params *params)
Vinod Koul4b68b4e2014-05-05 14:27:50 +053099{
100 struct sst_runtime_stream *stream;
101 int retval;
102 struct snd_sst_params str_params;
103 struct sst_compress_cb cb;
Kuninori Morimoto68409622018-01-29 02:41:28 +0000104 struct sst_data *ctx = snd_soc_component_get_drvdata(component);
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530105
106 stream = cstream->runtime->private_data;
107 /* construct fw structure for this*/
108 memset(&str_params, 0, sizeof(str_params));
109
Vinod Koul0ec66fe2014-06-13 18:03:57 +0530110 /* fill the device type and stream id to pass to SST driver */
111 retval = sst_fill_stream_params(cstream, ctx, &str_params, true);
112 pr_debug("compr_set_params: fill stream params ret_val = 0x%x\n", retval);
113 if (retval < 0)
114 return retval;
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530115
116 switch (params->codec.id) {
117 case SND_AUDIOCODEC_MP3: {
118 str_params.codec = SST_CODEC_TYPE_MP3;
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530119 str_params.sparams.uc.mp3_params.num_chan = params->codec.ch_in;
120 str_params.sparams.uc.mp3_params.pcm_wd_sz = 16;
121 break;
122 }
123
124 case SND_AUDIOCODEC_AAC: {
125 str_params.codec = SST_CODEC_TYPE_AAC;
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530126 str_params.sparams.uc.aac_params.num_chan = params->codec.ch_in;
127 str_params.sparams.uc.aac_params.pcm_wd_sz = 16;
128 if (params->codec.format == SND_AUDIOSTREAMFORMAT_MP4ADTS)
129 str_params.sparams.uc.aac_params.bs_format =
130 AAC_BIT_STREAM_ADTS;
131 else if (params->codec.format == SND_AUDIOSTREAMFORMAT_RAW)
132 str_params.sparams.uc.aac_params.bs_format =
133 AAC_BIT_STREAM_RAW;
134 else {
135 pr_err("Undefined format%d\n", params->codec.format);
136 return -EINVAL;
137 }
138 str_params.sparams.uc.aac_params.externalsr =
139 params->codec.sample_rate;
140 break;
141 }
142
143 default:
144 pr_err("codec not supported, id =%d\n", params->codec.id);
145 return -EINVAL;
146 }
147
148 str_params.aparams.ring_buf_info[0].addr =
149 virt_to_phys(cstream->runtime->buffer);
150 str_params.aparams.ring_buf_info[0].size =
151 cstream->runtime->buffer_size;
152 str_params.aparams.sg_count = 1;
153 str_params.aparams.frag_size = cstream->runtime->fragment_size;
154
155 cb.param = cstream;
156 cb.compr_cb = sst_compr_fragment_elapsed;
Vinod Koulbd17aa42014-05-15 21:38:16 +0530157 cb.drain_cb_param = cstream;
158 cb.drain_notify = sst_drain_notify;
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530159
Subhransu S. Prusty06cb1eb2014-08-21 18:20:49 +0530160 retval = stream->compr_ops->open(sst->dev, &str_params, &cb);
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530161 if (retval < 0) {
162 pr_err("stream allocation failed %d\n", retval);
163 return retval;
164 }
165
166 stream->id = retval;
167 return 0;
168}
169
Kuninori Morimotoc60e4452020-04-20 16:09:09 +0900170static int sst_platform_compr_trigger(struct snd_soc_component *component,
171 struct snd_compr_stream *cstream, int cmd)
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530172{
Subhransu S. Prusty06cb1eb2014-08-21 18:20:49 +0530173 struct sst_runtime_stream *stream = cstream->runtime->private_data;
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530174
Subhransu S. Prusty06cb1eb2014-08-21 18:20:49 +0530175 switch (cmd) {
176 case SNDRV_PCM_TRIGGER_START:
177 if (stream->compr_ops->stream_start)
178 return stream->compr_ops->stream_start(sst->dev, stream->id);
Alan Cox18f58392016-06-23 22:07:03 +0530179 break;
Subhransu S. Prusty06cb1eb2014-08-21 18:20:49 +0530180 case SNDRV_PCM_TRIGGER_STOP:
181 if (stream->compr_ops->stream_drop)
182 return stream->compr_ops->stream_drop(sst->dev, stream->id);
Alan Cox18f58392016-06-23 22:07:03 +0530183 break;
Subhransu S. Prusty06cb1eb2014-08-21 18:20:49 +0530184 case SND_COMPR_TRIGGER_DRAIN:
185 if (stream->compr_ops->stream_drain)
186 return stream->compr_ops->stream_drain(sst->dev, stream->id);
Alan Cox18f58392016-06-23 22:07:03 +0530187 break;
Subhransu S. Prusty06cb1eb2014-08-21 18:20:49 +0530188 case SND_COMPR_TRIGGER_PARTIAL_DRAIN:
189 if (stream->compr_ops->stream_partial_drain)
190 return stream->compr_ops->stream_partial_drain(sst->dev, stream->id);
Alan Cox18f58392016-06-23 22:07:03 +0530191 break;
Subhransu S. Prusty06cb1eb2014-08-21 18:20:49 +0530192 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
193 if (stream->compr_ops->stream_pause)
194 return stream->compr_ops->stream_pause(sst->dev, stream->id);
Alan Cox18f58392016-06-23 22:07:03 +0530195 break;
Subhransu S. Prusty06cb1eb2014-08-21 18:20:49 +0530196 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
197 if (stream->compr_ops->stream_pause_release)
198 return stream->compr_ops->stream_pause_release(sst->dev, stream->id);
Alan Cox18f58392016-06-23 22:07:03 +0530199 break;
Subhransu S. Prusty06cb1eb2014-08-21 18:20:49 +0530200 }
Alan Cox18f58392016-06-23 22:07:03 +0530201 return -EINVAL;
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530202}
203
Kuninori Morimotoc60e4452020-04-20 16:09:09 +0900204static int sst_platform_compr_pointer(struct snd_soc_component *component,
205 struct snd_compr_stream *cstream,
206 struct snd_compr_tstamp *tstamp)
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530207{
208 struct sst_runtime_stream *stream;
209
210 stream = cstream->runtime->private_data;
Subhransu S. Prusty06cb1eb2014-08-21 18:20:49 +0530211 stream->compr_ops->tstamp(sst->dev, stream->id, tstamp);
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530212 tstamp->byte_offset = tstamp->copied_total %
213 (u32)cstream->runtime->buffer_size;
214 pr_debug("calc bytes offset/copied bytes as %d\n", tstamp->byte_offset);
215 return 0;
216}
217
Kuninori Morimotoc60e4452020-04-20 16:09:09 +0900218static int sst_platform_compr_ack(struct snd_soc_component *component,
219 struct snd_compr_stream *cstream,
220 size_t bytes)
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530221{
222 struct sst_runtime_stream *stream;
223
224 stream = cstream->runtime->private_data;
Subhransu S. Prusty06cb1eb2014-08-21 18:20:49 +0530225 stream->compr_ops->ack(sst->dev, stream->id, (unsigned long)bytes);
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530226 stream->bytes_written += bytes;
227
228 return 0;
229}
230
Kuninori Morimotoc60e4452020-04-20 16:09:09 +0900231static int sst_platform_compr_get_caps(struct snd_soc_component *component,
232 struct snd_compr_stream *cstream,
233 struct snd_compr_caps *caps)
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530234{
235 struct sst_runtime_stream *stream =
236 cstream->runtime->private_data;
237
238 return stream->compr_ops->get_caps(caps);
239}
240
Kuninori Morimotoc60e4452020-04-20 16:09:09 +0900241static int sst_platform_compr_get_codec_caps(struct snd_soc_component *component,
242 struct snd_compr_stream *cstream,
243 struct snd_compr_codec_caps *codec)
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530244{
245 struct sst_runtime_stream *stream =
246 cstream->runtime->private_data;
247
248 return stream->compr_ops->get_codec_caps(codec);
249}
250
Kuninori Morimotoc60e4452020-04-20 16:09:09 +0900251static int sst_platform_compr_set_metadata(struct snd_soc_component *component,
252 struct snd_compr_stream *cstream,
253 struct snd_compr_metadata *metadata)
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530254{
255 struct sst_runtime_stream *stream =
256 cstream->runtime->private_data;
257
Subhransu S. Prusty06cb1eb2014-08-21 18:20:49 +0530258 return stream->compr_ops->set_metadata(sst->dev, stream->id, metadata);
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530259}
260
Kuninori Morimotoc60e4452020-04-20 16:09:09 +0900261const struct snd_compress_ops sst_platform_compress_ops = {
Vinod Koul4b68b4e2014-05-05 14:27:50 +0530262
263 .open = sst_platform_compr_open,
264 .free = sst_platform_compr_free,
265 .set_params = sst_platform_compr_set_params,
266 .set_metadata = sst_platform_compr_set_metadata,
267 .trigger = sst_platform_compr_trigger,
268 .pointer = sst_platform_compr_pointer,
269 .ack = sst_platform_compr_ack,
270 .get_caps = sst_platform_compr_get_caps,
271 .get_codec_caps = sst_platform_compr_get_codec_caps,
272};