xref: /openbmc/linux/sound/soc/samsung/speyside.c (revision 6774def6)
1 /*
2  * Speyside audio support
3  *
4  * Copyright 2011 Wolfson Microelectronics
5  *
6  * This program is free software; you can redistribute  it and/or modify it
7  * under  the terms of  the GNU General  Public License as published by the
8  * Free Software Foundation;  either version 2 of the  License, or (at your
9  * option) any later version.
10  */
11 
12 #include <sound/soc.h>
13 #include <sound/soc-dapm.h>
14 #include <sound/jack.h>
15 #include <linux/gpio.h>
16 #include <linux/module.h>
17 
18 #include "../codecs/wm8996.h"
19 #include "../codecs/wm9081.h"
20 
21 #define WM8996_HPSEL_GPIO 214
22 #define MCLK_AUDIO_RATE (512 * 48000)
23 
24 static int speyside_set_bias_level(struct snd_soc_card *card,
25 				   struct snd_soc_dapm_context *dapm,
26 				   enum snd_soc_bias_level level)
27 {
28 	struct snd_soc_dai *codec_dai = card->rtd[1].codec_dai;
29 	int ret;
30 
31 	if (dapm->dev != codec_dai->dev)
32 		return 0;
33 
34 	switch (level) {
35 	case SND_SOC_BIAS_STANDBY:
36 		ret = snd_soc_dai_set_sysclk(codec_dai, WM8996_SYSCLK_MCLK2,
37 					     32768, SND_SOC_CLOCK_IN);
38 		if (ret < 0)
39 			return ret;
40 
41 		ret = snd_soc_dai_set_pll(codec_dai, WM8996_FLL_MCLK2,
42 					  0, 0, 0);
43 		if (ret < 0) {
44 			pr_err("Failed to stop FLL\n");
45 			return ret;
46 		}
47 		break;
48 
49 	default:
50 		break;
51 	}
52 
53 	return 0;
54 }
55 
56 static int speyside_set_bias_level_post(struct snd_soc_card *card,
57 					struct snd_soc_dapm_context *dapm,
58 					enum snd_soc_bias_level level)
59 {
60 	struct snd_soc_dai *codec_dai = card->rtd[1].codec_dai;
61 	int ret;
62 
63 	if (dapm->dev != codec_dai->dev)
64 		return 0;
65 
66 	switch (level) {
67 	case SND_SOC_BIAS_PREPARE:
68 		if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
69 			ret = snd_soc_dai_set_pll(codec_dai, 0,
70 						  WM8996_FLL_MCLK2,
71 						  32768, MCLK_AUDIO_RATE);
72 			if (ret < 0) {
73 				pr_err("Failed to start FLL\n");
74 				return ret;
75 			}
76 
77 			ret = snd_soc_dai_set_sysclk(codec_dai,
78 						     WM8996_SYSCLK_FLL,
79 						     MCLK_AUDIO_RATE,
80 						     SND_SOC_CLOCK_IN);
81 			if (ret < 0)
82 				return ret;
83 		}
84 		break;
85 
86 	default:
87 		break;
88 	}
89 
90 	card->dapm.bias_level = level;
91 
92 	return 0;
93 }
94 
95 static struct snd_soc_jack speyside_headset;
96 
97 /* Headset jack detection DAPM pins */
98 static struct snd_soc_jack_pin speyside_headset_pins[] = {
99 	{
100 		.pin = "Headset Mic",
101 		.mask = SND_JACK_MICROPHONE,
102 	},
103 };
104 
105 /* Default the headphone selection to active high */
106 static int speyside_jack_polarity;
107 
108 static int speyside_get_micbias(struct snd_soc_dapm_widget *source,
109 				struct snd_soc_dapm_widget *sink)
110 {
111 	if (speyside_jack_polarity && (strcmp(source->name, "MICB1") == 0))
112 		return 1;
113 	if (!speyside_jack_polarity && (strcmp(source->name, "MICB2") == 0))
114 		return 1;
115 
116 	return 0;
117 }
118 
119 static void speyside_set_polarity(struct snd_soc_codec *codec,
120 				  int polarity)
121 {
122 	speyside_jack_polarity = !polarity;
123 	gpio_direction_output(WM8996_HPSEL_GPIO, speyside_jack_polarity);
124 
125 	/* Re-run DAPM to make sure we're using the correct mic bias */
126 	snd_soc_dapm_sync(&codec->dapm);
127 }
128 
129 static int speyside_wm0010_init(struct snd_soc_pcm_runtime *rtd)
130 {
131 	struct snd_soc_dai *dai = rtd->codec_dai;
132 	int ret;
133 
134 	ret = snd_soc_dai_set_sysclk(dai, 0, MCLK_AUDIO_RATE, 0);
135 	if (ret < 0)
136 		return ret;
137 
138 	return 0;
139 }
140 
141 static int speyside_wm8996_init(struct snd_soc_pcm_runtime *rtd)
142 {
143 	struct snd_soc_dai *dai = rtd->codec_dai;
144 	struct snd_soc_codec *codec = rtd->codec;
145 	int ret;
146 
147 	ret = snd_soc_dai_set_sysclk(dai, WM8996_SYSCLK_MCLK2, 32768, 0);
148 	if (ret < 0)
149 		return ret;
150 
151 	ret = gpio_request(WM8996_HPSEL_GPIO, "HP_SEL");
152 	if (ret != 0)
153 		pr_err("Failed to request HP_SEL GPIO: %d\n", ret);
154 	gpio_direction_output(WM8996_HPSEL_GPIO, speyside_jack_polarity);
155 
156 	ret = snd_soc_jack_new(codec, "Headset",
157 			       SND_JACK_LINEOUT | SND_JACK_HEADSET |
158 			       SND_JACK_BTN_0,
159 			       &speyside_headset);
160 	if (ret)
161 		return ret;
162 
163 	ret = snd_soc_jack_add_pins(&speyside_headset,
164 				    ARRAY_SIZE(speyside_headset_pins),
165 				    speyside_headset_pins);
166 	if (ret)
167 		return ret;
168 
169 	wm8996_detect(codec, &speyside_headset, speyside_set_polarity);
170 
171 	return 0;
172 }
173 
174 static int speyside_late_probe(struct snd_soc_card *card)
175 {
176 	snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
177 	snd_soc_dapm_ignore_suspend(&card->dapm, "Headset Mic");
178 	snd_soc_dapm_ignore_suspend(&card->dapm, "Main AMIC");
179 	snd_soc_dapm_ignore_suspend(&card->dapm, "Main DMIC");
180 	snd_soc_dapm_ignore_suspend(&card->dapm, "Main Speaker");
181 	snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Output");
182 	snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Input");
183 
184 	return 0;
185 }
186 
187 static const struct snd_soc_pcm_stream dsp_codec_params = {
188 	.formats = SNDRV_PCM_FMTBIT_S32_LE,
189 	.rate_min = 48000,
190 	.rate_max = 48000,
191 	.channels_min = 2,
192 	.channels_max = 2,
193 };
194 
195 static struct snd_soc_dai_link speyside_dai[] = {
196 	{
197 		.name = "CPU-DSP",
198 		.stream_name = "CPU-DSP",
199 		.cpu_dai_name = "samsung-i2s.0",
200 		.codec_dai_name = "wm0010-sdi1",
201 		.platform_name = "samsung-i2s.0",
202 		.codec_name = "spi0.0",
203 		.init = speyside_wm0010_init,
204 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
205 				| SND_SOC_DAIFMT_CBM_CFM,
206 	},
207 	{
208 		.name = "DSP-CODEC",
209 		.stream_name = "DSP-CODEC",
210 		.cpu_dai_name = "wm0010-sdi2",
211 		.codec_dai_name = "wm8996-aif1",
212 		.codec_name = "wm8996.1-001a",
213 		.init = speyside_wm8996_init,
214 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
215 				| SND_SOC_DAIFMT_CBM_CFM,
216 		.params = &dsp_codec_params,
217 		.ignore_suspend = 1,
218 	},
219 	{
220 		.name = "Baseband",
221 		.stream_name = "Baseband",
222 		.cpu_dai_name = "wm8996-aif2",
223 		.codec_dai_name = "wm1250-ev1",
224 		.codec_name = "wm1250-ev1.1-0027",
225 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
226 				| SND_SOC_DAIFMT_CBM_CFM,
227 		.ignore_suspend = 1,
228 	},
229 };
230 
231 static int speyside_wm9081_init(struct snd_soc_component *component)
232 {
233 	struct snd_soc_codec *codec = snd_soc_component_to_codec(component);
234 
235 	/* At any time the WM9081 is active it will have this clock */
236 	return snd_soc_codec_set_sysclk(codec, WM9081_SYSCLK_MCLK, 0,
237 					MCLK_AUDIO_RATE, 0);
238 }
239 
240 static struct snd_soc_aux_dev speyside_aux_dev[] = {
241 	{
242 		.name = "wm9081",
243 		.codec_name = "wm9081.1-006c",
244 		.init = speyside_wm9081_init,
245 	},
246 };
247 
248 static struct snd_soc_codec_conf speyside_codec_conf[] = {
249 	{
250 		.dev_name = "wm9081.1-006c",
251 		.name_prefix = "Sub",
252 	},
253 };
254 
255 static const struct snd_kcontrol_new controls[] = {
256 	SOC_DAPM_PIN_SWITCH("Main Speaker"),
257 	SOC_DAPM_PIN_SWITCH("Main DMIC"),
258 	SOC_DAPM_PIN_SWITCH("Main AMIC"),
259 	SOC_DAPM_PIN_SWITCH("WM1250 Input"),
260 	SOC_DAPM_PIN_SWITCH("WM1250 Output"),
261 	SOC_DAPM_PIN_SWITCH("Headphone"),
262 };
263 
264 static struct snd_soc_dapm_widget widgets[] = {
265 	SND_SOC_DAPM_HP("Headphone", NULL),
266 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
267 
268 	SND_SOC_DAPM_SPK("Main Speaker", NULL),
269 
270 	SND_SOC_DAPM_MIC("Main AMIC", NULL),
271 	SND_SOC_DAPM_MIC("Main DMIC", NULL),
272 };
273 
274 static struct snd_soc_dapm_route audio_paths[] = {
275 	{ "IN1RN", NULL, "MICB1" },
276 	{ "IN1RP", NULL, "MICB1" },
277 	{ "IN1RN", NULL, "MICB2" },
278 	{ "IN1RP", NULL, "MICB2" },
279 	{ "MICB1", NULL, "Headset Mic", speyside_get_micbias },
280 	{ "MICB2", NULL, "Headset Mic", speyside_get_micbias },
281 
282 	{ "IN1LP", NULL, "MICB2" },
283 	{ "IN1RN", NULL, "MICB1" },
284 	{ "MICB2", NULL, "Main AMIC" },
285 
286 	{ "DMIC1DAT", NULL, "MICB1" },
287 	{ "DMIC2DAT", NULL, "MICB1" },
288 	{ "MICB1", NULL, "Main DMIC" },
289 
290 	{ "Headphone", NULL, "HPOUT1L" },
291 	{ "Headphone", NULL, "HPOUT1R" },
292 
293 	{ "Sub IN1", NULL, "HPOUT2L" },
294 	{ "Sub IN2", NULL, "HPOUT2R" },
295 
296 	{ "Main Speaker", NULL, "Sub SPKN" },
297 	{ "Main Speaker", NULL, "Sub SPKP" },
298 	{ "Main Speaker", NULL, "SPKDAT" },
299 };
300 
301 static struct snd_soc_card speyside = {
302 	.name = "Speyside",
303 	.owner = THIS_MODULE,
304 	.dai_link = speyside_dai,
305 	.num_links = ARRAY_SIZE(speyside_dai),
306 	.aux_dev = speyside_aux_dev,
307 	.num_aux_devs = ARRAY_SIZE(speyside_aux_dev),
308 	.codec_conf = speyside_codec_conf,
309 	.num_configs = ARRAY_SIZE(speyside_codec_conf),
310 
311 	.set_bias_level = speyside_set_bias_level,
312 	.set_bias_level_post = speyside_set_bias_level_post,
313 
314 	.controls = controls,
315 	.num_controls = ARRAY_SIZE(controls),
316 	.dapm_widgets = widgets,
317 	.num_dapm_widgets = ARRAY_SIZE(widgets),
318 	.dapm_routes = audio_paths,
319 	.num_dapm_routes = ARRAY_SIZE(audio_paths),
320 	.fully_routed = true,
321 
322 	.late_probe = speyside_late_probe,
323 };
324 
325 static int speyside_probe(struct platform_device *pdev)
326 {
327 	struct snd_soc_card *card = &speyside;
328 	int ret;
329 
330 	card->dev = &pdev->dev;
331 
332 	ret = devm_snd_soc_register_card(&pdev->dev, card);
333 	if (ret)
334 		dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n",
335 			ret);
336 
337 	return ret;
338 }
339 
340 static struct platform_driver speyside_driver = {
341 	.driver = {
342 		.name = "speyside",
343 		.owner = THIS_MODULE,
344 		.pm = &snd_soc_pm_ops,
345 	},
346 	.probe = speyside_probe,
347 };
348 
349 module_platform_driver(speyside_driver);
350 
351 MODULE_DESCRIPTION("Speyside audio support");
352 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
353 MODULE_LICENSE("GPL");
354 MODULE_ALIAS("platform:speyside");
355