soc-compress.c 20 KB

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