sst_drv_interface.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * sst_drv_interface.c - Intel SST Driver for audio engine
  4. *
  5. * Copyright (C) 2008-14 Intel Corp
  6. * Authors: Vinod Koul <vinod.koul@intel.com>
  7. * Harsha Priya <priya.harsha@intel.com>
  8. * Dharageswari R <dharageswari.r@intel.com)
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. *
  11. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  12. */
  13. #include <linux/delay.h>
  14. #include <linux/pci.h>
  15. #include <linux/fs.h>
  16. #include <linux/firmware.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/pm_qos.h>
  19. #include <linux/math64.h>
  20. #include <sound/core.h>
  21. #include <sound/pcm.h>
  22. #include <sound/soc.h>
  23. #include <sound/compress_driver.h>
  24. #include <asm/platform_sst_audio.h>
  25. #include "../sst-mfld-platform.h"
  26. #include "sst.h"
  27. #define NUM_CODEC 2
  28. #define MIN_FRAGMENT 2
  29. #define MAX_FRAGMENT 4
  30. #define MIN_FRAGMENT_SIZE (50 * 1024)
  31. #define MAX_FRAGMENT_SIZE (1024 * 1024)
  32. #define SST_GET_BYTES_PER_SAMPLE(pcm_wd_sz) (((pcm_wd_sz + 15) >> 4) << 1)
  33. #ifdef CONFIG_PM
  34. #define GET_USAGE_COUNT(dev) (atomic_read(&dev->power.usage_count))
  35. #else
  36. #define GET_USAGE_COUNT(dev) 1
  37. #endif
  38. int free_stream_context(struct intel_sst_drv *ctx, unsigned int str_id)
  39. {
  40. struct stream_info *stream;
  41. int ret = 0;
  42. stream = get_stream_info(ctx, str_id);
  43. if (stream) {
  44. /* str_id is valid, so stream is alloacted */
  45. ret = sst_free_stream(ctx, str_id);
  46. if (ret)
  47. sst_clean_stream(&ctx->streams[str_id]);
  48. return ret;
  49. } else {
  50. dev_err(ctx->dev, "we tried to free stream context %d which was freed!!!\n", str_id);
  51. }
  52. return ret;
  53. }
  54. int sst_get_stream_allocated(struct intel_sst_drv *ctx,
  55. struct snd_sst_params *str_param,
  56. struct snd_sst_lib_download **lib_dnld)
  57. {
  58. int retval;
  59. retval = ctx->ops->alloc_stream(ctx, str_param);
  60. if (retval > 0)
  61. dev_dbg(ctx->dev, "Stream allocated %d\n", retval);
  62. return retval;
  63. }
  64. /*
  65. * sst_get_sfreq - this function returns the frequency of the stream
  66. *
  67. * @str_param : stream params
  68. */
  69. int sst_get_sfreq(struct snd_sst_params *str_param)
  70. {
  71. switch (str_param->codec) {
  72. case SST_CODEC_TYPE_PCM:
  73. return str_param->sparams.uc.pcm_params.sfreq;
  74. case SST_CODEC_TYPE_AAC:
  75. return str_param->sparams.uc.aac_params.externalsr;
  76. case SST_CODEC_TYPE_MP3:
  77. return 0;
  78. default:
  79. return -EINVAL;
  80. }
  81. }
  82. /*
  83. * sst_get_num_channel - get number of channels for the stream
  84. *
  85. * @str_param : stream params
  86. */
  87. int sst_get_num_channel(struct snd_sst_params *str_param)
  88. {
  89. switch (str_param->codec) {
  90. case SST_CODEC_TYPE_PCM:
  91. return str_param->sparams.uc.pcm_params.num_chan;
  92. case SST_CODEC_TYPE_MP3:
  93. return str_param->sparams.uc.mp3_params.num_chan;
  94. case SST_CODEC_TYPE_AAC:
  95. return str_param->sparams.uc.aac_params.num_chan;
  96. default:
  97. return -EINVAL;
  98. }
  99. }
  100. /*
  101. * sst_get_stream - this function prepares for stream allocation
  102. *
  103. * @str_param : stream param
  104. */
  105. int sst_get_stream(struct intel_sst_drv *ctx,
  106. struct snd_sst_params *str_param)
  107. {
  108. int retval;
  109. struct stream_info *str_info;
  110. /* stream is not allocated, we are allocating */
  111. retval = ctx->ops->alloc_stream(ctx, str_param);
  112. if (retval <= 0) {
  113. return -EIO;
  114. }
  115. /* store sampling freq */
  116. str_info = &ctx->streams[retval];
  117. str_info->sfreq = sst_get_sfreq(str_param);
  118. return retval;
  119. }
  120. static int sst_power_control(struct device *dev, bool state)
  121. {
  122. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  123. int ret = 0;
  124. int usage_count = 0;
  125. if (state) {
  126. ret = pm_runtime_resume_and_get(dev);
  127. usage_count = GET_USAGE_COUNT(dev);
  128. dev_dbg(ctx->dev, "Enable: pm usage count: %d\n", usage_count);
  129. if (ret < 0) {
  130. dev_err(ctx->dev, "Runtime get failed with err: %d\n", ret);
  131. return ret;
  132. }
  133. if ((ctx->sst_state == SST_RESET) && (usage_count == 1)) {
  134. ret = sst_load_fw(ctx);
  135. if (ret) {
  136. dev_err(dev, "FW download fail %d\n", ret);
  137. sst_set_fw_state_locked(ctx, SST_RESET);
  138. ret = sst_pm_runtime_put(ctx);
  139. }
  140. }
  141. } else {
  142. usage_count = GET_USAGE_COUNT(dev);
  143. dev_dbg(ctx->dev, "Disable: pm usage count: %d\n", usage_count);
  144. return sst_pm_runtime_put(ctx);
  145. }
  146. return ret;
  147. }
  148. /*
  149. * sst_open_pcm_stream - Open PCM interface
  150. *
  151. * @str_param: parameters of pcm stream
  152. *
  153. * This function is called by MID sound card driver to open
  154. * a new pcm interface
  155. */
  156. static int sst_open_pcm_stream(struct device *dev,
  157. struct snd_sst_params *str_param)
  158. {
  159. int retval;
  160. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  161. if (!str_param)
  162. return -EINVAL;
  163. retval = sst_get_stream(ctx, str_param);
  164. if (retval > 0)
  165. ctx->stream_cnt++;
  166. else
  167. dev_err(ctx->dev, "sst_get_stream returned err %d\n", retval);
  168. return retval;
  169. }
  170. static int sst_cdev_open(struct device *dev,
  171. struct snd_sst_params *str_params, struct sst_compress_cb *cb)
  172. {
  173. int str_id, retval;
  174. struct stream_info *stream;
  175. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  176. retval = pm_runtime_resume_and_get(ctx->dev);
  177. if (retval < 0)
  178. return retval;
  179. str_id = sst_get_stream(ctx, str_params);
  180. if (str_id > 0) {
  181. dev_dbg(dev, "stream allocated in sst_cdev_open %d\n", str_id);
  182. stream = &ctx->streams[str_id];
  183. stream->compr_cb = cb->compr_cb;
  184. stream->compr_cb_param = cb->param;
  185. stream->drain_notify = cb->drain_notify;
  186. stream->drain_cb_param = cb->drain_cb_param;
  187. } else {
  188. dev_err(dev, "stream encountered error during alloc %d\n", str_id);
  189. str_id = -EINVAL;
  190. sst_pm_runtime_put(ctx);
  191. }
  192. return str_id;
  193. }
  194. static int sst_cdev_close(struct device *dev, unsigned int str_id)
  195. {
  196. int retval;
  197. struct stream_info *stream;
  198. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  199. stream = get_stream_info(ctx, str_id);
  200. if (!stream) {
  201. dev_err(dev, "stream info is NULL for str %d!!!\n", str_id);
  202. return -EINVAL;
  203. }
  204. retval = sst_free_stream(ctx, str_id);
  205. stream->compr_cb_param = NULL;
  206. stream->compr_cb = NULL;
  207. if (retval)
  208. dev_err(dev, "free stream returned err %d\n", retval);
  209. dev_dbg(dev, "End\n");
  210. return retval;
  211. }
  212. static int sst_cdev_ack(struct device *dev, unsigned int str_id,
  213. unsigned long bytes)
  214. {
  215. struct stream_info *stream;
  216. struct snd_sst_tstamp fw_tstamp = {0,};
  217. int offset;
  218. void __iomem *addr;
  219. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  220. stream = get_stream_info(ctx, str_id);
  221. if (!stream)
  222. return -EINVAL;
  223. /* update bytes sent */
  224. stream->cumm_bytes += bytes;
  225. dev_dbg(dev, "bytes copied %d inc by %ld\n", stream->cumm_bytes, bytes);
  226. addr = ((void __iomem *)(ctx->mailbox + ctx->tstamp)) +
  227. (str_id * sizeof(fw_tstamp));
  228. memcpy_fromio(&fw_tstamp, addr, sizeof(fw_tstamp));
  229. fw_tstamp.bytes_copied = stream->cumm_bytes;
  230. dev_dbg(dev, "bytes sent to fw %llu inc by %ld\n",
  231. fw_tstamp.bytes_copied, bytes);
  232. offset = offsetof(struct snd_sst_tstamp, bytes_copied);
  233. sst_shim_write(addr, offset, fw_tstamp.bytes_copied);
  234. return 0;
  235. }
  236. static int sst_cdev_set_metadata(struct device *dev,
  237. unsigned int str_id, struct snd_compr_metadata *metadata)
  238. {
  239. int retval = 0;
  240. struct stream_info *str_info;
  241. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  242. dev_dbg(dev, "set metadata for stream %d\n", str_id);
  243. str_info = get_stream_info(ctx, str_id);
  244. if (!str_info)
  245. return -EINVAL;
  246. dev_dbg(dev, "pipe id = %d\n", str_info->pipe_id);
  247. retval = sst_prepare_and_post_msg(ctx, str_info->task_id, IPC_CMD,
  248. IPC_IA_SET_STREAM_PARAMS_MRFLD, str_info->pipe_id,
  249. sizeof(*metadata), metadata, NULL,
  250. true, true, true, false);
  251. return retval;
  252. }
  253. static int sst_cdev_stream_pause(struct device *dev, unsigned int str_id)
  254. {
  255. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  256. return sst_pause_stream(ctx, str_id);
  257. }
  258. static int sst_cdev_stream_pause_release(struct device *dev,
  259. unsigned int str_id)
  260. {
  261. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  262. return sst_resume_stream(ctx, str_id);
  263. }
  264. static int sst_cdev_stream_start(struct device *dev, unsigned int str_id)
  265. {
  266. struct stream_info *str_info;
  267. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  268. str_info = get_stream_info(ctx, str_id);
  269. if (!str_info)
  270. return -EINVAL;
  271. str_info->prev = str_info->status;
  272. str_info->status = STREAM_RUNNING;
  273. return sst_start_stream(ctx, str_id);
  274. }
  275. static int sst_cdev_stream_drop(struct device *dev, unsigned int str_id)
  276. {
  277. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  278. return sst_drop_stream(ctx, str_id);
  279. }
  280. static int sst_cdev_stream_drain(struct device *dev, unsigned int str_id)
  281. {
  282. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  283. return sst_drain_stream(ctx, str_id, false);
  284. }
  285. static int sst_cdev_stream_partial_drain(struct device *dev,
  286. unsigned int str_id)
  287. {
  288. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  289. return sst_drain_stream(ctx, str_id, true);
  290. }
  291. static int sst_cdev_tstamp(struct device *dev, unsigned int str_id,
  292. struct snd_compr_tstamp *tstamp)
  293. {
  294. struct snd_sst_tstamp fw_tstamp = {0,};
  295. struct stream_info *stream;
  296. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  297. void __iomem *addr;
  298. addr = (void __iomem *)(ctx->mailbox + ctx->tstamp) +
  299. (str_id * sizeof(fw_tstamp));
  300. memcpy_fromio(&fw_tstamp, addr, sizeof(fw_tstamp));
  301. stream = get_stream_info(ctx, str_id);
  302. if (!stream)
  303. return -EINVAL;
  304. dev_dbg(dev, "rb_counter %llu in bytes\n", fw_tstamp.ring_buffer_counter);
  305. tstamp->copied_total = fw_tstamp.ring_buffer_counter;
  306. tstamp->pcm_frames = fw_tstamp.frames_decoded;
  307. tstamp->pcm_io_frames = div_u64(fw_tstamp.hardware_counter,
  308. (u64)stream->num_ch * SST_GET_BYTES_PER_SAMPLE(24));
  309. tstamp->sampling_rate = fw_tstamp.sampling_frequency;
  310. dev_dbg(dev, "PCM = %u\n", tstamp->pcm_io_frames);
  311. dev_dbg(dev, "Ptr Query on strid = %d copied_total %d, decodec %d\n",
  312. str_id, tstamp->copied_total, tstamp->pcm_frames);
  313. dev_dbg(dev, "rendered %d\n", tstamp->pcm_io_frames);
  314. return 0;
  315. }
  316. static int sst_cdev_caps(struct snd_compr_caps *caps)
  317. {
  318. caps->num_codecs = NUM_CODEC;
  319. caps->min_fragment_size = MIN_FRAGMENT_SIZE; /* 50KB */
  320. caps->max_fragment_size = MAX_FRAGMENT_SIZE; /* 1024KB */
  321. caps->min_fragments = MIN_FRAGMENT;
  322. caps->max_fragments = MAX_FRAGMENT;
  323. caps->codecs[0] = SND_AUDIOCODEC_MP3;
  324. caps->codecs[1] = SND_AUDIOCODEC_AAC;
  325. return 0;
  326. }
  327. static const struct snd_compr_codec_caps caps_mp3 = {
  328. .num_descriptors = 1,
  329. .descriptor[0].max_ch = 2,
  330. .descriptor[0].sample_rates[0] = 48000,
  331. .descriptor[0].sample_rates[1] = 44100,
  332. .descriptor[0].sample_rates[2] = 32000,
  333. .descriptor[0].sample_rates[3] = 16000,
  334. .descriptor[0].sample_rates[4] = 8000,
  335. .descriptor[0].num_sample_rates = 5,
  336. .descriptor[0].bit_rate[0] = 320,
  337. .descriptor[0].bit_rate[1] = 192,
  338. .descriptor[0].num_bitrates = 2,
  339. .descriptor[0].profiles = 0,
  340. .descriptor[0].modes = SND_AUDIOCHANMODE_MP3_STEREO,
  341. .descriptor[0].formats = 0,
  342. };
  343. static const struct snd_compr_codec_caps caps_aac = {
  344. .num_descriptors = 2,
  345. .descriptor[1].max_ch = 2,
  346. .descriptor[0].sample_rates[0] = 48000,
  347. .descriptor[0].sample_rates[1] = 44100,
  348. .descriptor[0].sample_rates[2] = 32000,
  349. .descriptor[0].sample_rates[3] = 16000,
  350. .descriptor[0].sample_rates[4] = 8000,
  351. .descriptor[0].num_sample_rates = 5,
  352. .descriptor[1].bit_rate[0] = 320,
  353. .descriptor[1].bit_rate[1] = 192,
  354. .descriptor[1].num_bitrates = 2,
  355. .descriptor[1].profiles = 0,
  356. .descriptor[1].modes = 0,
  357. .descriptor[1].formats =
  358. (SND_AUDIOSTREAMFORMAT_MP4ADTS |
  359. SND_AUDIOSTREAMFORMAT_RAW),
  360. };
  361. static int sst_cdev_codec_caps(struct snd_compr_codec_caps *codec)
  362. {
  363. if (codec->codec == SND_AUDIOCODEC_MP3)
  364. *codec = caps_mp3;
  365. else if (codec->codec == SND_AUDIOCODEC_AAC)
  366. *codec = caps_aac;
  367. else
  368. return -EINVAL;
  369. return 0;
  370. }
  371. void sst_cdev_fragment_elapsed(struct intel_sst_drv *ctx, int str_id)
  372. {
  373. struct stream_info *stream;
  374. dev_dbg(ctx->dev, "fragment elapsed from firmware for str_id %d\n",
  375. str_id);
  376. stream = &ctx->streams[str_id];
  377. if (stream->compr_cb)
  378. stream->compr_cb(stream->compr_cb_param);
  379. }
  380. /*
  381. * sst_close_pcm_stream - Close PCM interface
  382. *
  383. * @str_id: stream id to be closed
  384. *
  385. * This function is called by MID sound card driver to close
  386. * an existing pcm interface
  387. */
  388. static int sst_close_pcm_stream(struct device *dev, unsigned int str_id)
  389. {
  390. struct stream_info *stream;
  391. int retval = 0;
  392. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  393. stream = get_stream_info(ctx, str_id);
  394. if (!stream) {
  395. dev_err(ctx->dev, "stream info is NULL for str %d!!!\n", str_id);
  396. return -EINVAL;
  397. }
  398. retval = free_stream_context(ctx, str_id);
  399. stream->pcm_substream = NULL;
  400. stream->status = STREAM_UN_INIT;
  401. stream->period_elapsed = NULL;
  402. ctx->stream_cnt--;
  403. if (retval)
  404. dev_err(ctx->dev, "free stream returned err %d\n", retval);
  405. dev_dbg(ctx->dev, "Exit\n");
  406. return 0;
  407. }
  408. static inline int sst_calc_tstamp(struct intel_sst_drv *ctx,
  409. struct pcm_stream_info *info,
  410. struct snd_pcm_substream *substream,
  411. struct snd_sst_tstamp *fw_tstamp)
  412. {
  413. size_t delay_bytes, delay_frames;
  414. size_t buffer_sz;
  415. u32 pointer_bytes, pointer_samples;
  416. dev_dbg(ctx->dev, "mrfld ring_buffer_counter %llu in bytes\n",
  417. fw_tstamp->ring_buffer_counter);
  418. dev_dbg(ctx->dev, "mrfld hardware_counter %llu in bytes\n",
  419. fw_tstamp->hardware_counter);
  420. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
  421. delay_bytes = (size_t) (fw_tstamp->ring_buffer_counter -
  422. fw_tstamp->hardware_counter);
  423. else
  424. delay_bytes = (size_t) (fw_tstamp->hardware_counter -
  425. fw_tstamp->ring_buffer_counter);
  426. delay_frames = bytes_to_frames(substream->runtime, delay_bytes);
  427. buffer_sz = snd_pcm_lib_buffer_bytes(substream);
  428. div_u64_rem(fw_tstamp->ring_buffer_counter, buffer_sz, &pointer_bytes);
  429. pointer_samples = bytes_to_samples(substream->runtime, pointer_bytes);
  430. dev_dbg(ctx->dev, "pcm delay %zu in bytes\n", delay_bytes);
  431. info->buffer_ptr = pointer_samples / substream->runtime->channels;
  432. info->pcm_delay = delay_frames;
  433. dev_dbg(ctx->dev, "buffer ptr %llu pcm_delay rep: %llu\n",
  434. info->buffer_ptr, info->pcm_delay);
  435. return 0;
  436. }
  437. static int sst_read_timestamp(struct device *dev, struct pcm_stream_info *info)
  438. {
  439. struct stream_info *stream;
  440. struct snd_pcm_substream *substream;
  441. struct snd_sst_tstamp fw_tstamp;
  442. unsigned int str_id;
  443. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  444. void __iomem *addr;
  445. str_id = info->str_id;
  446. stream = get_stream_info(ctx, str_id);
  447. if (!stream)
  448. return -EINVAL;
  449. if (!stream->pcm_substream)
  450. return -EINVAL;
  451. substream = stream->pcm_substream;
  452. addr = (void __iomem *)(ctx->mailbox + ctx->tstamp) +
  453. (str_id * sizeof(fw_tstamp));
  454. memcpy_fromio(&fw_tstamp, addr, sizeof(fw_tstamp));
  455. return sst_calc_tstamp(ctx, info, substream, &fw_tstamp);
  456. }
  457. static int sst_stream_start(struct device *dev, int str_id)
  458. {
  459. struct stream_info *str_info;
  460. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  461. if (ctx->sst_state != SST_FW_RUNNING)
  462. return 0;
  463. str_info = get_stream_info(ctx, str_id);
  464. if (!str_info)
  465. return -EINVAL;
  466. str_info->prev = str_info->status;
  467. str_info->status = STREAM_RUNNING;
  468. sst_start_stream(ctx, str_id);
  469. return 0;
  470. }
  471. static int sst_stream_drop(struct device *dev, int str_id)
  472. {
  473. struct stream_info *str_info;
  474. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  475. if (ctx->sst_state != SST_FW_RUNNING)
  476. return 0;
  477. str_info = get_stream_info(ctx, str_id);
  478. if (!str_info)
  479. return -EINVAL;
  480. str_info->prev = STREAM_UN_INIT;
  481. str_info->status = STREAM_INIT;
  482. return sst_drop_stream(ctx, str_id);
  483. }
  484. static int sst_stream_pause(struct device *dev, int str_id)
  485. {
  486. struct stream_info *str_info;
  487. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  488. if (ctx->sst_state != SST_FW_RUNNING)
  489. return 0;
  490. str_info = get_stream_info(ctx, str_id);
  491. if (!str_info)
  492. return -EINVAL;
  493. return sst_pause_stream(ctx, str_id);
  494. }
  495. static int sst_stream_resume(struct device *dev, int str_id)
  496. {
  497. struct stream_info *str_info;
  498. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  499. if (ctx->sst_state != SST_FW_RUNNING)
  500. return 0;
  501. str_info = get_stream_info(ctx, str_id);
  502. if (!str_info)
  503. return -EINVAL;
  504. return sst_resume_stream(ctx, str_id);
  505. }
  506. static int sst_stream_init(struct device *dev, struct pcm_stream_info *str_info)
  507. {
  508. int str_id = 0;
  509. struct stream_info *stream;
  510. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  511. str_id = str_info->str_id;
  512. if (ctx->sst_state != SST_FW_RUNNING)
  513. return 0;
  514. stream = get_stream_info(ctx, str_id);
  515. if (!stream)
  516. return -EINVAL;
  517. dev_dbg(ctx->dev, "setting the period ptrs\n");
  518. stream->pcm_substream = str_info->arg;
  519. stream->period_elapsed = str_info->period_elapsed;
  520. stream->sfreq = str_info->sfreq;
  521. stream->prev = stream->status;
  522. stream->status = STREAM_INIT;
  523. dev_dbg(ctx->dev,
  524. "pcm_substream %p, period_elapsed %p, sfreq %d, status %d\n",
  525. stream->pcm_substream, stream->period_elapsed,
  526. stream->sfreq, stream->status);
  527. return 0;
  528. }
  529. /*
  530. * sst_set_byte_stream - Set generic params
  531. *
  532. * @cmd: control cmd to be set
  533. * @arg: command argument
  534. *
  535. * This function is called by MID sound card driver to configure
  536. * SST runtime params.
  537. */
  538. static int sst_send_byte_stream(struct device *dev,
  539. struct snd_sst_bytes_v2 *bytes)
  540. {
  541. int ret_val = 0;
  542. struct intel_sst_drv *ctx = dev_get_drvdata(dev);
  543. if (NULL == bytes)
  544. return -EINVAL;
  545. ret_val = pm_runtime_resume_and_get(ctx->dev);
  546. if (ret_val < 0)
  547. return ret_val;
  548. ret_val = sst_send_byte_stream_mrfld(ctx, bytes);
  549. sst_pm_runtime_put(ctx);
  550. return ret_val;
  551. }
  552. static struct sst_ops pcm_ops = {
  553. .open = sst_open_pcm_stream,
  554. .stream_init = sst_stream_init,
  555. .stream_start = sst_stream_start,
  556. .stream_drop = sst_stream_drop,
  557. .stream_pause = sst_stream_pause,
  558. .stream_pause_release = sst_stream_resume,
  559. .stream_read_tstamp = sst_read_timestamp,
  560. .send_byte_stream = sst_send_byte_stream,
  561. .close = sst_close_pcm_stream,
  562. .power = sst_power_control,
  563. };
  564. static struct compress_sst_ops compr_ops = {
  565. .open = sst_cdev_open,
  566. .close = sst_cdev_close,
  567. .stream_pause = sst_cdev_stream_pause,
  568. .stream_pause_release = sst_cdev_stream_pause_release,
  569. .stream_start = sst_cdev_stream_start,
  570. .stream_drop = sst_cdev_stream_drop,
  571. .stream_drain = sst_cdev_stream_drain,
  572. .stream_partial_drain = sst_cdev_stream_partial_drain,
  573. .tstamp = sst_cdev_tstamp,
  574. .ack = sst_cdev_ack,
  575. .get_caps = sst_cdev_caps,
  576. .get_codec_caps = sst_cdev_codec_caps,
  577. .set_metadata = sst_cdev_set_metadata,
  578. .power = sst_power_control,
  579. };
  580. static struct sst_device sst_dsp_device = {
  581. .name = "Intel(R) SST LPE",
  582. .dev = NULL,
  583. .ops = &pcm_ops,
  584. .compr_ops = &compr_ops,
  585. };
  586. /*
  587. * sst_register - function to register DSP
  588. *
  589. * This functions registers DSP with the platform driver
  590. */
  591. int sst_register(struct device *dev)
  592. {
  593. int ret_val;
  594. sst_dsp_device.dev = dev;
  595. ret_val = sst_register_dsp(&sst_dsp_device);
  596. if (ret_val)
  597. dev_err(dev, "Unable to register DSP with platform driver\n");
  598. return ret_val;
  599. }
  600. int sst_unregister(struct device *dev)
  601. {
  602. return sst_unregister_dsp(&sst_dsp_device);
  603. }