sst-baytrail-pcm.c 13 KB

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