eti_b1_wm8731.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * eti_b1_wm8731 -- SoC audio for AT91RM9200-based Endrelia ETI_B1 board.
  3. *
  4. * Author: Frank Mandarino <fmandarino@endrelia.com>
  5. * Endrelia Technologies Inc.
  6. * Created: Mar 29, 2006
  7. *
  8. * Based on corgi.c by:
  9. *
  10. * Copyright 2005 Wolfson Microelectronics PLC.
  11. * Copyright 2005 Openedhand Ltd.
  12. *
  13. * Authors: Liam Girdwood <liam.girdwood@wolfsonmicro.com>
  14. * Richard Purdie <richard@openedhand.com>
  15. *
  16. * This program is free software; you can redistribute it and/or modify it
  17. * under the terms of the GNU General Public License as published by the
  18. * Free Software Foundation; either version 2 of the License, or (at your
  19. * option) any later version.
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/version.h>
  25. #include <linux/kernel.h>
  26. #include <linux/clk.h>
  27. #include <linux/timer.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/platform_device.h>
  30. #include <sound/core.h>
  31. #include <sound/pcm.h>
  32. #include <sound/soc.h>
  33. #include <sound/soc-dapm.h>
  34. #include <asm/hardware.h>
  35. #include <asm/arch/gpio.h>
  36. #include "../codecs/wm8731.h"
  37. #include "at91-pcm.h"
  38. #include "at91-ssc.h"
  39. #if 0
  40. #define DBG(x...) printk(KERN_INFO "eti_b1_wm8731: " x)
  41. #else
  42. #define DBG(x...)
  43. #endif
  44. static struct clk *pck1_clk;
  45. static struct clk *pllb_clk;
  46. static int eti_b1_startup(struct snd_pcm_substream *substream)
  47. {
  48. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  49. struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
  50. struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
  51. int ret;
  52. /* cpu clock is the AT91 master clock sent to the SSC */
  53. ret = cpu_dai->dai_ops.set_sysclk(cpu_dai, AT91_SYSCLK_MCK,
  54. 60000000, SND_SOC_CLOCK_IN);
  55. if (ret < 0)
  56. return ret;
  57. /* codec system clock is supplied by PCK1, set to 12MHz */
  58. ret = codec_dai->dai_ops.set_sysclk(codec_dai, WM8731_SYSCLK,
  59. 12000000, SND_SOC_CLOCK_IN);
  60. if (ret < 0)
  61. return ret;
  62. /* Start PCK1 clock. */
  63. clk_enable(pck1_clk);
  64. DBG("pck1 started\n");
  65. return 0;
  66. }
  67. static void eti_b1_shutdown(struct snd_pcm_substream *substream)
  68. {
  69. /* Stop PCK1 clock. */
  70. clk_disable(pck1_clk);
  71. DBG("pck1 stopped\n");
  72. }
  73. static int eti_b1_hw_params(struct snd_pcm_substream *substream,
  74. struct snd_pcm_hw_params *params)
  75. {
  76. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  77. struct snd_soc_codec_dai *codec_dai = rtd->dai->codec_dai;
  78. struct snd_soc_cpu_dai *cpu_dai = rtd->dai->cpu_dai;
  79. int ret;
  80. #ifdef CONFIG_SND_AT91_SOC_ETI_SLAVE
  81. unsigned int rate;
  82. int cmr_div, period;
  83. /* set codec DAI configuration */
  84. ret = codec_dai->dai_ops.set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  85. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  86. if (ret < 0)
  87. return ret;
  88. /* set cpu DAI configuration */
  89. ret = cpu_dai->dai_ops.set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  90. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  91. if (ret < 0)
  92. return ret;
  93. /*
  94. * The SSC clock dividers depend on the sample rate. The CMR.DIV
  95. * field divides the system master clock MCK to drive the SSC TK
  96. * signal which provides the codec BCLK. The TCMR.PERIOD and
  97. * RCMR.PERIOD fields further divide the BCLK signal to drive
  98. * the SSC TF and RF signals which provide the codec DACLRC and
  99. * ADCLRC clocks.
  100. *
  101. * The dividers were determined through trial and error, where a
  102. * CMR.DIV value is chosen such that the resulting BCLK value is
  103. * divisible, or almost divisible, by (2 * sample rate), and then
  104. * the TCMR.PERIOD or RCMR.PERIOD is BCLK / (2 * sample rate) - 1.
  105. */
  106. rate = params_rate(params);
  107. switch (rate) {
  108. case 8000:
  109. cmr_div = 25; /* BCLK = 60MHz/(2*25) = 1.2MHz */
  110. period = 74; /* LRC = BCLK/(2*(74+1)) = 8000Hz */
  111. break;
  112. case 32000:
  113. cmr_div = 7; /* BCLK = 60MHz/(2*7) ~= 4.28571428MHz */
  114. period = 66; /* LRC = BCLK/(2*(66+1)) = 31982.942Hz */
  115. break;
  116. case 48000:
  117. cmr_div = 13; /* BCLK = 60MHz/(2*13) ~= 2.3076923MHz */
  118. period = 23; /* LRC = BCLK/(2*(23+1)) = 48076.923Hz */
  119. break;
  120. default:
  121. printk(KERN_WARNING "unsupported rate %d on ETI-B1 board\n", rate);
  122. return -EINVAL;
  123. }
  124. /* set the MCK divider for BCLK */
  125. ret = cpu_dai->dai_ops.set_clkdiv(cpu_dai, AT91SSC_CMR_DIV, cmr_div);
  126. if (ret < 0)
  127. return ret;
  128. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  129. /* set the BCLK divider for DACLRC */
  130. ret = cpu_dai->dai_ops.set_clkdiv(cpu_dai,
  131. AT91SSC_TCMR_PERIOD, period);
  132. } else {
  133. /* set the BCLK divider for ADCLRC */
  134. ret = cpu_dai->dai_ops.set_clkdiv(cpu_dai,
  135. AT91SSC_RCMR_PERIOD, period);
  136. }
  137. if (ret < 0)
  138. return ret;
  139. #else /* CONFIG_SND_AT91_SOC_ETI_SLAVE */
  140. /*
  141. * Codec in Master Mode.
  142. */
  143. /* set codec DAI configuration */
  144. ret = codec_dai->dai_ops.set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  145. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
  146. if (ret < 0)
  147. return ret;
  148. /* set cpu DAI configuration */
  149. ret = cpu_dai->dai_ops.set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  150. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
  151. if (ret < 0)
  152. return ret;
  153. #endif /* CONFIG_SND_AT91_SOC_ETI_SLAVE */
  154. return 0;
  155. }
  156. static struct snd_soc_ops eti_b1_ops = {
  157. .startup = eti_b1_startup,
  158. .hw_params = eti_b1_hw_params,
  159. .shutdown = eti_b1_shutdown,
  160. };
  161. static const struct snd_soc_dapm_widget eti_b1_dapm_widgets[] = {
  162. SND_SOC_DAPM_MIC("Int Mic", NULL),
  163. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  164. };
  165. static const char *intercon[][3] = {
  166. /* speaker connected to LHPOUT */
  167. {"Ext Spk", NULL, "LHPOUT"},
  168. /* mic is connected to Mic Jack, with WM8731 Mic Bias */
  169. {"MICIN", NULL, "Mic Bias"},
  170. {"Mic Bias", NULL, "Int Mic"},
  171. /* terminator */
  172. {NULL, NULL, NULL},
  173. };
  174. /*
  175. * Logic for a wm8731 as connected on a Endrelia ETI-B1 board.
  176. */
  177. static int eti_b1_wm8731_init(struct snd_soc_codec *codec)
  178. {
  179. int i;
  180. DBG("eti_b1_wm8731_init() called\n");
  181. /* Add specific widgets */
  182. for(i = 0; i < ARRAY_SIZE(eti_b1_dapm_widgets); i++) {
  183. snd_soc_dapm_new_control(codec, &eti_b1_dapm_widgets[i]);
  184. }
  185. /* Set up specific audio path interconnects */
  186. for(i = 0; intercon[i][0] != NULL; i++) {
  187. snd_soc_dapm_connect_input(codec, intercon[i][0],
  188. intercon[i][1], intercon[i][2]);
  189. }
  190. /* not connected */
  191. snd_soc_dapm_set_endpoint(codec, "RLINEIN", 0);
  192. snd_soc_dapm_set_endpoint(codec, "LLINEIN", 0);
  193. /* always connected */
  194. snd_soc_dapm_set_endpoint(codec, "Int Mic", 1);
  195. snd_soc_dapm_set_endpoint(codec, "Ext Spk", 1);
  196. snd_soc_dapm_sync_endpoints(codec);
  197. return 0;
  198. }
  199. static struct snd_soc_dai_link eti_b1_dai = {
  200. .name = "WM8731",
  201. .stream_name = "WM8731 PCM",
  202. .cpu_dai = &at91_ssc_dai[1],
  203. .codec_dai = &wm8731_dai,
  204. .init = eti_b1_wm8731_init,
  205. .ops = &eti_b1_ops,
  206. };
  207. static struct snd_soc_machine snd_soc_machine_eti_b1 = {
  208. .name = "ETI_B1_WM8731",
  209. .dai_link = &eti_b1_dai,
  210. .num_links = 1,
  211. };
  212. static struct wm8731_setup_data eti_b1_wm8731_setup = {
  213. .i2c_address = 0x1a,
  214. };
  215. static struct snd_soc_device eti_b1_snd_devdata = {
  216. .machine = &snd_soc_machine_eti_b1,
  217. .platform = &at91_soc_platform,
  218. .codec_dev = &soc_codec_dev_wm8731,
  219. .codec_data = &eti_b1_wm8731_setup,
  220. };
  221. static struct platform_device *eti_b1_snd_device;
  222. static int __init eti_b1_init(void)
  223. {
  224. int ret;
  225. struct at91_ssc_periph *ssc = eti_b1_dai.cpu_dai->private_data;
  226. if (!request_mem_region(AT91RM9200_BASE_SSC1, SZ_16K, "soc-audio")) {
  227. DBG("SSC1 memory region is busy\n");
  228. return -EBUSY;
  229. }
  230. ssc->base = ioremap(AT91RM9200_BASE_SSC1, SZ_16K);
  231. if (!ssc->base) {
  232. DBG("SSC1 memory ioremap failed\n");
  233. ret = -ENOMEM;
  234. goto fail_release_mem;
  235. }
  236. ssc->pid = AT91RM9200_ID_SSC1;
  237. eti_b1_snd_device = platform_device_alloc("soc-audio", -1);
  238. if (!eti_b1_snd_device) {
  239. DBG("platform device allocation failed\n");
  240. ret = -ENOMEM;
  241. goto fail_io_unmap;
  242. }
  243. platform_set_drvdata(eti_b1_snd_device, &eti_b1_snd_devdata);
  244. eti_b1_snd_devdata.dev = &eti_b1_snd_device->dev;
  245. ret = platform_device_add(eti_b1_snd_device);
  246. if (ret) {
  247. DBG("platform device add failed\n");
  248. platform_device_put(eti_b1_snd_device);
  249. goto fail_io_unmap;
  250. }
  251. at91_set_A_periph(AT91_PIN_PB6, 0); /* TF1 */
  252. at91_set_A_periph(AT91_PIN_PB7, 0); /* TK1 */
  253. at91_set_A_periph(AT91_PIN_PB8, 0); /* TD1 */
  254. at91_set_A_periph(AT91_PIN_PB9, 0); /* RD1 */
  255. /* at91_set_A_periph(AT91_PIN_PB10, 0);*/ /* RK1 */
  256. at91_set_A_periph(AT91_PIN_PB11, 0); /* RF1 */
  257. /*
  258. * Set PCK1 parent to PLLB and its rate to 12 Mhz.
  259. */
  260. pllb_clk = clk_get(NULL, "pllb");
  261. pck1_clk = clk_get(NULL, "pck1");
  262. clk_set_parent(pck1_clk, pllb_clk);
  263. clk_set_rate(pck1_clk, 12000000);
  264. DBG("MCLK rate %luHz\n", clk_get_rate(pck1_clk));
  265. /* assign the GPIO pin to PCK1 */
  266. at91_set_B_periph(AT91_PIN_PA24, 0);
  267. #ifdef CONFIG_SND_AT91_SOC_ETI_SLAVE
  268. printk(KERN_INFO "eti_b1_wm8731: Codec in Slave Mode\n");
  269. #else
  270. printk(KERN_INFO "eti_b1_wm8731: Codec in Master Mode\n");
  271. #endif
  272. return ret;
  273. fail_io_unmap:
  274. iounmap(ssc->base);
  275. fail_release_mem:
  276. release_mem_region(AT91RM9200_BASE_SSC1, SZ_16K);
  277. return ret;
  278. }
  279. static void __exit eti_b1_exit(void)
  280. {
  281. struct at91_ssc_periph *ssc = eti_b1_dai.cpu_dai->private_data;
  282. clk_put(pck1_clk);
  283. clk_put(pllb_clk);
  284. platform_device_unregister(eti_b1_snd_device);
  285. iounmap(ssc->base);
  286. release_mem_region(AT91RM9200_BASE_SSC1, SZ_16K);
  287. }
  288. module_init(eti_b1_init);
  289. module_exit(eti_b1_exit);
  290. /* Module information */
  291. MODULE_AUTHOR("Frank Mandarino <fmandarino@endrelia.com>");
  292. MODULE_DESCRIPTION("ALSA SoC ETI-B1-WM8731");
  293. MODULE_LICENSE("GPL");