sam9g20_wm8731.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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_hw_params(struct snd_pcm_substream *substream,
  60. struct snd_pcm_hw_params *params)
  61. {
  62. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  63. struct snd_soc_dai *codec_dai = rtd->dai->codec_dai;
  64. struct snd_soc_dai *cpu_dai = rtd->dai->cpu_dai;
  65. int ret;
  66. /* set codec DAI configuration */
  67. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
  68. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
  69. if (ret < 0)
  70. return ret;
  71. /* set cpu DAI configuration */
  72. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S |
  73. SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBM_CFM);
  74. if (ret < 0)
  75. return ret;
  76. return 0;
  77. }
  78. static struct snd_soc_ops at91sam9g20ek_ops = {
  79. .hw_params = at91sam9g20ek_hw_params,
  80. };
  81. static int at91sam9g20ek_set_bias_level(struct snd_soc_card *card,
  82. enum snd_soc_bias_level level)
  83. {
  84. static int mclk_on;
  85. int ret = 0;
  86. switch (level) {
  87. case SND_SOC_BIAS_ON:
  88. case SND_SOC_BIAS_PREPARE:
  89. if (!mclk_on)
  90. ret = clk_enable(mclk);
  91. if (ret == 0)
  92. mclk_on = 1;
  93. break;
  94. case SND_SOC_BIAS_OFF:
  95. case SND_SOC_BIAS_STANDBY:
  96. if (mclk_on)
  97. clk_disable(mclk);
  98. mclk_on = 0;
  99. break;
  100. }
  101. return ret;
  102. }
  103. static const struct snd_soc_dapm_widget at91sam9g20ek_dapm_widgets[] = {
  104. SND_SOC_DAPM_MIC("Int Mic", NULL),
  105. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  106. };
  107. static const struct snd_soc_dapm_route intercon[] = {
  108. /* speaker connected to LHPOUT */
  109. {"Ext Spk", NULL, "LHPOUT"},
  110. /* mic is connected to Mic Jack, with WM8731 Mic Bias */
  111. {"MICIN", NULL, "Mic Bias"},
  112. {"Mic Bias", NULL, "Int Mic"},
  113. };
  114. /*
  115. * Logic for a wm8731 as connected on a at91sam9g20ek board.
  116. */
  117. static int at91sam9g20ek_wm8731_init(struct snd_soc_codec *codec)
  118. {
  119. struct snd_soc_dai *codec_dai = &codec->dai[0];
  120. int ret;
  121. printk(KERN_DEBUG
  122. "at91sam9g20ek_wm8731 "
  123. ": at91sam9g20ek_wm8731_init() called\n");
  124. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK,
  125. MCLK_RATE, SND_SOC_CLOCK_IN);
  126. if (ret < 0) {
  127. printk(KERN_ERR "Failed to set WM8731 SYSCLK: %d\n", ret);
  128. return ret;
  129. }
  130. /* Add specific widgets */
  131. snd_soc_dapm_new_controls(codec, at91sam9g20ek_dapm_widgets,
  132. ARRAY_SIZE(at91sam9g20ek_dapm_widgets));
  133. /* Set up specific audio path interconnects */
  134. snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
  135. /* not connected */
  136. snd_soc_dapm_nc_pin(codec, "RLINEIN");
  137. snd_soc_dapm_nc_pin(codec, "LLINEIN");
  138. #ifdef ENABLE_MIC_INPUT
  139. snd_soc_dapm_enable_pin(codec, "Int Mic");
  140. #else
  141. snd_soc_dapm_nc_pin(codec, "Int Mic");
  142. #endif
  143. /* always connected */
  144. snd_soc_dapm_enable_pin(codec, "Ext Spk");
  145. snd_soc_dapm_sync(codec);
  146. return 0;
  147. }
  148. static struct snd_soc_dai_link at91sam9g20ek_dai = {
  149. .name = "WM8731",
  150. .stream_name = "WM8731 PCM",
  151. .cpu_dai = &atmel_ssc_dai[0],
  152. .codec_dai = &wm8731_dai,
  153. .init = at91sam9g20ek_wm8731_init,
  154. .ops = &at91sam9g20ek_ops,
  155. };
  156. static struct snd_soc_card snd_soc_at91sam9g20ek = {
  157. .name = "AT91SAMG20-EK",
  158. .platform = &atmel_soc_platform,
  159. .dai_link = &at91sam9g20ek_dai,
  160. .num_links = 1,
  161. .set_bias_level = at91sam9g20ek_set_bias_level,
  162. };
  163. static struct snd_soc_device at91sam9g20ek_snd_devdata = {
  164. .card = &snd_soc_at91sam9g20ek,
  165. .codec_dev = &soc_codec_dev_wm8731,
  166. };
  167. static struct platform_device *at91sam9g20ek_snd_device;
  168. static int __init at91sam9g20ek_init(void)
  169. {
  170. struct atmel_ssc_info *ssc_p = at91sam9g20ek_dai.cpu_dai->private_data;
  171. struct ssc_device *ssc = NULL;
  172. struct clk *pllb;
  173. int ret;
  174. if (!machine_is_at91sam9g20ek())
  175. return -ENODEV;
  176. /*
  177. * Codec MCLK is supplied by PCK0 - set it up.
  178. */
  179. mclk = clk_get(NULL, "pck0");
  180. if (IS_ERR(mclk)) {
  181. printk(KERN_ERR "ASoC: Failed to get MCLK\n");
  182. ret = PTR_ERR(mclk);
  183. goto err;
  184. }
  185. pllb = clk_get(NULL, "pllb");
  186. if (IS_ERR(mclk)) {
  187. printk(KERN_ERR "ASoC: Failed to get PLLB\n");
  188. ret = PTR_ERR(mclk);
  189. goto err_mclk;
  190. }
  191. ret = clk_set_parent(mclk, pllb);
  192. clk_put(pllb);
  193. if (ret != 0) {
  194. printk(KERN_ERR "ASoC: Failed to set MCLK parent\n");
  195. goto err_mclk;
  196. }
  197. clk_set_rate(mclk, MCLK_RATE);
  198. /*
  199. * Request SSC device
  200. */
  201. ssc = ssc_request(0);
  202. if (IS_ERR(ssc)) {
  203. printk(KERN_ERR "ASoC: Failed to request SSC 0\n");
  204. ret = PTR_ERR(ssc);
  205. ssc = NULL;
  206. goto err_ssc;
  207. }
  208. ssc_p->ssc = ssc;
  209. at91sam9g20ek_snd_device = platform_device_alloc("soc-audio", -1);
  210. if (!at91sam9g20ek_snd_device) {
  211. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  212. ret = -ENOMEM;
  213. }
  214. platform_set_drvdata(at91sam9g20ek_snd_device,
  215. &at91sam9g20ek_snd_devdata);
  216. at91sam9g20ek_snd_devdata.dev = &at91sam9g20ek_snd_device->dev;
  217. ret = platform_device_add(at91sam9g20ek_snd_device);
  218. if (ret) {
  219. printk(KERN_ERR "ASoC: Platform device allocation failed\n");
  220. platform_device_put(at91sam9g20ek_snd_device);
  221. }
  222. return ret;
  223. err_ssc:
  224. ssc_free(ssc);
  225. ssc_p->ssc = NULL;
  226. err_mclk:
  227. clk_put(mclk);
  228. mclk = NULL;
  229. err:
  230. return ret;
  231. }
  232. static void __exit at91sam9g20ek_exit(void)
  233. {
  234. struct atmel_ssc_info *ssc_p = at91sam9g20ek_dai.cpu_dai->private_data;
  235. struct ssc_device *ssc;
  236. if (ssc_p != NULL) {
  237. ssc = ssc_p->ssc;
  238. if (ssc != NULL)
  239. ssc_free(ssc);
  240. ssc_p->ssc = NULL;
  241. }
  242. platform_device_unregister(at91sam9g20ek_snd_device);
  243. at91sam9g20ek_snd_device = NULL;
  244. clk_put(mclk);
  245. mclk = NULL;
  246. }
  247. module_init(at91sam9g20ek_init);
  248. module_exit(at91sam9g20ek_exit);
  249. /* Module information */
  250. MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
  251. MODULE_DESCRIPTION("ALSA SoC AT91SAM9G20EK_WM8731");
  252. MODULE_LICENSE("GPL");