edb93xx.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * SoC audio for EDB93xx
  3. *
  4. * Copyright (c) 2010 Alexander Sverdlin <subaparts@yandex.ru>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * This driver support CS4271 codec being master or slave, working
  17. * in control port mode, connected either via SPI or I2C.
  18. * The data format accepted is I2S or left-justified.
  19. * DAPM support not implemented.
  20. */
  21. #include <linux/platform_device.h>
  22. #include <linux/gpio.h>
  23. #include <sound/core.h>
  24. #include <sound/pcm.h>
  25. #include <sound/soc.h>
  26. #include <asm/mach-types.h>
  27. #include <mach/hardware.h>
  28. #include "ep93xx-pcm.h"
  29. #define edb93xx_has_audio() (machine_is_edb9301() || \
  30. machine_is_edb9302() || \
  31. machine_is_edb9302a() || \
  32. machine_is_edb9307a() || \
  33. machine_is_edb9315a())
  34. static int edb93xx_hw_params(struct snd_pcm_substream *substream,
  35. struct snd_pcm_hw_params *params)
  36. {
  37. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  38. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  39. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  40. int err;
  41. unsigned int mclk_rate;
  42. unsigned int rate = params_rate(params);
  43. /*
  44. * According to CS4271 datasheet we use MCLK/LRCK=256 for
  45. * rates below 50kHz and 128 for higher sample rates
  46. */
  47. if (rate < 50000)
  48. mclk_rate = rate * 64 * 4;
  49. else
  50. mclk_rate = rate * 64 * 2;
  51. err = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  52. SND_SOC_DAIFMT_NB_IF |
  53. SND_SOC_DAIFMT_CBS_CFS);
  54. if (err)
  55. return err;
  56. err = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  57. SND_SOC_DAIFMT_NB_IF |
  58. SND_SOC_DAIFMT_CBS_CFS);
  59. if (err)
  60. return err;
  61. err = snd_soc_dai_set_sysclk(codec_dai, 0, mclk_rate,
  62. SND_SOC_CLOCK_IN);
  63. if (err)
  64. return err;
  65. return snd_soc_dai_set_sysclk(cpu_dai, 0, mclk_rate,
  66. SND_SOC_CLOCK_OUT);
  67. }
  68. static struct snd_soc_ops edb93xx_ops = {
  69. .hw_params = edb93xx_hw_params,
  70. };
  71. static struct snd_soc_dai_link edb93xx_dai = {
  72. .name = "CS4271",
  73. .stream_name = "CS4271 HiFi",
  74. .platform_name = "ep93xx-pcm-audio",
  75. .cpu_dai_name = "ep93xx-i2s",
  76. .codec_name = "spi0.0",
  77. .codec_dai_name = "cs4271-hifi",
  78. .ops = &edb93xx_ops,
  79. };
  80. static struct snd_soc_card snd_soc_edb93xx = {
  81. .name = "EDB93XX",
  82. .dai_link = &edb93xx_dai,
  83. .num_links = 1,
  84. };
  85. static struct platform_device *edb93xx_snd_device;
  86. static int __init edb93xx_init(void)
  87. {
  88. int ret;
  89. if (!edb93xx_has_audio())
  90. return -ENODEV;
  91. ret = ep93xx_i2s_acquire(EP93XX_SYSCON_DEVCFG_I2SONAC97,
  92. EP93XX_SYSCON_I2SCLKDIV_ORIDE |
  93. EP93XX_SYSCON_I2SCLKDIV_SPOL);
  94. if (ret)
  95. return ret;
  96. edb93xx_snd_device = platform_device_alloc("soc-audio", -1);
  97. if (!edb93xx_snd_device) {
  98. ret = -ENOMEM;
  99. goto free_i2s;
  100. }
  101. platform_set_drvdata(edb93xx_snd_device, &snd_soc_edb93xx);
  102. ret = platform_device_add(edb93xx_snd_device);
  103. if (ret)
  104. goto device_put;
  105. return 0;
  106. device_put:
  107. platform_device_put(edb93xx_snd_device);
  108. free_i2s:
  109. ep93xx_i2s_release();
  110. return ret;
  111. }
  112. module_init(edb93xx_init);
  113. static void __exit edb93xx_exit(void)
  114. {
  115. platform_device_unregister(edb93xx_snd_device);
  116. ep93xx_i2s_release();
  117. }
  118. module_exit(edb93xx_exit);
  119. MODULE_AUTHOR("Alexander Sverdlin <subaparts@yandex.ru>");
  120. MODULE_DESCRIPTION("ALSA SoC EDB93xx");
  121. MODULE_LICENSE("GPL");