compress.c 11 KB

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