soc-compress.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. * soc-compress.c -- ALSA SoC Compress
  3. *
  4. * Copyright (C) 2012 Intel Corp.
  5. *
  6. * Authors: Namarta Kohli <namartax.kohli@intel.com>
  7. * Ramesh Babu K V <ramesh.babu@linux.intel.com>
  8. * Vinod Koul <vinod.koul@linux.intel.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/delay.h>
  19. #include <linux/slab.h>
  20. #include <linux/workqueue.h>
  21. #include <sound/core.h>
  22. #include <sound/compress_params.h>
  23. #include <sound/compress_driver.h>
  24. #include <sound/soc.h>
  25. #include <sound/initval.h>
  26. #include <sound/soc-dpcm.h>
  27. static int soc_compr_open(struct snd_compr_stream *cstream)
  28. {
  29. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  30. struct snd_soc_platform *platform = rtd->platform;
  31. 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 fe_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. fe_err:
  115. if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
  116. fe->dai_link->compr_ops->shutdown(cstream);
  117. machine_err:
  118. if (platform->driver->compr_ops && platform->driver->compr_ops->free)
  119. platform->driver->compr_ops->free(cstream);
  120. out:
  121. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  122. mutex_unlock(&fe->card->mutex);
  123. return ret;
  124. }
  125. /*
  126. * Power down the audio subsystem pmdown_time msecs after close is called.
  127. * This is to ensure there are no pops or clicks in between any music tracks
  128. * due to DAPM power cycling.
  129. */
  130. static void close_delayed_work(struct work_struct *work)
  131. {
  132. struct snd_soc_pcm_runtime *rtd =
  133. container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
  134. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  135. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  136. dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
  137. codec_dai->driver->playback.stream_name,
  138. codec_dai->playback_active ? "active" : "inactive",
  139. rtd->pop_wait ? "yes" : "no");
  140. /* are we waiting on this codec DAI stream */
  141. if (rtd->pop_wait == 1) {
  142. rtd->pop_wait = 0;
  143. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
  144. SND_SOC_DAPM_STREAM_STOP);
  145. }
  146. mutex_unlock(&rtd->pcm_mutex);
  147. }
  148. static int soc_compr_free(struct snd_compr_stream *cstream)
  149. {
  150. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  151. struct snd_soc_platform *platform = rtd->platform;
  152. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  153. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  154. int stream;
  155. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  156. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  157. stream = SNDRV_PCM_STREAM_PLAYBACK;
  158. else
  159. stream = SNDRV_PCM_STREAM_CAPTURE;
  160. snd_soc_runtime_deactivate(rtd, stream);
  161. snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
  162. if (!cpu_dai->active)
  163. cpu_dai->rate = 0;
  164. if (!codec_dai->active)
  165. codec_dai->rate = 0;
  166. if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
  167. rtd->dai_link->compr_ops->shutdown(cstream);
  168. if (platform->driver->compr_ops && platform->driver->compr_ops->free)
  169. platform->driver->compr_ops->free(cstream);
  170. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  171. if (snd_soc_runtime_ignore_pmdown_time(rtd)) {
  172. snd_soc_dapm_stream_event(rtd,
  173. SNDRV_PCM_STREAM_PLAYBACK,
  174. SND_SOC_DAPM_STREAM_STOP);
  175. } else {
  176. rtd->pop_wait = 1;
  177. queue_delayed_work(system_power_efficient_wq,
  178. &rtd->delayed_work,
  179. msecs_to_jiffies(rtd->pmdown_time));
  180. }
  181. } else {
  182. /* capture streams can be powered down now */
  183. snd_soc_dapm_stream_event(rtd,
  184. SNDRV_PCM_STREAM_CAPTURE,
  185. SND_SOC_DAPM_STREAM_STOP);
  186. }
  187. mutex_unlock(&rtd->pcm_mutex);
  188. return 0;
  189. }
  190. static int soc_compr_free_fe(struct snd_compr_stream *cstream)
  191. {
  192. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  193. struct snd_soc_platform *platform = fe->platform;
  194. struct snd_soc_dpcm *dpcm;
  195. int stream, ret;
  196. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  197. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  198. stream = SNDRV_PCM_STREAM_PLAYBACK;
  199. else
  200. stream = SNDRV_PCM_STREAM_CAPTURE;
  201. snd_soc_runtime_deactivate(fe, stream);
  202. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  203. ret = dpcm_be_dai_hw_free(fe, stream);
  204. if (ret < 0)
  205. dev_err(fe->dev, "compressed hw_free failed %d\n", ret);
  206. ret = dpcm_be_dai_shutdown(fe, stream);
  207. /* mark FE's links ready to prune */
  208. list_for_each_entry(dpcm, &fe->dpcm[stream].be_clients, list_be)
  209. dpcm->state = SND_SOC_DPCM_LINK_STATE_FREE;
  210. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_STOP);
  211. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_CLOSE;
  212. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  213. dpcm_be_disconnect(fe, stream);
  214. fe->dpcm[stream].runtime = NULL;
  215. if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->shutdown)
  216. fe->dai_link->compr_ops->shutdown(cstream);
  217. if (platform->driver->compr_ops && platform->driver->compr_ops->free)
  218. platform->driver->compr_ops->free(cstream);
  219. mutex_unlock(&fe->card->mutex);
  220. return 0;
  221. }
  222. static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
  223. {
  224. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  225. struct snd_soc_platform *platform = rtd->platform;
  226. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  227. int ret = 0;
  228. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  229. if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
  230. ret = platform->driver->compr_ops->trigger(cstream, cmd);
  231. if (ret < 0)
  232. goto out;
  233. }
  234. switch (cmd) {
  235. case SNDRV_PCM_TRIGGER_START:
  236. snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
  237. break;
  238. case SNDRV_PCM_TRIGGER_STOP:
  239. snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
  240. break;
  241. }
  242. out:
  243. mutex_unlock(&rtd->pcm_mutex);
  244. return ret;
  245. }
  246. static int soc_compr_trigger_fe(struct snd_compr_stream *cstream, int cmd)
  247. {
  248. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  249. struct snd_soc_platform *platform = fe->platform;
  250. int ret = 0, stream;
  251. if (cmd == SND_COMPR_TRIGGER_PARTIAL_DRAIN ||
  252. cmd == SND_COMPR_TRIGGER_DRAIN) {
  253. if (platform->driver->compr_ops &&
  254. platform->driver->compr_ops->trigger)
  255. return platform->driver->compr_ops->trigger(cstream,
  256. cmd);
  257. }
  258. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  259. stream = SNDRV_PCM_STREAM_PLAYBACK;
  260. else
  261. stream = SNDRV_PCM_STREAM_CAPTURE;
  262. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  263. if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
  264. ret = platform->driver->compr_ops->trigger(cstream, cmd);
  265. if (ret < 0)
  266. goto out;
  267. }
  268. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  269. ret = dpcm_be_dai_trigger(fe, stream, cmd);
  270. switch (cmd) {
  271. case SNDRV_PCM_TRIGGER_START:
  272. case SNDRV_PCM_TRIGGER_RESUME:
  273. case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
  274. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_START;
  275. break;
  276. case SNDRV_PCM_TRIGGER_STOP:
  277. case SNDRV_PCM_TRIGGER_SUSPEND:
  278. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_STOP;
  279. break;
  280. case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
  281. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PAUSED;
  282. break;
  283. }
  284. out:
  285. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  286. mutex_unlock(&fe->card->mutex);
  287. return ret;
  288. }
  289. static int soc_compr_set_params(struct snd_compr_stream *cstream,
  290. struct snd_compr_params *params)
  291. {
  292. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  293. struct snd_soc_platform *platform = rtd->platform;
  294. int ret = 0;
  295. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  296. /* first we call set_params for the platform driver
  297. * this should configure the soc side
  298. * if the machine has compressed ops then we call that as well
  299. * expectation is that platform and machine will configure everything
  300. * for this compress path, like configuring pcm port for codec
  301. */
  302. if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
  303. ret = platform->driver->compr_ops->set_params(cstream, params);
  304. if (ret < 0)
  305. goto err;
  306. }
  307. if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
  308. ret = rtd->dai_link->compr_ops->set_params(cstream);
  309. if (ret < 0)
  310. goto err;
  311. }
  312. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  313. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
  314. SND_SOC_DAPM_STREAM_START);
  315. else
  316. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
  317. SND_SOC_DAPM_STREAM_START);
  318. /* cancel any delayed stream shutdown that is pending */
  319. rtd->pop_wait = 0;
  320. mutex_unlock(&rtd->pcm_mutex);
  321. cancel_delayed_work_sync(&rtd->delayed_work);
  322. return ret;
  323. err:
  324. mutex_unlock(&rtd->pcm_mutex);
  325. return ret;
  326. }
  327. static int soc_compr_set_params_fe(struct snd_compr_stream *cstream,
  328. struct snd_compr_params *params)
  329. {
  330. struct snd_soc_pcm_runtime *fe = cstream->private_data;
  331. struct snd_pcm_substream *fe_substream =
  332. fe->pcm->streams[cstream->direction].substream;
  333. struct snd_soc_platform *platform = fe->platform;
  334. int ret = 0, stream;
  335. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  336. stream = SNDRV_PCM_STREAM_PLAYBACK;
  337. else
  338. stream = SNDRV_PCM_STREAM_CAPTURE;
  339. mutex_lock_nested(&fe->card->mutex, SND_SOC_CARD_CLASS_RUNTIME);
  340. if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
  341. ret = platform->driver->compr_ops->set_params(cstream, params);
  342. if (ret < 0)
  343. goto out;
  344. }
  345. if (fe->dai_link->compr_ops && fe->dai_link->compr_ops->set_params) {
  346. ret = fe->dai_link->compr_ops->set_params(cstream);
  347. if (ret < 0)
  348. goto out;
  349. }
  350. /*
  351. * Create an empty hw_params for the BE as the machine driver must
  352. * fix this up to match DSP decoder and ASRC configuration.
  353. * I.e. machine driver fixup for compressed BE is mandatory.
  354. */
  355. memset(&fe->dpcm[fe_substream->stream].hw_params, 0,
  356. sizeof(struct snd_pcm_hw_params));
  357. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_FE;
  358. ret = dpcm_be_dai_hw_params(fe, stream);
  359. if (ret < 0)
  360. goto out;
  361. ret = dpcm_be_dai_prepare(fe, stream);
  362. if (ret < 0)
  363. goto out;
  364. dpcm_dapm_stream_event(fe, stream, SND_SOC_DAPM_STREAM_START);
  365. fe->dpcm[stream].state = SND_SOC_DPCM_STATE_PREPARE;
  366. out:
  367. fe->dpcm[stream].runtime_update = SND_SOC_DPCM_UPDATE_NO;
  368. mutex_unlock(&fe->card->mutex);
  369. return ret;
  370. }
  371. static int soc_compr_get_params(struct snd_compr_stream *cstream,
  372. struct snd_codec *params)
  373. {
  374. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  375. struct snd_soc_platform *platform = rtd->platform;
  376. int ret = 0;
  377. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  378. if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
  379. ret = platform->driver->compr_ops->get_params(cstream, params);
  380. mutex_unlock(&rtd->pcm_mutex);
  381. return ret;
  382. }
  383. static int soc_compr_get_caps(struct snd_compr_stream *cstream,
  384. struct snd_compr_caps *caps)
  385. {
  386. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  387. struct snd_soc_platform *platform = rtd->platform;
  388. int ret = 0;
  389. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  390. if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
  391. ret = platform->driver->compr_ops->get_caps(cstream, caps);
  392. mutex_unlock(&rtd->pcm_mutex);
  393. return ret;
  394. }
  395. static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
  396. struct snd_compr_codec_caps *codec)
  397. {
  398. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  399. struct snd_soc_platform *platform = rtd->platform;
  400. int ret = 0;
  401. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  402. if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
  403. ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
  404. mutex_unlock(&rtd->pcm_mutex);
  405. return ret;
  406. }
  407. static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
  408. {
  409. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  410. struct snd_soc_platform *platform = rtd->platform;
  411. int ret = 0;
  412. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  413. if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
  414. ret = platform->driver->compr_ops->ack(cstream, bytes);
  415. mutex_unlock(&rtd->pcm_mutex);
  416. return ret;
  417. }
  418. static int soc_compr_pointer(struct snd_compr_stream *cstream,
  419. struct snd_compr_tstamp *tstamp)
  420. {
  421. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  422. struct snd_soc_platform *platform = rtd->platform;
  423. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  424. if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
  425. platform->driver->compr_ops->pointer(cstream, tstamp);
  426. mutex_unlock(&rtd->pcm_mutex);
  427. return 0;
  428. }
  429. static int soc_compr_copy(struct snd_compr_stream *cstream,
  430. char __user *buf, size_t count)
  431. {
  432. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  433. struct snd_soc_platform *platform = rtd->platform;
  434. int ret = 0;
  435. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  436. if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
  437. ret = platform->driver->compr_ops->copy(cstream, buf, count);
  438. mutex_unlock(&rtd->pcm_mutex);
  439. return ret;
  440. }
  441. static int soc_compr_set_metadata(struct snd_compr_stream *cstream,
  442. struct snd_compr_metadata *metadata)
  443. {
  444. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  445. struct snd_soc_platform *platform = rtd->platform;
  446. int ret = 0;
  447. if (platform->driver->compr_ops && platform->driver->compr_ops->set_metadata)
  448. ret = platform->driver->compr_ops->set_metadata(cstream, metadata);
  449. return ret;
  450. }
  451. static int soc_compr_get_metadata(struct snd_compr_stream *cstream,
  452. struct snd_compr_metadata *metadata)
  453. {
  454. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  455. struct snd_soc_platform *platform = rtd->platform;
  456. int ret = 0;
  457. if (platform->driver->compr_ops && platform->driver->compr_ops->get_metadata)
  458. ret = platform->driver->compr_ops->get_metadata(cstream, metadata);
  459. return ret;
  460. }
  461. /* ASoC Compress operations */
  462. static struct snd_compr_ops soc_compr_ops = {
  463. .open = soc_compr_open,
  464. .free = soc_compr_free,
  465. .set_params = soc_compr_set_params,
  466. .set_metadata = soc_compr_set_metadata,
  467. .get_metadata = soc_compr_get_metadata,
  468. .get_params = soc_compr_get_params,
  469. .trigger = soc_compr_trigger,
  470. .pointer = soc_compr_pointer,
  471. .ack = soc_compr_ack,
  472. .get_caps = soc_compr_get_caps,
  473. .get_codec_caps = soc_compr_get_codec_caps
  474. };
  475. /* ASoC Dynamic Compress operations */
  476. static struct snd_compr_ops soc_compr_dyn_ops = {
  477. .open = soc_compr_open_fe,
  478. .free = soc_compr_free_fe,
  479. .set_params = soc_compr_set_params_fe,
  480. .get_params = soc_compr_get_params,
  481. .set_metadata = soc_compr_set_metadata,
  482. .get_metadata = soc_compr_get_metadata,
  483. .trigger = soc_compr_trigger_fe,
  484. .pointer = soc_compr_pointer,
  485. .ack = soc_compr_ack,
  486. .get_caps = soc_compr_get_caps,
  487. .get_codec_caps = soc_compr_get_codec_caps
  488. };
  489. /**
  490. * snd_soc_new_compress - create a new compress.
  491. *
  492. * @rtd: The runtime for which we will create compress
  493. * @num: the device index number (zero based - shared with normal PCMs)
  494. *
  495. * Return: 0 for success, else error.
  496. */
  497. int snd_soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
  498. {
  499. struct snd_soc_codec *codec = rtd->codec;
  500. struct snd_soc_platform *platform = rtd->platform;
  501. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  502. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  503. struct snd_compr *compr;
  504. struct snd_pcm *be_pcm;
  505. char new_name[64];
  506. int ret = 0, direction = 0;
  507. int playback = 0, capture = 0;
  508. if (rtd->num_codecs > 1) {
  509. dev_err(rtd->card->dev, "Multicodec not supported for compressed stream\n");
  510. return -EINVAL;
  511. }
  512. /* check client and interface hw capabilities */
  513. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  514. rtd->dai_link->stream_name, codec_dai->name, num);
  515. if (codec_dai->driver->playback.channels_min)
  516. playback = 1;
  517. if (codec_dai->driver->capture.channels_min)
  518. capture = 1;
  519. capture = capture && cpu_dai->driver->capture.channels_min;
  520. playback = playback && cpu_dai->driver->playback.channels_min;
  521. /*
  522. * Compress devices are unidirectional so only one of the directions
  523. * should be set, check for that (xor)
  524. */
  525. if (playback + capture != 1) {
  526. dev_err(rtd->card->dev, "Invalid direction for compress P %d, C %d\n",
  527. playback, capture);
  528. return -EINVAL;
  529. }
  530. if(playback)
  531. direction = SND_COMPRESS_PLAYBACK;
  532. else
  533. direction = SND_COMPRESS_CAPTURE;
  534. compr = kzalloc(sizeof(*compr), GFP_KERNEL);
  535. if (compr == NULL) {
  536. snd_printk(KERN_ERR "Cannot allocate compr\n");
  537. return -ENOMEM;
  538. }
  539. compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
  540. GFP_KERNEL);
  541. if (compr->ops == NULL) {
  542. dev_err(rtd->card->dev, "Cannot allocate compressed ops\n");
  543. ret = -ENOMEM;
  544. goto compr_err;
  545. }
  546. if (rtd->dai_link->dynamic) {
  547. snprintf(new_name, sizeof(new_name), "(%s)",
  548. rtd->dai_link->stream_name);
  549. ret = snd_pcm_new_internal(rtd->card->snd_card, new_name, num,
  550. rtd->dai_link->dpcm_playback,
  551. rtd->dai_link->dpcm_capture, &be_pcm);
  552. if (ret < 0) {
  553. dev_err(rtd->card->dev, "ASoC: can't create compressed for %s\n",
  554. rtd->dai_link->name);
  555. goto compr_err;
  556. }
  557. rtd->pcm = be_pcm;
  558. rtd->fe_compr = 1;
  559. if (rtd->dai_link->dpcm_playback)
  560. be_pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].substream->private_data = rtd;
  561. else if (rtd->dai_link->dpcm_capture)
  562. be_pcm->streams[SNDRV_PCM_STREAM_CAPTURE].substream->private_data = rtd;
  563. memcpy(compr->ops, &soc_compr_dyn_ops, sizeof(soc_compr_dyn_ops));
  564. } else
  565. memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
  566. /* Add copy callback for not memory mapped DSPs */
  567. if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
  568. compr->ops->copy = soc_compr_copy;
  569. mutex_init(&compr->lock);
  570. ret = snd_compress_new(rtd->card->snd_card, num, direction, compr);
  571. if (ret < 0) {
  572. pr_err("compress asoc: can't create compress for codec %s\n",
  573. codec->component.name);
  574. goto compr_err;
  575. }
  576. /* DAPM dai link stream work */
  577. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  578. rtd->compr = compr;
  579. compr->private_data = rtd;
  580. printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name,
  581. cpu_dai->name);
  582. return ret;
  583. compr_err:
  584. kfree(compr);
  585. return ret;
  586. }
  587. EXPORT_SYMBOL_GPL(snd_soc_new_compress);