soc-compress.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. rtd->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. int ret = 0;
  132. /* first we call set_params for the platform driver
  133. * this should configure the soc side
  134. * if the machine has compressed ops then we call that as well
  135. * expectation is that platform and machine will configure everything
  136. * for this compress path, like configuring pcm port for codec
  137. */
  138. if (platform->driver->compr_ops && platform->driver->compr_ops->set_params) {
  139. ret = platform->driver->compr_ops->set_params(cstream, params);
  140. if (ret < 0)
  141. return ret;
  142. }
  143. if (rtd->dai_link->compr_ops && rtd->dai_link->compr_ops->set_params) {
  144. ret = rtd->dai_link->compr_ops->set_params(cstream);
  145. if (ret < 0)
  146. return ret;
  147. }
  148. snd_soc_dapm_stream_event(rtd, SNDRV_PCM_STREAM_PLAYBACK,
  149. SND_SOC_DAPM_STREAM_START);
  150. return ret;
  151. }
  152. static int soc_compr_get_params(struct snd_compr_stream *cstream,
  153. struct snd_codec *params)
  154. {
  155. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  156. struct snd_soc_platform *platform = rtd->platform;
  157. int ret = 0;
  158. if (platform->driver->compr_ops && platform->driver->compr_ops->get_params)
  159. ret = platform->driver->compr_ops->get_params(cstream, params);
  160. return ret;
  161. }
  162. static int soc_compr_get_caps(struct snd_compr_stream *cstream,
  163. struct snd_compr_caps *caps)
  164. {
  165. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  166. struct snd_soc_platform *platform = rtd->platform;
  167. int ret = 0;
  168. if (platform->driver->compr_ops && platform->driver->compr_ops->get_caps)
  169. ret = platform->driver->compr_ops->get_caps(cstream, caps);
  170. return ret;
  171. }
  172. static int soc_compr_get_codec_caps(struct snd_compr_stream *cstream,
  173. struct snd_compr_codec_caps *codec)
  174. {
  175. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  176. struct snd_soc_platform *platform = rtd->platform;
  177. int ret = 0;
  178. if (platform->driver->compr_ops && platform->driver->compr_ops->get_codec_caps)
  179. ret = platform->driver->compr_ops->get_codec_caps(cstream, codec);
  180. return ret;
  181. }
  182. static int soc_compr_ack(struct snd_compr_stream *cstream, size_t bytes)
  183. {
  184. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  185. struct snd_soc_platform *platform = rtd->platform;
  186. int ret = 0;
  187. if (platform->driver->compr_ops && platform->driver->compr_ops->ack)
  188. ret = platform->driver->compr_ops->ack(cstream, bytes);
  189. return ret;
  190. }
  191. static int soc_compr_pointer(struct snd_compr_stream *cstream,
  192. struct snd_compr_tstamp *tstamp)
  193. {
  194. struct snd_soc_pcm_runtime *rtd = cstream->private_data;
  195. struct snd_soc_platform *platform = rtd->platform;
  196. if (platform->driver->compr_ops && platform->driver->compr_ops->pointer)
  197. platform->driver->compr_ops->pointer(cstream, tstamp);
  198. return 0;
  199. }
  200. /* ASoC Compress operations */
  201. static struct snd_compr_ops soc_compr_ops = {
  202. .open = soc_compr_open,
  203. .free = soc_compr_free,
  204. .set_params = soc_compr_set_params,
  205. .get_params = soc_compr_get_params,
  206. .trigger = soc_compr_trigger,
  207. .pointer = soc_compr_pointer,
  208. .ack = soc_compr_ack,
  209. .get_caps = soc_compr_get_caps,
  210. .get_codec_caps = soc_compr_get_codec_caps
  211. };
  212. /* create a new compress */
  213. int soc_new_compress(struct snd_soc_pcm_runtime *rtd, int num)
  214. {
  215. struct snd_soc_codec *codec = rtd->codec;
  216. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  217. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  218. struct snd_compr *compr;
  219. char new_name[64];
  220. int ret = 0, direction = 0;
  221. /* check client and interface hw capabilities */
  222. snprintf(new_name, sizeof(new_name), "%s %s-%d",
  223. rtd->dai_link->stream_name, codec_dai->name, num);
  224. direction = SND_COMPRESS_PLAYBACK;
  225. compr = kzalloc(sizeof(*compr), GFP_KERNEL);
  226. if (compr == NULL) {
  227. snd_printk(KERN_ERR "Cannot allocate compr\n");
  228. return -ENOMEM;
  229. }
  230. compr->ops = &soc_compr_ops;
  231. mutex_init(&compr->lock);
  232. ret = snd_compress_new(rtd->card->snd_card, num, direction, compr);
  233. if (ret < 0) {
  234. pr_err("compress asoc: can't create compress for codec %s\n",
  235. codec->name);
  236. kfree(compr);
  237. return ret;
  238. }
  239. rtd->compr = compr;
  240. compr->private_data = rtd;
  241. printk(KERN_INFO "compress asoc: %s <-> %s mapping ok\n", codec_dai->name,
  242. cpu_dai->name);
  243. return ret;
  244. }