sam9g20_wm8731.c 9.9 KB

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