soc-compress.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * soc-compress.c -- ALSA SoC Compress
  3. *
  4. * Copyright (C) 2012 Intel Corp.
  5. *
  6. * Authors: Namarta Kohli <namartax.kohli@intel.com>
  7. * Ramesh Babu K V <ramesh.babu@linux.intel.com>
  8. * Vinod Koul <vinod.koul@linux.intel.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/slab.h>
  20. #include <linux/workqueue.h>
  21. #include <sound/core.h>
  22. #include <sound/compress_params.h>
  23. #include <sound/compress_driver.h>
  24. #include <sound/soc.h>
  25. #include <sound/initval.h>
  26. #include <sound/soc-dpcm.h>
  27. static int soc_compr_open(struct snd_compr_stream *cstream)
  28. {
  29. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  30. struct snd_soc_platform *platform = rtd->platform;
  31. int ret = 0;
  32. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  33. if (platform->driver->compr_ops && platform->driver->compr_ops->open) {
  34. ret = platform->driver->compr_ops->open(cstream);
  35. if (ret < 0) {
  36. pr_err("compress asoc: can't open platform %s\n", platform->name);
  37. goto out;
  38. }
  39. }
  40. if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->startup) {
  41. ret = rtd->dai_link->compr_ops->startup(cstream);
  42. if (ret < 0) {
  43. pr_err("compress asoc: %s startup failed\n", rtd->dai_link->name);
  44. goto machine_err;
  45. }
  46. }
  47. snd_soc_runtime_activate(rtd, cstream->direction);
  48. mutex_unlock(&rtd->pcm_mutex);
  49. return 0;
  50. machine_err:
  51. if (platform->driver->compr_ops && platform->driver->compr_ops->free)
  52. platform->driver->compr_ops->free(cstream);
  53. out:
  54. mutex_unlock(&rtd->pcm_mutex);
  55. return ret;
  56. }
  57. static int soc_compr_open_fe(struct snd_compr_stream *cstream)
  58. {
  59. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  60. struct snd_pcm_substream *fe_substream = fe->pcm->streams[0].substream;
  61. struct snd_soc_platform *platform = fe->platform;
  62. struct snd_soc_dpcm *dpcm;
  63. struct snd_soc_dapm_widget_list *list;
  64. int stream;
  65. int ret = 0;
  66. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  67. stream = SNDRV_PCM_STREAM_PLAYBACK;
  68. else
  69. stream = SNDRV_PCM_STREAM_CAPTURE;
  70. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  71. if (platform->driver->compr_ops && platform->driver->compr_ops->open) {
  72. ret = platform->driver->compr_ops->open(cstream);
  73. if (ret < 0) {
  74. pr_err("compress asoc: can't open platform %s\n", platform->name);
  75. goto out;
  76. }
  77. }
  78. if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->startup) {
  79. ret = fe->dai_link->compr_ops->startup(cstream);
  80. if (ret < 0) {
  81. pr_err("compress asoc: %s startup failed\n", fe->dai_link->name);
  82. goto machine_err;
  83. }
  84. }
  85. fe->dpcm[stream].runtime = fe_substream->runtime;
  86. if (dpcm_path_get(fe, stream, &list) <= 0) {
  87. dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
  88. fe->dai_link->name, stream ? "capture" : "playback");
  89. }
  90. /* calculate valid and active FE <-> BE dpcms */
  91. dpcm_process_paths(fe, stream, &list, 1);
  92. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  93. ret = dpcm_be_dai_startup(fe, stream);
  94. if (ret < 0) {
  95. /* clean up all links */
  96. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  97. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  98. dpcm_be_disconnect(fe, stream);
  99. fe->dpcm[stream].runtime = NULL;
  100. goto fe_err;
  101. }
  102. dpcm_clear_pending_state(fe, stream);
  103. dpcm_path_put(&list);
  104. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  105. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  106. snd_soc_runtime_activate(fe, stream);
  107. mutex_unlock(&fe->card->mutex);
  108. return 0;
  109. fe_err:
  110. if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
  111. fe->dai_link->compr_ops->shutdown(cstream);
  112. machine_err:
  113. if (platform->driver->compr_ops && platform->driver->compr_ops->free)
  114. platform->driver->compr_ops->free(cstream);
  115. out:
  116. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  117. mutex_unlock(&fe->card->mutex);
  118. return ret;
  119. }
  120. /*
  121. * Power down the audio subsystem pmdown_time msecs after close is called.
  122. * This is to ensure there are no pops or clicks in between any music tracks
  123. * due to DAPM power cycling.
  124. */
  125. static void close_delayed_work(struct work_struct *work)
  126. {
  127. struct snd_soc_pcm_runtime *rtd =
  128. container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
  129. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  130. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  131. dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
  132. codec_dai->driver->playback.stream_name,
  133. codec_dai->playback_active ? "active" : "inactive",
  134. rtd->pop_wait ? "yes" : "no");
  135. /* are we waiting on this codec DAI stream */
  136. if (rtd->pop_wait == 1) {
  137. rtd->pop_wait = 0;
  138. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
  139. SND_SOC_DAPM_STREAM_STOP);
  140. }
  141. mutex_unlock(&rtd->pcm_mutex);
  142. }
  143. static int soc_compr_free(struct snd_compr_stream *cstream)
  144. {
  145. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  146. struct snd_soc_platform *platform = rtd->platform;
  147. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  148. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  149. int stream;
  150. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  151. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  152. stream = SNDRV_PCM_STREAM_PLAYBACK;
  153. else
  154. stream = SNDRV_PCM_STREAM_CAPTURE;
  155. snd_soc_runtime_deactivate(rtd, stream);
  156. snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
  157. if (!cpu_dai->active)
  158. cpu_dai->rate = 0;
  159. if (!codec_dai->active)
  160. codec_dai->rate = 0;
  161. if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
  162. rtd->dai_link->compr_ops->shutdown(cstream);
  163. if (platform->driver->compr_ops && platform->driver->compr_ops->free)
  164. platform->driver->compr_ops->free(cstream);
  165. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  166. if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
  167. snd_soc_dapm_stream_event(rtd,
  168. SNDRV_PCM_STREAM_PLAYBACK,
  169. SND_SOC_DAPM_STREAM_STOP);
  170. } else {
  171. rtd->pop_wait = 1;
  172. queue_delayed_work(system_power_efficient_wq,
  173. &rtd->delayed_work,
  174. msecs_to_jiffies(rtd->pmdown_time));
  175. }
  176. } else {
  177. /* capture streams can be powered down now */
  178. snd_soc_dapm_stream_event(rtd,
  179. SNDRV_PCM_STREAM_CAPTURE,
  180. SND_SOC_DAPM_STREAM_STOP);
  181. }
  182. mutex_unlock(&rtd->pcm_mutex);
  183. return 0;
  184. }
  185. static int soc_compr_free_fe(struct snd_compr_stream *cstream)
  186. {
  187. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  188. struct snd_soc_platform *platform = fe->platform;
  189. struct snd_soc_dpcm *dpcm;
  190. int stream, ret;
  191. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  192. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  193. stream = SNDRV_PCM_STREAM_PLAYBACK;
  194. else
  195. stream = SNDRV_PCM_STREAM_CAPTURE;
  196. snd_soc_runtime_deactivate(fe, stream);
  197. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  198. ret = dpcm_be_dai_hw_free(fe, stream);
  199. if (ret < 0)
  200. dev_err(fe->dev, "compressed hw_free failed %d\n", ret);
  201. ret = dpcm_be_dai_shutdown(fe, stream);
  202. /* mark FE's links ready to prune */
  203. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  204. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  205. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  206. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
  207. else
  208. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
  209. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  210. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  211. dpcm_be_disconnect(fe, stream);
  212. fe->dpcm[stream].runtime = NULL;
  213. if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
  214. fe->dai_link->compr_ops->shutdown(cstream);
  215. if (platform->driver->compr_ops && platform->driver->compr_ops->free)
  216. platform->driver->compr_ops->free(cstream);
  217. mutex_unlock(&fe->card->mutex);
  218. return 0;
  219. }
  220. static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
  221. {
  222. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  223. struct snd_soc_platform *platform = rtd->platform;
  224. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  225. int ret = 0;
  226. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  227. if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
  228. ret = platform->driver->compr_ops->trigger(cstream, cmd);
  229. if (ret < 0)
  230. goto out;
  231. }
  232. switch (cmd) {
  233. case SNDRV_PCM_TRIGGER_START:
  234. snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
  235. break;
  236. case SNDRV_PCM_TRIGGER_STOP:
  237. snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
  238. break;
  239. }
  240. out:
  241. mutex_unlock(&rtd->pcm_mutex);
  242. return ret;
  243. }
  244. static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
  245. {
  246. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  247. struct snd_soc_platform *platform = fe->platform;
  248. int ret = 0, stream;
  249. if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
  250. cmd == SND_COMPR_TRIGGER_DRAIN) {
  251. if (platform->driver->compr_ops &&
  252. platform->driver->compr_ops->trigger)
  253. return platform->driver->compr_ops->trigger(cstream,
  254. cmd);
  255. }
  256. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  257. stream = SNDRV_PCM_STREAM_PLAYBACK;
  258. else
  259. stream = SNDRV_PCM_STREAM_CAPTURE;
  260. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  261. if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
  262. ret = platform->driver->compr_ops->trigger(cstream, cmd);
  263. if (ret < 0)
  264. goto out;
  265. }
  266. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  267. ret = dpcm_be_dai_trigger(fe, stream, cmd);
  268. switch (cmd) {
  269. case SNDRV_PCM_TRIGGER_START:
  270. case SNDRV_PCM_TRIGGER_RESUME:
  271. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  272. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  273. break;
  274. case SNDRV_PCM_TRIGGER_STOP:
  275. case SNDRV_PCM_TRIGGER_SUSPEND:
  276. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
  277. break;
  278. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  279. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
  280. break;
  281. }
  282. out:
  283. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  284. mutex_unlock(&fe->card->mutex);
  285. return ret;
  286. }
  287. static int soc_compr_set_params(struct snd_compr_stream *cstream,
  288. struct snd_compr_params *params)
  289. {
  290. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  291. struct snd_soc_platform *platform = rtd->platform;
  292. int ret = 0;
  293. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  294. /* first we call set_params for the platform driver
  295. * this should configure the soc side
  296. * if the machine has compressed ops then we call that as well
  297. * expectation is that platform and machine will configure everything
  298. * for this compress path, like configuring pcm port for codec
  299. */
  300. if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
  301. ret = platform->driver->compr_ops->set_params(cstream, params);
  302. if (ret < 0)
  303. goto err;
  304. }
  305. if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
  306. ret = rtd->dai_link->compr_ops->set_params(cstream);
  307. if (ret < 0)
  308. goto err;
  309. }
  310. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  311. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
  312. SND_SOC_DAPM_STREAM_START);
  313. else
  314. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
  315. SND_SOC_DAPM_STREAM_START);
  316. /* cancel any delayed stream shutdown that is pending */
  317. rtd->pop_wait = 0;
  318. mutex_unlock(&rtd->pcm_mutex);
  319. cancel_delayed_work_sync(&rtd->delayed_work);
  320. return ret;
  321. err:
  322. mutex_unlock(&rtd->pcm_mutex);
  323. return ret;
  324. }
  325. static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
  326. struct snd_compr_params *params)
  327. {
  328. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  329. struct snd_pcm_substream *fe_substream = fe->pcm->streams[0].substream;
  330. struct snd_soc_platform *platform = fe->platform;
  331. int ret = 0, stream;
  332. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  333. stream = SNDRV_PCM_STREAM_PLAYBACK;
  334. else
  335. stream = SNDRV_PCM_STREAM_CAPTURE;
  336. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  337. if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
  338. ret = platform->driver->compr_ops->set_params(cstream, params);
  339. if (ret < 0)
  340. goto out;
  341. }
  342. if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
  343. ret = fe->dai_link->compr_ops->set_params(cstream);
  344. if (ret < 0)
  345. goto out;
  346. }
  347. /*
  348. * Create an empty hw_params for the BE as the machine driver must
  349. * fix this up to match DSP decoder and ASRC configuration.
  350. * I.e. machine driver fixup for compressed BE is mandatory.
  351. */
  352. memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
  353. sizeof(struct snd_pcm_hw_params));
  354. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  355. ret = dpcm_be_dai_hw_params(fe, stream);
  356. if (ret < 0)
  357. goto out;
  358. ret = dpcm_be_dai_prepare(fe, stream);
  359. if (ret < 0)
  360. goto out;
  361. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  362. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
  363. else
  364. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
  365. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  366. out:
  367. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  368. mutex_unlock(&fe->card->mutex);
  369. return ret;
  370. }
  371. static int soc_compr_get_params(struct snd_compr_stream *cstream,
  372. struct snd_codec *params)
  373. {
  374. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  375. struct snd_soc_platform *platform = rtd->platform;
  376. int ret = 0;
  377. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  378. if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
  379. ret = platform->driver->compr_ops->get_params(cstream, params);
  380. mutex_unlock(&rtd->pcm_mutex);
  381. return ret;
  382. }
  383. static int soc_compr_get_caps(struct snd_compr_stream *cstream,
  384. struct snd_compr_caps *caps)
  385. {
  386. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  387. struct snd_soc_platform *platform = rtd->platform;
  388. int ret = 0;
  389. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  390. if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
  391. ret = platform->driver->compr_ops->get_caps(cstream, caps);
  392. mutex_unlock(&rtd->pcm_mutex);
  393. return ret;
  394. }
  395. static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
  396. struct snd_compr_codec_caps *codec)
  397. {
  398. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  399. struct snd_soc_platform *platform = rtd->platform;
  400. int ret = 0;
  401. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  402. if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
  403. ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
  404. mutex_unlock(&rtd->pcm_mutex);
  405. return ret;
  406. }
  407. static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
  408. {
  409. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  410. struct snd_soc_platform *platform = rtd->platform;
  411. int ret = 0;
  412. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  413. if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
  414. ret = platform->driver->compr_ops->ack(cstream, bytes);
  415. mutex_unlock(&rtd->pcm_mutex);
  416. return ret;
  417. }
  418. static int soc_compr_pointer(struct snd_compr_stream *cstream,
  419. struct snd_compr_tstamp *tstamp)
  420. {
  421. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  422. struct snd_soc_platform *platform = rtd->platform;
  423. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  424. if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
  425. platform->driver->compr_ops->pointer(cstream, tstamp);
  426. mutex_unlock(&rtd->pcm_mutex);
  427. return 0;
  428. }
  429. static int soc_compr_copy(struct snd_compr_stream *cstream,
  430. char __user *buf, size_t count)
  431. {
  432. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  433. struct snd_soc_platform *platform = rtd->platform;
  434. int ret = 0;
  435. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  436. if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
  437. ret = platform->driver->compr_ops->copy(cstream, buf, count);
  438. mutex_unlock(&rtd->pcm_mutex);
  439. return ret;
  440. }
  441. static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
  442. struct snd_compr_metadata *metadata)
  443. {
  444. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  445. struct snd_soc_platform *platform = rtd->platform;
  446. int ret = 0;
  447. if (platform->driver->compr_ops && platform->driver->compr_ops->set_metadata)
  448. ret = platform->driver->compr_ops->set_metadata(cstream, metadata);
  449. return ret;
  450. }
  451. static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
  452. struct snd_compr_metadata *metadata)
  453. {
  454. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  455. struct snd_soc_platform *platform = rtd->platform;
  456. int ret = 0;
  457. if (platform->driver->compr_ops && platform->driver->compr_ops->get_metadata)
  458. ret = platform->driver->compr_ops->get_metadata(cstream, metadata);
  459. return ret;
  460. }
  461. /* ASoC Compress operations */
  462. static struct snd_compr_ops soc_compr_ops = {
  463. .open = soc_compr_open,
  464. .free = soc_compr_free,
  465. .set_params = soc_compr_set_params,
  466. .set_metadata = soc_compr_set_metadata,
  467. .get_metadata = soc_compr_get_metadata,
  468. .get_params = soc_compr_get_params,
  469. .trigger = soc_compr_trigger,
  470. .pointer = soc_compr_pointer,
  471. .ack = soc_compr_ack,
  472. .get_caps = soc_compr_get_caps,
  473. .get_codec_caps = soc_compr_get_codec_caps
  474. };
  475. /* ASoC Dynamic Compress operations */
  476. static struct snd_compr_ops soc_compr_dyn_ops = {
  477. .open = soc_compr_open_fe,
  478. .free = soc_compr_free_fe,
  479. .set_params = soc_compr_set_params_fe,
  480. .get_params = soc_compr_get_params,
  481. .set_metadata = soc_compr_set_metadata,
  482. .get_metadata = soc_compr_get_metadata,
  483. .trigger = soc_compr_trigger_fe,
  484. .pointer = soc_compr_pointer,
  485. .ack = soc_compr_ack,
  486. .get_caps = soc_compr_get_caps,
  487. .get_codec_caps = soc_compr_get_codec_caps
  488. };
  489. /* create a new compress */
  490. int soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
  491. {
  492. struct snd_soc_codec *codec = rtd->codec;
  493. struct snd_soc_platform *platform = rtd->platform;
  494. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  495. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  496. struct snd_compr *compr;
  497. struct snd_pcm *be_pcm;
  498. char new_name[64];
  499. int ret = 0, direction = 0;
  500. /* check client and interface hw capabilities */
  501. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  502. rtd->dai_link->stream_name, codec_dai->name, num);
  503. if (codec_dai->driver->playback.channels_min)
  504. direction = SND_COMPRESS_PLAYBACK;
  505. else if (codec_dai->driver->capture.channels_min)
  506. direction = SND_COMPRESS_CAPTURE;
  507. else
  508. return -EINVAL;
  509. compr = kzalloc(sizeof(*compr), GFP_KERNEL);
  510. if (compr == NULL) {
  511. snd_printk(KERN_ERR "Cannot allocate compr\n");
  512. return -ENOMEM;
  513. }
  514. compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
  515. GFP_KERNEL);
  516. if (compr->ops == NULL) {
  517. dev_err(rtd->card->dev, "Cannot allocate compressed ops\n");
  518. ret = -ENOMEM;
  519. goto compr_err;
  520. }
  521. if (rtd->dai_link->dynamic) {
  522. snprintf(new_name, sizeof(new_name), "(%s)",
  523. rtd->dai_link->stream_name);
  524. ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
  525. 1, 0, &be_pcm);
  526. if (ret < 0) {
  527. dev_err(rtd->card->dev, "ASoC: can't create compressed for %s\n",
  528. rtd->dai_link->name);
  529. goto compr_err;
  530. }
  531. rtd->pcm = be_pcm;
  532. rtd->fe_compr = 1;
  533. be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
  534. be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
  535. memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
  536. } else
  537. memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
  538. /* Add copy callback for not memory mapped DSPs */
  539. if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
  540. compr->ops->copy = soc_compr_copy;
  541. mutex_init(&compr->lock);
  542. ret = snd_compress_new(rtd->card->snd_card, num, direction, compr);
  543. if (ret < 0) {
  544. pr_err("compress asoc: can't create compress for codec %s\n",
  545. codec->name);
  546. goto compr_err;
  547. }
  548. /* DAPM dai link stream work */
  549. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  550. rtd->compr = compr;
  551. compr->private_data = rtd;
  552. printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name,
  553. cpu_dai->name);
  554. return ret;
  555. compr_err:
  556. kfree(compr);
  557. return ret;
  558. }