ipc3-pcm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
  2. //
  3. // This file is provided under a dual BSD/GPLv2 license. When using or
  4. // redistributing this file, you may do so under either license.
  5. //
  6. // Copyright(c) 2021 Intel Corporation. All rights reserved.
  7. //
  8. //
  9. #include <sound/pcm_params.h>
  10. #include "ipc3-priv.h"
  11. #include "ops.h"
  12. #include "sof-priv.h"
  13. #include "sof-audio.h"
  14. static int sof_ipc3_pcm_hw_free(struct snd_soc_component *component,
  15. struct snd_pcm_substream *substream)
  16. {
  17. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  18. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  19. struct sof_ipc_stream stream;
  20. struct sof_ipc_reply reply;
  21. struct snd_sof_pcm *spcm;
  22. spcm = snd_sof_find_spcm_dai(component, rtd);
  23. if (!spcm)
  24. return -EINVAL;
  25. if (!spcm->prepared[substream->stream])
  26. return 0;
  27. stream.hdr.size = sizeof(stream);
  28. stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_FREE;
  29. stream.comp_id = spcm->stream[substream->stream].comp_id;
  30. /* send IPC to the DSP */
  31. return sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), &reply, sizeof(reply));
  32. }
  33. static int sof_ipc3_pcm_hw_params(struct snd_soc_component *component,
  34. struct snd_pcm_substream *substream,
  35. struct snd_pcm_hw_params *params,
  36. struct snd_sof_platform_stream_params *platform_params)
  37. {
  38. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  39. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  40. struct sof_ipc_fw_version *v = &sdev->fw_ready.version;
  41. struct snd_pcm_runtime *runtime = substream->runtime;
  42. struct sof_ipc_pcm_params_reply ipc_params_reply;
  43. struct sof_ipc_pcm_params pcm;
  44. struct snd_sof_pcm *spcm;
  45. int ret;
  46. spcm = snd_sof_find_spcm_dai(component, rtd);
  47. if (!spcm)
  48. return -EINVAL;
  49. memset(&pcm, 0, sizeof(pcm));
  50. /* number of pages should be rounded up */
  51. pcm.params.buffer.pages = PFN_UP(runtime->dma_bytes);
  52. /* set IPC PCM parameters */
  53. pcm.hdr.size = sizeof(pcm);
  54. pcm.hdr.cmd = SOF_IPC_GLB_STREAM_MSG | SOF_IPC_STREAM_PCM_PARAMS;
  55. pcm.comp_id = spcm->stream[substream->stream].comp_id;
  56. pcm.params.hdr.size = sizeof(pcm.params);
  57. pcm.params.buffer.phy_addr = spcm->stream[substream->stream].page_table.addr;
  58. pcm.params.buffer.size = runtime->dma_bytes;
  59. pcm.params.direction = substream->stream;
  60. pcm.params.sample_valid_bytes = params_width(params) >> 3;
  61. pcm.params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
  62. pcm.params.rate = params_rate(params);
  63. pcm.params.channels = params_channels(params);
  64. pcm.params.host_period_bytes = params_period_bytes(params);
  65. /* container size */
  66. ret = snd_pcm_format_physical_width(params_format(params));
  67. if (ret < 0)
  68. return ret;
  69. pcm.params.sample_container_bytes = ret >> 3;
  70. /* format */
  71. switch (params_format(params)) {
  72. case SNDRV_PCM_FORMAT_S16:
  73. pcm.params.frame_fmt = SOF_IPC_FRAME_S16_LE;
  74. break;
  75. case SNDRV_PCM_FORMAT_S24:
  76. pcm.params.frame_fmt = SOF_IPC_FRAME_S24_4LE;
  77. break;
  78. case SNDRV_PCM_FORMAT_S32:
  79. pcm.params.frame_fmt = SOF_IPC_FRAME_S32_LE;
  80. break;
  81. case SNDRV_PCM_FORMAT_FLOAT:
  82. pcm.params.frame_fmt = SOF_IPC_FRAME_FLOAT;
  83. break;
  84. default:
  85. return -EINVAL;
  86. }
  87. /* Update the IPC message with information from the platform */
  88. pcm.params.stream_tag = platform_params->stream_tag;
  89. if (platform_params->use_phy_address)
  90. pcm.params.buffer.phy_addr = platform_params->phy_addr;
  91. if (platform_params->no_ipc_position) {
  92. /* For older ABIs set host_period_bytes to zero to inform
  93. * FW we don't want position updates. Newer versions use
  94. * no_stream_position for this purpose.
  95. */
  96. if (v->abi_version < SOF_ABI_VER(3, 10, 0))
  97. pcm.params.host_period_bytes = 0;
  98. else
  99. pcm.params.no_stream_position = 1;
  100. }
  101. if (platform_params->cont_update_posn)
  102. pcm.params.cont_update_posn = 1;
  103. dev_dbg(component->dev, "stream_tag %d", pcm.params.stream_tag);
  104. /* send hw_params IPC to the DSP */
  105. ret = sof_ipc_tx_message(sdev->ipc, &pcm, sizeof(pcm),
  106. &ipc_params_reply, sizeof(ipc_params_reply));
  107. if (ret < 0) {
  108. dev_err(component->dev, "HW params ipc failed for stream %d\n",
  109. pcm.params.stream_tag);
  110. return ret;
  111. }
  112. ret = snd_sof_set_stream_data_offset(sdev, substream, ipc_params_reply.posn_offset);
  113. if (ret < 0) {
  114. dev_err(component->dev, "%s: invalid stream data offset for PCM %d\n",
  115. __func__, spcm->pcm.pcm_id);
  116. return ret;
  117. }
  118. return ret;
  119. }
  120. static int sof_ipc3_pcm_trigger(struct snd_soc_component *component,
  121. struct snd_pcm_substream *substream, int cmd)
  122. {
  123. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  124. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  125. struct sof_ipc_stream stream;
  126. struct sof_ipc_reply reply;
  127. struct snd_sof_pcm *spcm;
  128. spcm = snd_sof_find_spcm_dai(component, rtd);
  129. if (!spcm)
  130. return -EINVAL;
  131. stream.hdr.size = sizeof(stream);
  132. stream.hdr.cmd = SOF_IPC_GLB_STREAM_MSG;
  133. stream.comp_id = spcm->stream[substream->stream].comp_id;
  134. switch (cmd) {
  135. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  136. stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_PAUSE;
  137. break;
  138. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  139. stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_RELEASE;
  140. break;
  141. case SNDRV_PCM_TRIGGER_START:
  142. stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_START;
  143. break;
  144. case SNDRV_PCM_TRIGGER_SUSPEND:
  145. fallthrough;
  146. case SNDRV_PCM_TRIGGER_STOP:
  147. stream.hdr.cmd |= SOF_IPC_STREAM_TRIG_STOP;
  148. break;
  149. default:
  150. dev_err(component->dev, "Unhandled trigger cmd %d\n", cmd);
  151. return -EINVAL;
  152. }
  153. /* send IPC to the DSP */
  154. return sof_ipc_tx_message(sdev->ipc, &stream, sizeof(stream), &reply, sizeof(reply));
  155. }
  156. static void ssp_dai_config_pcm_params_match(struct snd_sof_dev *sdev, const char *link_name,
  157. struct snd_pcm_hw_params *params)
  158. {
  159. struct sof_ipc_dai_config *config;
  160. struct snd_sof_dai *dai;
  161. int i;
  162. /*
  163. * Search for all matching DAIs as we can have both playback and capture DAI
  164. * associated with the same link.
  165. */
  166. list_for_each_entry(dai, &sdev->dai_list, list) {
  167. if (!dai->name || strcmp(link_name, dai->name))
  168. continue;
  169. for (i = 0; i < dai->number_configs; i++) {
  170. struct sof_dai_private_data *private = dai->private;
  171. config = &private->dai_config[i];
  172. if (config->ssp.fsync_rate == params_rate(params)) {
  173. dev_dbg(sdev->dev, "DAI config %d matches pcm hw params\n", i);
  174. dai->current_config = i;
  175. break;
  176. }
  177. }
  178. }
  179. }
  180. static int sof_ipc3_pcm_dai_link_fixup(struct snd_soc_pcm_runtime *rtd,
  181. struct snd_pcm_hw_params *params)
  182. {
  183. struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, SOF_AUDIO_PCM_DRV_NAME);
  184. struct snd_interval *channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS);
  185. struct snd_sof_dai *dai = snd_sof_find_dai(component, (char *)rtd->dai_link->name);
  186. struct snd_interval *rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
  187. struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
  188. struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(component);
  189. struct sof_dai_private_data *private;
  190. struct snd_soc_dpcm *dpcm;
  191. if (!dai) {
  192. dev_err(component->dev, "%s: No DAI found with name %s\n", __func__,
  193. rtd->dai_link->name);
  194. return -EINVAL;
  195. }
  196. private = dai->private;
  197. if (!private) {
  198. dev_err(component->dev, "%s: No private data found for DAI %s\n", __func__,
  199. rtd->dai_link->name);
  200. return -EINVAL;
  201. }
  202. /* read format from topology */
  203. snd_mask_none(fmt);
  204. switch (private->comp_dai->config.frame_fmt) {
  205. case SOF_IPC_FRAME_S16_LE:
  206. snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
  207. break;
  208. case SOF_IPC_FRAME_S24_4LE:
  209. snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE);
  210. break;
  211. case SOF_IPC_FRAME_S32_LE:
  212. snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S32_LE);
  213. break;
  214. default:
  215. dev_err(component->dev, "No available DAI format!\n");
  216. return -EINVAL;
  217. }
  218. /* read rate and channels from topology */
  219. switch (private->dai_config->type) {
  220. case SOF_DAI_INTEL_SSP:
  221. /* search for config to pcm params match, if not found use default */
  222. ssp_dai_config_pcm_params_match(sdev, (char *)rtd->dai_link->name, params);
  223. rate->min = private->dai_config[dai->current_config].ssp.fsync_rate;
  224. rate->max = private->dai_config[dai->current_config].ssp.fsync_rate;
  225. channels->min = private->dai_config[dai->current_config].ssp.tdm_slots;
  226. channels->max = private->dai_config[dai->current_config].ssp.tdm_slots;
  227. dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
  228. dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
  229. channels->min, channels->max);
  230. break;
  231. case SOF_DAI_INTEL_DMIC:
  232. /* DMIC only supports 16 or 32 bit formats */
  233. if (private->comp_dai->config.frame_fmt == SOF_IPC_FRAME_S24_4LE) {
  234. dev_err(component->dev, "Invalid fmt %d for DAI type %d\n",
  235. private->comp_dai->config.frame_fmt,
  236. private->dai_config->type);
  237. }
  238. break;
  239. case SOF_DAI_INTEL_HDA:
  240. /*
  241. * HDAudio does not follow the default trigger
  242. * sequence due to firmware implementation
  243. */
  244. for_each_dpcm_fe(rtd, SNDRV_PCM_STREAM_PLAYBACK, dpcm) {
  245. struct snd_soc_pcm_runtime *fe = dpcm->fe;
  246. fe->dai_link->trigger[SNDRV_PCM_STREAM_PLAYBACK] =
  247. SND_SOC_DPCM_TRIGGER_POST;
  248. }
  249. break;
  250. case SOF_DAI_INTEL_ALH:
  251. /*
  252. * Dai could run with different channel count compared with
  253. * front end, so get dai channel count from topology
  254. */
  255. channels->min = private->dai_config->alh.channels;
  256. channels->max = private->dai_config->alh.channels;
  257. break;
  258. case SOF_DAI_IMX_ESAI:
  259. rate->min = private->dai_config->esai.fsync_rate;
  260. rate->max = private->dai_config->esai.fsync_rate;
  261. channels->min = private->dai_config->esai.tdm_slots;
  262. channels->max = private->dai_config->esai.tdm_slots;
  263. dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
  264. dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
  265. channels->min, channels->max);
  266. break;
  267. case SOF_DAI_MEDIATEK_AFE:
  268. rate->min = private->dai_config->afe.rate;
  269. rate->max = private->dai_config->afe.rate;
  270. channels->min = private->dai_config->afe.channels;
  271. channels->max = private->dai_config->afe.channels;
  272. dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
  273. dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
  274. channels->min, channels->max);
  275. break;
  276. case SOF_DAI_IMX_SAI:
  277. rate->min = private->dai_config->sai.fsync_rate;
  278. rate->max = private->dai_config->sai.fsync_rate;
  279. channels->min = private->dai_config->sai.tdm_slots;
  280. channels->max = private->dai_config->sai.tdm_slots;
  281. dev_dbg(component->dev, "rate_min: %d rate_max: %d\n", rate->min, rate->max);
  282. dev_dbg(component->dev, "channels_min: %d channels_max: %d\n",
  283. channels->min, channels->max);
  284. break;
  285. case SOF_DAI_AMD_BT:
  286. rate->min = private->dai_config->acpbt.fsync_rate;
  287. rate->max = private->dai_config->acpbt.fsync_rate;
  288. channels->min = private->dai_config->acpbt.tdm_slots;
  289. channels->max = private->dai_config->acpbt.tdm_slots;
  290. dev_dbg(component->dev,
  291. "AMD_BT rate_min: %d rate_max: %d\n", rate->min, rate->max);
  292. dev_dbg(component->dev, "AMD_BT channels_min: %d channels_max: %d\n",
  293. channels->min, channels->max);
  294. break;
  295. case SOF_DAI_AMD_SP:
  296. rate->min = private->dai_config->acpsp.fsync_rate;
  297. rate->max = private->dai_config->acpsp.fsync_rate;
  298. channels->min = private->dai_config->acpsp.tdm_slots;
  299. channels->max = private->dai_config->acpsp.tdm_slots;
  300. dev_dbg(component->dev,
  301. "AMD_SP rate_min: %d rate_max: %d\n", rate->min, rate->max);
  302. dev_dbg(component->dev, "AMD_SP channels_min: %d channels_max: %d\n",
  303. channels->min, channels->max);
  304. break;
  305. case SOF_DAI_AMD_HS:
  306. rate->min = private->dai_config->acphs.fsync_rate;
  307. rate->max = private->dai_config->acphs.fsync_rate;
  308. channels->min = private->dai_config->acphs.tdm_slots;
  309. channels->max = private->dai_config->acphs.tdm_slots;
  310. dev_dbg(component->dev,
  311. "AMD_HS channel_max: %d rate_max: %d\n", channels->max, rate->max);
  312. break;
  313. case SOF_DAI_AMD_DMIC:
  314. rate->min = private->dai_config->acpdmic.pdm_rate;
  315. rate->max = private->dai_config->acpdmic.pdm_rate;
  316. channels->min = private->dai_config->acpdmic.pdm_ch;
  317. channels->max = private->dai_config->acpdmic.pdm_ch;
  318. dev_dbg(component->dev,
  319. "AMD_DMIC rate_min: %d rate_max: %d\n", rate->min, rate->max);
  320. dev_dbg(component->dev, "AMD_DMIC channels_min: %d channels_max: %d\n",
  321. channels->min, channels->max);
  322. break;
  323. default:
  324. dev_err(component->dev, "Invalid DAI type %d\n", private->dai_config->type);
  325. break;
  326. }
  327. return 0;
  328. }
  329. const struct sof_ipc_pcm_ops ipc3_pcm_ops = {
  330. .hw_params = sof_ipc3_pcm_hw_params,
  331. .hw_free = sof_ipc3_pcm_hw_free,
  332. .trigger = sof_ipc3_pcm_trigger,
  333. .dai_link_fixup = sof_ipc3_pcm_dai_link_fixup,
  334. };