soc-utils.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 <[email protected]>
  8. // Liam Girdwood <[email protected]>
  9. #include <linux/platform_device.h>
  10. #include <linux/export.h>
  11. #include <linux/math.h>
  12. #include <sound/core.h>
  13. #include <sound/pcm.h>
  14. #include <sound/pcm_params.h>
  15. #include <sound/soc.h>
  16. int snd_soc_calc_frame_size(int sample_size, int channels, int tdm_slots)
  17. {
  18. return sample_size * channels * tdm_slots;
  19. }
  20. EXPORT_SYMBOL_GPL(snd_soc_calc_frame_size);
  21. int snd_soc_params_to_frame_size(struct snd_pcm_hw_params *params)
  22. {
  23. int sample_size;
  24. sample_size = snd_pcm_format_width(params_format(params));
  25. if (sample_size < 0)
  26. return sample_size;
  27. return snd_soc_calc_frame_size(sample_size, params_channels(params),
  28. 1);
  29. }
  30. EXPORT_SYMBOL_GPL(snd_soc_params_to_frame_size);
  31. int snd_soc_calc_bclk(int fs, int sample_size, int channels, int tdm_slots)
  32. {
  33. return fs * snd_soc_calc_frame_size(sample_size, channels, tdm_slots);
  34. }
  35. EXPORT_SYMBOL_GPL(snd_soc_calc_bclk);
  36. int snd_soc_params_to_bclk(struct snd_pcm_hw_params *params)
  37. {
  38. int ret;
  39. ret = snd_soc_params_to_frame_size(params);
  40. if (ret > 0)
  41. return ret * params_rate(params);
  42. else
  43. return ret;
  44. }
  45. EXPORT_SYMBOL_GPL(snd_soc_params_to_bclk);
  46. /**
  47. * snd_soc_tdm_params_to_bclk - calculate bclk from params and tdm slot info.
  48. *
  49. * Calculate the bclk from the params sample rate, the tdm slot count and the
  50. * tdm slot width. Optionally round-up the slot count to a given multiple.
  51. * Either or both of tdm_width and tdm_slots can be 0.
  52. *
  53. * If tdm_width == 0: use params_width() as the slot width.
  54. * If tdm_slots == 0: use params_channels() as the slot count.
  55. *
  56. * If slot_multiple > 1 the slot count (or params_channels() if tdm_slots == 0)
  57. * will be rounded up to a multiple of slot_multiple. This is mainly useful for
  58. * I2S mode, which has a left and right phase so the number of slots is always
  59. * a multiple of 2.
  60. *
  61. * If tdm_width == 0 && tdm_slots == 0 && slot_multiple < 2, this is equivalent
  62. * to calling snd_soc_params_to_bclk().
  63. *
  64. * @params: Pointer to struct_pcm_hw_params.
  65. * @tdm_width: Width in bits of the tdm slots. Must be >= 0.
  66. * @tdm_slots: Number of tdm slots per frame. Must be >= 0.
  67. * @slot_multiple: If >1 roundup slot count to a multiple of this value.
  68. *
  69. * Return: bclk frequency in Hz, else a negative error code if params format
  70. * is invalid.
  71. */
  72. int snd_soc_tdm_params_to_bclk(struct snd_pcm_hw_params *params,
  73. int tdm_width, int tdm_slots, int slot_multiple)
  74. {
  75. if (!tdm_slots)
  76. tdm_slots = params_channels(params);
  77. if (slot_multiple > 1)
  78. tdm_slots = roundup(tdm_slots, slot_multiple);
  79. if (!tdm_width) {
  80. tdm_width = snd_pcm_format_width(params_format(params));
  81. if (tdm_width < 0)
  82. return tdm_width;
  83. }
  84. return snd_soc_calc_bclk(params_rate(params), tdm_width, 1, tdm_slots);
  85. }
  86. EXPORT_SYMBOL_GPL(snd_soc_tdm_params_to_bclk);
  87. static const struct snd_pcm_hardware dummy_dma_hardware = {
  88. /* Random values to keep userspace happy when checking constraints */
  89. .info = SNDRV_PCM_INFO_INTERLEAVED |
  90. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  91. .buffer_bytes_max = 128*1024,
  92. .period_bytes_min = PAGE_SIZE,
  93. .period_bytes_max = PAGE_SIZE*2,
  94. .periods_min = 2,
  95. .periods_max = 128,
  96. };
  97. static const struct snd_soc_component_driver dummy_platform;
  98. static int dummy_dma_open(struct snd_soc_component *component,
  99. struct snd_pcm_substream *substream)
  100. {
  101. struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
  102. int i;
  103. /*
  104. * If there are other components associated with rtd, we shouldn't
  105. * override their hwparams
  106. */
  107. for_each_rtd_components(rtd, i, component) {
  108. if (component->driver == &dummy_platform)
  109. return 0;
  110. }
  111. /* BE's dont need dummy params */
  112. if (!rtd->dai_link->no_pcm)
  113. snd_soc_set_runtime_hwparams(substream, &dummy_dma_hardware);
  114. return 0;
  115. }
  116. static const struct snd_soc_component_driver dummy_platform = {
  117. .open = dummy_dma_open,
  118. };
  119. static const struct snd_soc_component_driver dummy_codec = {
  120. .idle_bias_on = 1,
  121. .use_pmdown_time = 1,
  122. .endianness = 1,
  123. };
  124. #define STUB_RATES SNDRV_PCM_RATE_8000_384000
  125. #define STUB_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
  126. SNDRV_PCM_FMTBIT_U8 | \
  127. SNDRV_PCM_FMTBIT_S16_LE | \
  128. SNDRV_PCM_FMTBIT_U16_LE | \
  129. SNDRV_PCM_FMTBIT_S24_LE | \
  130. SNDRV_PCM_FMTBIT_S24_3LE | \
  131. SNDRV_PCM_FMTBIT_U24_LE | \
  132. SNDRV_PCM_FMTBIT_S32_LE | \
  133. SNDRV_PCM_FMTBIT_U32_LE | \
  134. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
  135. /*
  136. * Select these from Sound Card Manually
  137. * SND_SOC_POSSIBLE_DAIFMT_CBP_CFP
  138. * SND_SOC_POSSIBLE_DAIFMT_CBP_CFC
  139. * SND_SOC_POSSIBLE_DAIFMT_CBC_CFP
  140. * SND_SOC_POSSIBLE_DAIFMT_CBC_CFC
  141. */
  142. static u64 dummy_dai_formats =
  143. SND_SOC_POSSIBLE_DAIFMT_I2S |
  144. SND_SOC_POSSIBLE_DAIFMT_RIGHT_J |
  145. SND_SOC_POSSIBLE_DAIFMT_LEFT_J |
  146. SND_SOC_POSSIBLE_DAIFMT_DSP_A |
  147. SND_SOC_POSSIBLE_DAIFMT_DSP_B |
  148. SND_SOC_POSSIBLE_DAIFMT_AC97 |
  149. SND_SOC_POSSIBLE_DAIFMT_PDM |
  150. SND_SOC_POSSIBLE_DAIFMT_GATED |
  151. SND_SOC_POSSIBLE_DAIFMT_CONT |
  152. SND_SOC_POSSIBLE_DAIFMT_NB_NF |
  153. SND_SOC_POSSIBLE_DAIFMT_NB_IF |
  154. SND_SOC_POSSIBLE_DAIFMT_IB_NF |
  155. SND_SOC_POSSIBLE_DAIFMT_IB_IF;
  156. static const struct snd_soc_dai_ops dummy_dai_ops = {
  157. .auto_selectable_formats = &dummy_dai_formats,
  158. .num_auto_selectable_formats = 1,
  159. };
  160. /*
  161. * The dummy CODEC is only meant to be used in situations where there is no
  162. * actual hardware.
  163. *
  164. * If there is actual hardware even if it does not have a control bus
  165. * the hardware will still have constraints like supported samplerates, etc.
  166. * which should be modelled. And the data flow graph also should be modelled
  167. * using DAPM.
  168. */
  169. static struct snd_soc_dai_driver dummy_dai = {
  170. .name = "snd-soc-dummy-dai",
  171. .playback = {
  172. .stream_name = "Playback",
  173. .channels_min = 1,
  174. .channels_max = 384,
  175. .rates = STUB_RATES,
  176. .formats = STUB_FORMATS,
  177. },
  178. .capture = {
  179. .stream_name = "Capture",
  180. .channels_min = 1,
  181. .channels_max = 384,
  182. .rates = STUB_RATES,
  183. .formats = STUB_FORMATS,
  184. },
  185. .ops = &dummy_dai_ops,
  186. };
  187. int snd_soc_dai_is_dummy(struct snd_soc_dai *dai)
  188. {
  189. if (dai->driver == &dummy_dai)
  190. return 1;
  191. return 0;
  192. }
  193. EXPORT_SYMBOL_GPL(snd_soc_dai_is_dummy);
  194. int snd_soc_component_is_dummy(struct snd_soc_component *component)
  195. {
  196. return ((component->driver == &dummy_platform) ||
  197. (component->driver == &dummy_codec));
  198. }
  199. static int snd_soc_dummy_probe(struct platform_device *pdev)
  200. {
  201. int ret;
  202. ret = devm_snd_soc_register_component(&pdev->dev,
  203. &dummy_codec, &dummy_dai, 1);
  204. if (ret < 0)
  205. return ret;
  206. ret = devm_snd_soc_register_component(&pdev->dev, &dummy_platform,
  207. NULL, 0);
  208. return ret;
  209. }
  210. static struct platform_driver soc_dummy_driver = {
  211. .driver = {
  212. .name = "snd-soc-dummy",
  213. },
  214. .probe = snd_soc_dummy_probe,
  215. };
  216. static struct platform_device *soc_dummy_dev;
  217. int __init snd_soc_util_init(void)
  218. {
  219. int ret;
  220. soc_dummy_dev =
  221. platform_device_register_simple("snd-soc-dummy", -1, NULL, 0);
  222. if (IS_ERR(soc_dummy_dev))
  223. return PTR_ERR(soc_dummy_dev);
  224. ret = platform_driver_register(&soc_dummy_driver);
  225. if (ret != 0)
  226. platform_device_unregister(soc_dummy_dev);
  227. return ret;
  228. }
  229. void snd_soc_util_exit(void)
  230. {
  231. platform_driver_unregister(&soc_dummy_driver);
  232. platform_device_unregister(soc_dummy_dev);
  233. }