1 /*
2  *  byt_cr_dpcm_rt5640.c - ASoc Machine driver for Intel Byt CR platform
3  *
4  *  Copyright (C) 2014 Intel Corp
5  *  Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
6  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; version 2 of the License.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  General Public License for more details.
16  *
17  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18  */
19 
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/acpi.h>
24 #include <linux/device.h>
25 #include <linux/dmi.h>
26 #include <linux/slab.h>
27 #include <asm/cpu_device_id.h>
28 #include <asm/platform_sst_audio.h>
29 #include <linux/clk.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/soc.h>
33 #include <sound/jack.h>
34 #include "../../codecs/rt5640.h"
35 #include "../atom/sst-atom-controls.h"
36 #include "../common/sst-acpi.h"
37 #include "../common/sst-dsp.h"
38 
39 enum {
40 	BYT_RT5640_DMIC1_MAP,
41 	BYT_RT5640_DMIC2_MAP,
42 	BYT_RT5640_IN1_MAP,
43 	BYT_RT5640_IN3_MAP,
44 };
45 
46 #define BYT_RT5640_MAP(quirk)	((quirk) & 0xff)
47 #define BYT_RT5640_DMIC_EN	BIT(16)
48 #define BYT_RT5640_MONO_SPEAKER BIT(17)
49 #define BYT_RT5640_DIFF_MIC     BIT(18) /* defaut is single-ended */
50 #define BYT_RT5640_SSP2_AIF2     BIT(19) /* default is using AIF1  */
51 #define BYT_RT5640_SSP0_AIF1     BIT(20)
52 #define BYT_RT5640_SSP0_AIF2     BIT(21)
53 #define BYT_RT5640_MCLK_EN	BIT(22)
54 #define BYT_RT5640_MCLK_25MHZ	BIT(23)
55 
56 struct byt_rt5640_private {
57 	struct clk *mclk;
58 };
59 
60 static unsigned long byt_rt5640_quirk = BYT_RT5640_MCLK_EN;
61 
62 static void log_quirks(struct device *dev)
63 {
64 	if (BYT_RT5640_MAP(byt_rt5640_quirk) == BYT_RT5640_DMIC1_MAP)
65 		dev_info(dev, "quirk DMIC1_MAP enabled");
66 	if (BYT_RT5640_MAP(byt_rt5640_quirk) == BYT_RT5640_DMIC2_MAP)
67 		dev_info(dev, "quirk DMIC2_MAP enabled");
68 	if (BYT_RT5640_MAP(byt_rt5640_quirk) == BYT_RT5640_IN1_MAP)
69 		dev_info(dev, "quirk IN1_MAP enabled");
70 	if (BYT_RT5640_MAP(byt_rt5640_quirk) == BYT_RT5640_IN3_MAP)
71 		dev_info(dev, "quirk IN3_MAP enabled");
72 	if (byt_rt5640_quirk & BYT_RT5640_DMIC_EN)
73 		dev_info(dev, "quirk DMIC enabled");
74 	if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER)
75 		dev_info(dev, "quirk MONO_SPEAKER enabled");
76 	if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
77 		dev_info(dev, "quirk DIFF_MIC enabled");
78 	if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2)
79 		dev_info(dev, "quirk SSP2_AIF2 enabled");
80 	if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1)
81 		dev_info(dev, "quirk SSP0_AIF1 enabled");
82 	if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)
83 		dev_info(dev, "quirk SSP0_AIF2 enabled");
84 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN)
85 		dev_info(dev, "quirk MCLK_EN enabled");
86 	if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
87 		dev_info(dev, "quirk MCLK_25MHZ enabled");
88 }
89 
90 
91 #define BYT_CODEC_DAI1	"rt5640-aif1"
92 #define BYT_CODEC_DAI2	"rt5640-aif2"
93 
94 static inline struct snd_soc_dai *byt_get_codec_dai(struct snd_soc_card *card)
95 {
96 	struct snd_soc_pcm_runtime *rtd;
97 
98 	list_for_each_entry(rtd, &card->rtd_list, list) {
99 		if (!strncmp(rtd->codec_dai->name, BYT_CODEC_DAI1,
100 			     strlen(BYT_CODEC_DAI1)))
101 			return rtd->codec_dai;
102 		if (!strncmp(rtd->codec_dai->name, BYT_CODEC_DAI2,
103 				strlen(BYT_CODEC_DAI2)))
104 			return rtd->codec_dai;
105 
106 	}
107 	return NULL;
108 }
109 
110 static int platform_clock_control(struct snd_soc_dapm_widget *w,
111 				  struct snd_kcontrol *k, int  event)
112 {
113 	struct snd_soc_dapm_context *dapm = w->dapm;
114 	struct snd_soc_card *card = dapm->card;
115 	struct snd_soc_dai *codec_dai;
116 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
117 	int ret;
118 
119 	codec_dai = byt_get_codec_dai(card);
120 	if (!codec_dai) {
121 		dev_err(card->dev,
122 			"Codec dai not found; Unable to set platform clock\n");
123 		return -EIO;
124 	}
125 
126 	if (SND_SOC_DAPM_EVENT_ON(event)) {
127 		if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && priv->mclk) {
128 			ret = clk_prepare_enable(priv->mclk);
129 			if (ret < 0) {
130 				dev_err(card->dev,
131 					"could not configure MCLK state");
132 				return ret;
133 			}
134 		}
135 		ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
136 					     48000 * 512,
137 					     SND_SOC_CLOCK_IN);
138 	} else {
139 		/*
140 		 * Set codec clock source to internal clock before
141 		 * turning off the platform clock. Codec needs clock
142 		 * for Jack detection and button press
143 		 */
144 		ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_RCCLK,
145 					     48000 * 512,
146 					     SND_SOC_CLOCK_IN);
147 		if (!ret) {
148 			if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && priv->mclk)
149 				clk_disable_unprepare(priv->mclk);
150 		}
151 	}
152 
153 	if (ret < 0) {
154 		dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
155 		return ret;
156 	}
157 
158 	return 0;
159 }
160 
161 static const struct snd_soc_dapm_widget byt_rt5640_widgets[] = {
162 	SND_SOC_DAPM_HP("Headphone", NULL),
163 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
164 	SND_SOC_DAPM_MIC("Internal Mic", NULL),
165 	SND_SOC_DAPM_SPK("Speaker", NULL),
166 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
167 			    platform_clock_control, SND_SOC_DAPM_PRE_PMU |
168 			    SND_SOC_DAPM_POST_PMD),
169 
170 };
171 
172 static const struct snd_soc_dapm_route byt_rt5640_audio_map[] = {
173 	{"Headphone", NULL, "Platform Clock"},
174 	{"Headset Mic", NULL, "Platform Clock"},
175 	{"Internal Mic", NULL, "Platform Clock"},
176 	{"Speaker", NULL, "Platform Clock"},
177 
178 	{"Headset Mic", NULL, "MICBIAS1"},
179 	{"IN2P", NULL, "Headset Mic"},
180 	{"Headphone", NULL, "HPOL"},
181 	{"Headphone", NULL, "HPOR"},
182 };
183 
184 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic1_map[] = {
185 	{"DMIC1", NULL, "Internal Mic"},
186 };
187 
188 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic2_map[] = {
189 	{"DMIC2", NULL, "Internal Mic"},
190 };
191 
192 static const struct snd_soc_dapm_route byt_rt5640_intmic_in1_map[] = {
193 	{"Internal Mic", NULL, "MICBIAS1"},
194 	{"IN1P", NULL, "Internal Mic"},
195 };
196 
197 static const struct snd_soc_dapm_route byt_rt5640_intmic_in3_map[] = {
198 	{"Internal Mic", NULL, "MICBIAS1"},
199 	{"IN3P", NULL, "Internal Mic"},
200 };
201 
202 static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif1_map[] = {
203 	{"ssp2 Tx", NULL, "codec_out0"},
204 	{"ssp2 Tx", NULL, "codec_out1"},
205 	{"codec_in0", NULL, "ssp2 Rx"},
206 	{"codec_in1", NULL, "ssp2 Rx"},
207 
208 	{"AIF1 Playback", NULL, "ssp2 Tx"},
209 	{"ssp2 Rx", NULL, "AIF1 Capture"},
210 };
211 
212 static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif2_map[] = {
213 	{"ssp2 Tx", NULL, "codec_out0"},
214 	{"ssp2 Tx", NULL, "codec_out1"},
215 	{"codec_in0", NULL, "ssp2 Rx"},
216 	{"codec_in1", NULL, "ssp2 Rx"},
217 
218 	{"AIF2 Playback", NULL, "ssp2 Tx"},
219 	{"ssp2 Rx", NULL, "AIF2 Capture"},
220 };
221 
222 static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif1_map[] = {
223 	{"ssp0 Tx", NULL, "modem_out"},
224 	{"modem_in", NULL, "ssp0 Rx"},
225 
226 	{"AIF1 Playback", NULL, "ssp0 Tx"},
227 	{"ssp0 Rx", NULL, "AIF1 Capture"},
228 };
229 
230 static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif2_map[] = {
231 	{"ssp0 Tx", NULL, "modem_out"},
232 	{"modem_in", NULL, "ssp0 Rx"},
233 
234 	{"AIF2 Playback", NULL, "ssp0 Tx"},
235 	{"ssp0 Rx", NULL, "AIF2 Capture"},
236 };
237 
238 static const struct snd_soc_dapm_route byt_rt5640_stereo_spk_map[] = {
239 	{"Speaker", NULL, "SPOLP"},
240 	{"Speaker", NULL, "SPOLN"},
241 	{"Speaker", NULL, "SPORP"},
242 	{"Speaker", NULL, "SPORN"},
243 };
244 
245 static const struct snd_soc_dapm_route byt_rt5640_mono_spk_map[] = {
246 	{"Speaker", NULL, "SPOLP"},
247 	{"Speaker", NULL, "SPOLN"},
248 };
249 
250 static const struct snd_kcontrol_new byt_rt5640_controls[] = {
251 	SOC_DAPM_PIN_SWITCH("Headphone"),
252 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
253 	SOC_DAPM_PIN_SWITCH("Internal Mic"),
254 	SOC_DAPM_PIN_SWITCH("Speaker"),
255 };
256 
257 static int byt_rt5640_aif1_hw_params(struct snd_pcm_substream *substream,
258 					struct snd_pcm_hw_params *params)
259 {
260 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
261 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
262 	int ret;
263 
264 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
265 				     params_rate(params) * 512,
266 				     SND_SOC_CLOCK_IN);
267 
268 	if (ret < 0) {
269 		dev_err(rtd->dev, "can't set codec clock %d\n", ret);
270 		return ret;
271 	}
272 
273 	if (!(byt_rt5640_quirk & BYT_RT5640_MCLK_EN)) {
274 		/* use bitclock as PLL input */
275 		if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
276 			(byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
277 
278 			/* 2x16 bit slots on SSP0 */
279 			ret = snd_soc_dai_set_pll(codec_dai, 0,
280 						RT5640_PLL1_S_BCLK1,
281 						params_rate(params) * 32,
282 						params_rate(params) * 512);
283 		} else {
284 			/* 2x15 bit slots on SSP2 */
285 			ret = snd_soc_dai_set_pll(codec_dai, 0,
286 						RT5640_PLL1_S_BCLK1,
287 						params_rate(params) * 50,
288 						params_rate(params) * 512);
289 		}
290 	} else {
291 		if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
292 			ret = snd_soc_dai_set_pll(codec_dai, 0,
293 						RT5640_PLL1_S_MCLK,
294 						25000000,
295 						params_rate(params) * 512);
296 		} else {
297 			ret = snd_soc_dai_set_pll(codec_dai, 0,
298 						RT5640_PLL1_S_MCLK,
299 						19200000,
300 						params_rate(params) * 512);
301 		}
302 	}
303 
304 	if (ret < 0) {
305 		dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
306 		return ret;
307 	}
308 
309 	return 0;
310 }
311 
312 static int byt_rt5640_quirk_cb(const struct dmi_system_id *id)
313 {
314 	byt_rt5640_quirk = (unsigned long)id->driver_data;
315 	return 1;
316 }
317 
318 static const struct dmi_system_id byt_rt5640_quirk_table[] = {
319 	{
320 		.callback = byt_rt5640_quirk_cb,
321 		.matches = {
322 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
323 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
324 		},
325 		.driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
326 						 BYT_RT5640_MCLK_EN),
327 	},
328 	{
329 		.callback = byt_rt5640_quirk_cb,
330 		.matches = {
331 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
332 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TAF"),
333 		},
334 		.driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
335 						 BYT_RT5640_MONO_SPEAKER |
336 						 BYT_RT5640_DIFF_MIC |
337 						 BYT_RT5640_SSP0_AIF2 |
338 						 BYT_RT5640_MCLK_EN
339 						 ),
340 	},
341 	{
342 		.callback = byt_rt5640_quirk_cb,
343 		.matches = {
344 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "DellInc."),
345 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"),
346 		},
347 		.driver_data = (unsigned long *)(BYT_RT5640_DMIC2_MAP |
348 						 BYT_RT5640_DMIC_EN |
349 						 BYT_RT5640_MCLK_EN),
350 	},
351 	{
352 		.callback = byt_rt5640_quirk_cb,
353 		.matches = {
354 			DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
355 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP ElitePad 1000 G2"),
356 		},
357 		.driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
358 						 BYT_RT5640_MCLK_EN),
359 	},
360 	{
361 		.callback = byt_rt5640_quirk_cb,
362 		.matches = {
363 			DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
364 			DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
365 		},
366 		.driver_data = (unsigned long *)(BYT_RT5640_DMIC1_MAP |
367 						 BYT_RT5640_DMIC_EN),
368 	},
369 	{
370 		.callback = byt_rt5640_quirk_cb,
371 		.matches = {
372 			DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
373 			DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
374 		},
375 		.driver_data = (unsigned long *)(BYT_RT5640_IN3_MAP |
376 						BYT_RT5640_MCLK_EN |
377 						BYT_RT5640_SSP0_AIF1),
378 	},
379 	{
380 		.callback = byt_rt5640_quirk_cb,
381 		.matches = {
382 			DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
383 			DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
384 		},
385 		.driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
386 						 BYT_RT5640_MCLK_EN |
387 						 BYT_RT5640_SSP0_AIF1),
388 
389 	},
390 	{}
391 };
392 
393 static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime)
394 {
395 	int ret;
396 	struct snd_soc_codec *codec = runtime->codec;
397 	struct snd_soc_card *card = runtime->card;
398 	const struct snd_soc_dapm_route *custom_map;
399 	struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
400 	int num_routes;
401 
402 	card->dapm.idle_bias_off = true;
403 
404 	rt5640_sel_asrc_clk_src(codec,
405 				RT5640_DA_STEREO_FILTER |
406 				RT5640_DA_MONO_L_FILTER	|
407 				RT5640_DA_MONO_R_FILTER	|
408 				RT5640_AD_STEREO_FILTER	|
409 				RT5640_AD_MONO_L_FILTER	|
410 				RT5640_AD_MONO_R_FILTER,
411 				RT5640_CLK_SEL_ASRC);
412 
413 	ret = snd_soc_add_card_controls(card, byt_rt5640_controls,
414 					ARRAY_SIZE(byt_rt5640_controls));
415 	if (ret) {
416 		dev_err(card->dev, "unable to add card controls\n");
417 		return ret;
418 	}
419 
420 	switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
421 	case BYT_RT5640_IN1_MAP:
422 		custom_map = byt_rt5640_intmic_in1_map;
423 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map);
424 		break;
425 	case BYT_RT5640_IN3_MAP:
426 		custom_map = byt_rt5640_intmic_in3_map;
427 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_in3_map);
428 		break;
429 	case BYT_RT5640_DMIC2_MAP:
430 		custom_map = byt_rt5640_intmic_dmic2_map;
431 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map);
432 		break;
433 	default:
434 		custom_map = byt_rt5640_intmic_dmic1_map;
435 		num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map);
436 	}
437 
438 	ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
439 	if (ret)
440 		return ret;
441 
442 	if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
443 		ret = snd_soc_dapm_add_routes(&card->dapm,
444 					byt_rt5640_ssp2_aif2_map,
445 					ARRAY_SIZE(byt_rt5640_ssp2_aif2_map));
446 	} else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
447 		ret = snd_soc_dapm_add_routes(&card->dapm,
448 					byt_rt5640_ssp0_aif1_map,
449 					ARRAY_SIZE(byt_rt5640_ssp0_aif1_map));
450 	} else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
451 		ret = snd_soc_dapm_add_routes(&card->dapm,
452 					byt_rt5640_ssp0_aif2_map,
453 					ARRAY_SIZE(byt_rt5640_ssp0_aif2_map));
454 	} else {
455 		ret = snd_soc_dapm_add_routes(&card->dapm,
456 					byt_rt5640_ssp2_aif1_map,
457 					ARRAY_SIZE(byt_rt5640_ssp2_aif1_map));
458 	}
459 	if (ret)
460 		return ret;
461 
462 	if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
463 		ret = snd_soc_dapm_add_routes(&card->dapm,
464 					byt_rt5640_mono_spk_map,
465 					ARRAY_SIZE(byt_rt5640_mono_spk_map));
466 	} else {
467 		ret = snd_soc_dapm_add_routes(&card->dapm,
468 					byt_rt5640_stereo_spk_map,
469 					ARRAY_SIZE(byt_rt5640_stereo_spk_map));
470 	}
471 	if (ret)
472 		return ret;
473 
474 	if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC) {
475 		snd_soc_update_bits(codec,  RT5640_IN1_IN2, RT5640_IN_DF1,
476 				    RT5640_IN_DF1);
477 	}
478 
479 	if (byt_rt5640_quirk & BYT_RT5640_DMIC_EN) {
480 		ret = rt5640_dmic_enable(codec, 0, 0);
481 		if (ret)
482 			return ret;
483 	}
484 
485 	snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
486 	snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
487 
488 	if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && priv->mclk) {
489 		/*
490 		 * The firmware might enable the clock at
491 		 * boot (this information may or may not
492 		 * be reflected in the enable clock register).
493 		 * To change the rate we must disable the clock
494 		 * first to cover these cases. Due to common
495 		 * clock framework restrictions that do not allow
496 		 * to disable a clock that has not been enabled,
497 		 * we need to enable the clock first.
498 		 */
499 		ret = clk_prepare_enable(priv->mclk);
500 		if (!ret)
501 			clk_disable_unprepare(priv->mclk);
502 
503 		if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
504 			ret = clk_set_rate(priv->mclk, 25000000);
505 		else
506 			ret = clk_set_rate(priv->mclk, 19200000);
507 
508 		if (ret)
509 			dev_err(card->dev, "unable to set MCLK rate\n");
510 	}
511 
512 	return ret;
513 }
514 
515 static const struct snd_soc_pcm_stream byt_rt5640_dai_params = {
516 	.formats = SNDRV_PCM_FMTBIT_S24_LE,
517 	.rate_min = 48000,
518 	.rate_max = 48000,
519 	.channels_min = 2,
520 	.channels_max = 2,
521 };
522 
523 static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd,
524 			    struct snd_pcm_hw_params *params)
525 {
526 	struct snd_interval *rate = hw_param_interval(params,
527 			SNDRV_PCM_HW_PARAM_RATE);
528 	struct snd_interval *channels = hw_param_interval(params,
529 						SNDRV_PCM_HW_PARAM_CHANNELS);
530 	int ret;
531 
532 	/* The DSP will covert the FE rate to 48k, stereo */
533 	rate->min = rate->max = 48000;
534 	channels->min = channels->max = 2;
535 
536 	if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
537 		(byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
538 
539 		/* set SSP0 to 16-bit */
540 		params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
541 
542 		/*
543 		 * Default mode for SSP configuration is TDM 4 slot, override config
544 		 * with explicit setting to I2S 2ch 16-bit. The word length is set with
545 		 * dai_set_tdm_slot() since there is no other API exposed
546 		 */
547 		ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
548 					SND_SOC_DAIFMT_I2S     |
549 					SND_SOC_DAIFMT_NB_IF   |
550 					SND_SOC_DAIFMT_CBS_CFS
551 			);
552 		if (ret < 0) {
553 			dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
554 			return ret;
555 		}
556 
557 		ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16);
558 		if (ret < 0) {
559 			dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
560 			return ret;
561 		}
562 
563 	} else {
564 
565 		/* set SSP2 to 24-bit */
566 		params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
567 
568 		/*
569 		 * Default mode for SSP configuration is TDM 4 slot, override config
570 		 * with explicit setting to I2S 2ch 24-bit. The word length is set with
571 		 * dai_set_tdm_slot() since there is no other API exposed
572 		 */
573 		ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
574 					SND_SOC_DAIFMT_I2S     |
575 					SND_SOC_DAIFMT_NB_IF   |
576 					SND_SOC_DAIFMT_CBS_CFS
577 			);
578 		if (ret < 0) {
579 			dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
580 			return ret;
581 		}
582 
583 		ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 24);
584 		if (ret < 0) {
585 			dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
586 			return ret;
587 		}
588 	}
589 	return 0;
590 }
591 
592 static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream)
593 {
594 	return snd_pcm_hw_constraint_single(substream->runtime,
595 			SNDRV_PCM_HW_PARAM_RATE, 48000);
596 }
597 
598 static const struct snd_soc_ops byt_rt5640_aif1_ops = {
599 	.startup = byt_rt5640_aif1_startup,
600 };
601 
602 static const struct snd_soc_ops byt_rt5640_be_ssp2_ops = {
603 	.hw_params = byt_rt5640_aif1_hw_params,
604 };
605 
606 static struct snd_soc_dai_link byt_rt5640_dais[] = {
607 	[MERR_DPCM_AUDIO] = {
608 		.name = "Baytrail Audio Port",
609 		.stream_name = "Baytrail Audio",
610 		.cpu_dai_name = "media-cpu-dai",
611 		.codec_dai_name = "snd-soc-dummy-dai",
612 		.codec_name = "snd-soc-dummy",
613 		.platform_name = "sst-mfld-platform",
614 		.ignore_suspend = 1,
615 		.dynamic = 1,
616 		.dpcm_playback = 1,
617 		.dpcm_capture = 1,
618 		.ops = &byt_rt5640_aif1_ops,
619 	},
620 	[MERR_DPCM_DEEP_BUFFER] = {
621 		.name = "Deep-Buffer Audio Port",
622 		.stream_name = "Deep-Buffer Audio",
623 		.cpu_dai_name = "deepbuffer-cpu-dai",
624 		.codec_dai_name = "snd-soc-dummy-dai",
625 		.codec_name = "snd-soc-dummy",
626 		.platform_name = "sst-mfld-platform",
627 		.ignore_suspend = 1,
628 		.nonatomic = true,
629 		.dynamic = 1,
630 		.dpcm_playback = 1,
631 		.ops = &byt_rt5640_aif1_ops,
632 	},
633 	[MERR_DPCM_COMPR] = {
634 		.name = "Baytrail Compressed Port",
635 		.stream_name = "Baytrail Compress",
636 		.cpu_dai_name = "compress-cpu-dai",
637 		.codec_dai_name = "snd-soc-dummy-dai",
638 		.codec_name = "snd-soc-dummy",
639 		.platform_name = "sst-mfld-platform",
640 	},
641 		/* back ends */
642 	{
643 		.name = "SSP2-Codec",
644 		.id = 1,
645 		.cpu_dai_name = "ssp2-port", /* overwritten for ssp0 routing */
646 		.platform_name = "sst-mfld-platform",
647 		.no_pcm = 1,
648 		.codec_dai_name = "rt5640-aif1", /* changed w/ quirk */
649 		.codec_name = "i2c-10EC5640:00", /* overwritten with HID */
650 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
651 						| SND_SOC_DAIFMT_CBS_CFS,
652 		.be_hw_params_fixup = byt_rt5640_codec_fixup,
653 		.ignore_suspend = 1,
654 		.dpcm_playback = 1,
655 		.dpcm_capture = 1,
656 		.init = byt_rt5640_init,
657 		.ops = &byt_rt5640_be_ssp2_ops,
658 	},
659 };
660 
661 /* SoC card */
662 static struct snd_soc_card byt_rt5640_card = {
663 	.name = "bytcr-rt5640",
664 	.owner = THIS_MODULE,
665 	.dai_link = byt_rt5640_dais,
666 	.num_links = ARRAY_SIZE(byt_rt5640_dais),
667 	.dapm_widgets = byt_rt5640_widgets,
668 	.num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets),
669 	.dapm_routes = byt_rt5640_audio_map,
670 	.num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map),
671 	.fully_routed = true,
672 };
673 
674 static char byt_rt5640_codec_name[16]; /* i2c-<HID>:00 with HID being 8 chars */
675 static char byt_rt5640_codec_aif_name[12]; /*  = "rt5640-aif[1|2]" */
676 static char byt_rt5640_cpu_dai_name[10]; /*  = "ssp[0|2]-port" */
677 
678 static bool is_valleyview(void)
679 {
680 	static const struct x86_cpu_id cpu_ids[] = {
681 		{ X86_VENDOR_INTEL, 6, 55 }, /* Valleyview, Bay Trail */
682 		{}
683 	};
684 
685 	if (!x86_match_cpu(cpu_ids))
686 		return false;
687 	return true;
688 }
689 
690 struct acpi_chan_package {   /* ACPICA seems to require 64 bit integers */
691 	u64 aif_value;       /* 1: AIF1, 2: AIF2 */
692 	u64 mclock_value;    /* usually 25MHz (0x17d7940), ignored */
693 };
694 
695 static int snd_byt_rt5640_mc_probe(struct platform_device *pdev)
696 {
697 	int ret_val = 0;
698 	struct sst_acpi_mach *mach;
699 	const char *i2c_name = NULL;
700 	int i;
701 	int dai_index;
702 	struct byt_rt5640_private *priv;
703 	bool is_bytcr = false;
704 
705 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_ATOMIC);
706 	if (!priv)
707 		return -ENOMEM;
708 
709 	/* register the soc card */
710 	byt_rt5640_card.dev = &pdev->dev;
711 	mach = byt_rt5640_card.dev->platform_data;
712 	snd_soc_card_set_drvdata(&byt_rt5640_card, priv);
713 
714 	/* fix index of codec dai */
715 	dai_index = MERR_DPCM_COMPR + 1;
716 	for (i = 0; i < ARRAY_SIZE(byt_rt5640_dais); i++) {
717 		if (!strcmp(byt_rt5640_dais[i].codec_name, "i2c-10EC5640:00")) {
718 			dai_index = i;
719 			break;
720 		}
721 	}
722 
723 	/* fixup codec name based on HID */
724 	i2c_name = sst_acpi_find_name_from_hid(mach->id);
725 	if (i2c_name != NULL) {
726 		snprintf(byt_rt5640_codec_name, sizeof(byt_rt5640_codec_name),
727 			"%s%s", "i2c-", i2c_name);
728 
729 		byt_rt5640_dais[dai_index].codec_name = byt_rt5640_codec_name;
730 	}
731 
732 	/*
733 	 * swap SSP0 if bytcr is detected
734 	 * (will be overridden if DMI quirk is detected)
735 	 */
736 	if (is_valleyview()) {
737 		struct sst_platform_info *p_info = mach->pdata;
738 		const struct sst_res_info *res_info = p_info->res_info;
739 
740 		if (res_info->acpi_ipc_irq_index == 0)
741 			is_bytcr = true;
742 	}
743 
744 	if (is_bytcr) {
745 		/*
746 		 * Baytrail CR platforms may have CHAN package in BIOS, try
747 		 * to find relevant routing quirk based as done on Windows
748 		 * platforms. We have to read the information directly from the
749 		 * BIOS, at this stage the card is not created and the links
750 		 * with the codec driver/pdata are non-existent
751 		 */
752 
753 		struct acpi_chan_package chan_package;
754 
755 		/* format specified: 2 64-bit integers */
756 		struct acpi_buffer format = {sizeof("NN"), "NN"};
757 		struct acpi_buffer state = {0, NULL};
758 		struct sst_acpi_package_context pkg_ctx;
759 		bool pkg_found = false;
760 
761 		state.length = sizeof(chan_package);
762 		state.pointer = &chan_package;
763 
764 		pkg_ctx.name = "CHAN";
765 		pkg_ctx.length = 2;
766 		pkg_ctx.format = &format;
767 		pkg_ctx.state = &state;
768 		pkg_ctx.data_valid = false;
769 
770 		pkg_found = sst_acpi_find_package_from_hid(mach->id, &pkg_ctx);
771 		if (pkg_found) {
772 			if (chan_package.aif_value == 1) {
773 				dev_info(&pdev->dev, "BIOS Routing: AIF1 connected\n");
774 				byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF1;
775 			} else  if (chan_package.aif_value == 2) {
776 				dev_info(&pdev->dev, "BIOS Routing: AIF2 connected\n");
777 				byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
778 			} else {
779 				dev_info(&pdev->dev, "BIOS Routing isn't valid, ignored\n");
780 				pkg_found = false;
781 			}
782 		}
783 
784 		if (!pkg_found) {
785 			/* no BIOS indications, assume SSP0-AIF2 connection */
786 			byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
787 		}
788 
789 		/* change defaults for Baytrail-CR capture */
790 		byt_rt5640_quirk |= BYT_RT5640_IN1_MAP;
791 		byt_rt5640_quirk |= BYT_RT5640_DIFF_MIC;
792 	} else {
793 		byt_rt5640_quirk |= (BYT_RT5640_DMIC1_MAP |
794 				BYT_RT5640_DMIC_EN);
795 	}
796 
797 	/* check quirks before creating card */
798 	dmi_check_system(byt_rt5640_quirk_table);
799 	log_quirks(&pdev->dev);
800 
801 	if ((byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) ||
802 	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
803 
804 		/* fixup codec aif name */
805 		snprintf(byt_rt5640_codec_aif_name,
806 			sizeof(byt_rt5640_codec_aif_name),
807 			"%s", "rt5640-aif2");
808 
809 		byt_rt5640_dais[dai_index].codec_dai_name =
810 			byt_rt5640_codec_aif_name;
811 	}
812 
813 	if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
814 	    (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
815 
816 		/* fixup cpu dai name name */
817 		snprintf(byt_rt5640_cpu_dai_name,
818 			sizeof(byt_rt5640_cpu_dai_name),
819 			"%s", "ssp0-port");
820 
821 		byt_rt5640_dais[dai_index].cpu_dai_name =
822 			byt_rt5640_cpu_dai_name;
823 	}
824 
825 	if ((byt_rt5640_quirk & BYT_RT5640_MCLK_EN) && (is_valleyview())) {
826 		priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
827 		if (IS_ERR(priv->mclk)) {
828 			ret_val = PTR_ERR(priv->mclk);
829 
830 			dev_err(&pdev->dev,
831 				"Failed to get MCLK from pmc_plt_clk_3: %d\n",
832 				ret_val);
833 
834 			/*
835 			 * Fall back to bit clock usage for -ENOENT (clock not
836 			 * available likely due to missing dependencies), bail
837 			 * for all other errors, including -EPROBE_DEFER
838 			 */
839 			if (ret_val != -ENOENT)
840 				return ret_val;
841 			byt_rt5640_quirk &= ~BYT_RT5640_MCLK_EN;
842 		}
843 	}
844 
845 	ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5640_card);
846 
847 	if (ret_val) {
848 		dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
849 			ret_val);
850 		return ret_val;
851 	}
852 	platform_set_drvdata(pdev, &byt_rt5640_card);
853 	return ret_val;
854 }
855 
856 static struct platform_driver snd_byt_rt5640_mc_driver = {
857 	.driver = {
858 		.name = "bytcr_rt5640",
859 		.pm = &snd_soc_pm_ops,
860 	},
861 	.probe = snd_byt_rt5640_mc_probe,
862 };
863 
864 module_platform_driver(snd_byt_rt5640_mc_driver);
865 
866 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
867 MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
868 MODULE_LICENSE("GPL v2");
869 MODULE_ALIAS("platform:bytcr_rt5640");
870