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, 170e7a73b05SKuninori Morimoto .c2c_params = &baseband_params, 171e7a73b05SKuninori 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[] = { 228*4d87362fSAlper Nebi Yasak SOC_DAPM_PIN_SWITCH("Headphone"), 229*4d87362fSAlper Nebi Yasak SOC_DAPM_PIN_SWITCH("Headset Mic"), 23031a2239aSMark Brown SOC_DAPM_PIN_SWITCH("WM1250 Input"), 23131a2239aSMark Brown SOC_DAPM_PIN_SWITCH("WM1250 Output"), 23231a2239aSMark Brown }; 23331a2239aSMark Brown 2345449fd7bSRikard Falkeborn static const struct snd_soc_dapm_widget widgets[] = { 2350a590b1dSMark Brown SND_SOC_DAPM_HP("Headphone", NULL), 236*4d87362fSAlper Nebi Yasak SND_SOC_DAPM_HP("Headset Mic", NULL), 2378f103167SMark Brown 2388f103167SMark Brown SND_SOC_DAPM_MIC("AMIC", NULL), 2398f103167SMark Brown SND_SOC_DAPM_MIC("DMIC", NULL), 24068a02db4SMark Brown 24168a02db4SMark Brown SND_SOC_DAPM_SUPPLY_S("Baseband Clock", -1, SND_SOC_NOPM, 0, 0, 24268a02db4SMark Brown bbclk_ev, 24368a02db4SMark Brown SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), 2440a590b1dSMark Brown }; 2450a590b1dSMark Brown 2465449fd7bSRikard Falkeborn static const struct snd_soc_dapm_route audio_paths[] = { 2470a590b1dSMark Brown { "Headphone", NULL, "HPOUT1L" }, 2480a590b1dSMark Brown { "Headphone", NULL, "HPOUT1R" }, 2498f103167SMark Brown 2508f103167SMark Brown { "AMIC", NULL, "MICBIAS1" }, /* Default for AMICBIAS jumper */ 2518f103167SMark Brown { "IN1LN", NULL, "AMIC" }, 2528f103167SMark Brown 2538f103167SMark Brown { "DMIC", NULL, "MICBIAS2" }, /* Default for DMICBIAS jumper */ 2548f103167SMark Brown { "DMIC1DAT", NULL, "DMIC" }, 2558f103167SMark Brown { "DMIC2DAT", NULL, "DMIC" }, 25668a02db4SMark Brown 25768a02db4SMark Brown { "AIF2CLK", NULL, "Baseband Clock" }, 2580a590b1dSMark Brown }; 2590a590b1dSMark Brown 260f2e2026cSMark Brown static struct snd_soc_jack littlemill_headset; 261*4d87362fSAlper Nebi Yasak static struct snd_soc_jack_pin littlemill_headset_pins[] = { 262*4d87362fSAlper Nebi Yasak { 263*4d87362fSAlper Nebi Yasak .pin = "Headphone", 264*4d87362fSAlper Nebi Yasak .mask = SND_JACK_HEADPHONE, 265*4d87362fSAlper Nebi Yasak }, 266*4d87362fSAlper Nebi Yasak { 267*4d87362fSAlper Nebi Yasak .pin = "Headset Mic", 268*4d87362fSAlper Nebi Yasak .mask = SND_JACK_MICROPHONE, 269*4d87362fSAlper Nebi Yasak }, 270*4d87362fSAlper Nebi Yasak }; 271f2e2026cSMark Brown 2720a590b1dSMark Brown static int littlemill_late_probe(struct snd_soc_card *card) 2730a590b1dSMark Brown { 2745015920aSMengdong Lin struct snd_soc_pcm_runtime *rtd; 27500a6941cSKuninori Morimoto struct snd_soc_component *component; 2765015920aSMengdong Lin struct snd_soc_dai *aif1_dai; 2775015920aSMengdong Lin struct snd_soc_dai *aif2_dai; 2780a590b1dSMark Brown int ret; 2790a590b1dSMark Brown 2804468189fSKuninori Morimoto rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]); 2817de6b6bcSKuninori Morimoto component = asoc_rtd_to_codec(rtd, 0)->component; 2827de6b6bcSKuninori Morimoto aif1_dai = asoc_rtd_to_codec(rtd, 0); 2835015920aSMengdong Lin 2844468189fSKuninori Morimoto rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[1]); 2857de6b6bcSKuninori Morimoto aif2_dai = asoc_rtd_to_cpu(rtd, 0); 2865015920aSMengdong Lin 2870cbe4b36SMark Brown ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2, 2880cbe4b36SMark Brown 32768, SND_SOC_CLOCK_IN); 2890cbe4b36SMark Brown if (ret < 0) 2900cbe4b36SMark Brown return ret; 2910cbe4b36SMark Brown 2920cbe4b36SMark Brown ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2, 2930a590b1dSMark Brown 32768, SND_SOC_CLOCK_IN); 2940a590b1dSMark Brown if (ret < 0) 2950a590b1dSMark Brown return ret; 2960a590b1dSMark Brown 297*4d87362fSAlper Nebi Yasak ret = snd_soc_card_jack_new_pins(card, "Headset", 298f2e2026cSMark Brown SND_JACK_HEADSET | SND_JACK_MECHANICAL | 299f2e2026cSMark Brown SND_JACK_BTN_0 | SND_JACK_BTN_1 | 300f2e2026cSMark Brown SND_JACK_BTN_2 | SND_JACK_BTN_3 | 301f2e2026cSMark Brown SND_JACK_BTN_4 | SND_JACK_BTN_5, 302*4d87362fSAlper Nebi Yasak &littlemill_headset, 303*4d87362fSAlper Nebi Yasak littlemill_headset_pins, 304*4d87362fSAlper Nebi Yasak ARRAY_SIZE(littlemill_headset_pins)); 305f2e2026cSMark Brown if (ret) 306f2e2026cSMark Brown return ret; 307f2e2026cSMark Brown 308f2e2026cSMark Brown /* This will check device compatibility itself */ 30900a6941cSKuninori Morimoto wm8958_mic_detect(component, &littlemill_headset, NULL, NULL, NULL, NULL); 310f2e2026cSMark Brown 311654a28c9SMark Brown /* As will this */ 31200a6941cSKuninori Morimoto wm8994_mic_detect(component, &littlemill_headset, 1); 313654a28c9SMark Brown 3140a590b1dSMark Brown return 0; 3150a590b1dSMark Brown } 3160a590b1dSMark Brown 3170a590b1dSMark Brown static struct snd_soc_card littlemill = { 3180a590b1dSMark Brown .name = "Littlemill", 319095d79dcSAxel Lin .owner = THIS_MODULE, 3200a590b1dSMark Brown .dai_link = littlemill_dai, 3210a590b1dSMark Brown .num_links = ARRAY_SIZE(littlemill_dai), 3220a590b1dSMark Brown 3230a590b1dSMark Brown .set_bias_level = littlemill_set_bias_level, 3240a590b1dSMark Brown .set_bias_level_post = littlemill_set_bias_level_post, 3250a590b1dSMark Brown 32631a2239aSMark Brown .controls = controls, 32731a2239aSMark Brown .num_controls = ARRAY_SIZE(controls), 3280a590b1dSMark Brown .dapm_widgets = widgets, 3290a590b1dSMark Brown .num_dapm_widgets = ARRAY_SIZE(widgets), 3300a590b1dSMark Brown .dapm_routes = audio_paths, 3310a590b1dSMark Brown .num_dapm_routes = ARRAY_SIZE(audio_paths), 3320a590b1dSMark Brown 3330a590b1dSMark Brown .late_probe = littlemill_late_probe, 3340a590b1dSMark Brown }; 3350a590b1dSMark Brown 336fdca21adSBill Pemberton static int littlemill_probe(struct platform_device *pdev) 3370a590b1dSMark Brown { 3380a590b1dSMark Brown struct snd_soc_card *card = &littlemill; 3390a590b1dSMark Brown int ret; 3400a590b1dSMark Brown 3410a590b1dSMark Brown card->dev = &pdev->dev; 3420a590b1dSMark Brown 343c583883eSTushar Behera ret = devm_snd_soc_register_card(&pdev->dev, card); 34427c6eaebSKuninori Morimoto if (ret) 34527c6eaebSKuninori Morimoto dev_err_probe(&pdev->dev, ret, "snd_soc_register_card() failed\n"); 346c583883eSTushar Behera 3470a590b1dSMark Brown return ret; 3480a590b1dSMark Brown } 3490a590b1dSMark Brown 3500a590b1dSMark Brown static struct platform_driver littlemill_driver = { 3510a590b1dSMark Brown .driver = { 3520a590b1dSMark Brown .name = "littlemill", 3530a590b1dSMark Brown .pm = &snd_soc_pm_ops, 3540a590b1dSMark Brown }, 3550a590b1dSMark Brown .probe = littlemill_probe, 3560a590b1dSMark Brown }; 3570a590b1dSMark Brown 3580a590b1dSMark Brown module_platform_driver(littlemill_driver); 3590a590b1dSMark Brown 3600a590b1dSMark Brown MODULE_DESCRIPTION("Littlemill audio support"); 3610a590b1dSMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 3620a590b1dSMark Brown MODULE_LICENSE("GPL"); 3630a590b1dSMark Brown MODULE_ALIAS("platform:littlemill"); 364