bf5xx-ad73311.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * File: sound/soc/blackfin/bf5xx-ad73311.c
  3. * Author: Cliff Cai <Cliff.Cai@analog.com>
  4. *
  5. * Created: Thur Sep 25 2008
  6. * Description: Board driver for ad73311 sound chip
  7. *
  8. * Modified:
  9. * Copyright 2008 Analog Devices Inc.
  10. *
  11. * Bugs: Enter bugs at http://blackfin.uclinux.org/
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, see the file COPYING, or write
  25. * to the Free Software Foundation, Inc.,
  26. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  27. */
  28. #include <linux/module.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/device.h>
  31. #include <linux/delay.h>
  32. #include <linux/gpio.h>
  33. #include <sound/core.h>
  34. #include <sound/pcm.h>
  35. #include <sound/soc.h>
  36. #include <sound/soc-dapm.h>
  37. #include <sound/pcm_params.h>
  38. #include <asm/blackfin.h>
  39. #include <asm/cacheflush.h>
  40. #include <asm/irq.h>
  41. #include <asm/dma.h>
  42. #include <asm/portmux.h>
  43. #include "../codecs/ad73311.h"
  44. #include "bf5xx-sport.h"
  45. #include "bf5xx-i2s-pcm.h"
  46. #if CONFIG_SND_BF5XX_SPORT_NUM == 0
  47. #define bfin_write_SPORT_TCR1 bfin_write_SPORT0_TCR1
  48. #define bfin_read_SPORT_TCR1 bfin_read_SPORT0_TCR1
  49. #define bfin_write_SPORT_TCR2 bfin_write_SPORT0_TCR2
  50. #define bfin_write_SPORT_TX16 bfin_write_SPORT0_TX16
  51. #define bfin_read_SPORT_STAT bfin_read_SPORT0_STAT
  52. #else
  53. #define bfin_write_SPORT_TCR1 bfin_write_SPORT1_TCR1
  54. #define bfin_read_SPORT_TCR1 bfin_read_SPORT1_TCR1
  55. #define bfin_write_SPORT_TCR2 bfin_write_SPORT1_TCR2
  56. #define bfin_write_SPORT_TX16 bfin_write_SPORT1_TX16
  57. #define bfin_read_SPORT_STAT bfin_read_SPORT1_STAT
  58. #endif
  59. #define GPIO_SE CONFIG_SND_BFIN_AD73311_SE
  60. static struct snd_soc_card bf5xx_ad73311;
  61. static int snd_ad73311_startup(void)
  62. {
  63. pr_debug("%s enter\n", __func__);
  64. /* Pull up SE pin on AD73311L */
  65. gpio_set_value(GPIO_SE, 1);
  66. return 0;
  67. }
  68. static int snd_ad73311_configure(void)
  69. {
  70. unsigned short ctrl_regs[6];
  71. unsigned short status = 0;
  72. int count = 0;
  73. /* DMCLK = MCLK = 16.384 MHz
  74. * SCLK = DMCLK/8 = 2.048 MHz
  75. * Sample Rate = DMCLK/2048 = 8 KHz
  76. */
  77. ctrl_regs[0] = AD_CONTROL | AD_WRITE | CTRL_REG_B | REGB_MCDIV(0) | \
  78. REGB_SCDIV(0) | REGB_DIRATE(0);
  79. ctrl_regs[1] = AD_CONTROL | AD_WRITE | CTRL_REG_C | REGC_PUDEV | \
  80. REGC_PUADC | REGC_PUDAC | REGC_PUREF | REGC_REFUSE ;
  81. ctrl_regs[2] = AD_CONTROL | AD_WRITE | CTRL_REG_D | REGD_OGS(2) | \
  82. REGD_IGS(2);
  83. ctrl_regs[3] = AD_CONTROL | AD_WRITE | CTRL_REG_E | REGE_DA(0x1f);
  84. ctrl_regs[4] = AD_CONTROL | AD_WRITE | CTRL_REG_F | REGF_SEEN ;
  85. ctrl_regs[5] = AD_CONTROL | AD_WRITE | CTRL_REG_A | REGA_MODE_DATA;
  86. local_irq_disable();
  87. snd_ad73311_startup();
  88. udelay(1);
  89. bfin_write_SPORT_TCR1(TFSR);
  90. bfin_write_SPORT_TCR2(0xF);
  91. SSYNC();
  92. /* SPORT Tx Register is a 8 x 16 FIFO, all the data can be put to
  93. * FIFO before enable SPORT to transfer the data
  94. */
  95. for (count = 0; count < 6; count++)
  96. bfin_write_SPORT_TX16(ctrl_regs[count]);
  97. SSYNC();
  98. bfin_write_SPORT_TCR1(bfin_read_SPORT_TCR1() | TSPEN);
  99. SSYNC();
  100. /* When TUVF is set, the data is already send out */
  101. while (!(status & TUVF) && ++count < 10000) {
  102. udelay(1);
  103. status = bfin_read_SPORT_STAT();
  104. SSYNC();
  105. }
  106. bfin_write_SPORT_TCR1(bfin_read_SPORT_TCR1() & ~TSPEN);
  107. SSYNC();
  108. local_irq_enable();
  109. if (count >= 10000) {
  110. printk(KERN_ERR "ad73311: failed to configure codec\n");
  111. return -1;
  112. }
  113. return 0;
  114. }
  115. static int bf5xx_probe(struct platform_device *pdev)
  116. {
  117. int err;
  118. if (gpio_request(GPIO_SE, "AD73311_SE")) {
  119. printk(KERN_ERR "%s: Failed ro request GPIO_%d\n", __func__, GPIO_SE);
  120. return -EBUSY;
  121. }
  122. gpio_direction_output(GPIO_SE, 0);
  123. err = snd_ad73311_configure();
  124. if (err < 0)
  125. return -EFAULT;
  126. return 0;
  127. }
  128. static int bf5xx_ad73311_startup(struct snd_pcm_substream *substream)
  129. {
  130. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  131. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  132. pr_debug("%s enter\n", __func__);
  133. snd_soc_dai_set_drvdata(cpu_dai, sport_handle);
  134. return 0;
  135. }
  136. static int bf5xx_ad73311_hw_params(struct snd_pcm_substream *substream,
  137. struct snd_pcm_hw_params *params)
  138. {
  139. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  140. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  141. int ret = 0;
  142. pr_debug("%s rate %d format %x\n", __func__, params_rate(params),
  143. params_format(params));
  144. /* set cpu DAI configuration */
  145. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_DSP_A |
  146. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
  147. if (ret < 0)
  148. return ret;
  149. return 0;
  150. }
  151. static struct snd_soc_ops bf5xx_ad73311_ops = {
  152. .startup = bf5xx_ad73311_startup,
  153. .hw_params = bf5xx_ad73311_hw_params,
  154. };
  155. static struct snd_soc_dai_link bf5xx_ad73311_dai = {
  156. .name = "ad73311",
  157. .stream_name = "AD73311",
  158. .cpu_dai_name = "bf5xx-i2s",
  159. .codec_dai_name = "ad73311-hifi",
  160. .platform_name = "bfin-pcm-audio",
  161. .codec_name = "ad73311-codec",
  162. .ops = &bf5xx_ad73311_ops,
  163. };
  164. static struct snd_soc_card bf5xx_ad73311 = {
  165. .name = "bf5xx_ad73311",
  166. .probe = bf5xx_probe,
  167. .dai_link = &bf5xx_ad73311_dai,
  168. .num_links = 1,
  169. };
  170. static struct platform_device *bf5xx_ad73311_snd_device;
  171. static int __init bf5xx_ad73311_init(void)
  172. {
  173. int ret;
  174. pr_debug("%s enter\n", __func__);
  175. bf5xx_ad73311_snd_device = platform_device_alloc("soc-audio", -1);
  176. if (!bf5xx_ad73311_snd_device)
  177. return -ENOMEM;
  178. platform_set_drvdata(bf5xx_ad73311_snd_device, &bf5xx_ad73311);
  179. ret = platform_device_add(bf5xx_ad73311_snd_device);
  180. if (ret)
  181. platform_device_put(bf5xx_ad73311_snd_device);
  182. return ret;
  183. }
  184. static void __exit bf5xx_ad73311_exit(void)
  185. {
  186. pr_debug("%s enter\n", __func__);
  187. platform_device_unregister(bf5xx_ad73311_snd_device);
  188. }
  189. module_init(bf5xx_ad73311_init);
  190. module_exit(bf5xx_ad73311_exit);
  191. /* Module information */
  192. MODULE_AUTHOR("Cliff Cai");
  193. MODULE_DESCRIPTION("ALSA SoC AD73311 Blackfin");
  194. MODULE_LICENSE("GPL");