xref: /linux/sound/soc/samsung/littlemill.c (revision 095d79dc491dab1311978e0efb252bc23da88b32)
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 {
260a590b1dSMark Brown 	struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
270a590b1dSMark Brown 	int ret;
280a590b1dSMark Brown 
290a590b1dSMark Brown 	if (dapm->dev != codec_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) {
390a590b1dSMark Brown 			ret = snd_soc_dai_set_pll(codec_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 
470a590b1dSMark Brown 			ret = snd_soc_dai_set_sysclk(codec_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 {
690a590b1dSMark Brown 	struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
700a590b1dSMark Brown 	int ret;
710a590b1dSMark Brown 
720a590b1dSMark Brown 	if (dapm->dev != codec_dai->dev)
730a590b1dSMark Brown 		return 0;
740a590b1dSMark Brown 
750a590b1dSMark Brown 	switch (level) {
760a590b1dSMark Brown 	case SND_SOC_BIAS_STANDBY:
770a590b1dSMark Brown 		ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_MCLK2,
780a590b1dSMark Brown 					     32768, SND_SOC_CLOCK_IN);
790a590b1dSMark Brown 		if (ret < 0) {
800a590b1dSMark Brown 			pr_err("Failed to switch away from FLL: %d\n", ret);
810a590b1dSMark Brown 			return ret;
820a590b1dSMark Brown 		}
830a590b1dSMark Brown 
840a590b1dSMark Brown 		ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1,
850a590b1dSMark Brown 					  0, 0, 0);
860a590b1dSMark Brown 		if (ret < 0) {
870a590b1dSMark Brown 			pr_err("Failed to stop FLL: %d\n", ret);
880a590b1dSMark Brown 			return ret;
890a590b1dSMark Brown 		}
900a590b1dSMark Brown 		break;
910a590b1dSMark 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 
1340a590b1dSMark Brown static struct snd_soc_dai_link littlemill_dai[] = {
1350a590b1dSMark Brown 	{
1360a590b1dSMark Brown 		.name = "CPU",
1370a590b1dSMark Brown 		.stream_name = "CPU",
1380a590b1dSMark Brown 		.cpu_dai_name = "samsung-i2s.0",
1390a590b1dSMark Brown 		.codec_dai_name = "wm8994-aif1",
1400a590b1dSMark Brown 		.platform_name = "samsung-audio",
1410a590b1dSMark Brown 		.codec_name = "wm8994-codec",
1420a590b1dSMark Brown 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
1430a590b1dSMark Brown 				| SND_SOC_DAIFMT_CBM_CFM,
1440a590b1dSMark Brown 		.ops = &littlemill_ops,
1450a590b1dSMark Brown 	},
1460a590b1dSMark Brown };
1470a590b1dSMark Brown 
1480a590b1dSMark Brown static struct snd_soc_dapm_widget widgets[] = {
1490a590b1dSMark Brown 	SND_SOC_DAPM_HP("Headphone", NULL),
1508f103167SMark Brown 
1518f103167SMark Brown 	SND_SOC_DAPM_MIC("AMIC", NULL),
1528f103167SMark Brown 	SND_SOC_DAPM_MIC("DMIC", NULL),
1530a590b1dSMark Brown };
1540a590b1dSMark Brown 
1550a590b1dSMark Brown static struct snd_soc_dapm_route audio_paths[] = {
1560a590b1dSMark Brown 	{ "Headphone", NULL, "HPOUT1L" },
1570a590b1dSMark Brown 	{ "Headphone", NULL, "HPOUT1R" },
1588f103167SMark Brown 
1598f103167SMark Brown 	{ "AMIC", NULL, "MICBIAS1" },   /* Default for AMICBIAS jumper */
1608f103167SMark Brown 	{ "IN1LN", NULL, "AMIC" },
1618f103167SMark Brown 
1628f103167SMark Brown 	{ "DMIC", NULL, "MICBIAS2" },   /* Default for DMICBIAS jumper */
1638f103167SMark Brown 	{ "DMIC1DAT", NULL, "DMIC" },
1648f103167SMark Brown 	{ "DMIC2DAT", NULL, "DMIC" },
1650a590b1dSMark Brown };
1660a590b1dSMark Brown 
167f2e2026cSMark Brown static struct snd_soc_jack littlemill_headset;
168f2e2026cSMark Brown 
1690a590b1dSMark Brown static int littlemill_late_probe(struct snd_soc_card *card)
1700a590b1dSMark Brown {
171f2e2026cSMark Brown 	struct snd_soc_codec *codec = card->rtd[0].codec;
1720a590b1dSMark Brown 	struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
1730a590b1dSMark Brown 	int ret;
1740a590b1dSMark Brown 
1750a590b1dSMark Brown 	ret = snd_soc_dai_set_sysclk(codec_dai, WM8994_SYSCLK_MCLK2,
1760a590b1dSMark Brown 				     32768, SND_SOC_CLOCK_IN);
1770a590b1dSMark Brown 	if (ret < 0)
1780a590b1dSMark Brown 		return ret;
1790a590b1dSMark Brown 
180f2e2026cSMark Brown 	ret = snd_soc_jack_new(codec, "Headset",
181f2e2026cSMark Brown 			       SND_JACK_HEADSET | SND_JACK_MECHANICAL |
182f2e2026cSMark Brown 			       SND_JACK_BTN_0 | SND_JACK_BTN_1 |
183f2e2026cSMark Brown 			       SND_JACK_BTN_2 | SND_JACK_BTN_3 |
184f2e2026cSMark Brown 			       SND_JACK_BTN_4 | SND_JACK_BTN_5,
185f2e2026cSMark Brown 			       &littlemill_headset);
186f2e2026cSMark Brown 	if (ret)
187f2e2026cSMark Brown 		return ret;
188f2e2026cSMark Brown 
189f2e2026cSMark Brown 	/* This will check device compatibility itself */
190f2e2026cSMark Brown 	wm8958_mic_detect(codec, &littlemill_headset, NULL, NULL);
191f2e2026cSMark Brown 
1920a590b1dSMark Brown 	return 0;
1930a590b1dSMark Brown }
1940a590b1dSMark Brown 
1950a590b1dSMark Brown static struct snd_soc_card littlemill = {
1960a590b1dSMark Brown 	.name = "Littlemill",
197*095d79dcSAxel Lin 	.owner = THIS_MODULE,
1980a590b1dSMark Brown 	.dai_link = littlemill_dai,
1990a590b1dSMark Brown 	.num_links = ARRAY_SIZE(littlemill_dai),
2000a590b1dSMark Brown 
2010a590b1dSMark Brown 	.set_bias_level = littlemill_set_bias_level,
2020a590b1dSMark Brown 	.set_bias_level_post = littlemill_set_bias_level_post,
2030a590b1dSMark Brown 
2040a590b1dSMark Brown 	.dapm_widgets = widgets,
2050a590b1dSMark Brown 	.num_dapm_widgets = ARRAY_SIZE(widgets),
2060a590b1dSMark Brown 	.dapm_routes = audio_paths,
2070a590b1dSMark Brown 	.num_dapm_routes = ARRAY_SIZE(audio_paths),
2080a590b1dSMark Brown 
2090a590b1dSMark Brown 	.late_probe = littlemill_late_probe,
2100a590b1dSMark Brown };
2110a590b1dSMark Brown 
2120a590b1dSMark Brown static __devinit int littlemill_probe(struct platform_device *pdev)
2130a590b1dSMark Brown {
2140a590b1dSMark Brown 	struct snd_soc_card *card = &littlemill;
2150a590b1dSMark Brown 	int ret;
2160a590b1dSMark Brown 
2170a590b1dSMark Brown 	card->dev = &pdev->dev;
2180a590b1dSMark Brown 
2190a590b1dSMark Brown 	ret = snd_soc_register_card(card);
2200a590b1dSMark Brown 	if (ret) {
2210a590b1dSMark Brown 		dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
2220a590b1dSMark Brown 			ret);
2230a590b1dSMark Brown 		return ret;
2240a590b1dSMark Brown 	}
2250a590b1dSMark Brown 
2260a590b1dSMark Brown 	return 0;
2270a590b1dSMark Brown }
2280a590b1dSMark Brown 
2290a590b1dSMark Brown static int __devexit littlemill_remove(struct platform_device *pdev)
2300a590b1dSMark Brown {
2310a590b1dSMark Brown 	struct snd_soc_card *card = platform_get_drvdata(pdev);
2320a590b1dSMark Brown 
2330a590b1dSMark Brown 	snd_soc_unregister_card(card);
2340a590b1dSMark Brown 
2350a590b1dSMark Brown 	return 0;
2360a590b1dSMark Brown }
2370a590b1dSMark Brown 
2380a590b1dSMark Brown static struct platform_driver littlemill_driver = {
2390a590b1dSMark Brown 	.driver = {
2400a590b1dSMark Brown 		.name = "littlemill",
2410a590b1dSMark Brown 		.owner = THIS_MODULE,
2420a590b1dSMark Brown 		.pm = &snd_soc_pm_ops,
2430a590b1dSMark Brown 	},
2440a590b1dSMark Brown 	.probe = littlemill_probe,
2450a590b1dSMark Brown 	.remove = __devexit_p(littlemill_remove),
2460a590b1dSMark Brown };
2470a590b1dSMark Brown 
2480a590b1dSMark Brown module_platform_driver(littlemill_driver);
2490a590b1dSMark Brown 
2500a590b1dSMark Brown MODULE_DESCRIPTION("Littlemill audio support");
2510a590b1dSMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
2520a590b1dSMark Brown MODULE_LICENSE("GPL");
2530a590b1dSMark Brown MODULE_ALIAS("platform:littlemill");
254