skl-pcm.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * skl-pcm.c -ASoC HDA Platform driver file implementing PCM functionality
  4. *
  5. * Copyright (C) 2014-2015 Intel Corp
  6. * Author: Jeeja KP <[email protected]>
  7. *
  8. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  9. *
  10. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  11. */
  12. #include <linux/pci.h>
  13. #include <linux/pm_runtime.h>
  14. #include <linux/delay.h>
  15. #include <sound/pcm_params.h>
  16. #include <sound/soc.h>
  17. #include "skl.h"
  18. #include "skl-topology.h"
  19. #include "skl-sst-dsp.h"
  20. #include "skl-sst-ipc.h"
  21. #define HDA_MONO 1
  22. #define HDA_STEREO 2
  23. #define HDA_QUAD 4
  24. #define HDA_MAX 8
  25. static const struct snd_pcm_hardware azx_pcm_hw = {
  26. .info = (SNDRV_PCM_INFO_MMAP |
  27. SNDRV_PCM_INFO_INTERLEAVED |
  28. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  29. SNDRV_PCM_INFO_MMAP_VALID |
  30. SNDRV_PCM_INFO_PAUSE |
  31. SNDRV_PCM_INFO_RESUME |
  32. SNDRV_PCM_INFO_SYNC_START |
  33. SNDRV_PCM_INFO_HAS_WALL_CLOCK | /* legacy */
  34. SNDRV_PCM_INFO_HAS_LINK_ATIME |
  35. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
  36. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  37. SNDRV_PCM_FMTBIT_S32_LE |
  38. SNDRV_PCM_FMTBIT_S24_LE,
  39. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 |
  40. SNDRV_PCM_RATE_8000,
  41. .rate_min = 8000,
  42. .rate_max = 48000,
  43. .channels_min = 1,
  44. .channels_max = 8,
  45. .buffer_bytes_max = AZX_MAX_BUF_SIZE,
  46. .period_bytes_min = 128,
  47. .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
  48. .periods_min = 2,
  49. .periods_max = AZX_MAX_FRAG,
  50. .fifo_size = 0,
  51. };
  52. static inline
  53. struct hdac_ext_stream *get_hdac_ext_stream(struct snd_pcm_substream *substream)
  54. {
  55. return substream->runtime->private_data;
  56. }
  57. static struct hdac_bus *get_bus_ctx(struct snd_pcm_substream *substream)
  58. {
  59. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  60. struct hdac_stream *hstream = hdac_stream(stream);
  61. struct hdac_bus *bus = hstream->bus;
  62. return bus;
  63. }
  64. static int skl_substream_alloc_pages(struct hdac_bus *bus,
  65. struct snd_pcm_substream *substream,
  66. size_t size)
  67. {
  68. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  69. hdac_stream(stream)->bufsize = 0;
  70. hdac_stream(stream)->period_bytes = 0;
  71. hdac_stream(stream)->format_val = 0;
  72. return 0;
  73. }
  74. static void skl_set_pcm_constrains(struct hdac_bus *bus,
  75. struct snd_pcm_runtime *runtime)
  76. {
  77. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  78. /* avoid wrap-around with wall-clock */
  79. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
  80. 20, 178000000);
  81. }
  82. static enum hdac_ext_stream_type skl_get_host_stream_type(struct hdac_bus *bus)
  83. {
  84. if (bus->ppcap)
  85. return HDAC_EXT_STREAM_TYPE_HOST;
  86. else
  87. return HDAC_EXT_STREAM_TYPE_COUPLED;
  88. }
  89. /*
  90. * check if the stream opened is marked as ignore_suspend by machine, if so
  91. * then enable suspend_active refcount
  92. *
  93. * The count supend_active does not need lock as it is used in open/close
  94. * and suspend context
  95. */
  96. static void skl_set_suspend_active(struct snd_pcm_substream *substream,
  97. struct snd_soc_dai *dai, bool enable)
  98. {
  99. struct hdac_bus *bus = dev_get_drvdata(dai->dev);
  100. struct snd_soc_dapm_widget *w;
  101. struct skl_dev *skl = bus_to_skl(bus);
  102. w = snd_soc_dai_get_widget(dai, substream->stream);
  103. if (w->ignore_suspend && enable)
  104. skl->supend_active++;
  105. else if (w->ignore_suspend && !enable)
  106. skl->supend_active--;
  107. }
  108. int skl_pcm_host_dma_prepare(struct device *dev, struct skl_pipe_params *params)
  109. {
  110. struct hdac_bus *bus = dev_get_drvdata(dev);
  111. struct skl_dev *skl = bus_to_skl(bus);
  112. unsigned int format_val;
  113. struct hdac_stream *hstream;
  114. struct hdac_ext_stream *stream;
  115. int err;
  116. hstream = snd_hdac_get_stream(bus, params->stream,
  117. params->host_dma_id + 1);
  118. if (!hstream)
  119. return -EINVAL;
  120. stream = stream_to_hdac_ext_stream(hstream);
  121. snd_hdac_ext_stream_decouple(bus, stream, true);
  122. format_val = snd_hdac_calc_stream_format(params->s_freq,
  123. params->ch, params->format, params->host_bps, 0);
  124. dev_dbg(dev, "format_val=%d, rate=%d, ch=%d, format=%d\n",
  125. format_val, params->s_freq, params->ch, params->format);
  126. snd_hdac_stream_reset(hdac_stream(stream));
  127. err = snd_hdac_stream_set_params(hdac_stream(stream), format_val);
  128. if (err < 0)
  129. return err;
  130. /*
  131. * The recommended SDxFMT programming sequence for BXT
  132. * platforms is to couple the stream before writing the format
  133. */
  134. if (IS_BXT(skl->pci)) {
  135. snd_hdac_ext_stream_decouple(bus, stream, false);
  136. err = snd_hdac_stream_setup(hdac_stream(stream));
  137. snd_hdac_ext_stream_decouple(bus, stream, true);
  138. } else {
  139. err = snd_hdac_stream_setup(hdac_stream(stream));
  140. }
  141. if (err < 0)
  142. return err;
  143. hdac_stream(stream)->prepared = 1;
  144. return 0;
  145. }
  146. int skl_pcm_link_dma_prepare(struct device *dev, struct skl_pipe_params *params)
  147. {
  148. struct hdac_bus *bus = dev_get_drvdata(dev);
  149. unsigned int format_val;
  150. struct hdac_stream *hstream;
  151. struct hdac_ext_stream *stream;
  152. struct hdac_ext_link *link;
  153. unsigned char stream_tag;
  154. hstream = snd_hdac_get_stream(bus, params->stream,
  155. params->link_dma_id + 1);
  156. if (!hstream)
  157. return -EINVAL;
  158. stream = stream_to_hdac_ext_stream(hstream);
  159. snd_hdac_ext_stream_decouple(bus, stream, true);
  160. format_val = snd_hdac_calc_stream_format(params->s_freq, params->ch,
  161. params->format, params->link_bps, 0);
  162. dev_dbg(dev, "format_val=%d, rate=%d, ch=%d, format=%d\n",
  163. format_val, params->s_freq, params->ch, params->format);
  164. snd_hdac_ext_link_stream_reset(stream);
  165. snd_hdac_ext_link_stream_setup(stream, format_val);
  166. stream_tag = hstream->stream_tag;
  167. if (stream->hstream.direction == SNDRV_PCM_STREAM_PLAYBACK) {
  168. list_for_each_entry(link, &bus->hlink_list, list) {
  169. if (link->index == params->link_index)
  170. snd_hdac_ext_link_set_stream_id(link,
  171. stream_tag);
  172. }
  173. }
  174. stream->link_prepared = 1;
  175. return 0;
  176. }
  177. static int skl_pcm_open(struct snd_pcm_substream *substream,
  178. struct snd_soc_dai *dai)
  179. {
  180. struct hdac_bus *bus = dev_get_drvdata(dai->dev);
  181. struct hdac_ext_stream *stream;
  182. struct snd_pcm_runtime *runtime = substream->runtime;
  183. struct skl_dma_params *dma_params;
  184. struct skl_dev *skl = get_skl_ctx(dai->dev);
  185. struct skl_module_cfg *mconfig;
  186. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  187. stream = snd_hdac_ext_stream_assign(bus, substream,
  188. skl_get_host_stream_type(bus));
  189. if (stream == NULL)
  190. return -EBUSY;
  191. skl_set_pcm_constrains(bus, runtime);
  192. /*
  193. * disable WALLCLOCK timestamps for capture streams
  194. * until we figure out how to handle digital inputs
  195. */
  196. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  197. runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK; /* legacy */
  198. runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_LINK_ATIME;
  199. }
  200. runtime->private_data = stream;
  201. dma_params = kzalloc(sizeof(*dma_params), GFP_KERNEL);
  202. if (!dma_params)
  203. return -ENOMEM;
  204. dma_params->stream_tag = hdac_stream(stream)->stream_tag;
  205. snd_soc_dai_set_dma_data(dai, substream, dma_params);
  206. dev_dbg(dai->dev, "stream tag set in dma params=%d\n",
  207. dma_params->stream_tag);
  208. skl_set_suspend_active(substream, dai, true);
  209. snd_pcm_set_sync(substream);
  210. mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
  211. if (!mconfig)
  212. return -EINVAL;
  213. skl_tplg_d0i3_get(skl, mconfig->d0i3_caps);
  214. return 0;
  215. }
  216. static int skl_pcm_prepare(struct snd_pcm_substream *substream,
  217. struct snd_soc_dai *dai)
  218. {
  219. struct skl_dev *skl = get_skl_ctx(dai->dev);
  220. struct skl_module_cfg *mconfig;
  221. int ret;
  222. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  223. mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
  224. /*
  225. * In case of XRUN recovery or in the case when the application
  226. * calls prepare another time, reset the FW pipe to clean state
  227. */
  228. if (mconfig &&
  229. (substream->runtime->state == SNDRV_PCM_STATE_XRUN ||
  230. mconfig->pipe->state == SKL_PIPE_CREATED ||
  231. mconfig->pipe->state == SKL_PIPE_PAUSED)) {
  232. ret = skl_reset_pipe(skl, mconfig->pipe);
  233. if (ret < 0)
  234. return ret;
  235. ret = skl_pcm_host_dma_prepare(dai->dev,
  236. mconfig->pipe->p_params);
  237. if (ret < 0)
  238. return ret;
  239. }
  240. return 0;
  241. }
  242. static int skl_pcm_hw_params(struct snd_pcm_substream *substream,
  243. struct snd_pcm_hw_params *params,
  244. struct snd_soc_dai *dai)
  245. {
  246. struct hdac_bus *bus = dev_get_drvdata(dai->dev);
  247. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  248. struct snd_pcm_runtime *runtime = substream->runtime;
  249. struct skl_pipe_params p_params = {0};
  250. struct skl_module_cfg *m_cfg;
  251. int ret, dma_id;
  252. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  253. ret = skl_substream_alloc_pages(bus, substream,
  254. params_buffer_bytes(params));
  255. if (ret < 0)
  256. return ret;
  257. dev_dbg(dai->dev, "format_val, rate=%d, ch=%d, format=%d\n",
  258. runtime->rate, runtime->channels, runtime->format);
  259. dma_id = hdac_stream(stream)->stream_tag - 1;
  260. dev_dbg(dai->dev, "dma_id=%d\n", dma_id);
  261. p_params.s_fmt = snd_pcm_format_width(params_format(params));
  262. p_params.s_cont = snd_pcm_format_physical_width(params_format(params));
  263. p_params.ch = params_channels(params);
  264. p_params.s_freq = params_rate(params);
  265. p_params.host_dma_id = dma_id;
  266. p_params.stream = substream->stream;
  267. p_params.format = params_format(params);
  268. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  269. p_params.host_bps = dai->driver->playback.sig_bits;
  270. else
  271. p_params.host_bps = dai->driver->capture.sig_bits;
  272. m_cfg = skl_tplg_fe_get_cpr_module(dai, p_params.stream);
  273. if (m_cfg)
  274. skl_tplg_update_pipe_params(dai->dev, m_cfg, &p_params);
  275. return 0;
  276. }
  277. static void skl_pcm_close(struct snd_pcm_substream *substream,
  278. struct snd_soc_dai *dai)
  279. {
  280. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  281. struct hdac_bus *bus = dev_get_drvdata(dai->dev);
  282. struct skl_dma_params *dma_params = NULL;
  283. struct skl_dev *skl = bus_to_skl(bus);
  284. struct skl_module_cfg *mconfig;
  285. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  286. snd_hdac_ext_stream_release(stream, skl_get_host_stream_type(bus));
  287. dma_params = snd_soc_dai_get_dma_data(dai, substream);
  288. /*
  289. * now we should set this to NULL as we are freeing by the
  290. * dma_params
  291. */
  292. snd_soc_dai_set_dma_data(dai, substream, NULL);
  293. skl_set_suspend_active(substream, dai, false);
  294. /*
  295. * check if close is for "Reference Pin" and set back the
  296. * CGCTL.MISCBDCGE if disabled by driver
  297. */
  298. if (!strncmp(dai->name, "Reference Pin", 13) &&
  299. skl->miscbdcg_disabled) {
  300. skl->enable_miscbdcge(dai->dev, true);
  301. skl->miscbdcg_disabled = false;
  302. }
  303. mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
  304. if (mconfig)
  305. skl_tplg_d0i3_put(skl, mconfig->d0i3_caps);
  306. kfree(dma_params);
  307. }
  308. static int skl_pcm_hw_free(struct snd_pcm_substream *substream,
  309. struct snd_soc_dai *dai)
  310. {
  311. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  312. struct skl_dev *skl = get_skl_ctx(dai->dev);
  313. struct skl_module_cfg *mconfig;
  314. int ret;
  315. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  316. mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
  317. if (mconfig) {
  318. ret = skl_reset_pipe(skl, mconfig->pipe);
  319. if (ret < 0)
  320. dev_err(dai->dev, "%s:Reset failed ret =%d",
  321. __func__, ret);
  322. }
  323. snd_hdac_stream_cleanup(hdac_stream(stream));
  324. hdac_stream(stream)->prepared = 0;
  325. return 0;
  326. }
  327. static int skl_be_hw_params(struct snd_pcm_substream *substream,
  328. struct snd_pcm_hw_params *params,
  329. struct snd_soc_dai *dai)
  330. {
  331. struct skl_pipe_params p_params = {0};
  332. p_params.s_fmt = snd_pcm_format_width(params_format(params));
  333. p_params.s_cont = snd_pcm_format_physical_width(params_format(params));
  334. p_params.ch = params_channels(params);
  335. p_params.s_freq = params_rate(params);
  336. p_params.stream = substream->stream;
  337. return skl_tplg_be_update_params(dai, &p_params);
  338. }
  339. static int skl_decoupled_trigger(struct snd_pcm_substream *substream,
  340. int cmd)
  341. {
  342. struct hdac_bus *bus = get_bus_ctx(substream);
  343. struct hdac_ext_stream *stream;
  344. int start;
  345. unsigned long cookie;
  346. struct hdac_stream *hstr;
  347. stream = get_hdac_ext_stream(substream);
  348. hstr = hdac_stream(stream);
  349. if (!hstr->prepared)
  350. return -EPIPE;
  351. switch (cmd) {
  352. case SNDRV_PCM_TRIGGER_START:
  353. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  354. case SNDRV_PCM_TRIGGER_RESUME:
  355. start = 1;
  356. break;
  357. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  358. case SNDRV_PCM_TRIGGER_SUSPEND:
  359. case SNDRV_PCM_TRIGGER_STOP:
  360. start = 0;
  361. break;
  362. default:
  363. return -EINVAL;
  364. }
  365. spin_lock_irqsave(&bus->reg_lock, cookie);
  366. if (start) {
  367. snd_hdac_stream_start(hdac_stream(stream), true);
  368. snd_hdac_stream_timecounter_init(hstr, 0);
  369. } else {
  370. snd_hdac_stream_stop(hdac_stream(stream));
  371. }
  372. spin_unlock_irqrestore(&bus->reg_lock, cookie);
  373. return 0;
  374. }
  375. static int skl_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
  376. struct snd_soc_dai *dai)
  377. {
  378. struct skl_dev *skl = get_skl_ctx(dai->dev);
  379. struct skl_module_cfg *mconfig;
  380. struct hdac_bus *bus = get_bus_ctx(substream);
  381. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  382. struct snd_soc_dapm_widget *w;
  383. int ret;
  384. mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
  385. if (!mconfig)
  386. return -EIO;
  387. w = snd_soc_dai_get_widget(dai, substream->stream);
  388. switch (cmd) {
  389. case SNDRV_PCM_TRIGGER_RESUME:
  390. if (!w->ignore_suspend) {
  391. /*
  392. * enable DMA Resume enable bit for the stream, set the
  393. * dpib & lpib position to resume before starting the
  394. * DMA
  395. */
  396. snd_hdac_ext_stream_drsm_enable(bus, true,
  397. hdac_stream(stream)->index);
  398. snd_hdac_ext_stream_set_dpibr(bus, stream,
  399. stream->lpib);
  400. snd_hdac_ext_stream_set_lpib(stream, stream->lpib);
  401. }
  402. fallthrough;
  403. case SNDRV_PCM_TRIGGER_START:
  404. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  405. /*
  406. * Start HOST DMA and Start FE Pipe.This is to make sure that
  407. * there are no underrun/overrun in the case when the FE
  408. * pipeline is started but there is a delay in starting the
  409. * DMA channel on the host.
  410. */
  411. ret = skl_decoupled_trigger(substream, cmd);
  412. if (ret < 0)
  413. return ret;
  414. return skl_run_pipe(skl, mconfig->pipe);
  415. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  416. case SNDRV_PCM_TRIGGER_SUSPEND:
  417. case SNDRV_PCM_TRIGGER_STOP:
  418. /*
  419. * Stop FE Pipe first and stop DMA. This is to make sure that
  420. * there are no underrun/overrun in the case if there is a delay
  421. * between the two operations.
  422. */
  423. ret = skl_stop_pipe(skl, mconfig->pipe);
  424. if (ret < 0)
  425. return ret;
  426. ret = skl_decoupled_trigger(substream, cmd);
  427. if ((cmd == SNDRV_PCM_TRIGGER_SUSPEND) && !w->ignore_suspend) {
  428. /* save the dpib and lpib positions */
  429. stream->dpib = readl(bus->remap_addr +
  430. AZX_REG_VS_SDXDPIB_XBASE +
  431. (AZX_REG_VS_SDXDPIB_XINTERVAL *
  432. hdac_stream(stream)->index));
  433. stream->lpib = snd_hdac_stream_get_pos_lpib(
  434. hdac_stream(stream));
  435. snd_hdac_ext_stream_decouple(bus, stream, false);
  436. }
  437. break;
  438. default:
  439. return -EINVAL;
  440. }
  441. return 0;
  442. }
  443. static int skl_link_hw_params(struct snd_pcm_substream *substream,
  444. struct snd_pcm_hw_params *params,
  445. struct snd_soc_dai *dai)
  446. {
  447. struct hdac_bus *bus = dev_get_drvdata(dai->dev);
  448. struct hdac_ext_stream *link_dev;
  449. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  450. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  451. struct skl_pipe_params p_params = {0};
  452. struct hdac_ext_link *link;
  453. int stream_tag;
  454. link_dev = snd_hdac_ext_stream_assign(bus, substream,
  455. HDAC_EXT_STREAM_TYPE_LINK);
  456. if (!link_dev)
  457. return -EBUSY;
  458. snd_soc_dai_set_dma_data(dai, substream, (void *)link_dev);
  459. link = snd_hdac_ext_bus_get_link(bus, codec_dai->component->name);
  460. if (!link)
  461. return -EINVAL;
  462. stream_tag = hdac_stream(link_dev)->stream_tag;
  463. /* set the hdac_stream in the codec dai */
  464. snd_soc_dai_set_stream(codec_dai, hdac_stream(link_dev), substream->stream);
  465. p_params.s_fmt = snd_pcm_format_width(params_format(params));
  466. p_params.s_cont = snd_pcm_format_physical_width(params_format(params));
  467. p_params.ch = params_channels(params);
  468. p_params.s_freq = params_rate(params);
  469. p_params.stream = substream->stream;
  470. p_params.link_dma_id = stream_tag - 1;
  471. p_params.link_index = link->index;
  472. p_params.format = params_format(params);
  473. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  474. p_params.link_bps = codec_dai->driver->playback.sig_bits;
  475. else
  476. p_params.link_bps = codec_dai->driver->capture.sig_bits;
  477. return skl_tplg_be_update_params(dai, &p_params);
  478. }
  479. static int skl_link_pcm_prepare(struct snd_pcm_substream *substream,
  480. struct snd_soc_dai *dai)
  481. {
  482. struct skl_dev *skl = get_skl_ctx(dai->dev);
  483. struct skl_module_cfg *mconfig = NULL;
  484. /* In case of XRUN recovery, reset the FW pipe to clean state */
  485. mconfig = skl_tplg_be_get_cpr_module(dai, substream->stream);
  486. if (mconfig && !mconfig->pipe->passthru &&
  487. (substream->runtime->state == SNDRV_PCM_STATE_XRUN))
  488. skl_reset_pipe(skl, mconfig->pipe);
  489. return 0;
  490. }
  491. static int skl_link_pcm_trigger(struct snd_pcm_substream *substream,
  492. int cmd, struct snd_soc_dai *dai)
  493. {
  494. struct hdac_ext_stream *link_dev =
  495. snd_soc_dai_get_dma_data(dai, substream);
  496. struct hdac_bus *bus = get_bus_ctx(substream);
  497. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  498. dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd);
  499. switch (cmd) {
  500. case SNDRV_PCM_TRIGGER_RESUME:
  501. case SNDRV_PCM_TRIGGER_START:
  502. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  503. snd_hdac_ext_link_stream_start(link_dev);
  504. break;
  505. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  506. case SNDRV_PCM_TRIGGER_SUSPEND:
  507. case SNDRV_PCM_TRIGGER_STOP:
  508. snd_hdac_ext_link_stream_clear(link_dev);
  509. if (cmd == SNDRV_PCM_TRIGGER_SUSPEND)
  510. snd_hdac_ext_stream_decouple(bus, stream, false);
  511. break;
  512. default:
  513. return -EINVAL;
  514. }
  515. return 0;
  516. }
  517. static int skl_link_hw_free(struct snd_pcm_substream *substream,
  518. struct snd_soc_dai *dai)
  519. {
  520. struct hdac_bus *bus = dev_get_drvdata(dai->dev);
  521. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  522. struct hdac_ext_stream *link_dev =
  523. snd_soc_dai_get_dma_data(dai, substream);
  524. struct hdac_ext_link *link;
  525. unsigned char stream_tag;
  526. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  527. link_dev->link_prepared = 0;
  528. link = snd_hdac_ext_bus_get_link(bus, asoc_rtd_to_codec(rtd, 0)->component->name);
  529. if (!link)
  530. return -EINVAL;
  531. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  532. stream_tag = hdac_stream(link_dev)->stream_tag;
  533. snd_hdac_ext_link_clear_stream_id(link, stream_tag);
  534. }
  535. snd_hdac_ext_stream_release(link_dev, HDAC_EXT_STREAM_TYPE_LINK);
  536. return 0;
  537. }
  538. static const struct snd_soc_dai_ops skl_pcm_dai_ops = {
  539. .startup = skl_pcm_open,
  540. .shutdown = skl_pcm_close,
  541. .prepare = skl_pcm_prepare,
  542. .hw_params = skl_pcm_hw_params,
  543. .hw_free = skl_pcm_hw_free,
  544. .trigger = skl_pcm_trigger,
  545. };
  546. static const struct snd_soc_dai_ops skl_dmic_dai_ops = {
  547. .hw_params = skl_be_hw_params,
  548. };
  549. static const struct snd_soc_dai_ops skl_be_ssp_dai_ops = {
  550. .hw_params = skl_be_hw_params,
  551. };
  552. static const struct snd_soc_dai_ops skl_link_dai_ops = {
  553. .prepare = skl_link_pcm_prepare,
  554. .hw_params = skl_link_hw_params,
  555. .hw_free = skl_link_hw_free,
  556. .trigger = skl_link_pcm_trigger,
  557. };
  558. static struct snd_soc_dai_driver skl_fe_dai[] = {
  559. {
  560. .name = "System Pin",
  561. .ops = &skl_pcm_dai_ops,
  562. .playback = {
  563. .stream_name = "System Playback",
  564. .channels_min = HDA_MONO,
  565. .channels_max = HDA_STEREO,
  566. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_8000,
  567. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  568. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
  569. .sig_bits = 32,
  570. },
  571. .capture = {
  572. .stream_name = "System Capture",
  573. .channels_min = HDA_MONO,
  574. .channels_max = HDA_STEREO,
  575. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
  576. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  577. .sig_bits = 32,
  578. },
  579. },
  580. {
  581. .name = "System Pin2",
  582. .ops = &skl_pcm_dai_ops,
  583. .playback = {
  584. .stream_name = "Headset Playback",
  585. .channels_min = HDA_MONO,
  586. .channels_max = HDA_STEREO,
  587. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 |
  588. SNDRV_PCM_RATE_8000,
  589. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  590. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
  591. },
  592. },
  593. {
  594. .name = "Echoref Pin",
  595. .ops = &skl_pcm_dai_ops,
  596. .capture = {
  597. .stream_name = "Echoreference Capture",
  598. .channels_min = HDA_STEREO,
  599. .channels_max = HDA_STEREO,
  600. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 |
  601. SNDRV_PCM_RATE_8000,
  602. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  603. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
  604. },
  605. },
  606. {
  607. .name = "Reference Pin",
  608. .ops = &skl_pcm_dai_ops,
  609. .capture = {
  610. .stream_name = "Reference Capture",
  611. .channels_min = HDA_MONO,
  612. .channels_max = HDA_QUAD,
  613. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
  614. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  615. .sig_bits = 32,
  616. },
  617. },
  618. {
  619. .name = "Deepbuffer Pin",
  620. .ops = &skl_pcm_dai_ops,
  621. .playback = {
  622. .stream_name = "Deepbuffer Playback",
  623. .channels_min = HDA_STEREO,
  624. .channels_max = HDA_STEREO,
  625. .rates = SNDRV_PCM_RATE_48000,
  626. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  627. .sig_bits = 32,
  628. },
  629. },
  630. {
  631. .name = "LowLatency Pin",
  632. .ops = &skl_pcm_dai_ops,
  633. .playback = {
  634. .stream_name = "Low Latency Playback",
  635. .channels_min = HDA_STEREO,
  636. .channels_max = HDA_STEREO,
  637. .rates = SNDRV_PCM_RATE_48000,
  638. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  639. .sig_bits = 32,
  640. },
  641. },
  642. {
  643. .name = "DMIC Pin",
  644. .ops = &skl_pcm_dai_ops,
  645. .capture = {
  646. .stream_name = "DMIC Capture",
  647. .channels_min = HDA_MONO,
  648. .channels_max = HDA_QUAD,
  649. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
  650. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  651. .sig_bits = 32,
  652. },
  653. },
  654. {
  655. .name = "HDMI1 Pin",
  656. .ops = &skl_pcm_dai_ops,
  657. .playback = {
  658. .stream_name = "HDMI1 Playback",
  659. .channels_min = HDA_STEREO,
  660. .channels_max = 8,
  661. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  662. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  663. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  664. SNDRV_PCM_RATE_192000,
  665. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  666. SNDRV_PCM_FMTBIT_S32_LE,
  667. .sig_bits = 32,
  668. },
  669. },
  670. {
  671. .name = "HDMI2 Pin",
  672. .ops = &skl_pcm_dai_ops,
  673. .playback = {
  674. .stream_name = "HDMI2 Playback",
  675. .channels_min = HDA_STEREO,
  676. .channels_max = 8,
  677. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  678. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  679. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  680. SNDRV_PCM_RATE_192000,
  681. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  682. SNDRV_PCM_FMTBIT_S32_LE,
  683. .sig_bits = 32,
  684. },
  685. },
  686. {
  687. .name = "HDMI3 Pin",
  688. .ops = &skl_pcm_dai_ops,
  689. .playback = {
  690. .stream_name = "HDMI3 Playback",
  691. .channels_min = HDA_STEREO,
  692. .channels_max = 8,
  693. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  694. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  695. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  696. SNDRV_PCM_RATE_192000,
  697. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  698. SNDRV_PCM_FMTBIT_S32_LE,
  699. .sig_bits = 32,
  700. },
  701. },
  702. };
  703. /* BE CPU Dais */
  704. static struct snd_soc_dai_driver skl_platform_dai[] = {
  705. {
  706. .name = "SSP0 Pin",
  707. .ops = &skl_be_ssp_dai_ops,
  708. .playback = {
  709. .stream_name = "ssp0 Tx",
  710. .channels_min = HDA_STEREO,
  711. .channels_max = HDA_STEREO,
  712. .rates = SNDRV_PCM_RATE_48000,
  713. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  714. },
  715. .capture = {
  716. .stream_name = "ssp0 Rx",
  717. .channels_min = HDA_STEREO,
  718. .channels_max = HDA_STEREO,
  719. .rates = SNDRV_PCM_RATE_48000,
  720. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  721. },
  722. },
  723. {
  724. .name = "SSP1 Pin",
  725. .ops = &skl_be_ssp_dai_ops,
  726. .playback = {
  727. .stream_name = "ssp1 Tx",
  728. .channels_min = HDA_STEREO,
  729. .channels_max = HDA_STEREO,
  730. .rates = SNDRV_PCM_RATE_48000,
  731. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  732. },
  733. .capture = {
  734. .stream_name = "ssp1 Rx",
  735. .channels_min = HDA_STEREO,
  736. .channels_max = HDA_STEREO,
  737. .rates = SNDRV_PCM_RATE_48000,
  738. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  739. },
  740. },
  741. {
  742. .name = "SSP2 Pin",
  743. .ops = &skl_be_ssp_dai_ops,
  744. .playback = {
  745. .stream_name = "ssp2 Tx",
  746. .channels_min = HDA_STEREO,
  747. .channels_max = HDA_STEREO,
  748. .rates = SNDRV_PCM_RATE_48000,
  749. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  750. },
  751. .capture = {
  752. .stream_name = "ssp2 Rx",
  753. .channels_min = HDA_STEREO,
  754. .channels_max = HDA_STEREO,
  755. .rates = SNDRV_PCM_RATE_48000,
  756. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  757. },
  758. },
  759. {
  760. .name = "SSP3 Pin",
  761. .ops = &skl_be_ssp_dai_ops,
  762. .playback = {
  763. .stream_name = "ssp3 Tx",
  764. .channels_min = HDA_STEREO,
  765. .channels_max = HDA_STEREO,
  766. .rates = SNDRV_PCM_RATE_48000,
  767. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  768. },
  769. .capture = {
  770. .stream_name = "ssp3 Rx",
  771. .channels_min = HDA_STEREO,
  772. .channels_max = HDA_STEREO,
  773. .rates = SNDRV_PCM_RATE_48000,
  774. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  775. },
  776. },
  777. {
  778. .name = "SSP4 Pin",
  779. .ops = &skl_be_ssp_dai_ops,
  780. .playback = {
  781. .stream_name = "ssp4 Tx",
  782. .channels_min = HDA_STEREO,
  783. .channels_max = HDA_STEREO,
  784. .rates = SNDRV_PCM_RATE_48000,
  785. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  786. },
  787. .capture = {
  788. .stream_name = "ssp4 Rx",
  789. .channels_min = HDA_STEREO,
  790. .channels_max = HDA_STEREO,
  791. .rates = SNDRV_PCM_RATE_48000,
  792. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  793. },
  794. },
  795. {
  796. .name = "SSP5 Pin",
  797. .ops = &skl_be_ssp_dai_ops,
  798. .playback = {
  799. .stream_name = "ssp5 Tx",
  800. .channels_min = HDA_STEREO,
  801. .channels_max = HDA_STEREO,
  802. .rates = SNDRV_PCM_RATE_48000,
  803. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  804. },
  805. .capture = {
  806. .stream_name = "ssp5 Rx",
  807. .channels_min = HDA_STEREO,
  808. .channels_max = HDA_STEREO,
  809. .rates = SNDRV_PCM_RATE_48000,
  810. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  811. },
  812. },
  813. {
  814. .name = "iDisp1 Pin",
  815. .ops = &skl_link_dai_ops,
  816. .playback = {
  817. .stream_name = "iDisp1 Tx",
  818. .channels_min = HDA_STEREO,
  819. .channels_max = 8,
  820. .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000,
  821. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE |
  822. SNDRV_PCM_FMTBIT_S24_LE,
  823. },
  824. },
  825. {
  826. .name = "iDisp2 Pin",
  827. .ops = &skl_link_dai_ops,
  828. .playback = {
  829. .stream_name = "iDisp2 Tx",
  830. .channels_min = HDA_STEREO,
  831. .channels_max = 8,
  832. .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|
  833. SNDRV_PCM_RATE_48000,
  834. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE |
  835. SNDRV_PCM_FMTBIT_S24_LE,
  836. },
  837. },
  838. {
  839. .name = "iDisp3 Pin",
  840. .ops = &skl_link_dai_ops,
  841. .playback = {
  842. .stream_name = "iDisp3 Tx",
  843. .channels_min = HDA_STEREO,
  844. .channels_max = 8,
  845. .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|
  846. SNDRV_PCM_RATE_48000,
  847. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE |
  848. SNDRV_PCM_FMTBIT_S24_LE,
  849. },
  850. },
  851. {
  852. .name = "DMIC01 Pin",
  853. .ops = &skl_dmic_dai_ops,
  854. .capture = {
  855. .stream_name = "DMIC01 Rx",
  856. .channels_min = HDA_MONO,
  857. .channels_max = HDA_QUAD,
  858. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
  859. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  860. },
  861. },
  862. {
  863. .name = "DMIC16k Pin",
  864. .ops = &skl_dmic_dai_ops,
  865. .capture = {
  866. .stream_name = "DMIC16k Rx",
  867. .channels_min = HDA_MONO,
  868. .channels_max = HDA_QUAD,
  869. .rates = SNDRV_PCM_RATE_16000,
  870. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  871. },
  872. },
  873. {
  874. .name = "Analog CPU DAI",
  875. .ops = &skl_link_dai_ops,
  876. .playback = {
  877. .stream_name = "Analog CPU Playback",
  878. .channels_min = HDA_MONO,
  879. .channels_max = HDA_MAX,
  880. .rates = SNDRV_PCM_RATE_8000_192000,
  881. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  882. SNDRV_PCM_FMTBIT_S32_LE,
  883. },
  884. .capture = {
  885. .stream_name = "Analog CPU Capture",
  886. .channels_min = HDA_MONO,
  887. .channels_max = HDA_MAX,
  888. .rates = SNDRV_PCM_RATE_8000_192000,
  889. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  890. SNDRV_PCM_FMTBIT_S32_LE,
  891. },
  892. },
  893. {
  894. .name = "Alt Analog CPU DAI",
  895. .ops = &skl_link_dai_ops,
  896. .playback = {
  897. .stream_name = "Alt Analog CPU Playback",
  898. .channels_min = HDA_MONO,
  899. .channels_max = HDA_MAX,
  900. .rates = SNDRV_PCM_RATE_8000_192000,
  901. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  902. SNDRV_PCM_FMTBIT_S32_LE,
  903. },
  904. .capture = {
  905. .stream_name = "Alt Analog CPU Capture",
  906. .channels_min = HDA_MONO,
  907. .channels_max = HDA_MAX,
  908. .rates = SNDRV_PCM_RATE_8000_192000,
  909. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  910. SNDRV_PCM_FMTBIT_S32_LE,
  911. },
  912. },
  913. {
  914. .name = "Digital CPU DAI",
  915. .ops = &skl_link_dai_ops,
  916. .playback = {
  917. .stream_name = "Digital CPU Playback",
  918. .channels_min = HDA_MONO,
  919. .channels_max = HDA_MAX,
  920. .rates = SNDRV_PCM_RATE_8000_192000,
  921. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  922. SNDRV_PCM_FMTBIT_S32_LE,
  923. },
  924. .capture = {
  925. .stream_name = "Digital CPU Capture",
  926. .channels_min = HDA_MONO,
  927. .channels_max = HDA_MAX,
  928. .rates = SNDRV_PCM_RATE_8000_192000,
  929. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  930. SNDRV_PCM_FMTBIT_S32_LE,
  931. },
  932. },
  933. };
  934. int skl_dai_load(struct snd_soc_component *cmp, int index,
  935. struct snd_soc_dai_driver *dai_drv,
  936. struct snd_soc_tplg_pcm *pcm, struct snd_soc_dai *dai)
  937. {
  938. dai_drv->ops = &skl_pcm_dai_ops;
  939. return 0;
  940. }
  941. static int skl_platform_soc_open(struct snd_soc_component *component,
  942. struct snd_pcm_substream *substream)
  943. {
  944. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  945. struct snd_soc_dai_link *dai_link = rtd->dai_link;
  946. dev_dbg(asoc_rtd_to_cpu(rtd, 0)->dev, "In %s:%s\n", __func__,
  947. dai_link->cpus->dai_name);
  948. snd_soc_set_runtime_hwparams(substream, &azx_pcm_hw);
  949. return 0;
  950. }
  951. static int skl_coupled_trigger(struct snd_pcm_substream *substream,
  952. int cmd)
  953. {
  954. struct hdac_bus *bus = get_bus_ctx(substream);
  955. struct hdac_ext_stream *stream;
  956. struct snd_pcm_substream *s;
  957. bool start;
  958. int sbits = 0;
  959. unsigned long cookie;
  960. struct hdac_stream *hstr;
  961. stream = get_hdac_ext_stream(substream);
  962. hstr = hdac_stream(stream);
  963. dev_dbg(bus->dev, "In %s cmd=%d\n", __func__, cmd);
  964. if (!hstr->prepared)
  965. return -EPIPE;
  966. switch (cmd) {
  967. case SNDRV_PCM_TRIGGER_START:
  968. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  969. case SNDRV_PCM_TRIGGER_RESUME:
  970. start = true;
  971. break;
  972. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  973. case SNDRV_PCM_TRIGGER_SUSPEND:
  974. case SNDRV_PCM_TRIGGER_STOP:
  975. start = false;
  976. break;
  977. default:
  978. return -EINVAL;
  979. }
  980. snd_pcm_group_for_each_entry(s, substream) {
  981. if (s->pcm->card != substream->pcm->card)
  982. continue;
  983. stream = get_hdac_ext_stream(s);
  984. sbits |= 1 << hdac_stream(stream)->index;
  985. snd_pcm_trigger_done(s, substream);
  986. }
  987. spin_lock_irqsave(&bus->reg_lock, cookie);
  988. /* first, set SYNC bits of corresponding streams */
  989. snd_hdac_stream_sync_trigger(hstr, true, sbits, AZX_REG_SSYNC);
  990. snd_pcm_group_for_each_entry(s, substream) {
  991. if (s->pcm->card != substream->pcm->card)
  992. continue;
  993. stream = get_hdac_ext_stream(s);
  994. if (start)
  995. snd_hdac_stream_start(hdac_stream(stream), true);
  996. else
  997. snd_hdac_stream_stop(hdac_stream(stream));
  998. }
  999. spin_unlock_irqrestore(&bus->reg_lock, cookie);
  1000. snd_hdac_stream_sync(hstr, start, sbits);
  1001. spin_lock_irqsave(&bus->reg_lock, cookie);
  1002. /* reset SYNC bits */
  1003. snd_hdac_stream_sync_trigger(hstr, false, sbits, AZX_REG_SSYNC);
  1004. if (start)
  1005. snd_hdac_stream_timecounter_init(hstr, sbits);
  1006. spin_unlock_irqrestore(&bus->reg_lock, cookie);
  1007. return 0;
  1008. }
  1009. static int skl_platform_soc_trigger(struct snd_soc_component *component,
  1010. struct snd_pcm_substream *substream,
  1011. int cmd)
  1012. {
  1013. struct hdac_bus *bus = get_bus_ctx(substream);
  1014. if (!bus->ppcap)
  1015. return skl_coupled_trigger(substream, cmd);
  1016. return 0;
  1017. }
  1018. static snd_pcm_uframes_t skl_platform_soc_pointer(
  1019. struct snd_soc_component *component,
  1020. struct snd_pcm_substream *substream)
  1021. {
  1022. struct hdac_ext_stream *hstream = get_hdac_ext_stream(substream);
  1023. struct hdac_bus *bus = get_bus_ctx(substream);
  1024. unsigned int pos;
  1025. /*
  1026. * Use DPIB for Playback stream as the periodic DMA Position-in-
  1027. * Buffer Writes may be scheduled at the same time or later than
  1028. * the MSI and does not guarantee to reflect the Position of the
  1029. * last buffer that was transferred. Whereas DPIB register in
  1030. * HAD space reflects the actual data that is transferred.
  1031. * Use the position buffer for capture, as DPIB write gets
  1032. * completed earlier than the actual data written to the DDR.
  1033. *
  1034. * For capture stream following workaround is required to fix the
  1035. * incorrect position reporting.
  1036. *
  1037. * 1. Wait for 20us before reading the DMA position in buffer once
  1038. * the interrupt is generated for stream completion as update happens
  1039. * on the HDA frame boundary i.e. 20.833uSec.
  1040. * 2. Read DPIB register to flush the DMA position value. This dummy
  1041. * read is required to flush DMA position value.
  1042. * 3. Read the DMA Position-in-Buffer. This value now will be equal to
  1043. * or greater than period boundary.
  1044. */
  1045. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  1046. pos = readl(bus->remap_addr + AZX_REG_VS_SDXDPIB_XBASE +
  1047. (AZX_REG_VS_SDXDPIB_XINTERVAL *
  1048. hdac_stream(hstream)->index));
  1049. } else {
  1050. udelay(20);
  1051. readl(bus->remap_addr +
  1052. AZX_REG_VS_SDXDPIB_XBASE +
  1053. (AZX_REG_VS_SDXDPIB_XINTERVAL *
  1054. hdac_stream(hstream)->index));
  1055. pos = snd_hdac_stream_get_pos_posbuf(hdac_stream(hstream));
  1056. }
  1057. if (pos >= hdac_stream(hstream)->bufsize)
  1058. pos = 0;
  1059. return bytes_to_frames(substream->runtime, pos);
  1060. }
  1061. static u64 skl_adjust_codec_delay(struct snd_pcm_substream *substream,
  1062. u64 nsec)
  1063. {
  1064. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  1065. struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
  1066. u64 codec_frames, codec_nsecs;
  1067. if (!codec_dai->driver->ops->delay)
  1068. return nsec;
  1069. codec_frames = codec_dai->driver->ops->delay(substream, codec_dai);
  1070. codec_nsecs = div_u64(codec_frames * 1000000000LL,
  1071. substream->runtime->rate);
  1072. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  1073. return nsec + codec_nsecs;
  1074. return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
  1075. }
  1076. static int skl_platform_soc_get_time_info(
  1077. struct snd_soc_component *component,
  1078. struct snd_pcm_substream *substream,
  1079. struct timespec64 *system_ts, struct timespec64 *audio_ts,
  1080. struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
  1081. struct snd_pcm_audio_tstamp_report *audio_tstamp_report)
  1082. {
  1083. struct hdac_ext_stream *sstream = get_hdac_ext_stream(substream);
  1084. struct hdac_stream *hstr = hdac_stream(sstream);
  1085. u64 nsec;
  1086. if ((substream->runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_ATIME) &&
  1087. (audio_tstamp_config->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK)) {
  1088. snd_pcm_gettime(substream->runtime, system_ts);
  1089. nsec = timecounter_read(&hstr->tc);
  1090. if (audio_tstamp_config->report_delay)
  1091. nsec = skl_adjust_codec_delay(substream, nsec);
  1092. *audio_ts = ns_to_timespec64(nsec);
  1093. audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
  1094. audio_tstamp_report->accuracy_report = 1; /* rest of struct is valid */
  1095. audio_tstamp_report->accuracy = 42; /* 24MHzWallClk == 42ns resolution */
  1096. } else {
  1097. audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
  1098. }
  1099. return 0;
  1100. }
  1101. #define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
  1102. static int skl_platform_soc_new(struct snd_soc_component *component,
  1103. struct snd_soc_pcm_runtime *rtd)
  1104. {
  1105. struct snd_soc_dai *dai = asoc_rtd_to_cpu(rtd, 0);
  1106. struct hdac_bus *bus = dev_get_drvdata(dai->dev);
  1107. struct snd_pcm *pcm = rtd->pcm;
  1108. unsigned int size;
  1109. struct skl_dev *skl = bus_to_skl(bus);
  1110. if (dai->driver->playback.channels_min ||
  1111. dai->driver->capture.channels_min) {
  1112. /* buffer pre-allocation */
  1113. size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
  1114. if (size > MAX_PREALLOC_SIZE)
  1115. size = MAX_PREALLOC_SIZE;
  1116. snd_pcm_set_managed_buffer_all(pcm,
  1117. SNDRV_DMA_TYPE_DEV_SG,
  1118. &skl->pci->dev,
  1119. size, MAX_PREALLOC_SIZE);
  1120. }
  1121. return 0;
  1122. }
  1123. static int skl_get_module_info(struct skl_dev *skl,
  1124. struct skl_module_cfg *mconfig)
  1125. {
  1126. struct skl_module_inst_id *pin_id;
  1127. guid_t *uuid_mod, *uuid_tplg;
  1128. struct skl_module *skl_module;
  1129. struct uuid_module *module;
  1130. int i, ret = -EIO;
  1131. uuid_mod = (guid_t *)mconfig->guid;
  1132. if (list_empty(&skl->uuid_list)) {
  1133. dev_err(skl->dev, "Module list is empty\n");
  1134. return -EIO;
  1135. }
  1136. for (i = 0; i < skl->nr_modules; i++) {
  1137. skl_module = skl->modules[i];
  1138. uuid_tplg = &skl_module->uuid;
  1139. if (guid_equal(uuid_mod, uuid_tplg)) {
  1140. mconfig->module = skl_module;
  1141. ret = 0;
  1142. break;
  1143. }
  1144. }
  1145. if (skl->nr_modules && ret)
  1146. return ret;
  1147. ret = -EIO;
  1148. list_for_each_entry(module, &skl->uuid_list, list) {
  1149. if (guid_equal(uuid_mod, &module->uuid)) {
  1150. mconfig->id.module_id = module->id;
  1151. mconfig->module->loadable = module->is_loadable;
  1152. ret = 0;
  1153. }
  1154. for (i = 0; i < MAX_IN_QUEUE; i++) {
  1155. pin_id = &mconfig->m_in_pin[i].id;
  1156. if (guid_equal(&pin_id->mod_uuid, &module->uuid))
  1157. pin_id->module_id = module->id;
  1158. }
  1159. for (i = 0; i < MAX_OUT_QUEUE; i++) {
  1160. pin_id = &mconfig->m_out_pin[i].id;
  1161. if (guid_equal(&pin_id->mod_uuid, &module->uuid))
  1162. pin_id->module_id = module->id;
  1163. }
  1164. }
  1165. return ret;
  1166. }
  1167. static int skl_populate_modules(struct skl_dev *skl)
  1168. {
  1169. struct skl_pipeline *p;
  1170. struct skl_pipe_module *m;
  1171. struct snd_soc_dapm_widget *w;
  1172. struct skl_module_cfg *mconfig;
  1173. int ret = 0;
  1174. list_for_each_entry(p, &skl->ppl_list, node) {
  1175. list_for_each_entry(m, &p->pipe->w_list, node) {
  1176. w = m->w;
  1177. mconfig = w->priv;
  1178. ret = skl_get_module_info(skl, mconfig);
  1179. if (ret < 0) {
  1180. dev_err(skl->dev,
  1181. "query module info failed\n");
  1182. return ret;
  1183. }
  1184. skl_tplg_add_moduleid_in_bind_params(skl, w);
  1185. }
  1186. }
  1187. return ret;
  1188. }
  1189. static int skl_platform_soc_probe(struct snd_soc_component *component)
  1190. {
  1191. struct hdac_bus *bus = dev_get_drvdata(component->dev);
  1192. struct skl_dev *skl = bus_to_skl(bus);
  1193. const struct skl_dsp_ops *ops;
  1194. int ret;
  1195. ret = pm_runtime_resume_and_get(component->dev);
  1196. if (ret < 0 && ret != -EACCES)
  1197. return ret;
  1198. if (bus->ppcap) {
  1199. skl->component = component;
  1200. /* init debugfs */
  1201. skl->debugfs = skl_debugfs_init(skl);
  1202. ret = skl_tplg_init(component, bus);
  1203. if (ret < 0) {
  1204. dev_err(component->dev, "Failed to init topology!\n");
  1205. return ret;
  1206. }
  1207. /* load the firmwares, since all is set */
  1208. ops = skl_get_dsp_ops(skl->pci->device);
  1209. if (!ops)
  1210. return -EIO;
  1211. /*
  1212. * Disable dynamic clock and power gating during firmware
  1213. * and library download
  1214. */
  1215. skl->enable_miscbdcge(component->dev, false);
  1216. skl->clock_power_gating(component->dev, false);
  1217. ret = ops->init_fw(component->dev, skl);
  1218. skl->enable_miscbdcge(component->dev, true);
  1219. skl->clock_power_gating(component->dev, true);
  1220. if (ret < 0) {
  1221. dev_err(component->dev, "Failed to boot first fw: %d\n", ret);
  1222. return ret;
  1223. }
  1224. skl_populate_modules(skl);
  1225. skl->update_d0i3c = skl_update_d0i3c;
  1226. if (skl->cfg.astate_cfg != NULL) {
  1227. skl_dsp_set_astate_cfg(skl,
  1228. skl->cfg.astate_cfg->count,
  1229. skl->cfg.astate_cfg);
  1230. }
  1231. }
  1232. pm_runtime_mark_last_busy(component->dev);
  1233. pm_runtime_put_autosuspend(component->dev);
  1234. return 0;
  1235. }
  1236. static void skl_platform_soc_remove(struct snd_soc_component *component)
  1237. {
  1238. struct hdac_bus *bus = dev_get_drvdata(component->dev);
  1239. struct skl_dev *skl = bus_to_skl(bus);
  1240. skl_tplg_exit(component, bus);
  1241. skl_debugfs_exit(skl);
  1242. }
  1243. static const struct snd_soc_component_driver skl_component = {
  1244. .name = "pcm",
  1245. .probe = skl_platform_soc_probe,
  1246. .remove = skl_platform_soc_remove,
  1247. .open = skl_platform_soc_open,
  1248. .trigger = skl_platform_soc_trigger,
  1249. .pointer = skl_platform_soc_pointer,
  1250. .get_time_info = skl_platform_soc_get_time_info,
  1251. .pcm_construct = skl_platform_soc_new,
  1252. .module_get_upon_open = 1, /* increment refcount when a pcm is opened */
  1253. };
  1254. int skl_platform_register(struct device *dev)
  1255. {
  1256. int ret;
  1257. struct snd_soc_dai_driver *dais;
  1258. int num_dais = ARRAY_SIZE(skl_platform_dai);
  1259. struct hdac_bus *bus = dev_get_drvdata(dev);
  1260. struct skl_dev *skl = bus_to_skl(bus);
  1261. skl->dais = kmemdup(skl_platform_dai, sizeof(skl_platform_dai),
  1262. GFP_KERNEL);
  1263. if (!skl->dais) {
  1264. ret = -ENOMEM;
  1265. goto err;
  1266. }
  1267. if (!skl->use_tplg_pcm) {
  1268. dais = krealloc(skl->dais, sizeof(skl_fe_dai) +
  1269. sizeof(skl_platform_dai), GFP_KERNEL);
  1270. if (!dais) {
  1271. ret = -ENOMEM;
  1272. goto err;
  1273. }
  1274. skl->dais = dais;
  1275. memcpy(&skl->dais[ARRAY_SIZE(skl_platform_dai)], skl_fe_dai,
  1276. sizeof(skl_fe_dai));
  1277. num_dais += ARRAY_SIZE(skl_fe_dai);
  1278. }
  1279. ret = devm_snd_soc_register_component(dev, &skl_component,
  1280. skl->dais, num_dais);
  1281. if (ret)
  1282. dev_err(dev, "soc component registration failed %d\n", ret);
  1283. err:
  1284. return ret;
  1285. }
  1286. int skl_platform_unregister(struct device *dev)
  1287. {
  1288. struct hdac_bus *bus = dev_get_drvdata(dev);
  1289. struct skl_dev *skl = bus_to_skl(bus);
  1290. struct skl_module_deferred_bind *modules, *tmp;
  1291. list_for_each_entry_safe(modules, tmp, &skl->bind_list, node) {
  1292. list_del(&modules->node);
  1293. kfree(modules);
  1294. }
  1295. kfree(skl->dais);
  1296. return 0;
  1297. }