sst_platform.c 14 KB

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