soc-compress.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // soc-compress.c -- ALSA SoC Compress
  4. //
  5. // Copyright (C) 2012 Intel Corp.
  6. //
  7. // Authors: Namarta Kohli <namartax.kohli@intel.com>
  8. // Ramesh Babu K V <ramesh.babu@linux.intel.com>
  9. // Vinod Koul <vinod.koul@linux.intel.com>
  10. #include <linux/kernel.h>
  11. #include <linux/init.h>
  12. #include <linux/delay.h>
  13. #include <linux/slab.h>
  14. #include <linux/workqueue.h>
  15. #include <sound/core.h>
  16. #include <sound/compress_params.h>
  17. #include <sound/compress_driver.h>
  18. #include <sound/soc.h>
  19. #include <sound/initval.h>
  20. #include <sound/soc-dpcm.h>
  21. #include <sound/soc-link.h>
  22. static int snd_soc_compr_components_open(struct snd_compr_stream *cstream)
  23. {
  24. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  25. struct snd_soc_component *component;
  26. int ret = 0;
  27. int i;
  28. for_each_rtd_components(rtd, i, component) {
  29. ret = snd_soc_component_module_get_when_open(component, cstream);
  30. if (ret < 0)
  31. break;
  32. ret = snd_soc_component_compr_open(component, cstream);
  33. if (ret < 0)
  34. break;
  35. }
  36. return ret;
  37. }
  38. static void snd_soc_compr_components_free(struct snd_compr_stream *cstream,
  39. int rollback)
  40. {
  41. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  42. struct snd_soc_component *component;
  43. int i;
  44. for_each_rtd_components(rtd, i, component) {
  45. snd_soc_component_compr_free(component, cstream, rollback);
  46. snd_soc_component_module_put_when_close(component, cstream, rollback);
  47. }
  48. }
  49. static int soc_compr_clean(struct snd_compr_stream *cstream, int rollback)
  50. {
  51. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  52. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  53. struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
  54. int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
  55. snd_soc_dpcm_mutex_lock(rtd);
  56. if (!rollback)
  57. snd_soc_runtime_deactivate(rtd, stream);
  58. snd_soc_dai_digital_mute(codec_dai, 1, stream);
  59. if (!snd_soc_dai_active(cpu_dai))
  60. cpu_dai->symmetric_rate = 0;
  61. if (!snd_soc_dai_active(codec_dai))
  62. codec_dai->symmetric_rate = 0;
  63. snd_soc_link_compr_shutdown(cstream, rollback);
  64. snd_soc_compr_components_free(cstream, rollback);
  65. snd_soc_dai_compr_shutdown(cpu_dai, cstream, rollback);
  66. if (!rollback)
  67. snd_soc_dapm_stream_stop(rtd, stream);
  68. snd_soc_dpcm_mutex_unlock(rtd);
  69. snd_soc_pcm_component_pm_runtime_put(rtd, cstream, rollback);
  70. return 0;
  71. }
  72. static int soc_compr_free(struct snd_compr_stream *cstream)
  73. {
  74. return soc_compr_clean(cstream, 0);
  75. }
  76. static int soc_compr_open(struct snd_compr_stream *cstream)
  77. {
  78. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  79. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  80. int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
  81. int ret;
  82. ret = snd_soc_pcm_component_pm_runtime_get(rtd, cstream);
  83. if (ret < 0)
  84. goto err_no_lock;
  85. snd_soc_dpcm_mutex_lock(rtd);
  86. ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
  87. if (ret < 0)
  88. goto err;
  89. ret = snd_soc_compr_components_open(cstream);
  90. if (ret < 0)
  91. goto err;
  92. ret = snd_soc_link_compr_startup(cstream);
  93. if (ret < 0)
  94. goto err;
  95. snd_soc_runtime_activate(rtd, stream);
  96. err:
  97. snd_soc_dpcm_mutex_unlock(rtd);
  98. err_no_lock:
  99. if (ret < 0)
  100. soc_compr_clean(cstream, 1);
  101. return ret;
  102. }
  103. static int soc_compr_open_fe(struct snd_compr_stream *cstream)
  104. {
  105. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  106. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(fe, 0);
  107. struct snd_soc_dpcm *dpcm;
  108. struct snd_soc_dapm_widget_list *list;
  109. int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
  110. int ret;
  111. snd_soc_card_mutex_lock(fe->card);
  112. ret = dpcm_path_get(fe, stream, &list);
  113. if (ret < 0)
  114. goto be_err;
  115. snd_soc_dpcm_mutex_lock(fe);
  116. /* calculate valid and active FE <-> BE dpcms */
  117. dpcm_add_paths(fe, stream, &list);
  118. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  119. ret = dpcm_be_dai_startup(fe, stream);
  120. if (ret < 0) {
  121. /* clean up all links */
  122. for_each_dpcm_be(fe, stream, dpcm)
  123. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  124. dpcm_be_disconnect(fe, stream);
  125. goto out;
  126. }
  127. ret = snd_soc_dai_compr_startup(cpu_dai, cstream);
  128. if (ret < 0)
  129. goto out;
  130. ret = snd_soc_compr_components_open(cstream);
  131. if (ret < 0)
  132. goto open_err;
  133. ret = snd_soc_link_compr_startup(cstream);
  134. if (ret < 0)
  135. goto machine_err;
  136. dpcm_clear_pending_state(fe, stream);
  137. dpcm_path_put(&list);
  138. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_OPEN;
  139. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  140. snd_soc_runtime_activate(fe, stream);
  141. snd_soc_dpcm_mutex_unlock(fe);
  142. snd_soc_card_mutex_unlock(fe->card);
  143. return 0;
  144. machine_err:
  145. snd_soc_compr_components_free(cstream, 1);
  146. open_err:
  147. snd_soc_dai_compr_shutdown(cpu_dai, cstream, 1);
  148. out:
  149. dpcm_path_put(&list);
  150. snd_soc_dpcm_mutex_unlock(fe);
  151. be_err:
  152. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  153. snd_soc_card_mutex_unlock(fe->card);
  154. return ret;
  155. }
  156. static int soc_compr_free_fe(struct snd_compr_stream *cstream)
  157. {
  158. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  159. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(fe, 0);
  160. struct snd_soc_dpcm *dpcm;
  161. int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
  162. snd_soc_card_mutex_lock(fe->card);
  163. snd_soc_dpcm_mutex_lock(fe);
  164. snd_soc_runtime_deactivate(fe, stream);
  165. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  166. dpcm_be_dai_hw_free(fe, stream);
  167. dpcm_be_dai_shutdown(fe, stream);
  168. /* mark FE's links ready to prune */
  169. for_each_dpcm_be(fe, stream, dpcm)
  170. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  171. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
  172. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  173. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  174. dpcm_be_disconnect(fe, stream);
  175. snd_soc_dpcm_mutex_unlock(fe);
  176. snd_soc_link_compr_shutdown(cstream, 0);
  177. snd_soc_compr_components_free(cstream, 0);
  178. snd_soc_dai_compr_shutdown(cpu_dai, cstream, 0);
  179. snd_soc_card_mutex_unlock(fe->card);
  180. return 0;
  181. }
  182. static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
  183. {
  184. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  185. struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
  186. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  187. int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
  188. int ret;
  189. snd_soc_dpcm_mutex_lock(rtd);
  190. ret = snd_soc_component_compr_trigger(cstream, cmd);
  191. if (ret < 0)
  192. goto out;
  193. ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
  194. if (ret < 0)
  195. goto out;
  196. switch (cmd) {
  197. case SNDRV_PCM_TRIGGER_START:
  198. snd_soc_dai_digital_mute(codec_dai, 0, stream);
  199. break;
  200. case SNDRV_PCM_TRIGGER_STOP:
  201. snd_soc_dai_digital_mute(codec_dai, 1, stream);
  202. break;
  203. }
  204. out:
  205. snd_soc_dpcm_mutex_unlock(rtd);
  206. return ret;
  207. }
  208. static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
  209. {
  210. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  211. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(fe, 0);
  212. int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
  213. int ret;
  214. if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
  215. cmd == SND_COMPR_TRIGGER_DRAIN)
  216. return snd_soc_component_compr_trigger(cstream, cmd);
  217. snd_soc_card_mutex_lock(fe->card);
  218. ret = snd_soc_dai_compr_trigger(cpu_dai, cstream, cmd);
  219. if (ret < 0)
  220. goto out;
  221. ret = snd_soc_component_compr_trigger(cstream, cmd);
  222. if (ret < 0)
  223. goto out;
  224. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  225. ret = dpcm_be_dai_trigger(fe, stream, cmd);
  226. switch (cmd) {
  227. case SNDRV_PCM_TRIGGER_START:
  228. case SNDRV_PCM_TRIGGER_RESUME:
  229. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  230. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  231. break;
  232. case SNDRV_PCM_TRIGGER_STOP:
  233. case SNDRV_PCM_TRIGGER_SUSPEND:
  234. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
  235. break;
  236. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  237. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
  238. break;
  239. }
  240. out:
  241. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  242. snd_soc_card_mutex_unlock(fe->card);
  243. return ret;
  244. }
  245. static int soc_compr_set_params(struct snd_compr_stream *cstream,
  246. struct snd_compr_params *params)
  247. {
  248. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  249. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  250. int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
  251. int ret;
  252. snd_soc_dpcm_mutex_lock(rtd);
  253. /*
  254. * First we call set_params for the CPU DAI, then the component
  255. * driver this should configure the SoC side. If the machine has
  256. * compressed ops then we call that as well. The expectation is
  257. * that these callbacks will configure everything for this compress
  258. * path, like configuring a PCM port for a CODEC.
  259. */
  260. ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
  261. if (ret < 0)
  262. goto err;
  263. ret = snd_soc_component_compr_set_params(cstream, params);
  264. if (ret < 0)
  265. goto err;
  266. ret = snd_soc_link_compr_set_params(cstream);
  267. if (ret < 0)
  268. goto err;
  269. snd_soc_dapm_stream_event(rtd, stream, SND_SOC_DAPM_STREAM_START);
  270. /* cancel any delayed stream shutdown that is pending */
  271. rtd->pop_wait = 0;
  272. snd_soc_dpcm_mutex_unlock(rtd);
  273. cancel_delayed_work_sync(&rtd->delayed_work);
  274. return 0;
  275. err:
  276. snd_soc_dpcm_mutex_unlock(rtd);
  277. return ret;
  278. }
  279. static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
  280. struct snd_compr_params *params)
  281. {
  282. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  283. struct snd_pcm_substream *fe_substream =
  284. fe->pcm->streams[cstream->direction].substream;
  285. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(fe, 0);
  286. int stream = cstream->direction; /* SND_COMPRESS_xxx is same as SNDRV_PCM_STREAM_xxx */
  287. int ret;
  288. snd_soc_card_mutex_lock(fe->card);
  289. /*
  290. * Create an empty hw_params for the BE as the machine driver must
  291. * fix this up to match DSP decoder and ASRC configuration.
  292. * I.e. machine driver fixup for compressed BE is mandatory.
  293. */
  294. memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
  295. sizeof(struct snd_pcm_hw_params));
  296. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  297. snd_soc_dpcm_mutex_lock(fe);
  298. ret = dpcm_be_dai_hw_params(fe, stream);
  299. snd_soc_dpcm_mutex_unlock(fe);
  300. if (ret < 0)
  301. goto out;
  302. snd_soc_dpcm_mutex_lock(fe);
  303. ret = dpcm_be_dai_prepare(fe, stream);
  304. snd_soc_dpcm_mutex_unlock(fe);
  305. if (ret < 0)
  306. goto out;
  307. ret = snd_soc_dai_compr_set_params(cpu_dai, cstream, params);
  308. if (ret < 0)
  309. goto out;
  310. ret = snd_soc_component_compr_set_params(cstream, params);
  311. if (ret < 0)
  312. goto out;
  313. ret = snd_soc_link_compr_set_params(cstream);
  314. if (ret < 0)
  315. goto out;
  316. snd_soc_dpcm_mutex_lock(fe);
  317. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
  318. snd_soc_dpcm_mutex_unlock(fe);
  319. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  320. out:
  321. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  322. snd_soc_card_mutex_unlock(fe->card);
  323. return ret;
  324. }
  325. static int soc_compr_get_params(struct snd_compr_stream *cstream,
  326. struct snd_codec *params)
  327. {
  328. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  329. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  330. int ret = 0;
  331. snd_soc_dpcm_mutex_lock(rtd);
  332. ret = snd_soc_dai_compr_get_params(cpu_dai, cstream, params);
  333. if (ret < 0)
  334. goto err;
  335. ret = snd_soc_component_compr_get_params(cstream, params);
  336. err:
  337. snd_soc_dpcm_mutex_unlock(rtd);
  338. return ret;
  339. }
  340. static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
  341. {
  342. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  343. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  344. int ret;
  345. snd_soc_dpcm_mutex_lock(rtd);
  346. ret = snd_soc_dai_compr_ack(cpu_dai, cstream, bytes);
  347. if (ret < 0)
  348. goto err;
  349. ret = snd_soc_component_compr_ack(cstream, bytes);
  350. err:
  351. snd_soc_dpcm_mutex_unlock(rtd);
  352. return ret;
  353. }
  354. static int soc_compr_pointer(struct snd_compr_stream *cstream,
  355. struct snd_compr_tstamp64 *tstamp)
  356. {
  357. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  358. int ret;
  359. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  360. snd_soc_dpcm_mutex_lock(rtd);
  361. ret = snd_soc_dai_compr_pointer(cpu_dai, cstream, tstamp);
  362. if (ret < 0)
  363. goto out;
  364. ret = snd_soc_component_compr_pointer(cstream, tstamp);
  365. out:
  366. snd_soc_dpcm_mutex_unlock(rtd);
  367. return ret;
  368. }
  369. static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
  370. struct snd_compr_metadata *metadata)
  371. {
  372. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  373. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  374. int ret;
  375. ret = snd_soc_dai_compr_set_metadata(cpu_dai, cstream, metadata);
  376. if (ret < 0)
  377. return ret;
  378. return snd_soc_component_compr_set_metadata(cstream, metadata);
  379. }
  380. static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
  381. struct snd_compr_metadata *metadata)
  382. {
  383. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  384. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  385. int ret;
  386. ret = snd_soc_dai_compr_get_metadata(cpu_dai, cstream, metadata);
  387. if (ret < 0)
  388. return ret;
  389. return snd_soc_component_compr_get_metadata(cstream, metadata);
  390. }
  391. /* ASoC Compress operations */
  392. static struct snd_compr_ops soc_compr_ops = {
  393. .open = soc_compr_open,
  394. .free = soc_compr_free,
  395. .set_params = soc_compr_set_params,
  396. .set_metadata = soc_compr_set_metadata,
  397. .get_metadata = soc_compr_get_metadata,
  398. .get_params = soc_compr_get_params,
  399. .trigger = soc_compr_trigger,
  400. .pointer = soc_compr_pointer,
  401. .ack = soc_compr_ack,
  402. .get_caps = snd_soc_component_compr_get_caps,
  403. .get_codec_caps = snd_soc_component_compr_get_codec_caps,
  404. };
  405. /* ASoC Dynamic Compress operations */
  406. static struct snd_compr_ops soc_compr_dyn_ops = {
  407. .open = soc_compr_open_fe,
  408. .free = soc_compr_free_fe,
  409. .set_params = soc_compr_set_params_fe,
  410. .get_params = soc_compr_get_params,
  411. .set_metadata = soc_compr_set_metadata,
  412. .get_metadata = soc_compr_get_metadata,
  413. .trigger = soc_compr_trigger_fe,
  414. .pointer = soc_compr_pointer,
  415. .ack = soc_compr_ack,
  416. .get_caps = snd_soc_component_compr_get_caps,
  417. .get_codec_caps = snd_soc_component_compr_get_codec_caps,
  418. };
  419. /**
  420. * snd_soc_new_compress - create a new compress.
  421. *
  422. * @rtd: The runtime for which we will create compress
  423. *
  424. * Return: 0 for success, else error.
  425. */
  426. int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd)
  427. {
  428. struct snd_soc_component *component;
  429. struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
  430. struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
  431. struct snd_compr *compr;
  432. struct snd_pcm *be_pcm;
  433. char new_name[64];
  434. int ret = 0, direction = 0;
  435. int playback = 0, capture = 0;
  436. int i;
  437. /*
  438. * make sure these are same value,
  439. * and then use these as equally
  440. */
  441. BUILD_BUG_ON((int)SNDRV_PCM_STREAM_PLAYBACK != (int)SND_COMPRESS_PLAYBACK);
  442. BUILD_BUG_ON((int)SNDRV_PCM_STREAM_CAPTURE != (int)SND_COMPRESS_CAPTURE);
  443. if (rtd->dai_link->num_cpus > 1 ||
  444. rtd->dai_link->num_codecs > 1) {
  445. dev_err(rtd->card->dev,
  446. "Compress ASoC: Multi CPU/Codec not supported\n");
  447. return -EINVAL;
  448. }
  449. if (!codec_dai) {
  450. dev_err(rtd->card->dev, "Missing codec\n");
  451. return -EINVAL;
  452. }
  453. /* check client and interface hw capabilities */
  454. if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_PLAYBACK) &&
  455. snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_PLAYBACK))
  456. playback = 1;
  457. if (snd_soc_dai_stream_valid(codec_dai, SNDRV_PCM_STREAM_CAPTURE) &&
  458. snd_soc_dai_stream_valid(cpu_dai, SNDRV_PCM_STREAM_CAPTURE))
  459. capture = 1;
  460. /*
  461. * Compress devices are unidirectional so only one of the directions
  462. * should be set, check for that (xor)
  463. */
  464. if (playback + capture != 1) {
  465. dev_err(rtd->card->dev,
  466. "Compress ASoC: Invalid direction for P %d, C %d\n",
  467. playback, capture);
  468. return -EINVAL;
  469. }
  470. if (playback)
  471. direction = SND_COMPRESS_PLAYBACK;
  472. else
  473. direction = SND_COMPRESS_CAPTURE;
  474. compr = devm_kzalloc(rtd->card->dev, sizeof(*compr), GFP_KERNEL);
  475. if (!compr)
  476. return -ENOMEM;
  477. compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
  478. GFP_KERNEL);
  479. if (!compr->ops)
  480. return -ENOMEM;
  481. if (rtd->dai_link->dynamic) {
  482. int playback = 1;
  483. int capture = 1;
  484. if (rtd->dai_link->capture_only)
  485. playback = 0;
  486. if (rtd->dai_link->playback_only)
  487. capture = 0;
  488. snprintf(new_name, sizeof(new_name), "(%s)",
  489. rtd->dai_link->stream_name);
  490. ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, rtd->id,
  491. playback, capture, &be_pcm);
  492. if (ret < 0) {
  493. dev_err(rtd->card->dev,
  494. "Compress ASoC: can't create compressed for %s: %d\n",
  495. rtd->dai_link->name, ret);
  496. return ret;
  497. }
  498. /* inherit atomicity from DAI link */
  499. be_pcm->nonatomic = rtd->dai_link->nonatomic;
  500. rtd->pcm = be_pcm;
  501. rtd->fe_compr = 1;
  502. if (playback)
  503. be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
  504. if (capture)
  505. be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
  506. memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
  507. } else {
  508. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  509. rtd->dai_link->stream_name, codec_dai->name, rtd->id);
  510. memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
  511. }
  512. for_each_rtd_components(rtd, i, component) {
  513. if (!component->driver->compress_ops ||
  514. !component->driver->compress_ops->copy)
  515. continue;
  516. compr->ops->copy = snd_soc_component_compr_copy;
  517. break;
  518. }
  519. ret = snd_compress_new(rtd->card->snd_card, rtd->id, direction,
  520. new_name, compr);
  521. if (ret < 0) {
  522. component = snd_soc_rtd_to_codec(rtd, 0)->component;
  523. dev_err(component->dev,
  524. "Compress ASoC: can't create compress for codec %s: %d\n",
  525. component->name, ret);
  526. return ret;
  527. }
  528. /* DAPM dai link stream work */
  529. rtd->close_delayed_work_func = snd_soc_close_delayed_work;
  530. rtd->compr = compr;
  531. compr->private_data = rtd;
  532. dev_dbg(rtd->card->dev, "Compress ASoC: %s <-> %s mapping ok\n",
  533. codec_dai->name, cpu_dai->name);
  534. return 0;
  535. }
  536. EXPORT_SYMBOL_GPL(snd_soc_new_compress);