0001
0002
0003
0004
0005
0006
0007 #include <sound/soc.h>
0008 #include <sound/sof.h>
0009 #include <sound/compress_driver.h>
0010 #include "sof-audio.h"
0011 #include "sof-priv.h"
0012 #include "sof-utils.h"
0013
0014 static void sof_set_transferred_bytes(struct snd_compr_tstamp *tstamp,
0015 u64 host_pos, u64 buffer_size)
0016 {
0017 u64 prev_pos;
0018 unsigned int copied;
0019
0020 div64_u64_rem(tstamp->copied_total, buffer_size, &prev_pos);
0021
0022 if (host_pos < prev_pos)
0023 copied = (buffer_size - prev_pos) + host_pos;
0024 else
0025 copied = host_pos - prev_pos;
0026
0027 tstamp->copied_total += copied;
0028 }
0029
0030 static void snd_sof_compr_fragment_elapsed_work(struct work_struct *work)
0031 {
0032 struct snd_sof_pcm_stream *sps =
0033 container_of(work, struct snd_sof_pcm_stream,
0034 period_elapsed_work);
0035
0036 snd_compr_fragment_elapsed(sps->cstream);
0037 }
0038
0039 void snd_sof_compr_init_elapsed_work(struct work_struct *work)
0040 {
0041 INIT_WORK(work, snd_sof_compr_fragment_elapsed_work);
0042 }
0043
0044
0045
0046
0047 void snd_sof_compr_fragment_elapsed(struct snd_compr_stream *cstream)
0048 {
0049 struct snd_soc_pcm_runtime *rtd;
0050 struct snd_compr_runtime *crtd;
0051 struct snd_soc_component *component;
0052 struct snd_compr_tstamp *tstamp;
0053 struct snd_sof_pcm *spcm;
0054
0055 if (!cstream)
0056 return;
0057
0058 rtd = cstream->private_data;
0059 crtd = cstream->runtime;
0060 tstamp = crtd->private_data;
0061 component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
0062
0063 spcm = snd_sof_find_spcm_dai(component, rtd);
0064 if (!spcm) {
0065 dev_err(component->dev,
0066 "fragment elapsed called for unknown stream!\n");
0067 return;
0068 }
0069
0070 sof_set_transferred_bytes(tstamp, spcm->stream[cstream->direction].posn.host_posn,
0071 crtd->buffer_size);
0072
0073
0074 schedule_work(&spcm->stream[cstream->direction].period_elapsed_work);
0075 }
0076
0077 static int create_page_table(struct snd_soc_component *component,
0078 struct snd_compr_stream *cstream,
0079 unsigned char *dma_area, size_t size)
0080 {
0081 struct snd_dma_buffer *dmab = cstream->runtime->dma_buffer_p;
0082 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0083 int dir = cstream->direction;
0084 struct snd_sof_pcm *spcm;
0085
0086 spcm = snd_sof_find_spcm_dai(component, rtd);
0087 if (!spcm)
0088 return -EINVAL;
0089
0090 return snd_sof_create_page_table(component->dev, dmab,
0091 spcm->stream[dir].page_table.area, size);
0092 }
0093
0094 static int sof_compr_open(struct snd_soc_component *component,
0095 struct snd_compr_stream *cstream)
0096 {
0097 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0098 struct snd_compr_runtime *crtd = cstream->runtime;
0099 struct snd_compr_tstamp *tstamp;
0100 struct snd_sof_pcm *spcm;
0101 int dir;
0102
0103 tstamp = kzalloc(sizeof(*tstamp), GFP_KERNEL);
0104 if (!tstamp)
0105 return -ENOMEM;
0106
0107 spcm = snd_sof_find_spcm_dai(component, rtd);
0108 if (!spcm) {
0109 kfree(tstamp);
0110 return -EINVAL;
0111 }
0112
0113 dir = cstream->direction;
0114
0115 if (spcm->stream[dir].cstream) {
0116 kfree(tstamp);
0117 return -EBUSY;
0118 }
0119
0120 spcm->stream[dir].cstream = cstream;
0121 spcm->stream[dir].posn.host_posn = 0;
0122 spcm->stream[dir].posn.dai_posn = 0;
0123 spcm->prepared[dir] = false;
0124
0125 crtd->private_data = tstamp;
0126
0127 return 0;
0128 }
0129
0130 static int sof_compr_free(struct snd_soc_component *component,
0131 struct snd_compr_stream *cstream)
0132 {
0133 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
0134 struct snd_compr_tstamp *tstamp = cstream->runtime->private_data;
0135 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0136 struct sof_ipc_stream stream;
0137 struct sof_ipc_reply reply;
0138 struct snd_sof_pcm *spcm;
0139 int ret = 0;
0140
0141 spcm = snd_sof_find_spcm_dai(component, rtd);
0142 if (!spcm)
0143 return -EINVAL;
0144
0145 stream.hdr.size = sizeof(stream);
0146 stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
0147 stream.comp_id = spcm->stream[cstream->direction].comp_id;
0148
0149 if (spcm->prepared[cstream->direction]) {
0150 ret = sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream),
0151 &reply, sizeof(reply));
0152 if (!ret)
0153 spcm->prepared[cstream->direction] = false;
0154 }
0155
0156 cancel_work_sync(&spcm->stream[cstream->direction].period_elapsed_work);
0157 spcm->stream[cstream->direction].cstream = NULL;
0158 kfree(tstamp);
0159
0160 return ret;
0161 }
0162
0163 static int sof_compr_set_params(struct snd_soc_component *component,
0164 struct snd_compr_stream *cstream, struct snd_compr_params *params)
0165 {
0166 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
0167 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0168 struct snd_compr_runtime *crtd = cstream->runtime;
0169 struct sof_ipc_pcm_params_reply ipc_params_reply;
0170 struct sof_ipc_fw_ready *ready = &sdev->fw_ready;
0171 struct sof_ipc_fw_version *v = &ready->version;
0172 struct snd_compr_tstamp *tstamp;
0173 struct sof_ipc_pcm_params *pcm;
0174 struct snd_sof_pcm *spcm;
0175 size_t ext_data_size;
0176 int ret;
0177
0178 if (v->abi_version < SOF_ABI_VER(3, 22, 0)) {
0179 dev_err(component->dev,
0180 "Compress params not supported with FW ABI version %d:%d:%d\n",
0181 SOF_ABI_VERSION_MAJOR(v->abi_version),
0182 SOF_ABI_VERSION_MINOR(v->abi_version),
0183 SOF_ABI_VERSION_PATCH(v->abi_version));
0184 return -EINVAL;
0185 }
0186
0187 tstamp = crtd->private_data;
0188
0189 spcm = snd_sof_find_spcm_dai(component, rtd);
0190
0191 if (!spcm)
0192 return -EINVAL;
0193
0194 ext_data_size = sizeof(params->codec);
0195
0196 if (sizeof(*pcm) + ext_data_size > sdev->ipc->max_payload_size)
0197 return -EINVAL;
0198
0199 pcm = kzalloc(sizeof(*pcm) + ext_data_size, GFP_KERNEL);
0200 if (!pcm)
0201 return -ENOMEM;
0202
0203 cstream->dma_buffer.dev.type = SNDRV_DMA_TYPE_DEV_SG;
0204 cstream->dma_buffer.dev.dev = sdev->dev;
0205 ret = snd_compr_malloc_pages(cstream, crtd->buffer_size);
0206 if (ret < 0)
0207 goto out;
0208
0209 ret = create_page_table(component, cstream, crtd->dma_area, crtd->dma_bytes);
0210 if (ret < 0)
0211 goto out;
0212
0213 pcm->params.buffer.pages = PFN_UP(crtd->dma_bytes);
0214 pcm->hdr.size = sizeof(*pcm) + ext_data_size;
0215 pcm->hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
0216
0217 pcm->comp_id = spcm->stream[cstream->direction].comp_id;
0218 pcm->params.hdr.size = sizeof(pcm->params) + ext_data_size;
0219 pcm->params.buffer.phy_addr = spcm->stream[cstream->direction].page_table.addr;
0220 pcm->params.buffer.size = crtd->dma_bytes;
0221 pcm->params.direction = cstream->direction;
0222 pcm->params.channels = params->codec.ch_out;
0223 pcm->params.rate = params->codec.sample_rate;
0224 pcm->params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
0225 pcm->params.frame_fmt = SOF_IPC_FRAME_S32_LE;
0226 pcm->params.sample_container_bytes =
0227 snd_pcm_format_physical_width(SNDRV_PCM_FORMAT_S32) >> 3;
0228 pcm->params.host_period_bytes = params->buffer.fragment_size;
0229 pcm->params.ext_data_length = ext_data_size;
0230
0231 memcpy((u8 *)pcm->params.ext_data, ¶ms->codec, ext_data_size);
0232
0233 ret = sof_ipc_tx_message(sdev->ipc, pcm, sizeof(*pcm) + ext_data_size,
0234 &ipc_params_reply, sizeof(ipc_params_reply));
0235 if (ret < 0) {
0236 dev_err(component->dev, "error ipc failed\n");
0237 goto out;
0238 }
0239
0240 tstamp->byte_offset = sdev->stream_box.offset + ipc_params_reply.posn_offset;
0241 tstamp->sampling_rate = params->codec.sample_rate;
0242
0243 spcm->prepared[cstream->direction] = true;
0244
0245 out:
0246 kfree(pcm);
0247
0248 return ret;
0249 }
0250
0251 static int sof_compr_get_params(struct snd_soc_component *component,
0252 struct snd_compr_stream *cstream, struct snd_codec *params)
0253 {
0254
0255
0256
0257 return 0;
0258 }
0259
0260 static int sof_compr_trigger(struct snd_soc_component *component,
0261 struct snd_compr_stream *cstream, int cmd)
0262 {
0263 struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
0264 struct snd_soc_pcm_runtime *rtd = cstream->private_data;
0265 struct sof_ipc_stream stream;
0266 struct sof_ipc_reply reply;
0267 struct snd_sof_pcm *spcm;
0268
0269 spcm = snd_sof_find_spcm_dai(component, rtd);
0270 if (!spcm)
0271 return -EINVAL;
0272
0273 stream.hdr.size = sizeof(stream);
0274 stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
0275 stream.comp_id = spcm->stream[cstream->direction].comp_id;
0276
0277 switch (cmd) {
0278 case SNDRV_PCM_TRIGGER_START:
0279 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
0280 break;
0281 case SNDRV_PCM_TRIGGER_STOP:
0282 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
0283 break;
0284 case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
0285 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
0286 break;
0287 case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
0288 stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
0289 break;
0290 default:
0291 dev_err(component->dev, "error: unhandled trigger cmd %d\n", cmd);
0292 break;
0293 }
0294
0295 return sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream),
0296 &reply, sizeof(reply));
0297 }
0298
0299 static int sof_compr_copy(struct snd_soc_component *component,
0300 struct snd_compr_stream *cstream,
0301 char __user *buf, size_t count)
0302 {
0303 struct snd_compr_runtime *rtd = cstream->runtime;
0304 unsigned int offset, n;
0305 void *ptr;
0306 int ret;
0307
0308 if (count > rtd->buffer_size)
0309 count = rtd->buffer_size;
0310
0311 div_u64_rem(rtd->total_bytes_available, rtd->buffer_size, &offset);
0312 ptr = rtd->dma_area + offset;
0313 n = rtd->buffer_size - offset;
0314
0315 if (count < n) {
0316 ret = copy_from_user(ptr, buf, count);
0317 } else {
0318 ret = copy_from_user(ptr, buf, n);
0319 ret += copy_from_user(rtd->dma_area, buf + n, count - n);
0320 }
0321
0322 return count - ret;
0323 }
0324
0325 static int sof_compr_pointer(struct snd_soc_component *component,
0326 struct snd_compr_stream *cstream,
0327 struct snd_compr_tstamp *tstamp)
0328 {
0329 struct snd_compr_tstamp *pstamp = cstream->runtime->private_data;
0330
0331 tstamp->sampling_rate = pstamp->sampling_rate;
0332 tstamp->copied_total = pstamp->copied_total;
0333
0334 return 0;
0335 }
0336
0337 struct snd_compress_ops sof_compressed_ops = {
0338 .open = sof_compr_open,
0339 .free = sof_compr_free,
0340 .set_params = sof_compr_set_params,
0341 .get_params = sof_compr_get_params,
0342 .trigger = sof_compr_trigger,
0343 .pointer = sof_compr_pointer,
0344 .copy = sof_compr_copy,
0345 };
0346 EXPORT_SYMBOL(sof_compressed_ops);