skl-pcm.c 39 KB

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