littlemill.c 8.2 KB

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