soc-compress.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. static int soc_compr_open(struct snd_compr_stream *cstream)
  27. {
  28. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  29. struct snd_soc_platform *platform = rtd->platform;
  30. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  31. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  32. int ret = 0;
  33. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  34. if (platform->driver->compr_ops && platform->driver->compr_ops->open) {
  35. ret = platform->driver->compr_ops->open(cstream);
  36. if (ret < 0) {
  37. pr_err("compress asoc: can't open platform %s\n", platform->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. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  49. cpu_dai->playback_active++;
  50. codec_dai->playback_active++;
  51. } else {
  52. cpu_dai->capture_active++;
  53. codec_dai->capture_active++;
  54. }
  55. cpu_dai->active++;
  56. codec_dai->active++;
  57. rtd->codec->active++;
  58. mutex_unlock(&rtd->pcm_mutex);
  59. return 0;
  60. machine_err:
  61. if (platform->driver->compr_ops && platform->driver->compr_ops->free)
  62. platform->driver->compr_ops->free(cstream);
  63. out:
  64. mutex_unlock(&rtd->pcm_mutex);
  65. return ret;
  66. }
  67. /*
  68. * Power down the audio subsystem pmdown_time msecs after close is called.
  69. * This is to ensure there are no pops or clicks in between any music tracks
  70. * due to DAPM power cycling.
  71. */
  72. static void close_delayed_work(struct work_struct *work)
  73. {
  74. struct snd_soc_pcm_runtime *rtd =
  75. container_of(work, struct snd_soc_pcm_runtime, delayed_work.work);
  76. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  77. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  78. dev_dbg(rtd->dev, "ASoC: pop wq checking: %s status: %s waiting: %s\n",
  79. codec_dai->driver->playback.stream_name,
  80. codec_dai->playback_active ? "active" : "inactive",
  81. rtd->pop_wait ? "yes" : "no");
  82. /* are we waiting on this codec DAI stream */
  83. if (rtd->pop_wait == 1) {
  84. rtd->pop_wait = 0;
  85. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
  86. SND_SOC_DAPM_STREAM_STOP);
  87. }
  88. mutex_unlock(&rtd->pcm_mutex);
  89. }
  90. static int soc_compr_free(struct snd_compr_stream *cstream)
  91. {
  92. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  93. struct snd_soc_platform *platform = rtd->platform;
  94. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  95. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  96. struct snd_soc_codec *codec = rtd->codec;
  97. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  98. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  99. cpu_dai->playback_active--;
  100. codec_dai->playback_active--;
  101. } else {
  102. cpu_dai->capture_active--;
  103. codec_dai->capture_active--;
  104. }
  105. snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
  106. cpu_dai->active--;
  107. codec_dai->active--;
  108. codec->active--;
  109. if (!cpu_dai->active)
  110. cpu_dai->rate = 0;
  111. if (!codec_dai->active)
  112. codec_dai->rate = 0;
  113. if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
  114. rtd->dai_link->compr_ops->shutdown(cstream);
  115. if (platform->driver->compr_ops && platform->driver->compr_ops->free)
  116. platform->driver->compr_ops->free(cstream);
  117. cpu_dai->runtime = NULL;
  118. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  119. if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
  120. rtd->dai_link->ignore_pmdown_time) {
  121. snd_soc_dapm_stream_event(rtd,
  122. SNDRV_PCM_STREAM_PLAYBACK,
  123. SND_SOC_DAPM_STREAM_STOP);
  124. } else {
  125. rtd->pop_wait = 1;
  126. schedule_delayed_work(&rtd->delayed_work,
  127. msecs_to_jiffies(rtd->pmdown_time));
  128. }
  129. } else {
  130. /* capture streams can be powered down now */
  131. snd_soc_dapm_stream_event(rtd,
  132. SNDRV_PCM_STREAM_CAPTURE,
  133. SND_SOC_DAPM_STREAM_STOP);
  134. }
  135. mutex_unlock(&rtd->pcm_mutex);
  136. return 0;
  137. }
  138. static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
  139. {
  140. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  141. struct snd_soc_platform *platform = rtd->platform;
  142. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  143. int ret = 0;
  144. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  145. if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
  146. ret = platform->driver->compr_ops->trigger(cstream, cmd);
  147. if (ret < 0)
  148. goto out;
  149. }
  150. switch (cmd) {
  151. case SNDRV_PCM_TRIGGER_START:
  152. snd_soc_dai_digital_mute(codec_dai, 0, cstream->direction);
  153. break;
  154. case SNDRV_PCM_TRIGGER_STOP:
  155. snd_soc_dai_digital_mute(codec_dai, 1, cstream->direction);
  156. break;
  157. }
  158. out:
  159. mutex_unlock(&rtd->pcm_mutex);
  160. return ret;
  161. }
  162. static int soc_compr_set_params(struct snd_compr_stream *cstream,
  163. struct snd_compr_params *params)
  164. {
  165. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  166. struct snd_soc_platform *platform = rtd->platform;
  167. int ret = 0;
  168. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  169. /* first we call set_params for the platform driver
  170. * this should configure the soc side
  171. * if the machine has compressed ops then we call that as well
  172. * expectation is that platform and machine will configure everything
  173. * for this compress path, like configuring pcm port for codec
  174. */
  175. if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
  176. ret = platform->driver->compr_ops->set_params(cstream, params);
  177. if (ret < 0)
  178. goto err;
  179. }
  180. if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
  181. ret = rtd->dai_link->compr_ops->set_params(cstream);
  182. if (ret < 0)
  183. goto err;
  184. }
  185. if (cstream->direction == SND_COMPRESS_PLAYBACK)
  186. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
  187. SND_SOC_DAPM_STREAM_START);
  188. else
  189. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_CAPTURE,
  190. SND_SOC_DAPM_STREAM_START);
  191. /* cancel any delayed stream shutdown that is pending */
  192. rtd->pop_wait = 0;
  193. mutex_unlock(&rtd->pcm_mutex);
  194. cancel_delayed_work_sync(&rtd->delayed_work);
  195. return ret;
  196. err:
  197. mutex_unlock(&rtd->pcm_mutex);
  198. return ret;
  199. }
  200. static int soc_compr_get_params(struct snd_compr_stream *cstream,
  201. struct snd_codec *params)
  202. {
  203. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  204. struct snd_soc_platform *platform = rtd->platform;
  205. int ret = 0;
  206. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  207. if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
  208. ret = platform->driver->compr_ops->get_params(cstream, params);
  209. mutex_unlock(&rtd->pcm_mutex);
  210. return ret;
  211. }
  212. static int soc_compr_get_caps(struct snd_compr_stream *cstream,
  213. struct snd_compr_caps *caps)
  214. {
  215. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  216. struct snd_soc_platform *platform = rtd->platform;
  217. int ret = 0;
  218. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  219. if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
  220. ret = platform->driver->compr_ops->get_caps(cstream, caps);
  221. mutex_unlock(&rtd->pcm_mutex);
  222. return ret;
  223. }
  224. static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
  225. struct snd_compr_codec_caps *codec)
  226. {
  227. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  228. struct snd_soc_platform *platform = rtd->platform;
  229. int ret = 0;
  230. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  231. if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
  232. ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
  233. mutex_unlock(&rtd->pcm_mutex);
  234. return ret;
  235. }
  236. static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
  237. {
  238. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  239. struct snd_soc_platform *platform = rtd->platform;
  240. int ret = 0;
  241. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  242. if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
  243. ret = platform->driver->compr_ops->ack(cstream, bytes);
  244. mutex_unlock(&rtd->pcm_mutex);
  245. return ret;
  246. }
  247. static int soc_compr_pointer(struct snd_compr_stream *cstream,
  248. struct snd_compr_tstamp *tstamp)
  249. {
  250. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  251. struct snd_soc_platform *platform = rtd->platform;
  252. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  253. if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
  254. platform->driver->compr_ops->pointer(cstream, tstamp);
  255. mutex_unlock(&rtd->pcm_mutex);
  256. return 0;
  257. }
  258. static int soc_compr_copy(struct snd_compr_stream *cstream,
  259. char __user *buf, size_t count)
  260. {
  261. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  262. struct snd_soc_platform *platform = rtd->platform;
  263. int ret = 0;
  264. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  265. if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
  266. ret = platform->driver->compr_ops->copy(cstream, buf, count);
  267. mutex_unlock(&rtd->pcm_mutex);
  268. return ret;
  269. }
  270. static int sst_compr_set_metadata(struct snd_compr_stream *cstream,
  271. struct snd_compr_metadata *metadata)
  272. {
  273. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  274. struct snd_soc_platform *platform = rtd->platform;
  275. int ret = 0;
  276. if (platform->driver->compr_ops && platform->driver->compr_ops->set_metadata)
  277. ret = platform->driver->compr_ops->set_metadata(cstream, metadata);
  278. return ret;
  279. }
  280. static int sst_compr_get_metadata(struct snd_compr_stream *cstream,
  281. struct snd_compr_metadata *metadata)
  282. {
  283. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  284. struct snd_soc_platform *platform = rtd->platform;
  285. int ret = 0;
  286. if (platform->driver->compr_ops && platform->driver->compr_ops->get_metadata)
  287. ret = platform->driver->compr_ops->get_metadata(cstream, metadata);
  288. return ret;
  289. }
  290. /* ASoC Compress operations */
  291. static struct snd_compr_ops soc_compr_ops = {
  292. .open = soc_compr_open,
  293. .free = soc_compr_free,
  294. .set_params = soc_compr_set_params,
  295. .set_metadata = sst_compr_set_metadata,
  296. .get_metadata = sst_compr_get_metadata,
  297. .get_params = soc_compr_get_params,
  298. .trigger = soc_compr_trigger,
  299. .pointer = soc_compr_pointer,
  300. .ack = soc_compr_ack,
  301. .get_caps = soc_compr_get_caps,
  302. .get_codec_caps = soc_compr_get_codec_caps
  303. };
  304. /* create a new compress */
  305. int soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
  306. {
  307. struct snd_soc_codec *codec = rtd->codec;
  308. struct snd_soc_platform *platform = rtd->platform;
  309. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  310. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  311. struct snd_compr *compr;
  312. char new_name[64];
  313. int ret = 0, direction = 0;
  314. /* check client and interface hw capabilities */
  315. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  316. rtd->dai_link->stream_name, codec_dai->name, num);
  317. if (codec_dai->driver->playback.channels_min)
  318. direction = SND_COMPRESS_PLAYBACK;
  319. else if (codec_dai->driver->capture.channels_min)
  320. direction = SND_COMPRESS_CAPTURE;
  321. else
  322. return -EINVAL;
  323. compr = kzalloc(sizeof(*compr), GFP_KERNEL);
  324. if (compr == NULL) {
  325. snd_printk(KERN_ERR "Cannot allocate compr\n");
  326. return -ENOMEM;
  327. }
  328. compr->ops = devm_kzalloc(rtd->card->dev, sizeof(soc_compr_ops),
  329. GFP_KERNEL);
  330. if (compr->ops == NULL) {
  331. dev_err(rtd->card->dev, "Cannot allocate compressed ops\n");
  332. ret = -ENOMEM;
  333. goto compr_err;
  334. }
  335. memcpy(compr->ops, &soc_compr_ops, sizeof(soc_compr_ops));
  336. /* Add copy callback for not memory mapped DSPs */
  337. if (platform->driver->compr_ops && platform->driver->compr_ops->copy)
  338. compr->ops->copy = soc_compr_copy;
  339. mutex_init(&compr->lock);
  340. ret = snd_compress_new(rtd->card->snd_card, num, direction, compr);
  341. if (ret < 0) {
  342. pr_err("compress asoc: can't create compress for codec %s\n",
  343. codec->name);
  344. goto compr_err;
  345. }
  346. /* DAPM dai link stream work */
  347. INIT_DELAYED_WORK(&rtd->delayed_work, close_delayed_work);
  348. rtd->compr = compr;
  349. compr->private_data = rtd;
  350. printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name,
  351. cpu_dai->name);
  352. return ret;
  353. compr_err:
  354. kfree(compr);
  355. return ret;
  356. }