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 	dapm->bias_level = level;
99 
100 	return 0;
101 }
102 
littlemill_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)103 static int littlemill_hw_params(struct snd_pcm_substream *substream,
104 				struct snd_pcm_hw_params *params)
105 {
106 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
107 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
108 	int ret;
109 
110 	sample_rate = params_rate(params);
111 
112 	ret = snd_soc_dai_set_pll(codec_dai, WM8994_FLL1,
113 				  WM8994_FLL_SRC_MCLK2, 32768,
114 				  sample_rate * 512);
115 	if (ret < 0) {
116 		pr_err("Failed to start FLL: %d\n", ret);
117 		return ret;
118 	}
119 
120 	ret = snd_soc_dai_set_sysclk(codec_dai,
121 				     WM8994_SYSCLK_FLL1,
122 				     sample_rate * 512,
123 				     SND_SOC_CLOCK_IN);
124 	if (ret < 0) {
125 		pr_err("Failed to set SYSCLK: %d\n", ret);
126 		return ret;
127 	}
128 
129 	return 0;
130 }
131 
132 static const struct snd_soc_ops littlemill_ops = {
133 	.hw_params = littlemill_hw_params,
134 };
135 
136 static const struct snd_soc_pcm_stream baseband_params = {
137 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
138 	.rate_min = 8000,
139 	.rate_max = 8000,
140 	.channels_min = 2,
141 	.channels_max = 2,
142 };
143 
144 SND_SOC_DAILINK_DEFS(cpu,
145 	DAILINK_COMP_ARRAY(COMP_CPU("samsung-i2s.0")),
146 	DAILINK_COMP_ARRAY(COMP_CODEC("wm8994-codec", "wm8994-aif1")),
147 	DAILINK_COMP_ARRAY(COMP_PLATFORM("samsung-i2s.0")));
148 
149 SND_SOC_DAILINK_DEFS(baseband,
150 	DAILINK_COMP_ARRAY(COMP_CPU("wm8994-aif2")),
151 	DAILINK_COMP_ARRAY(COMP_CODEC("wm1250-ev1.1-0027",
152 				      "wm1250-ev1")));
153 
154 static struct snd_soc_dai_link littlemill_dai[] = {
155 	{
156 		.name = "CPU",
157 		.stream_name = "CPU",
158 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
159 				| SND_SOC_DAIFMT_CBP_CFP,
160 		.ops = &littlemill_ops,
161 		SND_SOC_DAILINK_REG(cpu),
162 	},
163 	{
164 		.name = "Baseband",
165 		.stream_name = "Baseband",
166 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
167 				| SND_SOC_DAIFMT_CBP_CFP,
168 		.ignore_suspend = 1,
169 		.c2c_params = &baseband_params,
170 		.num_c2c_params = 1,
171 		SND_SOC_DAILINK_REG(baseband),
172 	},
173 };
174 
bbclk_ev(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)175 static int bbclk_ev(struct snd_soc_dapm_widget *w,
176 		    struct snd_kcontrol *kcontrol, int event)
177 {
178 	struct snd_soc_card *card = w->dapm->card;
179 	struct snd_soc_pcm_runtime *rtd;
180 	struct snd_soc_dai *aif2_dai;
181 	int ret;
182 
183 	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[1]);
184 	aif2_dai = snd_soc_rtd_to_cpu(rtd, 0);
185 
186 	switch (event) {
187 	case SND_SOC_DAPM_PRE_PMU:
188 		ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2,
189 					  WM8994_FLL_SRC_BCLK, 64 * 8000,
190 					  8000 * 256);
191 		if (ret < 0) {
192 			pr_err("Failed to start FLL: %d\n", ret);
193 			return ret;
194 		}
195 
196 		ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_FLL2,
197 					     8000 * 256,
198 					     SND_SOC_CLOCK_IN);
199 		if (ret < 0) {
200 			pr_err("Failed to set SYSCLK: %d\n", ret);
201 			return ret;
202 		}
203 		break;
204 	case SND_SOC_DAPM_POST_PMD:
205 		ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2,
206 					     32768, SND_SOC_CLOCK_IN);
207 		if (ret < 0) {
208 			pr_err("Failed to switch away from FLL2: %d\n", ret);
209 			return ret;
210 		}
211 
212 		ret = snd_soc_dai_set_pll(aif2_dai, WM8994_FLL2,
213 					  0, 0, 0);
214 		if (ret < 0) {
215 			pr_err("Failed to stop FLL2: %d\n", ret);
216 			return ret;
217 		}
218 		break;
219 	default:
220 		return -EINVAL;
221 	}
222 
223 	return 0;
224 }
225 
226 static const struct snd_kcontrol_new controls[] = {
227 	SOC_DAPM_PIN_SWITCH("Headphone"),
228 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
229 	SOC_DAPM_PIN_SWITCH("WM1250 Input"),
230 	SOC_DAPM_PIN_SWITCH("WM1250 Output"),
231 };
232 
233 static const struct snd_soc_dapm_widget widgets[] = {
234 	SND_SOC_DAPM_HP("Headphone", NULL),
235 	SND_SOC_DAPM_HP("Headset Mic", NULL),
236 
237 	SND_SOC_DAPM_MIC("AMIC", NULL),
238 	SND_SOC_DAPM_MIC("DMIC", NULL),
239 
240 	SND_SOC_DAPM_SUPPLY_S("Baseband Clock", -1, SND_SOC_NOPM, 0, 0,
241 			      bbclk_ev,
242 			      SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
243 };
244 
245 static const struct snd_soc_dapm_route audio_paths[] = {
246 	{ "Headphone", NULL, "HPOUT1L" },
247 	{ "Headphone", NULL, "HPOUT1R" },
248 
249 	{ "AMIC", NULL, "MICBIAS1" },   /* Default for AMICBIAS jumper */
250 	{ "IN1LN", NULL, "AMIC" },
251 
252 	{ "DMIC", NULL, "MICBIAS2" },   /* Default for DMICBIAS jumper */
253 	{ "DMIC1DAT", NULL, "DMIC" },
254 	{ "DMIC2DAT", NULL, "DMIC" },
255 
256 	{ "AIF2CLK", NULL, "Baseband Clock" },
257 };
258 
259 static struct snd_soc_jack littlemill_headset;
260 static struct snd_soc_jack_pin littlemill_headset_pins[] = {
261 	{
262 		.pin = "Headphone",
263 		.mask = SND_JACK_HEADPHONE,
264 	},
265 	{
266 		.pin = "Headset Mic",
267 		.mask = SND_JACK_MICROPHONE,
268 	},
269 };
270 
littlemill_late_probe(struct snd_soc_card * card)271 static int littlemill_late_probe(struct snd_soc_card *card)
272 {
273 	struct snd_soc_pcm_runtime *rtd;
274 	struct snd_soc_component *component;
275 	struct snd_soc_dai *aif1_dai;
276 	struct snd_soc_dai *aif2_dai;
277 	int ret;
278 
279 	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[0]);
280 	component = snd_soc_rtd_to_codec(rtd, 0)->component;
281 	aif1_dai = snd_soc_rtd_to_codec(rtd, 0);
282 
283 	rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[1]);
284 	aif2_dai = snd_soc_rtd_to_cpu(rtd, 0);
285 
286 	ret = snd_soc_dai_set_sysclk(aif1_dai, WM8994_SYSCLK_MCLK2,
287 				     32768, SND_SOC_CLOCK_IN);
288 	if (ret < 0)
289 		return ret;
290 
291 	ret = snd_soc_dai_set_sysclk(aif2_dai, WM8994_SYSCLK_MCLK2,
292 				     32768, SND_SOC_CLOCK_IN);
293 	if (ret < 0)
294 		return ret;
295 
296 	ret = snd_soc_card_jack_new_pins(card, "Headset",
297 					 SND_JACK_HEADSET | SND_JACK_MECHANICAL |
298 					 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
299 					 SND_JACK_BTN_2 | SND_JACK_BTN_3 |
300 					 SND_JACK_BTN_4 | SND_JACK_BTN_5,
301 					 &littlemill_headset,
302 					 littlemill_headset_pins,
303 					 ARRAY_SIZE(littlemill_headset_pins));
304 	if (ret)
305 		return ret;
306 
307 	/* This will check device compatibility itself */
308 	wm8958_mic_detect(component, &littlemill_headset, NULL, NULL, NULL, NULL);
309 
310 	/* As will this */
311 	wm8994_mic_detect(component, &littlemill_headset, 1);
312 
313 	return 0;
314 }
315 
316 static struct snd_soc_card littlemill = {
317 	.name = "Littlemill",
318 	.owner = THIS_MODULE,
319 	.dai_link = littlemill_dai,
320 	.num_links = ARRAY_SIZE(littlemill_dai),
321 
322 	.set_bias_level = littlemill_set_bias_level,
323 	.set_bias_level_post = littlemill_set_bias_level_post,
324 
325 	.controls = controls,
326 	.num_controls = ARRAY_SIZE(controls),
327 	.dapm_widgets = widgets,
328 	.num_dapm_widgets = ARRAY_SIZE(widgets),
329 	.dapm_routes = audio_paths,
330 	.num_dapm_routes = ARRAY_SIZE(audio_paths),
331 
332 	.late_probe = littlemill_late_probe,
333 };
334 
littlemill_probe(struct platform_device * pdev)335 static int littlemill_probe(struct platform_device *pdev)
336 {
337 	struct snd_soc_card *card = &littlemill;
338 	int ret;
339 
340 	card->dev = &pdev->dev;
341 
342 	ret = devm_snd_soc_register_card(&pdev->dev, card);
343 	if (ret)
344 		dev_err_probe(&pdev->dev, ret, "snd_soc_register_card() failed\n");
345 
346 	return ret;
347 }
348 
349 static struct platform_driver littlemill_driver = {
350 	.driver = {
351 		.name = "littlemill",
352 		.pm = &snd_soc_pm_ops,
353 	},
354 	.probe = littlemill_probe,
355 };
356 
357 module_platform_driver(littlemill_driver);
358 
359 MODULE_DESCRIPTION("Littlemill audio support");
360 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
361 MODULE_LICENSE("GPL");
362 MODULE_ALIAS("platform:littlemill");
363