soc-compress.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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 =
  62. fe->pcm->streams[cstream->direction].substream;
  63. struct snd_soc_platform *platform = fe->platform;
  64. struct snd_soc_dpcm *dpcm;
  65. struct snd_soc_dapm_widget_list *list;
  66. int stream;
  67. int ret = 0;
  68. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  69. stream = SNDRV_PCM_STREAM_PLAYBACK;
  70. else
  71. stream = SNDRV_PCM_STREAM_CAPTURE;
  72. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  73. if (platform->driver->compr_ops && platform->driver->compr_ops->open) {
  74. ret = platform->driver->compr_ops->open(cstream);
  75. if (ret < 0) {
  76. pr_err("compress asoc: can't open platform %s\n",
  77. platform->component.name);
  78. goto out;
  79. }
  80. }
  81. if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->startup) {
  82. ret = fe->dai_link->compr_ops->startup(cstream);
  83. if (ret < 0) {
  84. pr_err("compress asoc: %s startup failed\n", fe->dai_link->name);
  85. goto machine_err;
  86. }
  87. }
  88. fe->dpcm[stream].runtime = fe_substream->runtime;
  89. ret = dpcm_path_get(fe, stream, &list);
  90. if (ret < 0)
  91. goto fe_err;
  92. else if (ret == 0)
  93. dev_dbg(fe->dev, "ASoC: %s no valid %s route\n",
  94. fe->dai_link->name, stream ? "capture" : "playback");
  95. /* calculate valid and active FE <-> BE dpcms */
  96. dpcm_process_paths(fe, stream, &list, 1);
  97. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  98. ret = dpcm_be_dai_startup(fe, stream);
  99. if (ret < 0) {
  100. /* clean up all links */
  101. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  102. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  103. dpcm_be_disconnect(fe, stream);
  104. fe->dpcm[stream].runtime = NULL;
  105. goto path_err;
  106. }
  107. dpcm_clear_pending_state(fe, stream);
  108. dpcm_path_put(&list);
  109. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  110. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  111. snd_soc_runtime_activate(fe, stream);
  112. mutex_unlock(&fe->card->mutex);
  113. return 0;
  114. path_err:
  115. dpcm_path_put(&list);
  116. fe_err:
  117. if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
  118. fe->dai_link->compr_ops->shutdown(cstream);
  119. machine_err:
  120. if (platform->driver->compr_ops && platform->driver->compr_ops->free)
  121. platform->driver->compr_ops->free(cstream);
  122. out:
  123. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  124. mutex_unlock(&fe->card->mutex);
  125. return ret;
  126. }
  127. /*
  128. * Power down the audio subsystem pmdown_time msecs after close is called.
  129. * This is to ensure there are no pops or clicks in between any music tracks
  130. * due to DAPM power cycling.
  131. */
  132. static void close_delayed_work(struct work_struct *work)
  133. {
  134. struct snd_soc_pcm_runtime *rtd =
  135. container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
  136. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  137. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  138. dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
  139. codec_dai->driver->playback.stream_name,
  140. codec_dai->playback_active ? "active" : "inactive",
  141. rtd->pop_wait ? "yes" : "no");
  142. /* are we waiting on this codec DAI stream */
  143. if (rtd->pop_wait == 1) {
  144. rtd->pop_wait = 0;
  145. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
  146. SND_SOC_DAPM_STREAM_STOP);
  147. }
  148. mutex_unlock(&rtd->pcm_mutex);
  149. }
  150. static int soc_compr_free(struct snd_compr_stream *cstream)
  151. {
  152. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  153. struct snd_soc_platform *platform = rtd->platform;
  154. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  155. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  156. int stream;
  157. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  158. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  159. stream = SNDRV_PCM_STREAM_PLAYBACK;
  160. else
  161. stream = SNDRV_PCM_STREAM_CAPTURE;
  162. snd_soc_runtime_deactivate(rtd, stream);
  163. snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
  164. if (!cpu_dai->active)
  165. cpu_dai->rate = 0;
  166. if (!codec_dai->active)
  167. codec_dai->rate = 0;
  168. if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
  169. rtd->dai_link->compr_ops->shutdown(cstream);
  170. if (platform->driver->compr_ops && platform->driver->compr_ops->free)
  171. platform->driver->compr_ops->free(cstream);
  172. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  173. if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
  174. snd_soc_dapm_stream_event(rtd,
  175. SNDRV_PCM_STREAM_PLAYBACK,
  176. SND_SOC_DAPM_STREAM_STOP);
  177. } else {
  178. rtd->pop_wait = 1;
  179. queue_delayed_work(system_power_efficient_wq,
  180. &rtd->delayed_work,
  181. msecs_to_jiffies(rtd->pmdown_time));
  182. }
  183. } else {
  184. /* capture streams can be powered down now */
  185. snd_soc_dapm_stream_event(rtd,
  186. SNDRV_PCM_STREAM_CAPTURE,
  187. SND_SOC_DAPM_STREAM_STOP);
  188. }
  189. mutex_unlock(&rtd->pcm_mutex);
  190. return 0;
  191. }
  192. static int soc_compr_free_fe(struct snd_compr_stream *cstream)
  193. {
  194. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  195. struct snd_soc_platform *platform = fe->platform;
  196. struct snd_soc_dpcm *dpcm;
  197. int stream, ret;
  198. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  199. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  200. stream = SNDRV_PCM_STREAM_PLAYBACK;
  201. else
  202. stream = SNDRV_PCM_STREAM_CAPTURE;
  203. snd_soc_runtime_deactivate(fe, stream);
  204. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  205. ret = dpcm_be_dai_hw_free(fe, stream);
  206. if (ret < 0)
  207. dev_err(fe->dev, "compressed hw_free failed %d\n", ret);
  208. ret = dpcm_be_dai_shutdown(fe, stream);
  209. /* mark FE's links ready to prune */
  210. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  211. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  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 =
  334. fe->pcm->streams[cstream->direction].substream;
  335. struct snd_soc_platform *platform = fe->platform;
  336. int ret = 0, stream;
  337. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  338. stream = SNDRV_PCM_STREAM_PLAYBACK;
  339. else
  340. stream = SNDRV_PCM_STREAM_CAPTURE;
  341. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  342. if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
  343. ret = platform->driver->compr_ops->set_params(cstream, params);
  344. if (ret < 0)
  345. goto out;
  346. }
  347. if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
  348. ret = fe->dai_link->compr_ops->set_params(cstream);
  349. if (ret < 0)
  350. goto out;
  351. }
  352. /*
  353. * Create an empty hw_params for the BE as the machine driver must
  354. * fix this up to match DSP decoder and ASRC configuration.
  355. * I.e. machine driver fixup for compressed BE is mandatory.
  356. */
  357. memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
  358. sizeof(struct snd_pcm_hw_params));
  359. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  360. ret = dpcm_be_dai_hw_params(fe, stream);
  361. if (ret < 0)
  362. goto out;
  363. ret = dpcm_be_dai_prepare(fe, stream);
  364. if (ret < 0)
  365. goto out;
  366. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
  367. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  368. out:
  369. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  370. mutex_unlock(&fe->card->mutex);
  371. return ret;
  372. }
  373. static int soc_compr_get_params(struct snd_compr_stream *cstream,
  374. struct snd_codec *params)
  375. {
  376. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  377. struct snd_soc_platform *platform = rtd->platform;
  378. int ret = 0;
  379. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  380. if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
  381. ret = platform->driver->compr_ops->get_params(cstream, params);
  382. mutex_unlock(&rtd->pcm_mutex);
  383. return ret;
  384. }
  385. static int soc_compr_get_caps(struct snd_compr_stream *cstream,
  386. struct snd_compr_caps *caps)
  387. {
  388. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  389. struct snd_soc_platform *platform = rtd->platform;
  390. int ret = 0;
  391. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  392. if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
  393. ret = platform->driver->compr_ops->get_caps(cstream, caps);
  394. mutex_unlock(&rtd->pcm_mutex);
  395. return ret;
  396. }
  397. static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
  398. struct snd_compr_codec_caps *codec)
  399. {
  400. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  401. struct snd_soc_platform *platform = rtd->platform;
  402. int ret = 0;
  403. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  404. if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
  405. ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
  406. mutex_unlock(&rtd->pcm_mutex);
  407. return ret;
  408. }
  409. static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
  410. {
  411. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  412. struct snd_soc_platform *platform = rtd->platform;
  413. int ret = 0;
  414. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  415. if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
  416. ret = platform->driver->compr_ops->ack(cstream, bytes);
  417. mutex_unlock(&rtd->pcm_mutex);
  418. return ret;
  419. }
  420. static int soc_compr_pointer(struct snd_compr_stream *cstream,
  421. struct snd_compr_tstamp *tstamp)
  422. {
  423. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  424. struct snd_soc_platform *platform = rtd->platform;
  425. int ret = 0;
  426. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  427. if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
  428. ret = platform->driver->compr_ops->pointer(cstream, tstamp);
  429. mutex_unlock(&rtd->pcm_mutex);
  430. return ret;
  431. }
  432. static int soc_compr_copy(struct snd_compr_stream *cstream,
  433. char __user *buf, size_t count)
  434. {
  435. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  436. struct snd_soc_platform *platform = rtd->platform;
  437. int ret = 0;
  438. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  439. if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
  440. ret = platform->driver->compr_ops->copy(cstream, buf, count);
  441. mutex_unlock(&rtd->pcm_mutex);
  442. return ret;
  443. }
  444. static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
  445. struct snd_compr_metadata *metadata)
  446. {
  447. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  448. struct snd_soc_platform *platform = rtd->platform;
  449. int ret = 0;
  450. if (platform->driver->compr_ops && platform->driver->compr_ops->set_metadata)
  451. ret = platform->driver->compr_ops->set_metadata(cstream, metadata);
  452. return ret;
  453. }
  454. static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
  455. struct snd_compr_metadata *metadata)
  456. {
  457. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  458. struct snd_soc_platform *platform = rtd->platform;
  459. int ret = 0;
  460. if (platform->driver->compr_ops && platform->driver->compr_ops->get_metadata)
  461. ret = platform->driver->compr_ops->get_metadata(cstream, metadata);
  462. return ret;
  463. }
  464. /* ASoC Compress operations */
  465. static struct snd_compr_ops soc_compr_ops = {
  466. .open = soc_compr_open,
  467. .free = soc_compr_free,
  468. .set_params = soc_compr_set_params,
  469. .set_metadata = soc_compr_set_metadata,
  470. .get_metadata = soc_compr_get_metadata,
  471. .get_params = soc_compr_get_params,
  472. .trigger = soc_compr_trigger,
  473. .pointer = soc_compr_pointer,
  474. .ack = soc_compr_ack,
  475. .get_caps = soc_compr_get_caps,
  476. .get_codec_caps = soc_compr_get_codec_caps
  477. };
  478. /* ASoC Dynamic Compress operations */
  479. static struct snd_compr_ops soc_compr_dyn_ops = {
  480. .open = soc_compr_open_fe,
  481. .free = soc_compr_free_fe,
  482. .set_params = soc_compr_set_params_fe,
  483. .get_params = soc_compr_get_params,
  484. .set_metadata = soc_compr_set_metadata,
  485. .get_metadata = soc_compr_get_metadata,
  486. .trigger = soc_compr_trigger_fe,
  487. .pointer = soc_compr_pointer,
  488. .ack = soc_compr_ack,
  489. .get_caps = soc_compr_get_caps,
  490. .get_codec_caps = soc_compr_get_codec_caps
  491. };
  492. /**
  493. * snd_soc_new_compress - create a new compress.
  494. *
  495. * @rtd: The runtime for which we will create compress
  496. * @num: the device index number (zero based - shared with normal PCMs)
  497. *
  498. * Return: 0 for success, else error.
  499. */
  500. int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
  501. {
  502. struct snd_soc_codec *codec = rtd->codec;
  503. struct snd_soc_platform *platform = rtd->platform;
  504. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  505. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  506. struct snd_compr *compr;
  507. struct snd_pcm *be_pcm;
  508. char new_name[64];
  509. int ret = 0, direction = 0;
  510. int playback = 0, capture = 0;
  511. if (rtd->num_codecs > 1) {
  512. dev_err(rtd->card->dev, "Multicodec not supported for compressed stream\n");
  513. return -EINVAL;
  514. }
  515. /* check client and interface hw capabilities */
  516. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  517. rtd->dai_link->stream_name, codec_dai->name, num);
  518. if (codec_dai->driver->playback.channels_min)
  519. playback = 1;
  520. if (codec_dai->driver->capture.channels_min)
  521. capture = 1;
  522. capture = capture && cpu_dai->driver->capture.channels_min;
  523. playback = playback && cpu_dai->driver->playback.channels_min;
  524. /*
  525. * Compress devices are unidirectional so only one of the directions
  526. * should be set, check for that (xor)
  527. */
  528. if (playback + capture != 1) {
  529. dev_err(rtd->card->dev, "Invalid direction for compress P %d, C %d\n",
  530. playback, capture);
  531. return -EINVAL;
  532. }
  533. if(playback)
  534. direction = SND_COMPRESS_PLAYBACK;
  535. else
  536. direction = SND_COMPRESS_CAPTURE;
  537. compr = kzalloc(sizeof(*compr), GFP_KERNEL);
  538. if (compr == NULL) {
  539. snd_printk(KERN_ERR "Cannot allocate compr\n");
  540. return -ENOMEM;
  541. }
  542. compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
  543. GFP_KERNEL);
  544. if (compr->ops == NULL) {
  545. dev_err(rtd->card->dev, "Cannot allocate compressed ops\n");
  546. ret = -ENOMEM;
  547. goto compr_err;
  548. }
  549. if (rtd->dai_link->dynamic) {
  550. snprintf(new_name, sizeof(new_name), "(%s)",
  551. rtd->dai_link->stream_name);
  552. ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
  553. rtd->dai_link->dpcm_playback,
  554. rtd->dai_link->dpcm_capture, &be_pcm);
  555. if (ret < 0) {
  556. dev_err(rtd->card->dev, "ASoC: can't create compressed for %s\n",
  557. rtd->dai_link->name);
  558. goto compr_err;
  559. }
  560. rtd->pcm = be_pcm;
  561. rtd->fe_compr = 1;
  562. if (rtd->dai_link->dpcm_playback)
  563. be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
  564. else if (rtd->dai_link->dpcm_capture)
  565. be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
  566. memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
  567. } else
  568. memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
  569. /* Add copy callback for not memory mapped DSPs */
  570. if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
  571. compr->ops->copy = soc_compr_copy;
  572. mutex_init(&compr->lock);
  573. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  574. rtd->dai_link->stream_name,
  575. rtd->codec_dai->name, num);
  576. ret = snd_compress_new(rtd->card->snd_card, num, direction,
  577. new_name, compr);
  578. if (ret < 0) {
  579. pr_err("compress asoc: can't create compress for codec %s\n",
  580. codec->component.name);
  581. goto compr_err;
  582. }
  583. /* DAPM dai link stream work */
  584. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  585. rtd->compr = compr;
  586. compr->private_data = rtd;
  587. printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name,
  588. cpu_dai->name);
  589. return ret;
  590. compr_err:
  591. kfree(compr);
  592. return ret;
  593. }
  594. EXPORT_SYMBOL_GPL(snd_soc_new_compress);