eti_b1_wm8731.c 8.9 KB

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