sst-baytrail-pcm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Intel Baytrail SST PCM Support
  3. * Copyright (c) 2014, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/dma-mapping.h>
  16. #include <linux/slab.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/pcm_params.h>
  20. #include <sound/soc.h>
  21. #include "sst-baytrail-ipc.h"
  22. #include "../common/sst-dsp-priv.h"
  23. #include "../common/sst-dsp.h"
  24. #define BYT_PCM_COUNT 2
  25. static const struct snd_pcm_hardware sst_byt_pcm_hardware = {
  26. .info = SNDRV_PCM_INFO_MMAP |
  27. SNDRV_PCM_INFO_MMAP_VALID |
  28. SNDRV_PCM_INFO_INTERLEAVED |
  29. SNDRV_PCM_INFO_PAUSE |
  30. SNDRV_PCM_INFO_RESUME,
  31. .formats = SNDRV_PCM_FMTBIT_S16_LE |
  32. SNDRV_PCM_FMTBIT_S24_LE,
  33. .period_bytes_min = 384,
  34. .period_bytes_max = 48000,
  35. .periods_min = 2,
  36. .periods_max = 250,
  37. .buffer_bytes_max = 96000,
  38. };
  39. /* private data for each PCM DSP stream */
  40. struct sst_byt_pcm_data {
  41. struct sst_byt_stream *stream;
  42. struct snd_pcm_substream *substream;
  43. struct mutex mutex;
  44. /* latest DSP DMA hw pointer */
  45. u32 hw_ptr;
  46. struct work_struct work;
  47. };
  48. /* private data for the driver */
  49. struct sst_byt_priv_data {
  50. /* runtime DSP */
  51. struct sst_byt *byt;
  52. /* DAI data */
  53. struct sst_byt_pcm_data pcm[BYT_PCM_COUNT];
  54. /* flag indicating is stream context restore needed after suspend */
  55. bool restore_stream;
  56. };
  57. /* this may get called several times by oss emulation */
  58. static int sst_byt_pcm_hw_params(struct snd_pcm_substream *substream,
  59. struct snd_pcm_hw_params *params)
  60. {
  61. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  62. struct sst_byt_priv_data *pdata =
  63. snd_soc_platform_get_drvdata(rtd->platform);
  64. struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
  65. struct sst_byt *byt = pdata->byt;
  66. u32 rate, bits;
  67. u8 channels;
  68. int ret, playback = (substream->stream == SNDRV_PCM_STREAM_PLAYBACK);
  69. dev_dbg(rtd->dev, "PCM: hw_params, pcm_data %p\n", pcm_data);
  70. ret = sst_byt_stream_type(byt, pcm_data->stream,
  71. 1, 1, !playback);
  72. if (ret < 0) {
  73. dev_err(rtd->dev, "failed to set stream format %d\n", ret);
  74. return ret;
  75. }
  76. rate = params_rate(params);
  77. ret = sst_byt_stream_set_rate(byt, pcm_data->stream, rate);
  78. if (ret < 0) {
  79. dev_err(rtd->dev, "could not set rate %d\n", rate);
  80. return ret;
  81. }
  82. bits = snd_pcm_format_width(params_format(params));
  83. ret = sst_byt_stream_set_bits(byt, pcm_data->stream, bits);
  84. if (ret < 0) {
  85. dev_err(rtd->dev, "could not set formats %d\n",
  86. params_rate(params));
  87. return ret;
  88. }
  89. channels = (u8)(params_channels(params) & 0xF);
  90. ret = sst_byt_stream_set_channels(byt, pcm_data->stream, channels);
  91. if (ret < 0) {
  92. dev_err(rtd->dev, "could not set channels %d\n",
  93. params_rate(params));
  94. return ret;
  95. }
  96. snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  97. ret = sst_byt_stream_buffer(byt, pcm_data->stream,
  98. substream->dma_buffer.addr,
  99. params_buffer_bytes(params));
  100. if (ret < 0) {
  101. dev_err(rtd->dev, "PCM: failed to set DMA buffer %d\n", ret);
  102. return ret;
  103. }
  104. ret = sst_byt_stream_commit(byt, pcm_data->stream);
  105. if (ret < 0) {
  106. dev_err(rtd->dev, "PCM: failed stream commit %d\n", ret);
  107. return ret;
  108. }
  109. return 0;
  110. }
  111. static int sst_byt_pcm_hw_free(struct snd_pcm_substream *substream)
  112. {
  113. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  114. dev_dbg(rtd->dev, "PCM: hw_free\n");
  115. snd_pcm_lib_free_pages(substream);
  116. return 0;
  117. }
  118. static int sst_byt_pcm_restore_stream_context(struct snd_pcm_substream *substream)
  119. {
  120. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  121. struct sst_byt_priv_data *pdata =
  122. snd_soc_platform_get_drvdata(rtd->platform);
  123. struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
  124. struct sst_byt *byt = pdata->byt;
  125. int ret;
  126. /* commit stream using existing stream params */
  127. ret = sst_byt_stream_commit(byt, pcm_data->stream);
  128. if (ret < 0) {
  129. dev_err(rtd->dev, "PCM: failed stream commit %d\n", ret);
  130. return ret;
  131. }
  132. sst_byt_stream_start(byt, pcm_data->stream, pcm_data->hw_ptr);
  133. dev_dbg(rtd->dev, "stream context restored at offset %d\n",
  134. pcm_data->hw_ptr);
  135. return 0;
  136. }
  137. static void sst_byt_pcm_work(struct work_struct *work)
  138. {
  139. struct sst_byt_pcm_data *pcm_data =
  140. container_of(work, struct sst_byt_pcm_data, work);
  141. if (snd_pcm_running(pcm_data->substream))
  142. sst_byt_pcm_restore_stream_context(pcm_data->substream);
  143. }
  144. static int sst_byt_pcm_trigger(struct snd_pcm_substream *substream, int cmd)
  145. {
  146. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  147. struct sst_byt_priv_data *pdata =
  148. snd_soc_platform_get_drvdata(rtd->platform);
  149. struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
  150. struct sst_byt *byt = pdata->byt;
  151. dev_dbg(rtd->dev, "PCM: trigger %d\n", cmd);
  152. switch (cmd) {
  153. case SNDRV_PCM_TRIGGER_START:
  154. pcm_data->hw_ptr = 0;
  155. sst_byt_stream_start(byt, pcm_data->stream, 0);
  156. break;
  157. case SNDRV_PCM_TRIGGER_RESUME:
  158. if (pdata->restore_stream == true)
  159. schedule_work(&pcm_data->work);
  160. else
  161. sst_byt_stream_resume(byt, pcm_data->stream);
  162. break;
  163. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  164. sst_byt_stream_resume(byt, pcm_data->stream);
  165. break;
  166. case SNDRV_PCM_TRIGGER_STOP:
  167. sst_byt_stream_stop(byt, pcm_data->stream);
  168. break;
  169. case SNDRV_PCM_TRIGGER_SUSPEND:
  170. pdata->restore_stream = false;
  171. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  172. sst_byt_stream_pause(byt, pcm_data->stream);
  173. break;
  174. default:
  175. break;
  176. }
  177. return 0;
  178. }
  179. static u32 byt_notify_pointer(struct sst_byt_stream *stream, void *data)
  180. {
  181. struct sst_byt_pcm_data *pcm_data = data;
  182. struct snd_pcm_substream *substream = pcm_data->substream;
  183. struct snd_pcm_runtime *runtime = substream->runtime;
  184. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  185. struct sst_byt_priv_data *pdata =
  186. snd_soc_platform_get_drvdata(rtd->platform);
  187. struct sst_byt *byt = pdata->byt;
  188. u32 pos, hw_pos;
  189. hw_pos = sst_byt_get_dsp_position(byt, pcm_data->stream,
  190. snd_pcm_lib_buffer_bytes(substream));
  191. pcm_data->hw_ptr = hw_pos;
  192. pos = frames_to_bytes(runtime,
  193. (runtime->control->appl_ptr %
  194. runtime->buffer_size));
  195. dev_dbg(rtd->dev, "PCM: App/DMA pointer %u/%u bytes\n", pos, hw_pos);
  196. snd_pcm_period_elapsed(substream);
  197. return pos;
  198. }
  199. static snd_pcm_uframes_t sst_byt_pcm_pointer(struct snd_pcm_substream *substream)
  200. {
  201. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  202. struct snd_pcm_runtime *runtime = substream->runtime;
  203. struct sst_byt_priv_data *pdata =
  204. snd_soc_platform_get_drvdata(rtd->platform);
  205. struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
  206. dev_dbg(rtd->dev, "PCM: DMA pointer %u bytes\n", pcm_data->hw_ptr);
  207. return bytes_to_frames(runtime, pcm_data->hw_ptr);
  208. }
  209. static int sst_byt_pcm_open(struct snd_pcm_substream *substream)
  210. {
  211. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  212. struct sst_byt_priv_data *pdata =
  213. snd_soc_platform_get_drvdata(rtd->platform);
  214. struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
  215. struct sst_byt *byt = pdata->byt;
  216. dev_dbg(rtd->dev, "PCM: open\n");
  217. mutex_lock(&pcm_data->mutex);
  218. pcm_data->substream = substream;
  219. snd_soc_set_runtime_hwparams(substream, &sst_byt_pcm_hardware);
  220. pcm_data->stream = sst_byt_stream_new(byt, substream->stream + 1,
  221. byt_notify_pointer, pcm_data);
  222. if (pcm_data->stream == NULL) {
  223. dev_err(rtd->dev, "failed to create stream\n");
  224. mutex_unlock(&pcm_data->mutex);
  225. return -EINVAL;
  226. }
  227. mutex_unlock(&pcm_data->mutex);
  228. return 0;
  229. }
  230. static int sst_byt_pcm_close(struct snd_pcm_substream *substream)
  231. {
  232. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  233. struct sst_byt_priv_data *pdata =
  234. snd_soc_platform_get_drvdata(rtd->platform);
  235. struct sst_byt_pcm_data *pcm_data = &pdata->pcm[substream->stream];
  236. struct sst_byt *byt = pdata->byt;
  237. int ret;
  238. dev_dbg(rtd->dev, "PCM: close\n");
  239. cancel_work_sync(&pcm_data->work);
  240. mutex_lock(&pcm_data->mutex);
  241. ret = sst_byt_stream_free(byt, pcm_data->stream);
  242. if (ret < 0) {
  243. dev_dbg(rtd->dev, "Free stream fail\n");
  244. goto out;
  245. }
  246. pcm_data->stream = NULL;
  247. out:
  248. mutex_unlock(&pcm_data->mutex);
  249. return ret;
  250. }
  251. static int sst_byt_pcm_mmap(struct snd_pcm_substream *substream,
  252. struct vm_area_struct *vma)
  253. {
  254. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  255. dev_dbg(rtd->dev, "PCM: mmap\n");
  256. return snd_pcm_lib_default_mmap(substream, vma);
  257. }
  258. static struct snd_pcm_ops sst_byt_pcm_ops = {
  259. .open = sst_byt_pcm_open,
  260. .close = sst_byt_pcm_close,
  261. .ioctl = snd_pcm_lib_ioctl,
  262. .hw_params = sst_byt_pcm_hw_params,
  263. .hw_free = sst_byt_pcm_hw_free,
  264. .trigger = sst_byt_pcm_trigger,
  265. .pointer = sst_byt_pcm_pointer,
  266. .mmap = sst_byt_pcm_mmap,
  267. };
  268. static int sst_byt_pcm_new(struct snd_soc_pcm_runtime *rtd)
  269. {
  270. struct snd_pcm *pcm = rtd->pcm;
  271. size_t size;
  272. struct snd_soc_platform *platform = rtd->platform;
  273. struct sst_pdata *pdata = dev_get_platdata(platform->dev);
  274. int ret = 0;
  275. if (pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream ||
  276. pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream) {
  277. size = sst_byt_pcm_hardware.buffer_bytes_max;
  278. ret = snd_pcm_lib_preallocate_pages_for_all(pcm,
  279. SNDRV_DMA_TYPE_DEV,
  280. pdata->dma_dev,
  281. size, size);
  282. if (ret) {
  283. dev_err(rtd->dev, "dma buffer allocation failed %d\n",
  284. ret);
  285. return ret;
  286. }
  287. }
  288. return ret;
  289. }
  290. static struct snd_soc_dai_driver byt_dais[] = {
  291. {
  292. .name = "Baytrail PCM",
  293. .playback = {
  294. .stream_name = "System Playback",
  295. .channels_min = 2,
  296. .channels_max = 2,
  297. .rates = SNDRV_PCM_RATE_48000,
  298. .formats = SNDRV_PCM_FMTBIT_S24_3LE |
  299. SNDRV_PCM_FMTBIT_S16_LE,
  300. },
  301. .capture = {
  302. .stream_name = "Analog Capture",
  303. .channels_min = 2,
  304. .channels_max = 2,
  305. .rates = SNDRV_PCM_RATE_48000,
  306. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  307. },
  308. },
  309. };
  310. static int sst_byt_pcm_probe(struct snd_soc_platform *platform)
  311. {
  312. struct sst_pdata *plat_data = dev_get_platdata(platform->dev);
  313. struct sst_byt_priv_data *priv_data;
  314. int i;
  315. if (!plat_data)
  316. return -ENODEV;
  317. priv_data = devm_kzalloc(platform->dev, sizeof(*priv_data),
  318. GFP_KERNEL);
  319. priv_data->byt = plat_data->dsp;
  320. snd_soc_platform_set_drvdata(platform, priv_data);
  321. for (i = 0; i < BYT_PCM_COUNT; i++) {
  322. mutex_init(&priv_data->pcm[i].mutex);
  323. INIT_WORK(&priv_data->pcm[i].work, sst_byt_pcm_work);
  324. }
  325. return 0;
  326. }
  327. static int sst_byt_pcm_remove(struct snd_soc_platform *platform)
  328. {
  329. return 0;
  330. }
  331. static struct snd_soc_platform_driver byt_soc_platform = {
  332. .probe = sst_byt_pcm_probe,
  333. .remove = sst_byt_pcm_remove,
  334. .ops = &sst_byt_pcm_ops,
  335. .pcm_new = sst_byt_pcm_new,
  336. };
  337. static const struct snd_soc_component_driver byt_dai_component = {
  338. .name = "byt-dai",
  339. };
  340. #ifdef CONFIG_PM
  341. static int sst_byt_pcm_dev_suspend_late(struct device *dev)
  342. {
  343. struct sst_pdata *sst_pdata = dev_get_platdata(dev);
  344. struct sst_byt_priv_data *priv_data = dev_get_drvdata(dev);
  345. int ret;
  346. dev_dbg(dev, "suspending late\n");
  347. ret = sst_byt_dsp_suspend_late(dev, sst_pdata);
  348. if (ret < 0) {
  349. dev_err(dev, "failed to suspend %d\n", ret);
  350. return ret;
  351. }
  352. priv_data->restore_stream = true;
  353. return ret;
  354. }
  355. static int sst_byt_pcm_dev_resume_early(struct device *dev)
  356. {
  357. struct sst_pdata *sst_pdata = dev_get_platdata(dev);
  358. int ret;
  359. dev_dbg(dev, "resume early\n");
  360. /* load fw and boot DSP */
  361. ret = sst_byt_dsp_boot(dev, sst_pdata);
  362. if (ret)
  363. return ret;
  364. /* wait for FW to finish booting */
  365. return sst_byt_dsp_wait_for_ready(dev, sst_pdata);
  366. }
  367. static const struct dev_pm_ops sst_byt_pm_ops = {
  368. .suspend_late = sst_byt_pcm_dev_suspend_late,
  369. .resume_early = sst_byt_pcm_dev_resume_early,
  370. };
  371. #define SST_BYT_PM_OPS (&sst_byt_pm_ops)
  372. #else
  373. #define SST_BYT_PM_OPS NULL
  374. #endif
  375. static int sst_byt_pcm_dev_probe(struct platform_device *pdev)
  376. {
  377. struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
  378. int ret;
  379. ret = sst_byt_dsp_init(&pdev->dev, sst_pdata);
  380. if (ret < 0)
  381. return -ENODEV;
  382. ret = snd_soc_register_platform(&pdev->dev, &byt_soc_platform);
  383. if (ret < 0)
  384. goto err_plat;
  385. ret = snd_soc_register_component(&pdev->dev, &byt_dai_component,
  386. byt_dais, ARRAY_SIZE(byt_dais));
  387. if (ret < 0)
  388. goto err_comp;
  389. return 0;
  390. err_comp:
  391. snd_soc_unregister_platform(&pdev->dev);
  392. err_plat:
  393. sst_byt_dsp_free(&pdev->dev, sst_pdata);
  394. return ret;
  395. }
  396. static int sst_byt_pcm_dev_remove(struct platform_device *pdev)
  397. {
  398. struct sst_pdata *sst_pdata = dev_get_platdata(&pdev->dev);
  399. snd_soc_unregister_platform(&pdev->dev);
  400. snd_soc_unregister_component(&pdev->dev);
  401. sst_byt_dsp_free(&pdev->dev, sst_pdata);
  402. return 0;
  403. }
  404. static struct platform_driver sst_byt_pcm_driver = {
  405. .driver = {
  406. .name = "baytrail-pcm-audio",
  407. .pm = SST_BYT_PM_OPS,
  408. },
  409. .probe = sst_byt_pcm_dev_probe,
  410. .remove = sst_byt_pcm_dev_remove,
  411. };
  412. module_platform_driver(sst_byt_pcm_driver);
  413. MODULE_AUTHOR("Jarkko Nikula");
  414. MODULE_DESCRIPTION("Baytrail PCM");
  415. MODULE_LICENSE("GPL v2");
  416. MODULE_ALIAS("platform:baytrail-pcm-audio");