skl-pcm.c 24 KB

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