1858f7a5cSDaniel Baluta // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2858f7a5cSDaniel Baluta // 3858f7a5cSDaniel Baluta // Copyright 2021 NXP 4858f7a5cSDaniel Baluta // 5858f7a5cSDaniel Baluta // Author: Daniel Baluta <daniel.baluta@nxp.com> 6858f7a5cSDaniel Baluta 7858f7a5cSDaniel Baluta #include <sound/soc.h> 8858f7a5cSDaniel Baluta #include <sound/sof.h> 9858f7a5cSDaniel Baluta #include <sound/compress_driver.h> 10858f7a5cSDaniel Baluta #include "sof-audio.h" 11858f7a5cSDaniel Baluta #include "sof-priv.h" 12ee844305SPeter Ujfalusi #include "sof-utils.h" 13858f7a5cSDaniel Baluta 146324cf90SDaniel Baluta static void sof_set_transferred_bytes(struct snd_compr_tstamp *tstamp, 156324cf90SDaniel Baluta u64 host_pos, u64 buffer_size) 166324cf90SDaniel Baluta { 176324cf90SDaniel Baluta u64 prev_pos; 186324cf90SDaniel Baluta unsigned int copied; 196324cf90SDaniel Baluta 206324cf90SDaniel Baluta div64_u64_rem(tstamp->copied_total, buffer_size, &prev_pos); 216324cf90SDaniel Baluta 226324cf90SDaniel Baluta if (host_pos < prev_pos) 236324cf90SDaniel Baluta copied = (buffer_size - prev_pos) + host_pos; 246324cf90SDaniel Baluta else 256324cf90SDaniel Baluta copied = host_pos - prev_pos; 266324cf90SDaniel Baluta 276324cf90SDaniel Baluta tstamp->copied_total += copied; 286324cf90SDaniel Baluta } 296324cf90SDaniel Baluta 30858f7a5cSDaniel Baluta static void snd_sof_compr_fragment_elapsed_work(struct work_struct *work) 31858f7a5cSDaniel Baluta { 32858f7a5cSDaniel Baluta struct snd_sof_pcm_stream *sps = 33858f7a5cSDaniel Baluta container_of(work, struct snd_sof_pcm_stream, 34858f7a5cSDaniel Baluta period_elapsed_work); 35858f7a5cSDaniel Baluta 36858f7a5cSDaniel Baluta snd_compr_fragment_elapsed(sps->cstream); 37858f7a5cSDaniel Baluta } 38858f7a5cSDaniel Baluta 39858f7a5cSDaniel Baluta void snd_sof_compr_init_elapsed_work(struct work_struct *work) 40858f7a5cSDaniel Baluta { 41858f7a5cSDaniel Baluta INIT_WORK(work, snd_sof_compr_fragment_elapsed_work); 42858f7a5cSDaniel Baluta } 43858f7a5cSDaniel Baluta 44858f7a5cSDaniel Baluta /* 45858f7a5cSDaniel Baluta * sof compr fragment elapse, this could be called in irq thread context 46858f7a5cSDaniel Baluta */ 47858f7a5cSDaniel Baluta void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream) 48858f7a5cSDaniel Baluta { 49*7e4bfcf1SWeiguo Li struct snd_soc_pcm_runtime *rtd; 50*7e4bfcf1SWeiguo Li struct snd_compr_runtime *crtd; 51858f7a5cSDaniel Baluta struct snd_soc_component *component; 526324cf90SDaniel Baluta struct snd_compr_tstamp *tstamp; 53858f7a5cSDaniel Baluta struct snd_sof_pcm *spcm; 54858f7a5cSDaniel Baluta 55858f7a5cSDaniel Baluta if (!cstream) 56858f7a5cSDaniel Baluta return; 57858f7a5cSDaniel Baluta 58*7e4bfcf1SWeiguo Li rtd = cstream->private_data; 59*7e4bfcf1SWeiguo Li crtd = cstream->runtime; 606324cf90SDaniel Baluta tstamp = crtd->private_data; 61858f7a5cSDaniel Baluta component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME); 62858f7a5cSDaniel Baluta 63858f7a5cSDaniel Baluta spcm = snd_sof_find_spcm_dai(component, rtd); 64858f7a5cSDaniel Baluta if (!spcm) { 65858f7a5cSDaniel Baluta dev_err(component->dev, 66858f7a5cSDaniel Baluta "fragment elapsed called for unknown stream!\n"); 67858f7a5cSDaniel Baluta return; 68858f7a5cSDaniel Baluta } 69858f7a5cSDaniel Baluta 706324cf90SDaniel Baluta sof_set_transferred_bytes(tstamp, spcm->stream[cstream->direction].posn.host_posn, 716324cf90SDaniel Baluta crtd->buffer_size); 726324cf90SDaniel Baluta 73858f7a5cSDaniel Baluta /* use the same workqueue-based solution as for PCM, cf. snd_sof_pcm_elapsed */ 74858f7a5cSDaniel Baluta schedule_work(&spcm->stream[cstream->direction].period_elapsed_work); 75858f7a5cSDaniel Baluta } 766324cf90SDaniel Baluta 776324cf90SDaniel Baluta static int create_page_table(struct snd_soc_component *component, 786324cf90SDaniel Baluta struct snd_compr_stream *cstream, 796324cf90SDaniel Baluta unsigned char *dma_area, size_t size) 806324cf90SDaniel Baluta { 816324cf90SDaniel Baluta struct snd_dma_buffer *dmab = cstream->runtime->dma_buffer_p; 826324cf90SDaniel Baluta struct snd_soc_pcm_runtime *rtd = cstream->private_data; 836324cf90SDaniel Baluta int dir = cstream->direction; 846324cf90SDaniel Baluta struct snd_sof_pcm *spcm; 856324cf90SDaniel Baluta 866324cf90SDaniel Baluta spcm = snd_sof_find_spcm_dai(component, rtd); 876324cf90SDaniel Baluta if (!spcm) 886324cf90SDaniel Baluta return -EINVAL; 896324cf90SDaniel Baluta 906324cf90SDaniel Baluta return snd_sof_create_page_table(component->dev, dmab, 916324cf90SDaniel Baluta spcm->stream[dir].page_table.area, size); 926324cf90SDaniel Baluta } 936324cf90SDaniel Baluta 94e7c799e7SDaniel Baluta static int sof_compr_open(struct snd_soc_component *component, 956324cf90SDaniel Baluta struct snd_compr_stream *cstream) 966324cf90SDaniel Baluta { 976324cf90SDaniel Baluta struct snd_soc_pcm_runtime *rtd = cstream->private_data; 986324cf90SDaniel Baluta struct snd_compr_runtime *crtd = cstream->runtime; 996324cf90SDaniel Baluta struct snd_compr_tstamp *tstamp; 1006324cf90SDaniel Baluta struct snd_sof_pcm *spcm; 1016324cf90SDaniel Baluta int dir; 1026324cf90SDaniel Baluta 1036324cf90SDaniel Baluta tstamp = kzalloc(sizeof(*tstamp), GFP_KERNEL); 1046324cf90SDaniel Baluta if (!tstamp) 1056324cf90SDaniel Baluta return -ENOMEM; 1066324cf90SDaniel Baluta 1076324cf90SDaniel Baluta spcm = snd_sof_find_spcm_dai(component, rtd); 1086324cf90SDaniel Baluta if (!spcm) { 1096324cf90SDaniel Baluta kfree(tstamp); 1106324cf90SDaniel Baluta return -EINVAL; 1116324cf90SDaniel Baluta } 1126324cf90SDaniel Baluta 1136324cf90SDaniel Baluta dir = cstream->direction; 1146324cf90SDaniel Baluta 1156324cf90SDaniel Baluta if (spcm->stream[dir].cstream) { 1166324cf90SDaniel Baluta kfree(tstamp); 1176324cf90SDaniel Baluta return -EBUSY; 1186324cf90SDaniel Baluta } 1196324cf90SDaniel Baluta 1206324cf90SDaniel Baluta spcm->stream[dir].cstream = cstream; 1216324cf90SDaniel Baluta spcm->stream[dir].posn.host_posn = 0; 1226324cf90SDaniel Baluta spcm->stream[dir].posn.dai_posn = 0; 1236324cf90SDaniel Baluta spcm->prepared[dir] = false; 1246324cf90SDaniel Baluta 1256324cf90SDaniel Baluta crtd->private_data = tstamp; 1266324cf90SDaniel Baluta 1276324cf90SDaniel Baluta return 0; 1286324cf90SDaniel Baluta } 1296324cf90SDaniel Baluta 130e7c799e7SDaniel Baluta static int sof_compr_free(struct snd_soc_component *component, 1316324cf90SDaniel Baluta struct snd_compr_stream *cstream) 1326324cf90SDaniel Baluta { 1336324cf90SDaniel Baluta struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 1346324cf90SDaniel Baluta struct snd_compr_tstamp *tstamp = cstream->runtime->private_data; 1356324cf90SDaniel Baluta struct snd_soc_pcm_runtime *rtd = cstream->private_data; 1366324cf90SDaniel Baluta struct sof_ipc_stream stream; 1376324cf90SDaniel Baluta struct sof_ipc_reply reply; 1386324cf90SDaniel Baluta struct snd_sof_pcm *spcm; 1396324cf90SDaniel Baluta int ret = 0; 1406324cf90SDaniel Baluta 1416324cf90SDaniel Baluta spcm = snd_sof_find_spcm_dai(component, rtd); 1426324cf90SDaniel Baluta if (!spcm) 1436324cf90SDaniel Baluta return -EINVAL; 1446324cf90SDaniel Baluta 1456324cf90SDaniel Baluta stream.hdr.size = sizeof(stream); 1466324cf90SDaniel Baluta stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE; 1476324cf90SDaniel Baluta stream.comp_id = spcm->stream[cstream->direction].comp_id; 1486324cf90SDaniel Baluta 1496324cf90SDaniel Baluta if (spcm->prepared[cstream->direction]) { 1506324cf90SDaniel Baluta ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, 1516324cf90SDaniel Baluta &stream, sizeof(stream), 1526324cf90SDaniel Baluta &reply, sizeof(reply)); 1536324cf90SDaniel Baluta if (!ret) 1546324cf90SDaniel Baluta spcm->prepared[cstream->direction] = false; 1556324cf90SDaniel Baluta } 1566324cf90SDaniel Baluta 1576324cf90SDaniel Baluta cancel_work_sync(&spcm->stream[cstream->direction].period_elapsed_work); 1586324cf90SDaniel Baluta spcm->stream[cstream->direction].cstream = NULL; 1596324cf90SDaniel Baluta kfree(tstamp); 1606324cf90SDaniel Baluta 1616324cf90SDaniel Baluta return ret; 1626324cf90SDaniel Baluta } 1636324cf90SDaniel Baluta 164e7c799e7SDaniel Baluta static int sof_compr_set_params(struct snd_soc_component *component, 1656324cf90SDaniel Baluta struct snd_compr_stream *cstream, struct snd_compr_params *params) 1666324cf90SDaniel Baluta { 1676324cf90SDaniel Baluta struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 1686324cf90SDaniel Baluta struct snd_soc_pcm_runtime *rtd = cstream->private_data; 1696324cf90SDaniel Baluta struct snd_compr_runtime *crtd = cstream->runtime; 1706324cf90SDaniel Baluta struct sof_ipc_pcm_params_reply ipc_params_reply; 1716324cf90SDaniel Baluta struct snd_compr_tstamp *tstamp; 1726324cf90SDaniel Baluta struct sof_ipc_pcm_params pcm; 1736324cf90SDaniel Baluta struct snd_sof_pcm *spcm; 1746324cf90SDaniel Baluta int ret; 1756324cf90SDaniel Baluta 1766324cf90SDaniel Baluta tstamp = crtd->private_data; 1776324cf90SDaniel Baluta 1786324cf90SDaniel Baluta spcm = snd_sof_find_spcm_dai(component, rtd); 1796324cf90SDaniel Baluta 1806324cf90SDaniel Baluta if (!spcm) 1816324cf90SDaniel Baluta return -EINVAL; 1826324cf90SDaniel Baluta 1836324cf90SDaniel Baluta cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG; 1846324cf90SDaniel Baluta cstream->dma_buffer.dev.dev = sdev->dev; 1856324cf90SDaniel Baluta ret = snd_compr_malloc_pages(cstream, crtd->buffer_size); 1866324cf90SDaniel Baluta if (ret < 0) 1876324cf90SDaniel Baluta return ret; 1886324cf90SDaniel Baluta 1896324cf90SDaniel Baluta ret = create_page_table(component, cstream, crtd->dma_area, crtd->dma_bytes); 1906324cf90SDaniel Baluta if (ret < 0) 1916324cf90SDaniel Baluta return ret; 1926324cf90SDaniel Baluta 1936324cf90SDaniel Baluta memset(&pcm, 0, sizeof(pcm)); 1946324cf90SDaniel Baluta 1956324cf90SDaniel Baluta pcm.params.buffer.pages = PFN_UP(crtd->dma_bytes); 1966324cf90SDaniel Baluta pcm.hdr.size = sizeof(pcm); 1976324cf90SDaniel Baluta pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS; 1986324cf90SDaniel Baluta 1996324cf90SDaniel Baluta pcm.comp_id = spcm->stream[cstream->direction].comp_id; 2006324cf90SDaniel Baluta pcm.params.hdr.size = sizeof(pcm.params); 2016324cf90SDaniel Baluta pcm.params.buffer.phy_addr = spcm->stream[cstream->direction].page_table.addr; 2026324cf90SDaniel Baluta pcm.params.buffer.size = crtd->dma_bytes; 2036324cf90SDaniel Baluta pcm.params.direction = cstream->direction; 2046324cf90SDaniel Baluta pcm.params.channels = params->codec.ch_out; 2056324cf90SDaniel Baluta pcm.params.rate = params->codec.sample_rate; 2066324cf90SDaniel Baluta pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED; 2076324cf90SDaniel Baluta pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE; 2086324cf90SDaniel Baluta pcm.params.sample_container_bytes = 2096324cf90SDaniel Baluta snd_pcm_format_physical_width(SNDRV_PCM_FORMAT_S32) >> 3; 2106324cf90SDaniel Baluta pcm.params.host_period_bytes = params->buffer.fragment_size; 2116324cf90SDaniel Baluta 2126324cf90SDaniel Baluta ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm), 2136324cf90SDaniel Baluta &ipc_params_reply, sizeof(ipc_params_reply)); 2146324cf90SDaniel Baluta if (ret < 0) { 2156324cf90SDaniel Baluta dev_err(component->dev, "error ipc failed\n"); 2166324cf90SDaniel Baluta return ret; 2176324cf90SDaniel Baluta } 2186324cf90SDaniel Baluta 2196324cf90SDaniel Baluta tstamp->byte_offset = sdev->stream_box.offset + ipc_params_reply.posn_offset; 2206324cf90SDaniel Baluta tstamp->sampling_rate = params->codec.sample_rate; 2216324cf90SDaniel Baluta 2226324cf90SDaniel Baluta spcm->prepared[cstream->direction] = true; 2236324cf90SDaniel Baluta 2246324cf90SDaniel Baluta return 0; 2256324cf90SDaniel Baluta } 2266324cf90SDaniel Baluta 227e7c799e7SDaniel Baluta static int sof_compr_get_params(struct snd_soc_component *component, 2286324cf90SDaniel Baluta struct snd_compr_stream *cstream, struct snd_codec *params) 2296324cf90SDaniel Baluta { 2306324cf90SDaniel Baluta /* TODO: we don't query the supported codecs for now, if the 2316324cf90SDaniel Baluta * application asks for an unsupported codec the set_params() will fail. 2326324cf90SDaniel Baluta */ 2336324cf90SDaniel Baluta return 0; 2346324cf90SDaniel Baluta } 2356324cf90SDaniel Baluta 236e7c799e7SDaniel Baluta static int sof_compr_trigger(struct snd_soc_component *component, 2376324cf90SDaniel Baluta struct snd_compr_stream *cstream, int cmd) 2386324cf90SDaniel Baluta { 2396324cf90SDaniel Baluta struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 2406324cf90SDaniel Baluta struct snd_soc_pcm_runtime *rtd = cstream->private_data; 2416324cf90SDaniel Baluta struct sof_ipc_stream stream; 2426324cf90SDaniel Baluta struct sof_ipc_reply reply; 2436324cf90SDaniel Baluta struct snd_sof_pcm *spcm; 2446324cf90SDaniel Baluta 2456324cf90SDaniel Baluta spcm = snd_sof_find_spcm_dai(component, rtd); 2466324cf90SDaniel Baluta if (!spcm) 2476324cf90SDaniel Baluta return -EINVAL; 2486324cf90SDaniel Baluta 2496324cf90SDaniel Baluta stream.hdr.size = sizeof(stream); 2506324cf90SDaniel Baluta stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG; 2516324cf90SDaniel Baluta stream.comp_id = spcm->stream[cstream->direction].comp_id; 2526324cf90SDaniel Baluta 2536324cf90SDaniel Baluta switch (cmd) { 2546324cf90SDaniel Baluta case SNDRV_PCM_TRIGGER_START: 2556324cf90SDaniel Baluta stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START; 2566324cf90SDaniel Baluta break; 2576324cf90SDaniel Baluta case SNDRV_PCM_TRIGGER_STOP: 2586324cf90SDaniel Baluta stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP; 2596324cf90SDaniel Baluta break; 2606324cf90SDaniel Baluta case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 2616324cf90SDaniel Baluta stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE; 2626324cf90SDaniel Baluta break; 2636324cf90SDaniel Baluta case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 2646324cf90SDaniel Baluta stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE; 2656324cf90SDaniel Baluta break; 2666324cf90SDaniel Baluta default: 2676324cf90SDaniel Baluta dev_err(component->dev, "error: unhandled trigger cmd %d\n", cmd); 2686324cf90SDaniel Baluta break; 2696324cf90SDaniel Baluta } 2706324cf90SDaniel Baluta 2716324cf90SDaniel Baluta return sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, 2726324cf90SDaniel Baluta &stream, sizeof(stream), 2736324cf90SDaniel Baluta &reply, sizeof(reply)); 2746324cf90SDaniel Baluta } 2756324cf90SDaniel Baluta 276e7c799e7SDaniel Baluta static int sof_compr_copy(struct snd_soc_component *component, 2776324cf90SDaniel Baluta struct snd_compr_stream *cstream, 2786324cf90SDaniel Baluta char __user *buf, size_t count) 2796324cf90SDaniel Baluta { 2806324cf90SDaniel Baluta struct snd_compr_runtime *rtd = cstream->runtime; 2816324cf90SDaniel Baluta unsigned int offset, n; 2826324cf90SDaniel Baluta void *ptr; 2836324cf90SDaniel Baluta int ret; 2846324cf90SDaniel Baluta 2856324cf90SDaniel Baluta if (count > rtd->buffer_size) 2866324cf90SDaniel Baluta count = rtd->buffer_size; 2876324cf90SDaniel Baluta 2886324cf90SDaniel Baluta div_u64_rem(rtd->total_bytes_available, rtd->buffer_size, &offset); 2896324cf90SDaniel Baluta ptr = rtd->dma_area + offset; 2906324cf90SDaniel Baluta n = rtd->buffer_size - offset; 2916324cf90SDaniel Baluta 2926324cf90SDaniel Baluta if (count < n) { 2936324cf90SDaniel Baluta ret = copy_from_user(ptr, buf, count); 2946324cf90SDaniel Baluta } else { 2956324cf90SDaniel Baluta ret = copy_from_user(ptr, buf, n); 2966324cf90SDaniel Baluta ret += copy_from_user(rtd->dma_area, buf + n, count - n); 2976324cf90SDaniel Baluta } 2986324cf90SDaniel Baluta 2996324cf90SDaniel Baluta return count - ret; 3006324cf90SDaniel Baluta } 3016324cf90SDaniel Baluta 3026324cf90SDaniel Baluta static int sof_compr_pointer(struct snd_soc_component *component, 3036324cf90SDaniel Baluta struct snd_compr_stream *cstream, 3046324cf90SDaniel Baluta struct snd_compr_tstamp *tstamp) 3056324cf90SDaniel Baluta { 3066324cf90SDaniel Baluta struct snd_compr_tstamp *pstamp = cstream->runtime->private_data; 3076324cf90SDaniel Baluta 3086324cf90SDaniel Baluta tstamp->sampling_rate = pstamp->sampling_rate; 3096324cf90SDaniel Baluta tstamp->copied_total = pstamp->copied_total; 3106324cf90SDaniel Baluta 3116324cf90SDaniel Baluta return 0; 3126324cf90SDaniel Baluta } 3136324cf90SDaniel Baluta 3146324cf90SDaniel Baluta struct snd_compress_ops sof_compressed_ops = { 3156324cf90SDaniel Baluta .open = sof_compr_open, 3166324cf90SDaniel Baluta .free = sof_compr_free, 3176324cf90SDaniel Baluta .set_params = sof_compr_set_params, 3186324cf90SDaniel Baluta .get_params = sof_compr_get_params, 3196324cf90SDaniel Baluta .trigger = sof_compr_trigger, 3206324cf90SDaniel Baluta .pointer = sof_compr_pointer, 3216324cf90SDaniel Baluta .copy = sof_compr_copy, 3226324cf90SDaniel Baluta }; 3236324cf90SDaniel Baluta EXPORT_SYMBOL(sof_compressed_ops); 324