skl-pcm.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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. #define HDA_MONO 1
  28. #define HDA_STEREO 2
  29. static struct snd_pcm_hardware azx_pcm_hw = {
  30. .info = (SNDRV_PCM_INFO_MMAP |
  31. SNDRV_PCM_INFO_INTERLEAVED |
  32. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  33. SNDRV_PCM_INFO_MMAP_VALID |
  34. SNDRV_PCM_INFO_PAUSE |
  35. SNDRV_PCM_INFO_SYNC_START |
  36. SNDRV_PCM_INFO_HAS_WALL_CLOCK | /* legacy */
  37. SNDRV_PCM_INFO_HAS_LINK_ATIME |
  38. SNDRV_PCM_INFO_NO_PERIOD_WAKEUP),
  39. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  40. .rates = SNDRV_PCM_RATE_48000,
  41. .rate_min = 48000,
  42. .rate_max = 48000,
  43. .channels_min = 2,
  44. .channels_max = 2,
  45. .buffer_bytes_max = AZX_MAX_BUF_SIZE,
  46. .period_bytes_min = 128,
  47. .period_bytes_max = AZX_MAX_BUF_SIZE / 2,
  48. .periods_min = 2,
  49. .periods_max = AZX_MAX_FRAG,
  50. .fifo_size = 0,
  51. };
  52. static inline
  53. struct hdac_ext_stream *get_hdac_ext_stream(struct snd_pcm_substream *substream)
  54. {
  55. return substream->runtime->private_data;
  56. }
  57. static struct hdac_ext_bus *get_bus_ctx(struct snd_pcm_substream *substream)
  58. {
  59. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  60. struct hdac_stream *hstream = hdac_stream(stream);
  61. struct hdac_bus *bus = hstream->bus;
  62. return hbus_to_ebus(bus);
  63. }
  64. static int skl_substream_alloc_pages(struct hdac_ext_bus *ebus,
  65. struct snd_pcm_substream *substream,
  66. size_t size)
  67. {
  68. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  69. hdac_stream(stream)->bufsize = 0;
  70. hdac_stream(stream)->period_bytes = 0;
  71. hdac_stream(stream)->format_val = 0;
  72. return snd_pcm_lib_malloc_pages(substream, size);
  73. }
  74. static int skl_substream_free_pages(struct hdac_bus *bus,
  75. struct snd_pcm_substream *substream)
  76. {
  77. return snd_pcm_lib_free_pages(substream);
  78. }
  79. static void skl_set_pcm_constrains(struct hdac_ext_bus *ebus,
  80. struct snd_pcm_runtime *runtime)
  81. {
  82. snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS);
  83. /* avoid wrap-around with wall-clock */
  84. snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_BUFFER_TIME,
  85. 20, 178000000);
  86. }
  87. static enum hdac_ext_stream_type skl_get_host_stream_type(struct hdac_ext_bus *ebus)
  88. {
  89. if (ebus->ppcap)
  90. return HDAC_EXT_STREAM_TYPE_HOST;
  91. else
  92. return HDAC_EXT_STREAM_TYPE_COUPLED;
  93. }
  94. static int skl_pcm_open(struct snd_pcm_substream *substream,
  95. struct snd_soc_dai *dai)
  96. {
  97. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  98. struct hdac_ext_stream *stream;
  99. struct snd_pcm_runtime *runtime = substream->runtime;
  100. struct skl_dma_params *dma_params;
  101. int ret;
  102. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  103. ret = pm_runtime_get_sync(dai->dev);
  104. if (ret < 0)
  105. return ret;
  106. stream = snd_hdac_ext_stream_assign(ebus, substream,
  107. skl_get_host_stream_type(ebus));
  108. if (stream == NULL)
  109. return -EBUSY;
  110. skl_set_pcm_constrains(ebus, runtime);
  111. /*
  112. * disable WALLCLOCK timestamps for capture streams
  113. * until we figure out how to handle digital inputs
  114. */
  115. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
  116. runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_WALL_CLOCK; /* legacy */
  117. runtime->hw.info &= ~SNDRV_PCM_INFO_HAS_LINK_ATIME;
  118. }
  119. runtime->private_data = stream;
  120. dma_params = kzalloc(sizeof(*dma_params), GFP_KERNEL);
  121. if (!dma_params)
  122. return -ENOMEM;
  123. dma_params->stream_tag = hdac_stream(stream)->stream_tag;
  124. snd_soc_dai_set_dma_data(dai, substream, dma_params);
  125. dev_dbg(dai->dev, "stream tag set in dma params=%d\n",
  126. dma_params->stream_tag);
  127. snd_pcm_set_sync(substream);
  128. return 0;
  129. }
  130. static int skl_get_format(struct snd_pcm_substream *substream,
  131. struct snd_soc_dai *dai)
  132. {
  133. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  134. struct skl_dma_params *dma_params;
  135. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  136. int format_val = 0;
  137. if (ebus->ppcap) {
  138. struct snd_pcm_runtime *runtime = substream->runtime;
  139. format_val = snd_hdac_calc_stream_format(runtime->rate,
  140. runtime->channels,
  141. runtime->format,
  142. 32, 0);
  143. } else {
  144. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  145. dma_params = snd_soc_dai_get_dma_data(codec_dai, substream);
  146. if (dma_params)
  147. format_val = dma_params->format;
  148. }
  149. return format_val;
  150. }
  151. static int skl_pcm_prepare(struct snd_pcm_substream *substream,
  152. struct snd_soc_dai *dai)
  153. {
  154. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  155. unsigned int format_val;
  156. int err;
  157. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  158. if (hdac_stream(stream)->prepared) {
  159. dev_dbg(dai->dev, "already stream is prepared - returning\n");
  160. return 0;
  161. }
  162. format_val = skl_get_format(substream, dai);
  163. dev_dbg(dai->dev, "stream_tag=%d formatvalue=%d\n",
  164. hdac_stream(stream)->stream_tag, format_val);
  165. snd_hdac_stream_reset(hdac_stream(stream));
  166. err = snd_hdac_stream_set_params(hdac_stream(stream), format_val);
  167. if (err < 0)
  168. return err;
  169. err = snd_hdac_stream_setup(hdac_stream(stream));
  170. if (err < 0)
  171. return err;
  172. hdac_stream(stream)->prepared = 1;
  173. return err;
  174. }
  175. static int skl_pcm_hw_params(struct snd_pcm_substream *substream,
  176. struct snd_pcm_hw_params *params,
  177. struct snd_soc_dai *dai)
  178. {
  179. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  180. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  181. struct snd_pcm_runtime *runtime = substream->runtime;
  182. struct skl_pipe_params p_params = {0};
  183. struct skl_module_cfg *m_cfg;
  184. int ret, dma_id;
  185. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  186. ret = skl_substream_alloc_pages(ebus, substream,
  187. params_buffer_bytes(params));
  188. if (ret < 0)
  189. return ret;
  190. dev_dbg(dai->dev, "format_val, rate=%d, ch=%d, format=%d\n",
  191. runtime->rate, runtime->channels, runtime->format);
  192. dma_id = hdac_stream(stream)->stream_tag - 1;
  193. dev_dbg(dai->dev, "dma_id=%d\n", dma_id);
  194. p_params.s_fmt = snd_pcm_format_width(params_format(params));
  195. p_params.ch = params_channels(params);
  196. p_params.s_freq = params_rate(params);
  197. p_params.host_dma_id = dma_id;
  198. p_params.stream = substream->stream;
  199. m_cfg = skl_tplg_fe_get_cpr_module(dai, p_params.stream);
  200. if (m_cfg)
  201. skl_tplg_update_pipe_params(dai->dev, m_cfg, &p_params);
  202. return 0;
  203. }
  204. static void skl_pcm_close(struct snd_pcm_substream *substream,
  205. struct snd_soc_dai *dai)
  206. {
  207. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  208. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  209. struct skl_dma_params *dma_params = NULL;
  210. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  211. snd_hdac_ext_stream_release(stream, skl_get_host_stream_type(ebus));
  212. dma_params = snd_soc_dai_get_dma_data(dai, substream);
  213. /*
  214. * now we should set this to NULL as we are freeing by the
  215. * dma_params
  216. */
  217. snd_soc_dai_set_dma_data(dai, substream, NULL);
  218. pm_runtime_mark_last_busy(dai->dev);
  219. pm_runtime_put_autosuspend(dai->dev);
  220. kfree(dma_params);
  221. }
  222. static int skl_pcm_hw_free(struct snd_pcm_substream *substream,
  223. struct snd_soc_dai *dai)
  224. {
  225. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  226. struct hdac_ext_stream *stream = get_hdac_ext_stream(substream);
  227. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  228. snd_hdac_stream_cleanup(hdac_stream(stream));
  229. hdac_stream(stream)->prepared = 0;
  230. return skl_substream_free_pages(ebus_to_hbus(ebus), substream);
  231. }
  232. static int skl_be_hw_params(struct snd_pcm_substream *substream,
  233. struct snd_pcm_hw_params *params,
  234. struct snd_soc_dai *dai)
  235. {
  236. struct skl_pipe_params p_params = {0};
  237. p_params.s_fmt = snd_pcm_format_width(params_format(params));
  238. p_params.ch = params_channels(params);
  239. p_params.s_freq = params_rate(params);
  240. p_params.stream = substream->stream;
  241. skl_tplg_be_update_params(dai, &p_params);
  242. return 0;
  243. }
  244. static int skl_pcm_trigger(struct snd_pcm_substream *substream, int cmd,
  245. struct snd_soc_dai *dai)
  246. {
  247. struct skl *skl = get_skl_ctx(dai->dev);
  248. struct skl_sst *ctx = skl->skl_sst;
  249. struct skl_module_cfg *mconfig;
  250. mconfig = skl_tplg_fe_get_cpr_module(dai, substream->stream);
  251. if (!mconfig)
  252. return -EIO;
  253. switch (cmd) {
  254. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  255. case SNDRV_PCM_TRIGGER_RESUME:
  256. return skl_run_pipe(ctx, mconfig->pipe);
  257. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  258. case SNDRV_PCM_TRIGGER_SUSPEND:
  259. return skl_stop_pipe(ctx, mconfig->pipe);
  260. default:
  261. return 0;
  262. }
  263. }
  264. static int skl_link_hw_params(struct snd_pcm_substream *substream,
  265. struct snd_pcm_hw_params *params,
  266. struct snd_soc_dai *dai)
  267. {
  268. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  269. struct hdac_ext_stream *link_dev;
  270. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  271. struct skl_dma_params *dma_params;
  272. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  273. struct skl_pipe_params p_params = {0};
  274. link_dev = snd_hdac_ext_stream_assign(ebus, substream,
  275. HDAC_EXT_STREAM_TYPE_LINK);
  276. if (!link_dev)
  277. return -EBUSY;
  278. snd_soc_dai_set_dma_data(dai, substream, (void *)link_dev);
  279. /* set the stream tag in the codec dai dma params */
  280. dma_params = (struct skl_dma_params *)
  281. snd_soc_dai_get_dma_data(codec_dai, substream);
  282. if (dma_params)
  283. dma_params->stream_tag = hdac_stream(link_dev)->stream_tag;
  284. snd_soc_dai_set_dma_data(codec_dai, substream, (void *)dma_params);
  285. p_params.s_fmt = snd_pcm_format_width(params_format(params));
  286. p_params.ch = params_channels(params);
  287. p_params.s_freq = params_rate(params);
  288. p_params.stream = substream->stream;
  289. p_params.link_dma_id = hdac_stream(link_dev)->stream_tag - 1;
  290. skl_tplg_be_update_params(dai, &p_params);
  291. return 0;
  292. }
  293. static int skl_link_pcm_prepare(struct snd_pcm_substream *substream,
  294. struct snd_soc_dai *dai)
  295. {
  296. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  297. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  298. struct hdac_ext_stream *link_dev =
  299. snd_soc_dai_get_dma_data(dai, substream);
  300. unsigned int format_val = 0;
  301. struct skl_dma_params *dma_params;
  302. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  303. struct hdac_ext_link *link;
  304. if (link_dev->link_prepared) {
  305. dev_dbg(dai->dev, "already stream is prepared - returning\n");
  306. return 0;
  307. }
  308. dma_params = (struct skl_dma_params *)
  309. snd_soc_dai_get_dma_data(codec_dai, substream);
  310. if (dma_params)
  311. format_val = dma_params->format;
  312. dev_dbg(dai->dev, "stream_tag=%d formatvalue=%d codec_dai_name=%s\n",
  313. hdac_stream(link_dev)->stream_tag, format_val, codec_dai->name);
  314. snd_hdac_ext_link_stream_reset(link_dev);
  315. snd_hdac_ext_link_stream_setup(link_dev, format_val);
  316. link = snd_hdac_ext_bus_get_link(ebus, rtd->codec->component.name);
  317. if (!link)
  318. return -EINVAL;
  319. snd_hdac_ext_link_set_stream_id(link, hdac_stream(link_dev)->stream_tag);
  320. link_dev->link_prepared = 1;
  321. return 0;
  322. }
  323. static int skl_link_pcm_trigger(struct snd_pcm_substream *substream,
  324. int cmd, struct snd_soc_dai *dai)
  325. {
  326. struct hdac_ext_stream *link_dev =
  327. snd_soc_dai_get_dma_data(dai, substream);
  328. dev_dbg(dai->dev, "In %s cmd=%d\n", __func__, cmd);
  329. switch (cmd) {
  330. case SNDRV_PCM_TRIGGER_START:
  331. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  332. case SNDRV_PCM_TRIGGER_RESUME:
  333. snd_hdac_ext_link_stream_start(link_dev);
  334. break;
  335. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  336. case SNDRV_PCM_TRIGGER_SUSPEND:
  337. case SNDRV_PCM_TRIGGER_STOP:
  338. snd_hdac_ext_link_stream_clear(link_dev);
  339. break;
  340. default:
  341. return -EINVAL;
  342. }
  343. return 0;
  344. }
  345. static int skl_link_hw_free(struct snd_pcm_substream *substream,
  346. struct snd_soc_dai *dai)
  347. {
  348. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  349. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  350. struct hdac_ext_stream *link_dev =
  351. snd_soc_dai_get_dma_data(dai, substream);
  352. struct hdac_ext_link *link;
  353. dev_dbg(dai->dev, "%s: %s\n", __func__, dai->name);
  354. link_dev->link_prepared = 0;
  355. link = snd_hdac_ext_bus_get_link(ebus, rtd->codec->component.name);
  356. if (!link)
  357. return -EINVAL;
  358. snd_hdac_ext_link_clear_stream_id(link, hdac_stream(link_dev)->stream_tag);
  359. snd_hdac_ext_stream_release(link_dev, HDAC_EXT_STREAM_TYPE_LINK);
  360. return 0;
  361. }
  362. static int skl_be_startup(struct snd_pcm_substream *substream,
  363. struct snd_soc_dai *dai)
  364. {
  365. return pm_runtime_get_sync(dai->dev);
  366. }
  367. static void skl_be_shutdown(struct snd_pcm_substream *substream,
  368. struct snd_soc_dai *dai)
  369. {
  370. pm_runtime_mark_last_busy(dai->dev);
  371. pm_runtime_put_autosuspend(dai->dev);
  372. }
  373. static struct snd_soc_dai_ops skl_pcm_dai_ops = {
  374. .startup = skl_pcm_open,
  375. .shutdown = skl_pcm_close,
  376. .prepare = skl_pcm_prepare,
  377. .hw_params = skl_pcm_hw_params,
  378. .hw_free = skl_pcm_hw_free,
  379. .trigger = skl_pcm_trigger,
  380. };
  381. static struct snd_soc_dai_ops skl_dmic_dai_ops = {
  382. .startup = skl_be_startup,
  383. .hw_params = skl_be_hw_params,
  384. .shutdown = skl_be_shutdown,
  385. };
  386. static struct snd_soc_dai_ops skl_be_ssp_dai_ops = {
  387. .startup = skl_be_startup,
  388. .hw_params = skl_be_hw_params,
  389. .shutdown = skl_be_shutdown,
  390. };
  391. static struct snd_soc_dai_ops skl_link_dai_ops = {
  392. .startup = skl_be_startup,
  393. .prepare = skl_link_pcm_prepare,
  394. .hw_params = skl_link_hw_params,
  395. .hw_free = skl_link_hw_free,
  396. .trigger = skl_link_pcm_trigger,
  397. .shutdown = skl_be_shutdown,
  398. };
  399. static struct snd_soc_dai_driver skl_platform_dai[] = {
  400. {
  401. .name = "System Pin",
  402. .ops = &skl_pcm_dai_ops,
  403. .playback = {
  404. .stream_name = "System Playback",
  405. .channels_min = HDA_MONO,
  406. .channels_max = HDA_STEREO,
  407. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_8000,
  408. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  409. },
  410. .capture = {
  411. .stream_name = "System Capture",
  412. .channels_min = HDA_MONO,
  413. .channels_max = HDA_STEREO,
  414. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
  415. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  416. },
  417. },
  418. {
  419. .name = "Reference Pin",
  420. .ops = &skl_pcm_dai_ops,
  421. .capture = {
  422. .stream_name = "Reference Capture",
  423. .channels_min = HDA_MONO,
  424. .channels_max = HDA_STEREO,
  425. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
  426. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  427. },
  428. },
  429. {
  430. .name = "Deepbuffer Pin",
  431. .ops = &skl_pcm_dai_ops,
  432. .playback = {
  433. .stream_name = "Deepbuffer Playback",
  434. .channels_min = HDA_STEREO,
  435. .channels_max = HDA_STEREO,
  436. .rates = SNDRV_PCM_RATE_48000,
  437. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  438. },
  439. },
  440. {
  441. .name = "LowLatency Pin",
  442. .ops = &skl_pcm_dai_ops,
  443. .playback = {
  444. .stream_name = "Low Latency Playback",
  445. .channels_min = HDA_STEREO,
  446. .channels_max = HDA_STEREO,
  447. .rates = SNDRV_PCM_RATE_48000,
  448. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  449. },
  450. },
  451. /* BE CPU Dais */
  452. {
  453. .name = "SSP0 Pin",
  454. .ops = &skl_be_ssp_dai_ops,
  455. .playback = {
  456. .stream_name = "ssp0 Tx",
  457. .channels_min = HDA_STEREO,
  458. .channels_max = HDA_STEREO,
  459. .rates = SNDRV_PCM_RATE_48000,
  460. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  461. },
  462. .capture = {
  463. .stream_name = "ssp0 Rx",
  464. .channels_min = HDA_STEREO,
  465. .channels_max = HDA_STEREO,
  466. .rates = SNDRV_PCM_RATE_48000,
  467. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  468. },
  469. },
  470. {
  471. .name = "iDisp Pin",
  472. .ops = &skl_link_dai_ops,
  473. .playback = {
  474. .stream_name = "iDisp Tx",
  475. .channels_min = HDA_STEREO,
  476. .channels_max = HDA_STEREO,
  477. .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000,
  478. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  479. },
  480. },
  481. {
  482. .name = "DMIC01 Pin",
  483. .ops = &skl_dmic_dai_ops,
  484. .capture = {
  485. .stream_name = "DMIC01 Rx",
  486. .channels_min = HDA_STEREO,
  487. .channels_max = HDA_STEREO,
  488. .rates = SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_16000,
  489. .formats = SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE,
  490. },
  491. },
  492. {
  493. .name = "HD-Codec Pin",
  494. .ops = &skl_link_dai_ops,
  495. .playback = {
  496. .stream_name = "HD-Codec Tx",
  497. .channels_min = HDA_STEREO,
  498. .channels_max = HDA_STEREO,
  499. .rates = SNDRV_PCM_RATE_48000,
  500. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  501. },
  502. .capture = {
  503. .stream_name = "HD-Codec Rx",
  504. .channels_min = HDA_STEREO,
  505. .channels_max = HDA_STEREO,
  506. .rates = SNDRV_PCM_RATE_48000,
  507. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  508. },
  509. },
  510. };
  511. static int skl_platform_open(struct snd_pcm_substream *substream)
  512. {
  513. struct snd_pcm_runtime *runtime;
  514. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  515. struct snd_soc_dai_link *dai_link = rtd->dai_link;
  516. dev_dbg(rtd->cpu_dai->dev, "In %s:%s\n", __func__,
  517. dai_link->cpu_dai_name);
  518. runtime = substream->runtime;
  519. snd_soc_set_runtime_hwparams(substream, &azx_pcm_hw);
  520. return 0;
  521. }
  522. static int skl_coupled_trigger(struct snd_pcm_substream *substream,
  523. int cmd)
  524. {
  525. struct hdac_ext_bus *ebus = get_bus_ctx(substream);
  526. struct hdac_bus *bus = ebus_to_hbus(ebus);
  527. struct hdac_ext_stream *stream;
  528. struct snd_pcm_substream *s;
  529. bool start;
  530. int sbits = 0;
  531. unsigned long cookie;
  532. struct hdac_stream *hstr;
  533. stream = get_hdac_ext_stream(substream);
  534. hstr = hdac_stream(stream);
  535. dev_dbg(bus->dev, "In %s cmd=%d\n", __func__, cmd);
  536. if (!hstr->prepared)
  537. return -EPIPE;
  538. switch (cmd) {
  539. case SNDRV_PCM_TRIGGER_START:
  540. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  541. case SNDRV_PCM_TRIGGER_RESUME:
  542. start = true;
  543. break;
  544. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  545. case SNDRV_PCM_TRIGGER_SUSPEND:
  546. case SNDRV_PCM_TRIGGER_STOP:
  547. start = false;
  548. break;
  549. default:
  550. return -EINVAL;
  551. }
  552. snd_pcm_group_for_each_entry(s, substream) {
  553. if (s->pcm->card != substream->pcm->card)
  554. continue;
  555. stream = get_hdac_ext_stream(s);
  556. sbits |= 1 << hdac_stream(stream)->index;
  557. snd_pcm_trigger_done(s, substream);
  558. }
  559. spin_lock_irqsave(&bus->reg_lock, cookie);
  560. /* first, set SYNC bits of corresponding streams */
  561. snd_hdac_stream_sync_trigger(hstr, true, sbits, AZX_REG_SSYNC);
  562. snd_pcm_group_for_each_entry(s, substream) {
  563. if (s->pcm->card != substream->pcm->card)
  564. continue;
  565. stream = get_hdac_ext_stream(s);
  566. if (start)
  567. snd_hdac_stream_start(hdac_stream(stream), true);
  568. else
  569. snd_hdac_stream_stop(hdac_stream(stream));
  570. }
  571. spin_unlock_irqrestore(&bus->reg_lock, cookie);
  572. snd_hdac_stream_sync(hstr, start, sbits);
  573. spin_lock_irqsave(&bus->reg_lock, cookie);
  574. /* reset SYNC bits */
  575. snd_hdac_stream_sync_trigger(hstr, false, sbits, AZX_REG_SSYNC);
  576. if (start)
  577. snd_hdac_stream_timecounter_init(hstr, sbits);
  578. spin_unlock_irqrestore(&bus->reg_lock, cookie);
  579. return 0;
  580. }
  581. static int skl_decoupled_trigger(struct snd_pcm_substream *substream,
  582. int cmd)
  583. {
  584. struct hdac_ext_bus *ebus = get_bus_ctx(substream);
  585. struct hdac_bus *bus = ebus_to_hbus(ebus);
  586. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  587. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  588. struct hdac_ext_stream *stream;
  589. int start;
  590. unsigned long cookie;
  591. struct hdac_stream *hstr;
  592. dev_dbg(bus->dev, "In %s cmd=%d streamname=%s\n", __func__, cmd, cpu_dai->name);
  593. stream = get_hdac_ext_stream(substream);
  594. hstr = hdac_stream(stream);
  595. if (!hstr->prepared)
  596. return -EPIPE;
  597. switch (cmd) {
  598. case SNDRV_PCM_TRIGGER_START:
  599. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  600. case SNDRV_PCM_TRIGGER_RESUME:
  601. start = 1;
  602. break;
  603. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  604. case SNDRV_PCM_TRIGGER_SUSPEND:
  605. case SNDRV_PCM_TRIGGER_STOP:
  606. start = 0;
  607. break;
  608. default:
  609. return -EINVAL;
  610. }
  611. spin_lock_irqsave(&bus->reg_lock, cookie);
  612. if (start)
  613. snd_hdac_stream_start(hdac_stream(stream), true);
  614. else
  615. snd_hdac_stream_stop(hdac_stream(stream));
  616. if (start)
  617. snd_hdac_stream_timecounter_init(hstr, 0);
  618. spin_unlock_irqrestore(&bus->reg_lock, cookie);
  619. return 0;
  620. }
  621. static int skl_platform_pcm_trigger(struct snd_pcm_substream *substream,
  622. int cmd)
  623. {
  624. struct hdac_ext_bus *ebus = get_bus_ctx(substream);
  625. if (ebus->ppcap)
  626. return skl_decoupled_trigger(substream, cmd);
  627. else
  628. return skl_coupled_trigger(substream, cmd);
  629. }
  630. /* calculate runtime delay from LPIB */
  631. static int skl_get_delay_from_lpib(struct hdac_ext_bus *ebus,
  632. struct hdac_ext_stream *sstream,
  633. unsigned int pos)
  634. {
  635. struct hdac_bus *bus = ebus_to_hbus(ebus);
  636. struct hdac_stream *hstream = hdac_stream(sstream);
  637. struct snd_pcm_substream *substream = hstream->substream;
  638. int stream = substream->stream;
  639. unsigned int lpib_pos = snd_hdac_stream_get_pos_lpib(hstream);
  640. int delay;
  641. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  642. delay = pos - lpib_pos;
  643. else
  644. delay = lpib_pos - pos;
  645. if (delay < 0) {
  646. if (delay >= hstream->delay_negative_threshold)
  647. delay = 0;
  648. else
  649. delay += hstream->bufsize;
  650. }
  651. if (delay >= hstream->period_bytes) {
  652. dev_info(bus->dev,
  653. "Unstable LPIB (%d >= %d); disabling LPIB delay counting\n",
  654. delay, hstream->period_bytes);
  655. delay = 0;
  656. }
  657. return bytes_to_frames(substream->runtime, delay);
  658. }
  659. static unsigned int skl_get_position(struct hdac_ext_stream *hstream,
  660. int codec_delay)
  661. {
  662. struct hdac_stream *hstr = hdac_stream(hstream);
  663. struct snd_pcm_substream *substream = hstr->substream;
  664. struct hdac_ext_bus *ebus = get_bus_ctx(substream);
  665. unsigned int pos;
  666. int delay;
  667. /* use the position buffer as default */
  668. pos = snd_hdac_stream_get_pos_posbuf(hdac_stream(hstream));
  669. if (pos >= hdac_stream(hstream)->bufsize)
  670. pos = 0;
  671. if (substream->runtime) {
  672. delay = skl_get_delay_from_lpib(ebus, hstream, pos)
  673. + codec_delay;
  674. substream->runtime->delay += delay;
  675. }
  676. return pos;
  677. }
  678. static snd_pcm_uframes_t skl_platform_pcm_pointer
  679. (struct snd_pcm_substream *substream)
  680. {
  681. struct hdac_ext_stream *hstream = get_hdac_ext_stream(substream);
  682. return bytes_to_frames(substream->runtime,
  683. skl_get_position(hstream, 0));
  684. }
  685. static u64 skl_adjust_codec_delay(struct snd_pcm_substream *substream,
  686. u64 nsec)
  687. {
  688. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  689. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  690. u64 codec_frames, codec_nsecs;
  691. if (!codec_dai->driver->ops->delay)
  692. return nsec;
  693. codec_frames = codec_dai->driver->ops->delay(substream, codec_dai);
  694. codec_nsecs = div_u64(codec_frames * 1000000000LL,
  695. substream->runtime->rate);
  696. if (substream->stream == SNDRV_PCM_STREAM_CAPTURE)
  697. return nsec + codec_nsecs;
  698. return (nsec > codec_nsecs) ? nsec - codec_nsecs : 0;
  699. }
  700. static int skl_get_time_info(struct snd_pcm_substream *substream,
  701. struct timespec *system_ts, struct timespec *audio_ts,
  702. struct snd_pcm_audio_tstamp_config *audio_tstamp_config,
  703. struct snd_pcm_audio_tstamp_report *audio_tstamp_report)
  704. {
  705. struct hdac_ext_stream *sstream = get_hdac_ext_stream(substream);
  706. struct hdac_stream *hstr = hdac_stream(sstream);
  707. u64 nsec;
  708. if ((substream->runtime->hw.info & SNDRV_PCM_INFO_HAS_LINK_ATIME) &&
  709. (audio_tstamp_config->type_requested == SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK)) {
  710. snd_pcm_gettime(substream->runtime, system_ts);
  711. nsec = timecounter_read(&hstr->tc);
  712. nsec = div_u64(nsec, 3); /* can be optimized */
  713. if (audio_tstamp_config->report_delay)
  714. nsec = skl_adjust_codec_delay(substream, nsec);
  715. *audio_ts = ns_to_timespec(nsec);
  716. audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_LINK;
  717. audio_tstamp_report->accuracy_report = 1; /* rest of struct is valid */
  718. audio_tstamp_report->accuracy = 42; /* 24MHzWallClk == 42ns resolution */
  719. } else {
  720. audio_tstamp_report->actual_type = SNDRV_PCM_AUDIO_TSTAMP_TYPE_DEFAULT;
  721. }
  722. return 0;
  723. }
  724. static struct snd_pcm_ops skl_platform_ops = {
  725. .open = skl_platform_open,
  726. .ioctl = snd_pcm_lib_ioctl,
  727. .trigger = skl_platform_pcm_trigger,
  728. .pointer = skl_platform_pcm_pointer,
  729. .get_time_info = skl_get_time_info,
  730. .mmap = snd_pcm_lib_default_mmap,
  731. .page = snd_pcm_sgbuf_ops_page,
  732. };
  733. static void skl_pcm_free(struct snd_pcm *pcm)
  734. {
  735. snd_pcm_lib_preallocate_free_for_all(pcm);
  736. }
  737. #define MAX_PREALLOC_SIZE (32 * 1024 * 1024)
  738. static int skl_pcm_new(struct snd_soc_pcm_runtime *rtd)
  739. {
  740. struct snd_soc_dai *dai = rtd->cpu_dai;
  741. struct hdac_ext_bus *ebus = dev_get_drvdata(dai->dev);
  742. struct snd_pcm *pcm = rtd->pcm;
  743. unsigned int size;
  744. int retval = 0;
  745. struct skl *skl = ebus_to_skl(ebus);
  746. if (dai->driver->playback.channels_min ||
  747. dai->driver->capture.channels_min) {
  748. /* buffer pre-allocation */
  749. size = CONFIG_SND_HDA_PREALLOC_SIZE * 1024;
  750. if (size > MAX_PREALLOC_SIZE)
  751. size = MAX_PREALLOC_SIZE;
  752. retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
  753. SNDRV_DMA_TYPE_DEV_SG,
  754. snd_dma_pci_data(skl->pci),
  755. size, MAX_PREALLOC_SIZE);
  756. if (retval) {
  757. dev_err(dai->dev, "dma buffer allocationf fail\n");
  758. return retval;
  759. }
  760. }
  761. return retval;
  762. }
  763. static int skl_platform_soc_probe(struct snd_soc_platform *platform)
  764. {
  765. struct hdac_ext_bus *ebus = dev_get_drvdata(platform->dev);
  766. if (ebus->ppcap)
  767. return skl_tplg_init(platform, ebus);
  768. return 0;
  769. }
  770. static struct snd_soc_platform_driver skl_platform_drv = {
  771. .probe = skl_platform_soc_probe,
  772. .ops = &skl_platform_ops,
  773. .pcm_new = skl_pcm_new,
  774. .pcm_free = skl_pcm_free,
  775. };
  776. static const struct snd_soc_component_driver skl_component = {
  777. .name = "pcm",
  778. };
  779. int skl_platform_register(struct device *dev)
  780. {
  781. int ret;
  782. struct hdac_ext_bus *ebus = dev_get_drvdata(dev);
  783. struct skl *skl = ebus_to_skl(ebus);
  784. INIT_LIST_HEAD(&skl->ppl_list);
  785. INIT_LIST_HEAD(&skl->dapm_path_list);
  786. ret = snd_soc_register_platform(dev, &skl_platform_drv);
  787. if (ret) {
  788. dev_err(dev, "soc platform registration failed %d\n", ret);
  789. return ret;
  790. }
  791. ret = snd_soc_register_component(dev, &skl_component,
  792. skl_platform_dai,
  793. ARRAY_SIZE(skl_platform_dai));
  794. if (ret) {
  795. dev_err(dev, "soc component registration failed %d\n", ret);
  796. snd_soc_unregister_platform(dev);
  797. }
  798. return ret;
  799. }
  800. int skl_platform_unregister(struct device *dev)
  801. {
  802. snd_soc_unregister_component(dev);
  803. snd_soc_unregister_platform(dev);
  804. return 0;
  805. }