soc-compress.c 24 KB

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