sam9g20_wm8731.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. /*
  245. * FIXME: This is a temporary bodge to avoid cross-tree merge issues.
  246. * New drivers should register the wm8731 I2C device in the machine
  247. * setup code (under arch/arm for ARM systems).
  248. */
  249. static int wm8731_i2c_register(void)
  250. {
  251. struct i2c_board_info info;
  252. struct i2c_adapter *adapter;
  253. struct i2c_client *client;
  254. memset(&info, 0, sizeof(struct i2c_board_info));
  255. info.addr = 0x1b;
  256. strlcpy(info.type, "wm8731", I2C_NAME_SIZE);
  257. adapter = i2c_get_adapter(0);
  258. if (!adapter) {
  259. printk(KERN_ERR "can't get i2c adapter 0\n");
  260. return -ENODEV;
  261. }
  262. client = i2c_new_device(adapter, &info);
  263. i2c_put_adapter(adapter);
  264. if (!client) {
  265. printk(KERN_ERR "can't add i2c device at 0x%x\n",
  266. (unsigned int)info.addr);
  267. return -ENODEV;
  268. }
  269. return 0;
  270. }
  271. static struct snd_soc_device at91sam9g20ek_snd_devdata = {
  272. .card = &snd_soc_at91sam9g20ek,
  273. .codec_dev = &soc_codec_dev_wm8731,
  274. };
  275. static struct platform_device *at91sam9g20ek_snd_device;
  276. static int __init at91sam9g20ek_init(void)
  277. {
  278. struct atmel_ssc_info *ssc_p = at91sam9g20ek_dai.cpu_dai->private_data;
  279. struct ssc_device *ssc = NULL;
  280. struct clk *pllb;
  281. int ret;
  282. if (!machine_is_at91sam9g20ek())
  283. return -ENODEV;
  284. /*
  285. * Codec MCLK is supplied by PCK0 - set it up.
  286. */
  287. mclk = clk_get(NULL, "pck0");
  288. if (IS_ERR(mclk)) {
  289. printk(KERN_ERR "ASoC: Failed to get MCLK\n");
  290. ret = PTR_ERR(mclk);
  291. goto err;
  292. }
  293. pllb = clk_get(NULL, "pllb");
  294. if (IS_ERR(mclk)) {
  295. printk(KERN_ERR "ASoC: Failed to get PLLB\n");
  296. ret = PTR_ERR(mclk);
  297. goto err_mclk;
  298. }
  299. ret = clk_set_parent(mclk, pllb);
  300. clk_put(pllb);
  301. if (ret != 0) {
  302. printk(KERN_ERR "ASoC: Failed to set MCLK parent\n");
  303. goto err_mclk;
  304. }
  305. clk_set_rate(mclk, MCLK_RATE);
  306. /*
  307. * Request SSC device
  308. */
  309. ssc = ssc_request(0);
  310. if (IS_ERR(ssc)) {
  311. printk(KERN_ERR "ASoC: Failed to request SSC 0\n");
  312. ret = PTR_ERR(ssc);
  313. ssc = NULL;
  314. goto err_ssc;
  315. }
  316. ssc_p->ssc = ssc;
  317. ret = wm8731_i2c_register();
  318. if (ret != 0)
  319. goto err_ssc;
  320. at91sam9g20ek_snd_device = platform_device_alloc("soc-audio", -1);
  321. if (!at91sam9g20ek_snd_device) {
  322. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  323. ret = -ENOMEM;
  324. }
  325. platform_set_drvdata(at91sam9g20ek_snd_device,
  326. &at91sam9g20ek_snd_devdata);
  327. at91sam9g20ek_snd_devdata.dev = &at91sam9g20ek_snd_device->dev;
  328. ret = platform_device_add(at91sam9g20ek_snd_device);
  329. if (ret) {
  330. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  331. platform_device_put(at91sam9g20ek_snd_device);
  332. }
  333. return ret;
  334. err_ssc:
  335. ssc_free(ssc);
  336. ssc_p->ssc = NULL;
  337. err_mclk:
  338. clk_put(mclk);
  339. mclk = NULL;
  340. err:
  341. return ret;
  342. }
  343. static void __exit at91sam9g20ek_exit(void)
  344. {
  345. struct atmel_ssc_info *ssc_p = at91sam9g20ek_dai.cpu_dai->private_data;
  346. struct ssc_device *ssc;
  347. if (ssc_p != NULL) {
  348. ssc = ssc_p->ssc;
  349. if (ssc != NULL)
  350. ssc_free(ssc);
  351. ssc_p->ssc = NULL;
  352. }
  353. platform_device_unregister(at91sam9g20ek_snd_device);
  354. at91sam9g20ek_snd_device = NULL;
  355. clk_put(mclk);
  356. mclk = NULL;
  357. }
  358. module_init(at91sam9g20ek_init);
  359. module_exit(at91sam9g20ek_exit);
  360. /* Module information */
  361. MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
  362. MODULE_DESCRIPTION("ALSA SoC AT91SAM9G20EK_WM8731");
  363. MODULE_LICENSE("GPL");