hda-dai.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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) 2018 Intel Corporation
  7. //
  8. // Authors: Keyon Jie <yang.jie@linux.intel.com>
  9. //
  10. #include <sound/pcm_params.h>
  11. #include <sound/hdaudio_ext.h>
  12. #include <sound/hda-mlink.h>
  13. #include <sound/hda_register.h>
  14. #include <sound/intel-nhlt.h>
  15. #include <sound/sof/ipc4/header.h>
  16. #include <uapi/sound/sof/header.h>
  17. #include "../ipc4-priv.h"
  18. #include "../ipc4-topology.h"
  19. #include "../sof-priv.h"
  20. #include "../sof-audio.h"
  21. #include "hda.h"
  22. /*
  23. * The default method is to fetch NHLT from BIOS. With this parameter set
  24. * it is possible to override that with NHLT in the SOF topology manifest.
  25. */
  26. static bool hda_use_tplg_nhlt;
  27. module_param_named(sof_use_tplg_nhlt, hda_use_tplg_nhlt, bool, 0444);
  28. MODULE_PARM_DESC(sof_use_tplg_nhlt, "SOF topology nhlt override");
  29. int hda_dai_config(struct snd_soc_dapm_widget *w, unsigned int flags,
  30. struct snd_sof_dai_config_data *data)
  31. {
  32. struct snd_sof_widget *swidget = w->dobj.private;
  33. const struct sof_ipc_tplg_ops *tplg_ops;
  34. struct snd_sof_dev *sdev;
  35. int ret;
  36. if (!swidget)
  37. return 0;
  38. sdev = widget_to_sdev(w);
  39. tplg_ops = sof_ipc_get_ops(sdev, tplg);
  40. if (tplg_ops && tplg_ops->dai_config) {
  41. ret = tplg_ops->dai_config(sdev, swidget, flags, data);
  42. if (ret < 0) {
  43. dev_err(sdev->dev, "DAI config with flags %x failed for widget %s\n",
  44. flags, w->name);
  45. return ret;
  46. }
  47. }
  48. return 0;
  49. }
  50. EXPORT_SYMBOL_NS(hda_dai_config, SND_SOC_SOF_INTEL_HDA_COMMON);
  51. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_LINK)
  52. static struct snd_sof_dev *dai_to_sdev(struct snd_pcm_substream *substream,
  53. struct snd_soc_dai *cpu_dai)
  54. {
  55. struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream);
  56. return widget_to_sdev(w);
  57. }
  58. static const struct hda_dai_widget_dma_ops *
  59. hda_dai_get_ops(struct snd_pcm_substream *substream, struct snd_soc_dai *cpu_dai)
  60. {
  61. struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream);
  62. struct snd_sof_widget *swidget = w->dobj.private;
  63. struct snd_sof_dev *sdev;
  64. struct snd_sof_dai *sdai;
  65. sdev = widget_to_sdev(w);
  66. if (!swidget) {
  67. dev_err(sdev->dev, "%s: swidget is NULL\n", __func__);
  68. return NULL;
  69. }
  70. if (sdev->dspless_mode_selected)
  71. return hda_select_dai_widget_ops(sdev, swidget);
  72. sdai = swidget->private;
  73. /* select and set the DAI widget ops if not set already */
  74. if (!sdai->platform_private) {
  75. const struct hda_dai_widget_dma_ops *ops =
  76. hda_select_dai_widget_ops(sdev, swidget);
  77. if (!ops)
  78. return NULL;
  79. /* check if mandatory ops are set */
  80. if (!ops || !ops->get_hext_stream)
  81. return NULL;
  82. sdai->platform_private = ops;
  83. }
  84. return sdai->platform_private;
  85. }
  86. static int
  87. hda_link_dma_cleanup(struct snd_pcm_substream *substream,
  88. struct hdac_ext_stream *hext_stream,
  89. struct snd_soc_dai *cpu_dai, bool release)
  90. {
  91. const struct hda_dai_widget_dma_ops *ops = hda_dai_get_ops(substream, cpu_dai);
  92. struct sof_intel_hda_stream *hda_stream;
  93. struct hdac_ext_link *hlink;
  94. struct snd_sof_dev *sdev;
  95. int stream_tag;
  96. if (!ops) {
  97. dev_err(cpu_dai->dev, "DAI widget ops not set\n");
  98. return -EINVAL;
  99. }
  100. sdev = dai_to_sdev(substream, cpu_dai);
  101. hlink = ops->get_hlink(sdev, substream);
  102. if (!hlink)
  103. return -EINVAL;
  104. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  105. stream_tag = hdac_stream(hext_stream)->stream_tag;
  106. snd_hdac_ext_bus_link_clear_stream_id(hlink, stream_tag);
  107. }
  108. if (!release) {
  109. /*
  110. * Force stream reconfiguration without releasing the channel on
  111. * subsequent stream restart (without free), including LinkDMA
  112. * reset.
  113. * The stream is released via hda_dai_hw_free()
  114. */
  115. hext_stream->link_prepared = 0;
  116. return 0;
  117. }
  118. if (ops->release_hext_stream)
  119. ops->release_hext_stream(sdev, cpu_dai, substream);
  120. hext_stream->link_prepared = 0;
  121. /* free the host DMA channel reserved by hostless streams */
  122. hda_stream = hstream_to_sof_hda_stream(hext_stream);
  123. hda_stream->host_reserved = 0;
  124. return 0;
  125. }
  126. static int hda_link_dma_hw_params(struct snd_pcm_substream *substream,
  127. struct snd_pcm_hw_params *params, struct snd_soc_dai *cpu_dai)
  128. {
  129. const struct hda_dai_widget_dma_ops *ops = hda_dai_get_ops(substream, cpu_dai);
  130. struct hdac_ext_stream *hext_stream;
  131. struct hdac_stream *hstream;
  132. struct hdac_ext_link *hlink;
  133. struct snd_sof_dev *sdev;
  134. int stream_tag;
  135. if (!ops) {
  136. dev_err(cpu_dai->dev, "DAI widget ops not set\n");
  137. return -EINVAL;
  138. }
  139. sdev = dai_to_sdev(substream, cpu_dai);
  140. hlink = ops->get_hlink(sdev, substream);
  141. if (!hlink)
  142. return -EINVAL;
  143. hext_stream = ops->get_hext_stream(sdev, cpu_dai, substream);
  144. if (!hext_stream) {
  145. if (ops->assign_hext_stream)
  146. hext_stream = ops->assign_hext_stream(sdev, cpu_dai, substream);
  147. }
  148. if (!hext_stream)
  149. return -EBUSY;
  150. hstream = &hext_stream->hstream;
  151. stream_tag = hstream->stream_tag;
  152. if (hext_stream->hstream.direction == SNDRV_PCM_STREAM_PLAYBACK)
  153. snd_hdac_ext_bus_link_set_stream_id(hlink, stream_tag);
  154. /* set the hdac_stream in the codec dai */
  155. if (ops->codec_dai_set_stream)
  156. ops->codec_dai_set_stream(sdev, substream, hstream);
  157. if (ops->reset_hext_stream)
  158. ops->reset_hext_stream(sdev, hext_stream);
  159. if (ops->calc_stream_format && ops->setup_hext_stream) {
  160. unsigned int format_val = ops->calc_stream_format(sdev, substream, params);
  161. ops->setup_hext_stream(sdev, hext_stream, format_val);
  162. }
  163. hext_stream->link_prepared = 1;
  164. return 0;
  165. }
  166. static int __maybe_unused hda_dai_hw_free(struct snd_pcm_substream *substream,
  167. struct snd_soc_dai *cpu_dai)
  168. {
  169. const struct hda_dai_widget_dma_ops *ops = hda_dai_get_ops(substream, cpu_dai);
  170. struct hdac_ext_stream *hext_stream;
  171. struct snd_sof_dev *sdev = dai_to_sdev(substream, cpu_dai);
  172. if (!ops) {
  173. dev_err(cpu_dai->dev, "DAI widget ops not set\n");
  174. return -EINVAL;
  175. }
  176. hext_stream = ops->get_hext_stream(sdev, cpu_dai, substream);
  177. if (!hext_stream)
  178. return 0;
  179. return hda_link_dma_cleanup(substream, hext_stream, cpu_dai, true);
  180. }
  181. static int __maybe_unused hda_dai_hw_params_data(struct snd_pcm_substream *substream,
  182. struct snd_pcm_hw_params *params,
  183. struct snd_soc_dai *dai,
  184. struct snd_sof_dai_config_data *data,
  185. unsigned int flags)
  186. {
  187. struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(dai, substream->stream);
  188. const struct hda_dai_widget_dma_ops *ops = hda_dai_get_ops(substream, dai);
  189. struct hdac_ext_stream *hext_stream;
  190. struct snd_sof_dev *sdev = widget_to_sdev(w);
  191. int ret;
  192. if (!ops) {
  193. dev_err(sdev->dev, "DAI widget ops not set\n");
  194. return -EINVAL;
  195. }
  196. hext_stream = ops->get_hext_stream(sdev, dai, substream);
  197. if (hext_stream && hext_stream->link_prepared)
  198. return 0;
  199. ret = hda_link_dma_hw_params(substream, params, dai);
  200. if (ret < 0)
  201. return ret;
  202. hext_stream = ops->get_hext_stream(sdev, dai, substream);
  203. flags |= SOF_DAI_CONFIG_FLAGS_2_STEP_STOP << SOF_DAI_CONFIG_FLAGS_QUIRK_SHIFT;
  204. data->dai_data = hdac_stream(hext_stream)->stream_tag - 1;
  205. return hda_dai_config(w, flags, data);
  206. }
  207. static int __maybe_unused hda_dai_hw_params(struct snd_pcm_substream *substream,
  208. struct snd_pcm_hw_params *params,
  209. struct snd_soc_dai *dai)
  210. {
  211. struct snd_sof_dai_config_data data = { 0 };
  212. unsigned int flags = SOF_DAI_CONFIG_FLAGS_HW_PARAMS;
  213. return hda_dai_hw_params_data(substream, params, dai, &data, flags);
  214. }
  215. /*
  216. * In contrast to IPC3, the dai trigger in IPC4 mixes pipeline state changes
  217. * (over IPC channel) and DMA state change (direct host register changes).
  218. */
  219. static int __maybe_unused hda_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  220. struct snd_soc_dai *dai)
  221. {
  222. const struct hda_dai_widget_dma_ops *ops = hda_dai_get_ops(substream, dai);
  223. struct hdac_ext_stream *hext_stream;
  224. struct snd_sof_dev *sdev;
  225. int ret;
  226. if (!ops) {
  227. dev_err(dai->dev, "DAI widget ops not set\n");
  228. return -EINVAL;
  229. }
  230. dev_dbg(dai->dev, "cmd=%d dai %s direction %d\n", cmd,
  231. dai->name, substream->stream);
  232. sdev = dai_to_sdev(substream, dai);
  233. hext_stream = ops->get_hext_stream(sdev, dai, substream);
  234. if (!hext_stream)
  235. return -EINVAL;
  236. if (ops->pre_trigger) {
  237. ret = ops->pre_trigger(sdev, dai, substream, cmd);
  238. if (ret < 0)
  239. return ret;
  240. }
  241. if (ops->trigger) {
  242. ret = ops->trigger(sdev, dai, substream, cmd);
  243. if (ret < 0)
  244. return ret;
  245. }
  246. if (ops->post_trigger) {
  247. ret = ops->post_trigger(sdev, dai, substream, cmd);
  248. if (ret < 0)
  249. return ret;
  250. }
  251. switch (cmd) {
  252. case SNDRV_PCM_TRIGGER_STOP:
  253. case SNDRV_PCM_TRIGGER_SUSPEND:
  254. ret = hda_link_dma_cleanup(substream, hext_stream, dai,
  255. cmd == SNDRV_PCM_TRIGGER_STOP ? false : true);
  256. if (ret < 0) {
  257. dev_err(sdev->dev, "%s: failed to clean up link DMA\n", __func__);
  258. return ret;
  259. }
  260. break;
  261. default:
  262. break;
  263. }
  264. return 0;
  265. }
  266. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
  267. static int hda_dai_prepare(struct snd_pcm_substream *substream, struct snd_soc_dai *dai)
  268. {
  269. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  270. int stream = substream->stream;
  271. return hda_dai_hw_params(substream, &rtd->dpcm[stream].hw_params, dai);
  272. }
  273. static const struct snd_soc_dai_ops hda_dai_ops = {
  274. .hw_params = hda_dai_hw_params,
  275. .hw_free = hda_dai_hw_free,
  276. .trigger = hda_dai_trigger,
  277. .prepare = hda_dai_prepare,
  278. };
  279. #endif
  280. static struct sof_ipc4_copier *widget_to_copier(struct snd_soc_dapm_widget *w)
  281. {
  282. struct snd_sof_widget *swidget = w->dobj.private;
  283. struct snd_sof_dai *sdai = swidget->private;
  284. struct sof_ipc4_copier *ipc4_copier = (struct sof_ipc4_copier *)sdai->private;
  285. return ipc4_copier;
  286. }
  287. static int non_hda_dai_hw_params_data(struct snd_pcm_substream *substream,
  288. struct snd_pcm_hw_params *params,
  289. struct snd_soc_dai *cpu_dai,
  290. struct snd_sof_dai_config_data *data,
  291. unsigned int flags)
  292. {
  293. struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream);
  294. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  295. struct sof_ipc4_dma_config_tlv *dma_config_tlv;
  296. const struct hda_dai_widget_dma_ops *ops;
  297. struct sof_ipc4_dma_config *dma_config;
  298. struct sof_ipc4_copier *ipc4_copier;
  299. struct hdac_ext_stream *hext_stream;
  300. struct hdac_stream *hstream;
  301. struct snd_sof_dev *sdev;
  302. struct snd_soc_dai *dai;
  303. int cpu_dai_id;
  304. int stream_id;
  305. int ret;
  306. ops = hda_dai_get_ops(substream, cpu_dai);
  307. if (!ops) {
  308. dev_err(cpu_dai->dev, "DAI widget ops not set\n");
  309. return -EINVAL;
  310. }
  311. sdev = widget_to_sdev(w);
  312. hext_stream = ops->get_hext_stream(sdev, cpu_dai, substream);
  313. /* nothing more to do if the link is already prepared */
  314. if (hext_stream && hext_stream->link_prepared)
  315. return 0;
  316. /* use HDaudio stream handling */
  317. ret = hda_dai_hw_params_data(substream, params, cpu_dai, data, flags);
  318. if (ret < 0) {
  319. dev_err(cpu_dai->dev, "%s: hda_dai_hw_params_data failed: %d\n", __func__, ret);
  320. return ret;
  321. }
  322. if (sdev->dspless_mode_selected)
  323. return 0;
  324. /* get stream_id */
  325. hext_stream = ops->get_hext_stream(sdev, cpu_dai, substream);
  326. if (!hext_stream) {
  327. dev_err(cpu_dai->dev, "%s: no hext_stream found\n", __func__);
  328. return -ENODEV;
  329. }
  330. hstream = &hext_stream->hstream;
  331. stream_id = hstream->stream_tag;
  332. if (!stream_id) {
  333. dev_err(cpu_dai->dev, "%s: no stream_id allocated\n", __func__);
  334. return -ENODEV;
  335. }
  336. /* configure TLV */
  337. ipc4_copier = widget_to_copier(w);
  338. for_each_rtd_cpu_dais(rtd, cpu_dai_id, dai) {
  339. if (dai == cpu_dai)
  340. break;
  341. }
  342. dma_config_tlv = &ipc4_copier->dma_config_tlv[cpu_dai_id];
  343. dma_config_tlv->type = SOF_IPC4_GTW_DMA_CONFIG_ID;
  344. /* dma_config_priv_size is zero */
  345. dma_config_tlv->length = sizeof(dma_config_tlv->dma_config);
  346. dma_config = &dma_config_tlv->dma_config;
  347. dma_config->dma_method = SOF_IPC4_DMA_METHOD_HDA;
  348. dma_config->pre_allocated_by_host = 1;
  349. dma_config->dma_channel_id = stream_id - 1;
  350. dma_config->stream_id = stream_id;
  351. /*
  352. * Currently we use a DMA for each device in ALH blob. The device will
  353. * be copied in sof_ipc4_prepare_copier_module.
  354. */
  355. dma_config->dma_stream_channel_map.device_count = 1;
  356. dma_config->dma_priv_config_size = 0;
  357. return 0;
  358. }
  359. static int non_hda_dai_hw_params(struct snd_pcm_substream *substream,
  360. struct snd_pcm_hw_params *params,
  361. struct snd_soc_dai *cpu_dai)
  362. {
  363. struct snd_sof_dai_config_data data = { 0 };
  364. unsigned int flags = SOF_DAI_CONFIG_FLAGS_HW_PARAMS;
  365. return non_hda_dai_hw_params_data(substream, params, cpu_dai, &data, flags);
  366. }
  367. static int non_hda_dai_prepare(struct snd_pcm_substream *substream,
  368. struct snd_soc_dai *cpu_dai)
  369. {
  370. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  371. int stream = substream->stream;
  372. return non_hda_dai_hw_params(substream, &rtd->dpcm[stream].hw_params, cpu_dai);
  373. }
  374. static const struct snd_soc_dai_ops ssp_dai_ops = {
  375. .hw_params = non_hda_dai_hw_params,
  376. .hw_free = hda_dai_hw_free,
  377. .trigger = hda_dai_trigger,
  378. .prepare = non_hda_dai_prepare,
  379. };
  380. static const struct snd_soc_dai_ops dmic_dai_ops = {
  381. .hw_params = non_hda_dai_hw_params,
  382. .hw_free = hda_dai_hw_free,
  383. .trigger = hda_dai_trigger,
  384. .prepare = non_hda_dai_prepare,
  385. };
  386. int sdw_hda_dai_hw_params(struct snd_pcm_substream *substream,
  387. struct snd_pcm_hw_params *params,
  388. struct snd_soc_dai *cpu_dai,
  389. int link_id,
  390. int intel_alh_id)
  391. {
  392. struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream);
  393. struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
  394. struct sof_ipc4_dma_config_tlv *dma_config_tlv;
  395. struct snd_sof_dai_config_data data = { 0 };
  396. unsigned int flags = SOF_DAI_CONFIG_FLAGS_HW_PARAMS;
  397. const struct hda_dai_widget_dma_ops *ops;
  398. struct sof_ipc4_dma_config *dma_config;
  399. struct sof_ipc4_copier *ipc4_copier;
  400. struct hdac_ext_stream *hext_stream;
  401. struct snd_soc_dai *dai;
  402. struct snd_sof_dev *sdev;
  403. bool cpu_dai_found = false;
  404. int cpu_dai_id;
  405. int ch_mask;
  406. int ret;
  407. int i;
  408. if (!w) {
  409. dev_err(cpu_dai->dev, "%s widget not found, check amp link num in the topology\n",
  410. cpu_dai->name);
  411. return -EINVAL;
  412. }
  413. ops = hda_dai_get_ops(substream, cpu_dai);
  414. if (!ops) {
  415. dev_err(cpu_dai->dev, "DAI widget ops not set\n");
  416. return -EINVAL;
  417. }
  418. sdev = widget_to_sdev(w);
  419. hext_stream = ops->get_hext_stream(sdev, cpu_dai, substream);
  420. /* nothing more to do if the link is already prepared */
  421. if (hext_stream && hext_stream->link_prepared)
  422. return 0;
  423. /*
  424. * reset the PCMSyCM registers to handle a prepare callback when the PCM is restarted
  425. * due to xruns or after a call to snd_pcm_drain/drop()
  426. */
  427. ret = hdac_bus_eml_sdw_map_stream_ch(sof_to_bus(sdev), link_id, cpu_dai->id,
  428. 0, 0, substream->stream);
  429. if (ret < 0) {
  430. dev_err(cpu_dai->dev, "%s: hdac_bus_eml_sdw_map_stream_ch failed %d\n",
  431. __func__, ret);
  432. return ret;
  433. }
  434. data.dai_index = (link_id << 8) | cpu_dai->id;
  435. data.dai_node_id = intel_alh_id;
  436. ret = non_hda_dai_hw_params_data(substream, params, cpu_dai, &data, flags);
  437. if (ret < 0) {
  438. dev_err(cpu_dai->dev, "%s: non_hda_dai_hw_params failed %d\n", __func__, ret);
  439. return ret;
  440. }
  441. hext_stream = ops->get_hext_stream(sdev, cpu_dai, substream);
  442. if (!hext_stream)
  443. return -ENODEV;
  444. /*
  445. * in the case of SoundWire we need to program the PCMSyCM registers. In case
  446. * of aggregated devices, we need to define the channel mask for each sublink
  447. * by reconstructing the split done in soc-pcm.c
  448. */
  449. for_each_rtd_cpu_dais(rtd, cpu_dai_id, dai) {
  450. if (dai == cpu_dai) {
  451. cpu_dai_found = true;
  452. break;
  453. }
  454. }
  455. if (!cpu_dai_found)
  456. return -ENODEV;
  457. ch_mask = GENMASK(params_channels(params) - 1, 0);
  458. ret = hdac_bus_eml_sdw_map_stream_ch(sof_to_bus(sdev), link_id, cpu_dai->id,
  459. ch_mask,
  460. hdac_stream(hext_stream)->stream_tag,
  461. substream->stream);
  462. if (ret < 0) {
  463. dev_err(cpu_dai->dev, "%s: hdac_bus_eml_sdw_map_stream_ch failed %d\n",
  464. __func__, ret);
  465. return ret;
  466. }
  467. if (sdev->dspless_mode_selected)
  468. return 0;
  469. ipc4_copier = widget_to_copier(w);
  470. dma_config_tlv = &ipc4_copier->dma_config_tlv[cpu_dai_id];
  471. dma_config = &dma_config_tlv->dma_config;
  472. dma_config->dma_stream_channel_map.mapping[0].device = data.dai_index;
  473. dma_config->dma_stream_channel_map.mapping[0].channel_mask = ch_mask;
  474. /*
  475. * copy the dma_config_tlv to all ipc4_copier in the same link. Because only one copier
  476. * will be handled in sof_ipc4_prepare_copier_module.
  477. */
  478. for_each_rtd_cpu_dais(rtd, i, dai) {
  479. w = snd_soc_dai_get_widget(dai, substream->stream);
  480. if (!w) {
  481. dev_err(cpu_dai->dev,
  482. "%s widget not found, check amp link num in the topology\n",
  483. dai->name);
  484. return -EINVAL;
  485. }
  486. ipc4_copier = widget_to_copier(w);
  487. memcpy(&ipc4_copier->dma_config_tlv[cpu_dai_id], dma_config_tlv,
  488. sizeof(*dma_config_tlv));
  489. }
  490. return 0;
  491. }
  492. EXPORT_SYMBOL_NS(sdw_hda_dai_hw_params, SND_SOC_SOF_INTEL_HDA_COMMON);
  493. int sdw_hda_dai_hw_free(struct snd_pcm_substream *substream,
  494. struct snd_soc_dai *cpu_dai,
  495. int link_id)
  496. {
  497. struct snd_soc_dapm_widget *w = snd_soc_dai_get_widget(cpu_dai, substream->stream);
  498. struct snd_sof_dev *sdev;
  499. int ret;
  500. ret = hda_dai_hw_free(substream, cpu_dai);
  501. if (ret < 0) {
  502. dev_err(cpu_dai->dev, "%s: non_hda_dai_hw_free failed %d\n", __func__, ret);
  503. return ret;
  504. }
  505. sdev = widget_to_sdev(w);
  506. /* in the case of SoundWire we need to reset the PCMSyCM registers */
  507. ret = hdac_bus_eml_sdw_map_stream_ch(sof_to_bus(sdev), link_id, cpu_dai->id,
  508. 0, 0, substream->stream);
  509. if (ret < 0) {
  510. dev_err(cpu_dai->dev, "%s: hdac_bus_eml_sdw_map_stream_ch failed %d\n",
  511. __func__, ret);
  512. return ret;
  513. }
  514. return 0;
  515. }
  516. EXPORT_SYMBOL_NS(sdw_hda_dai_hw_free, SND_SOC_SOF_INTEL_HDA_COMMON);
  517. int sdw_hda_dai_trigger(struct snd_pcm_substream *substream, int cmd,
  518. struct snd_soc_dai *cpu_dai)
  519. {
  520. return hda_dai_trigger(substream, cmd, cpu_dai);
  521. }
  522. EXPORT_SYMBOL_NS(sdw_hda_dai_trigger, SND_SOC_SOF_INTEL_HDA_COMMON);
  523. static int hda_dai_suspend(struct hdac_bus *bus)
  524. {
  525. struct snd_soc_pcm_runtime *rtd;
  526. struct hdac_ext_stream *hext_stream;
  527. struct hdac_stream *s;
  528. int ret;
  529. /* set internal flag for BE */
  530. list_for_each_entry(s, &bus->stream_list, list) {
  531. hext_stream = stream_to_hdac_ext_stream(s);
  532. /*
  533. * clear stream. This should already be taken care for running
  534. * streams when the SUSPEND trigger is called. But paused
  535. * streams do not get suspended, so this needs to be done
  536. * explicitly during suspend.
  537. */
  538. if (hext_stream->link_substream) {
  539. const struct hda_dai_widget_dma_ops *ops;
  540. struct snd_sof_widget *swidget;
  541. struct snd_soc_dapm_widget *w;
  542. struct snd_soc_dai *cpu_dai;
  543. struct snd_sof_dev *sdev;
  544. struct snd_sof_dai *sdai;
  545. rtd = snd_soc_substream_to_rtd(hext_stream->link_substream);
  546. cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  547. w = snd_soc_dai_get_widget(cpu_dai, hdac_stream(hext_stream)->direction);
  548. swidget = w->dobj.private;
  549. sdev = widget_to_sdev(w);
  550. sdai = swidget->private;
  551. ops = sdai->platform_private;
  552. /* for consistency with TRIGGER_SUSPEND */
  553. if (ops->post_trigger) {
  554. ret = ops->post_trigger(sdev, cpu_dai,
  555. hext_stream->link_substream,
  556. SNDRV_PCM_TRIGGER_SUSPEND);
  557. if (ret < 0)
  558. return ret;
  559. }
  560. ret = hda_link_dma_cleanup(hext_stream->link_substream,
  561. hext_stream, cpu_dai, true);
  562. if (ret < 0)
  563. return ret;
  564. }
  565. }
  566. return 0;
  567. }
  568. static void ssp_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops)
  569. {
  570. const struct sof_intel_dsp_desc *chip;
  571. int i;
  572. chip = get_chip_info(sdev->pdata);
  573. if (chip->hw_ip_version >= SOF_INTEL_ACE_2_0) {
  574. for (i = 0; i < ops->num_drv; i++) {
  575. if (strstr(ops->drv[i].name, "SSP"))
  576. ops->drv[i].ops = &ssp_dai_ops;
  577. }
  578. }
  579. }
  580. static void dmic_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops)
  581. {
  582. const struct sof_intel_dsp_desc *chip;
  583. int i;
  584. chip = get_chip_info(sdev->pdata);
  585. if (chip->hw_ip_version >= SOF_INTEL_ACE_2_0) {
  586. for (i = 0; i < ops->num_drv; i++) {
  587. if (strstr(ops->drv[i].name, "DMIC"))
  588. ops->drv[i].ops = &dmic_dai_ops;
  589. }
  590. }
  591. }
  592. #else
  593. static inline void ssp_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops) {}
  594. static inline void dmic_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops) {}
  595. #endif /* CONFIG_SND_SOC_SOF_HDA_LINK */
  596. void hda_set_dai_drv_ops(struct snd_sof_dev *sdev, struct snd_sof_dsp_ops *ops)
  597. {
  598. int i;
  599. for (i = 0; i < ops->num_drv; i++) {
  600. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
  601. if (strstr(ops->drv[i].name, "iDisp") ||
  602. strstr(ops->drv[i].name, "Analog") ||
  603. strstr(ops->drv[i].name, "Digital"))
  604. ops->drv[i].ops = &hda_dai_ops;
  605. #endif
  606. }
  607. ssp_set_dai_drv_ops(sdev, ops);
  608. dmic_set_dai_drv_ops(sdev, ops);
  609. if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4 && !hda_use_tplg_nhlt) {
  610. struct sof_ipc4_fw_data *ipc4_data = sdev->private;
  611. ipc4_data->nhlt = intel_nhlt_init(sdev->dev);
  612. }
  613. }
  614. EXPORT_SYMBOL_NS(hda_set_dai_drv_ops, SND_SOC_SOF_INTEL_HDA_COMMON);
  615. void hda_ops_free(struct snd_sof_dev *sdev)
  616. {
  617. if (sdev->pdata->ipc_type == SOF_IPC_TYPE_4) {
  618. struct sof_ipc4_fw_data *ipc4_data = sdev->private;
  619. if (!hda_use_tplg_nhlt)
  620. intel_nhlt_free(ipc4_data->nhlt);
  621. kfree(sdev->private);
  622. sdev->private = NULL;
  623. }
  624. }
  625. EXPORT_SYMBOL_NS(hda_ops_free, SND_SOC_SOF_INTEL_HDA_COMMON);
  626. /*
  627. * common dai driver for skl+ platforms.
  628. * some products who use this DAI array only physically have a subset of
  629. * the DAIs, but no harm is done here by adding the whole set.
  630. */
  631. struct snd_soc_dai_driver skl_dai[] = {
  632. {
  633. .name = "SSP0 Pin",
  634. .playback = {
  635. .channels_min = 1,
  636. .channels_max = 8,
  637. },
  638. .capture = {
  639. .channels_min = 1,
  640. .channels_max = 8,
  641. },
  642. },
  643. {
  644. .name = "SSP1 Pin",
  645. .playback = {
  646. .channels_min = 1,
  647. .channels_max = 8,
  648. },
  649. .capture = {
  650. .channels_min = 1,
  651. .channels_max = 8,
  652. },
  653. },
  654. {
  655. .name = "SSP2 Pin",
  656. .playback = {
  657. .channels_min = 1,
  658. .channels_max = 8,
  659. },
  660. .capture = {
  661. .channels_min = 1,
  662. .channels_max = 8,
  663. },
  664. },
  665. {
  666. .name = "SSP3 Pin",
  667. .playback = {
  668. .channels_min = 1,
  669. .channels_max = 8,
  670. },
  671. .capture = {
  672. .channels_min = 1,
  673. .channels_max = 8,
  674. },
  675. },
  676. {
  677. .name = "SSP4 Pin",
  678. .playback = {
  679. .channels_min = 1,
  680. .channels_max = 8,
  681. },
  682. .capture = {
  683. .channels_min = 1,
  684. .channels_max = 8,
  685. },
  686. },
  687. {
  688. .name = "SSP5 Pin",
  689. .playback = {
  690. .channels_min = 1,
  691. .channels_max = 8,
  692. },
  693. .capture = {
  694. .channels_min = 1,
  695. .channels_max = 8,
  696. },
  697. },
  698. {
  699. .name = "DMIC01 Pin",
  700. .capture = {
  701. .channels_min = 1,
  702. .channels_max = 4,
  703. },
  704. },
  705. {
  706. .name = "DMIC16k Pin",
  707. .capture = {
  708. .channels_min = 1,
  709. .channels_max = 4,
  710. },
  711. },
  712. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
  713. {
  714. .name = "iDisp1 Pin",
  715. .playback = {
  716. .channels_min = 1,
  717. .channels_max = 8,
  718. },
  719. },
  720. {
  721. .name = "iDisp2 Pin",
  722. .playback = {
  723. .channels_min = 1,
  724. .channels_max = 8,
  725. },
  726. },
  727. {
  728. .name = "iDisp3 Pin",
  729. .playback = {
  730. .channels_min = 1,
  731. .channels_max = 8,
  732. },
  733. },
  734. {
  735. .name = "iDisp4 Pin",
  736. .playback = {
  737. .channels_min = 1,
  738. .channels_max = 8,
  739. },
  740. },
  741. {
  742. .name = "Analog CPU DAI",
  743. .playback = {
  744. .channels_min = 1,
  745. .channels_max = 16,
  746. },
  747. .capture = {
  748. .channels_min = 1,
  749. .channels_max = 16,
  750. },
  751. },
  752. {
  753. .name = "Digital CPU DAI",
  754. .playback = {
  755. .channels_min = 1,
  756. .channels_max = 16,
  757. },
  758. .capture = {
  759. .channels_min = 1,
  760. .channels_max = 16,
  761. },
  762. },
  763. {
  764. .name = "Alt Analog CPU DAI",
  765. .playback = {
  766. .channels_min = 1,
  767. .channels_max = 16,
  768. },
  769. .capture = {
  770. .channels_min = 1,
  771. .channels_max = 16,
  772. },
  773. },
  774. #endif
  775. };
  776. EXPORT_SYMBOL_NS(skl_dai, SND_SOC_SOF_INTEL_HDA_COMMON);
  777. int hda_dsp_dais_suspend(struct snd_sof_dev *sdev)
  778. {
  779. /*
  780. * In the corner case where a SUSPEND happens during a PAUSE, the ALSA core
  781. * does not throw the TRIGGER_SUSPEND. This leaves the DAIs in an unbalanced state.
  782. * Since the component suspend is called last, we can trap this corner case
  783. * and force the DAIs to release their resources.
  784. */
  785. #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_LINK)
  786. int ret;
  787. ret = hda_dai_suspend(sof_to_bus(sdev));
  788. if (ret < 0)
  789. return ret;
  790. #endif
  791. return 0;
  792. }