soc-compress.c 8.2 KB

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