sam9g20_wm8731.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * sam9g20_wm8731 -- SoC audio for AT91SAM9G20-based
  3. * ATMEL AT91SAM9G20ek board.
  4. *
  5. * Copyright (C) 2005 SAN People
  6. * Copyright (C) 2008 Atmel
  7. *
  8. * Authors: Sedji Gaouaou <sedji.gaouaou@atmel.com>
  9. *
  10. * Based on ati_b1_wm8731.c by:
  11. * Frank Mandarino <fmandarino@endrelia.com>
  12. * Copyright 2006 Endrelia Technologies Inc.
  13. * Based on corgi.c by:
  14. * Copyright 2005 Wolfson Microelectronics PLC.
  15. * Copyright 2005 Openedhand Ltd.
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. */
  31. #include <linux/module.h>
  32. #include <linux/moduleparam.h>
  33. #include <linux/kernel.h>
  34. #include <linux/clk.h>
  35. #include <linux/timer.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/atmel-ssc.h>
  39. #include <sound/core.h>
  40. #include <sound/pcm.h>
  41. #include <sound/pcm_params.h>
  42. #include <sound/soc.h>
  43. #include <sound/soc-dapm.h>
  44. #include <asm/mach-types.h>
  45. #include <mach/hardware.h>
  46. #include <mach/gpio.h>
  47. #include "../codecs/wm8731.h"
  48. #include "atmel-pcm.h"
  49. #include "atmel_ssc_dai.h"
  50. #define MCLK_RATE 12000000
  51. static struct clk *mclk;
  52. static int at91sam9g20ek_startup(struct snd_pcm_substream *substream)
  53. {
  54. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  55. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  56. int ret;
  57. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK,
  58. MCLK_RATE, SND_SOC_CLOCK_IN);
  59. if (ret < 0) {
  60. clk_disable(mclk);
  61. return ret;
  62. }
  63. return 0;
  64. }
  65. static void at91sam9g20ek_shutdown(struct snd_pcm_substream *substream)
  66. {
  67. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  68. dev_dbg(rtd->socdev->dev, "shutdown");
  69. }
  70. static int at91sam9g20ek_hw_params(struct snd_pcm_substream *substream,
  71. struct snd_pcm_hw_params *params)
  72. {
  73. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  74. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  75. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  76. struct atmel_ssc_info *ssc_p = cpu_dai->private_data;
  77. struct ssc_device *ssc = ssc_p->ssc;
  78. int ret;
  79. unsigned int rate;
  80. int cmr_div, period;
  81. if (ssc == NULL) {
  82. printk(KERN_INFO "at91sam9g20ek_hw_params: ssc is NULL!\n");
  83. return -EINVAL;
  84. }
  85. /* set codec DAI configuration */
  86. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  87. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  88. if (ret < 0)
  89. return ret;
  90. /* set cpu DAI configuration */
  91. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  92. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS);
  93. if (ret < 0)
  94. return ret;
  95. /*
  96. * The SSC clock dividers depend on the sample rate. The CMR.DIV
  97. * field divides the system master clock MCK to drive the SSC TK
  98. * signal which provides the codec BCLK. The TCMR.PERIOD and
  99. * RCMR.PERIOD fields further divide the BCLK signal to drive
  100. * the SSC TF and RF signals which provide the codec DACLRC and
  101. * ADCLRC clocks.
  102. *
  103. * The dividers were determined through trial and error, where a
  104. * CMR.DIV value is chosen such that the resulting BCLK value is
  105. * divisible, or almost divisible, by (2 * sample rate), and then
  106. * the TCMR.PERIOD or RCMR.PERIOD is BCLK / (2 * sample rate) - 1.
  107. */
  108. rate = params_rate(params);
  109. switch (rate) {
  110. case 8000:
  111. cmr_div = 55; /* BCLK = 133MHz/(2*55) = 1.209MHz */
  112. period = 74; /* LRC = BCLK/(2*(74+1)) ~= 8060,6Hz */
  113. break;
  114. case 11025:
  115. cmr_div = 67; /* BCLK = 133MHz/(2*60) = 1.108MHz */
  116. period = 45; /* LRC = BCLK/(2*(49+1)) = 11083,3Hz */
  117. break;
  118. case 16000:
  119. cmr_div = 63; /* BCLK = 133MHz/(2*63) = 1.055MHz */
  120. period = 32; /* LRC = BCLK/(2*(32+1)) = 15993,2Hz */
  121. break;
  122. case 22050:
  123. cmr_div = 52; /* BCLK = 133MHz/(2*52) = 1.278MHz */
  124. period = 28; /* LRC = BCLK/(2*(28+1)) = 22049Hz */
  125. break;
  126. case 32000:
  127. cmr_div = 66; /* BCLK = 133MHz/(2*66) = 1.007MHz */
  128. period = 15; /* LRC = BCLK/(2*(15+1)) = 31486,742Hz */
  129. break;
  130. case 44100:
  131. cmr_div = 29; /* BCLK = 133MHz/(2*29) = 2.293MHz */
  132. period = 25; /* LRC = BCLK/(2*(25+1)) = 44098Hz */
  133. break;
  134. case 48000:
  135. cmr_div = 33; /* BCLK = 133MHz/(2*33) = 2.015MHz */
  136. period = 20; /* LRC = BCLK/(2*(20+1)) = 47979,79Hz */
  137. break;
  138. case 88200:
  139. cmr_div = 29; /* BCLK = 133MHz/(2*29) = 2.293MHz */
  140. period = 12; /* LRC = BCLK/(2*(12+1)) = 88196Hz */
  141. break;
  142. case 96000:
  143. cmr_div = 23; /* BCLK = 133MHz/(2*23) = 2.891MHz */
  144. period = 14; /* LRC = BCLK/(2*(14+1)) = 96376Hz */
  145. break;
  146. default:
  147. printk(KERN_WARNING "unsupported rate %d"
  148. " on at91sam9g20ek board\n", rate);
  149. return -EINVAL;
  150. }
  151. /* set the MCK divider for BCLK */
  152. ret = snd_soc_dai_set_clkdiv(cpu_dai, ATMEL_SSC_CMR_DIV, cmr_div);
  153. if (ret < 0)
  154. return ret;
  155. if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
  156. /* set the BCLK divider for DACLRC */
  157. ret = snd_soc_dai_set_clkdiv(cpu_dai,
  158. ATMEL_SSC_TCMR_PERIOD, period);
  159. } else {
  160. /* set the BCLK divider for ADCLRC */
  161. ret = snd_soc_dai_set_clkdiv(cpu_dai,
  162. ATMEL_SSC_RCMR_PERIOD, period);
  163. }
  164. if (ret < 0)
  165. return ret;
  166. return 0;
  167. }
  168. static struct snd_soc_ops at91sam9g20ek_ops = {
  169. .startup = at91sam9g20ek_startup,
  170. .hw_params = at91sam9g20ek_hw_params,
  171. .shutdown = at91sam9g20ek_shutdown,
  172. };
  173. static int at91sam9g20ek_set_bias_level(struct snd_soc_card *card,
  174. enum snd_soc_bias_level level)
  175. {
  176. static int mclk_on;
  177. int ret = 0;
  178. switch (level) {
  179. case SND_SOC_BIAS_ON:
  180. case SND_SOC_BIAS_PREPARE:
  181. if (!mclk_on)
  182. ret = clk_enable(mclk);
  183. if (ret == 0)
  184. mclk_on = 1;
  185. break;
  186. case SND_SOC_BIAS_OFF:
  187. case SND_SOC_BIAS_STANDBY:
  188. if (mclk_on)
  189. clk_disable(mclk);
  190. mclk_on = 0;
  191. break;
  192. }
  193. return ret;
  194. }
  195. static const struct snd_soc_dapm_widget at91sam9g20ek_dapm_widgets[] = {
  196. SND_SOC_DAPM_MIC("Int Mic", NULL),
  197. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  198. };
  199. static const struct snd_soc_dapm_route intercon[] = {
  200. /* speaker connected to LHPOUT */
  201. {"Ext Spk", NULL, "LHPOUT"},
  202. /* mic is connected to Mic Jack, with WM8731 Mic Bias */
  203. {"MICIN", NULL, "Mic Bias"},
  204. {"Mic Bias", NULL, "Int Mic"},
  205. };
  206. /*
  207. * Logic for a wm8731 as connected on a at91sam9g20ek board.
  208. */
  209. static int at91sam9g20ek_wm8731_init(struct snd_soc_codec *codec)
  210. {
  211. printk(KERN_DEBUG
  212. "at91sam9g20ek_wm8731 "
  213. ": at91sam9g20ek_wm8731_init() called\n");
  214. /* Add specific widgets */
  215. snd_soc_dapm_new_controls(codec, at91sam9g20ek_dapm_widgets,
  216. ARRAY_SIZE(at91sam9g20ek_dapm_widgets));
  217. /* Set up specific audio path interconnects */
  218. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  219. /* not connected */
  220. snd_soc_dapm_nc_pin(codec, "RLINEIN");
  221. snd_soc_dapm_nc_pin(codec, "LLINEIN");
  222. /* always connected */
  223. snd_soc_dapm_enable_pin(codec, "Int Mic");
  224. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  225. snd_soc_dapm_sync(codec);
  226. return 0;
  227. }
  228. static struct snd_soc_dai_link at91sam9g20ek_dai = {
  229. .name = "WM8731",
  230. .stream_name = "WM8731 PCM",
  231. .cpu_dai = &atmel_ssc_dai[0],
  232. .codec_dai = &wm8731_dai,
  233. .init = at91sam9g20ek_wm8731_init,
  234. .ops = &at91sam9g20ek_ops,
  235. };
  236. static struct snd_soc_card snd_soc_at91sam9g20ek = {
  237. .name = "AT91SAMG20-EK",
  238. .platform = &atmel_soc_platform,
  239. .dai_link = &at91sam9g20ek_dai,
  240. .num_links = 1,
  241. .set_bias_level = at91sam9g20ek_set_bias_level,
  242. };
  243. static struct wm8731_setup_data at91sam9g20ek_wm8731_setup = {
  244. .i2c_bus = 0,
  245. .i2c_address = 0x1b,
  246. };
  247. static struct snd_soc_device at91sam9g20ek_snd_devdata = {
  248. .card = &snd_soc_at91sam9g20ek,
  249. .codec_dev = &soc_codec_dev_wm8731,
  250. .codec_data = &at91sam9g20ek_wm8731_setup,
  251. };
  252. static struct platform_device *at91sam9g20ek_snd_device;
  253. static int __init at91sam9g20ek_init(void)
  254. {
  255. struct atmel_ssc_info *ssc_p = at91sam9g20ek_dai.cpu_dai->private_data;
  256. struct ssc_device *ssc = NULL;
  257. struct clk *pllb;
  258. int ret;
  259. if (!machine_is_at91sam9g20ek())
  260. return -ENODEV;
  261. /*
  262. * Codec MCLK is supplied by PCK0 - set it up.
  263. */
  264. mclk = clk_get(NULL, "pck0");
  265. if (IS_ERR(mclk)) {
  266. printk(KERN_ERR "ASoC: Failed to get MCLK\n");
  267. ret = PTR_ERR(mclk);
  268. goto err;
  269. }
  270. pllb = clk_get(NULL, "pllb");
  271. if (IS_ERR(mclk)) {
  272. printk(KERN_ERR "ASoC: Failed to get PLLB\n");
  273. ret = PTR_ERR(mclk);
  274. goto err_mclk;
  275. }
  276. ret = clk_set_parent(mclk, pllb);
  277. clk_put(pllb);
  278. if (ret != 0) {
  279. printk(KERN_ERR "ASoC: Failed to set MCLK parent\n");
  280. goto err_mclk;
  281. }
  282. clk_set_rate(mclk, MCLK_RATE);
  283. /*
  284. * Request SSC device
  285. */
  286. ssc = ssc_request(0);
  287. if (IS_ERR(ssc)) {
  288. printk(KERN_ERR "ASoC: Failed to request SSC 0\n");
  289. ret = PTR_ERR(ssc);
  290. ssc = NULL;
  291. goto err_ssc;
  292. }
  293. ssc_p->ssc = ssc;
  294. at91sam9g20ek_snd_device = platform_device_alloc("soc-audio", -1);
  295. if (!at91sam9g20ek_snd_device) {
  296. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  297. ret = -ENOMEM;
  298. }
  299. platform_set_drvdata(at91sam9g20ek_snd_device,
  300. &at91sam9g20ek_snd_devdata);
  301. at91sam9g20ek_snd_devdata.dev = &at91sam9g20ek_snd_device->dev;
  302. ret = platform_device_add(at91sam9g20ek_snd_device);
  303. if (ret) {
  304. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  305. platform_device_put(at91sam9g20ek_snd_device);
  306. }
  307. return ret;
  308. err_ssc:
  309. err_mclk:
  310. clk_put(mclk);
  311. mclk = NULL;
  312. err:
  313. return ret;
  314. }
  315. static void __exit at91sam9g20ek_exit(void)
  316. {
  317. struct atmel_ssc_info *ssc_p = at91sam9g20ek_dai.cpu_dai->private_data;
  318. struct ssc_device *ssc;
  319. if (ssc_p != NULL) {
  320. ssc = ssc_p->ssc;
  321. if (ssc != NULL)
  322. ssc_free(ssc);
  323. ssc_p->ssc = NULL;
  324. }
  325. platform_device_unregister(at91sam9g20ek_snd_device);
  326. at91sam9g20ek_snd_device = NULL;
  327. clk_put(mclk);
  328. mclk = NULL;
  329. }
  330. module_init(at91sam9g20ek_init);
  331. module_exit(at91sam9g20ek_exit);
  332. /* Module information */
  333. MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
  334. MODULE_DESCRIPTION("ALSA SoC AT91SAM9G20EK_WM8731");
  335. MODULE_LICENSE("GPL");