sst_platform.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. .capture = {
  74. .channels_min = 1,
  75. .channels_max = 5,
  76. .rates = SNDRV_PCM_RATE_48000,
  77. .formats = SNDRV_PCM_FMTBIT_S24_LE,
  78. },
  79. },
  80. {
  81. .name = "Speaker-cpu-dai",
  82. .id = 1,
  83. .playback = {
  84. .channels_min = SST_MONO,
  85. .channels_max = SST_STEREO,
  86. .rates = SNDRV_PCM_RATE_48000,
  87. .formats = SNDRV_PCM_FMTBIT_S24_LE,
  88. },
  89. },
  90. {
  91. .name = "Vibra1-cpu-dai",
  92. .id = 2,
  93. .playback = {
  94. .channels_min = SST_MONO,
  95. .channels_max = SST_MONO,
  96. .rates = SNDRV_PCM_RATE_48000,
  97. .formats = SNDRV_PCM_FMTBIT_S24_LE,
  98. },
  99. },
  100. {
  101. .name = "Vibra2-cpu-dai",
  102. .id = 3,
  103. .playback = {
  104. .channels_min = SST_MONO,
  105. .channels_max = SST_STEREO,
  106. .rates = SNDRV_PCM_RATE_48000,
  107. .formats = SNDRV_PCM_FMTBIT_S24_LE,
  108. },
  109. },
  110. };
  111. /* helper functions */
  112. static inline void sst_set_stream_status(struct sst_runtime_stream *stream,
  113. int state)
  114. {
  115. spin_lock(&stream->status_lock);
  116. stream->stream_status = state;
  117. spin_unlock(&stream->status_lock);
  118. }
  119. static inline int sst_get_stream_status(struct sst_runtime_stream *stream)
  120. {
  121. int state;
  122. spin_lock(&stream->status_lock);
  123. state = stream->stream_status;
  124. spin_unlock(&stream->status_lock);
  125. return state;
  126. }
  127. static void sst_fill_pcm_params(struct snd_pcm_substream *substream,
  128. struct snd_sst_stream_params *param)
  129. {
  130. param->uc.pcm_params.codec = SST_CODEC_TYPE_PCM;
  131. param->uc.pcm_params.num_chan = (u8) substream->runtime->channels;
  132. param->uc.pcm_params.pcm_wd_sz = substream->runtime->sample_bits;
  133. param->uc.pcm_params.reserved = 0;
  134. param->uc.pcm_params.sfreq = substream->runtime->rate;
  135. param->uc.pcm_params.ring_buffer_size =
  136. snd_pcm_lib_buffer_bytes(substream);
  137. param->uc.pcm_params.period_count = substream->runtime->period_size;
  138. param->uc.pcm_params.ring_buffer_addr =
  139. virt_to_phys(substream->dma_buffer.area);
  140. pr_debug("period_cnt = %d\n", param->uc.pcm_params.period_count);
  141. pr_debug("sfreq= %d, wd_sz = %d\n",
  142. param->uc.pcm_params.sfreq, param->uc.pcm_params.pcm_wd_sz);
  143. }
  144. static int sst_platform_alloc_stream(struct snd_pcm_substream *substream)
  145. {
  146. struct sst_runtime_stream *stream =
  147. substream->runtime->private_data;
  148. struct snd_sst_stream_params param = {{{0,},},};
  149. struct snd_sst_params str_params = {0};
  150. int ret_val;
  151. /* set codec params and inform SST driver the same */
  152. sst_fill_pcm_params(substream, &param);
  153. substream->runtime->dma_area = substream->dma_buffer.area;
  154. str_params.sparams = param;
  155. str_params.codec = param.uc.pcm_params.codec;
  156. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  157. str_params.ops = STREAM_OPS_PLAYBACK;
  158. str_params.device_type = substream->pcm->device + 1;
  159. pr_debug("Playbck stream,Device %d\n",
  160. substream->pcm->device);
  161. } else {
  162. str_params.ops = STREAM_OPS_CAPTURE;
  163. str_params.device_type = SND_SST_DEVICE_CAPTURE;
  164. pr_debug("Capture stream,Device %d\n",
  165. substream->pcm->device);
  166. }
  167. ret_val = stream->sstdrv_ops->pcm_control->open(&str_params);
  168. pr_debug("SST_SND_PLAY/CAPTURE ret_val = %x\n", ret_val);
  169. if (ret_val < 0)
  170. return ret_val;
  171. stream->stream_info.str_id = ret_val;
  172. pr_debug("str id : %d\n", stream->stream_info.str_id);
  173. return ret_val;
  174. }
  175. static void sst_period_elapsed(void *mad_substream)
  176. {
  177. struct snd_pcm_substream *substream = mad_substream;
  178. struct sst_runtime_stream *stream;
  179. int status;
  180. if (!substream || !substream->runtime)
  181. return;
  182. stream = substream->runtime->private_data;
  183. if (!stream)
  184. return;
  185. status = sst_get_stream_status(stream);
  186. if (status != SST_PLATFORM_RUNNING)
  187. return;
  188. snd_pcm_period_elapsed(substream);
  189. }
  190. static int sst_platform_init_stream(struct snd_pcm_substream *substream)
  191. {
  192. struct sst_runtime_stream *stream =
  193. substream->runtime->private_data;
  194. int ret_val;
  195. pr_debug("setting buffer ptr param\n");
  196. sst_set_stream_status(stream, SST_PLATFORM_INIT);
  197. stream->stream_info.period_elapsed = sst_period_elapsed;
  198. stream->stream_info.mad_substream = substream;
  199. stream->stream_info.buffer_ptr = 0;
  200. stream->stream_info.sfreq = substream->runtime->rate;
  201. ret_val = stream->sstdrv_ops->pcm_control->device_control(
  202. SST_SND_STREAM_INIT, &stream->stream_info);
  203. if (ret_val)
  204. pr_err("control_set ret error %d\n", ret_val);
  205. return ret_val;
  206. }
  207. /* end -- helper functions */
  208. static int sst_platform_open(struct snd_pcm_substream *substream)
  209. {
  210. struct snd_pcm_runtime *runtime;
  211. struct sst_runtime_stream *stream;
  212. int ret_val = 0;
  213. pr_debug("sst_platform_open called\n");
  214. runtime = substream->runtime;
  215. runtime->hw = sst_platform_pcm_hw;
  216. stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  217. if (!stream)
  218. return -ENOMEM;
  219. spin_lock_init(&stream->status_lock);
  220. stream->stream_info.str_id = 0;
  221. sst_set_stream_status(stream, SST_PLATFORM_INIT);
  222. stream->stream_info.mad_substream = substream;
  223. /* allocate memory for SST API set */
  224. stream->sstdrv_ops = kzalloc(sizeof(*stream->sstdrv_ops),
  225. GFP_KERNEL);
  226. if (!stream->sstdrv_ops) {
  227. pr_err("sst: mem allocation for ops fail\n");
  228. kfree(stream);
  229. return -ENOMEM;
  230. }
  231. stream->sstdrv_ops->vendor_id = MSIC_VENDOR_ID;
  232. /* registering with SST driver to get access to SST APIs to use */
  233. ret_val = register_sst_card(stream->sstdrv_ops);
  234. if (ret_val) {
  235. pr_err("sst: sst card registration failed\n");
  236. return ret_val;
  237. }
  238. runtime->private_data = stream;
  239. return snd_pcm_hw_constraint_integer(runtime,
  240. SNDRV_PCM_HW_PARAM_PERIODS);
  241. }
  242. static int sst_platform_close(struct snd_pcm_substream *substream)
  243. {
  244. struct sst_runtime_stream *stream;
  245. int ret_val = 0, str_id;
  246. pr_debug("sst_platform_close called\n");
  247. stream = substream->runtime->private_data;
  248. str_id = stream->stream_info.str_id;
  249. if (str_id)
  250. ret_val = stream->sstdrv_ops->pcm_control->close(str_id);
  251. kfree(stream->sstdrv_ops);
  252. kfree(stream);
  253. return ret_val;
  254. }
  255. static int sst_platform_pcm_prepare(struct snd_pcm_substream *substream)
  256. {
  257. struct sst_runtime_stream *stream;
  258. int ret_val = 0, str_id;
  259. pr_debug("sst_platform_pcm_prepare called\n");
  260. stream = substream->runtime->private_data;
  261. str_id = stream->stream_info.str_id;
  262. if (stream->stream_info.str_id) {
  263. ret_val = stream->sstdrv_ops->pcm_control->device_control(
  264. SST_SND_DROP, &str_id);
  265. return ret_val;
  266. }
  267. ret_val = sst_platform_alloc_stream(substream);
  268. if (ret_val < 0)
  269. return ret_val;
  270. snprintf(substream->pcm->id, sizeof(substream->pcm->id),
  271. "%d", stream->stream_info.str_id);
  272. ret_val = sst_platform_init_stream(substream);
  273. if (ret_val)
  274. return ret_val;
  275. substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
  276. return ret_val;
  277. }
  278. static int sst_platform_pcm_trigger(struct snd_pcm_substream *substream,
  279. int cmd)
  280. {
  281. int ret_val = 0, str_id;
  282. struct sst_runtime_stream *stream;
  283. int str_cmd, status;
  284. pr_debug("sst_platform_pcm_trigger called\n");
  285. stream = substream->runtime->private_data;
  286. str_id = stream->stream_info.str_id;
  287. switch (cmd) {
  288. case SNDRV_PCM_TRIGGER_START:
  289. pr_debug("sst: Trigger Start\n");
  290. str_cmd = SST_SND_START;
  291. status = SST_PLATFORM_RUNNING;
  292. stream->stream_info.mad_substream = substream;
  293. break;
  294. case SNDRV_PCM_TRIGGER_STOP:
  295. pr_debug("sst: in stop\n");
  296. str_cmd = SST_SND_DROP;
  297. status = SST_PLATFORM_DROPPED;
  298. break;
  299. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  300. pr_debug("sst: in pause\n");
  301. str_cmd = SST_SND_PAUSE;
  302. status = SST_PLATFORM_PAUSED;
  303. break;
  304. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  305. pr_debug("sst: in pause release\n");
  306. str_cmd = SST_SND_RESUME;
  307. status = SST_PLATFORM_RUNNING;
  308. break;
  309. default:
  310. return -EINVAL;
  311. }
  312. ret_val = stream->sstdrv_ops->pcm_control->device_control(str_cmd,
  313. &str_id);
  314. if (!ret_val)
  315. sst_set_stream_status(stream, status);
  316. return ret_val;
  317. }
  318. static snd_pcm_uframes_t sst_platform_pcm_pointer
  319. (struct snd_pcm_substream *substream)
  320. {
  321. struct sst_runtime_stream *stream;
  322. int ret_val, status;
  323. struct pcm_stream_info *str_info;
  324. stream = substream->runtime->private_data;
  325. status = sst_get_stream_status(stream);
  326. if (status == SST_PLATFORM_INIT)
  327. return 0;
  328. str_info = &stream->stream_info;
  329. ret_val = stream->sstdrv_ops->pcm_control->device_control(
  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 int sst_platform_pcm_hw_params(struct snd_pcm_substream *substream,
  338. struct snd_pcm_hw_params *params)
  339. {
  340. snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  341. memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
  342. return 0;
  343. }
  344. static struct snd_pcm_ops sst_platform_ops = {
  345. .open = sst_platform_open,
  346. .close = sst_platform_close,
  347. .ioctl = snd_pcm_lib_ioctl,
  348. .prepare = sst_platform_pcm_prepare,
  349. .trigger = sst_platform_pcm_trigger,
  350. .pointer = sst_platform_pcm_pointer,
  351. .hw_params = sst_platform_pcm_hw_params,
  352. };
  353. static void sst_pcm_free(struct snd_pcm *pcm)
  354. {
  355. pr_debug("sst_pcm_free called\n");
  356. snd_pcm_lib_preallocate_free_for_all(pcm);
  357. }
  358. int sst_pcm_new(struct snd_card *card, struct snd_soc_dai *dai,
  359. struct snd_pcm *pcm)
  360. {
  361. int retval = 0;
  362. pr_debug("sst_pcm_new called\n");
  363. if (dai->driver->playback.channels_min ||
  364. dai->driver->capture.channels_min) {
  365. retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
  366. SNDRV_DMA_TYPE_CONTINUOUS,
  367. snd_dma_continuous_data(GFP_KERNEL),
  368. SST_MIN_BUFFER, SST_MAX_BUFFER);
  369. if (retval) {
  370. pr_err("dma buffer allocationf fail\n");
  371. return retval;
  372. }
  373. }
  374. return retval;
  375. }
  376. struct snd_soc_platform_driver sst_soc_platform_drv = {
  377. .ops = &sst_platform_ops,
  378. .pcm_new = sst_pcm_new,
  379. .pcm_free = sst_pcm_free,
  380. };
  381. static int sst_platform_probe(struct platform_device *pdev)
  382. {
  383. int ret;
  384. pr_debug("sst_platform_probe called\n");
  385. ret = snd_soc_register_platform(&pdev->dev, &sst_soc_platform_drv);
  386. if (ret) {
  387. pr_err("registering soc platform failed\n");
  388. return ret;
  389. }
  390. ret = snd_soc_register_dais(&pdev->dev,
  391. sst_platform_dai, ARRAY_SIZE(sst_platform_dai));
  392. if (ret) {
  393. pr_err("registering cpu dais failed\n");
  394. snd_soc_unregister_platform(&pdev->dev);
  395. }
  396. return ret;
  397. }
  398. static int sst_platform_remove(struct platform_device *pdev)
  399. {
  400. snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(sst_platform_dai));
  401. snd_soc_unregister_platform(&pdev->dev);
  402. pr_debug("sst_platform_remove sucess\n");
  403. return 0;
  404. }
  405. static struct platform_driver sst_platform_driver = {
  406. .driver = {
  407. .name = "sst-platform",
  408. .owner = THIS_MODULE,
  409. },
  410. .probe = sst_platform_probe,
  411. .remove = sst_platform_remove,
  412. };
  413. static int __init sst_soc_platform_init(void)
  414. {
  415. pr_debug("sst_soc_platform_init called\n");
  416. return platform_driver_register(&sst_platform_driver);
  417. }
  418. module_init(sst_soc_platform_init);
  419. static void __exit sst_soc_platform_exit(void)
  420. {
  421. platform_driver_unregister(&sst_platform_driver);
  422. pr_debug("sst_soc_platform_exit sucess\n");
  423. }
  424. module_exit(sst_soc_platform_exit);
  425. MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");
  426. MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
  427. MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
  428. MODULE_LICENSE("GPL v2");
  429. MODULE_ALIAS("platform:sst-platform");