soc-compress.c 26 KB

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