skl-pcm.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. /*
  2. * skl-pcm.c -ASoC HDA Platform driver file implementing PCM functionality
  3. *
  4. * Copyright (C) 2014-2015 Intel Corp
  5. * Author: Jeeja KP <jeeja.kp@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. *
  20. */
  21. #include <linux/pci.h>
  22. #include <linux/pm_runtime.h>
  23. #include <sound/pcm_params.h>
  24. #include <sound/soc.h>
  25. #include "skl.h"
  26. #include "skl-topology.h"
  27. #include "skl-sst-dsp.h"
  28. #include "skl-sst-ipc.h"
  29. #define HDA_MONO 1
  30. #define HDA_STEREO 2
  31. #define HDA_QUAD 4
  32. static struct snd_pcm_hardware azx_pcm_hw = {
  33. .info = (SNDRV_PCM_INFO_MMAP |
  34. SNDRV_PCM_INFO_INTERLEAVED |
  35. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  36. SNDRV_PCM_INFO_MMAP_VALID |
  37. SNDRV_PCM_INFO_PAUSE |
  38. SNDRV_PCM_INFO_RESUME |
  39. SNDRV_PCM_INFO_SYNC_START |
  40. SNDRV_PCM_INFO_HAS_WALL_CLOCK | /* legacy */
  41. SNDRV_PCM_INFO_HAS_LINK_ATIME |
  42. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
  43. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  44. SNDRV_PCM_FMTBIT_S32_LE |
  45. SNDRV_PCM_FMTBIT_S24_LE,
  46. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 |
  47. SNDRV_PCM_RATE_8000,
  48. .rate_min = 8000,
  49. .rate_max = 48000,
  50. .channels_min = 1,
  51. .channels_max = 8,
  52. .buffer_bytes_max = AZX_MAX_BUF_SIZE,
  53. .period_bytes_min = 128,
  54. .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
  55. .periods_min = 2,
  56. .periods_max = AZX_MAX_FRAG,
  57. .fifo_size = 0,
  58. };
  59. static inline
  60. struct hdac_ext_stream *get_hdac_ext_stream(struct snd_pcm_substream *substream)
  61. {
  62. return substream->runtime->private_data;
  63. }
  64. static struct hdac_ext_bus *get_bus_ctx(struct snd_pcm_substream *substream)
  65. {
  66. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  67. struct hdac_stream *hstream = hdac_stream(stream);
  68. struct hdac_bus *bus = hstream->bus;
  69. return hbus_to_ebus(bus);
  70. }
  71. static int skl_substream_alloc_pages(struct hdac_ext_bus *ebus,
  72. struct snd_pcm_substream *substream,
  73. size_t size)
  74. {
  75. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  76. hdac_stream(stream)->bufsize = 0;
  77. hdac_stream(stream)->period_bytes = 0;
  78. hdac_stream(stream)->format_val = 0;
  79. return snd_pcm_lib_malloc_pages(substream, size);
  80. }
  81. static int skl_substream_free_pages(struct hdac_bus *bus,
  82. struct snd_pcm_substream *substream)
  83. {
  84. return snd_pcm_lib_free_pages(substream);
  85. }
  86. static void skl_set_pcm_constrains(struct hdac_ext_bus *ebus,
  87. struct snd_pcm_runtime *runtime)
  88. {
  89. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  90. /* avoid wrap-around with wall-clock */
  91. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
  92. 20, 178000000);
  93. }
  94. static enum hdac_ext_stream_type skl_get_host_stream_type(struct hdac_ext_bus *ebus)
  95. {
  96. if ((ebus_to_hbus(ebus))->ppcap)
  97. return HDAC_EXT_STREAM_TYPE_HOST;
  98. else
  99. return HDAC_EXT_STREAM_TYPE_COUPLED;
  100. }
  101. /*
  102. * check if the stream opened is marked as ignore_suspend by machine, if so
  103. * then enable suspend_active refcount
  104. *
  105. * The count supend_active does not need lock as it is used in open/close
  106. * and suspend context
  107. */
  108. static void skl_set_suspend_active(struct snd_pcm_substream *substream,
  109. struct snd_soc_dai *dai, bool enable)
  110. {
  111. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  112. struct snd_soc_dapm_widget *w;
  113. struct skl *skl = ebus_to_skl(ebus);
  114. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  115. w = dai->playback_widget;
  116. else
  117. w = dai->capture_widget;
  118. if (w->ignore_suspend && enable)
  119. skl->supend_active++;
  120. else if (w->ignore_suspend && !enable)
  121. skl->supend_active--;
  122. }
  123. static int skl_pcm_open(struct snd_pcm_substream *substream,
  124. struct snd_soc_dai *dai)
  125. {
  126. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  127. struct hdac_ext_stream *stream;
  128. struct snd_pcm_runtime *runtime = substream->runtime;
  129. struct skl_dma_params *dma_params;
  130. struct skl *skl = get_skl_ctx(dai->dev);
  131. struct skl_module_cfg *mconfig;
  132. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  133. stream = snd_hdac_ext_stream_assign(ebus, substream,
  134. skl_get_host_stream_type(ebus));
  135. if (stream == NULL)
  136. return -EBUSY;
  137. skl_set_pcm_constrains(ebus, runtime);
  138. /*
  139. * disable WALLCLOCK timestamps for capture streams
  140. * until we figure out how to handle digital inputs
  141. */
  142. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  143. runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK; /* legacy */
  144. runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_LINK_ATIME;
  145. }
  146. runtime->private_data = stream;
  147. dma_params = kzalloc(sizeof(*dma_params), GFP_KERNEL);
  148. if (!dma_params)
  149. return -ENOMEM;
  150. dma_params->stream_tag = hdac_stream(stream)->stream_tag;
  151. snd_soc_dai_set_dma_data(dai, substream, dma_params);
  152. dev_dbg(dai->dev, "stream tag set in dma params=%d\n",
  153. dma_params->stream_tag);
  154. skl_set_suspend_active(substream, dai, true);
  155. snd_pcm_set_sync(substream);
  156. mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
  157. if (!mconfig)
  158. return -EINVAL;
  159. skl_tplg_d0i3_get(skl, mconfig->d0i3_caps);
  160. return 0;
  161. }
  162. static int skl_get_format(struct snd_pcm_substream *substream,
  163. struct snd_soc_dai *dai)
  164. {
  165. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  166. struct skl_dma_params *dma_params;
  167. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  168. int format_val = 0;
  169. if ((ebus_to_hbus(ebus))->ppcap) {
  170. struct snd_pcm_runtime *runtime = substream->runtime;
  171. format_val = snd_hdac_calc_stream_format(runtime->rate,
  172. runtime->channels,
  173. runtime->format,
  174. 32, 0);
  175. } else {
  176. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  177. dma_params = snd_soc_dai_get_dma_data(codec_dai, substream);
  178. if (dma_params)
  179. format_val = dma_params->format;
  180. }
  181. return format_val;
  182. }
  183. static int skl_be_prepare(struct snd_pcm_substream *substream,
  184. struct snd_soc_dai *dai)
  185. {
  186. struct skl *skl = get_skl_ctx(dai->dev);
  187. struct skl_sst *ctx = skl->skl_sst;
  188. struct skl_module_cfg *mconfig;
  189. if (dai->playback_widget->power || dai->capture_widget->power)
  190. return 0;
  191. mconfig = skl_tplg_be_get_cpr_module(dai, substream->stream);
  192. if (mconfig == NULL)
  193. return -EINVAL;
  194. return skl_dsp_set_dma_control(ctx, mconfig);
  195. }
  196. static int skl_pcm_prepare(struct snd_pcm_substream *substream,
  197. struct snd_soc_dai *dai)
  198. {
  199. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  200. struct skl *skl = get_skl_ctx(dai->dev);
  201. unsigned int format_val;
  202. int err;
  203. struct skl_module_cfg *mconfig;
  204. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  205. mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
  206. format_val = skl_get_format(substream, dai);
  207. dev_dbg(dai->dev, "stream_tag=%d formatvalue=%d\n",
  208. hdac_stream(stream)->stream_tag, format_val);
  209. snd_hdac_stream_reset(hdac_stream(stream));
  210. /* In case of XRUN recovery, reset the FW pipe to clean state */
  211. if (mconfig && (substream->runtime->status->state ==
  212. SNDRV_PCM_STATE_XRUN))
  213. skl_reset_pipe(skl->skl_sst, mconfig->pipe);
  214. err = snd_hdac_stream_set_params(hdac_stream(stream), format_val);
  215. if (err < 0)
  216. return err;
  217. err = snd_hdac_stream_setup(hdac_stream(stream));
  218. if (err < 0)
  219. return err;
  220. hdac_stream(stream)->prepared = 1;
  221. return err;
  222. }
  223. static int skl_pcm_hw_params(struct snd_pcm_substream *substream,
  224. struct snd_pcm_hw_params *params,
  225. struct snd_soc_dai *dai)
  226. {
  227. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  228. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  229. struct snd_pcm_runtime *runtime = substream->runtime;
  230. struct skl_pipe_params p_params = {0};
  231. struct skl_module_cfg *m_cfg;
  232. int ret, dma_id;
  233. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  234. ret = skl_substream_alloc_pages(ebus, substream,
  235. params_buffer_bytes(params));
  236. if (ret < 0)
  237. return ret;
  238. dev_dbg(dai->dev, "format_val, rate=%d, ch=%d, format=%d\n",
  239. runtime->rate, runtime->channels, runtime->format);
  240. dma_id = hdac_stream(stream)->stream_tag - 1;
  241. dev_dbg(dai->dev, "dma_id=%d\n", dma_id);
  242. p_params.s_fmt = snd_pcm_format_width(params_format(params));
  243. p_params.ch = params_channels(params);
  244. p_params.s_freq = params_rate(params);
  245. p_params.host_dma_id = dma_id;
  246. p_params.stream = substream->stream;
  247. m_cfg = skl_tplg_fe_get_cpr_module(dai, p_params.stream);
  248. if (m_cfg)
  249. skl_tplg_update_pipe_params(dai->dev, m_cfg, &p_params);
  250. return 0;
  251. }
  252. static void skl_pcm_close(struct snd_pcm_substream *substream,
  253. struct snd_soc_dai *dai)
  254. {
  255. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  256. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  257. struct skl_dma_params *dma_params = NULL;
  258. struct skl *skl = ebus_to_skl(ebus);
  259. struct skl_module_cfg *mconfig;
  260. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  261. snd_hdac_ext_stream_release(stream, skl_get_host_stream_type(ebus));
  262. dma_params = snd_soc_dai_get_dma_data(dai, substream);
  263. /*
  264. * now we should set this to NULL as we are freeing by the
  265. * dma_params
  266. */
  267. snd_soc_dai_set_dma_data(dai, substream, NULL);
  268. skl_set_suspend_active(substream, dai, false);
  269. /*
  270. * check if close is for "Reference Pin" and set back the
  271. * CGCTL.MISCBDCGE if disabled by driver
  272. */
  273. if (!strncmp(dai->name, "Reference Pin", 13) &&
  274. skl->skl_sst->miscbdcg_disabled) {
  275. skl->skl_sst->enable_miscbdcge(dai->dev, true);
  276. skl->skl_sst->miscbdcg_disabled = false;
  277. }
  278. mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
  279. skl_tplg_d0i3_put(skl, mconfig->d0i3_caps);
  280. kfree(dma_params);
  281. }
  282. static int skl_pcm_hw_free(struct snd_pcm_substream *substream,
  283. struct snd_soc_dai *dai)
  284. {
  285. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  286. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  287. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  288. snd_hdac_stream_cleanup(hdac_stream(stream));
  289. hdac_stream(stream)->prepared = 0;
  290. return skl_substream_free_pages(ebus_to_hbus(ebus), substream);
  291. }
  292. static int skl_be_hw_params(struct snd_pcm_substream *substream,
  293. struct snd_pcm_hw_params *params,
  294. struct snd_soc_dai *dai)
  295. {
  296. struct skl_pipe_params p_params = {0};
  297. p_params.s_fmt = snd_pcm_format_width(params_format(params));
  298. p_params.ch = params_channels(params);
  299. p_params.s_freq = params_rate(params);
  300. p_params.stream = substream->stream;
  301. return skl_tplg_be_update_params(dai, &p_params);
  302. }
  303. static int skl_decoupled_trigger(struct snd_pcm_substream *substream,
  304. int cmd)
  305. {
  306. struct hdac_ext_bus *ebus = get_bus_ctx(substream);
  307. struct hdac_bus *bus = ebus_to_hbus(ebus);
  308. struct hdac_ext_stream *stream;
  309. int start;
  310. unsigned long cookie;
  311. struct hdac_stream *hstr;
  312. stream = get_hdac_ext_stream(substream);
  313. hstr = hdac_stream(stream);
  314. if (!hstr->prepared)
  315. return -EPIPE;
  316. switch (cmd) {
  317. case SNDRV_PCM_TRIGGER_START:
  318. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  319. case SNDRV_PCM_TRIGGER_RESUME:
  320. start = 1;
  321. break;
  322. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  323. case SNDRV_PCM_TRIGGER_SUSPEND:
  324. case SNDRV_PCM_TRIGGER_STOP:
  325. start = 0;
  326. break;
  327. default:
  328. return -EINVAL;
  329. }
  330. spin_lock_irqsave(&bus->reg_lock, cookie);
  331. if (start) {
  332. snd_hdac_stream_start(hdac_stream(stream), true);
  333. snd_hdac_stream_timecounter_init(hstr, 0);
  334. } else {
  335. snd_hdac_stream_stop(hdac_stream(stream));
  336. }
  337. spin_unlock_irqrestore(&bus->reg_lock, cookie);
  338. return 0;
  339. }
  340. static int skl_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
  341. struct snd_soc_dai *dai)
  342. {
  343. struct skl *skl = get_skl_ctx(dai->dev);
  344. struct skl_sst *ctx = skl->skl_sst;
  345. struct skl_module_cfg *mconfig;
  346. struct hdac_ext_bus *ebus = get_bus_ctx(substream);
  347. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  348. struct snd_soc_dapm_widget *w;
  349. int ret;
  350. mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
  351. if (!mconfig)
  352. return -EIO;
  353. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  354. w = dai->playback_widget;
  355. else
  356. w = dai->capture_widget;
  357. switch (cmd) {
  358. case SNDRV_PCM_TRIGGER_RESUME:
  359. if (!w->ignore_suspend) {
  360. skl_pcm_prepare(substream, dai);
  361. /*
  362. * enable DMA Resume enable bit for the stream, set the
  363. * dpib & lpib position to resume before starting the
  364. * DMA
  365. */
  366. snd_hdac_ext_stream_drsm_enable(ebus, true,
  367. hdac_stream(stream)->index);
  368. snd_hdac_ext_stream_set_dpibr(ebus, stream,
  369. stream->dpib);
  370. snd_hdac_ext_stream_set_lpib(stream, stream->lpib);
  371. }
  372. case SNDRV_PCM_TRIGGER_START:
  373. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  374. /*
  375. * Start HOST DMA and Start FE Pipe.This is to make sure that
  376. * there are no underrun/overrun in the case when the FE
  377. * pipeline is started but there is a delay in starting the
  378. * DMA channel on the host.
  379. */
  380. snd_hdac_ext_stream_decouple(ebus, stream, true);
  381. ret = skl_decoupled_trigger(substream, cmd);
  382. if (ret < 0)
  383. return ret;
  384. return skl_run_pipe(ctx, mconfig->pipe);
  385. break;
  386. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  387. case SNDRV_PCM_TRIGGER_SUSPEND:
  388. case SNDRV_PCM_TRIGGER_STOP:
  389. /*
  390. * Stop FE Pipe first and stop DMA. This is to make sure that
  391. * there are no underrun/overrun in the case if there is a delay
  392. * between the two operations.
  393. */
  394. ret = skl_stop_pipe(ctx, mconfig->pipe);
  395. if (ret < 0)
  396. return ret;
  397. ret = skl_decoupled_trigger(substream, cmd);
  398. if ((cmd == SNDRV_PCM_TRIGGER_SUSPEND) && !w->ignore_suspend) {
  399. /* save the dpib and lpib positions */
  400. stream->dpib = readl(ebus->bus.remap_addr +
  401. AZX_REG_VS_SDXDPIB_XBASE +
  402. (AZX_REG_VS_SDXDPIB_XINTERVAL *
  403. hdac_stream(stream)->index));
  404. stream->lpib = snd_hdac_stream_get_pos_lpib(
  405. hdac_stream(stream));
  406. snd_hdac_ext_stream_decouple(ebus, stream, false);
  407. }
  408. break;
  409. default:
  410. return -EINVAL;
  411. }
  412. return 0;
  413. }
  414. static int skl_link_hw_params(struct snd_pcm_substream *substream,
  415. struct snd_pcm_hw_params *params,
  416. struct snd_soc_dai *dai)
  417. {
  418. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  419. struct hdac_ext_stream *link_dev;
  420. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  421. struct hdac_ext_dma_params *dma_params;
  422. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  423. struct skl_pipe_params p_params = {0};
  424. link_dev = snd_hdac_ext_stream_assign(ebus, substream,
  425. HDAC_EXT_STREAM_TYPE_LINK);
  426. if (!link_dev)
  427. return -EBUSY;
  428. snd_soc_dai_set_dma_data(dai, substream, (void *)link_dev);
  429. /* set the stream tag in the codec dai dma params */
  430. dma_params = snd_soc_dai_get_dma_data(codec_dai, substream);
  431. if (dma_params)
  432. dma_params->stream_tag = hdac_stream(link_dev)->stream_tag;
  433. p_params.s_fmt = snd_pcm_format_width(params_format(params));
  434. p_params.ch = params_channels(params);
  435. p_params.s_freq = params_rate(params);
  436. p_params.stream = substream->stream;
  437. p_params.link_dma_id = hdac_stream(link_dev)->stream_tag - 1;
  438. return skl_tplg_be_update_params(dai, &p_params);
  439. }
  440. static int skl_link_pcm_prepare(struct snd_pcm_substream *substream,
  441. struct snd_soc_dai *dai)
  442. {
  443. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  444. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  445. struct hdac_ext_stream *link_dev =
  446. snd_soc_dai_get_dma_data(dai, substream);
  447. unsigned int format_val = 0;
  448. struct skl_dma_params *dma_params;
  449. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  450. struct hdac_ext_link *link;
  451. struct skl *skl = get_skl_ctx(dai->dev);
  452. struct skl_module_cfg *mconfig = NULL;
  453. dma_params = (struct skl_dma_params *)
  454. snd_soc_dai_get_dma_data(codec_dai, substream);
  455. if (dma_params)
  456. format_val = dma_params->format;
  457. dev_dbg(dai->dev, "stream_tag=%d formatvalue=%d codec_dai_name=%s\n",
  458. hdac_stream(link_dev)->stream_tag, format_val, codec_dai->name);
  459. link = snd_hdac_ext_bus_get_link(ebus, rtd->codec->component.name);
  460. if (!link)
  461. return -EINVAL;
  462. snd_hdac_ext_link_stream_reset(link_dev);
  463. /* In case of XRUN recovery, reset the FW pipe to clean state */
  464. mconfig = skl_tplg_be_get_cpr_module(dai, substream->stream);
  465. if (mconfig && (substream->runtime->status->state ==
  466. SNDRV_PCM_STATE_XRUN))
  467. skl_reset_pipe(skl->skl_sst, mconfig->pipe);
  468. snd_hdac_ext_link_stream_setup(link_dev, format_val);
  469. snd_hdac_ext_link_set_stream_id(link, hdac_stream(link_dev)->stream_tag);
  470. link_dev->link_prepared = 1;
  471. return 0;
  472. }
  473. static int skl_link_pcm_trigger(struct snd_pcm_substream *substream,
  474. int cmd, struct snd_soc_dai *dai)
  475. {
  476. struct hdac_ext_stream *link_dev =
  477. snd_soc_dai_get_dma_data(dai, substream);
  478. struct hdac_ext_bus *ebus = get_bus_ctx(substream);
  479. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  480. dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd);
  481. switch (cmd) {
  482. case SNDRV_PCM_TRIGGER_RESUME:
  483. skl_link_pcm_prepare(substream, dai);
  484. case SNDRV_PCM_TRIGGER_START:
  485. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  486. snd_hdac_ext_stream_decouple(ebus, stream, true);
  487. snd_hdac_ext_link_stream_start(link_dev);
  488. break;
  489. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  490. case SNDRV_PCM_TRIGGER_SUSPEND:
  491. case SNDRV_PCM_TRIGGER_STOP:
  492. snd_hdac_ext_link_stream_clear(link_dev);
  493. if (cmd == SNDRV_PCM_TRIGGER_SUSPEND)
  494. snd_hdac_ext_stream_decouple(ebus, stream, false);
  495. break;
  496. default:
  497. return -EINVAL;
  498. }
  499. return 0;
  500. }
  501. static int skl_link_hw_free(struct snd_pcm_substream *substream,
  502. struct snd_soc_dai *dai)
  503. {
  504. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  505. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  506. struct hdac_ext_stream *link_dev =
  507. snd_soc_dai_get_dma_data(dai, substream);
  508. struct hdac_ext_link *link;
  509. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  510. link_dev->link_prepared = 0;
  511. link = snd_hdac_ext_bus_get_link(ebus, rtd->codec->component.name);
  512. if (!link)
  513. return -EINVAL;
  514. snd_hdac_ext_link_clear_stream_id(link, hdac_stream(link_dev)->stream_tag);
  515. snd_hdac_ext_stream_release(link_dev, HDAC_EXT_STREAM_TYPE_LINK);
  516. return 0;
  517. }
  518. static struct snd_soc_dai_ops skl_pcm_dai_ops = {
  519. .startup = skl_pcm_open,
  520. .shutdown = skl_pcm_close,
  521. .prepare = skl_pcm_prepare,
  522. .hw_params = skl_pcm_hw_params,
  523. .hw_free = skl_pcm_hw_free,
  524. .trigger = skl_pcm_trigger,
  525. };
  526. static struct snd_soc_dai_ops skl_dmic_dai_ops = {
  527. .hw_params = skl_be_hw_params,
  528. };
  529. static struct snd_soc_dai_ops skl_be_ssp_dai_ops = {
  530. .hw_params = skl_be_hw_params,
  531. .prepare = skl_be_prepare,
  532. };
  533. static struct snd_soc_dai_ops skl_link_dai_ops = {
  534. .prepare = skl_link_pcm_prepare,
  535. .hw_params = skl_link_hw_params,
  536. .hw_free = skl_link_hw_free,
  537. .trigger = skl_link_pcm_trigger,
  538. };
  539. static struct snd_soc_dai_driver skl_platform_dai[] = {
  540. {
  541. .name = "System Pin",
  542. .ops = &skl_pcm_dai_ops,
  543. .playback = {
  544. .stream_name = "System Playback",
  545. .channels_min = HDA_MONO,
  546. .channels_max = HDA_STEREO,
  547. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_8000,
  548. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  549. SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE,
  550. },
  551. .capture = {
  552. .stream_name = "System Capture",
  553. .channels_min = HDA_MONO,
  554. .channels_max = HDA_STEREO,
  555. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
  556. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  557. },
  558. },
  559. {
  560. .name = "Reference Pin",
  561. .ops = &skl_pcm_dai_ops,
  562. .capture = {
  563. .stream_name = "Reference Capture",
  564. .channels_min = HDA_MONO,
  565. .channels_max = HDA_QUAD,
  566. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
  567. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  568. },
  569. },
  570. {
  571. .name = "Deepbuffer Pin",
  572. .ops = &skl_pcm_dai_ops,
  573. .playback = {
  574. .stream_name = "Deepbuffer Playback",
  575. .channels_min = HDA_STEREO,
  576. .channels_max = HDA_STEREO,
  577. .rates = SNDRV_PCM_RATE_48000,
  578. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  579. },
  580. },
  581. {
  582. .name = "LowLatency Pin",
  583. .ops = &skl_pcm_dai_ops,
  584. .playback = {
  585. .stream_name = "Low Latency Playback",
  586. .channels_min = HDA_STEREO,
  587. .channels_max = HDA_STEREO,
  588. .rates = SNDRV_PCM_RATE_48000,
  589. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  590. },
  591. },
  592. {
  593. .name = "DMIC Pin",
  594. .ops = &skl_pcm_dai_ops,
  595. .capture = {
  596. .stream_name = "DMIC Capture",
  597. .channels_min = HDA_MONO,
  598. .channels_max = HDA_QUAD,
  599. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
  600. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  601. },
  602. },
  603. {
  604. .name = "HDMI1 Pin",
  605. .ops = &skl_pcm_dai_ops,
  606. .playback = {
  607. .stream_name = "HDMI1 Playback",
  608. .channels_min = HDA_STEREO,
  609. .channels_max = 8,
  610. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  611. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  612. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  613. SNDRV_PCM_RATE_192000,
  614. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  615. SNDRV_PCM_FMTBIT_S32_LE,
  616. },
  617. },
  618. {
  619. .name = "HDMI2 Pin",
  620. .ops = &skl_pcm_dai_ops,
  621. .playback = {
  622. .stream_name = "HDMI2 Playback",
  623. .channels_min = HDA_STEREO,
  624. .channels_max = 8,
  625. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  626. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  627. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  628. SNDRV_PCM_RATE_192000,
  629. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  630. SNDRV_PCM_FMTBIT_S32_LE,
  631. },
  632. },
  633. {
  634. .name = "HDMI3 Pin",
  635. .ops = &skl_pcm_dai_ops,
  636. .playback = {
  637. .stream_name = "HDMI3 Playback",
  638. .channels_min = HDA_STEREO,
  639. .channels_max = 8,
  640. .rates = SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 |
  641. SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200 |
  642. SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_176400 |
  643. SNDRV_PCM_RATE_192000,
  644. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE |
  645. SNDRV_PCM_FMTBIT_S32_LE,
  646. },
  647. },
  648. /* BE CPU Dais */
  649. {
  650. .name = "SSP0 Pin",
  651. .ops = &skl_be_ssp_dai_ops,
  652. .playback = {
  653. .stream_name = "ssp0 Tx",
  654. .channels_min = HDA_STEREO,
  655. .channels_max = HDA_STEREO,
  656. .rates = SNDRV_PCM_RATE_48000,
  657. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  658. },
  659. .capture = {
  660. .stream_name = "ssp0 Rx",
  661. .channels_min = HDA_STEREO,
  662. .channels_max = HDA_STEREO,
  663. .rates = SNDRV_PCM_RATE_48000,
  664. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  665. },
  666. },
  667. {
  668. .name = "SSP1 Pin",
  669. .ops = &skl_be_ssp_dai_ops,
  670. .playback = {
  671. .stream_name = "ssp1 Tx",
  672. .channels_min = HDA_STEREO,
  673. .channels_max = HDA_STEREO,
  674. .rates = SNDRV_PCM_RATE_48000,
  675. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  676. },
  677. .capture = {
  678. .stream_name = "ssp1 Rx",
  679. .channels_min = HDA_STEREO,
  680. .channels_max = HDA_STEREO,
  681. .rates = SNDRV_PCM_RATE_48000,
  682. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  683. },
  684. },
  685. {
  686. .name = "SSP2 Pin",
  687. .ops = &skl_be_ssp_dai_ops,
  688. .playback = {
  689. .stream_name = "ssp2 Tx",
  690. .channels_min = HDA_STEREO,
  691. .channels_max = HDA_STEREO,
  692. .rates = SNDRV_PCM_RATE_48000,
  693. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  694. },
  695. .capture = {
  696. .stream_name = "ssp2 Rx",
  697. .channels_min = HDA_STEREO,
  698. .channels_max = HDA_STEREO,
  699. .rates = SNDRV_PCM_RATE_48000,
  700. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  701. },
  702. },
  703. {
  704. .name = "SSP3 Pin",
  705. .ops = &skl_be_ssp_dai_ops,
  706. .playback = {
  707. .stream_name = "ssp3 Tx",
  708. .channels_min = HDA_STEREO,
  709. .channels_max = HDA_STEREO,
  710. .rates = SNDRV_PCM_RATE_48000,
  711. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  712. },
  713. .capture = {
  714. .stream_name = "ssp3 Rx",
  715. .channels_min = HDA_STEREO,
  716. .channels_max = HDA_STEREO,
  717. .rates = SNDRV_PCM_RATE_48000,
  718. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  719. },
  720. },
  721. {
  722. .name = "SSP4 Pin",
  723. .ops = &skl_be_ssp_dai_ops,
  724. .playback = {
  725. .stream_name = "ssp4 Tx",
  726. .channels_min = HDA_STEREO,
  727. .channels_max = HDA_STEREO,
  728. .rates = SNDRV_PCM_RATE_48000,
  729. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  730. },
  731. .capture = {
  732. .stream_name = "ssp4 Rx",
  733. .channels_min = HDA_STEREO,
  734. .channels_max = HDA_STEREO,
  735. .rates = SNDRV_PCM_RATE_48000,
  736. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  737. },
  738. },
  739. {
  740. .name = "SSP5 Pin",
  741. .ops = &skl_be_ssp_dai_ops,
  742. .playback = {
  743. .stream_name = "ssp5 Tx",
  744. .channels_min = HDA_STEREO,
  745. .channels_max = HDA_STEREO,
  746. .rates = SNDRV_PCM_RATE_48000,
  747. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  748. },
  749. .capture = {
  750. .stream_name = "ssp5 Rx",
  751. .channels_min = HDA_STEREO,
  752. .channels_max = HDA_STEREO,
  753. .rates = SNDRV_PCM_RATE_48000,
  754. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  755. },
  756. },
  757. {
  758. .name = "iDisp1 Pin",
  759. .ops = &skl_link_dai_ops,
  760. .playback = {
  761. .stream_name = "iDisp1 Tx",
  762. .channels_min = HDA_STEREO,
  763. .channels_max = 8,
  764. .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000,
  765. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE |
  766. SNDRV_PCM_FMTBIT_S24_LE,
  767. },
  768. },
  769. {
  770. .name = "iDisp2 Pin",
  771. .ops = &skl_link_dai_ops,
  772. .playback = {
  773. .stream_name = "iDisp2 Tx",
  774. .channels_min = HDA_STEREO,
  775. .channels_max = 8,
  776. .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|
  777. SNDRV_PCM_RATE_48000,
  778. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE |
  779. SNDRV_PCM_FMTBIT_S24_LE,
  780. },
  781. },
  782. {
  783. .name = "iDisp3 Pin",
  784. .ops = &skl_link_dai_ops,
  785. .playback = {
  786. .stream_name = "iDisp3 Tx",
  787. .channels_min = HDA_STEREO,
  788. .channels_max = 8,
  789. .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|
  790. SNDRV_PCM_RATE_48000,
  791. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE |
  792. SNDRV_PCM_FMTBIT_S24_LE,
  793. },
  794. },
  795. {
  796. .name = "DMIC01 Pin",
  797. .ops = &skl_dmic_dai_ops,
  798. .capture = {
  799. .stream_name = "DMIC01 Rx",
  800. .channels_min = HDA_MONO,
  801. .channels_max = HDA_QUAD,
  802. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
  803. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  804. },
  805. },
  806. {
  807. .name = "HD-Codec Pin",
  808. .ops = &skl_link_dai_ops,
  809. .playback = {
  810. .stream_name = "HD-Codec Tx",
  811. .channels_min = HDA_STEREO,
  812. .channels_max = HDA_STEREO,
  813. .rates = SNDRV_PCM_RATE_48000,
  814. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  815. },
  816. .capture = {
  817. .stream_name = "HD-Codec Rx",
  818. .channels_min = HDA_STEREO,
  819. .channels_max = HDA_STEREO,
  820. .rates = SNDRV_PCM_RATE_48000,
  821. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  822. },
  823. },
  824. };
  825. static int skl_platform_open(struct snd_pcm_substream *substream)
  826. {
  827. struct snd_pcm_runtime *runtime;
  828. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  829. struct snd_soc_dai_link *dai_link = rtd->dai_link;
  830. dev_dbg(rtd->cpu_dai->dev, "In %s:%s\n", __func__,
  831. dai_link->cpu_dai_name);
  832. runtime = substream->runtime;
  833. snd_soc_set_runtime_hwparams(substream, &azx_pcm_hw);
  834. return 0;
  835. }
  836. static int skl_coupled_trigger(struct snd_pcm_substream *substream,
  837. int cmd)
  838. {
  839. struct hdac_ext_bus *ebus = get_bus_ctx(substream);
  840. struct hdac_bus *bus = ebus_to_hbus(ebus);
  841. struct hdac_ext_stream *stream;
  842. struct snd_pcm_substream *s;
  843. bool start;
  844. int sbits = 0;
  845. unsigned long cookie;
  846. struct hdac_stream *hstr;
  847. stream = get_hdac_ext_stream(substream);
  848. hstr = hdac_stream(stream);
  849. dev_dbg(bus->dev, "In %s cmd=%d\n", __func__, cmd);
  850. if (!hstr->prepared)
  851. return -EPIPE;
  852. switch (cmd) {
  853. case SNDRV_PCM_TRIGGER_START:
  854. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  855. case SNDRV_PCM_TRIGGER_RESUME:
  856. start = true;
  857. break;
  858. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  859. case SNDRV_PCM_TRIGGER_SUSPEND:
  860. case SNDRV_PCM_TRIGGER_STOP:
  861. start = false;
  862. break;
  863. default:
  864. return -EINVAL;
  865. }
  866. snd_pcm_group_for_each_entry(s, substream) {
  867. if (s->pcm->card != substream->pcm->card)
  868. continue;
  869. stream = get_hdac_ext_stream(s);
  870. sbits |= 1 << hdac_stream(stream)->index;
  871. snd_pcm_trigger_done(s, substream);
  872. }
  873. spin_lock_irqsave(&bus->reg_lock, cookie);
  874. /* first, set SYNC bits of corresponding streams */
  875. snd_hdac_stream_sync_trigger(hstr, true, sbits, AZX_REG_SSYNC);
  876. snd_pcm_group_for_each_entry(s, substream) {
  877. if (s->pcm->card != substream->pcm->card)
  878. continue;
  879. stream = get_hdac_ext_stream(s);
  880. if (start)
  881. snd_hdac_stream_start(hdac_stream(stream), true);
  882. else
  883. snd_hdac_stream_stop(hdac_stream(stream));
  884. }
  885. spin_unlock_irqrestore(&bus->reg_lock, cookie);
  886. snd_hdac_stream_sync(hstr, start, sbits);
  887. spin_lock_irqsave(&bus->reg_lock, cookie);
  888. /* reset SYNC bits */
  889. snd_hdac_stream_sync_trigger(hstr, false, sbits, AZX_REG_SSYNC);
  890. if (start)
  891. snd_hdac_stream_timecounter_init(hstr, sbits);
  892. spin_unlock_irqrestore(&bus->reg_lock, cookie);
  893. return 0;
  894. }
  895. static int skl_platform_pcm_trigger(struct snd_pcm_substream *substream,
  896. int cmd)
  897. {
  898. struct hdac_ext_bus *ebus = get_bus_ctx(substream);
  899. if (!(ebus_to_hbus(ebus))->ppcap)
  900. return skl_coupled_trigger(substream, cmd);
  901. return 0;
  902. }
  903. static snd_pcm_uframes_t skl_platform_pcm_pointer
  904. (struct snd_pcm_substream *substream)
  905. {
  906. struct hdac_ext_stream *hstream = get_hdac_ext_stream(substream);
  907. struct hdac_ext_bus *ebus = get_bus_ctx(substream);
  908. unsigned int pos;
  909. /*
  910. * Use DPIB for Playback stream as the periodic DMA Position-in-
  911. * Buffer Writes may be scheduled at the same time or later than
  912. * the MSI and does not guarantee to reflect the Position of the
  913. * last buffer that was transferred. Whereas DPIB register in
  914. * HAD space reflects the actual data that is transferred.
  915. * Use the position buffer for capture, as DPIB write gets
  916. * completed earlier than the actual data written to the DDR.
  917. */
  918. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  919. pos = readl(ebus->bus.remap_addr + AZX_REG_VS_SDXDPIB_XBASE +
  920. (AZX_REG_VS_SDXDPIB_XINTERVAL *
  921. hdac_stream(hstream)->index));
  922. else
  923. pos = snd_hdac_stream_get_pos_posbuf(hdac_stream(hstream));
  924. if (pos >= hdac_stream(hstream)->bufsize)
  925. pos = 0;
  926. return bytes_to_frames(substream->runtime, pos);
  927. }
  928. static u64 skl_adjust_codec_delay(struct snd_pcm_substream *substream,
  929. u64 nsec)
  930. {
  931. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  932. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  933. u64 codec_frames, codec_nsecs;
  934. if (!codec_dai->driver->ops->delay)
  935. return nsec;
  936. codec_frames = codec_dai->driver->ops->delay(substream, codec_dai);
  937. codec_nsecs = div_u64(codec_frames * 1000000000LL,
  938. substream->runtime->rate);
  939. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  940. return nsec + codec_nsecs;
  941. return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
  942. }
  943. static int skl_get_time_info(struct snd_pcm_substream *substream,
  944. struct timespec *system_ts, struct timespec *audio_ts,
  945. struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
  946. struct snd_pcm_audio_tstamp_report *audio_tstamp_report)
  947. {
  948. struct hdac_ext_stream *sstream = get_hdac_ext_stream(substream);
  949. struct hdac_stream *hstr = hdac_stream(sstream);
  950. u64 nsec;
  951. if ((substream->runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_ATIME) &&
  952. (audio_tstamp_config->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK)) {
  953. snd_pcm_gettime(substream->runtime, system_ts);
  954. nsec = timecounter_read(&hstr->tc);
  955. nsec = div_u64(nsec, 3); /* can be optimized */
  956. if (audio_tstamp_config->report_delay)
  957. nsec = skl_adjust_codec_delay(substream, nsec);
  958. *audio_ts = ns_to_timespec(nsec);
  959. audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
  960. audio_tstamp_report->accuracy_report = 1; /* rest of struct is valid */
  961. audio_tstamp_report->accuracy = 42; /* 24MHzWallClk == 42ns resolution */
  962. } else {
  963. audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
  964. }
  965. return 0;
  966. }
  967. static const struct snd_pcm_ops skl_platform_ops = {
  968. .open = skl_platform_open,
  969. .ioctl = snd_pcm_lib_ioctl,
  970. .trigger = skl_platform_pcm_trigger,
  971. .pointer = skl_platform_pcm_pointer,
  972. .get_time_info = skl_get_time_info,
  973. .mmap = snd_pcm_lib_default_mmap,
  974. .page = snd_pcm_sgbuf_ops_page,
  975. };
  976. static void skl_pcm_free(struct snd_pcm *pcm)
  977. {
  978. snd_pcm_lib_preallocate_free_for_all(pcm);
  979. }
  980. #define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
  981. static int skl_pcm_new(struct snd_soc_pcm_runtime *rtd)
  982. {
  983. struct snd_soc_dai *dai = rtd->cpu_dai;
  984. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  985. struct snd_pcm *pcm = rtd->pcm;
  986. unsigned int size;
  987. int retval = 0;
  988. struct skl *skl = ebus_to_skl(ebus);
  989. if (dai->driver->playback.channels_min ||
  990. dai->driver->capture.channels_min) {
  991. /* buffer pre-allocation */
  992. size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
  993. if (size > MAX_PREALLOC_SIZE)
  994. size = MAX_PREALLOC_SIZE;
  995. retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
  996. SNDRV_DMA_TYPE_DEV_SG,
  997. snd_dma_pci_data(skl->pci),
  998. size, MAX_PREALLOC_SIZE);
  999. if (retval) {
  1000. dev_err(dai->dev, "dma buffer allocationf fail\n");
  1001. return retval;
  1002. }
  1003. }
  1004. return retval;
  1005. }
  1006. static int skl_populate_modules(struct skl *skl)
  1007. {
  1008. struct skl_pipeline *p;
  1009. struct skl_pipe_module *m;
  1010. struct snd_soc_dapm_widget *w;
  1011. struct skl_module_cfg *mconfig;
  1012. int ret;
  1013. list_for_each_entry(p, &skl->ppl_list, node) {
  1014. list_for_each_entry(m, &p->pipe->w_list, node) {
  1015. w = m->w;
  1016. mconfig = w->priv;
  1017. ret = snd_skl_get_module_info(skl->skl_sst, mconfig);
  1018. if (ret < 0) {
  1019. dev_err(skl->skl_sst->dev,
  1020. "query module info failed:%d\n", ret);
  1021. goto err;
  1022. }
  1023. }
  1024. }
  1025. err:
  1026. return ret;
  1027. }
  1028. static int skl_platform_soc_probe(struct snd_soc_platform *platform)
  1029. {
  1030. struct hdac_ext_bus *ebus = dev_get_drvdata(platform->dev);
  1031. struct skl *skl = ebus_to_skl(ebus);
  1032. const struct skl_dsp_ops *ops;
  1033. int ret;
  1034. pm_runtime_get_sync(platform->dev);
  1035. if ((ebus_to_hbus(ebus))->ppcap) {
  1036. ret = skl_tplg_init(platform, ebus);
  1037. if (ret < 0) {
  1038. dev_err(platform->dev, "Failed to init topology!\n");
  1039. return ret;
  1040. }
  1041. skl->platform = platform;
  1042. /* load the firmwares, since all is set */
  1043. ops = skl_get_dsp_ops(skl->pci->device);
  1044. if (!ops)
  1045. return -EIO;
  1046. if (skl->skl_sst->is_first_boot == false) {
  1047. dev_err(platform->dev, "DSP reports first boot done!!!\n");
  1048. return -EIO;
  1049. }
  1050. ret = ops->init_fw(platform->dev, skl->skl_sst);
  1051. if (ret < 0) {
  1052. dev_err(platform->dev, "Failed to boot first fw: %d\n", ret);
  1053. return ret;
  1054. }
  1055. skl_populate_modules(skl);
  1056. skl->skl_sst->update_d0i3c = skl_update_d0i3c;
  1057. }
  1058. pm_runtime_mark_last_busy(platform->dev);
  1059. pm_runtime_put_autosuspend(platform->dev);
  1060. return 0;
  1061. }
  1062. static struct snd_soc_platform_driver skl_platform_drv = {
  1063. .probe = skl_platform_soc_probe,
  1064. .ops = &skl_platform_ops,
  1065. .pcm_new = skl_pcm_new,
  1066. .pcm_free = skl_pcm_free,
  1067. };
  1068. static const struct snd_soc_component_driver skl_component = {
  1069. .name = "pcm",
  1070. };
  1071. int skl_platform_register(struct device *dev)
  1072. {
  1073. int ret;
  1074. struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
  1075. struct skl *skl = ebus_to_skl(ebus);
  1076. INIT_LIST_HEAD(&skl->ppl_list);
  1077. ret = snd_soc_register_platform(dev, &skl_platform_drv);
  1078. if (ret) {
  1079. dev_err(dev, "soc platform registration failed %d\n", ret);
  1080. return ret;
  1081. }
  1082. ret = snd_soc_register_component(dev, &skl_component,
  1083. skl_platform_dai,
  1084. ARRAY_SIZE(skl_platform_dai));
  1085. if (ret) {
  1086. dev_err(dev, "soc component registration failed %d\n", ret);
  1087. snd_soc_unregister_platform(dev);
  1088. }
  1089. return ret;
  1090. }
  1091. int skl_platform_unregister(struct device *dev)
  1092. {
  1093. snd_soc_unregister_component(dev);
  1094. snd_soc_unregister_platform(dev);
  1095. return 0;
  1096. }