sst_platform.c 14 KB

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