speyside.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Speyside 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/wm8996.h"
  12. #include "../codecs/wm9081.h"
  13. #define WM8996_HPSEL_GPIO 214
  14. #define MCLK_AUDIO_RATE (512 * 48000)
  15. static int speyside_set_bias_level(struct snd_soc_card *card,
  16. struct snd_soc_dapm_context *dapm,
  17. enum snd_soc_bias_level level)
  18. {
  19. struct snd_soc_pcm_runtime *rtd;
  20. struct snd_soc_dai *codec_dai;
  21. int ret;
  22. rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[1]);
  23. codec_dai = asoc_rtd_to_codec(rtd, 0);
  24. if (dapm->dev != codec_dai->dev)
  25. return 0;
  26. switch (level) {
  27. case SND_SOC_BIAS_STANDBY:
  28. ret = snd_soc_dai_set_sysclk(codec_dai, WM8996_SYSCLK_MCLK2,
  29. 32768, SND_SOC_CLOCK_IN);
  30. if (ret < 0)
  31. return ret;
  32. ret = snd_soc_dai_set_pll(codec_dai, WM8996_FLL_MCLK2,
  33. 0, 0, 0);
  34. if (ret < 0) {
  35. pr_err("Failed to stop FLL\n");
  36. return ret;
  37. }
  38. break;
  39. default:
  40. break;
  41. }
  42. return 0;
  43. }
  44. static int speyside_set_bias_level_post(struct snd_soc_card *card,
  45. struct snd_soc_dapm_context *dapm,
  46. enum snd_soc_bias_level level)
  47. {
  48. struct snd_soc_pcm_runtime *rtd;
  49. struct snd_soc_dai *codec_dai;
  50. int ret;
  51. rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[1]);
  52. codec_dai = asoc_rtd_to_codec(rtd, 0);
  53. if (dapm->dev != codec_dai->dev)
  54. return 0;
  55. switch (level) {
  56. case SND_SOC_BIAS_PREPARE:
  57. if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
  58. ret = snd_soc_dai_set_pll(codec_dai, 0,
  59. WM8996_FLL_MCLK2,
  60. 32768, MCLK_AUDIO_RATE);
  61. if (ret < 0) {
  62. pr_err("Failed to start FLL\n");
  63. return ret;
  64. }
  65. ret = snd_soc_dai_set_sysclk(codec_dai,
  66. WM8996_SYSCLK_FLL,
  67. MCLK_AUDIO_RATE,
  68. SND_SOC_CLOCK_IN);
  69. if (ret < 0)
  70. return ret;
  71. }
  72. break;
  73. default:
  74. break;
  75. }
  76. card->dapm.bias_level = level;
  77. return 0;
  78. }
  79. static struct snd_soc_jack speyside_headset;
  80. /* Headset jack detection DAPM pins */
  81. static struct snd_soc_jack_pin speyside_headset_pins[] = {
  82. {
  83. .pin = "Headset Mic",
  84. .mask = SND_JACK_MICROPHONE,
  85. },
  86. };
  87. /* Default the headphone selection to active high */
  88. static int speyside_jack_polarity;
  89. static int speyside_get_micbias(struct snd_soc_dapm_widget *source,
  90. struct snd_soc_dapm_widget *sink)
  91. {
  92. if (speyside_jack_polarity && (strcmp(source->name, "MICB1") == 0))
  93. return 1;
  94. if (!speyside_jack_polarity && (strcmp(source->name, "MICB2") == 0))
  95. return 1;
  96. return 0;
  97. }
  98. static void speyside_set_polarity(struct snd_soc_component *component,
  99. int polarity)
  100. {
  101. speyside_jack_polarity = !polarity;
  102. gpio_direction_output(WM8996_HPSEL_GPIO, speyside_jack_polarity);
  103. /* Re-run DAPM to make sure we're using the correct mic bias */
  104. snd_soc_dapm_sync(snd_soc_component_get_dapm(component));
  105. }
  106. static int speyside_wm0010_init(struct snd_soc_pcm_runtime *rtd)
  107. {
  108. struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
  109. int ret;
  110. ret = snd_soc_dai_set_sysclk(dai, 0, MCLK_AUDIO_RATE, 0);
  111. if (ret < 0)
  112. return ret;
  113. return 0;
  114. }
  115. static int speyside_wm8996_init(struct snd_soc_pcm_runtime *rtd)
  116. {
  117. struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
  118. struct snd_soc_component *component = dai->component;
  119. int ret;
  120. ret = snd_soc_dai_set_sysclk(dai, WM8996_SYSCLK_MCLK2, 32768, 0);
  121. if (ret < 0)
  122. return ret;
  123. ret = gpio_request(WM8996_HPSEL_GPIO, "HP_SEL");
  124. if (ret != 0)
  125. pr_err("Failed to request HP_SEL GPIO: %d\n", ret);
  126. gpio_direction_output(WM8996_HPSEL_GPIO, speyside_jack_polarity);
  127. ret = snd_soc_card_jack_new_pins(rtd->card, "Headset",
  128. SND_JACK_LINEOUT | SND_JACK_HEADSET |
  129. SND_JACK_BTN_0,
  130. &speyside_headset,
  131. speyside_headset_pins,
  132. ARRAY_SIZE(speyside_headset_pins));
  133. if (ret)
  134. return ret;
  135. wm8996_detect(component, &speyside_headset, speyside_set_polarity);
  136. return 0;
  137. }
  138. static int speyside_late_probe(struct snd_soc_card *card)
  139. {
  140. snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
  141. snd_soc_dapm_ignore_suspend(&card->dapm, "Headset Mic");
  142. snd_soc_dapm_ignore_suspend(&card->dapm, "Main AMIC");
  143. snd_soc_dapm_ignore_suspend(&card->dapm, "Main DMIC");
  144. snd_soc_dapm_ignore_suspend(&card->dapm, "Main Speaker");
  145. snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Output");
  146. snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Input");
  147. return 0;
  148. }
  149. static const struct snd_soc_pcm_stream dsp_codec_params = {
  150. .formats = SNDRV_PCM_FMTBIT_S32_LE,
  151. .rate_min = 48000,
  152. .rate_max = 48000,
  153. .channels_min = 2,
  154. .channels_max = 2,
  155. };
  156. SND_SOC_DAILINK_DEFS(cpu_dsp,
  157. DAILINK_COMP_ARRAY(COMP_CPU("samsung-i2s.0")),
  158. DAILINK_COMP_ARRAY(COMP_CODEC("spi0.0", "wm0010-sdi1")),
  159. DAILINK_COMP_ARRAY(COMP_PLATFORM("samsung-i2s.0")));
  160. SND_SOC_DAILINK_DEFS(dsp_codec,
  161. DAILINK_COMP_ARRAY(COMP_CPU("wm0010-sdi2")),
  162. DAILINK_COMP_ARRAY(COMP_CODEC("wm8996.1-001a", "wm8996-aif1")));
  163. SND_SOC_DAILINK_DEFS(baseband,
  164. DAILINK_COMP_ARRAY(COMP_CPU("wm8996-aif2")),
  165. DAILINK_COMP_ARRAY(COMP_CODEC("wm1250-ev1.1-0027", "wm1250-ev1")));
  166. static struct snd_soc_dai_link speyside_dai[] = {
  167. {
  168. .name = "CPU-DSP",
  169. .stream_name = "CPU-DSP",
  170. .init = speyside_wm0010_init,
  171. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  172. | SND_SOC_DAIFMT_CBM_CFM,
  173. SND_SOC_DAILINK_REG(cpu_dsp),
  174. },
  175. {
  176. .name = "DSP-CODEC",
  177. .stream_name = "DSP-CODEC",
  178. .init = speyside_wm8996_init,
  179. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  180. | SND_SOC_DAIFMT_CBM_CFM,
  181. .params = &dsp_codec_params,
  182. .ignore_suspend = 1,
  183. SND_SOC_DAILINK_REG(dsp_codec),
  184. },
  185. {
  186. .name = "Baseband",
  187. .stream_name = "Baseband",
  188. .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
  189. | SND_SOC_DAIFMT_CBM_CFM,
  190. .ignore_suspend = 1,
  191. SND_SOC_DAILINK_REG(baseband),
  192. },
  193. };
  194. static int speyside_wm9081_init(struct snd_soc_component *component)
  195. {
  196. /* At any time the WM9081 is active it will have this clock */
  197. return snd_soc_component_set_sysclk(component, WM9081_SYSCLK_MCLK, 0,
  198. MCLK_AUDIO_RATE, 0);
  199. }
  200. static struct snd_soc_aux_dev speyside_aux_dev[] = {
  201. {
  202. .dlc = COMP_AUX("wm9081.1-006c"),
  203. .init = speyside_wm9081_init,
  204. },
  205. };
  206. static struct snd_soc_codec_conf speyside_codec_conf[] = {
  207. {
  208. .dlc = COMP_CODEC_CONF("wm9081.1-006c"),
  209. .name_prefix = "Sub",
  210. },
  211. };
  212. static const struct snd_kcontrol_new controls[] = {
  213. SOC_DAPM_PIN_SWITCH("Main Speaker"),
  214. SOC_DAPM_PIN_SWITCH("Main DMIC"),
  215. SOC_DAPM_PIN_SWITCH("Main AMIC"),
  216. SOC_DAPM_PIN_SWITCH("WM1250 Input"),
  217. SOC_DAPM_PIN_SWITCH("WM1250 Output"),
  218. SOC_DAPM_PIN_SWITCH("Headphone"),
  219. };
  220. static const struct snd_soc_dapm_widget widgets[] = {
  221. SND_SOC_DAPM_HP("Headphone", NULL),
  222. SND_SOC_DAPM_MIC("Headset Mic", NULL),
  223. SND_SOC_DAPM_SPK("Main Speaker", NULL),
  224. SND_SOC_DAPM_MIC("Main AMIC", NULL),
  225. SND_SOC_DAPM_MIC("Main DMIC", NULL),
  226. };
  227. static const struct snd_soc_dapm_route audio_paths[] = {
  228. { "IN1RN", NULL, "MICB1" },
  229. { "IN1RP", NULL, "MICB1" },
  230. { "IN1RN", NULL, "MICB2" },
  231. { "IN1RP", NULL, "MICB2" },
  232. { "MICB1", NULL, "Headset Mic", speyside_get_micbias },
  233. { "MICB2", NULL, "Headset Mic", speyside_get_micbias },
  234. { "IN1LP", NULL, "MICB2" },
  235. { "IN1RN", NULL, "MICB1" },
  236. { "MICB2", NULL, "Main AMIC" },
  237. { "DMIC1DAT", NULL, "MICB1" },
  238. { "DMIC2DAT", NULL, "MICB1" },
  239. { "MICB1", NULL, "Main DMIC" },
  240. { "Headphone", NULL, "HPOUT1L" },
  241. { "Headphone", NULL, "HPOUT1R" },
  242. { "Sub IN1", NULL, "HPOUT2L" },
  243. { "Sub IN2", NULL, "HPOUT2R" },
  244. { "Main Speaker", NULL, "Sub SPKN" },
  245. { "Main Speaker", NULL, "Sub SPKP" },
  246. { "Main Speaker", NULL, "SPKDAT" },
  247. };
  248. static struct snd_soc_card speyside = {
  249. .name = "Speyside",
  250. .owner = THIS_MODULE,
  251. .dai_link = speyside_dai,
  252. .num_links = ARRAY_SIZE(speyside_dai),
  253. .aux_dev = speyside_aux_dev,
  254. .num_aux_devs = ARRAY_SIZE(speyside_aux_dev),
  255. .codec_conf = speyside_codec_conf,
  256. .num_configs = ARRAY_SIZE(speyside_codec_conf),
  257. .set_bias_level = speyside_set_bias_level,
  258. .set_bias_level_post = speyside_set_bias_level_post,
  259. .controls = controls,
  260. .num_controls = ARRAY_SIZE(controls),
  261. .dapm_widgets = widgets,
  262. .num_dapm_widgets = ARRAY_SIZE(widgets),
  263. .dapm_routes = audio_paths,
  264. .num_dapm_routes = ARRAY_SIZE(audio_paths),
  265. .fully_routed = true,
  266. .late_probe = speyside_late_probe,
  267. };
  268. static int speyside_probe(struct platform_device *pdev)
  269. {
  270. struct snd_soc_card *card = &speyside;
  271. int ret;
  272. card->dev = &pdev->dev;
  273. ret = devm_snd_soc_register_card(&pdev->dev, card);
  274. if (ret)
  275. dev_err_probe(&pdev->dev, ret, "snd_soc_register_card() failed\n");
  276. return ret;
  277. }
  278. static struct platform_driver speyside_driver = {
  279. .driver = {
  280. .name = "speyside",
  281. .pm = &snd_soc_pm_ops,
  282. },
  283. .probe = speyside_probe,
  284. };
  285. module_platform_driver(speyside_driver);
  286. MODULE_DESCRIPTION("Speyside audio support");
  287. MODULE_AUTHOR("Mark Brown <[email protected]>");
  288. MODULE_LICENSE("GPL");
  289. MODULE_ALIAS("platform:speyside");