xref: /linux/sound/soc/intel/boards/bytcr_wm5102.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  bytcr_wm5102.c - ASoc Machine driver for Intel Baytrail platforms with a
4  *                   Wolfson Microelectronics WM5102 codec
5  *
6  *  Copyright (C) 2020 Hans de Goede <hdegoede@redhat.com>
7  *  Loosely based on bytcr_rt5640.c which is:
8  *  Copyright (C) 2014-2020 Intel Corp
9  *  Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
10  */
11 
12 #include <linux/acpi.h>
13 #include <linux/bitfield.h>
14 #include <linux/clk.h>
15 #include <linux/device.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/platform_data/x86/soc.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/spi/spi.h>
23 #include <sound/jack.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/soc-acpi.h>
28 #include "../../codecs/wm5102.h"
29 #include "../atom/sst-atom-controls.h"
30 
31 #define WM5102_MAX_SYSCLK_4K	49152000 /* max sysclk for 4K family */
32 #define WM5102_MAX_SYSCLK_11025	45158400 /* max sysclk for 11.025K family */
33 
34 struct byt_wm5102_private {
35 	struct snd_soc_jack jack;
36 	struct clk *mclk;
37 	struct gpio_desc *spkvdd_en_gpio;
38 	int mclk_freq;
39 };
40 
41 #define BYT_WM5102_IN_MAP		GENMASK(3, 0)
42 #define BYT_WM5102_OUT_MAP		GENMASK(7, 4)
43 #define BYT_WM5102_SSP2			BIT(16)
44 #define BYT_WM5102_MCLK_19_2MHZ		BIT(17)
45 
46 enum {
47 	BYT_WM5102_INTMIC_IN3L_HSMIC_IN1L,
48 	BYT_WM5102_INTMIC_IN1L_HSMIC_IN2L,
49 };
50 
51 /* Note these values are pre-shifted for easy use of setting quirks */
52 enum {
53 	BYT_WM5102_SPK_SPK_MAP		= FIELD_PREP_CONST(BYT_WM5102_OUT_MAP, 0),
54 	BYT_WM5102_SPK_HPOUT2_MAP	= FIELD_PREP_CONST(BYT_WM5102_OUT_MAP, 1),
55 };
56 
57 static unsigned long quirk;
58 
59 static int quirk_override = -1;
60 module_param_named(quirk, quirk_override, int, 0444);
61 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
62 
63 static void log_quirks(struct device *dev)
64 {
65 	switch (quirk & BYT_WM5102_IN_MAP) {
66 	case BYT_WM5102_INTMIC_IN3L_HSMIC_IN1L:
67 		dev_info_once(dev, "quirk INTMIC_IN3L_HSMIC_IN1L enabled\n");
68 		break;
69 	case BYT_WM5102_INTMIC_IN1L_HSMIC_IN2L:
70 		dev_info_once(dev, "quirk INTMIC_IN1L_HSMIC_IN2L enabled\n");
71 		break;
72 	default:
73 		dev_warn_once(dev, "quirk sets invalid input map: 0x%lx, defaulting to INTMIC_IN3L_HSMIC_IN1L\n",
74 			      quirk & BYT_WM5102_IN_MAP);
75 		quirk &= ~BYT_WM5102_IN_MAP;
76 		quirk |= BYT_WM5102_INTMIC_IN3L_HSMIC_IN1L;
77 		break;
78 	}
79 	switch (quirk & BYT_WM5102_OUT_MAP) {
80 	case BYT_WM5102_SPK_SPK_MAP:
81 		dev_info_once(dev, "quirk SPK_SPK_MAP enabled\n");
82 		break;
83 	case BYT_WM5102_SPK_HPOUT2_MAP:
84 		dev_info_once(dev, "quirk SPK_HPOUT2_MAP enabled\n");
85 		break;
86 	default:
87 		dev_warn_once(dev, "quirk sets invalid output map: 0x%lx, defaulting to SPK_SPK_MAP\n",
88 			      quirk & BYT_WM5102_OUT_MAP);
89 		quirk &= ~BYT_WM5102_OUT_MAP;
90 		quirk |= BYT_WM5102_SPK_SPK_MAP;
91 		break;
92 	}
93 	if (quirk & BYT_WM5102_SSP2)
94 		dev_info_once(dev, "quirk SSP2 enabled");
95 	if (quirk & BYT_WM5102_MCLK_19_2MHZ)
96 		dev_info_once(dev, "quirk MCLK 19.2MHz enabled");
97 }
98 
99 static int byt_wm5102_spkvdd_power_event(struct snd_soc_dapm_widget *w,
100 	struct snd_kcontrol *kcontrol, int event)
101 {
102 	struct snd_soc_card *card = w->dapm->card;
103 	struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
104 
105 	gpiod_set_value_cansleep(priv->spkvdd_en_gpio,
106 				 !!SND_SOC_DAPM_EVENT_ON(event));
107 
108 	return 0;
109 }
110 
111 static int byt_wm5102_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai, int rate)
112 {
113 	struct snd_soc_component *codec_component = codec_dai->component;
114 	struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(codec_component->card);
115 	int sr_mult = ((rate % 4000) == 0) ?
116 		(WM5102_MAX_SYSCLK_4K / rate) :
117 		(WM5102_MAX_SYSCLK_11025 / rate);
118 	int ret;
119 
120 	/* Reset FLL1 */
121 	snd_soc_dai_set_pll(codec_dai, WM5102_FLL1_REFCLK, ARIZONA_FLL_SRC_NONE, 0, 0);
122 	snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_FLL_SRC_NONE, 0, 0);
123 
124 	/* Configure the FLL1 PLL before selecting it */
125 	ret = snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_CLK_SRC_MCLK1,
126 				  priv->mclk_freq, rate * sr_mult);
127 	if (ret) {
128 		dev_err(codec_component->dev, "Error setting PLL: %d\n", ret);
129 		return ret;
130 	}
131 
132 	ret = snd_soc_component_set_sysclk(codec_component, ARIZONA_CLK_SYSCLK,
133 					   ARIZONA_CLK_SRC_FLL1, rate * sr_mult,
134 					   SND_SOC_CLOCK_IN);
135 	if (ret) {
136 		dev_err(codec_component->dev, "Error setting SYSCLK: %d\n", ret);
137 		return ret;
138 	}
139 
140 	ret = snd_soc_dai_set_sysclk(codec_dai, ARIZONA_CLK_SYSCLK,
141 				     rate * 512, SND_SOC_CLOCK_IN);
142 	if (ret) {
143 		dev_err(codec_component->dev, "Error setting clock: %d\n", ret);
144 		return ret;
145 	}
146 
147 	return 0;
148 }
149 
150 static int platform_clock_control(struct snd_soc_dapm_widget *w,
151 				  struct snd_kcontrol *k, int event)
152 {
153 	struct snd_soc_dapm_context *dapm = w->dapm;
154 	struct snd_soc_card *card = dapm->card;
155 	struct snd_soc_dai *codec_dai;
156 	struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
157 	int ret;
158 
159 	codec_dai = snd_soc_card_get_codec_dai(card, "wm5102-aif1");
160 	if (!codec_dai) {
161 		dev_err(card->dev, "Error codec DAI not found\n");
162 		return -EIO;
163 	}
164 
165 	if (SND_SOC_DAPM_EVENT_ON(event)) {
166 		ret = clk_prepare_enable(priv->mclk);
167 		if (ret) {
168 			dev_err(card->dev, "Error enabling MCLK: %d\n", ret);
169 			return ret;
170 		}
171 		ret = byt_wm5102_prepare_and_enable_pll1(codec_dai, 48000);
172 		if (ret) {
173 			dev_err(card->dev, "Error setting codec sysclk: %d\n", ret);
174 			return ret;
175 		}
176 	} else {
177 		/*
178 		 * The WM5102 has a separate 32KHz clock for jack-detect
179 		 * so we can disable the PLL, followed by disabling the
180 		 * platform clock which is the source-clock for the PLL.
181 		 */
182 		snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_FLL_SRC_NONE, 0, 0);
183 		clk_disable_unprepare(priv->mclk);
184 	}
185 
186 	return 0;
187 }
188 
189 static const struct snd_soc_dapm_widget byt_wm5102_widgets[] = {
190 	SND_SOC_DAPM_HP("Headphone", NULL),
191 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
192 	SND_SOC_DAPM_MIC("Internal Mic", NULL),
193 	SND_SOC_DAPM_SPK("Speaker", NULL),
194 	SND_SOC_DAPM_LINE("Line Out", NULL),
195 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
196 			    platform_clock_control, SND_SOC_DAPM_PRE_PMU |
197 			    SND_SOC_DAPM_POST_PMD),
198 	SND_SOC_DAPM_SUPPLY("Speaker VDD", SND_SOC_NOPM, 0, 0,
199 			    byt_wm5102_spkvdd_power_event,
200 			    SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
201 };
202 
203 static const struct snd_soc_dapm_route byt_wm5102_audio_map[] = {
204 	{"Headphone", NULL, "Platform Clock"},
205 	{"Headset Mic", NULL, "Platform Clock"},
206 	{"Internal Mic", NULL, "Platform Clock"},
207 	{"Speaker", NULL, "Platform Clock"},
208 	{"Speaker", NULL, "Speaker VDD"},
209 
210 	{"Headphone", NULL, "HPOUT1L"},
211 	{"Headphone", NULL, "HPOUT1R"},
212 
213 	/*
214 	 * The Headset Mix uses MICBIAS1 or 2 depending on if a CTIA/OMTP Headset
215 	 * is connected, as the MICBIAS is applied after the CTIA/OMTP cross-switch.
216 	 */
217 	{"Headset Mic", NULL, "MICBIAS1"},
218 	{"Headset Mic", NULL, "MICBIAS2"},
219 	{"Internal Mic", NULL, "MICBIAS3"},
220 };
221 
222 static const struct snd_soc_dapm_route bytcr_wm5102_ssp0_map[] = {
223 	{"AIF1 Playback", NULL, "ssp0 Tx"},
224 	{"ssp0 Tx", NULL, "modem_out"},
225 	{"modem_in", NULL, "ssp0 Rx"},
226 	{"ssp0 Rx", NULL, "AIF1 Capture"},
227 };
228 
229 static const struct snd_soc_dapm_route bytcr_wm5102_ssp2_map[] = {
230 	{"AIF1 Playback", NULL, "ssp2 Tx"},
231 	{"ssp2 Tx", NULL, "codec_out0"},
232 	{"ssp2 Tx", NULL, "codec_out1"},
233 	{"codec_in0", NULL, "ssp2 Rx"},
234 	{"codec_in1", NULL, "ssp2 Rx"},
235 	{"ssp2 Rx", NULL, "AIF1 Capture"},
236 };
237 
238 static const struct snd_soc_dapm_route byt_wm5102_spk_spk_map[] = {
239 	{"Speaker", NULL, "SPKOUTLP"},
240 	{"Speaker", NULL, "SPKOUTLN"},
241 	{"Speaker", NULL, "SPKOUTRP"},
242 	{"Speaker", NULL, "SPKOUTRN"},
243 };
244 
245 static const struct snd_soc_dapm_route byt_wm5102_spk_hpout2_map[] = {
246 	{"Speaker", NULL, "HPOUT2L"},
247 	{"Speaker", NULL, "HPOUT2R"},
248 };
249 
250 static const struct snd_soc_dapm_route byt_wm5102_intmic_in3l_hsmic_in1l_map[] = {
251 	{"IN3L", NULL, "Internal Mic"},
252 	{"IN1L", NULL, "Headset Mic"},
253 };
254 
255 static const struct snd_soc_dapm_route byt_wm5102_intmic_in1l_hsmic_in2l_map[] = {
256 	{"IN1L", NULL, "Internal Mic"},
257 	{"IN2L", NULL, "Headset Mic"},
258 };
259 
260 static const struct snd_kcontrol_new byt_wm5102_controls[] = {
261 	SOC_DAPM_PIN_SWITCH("Headphone"),
262 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
263 	SOC_DAPM_PIN_SWITCH("Internal Mic"),
264 	SOC_DAPM_PIN_SWITCH("Speaker"),
265 	SOC_DAPM_PIN_SWITCH("Line Out"),
266 };
267 
268 static struct snd_soc_jack_pin byt_wm5102_pins[] = {
269 	{
270 		.pin	= "Headphone",
271 		.mask	= SND_JACK_HEADPHONE,
272 	},
273 	{
274 		.pin	= "Headset Mic",
275 		.mask	= SND_JACK_MICROPHONE,
276 	},
277 	{
278 		.pin	= "Line Out",
279 		.mask	= SND_JACK_LINEOUT,
280 	},
281 };
282 
283 static int byt_wm5102_init(struct snd_soc_pcm_runtime *runtime)
284 {
285 	struct snd_soc_card *card = runtime->card;
286 	struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
287 	struct snd_soc_component *component = snd_soc_rtd_to_codec(runtime, 0)->component;
288 	const struct snd_soc_dapm_route *custom_map = NULL;
289 	int ret, jack_type, num_routes = 0;
290 
291 	card->dapm.idle_bias_off = true;
292 
293 	ret = snd_soc_add_card_controls(card, byt_wm5102_controls,
294 					ARRAY_SIZE(byt_wm5102_controls));
295 	if (ret) {
296 		dev_err(card->dev, "Error adding card controls: %d\n", ret);
297 		return ret;
298 	}
299 
300 	switch (quirk & BYT_WM5102_IN_MAP) {
301 	case BYT_WM5102_INTMIC_IN3L_HSMIC_IN1L:
302 		custom_map = byt_wm5102_intmic_in3l_hsmic_in1l_map;
303 		num_routes = ARRAY_SIZE(byt_wm5102_intmic_in3l_hsmic_in1l_map);
304 		break;
305 	case BYT_WM5102_INTMIC_IN1L_HSMIC_IN2L:
306 		custom_map = byt_wm5102_intmic_in1l_hsmic_in2l_map;
307 		num_routes = ARRAY_SIZE(byt_wm5102_intmic_in1l_hsmic_in2l_map);
308 		break;
309 	}
310 	ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
311 	if (ret)
312 		return ret;
313 
314 	switch (quirk & BYT_WM5102_OUT_MAP) {
315 	case BYT_WM5102_SPK_SPK_MAP:
316 		custom_map = byt_wm5102_spk_spk_map;
317 		num_routes = ARRAY_SIZE(byt_wm5102_spk_spk_map);
318 		break;
319 	case BYT_WM5102_SPK_HPOUT2_MAP:
320 		custom_map = byt_wm5102_spk_hpout2_map;
321 		num_routes = ARRAY_SIZE(byt_wm5102_spk_hpout2_map);
322 		break;
323 	}
324 	ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
325 	if (ret)
326 		return ret;
327 
328 	if (quirk & BYT_WM5102_SSP2) {
329 		custom_map = bytcr_wm5102_ssp2_map;
330 		num_routes = ARRAY_SIZE(bytcr_wm5102_ssp2_map);
331 	} else {
332 		custom_map = bytcr_wm5102_ssp0_map;
333 		num_routes = ARRAY_SIZE(bytcr_wm5102_ssp0_map);
334 	}
335 	ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
336 	if (ret)
337 		return ret;
338 
339 	if (quirk & BYT_WM5102_MCLK_19_2MHZ)
340 		priv->mclk_freq = 19200000;
341 	else
342 		priv->mclk_freq = 25000000;
343 
344 	/*
345 	 * The firmware might enable the clock at boot (this information
346 	 * may or may not be reflected in the enable clock register).
347 	 * To change the rate we must disable the clock first to cover these
348 	 * cases. Due to common clock framework restrictions that do not allow
349 	 * to disable a clock that has not been enabled, we need to enable
350 	 * the clock first.
351 	 */
352 	ret = clk_prepare_enable(priv->mclk);
353 	if (!ret)
354 		clk_disable_unprepare(priv->mclk);
355 
356 	ret = clk_set_rate(priv->mclk, priv->mclk_freq);
357 	if (ret) {
358 		dev_err(card->dev, "Error setting MCLK rate: %d\n", ret);
359 		return ret;
360 	}
361 
362 	jack_type = ARIZONA_JACK_MASK | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
363 		    SND_JACK_BTN_2 | SND_JACK_BTN_3;
364 	ret = snd_soc_card_jack_new_pins(card, "Headset", jack_type,
365 					 &priv->jack, byt_wm5102_pins,
366 					 ARRAY_SIZE(byt_wm5102_pins));
367 	if (ret) {
368 		dev_err(card->dev, "Error creating jack: %d\n", ret);
369 		return ret;
370 	}
371 
372 	snd_soc_component_set_jack(component, &priv->jack, NULL);
373 
374 	return 0;
375 }
376 
377 static int byt_wm5102_codec_fixup(struct snd_soc_pcm_runtime *rtd,
378 				  struct snd_pcm_hw_params *params)
379 {
380 	struct snd_interval *rate = hw_param_interval(params,
381 						      SNDRV_PCM_HW_PARAM_RATE);
382 	struct snd_interval *channels = hw_param_interval(params,
383 							  SNDRV_PCM_HW_PARAM_CHANNELS);
384 	int ret, bits;
385 
386 	/* The DSP will convert the FE rate to 48k, stereo */
387 	rate->min = 48000;
388 	rate->max = 48000;
389 	channels->min = 2;
390 	channels->max = 2;
391 
392 	if (quirk & BYT_WM5102_SSP2) {
393 		/* set SSP2 to 24-bit */
394 		params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
395 		bits = 24;
396 	} else {
397 		/* set SSP0 to 16-bit */
398 		params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
399 		bits = 16;
400 	}
401 
402 	/*
403 	 * Default mode for SSP configuration is TDM 4 slot, override config
404 	 * with explicit setting to I2S 2ch 16-bit. The word length is set with
405 	 * dai_set_tdm_slot() since there is no other API exposed
406 	 */
407 	ret = snd_soc_dai_set_fmt(snd_soc_rtd_to_cpu(rtd, 0),
408 				  SND_SOC_DAIFMT_I2S     |
409 				  SND_SOC_DAIFMT_NB_NF   |
410 				  SND_SOC_DAIFMT_BP_FP);
411 	if (ret) {
412 		dev_err(rtd->dev, "Error setting format to I2S: %d\n", ret);
413 		return ret;
414 	}
415 
416 	ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits);
417 	if (ret) {
418 		dev_err(rtd->dev, "Error setting I2S config: %d\n", ret);
419 		return ret;
420 	}
421 
422 	return 0;
423 }
424 
425 static int byt_wm5102_aif1_startup(struct snd_pcm_substream *substream)
426 {
427 	return snd_pcm_hw_constraint_single(substream->runtime,
428 					    SNDRV_PCM_HW_PARAM_RATE, 48000);
429 }
430 
431 static const struct snd_soc_ops byt_wm5102_aif1_ops = {
432 	.startup = byt_wm5102_aif1_startup,
433 };
434 
435 SND_SOC_DAILINK_DEF(dummy,
436 	DAILINK_COMP_ARRAY(COMP_DUMMY()));
437 
438 SND_SOC_DAILINK_DEF(media,
439 	DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
440 
441 SND_SOC_DAILINK_DEF(deepbuffer,
442 	DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
443 
444 SND_SOC_DAILINK_DEF(ssp0_port,
445 	DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
446 
447 SND_SOC_DAILINK_DEF(ssp0_codec,
448 	DAILINK_COMP_ARRAY(COMP_CODEC(
449 	/*
450 	 * Note there is no need to overwrite the codec-name as is done in
451 	 * other bytcr machine drivers, because the codec is a MFD child-dev.
452 	 */
453 	"wm5102-codec",
454 	"wm5102-aif1")));
455 
456 SND_SOC_DAILINK_DEF(platform,
457 	DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
458 
459 static struct snd_soc_dai_link byt_wm5102_dais[] = {
460 	[MERR_DPCM_AUDIO] = {
461 		.name = "Baytrail Audio Port",
462 		.stream_name = "Baytrail Audio",
463 		.nonatomic = true,
464 		.dynamic = 1,
465 		.dpcm_playback = 1,
466 		.dpcm_capture = 1,
467 		.ops = &byt_wm5102_aif1_ops,
468 		SND_SOC_DAILINK_REG(media, dummy, platform),
469 
470 	},
471 	[MERR_DPCM_DEEP_BUFFER] = {
472 		.name = "Deep-Buffer Audio Port",
473 		.stream_name = "Deep-Buffer Audio",
474 		.nonatomic = true,
475 		.dynamic = 1,
476 		.dpcm_playback = 1,
477 		.ops = &byt_wm5102_aif1_ops,
478 		SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
479 	},
480 		/* back ends */
481 	{
482 		/*
483 		 * This dailink is updated dynamically to point to SSP0 or SSP2.
484 		 * Yet its name is always kept as "SSP2-Codec" because the SOF
485 		 * tplg files hardcode "SSP2-Codec" even in byt-foo-ssp0.tplg.
486 		 */
487 		.name = "SSP2-Codec",
488 		.id = 0,
489 		.no_pcm = 1,
490 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
491 						| SND_SOC_DAIFMT_CBC_CFC,
492 		.be_hw_params_fixup = byt_wm5102_codec_fixup,
493 		.dpcm_playback = 1,
494 		.dpcm_capture = 1,
495 		.init = byt_wm5102_init,
496 		SND_SOC_DAILINK_REG(ssp0_port, ssp0_codec, platform),
497 	},
498 };
499 
500 /* use space before codec name to simplify card ID, and simplify driver name */
501 #define SOF_CARD_NAME "bytcht wm5102" /* card name will be 'sof-bytcht wm5102' */
502 #define SOF_DRIVER_NAME "SOF"
503 
504 #define CARD_NAME "bytcr-wm5102"
505 #define DRIVER_NAME NULL /* card name will be used for driver name */
506 
507 /* SoC card */
508 static struct snd_soc_card byt_wm5102_card = {
509 	.owner = THIS_MODULE,
510 	.dai_link = byt_wm5102_dais,
511 	.num_links = ARRAY_SIZE(byt_wm5102_dais),
512 	.dapm_widgets = byt_wm5102_widgets,
513 	.num_dapm_widgets = ARRAY_SIZE(byt_wm5102_widgets),
514 	.dapm_routes = byt_wm5102_audio_map,
515 	.num_dapm_routes = ARRAY_SIZE(byt_wm5102_audio_map),
516 	.fully_routed = true,
517 };
518 
519 static char byt_wm5102_components[64]; /* = "cfg-spk:* cfg-int-mic:* cfg-hs-mic:* ..." */
520 
521 static int snd_byt_wm5102_mc_probe(struct platform_device *pdev)
522 {
523 	static const char * const out_map_name[] = { "spk", "hpout2" };
524 	static const char * const intmic_map_name[] = { "in3l", "in1l" };
525 	static const char * const hsmic_map_name[] = { "in1l", "in2l" };
526 	char codec_name[SND_ACPI_I2C_ID_LEN];
527 	struct device *dev = &pdev->dev;
528 	struct byt_wm5102_private *priv;
529 	struct snd_soc_acpi_mach *mach;
530 	const char *platform_name;
531 	struct acpi_device *adev;
532 	struct device *codec_dev;
533 	int dai_index = 0;
534 	bool sof_parent;
535 	int i, ret;
536 
537 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
538 	if (!priv)
539 		return -ENOMEM;
540 
541 	/* Get MCLK */
542 	priv->mclk = devm_clk_get(dev, "pmc_plt_clk_3");
543 	if (IS_ERR(priv->mclk))
544 		return dev_err_probe(dev, PTR_ERR(priv->mclk), "getting pmc_plt_clk_3\n");
545 
546 	/*
547 	 * Get speaker VDD enable GPIO:
548 	 * 1. Get codec-device-name
549 	 * 2. Get codec-device
550 	 * 3. Get GPIO from codec-device
551 	 */
552 	mach = dev->platform_data;
553 	adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
554 	if (adev) {
555 		snprintf(codec_name, sizeof(codec_name), "spi-%s", acpi_dev_name(adev));
556 		acpi_dev_put(adev);
557 	} else {
558 		/* Special case for when the codec is missing from the DSTD */
559 		strscpy(codec_name, "spi1.0", sizeof(codec_name));
560 	}
561 
562 	codec_dev = bus_find_device_by_name(&spi_bus_type, NULL, codec_name);
563 	if (!codec_dev)
564 		return -EPROBE_DEFER;
565 
566 	/* Note no devm_ here since we call gpiod_get on codec_dev rather then dev */
567 	priv->spkvdd_en_gpio = gpiod_get(codec_dev, "wlf,spkvdd-ena", GPIOD_OUT_LOW);
568 	put_device(codec_dev);
569 
570 	if (IS_ERR(priv->spkvdd_en_gpio)) {
571 		ret = PTR_ERR(priv->spkvdd_en_gpio);
572 		/*
573 		 * The spkvdd gpio-lookup is registered by: drivers/mfd/arizona-spi.c,
574 		 * so -ENOENT means that arizona-spi hasn't probed yet.
575 		 */
576 		if (ret == -ENOENT)
577 			ret = -EPROBE_DEFER;
578 
579 		return dev_err_probe(dev, ret, "getting spkvdd-GPIO\n");
580 	}
581 
582 	if (soc_intel_is_cht()) {
583 		/*
584 		 * CHT always uses SSP2 and 19.2 MHz; and
585 		 * the one currently supported CHT design uses HPOUT2 as
586 		 * speaker output and has the intmic on IN1L + hsmic on IN2L.
587 		 */
588 		quirk = BYT_WM5102_SSP2 | BYT_WM5102_MCLK_19_2MHZ |
589 			BYT_WM5102_INTMIC_IN1L_HSMIC_IN2L |
590 			BYT_WM5102_SPK_HPOUT2_MAP;
591 	}
592 	if (quirk_override != -1) {
593 		dev_info_once(dev, "Overriding quirk 0x%lx => 0x%x\n",
594 			      quirk, quirk_override);
595 		quirk = quirk_override;
596 	}
597 	log_quirks(dev);
598 
599 	snprintf(byt_wm5102_components, sizeof(byt_wm5102_components),
600 		 "cfg-spk:%s cfg-intmic:%s cfg-hsmic:%s",
601 		 out_map_name[FIELD_GET(BYT_WM5102_OUT_MAP, quirk)],
602 		 intmic_map_name[FIELD_GET(BYT_WM5102_IN_MAP, quirk)],
603 		 hsmic_map_name[FIELD_GET(BYT_WM5102_IN_MAP, quirk)]);
604 	byt_wm5102_card.components = byt_wm5102_components;
605 
606 	/* find index of codec dai */
607 	for (i = 0; i < ARRAY_SIZE(byt_wm5102_dais); i++) {
608 		if (byt_wm5102_dais[i].num_codecs &&
609 		    !strcmp(byt_wm5102_dais[i].codecs->name,
610 			    "wm5102-codec")) {
611 			dai_index = i;
612 			break;
613 		}
614 	}
615 
616 	/* override platform name, if required */
617 	byt_wm5102_card.dev = dev;
618 	platform_name = mach->mach_params.platform;
619 	ret = snd_soc_fixup_dai_links_platform_name(&byt_wm5102_card, platform_name);
620 	if (ret)
621 		goto out_put_gpio;
622 
623 	/* override SSP port, if required */
624 	if (quirk & BYT_WM5102_SSP2)
625 		byt_wm5102_dais[dai_index].cpus->dai_name = "ssp2-port";
626 
627 	/* set card and driver name and pm-ops */
628 	sof_parent = snd_soc_acpi_sof_parent(dev);
629 	if (sof_parent) {
630 		byt_wm5102_card.name = SOF_CARD_NAME;
631 		byt_wm5102_card.driver_name = SOF_DRIVER_NAME;
632 		dev->driver->pm = &snd_soc_pm_ops;
633 	} else {
634 		byt_wm5102_card.name = CARD_NAME;
635 		byt_wm5102_card.driver_name = DRIVER_NAME;
636 	}
637 
638 	snd_soc_card_set_drvdata(&byt_wm5102_card, priv);
639 	ret = devm_snd_soc_register_card(dev, &byt_wm5102_card);
640 	if (ret) {
641 		dev_err_probe(dev, ret, "registering card\n");
642 		goto out_put_gpio;
643 	}
644 
645 	platform_set_drvdata(pdev, &byt_wm5102_card);
646 	return 0;
647 
648 out_put_gpio:
649 	gpiod_put(priv->spkvdd_en_gpio);
650 	return ret;
651 }
652 
653 static void snd_byt_wm5102_mc_remove(struct platform_device *pdev)
654 {
655 	struct snd_soc_card *card = platform_get_drvdata(pdev);
656 	struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
657 
658 	gpiod_put(priv->spkvdd_en_gpio);
659 }
660 
661 static struct platform_driver snd_byt_wm5102_mc_driver = {
662 	.driver = {
663 		.name = "bytcr_wm5102",
664 	},
665 	.probe = snd_byt_wm5102_mc_probe,
666 	.remove = snd_byt_wm5102_mc_remove,
667 };
668 
669 module_platform_driver(snd_byt_wm5102_mc_driver);
670 
671 MODULE_DESCRIPTION("ASoC Baytrail with WM5102 codec machine driver");
672 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
673 MODULE_LICENSE("GPL v2");
674 MODULE_ALIAS("platform:bytcr_wm5102");
675