soc-compress.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  210. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
  211. else
  212. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
  213. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  214. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  215. dpcm_be_disconnect(fe, stream);
  216. fe->dpcm[stream].runtime = NULL;
  217. if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
  218. fe->dai_link->compr_ops->shutdown(cstream);
  219. if (platform->driver->compr_ops && platform->driver->compr_ops->free)
  220. platform->driver->compr_ops->free(cstream);
  221. mutex_unlock(&fe->card->mutex);
  222. return 0;
  223. }
  224. static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
  225. {
  226. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  227. struct snd_soc_platform *platform = rtd->platform;
  228. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  229. int ret = 0;
  230. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  231. if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
  232. ret = platform->driver->compr_ops->trigger(cstream, cmd);
  233. if (ret < 0)
  234. goto out;
  235. }
  236. switch (cmd) {
  237. case SNDRV_PCM_TRIGGER_START:
  238. snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
  239. break;
  240. case SNDRV_PCM_TRIGGER_STOP:
  241. snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
  242. break;
  243. }
  244. out:
  245. mutex_unlock(&rtd->pcm_mutex);
  246. return ret;
  247. }
  248. static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
  249. {
  250. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  251. struct snd_soc_platform *platform = fe->platform;
  252. int ret = 0, stream;
  253. if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
  254. cmd == SND_COMPR_TRIGGER_DRAIN) {
  255. if (platform->driver->compr_ops &&
  256. platform->driver->compr_ops->trigger)
  257. return platform->driver->compr_ops->trigger(cstream,
  258. cmd);
  259. }
  260. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  261. stream = SNDRV_PCM_STREAM_PLAYBACK;
  262. else
  263. stream = SNDRV_PCM_STREAM_CAPTURE;
  264. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  265. if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
  266. ret = platform->driver->compr_ops->trigger(cstream, cmd);
  267. if (ret < 0)
  268. goto out;
  269. }
  270. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  271. ret = dpcm_be_dai_trigger(fe, stream, cmd);
  272. switch (cmd) {
  273. case SNDRV_PCM_TRIGGER_START:
  274. case SNDRV_PCM_TRIGGER_RESUME:
  275. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  276. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  277. break;
  278. case SNDRV_PCM_TRIGGER_STOP:
  279. case SNDRV_PCM_TRIGGER_SUSPEND:
  280. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
  281. break;
  282. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  283. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
  284. break;
  285. }
  286. out:
  287. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  288. mutex_unlock(&fe->card->mutex);
  289. return ret;
  290. }
  291. static int soc_compr_set_params(struct snd_compr_stream *cstream,
  292. struct snd_compr_params *params)
  293. {
  294. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  295. struct snd_soc_platform *platform = rtd->platform;
  296. int ret = 0;
  297. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  298. /* first we call set_params for the platform driver
  299. * this should configure the soc side
  300. * if the machine has compressed ops then we call that as well
  301. * expectation is that platform and machine will configure everything
  302. * for this compress path, like configuring pcm port for codec
  303. */
  304. if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
  305. ret = platform->driver->compr_ops->set_params(cstream, params);
  306. if (ret < 0)
  307. goto err;
  308. }
  309. if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
  310. ret = rtd->dai_link->compr_ops->set_params(cstream);
  311. if (ret < 0)
  312. goto err;
  313. }
  314. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  315. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
  316. SND_SOC_DAPM_STREAM_START);
  317. else
  318. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
  319. SND_SOC_DAPM_STREAM_START);
  320. /* cancel any delayed stream shutdown that is pending */
  321. rtd->pop_wait = 0;
  322. mutex_unlock(&rtd->pcm_mutex);
  323. cancel_delayed_work_sync(&rtd->delayed_work);
  324. return ret;
  325. err:
  326. mutex_unlock(&rtd->pcm_mutex);
  327. return ret;
  328. }
  329. static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
  330. struct snd_compr_params *params)
  331. {
  332. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  333. struct snd_pcm_substream *fe_substream = fe->pcm->streams[0].substream;
  334. struct snd_soc_platform *platform = fe->platform;
  335. int ret = 0, stream;
  336. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  337. stream = SNDRV_PCM_STREAM_PLAYBACK;
  338. else
  339. stream = SNDRV_PCM_STREAM_CAPTURE;
  340. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  341. if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
  342. ret = platform->driver->compr_ops->set_params(cstream, params);
  343. if (ret < 0)
  344. goto out;
  345. }
  346. if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
  347. ret = fe->dai_link->compr_ops->set_params(cstream);
  348. if (ret < 0)
  349. goto out;
  350. }
  351. /*
  352. * Create an empty hw_params for the BE as the machine driver must
  353. * fix this up to match DSP decoder and ASRC configuration.
  354. * I.e. machine driver fixup for compressed BE is mandatory.
  355. */
  356. memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
  357. sizeof(struct snd_pcm_hw_params));
  358. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  359. ret = dpcm_be_dai_hw_params(fe, stream);
  360. if (ret < 0)
  361. goto out;
  362. ret = dpcm_be_dai_prepare(fe, stream);
  363. if (ret < 0)
  364. goto out;
  365. if (stream == SNDRV_PCM_STREAM_PLAYBACK)
  366. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
  367. else
  368. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
  369. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  370. out:
  371. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  372. mutex_unlock(&fe->card->mutex);
  373. return ret;
  374. }
  375. static int soc_compr_get_params(struct snd_compr_stream *cstream,
  376. struct snd_codec *params)
  377. {
  378. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  379. struct snd_soc_platform *platform = rtd->platform;
  380. int ret = 0;
  381. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  382. if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
  383. ret = platform->driver->compr_ops->get_params(cstream, params);
  384. mutex_unlock(&rtd->pcm_mutex);
  385. return ret;
  386. }
  387. static int soc_compr_get_caps(struct snd_compr_stream *cstream,
  388. struct snd_compr_caps *caps)
  389. {
  390. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  391. struct snd_soc_platform *platform = rtd->platform;
  392. int ret = 0;
  393. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  394. if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
  395. ret = platform->driver->compr_ops->get_caps(cstream, caps);
  396. mutex_unlock(&rtd->pcm_mutex);
  397. return ret;
  398. }
  399. static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
  400. struct snd_compr_codec_caps *codec)
  401. {
  402. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  403. struct snd_soc_platform *platform = rtd->platform;
  404. int ret = 0;
  405. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  406. if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
  407. ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
  408. mutex_unlock(&rtd->pcm_mutex);
  409. return ret;
  410. }
  411. static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
  412. {
  413. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  414. struct snd_soc_platform *platform = rtd->platform;
  415. int ret = 0;
  416. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  417. if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
  418. ret = platform->driver->compr_ops->ack(cstream, bytes);
  419. mutex_unlock(&rtd->pcm_mutex);
  420. return ret;
  421. }
  422. static int soc_compr_pointer(struct snd_compr_stream *cstream,
  423. struct snd_compr_tstamp *tstamp)
  424. {
  425. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  426. struct snd_soc_platform *platform = rtd->platform;
  427. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  428. if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
  429. platform->driver->compr_ops->pointer(cstream, tstamp);
  430. mutex_unlock(&rtd->pcm_mutex);
  431. return 0;
  432. }
  433. static int soc_compr_copy(struct snd_compr_stream *cstream,
  434. char __user *buf, size_t count)
  435. {
  436. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  437. struct snd_soc_platform *platform = rtd->platform;
  438. int ret = 0;
  439. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  440. if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
  441. ret = platform->driver->compr_ops->copy(cstream, buf, count);
  442. mutex_unlock(&rtd->pcm_mutex);
  443. return ret;
  444. }
  445. static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
  446. struct snd_compr_metadata *metadata)
  447. {
  448. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  449. struct snd_soc_platform *platform = rtd->platform;
  450. int ret = 0;
  451. if (platform->driver->compr_ops && platform->driver->compr_ops->set_metadata)
  452. ret = platform->driver->compr_ops->set_metadata(cstream, metadata);
  453. return ret;
  454. }
  455. static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
  456. struct snd_compr_metadata *metadata)
  457. {
  458. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  459. struct snd_soc_platform *platform = rtd->platform;
  460. int ret = 0;
  461. if (platform->driver->compr_ops && platform->driver->compr_ops->get_metadata)
  462. ret = platform->driver->compr_ops->get_metadata(cstream, metadata);
  463. return ret;
  464. }
  465. /* ASoC Compress operations */
  466. static struct snd_compr_ops soc_compr_ops = {
  467. .open = soc_compr_open,
  468. .free = soc_compr_free,
  469. .set_params = soc_compr_set_params,
  470. .set_metadata = soc_compr_set_metadata,
  471. .get_metadata = soc_compr_get_metadata,
  472. .get_params = soc_compr_get_params,
  473. .trigger = soc_compr_trigger,
  474. .pointer = soc_compr_pointer,
  475. .ack = soc_compr_ack,
  476. .get_caps = soc_compr_get_caps,
  477. .get_codec_caps = soc_compr_get_codec_caps
  478. };
  479. /* ASoC Dynamic Compress operations */
  480. static struct snd_compr_ops soc_compr_dyn_ops = {
  481. .open = soc_compr_open_fe,
  482. .free = soc_compr_free_fe,
  483. .set_params = soc_compr_set_params_fe,
  484. .get_params = soc_compr_get_params,
  485. .set_metadata = soc_compr_set_metadata,
  486. .get_metadata = soc_compr_get_metadata,
  487. .trigger = soc_compr_trigger_fe,
  488. .pointer = soc_compr_pointer,
  489. .ack = soc_compr_ack,
  490. .get_caps = soc_compr_get_caps,
  491. .get_codec_caps = soc_compr_get_codec_caps
  492. };
  493. /* create a new compress */
  494. int soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
  495. {
  496. struct snd_soc_codec *codec = rtd->codec;
  497. struct snd_soc_platform *platform = rtd->platform;
  498. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  499. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  500. struct snd_compr *compr;
  501. struct snd_pcm *be_pcm;
  502. char new_name[64];
  503. int ret = 0, direction = 0;
  504. if (rtd->num_codecs > 1) {
  505. dev_err(rtd->card->dev, "Multicodec not supported for compressed stream\n");
  506. return -EINVAL;
  507. }
  508. /* check client and interface hw capabilities */
  509. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  510. rtd->dai_link->stream_name, codec_dai->name, num);
  511. if (codec_dai->driver->playback.channels_min)
  512. direction = SND_COMPRESS_PLAYBACK;
  513. else if (codec_dai->driver->capture.channels_min)
  514. direction = SND_COMPRESS_CAPTURE;
  515. else
  516. return -EINVAL;
  517. compr = kzalloc(sizeof(*compr), GFP_KERNEL);
  518. if (compr == NULL) {
  519. snd_printk(KERN_ERR "Cannot allocate compr\n");
  520. return -ENOMEM;
  521. }
  522. compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
  523. GFP_KERNEL);
  524. if (compr->ops == NULL) {
  525. dev_err(rtd->card->dev, "Cannot allocate compressed ops\n");
  526. ret = -ENOMEM;
  527. goto compr_err;
  528. }
  529. if (rtd->dai_link->dynamic) {
  530. snprintf(new_name, sizeof(new_name), "(%s)",
  531. rtd->dai_link->stream_name);
  532. ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
  533. rtd->dai_link->dpcm_playback,
  534. rtd->dai_link->dpcm_capture, &be_pcm);
  535. if (ret < 0) {
  536. dev_err(rtd->card->dev, "ASoC: can't create compressed for %s\n",
  537. rtd->dai_link->name);
  538. goto compr_err;
  539. }
  540. rtd->pcm = be_pcm;
  541. rtd->fe_compr = 1;
  542. if (rtd->dai_link->dpcm_playback)
  543. be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
  544. else if (rtd->dai_link->dpcm_capture)
  545. be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
  546. memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
  547. } else
  548. memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
  549. /* Add copy callback for not memory mapped DSPs */
  550. if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
  551. compr->ops->copy = soc_compr_copy;
  552. mutex_init(&compr->lock);
  553. ret = snd_compress_new(rtd->card->snd_card, num, direction, compr);
  554. if (ret < 0) {
  555. pr_err("compress asoc: can't create compress for codec %s\n",
  556. codec->component.name);
  557. goto compr_err;
  558. }
  559. /* DAPM dai link stream work */
  560. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  561. rtd->compr = compr;
  562. compr->private_data = rtd;
  563. printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name,
  564. cpu_dai->name);
  565. return ret;
  566. compr_err:
  567. kfree(compr);
  568. return ret;
  569. }