littlemill.c 8.2 KB

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