sst_platform.c 13 KB

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