speyside.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. #define MCLK_AUDIO_RATE (512 * 48000)
  20. static int speyside_set_bias_level(struct snd_soc_card *card,
  21. struct snd_soc_dapm_context *dapm,
  22. enum snd_soc_bias_level level)
  23. {
  24. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  25. int ret;
  26. if (dapm->dev != codec_dai->dev)
  27. return 0;
  28. switch (level) {
  29. case SND_SOC_BIAS_STANDBY:
  30. ret = snd_soc_dai_set_sysclk(codec_dai, WM8996_SYSCLK_MCLK2,
  31. 32768, SND_SOC_CLOCK_IN);
  32. if (ret < 0)
  33. return ret;
  34. ret = snd_soc_dai_set_pll(codec_dai, WM8996_FLL_MCLK2,
  35. 0, 0, 0);
  36. if (ret < 0) {
  37. pr_err("Failed to stop FLL\n");
  38. return ret;
  39. }
  40. break;
  41. default:
  42. break;
  43. }
  44. return 0;
  45. }
  46. static int speyside_set_bias_level_post(struct snd_soc_card *card,
  47. struct snd_soc_dapm_context *dapm,
  48. enum snd_soc_bias_level level)
  49. {
  50. struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
  51. int ret;
  52. if (dapm->dev != codec_dai->dev)
  53. return 0;
  54. switch (level) {
  55. case SND_SOC_BIAS_PREPARE:
  56. if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
  57. ret = snd_soc_dai_set_pll(codec_dai, 0,
  58. WM8996_FLL_MCLK2,
  59. 32768, MCLK_AUDIO_RATE);
  60. if (ret < 0) {
  61. pr_err("Failed to start FLL\n");
  62. return ret;
  63. }
  64. ret = snd_soc_dai_set_sysclk(codec_dai,
  65. WM8996_SYSCLK_FLL,
  66. MCLK_AUDIO_RATE,
  67. SND_SOC_CLOCK_IN);
  68. if (ret < 0)
  69. return ret;
  70. }
  71. break;
  72. default:
  73. break;
  74. }
  75. card->dapm.bias_level = level;
  76. return 0;
  77. }
  78. static int speyside_hw_params(struct snd_pcm_substream *substream,
  79. struct snd_pcm_hw_params *params)
  80. {
  81. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  82. struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
  83. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  84. int ret;
  85. ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
  86. | SND_SOC_DAIFMT_NB_NF
  87. | SND_SOC_DAIFMT_CBM_CFM);
  88. if (ret < 0)
  89. return ret;
  90. ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S
  91. | SND_SOC_DAIFMT_NB_NF
  92. | SND_SOC_DAIFMT_CBM_CFM);
  93. if (ret < 0)
  94. return ret;
  95. return 0;
  96. }
  97. static struct snd_soc_ops speyside_ops = {
  98. .hw_params = speyside_hw_params,
  99. };
  100. static struct snd_soc_jack speyside_headset;
  101. /* Headset jack detection DAPM pins */
  102. static struct snd_soc_jack_pin speyside_headset_pins[] = {
  103. {
  104. .pin = "Headset Mic",
  105. .mask = SND_JACK_MICROPHONE,
  106. },
  107. };
  108. /* Default the headphone selection to active high */
  109. static int speyside_jack_polarity;
  110. static int speyside_get_micbias(struct snd_soc_dapm_widget *source,
  111. struct snd_soc_dapm_widget *sink)
  112. {
  113. if (speyside_jack_polarity && (strcmp(source->name, "MICB1") == 0))
  114. return 1;
  115. if (!speyside_jack_polarity && (strcmp(source->name, "MICB2") == 0))
  116. return 1;
  117. return 0;
  118. }
  119. static void speyside_set_polarity(struct snd_soc_codec *codec,
  120. int polarity)
  121. {
  122. speyside_jack_polarity = !polarity;
  123. gpio_direction_output(WM8996_HPSEL_GPIO, speyside_jack_polarity);
  124. /* Re-run DAPM to make sure we're using the correct mic bias */
  125. snd_soc_dapm_sync(&codec->dapm);
  126. }
  127. static int speyside_wm8996_init(struct snd_soc_pcm_runtime *rtd)
  128. {
  129. struct snd_soc_dai *dai = rtd->codec_dai;
  130. struct snd_soc_codec *codec = rtd->codec;
  131. int ret;
  132. ret = snd_soc_dai_set_sysclk(dai, WM8996_SYSCLK_MCLK2, 32768, 0);
  133. if (ret < 0)
  134. return ret;
  135. ret = gpio_request(WM8996_HPSEL_GPIO, "HP_SEL");
  136. if (ret != 0)
  137. pr_err("Failed to request HP_SEL GPIO: %d\n", ret);
  138. gpio_direction_output(WM8996_HPSEL_GPIO, speyside_jack_polarity);
  139. ret = snd_soc_jack_new(codec, "Headset",
  140. SND_JACK_LINEOUT | SND_JACK_HEADSET |
  141. SND_JACK_BTN_0,
  142. &speyside_headset);
  143. if (ret)
  144. return ret;
  145. ret = snd_soc_jack_add_pins(&speyside_headset,
  146. ARRAY_SIZE(speyside_headset_pins),
  147. speyside_headset_pins);
  148. if (ret)
  149. return ret;
  150. wm8996_detect(codec, &speyside_headset, speyside_set_polarity);
  151. return 0;
  152. }
  153. static int speyside_late_probe(struct snd_soc_card *card)
  154. {
  155. snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
  156. snd_soc_dapm_ignore_suspend(&card->dapm, "Headset Mic");
  157. snd_soc_dapm_ignore_suspend(&card->dapm, "Main AMIC");
  158. snd_soc_dapm_ignore_suspend(&card->dapm, "Main DMIC");
  159. snd_soc_dapm_ignore_suspend(&card->dapm, "Main Speaker");
  160. snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Output");
  161. snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Input");
  162. return 0;
  163. }
  164. static struct snd_soc_dai_link speyside_dai[] = {
  165. {
  166. .name = "CPU",
  167. .stream_name = "CPU",
  168. .cpu_dai_name = "samsung-i2s.0",
  169. .codec_dai_name = "wm8996-aif1",
  170. .platform_name = "samsung-audio",
  171. .codec_name = "wm8996.1-001a",
  172. .init = speyside_wm8996_init,
  173. .ops = &speyside_ops,
  174. },
  175. {
  176. .name = "Baseband",
  177. .stream_name = "Baseband",
  178. .cpu_dai_name = "wm8996-aif2",
  179. .codec_dai_name = "wm1250-ev1",
  180. .codec_name = "wm1250-ev1.1-0027",
  181. .ops = &speyside_ops,
  182. .ignore_suspend = 1,
  183. },
  184. };
  185. static int speyside_wm9081_init(struct snd_soc_dapm_context *dapm)
  186. {
  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. MCLK_AUDIO_RATE, 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. .owner = THIS_MODULE,
  243. .dai_link = speyside_dai,
  244. .num_links = ARRAY_SIZE(speyside_dai),
  245. .aux_dev = speyside_aux_dev,
  246. .num_aux_devs = ARRAY_SIZE(speyside_aux_dev),
  247. .codec_conf = speyside_codec_conf,
  248. .num_configs = ARRAY_SIZE(speyside_codec_conf),
  249. .set_bias_level = speyside_set_bias_level,
  250. .set_bias_level_post = speyside_set_bias_level_post,
  251. .controls = controls,
  252. .num_controls = ARRAY_SIZE(controls),
  253. .dapm_widgets = widgets,
  254. .num_dapm_widgets = ARRAY_SIZE(widgets),
  255. .dapm_routes = audio_paths,
  256. .num_dapm_routes = ARRAY_SIZE(audio_paths),
  257. .fully_routed = true,
  258. .late_probe = speyside_late_probe,
  259. };
  260. static __devinit int speyside_probe(struct platform_device *pdev)
  261. {
  262. struct snd_soc_card *card = &speyside;
  263. int ret;
  264. card->dev = &pdev->dev;
  265. ret = snd_soc_register_card(card);
  266. if (ret) {
  267. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  268. ret);
  269. return ret;
  270. }
  271. return 0;
  272. }
  273. static int __devexit speyside_remove(struct platform_device *pdev)
  274. {
  275. struct snd_soc_card *card = platform_get_drvdata(pdev);
  276. snd_soc_unregister_card(card);
  277. return 0;
  278. }
  279. static struct platform_driver speyside_driver = {
  280. .driver = {
  281. .name = "speyside",
  282. .owner = THIS_MODULE,
  283. .pm = &snd_soc_pm_ops,
  284. },
  285. .probe = speyside_probe,
  286. .remove = __devexit_p(speyside_remove),
  287. };
  288. module_platform_driver(speyside_driver);
  289. MODULE_DESCRIPTION("Speyside audio support");
  290. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  291. MODULE_LICENSE("GPL");
  292. MODULE_ALIAS("platform:speyside");