sam9g20_wm8731.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. /*
  53. * As shipped the board does not have inputs. However, it is relatively
  54. * straightforward to modify the board to hook them up so support is left
  55. * in the driver.
  56. */
  57. #undef ENABLE_MIC_INPUT
  58. static struct clk *mclk;
  59. static int at91sam9g20ek_startup(struct snd_pcm_substream *substream)
  60. {
  61. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  62. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  63. int ret;
  64. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK,
  65. MCLK_RATE, SND_SOC_CLOCK_IN);
  66. if (ret < 0) {
  67. clk_disable(mclk);
  68. return ret;
  69. }
  70. return 0;
  71. }
  72. static void at91sam9g20ek_shutdown(struct snd_pcm_substream *substream)
  73. {
  74. struct snd_soc_pcm_runtime *rtd = snd_pcm_substream_chip(substream);
  75. dev_dbg(rtd->socdev->dev, "shutdown");
  76. }
  77. static int at91sam9g20ek_hw_params(struct snd_pcm_substream *substream,
  78. struct snd_pcm_hw_params *params)
  79. {
  80. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  81. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  82. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  83. int ret;
  84. /* set codec DAI configuration */
  85. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  86. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
  87. if (ret < 0)
  88. return ret;
  89. /* set cpu DAI configuration */
  90. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  91. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
  92. if (ret < 0)
  93. return ret;
  94. return 0;
  95. }
  96. static struct snd_soc_ops at91sam9g20ek_ops = {
  97. .startup = at91sam9g20ek_startup,
  98. .hw_params = at91sam9g20ek_hw_params,
  99. .shutdown = at91sam9g20ek_shutdown,
  100. };
  101. static int at91sam9g20ek_set_bias_level(struct snd_soc_card *card,
  102. enum snd_soc_bias_level level)
  103. {
  104. static int mclk_on;
  105. int ret = 0;
  106. switch (level) {
  107. case SND_SOC_BIAS_ON:
  108. case SND_SOC_BIAS_PREPARE:
  109. if (!mclk_on)
  110. ret = clk_enable(mclk);
  111. if (ret == 0)
  112. mclk_on = 1;
  113. break;
  114. case SND_SOC_BIAS_OFF:
  115. case SND_SOC_BIAS_STANDBY:
  116. if (mclk_on)
  117. clk_disable(mclk);
  118. mclk_on = 0;
  119. break;
  120. }
  121. return ret;
  122. }
  123. static const struct snd_soc_dapm_widget at91sam9g20ek_dapm_widgets[] = {
  124. SND_SOC_DAPM_MIC("Int Mic", NULL),
  125. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  126. };
  127. static const struct snd_soc_dapm_route intercon[] = {
  128. /* speaker connected to LHPOUT */
  129. {"Ext Spk", NULL, "LHPOUT"},
  130. /* mic is connected to Mic Jack, with WM8731 Mic Bias */
  131. {"MICIN", NULL, "Mic Bias"},
  132. {"Mic Bias", NULL, "Int Mic"},
  133. };
  134. /*
  135. * Logic for a wm8731 as connected on a at91sam9g20ek board.
  136. */
  137. static int at91sam9g20ek_wm8731_init(struct snd_soc_codec *codec)
  138. {
  139. printk(KERN_DEBUG
  140. "at91sam9g20ek_wm8731 "
  141. ": at91sam9g20ek_wm8731_init() called\n");
  142. /* Add specific widgets */
  143. snd_soc_dapm_new_controls(codec, at91sam9g20ek_dapm_widgets,
  144. ARRAY_SIZE(at91sam9g20ek_dapm_widgets));
  145. /* Set up specific audio path interconnects */
  146. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  147. /* not connected */
  148. snd_soc_dapm_nc_pin(codec, "RLINEIN");
  149. snd_soc_dapm_nc_pin(codec, "LLINEIN");
  150. #ifdef ENABLE_MIC_INPUT
  151. snd_soc_dapm_enable_pin(codec, "Int Mic");
  152. #else
  153. snd_soc_dapm_nc_pin(codec, "Int Mic");
  154. #endif
  155. /* always connected */
  156. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  157. snd_soc_dapm_sync(codec);
  158. return 0;
  159. }
  160. static struct snd_soc_dai_link at91sam9g20ek_dai = {
  161. .name = "WM8731",
  162. .stream_name = "WM8731 PCM",
  163. .cpu_dai = &atmel_ssc_dai[0],
  164. .codec_dai = &wm8731_dai,
  165. .init = at91sam9g20ek_wm8731_init,
  166. .ops = &at91sam9g20ek_ops,
  167. };
  168. static struct snd_soc_card snd_soc_at91sam9g20ek = {
  169. .name = "AT91SAMG20-EK",
  170. .platform = &atmel_soc_platform,
  171. .dai_link = &at91sam9g20ek_dai,
  172. .num_links = 1,
  173. .set_bias_level = at91sam9g20ek_set_bias_level,
  174. };
  175. /*
  176. * FIXME: This is a temporary bodge to avoid cross-tree merge issues.
  177. * New drivers should register the wm8731 I2C device in the machine
  178. * setup code (under arch/arm for ARM systems).
  179. */
  180. static int wm8731_i2c_register(void)
  181. {
  182. struct i2c_board_info info;
  183. struct i2c_adapter *adapter;
  184. struct i2c_client *client;
  185. memset(&info, 0, sizeof(struct i2c_board_info));
  186. info.addr = 0x1b;
  187. strlcpy(info.type, "wm8731", I2C_NAME_SIZE);
  188. adapter = i2c_get_adapter(0);
  189. if (!adapter) {
  190. printk(KERN_ERR "can't get i2c adapter 0\n");
  191. return -ENODEV;
  192. }
  193. client = i2c_new_device(adapter, &info);
  194. i2c_put_adapter(adapter);
  195. if (!client) {
  196. printk(KERN_ERR "can't add i2c device at 0x%x\n",
  197. (unsigned int)info.addr);
  198. return -ENODEV;
  199. }
  200. return 0;
  201. }
  202. static struct snd_soc_device at91sam9g20ek_snd_devdata = {
  203. .card = &snd_soc_at91sam9g20ek,
  204. .codec_dev = &soc_codec_dev_wm8731,
  205. };
  206. static struct platform_device *at91sam9g20ek_snd_device;
  207. static int __init at91sam9g20ek_init(void)
  208. {
  209. struct atmel_ssc_info *ssc_p = at91sam9g20ek_dai.cpu_dai->private_data;
  210. struct ssc_device *ssc = NULL;
  211. struct clk *pllb;
  212. int ret;
  213. if (!machine_is_at91sam9g20ek())
  214. return -ENODEV;
  215. /*
  216. * Codec MCLK is supplied by PCK0 - set it up.
  217. */
  218. mclk = clk_get(NULL, "pck0");
  219. if (IS_ERR(mclk)) {
  220. printk(KERN_ERR "ASoC: Failed to get MCLK\n");
  221. ret = PTR_ERR(mclk);
  222. goto err;
  223. }
  224. pllb = clk_get(NULL, "pllb");
  225. if (IS_ERR(mclk)) {
  226. printk(KERN_ERR "ASoC: Failed to get PLLB\n");
  227. ret = PTR_ERR(mclk);
  228. goto err_mclk;
  229. }
  230. ret = clk_set_parent(mclk, pllb);
  231. clk_put(pllb);
  232. if (ret != 0) {
  233. printk(KERN_ERR "ASoC: Failed to set MCLK parent\n");
  234. goto err_mclk;
  235. }
  236. clk_set_rate(mclk, MCLK_RATE);
  237. /*
  238. * Request SSC device
  239. */
  240. ssc = ssc_request(0);
  241. if (IS_ERR(ssc)) {
  242. printk(KERN_ERR "ASoC: Failed to request SSC 0\n");
  243. ret = PTR_ERR(ssc);
  244. ssc = NULL;
  245. goto err_ssc;
  246. }
  247. ssc_p->ssc = ssc;
  248. ret = wm8731_i2c_register();
  249. if (ret != 0)
  250. goto err_ssc;
  251. at91sam9g20ek_snd_device = platform_device_alloc("soc-audio", -1);
  252. if (!at91sam9g20ek_snd_device) {
  253. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  254. ret = -ENOMEM;
  255. }
  256. platform_set_drvdata(at91sam9g20ek_snd_device,
  257. &at91sam9g20ek_snd_devdata);
  258. at91sam9g20ek_snd_devdata.dev = &at91sam9g20ek_snd_device->dev;
  259. ret = platform_device_add(at91sam9g20ek_snd_device);
  260. if (ret) {
  261. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  262. platform_device_put(at91sam9g20ek_snd_device);
  263. }
  264. return ret;
  265. err_ssc:
  266. ssc_free(ssc);
  267. ssc_p->ssc = NULL;
  268. err_mclk:
  269. clk_put(mclk);
  270. mclk = NULL;
  271. err:
  272. return ret;
  273. }
  274. static void __exit at91sam9g20ek_exit(void)
  275. {
  276. struct atmel_ssc_info *ssc_p = at91sam9g20ek_dai.cpu_dai->private_data;
  277. struct ssc_device *ssc;
  278. if (ssc_p != NULL) {
  279. ssc = ssc_p->ssc;
  280. if (ssc != NULL)
  281. ssc_free(ssc);
  282. ssc_p->ssc = NULL;
  283. }
  284. platform_device_unregister(at91sam9g20ek_snd_device);
  285. at91sam9g20ek_snd_device = NULL;
  286. clk_put(mclk);
  287. mclk = NULL;
  288. }
  289. module_init(at91sam9g20ek_init);
  290. module_exit(at91sam9g20ek_exit);
  291. /* Module information */
  292. MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
  293. MODULE_DESCRIPTION("ALSA SoC AT91SAM9G20EK_WM8731");
  294. MODULE_LICENSE("GPL");