soc-utils.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. .formats = 0xffffffff,
  53. .channels_min = 1,
  54. .channels_max = UINT_MAX,
  55. /* Random values to keep userspace happy when checking constraints */
  56. .info = SNDRV_PCM_INFO_INTERLEAVED |
  57. SNDRV_PCM_INFO_BLOCK_TRANSFER,
  58. .buffer_bytes_max = 128*1024,
  59. .period_bytes_min = PAGE_SIZE,
  60. .period_bytes_max = PAGE_SIZE*2,
  61. .periods_min = 2,
  62. .periods_max = 128,
  63. };
  64. static int dummy_dma_open(struct snd_pcm_substream *substream)
  65. {
  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. static struct snd_soc_dai_driver dummy_dai = {
  88. .name = "snd-soc-dummy-dai",
  89. .playback = {
  90. .stream_name = "Playback",
  91. .channels_min = 1,
  92. .channels_max = 384,
  93. .rates = STUB_RATES,
  94. .formats = STUB_FORMATS,
  95. },
  96. .capture = {
  97. .stream_name = "Capture",
  98. .channels_min = 1,
  99. .channels_max = 384,
  100. .rates = STUB_RATES,
  101. .formats = STUB_FORMATS,
  102. },
  103. };
  104. static int snd_soc_dummy_probe(struct platform_device *pdev)
  105. {
  106. int ret;
  107. ret = snd_soc_register_codec(&pdev->dev, &dummy_codec, &dummy_dai, 1);
  108. if (ret < 0)
  109. return ret;
  110. ret = snd_soc_register_platform(&pdev->dev, &dummy_platform);
  111. if (ret < 0) {
  112. snd_soc_unregister_codec(&pdev->dev);
  113. return ret;
  114. }
  115. return ret;
  116. }
  117. static int snd_soc_dummy_remove(struct platform_device *pdev)
  118. {
  119. snd_soc_unregister_platform(&pdev->dev);
  120. snd_soc_unregister_codec(&pdev->dev);
  121. return 0;
  122. }
  123. static struct platform_driver soc_dummy_driver = {
  124. .driver = {
  125. .name = "snd-soc-dummy",
  126. .owner = THIS_MODULE,
  127. },
  128. .probe = snd_soc_dummy_probe,
  129. .remove = snd_soc_dummy_remove,
  130. };
  131. static struct platform_device *soc_dummy_dev;
  132. int __init snd_soc_util_init(void)
  133. {
  134. int ret;
  135. soc_dummy_dev =
  136. platform_device_register_simple("snd-soc-dummy", -1, NULL, 0);
  137. if (IS_ERR(soc_dummy_dev))
  138. return PTR_ERR(soc_dummy_dev);
  139. ret = platform_driver_register(&soc_dummy_driver);
  140. if (ret != 0)
  141. platform_device_unregister(soc_dummy_dev);
  142. return ret;
  143. }
  144. void __exit snd_soc_util_exit(void)
  145. {
  146. platform_device_unregister(soc_dummy_dev);
  147. platform_driver_unregister(&soc_dummy_driver);
  148. }