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