soc-utils.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 <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(const 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(const 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(const 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 = snd_soc_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_FORMATS (SNDRV_PCM_FMTBIT_S8 | \
  125. SNDRV_PCM_FMTBIT_U8 | \
  126. SNDRV_PCM_FMTBIT_S16_LE | \
  127. SNDRV_PCM_FMTBIT_U16_LE | \
  128. SNDRV_PCM_FMTBIT_S24_LE | \
  129. SNDRV_PCM_FMTBIT_S24_3LE | \
  130. SNDRV_PCM_FMTBIT_U24_LE | \
  131. SNDRV_PCM_FMTBIT_S32_LE | \
  132. SNDRV_PCM_FMTBIT_U32_LE | \
  133. SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_LE)
  134. /*
  135. * Select these from Sound Card Manually
  136. * SND_SOC_POSSIBLE_DAIFMT_CBP_CFP
  137. * SND_SOC_POSSIBLE_DAIFMT_CBP_CFC
  138. * SND_SOC_POSSIBLE_DAIFMT_CBC_CFP
  139. * SND_SOC_POSSIBLE_DAIFMT_CBC_CFC
  140. */
  141. static const u64 dummy_dai_formats =
  142. SND_SOC_POSSIBLE_DAIFMT_I2S |
  143. SND_SOC_POSSIBLE_DAIFMT_RIGHT_J |
  144. SND_SOC_POSSIBLE_DAIFMT_LEFT_J |
  145. SND_SOC_POSSIBLE_DAIFMT_DSP_A |
  146. SND_SOC_POSSIBLE_DAIFMT_DSP_B |
  147. SND_SOC_POSSIBLE_DAIFMT_AC97 |
  148. SND_SOC_POSSIBLE_DAIFMT_PDM |
  149. SND_SOC_POSSIBLE_DAIFMT_GATED |
  150. SND_SOC_POSSIBLE_DAIFMT_CONT |
  151. SND_SOC_POSSIBLE_DAIFMT_NB_NF |
  152. SND_SOC_POSSIBLE_DAIFMT_NB_IF |
  153. SND_SOC_POSSIBLE_DAIFMT_IB_NF |
  154. SND_SOC_POSSIBLE_DAIFMT_IB_IF;
  155. static const struct snd_soc_dai_ops dummy_dai_ops = {
  156. .auto_selectable_formats = &dummy_dai_formats,
  157. .num_auto_selectable_formats = 1,
  158. };
  159. /*
  160. * The dummy CODEC is only meant to be used in situations where there is no
  161. * actual hardware.
  162. *
  163. * If there is actual hardware even if it does not have a control bus
  164. * the hardware will still have constraints like supported samplerates, etc.
  165. * which should be modelled. And the data flow graph also should be modelled
  166. * using DAPM.
  167. */
  168. static struct snd_soc_dai_driver dummy_dai = {
  169. .name = "snd-soc-dummy-dai",
  170. .playback = {
  171. .stream_name = "Playback",
  172. .channels_min = 1,
  173. .channels_max = 384,
  174. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  175. .rate_min = 5512,
  176. .rate_max = 768000,
  177. .formats = STUB_FORMATS,
  178. },
  179. .capture = {
  180. .stream_name = "Capture",
  181. .channels_min = 1,
  182. .channels_max = 384,
  183. .rates = SNDRV_PCM_RATE_CONTINUOUS,
  184. .rate_min = 5512,
  185. .rate_max = 768000,
  186. .formats = STUB_FORMATS,
  187. },
  188. .ops = &dummy_dai_ops,
  189. };
  190. int snd_soc_dai_is_dummy(const struct snd_soc_dai *dai)
  191. {
  192. if (dai->driver == &dummy_dai)
  193. return 1;
  194. return 0;
  195. }
  196. EXPORT_SYMBOL_GPL(snd_soc_dai_is_dummy);
  197. int snd_soc_component_is_dummy(struct snd_soc_component *component)
  198. {
  199. return ((component->driver == &dummy_platform) ||
  200. (component->driver == &dummy_codec));
  201. }
  202. struct snd_soc_dai_link_component snd_soc_dummy_dlc = {
  203. .of_node = NULL,
  204. .dai_name = "snd-soc-dummy-dai",
  205. .name = "snd-soc-dummy",
  206. };
  207. EXPORT_SYMBOL_GPL(snd_soc_dummy_dlc);
  208. static int snd_soc_dummy_probe(struct platform_device *pdev)
  209. {
  210. int ret;
  211. ret = devm_snd_soc_register_component(&pdev->dev,
  212. &dummy_codec, &dummy_dai, 1);
  213. if (ret < 0)
  214. return ret;
  215. ret = devm_snd_soc_register_component(&pdev->dev, &dummy_platform,
  216. NULL, 0);
  217. return ret;
  218. }
  219. static struct platform_driver soc_dummy_driver = {
  220. .driver = {
  221. .name = "snd-soc-dummy",
  222. },
  223. .probe = snd_soc_dummy_probe,
  224. };
  225. static struct platform_device *soc_dummy_dev;
  226. int __init snd_soc_util_init(void)
  227. {
  228. int ret;
  229. soc_dummy_dev =
  230. platform_device_register_simple("snd-soc-dummy", -1, NULL, 0);
  231. if (IS_ERR(soc_dummy_dev))
  232. return PTR_ERR(soc_dummy_dev);
  233. ret = platform_driver_register(&soc_dummy_driver);
  234. if (ret != 0)
  235. platform_device_unregister(soc_dummy_dev);
  236. return ret;
  237. }
  238. void snd_soc_util_exit(void)
  239. {
  240. platform_driver_unregister(&soc_dummy_driver);
  241. platform_device_unregister(soc_dummy_dev);
  242. }