sam9g20_wm8731.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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/of.h>
  40. #include <linux/atmel-ssc.h>
  41. #include <sound/core.h>
  42. #include <sound/pcm.h>
  43. #include <sound/pcm_params.h>
  44. #include <sound/soc.h>
  45. #include "../codecs/wm8731.h"
  46. #include "atmel-pcm.h"
  47. #include "atmel_ssc_dai.h"
  48. #define MCLK_RATE 12000000
  49. /*
  50. * As shipped the board does not have inputs. However, it is relatively
  51. * straightforward to modify the board to hook them up so support is left
  52. * in the driver.
  53. */
  54. #undef ENABLE_MIC_INPUT
  55. static struct clk *mclk;
  56. static int at91sam9g20ek_set_bias_level(struct snd_soc_card *card,
  57. struct snd_soc_dapm_context *dapm,
  58. enum snd_soc_bias_level level)
  59. {
  60. static int mclk_on;
  61. int ret = 0;
  62. switch (level) {
  63. case SND_SOC_BIAS_ON:
  64. case SND_SOC_BIAS_PREPARE:
  65. if (!mclk_on)
  66. ret = clk_enable(mclk);
  67. if (ret == 0)
  68. mclk_on = 1;
  69. break;
  70. case SND_SOC_BIAS_OFF:
  71. case SND_SOC_BIAS_STANDBY:
  72. if (mclk_on)
  73. clk_disable(mclk);
  74. mclk_on = 0;
  75. break;
  76. }
  77. return ret;
  78. }
  79. static const struct snd_soc_dapm_widget at91sam9g20ek_dapm_widgets[] = {
  80. SND_SOC_DAPM_MIC("Int Mic", NULL),
  81. SND_SOC_DAPM_SPK("Ext Spk", NULL),
  82. };
  83. static const struct snd_soc_dapm_route intercon[] = {
  84. /* speaker connected to LHPOUT */
  85. {"Ext Spk", NULL, "LHPOUT"},
  86. /* mic is connected to Mic Jack, with WM8731 Mic Bias */
  87. {"MICIN", NULL, "Mic Bias"},
  88. {"Mic Bias", NULL, "Int Mic"},
  89. };
  90. /*
  91. * Logic for a wm8731 as connected on a at91sam9g20ek board.
  92. */
  93. static int at91sam9g20ek_wm8731_init(struct snd_soc_pcm_runtime *rtd)
  94. {
  95. struct snd_soc_codec *codec = rtd->codec;
  96. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  97. struct snd_soc_dapm_context *dapm = &codec->dapm;
  98. int ret;
  99. printk(KERN_DEBUG
  100. "at91sam9g20ek_wm8731 "
  101. ": at91sam9g20ek_wm8731_init() called\n");
  102. ret = snd_soc_dai_set_sysclk(codec_dai, WM8731_SYSCLK_MCLK,
  103. MCLK_RATE, SND_SOC_CLOCK_IN);
  104. if (ret < 0) {
  105. printk(KERN_ERR "Failed to set WM8731 SYSCLK: %d\n", ret);
  106. return ret;
  107. }
  108. /* not connected */
  109. snd_soc_dapm_nc_pin(dapm, "RLINEIN");
  110. snd_soc_dapm_nc_pin(dapm, "LLINEIN");
  111. #ifndef ENABLE_MIC_INPUT
  112. snd_soc_dapm_nc_pin(&rtd->card->dapm, "Int Mic");
  113. #endif
  114. return 0;
  115. }
  116. static struct snd_soc_dai_link at91sam9g20ek_dai = {
  117. .name = "WM8731",
  118. .stream_name = "WM8731 PCM",
  119. .cpu_dai_name = "at91rm9200_ssc.0",
  120. .codec_dai_name = "wm8731-hifi",
  121. .init = at91sam9g20ek_wm8731_init,
  122. .platform_name = "at91rm9200_ssc.0",
  123. .codec_name = "wm8731.0-001b",
  124. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
  125. SND_SOC_DAIFMT_CBM_CFM,
  126. };
  127. static struct snd_soc_card snd_soc_at91sam9g20ek = {
  128. .name = "AT91SAMG20-EK",
  129. .owner = THIS_MODULE,
  130. .dai_link = &at91sam9g20ek_dai,
  131. .num_links = 1,
  132. .set_bias_level = at91sam9g20ek_set_bias_level,
  133. .dapm_widgets = at91sam9g20ek_dapm_widgets,
  134. .num_dapm_widgets = ARRAY_SIZE(at91sam9g20ek_dapm_widgets),
  135. .dapm_routes = intercon,
  136. .num_dapm_routes = ARRAY_SIZE(intercon),
  137. };
  138. static int at91sam9g20ek_audio_probe(struct platform_device *pdev)
  139. {
  140. struct device_node *np = pdev->dev.of_node;
  141. struct device_node *codec_np, *cpu_np;
  142. struct clk *pllb;
  143. struct snd_soc_card *card = &snd_soc_at91sam9g20ek;
  144. int ret;
  145. if (!np) {
  146. return -ENODEV;
  147. }
  148. ret = atmel_ssc_set_audio(0);
  149. if (ret) {
  150. dev_err(&pdev->dev, "ssc channel is not valid\n");
  151. return -EINVAL;
  152. }
  153. /*
  154. * Codec MCLK is supplied by PCK0 - set it up.
  155. */
  156. mclk = clk_get(NULL, "pck0");
  157. if (IS_ERR(mclk)) {
  158. printk(KERN_ERR "ASoC: Failed to get MCLK\n");
  159. ret = PTR_ERR(mclk);
  160. goto err;
  161. }
  162. pllb = clk_get(NULL, "pllb");
  163. if (IS_ERR(pllb)) {
  164. printk(KERN_ERR "ASoC: Failed to get PLLB\n");
  165. ret = PTR_ERR(pllb);
  166. goto err_mclk;
  167. }
  168. ret = clk_set_parent(mclk, pllb);
  169. clk_put(pllb);
  170. if (ret != 0) {
  171. printk(KERN_ERR "ASoC: Failed to set MCLK parent\n");
  172. goto err_mclk;
  173. }
  174. clk_set_rate(mclk, MCLK_RATE);
  175. card->dev = &pdev->dev;
  176. /* Parse device node info */
  177. ret = snd_soc_of_parse_card_name(card, "atmel,model");
  178. if (ret)
  179. goto err;
  180. ret = snd_soc_of_parse_audio_routing(card,
  181. "atmel,audio-routing");
  182. if (ret)
  183. goto err;
  184. /* Parse codec info */
  185. at91sam9g20ek_dai.codec_name = NULL;
  186. codec_np = of_parse_phandle(np, "atmel,audio-codec", 0);
  187. if (!codec_np) {
  188. dev_err(&pdev->dev, "codec info missing\n");
  189. return -EINVAL;
  190. }
  191. at91sam9g20ek_dai.codec_of_node = codec_np;
  192. /* Parse dai and platform info */
  193. at91sam9g20ek_dai.cpu_dai_name = NULL;
  194. at91sam9g20ek_dai.platform_name = NULL;
  195. cpu_np = of_parse_phandle(np, "atmel,ssc-controller", 0);
  196. if (!cpu_np) {
  197. dev_err(&pdev->dev, "dai and pcm info missing\n");
  198. return -EINVAL;
  199. }
  200. at91sam9g20ek_dai.cpu_of_node = cpu_np;
  201. at91sam9g20ek_dai.platform_of_node = cpu_np;
  202. of_node_put(codec_np);
  203. of_node_put(cpu_np);
  204. ret = snd_soc_register_card(card);
  205. if (ret) {
  206. printk(KERN_ERR "ASoC: snd_soc_register_card() failed\n");
  207. }
  208. return ret;
  209. err_mclk:
  210. clk_put(mclk);
  211. mclk = NULL;
  212. err:
  213. atmel_ssc_put_audio(0);
  214. return ret;
  215. }
  216. static int at91sam9g20ek_audio_remove(struct platform_device *pdev)
  217. {
  218. struct snd_soc_card *card = platform_get_drvdata(pdev);
  219. clk_disable(mclk);
  220. mclk = NULL;
  221. snd_soc_unregister_card(card);
  222. atmel_ssc_put_audio(0);
  223. return 0;
  224. }
  225. #ifdef CONFIG_OF
  226. static const struct of_device_id at91sam9g20ek_wm8731_dt_ids[] = {
  227. { .compatible = "atmel,at91sam9g20ek-wm8731-audio", },
  228. { }
  229. };
  230. MODULE_DEVICE_TABLE(of, at91sam9g20ek_wm8731_dt_ids);
  231. #endif
  232. static struct platform_driver at91sam9g20ek_audio_driver = {
  233. .driver = {
  234. .name = "at91sam9g20ek-audio",
  235. .of_match_table = of_match_ptr(at91sam9g20ek_wm8731_dt_ids),
  236. },
  237. .probe = at91sam9g20ek_audio_probe,
  238. .remove = at91sam9g20ek_audio_remove,
  239. };
  240. module_platform_driver(at91sam9g20ek_audio_driver);
  241. /* Module information */
  242. MODULE_AUTHOR("Sedji Gaouaou <sedji.gaouaou@atmel.com>");
  243. MODULE_DESCRIPTION("ALSA SoC AT91SAM9G20EK_WM8731");
  244. MODULE_ALIAS("platform:at91sam9g20ek-audio");
  245. MODULE_LICENSE("GPL");