sst_platform.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * sst_platform.c - Intel MID Platform driver
  3. *
  4. * Copyright (C) 2010 Intel Corp
  5. * Author: Vinod Koul <vinod.koul@intel.com>
  6. * Author: Harsha Priya <priya.harsha@intel.com>
  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. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  21. *
  22. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  23. *
  24. *
  25. */
  26. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  27. #include <linux/slab.h>
  28. #include <linux/io.h>
  29. #include <sound/core.h>
  30. #include <sound/pcm.h>
  31. #include <sound/pcm_params.h>
  32. #include <sound/soc.h>
  33. #include "../../../drivers/staging/intel_sst/intel_sst_ioctl.h"
  34. #include "../../../drivers/staging/intel_sst/intel_sst.h"
  35. #include "sst_platform.h"
  36. static struct snd_pcm_hardware sst_platform_pcm_hw = {
  37. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  38. SNDRV_PCM_INFO_DOUBLE |
  39. SNDRV_PCM_INFO_PAUSE |
  40. SNDRV_PCM_INFO_RESUME |
  41. SNDRV_PCM_INFO_MMAP|
  42. SNDRV_PCM_INFO_MMAP_VALID |
  43. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  44. SNDRV_PCM_INFO_SYNC_START),
  45. .formats = (SNDRV_PCM_FMTBIT_S16 | SNDRV_PCM_FMTBIT_U16 |
  46. SNDRV_PCM_FMTBIT_S24 | SNDRV_PCM_FMTBIT_U24 |
  47. SNDRV_PCM_FMTBIT_S32 | SNDRV_PCM_FMTBIT_U32),
  48. .rates = (SNDRV_PCM_RATE_8000|
  49. SNDRV_PCM_RATE_44100 |
  50. SNDRV_PCM_RATE_48000),
  51. .rate_min = SST_MIN_RATE,
  52. .rate_max = SST_MAX_RATE,
  53. .channels_min = SST_MIN_CHANNEL,
  54. .channels_max = SST_MAX_CHANNEL,
  55. .buffer_bytes_max = SST_MAX_BUFFER,
  56. .period_bytes_min = SST_MIN_PERIOD_BYTES,
  57. .period_bytes_max = SST_MAX_PERIOD_BYTES,
  58. .periods_min = SST_MIN_PERIODS,
  59. .periods_max = SST_MAX_PERIODS,
  60. .fifo_size = SST_FIFO_SIZE,
  61. };
  62. /* MFLD - MSIC */
  63. struct snd_soc_dai_driver sst_platform_dai[] = {
  64. {
  65. .name = "Headset-cpu-dai",
  66. .id = 0,
  67. .playback = {
  68. .channels_min = SST_STEREO,
  69. .channels_max = SST_STEREO,
  70. .rates = SNDRV_PCM_RATE_48000,
  71. .formats = SNDRV_PCM_FMTBIT_S24_LE,
  72. },
  73. },
  74. {
  75. .name = "Speaker-cpu-dai",
  76. .id = 1,
  77. .playback = {
  78. .channels_min = SST_MONO,
  79. .channels_max = SST_STEREO,
  80. .rates = SNDRV_PCM_RATE_48000,
  81. .formats = SNDRV_PCM_FMTBIT_S24_LE,
  82. },
  83. },
  84. {
  85. .name = "Vibra1-cpu-dai",
  86. .id = 2,
  87. .playback = {
  88. .channels_min = SST_MONO,
  89. .channels_max = SST_MONO,
  90. .rates = SNDRV_PCM_RATE_48000,
  91. .formats = SNDRV_PCM_FMTBIT_S24_LE,
  92. },
  93. },
  94. {
  95. .name = "Vibra2-cpu-dai",
  96. .id = 3,
  97. .playback = {
  98. .channels_min = SST_MONO,
  99. .channels_max = SST_STEREO,
  100. .rates = SNDRV_PCM_RATE_48000,
  101. .formats = SNDRV_PCM_FMTBIT_S24_LE,
  102. },
  103. },
  104. };
  105. /* helper functions */
  106. static int sst_platform_alloc_stream(struct snd_pcm_substream *substream)
  107. {
  108. struct sst_runtime_stream *stream =
  109. substream->runtime->private_data;
  110. struct snd_sst_stream_params param = {{{0,},},};
  111. struct snd_sst_params str_params = {0};
  112. int ret_val;
  113. /* set codec params and inform SST driver the same */
  114. param.uc.pcm_params.codec = SST_CODEC_TYPE_PCM;
  115. param.uc.pcm_params.num_chan = (u8) substream->runtime->channels;
  116. param.uc.pcm_params.pcm_wd_sz = substream->runtime->sample_bits;
  117. param.uc.pcm_params.reserved = 0;
  118. param.uc.pcm_params.sfreq = substream->runtime->rate;
  119. param.uc.pcm_params.ring_buffer_size =
  120. snd_pcm_lib_buffer_bytes(substream);
  121. param.uc.pcm_params.period_count = substream->runtime->period_size;
  122. param.uc.pcm_params.ring_buffer_addr =
  123. virt_to_phys(substream->dma_buffer.area);
  124. substream->runtime->dma_area = substream->dma_buffer.area;
  125. pr_debug("period_cnt = %d\n", param.uc.pcm_params.period_count);
  126. pr_debug("sfreq= %d, wd_sz = %d\n",
  127. param.uc.pcm_params.sfreq, param.uc.pcm_params.pcm_wd_sz);
  128. str_params.sparams = param;
  129. str_params.codec = SST_CODEC_TYPE_PCM;
  130. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  131. str_params.ops = STREAM_OPS_PLAYBACK;
  132. str_params.device_type = substream->pcm->device + 1;
  133. pr_debug("Playbck stream,Device %d\n",
  134. substream->pcm->device);
  135. } else {
  136. str_params.ops = STREAM_OPS_CAPTURE;
  137. str_params.device_type = SND_SST_DEVICE_CAPTURE;
  138. pr_debug("Capture stream,Device %d\n",
  139. substream->pcm->device);
  140. }
  141. ret_val = stream->sstdrv_ops->control_set(SST_SND_ALLOC, &str_params);
  142. pr_debug("SST_SND_PLAY/CAPTURE ret_val = %x\n", ret_val);
  143. if (ret_val < 0)
  144. return ret_val;
  145. stream->stream_info.str_id = ret_val;
  146. pr_debug("str id : %d\n", stream->stream_info.str_id);
  147. return ret_val;
  148. }
  149. static void sst_period_elapsed(void *mad_substream)
  150. {
  151. struct snd_pcm_substream *substream = mad_substream;
  152. struct sst_runtime_stream *stream;
  153. if (!substream || !substream->runtime)
  154. return;
  155. stream = substream->runtime->private_data;
  156. if (!stream)
  157. return;
  158. spin_lock(&stream->status_lock);
  159. if (stream->stream_status != SST_PLATFORM_RUNNING) {
  160. spin_unlock(&stream->status_lock);
  161. return;
  162. }
  163. spin_unlock(&stream->status_lock);
  164. snd_pcm_period_elapsed(substream);
  165. return;
  166. }
  167. static int sst_platform_init_stream(struct snd_pcm_substream *substream)
  168. {
  169. struct sst_runtime_stream *stream =
  170. substream->runtime->private_data;
  171. int ret_val;
  172. pr_debug("setting buffer ptr param\n");
  173. spin_lock(&stream->status_lock);
  174. stream->stream_status = SST_PLATFORM_INIT;
  175. spin_unlock(&stream->status_lock);
  176. stream->stream_info.period_elapsed = sst_period_elapsed;
  177. stream->stream_info.mad_substream = substream;
  178. stream->stream_info.buffer_ptr = 0;
  179. stream->stream_info.sfreq = substream->runtime->rate;
  180. ret_val = stream->sstdrv_ops->control_set(SST_SND_STREAM_INIT,
  181. &stream->stream_info);
  182. if (ret_val)
  183. pr_err("control_set ret error %d\n", ret_val);
  184. return ret_val;
  185. }
  186. /* end -- helper functions */
  187. static int sst_platform_open(struct snd_pcm_substream *substream)
  188. {
  189. struct snd_pcm_runtime *runtime;
  190. struct sst_runtime_stream *stream;
  191. int ret_val = 0;
  192. pr_debug("sst_platform_open called\n");
  193. runtime = substream->runtime;
  194. runtime->hw = sst_platform_pcm_hw;
  195. stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  196. if (!stream)
  197. return -ENOMEM;
  198. spin_lock_init(&stream->status_lock);
  199. stream->stream_info.str_id = 0;
  200. spin_lock(&stream->status_lock);
  201. stream->stream_status = SST_PLATFORM_INIT;
  202. spin_unlock(&stream->status_lock);
  203. stream->stream_info.mad_substream = substream;
  204. /* allocate memory for SST API set */
  205. stream->sstdrv_ops = kzalloc(sizeof(*stream->sstdrv_ops),
  206. GFP_KERNEL);
  207. if (!stream->sstdrv_ops) {
  208. pr_err("sst: mem allocation for ops fail\n");
  209. kfree(stream);
  210. return -ENOMEM;
  211. }
  212. stream->sstdrv_ops->vendor_id = MSIC_VENDOR_ID;
  213. /* registering with SST driver to get access to SST APIs to use */
  214. ret_val = register_sst_card(stream->sstdrv_ops);
  215. if (ret_val) {
  216. pr_err("sst: sst card registration failed\n");
  217. return ret_val;
  218. }
  219. runtime->private_data = stream;
  220. return snd_pcm_hw_constraint_integer(runtime,
  221. SNDRV_PCM_HW_PARAM_PERIODS);
  222. }
  223. static int sst_platform_close(struct snd_pcm_substream *substream)
  224. {
  225. struct sst_runtime_stream *stream;
  226. int ret_val = 0, str_id;
  227. pr_debug("sst_platform_close called\n");
  228. stream = substream->runtime->private_data;
  229. str_id = stream->stream_info.str_id;
  230. if (str_id)
  231. ret_val = stream->sstdrv_ops->control_set(
  232. SST_SND_FREE, &str_id);
  233. kfree(stream->sstdrv_ops);
  234. kfree(stream);
  235. return ret_val;
  236. }
  237. static int sst_platform_pcm_prepare(struct snd_pcm_substream *substream)
  238. {
  239. struct sst_runtime_stream *stream;
  240. int ret_val = 0, str_id;
  241. pr_debug("sst_platform_pcm_prepare called\n");
  242. stream = substream->runtime->private_data;
  243. str_id = stream->stream_info.str_id;
  244. if (stream->stream_info.str_id) {
  245. ret_val = stream->sstdrv_ops->control_set(
  246. SST_SND_DROP, &str_id);
  247. return ret_val;
  248. }
  249. ret_val = sst_platform_alloc_stream(substream);
  250. if (ret_val < 0)
  251. return ret_val;
  252. snprintf(substream->pcm->id, sizeof(substream->pcm->id),
  253. "%d", stream->stream_info.str_id);
  254. ret_val = sst_platform_init_stream(substream);
  255. if (ret_val)
  256. return ret_val;
  257. substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
  258. return ret_val;
  259. }
  260. static int sst_platform_pcm_trigger(struct snd_pcm_substream *substream,
  261. int cmd)
  262. {
  263. int ret_val = 0, str_id;
  264. struct sst_runtime_stream *stream;
  265. pr_debug("sst_platform_pcm_trigger called\n");
  266. stream = substream->runtime->private_data;
  267. str_id = stream->stream_info.str_id;
  268. switch (cmd) {
  269. case SNDRV_PCM_TRIGGER_START:
  270. pr_debug("sst: Trigger Start\n");
  271. ret_val = stream->sstdrv_ops->control_set(
  272. SST_SND_START, &str_id);
  273. if (ret_val)
  274. break;
  275. spin_lock(&stream->status_lock);
  276. stream->stream_status = SST_PLATFORM_RUNNING;
  277. spin_unlock(&stream->status_lock);
  278. stream->stream_info.mad_substream = substream;
  279. break;
  280. case SNDRV_PCM_TRIGGER_STOP:
  281. pr_debug("sst: in stop\n");
  282. ret_val = stream->sstdrv_ops->control_set(
  283. SST_SND_DROP, &str_id);
  284. if (ret_val)
  285. break;
  286. spin_lock(&stream->status_lock);
  287. stream->stream_status = SST_PLATFORM_DROPPED;
  288. spin_unlock(&stream->status_lock);
  289. break;
  290. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  291. pr_debug("sst: in pause\n");
  292. ret_val = stream->sstdrv_ops->control_set(
  293. SST_SND_PAUSE, &str_id);
  294. if (ret_val)
  295. break;
  296. spin_lock(&stream->status_lock);
  297. stream->stream_status = SST_PLATFORM_PAUSED;
  298. spin_unlock(&stream->status_lock);
  299. break;
  300. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  301. pr_debug("sst: in pause release\n");
  302. ret_val = stream->sstdrv_ops->control_set(
  303. SST_SND_RESUME, &str_id);
  304. if (ret_val)
  305. break;
  306. spin_lock(&stream->status_lock);
  307. stream->stream_status = SST_PLATFORM_RUNNING;
  308. spin_unlock(&stream->status_lock);
  309. break;
  310. default:
  311. ret_val = -EINVAL;
  312. }
  313. return ret_val;
  314. }
  315. static snd_pcm_uframes_t sst_platform_pcm_pointer
  316. (struct snd_pcm_substream *substream)
  317. {
  318. struct sst_runtime_stream *stream;
  319. int ret_val;
  320. struct pcm_stream_info *str_info;
  321. stream = substream->runtime->private_data;
  322. spin_lock(&stream->status_lock);
  323. if (stream->stream_status == SST_PLATFORM_INIT) {
  324. spin_unlock(&stream->status_lock);
  325. return 0;
  326. }
  327. spin_unlock(&stream->status_lock);
  328. str_info = &stream->stream_info;
  329. ret_val = stream->sstdrv_ops->control_set(
  330. SST_SND_BUFFER_POINTER, str_info);
  331. if (ret_val) {
  332. pr_err("sst: error code = %d\n", ret_val);
  333. return ret_val;
  334. }
  335. return stream->stream_info.buffer_ptr;
  336. }
  337. static struct snd_pcm_ops sst_platform_ops = {
  338. .open = sst_platform_open,
  339. .close = sst_platform_close,
  340. .ioctl = snd_pcm_lib_ioctl,
  341. .prepare = sst_platform_pcm_prepare,
  342. .trigger = sst_platform_pcm_trigger,
  343. .pointer = sst_platform_pcm_pointer,
  344. };
  345. static void sst_pcm_free(struct snd_pcm *pcm)
  346. {
  347. pr_debug("sst_pcm_free called\n");
  348. snd_pcm_lib_preallocate_free_for_all(pcm);
  349. }
  350. int sst_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
  351. struct snd_pcm *pcm)
  352. {
  353. int retval = 0;
  354. pr_debug("sst_pcm_new called\n");
  355. if (dai->driver->playback.channels_min ||
  356. dai->driver->capture.channels_min) {
  357. retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
  358. SNDRV_DMA_TYPE_CONTINUOUS,
  359. snd_dma_continuous_data(GFP_KERNEL),
  360. SST_MIN_BUFFER, SST_MAX_BUFFER);
  361. if (retval) {
  362. pr_err("dma buffer allocationf fail\n");
  363. return retval;
  364. }
  365. }
  366. return retval;
  367. }
  368. struct snd_soc_platform_driver sst_soc_platform_drv = {
  369. .ops = &sst_platform_ops,
  370. .pcm_new = sst_pcm_new,
  371. .pcm_free = sst_pcm_free,
  372. };
  373. static int sst_platform_probe(struct platform_device *pdev)
  374. {
  375. int ret;
  376. pr_debug("sst_platform_probe called\n");
  377. ret = snd_soc_register_platform(&pdev->dev, &sst_soc_platform_drv);
  378. if (ret) {
  379. pr_err("registering soc platform failed\n");
  380. return ret;
  381. }
  382. ret = snd_soc_register_dais(&pdev->dev,
  383. sst_platform_dai, ARRAY_SIZE(sst_platform_dai));
  384. if (ret) {
  385. pr_err("registering cpu dais failed\n");
  386. snd_soc_unregister_platform(&pdev->dev);
  387. }
  388. return ret;
  389. }
  390. static int sst_platform_remove(struct platform_device *pdev)
  391. {
  392. snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(sst_platform_dai));
  393. snd_soc_unregister_platform(&pdev->dev);
  394. pr_debug("sst_platform_remove sucess\n");
  395. return 0;
  396. }
  397. static struct platform_driver sst_platform_driver = {
  398. .driver = {
  399. .name = "sst-platform",
  400. .owner = THIS_MODULE,
  401. },
  402. .probe = sst_platform_probe,
  403. .remove = sst_platform_remove,
  404. };
  405. static int __init sst_soc_platform_init(void)
  406. {
  407. pr_debug("sst_soc_platform_init called\n");
  408. return platform_driver_register(&sst_platform_driver);
  409. }
  410. module_init(sst_soc_platform_init);
  411. static void __exit sst_soc_platform_exit(void)
  412. {
  413. platform_driver_unregister(&sst_platform_driver);
  414. pr_debug("sst_soc_platform_exit sucess\n");
  415. }
  416. module_exit(sst_soc_platform_exit);
  417. MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");
  418. MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
  419. MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
  420. MODULE_LICENSE("GPL v2");
  421. MODULE_ALIAS("platfrom: sst-platform");