sst_drv_interface.c 19 KB

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