sst-mfld-platform-pcm.c 21 KB

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