speyside.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Speyside 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 <linux/module.h>
  16. #include "../codecs/wm8996.h"
  17. #include "../codecs/wm9081.h"
  18. #define WM8996_HPSEL_GPIO 214
  19. static int speyside_set_bias_level(struct snd_soc_card *card,
  20. struct snd_soc_dapm_context *dapm,
  21. enum snd_soc_bias_level level)
  22. {
  23. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  24. int ret;
  25. if (dapm->dev != codec_dai->dev)
  26. return 0;
  27. switch (level) {
  28. case SND_SOC_BIAS_STANDBY:
  29. ret = snd_soc_dai_set_sysclk(codec_dai, WM8996_SYSCLK_MCLK2,
  30. 32768, SND_SOC_CLOCK_IN);
  31. if (ret < 0)
  32. return ret;
  33. ret = snd_soc_dai_set_pll(codec_dai, WM8996_FLL_MCLK2,
  34. 0, 0, 0);
  35. if (ret < 0) {
  36. pr_err("Failed to stop FLL\n");
  37. return ret;
  38. }
  39. break;
  40. default:
  41. break;
  42. }
  43. return 0;
  44. }
  45. static int speyside_set_bias_level_post(struct snd_soc_card *card,
  46. struct snd_soc_dapm_context *dapm,
  47. enum snd_soc_bias_level level)
  48. {
  49. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  50. int ret;
  51. if (dapm->dev != codec_dai->dev)
  52. return 0;
  53. switch (level) {
  54. case SND_SOC_BIAS_PREPARE:
  55. if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
  56. ret = snd_soc_dai_set_pll(codec_dai, 0,
  57. WM8996_FLL_MCLK2,
  58. 32768, 48000 * 256);
  59. if (ret < 0) {
  60. pr_err("Failed to start FLL\n");
  61. return ret;
  62. }
  63. ret = snd_soc_dai_set_sysclk(codec_dai,
  64. WM8996_SYSCLK_FLL,
  65. 48000 * 256,
  66. SND_SOC_CLOCK_IN);
  67. if (ret < 0)
  68. return ret;
  69. }
  70. break;
  71. default:
  72. break;
  73. }
  74. card->dapm.bias_level = level;
  75. return 0;
  76. }
  77. static int speyside_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. return 0;
  95. }
  96. static struct snd_soc_ops speyside_ops = {
  97. .hw_params = speyside_hw_params,
  98. };
  99. static struct snd_soc_jack speyside_headset;
  100. /* Headset jack detection DAPM pins */
  101. static struct snd_soc_jack_pin speyside_headset_pins[] = {
  102. {
  103. .pin = "Headset Mic",
  104. .mask = SND_JACK_MICROPHONE,
  105. },
  106. };
  107. /* Default the headphone selection to active high */
  108. static int speyside_jack_polarity;
  109. static int speyside_get_micbias(struct snd_soc_dapm_widget *source,
  110. struct snd_soc_dapm_widget *sink)
  111. {
  112. if (speyside_jack_polarity && (strcmp(source->name, "MICB1") == 0))
  113. return 1;
  114. if (!speyside_jack_polarity && (strcmp(source->name, "MICB2") == 0))
  115. return 1;
  116. return 0;
  117. }
  118. static void speyside_set_polarity(struct snd_soc_codec *codec,
  119. int polarity)
  120. {
  121. speyside_jack_polarity = !polarity;
  122. gpio_direction_output(WM8996_HPSEL_GPIO, speyside_jack_polarity);
  123. /* Re-run DAPM to make sure we're using the correct mic bias */
  124. snd_soc_dapm_sync(&codec->dapm);
  125. }
  126. static int speyside_wm8996_init(struct snd_soc_pcm_runtime *rtd)
  127. {
  128. struct snd_soc_dai *dai = rtd->codec_dai;
  129. struct snd_soc_codec *codec = rtd->codec;
  130. int ret;
  131. ret = snd_soc_dai_set_sysclk(dai, WM8996_SYSCLK_MCLK2, 32768, 0);
  132. if (ret < 0)
  133. return ret;
  134. ret = gpio_request(WM8996_HPSEL_GPIO, "HP_SEL");
  135. if (ret != 0)
  136. pr_err("Failed to request HP_SEL GPIO: %d\n", ret);
  137. gpio_direction_output(WM8996_HPSEL_GPIO, speyside_jack_polarity);
  138. ret = snd_soc_jack_new(codec, "Headset",
  139. SND_JACK_LINEOUT | SND_JACK_HEADSET |
  140. SND_JACK_BTN_0,
  141. &speyside_headset);
  142. if (ret)
  143. return ret;
  144. ret = snd_soc_jack_add_pins(&speyside_headset,
  145. ARRAY_SIZE(speyside_headset_pins),
  146. speyside_headset_pins);
  147. if (ret)
  148. return ret;
  149. wm8996_detect(codec, &speyside_headset, speyside_set_polarity);
  150. return 0;
  151. }
  152. static int speyside_late_probe(struct snd_soc_card *card)
  153. {
  154. snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
  155. snd_soc_dapm_ignore_suspend(&card->dapm, "Headset Mic");
  156. snd_soc_dapm_ignore_suspend(&card->dapm, "Main AMIC");
  157. snd_soc_dapm_ignore_suspend(&card->dapm, "Main DMIC");
  158. snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
  159. snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Output");
  160. snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Input");
  161. return 0;
  162. }
  163. static struct snd_soc_dai_link speyside_dai[] = {
  164. {
  165. .name = "CPU",
  166. .stream_name = "CPU",
  167. .cpu_dai_name = "samsung-i2s.0",
  168. .codec_dai_name = "wm8996-aif1",
  169. .platform_name = "samsung-audio",
  170. .codec_name = "wm8996.1-001a",
  171. .init = speyside_wm8996_init,
  172. .ops = &speyside_ops,
  173. },
  174. {
  175. .name = "Baseband",
  176. .stream_name = "Baseband",
  177. .cpu_dai_name = "wm8996-aif2",
  178. .codec_dai_name = "wm1250-ev1",
  179. .codec_name = "wm1250-ev1.1-0027",
  180. .ops = &speyside_ops,
  181. .ignore_suspend = 1,
  182. },
  183. };
  184. static int speyside_wm9081_init(struct snd_soc_dapm_context *dapm)
  185. {
  186. snd_soc_dapm_nc_pin(dapm, "LINEOUT");
  187. /* At any time the WM9081 is active it will have this clock */
  188. return snd_soc_codec_set_sysclk(dapm->codec, WM9081_SYSCLK_MCLK, 0,
  189. 48000 * 256, 0);
  190. }
  191. static struct snd_soc_aux_dev speyside_aux_dev[] = {
  192. {
  193. .name = "wm9081",
  194. .codec_name = "wm9081.1-006c",
  195. .init = speyside_wm9081_init,
  196. },
  197. };
  198. static struct snd_soc_codec_conf speyside_codec_conf[] = {
  199. {
  200. .dev_name = "wm9081.1-006c",
  201. .name_prefix = "Sub",
  202. },
  203. };
  204. static const struct snd_kcontrol_new controls[] = {
  205. SOC_DAPM_PIN_SWITCH("Main Speaker"),
  206. SOC_DAPM_PIN_SWITCH("Main DMIC"),
  207. SOC_DAPM_PIN_SWITCH("Main AMIC"),
  208. SOC_DAPM_PIN_SWITCH("WM1250 Input"),
  209. SOC_DAPM_PIN_SWITCH("WM1250 Output"),
  210. SOC_DAPM_PIN_SWITCH("Headphone"),
  211. };
  212. static struct snd_soc_dapm_widget widgets[] = {
  213. SND_SOC_DAPM_HP("Headphone", NULL),
  214. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  215. SND_SOC_DAPM_SPK("Main Speaker", NULL),
  216. SND_SOC_DAPM_MIC("Main AMIC", NULL),
  217. SND_SOC_DAPM_MIC("Main DMIC", NULL),
  218. };
  219. static struct snd_soc_dapm_route audio_paths[] = {
  220. { "IN1RN", NULL, "MICB1" },
  221. { "IN1RP", NULL, "MICB1" },
  222. { "IN1RN", NULL, "MICB2" },
  223. { "IN1RP", NULL, "MICB2" },
  224. { "MICB1", NULL, "Headset Mic", speyside_get_micbias },
  225. { "MICB2", NULL, "Headset Mic", speyside_get_micbias },
  226. { "IN1LP", NULL, "MICB2" },
  227. { "IN1RN", NULL, "MICB1" },
  228. { "MICB2", NULL, "Main AMIC" },
  229. { "DMIC1DAT", NULL, "MICB1" },
  230. { "DMIC2DAT", NULL, "MICB1" },
  231. { "MICB1", NULL, "Main DMIC" },
  232. { "Headphone", NULL, "HPOUT1L" },
  233. { "Headphone", NULL, "HPOUT1R" },
  234. { "Sub IN1", NULL, "HPOUT2L" },
  235. { "Sub IN2", NULL, "HPOUT2R" },
  236. { "Main Speaker", NULL, "Sub SPKN" },
  237. { "Main Speaker", NULL, "Sub SPKP" },
  238. { "Main Speaker", NULL, "SPKDAT" },
  239. };
  240. static struct snd_soc_card speyside = {
  241. .name = "Speyside",
  242. .dai_link = speyside_dai,
  243. .num_links = ARRAY_SIZE(speyside_dai),
  244. .aux_dev = speyside_aux_dev,
  245. .num_aux_devs = ARRAY_SIZE(speyside_aux_dev),
  246. .codec_conf = speyside_codec_conf,
  247. .num_configs = ARRAY_SIZE(speyside_codec_conf),
  248. .set_bias_level = speyside_set_bias_level,
  249. .set_bias_level_post = speyside_set_bias_level_post,
  250. .controls = controls,
  251. .num_controls = ARRAY_SIZE(controls),
  252. .dapm_widgets = widgets,
  253. .num_dapm_widgets = ARRAY_SIZE(widgets),
  254. .dapm_routes = audio_paths,
  255. .num_dapm_routes = ARRAY_SIZE(audio_paths),
  256. .late_probe = speyside_late_probe,
  257. };
  258. static __devinit int speyside_probe(struct platform_device *pdev)
  259. {
  260. struct snd_soc_card *card = &speyside;
  261. int ret;
  262. card->dev = &pdev->dev;
  263. ret = snd_soc_register_card(card);
  264. if (ret) {
  265. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  266. ret);
  267. return ret;
  268. }
  269. return 0;
  270. }
  271. static int __devexit speyside_remove(struct platform_device *pdev)
  272. {
  273. struct snd_soc_card *card = platform_get_drvdata(pdev);
  274. snd_soc_unregister_card(card);
  275. return 0;
  276. }
  277. static struct platform_driver speyside_driver = {
  278. .driver = {
  279. .name = "speyside",
  280. .owner = THIS_MODULE,
  281. .pm = &snd_soc_pm_ops,
  282. },
  283. .probe = speyside_probe,
  284. .remove = __devexit_p(speyside_remove),
  285. };
  286. static int __init speyside_audio_init(void)
  287. {
  288. return platform_driver_register(&speyside_driver);
  289. }
  290. module_init(speyside_audio_init);
  291. static void __exit speyside_audio_exit(void)
  292. {
  293. platform_driver_unregister(&speyside_driver);
  294. }
  295. module_exit(speyside_audio_exit);
  296. MODULE_DESCRIPTION("Speyside audio support");
  297. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  298. MODULE_LICENSE("GPL");
  299. MODULE_ALIAS("platform:speyside");