1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
5 // Author: Daniel Baluta <daniel.baluta@nxp.com>
9 #include <sound/compress_driver.h>
10 #include "sof-audio.h"
12 #include "sof-utils.h"
14 static void sof_set_transferred_bytes(struct sof_compr_stream *sstream,
15 u64 host_pos, u64 buffer_size)
20 div64_u64_rem(sstream->copied_total, buffer_size, &prev_pos);
22 if (host_pos < prev_pos)
23 copied = (buffer_size - prev_pos) + host_pos;
25 copied = host_pos - prev_pos;
27 sstream->copied_total += copied;
30 static void snd_sof_compr_fragment_elapsed_work(struct work_struct *work)
32 struct snd_sof_pcm_stream *sps =
33 container_of(work, struct snd_sof_pcm_stream,
36 snd_compr_fragment_elapsed(sps->cstream);
39 void snd_sof_compr_init_elapsed_work(struct work_struct *work)
41 INIT_WORK(work, snd_sof_compr_fragment_elapsed_work);
45 * sof compr fragment elapse, this could be called in irq thread context
47 void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream)
49 struct snd_soc_pcm_runtime *rtd;
50 struct snd_compr_runtime *crtd;
51 struct snd_soc_component *component;
52 struct sof_compr_stream *sstream;
53 struct snd_sof_pcm *spcm;
58 rtd = cstream->private_data;
59 crtd = cstream->runtime;
60 sstream = crtd->private_data;
61 component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
63 spcm = snd_sof_find_spcm_dai(component, rtd);
65 dev_err(component->dev,
66 "fragment elapsed called for unknown stream!\n");
70 sof_set_transferred_bytes(sstream, spcm->stream[cstream->direction].posn.host_posn,
73 /* use the same workqueue-based solution as for PCM, cf. snd_sof_pcm_elapsed */
74 schedule_work(&spcm->stream[cstream->direction].period_elapsed_work);
77 static int create_page_table(struct snd_soc_component *component,
78 struct snd_compr_stream *cstream,
79 unsigned char *dma_area, size_t size)
81 struct snd_dma_buffer *dmab = cstream->runtime->dma_buffer_p;
82 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
83 int dir = cstream->direction;
84 struct snd_sof_pcm *spcm;
86 spcm = snd_sof_find_spcm_dai(component, rtd);
90 return snd_sof_create_page_table(component->dev, dmab,
91 spcm->stream[dir].page_table.area, size);
94 static int sof_compr_open(struct snd_soc_component *component,
95 struct snd_compr_stream *cstream)
97 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
98 struct snd_compr_runtime *crtd = cstream->runtime;
99 struct sof_compr_stream *sstream;
100 struct snd_sof_pcm *spcm;
103 sstream = kzalloc(sizeof(*sstream), GFP_KERNEL);
107 spcm = snd_sof_find_spcm_dai(component, rtd);
113 dir = cstream->direction;
115 if (spcm->stream[dir].cstream) {
120 spcm->stream[dir].cstream = cstream;
121 spcm->stream[dir].posn.host_posn = 0;
122 spcm->stream[dir].posn.dai_posn = 0;
123 spcm->prepared[dir] = false;
125 crtd->private_data = sstream;
130 static int sof_compr_free(struct snd_soc_component *component,
131 struct snd_compr_stream *cstream)
133 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
134 struct sof_compr_stream *sstream = cstream->runtime->private_data;
135 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
136 struct sof_ipc_stream stream;
137 struct sof_ipc_reply reply;
138 struct snd_sof_pcm *spcm;
141 spcm = snd_sof_find_spcm_dai(component, rtd);
145 stream.hdr.size = sizeof(stream);
146 stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
147 stream.comp_id = spcm->stream[cstream->direction].comp_id;
149 if (spcm->prepared[cstream->direction]) {
150 ret = sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream),
151 &reply, sizeof(reply));
153 spcm->prepared[cstream->direction] = false;
156 cancel_work_sync(&spcm->stream[cstream->direction].period_elapsed_work);
157 spcm->stream[cstream->direction].cstream = NULL;
163 static int sof_compr_set_params(struct snd_soc_component *component,
164 struct snd_compr_stream *cstream, struct snd_compr_params *params)
166 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
167 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
168 struct snd_compr_runtime *crtd = cstream->runtime;
169 struct sof_ipc_pcm_params_reply ipc_params_reply;
170 struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
171 struct sof_ipc_fw_version *v = &ready->version;
172 struct sof_compr_stream *sstream;
173 struct sof_ipc_pcm_params *pcm;
174 struct snd_sof_pcm *spcm;
175 size_t ext_data_size;
178 if (v->abi_version < SOF_ABI_VER(3, 22, 0)) {
179 dev_err(component->dev,
180 "Compress params not supported with FW ABI version %d:%d:%d\n",
181 SOF_ABI_VERSION_MAJOR(v->abi_version),
182 SOF_ABI_VERSION_MINOR(v->abi_version),
183 SOF_ABI_VERSION_PATCH(v->abi_version));
187 sstream = crtd->private_data;
189 spcm = snd_sof_find_spcm_dai(component, rtd);
194 ext_data_size = sizeof(params->codec);
196 if (sizeof(*pcm) + ext_data_size > sdev->ipc->max_payload_size)
199 pcm = kzalloc(sizeof(*pcm) + ext_data_size, GFP_KERNEL);
203 cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG;
204 cstream->dma_buffer.dev.dev = sdev->dev;
205 ret = snd_compr_malloc_pages(cstream, crtd->buffer_size);
209 ret = create_page_table(component, cstream, crtd->dma_area, crtd->dma_bytes);
213 pcm->params.buffer.pages = PFN_UP(crtd->dma_bytes);
214 pcm->hdr.size = sizeof(*pcm) + ext_data_size;
215 pcm->hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
217 pcm->comp_id = spcm->stream[cstream->direction].comp_id;
218 pcm->params.hdr.size = sizeof(pcm->params) + ext_data_size;
219 pcm->params.buffer.phy_addr = spcm->stream[cstream->direction].page_table.addr;
220 pcm->params.buffer.size = crtd->dma_bytes;
221 pcm->params.direction = cstream->direction;
222 pcm->params.channels = params->codec.ch_out;
223 pcm->params.rate = params->codec.sample_rate;
224 pcm->params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
225 pcm->params.frame_fmt = SOF_IPC_FRAME_S32_LE;
226 pcm->params.sample_container_bytes =
227 snd_pcm_format_physical_width(SNDRV_PCM_FORMAT_S32) >> 3;
228 pcm->params.host_period_bytes = params->buffer.fragment_size;
229 pcm->params.ext_data_length = ext_data_size;
231 memcpy((u8 *)pcm->params.ext_data, ¶ms->codec, ext_data_size);
233 ret = sof_ipc_tx_message(sdev->ipc, pcm, sizeof(*pcm) + ext_data_size,
234 &ipc_params_reply, sizeof(ipc_params_reply));
236 dev_err(component->dev, "error ipc failed\n");
240 sstream->sampling_rate = params->codec.sample_rate;
241 sstream->channels = params->codec.ch_out;
242 sstream->sample_container_bytes = pcm->params.sample_container_bytes;
244 spcm->prepared[cstream->direction] = true;
252 static int sof_compr_get_params(struct snd_soc_component *component,
253 struct snd_compr_stream *cstream, struct snd_codec *params)
255 /* TODO: we don't query the supported codecs for now, if the
256 * application asks for an unsupported codec the set_params() will fail.
261 static int sof_compr_trigger(struct snd_soc_component *component,
262 struct snd_compr_stream *cstream, int cmd)
264 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
265 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
266 struct sof_ipc_stream stream;
267 struct sof_ipc_reply reply;
268 struct snd_sof_pcm *spcm;
270 spcm = snd_sof_find_spcm_dai(component, rtd);
274 stream.hdr.size = sizeof(stream);
275 stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
276 stream.comp_id = spcm->stream[cstream->direction].comp_id;
279 case SNDRV_PCM_TRIGGER_START:
280 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
282 case SNDRV_PCM_TRIGGER_STOP:
283 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
285 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
286 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
288 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
289 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
292 dev_err(component->dev, "error: unhandled trigger cmd %d\n", cmd);
296 return sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream),
297 &reply, sizeof(reply));
300 static int sof_compr_copy_playback(struct snd_compr_runtime *rtd,
301 char __user *buf, size_t count)
304 unsigned int offset, n;
307 div_u64_rem(rtd->total_bytes_available, rtd->buffer_size, &offset);
308 ptr = rtd->dma_area + offset;
309 n = rtd->buffer_size - offset;
312 ret = copy_from_user(ptr, buf, count);
314 ret = copy_from_user(ptr, buf, n);
315 ret += copy_from_user(rtd->dma_area, buf + n, count - n);
321 static int sof_compr_copy_capture(struct snd_compr_runtime *rtd,
322 char __user *buf, size_t count)
325 unsigned int offset, n;
328 div_u64_rem(rtd->total_bytes_transferred, rtd->buffer_size, &offset);
329 ptr = rtd->dma_area + offset;
330 n = rtd->buffer_size - offset;
333 ret = copy_to_user(buf, ptr, count);
335 ret = copy_to_user(buf, ptr, n);
336 ret += copy_to_user(buf + n, rtd->dma_area, count - n);
342 static int sof_compr_copy(struct snd_soc_component *component,
343 struct snd_compr_stream *cstream,
344 char __user *buf, size_t count)
346 struct snd_compr_runtime *rtd = cstream->runtime;
348 if (count > rtd->buffer_size)
349 count = rtd->buffer_size;
351 if (cstream->direction == SND_COMPRESS_PLAYBACK)
352 return sof_compr_copy_playback(rtd, buf, count);
354 return sof_compr_copy_capture(rtd, buf, count);
357 static int sof_compr_pointer(struct snd_soc_component *component,
358 struct snd_compr_stream *cstream,
359 struct snd_compr_tstamp *tstamp)
361 struct snd_sof_pcm *spcm;
362 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
363 struct sof_compr_stream *sstream = cstream->runtime->private_data;
365 spcm = snd_sof_find_spcm_dai(component, rtd);
369 tstamp->sampling_rate = sstream->sampling_rate;
370 tstamp->copied_total = sstream->copied_total;
371 tstamp->pcm_io_frames = div_u64(spcm->stream[cstream->direction].posn.dai_posn,
372 sstream->channels * sstream->sample_container_bytes);
377 struct snd_compress_ops sof_compressed_ops = {
378 .open = sof_compr_open,
379 .free = sof_compr_free,
380 .set_params = sof_compr_set_params,
381 .get_params = sof_compr_get_params,
382 .trigger = sof_compr_trigger,
383 .pointer = sof_compr_pointer,
384 .copy = sof_compr_copy,
386 EXPORT_SYMBOL(sof_compressed_ops);