xref: /linux/sound/soc/samsung/littlemill.c (revision fdca21ad4603200ac39268be3a2b93907a6b85e4)
10a590b1dSMark Brown /*
20a590b1dSMark Brown  * Littlemill audio support
30a590b1dSMark Brown  *
40a590b1dSMark Brown  * Copyright 2011 Wolfson Microelectronics
50a590b1dSMark Brown  *
60a590b1dSMark Brown  * This program is free software; you can redistribute  it and/or modify it
70a590b1dSMark Brown  * under  the terms of  the GNU General  Public License as published by the
80a590b1dSMark Brown  * Free Software Foundation;  either version 2 of the  License, or (at your
90a590b1dSMark Brown  * option) any later version.
100a590b1dSMark Brown  */
110a590b1dSMark Brown 
120a590b1dSMark Brown #include <sound/soc.h>
130a590b1dSMark Brown #include <sound/soc-dapm.h>
140a590b1dSMark Brown #include <sound/jack.h>
150a590b1dSMark Brown #include <linux/gpio.h>
160a590b1dSMark Brown #include <linux/module.h>
170a590b1dSMark Brown 
180a590b1dSMark Brown #include "../codecs/wm8994.h"
190a590b1dSMark Brown 
200a590b1dSMark Brown static int sample_rate = 44100;
210a590b1dSMark Brown 
220a590b1dSMark Brown static int littlemill_set_bias_level(struct snd_soc_card *card,
230a590b1dSMark Brown 					  struct snd_soc_dapm_context *dapm,
240a590b1dSMark Brown 					  enum snd_soc_bias_level level)
250a590b1dSMark Brown {
260cbe4b36SMark Brown 	struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
270a590b1dSMark Brown 	int ret;
280a590b1dSMark Brown 
290cbe4b36SMark Brown 	if (dapm->dev != aif1_dai->dev)
300a590b1dSMark Brown 		return 0;
310a590b1dSMark Brown 
320a590b1dSMark Brown 	switch (level) {
330a590b1dSMark Brown 	case SND_SOC_BIAS_PREPARE:
340a590b1dSMark Brown 		/*
350a590b1dSMark Brown 		 * If we've not already clocked things via hw_params()
360a590b1dSMark Brown 		 * then do so now, otherwise these are noops.
370a590b1dSMark Brown 		 */
380a590b1dSMark Brown 		if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
390cbe4b36SMark Brown 			ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1,
400a590b1dSMark Brown 						  WM8994_FLL_SRC_MCLK2, 32768,
410a590b1dSMark Brown 						  sample_rate * 512);
420a590b1dSMark Brown 			if (ret < 0) {
430a590b1dSMark Brown 				pr_err("Failed to start FLL: %d\n", ret);
440a590b1dSMark Brown 				return ret;
450a590b1dSMark Brown 			}
460a590b1dSMark Brown 
470cbe4b36SMark Brown 			ret = snd_soc_dai_set_sysclk(aif1_dai,
480a590b1dSMark Brown 						     WM8994_SYSCLK_FLL1,
490a590b1dSMark Brown 						     sample_rate * 512,
500a590b1dSMark Brown 						     SND_SOC_CLOCK_IN);
510a590b1dSMark Brown 			if (ret < 0) {
520a590b1dSMark Brown 				pr_err("Failed to set SYSCLK: %d\n", ret);
530a590b1dSMark Brown 				return ret;
540a590b1dSMark Brown 			}
550a590b1dSMark Brown 		}
560a590b1dSMark Brown 		break;
570a590b1dSMark Brown 
580a590b1dSMark Brown 	default:
590a590b1dSMark Brown 		break;
600a590b1dSMark Brown 	}
610a590b1dSMark Brown 
620a590b1dSMark Brown 	return 0;
630a590b1dSMark Brown }
640a590b1dSMark Brown 
650a590b1dSMark Brown static int littlemill_set_bias_level_post(struct snd_soc_card *card,
660a590b1dSMark Brown 					       struct snd_soc_dapm_context *dapm,
670a590b1dSMark Brown 					       enum snd_soc_bias_level level)
680a590b1dSMark Brown {
690cbe4b36SMark Brown 	struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
700a590b1dSMark Brown 	int ret;
710a590b1dSMark Brown 
720cbe4b36SMark Brown 	if (dapm->dev != aif1_dai->dev)
730a590b1dSMark Brown 		return 0;
740a590b1dSMark Brown 
750a590b1dSMark Brown 	switch (level) {
760a590b1dSMark Brown 	case SND_SOC_BIAS_STANDBY:
770cbe4b36SMark Brown 		ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
780cbe4b36SMark Brown 					     32768, SND_SOC_CLOCK_IN);
790cbe4b36SMark Brown 		if (ret < 0) {
800cbe4b36SMark Brown 			pr_err("Failed to switch away from FLL1: %d\n", ret);
810cbe4b36SMark Brown 			return ret;
820cbe4b36SMark Brown 		}
830cbe4b36SMark Brown 
840cbe4b36SMark Brown 		ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1,
850cbe4b36SMark Brown 					  0, 0, 0);
860cbe4b36SMark Brown 		if (ret < 0) {
870cbe4b36SMark Brown 			pr_err("Failed to stop FLL1: %d\n", ret);
880cbe4b36SMark Brown 			return ret;
890cbe4b36SMark Brown 		}
900cbe4b36SMark Brown 		break;
910cbe4b36SMark Brown 
920a590b1dSMark Brown 	default:
930a590b1dSMark Brown 		break;
940a590b1dSMark Brown 	}
950a590b1dSMark Brown 
960a590b1dSMark Brown 	dapm->bias_level = level;
970a590b1dSMark Brown 
980a590b1dSMark Brown 	return 0;
990a590b1dSMark Brown }
1000a590b1dSMark Brown 
1010a590b1dSMark Brown static int littlemill_hw_params(struct snd_pcm_substream *substream,
1020a590b1dSMark Brown 				struct snd_pcm_hw_params *params)
1030a590b1dSMark Brown {
1040a590b1dSMark Brown 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
1050a590b1dSMark Brown 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
1060a590b1dSMark Brown 	int ret;
1070a590b1dSMark Brown 
1080a590b1dSMark Brown 	sample_rate = params_rate(params);
1090a590b1dSMark Brown 
1100a590b1dSMark Brown 	ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1,
1110a590b1dSMark Brown 				  WM8994_FLL_SRC_MCLK2, 32768,
1120a590b1dSMark Brown 				  sample_rate * 512);
1130a590b1dSMark Brown 	if (ret < 0) {
1140a590b1dSMark Brown 		pr_err("Failed to start FLL: %d\n", ret);
1150a590b1dSMark Brown 		return ret;
1160a590b1dSMark Brown 	}
1170a590b1dSMark Brown 
1180a590b1dSMark Brown 	ret = snd_soc_dai_set_sysclk(codec_dai,
1190a590b1dSMark Brown 				     WM8994_SYSCLK_FLL1,
1200a590b1dSMark Brown 				     sample_rate * 512,
1210a590b1dSMark Brown 				     SND_SOC_CLOCK_IN);
1220a590b1dSMark Brown 	if (ret < 0) {
1230a590b1dSMark Brown 		pr_err("Failed to set SYSCLK: %d\n", ret);
1240a590b1dSMark Brown 		return ret;
1250a590b1dSMark Brown 	}
1260a590b1dSMark Brown 
1270a590b1dSMark Brown 	return 0;
1280a590b1dSMark Brown }
1290a590b1dSMark Brown 
1300a590b1dSMark Brown static struct snd_soc_ops littlemill_ops = {
1310a590b1dSMark Brown 	.hw_params = littlemill_hw_params,
1320a590b1dSMark Brown };
1330a590b1dSMark Brown 
1340cbe4b36SMark Brown static const struct snd_soc_pcm_stream baseband_params = {
1350cbe4b36SMark Brown 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
1360cbe4b36SMark Brown 	.rate_min = 8000,
1370cbe4b36SMark Brown 	.rate_max = 8000,
1380cbe4b36SMark Brown 	.channels_min = 2,
1390cbe4b36SMark Brown 	.channels_max = 2,
1400cbe4b36SMark Brown };
1410cbe4b36SMark Brown 
1420a590b1dSMark Brown static struct snd_soc_dai_link littlemill_dai[] = {
1430a590b1dSMark Brown 	{
1440a590b1dSMark Brown 		.name = "CPU",
1450a590b1dSMark Brown 		.stream_name = "CPU",
1460a590b1dSMark Brown 		.cpu_dai_name = "samsung-i2s.0",
1470a590b1dSMark Brown 		.codec_dai_name = "wm8994-aif1",
148a08485d8SPadmavathi Venna 		.platform_name = "samsung-i2s.0",
1490a590b1dSMark Brown 		.codec_name = "wm8994-codec",
1500a590b1dSMark Brown 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
1510a590b1dSMark Brown 				| SND_SOC_DAIFMT_CBM_CFM,
1520a590b1dSMark Brown 		.ops = &littlemill_ops,
1530a590b1dSMark Brown 	},
1540cbe4b36SMark Brown 	{
1550cbe4b36SMark Brown 		.name = "Baseband",
1560cbe4b36SMark Brown 		.stream_name = "Baseband",
1570cbe4b36SMark Brown 		.cpu_dai_name = "wm8994-aif2",
1580cbe4b36SMark Brown 		.codec_dai_name = "wm1250-ev1",
1590cbe4b36SMark Brown 		.codec_name = "wm1250-ev1.1-0027",
1600cbe4b36SMark Brown 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
1610cbe4b36SMark Brown 				| SND_SOC_DAIFMT_CBM_CFM,
1620cbe4b36SMark Brown 		.ignore_suspend = 1,
1630cbe4b36SMark Brown 		.params = &baseband_params,
1640cbe4b36SMark Brown 	},
1650a590b1dSMark Brown };
1660a590b1dSMark Brown 
16768a02db4SMark Brown static int bbclk_ev(struct snd_soc_dapm_widget *w,
16868a02db4SMark Brown 		    struct snd_kcontrol *kcontrol, int event)
16968a02db4SMark Brown {
17068a02db4SMark Brown 	struct snd_soc_card *card = w->dapm->card;
17168a02db4SMark Brown 	struct snd_soc_dai *aif2_dai = card->rtd[1].cpu_dai;
17268a02db4SMark Brown 	int ret;
17368a02db4SMark Brown 
17468a02db4SMark Brown 	switch (event) {
17568a02db4SMark Brown 	case SND_SOC_DAPM_PRE_PMU:
17668a02db4SMark Brown 		ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2,
17768a02db4SMark Brown 					  WM8994_FLL_SRC_BCLK, 64 * 8000,
17868a02db4SMark Brown 					  8000 * 256);
17968a02db4SMark Brown 		if (ret < 0) {
18068a02db4SMark Brown 			pr_err("Failed to start FLL: %d\n", ret);
18168a02db4SMark Brown 			return ret;
18268a02db4SMark Brown 		}
18368a02db4SMark Brown 
18468a02db4SMark Brown 		ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_FLL2,
18568a02db4SMark Brown 					     8000 * 256,
18668a02db4SMark Brown 					     SND_SOC_CLOCK_IN);
18768a02db4SMark Brown 		if (ret < 0) {
18868a02db4SMark Brown 			pr_err("Failed to set SYSCLK: %d\n", ret);
18968a02db4SMark Brown 			return ret;
19068a02db4SMark Brown 		}
19168a02db4SMark Brown 		break;
19268a02db4SMark Brown 	case SND_SOC_DAPM_POST_PMD:
19368a02db4SMark Brown 		ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2,
19468a02db4SMark Brown 					     32768, SND_SOC_CLOCK_IN);
19568a02db4SMark Brown 		if (ret < 0) {
19668a02db4SMark Brown 			pr_err("Failed to switch away from FLL2: %d\n", ret);
19768a02db4SMark Brown 			return ret;
19868a02db4SMark Brown 		}
19968a02db4SMark Brown 
20068a02db4SMark Brown 		ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2,
20168a02db4SMark Brown 					  0, 0, 0);
20268a02db4SMark Brown 		if (ret < 0) {
20368a02db4SMark Brown 			pr_err("Failed to stop FLL2: %d\n", ret);
20468a02db4SMark Brown 			return ret;
20568a02db4SMark Brown 		}
20668a02db4SMark Brown 		break;
20768a02db4SMark Brown 	default:
20868a02db4SMark Brown 		return -EINVAL;
20968a02db4SMark Brown 	}
21068a02db4SMark Brown 
21168a02db4SMark Brown 	return 0;
21268a02db4SMark Brown }
21368a02db4SMark Brown 
21431a2239aSMark Brown static const struct snd_kcontrol_new controls[] = {
21531a2239aSMark Brown 	SOC_DAPM_PIN_SWITCH("WM1250 Input"),
21631a2239aSMark Brown 	SOC_DAPM_PIN_SWITCH("WM1250 Output"),
21731a2239aSMark Brown };
21831a2239aSMark Brown 
2190a590b1dSMark Brown static struct snd_soc_dapm_widget widgets[] = {
2200a590b1dSMark Brown 	SND_SOC_DAPM_HP("Headphone", NULL),
2218f103167SMark Brown 
2228f103167SMark Brown 	SND_SOC_DAPM_MIC("AMIC", NULL),
2238f103167SMark Brown 	SND_SOC_DAPM_MIC("DMIC", NULL),
22468a02db4SMark Brown 
22568a02db4SMark Brown 	SND_SOC_DAPM_SUPPLY_S("Baseband Clock", -1, SND_SOC_NOPM, 0, 0,
22668a02db4SMark Brown 			      bbclk_ev,
22768a02db4SMark Brown 			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
2280a590b1dSMark Brown };
2290a590b1dSMark Brown 
2300a590b1dSMark Brown static struct snd_soc_dapm_route audio_paths[] = {
2310a590b1dSMark Brown 	{ "Headphone", NULL, "HPOUT1L" },
2320a590b1dSMark Brown 	{ "Headphone", NULL, "HPOUT1R" },
2338f103167SMark Brown 
2348f103167SMark Brown 	{ "AMIC", NULL, "MICBIAS1" },   /* Default for AMICBIAS jumper */
2358f103167SMark Brown 	{ "IN1LN", NULL, "AMIC" },
2368f103167SMark Brown 
2378f103167SMark Brown 	{ "DMIC", NULL, "MICBIAS2" },   /* Default for DMICBIAS jumper */
2388f103167SMark Brown 	{ "DMIC1DAT", NULL, "DMIC" },
2398f103167SMark Brown 	{ "DMIC2DAT", NULL, "DMIC" },
24068a02db4SMark Brown 
24168a02db4SMark Brown 	{ "AIF2CLK", NULL, "Baseband Clock" },
2420a590b1dSMark Brown };
2430a590b1dSMark Brown 
244f2e2026cSMark Brown static struct snd_soc_jack littlemill_headset;
245f2e2026cSMark Brown 
2460a590b1dSMark Brown static int littlemill_late_probe(struct snd_soc_card *card)
2470a590b1dSMark Brown {
248f2e2026cSMark Brown 	struct snd_soc_codec *codec = card->rtd[0].codec;
2490cbe4b36SMark Brown 	struct snd_soc_dai *aif1_dai = card->rtd[0].codec_dai;
2500cbe4b36SMark Brown 	struct snd_soc_dai *aif2_dai = card->rtd[1].cpu_dai;
2510a590b1dSMark Brown 	int ret;
2520a590b1dSMark Brown 
2530cbe4b36SMark Brown 	ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
2540cbe4b36SMark Brown 				     32768, SND_SOC_CLOCK_IN);
2550cbe4b36SMark Brown 	if (ret < 0)
2560cbe4b36SMark Brown 		return ret;
2570cbe4b36SMark Brown 
2580cbe4b36SMark Brown 	ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2,
2590a590b1dSMark Brown 				     32768, SND_SOC_CLOCK_IN);
2600a590b1dSMark Brown 	if (ret < 0)
2610a590b1dSMark Brown 		return ret;
2620a590b1dSMark Brown 
263f2e2026cSMark Brown 	ret = snd_soc_jack_new(codec, "Headset",
264f2e2026cSMark Brown 			       SND_JACK_HEADSET | SND_JACK_MECHANICAL |
265f2e2026cSMark Brown 			       SND_JACK_BTN_0 | SND_JACK_BTN_1 |
266f2e2026cSMark Brown 			       SND_JACK_BTN_2 | SND_JACK_BTN_3 |
267f2e2026cSMark Brown 			       SND_JACK_BTN_4 | SND_JACK_BTN_5,
268f2e2026cSMark Brown 			       &littlemill_headset);
269f2e2026cSMark Brown 	if (ret)
270f2e2026cSMark Brown 		return ret;
271f2e2026cSMark Brown 
272f2e2026cSMark Brown 	/* This will check device compatibility itself */
27398869f68SMark Brown 	wm8958_mic_detect(codec, &littlemill_headset, NULL, NULL, NULL, NULL);
274f2e2026cSMark Brown 
275654a28c9SMark Brown 	/* As will this */
276654a28c9SMark Brown 	wm8994_mic_detect(codec, &littlemill_headset, 1);
277654a28c9SMark Brown 
2780a590b1dSMark Brown 	return 0;
2790a590b1dSMark Brown }
2800a590b1dSMark Brown 
2810a590b1dSMark Brown static struct snd_soc_card littlemill = {
2820a590b1dSMark Brown 	.name = "Littlemill",
283095d79dcSAxel Lin 	.owner = THIS_MODULE,
2840a590b1dSMark Brown 	.dai_link = littlemill_dai,
2850a590b1dSMark Brown 	.num_links = ARRAY_SIZE(littlemill_dai),
2860a590b1dSMark Brown 
2870a590b1dSMark Brown 	.set_bias_level = littlemill_set_bias_level,
2880a590b1dSMark Brown 	.set_bias_level_post = littlemill_set_bias_level_post,
2890a590b1dSMark Brown 
29031a2239aSMark Brown 	.controls = controls,
29131a2239aSMark Brown 	.num_controls = ARRAY_SIZE(controls),
2920a590b1dSMark Brown 	.dapm_widgets = widgets,
2930a590b1dSMark Brown 	.num_dapm_widgets = ARRAY_SIZE(widgets),
2940a590b1dSMark Brown 	.dapm_routes = audio_paths,
2950a590b1dSMark Brown 	.num_dapm_routes = ARRAY_SIZE(audio_paths),
2960a590b1dSMark Brown 
2970a590b1dSMark Brown 	.late_probe = littlemill_late_probe,
2980a590b1dSMark Brown };
2990a590b1dSMark Brown 
300*fdca21adSBill Pemberton static int littlemill_probe(struct platform_device *pdev)
3010a590b1dSMark Brown {
3020a590b1dSMark Brown 	struct snd_soc_card *card = &littlemill;
3030a590b1dSMark Brown 	int ret;
3040a590b1dSMark Brown 
3050a590b1dSMark Brown 	card->dev = &pdev->dev;
3060a590b1dSMark Brown 
3070a590b1dSMark Brown 	ret = snd_soc_register_card(card);
3080a590b1dSMark Brown 	if (ret) {
3090a590b1dSMark Brown 		dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
3100a590b1dSMark Brown 			ret);
3110a590b1dSMark Brown 		return ret;
3120a590b1dSMark Brown 	}
3130a590b1dSMark Brown 
3140a590b1dSMark Brown 	return 0;
3150a590b1dSMark Brown }
3160a590b1dSMark Brown 
317*fdca21adSBill Pemberton static int littlemill_remove(struct platform_device *pdev)
3180a590b1dSMark Brown {
3190a590b1dSMark Brown 	struct snd_soc_card *card = platform_get_drvdata(pdev);
3200a590b1dSMark Brown 
3210a590b1dSMark Brown 	snd_soc_unregister_card(card);
3220a590b1dSMark Brown 
3230a590b1dSMark Brown 	return 0;
3240a590b1dSMark Brown }
3250a590b1dSMark Brown 
3260a590b1dSMark Brown static struct platform_driver littlemill_driver = {
3270a590b1dSMark Brown 	.driver = {
3280a590b1dSMark Brown 		.name = "littlemill",
3290a590b1dSMark Brown 		.owner = THIS_MODULE,
3300a590b1dSMark Brown 		.pm = &snd_soc_pm_ops,
3310a590b1dSMark Brown 	},
3320a590b1dSMark Brown 	.probe = littlemill_probe,
333*fdca21adSBill Pemberton 	.remove = littlemill_remove,
3340a590b1dSMark Brown };
3350a590b1dSMark Brown 
3360a590b1dSMark Brown module_platform_driver(littlemill_driver);
3370a590b1dSMark Brown 
3380a590b1dSMark Brown MODULE_DESCRIPTION("Littlemill audio support");
3390a590b1dSMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
3400a590b1dSMark Brown MODULE_LICENSE("GPL");
3410a590b1dSMark Brown MODULE_ALIAS("platform:littlemill");
342