xref: /linux/sound/soc/samsung/littlemill.c (revision 177bf8620cf4ed290ee170a6c5966adc0924b336)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Littlemill audio support
4 //
5 // Copyright 2011 Wolfson Microelectronics
6 
7 #include <sound/soc.h>
8 #include <sound/soc-dapm.h>
9 #include <sound/jack.h>
10 #include <linux/module.h>
11 
12 #include "../codecs/wm8994.h"
13 
14 static int sample_rate = 44100;
15 
littlemill_set_bias_level(struct snd_soc_card * card,struct snd_soc_dapm_context * dapm,enum snd_soc_bias_level level)16 static int littlemill_set_bias_level(struct snd_soc_card *card,
17 					  struct snd_soc_dapm_context *dapm,
18 					  enum snd_soc_bias_level level)
19 {
20 	struct snd_soc_pcm_runtime *rtd;
21 	struct snd_soc_dai *aif1_dai;
22 	int ret;
23 
24 	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
25 	aif1_dai = snd_soc_rtd_to_codec(rtd, 0);
26 
27 	if (dapm->dev != aif1_dai->dev)
28 		return 0;
29 
30 	switch (level) {
31 	case SND_SOC_BIAS_PREPARE:
32 		/*
33 		 * If we've not already clocked things via hw_params()
34 		 * then do so now, otherwise these are noops.
35 		 */
36 		if (dapm->bias_level == SND_SOC_BIAS_STANDBY) {
37 			ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1,
38 						  WM8994_FLL_SRC_MCLK2, 32768,
39 						  sample_rate * 512);
40 			if (ret < 0) {
41 				pr_err("Failed to start FLL: %d\n", ret);
42 				return ret;
43 			}
44 
45 			ret = snd_soc_dai_set_sysclk(aif1_dai,
46 						     WM8994_SYSCLK_FLL1,
47 						     sample_rate * 512,
48 						     SND_SOC_CLOCK_IN);
49 			if (ret < 0) {
50 				pr_err("Failed to set SYSCLK: %d\n", ret);
51 				return ret;
52 			}
53 		}
54 		break;
55 
56 	default:
57 		break;
58 	}
59 
60 	return 0;
61 }
62 
littlemill_set_bias_level_post(struct snd_soc_card * card,struct snd_soc_dapm_context * dapm,enum snd_soc_bias_level level)63 static int littlemill_set_bias_level_post(struct snd_soc_card *card,
64 					       struct snd_soc_dapm_context *dapm,
65 					       enum snd_soc_bias_level level)
66 {
67 	struct snd_soc_pcm_runtime *rtd;
68 	struct snd_soc_dai *aif1_dai;
69 	int ret;
70 
71 	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
72 	aif1_dai = snd_soc_rtd_to_codec(rtd, 0);
73 
74 	if (dapm->dev != aif1_dai->dev)
75 		return 0;
76 
77 	switch (level) {
78 	case SND_SOC_BIAS_STANDBY:
79 		ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
80 					     32768, SND_SOC_CLOCK_IN);
81 		if (ret < 0) {
82 			pr_err("Failed to switch away from FLL1: %d\n", ret);
83 			return ret;
84 		}
85 
86 		ret = snd_soc_dai_set_pll(aif1_dai, WM8994_FLL1,
87 					  0, 0, 0);
88 		if (ret < 0) {
89 			pr_err("Failed to stop FLL1: %d\n", ret);
90 			return ret;
91 		}
92 		break;
93 
94 	default:
95 		break;
96 	}
97 
98 	return 0;
99 }
100 
littlemill_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)101 static int littlemill_hw_params(struct snd_pcm_substream *substream,
102 				struct snd_pcm_hw_params *params)
103 {
104 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
105 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
106 	int ret;
107 
108 	sample_rate = params_rate(params);
109 
110 	ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1,
111 				  WM8994_FLL_SRC_MCLK2, 32768,
112 				  sample_rate * 512);
113 	if (ret < 0) {
114 		pr_err("Failed to start FLL: %d\n", ret);
115 		return ret;
116 	}
117 
118 	ret = snd_soc_dai_set_sysclk(codec_dai,
119 				     WM8994_SYSCLK_FLL1,
120 				     sample_rate * 512,
121 				     SND_SOC_CLOCK_IN);
122 	if (ret < 0) {
123 		pr_err("Failed to set SYSCLK: %d\n", ret);
124 		return ret;
125 	}
126 
127 	return 0;
128 }
129 
130 static const struct snd_soc_ops littlemill_ops = {
131 	.hw_params = littlemill_hw_params,
132 };
133 
134 static const struct snd_soc_pcm_stream baseband_params = {
135 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
136 	.rate_min = 8000,
137 	.rate_max = 8000,
138 	.channels_min = 2,
139 	.channels_max = 2,
140 };
141 
142 SND_SOC_DAILINK_DEFS(cpu,
143 	DAILINK_COMP_ARRAY(COMP_CPU("samsung-i2s.0")),
144 	DAILINK_COMP_ARRAY(COMP_CODEC("wm8994-codec", "wm8994-aif1")),
145 	DAILINK_COMP_ARRAY(COMP_PLATFORM("samsung-i2s.0")));
146 
147 SND_SOC_DAILINK_DEFS(baseband,
148 	DAILINK_COMP_ARRAY(COMP_CPU("wm8994-aif2")),
149 	DAILINK_COMP_ARRAY(COMP_CODEC("wm1250-ev1.1-0027",
150 				      "wm1250-ev1")));
151 
152 static struct snd_soc_dai_link littlemill_dai[] = {
153 	{
154 		.name = "CPU",
155 		.stream_name = "CPU",
156 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
157 				| SND_SOC_DAIFMT_CBP_CFP,
158 		.ops = &littlemill_ops,
159 		SND_SOC_DAILINK_REG(cpu),
160 	},
161 	{
162 		.name = "Baseband",
163 		.stream_name = "Baseband",
164 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
165 				| SND_SOC_DAIFMT_CBP_CFP,
166 		.ignore_suspend = 1,
167 		.c2c_params = &baseband_params,
168 		.num_c2c_params = 1,
169 		SND_SOC_DAILINK_REG(baseband),
170 	},
171 };
172 
bbclk_ev(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)173 static int bbclk_ev(struct snd_soc_dapm_widget *w,
174 		    struct snd_kcontrol *kcontrol, int event)
175 {
176 	struct snd_soc_card *card = w->dapm->card;
177 	struct snd_soc_pcm_runtime *rtd;
178 	struct snd_soc_dai *aif2_dai;
179 	int ret;
180 
181 	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[1]);
182 	aif2_dai = snd_soc_rtd_to_cpu(rtd, 0);
183 
184 	switch (event) {
185 	case SND_SOC_DAPM_PRE_PMU:
186 		ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2,
187 					  WM8994_FLL_SRC_BCLK, 64 * 8000,
188 					  8000 * 256);
189 		if (ret < 0) {
190 			pr_err("Failed to start FLL: %d\n", ret);
191 			return ret;
192 		}
193 
194 		ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_FLL2,
195 					     8000 * 256,
196 					     SND_SOC_CLOCK_IN);
197 		if (ret < 0) {
198 			pr_err("Failed to set SYSCLK: %d\n", ret);
199 			return ret;
200 		}
201 		break;
202 	case SND_SOC_DAPM_POST_PMD:
203 		ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2,
204 					     32768, SND_SOC_CLOCK_IN);
205 		if (ret < 0) {
206 			pr_err("Failed to switch away from FLL2: %d\n", ret);
207 			return ret;
208 		}
209 
210 		ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2,
211 					  0, 0, 0);
212 		if (ret < 0) {
213 			pr_err("Failed to stop FLL2: %d\n", ret);
214 			return ret;
215 		}
216 		break;
217 	default:
218 		return -EINVAL;
219 	}
220 
221 	return 0;
222 }
223 
224 static const struct snd_kcontrol_new controls[] = {
225 	SOC_DAPM_PIN_SWITCH("Headphone"),
226 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
227 	SOC_DAPM_PIN_SWITCH("WM1250 Input"),
228 	SOC_DAPM_PIN_SWITCH("WM1250 Output"),
229 };
230 
231 static const struct snd_soc_dapm_widget widgets[] = {
232 	SND_SOC_DAPM_HP("Headphone", NULL),
233 	SND_SOC_DAPM_HP("Headset Mic", NULL),
234 
235 	SND_SOC_DAPM_MIC("AMIC", NULL),
236 	SND_SOC_DAPM_MIC("DMIC", NULL),
237 
238 	SND_SOC_DAPM_SUPPLY_S("Baseband Clock", -1, SND_SOC_NOPM, 0, 0,
239 			      bbclk_ev,
240 			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
241 };
242 
243 static const struct snd_soc_dapm_route audio_paths[] = {
244 	{ "Headphone", NULL, "HPOUT1L" },
245 	{ "Headphone", NULL, "HPOUT1R" },
246 
247 	{ "AMIC", NULL, "MICBIAS1" },   /* Default for AMICBIAS jumper */
248 	{ "IN1LN", NULL, "AMIC" },
249 
250 	{ "DMIC", NULL, "MICBIAS2" },   /* Default for DMICBIAS jumper */
251 	{ "DMIC1DAT", NULL, "DMIC" },
252 	{ "DMIC2DAT", NULL, "DMIC" },
253 
254 	{ "AIF2CLK", NULL, "Baseband Clock" },
255 };
256 
257 static struct snd_soc_jack littlemill_headset;
258 static struct snd_soc_jack_pin littlemill_headset_pins[] = {
259 	{
260 		.pin = "Headphone",
261 		.mask = SND_JACK_HEADPHONE,
262 	},
263 	{
264 		.pin = "Headset Mic",
265 		.mask = SND_JACK_MICROPHONE,
266 	},
267 };
268 
littlemill_late_probe(struct snd_soc_card * card)269 static int littlemill_late_probe(struct snd_soc_card *card)
270 {
271 	struct snd_soc_pcm_runtime *rtd;
272 	struct snd_soc_component *component;
273 	struct snd_soc_dai *aif1_dai;
274 	struct snd_soc_dai *aif2_dai;
275 	int ret;
276 
277 	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
278 	component = snd_soc_rtd_to_codec(rtd, 0)->component;
279 	aif1_dai = snd_soc_rtd_to_codec(rtd, 0);
280 
281 	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[1]);
282 	aif2_dai = snd_soc_rtd_to_cpu(rtd, 0);
283 
284 	ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
285 				     32768, SND_SOC_CLOCK_IN);
286 	if (ret < 0)
287 		return ret;
288 
289 	ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2,
290 				     32768, SND_SOC_CLOCK_IN);
291 	if (ret < 0)
292 		return ret;
293 
294 	ret = snd_soc_card_jack_new_pins(card, "Headset",
295 					 SND_JACK_HEADSET | SND_JACK_MECHANICAL |
296 					 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
297 					 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
298 					 SND_JACK_BTN_4 | SND_JACK_BTN_5,
299 					 &littlemill_headset,
300 					 littlemill_headset_pins,
301 					 ARRAY_SIZE(littlemill_headset_pins));
302 	if (ret)
303 		return ret;
304 
305 	/* This will check device compatibility itself */
306 	wm8958_mic_detect(component, &littlemill_headset, NULL, NULL, NULL, NULL);
307 
308 	/* As will this */
309 	wm8994_mic_detect(component, &littlemill_headset, 1);
310 
311 	return 0;
312 }
313 
314 static struct snd_soc_card littlemill = {
315 	.name = "Littlemill",
316 	.owner = THIS_MODULE,
317 	.dai_link = littlemill_dai,
318 	.num_links = ARRAY_SIZE(littlemill_dai),
319 
320 	.set_bias_level = littlemill_set_bias_level,
321 	.set_bias_level_post = littlemill_set_bias_level_post,
322 
323 	.controls = controls,
324 	.num_controls = ARRAY_SIZE(controls),
325 	.dapm_widgets = widgets,
326 	.num_dapm_widgets = ARRAY_SIZE(widgets),
327 	.dapm_routes = audio_paths,
328 	.num_dapm_routes = ARRAY_SIZE(audio_paths),
329 
330 	.late_probe = littlemill_late_probe,
331 };
332 
littlemill_probe(struct platform_device * pdev)333 static int littlemill_probe(struct platform_device *pdev)
334 {
335 	struct snd_soc_card *card = &littlemill;
336 	int ret;
337 
338 	card->dev = &pdev->dev;
339 
340 	ret = devm_snd_soc_register_card(&pdev->dev, card);
341 	if (ret)
342 		dev_err_probe(&pdev->dev, ret, "snd_soc_register_card() failed\n");
343 
344 	return ret;
345 }
346 
347 static struct platform_driver littlemill_driver = {
348 	.driver = {
349 		.name = "littlemill",
350 		.pm = &snd_soc_pm_ops,
351 	},
352 	.probe = littlemill_probe,
353 };
354 
355 module_platform_driver(littlemill_driver);
356 
357 MODULE_DESCRIPTION("Littlemill audio support");
358 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
359 MODULE_LICENSE("GPL");
360 MODULE_ALIAS("platform:littlemill");
361