soc-compress.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. static int soc_compr_free(struct snd_compr_stream *cstream)
  68. {
  69. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  70. struct snd_soc_platform *platform = rtd->platform;
  71. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  72. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  73. struct snd_soc_codec *codec = rtd->codec;
  74. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  75. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  76. cpu_dai->playback_active--;
  77. codec_dai->playback_active--;
  78. } else {
  79. cpu_dai->capture_active--;
  80. codec_dai->capture_active--;
  81. }
  82. snd_soc_dai_digital_mute(codec_dai, 1);
  83. cpu_dai->active--;
  84. codec_dai->active--;
  85. codec->active--;
  86. if (!cpu_dai->active)
  87. cpu_dai->rate = 0;
  88. if (!codec_dai->active)
  89. codec_dai->rate = 0;
  90. if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->shutdown)
  91. rtd->dai_link->compr_ops->shutdown(cstream);
  92. if (platform->driver->compr_ops && platform->driver->compr_ops->free)
  93. platform->driver->compr_ops->free(cstream);
  94. cpu_dai->runtime = NULL;
  95. if (cstream->direction == SND_COMPRESS_PLAYBACK) {
  96. if (!rtd->pmdown_time || codec->ignore_pmdown_time ||
  97. rtd->dai_link->ignore_pmdown_time) {
  98. snd_soc_dapm_stream_event(rtd,
  99. SNDRV_PCM_STREAM_PLAYBACK,
  100. SND_SOC_DAPM_STREAM_STOP);
  101. } else {
  102. rtd->pop_wait = 1;
  103. schedule_delayed_work(&rtd->delayed_work,
  104. msecs_to_jiffies(rtd->pmdown_time));
  105. }
  106. } else {
  107. /* capture streams can be powered down now */
  108. snd_soc_dapm_stream_event(rtd,
  109. SNDRV_PCM_STREAM_CAPTURE,
  110. SND_SOC_DAPM_STREAM_STOP);
  111. }
  112. mutex_unlock(&rtd->pcm_mutex);
  113. return 0;
  114. }
  115. static int soc_compr_trigger(struct snd_compr_stream *cstream, int cmd)
  116. {
  117. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  118. struct snd_soc_platform *platform = rtd->platform;
  119. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  120. int ret = 0;
  121. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  122. if (platform->driver->compr_ops && platform->driver->compr_ops->trigger) {
  123. ret = platform->driver->compr_ops->trigger(cstream, cmd);
  124. if (ret < 0)
  125. goto out;
  126. }
  127. if (cmd == SNDRV_PCM_TRIGGER_START)
  128. snd_soc_dai_digital_mute(codec_dai, 0);
  129. else if (cmd == SNDRV_PCM_TRIGGER_STOP)
  130. snd_soc_dai_digital_mute(codec_dai, 1);
  131. out:
  132. mutex_unlock(&rtd->pcm_mutex);
  133. return ret;
  134. }
  135. static int soc_compr_set_params(struct snd_compr_stream *cstream,
  136. struct snd_compr_params *params)
  137. {
  138. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  139. struct snd_soc_platform *platform = rtd->platform;
  140. int ret = 0;
  141. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  142. /* first we call set_params for the platform driver
  143. * this should configure the soc side
  144. * if the machine has compressed ops then we call that as well
  145. * expectation is that platform and machine will configure everything
  146. * for this compress path, like configuring pcm port for codec
  147. */
  148. if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
  149. ret = platform->driver->compr_ops->set_params(cstream, params);
  150. if (ret < 0)
  151. goto out;
  152. }
  153. if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
  154. ret = rtd->dai_link->compr_ops->set_params(cstream);
  155. if (ret < 0)
  156. goto out;
  157. }
  158. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
  159. SND_SOC_DAPM_STREAM_START);
  160. out:
  161. mutex_unlock(&rtd->pcm_mutex);
  162. return ret;
  163. }
  164. static int soc_compr_get_params(struct snd_compr_stream *cstream,
  165. struct snd_codec *params)
  166. {
  167. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  168. struct snd_soc_platform *platform = rtd->platform;
  169. int ret = 0;
  170. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  171. if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
  172. ret = platform->driver->compr_ops->get_params(cstream, params);
  173. mutex_unlock(&rtd->pcm_mutex);
  174. return ret;
  175. }
  176. static int soc_compr_get_caps(struct snd_compr_stream *cstream,
  177. struct snd_compr_caps *caps)
  178. {
  179. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  180. struct snd_soc_platform *platform = rtd->platform;
  181. int ret = 0;
  182. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  183. if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
  184. ret = platform->driver->compr_ops->get_caps(cstream, caps);
  185. mutex_unlock(&rtd->pcm_mutex);
  186. return ret;
  187. }
  188. static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
  189. struct snd_compr_codec_caps *codec)
  190. {
  191. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  192. struct snd_soc_platform *platform = rtd->platform;
  193. int ret = 0;
  194. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  195. if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
  196. ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
  197. mutex_unlock(&rtd->pcm_mutex);
  198. return ret;
  199. }
  200. static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
  201. {
  202. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  203. struct snd_soc_platform *platform = rtd->platform;
  204. int ret = 0;
  205. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  206. if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
  207. ret = platform->driver->compr_ops->ack(cstream, bytes);
  208. mutex_unlock(&rtd->pcm_mutex);
  209. return ret;
  210. }
  211. static int soc_compr_pointer(struct snd_compr_stream *cstream,
  212. struct snd_compr_tstamp *tstamp)
  213. {
  214. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  215. struct snd_soc_platform *platform = rtd->platform;
  216. mutex_lock_nested(&rtd->pcm_mutex, rtd->pcm_subclass);
  217. if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
  218. platform->driver->compr_ops->pointer(cstream, tstamp);
  219. mutex_unlock(&rtd->pcm_mutex);
  220. return 0;
  221. }
  222. /* ASoC Compress operations */
  223. static struct snd_compr_ops soc_compr_ops = {
  224. .open = soc_compr_open,
  225. .free = soc_compr_free,
  226. .set_params = soc_compr_set_params,
  227. .get_params = soc_compr_get_params,
  228. .trigger = soc_compr_trigger,
  229. .pointer = soc_compr_pointer,
  230. .ack = soc_compr_ack,
  231. .get_caps = soc_compr_get_caps,
  232. .get_codec_caps = soc_compr_get_codec_caps
  233. };
  234. /* create a new compress */
  235. int soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
  236. {
  237. struct snd_soc_codec *codec = rtd->codec;
  238. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  239. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  240. struct snd_compr *compr;
  241. char new_name[64];
  242. int ret = 0, direction = 0;
  243. /* check client and interface hw capabilities */
  244. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  245. rtd->dai_link->stream_name, codec_dai->name, num);
  246. direction = SND_COMPRESS_PLAYBACK;
  247. compr = kzalloc(sizeof(*compr), GFP_KERNEL);
  248. if (compr == NULL) {
  249. snd_printk(KERN_ERR "Cannot allocate compr\n");
  250. return -ENOMEM;
  251. }
  252. compr->ops = &soc_compr_ops;
  253. mutex_init(&compr->lock);
  254. ret = snd_compress_new(rtd->card->snd_card, num, direction, compr);
  255. if (ret < 0) {
  256. pr_err("compress asoc: can't create compress for codec %s\n",
  257. codec->name);
  258. kfree(compr);
  259. return ret;
  260. }
  261. rtd->compr = compr;
  262. compr->private_data = rtd;
  263. printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name,
  264. cpu_dai->name);
  265. return ret;
  266. }