sst-mfld-platform-pcm.c 21 KB

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