sst_platform.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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 = substream->runtime;
  213. struct sst_runtime_stream *stream;
  214. int ret_val = 0;
  215. pr_debug("sst_platform_open called\n");
  216. snd_soc_set_runtime_hwparams(substream, &sst_platform_pcm_hw);
  217. stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  218. if (!stream)
  219. return -ENOMEM;
  220. spin_lock_init(&stream->status_lock);
  221. stream->stream_info.str_id = 0;
  222. sst_set_stream_status(stream, SST_PLATFORM_INIT);
  223. stream->stream_info.mad_substream = substream;
  224. /* allocate memory for SST API set */
  225. stream->sstdrv_ops = kzalloc(sizeof(*stream->sstdrv_ops),
  226. GFP_KERNEL);
  227. if (!stream->sstdrv_ops) {
  228. pr_err("sst: mem allocation for ops fail\n");
  229. kfree(stream);
  230. return -ENOMEM;
  231. }
  232. stream->sstdrv_ops->vendor_id = MSIC_VENDOR_ID;
  233. stream->sstdrv_ops->module_name = SST_CARD_NAMES;
  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. kfree(stream->sstdrv_ops);
  239. kfree(stream);
  240. return ret_val;
  241. }
  242. runtime->private_data = stream;
  243. return snd_pcm_hw_constraint_integer(runtime,
  244. SNDRV_PCM_HW_PARAM_PERIODS);
  245. }
  246. static int sst_platform_close(struct snd_pcm_substream *substream)
  247. {
  248. struct sst_runtime_stream *stream;
  249. int ret_val = 0, str_id;
  250. pr_debug("sst_platform_close called\n");
  251. stream = substream->runtime->private_data;
  252. str_id = stream->stream_info.str_id;
  253. if (str_id)
  254. ret_val = stream->sstdrv_ops->pcm_control->close(str_id);
  255. unregister_sst_card(stream->sstdrv_ops);
  256. kfree(stream->sstdrv_ops);
  257. kfree(stream);
  258. return ret_val;
  259. }
  260. static int sst_platform_pcm_prepare(struct snd_pcm_substream *substream)
  261. {
  262. struct sst_runtime_stream *stream;
  263. int ret_val = 0, str_id;
  264. pr_debug("sst_platform_pcm_prepare called\n");
  265. stream = substream->runtime->private_data;
  266. str_id = stream->stream_info.str_id;
  267. if (stream->stream_info.str_id) {
  268. ret_val = stream->sstdrv_ops->pcm_control->device_control(
  269. SST_SND_DROP, &str_id);
  270. return ret_val;
  271. }
  272. ret_val = sst_platform_alloc_stream(substream);
  273. if (ret_val < 0)
  274. return ret_val;
  275. snprintf(substream->pcm->id, sizeof(substream->pcm->id),
  276. "%d", stream->stream_info.str_id);
  277. ret_val = sst_platform_init_stream(substream);
  278. if (ret_val)
  279. return ret_val;
  280. substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
  281. return ret_val;
  282. }
  283. static int sst_platform_pcm_trigger(struct snd_pcm_substream *substream,
  284. int cmd)
  285. {
  286. int ret_val = 0, str_id;
  287. struct sst_runtime_stream *stream;
  288. int str_cmd, status;
  289. pr_debug("sst_platform_pcm_trigger called\n");
  290. stream = substream->runtime->private_data;
  291. str_id = stream->stream_info.str_id;
  292. switch (cmd) {
  293. case SNDRV_PCM_TRIGGER_START:
  294. pr_debug("sst: Trigger Start\n");
  295. str_cmd = SST_SND_START;
  296. status = SST_PLATFORM_RUNNING;
  297. stream->stream_info.mad_substream = substream;
  298. break;
  299. case SNDRV_PCM_TRIGGER_STOP:
  300. pr_debug("sst: in stop\n");
  301. str_cmd = SST_SND_DROP;
  302. status = SST_PLATFORM_DROPPED;
  303. break;
  304. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  305. pr_debug("sst: in pause\n");
  306. str_cmd = SST_SND_PAUSE;
  307. status = SST_PLATFORM_PAUSED;
  308. break;
  309. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  310. pr_debug("sst: in pause release\n");
  311. str_cmd = SST_SND_RESUME;
  312. status = SST_PLATFORM_RUNNING;
  313. break;
  314. default:
  315. return -EINVAL;
  316. }
  317. ret_val = stream->sstdrv_ops->pcm_control->device_control(str_cmd,
  318. &str_id);
  319. if (!ret_val)
  320. sst_set_stream_status(stream, status);
  321. return ret_val;
  322. }
  323. static snd_pcm_uframes_t sst_platform_pcm_pointer
  324. (struct snd_pcm_substream *substream)
  325. {
  326. struct sst_runtime_stream *stream;
  327. int ret_val, status;
  328. struct pcm_stream_info *str_info;
  329. stream = substream->runtime->private_data;
  330. status = sst_get_stream_status(stream);
  331. if (status == SST_PLATFORM_INIT)
  332. return 0;
  333. str_info = &stream->stream_info;
  334. ret_val = stream->sstdrv_ops->pcm_control->device_control(
  335. SST_SND_BUFFER_POINTER, str_info);
  336. if (ret_val) {
  337. pr_err("sst: error code = %d\n", ret_val);
  338. return ret_val;
  339. }
  340. return stream->stream_info.buffer_ptr;
  341. }
  342. static int sst_platform_pcm_hw_params(struct snd_pcm_substream *substream,
  343. struct snd_pcm_hw_params *params)
  344. {
  345. snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  346. memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
  347. return 0;
  348. }
  349. static int sst_platform_pcm_hw_free(struct snd_pcm_substream *substream)
  350. {
  351. return snd_pcm_lib_free_pages(substream);
  352. }
  353. static struct snd_pcm_ops sst_platform_ops = {
  354. .open = sst_platform_open,
  355. .close = sst_platform_close,
  356. .ioctl = snd_pcm_lib_ioctl,
  357. .prepare = sst_platform_pcm_prepare,
  358. .trigger = sst_platform_pcm_trigger,
  359. .pointer = sst_platform_pcm_pointer,
  360. .hw_params = sst_platform_pcm_hw_params,
  361. .hw_free = sst_platform_pcm_hw_free,
  362. };
  363. static void sst_pcm_free(struct snd_pcm *pcm)
  364. {
  365. pr_debug("sst_pcm_free called\n");
  366. snd_pcm_lib_preallocate_free_for_all(pcm);
  367. }
  368. int sst_pcm_new(struct snd_soc_pcm_runtime *rtd)
  369. {
  370. struct snd_soc_dai *dai = rtd->cpu_dai;
  371. struct snd_pcm *pcm = rtd->pcm;
  372. int retval = 0;
  373. pr_debug("sst_pcm_new called\n");
  374. if (dai->driver->playback.channels_min ||
  375. dai->driver->capture.channels_min) {
  376. retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
  377. SNDRV_DMA_TYPE_CONTINUOUS,
  378. snd_dma_continuous_data(GFP_KERNEL),
  379. SST_MIN_BUFFER, SST_MAX_BUFFER);
  380. if (retval) {
  381. pr_err("dma buffer allocationf fail\n");
  382. return retval;
  383. }
  384. }
  385. return retval;
  386. }
  387. struct snd_soc_platform_driver sst_soc_platform_drv = {
  388. .ops = &sst_platform_ops,
  389. .pcm_new = sst_pcm_new,
  390. .pcm_free = sst_pcm_free,
  391. };
  392. static int sst_platform_probe(struct platform_device *pdev)
  393. {
  394. int ret;
  395. pr_debug("sst_platform_probe called\n");
  396. ret = snd_soc_register_platform(&pdev->dev, &sst_soc_platform_drv);
  397. if (ret) {
  398. pr_err("registering soc platform failed\n");
  399. return ret;
  400. }
  401. ret = snd_soc_register_dais(&pdev->dev,
  402. sst_platform_dai, ARRAY_SIZE(sst_platform_dai));
  403. if (ret) {
  404. pr_err("registering cpu dais failed\n");
  405. snd_soc_unregister_platform(&pdev->dev);
  406. }
  407. return ret;
  408. }
  409. static int sst_platform_remove(struct platform_device *pdev)
  410. {
  411. snd_soc_unregister_dais(&pdev->dev, ARRAY_SIZE(sst_platform_dai));
  412. snd_soc_unregister_platform(&pdev->dev);
  413. pr_debug("sst_platform_remove success\n");
  414. return 0;
  415. }
  416. static struct platform_driver sst_platform_driver = {
  417. .driver = {
  418. .name = "sst-platform",
  419. .owner = THIS_MODULE,
  420. },
  421. .probe = sst_platform_probe,
  422. .remove = sst_platform_remove,
  423. };
  424. static int __init sst_soc_platform_init(void)
  425. {
  426. pr_debug("sst_soc_platform_init called\n");
  427. return platform_driver_register(&sst_platform_driver);
  428. }
  429. module_init(sst_soc_platform_init);
  430. static void __exit sst_soc_platform_exit(void)
  431. {
  432. platform_driver_unregister(&sst_platform_driver);
  433. pr_debug("sst_soc_platform_exit success\n");
  434. }
  435. module_exit(sst_soc_platform_exit);
  436. MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");
  437. MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
  438. MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
  439. MODULE_LICENSE("GPL v2");
  440. MODULE_ALIAS("platform:sst-platform");