xref: /linux/sound/soc/intel/atom/sst-mfld-platform-compress.c (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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  *
9  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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 <asm/div64.h>
22 #include "sst-mfld-platform.h"
23 
24 /* compress stream operations */
25 static void sst_compr_fragment_elapsed(void *arg)
26 {
27 	struct snd_compr_stream *cstream = (struct snd_compr_stream *)arg;
28 
29 	pr_debug("fragment elapsed by driver\n");
30 	if (cstream)
31 		snd_compr_fragment_elapsed(cstream);
32 }
33 
34 static void sst_drain_notify(void *arg)
35 {
36 	struct snd_compr_stream *cstream = (struct snd_compr_stream *)arg;
37 
38 	pr_debug("drain notify by driver\n");
39 	if (cstream)
40 		snd_compr_drain_notify(cstream);
41 }
42 
43 static int sst_platform_compr_open(struct snd_soc_component *component,
44 				   struct snd_compr_stream *cstream)
45 {
46 	int ret_val;
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;
63 	stream->id = 0;
64 
65 	/* Turn on LPE */
66 	sst->compr_ops->power(sst->dev, true);
67 
68 	sst_set_stream_status(stream, SST_PLATFORM_INIT);
69 	runtime->private_data = stream;
70 	return 0;
71 out_ops:
72 	kfree(stream);
73 	return ret_val;
74 }
75 
76 static int sst_platform_compr_free(struct snd_soc_component *component,
77 				   struct snd_compr_stream *cstream)
78 {
79 	struct sst_runtime_stream *stream;
80 	int ret_val = 0, str_id;
81 
82 	stream = cstream->runtime->private_data;
83 	/* Turn off LPE */
84 	sst->compr_ops->power(sst->dev, false);
85 
86 	/*need to check*/
87 	str_id = stream->id;
88 	if (str_id)
89 		ret_val = stream->compr_ops->close(sst->dev, str_id);
90 	module_put(sst->dev->driver->owner);
91 	kfree(stream);
92 	pr_debug("%s: %d\n", __func__, ret_val);
93 	return 0;
94 }
95 
96 static int sst_platform_compr_set_params(struct snd_soc_component *component,
97 					 struct snd_compr_stream *cstream,
98 					 struct snd_compr_params *params)
99 {
100 	struct sst_runtime_stream *stream;
101 	int retval;
102 	struct snd_sst_params str_params;
103 	struct sst_compress_cb cb;
104 	struct sst_data *ctx = snd_soc_component_get_drvdata(component);
105 
106 	stream = cstream->runtime->private_data;
107 	/* construct fw structure for this*/
108 	memset(&str_params, 0, sizeof(str_params));
109 
110 	/* 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;
115 
116 	switch (params->codec.id) {
117 	case SND_AUDIOCODEC_MP3: {
118 		str_params.codec = SST_CODEC_TYPE_MP3;
119 		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;
126 		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;
157 	cb.drain_cb_param = cstream;
158 	cb.drain_notify = sst_drain_notify;
159 
160 	retval = stream->compr_ops->open(sst->dev, &str_params, &cb);
161 	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 
170 static int sst_platform_compr_trigger(struct snd_soc_component *component,
171 				      struct snd_compr_stream *cstream, int cmd)
172 {
173 	struct sst_runtime_stream *stream = cstream->runtime->private_data;
174 
175 	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);
179 		break;
180 	case SNDRV_PCM_TRIGGER_STOP:
181 		if (stream->compr_ops->stream_drop)
182 			return stream->compr_ops->stream_drop(sst->dev, stream->id);
183 		break;
184 	case SND_COMPR_TRIGGER_DRAIN:
185 		if (stream->compr_ops->stream_drain)
186 			return stream->compr_ops->stream_drain(sst->dev, stream->id);
187 		break;
188 	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);
191 		break;
192 	case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
193 		if (stream->compr_ops->stream_pause)
194 			return stream->compr_ops->stream_pause(sst->dev, stream->id);
195 		break;
196 	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);
199 		break;
200 	}
201 	return -EINVAL;
202 }
203 
204 static int sst_platform_compr_pointer(struct snd_soc_component *component,
205 				      struct snd_compr_stream *cstream,
206 				      struct snd_compr_tstamp64 *tstamp)
207 {
208 	struct sst_runtime_stream *stream;
209 	u64 temp_copied_total = tstamp->copied_total;
210 
211 	stream = cstream->runtime->private_data;
212 	stream->compr_ops->tstamp(sst->dev, stream->id, tstamp);
213 	tstamp->byte_offset =
214 		do_div(temp_copied_total, cstream->runtime->buffer_size);
215 	pr_debug("calc bytes offset/copied bytes as %u\n", tstamp->byte_offset);
216 	return 0;
217 }
218 
219 static int sst_platform_compr_ack(struct snd_soc_component *component,
220 				  struct snd_compr_stream *cstream,
221 				  size_t bytes)
222 {
223 	struct sst_runtime_stream *stream;
224 
225 	stream  = cstream->runtime->private_data;
226 	stream->compr_ops->ack(sst->dev, stream->id, (unsigned long)bytes);
227 	stream->bytes_written += bytes;
228 
229 	return 0;
230 }
231 
232 static int sst_platform_compr_get_caps(struct snd_soc_component *component,
233 				       struct snd_compr_stream *cstream,
234 				       struct snd_compr_caps *caps)
235 {
236 	struct sst_runtime_stream *stream =
237 		cstream->runtime->private_data;
238 
239 	return stream->compr_ops->get_caps(caps);
240 }
241 
242 static int sst_platform_compr_get_codec_caps(struct snd_soc_component *component,
243 					     struct snd_compr_stream *cstream,
244 					     struct snd_compr_codec_caps *codec)
245 {
246 	struct sst_runtime_stream *stream =
247 		cstream->runtime->private_data;
248 
249 	return stream->compr_ops->get_codec_caps(codec);
250 }
251 
252 static int sst_platform_compr_set_metadata(struct snd_soc_component *component,
253 					   struct snd_compr_stream *cstream,
254 					   struct snd_compr_metadata *metadata)
255 {
256 	struct sst_runtime_stream *stream  =
257 		 cstream->runtime->private_data;
258 
259 	return stream->compr_ops->set_metadata(sst->dev, stream->id, metadata);
260 }
261 
262 const struct snd_compress_ops sst_platform_compress_ops = {
263 
264 	.open = sst_platform_compr_open,
265 	.free = sst_platform_compr_free,
266 	.set_params = sst_platform_compr_set_params,
267 	.set_metadata = sst_platform_compr_set_metadata,
268 	.trigger = sst_platform_compr_trigger,
269 	.pointer = sst_platform_compr_pointer,
270 	.ack = sst_platform_compr_ack,
271 	.get_caps = sst_platform_compr_get_caps,
272 	.get_codec_caps = sst_platform_compr_get_codec_caps,
273 };
274