soc-compress.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /*
  2. * soc-compress.c -- ALSA SoC Compress
  3. *
  4. * Copyright (C) 2012 Intel Corp.
  5. *
  6. * Authors: Namarta Kohli <[email protected]>
  7. * Ramesh Babu K V <[email protected]>
  8. * Vinod Koul <[email protected]>
  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 void dpcm_be_hw_params_prepare(void *data)
  330. {
  331. struct snd_compr_stream *cstream = data;
  332. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  333. struct snd_soc_pcm_runtime *be = cstream->be;
  334. int stream, ret;
  335. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  336. stream = SNDRV_PCM_STREAM_PLAYBACK;
  337. else
  338. stream = SNDRV_PCM_STREAM_CAPTURE;
  339. ret = dpcm_fe_dai_hw_params_be(fe, be,
  340. &fe->dpcm[stream].hw_params, stream);
  341. if (ret < 0) {
  342. fe->err_ops = ret;
  343. return;
  344. }
  345. ret = dpcm_fe_dai_prepare_be(fe, be, stream);
  346. if (ret < 0) {
  347. fe->err_ops = ret;
  348. return;
  349. }
  350. }
  351. static void dpcm_be_hw_params_prepare_async(void *data, async_cookie_t cookie)
  352. {
  353. dpcm_be_hw_params_prepare(data);
  354. }
  355. static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
  356. struct snd_compr_params *params)
  357. {
  358. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  359. struct snd_pcm_substream *fe_substream =
  360. fe->pcm->streams[cstream->direction].substream;
  361. struct snd_soc_platform *platform = fe->platform;
  362. struct snd_soc_pcm_runtime *be_list[DPCM_MAX_BE_USERS];
  363. struct snd_soc_dpcm *dpcm;
  364. int ret = 0, stream, i, j = 0;
  365. ASYNC_DOMAIN_EXCLUSIVE(async_domain);
  366. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  367. stream = SNDRV_PCM_STREAM_PLAYBACK;
  368. else
  369. stream = SNDRV_PCM_STREAM_CAPTURE;
  370. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  371. if (!(fe->dai_link->async_ops & ASYNC_DPCM_SND_SOC_HW_PARAMS)) {
  372. /* first we call set_params for the platform driver
  373. * this should configure the soc side
  374. * if the machine has compressed ops then we call that as well
  375. * expectation is that platform and machine will configure
  376. * everything for this compress path, like configuring pcm
  377. * port for codec
  378. */
  379. if (platform->driver->compr_ops &&
  380. platform->driver->compr_ops->set_params) {
  381. ret = platform->driver->compr_ops->set_params(cstream,
  382. params);
  383. if (ret < 0)
  384. goto out;
  385. }
  386. if (fe->dai_link->compr_ops &&
  387. fe->dai_link->compr_ops->set_params) {
  388. ret = fe->dai_link->compr_ops->set_params(cstream);
  389. if (ret < 0)
  390. goto out;
  391. }
  392. /*
  393. * Create an empty hw_params for the BE as the machine
  394. * driver must fix this up to match DSP decoder and
  395. * ASRC configuration.
  396. * I.e. machine driver fixup for compressed BE is
  397. * mandatory.
  398. */
  399. memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
  400. sizeof(struct snd_pcm_hw_params));
  401. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  402. ret = dpcm_be_dai_hw_params(fe, stream);
  403. if (ret < 0)
  404. goto out;
  405. ret = dpcm_be_dai_prepare(fe, stream);
  406. if (ret < 0)
  407. goto out;
  408. } else {
  409. /*
  410. * Create an empty hw_params for the BE as the machine
  411. * driver must fix this up to match DSP decoder and
  412. * ASRC configuration.
  413. * I.e. machine driver fixup for compressed BE is
  414. * mandatory.
  415. */
  416. memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
  417. sizeof(struct snd_pcm_hw_params));
  418. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  419. list_for_each_entry(dpcm,
  420. &fe->dpcm[stream].be_clients, list_be) {
  421. struct snd_soc_pcm_runtime *be = dpcm->be;
  422. if (be->dai_link->async_ops &
  423. ASYNC_DPCM_SND_SOC_HW_PARAMS) {
  424. cstream->be = be;
  425. async_schedule_domain(
  426. dpcm_be_hw_params_prepare_async,
  427. cstream, &async_domain);
  428. } else {
  429. be_list[j++] = be;
  430. if (j == DPCM_MAX_BE_USERS) {
  431. dev_dbg(fe->dev,
  432. "ASoC: MAX backend users!\n");
  433. break;
  434. }
  435. }
  436. }
  437. for (i = 0; i < j; i++) {
  438. cstream->be = be_list[i];
  439. dpcm_be_hw_params_prepare(cstream);
  440. }
  441. /* first we call set_params for the platform driver
  442. * this should configure the soc side
  443. * if the machine has compressed ops then we call that as well
  444. * expectation is that platform and machine will configure
  445. * everything this compress path, like configuring pcm port
  446. * for codec
  447. */
  448. if (platform->driver->compr_ops &&
  449. platform->driver->compr_ops->set_params) {
  450. ret = platform->driver->compr_ops->set_params(cstream,
  451. params);
  452. if (ret < 0)
  453. goto exit;
  454. }
  455. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
  456. if (fe->dai_link->compr_ops &&
  457. fe->dai_link->compr_ops->set_params) {
  458. ret = fe->dai_link->compr_ops->set_params(cstream);
  459. if (ret < 0)
  460. goto exit;
  461. }
  462. exit:
  463. async_synchronize_full_domain(&async_domain);
  464. if (fe->err_ops < 0 || ret < 0)
  465. goto out;
  466. }
  467. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
  468. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  469. out:
  470. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  471. mutex_unlock(&fe->card->mutex);
  472. return ret;
  473. }
  474. static int soc_compr_get_params(struct snd_compr_stream *cstream,
  475. struct snd_codec *params)
  476. {
  477. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  478. struct snd_soc_platform *platform = rtd->platform;
  479. int ret = 0;
  480. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  481. if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
  482. ret = platform->driver->compr_ops->get_params(cstream, params);
  483. mutex_unlock(&rtd->pcm_mutex);
  484. return ret;
  485. }
  486. static int soc_compr_get_caps(struct snd_compr_stream *cstream,
  487. struct snd_compr_caps *caps)
  488. {
  489. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  490. struct snd_soc_platform *platform = rtd->platform;
  491. int ret = 0;
  492. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  493. if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
  494. ret = platform->driver->compr_ops->get_caps(cstream, caps);
  495. mutex_unlock(&rtd->pcm_mutex);
  496. return ret;
  497. }
  498. static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
  499. struct snd_compr_codec_caps *codec)
  500. {
  501. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  502. struct snd_soc_platform *platform = rtd->platform;
  503. int ret = 0;
  504. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  505. if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
  506. ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
  507. mutex_unlock(&rtd->pcm_mutex);
  508. return ret;
  509. }
  510. static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
  511. {
  512. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  513. struct snd_soc_platform *platform = rtd->platform;
  514. int ret = 0;
  515. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  516. if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
  517. ret = platform->driver->compr_ops->ack(cstream, bytes);
  518. mutex_unlock(&rtd->pcm_mutex);
  519. return ret;
  520. }
  521. static int soc_compr_pointer(struct snd_compr_stream *cstream,
  522. struct snd_compr_tstamp *tstamp)
  523. {
  524. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  525. struct snd_soc_platform *platform = rtd->platform;
  526. int ret = 0;
  527. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  528. if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
  529. ret = platform->driver->compr_ops->pointer(cstream, tstamp);
  530. mutex_unlock(&rtd->pcm_mutex);
  531. return ret;
  532. }
  533. static int soc_compr_copy(struct snd_compr_stream *cstream,
  534. char __user *buf, size_t count)
  535. {
  536. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  537. struct snd_soc_platform *platform = rtd->platform;
  538. int ret = 0;
  539. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  540. if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
  541. ret = platform->driver->compr_ops->copy(cstream, buf, count);
  542. mutex_unlock(&rtd->pcm_mutex);
  543. return ret;
  544. }
  545. static int sst_compr_set_next_track_param(struct snd_compr_stream *cstream,
  546. union snd_codec_options *codec_options)
  547. {
  548. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  549. struct snd_soc_platform *platform = rtd->platform;
  550. int ret = 0;
  551. if (platform->driver->compr_ops &&
  552. platform->driver->compr_ops->set_next_track_param)
  553. ret = platform->driver->compr_ops->set_next_track_param(cstream,
  554. codec_options);
  555. return ret;
  556. }
  557. static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
  558. struct snd_compr_metadata *metadata)
  559. {
  560. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  561. struct snd_soc_platform *platform = rtd->platform;
  562. int ret = 0;
  563. if (platform->driver->compr_ops && platform->driver->compr_ops->set_metadata)
  564. ret = platform->driver->compr_ops->set_metadata(cstream, metadata);
  565. return ret;
  566. }
  567. static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
  568. struct snd_compr_metadata *metadata)
  569. {
  570. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  571. struct snd_soc_platform *platform = rtd->platform;
  572. int ret = 0;
  573. if (platform->driver->compr_ops && platform->driver->compr_ops->get_metadata)
  574. ret = platform->driver->compr_ops->get_metadata(cstream, metadata);
  575. return ret;
  576. }
  577. /* ASoC Compress operations */
  578. static struct snd_compr_ops soc_compr_ops = {
  579. .open = soc_compr_open,
  580. .free = soc_compr_free,
  581. .set_params = soc_compr_set_params,
  582. .set_metadata = soc_compr_set_metadata,
  583. .set_next_track_param = sst_compr_set_next_track_param,
  584. .get_metadata = soc_compr_get_metadata,
  585. .get_params = soc_compr_get_params,
  586. .trigger = soc_compr_trigger,
  587. .pointer = soc_compr_pointer,
  588. .ack = soc_compr_ack,
  589. .get_caps = soc_compr_get_caps,
  590. .get_codec_caps = soc_compr_get_codec_caps
  591. };
  592. /* ASoC Dynamic Compress operations */
  593. static struct snd_compr_ops soc_compr_dyn_ops = {
  594. .open = soc_compr_open_fe,
  595. .free = soc_compr_free_fe,
  596. .set_params = soc_compr_set_params_fe,
  597. .get_params = soc_compr_get_params,
  598. .set_metadata = soc_compr_set_metadata,
  599. .set_next_track_param = sst_compr_set_next_track_param,
  600. .get_metadata = soc_compr_get_metadata,
  601. .trigger = soc_compr_trigger_fe,
  602. .pointer = soc_compr_pointer,
  603. .ack = soc_compr_ack,
  604. .get_caps = soc_compr_get_caps,
  605. .get_codec_caps = soc_compr_get_codec_caps
  606. };
  607. /**
  608. * snd_soc_new_compress - create a new compress.
  609. *
  610. * @rtd: The runtime for which we will create compress
  611. * @num: the device index number (zero based - shared with normal PCMs)
  612. *
  613. * Return: 0 for success, else error.
  614. */
  615. int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
  616. {
  617. struct snd_soc_codec *codec = rtd->codec;
  618. struct snd_soc_platform *platform = rtd->platform;
  619. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  620. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  621. struct snd_compr *compr;
  622. struct snd_pcm *be_pcm;
  623. char new_name[64];
  624. int ret = 0, direction = 0;
  625. int playback = 0, capture = 0;
  626. if (rtd->num_codecs > 1) {
  627. dev_err(rtd->card->dev, "Multicodec not supported for compressed stream\n");
  628. return -EINVAL;
  629. }
  630. /* check client and interface hw capabilities */
  631. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  632. rtd->dai_link->stream_name, codec_dai->name, num);
  633. if (codec_dai->driver->playback.channels_min)
  634. playback = 1;
  635. if (codec_dai->driver->capture.channels_min)
  636. capture = 1;
  637. capture = capture && cpu_dai->driver->capture.channels_min;
  638. playback = playback && cpu_dai->driver->playback.channels_min;
  639. /*
  640. * Compress devices are unidirectional so only one of the directions
  641. * should be set, check for that (xor)
  642. */
  643. if (playback + capture != 1) {
  644. dev_err(rtd->card->dev, "Invalid direction for compress P %d, C %d\n",
  645. playback, capture);
  646. return -EINVAL;
  647. }
  648. if(playback)
  649. direction = SND_COMPRESS_PLAYBACK;
  650. else
  651. direction = SND_COMPRESS_CAPTURE;
  652. compr = kzalloc(sizeof(*compr), GFP_KERNEL);
  653. if (compr == NULL) {
  654. snd_printk(KERN_ERR "Cannot allocate compr\n");
  655. return -ENOMEM;
  656. }
  657. compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
  658. GFP_KERNEL);
  659. if (compr->ops == NULL) {
  660. dev_err(rtd->card->dev, "Cannot allocate compressed ops\n");
  661. ret = -ENOMEM;
  662. goto compr_err;
  663. }
  664. if (rtd->dai_link->dynamic) {
  665. snprintf(new_name, sizeof(new_name), "(%s)",
  666. rtd->dai_link->stream_name);
  667. ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
  668. rtd->dai_link->dpcm_playback,
  669. rtd->dai_link->dpcm_capture, &be_pcm);
  670. if (ret < 0) {
  671. dev_err(rtd->card->dev, "ASoC: can't create compressed for %s\n",
  672. rtd->dai_link->name);
  673. goto compr_err;
  674. }
  675. rtd->pcm = be_pcm;
  676. rtd->fe_compr = 1;
  677. if (rtd->dai_link->dpcm_playback)
  678. be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
  679. else if (rtd->dai_link->dpcm_capture)
  680. be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
  681. memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
  682. } else
  683. memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
  684. /* Add copy callback for not memory mapped DSPs */
  685. if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
  686. compr->ops->copy = soc_compr_copy;
  687. mutex_init(&compr->lock);
  688. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  689. rtd->dai_link->stream_name,
  690. rtd->codec_dai->name, num);
  691. ret = snd_compress_new(rtd->card->snd_card, num, direction,
  692. new_name, compr);
  693. if (ret < 0) {
  694. pr_err("compress asoc: can't create compress for codec %s\n",
  695. codec->component.name);
  696. goto compr_err;
  697. }
  698. /* DAPM dai link stream work */
  699. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  700. rtd->compr = compr;
  701. compr->private_data = rtd;
  702. if (platform->driver->pcm_new) {
  703. ret = platform->driver->pcm_new(rtd);
  704. if (ret < 0) {
  705. pr_err("asoc: compress pcm constructor failed\n");
  706. goto compr_err;
  707. }
  708. }
  709. dev_dbg(rtd->card->dev, "compress asoc: %s <-> %s mapping ok\n",
  710. codec_dai->name, cpu_dai->name);
  711. return ret;
  712. compr_err:
  713. kfree(compr);
  714. return ret;
  715. }
  716. EXPORT_SYMBOL_GPL(snd_soc_new_compress);