11643b9fbSSylwester Nawrocki // SPDX-License-Identifier: GPL-2.0+ 21643b9fbSSylwester Nawrocki // 31643b9fbSSylwester Nawrocki // Littlemill audio support 41643b9fbSSylwester Nawrocki // 51643b9fbSSylwester Nawrocki // Copyright 2011 Wolfson Microelectronics 60a590b1dSMark Brown 70a590b1dSMark Brown #include <sound/soc.h> 80a590b1dSMark Brown #include <sound/soc-dapm.h> 90a590b1dSMark Brown #include <sound/jack.h> 100a590b1dSMark Brown #include <linux/gpio.h> 110a590b1dSMark Brown #include <linux/module.h> 120a590b1dSMark Brown 130a590b1dSMark Brown #include "../codecs/wm8994.h" 140a590b1dSMark Brown 150a590b1dSMark Brown static int sample_rate = 44100; 160a590b1dSMark Brown 170a590b1dSMark Brown static int littlemill_set_bias_level(struct snd_soc_card *card, 180a590b1dSMark Brown struct snd_soc_dapm_context *dapm, 190a590b1dSMark Brown enum snd_soc_bias_level level) 200a590b1dSMark Brown { 215015920aSMengdong Lin struct snd_soc_pcm_runtime *rtd; 225015920aSMengdong Lin struct snd_soc_dai *aif1_dai; 230a590b1dSMark Brown int ret; 240a590b1dSMark Brown 254468189fSKuninori Morimoto rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]); 267de6b6bcSKuninori Morimoto aif1_dai = asoc_rtd_to_codec(rtd, 0); 275015920aSMengdong Lin 280cbe4b36SMark Brown if (dapm->dev != aif1_dai->dev) 290a590b1dSMark Brown return 0; 300a590b1dSMark Brown 310a590b1dSMark Brown switch (level) { 320a590b1dSMark Brown case SND_SOC_BIAS_PREPARE: 330a590b1dSMark Brown /* 340a590b1dSMark Brown * If we've not already clocked things via hw_params() 350a590b1dSMark Brown * then do so now, otherwise these are noops. 360a590b1dSMark Brown */ 370a590b1dSMark Brown if (dapm->bias_level == SND_SOC_BIAS_STANDBY) { 380cbe4b36SMark Brown ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1, 390a590b1dSMark Brown WM8994_FLL_SRC_MCLK2, 32768, 400a590b1dSMark Brown sample_rate * 512); 410a590b1dSMark Brown if (ret < 0) { 420a590b1dSMark Brown pr_err("Failed to start FLL: %d\n", ret); 430a590b1dSMark Brown return ret; 440a590b1dSMark Brown } 450a590b1dSMark Brown 460cbe4b36SMark Brown ret = snd_soc_dai_set_sysclk(aif1_dai, 470a590b1dSMark Brown WM8994_SYSCLK_FLL1, 480a590b1dSMark Brown sample_rate * 512, 490a590b1dSMark Brown SND_SOC_CLOCK_IN); 500a590b1dSMark Brown if (ret < 0) { 510a590b1dSMark Brown pr_err("Failed to set SYSCLK: %d\n", ret); 520a590b1dSMark Brown return ret; 530a590b1dSMark Brown } 540a590b1dSMark Brown } 550a590b1dSMark Brown break; 560a590b1dSMark Brown 570a590b1dSMark Brown default: 580a590b1dSMark Brown break; 590a590b1dSMark Brown } 600a590b1dSMark Brown 610a590b1dSMark Brown return 0; 620a590b1dSMark Brown } 630a590b1dSMark Brown 640a590b1dSMark Brown static int littlemill_set_bias_level_post(struct snd_soc_card *card, 650a590b1dSMark Brown struct snd_soc_dapm_context *dapm, 660a590b1dSMark Brown enum snd_soc_bias_level level) 670a590b1dSMark Brown { 685015920aSMengdong Lin struct snd_soc_pcm_runtime *rtd; 695015920aSMengdong Lin struct snd_soc_dai *aif1_dai; 700a590b1dSMark Brown int ret; 710a590b1dSMark Brown 724468189fSKuninori Morimoto rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]); 737de6b6bcSKuninori Morimoto aif1_dai = asoc_rtd_to_codec(rtd, 0); 745015920aSMengdong Lin 750cbe4b36SMark Brown if (dapm->dev != aif1_dai->dev) 760a590b1dSMark Brown return 0; 770a590b1dSMark Brown 780a590b1dSMark Brown switch (level) { 790a590b1dSMark Brown case SND_SOC_BIAS_STANDBY: 800cbe4b36SMark Brown ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2, 810cbe4b36SMark Brown 32768, SND_SOC_CLOCK_IN); 820cbe4b36SMark Brown if (ret < 0) { 830cbe4b36SMark Brown pr_err("Failed to switch away from FLL1: %d\n", ret); 840cbe4b36SMark Brown return ret; 850cbe4b36SMark Brown } 860cbe4b36SMark Brown 870cbe4b36SMark Brown ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1, 880cbe4b36SMark Brown 0, 0, 0); 890cbe4b36SMark Brown if (ret < 0) { 900cbe4b36SMark Brown pr_err("Failed to stop FLL1: %d\n", ret); 910cbe4b36SMark Brown return ret; 920cbe4b36SMark Brown } 930cbe4b36SMark Brown break; 940cbe4b36SMark Brown 950a590b1dSMark Brown default: 960a590b1dSMark Brown break; 970a590b1dSMark Brown } 980a590b1dSMark Brown 990a590b1dSMark Brown dapm->bias_level = level; 1000a590b1dSMark Brown 1010a590b1dSMark Brown return 0; 1020a590b1dSMark Brown } 1030a590b1dSMark Brown 1040a590b1dSMark Brown static int littlemill_hw_params(struct snd_pcm_substream *substream, 1050a590b1dSMark Brown struct snd_pcm_hw_params *params) 1060a590b1dSMark Brown { 107c101ce88SKuninori Morimoto struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 1087de6b6bcSKuninori Morimoto struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 1090a590b1dSMark Brown int ret; 1100a590b1dSMark Brown 1110a590b1dSMark Brown sample_rate = params_rate(params); 1120a590b1dSMark Brown 1130a590b1dSMark Brown ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1, 1140a590b1dSMark Brown WM8994_FLL_SRC_MCLK2, 32768, 1150a590b1dSMark Brown sample_rate * 512); 1160a590b1dSMark Brown if (ret < 0) { 1170a590b1dSMark Brown pr_err("Failed to start FLL: %d\n", ret); 1180a590b1dSMark Brown return ret; 1190a590b1dSMark Brown } 1200a590b1dSMark Brown 1210a590b1dSMark Brown ret = snd_soc_dai_set_sysclk(codec_dai, 1220a590b1dSMark Brown WM8994_SYSCLK_FLL1, 1230a590b1dSMark Brown sample_rate * 512, 1240a590b1dSMark Brown SND_SOC_CLOCK_IN); 1250a590b1dSMark Brown if (ret < 0) { 1260a590b1dSMark Brown pr_err("Failed to set SYSCLK: %d\n", ret); 1270a590b1dSMark Brown return ret; 1280a590b1dSMark Brown } 1290a590b1dSMark Brown 1300a590b1dSMark Brown return 0; 1310a590b1dSMark Brown } 1320a590b1dSMark Brown 1332080acf3SRikard Falkeborn static const struct snd_soc_ops littlemill_ops = { 1340a590b1dSMark Brown .hw_params = littlemill_hw_params, 1350a590b1dSMark Brown }; 1360a590b1dSMark Brown 1370cbe4b36SMark Brown static const struct snd_soc_pcm_stream baseband_params = { 1380cbe4b36SMark Brown .formats = SNDRV_PCM_FMTBIT_S32_LE, 1390cbe4b36SMark Brown .rate_min = 8000, 1400cbe4b36SMark Brown .rate_max = 8000, 1410cbe4b36SMark Brown .channels_min = 2, 1420cbe4b36SMark Brown .channels_max = 2, 1430cbe4b36SMark Brown }; 1440cbe4b36SMark Brown 145e28d6e02SKuninori Morimoto SND_SOC_DAILINK_DEFS(cpu, 146e28d6e02SKuninori Morimoto DAILINK_COMP_ARRAY(COMP_CPU("samsung-i2s.0")), 147e28d6e02SKuninori Morimoto DAILINK_COMP_ARRAY(COMP_CODEC("wm8994-codec", "wm8994-aif1")), 148e28d6e02SKuninori Morimoto DAILINK_COMP_ARRAY(COMP_PLATFORM("samsung-i2s.0"))); 149e28d6e02SKuninori Morimoto 150e28d6e02SKuninori Morimoto SND_SOC_DAILINK_DEFS(baseband, 151e28d6e02SKuninori Morimoto DAILINK_COMP_ARRAY(COMP_CPU("wm8994-aif2")), 152e28d6e02SKuninori Morimoto DAILINK_COMP_ARRAY(COMP_CODEC("wm1250-ev1.1-0027", 153e28d6e02SKuninori Morimoto "wm1250-ev1"))); 154e28d6e02SKuninori Morimoto 1550a590b1dSMark Brown static struct snd_soc_dai_link littlemill_dai[] = { 1560a590b1dSMark Brown { 1570a590b1dSMark Brown .name = "CPU", 1580a590b1dSMark Brown .stream_name = "CPU", 1590a590b1dSMark Brown .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF 1600a590b1dSMark Brown | SND_SOC_DAIFMT_CBM_CFM, 1610a590b1dSMark Brown .ops = &littlemill_ops, 162e28d6e02SKuninori Morimoto SND_SOC_DAILINK_REG(cpu), 1630a590b1dSMark Brown }, 1640cbe4b36SMark Brown { 1650cbe4b36SMark Brown .name = "Baseband", 1660cbe4b36SMark Brown .stream_name = "Baseband", 1670cbe4b36SMark Brown .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF 1680cbe4b36SMark Brown | SND_SOC_DAIFMT_CBM_CFM, 1690cbe4b36SMark Brown .ignore_suspend = 1, 170*e7a73b05SKuninori Morimoto .c2c_params = &baseband_params, 171*e7a73b05SKuninori Morimoto .num_c2c_params = 1, 172e28d6e02SKuninori Morimoto SND_SOC_DAILINK_REG(baseband), 1730cbe4b36SMark Brown }, 1740a590b1dSMark Brown }; 1750a590b1dSMark Brown 17668a02db4SMark Brown static int bbclk_ev(struct snd_soc_dapm_widget *w, 17768a02db4SMark Brown struct snd_kcontrol *kcontrol, int event) 17868a02db4SMark Brown { 17968a02db4SMark Brown struct snd_soc_card *card = w->dapm->card; 1805015920aSMengdong Lin struct snd_soc_pcm_runtime *rtd; 1815015920aSMengdong Lin struct snd_soc_dai *aif2_dai; 18268a02db4SMark Brown int ret; 18368a02db4SMark Brown 1844468189fSKuninori Morimoto rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[1]); 1857de6b6bcSKuninori Morimoto aif2_dai = asoc_rtd_to_cpu(rtd, 0); 1865015920aSMengdong Lin 18768a02db4SMark Brown switch (event) { 18868a02db4SMark Brown case SND_SOC_DAPM_PRE_PMU: 18968a02db4SMark Brown ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2, 19068a02db4SMark Brown WM8994_FLL_SRC_BCLK, 64 * 8000, 19168a02db4SMark Brown 8000 * 256); 19268a02db4SMark Brown if (ret < 0) { 19368a02db4SMark Brown pr_err("Failed to start FLL: %d\n", ret); 19468a02db4SMark Brown return ret; 19568a02db4SMark Brown } 19668a02db4SMark Brown 19768a02db4SMark Brown ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_FLL2, 19868a02db4SMark Brown 8000 * 256, 19968a02db4SMark Brown SND_SOC_CLOCK_IN); 20068a02db4SMark Brown if (ret < 0) { 20168a02db4SMark Brown pr_err("Failed to set SYSCLK: %d\n", ret); 20268a02db4SMark Brown return ret; 20368a02db4SMark Brown } 20468a02db4SMark Brown break; 20568a02db4SMark Brown case SND_SOC_DAPM_POST_PMD: 20668a02db4SMark Brown ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2, 20768a02db4SMark Brown 32768, SND_SOC_CLOCK_IN); 20868a02db4SMark Brown if (ret < 0) { 20968a02db4SMark Brown pr_err("Failed to switch away from FLL2: %d\n", ret); 21068a02db4SMark Brown return ret; 21168a02db4SMark Brown } 21268a02db4SMark Brown 21368a02db4SMark Brown ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2, 21468a02db4SMark Brown 0, 0, 0); 21568a02db4SMark Brown if (ret < 0) { 21668a02db4SMark Brown pr_err("Failed to stop FLL2: %d\n", ret); 21768a02db4SMark Brown return ret; 21868a02db4SMark Brown } 21968a02db4SMark Brown break; 22068a02db4SMark Brown default: 22168a02db4SMark Brown return -EINVAL; 22268a02db4SMark Brown } 22368a02db4SMark Brown 22468a02db4SMark Brown return 0; 22568a02db4SMark Brown } 22668a02db4SMark Brown 22731a2239aSMark Brown static const struct snd_kcontrol_new controls[] = { 22831a2239aSMark Brown SOC_DAPM_PIN_SWITCH("WM1250 Input"), 22931a2239aSMark Brown SOC_DAPM_PIN_SWITCH("WM1250 Output"), 23031a2239aSMark Brown }; 23131a2239aSMark Brown 2325449fd7bSRikard Falkeborn static const struct snd_soc_dapm_widget widgets[] = { 2330a590b1dSMark Brown SND_SOC_DAPM_HP("Headphone", NULL), 2348f103167SMark Brown 2358f103167SMark Brown SND_SOC_DAPM_MIC("AMIC", NULL), 2368f103167SMark Brown SND_SOC_DAPM_MIC("DMIC", NULL), 23768a02db4SMark Brown 23868a02db4SMark Brown SND_SOC_DAPM_SUPPLY_S("Baseband Clock", -1, SND_SOC_NOPM, 0, 0, 23968a02db4SMark Brown bbclk_ev, 24068a02db4SMark Brown SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), 2410a590b1dSMark Brown }; 2420a590b1dSMark Brown 2435449fd7bSRikard Falkeborn static const struct snd_soc_dapm_route audio_paths[] = { 2440a590b1dSMark Brown { "Headphone", NULL, "HPOUT1L" }, 2450a590b1dSMark Brown { "Headphone", NULL, "HPOUT1R" }, 2468f103167SMark Brown 2478f103167SMark Brown { "AMIC", NULL, "MICBIAS1" }, /* Default for AMICBIAS jumper */ 2488f103167SMark Brown { "IN1LN", NULL, "AMIC" }, 2498f103167SMark Brown 2508f103167SMark Brown { "DMIC", NULL, "MICBIAS2" }, /* Default for DMICBIAS jumper */ 2518f103167SMark Brown { "DMIC1DAT", NULL, "DMIC" }, 2528f103167SMark Brown { "DMIC2DAT", NULL, "DMIC" }, 25368a02db4SMark Brown 25468a02db4SMark Brown { "AIF2CLK", NULL, "Baseband Clock" }, 2550a590b1dSMark Brown }; 2560a590b1dSMark Brown 257f2e2026cSMark Brown static struct snd_soc_jack littlemill_headset; 258f2e2026cSMark Brown 2590a590b1dSMark Brown static int littlemill_late_probe(struct snd_soc_card *card) 2600a590b1dSMark Brown { 2615015920aSMengdong Lin struct snd_soc_pcm_runtime *rtd; 26200a6941cSKuninori Morimoto struct snd_soc_component *component; 2635015920aSMengdong Lin struct snd_soc_dai *aif1_dai; 2645015920aSMengdong Lin struct snd_soc_dai *aif2_dai; 2650a590b1dSMark Brown int ret; 2660a590b1dSMark Brown 2674468189fSKuninori Morimoto rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]); 2687de6b6bcSKuninori Morimoto component = asoc_rtd_to_codec(rtd, 0)->component; 2697de6b6bcSKuninori Morimoto aif1_dai = asoc_rtd_to_codec(rtd, 0); 2705015920aSMengdong Lin 2714468189fSKuninori Morimoto rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[1]); 2727de6b6bcSKuninori Morimoto aif2_dai = asoc_rtd_to_cpu(rtd, 0); 2735015920aSMengdong Lin 2740cbe4b36SMark Brown ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2, 2750cbe4b36SMark Brown 32768, SND_SOC_CLOCK_IN); 2760cbe4b36SMark Brown if (ret < 0) 2770cbe4b36SMark Brown return ret; 2780cbe4b36SMark Brown 2790cbe4b36SMark Brown ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2, 2800a590b1dSMark Brown 32768, SND_SOC_CLOCK_IN); 2810a590b1dSMark Brown if (ret < 0) 2820a590b1dSMark Brown return ret; 2830a590b1dSMark Brown 28439ec5109SLars-Peter Clausen ret = snd_soc_card_jack_new(card, "Headset", 285f2e2026cSMark Brown SND_JACK_HEADSET | SND_JACK_MECHANICAL | 286f2e2026cSMark Brown SND_JACK_BTN_0 | SND_JACK_BTN_1 | 287f2e2026cSMark Brown SND_JACK_BTN_2 | SND_JACK_BTN_3 | 288f2e2026cSMark Brown SND_JACK_BTN_4 | SND_JACK_BTN_5, 28919aed2d6SAkihiko Odaki &littlemill_headset); 290f2e2026cSMark Brown if (ret) 291f2e2026cSMark Brown return ret; 292f2e2026cSMark Brown 293f2e2026cSMark Brown /* This will check device compatibility itself */ 29400a6941cSKuninori Morimoto wm8958_mic_detect(component, &littlemill_headset, NULL, NULL, NULL, NULL); 295f2e2026cSMark Brown 296654a28c9SMark Brown /* As will this */ 29700a6941cSKuninori Morimoto wm8994_mic_detect(component, &littlemill_headset, 1); 298654a28c9SMark Brown 2990a590b1dSMark Brown return 0; 3000a590b1dSMark Brown } 3010a590b1dSMark Brown 3020a590b1dSMark Brown static struct snd_soc_card littlemill = { 3030a590b1dSMark Brown .name = "Littlemill", 304095d79dcSAxel Lin .owner = THIS_MODULE, 3050a590b1dSMark Brown .dai_link = littlemill_dai, 3060a590b1dSMark Brown .num_links = ARRAY_SIZE(littlemill_dai), 3070a590b1dSMark Brown 3080a590b1dSMark Brown .set_bias_level = littlemill_set_bias_level, 3090a590b1dSMark Brown .set_bias_level_post = littlemill_set_bias_level_post, 3100a590b1dSMark Brown 31131a2239aSMark Brown .controls = controls, 31231a2239aSMark Brown .num_controls = ARRAY_SIZE(controls), 3130a590b1dSMark Brown .dapm_widgets = widgets, 3140a590b1dSMark Brown .num_dapm_widgets = ARRAY_SIZE(widgets), 3150a590b1dSMark Brown .dapm_routes = audio_paths, 3160a590b1dSMark Brown .num_dapm_routes = ARRAY_SIZE(audio_paths), 3170a590b1dSMark Brown 3180a590b1dSMark Brown .late_probe = littlemill_late_probe, 3190a590b1dSMark Brown }; 3200a590b1dSMark Brown 321fdca21adSBill Pemberton static int littlemill_probe(struct platform_device *pdev) 3220a590b1dSMark Brown { 3230a590b1dSMark Brown struct snd_soc_card *card = &littlemill; 3240a590b1dSMark Brown int ret; 3250a590b1dSMark Brown 3260a590b1dSMark Brown card->dev = &pdev->dev; 3270a590b1dSMark Brown 328c583883eSTushar Behera ret = devm_snd_soc_register_card(&pdev->dev, card); 32927c6eaebSKuninori Morimoto if (ret) 33027c6eaebSKuninori Morimoto dev_err_probe(&pdev->dev, ret, "snd_soc_register_card() failed\n"); 331c583883eSTushar Behera 3320a590b1dSMark Brown return ret; 3330a590b1dSMark Brown } 3340a590b1dSMark Brown 3350a590b1dSMark Brown static struct platform_driver littlemill_driver = { 3360a590b1dSMark Brown .driver = { 3370a590b1dSMark Brown .name = "littlemill", 3380a590b1dSMark Brown .pm = &snd_soc_pm_ops, 3390a590b1dSMark Brown }, 3400a590b1dSMark Brown .probe = littlemill_probe, 3410a590b1dSMark Brown }; 3420a590b1dSMark Brown 3430a590b1dSMark Brown module_platform_driver(littlemill_driver); 3440a590b1dSMark Brown 3450a590b1dSMark Brown MODULE_DESCRIPTION("Littlemill audio support"); 3460a590b1dSMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 3470a590b1dSMark Brown MODULE_LICENSE("GPL"); 3480a590b1dSMark Brown MODULE_ALIAS("platform:littlemill"); 349