sst_platform.c 14 KB

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