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 const 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. goto out_power_up;
  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. mutex_unlock(&sst_lock);
  298. out_power_up:
  299. kfree(stream);
  300. return ret_val;
  301. }
  302. static void sst_media_close(struct snd_pcm_substream *substream,
  303. struct snd_soc_dai *dai)
  304. {
  305. struct sst_runtime_stream *stream;
  306. int str_id;
  307. stream = substream->runtime->private_data;
  308. power_down_sst(stream);
  309. str_id = stream->stream_info.str_id;
  310. if (str_id)
  311. stream->ops->close(sst->dev, str_id);
  312. module_put(sst->dev->driver->owner);
  313. kfree(stream);
  314. }
  315. static int sst_media_prepare(struct snd_pcm_substream *substream,
  316. struct snd_soc_dai *dai)
  317. {
  318. struct sst_runtime_stream *stream;
  319. int ret_val = 0, str_id;
  320. stream = substream->runtime->private_data;
  321. str_id = stream->stream_info.str_id;
  322. if (stream->stream_info.str_id) {
  323. ret_val = stream->ops->stream_drop(sst->dev, str_id);
  324. return ret_val;
  325. }
  326. ret_val = sst_platform_alloc_stream(substream, dai);
  327. if (ret_val <= 0)
  328. return ret_val;
  329. snprintf(substream->pcm->id, sizeof(substream->pcm->id),
  330. "%d", stream->stream_info.str_id);
  331. ret_val = sst_platform_init_stream(substream);
  332. if (ret_val)
  333. return ret_val;
  334. substream->runtime->hw.info = SNDRV_PCM_INFO_BLOCK_TRANSFER;
  335. return ret_val;
  336. }
  337. static int sst_media_hw_params(struct snd_pcm_substream *substream,
  338. struct snd_pcm_hw_params *params,
  339. struct snd_soc_dai *dai)
  340. {
  341. int ret;
  342. ret =
  343. snd_pcm_lib_malloc_pages(substream,
  344. params_buffer_bytes(params));
  345. if (ret)
  346. return ret;
  347. memset(substream->runtime->dma_area, 0, params_buffer_bytes(params));
  348. return 0;
  349. }
  350. static int sst_media_hw_free(struct snd_pcm_substream *substream,
  351. struct snd_soc_dai *dai)
  352. {
  353. return snd_pcm_lib_free_pages(substream);
  354. }
  355. static int sst_enable_ssp(struct snd_pcm_substream *substream,
  356. struct snd_soc_dai *dai)
  357. {
  358. int ret = 0;
  359. if (!dai->active) {
  360. ret = sst_handle_vb_timer(dai, true);
  361. sst_fill_ssp_defaults(dai);
  362. }
  363. return ret;
  364. }
  365. static int sst_be_hw_params(struct snd_pcm_substream *substream,
  366. struct snd_pcm_hw_params *params,
  367. struct snd_soc_dai *dai)
  368. {
  369. int ret = 0;
  370. if (dai->active == 1)
  371. ret = send_ssp_cmd(dai, dai->name, 1);
  372. return ret;
  373. }
  374. static int sst_set_format(struct snd_soc_dai *dai, unsigned int fmt)
  375. {
  376. int ret = 0;
  377. if (!dai->active)
  378. return 0;
  379. ret = sst_fill_ssp_config(dai, fmt);
  380. if (ret < 0)
  381. dev_err(dai->dev, "sst_set_format failed..\n");
  382. return ret;
  383. }
  384. static int sst_platform_set_ssp_slot(struct snd_soc_dai *dai,
  385. unsigned int tx_mask, unsigned int rx_mask,
  386. int slots, int slot_width) {
  387. int ret = 0;
  388. if (!dai->active)
  389. return ret;
  390. ret = sst_fill_ssp_slot(dai, tx_mask, rx_mask, slots, slot_width);
  391. if (ret < 0)
  392. dev_err(dai->dev, "sst_fill_ssp_slot failed..%d\n", ret);
  393. return ret;
  394. }
  395. static void sst_disable_ssp(struct snd_pcm_substream *substream,
  396. struct snd_soc_dai *dai)
  397. {
  398. if (!dai->active) {
  399. send_ssp_cmd(dai, dai->name, 0);
  400. sst_handle_vb_timer(dai, false);
  401. }
  402. }
  403. static const struct snd_soc_dai_ops sst_media_dai_ops = {
  404. .startup = sst_media_open,
  405. .shutdown = sst_media_close,
  406. .prepare = sst_media_prepare,
  407. .hw_params = sst_media_hw_params,
  408. .hw_free = sst_media_hw_free,
  409. .mute_stream = sst_media_digital_mute,
  410. };
  411. static const struct snd_soc_dai_ops sst_compr_dai_ops = {
  412. .mute_stream = sst_media_digital_mute,
  413. };
  414. static const struct snd_soc_dai_ops sst_be_dai_ops = {
  415. .startup = sst_enable_ssp,
  416. .hw_params = sst_be_hw_params,
  417. .set_fmt = sst_set_format,
  418. .set_tdm_slot = sst_platform_set_ssp_slot,
  419. .shutdown = sst_disable_ssp,
  420. };
  421. static struct snd_soc_dai_driver sst_platform_dai[] = {
  422. {
  423. .name = "media-cpu-dai",
  424. .ops = &sst_media_dai_ops,
  425. .playback = {
  426. .stream_name = "Headset Playback",
  427. .channels_min = SST_STEREO,
  428. .channels_max = SST_STEREO,
  429. .rates = SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000,
  430. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  431. },
  432. .capture = {
  433. .stream_name = "Headset Capture",
  434. .channels_min = 1,
  435. .channels_max = 2,
  436. .rates = SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000,
  437. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  438. },
  439. },
  440. {
  441. .name = "deepbuffer-cpu-dai",
  442. .ops = &sst_media_dai_ops,
  443. .playback = {
  444. .stream_name = "Deepbuffer Playback",
  445. .channels_min = SST_STEREO,
  446. .channels_max = SST_STEREO,
  447. .rates = SNDRV_PCM_RATE_44100|SNDRV_PCM_RATE_48000,
  448. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  449. },
  450. },
  451. {
  452. .name = "compress-cpu-dai",
  453. .compress_new = snd_soc_new_compress,
  454. .ops = &sst_compr_dai_ops,
  455. .playback = {
  456. .stream_name = "Compress Playback",
  457. .channels_min = 1,
  458. },
  459. },
  460. /* BE CPU Dais */
  461. {
  462. .name = "ssp0-port",
  463. .ops = &sst_be_dai_ops,
  464. .playback = {
  465. .stream_name = "ssp0 Tx",
  466. .channels_min = SST_STEREO,
  467. .channels_max = SST_STEREO,
  468. .rates = SNDRV_PCM_RATE_48000,
  469. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  470. },
  471. .capture = {
  472. .stream_name = "ssp0 Rx",
  473. .channels_min = SST_STEREO,
  474. .channels_max = SST_STEREO,
  475. .rates = SNDRV_PCM_RATE_48000,
  476. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  477. },
  478. },
  479. {
  480. .name = "ssp1-port",
  481. .ops = &sst_be_dai_ops,
  482. .playback = {
  483. .stream_name = "ssp1 Tx",
  484. .channels_min = SST_STEREO,
  485. .channels_max = SST_STEREO,
  486. .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000,
  487. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  488. },
  489. .capture = {
  490. .stream_name = "ssp1 Rx",
  491. .channels_min = SST_STEREO,
  492. .channels_max = SST_STEREO,
  493. .rates = SNDRV_PCM_RATE_8000|SNDRV_PCM_RATE_16000|SNDRV_PCM_RATE_48000,
  494. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  495. },
  496. },
  497. {
  498. .name = "ssp2-port",
  499. .ops = &sst_be_dai_ops,
  500. .playback = {
  501. .stream_name = "ssp2 Tx",
  502. .channels_min = SST_STEREO,
  503. .channels_max = SST_STEREO,
  504. .rates = SNDRV_PCM_RATE_48000,
  505. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  506. },
  507. .capture = {
  508. .stream_name = "ssp2 Rx",
  509. .channels_min = SST_STEREO,
  510. .channels_max = SST_STEREO,
  511. .rates = SNDRV_PCM_RATE_48000,
  512. .formats = SNDRV_PCM_FMTBIT_S16_LE,
  513. },
  514. },
  515. };
  516. static int sst_platform_open(struct snd_pcm_substream *substream)
  517. {
  518. struct snd_pcm_runtime *runtime;
  519. if (substream->pcm->internal)
  520. return 0;
  521. runtime = substream->runtime;
  522. runtime->hw = sst_platform_pcm_hw;
  523. return 0;
  524. }
  525. static int sst_platform_pcm_trigger(struct snd_pcm_substream *substream,
  526. int cmd)
  527. {
  528. int ret_val = 0, str_id;
  529. struct sst_runtime_stream *stream;
  530. int status;
  531. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  532. dev_dbg(rtd->dev, "sst_platform_pcm_trigger called\n");
  533. if (substream->pcm->internal)
  534. return 0;
  535. stream = substream->runtime->private_data;
  536. str_id = stream->stream_info.str_id;
  537. switch (cmd) {
  538. case SNDRV_PCM_TRIGGER_START:
  539. dev_dbg(rtd->dev, "sst: Trigger Start\n");
  540. status = SST_PLATFORM_RUNNING;
  541. stream->stream_info.arg = substream;
  542. ret_val = stream->ops->stream_start(sst->dev, str_id);
  543. break;
  544. case SNDRV_PCM_TRIGGER_STOP:
  545. dev_dbg(rtd->dev, "sst: in stop\n");
  546. status = SST_PLATFORM_DROPPED;
  547. ret_val = stream->ops->stream_drop(sst->dev, str_id);
  548. break;
  549. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  550. case SNDRV_PCM_TRIGGER_SUSPEND:
  551. dev_dbg(rtd->dev, "sst: in pause\n");
  552. status = SST_PLATFORM_PAUSED;
  553. ret_val = stream->ops->stream_pause(sst->dev, str_id);
  554. break;
  555. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  556. case SNDRV_PCM_TRIGGER_RESUME:
  557. dev_dbg(rtd->dev, "sst: in pause release\n");
  558. status = SST_PLATFORM_RUNNING;
  559. ret_val = stream->ops->stream_pause_release(sst->dev, str_id);
  560. break;
  561. default:
  562. return -EINVAL;
  563. }
  564. if (!ret_val)
  565. sst_set_stream_status(stream, status);
  566. return ret_val;
  567. }
  568. static snd_pcm_uframes_t sst_platform_pcm_pointer
  569. (struct snd_pcm_substream *substream)
  570. {
  571. struct sst_runtime_stream *stream;
  572. int ret_val, status;
  573. struct pcm_stream_info *str_info;
  574. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  575. stream = substream->runtime->private_data;
  576. status = sst_get_stream_status(stream);
  577. if (status == SST_PLATFORM_INIT)
  578. return 0;
  579. str_info = &stream->stream_info;
  580. ret_val = stream->ops->stream_read_tstamp(sst->dev, str_info);
  581. if (ret_val) {
  582. dev_err(rtd->dev, "sst: error code = %d\n", ret_val);
  583. return ret_val;
  584. }
  585. substream->runtime->delay = str_info->pcm_delay;
  586. return str_info->buffer_ptr;
  587. }
  588. static const struct snd_pcm_ops sst_platform_ops = {
  589. .open = sst_platform_open,
  590. .ioctl = snd_pcm_lib_ioctl,
  591. .trigger = sst_platform_pcm_trigger,
  592. .pointer = sst_platform_pcm_pointer,
  593. };
  594. static int sst_pcm_new(struct snd_soc_pcm_runtime *rtd)
  595. {
  596. struct snd_soc_dai *dai = rtd->cpu_dai;
  597. struct snd_pcm *pcm = rtd->pcm;
  598. int retval = 0;
  599. if (dai->driver->playback.channels_min ||
  600. dai->driver->capture.channels_min) {
  601. retval = snd_pcm_lib_preallocate_pages_for_all(pcm,
  602. SNDRV_DMA_TYPE_CONTINUOUS,
  603. snd_dma_continuous_data(GFP_DMA),
  604. SST_MIN_BUFFER, SST_MAX_BUFFER);
  605. if (retval) {
  606. dev_err(rtd->dev, "dma buffer allocation failure\n");
  607. return retval;
  608. }
  609. }
  610. return retval;
  611. }
  612. static int sst_soc_probe(struct snd_soc_component *component)
  613. {
  614. struct sst_data *drv = dev_get_drvdata(component->dev);
  615. drv->soc_card = component->card;
  616. return sst_dsp_init_v2_dpcm(component);
  617. }
  618. static void sst_soc_remove(struct snd_soc_component *component)
  619. {
  620. struct sst_data *drv = dev_get_drvdata(component->dev);
  621. drv->soc_card = NULL;
  622. }
  623. static const struct snd_soc_component_driver sst_soc_platform_drv = {
  624. .name = DRV_NAME,
  625. .probe = sst_soc_probe,
  626. .remove = sst_soc_remove,
  627. .ops = &sst_platform_ops,
  628. .compr_ops = &sst_platform_compr_ops,
  629. .pcm_new = sst_pcm_new,
  630. };
  631. static int sst_platform_probe(struct platform_device *pdev)
  632. {
  633. struct sst_data *drv;
  634. int ret;
  635. struct sst_platform_data *pdata;
  636. drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
  637. if (drv == NULL) {
  638. return -ENOMEM;
  639. }
  640. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  641. if (pdata == NULL) {
  642. return -ENOMEM;
  643. }
  644. pdata->pdev_strm_map = dpcm_strm_map;
  645. pdata->strm_map_size = ARRAY_SIZE(dpcm_strm_map);
  646. drv->pdata = pdata;
  647. drv->pdev = pdev;
  648. mutex_init(&drv->lock);
  649. dev_set_drvdata(&pdev->dev, drv);
  650. ret = devm_snd_soc_register_component(&pdev->dev, &sst_soc_platform_drv,
  651. sst_platform_dai, ARRAY_SIZE(sst_platform_dai));
  652. if (ret)
  653. dev_err(&pdev->dev, "registering cpu dais failed\n");
  654. return ret;
  655. }
  656. static int sst_platform_remove(struct platform_device *pdev)
  657. {
  658. dev_dbg(&pdev->dev, "sst_platform_remove success\n");
  659. return 0;
  660. }
  661. #ifdef CONFIG_PM_SLEEP
  662. static int sst_soc_prepare(struct device *dev)
  663. {
  664. struct sst_data *drv = dev_get_drvdata(dev);
  665. struct snd_soc_pcm_runtime *rtd;
  666. if (!drv->soc_card)
  667. return 0;
  668. /* suspend all pcms first */
  669. snd_soc_suspend(drv->soc_card->dev);
  670. snd_soc_poweroff(drv->soc_card->dev);
  671. /* set the SSPs to idle */
  672. list_for_each_entry(rtd, &drv->soc_card->rtd_list, list) {
  673. struct snd_soc_dai *dai = rtd->cpu_dai;
  674. if (dai->active) {
  675. send_ssp_cmd(dai, dai->name, 0);
  676. sst_handle_vb_timer(dai, false);
  677. }
  678. }
  679. return 0;
  680. }
  681. static void sst_soc_complete(struct device *dev)
  682. {
  683. struct sst_data *drv = dev_get_drvdata(dev);
  684. struct snd_soc_pcm_runtime *rtd;
  685. if (!drv->soc_card)
  686. return;
  687. /* restart SSPs */
  688. list_for_each_entry(rtd, &drv->soc_card->rtd_list, list) {
  689. struct snd_soc_dai *dai = rtd->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-atom-hifi2-platform");
  719. MODULE_ALIAS("platform:sst-mfld-platform");