xref: /linux/sound/soc/samsung/littlemill.c (revision 19aed2d6cdb72a7c92909832b9480d9cadebeef9)
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,
1700cbe4b36SMark Brown 		.params = &baseband_params,
171e28d6e02SKuninori Morimoto 		SND_SOC_DAILINK_REG(baseband),
1720cbe4b36SMark Brown 	},
1730a590b1dSMark Brown };
1740a590b1dSMark Brown 
17568a02db4SMark Brown static int bbclk_ev(struct snd_soc_dapm_widget *w,
17668a02db4SMark Brown 		    struct snd_kcontrol *kcontrol, int event)
17768a02db4SMark Brown {
17868a02db4SMark Brown 	struct snd_soc_card *card = w->dapm->card;
1795015920aSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
1805015920aSMengdong Lin 	struct snd_soc_dai *aif2_dai;
18168a02db4SMark Brown 	int ret;
18268a02db4SMark Brown 
1834468189fSKuninori Morimoto 	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[1]);
1847de6b6bcSKuninori Morimoto 	aif2_dai = asoc_rtd_to_cpu(rtd, 0);
1855015920aSMengdong Lin 
18668a02db4SMark Brown 	switch (event) {
18768a02db4SMark Brown 	case SND_SOC_DAPM_PRE_PMU:
18868a02db4SMark Brown 		ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2,
18968a02db4SMark Brown 					  WM8994_FLL_SRC_BCLK, 64 * 8000,
19068a02db4SMark Brown 					  8000 * 256);
19168a02db4SMark Brown 		if (ret < 0) {
19268a02db4SMark Brown 			pr_err("Failed to start FLL: %d\n", ret);
19368a02db4SMark Brown 			return ret;
19468a02db4SMark Brown 		}
19568a02db4SMark Brown 
19668a02db4SMark Brown 		ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_FLL2,
19768a02db4SMark Brown 					     8000 * 256,
19868a02db4SMark Brown 					     SND_SOC_CLOCK_IN);
19968a02db4SMark Brown 		if (ret < 0) {
20068a02db4SMark Brown 			pr_err("Failed to set SYSCLK: %d\n", ret);
20168a02db4SMark Brown 			return ret;
20268a02db4SMark Brown 		}
20368a02db4SMark Brown 		break;
20468a02db4SMark Brown 	case SND_SOC_DAPM_POST_PMD:
20568a02db4SMark Brown 		ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2,
20668a02db4SMark Brown 					     32768, SND_SOC_CLOCK_IN);
20768a02db4SMark Brown 		if (ret < 0) {
20868a02db4SMark Brown 			pr_err("Failed to switch away from FLL2: %d\n", ret);
20968a02db4SMark Brown 			return ret;
21068a02db4SMark Brown 		}
21168a02db4SMark Brown 
21268a02db4SMark Brown 		ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2,
21368a02db4SMark Brown 					  0, 0, 0);
21468a02db4SMark Brown 		if (ret < 0) {
21568a02db4SMark Brown 			pr_err("Failed to stop FLL2: %d\n", ret);
21668a02db4SMark Brown 			return ret;
21768a02db4SMark Brown 		}
21868a02db4SMark Brown 		break;
21968a02db4SMark Brown 	default:
22068a02db4SMark Brown 		return -EINVAL;
22168a02db4SMark Brown 	}
22268a02db4SMark Brown 
22368a02db4SMark Brown 	return 0;
22468a02db4SMark Brown }
22568a02db4SMark Brown 
22631a2239aSMark Brown static const struct snd_kcontrol_new controls[] = {
22731a2239aSMark Brown 	SOC_DAPM_PIN_SWITCH("WM1250 Input"),
22831a2239aSMark Brown 	SOC_DAPM_PIN_SWITCH("WM1250 Output"),
22931a2239aSMark Brown };
23031a2239aSMark Brown 
2315449fd7bSRikard Falkeborn static const struct snd_soc_dapm_widget widgets[] = {
2320a590b1dSMark Brown 	SND_SOC_DAPM_HP("Headphone", NULL),
2338f103167SMark Brown 
2348f103167SMark Brown 	SND_SOC_DAPM_MIC("AMIC", NULL),
2358f103167SMark Brown 	SND_SOC_DAPM_MIC("DMIC", NULL),
23668a02db4SMark Brown 
23768a02db4SMark Brown 	SND_SOC_DAPM_SUPPLY_S("Baseband Clock", -1, SND_SOC_NOPM, 0, 0,
23868a02db4SMark Brown 			      bbclk_ev,
23968a02db4SMark Brown 			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
2400a590b1dSMark Brown };
2410a590b1dSMark Brown 
2425449fd7bSRikard Falkeborn static const struct snd_soc_dapm_route audio_paths[] = {
2430a590b1dSMark Brown 	{ "Headphone", NULL, "HPOUT1L" },
2440a590b1dSMark Brown 	{ "Headphone", NULL, "HPOUT1R" },
2458f103167SMark Brown 
2468f103167SMark Brown 	{ "AMIC", NULL, "MICBIAS1" },   /* Default for AMICBIAS jumper */
2478f103167SMark Brown 	{ "IN1LN", NULL, "AMIC" },
2488f103167SMark Brown 
2498f103167SMark Brown 	{ "DMIC", NULL, "MICBIAS2" },   /* Default for DMICBIAS jumper */
2508f103167SMark Brown 	{ "DMIC1DAT", NULL, "DMIC" },
2518f103167SMark Brown 	{ "DMIC2DAT", NULL, "DMIC" },
25268a02db4SMark Brown 
25368a02db4SMark Brown 	{ "AIF2CLK", NULL, "Baseband Clock" },
2540a590b1dSMark Brown };
2550a590b1dSMark Brown 
256f2e2026cSMark Brown static struct snd_soc_jack littlemill_headset;
257f2e2026cSMark Brown 
2580a590b1dSMark Brown static int littlemill_late_probe(struct snd_soc_card *card)
2590a590b1dSMark Brown {
2605015920aSMengdong Lin 	struct snd_soc_pcm_runtime *rtd;
26100a6941cSKuninori Morimoto 	struct snd_soc_component *component;
2625015920aSMengdong Lin 	struct snd_soc_dai *aif1_dai;
2635015920aSMengdong Lin 	struct snd_soc_dai *aif2_dai;
2640a590b1dSMark Brown 	int ret;
2650a590b1dSMark Brown 
2664468189fSKuninori Morimoto 	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
2677de6b6bcSKuninori Morimoto 	component = asoc_rtd_to_codec(rtd, 0)->component;
2687de6b6bcSKuninori Morimoto 	aif1_dai = asoc_rtd_to_codec(rtd, 0);
2695015920aSMengdong Lin 
2704468189fSKuninori Morimoto 	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[1]);
2717de6b6bcSKuninori Morimoto 	aif2_dai = asoc_rtd_to_cpu(rtd, 0);
2725015920aSMengdong Lin 
2730cbe4b36SMark Brown 	ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
2740cbe4b36SMark Brown 				     32768, SND_SOC_CLOCK_IN);
2750cbe4b36SMark Brown 	if (ret < 0)
2760cbe4b36SMark Brown 		return ret;
2770cbe4b36SMark Brown 
2780cbe4b36SMark Brown 	ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2,
2790a590b1dSMark Brown 				     32768, SND_SOC_CLOCK_IN);
2800a590b1dSMark Brown 	if (ret < 0)
2810a590b1dSMark Brown 		return ret;
2820a590b1dSMark Brown 
28339ec5109SLars-Peter Clausen 	ret = snd_soc_card_jack_new(card, "Headset",
284f2e2026cSMark Brown 				    SND_JACK_HEADSET | SND_JACK_MECHANICAL |
285f2e2026cSMark Brown 				    SND_JACK_BTN_0 | SND_JACK_BTN_1 |
286f2e2026cSMark Brown 				    SND_JACK_BTN_2 | SND_JACK_BTN_3 |
287f2e2026cSMark Brown 				    SND_JACK_BTN_4 | SND_JACK_BTN_5,
288*19aed2d6SAkihiko Odaki 				    &littlemill_headset);
289f2e2026cSMark Brown 	if (ret)
290f2e2026cSMark Brown 		return ret;
291f2e2026cSMark Brown 
292f2e2026cSMark Brown 	/* This will check device compatibility itself */
29300a6941cSKuninori Morimoto 	wm8958_mic_detect(component, &littlemill_headset, NULL, NULL, NULL, NULL);
294f2e2026cSMark Brown 
295654a28c9SMark Brown 	/* As will this */
29600a6941cSKuninori Morimoto 	wm8994_mic_detect(component, &littlemill_headset, 1);
297654a28c9SMark Brown 
2980a590b1dSMark Brown 	return 0;
2990a590b1dSMark Brown }
3000a590b1dSMark Brown 
3010a590b1dSMark Brown static struct snd_soc_card littlemill = {
3020a590b1dSMark Brown 	.name = "Littlemill",
303095d79dcSAxel Lin 	.owner = THIS_MODULE,
3040a590b1dSMark Brown 	.dai_link = littlemill_dai,
3050a590b1dSMark Brown 	.num_links = ARRAY_SIZE(littlemill_dai),
3060a590b1dSMark Brown 
3070a590b1dSMark Brown 	.set_bias_level = littlemill_set_bias_level,
3080a590b1dSMark Brown 	.set_bias_level_post = littlemill_set_bias_level_post,
3090a590b1dSMark Brown 
31031a2239aSMark Brown 	.controls = controls,
31131a2239aSMark Brown 	.num_controls = ARRAY_SIZE(controls),
3120a590b1dSMark Brown 	.dapm_widgets = widgets,
3130a590b1dSMark Brown 	.num_dapm_widgets = ARRAY_SIZE(widgets),
3140a590b1dSMark Brown 	.dapm_routes = audio_paths,
3150a590b1dSMark Brown 	.num_dapm_routes = ARRAY_SIZE(audio_paths),
3160a590b1dSMark Brown 
3170a590b1dSMark Brown 	.late_probe = littlemill_late_probe,
3180a590b1dSMark Brown };
3190a590b1dSMark Brown 
320fdca21adSBill Pemberton static int littlemill_probe(struct platform_device *pdev)
3210a590b1dSMark Brown {
3220a590b1dSMark Brown 	struct snd_soc_card *card = &littlemill;
3230a590b1dSMark Brown 	int ret;
3240a590b1dSMark Brown 
3250a590b1dSMark Brown 	card->dev = &pdev->dev;
3260a590b1dSMark Brown 
327c583883eSTushar Behera 	ret = devm_snd_soc_register_card(&pdev->dev, card);
32827c6eaebSKuninori Morimoto 	if (ret)
32927c6eaebSKuninori Morimoto 		dev_err_probe(&pdev->dev, ret, "snd_soc_register_card() failed\n");
330c583883eSTushar Behera 
3310a590b1dSMark Brown 	return ret;
3320a590b1dSMark Brown }
3330a590b1dSMark Brown 
3340a590b1dSMark Brown static struct platform_driver littlemill_driver = {
3350a590b1dSMark Brown 	.driver = {
3360a590b1dSMark Brown 		.name = "littlemill",
3370a590b1dSMark Brown 		.pm = &snd_soc_pm_ops,
3380a590b1dSMark Brown 	},
3390a590b1dSMark Brown 	.probe = littlemill_probe,
3400a590b1dSMark Brown };
3410a590b1dSMark Brown 
3420a590b1dSMark Brown module_platform_driver(littlemill_driver);
3430a590b1dSMark Brown 
3440a590b1dSMark Brown MODULE_DESCRIPTION("Littlemill audio support");
3450a590b1dSMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
3460a590b1dSMark Brown MODULE_LICENSE("GPL");
3470a590b1dSMark Brown MODULE_ALIAS("platform:littlemill");
348