speyside_wm8962.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Speyside with WM8962 audio support
  3. *
  4. * Copyright 2011 Wolfson Microelectronics
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. */
  11. #include <sound/soc.h>
  12. #include <sound/soc-dapm.h>
  13. #include <sound/jack.h>
  14. #include <linux/gpio.h>
  15. #include "../codecs/wm8962.h"
  16. static int sample_rate = 44100;
  17. static int speyside_wm8962_set_bias_level(struct snd_soc_card *card,
  18. struct snd_soc_dapm_context *dapm,
  19. enum snd_soc_bias_level level)
  20. {
  21. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  22. int ret;
  23. if (dapm->dev != codec_dai->dev)
  24. return 0;
  25. switch (level) {
  26. case SND_SOC_BIAS_PREPARE:
  27. if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
  28. ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
  29. WM8962_FLL_MCLK, 32768,
  30. sample_rate * 512);
  31. if (ret < 0)
  32. pr_err("Failed to start FLL: %d\n", ret);
  33. ret = snd_soc_dai_set_sysclk(codec_dai,
  34. WM8962_SYSCLK_FLL,
  35. sample_rate * 512,
  36. SND_SOC_CLOCK_IN);
  37. if (ret < 0) {
  38. pr_err("Failed to set SYSCLK: %d\n", ret);
  39. return ret;
  40. }
  41. }
  42. break;
  43. default:
  44. break;
  45. }
  46. return 0;
  47. }
  48. static int speyside_wm8962_set_bias_level_post(struct snd_soc_card *card,
  49. struct snd_soc_dapm_context *dapm,
  50. enum snd_soc_bias_level level)
  51. {
  52. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  53. int ret;
  54. if (dapm->dev != codec_dai->dev)
  55. return 0;
  56. switch (level) {
  57. case SND_SOC_BIAS_STANDBY:
  58. ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
  59. 32768, SND_SOC_CLOCK_IN);
  60. if (ret < 0) {
  61. pr_err("Failed to switch away from FLL: %d\n", ret);
  62. return ret;
  63. }
  64. ret = snd_soc_dai_set_pll(codec_dai, WM8962_FLL,
  65. 0, 0, 0);
  66. if (ret < 0) {
  67. pr_err("Failed to stop FLL: %d\n", ret);
  68. return ret;
  69. }
  70. break;
  71. default:
  72. break;
  73. }
  74. dapm->bias_level = level;
  75. return 0;
  76. }
  77. static int speyside_wm8962_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 *cpu_dai = rtd->cpu_dai;
  82. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  83. int ret;
  84. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
  85. | SND_SOC_DAIFMT_NB_NF
  86. | SND_SOC_DAIFMT_CBM_CFM);
  87. if (ret < 0)
  88. return ret;
  89. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S
  90. | SND_SOC_DAIFMT_NB_NF
  91. | SND_SOC_DAIFMT_CBM_CFM);
  92. if (ret < 0)
  93. return ret;
  94. sample_rate = params_rate(params);
  95. return 0;
  96. }
  97. static struct snd_soc_ops speyside_wm8962_ops = {
  98. .hw_params = speyside_wm8962_hw_params,
  99. };
  100. static struct snd_soc_dai_link speyside_wm8962_dai[] = {
  101. {
  102. .name = "CPU",
  103. .stream_name = "CPU",
  104. .cpu_dai_name = "samsung-i2s.0",
  105. .codec_dai_name = "wm8962",
  106. .platform_name = "samsung-audio",
  107. .codec_name = "wm8962.1-001a",
  108. .ops = &speyside_wm8962_ops,
  109. },
  110. };
  111. static const struct snd_kcontrol_new controls[] = {
  112. SOC_DAPM_PIN_SWITCH("Main Speaker"),
  113. };
  114. static struct snd_soc_dapm_widget widgets[] = {
  115. SND_SOC_DAPM_HP("Headphone", NULL),
  116. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  117. SND_SOC_DAPM_MIC("DMIC", NULL),
  118. SND_SOC_DAPM_SPK("Main Speaker", NULL),
  119. };
  120. static struct snd_soc_dapm_route audio_paths[] = {
  121. { "Headphone", NULL, "HPOUTL" },
  122. { "Headphone", NULL, "HPOUTR" },
  123. { "Main Speaker", NULL, "SPKOUTL" },
  124. { "Main Speaker", NULL, "SPKOUTR" },
  125. { "MICBIAS", NULL, "Headset Mic" },
  126. { "IN4L", NULL, "MICBIAS" },
  127. { "IN4R", NULL, "MICBIAS" },
  128. { "MICBIAS", NULL, "DMIC" },
  129. { "DMICDAT", NULL, "MICBIAS" },
  130. };
  131. static struct snd_soc_jack speyside_wm8962_headset;
  132. /* Headset jack detection DAPM pins */
  133. static struct snd_soc_jack_pin speyside_wm8962_headset_pins[] = {
  134. {
  135. .pin = "Headset Mic",
  136. .mask = SND_JACK_MICROPHONE,
  137. },
  138. {
  139. .pin = "Headphone",
  140. .mask = SND_JACK_MICROPHONE,
  141. },
  142. };
  143. static int speyside_wm8962_late_probe(struct snd_soc_card *card)
  144. {
  145. struct snd_soc_codec *codec = card->rtd[0].codec;
  146. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  147. int ret;
  148. ret = snd_soc_dai_set_sysclk(codec_dai, WM8962_SYSCLK_MCLK,
  149. 32768, SND_SOC_CLOCK_IN);
  150. if (ret < 0)
  151. return ret;
  152. ret = snd_soc_jack_new(codec, "Headset",
  153. SND_JACK_HEADSET | SND_JACK_BTN_0,
  154. &speyside_wm8962_headset);
  155. if (ret)
  156. return ret;
  157. ret = snd_soc_jack_add_pins(&speyside_wm8962_headset,
  158. ARRAY_SIZE(speyside_wm8962_headset_pins),
  159. speyside_wm8962_headset_pins);
  160. if (ret)
  161. return ret;
  162. wm8962_mic_detect(codec, &speyside_wm8962_headset);
  163. return 0;
  164. }
  165. static struct snd_soc_card speyside_wm8962 = {
  166. .name = "Speyside WM8962",
  167. .dai_link = speyside_wm8962_dai,
  168. .num_links = ARRAY_SIZE(speyside_wm8962_dai),
  169. .set_bias_level = speyside_wm8962_set_bias_level,
  170. .set_bias_level_post = speyside_wm8962_set_bias_level_post,
  171. .controls = controls,
  172. .num_controls = ARRAY_SIZE(controls),
  173. .dapm_widgets = widgets,
  174. .num_dapm_widgets = ARRAY_SIZE(widgets),
  175. .dapm_routes = audio_paths,
  176. .num_dapm_routes = ARRAY_SIZE(audio_paths),
  177. .late_probe = speyside_wm8962_late_probe,
  178. };
  179. static __devinit int speyside_wm8962_probe(struct platform_device *pdev)
  180. {
  181. struct snd_soc_card *card = &speyside_wm8962;
  182. int ret;
  183. card->dev = &pdev->dev;
  184. ret = snd_soc_register_card(card);
  185. if (ret) {
  186. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  187. ret);
  188. return ret;
  189. }
  190. return 0;
  191. }
  192. static int __devexit speyside_wm8962_remove(struct platform_device *pdev)
  193. {
  194. struct snd_soc_card *card = platform_get_drvdata(pdev);
  195. snd_soc_unregister_card(card);
  196. return 0;
  197. }
  198. static struct platform_driver speyside_wm8962_driver = {
  199. .driver = {
  200. .name = "speyside-wm8962",
  201. .owner = THIS_MODULE,
  202. .pm = &snd_soc_pm_ops,
  203. },
  204. .probe = speyside_wm8962_probe,
  205. .remove = __devexit_p(speyside_wm8962_remove),
  206. };
  207. static int __init speyside_wm8962_audio_init(void)
  208. {
  209. return platform_driver_register(&speyside_wm8962_driver);
  210. }
  211. module_init(speyside_wm8962_audio_init);
  212. static void __exit speyside_wm8962_audio_exit(void)
  213. {
  214. platform_driver_unregister(&speyside_wm8962_driver);
  215. }
  216. module_exit(speyside_wm8962_audio_exit);
  217. MODULE_DESCRIPTION("Speyside WM8962 audio support");
  218. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  219. MODULE_LICENSE("GPL");
  220. MODULE_ALIAS("platform:speyside-wm8962");