sst-mfld-platform-pcm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. * sst_mfld_platform.c - Intel MID Platform driver
  3. *
  4. * Copyright (C) 2010-2014 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. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/slab.h>
  22. #include <linux/io.h>
  23. #include <linux/module.h>
  24. #include <sound/core.h>
  25. #include <sound/pcm.h>
  26. #include <sound/pcm_params.h>
  27. #include <sound/soc.h>
  28. #include <sound/compress_driver.h>
  29. #include <asm/platform_sst_audio.h>
  30. #include "sst-mfld-platform.h"
  31. #include "sst-atom-controls.h"
  32. struct sst_device *sst;
  33. static DEFINE_MUTEX(sst_lock);
  34. int sst_register_dsp(struct sst_device *dev)
  35. {
  36. if (WARN_ON(!dev))
  37. return -EINVAL;
  38. if (!try_module_get(dev->dev->driver->owner))
  39. return -ENODEV;
  40. mutex_lock(&sst_lock);
  41. if (sst) {
  42. dev_err(dev->dev, "we already have a device %s\n", sst->name);
  43. module_put(dev->dev->driver->owner);
  44. mutex_unlock(&sst_lock);
  45. return -EEXIST;
  46. }
  47. dev_dbg(dev->dev, "registering device %s\n", dev->name);
  48. sst = dev;
  49. mutex_unlock(&sst_lock);
  50. return 0;
  51. }
  52. EXPORT_SYMBOL_GPL(sst_register_dsp);
  53. int sst_unregister_dsp(struct sst_device *dev)
  54. {
  55. if (WARN_ON(!dev))
  56. return -EINVAL;
  57. if (dev != sst)
  58. return -EINVAL;
  59. mutex_lock(&sst_lock);
  60. if (!sst) {
  61. mutex_unlock(&sst_lock);
  62. return -EIO;
  63. }
  64. module_put(sst->dev->driver->owner);
  65. dev_dbg(dev->dev, "unreg %s\n", sst->name);
  66. sst = NULL;
  67. mutex_unlock(&sst_lock);
  68. return 0;
  69. }
  70. EXPORT_SYMBOL_GPL(sst_unregister_dsp);
  71. static struct snd_pcm_hardware sst_platform_pcm_hw = {
  72. .info = (SNDRV_PCM_INFO_INTERLEAVED |
  73. SNDRV_PCM_INFO_DOUBLE |
  74. SNDRV_PCM_INFO_PAUSE |
  75. SNDRV_PCM_INFO_RESUME |
  76. SNDRV_PCM_INFO_MMAP|
  77. SNDRV_PCM_INFO_MMAP_VALID |
  78. SNDRV_PCM_INFO_BLOCK_TRANSFER |
  79. SNDRV_PCM_INFO_SYNC_START),
  80. .buffer_bytes_max = SST_MAX_BUFFER,
  81. .period_bytes_min = SST_MIN_PERIOD_BYTES,
  82. .period_bytes_max = SST_MAX_PERIOD_BYTES,
  83. .periods_min = SST_MIN_PERIODS,
  84. .periods_max = SST_MAX_PERIODS,
  85. .fifo_size = SST_FIFO_SIZE,
  86. };
  87. static struct sst_dev_stream_map dpcm_strm_map[] = {
  88. {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, /* Reserved, not in use */
  89. {MERR_DPCM_AUDIO, 0, SNDRV_PCM_STREAM_PLAYBACK, PIPE_MEDIA1_IN, SST_TASK_ID_MEDIA, 0},
  90. {MERR_DPCM_COMPR, 0, SNDRV_PCM_STREAM_PLAYBACK, PIPE_MEDIA0_IN, SST_TASK_ID_MEDIA, 0},
  91. {MERR_DPCM_AUDIO, 0, SNDRV_PCM_STREAM_CAPTURE, PIPE_PCM1_OUT, SST_TASK_ID_MEDIA, 0},
  92. };
  93. static int sst_media_digital_mute(struct snd_soc_dai *dai, int mute, int stream)
  94. {
  95. return sst_send_pipe_gains(dai, stream, mute);
  96. }
  97. /* helper functions */
  98. void sst_set_stream_status(struct sst_runtime_stream *stream,
  99. int state)
  100. {
  101. unsigned long flags;
  102. spin_lock_irqsave(&stream->status_lock, flags);
  103. stream->stream_status = state;
  104. spin_unlock_irqrestore(&stream->status_lock, flags);
  105. }
  106. static inline int sst_get_stream_status(struct sst_runtime_stream *stream)
  107. {
  108. int state;
  109. unsigned long flags;
  110. spin_lock_irqsave(&stream->status_lock, flags);
  111. state = stream->stream_status;
  112. spin_unlock_irqrestore(&stream->status_lock, flags);
  113. return state;
  114. }
  115. static void sst_fill_alloc_params(struct snd_pcm_substream *substream,
  116. struct snd_sst_alloc_params_ext *alloc_param)
  117. {
  118. unsigned int channels;
  119. snd_pcm_uframes_t period_size;
  120. ssize_t periodbytes;
  121. ssize_t buffer_bytes = snd_pcm_lib_buffer_bytes(substream);
  122. u32 buffer_addr = virt_to_phys(substream->dma_buffer.area);
  123. channels = substream->runtime->channels;
  124. period_size = substream->runtime->period_size;
  125. periodbytes = samples_to_bytes(substream->runtime, period_size);
  126. alloc_param->ring_buf_info[0].addr = buffer_addr;
  127. alloc_param->ring_buf_info[0].size = buffer_bytes;
  128. alloc_param->sg_count = 1;
  129. alloc_param->reserved = 0;
  130. alloc_param->frag_size = periodbytes * channels;
  131. }
  132. static void sst_fill_pcm_params(struct snd_pcm_substream *substream,
  133. struct snd_sst_stream_params *param)
  134. {
  135. param->uc.pcm_params.num_chan = (u8) substream->runtime->channels;
  136. param->uc.pcm_params.pcm_wd_sz = substream->runtime->sample_bits;
  137. param->uc.pcm_params.sfreq = substream->runtime->rate;
  138. /* PCM stream via ALSA interface */
  139. param->uc.pcm_params.use_offload_path = 0;
  140. param->uc.pcm_params.reserved2 = 0;
  141. memset(param->uc.pcm_params.channel_map, 0, sizeof(u8));
  142. }
  143. static int sst_get_stream_mapping(int dev, int sdev, int dir,
  144. struct sst_dev_stream_map *map, int size)
  145. {
  146. int i;
  147. if (map == NULL)
  148. return -EINVAL;
  149. /* index 0 is not used in stream map */
  150. for (i = 1; i < size; i++) {
  151. if ((map[i].dev_num == dev) && (map[i].direction == dir))
  152. return i;
  153. }
  154. return 0;
  155. }
  156. int sst_fill_stream_params(void *substream,
  157. const struct sst_data *ctx, struct snd_sst_params *str_params, bool is_compress)
  158. {
  159. int map_size;
  160. int index;
  161. struct sst_dev_stream_map *map;
  162. struct snd_pcm_substream *pstream = NULL;
  163. struct snd_compr_stream *cstream = NULL;
  164. map = ctx->pdata->pdev_strm_map;
  165. map_size = ctx->pdata->strm_map_size;
  166. if (is_compress == true)
  167. cstream = (struct snd_compr_stream *)substream;
  168. else
  169. pstream = (struct snd_pcm_substream *)substream;
  170. str_params->stream_type = SST_STREAM_TYPE_MUSIC;
  171. /* For pcm streams */
  172. if (pstream) {
  173. index = sst_get_stream_mapping(pstream->pcm->device,
  174. pstream->number, pstream->stream,
  175. map, map_size);
  176. if (index <= 0)
  177. return -EINVAL;
  178. str_params->stream_id = index;
  179. str_params->device_type = map[index].device_id;
  180. str_params->task = map[index].task_id;
  181. str_params->ops = (u8)pstream->stream;
  182. }
  183. if (cstream) {
  184. index = sst_get_stream_mapping(cstream->device->device,
  185. 0, cstream->direction,
  186. map, map_size);
  187. if (index <= 0)
  188. return -EINVAL;
  189. str_params->stream_id = index;
  190. str_params->device_type = map[index].device_id;
  191. str_params->task = map[index].task_id;
  192. str_params->ops = (u8)cstream->direction;
  193. }
  194. return 0;
  195. }
  196. static int sst_platform_alloc_stream(struct snd_pcm_substream *substream,
  197. struct snd_soc_dai *dai)
  198. {
  199. struct sst_runtime_stream *stream =
  200. substream->runtime->private_data;
  201. struct snd_sst_stream_params param = {{{0,},},};
  202. struct snd_sst_params str_params = {0};
  203. struct snd_sst_alloc_params_ext alloc_params = {0};
  204. int ret_val = 0;
  205. struct sst_data *ctx = snd_soc_dai_get_drvdata(dai);
  206. /* set codec params and inform SST driver the same */
  207. sst_fill_pcm_params(substream, &param);
  208. sst_fill_alloc_params(substream, &alloc_params);
  209. substream->runtime->dma_area = substream->dma_buffer.area;
  210. str_params.sparams = param;
  211. str_params.aparams = alloc_params;
  212. str_params.codec = SST_CODEC_TYPE_PCM;
  213. /* fill the device type and stream id to pass to SST driver */
  214. ret_val = sst_fill_stream_params(substream, ctx, &str_params, false);
  215. if (ret_val < 0)
  216. return ret_val;
  217. stream->stream_info.str_id = str_params.stream_id;
  218. ret_val = stream->ops->open(sst->dev, &str_params);
  219. if (ret_val <= 0)
  220. return ret_val;
  221. return ret_val;
  222. }
  223. static void sst_period_elapsed(void *arg)
  224. {
  225. struct snd_pcm_substream *substream = arg;
  226. struct sst_runtime_stream *stream;
  227. int status;
  228. if (!substream || !substream->runtime)
  229. return;
  230. stream = substream->runtime->private_data;
  231. if (!stream)
  232. return;
  233. status = sst_get_stream_status(stream);
  234. if (status != SST_PLATFORM_RUNNING)
  235. return;
  236. snd_pcm_period_elapsed(substream);
  237. }
  238. static int sst_platform_init_stream(struct snd_pcm_substream *substream)
  239. {
  240. struct sst_runtime_stream *stream =
  241. substream->runtime->private_data;
  242. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  243. int ret_val;
  244. dev_dbg(rtd->dev, "setting buffer ptr param\n");
  245. sst_set_stream_status(stream, SST_PLATFORM_INIT);
  246. stream->stream_info.period_elapsed = sst_period_elapsed;
  247. stream->stream_info.arg = substream;
  248. stream->stream_info.buffer_ptr = 0;
  249. stream->stream_info.sfreq = substream->runtime->rate;
  250. ret_val = stream->ops->stream_init(sst->dev, &stream->stream_info);
  251. if (ret_val)
  252. dev_err(rtd->dev, "control_set ret error %d\n", ret_val);
  253. return ret_val;
  254. }
  255. static int power_up_sst(struct sst_runtime_stream *stream)
  256. {
  257. return stream->ops->power(sst->dev, true);
  258. }
  259. static void power_down_sst(struct sst_runtime_stream *stream)
  260. {
  261. stream->ops->power(sst->dev, false);
  262. }
  263. static int sst_media_open(struct snd_pcm_substream *substream,
  264. struct snd_soc_dai *dai)
  265. {
  266. int ret_val = 0;
  267. struct snd_pcm_runtime *runtime = substream->runtime;
  268. struct sst_runtime_stream *stream;
  269. stream = kzalloc(sizeof(*stream), GFP_KERNEL);
  270. if (!stream)
  271. return -ENOMEM;
  272. spin_lock_init(&stream->status_lock);
  273. /* get the sst ops */
  274. mutex_lock(&sst_lock);
  275. if (!sst ||
  276. !try_module_get(sst->dev->driver->owner)) {
  277. dev_err(dai->dev, "no device available to run\n");
  278. ret_val = -ENODEV;
  279. goto out_ops;
  280. }
  281. stream->ops = sst->ops;
  282. mutex_unlock(&sst_lock);
  283. stream->stream_info.str_id = 0;
  284. stream->stream_info.arg = substream;
  285. /* allocate memory for SST API set */
  286. runtime->private_data = stream;
  287. ret_val = power_up_sst(stream);
  288. if (ret_val < 0)
  289. return ret_val;
  290. /* Make sure, that the period size is always even */
  291. snd_pcm_hw_constraint_step(substream->runtime, 0,
  292. SNDRV_PCM_HW_PARAM_PERIODS, 2);
  293. return snd_pcm_hw_constraint_integer(runtime,
  294. SNDRV_PCM_HW_PARAM_PERIODS);
  295. out_ops:
  296. kfree(stream);
  297. mutex_unlock(&sst_lock);
  298. return ret_val;
  299. }
  300. static void sst_media_close(struct snd_pcm_substream *substream,
  301. struct snd_soc_dai *dai)
  302. {
  303. struct sst_runtime_stream *stream;
  304. int ret_val = 0, str_id;
  305. stream = substream->runtime->private_data;
  306. power_down_sst(stream);
  307. str_id = stream->stream_info.str_id;
  308. if (str_id)
  309. ret_val = stream->ops->close(sst->dev, str_id);
  310. module_put(sst->dev->driver->owner);
  311. kfree(stream);
  312. }
  313. static inline unsigned int get_current_pipe_id(struct snd_soc_dai *dai,
  314. struct snd_pcm_substream *substream)
  315. {
  316. struct sst_data *sst = snd_soc_dai_get_drvdata(dai);
  317. struct sst_dev_stream_map *map = sst->pdata->pdev_strm_map;
  318. struct sst_runtime_stream *stream =
  319. substream->runtime->private_data;
  320. u32 str_id = stream->stream_info.str_id;
  321. unsigned int pipe_id;
  322. pipe_id = map[str_id].device_id;
  323. dev_dbg(dai->dev, "got pipe_id = %#x for str_id = %d\n",
  324. pipe_id, str_id);
  325. return pipe_id;
  326. }
  327. static int sst_media_prepare(struct snd_pcm_substream *substream,
  328. struct snd_soc_dai *dai)
  329. {
  330. struct sst_runtime_stream *stream;
  331. int ret_val = 0, str_id;
  332. stream = substream->runtime->private_data;
  333. str_id = stream->stream_info.str_id;
  334. if (stream->stream_info.str_id) {
  335. ret_val = stream->ops->stream_drop(sst->dev, str_id);
  336. return ret_val;
  337. }
  338. ret_val = sst_platform_alloc_stream(substream, dai);
  339. if (ret_val <= 0)
  340. return ret_val;
  341. snprintf(substream->pcm->id, sizeof(substream->pcm->id),
  342. "%d", stream->stream_info.str_id);
  343. ret_val = sst_platform_init_stream(substream);
  344. if (ret_val)
  345. return ret_val;
  346. substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
  347. return ret_val;
  348. }
  349. static int sst_media_hw_params(struct snd_pcm_substream *substream,
  350. struct snd_pcm_hw_params *params,
  351. struct snd_soc_dai *dai)
  352. {
  353. snd_pcm_lib_malloc_pages(substream, params_buffer_bytes(params));
  354. memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
  355. return 0;
  356. }
  357. static int sst_media_hw_free(struct snd_pcm_substream *substream,
  358. struct snd_soc_dai *dai)
  359. {
  360. return snd_pcm_lib_free_pages(substream);
  361. }
  362. static int sst_enable_ssp(struct snd_pcm_substream *substream,
  363. struct snd_soc_dai *dai)
  364. {
  365. int ret = 0;
  366. if (!dai->active) {
  367. ret = sst_handle_vb_timer(dai, true);
  368. sst_fill_ssp_defaults(dai);
  369. }
  370. return ret;
  371. }
  372. static int sst_be_hw_params(struct snd_pcm_substream *substream,
  373. struct snd_pcm_hw_params *params,
  374. struct snd_soc_dai *dai)
  375. {
  376. int ret = 0;
  377. if (dai->active == 1)
  378. ret = send_ssp_cmd(dai, dai->name, 1);
  379. return ret;
  380. }
  381. static int sst_set_format(struct snd_soc_dai *dai, unsigned int fmt)
  382. {
  383. int ret = 0;
  384. if (!dai->active)
  385. return 0;
  386. ret = sst_fill_ssp_config(dai, fmt);
  387. if (ret < 0)
  388. dev_err(dai->dev, "sst_set_format failed..\n");
  389. return ret;
  390. }
  391. static int sst_platform_set_ssp_slot(struct snd_soc_dai *dai,
  392. unsigned int tx_mask, unsigned int rx_mask,
  393. int slots, int slot_width) {
  394. int ret = 0;
  395. if (!dai->active)
  396. return ret;
  397. ret = sst_fill_ssp_slot(dai, tx_mask, rx_mask, slots, slot_width);
  398. if (ret < 0)
  399. dev_err(dai->dev, "sst_fill_ssp_slot failed..%d\n", ret);
  400. return ret;
  401. }
  402. static void sst_disable_ssp(struct snd_pcm_substream *substream,
  403. struct snd_soc_dai *dai)
  404. {
  405. if (!dai->active) {
  406. send_ssp_cmd(dai, dai->name, 0);
  407. sst_handle_vb_timer(dai, false);
  408. }
  409. }
  410. static struct snd_soc_dai_ops sst_media_dai_ops = {
  411. .startup = sst_media_open,
  412. .shutdown = sst_media_close,
  413. .prepare = sst_media_prepare,
  414. .hw_params = sst_media_hw_params,
  415. .hw_free = sst_media_hw_free,
  416. .mute_stream = sst_media_digital_mute,
  417. };
  418. static struct snd_soc_dai_ops sst_compr_dai_ops = {
  419. .mute_stream = sst_media_digital_mute,
  420. };
  421. static struct snd_soc_dai_ops sst_be_dai_ops = {
  422. .startup = sst_enable_ssp,
  423. .hw_params = sst_be_hw_params,
  424. .set_fmt = sst_set_format,
  425. .set_tdm_slot = sst_platform_set_ssp_slot,
  426. .shutdown = sst_disable_ssp,
  427. };
  428. static struct snd_soc_dai_driver sst_platform_dai[] = {
  429. {
  430. .name = "media-cpu-dai",
  431. .ops = &sst_media_dai_ops,
  432. .playback = {
  433. .stream_name = "Headset Playback",
  434. .channels_min = SST_STEREO,
  435. .channels_max = SST_STEREO,
  436. .rates = SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000,
  437. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  438. },
  439. .capture = {
  440. .stream_name = "Headset Capture",
  441. .channels_min = 1,
  442. .channels_max = 2,
  443. .rates = SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000,
  444. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  445. },
  446. },
  447. {
  448. .name = "compress-cpu-dai",
  449. .compress_dai = 1,
  450. .ops = &sst_compr_dai_ops,
  451. .playback = {
  452. .stream_name = "Compress Playback",
  453. .channels_min = SST_STEREO,
  454. .channels_max = SST_STEREO,
  455. .rates = SNDRV_PCM_RATE_48000,
  456. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  457. },
  458. },
  459. /* BE CPU Dais */
  460. {
  461. .name = "ssp0-port",
  462. .ops = &sst_be_dai_ops,
  463. .playback = {
  464. .stream_name = "ssp0 Tx",
  465. .channels_min = SST_STEREO,
  466. .channels_max = SST_STEREO,
  467. .rates = SNDRV_PCM_RATE_48000,
  468. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  469. },
  470. .capture = {
  471. .stream_name = "ssp0 Rx",
  472. .channels_min = SST_STEREO,
  473. .channels_max = SST_STEREO,
  474. .rates = SNDRV_PCM_RATE_48000,
  475. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  476. },
  477. },
  478. {
  479. .name = "ssp1-port",
  480. .ops = &sst_be_dai_ops,
  481. .playback = {
  482. .stream_name = "ssp1 Tx",
  483. .channels_min = SST_STEREO,
  484. .channels_max = SST_STEREO,
  485. .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000,
  486. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  487. },
  488. .capture = {
  489. .stream_name = "ssp1 Rx",
  490. .channels_min = SST_STEREO,
  491. .channels_max = SST_STEREO,
  492. .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000,
  493. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  494. },
  495. },
  496. {
  497. .name = "ssp2-port",
  498. .ops = &sst_be_dai_ops,
  499. .playback = {
  500. .stream_name = "ssp2 Tx",
  501. .channels_min = SST_STEREO,
  502. .channels_max = SST_STEREO,
  503. .rates = SNDRV_PCM_RATE_48000,
  504. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  505. },
  506. .capture = {
  507. .stream_name = "ssp2 Rx",
  508. .channels_min = SST_STEREO,
  509. .channels_max = SST_STEREO,
  510. .rates = SNDRV_PCM_RATE_48000,
  511. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  512. },
  513. },
  514. };
  515. static int sst_platform_open(struct snd_pcm_substream *substream)
  516. {
  517. struct snd_pcm_runtime *runtime;
  518. if (substream->pcm->internal)
  519. return 0;
  520. runtime = substream->runtime;
  521. runtime->hw = sst_platform_pcm_hw;
  522. return 0;
  523. }
  524. static int sst_platform_pcm_trigger(struct snd_pcm_substream *substream,
  525. int cmd)
  526. {
  527. int ret_val = 0, str_id;
  528. struct sst_runtime_stream *stream;
  529. int status;
  530. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  531. dev_dbg(rtd->dev, "sst_platform_pcm_trigger called\n");
  532. if (substream->pcm->internal)
  533. return 0;
  534. stream = substream->runtime->private_data;
  535. str_id = stream->stream_info.str_id;
  536. switch (cmd) {
  537. case SNDRV_PCM_TRIGGER_START:
  538. dev_dbg(rtd->dev, "sst: Trigger Start\n");
  539. status = SST_PLATFORM_RUNNING;
  540. stream->stream_info.arg = substream;
  541. ret_val = stream->ops->stream_start(sst->dev, str_id);
  542. break;
  543. case SNDRV_PCM_TRIGGER_STOP:
  544. dev_dbg(rtd->dev, "sst: in stop\n");
  545. status = SST_PLATFORM_DROPPED;
  546. ret_val = stream->ops->stream_drop(sst->dev, str_id);
  547. break;
  548. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  549. case SNDRV_PCM_TRIGGER_SUSPEND:
  550. dev_dbg(rtd->dev, "sst: in pause\n");
  551. status = SST_PLATFORM_PAUSED;
  552. ret_val = stream->ops->stream_pause(sst->dev, str_id);
  553. break;
  554. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  555. case SNDRV_PCM_TRIGGER_RESUME:
  556. dev_dbg(rtd->dev, "sst: in pause release\n");
  557. status = SST_PLATFORM_RUNNING;
  558. ret_val = stream->ops->stream_pause_release(sst->dev, str_id);
  559. break;
  560. default:
  561. return -EINVAL;
  562. }
  563. if (!ret_val)
  564. sst_set_stream_status(stream, status);
  565. return ret_val;
  566. }
  567. static snd_pcm_uframes_t sst_platform_pcm_pointer
  568. (struct snd_pcm_substream *substream)
  569. {
  570. struct sst_runtime_stream *stream;
  571. int ret_val, status;
  572. struct pcm_stream_info *str_info;
  573. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  574. stream = substream->runtime->private_data;
  575. status = sst_get_stream_status(stream);
  576. if (status == SST_PLATFORM_INIT)
  577. return 0;
  578. str_info = &stream->stream_info;
  579. ret_val = stream->ops->stream_read_tstamp(sst->dev, str_info);
  580. if (ret_val) {
  581. dev_err(rtd->dev, "sst: error code = %d\n", ret_val);
  582. return ret_val;
  583. }
  584. substream->runtime->delay = str_info->pcm_delay;
  585. return str_info->buffer_ptr;
  586. }
  587. static struct snd_pcm_ops sst_platform_ops = {
  588. .open = sst_platform_open,
  589. .ioctl = snd_pcm_lib_ioctl,
  590. .trigger = sst_platform_pcm_trigger,
  591. .pointer = sst_platform_pcm_pointer,
  592. };
  593. static int sst_pcm_new(struct snd_soc_pcm_runtime *rtd)
  594. {
  595. struct snd_soc_dai *dai = rtd->cpu_dai;
  596. struct snd_pcm *pcm = rtd->pcm;
  597. int retval = 0;
  598. if (dai->driver->playback.channels_min ||
  599. dai->driver->capture.channels_min) {
  600. retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
  601. SNDRV_DMA_TYPE_CONTINUOUS,
  602. snd_dma_continuous_data(GFP_DMA),
  603. SST_MIN_BUFFER, SST_MAX_BUFFER);
  604. if (retval) {
  605. dev_err(rtd->dev, "dma buffer allocationf fail\n");
  606. return retval;
  607. }
  608. }
  609. return retval;
  610. }
  611. static int sst_soc_probe(struct snd_soc_platform *platform)
  612. {
  613. struct sst_data *drv = dev_get_drvdata(platform->dev);
  614. drv->soc_card = platform->component.card;
  615. return sst_dsp_init_v2_dpcm(platform);
  616. }
  617. static struct snd_soc_platform_driver sst_soc_platform_drv = {
  618. .probe = sst_soc_probe,
  619. .ops = &sst_platform_ops,
  620. .compr_ops = &sst_platform_compr_ops,
  621. .pcm_new = sst_pcm_new,
  622. };
  623. static const struct snd_soc_component_driver sst_component = {
  624. .name = "sst",
  625. };
  626. static int sst_platform_probe(struct platform_device *pdev)
  627. {
  628. struct sst_data *drv;
  629. int ret;
  630. struct sst_platform_data *pdata;
  631. drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
  632. if (drv == NULL) {
  633. return -ENOMEM;
  634. }
  635. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  636. if (pdata == NULL) {
  637. return -ENOMEM;
  638. }
  639. pdata->pdev_strm_map = dpcm_strm_map;
  640. pdata->strm_map_size = ARRAY_SIZE(dpcm_strm_map);
  641. drv->pdata = pdata;
  642. drv->pdev = pdev;
  643. mutex_init(&drv->lock);
  644. dev_set_drvdata(&pdev->dev, drv);
  645. ret = snd_soc_register_platform(&pdev->dev, &sst_soc_platform_drv);
  646. if (ret) {
  647. dev_err(&pdev->dev, "registering soc platform failed\n");
  648. return ret;
  649. }
  650. ret = snd_soc_register_component(&pdev->dev, &sst_component,
  651. sst_platform_dai, ARRAY_SIZE(sst_platform_dai));
  652. if (ret) {
  653. dev_err(&pdev->dev, "registering cpu dais failed\n");
  654. snd_soc_unregister_platform(&pdev->dev);
  655. }
  656. return ret;
  657. }
  658. static int sst_platform_remove(struct platform_device *pdev)
  659. {
  660. snd_soc_unregister_component(&pdev->dev);
  661. snd_soc_unregister_platform(&pdev->dev);
  662. dev_dbg(&pdev->dev, "sst_platform_remove success\n");
  663. return 0;
  664. }
  665. #ifdef CONFIG_PM_SLEEP
  666. static int sst_soc_prepare(struct device *dev)
  667. {
  668. struct sst_data *drv = dev_get_drvdata(dev);
  669. int i;
  670. /* suspend all pcms first */
  671. snd_soc_suspend(drv->soc_card->dev);
  672. snd_soc_poweroff(drv->soc_card->dev);
  673. /* set the SSPs to idle */
  674. for (i = 0; i < drv->soc_card->num_rtd; i++) {
  675. struct snd_soc_dai *dai = drv->soc_card->rtd[i].cpu_dai;
  676. if (dai->active) {
  677. send_ssp_cmd(dai, dai->name, 0);
  678. sst_handle_vb_timer(dai, false);
  679. }
  680. }
  681. return 0;
  682. }
  683. static void sst_soc_complete(struct device *dev)
  684. {
  685. struct sst_data *drv = dev_get_drvdata(dev);
  686. int i;
  687. /* restart SSPs */
  688. for (i = 0; i < drv->soc_card->num_rtd; i++) {
  689. struct snd_soc_dai *dai = drv->soc_card->rtd[i].cpu_dai;
  690. if (dai->active) {
  691. sst_handle_vb_timer(dai, true);
  692. send_ssp_cmd(dai, dai->name, 1);
  693. }
  694. }
  695. snd_soc_resume(drv->soc_card->dev);
  696. }
  697. #else
  698. #define sst_soc_prepare NULL
  699. #define sst_soc_complete NULL
  700. #endif
  701. static const struct dev_pm_ops sst_platform_pm = {
  702. .prepare = sst_soc_prepare,
  703. .complete = sst_soc_complete,
  704. };
  705. static struct platform_driver sst_platform_driver = {
  706. .driver = {
  707. .name = "sst-mfld-platform",
  708. .pm = &sst_platform_pm,
  709. },
  710. .probe = sst_platform_probe,
  711. .remove = sst_platform_remove,
  712. };
  713. module_platform_driver(sst_platform_driver);
  714. MODULE_DESCRIPTION("ASoC Intel(R) MID Platform driver");
  715. MODULE_AUTHOR("Vinod Koul <vinod.koul@intel.com>");
  716. MODULE_AUTHOR("Harsha Priya <priya.harsha@intel.com>");
  717. MODULE_LICENSE("GPL v2");
  718. MODULE_ALIAS("platform:sst-mfld-platform");