soc-utils.c 4.3 KB

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