soc-compress.c 18 KB

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