soc-utils.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. static struct snd_soc_dai_driver dummy_dai = {
  78. .name = "snd-soc-dummy-dai",
  79. };
  80. static __devinit int snd_soc_dummy_probe(struct platform_device *pdev)
  81. {
  82. int ret;
  83. memset(&dummy_codec, 0,
  84. sizeof(struct snd_soc_codec_driver));
  85. ret = snd_soc_register_codec(&pdev->dev, &dummy_codec, &dummy_dai, 1);
  86. if (ret < 0)
  87. return ret;
  88. ret = snd_soc_register_platform(&pdev->dev, &dummy_platform);
  89. if (ret < 0) {
  90. snd_soc_unregister_codec(&pdev->dev);
  91. return ret;
  92. }
  93. return ret;
  94. }
  95. static __devexit int snd_soc_dummy_remove(struct platform_device *pdev)
  96. {
  97. snd_soc_unregister_platform(&pdev->dev);
  98. snd_soc_unregister_codec(&pdev->dev);
  99. return 0;
  100. }
  101. static struct platform_driver soc_dummy_driver = {
  102. .driver = {
  103. .name = "snd-soc-dummy",
  104. .owner = THIS_MODULE,
  105. },
  106. .probe = snd_soc_dummy_probe,
  107. .remove = __devexit_p(snd_soc_dummy_remove),
  108. };
  109. static struct platform_device *soc_dummy_dev;
  110. int __init snd_soc_util_init(void)
  111. {
  112. int ret;
  113. soc_dummy_dev = platform_device_alloc("snd-soc-dummy", -1);
  114. if (!soc_dummy_dev)
  115. return -ENOMEM;
  116. ret = platform_device_add(soc_dummy_dev);
  117. if (ret != 0) {
  118. platform_device_put(soc_dummy_dev);
  119. return ret;
  120. }
  121. ret = platform_driver_register(&soc_dummy_driver);
  122. if (ret != 0)
  123. platform_device_unregister(soc_dummy_dev);
  124. return ret;
  125. }
  126. void __exit snd_soc_util_exit(void)
  127. {
  128. platform_device_unregister(soc_dummy_dev);
  129. platform_driver_unregister(&soc_dummy_driver);
  130. }