1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause) 2 // 3 // Copyright 2021 NXP 4 // 5 // Author: Daniel Baluta <daniel.baluta@nxp.com> 6 7 #include <sound/soc.h> 8 #include <sound/sof.h> 9 #include <sound/compress_driver.h> 10 #include "sof-audio.h" 11 #include "sof-priv.h" 12 13 static void sof_set_transferred_bytes(struct snd_compr_tstamp *tstamp, 14 u64 host_pos, u64 buffer_size) 15 { 16 u64 prev_pos; 17 unsigned int copied; 18 19 div64_u64_rem(tstamp->copied_total, buffer_size, &prev_pos); 20 21 if (host_pos < prev_pos) 22 copied = (buffer_size - prev_pos) + host_pos; 23 else 24 copied = host_pos - prev_pos; 25 26 tstamp->copied_total += copied; 27 } 28 29 static void snd_sof_compr_fragment_elapsed_work(struct work_struct *work) 30 { 31 struct snd_sof_pcm_stream *sps = 32 container_of(work, struct snd_sof_pcm_stream, 33 period_elapsed_work); 34 35 snd_compr_fragment_elapsed(sps->cstream); 36 } 37 38 void snd_sof_compr_init_elapsed_work(struct work_struct *work) 39 { 40 INIT_WORK(work, snd_sof_compr_fragment_elapsed_work); 41 } 42 43 /* 44 * sof compr fragment elapse, this could be called in irq thread context 45 */ 46 void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream) 47 { 48 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 49 struct snd_compr_runtime *crtd = cstream->runtime; 50 struct snd_soc_component *component; 51 struct snd_compr_tstamp *tstamp; 52 struct snd_sof_pcm *spcm; 53 54 if (!cstream) 55 return; 56 57 tstamp = crtd->private_data; 58 component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME); 59 60 spcm = snd_sof_find_spcm_dai(component, rtd); 61 if (!spcm) { 62 dev_err(component->dev, 63 "fragment elapsed called for unknown stream!\n"); 64 return; 65 } 66 67 sof_set_transferred_bytes(tstamp, spcm->stream[cstream->direction].posn.host_posn, 68 crtd->buffer_size); 69 70 /* use the same workqueue-based solution as for PCM, cf. snd_sof_pcm_elapsed */ 71 schedule_work(&spcm->stream[cstream->direction].period_elapsed_work); 72 } 73 74 static int create_page_table(struct snd_soc_component *component, 75 struct snd_compr_stream *cstream, 76 unsigned char *dma_area, size_t size) 77 { 78 struct snd_dma_buffer *dmab = cstream->runtime->dma_buffer_p; 79 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 80 int dir = cstream->direction; 81 struct snd_sof_pcm *spcm; 82 83 spcm = snd_sof_find_spcm_dai(component, rtd); 84 if (!spcm) 85 return -EINVAL; 86 87 return snd_sof_create_page_table(component->dev, dmab, 88 spcm->stream[dir].page_table.area, size); 89 } 90 91 int sof_compr_open(struct snd_soc_component *component, 92 struct snd_compr_stream *cstream) 93 { 94 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 95 struct snd_compr_runtime *crtd = cstream->runtime; 96 struct snd_compr_tstamp *tstamp; 97 struct snd_sof_pcm *spcm; 98 int dir; 99 100 tstamp = kzalloc(sizeof(*tstamp), GFP_KERNEL); 101 if (!tstamp) 102 return -ENOMEM; 103 104 spcm = snd_sof_find_spcm_dai(component, rtd); 105 if (!spcm) { 106 kfree(tstamp); 107 return -EINVAL; 108 } 109 110 dir = cstream->direction; 111 112 if (spcm->stream[dir].cstream) { 113 kfree(tstamp); 114 return -EBUSY; 115 } 116 117 spcm->stream[dir].cstream = cstream; 118 spcm->stream[dir].posn.host_posn = 0; 119 spcm->stream[dir].posn.dai_posn = 0; 120 spcm->prepared[dir] = false; 121 122 crtd->private_data = tstamp; 123 124 return 0; 125 } 126 127 int sof_compr_free(struct snd_soc_component *component, 128 struct snd_compr_stream *cstream) 129 { 130 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 131 struct snd_compr_tstamp *tstamp = cstream->runtime->private_data; 132 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 133 struct sof_ipc_stream stream; 134 struct sof_ipc_reply reply; 135 struct snd_sof_pcm *spcm; 136 int ret = 0; 137 138 spcm = snd_sof_find_spcm_dai(component, rtd); 139 if (!spcm) 140 return -EINVAL; 141 142 stream.hdr.size = sizeof(stream); 143 stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE; 144 stream.comp_id = spcm->stream[cstream->direction].comp_id; 145 146 if (spcm->prepared[cstream->direction]) { 147 ret = sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, 148 &stream, sizeof(stream), 149 &reply, sizeof(reply)); 150 if (!ret) 151 spcm->prepared[cstream->direction] = false; 152 } 153 154 cancel_work_sync(&spcm->stream[cstream->direction].period_elapsed_work); 155 spcm->stream[cstream->direction].cstream = NULL; 156 kfree(tstamp); 157 158 return ret; 159 } 160 161 int sof_compr_set_params(struct snd_soc_component *component, 162 struct snd_compr_stream *cstream, struct snd_compr_params *params) 163 { 164 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 165 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 166 struct snd_compr_runtime *crtd = cstream->runtime; 167 struct sof_ipc_pcm_params_reply ipc_params_reply; 168 struct snd_compr_tstamp *tstamp; 169 struct sof_ipc_pcm_params pcm; 170 struct snd_sof_pcm *spcm; 171 int ret; 172 173 tstamp = crtd->private_data; 174 175 spcm = snd_sof_find_spcm_dai(component, rtd); 176 177 if (!spcm) 178 return -EINVAL; 179 180 cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG; 181 cstream->dma_buffer.dev.dev = sdev->dev; 182 ret = snd_compr_malloc_pages(cstream, crtd->buffer_size); 183 if (ret < 0) 184 return ret; 185 186 ret = create_page_table(component, cstream, crtd->dma_area, crtd->dma_bytes); 187 if (ret < 0) 188 return ret; 189 190 memset(&pcm, 0, sizeof(pcm)); 191 192 pcm.params.buffer.pages = PFN_UP(crtd->dma_bytes); 193 pcm.hdr.size = sizeof(pcm); 194 pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS; 195 196 pcm.comp_id = spcm->stream[cstream->direction].comp_id; 197 pcm.params.hdr.size = sizeof(pcm.params); 198 pcm.params.buffer.phy_addr = spcm->stream[cstream->direction].page_table.addr; 199 pcm.params.buffer.size = crtd->dma_bytes; 200 pcm.params.direction = cstream->direction; 201 pcm.params.channels = params->codec.ch_out; 202 pcm.params.rate = params->codec.sample_rate; 203 pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED; 204 pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE; 205 pcm.params.sample_container_bytes = 206 snd_pcm_format_physical_width(SNDRV_PCM_FORMAT_S32) >> 3; 207 pcm.params.host_period_bytes = params->buffer.fragment_size; 208 209 ret = sof_ipc_tx_message(sdev->ipc, pcm.hdr.cmd, &pcm, sizeof(pcm), 210 &ipc_params_reply, sizeof(ipc_params_reply)); 211 if (ret < 0) { 212 dev_err(component->dev, "error ipc failed\n"); 213 return ret; 214 } 215 216 tstamp->byte_offset = sdev->stream_box.offset + ipc_params_reply.posn_offset; 217 tstamp->sampling_rate = params->codec.sample_rate; 218 219 spcm->prepared[cstream->direction] = true; 220 221 return 0; 222 } 223 224 int sof_compr_get_params(struct snd_soc_component *component, 225 struct snd_compr_stream *cstream, struct snd_codec *params) 226 { 227 /* TODO: we don't query the supported codecs for now, if the 228 * application asks for an unsupported codec the set_params() will fail. 229 */ 230 return 0; 231 } 232 233 int sof_compr_trigger(struct snd_soc_component *component, 234 struct snd_compr_stream *cstream, int cmd) 235 { 236 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component); 237 struct snd_soc_pcm_runtime *rtd = cstream->private_data; 238 struct sof_ipc_stream stream; 239 struct sof_ipc_reply reply; 240 struct snd_sof_pcm *spcm; 241 242 spcm = snd_sof_find_spcm_dai(component, rtd); 243 if (!spcm) 244 return -EINVAL; 245 246 stream.hdr.size = sizeof(stream); 247 stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG; 248 stream.comp_id = spcm->stream[cstream->direction].comp_id; 249 250 switch (cmd) { 251 case SNDRV_PCM_TRIGGER_START: 252 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START; 253 break; 254 case SNDRV_PCM_TRIGGER_STOP: 255 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP; 256 break; 257 case SNDRV_PCM_TRIGGER_PAUSE_PUSH: 258 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE; 259 break; 260 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE: 261 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE; 262 break; 263 default: 264 dev_err(component->dev, "error: unhandled trigger cmd %d\n", cmd); 265 break; 266 } 267 268 return sof_ipc_tx_message(sdev->ipc, stream.hdr.cmd, 269 &stream, sizeof(stream), 270 &reply, sizeof(reply)); 271 } 272 273 int sof_compr_copy(struct snd_soc_component *component, 274 struct snd_compr_stream *cstream, 275 char __user *buf, size_t count) 276 { 277 struct snd_compr_runtime *rtd = cstream->runtime; 278 unsigned int offset, n; 279 void *ptr; 280 int ret; 281 282 if (count > rtd->buffer_size) 283 count = rtd->buffer_size; 284 285 div_u64_rem(rtd->total_bytes_available, rtd->buffer_size, &offset); 286 ptr = rtd->dma_area + offset; 287 n = rtd->buffer_size - offset; 288 289 if (count < n) { 290 ret = copy_from_user(ptr, buf, count); 291 } else { 292 ret = copy_from_user(ptr, buf, n); 293 ret += copy_from_user(rtd->dma_area, buf + n, count - n); 294 } 295 296 return count - ret; 297 } 298 299 static int sof_compr_pointer(struct snd_soc_component *component, 300 struct snd_compr_stream *cstream, 301 struct snd_compr_tstamp *tstamp) 302 { 303 struct snd_compr_tstamp *pstamp = cstream->runtime->private_data; 304 305 tstamp->sampling_rate = pstamp->sampling_rate; 306 tstamp->copied_total = pstamp->copied_total; 307 308 return 0; 309 } 310 311 struct snd_compress_ops sof_compressed_ops = { 312 .open = sof_compr_open, 313 .free = sof_compr_free, 314 .set_params = sof_compr_set_params, 315 .get_params = sof_compr_get_params, 316 .trigger = sof_compr_trigger, 317 .pointer = sof_compr_pointer, 318 .copy = sof_compr_copy, 319 }; 320 EXPORT_SYMBOL(sof_compressed_ops); 321