soc-utils.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * soc-util.c -- ALSA SoC Audio Layer utility functions
  3. *
  4. * Copyright 2009 Wolfson Microelectronics PLC.
  5. *
  6. * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
  7. * Liam Girdwood <lrg@slimlogic.co.uk>
  8. *
  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. #include <linux/platform_device.h>
  16. #include <linux/export.h>
  17. #include <sound/core.h>
  18. #include <sound/pcm.h>
  19. #include <sound/pcm_params.h>
  20. #include <sound/soc.h>
  21. int snd_soc_calc_frame_size(int sample_size, int channels, int tdm_slots)
  22. {
  23. return sample_size * channels * tdm_slots;
  24. }
  25. EXPORT_SYMBOL_GPL(snd_soc_calc_frame_size);
  26. int snd_soc_params_to_frame_size(struct snd_pcm_hw_params *params)
  27. {
  28. int sample_size;
  29. sample_size = snd_pcm_format_width(params_format(params));
  30. if (sample_size < 0)
  31. return sample_size;
  32. return snd_soc_calc_frame_size(sample_size, params_channels(params),
  33. 1);
  34. }
  35. EXPORT_SYMBOL_GPL(snd_soc_params_to_frame_size);
  36. int snd_soc_calc_bclk(int fs, int sample_size, int channels, int tdm_slots)
  37. {
  38. return fs * snd_soc_calc_frame_size(sample_size, channels, tdm_slots);
  39. }
  40. EXPORT_SYMBOL_GPL(snd_soc_calc_bclk);
  41. int snd_soc_params_to_bclk(struct snd_pcm_hw_params *params)
  42. {
  43. int ret;
  44. ret = snd_soc_params_to_frame_size(params);
  45. if (ret > 0)
  46. return ret * params_rate(params);
  47. else
  48. return ret;
  49. }
  50. EXPORT_SYMBOL_GPL(snd_soc_params_to_bclk);
  51. static const struct snd_pcm_hardware dummy_dma_hardware = {
  52. /* Random values to keep userspace happy when checking constraints */
  53. .info = SNDRV_PCM_INFO_INTERLEAVED |
  54. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  55. .buffer_bytes_max = 128*1024,
  56. .period_bytes_min = PAGE_SIZE,
  57. .period_bytes_max = PAGE_SIZE*2,
  58. .periods_min = 2,
  59. .periods_max = 128,
  60. };
  61. static int dummy_dma_open(struct snd_pcm_substream *substream)
  62. {
  63. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  64. /* BE's dont need dummy params */
  65. if (!rtd->dai_link->no_pcm)
  66. snd_soc_set_runtime_hwparams(substream, &dummy_dma_hardware);
  67. return 0;
  68. }
  69. static struct snd_pcm_ops dummy_dma_ops = {
  70. .open = dummy_dma_open,
  71. .ioctl = snd_pcm_lib_ioctl,
  72. };
  73. static struct snd_soc_platform_driver dummy_platform = {
  74. .ops = &dummy_dma_ops,
  75. };
  76. static struct snd_soc_codec_driver dummy_codec;
  77. #define STUB_RATES SNDRV_PCM_RATE_8000_192000
  78. #define STUB_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
  79. SNDRV_PCM_FMTBIT_U8 | \
  80. SNDRV_PCM_FMTBIT_S16_LE | \
  81. SNDRV_PCM_FMTBIT_U16_LE | \
  82. SNDRV_PCM_FMTBIT_S24_LE | \
  83. SNDRV_PCM_FMTBIT_U24_LE | \
  84. SNDRV_PCM_FMTBIT_S32_LE | \
  85. SNDRV_PCM_FMTBIT_U32_LE | \
  86. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
  87. /*
  88. * The dummy CODEC is only meant to be used in situations where there is no
  89. * actual hardware.
  90. *
  91. * If there is actual hardware even if it does not have a control bus
  92. * the hardware will still have constraints like supported samplerates, etc.
  93. * which should be modelled. And the data flow graph also should be modelled
  94. * using DAPM.
  95. */
  96. static struct snd_soc_dai_driver dummy_dai = {
  97. .name = "snd-soc-dummy-dai",
  98. .playback = {
  99. .stream_name = "Playback",
  100. .channels_min = 1,
  101. .channels_max = 384,
  102. .rates = STUB_RATES,
  103. .formats = STUB_FORMATS,
  104. },
  105. .capture = {
  106. .stream_name = "Capture",
  107. .channels_min = 1,
  108. .channels_max = 384,
  109. .rates = STUB_RATES,
  110. .formats = STUB_FORMATS,
  111. },
  112. };
  113. int snd_soc_dai_is_dummy(struct snd_soc_dai *dai)
  114. {
  115. if (dai->driver == &dummy_dai)
  116. return 1;
  117. return 0;
  118. }
  119. static int snd_soc_dummy_probe(struct platform_device *pdev)
  120. {
  121. int ret;
  122. ret = snd_soc_register_codec(&pdev->dev, &dummy_codec, &dummy_dai, 1);
  123. if (ret < 0)
  124. return ret;
  125. ret = snd_soc_register_platform(&pdev->dev, &dummy_platform);
  126. if (ret < 0) {
  127. snd_soc_unregister_codec(&pdev->dev);
  128. return ret;
  129. }
  130. return ret;
  131. }
  132. static int snd_soc_dummy_remove(struct platform_device *pdev)
  133. {
  134. snd_soc_unregister_platform(&pdev->dev);
  135. snd_soc_unregister_codec(&pdev->dev);
  136. return 0;
  137. }
  138. static struct platform_driver soc_dummy_driver = {
  139. .driver = {
  140. .name = "snd-soc-dummy",
  141. },
  142. .probe = snd_soc_dummy_probe,
  143. .remove = snd_soc_dummy_remove,
  144. };
  145. static struct platform_device *soc_dummy_dev;
  146. int __init snd_soc_util_init(void)
  147. {
  148. int ret;
  149. soc_dummy_dev =
  150. platform_device_register_simple("snd-soc-dummy", -1, NULL, 0);
  151. if (IS_ERR(soc_dummy_dev))
  152. return PTR_ERR(soc_dummy_dev);
  153. ret = platform_driver_register(&soc_dummy_driver);
  154. if (ret != 0)
  155. platform_device_unregister(soc_dummy_dev);
  156. return ret;
  157. }
  158. void __exit snd_soc_util_exit(void)
  159. {
  160. platform_device_unregister(soc_dummy_dev);
  161. platform_driver_unregister(&soc_dummy_driver);
  162. }