littlemill.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Littlemill 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/wm8994.h"
  17. static int sample_rate = 44100;
  18. static int littlemill_set_bias_level(struct snd_soc_card *card,
  19. struct snd_soc_dapm_context *dapm,
  20. enum snd_soc_bias_level level)
  21. {
  22. struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
  23. int ret;
  24. if (dapm->dev != aif1_dai->dev)
  25. return 0;
  26. switch (level) {
  27. case SND_SOC_BIAS_PREPARE:
  28. /*
  29. * If we've not already clocked things via hw_params()
  30. * then do so now, otherwise these are noops.
  31. */
  32. if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
  33. ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1,
  34. WM8994_FLL_SRC_MCLK2, 32768,
  35. sample_rate * 512);
  36. if (ret < 0) {
  37. pr_err("Failed to start FLL: %d\n", ret);
  38. return ret;
  39. }
  40. ret = snd_soc_dai_set_sysclk(aif1_dai,
  41. WM8994_SYSCLK_FLL1,
  42. sample_rate * 512,
  43. SND_SOC_CLOCK_IN);
  44. if (ret < 0) {
  45. pr_err("Failed to set SYSCLK: %d\n", ret);
  46. return ret;
  47. }
  48. }
  49. break;
  50. default:
  51. break;
  52. }
  53. return 0;
  54. }
  55. static int littlemill_set_bias_level_post(struct snd_soc_card *card,
  56. struct snd_soc_dapm_context *dapm,
  57. enum snd_soc_bias_level level)
  58. {
  59. struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
  60. int ret;
  61. if (dapm->dev != aif1_dai->dev)
  62. return 0;
  63. switch (level) {
  64. case SND_SOC_BIAS_STANDBY:
  65. ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
  66. 32768, SND_SOC_CLOCK_IN);
  67. if (ret < 0) {
  68. pr_err("Failed to switch away from FLL1: %d\n", ret);
  69. return ret;
  70. }
  71. ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1,
  72. 0, 0, 0);
  73. if (ret < 0) {
  74. pr_err("Failed to stop FLL1: %d\n", ret);
  75. return ret;
  76. }
  77. break;
  78. default:
  79. break;
  80. }
  81. dapm->bias_level = level;
  82. return 0;
  83. }
  84. static int littlemill_hw_params(struct snd_pcm_substream *substream,
  85. struct snd_pcm_hw_params *params)
  86. {
  87. struct snd_soc_pcm_runtime *rtd = substream->private_data;
  88. struct snd_soc_dai *codec_dai = rtd->codec_dai;
  89. int ret;
  90. sample_rate = params_rate(params);
  91. ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1,
  92. WM8994_FLL_SRC_MCLK2, 32768,
  93. sample_rate * 512);
  94. if (ret < 0) {
  95. pr_err("Failed to start FLL: %d\n", ret);
  96. return ret;
  97. }
  98. ret = snd_soc_dai_set_sysclk(codec_dai,
  99. WM8994_SYSCLK_FLL1,
  100. sample_rate * 512,
  101. SND_SOC_CLOCK_IN);
  102. if (ret < 0) {
  103. pr_err("Failed to set SYSCLK: %d\n", ret);
  104. return ret;
  105. }
  106. return 0;
  107. }
  108. static struct snd_soc_ops littlemill_ops = {
  109. .hw_params = littlemill_hw_params,
  110. };
  111. static const struct snd_soc_pcm_stream baseband_params = {
  112. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  113. .rate_min = 8000,
  114. .rate_max = 8000,
  115. .channels_min = 2,
  116. .channels_max = 2,
  117. };
  118. static struct snd_soc_dai_link littlemill_dai[] = {
  119. {
  120. .name = "CPU",
  121. .stream_name = "CPU",
  122. .cpu_dai_name = "samsung-i2s.0",
  123. .codec_dai_name = "wm8994-aif1",
  124. .platform_name = "samsung-i2s.0",
  125. .codec_name = "wm8994-codec",
  126. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  127. | SND_SOC_DAIFMT_CBM_CFM,
  128. .ops = &littlemill_ops,
  129. },
  130. {
  131. .name = "Baseband",
  132. .stream_name = "Baseband",
  133. .cpu_dai_name = "wm8994-aif2",
  134. .codec_dai_name = "wm1250-ev1",
  135. .codec_name = "wm1250-ev1.1-0027",
  136. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  137. | SND_SOC_DAIFMT_CBM_CFM,
  138. .ignore_suspend = 1,
  139. .params = &baseband_params,
  140. },
  141. };
  142. static int bbclk_ev(struct snd_soc_dapm_widget *w,
  143. struct snd_kcontrol *kcontrol, int event)
  144. {
  145. struct snd_soc_card *card = w->dapm->card;
  146. struct snd_soc_dai *aif2_dai = card->rtd[1].cpu_dai;
  147. int ret;
  148. switch (event) {
  149. case SND_SOC_DAPM_PRE_PMU:
  150. ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2,
  151. WM8994_FLL_SRC_BCLK, 64 * 8000,
  152. 8000 * 256);
  153. if (ret < 0) {
  154. pr_err("Failed to start FLL: %d\n", ret);
  155. return ret;
  156. }
  157. ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_FLL2,
  158. 8000 * 256,
  159. SND_SOC_CLOCK_IN);
  160. if (ret < 0) {
  161. pr_err("Failed to set SYSCLK: %d\n", ret);
  162. return ret;
  163. }
  164. break;
  165. case SND_SOC_DAPM_POST_PMD:
  166. ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2,
  167. 32768, SND_SOC_CLOCK_IN);
  168. if (ret < 0) {
  169. pr_err("Failed to switch away from FLL2: %d\n", ret);
  170. return ret;
  171. }
  172. ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2,
  173. 0, 0, 0);
  174. if (ret < 0) {
  175. pr_err("Failed to stop FLL2: %d\n", ret);
  176. return ret;
  177. }
  178. break;
  179. default:
  180. return -EINVAL;
  181. }
  182. return 0;
  183. }
  184. static const struct snd_kcontrol_new controls[] = {
  185. SOC_DAPM_PIN_SWITCH("WM1250 Input"),
  186. SOC_DAPM_PIN_SWITCH("WM1250 Output"),
  187. };
  188. static struct snd_soc_dapm_widget widgets[] = {
  189. SND_SOC_DAPM_HP("Headphone", NULL),
  190. SND_SOC_DAPM_MIC("AMIC", NULL),
  191. SND_SOC_DAPM_MIC("DMIC", NULL),
  192. SND_SOC_DAPM_SUPPLY_S("Baseband Clock", -1, SND_SOC_NOPM, 0, 0,
  193. bbclk_ev,
  194. SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
  195. };
  196. static struct snd_soc_dapm_route audio_paths[] = {
  197. { "Headphone", NULL, "HPOUT1L" },
  198. { "Headphone", NULL, "HPOUT1R" },
  199. { "AMIC", NULL, "MICBIAS1" }, /* Default for AMICBIAS jumper */
  200. { "IN1LN", NULL, "AMIC" },
  201. { "DMIC", NULL, "MICBIAS2" }, /* Default for DMICBIAS jumper */
  202. { "DMIC1DAT", NULL, "DMIC" },
  203. { "DMIC2DAT", NULL, "DMIC" },
  204. { "AIF2CLK", NULL, "Baseband Clock" },
  205. };
  206. static struct snd_soc_jack littlemill_headset;
  207. static int littlemill_late_probe(struct snd_soc_card *card)
  208. {
  209. struct snd_soc_codec *codec = card->rtd[0].codec;
  210. struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
  211. struct snd_soc_dai *aif2_dai = card->rtd[1].cpu_dai;
  212. int ret;
  213. ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
  214. 32768, SND_SOC_CLOCK_IN);
  215. if (ret < 0)
  216. return ret;
  217. ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2,
  218. 32768, SND_SOC_CLOCK_IN);
  219. if (ret < 0)
  220. return ret;
  221. ret = snd_soc_jack_new(codec, "Headset",
  222. SND_JACK_HEADSET | SND_JACK_MECHANICAL |
  223. SND_JACK_BTN_0 | SND_JACK_BTN_1 |
  224. SND_JACK_BTN_2 | SND_JACK_BTN_3 |
  225. SND_JACK_BTN_4 | SND_JACK_BTN_5,
  226. &littlemill_headset);
  227. if (ret)
  228. return ret;
  229. /* This will check device compatibility itself */
  230. wm8958_mic_detect(codec, &littlemill_headset, NULL, NULL, NULL, NULL);
  231. /* As will this */
  232. wm8994_mic_detect(codec, &littlemill_headset, 1);
  233. return 0;
  234. }
  235. static struct snd_soc_card littlemill = {
  236. .name = "Littlemill",
  237. .owner = THIS_MODULE,
  238. .dai_link = littlemill_dai,
  239. .num_links = ARRAY_SIZE(littlemill_dai),
  240. .set_bias_level = littlemill_set_bias_level,
  241. .set_bias_level_post = littlemill_set_bias_level_post,
  242. .controls = controls,
  243. .num_controls = ARRAY_SIZE(controls),
  244. .dapm_widgets = widgets,
  245. .num_dapm_widgets = ARRAY_SIZE(widgets),
  246. .dapm_routes = audio_paths,
  247. .num_dapm_routes = ARRAY_SIZE(audio_paths),
  248. .late_probe = littlemill_late_probe,
  249. };
  250. static int littlemill_probe(struct platform_device *pdev)
  251. {
  252. struct snd_soc_card *card = &littlemill;
  253. int ret;
  254. card->dev = &pdev->dev;
  255. ret = snd_soc_register_card(card);
  256. if (ret) {
  257. dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
  258. ret);
  259. return ret;
  260. }
  261. return 0;
  262. }
  263. static int littlemill_remove(struct platform_device *pdev)
  264. {
  265. struct snd_soc_card *card = platform_get_drvdata(pdev);
  266. snd_soc_unregister_card(card);
  267. return 0;
  268. }
  269. static struct platform_driver littlemill_driver = {
  270. .driver = {
  271. .name = "littlemill",
  272. .owner = THIS_MODULE,
  273. .pm = &snd_soc_pm_ops,
  274. },
  275. .probe = littlemill_probe,
  276. .remove = littlemill_remove,
  277. };
  278. module_platform_driver(littlemill_driver);
  279. MODULE_DESCRIPTION("Littlemill audio support");
  280. MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
  281. MODULE_LICENSE("GPL");
  282. MODULE_ALIAS("platform:littlemill");