1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * byt_cr_dpcm_rt5640.c - ASoc Machine driver for Intel Byt CR platform
4 *
5 * Copyright (C) 2014 Intel Corp
6 * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 */
11
12 #include <linux/i2c.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/platform_device.h>
17 #include <linux/acpi.h>
18 #include <linux/clk.h>
19 #include <linux/device.h>
20 #include <linux/dmi.h>
21 #include <linux/input.h>
22 #include <linux/slab.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/soc.h>
26 #include <sound/jack.h>
27 #include <sound/soc-acpi.h>
28 #include <dt-bindings/sound/rt5640.h>
29 #include "../../codecs/rt5640.h"
30 #include "../atom/sst-atom-controls.h"
31 #include "../common/soc-intel-quirks.h"
32
33 enum {
34 BYT_RT5640_DMIC1_MAP,
35 BYT_RT5640_DMIC2_MAP,
36 BYT_RT5640_IN1_MAP,
37 BYT_RT5640_IN3_MAP,
38 };
39
40 enum {
41 BYT_RT5640_JD_SRC_GPIO1 = (RT5640_JD_SRC_GPIO1 << 4),
42 BYT_RT5640_JD_SRC_JD1_IN4P = (RT5640_JD_SRC_JD1_IN4P << 4),
43 BYT_RT5640_JD_SRC_JD2_IN4N = (RT5640_JD_SRC_JD2_IN4N << 4),
44 BYT_RT5640_JD_SRC_GPIO2 = (RT5640_JD_SRC_GPIO2 << 4),
45 BYT_RT5640_JD_SRC_GPIO3 = (RT5640_JD_SRC_GPIO3 << 4),
46 BYT_RT5640_JD_SRC_GPIO4 = (RT5640_JD_SRC_GPIO4 << 4),
47 };
48
49 enum {
50 BYT_RT5640_OVCD_TH_600UA = (6 << 8),
51 BYT_RT5640_OVCD_TH_1500UA = (15 << 8),
52 BYT_RT5640_OVCD_TH_2000UA = (20 << 8),
53 };
54
55 enum {
56 BYT_RT5640_OVCD_SF_0P5 = (RT5640_OVCD_SF_0P5 << 13),
57 BYT_RT5640_OVCD_SF_0P75 = (RT5640_OVCD_SF_0P75 << 13),
58 BYT_RT5640_OVCD_SF_1P0 = (RT5640_OVCD_SF_1P0 << 13),
59 BYT_RT5640_OVCD_SF_1P5 = (RT5640_OVCD_SF_1P5 << 13),
60 };
61
62 #define BYT_RT5640_MAP(quirk) ((quirk) & GENMASK(3, 0))
63 #define BYT_RT5640_JDSRC(quirk) (((quirk) & GENMASK(7, 4)) >> 4)
64 #define BYT_RT5640_OVCD_TH(quirk) (((quirk) & GENMASK(12, 8)) >> 8)
65 #define BYT_RT5640_OVCD_SF(quirk) (((quirk) & GENMASK(14, 13)) >> 13)
66 #define BYT_RT5640_JD_NOT_INV BIT(16)
67 #define BYT_RT5640_MONO_SPEAKER BIT(17)
68 #define BYT_RT5640_DIFF_MIC BIT(18) /* default is single-ended */
69 #define BYT_RT5640_SSP2_AIF2 BIT(19) /* default is using AIF1 */
70 #define BYT_RT5640_SSP0_AIF1 BIT(20)
71 #define BYT_RT5640_SSP0_AIF2 BIT(21)
72 #define BYT_RT5640_MCLK_EN BIT(22)
73 #define BYT_RT5640_MCLK_25MHZ BIT(23)
74 #define BYT_RT5640_NO_SPEAKERS BIT(24)
75
76 #define BYTCR_INPUT_DEFAULTS \
77 (BYT_RT5640_IN3_MAP | \
78 BYT_RT5640_JD_SRC_JD1_IN4P | \
79 BYT_RT5640_OVCD_TH_2000UA | \
80 BYT_RT5640_OVCD_SF_0P75 | \
81 BYT_RT5640_DIFF_MIC)
82
83 /* in-diff or dmic-pin + jdsrc + ovcd-th + -sf + jd-inv + terminating entry */
84 #define MAX_NO_PROPS 6
85
86 struct byt_rt5640_private {
87 struct snd_soc_jack jack;
88 struct clk *mclk;
89 };
90 static bool is_bytcr;
91
92 static unsigned long byt_rt5640_quirk = BYT_RT5640_MCLK_EN;
93 static int quirk_override = -1;
94 module_param_named(quirk, quirk_override, int, 0444);
95 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
96
log_quirks(struct device * dev)97 static void log_quirks(struct device *dev)
98 {
99 int map;
100 bool has_mclk = false;
101 bool has_ssp0 = false;
102 bool has_ssp0_aif1 = false;
103 bool has_ssp0_aif2 = false;
104 bool has_ssp2_aif2 = false;
105
106 map = BYT_RT5640_MAP(byt_rt5640_quirk);
107 switch (map) {
108 case BYT_RT5640_DMIC1_MAP:
109 dev_info(dev, "quirk DMIC1_MAP enabled\n");
110 break;
111 case BYT_RT5640_DMIC2_MAP:
112 dev_info(dev, "quirk DMIC2_MAP enabled\n");
113 break;
114 case BYT_RT5640_IN1_MAP:
115 dev_info(dev, "quirk IN1_MAP enabled\n");
116 break;
117 case BYT_RT5640_IN3_MAP:
118 dev_info(dev, "quirk IN3_MAP enabled\n");
119 break;
120 default:
121 dev_err(dev, "quirk map 0x%x is not supported, microphone input will not work\n", map);
122 break;
123 }
124 if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
125 dev_info(dev, "quirk realtek,jack-detect-source %ld\n",
126 BYT_RT5640_JDSRC(byt_rt5640_quirk));
127 dev_info(dev, "quirk realtek,over-current-threshold-microamp %ld\n",
128 BYT_RT5640_OVCD_TH(byt_rt5640_quirk) * 100);
129 dev_info(dev, "quirk realtek,over-current-scale-factor %ld\n",
130 BYT_RT5640_OVCD_SF(byt_rt5640_quirk));
131 }
132 if (byt_rt5640_quirk & BYT_RT5640_JD_NOT_INV)
133 dev_info(dev, "quirk JD_NOT_INV enabled\n");
134 if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER)
135 dev_info(dev, "quirk MONO_SPEAKER enabled\n");
136 if (byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS)
137 dev_info(dev, "quirk NO_SPEAKERS enabled\n");
138 if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
139 dev_info(dev, "quirk DIFF_MIC enabled\n");
140 if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
141 dev_info(dev, "quirk SSP0_AIF1 enabled\n");
142 has_ssp0 = true;
143 has_ssp0_aif1 = true;
144 }
145 if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
146 dev_info(dev, "quirk SSP0_AIF2 enabled\n");
147 has_ssp0 = true;
148 has_ssp0_aif2 = true;
149 }
150 if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
151 dev_info(dev, "quirk SSP2_AIF2 enabled\n");
152 has_ssp2_aif2 = true;
153 }
154 if (is_bytcr && !has_ssp0)
155 dev_err(dev, "Invalid routing, bytcr detected but no SSP0-based quirk, audio cannot work with SSP2 on bytcr\n");
156 if (has_ssp0_aif1 && has_ssp0_aif2)
157 dev_err(dev, "Invalid routing, SSP0 cannot be connected to both AIF1 and AIF2\n");
158 if (has_ssp0 && has_ssp2_aif2)
159 dev_err(dev, "Invalid routing, cannot have both SSP0 and SSP2 connected to codec\n");
160
161 if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
162 dev_info(dev, "quirk MCLK_EN enabled\n");
163 has_mclk = true;
164 }
165 if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
166 if (has_mclk)
167 dev_info(dev, "quirk MCLK_25MHZ enabled\n");
168 else
169 dev_err(dev, "quirk MCLK_25MHZ enabled but quirk MCLK not selected, will be ignored\n");
170 }
171 }
172
byt_rt5640_prepare_and_enable_pll1(struct snd_soc_dai * codec_dai,int rate)173 static int byt_rt5640_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai,
174 int rate)
175 {
176 int ret;
177
178 /* Configure the PLL before selecting it */
179 if (!(byt_rt5640_quirk & BYT_RT5640_MCLK_EN)) {
180 /* use bitclock as PLL input */
181 if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
182 (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
183 /* 2x16 bit slots on SSP0 */
184 ret = snd_soc_dai_set_pll(codec_dai, 0,
185 RT5640_PLL1_S_BCLK1,
186 rate * 32, rate * 512);
187 } else {
188 /* 2x15 bit slots on SSP2 */
189 ret = snd_soc_dai_set_pll(codec_dai, 0,
190 RT5640_PLL1_S_BCLK1,
191 rate * 50, rate * 512);
192 }
193 } else {
194 if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ) {
195 ret = snd_soc_dai_set_pll(codec_dai, 0,
196 RT5640_PLL1_S_MCLK,
197 25000000, rate * 512);
198 } else {
199 ret = snd_soc_dai_set_pll(codec_dai, 0,
200 RT5640_PLL1_S_MCLK,
201 19200000, rate * 512);
202 }
203 }
204
205 if (ret < 0) {
206 dev_err(codec_dai->component->dev, "can't set pll: %d\n", ret);
207 return ret;
208 }
209
210 ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
211 rate * 512, SND_SOC_CLOCK_IN);
212 if (ret < 0) {
213 dev_err(codec_dai->component->dev, "can't set clock %d\n", ret);
214 return ret;
215 }
216
217 return 0;
218 }
219
220 #define BYT_CODEC_DAI1 "rt5640-aif1"
221 #define BYT_CODEC_DAI2 "rt5640-aif2"
222
platform_clock_control(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)223 static int platform_clock_control(struct snd_soc_dapm_widget *w,
224 struct snd_kcontrol *k, int event)
225 {
226 struct snd_soc_dapm_context *dapm = w->dapm;
227 struct snd_soc_card *card = dapm->card;
228 struct snd_soc_dai *codec_dai;
229 struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
230 int ret;
231
232 codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1);
233 if (!codec_dai)
234 codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI2);
235
236 if (!codec_dai) {
237 dev_err(card->dev,
238 "Codec dai not found; Unable to set platform clock\n");
239 return -EIO;
240 }
241
242 if (SND_SOC_DAPM_EVENT_ON(event)) {
243 if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
244 ret = clk_prepare_enable(priv->mclk);
245 if (ret < 0) {
246 dev_err(card->dev,
247 "could not configure MCLK state\n");
248 return ret;
249 }
250 }
251 ret = byt_rt5640_prepare_and_enable_pll1(codec_dai, 48000);
252 } else {
253 /*
254 * Set codec clock source to internal clock before
255 * turning off the platform clock. Codec needs clock
256 * for Jack detection and button press
257 */
258 ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_RCCLK,
259 48000 * 512,
260 SND_SOC_CLOCK_IN);
261 if (!ret) {
262 if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN)
263 clk_disable_unprepare(priv->mclk);
264 }
265 }
266
267 if (ret < 0) {
268 dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
269 return ret;
270 }
271
272 return 0;
273 }
274
275 static const struct snd_soc_dapm_widget byt_rt5640_widgets[] = {
276 SND_SOC_DAPM_HP("Headphone", NULL),
277 SND_SOC_DAPM_MIC("Headset Mic", NULL),
278 SND_SOC_DAPM_MIC("Internal Mic", NULL),
279 SND_SOC_DAPM_SPK("Speaker", NULL),
280 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
281 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
282 SND_SOC_DAPM_POST_PMD),
283
284 };
285
286 static const struct snd_soc_dapm_route byt_rt5640_audio_map[] = {
287 {"Headphone", NULL, "Platform Clock"},
288 {"Headset Mic", NULL, "Platform Clock"},
289 {"Headset Mic", NULL, "MICBIAS1"},
290 {"IN2P", NULL, "Headset Mic"},
291 {"Headphone", NULL, "HPOL"},
292 {"Headphone", NULL, "HPOR"},
293 };
294
295 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic1_map[] = {
296 {"Internal Mic", NULL, "Platform Clock"},
297 {"DMIC1", NULL, "Internal Mic"},
298 };
299
300 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic2_map[] = {
301 {"Internal Mic", NULL, "Platform Clock"},
302 {"DMIC2", NULL, "Internal Mic"},
303 };
304
305 static const struct snd_soc_dapm_route byt_rt5640_intmic_in1_map[] = {
306 {"Internal Mic", NULL, "Platform Clock"},
307 {"Internal Mic", NULL, "MICBIAS1"},
308 {"IN1P", NULL, "Internal Mic"},
309 };
310
311 static const struct snd_soc_dapm_route byt_rt5640_intmic_in3_map[] = {
312 {"Internal Mic", NULL, "Platform Clock"},
313 {"Internal Mic", NULL, "MICBIAS1"},
314 {"IN3P", NULL, "Internal Mic"},
315 };
316
317 static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif1_map[] = {
318 {"ssp2 Tx", NULL, "codec_out0"},
319 {"ssp2 Tx", NULL, "codec_out1"},
320 {"codec_in0", NULL, "ssp2 Rx"},
321 {"codec_in1", NULL, "ssp2 Rx"},
322
323 {"AIF1 Playback", NULL, "ssp2 Tx"},
324 {"ssp2 Rx", NULL, "AIF1 Capture"},
325 };
326
327 static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif2_map[] = {
328 {"ssp2 Tx", NULL, "codec_out0"},
329 {"ssp2 Tx", NULL, "codec_out1"},
330 {"codec_in0", NULL, "ssp2 Rx"},
331 {"codec_in1", NULL, "ssp2 Rx"},
332
333 {"AIF2 Playback", NULL, "ssp2 Tx"},
334 {"ssp2 Rx", NULL, "AIF2 Capture"},
335 };
336
337 static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif1_map[] = {
338 {"ssp0 Tx", NULL, "modem_out"},
339 {"modem_in", NULL, "ssp0 Rx"},
340
341 {"AIF1 Playback", NULL, "ssp0 Tx"},
342 {"ssp0 Rx", NULL, "AIF1 Capture"},
343 };
344
345 static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif2_map[] = {
346 {"ssp0 Tx", NULL, "modem_out"},
347 {"modem_in", NULL, "ssp0 Rx"},
348
349 {"AIF2 Playback", NULL, "ssp0 Tx"},
350 {"ssp0 Rx", NULL, "AIF2 Capture"},
351 };
352
353 static const struct snd_soc_dapm_route byt_rt5640_stereo_spk_map[] = {
354 {"Speaker", NULL, "Platform Clock"},
355 {"Speaker", NULL, "SPOLP"},
356 {"Speaker", NULL, "SPOLN"},
357 {"Speaker", NULL, "SPORP"},
358 {"Speaker", NULL, "SPORN"},
359 };
360
361 static const struct snd_soc_dapm_route byt_rt5640_mono_spk_map[] = {
362 {"Speaker", NULL, "Platform Clock"},
363 {"Speaker", NULL, "SPOLP"},
364 {"Speaker", NULL, "SPOLN"},
365 };
366
367 static const struct snd_kcontrol_new byt_rt5640_controls[] = {
368 SOC_DAPM_PIN_SWITCH("Headphone"),
369 SOC_DAPM_PIN_SWITCH("Headset Mic"),
370 SOC_DAPM_PIN_SWITCH("Internal Mic"),
371 SOC_DAPM_PIN_SWITCH("Speaker"),
372 };
373
374 static struct snd_soc_jack_pin rt5640_pins[] = {
375 {
376 .pin = "Headphone",
377 .mask = SND_JACK_HEADPHONE,
378 },
379 {
380 .pin = "Headset Mic",
381 .mask = SND_JACK_MICROPHONE,
382 },
383 };
384
byt_rt5640_aif1_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)385 static int byt_rt5640_aif1_hw_params(struct snd_pcm_substream *substream,
386 struct snd_pcm_hw_params *params)
387 {
388 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
389 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
390
391 return byt_rt5640_prepare_and_enable_pll1(dai, params_rate(params));
392 }
393
394 /* Please keep this list alphabetically sorted */
395 static const struct dmi_system_id byt_rt5640_quirk_table[] = {
396 { /* Acer Iconia Tab 8 W1-810 */
397 .matches = {
398 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Acer"),
399 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Iconia W1-810"),
400 },
401 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
402 BYT_RT5640_JD_SRC_JD1_IN4P |
403 BYT_RT5640_OVCD_TH_1500UA |
404 BYT_RT5640_OVCD_SF_0P75 |
405 BYT_RT5640_SSP0_AIF1 |
406 BYT_RT5640_MCLK_EN),
407 },
408 { /* Acer One 10 S1002 */
409 .matches = {
410 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
411 DMI_MATCH(DMI_PRODUCT_NAME, "One S1002"),
412 },
413 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
414 BYT_RT5640_JD_SRC_JD2_IN4N |
415 BYT_RT5640_OVCD_TH_2000UA |
416 BYT_RT5640_OVCD_SF_0P75 |
417 BYT_RT5640_DIFF_MIC |
418 BYT_RT5640_SSP0_AIF2 |
419 BYT_RT5640_MCLK_EN),
420 },
421 {
422 .matches = {
423 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
424 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire SW5-012"),
425 },
426 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
427 BYT_RT5640_JD_SRC_JD2_IN4N |
428 BYT_RT5640_OVCD_TH_2000UA |
429 BYT_RT5640_OVCD_SF_0P75 |
430 BYT_RT5640_SSP0_AIF1 |
431 BYT_RT5640_MCLK_EN),
432 },
433 {
434 .matches = {
435 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
436 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 80 Cesium"),
437 },
438 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
439 BYT_RT5640_MONO_SPEAKER |
440 BYT_RT5640_SSP0_AIF1 |
441 BYT_RT5640_MCLK_EN),
442 },
443 {
444 .matches = {
445 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ARCHOS"),
446 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ARCHOS 140 CESIUM"),
447 },
448 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
449 BYT_RT5640_JD_SRC_JD2_IN4N |
450 BYT_RT5640_OVCD_TH_2000UA |
451 BYT_RT5640_OVCD_SF_0P75 |
452 BYT_RT5640_SSP0_AIF1 |
453 BYT_RT5640_MCLK_EN),
454 },
455 {
456 .matches = {
457 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
458 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "ME176C"),
459 },
460 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
461 BYT_RT5640_JD_SRC_JD2_IN4N |
462 BYT_RT5640_OVCD_TH_2000UA |
463 BYT_RT5640_OVCD_SF_0P75 |
464 BYT_RT5640_SSP0_AIF1 |
465 BYT_RT5640_MCLK_EN),
466 },
467 {
468 .matches = {
469 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
470 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
471 },
472 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
473 BYT_RT5640_JD_SRC_JD2_IN4N |
474 BYT_RT5640_OVCD_TH_2000UA |
475 BYT_RT5640_OVCD_SF_0P75 |
476 BYT_RT5640_MCLK_EN),
477 },
478 {
479 .matches = {
480 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
481 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TAF"),
482 },
483 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
484 BYT_RT5640_JD_SRC_JD2_IN4N |
485 BYT_RT5640_OVCD_TH_2000UA |
486 BYT_RT5640_OVCD_SF_0P75 |
487 BYT_RT5640_MONO_SPEAKER |
488 BYT_RT5640_DIFF_MIC |
489 BYT_RT5640_SSP0_AIF2 |
490 BYT_RT5640_MCLK_EN),
491 },
492 { /* Chuwi Vi8 (CWI506) */
493 .matches = {
494 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
495 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "i86"),
496 /* The above are too generic, also match BIOS info */
497 DMI_MATCH(DMI_BIOS_VERSION, "CHUWI.D86JLBNR"),
498 },
499 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
500 BYT_RT5640_MONO_SPEAKER |
501 BYT_RT5640_SSP0_AIF1 |
502 BYT_RT5640_MCLK_EN),
503 },
504 {
505 /* Chuwi Vi10 (CWI505) */
506 .matches = {
507 DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
508 DMI_MATCH(DMI_BOARD_NAME, "BYT-PF02"),
509 DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
510 DMI_MATCH(DMI_PRODUCT_NAME, "S165"),
511 },
512 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
513 BYT_RT5640_JD_SRC_JD2_IN4N |
514 BYT_RT5640_OVCD_TH_2000UA |
515 BYT_RT5640_OVCD_SF_0P75 |
516 BYT_RT5640_DIFF_MIC |
517 BYT_RT5640_SSP0_AIF1 |
518 BYT_RT5640_MCLK_EN),
519 },
520 {
521 /* Chuwi Hi8 (CWI509) */
522 .matches = {
523 DMI_MATCH(DMI_BOARD_VENDOR, "Hampoo"),
524 DMI_MATCH(DMI_BOARD_NAME, "BYT-PA03C"),
525 DMI_MATCH(DMI_SYS_VENDOR, "ilife"),
526 DMI_MATCH(DMI_PRODUCT_NAME, "S806"),
527 },
528 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
529 BYT_RT5640_JD_SRC_JD2_IN4N |
530 BYT_RT5640_OVCD_TH_2000UA |
531 BYT_RT5640_OVCD_SF_0P75 |
532 BYT_RT5640_MONO_SPEAKER |
533 BYT_RT5640_DIFF_MIC |
534 BYT_RT5640_SSP0_AIF1 |
535 BYT_RT5640_MCLK_EN),
536 },
537 {
538 .matches = {
539 DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
540 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
541 },
542 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP),
543 },
544 { /* Connect Tablet 9 */
545 .matches = {
546 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Connect"),
547 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Tablet 9"),
548 },
549 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
550 BYT_RT5640_MONO_SPEAKER |
551 BYT_RT5640_SSP0_AIF1 |
552 BYT_RT5640_MCLK_EN),
553 },
554 {
555 .matches = {
556 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
557 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"),
558 },
559 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
560 BYT_RT5640_JD_SRC_JD2_IN4N |
561 BYT_RT5640_OVCD_TH_2000UA |
562 BYT_RT5640_OVCD_SF_0P75 |
563 BYT_RT5640_MONO_SPEAKER |
564 BYT_RT5640_MCLK_EN),
565 },
566 { /* Estar Beauty HD MID 7316R */
567 .matches = {
568 DMI_MATCH(DMI_SYS_VENDOR, "Estar"),
569 DMI_MATCH(DMI_PRODUCT_NAME, "eSTAR BEAUTY HD Intel Quad core"),
570 },
571 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
572 BYT_RT5640_MONO_SPEAKER |
573 BYT_RT5640_SSP0_AIF1 |
574 BYT_RT5640_MCLK_EN),
575 },
576 { /* Glavey TM800A550L */
577 .matches = {
578 DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
579 DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
580 /* Above strings are too generic, also match on BIOS version */
581 DMI_MATCH(DMI_BIOS_VERSION, "ZY-8-BI-PX4S70VTR400-X423B-005-D"),
582 },
583 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
584 BYT_RT5640_SSP0_AIF1 |
585 BYT_RT5640_MCLK_EN),
586 },
587 {
588 .matches = {
589 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
590 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP ElitePad 1000 G2"),
591 },
592 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
593 BYT_RT5640_MCLK_EN),
594 },
595 { /* HP Pavilion x2 10-k0XX, 10-n0XX */
596 .matches = {
597 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
598 DMI_MATCH(DMI_PRODUCT_NAME, "HP Pavilion x2 Detachable"),
599 },
600 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
601 BYT_RT5640_JD_SRC_JD2_IN4N |
602 BYT_RT5640_OVCD_TH_1500UA |
603 BYT_RT5640_OVCD_SF_0P75 |
604 BYT_RT5640_SSP0_AIF1 |
605 BYT_RT5640_MCLK_EN),
606 },
607 { /* HP Pavilion x2 10-p0XX */
608 .matches = {
609 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
610 DMI_MATCH(DMI_PRODUCT_NAME, "HP x2 Detachable 10-p0XX"),
611 },
612 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
613 BYT_RT5640_JD_SRC_JD1_IN4P |
614 BYT_RT5640_OVCD_TH_2000UA |
615 BYT_RT5640_OVCD_SF_0P75 |
616 BYT_RT5640_MCLK_EN),
617 },
618 { /* HP Stream 7 */
619 .matches = {
620 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
621 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP Stream 7 Tablet"),
622 },
623 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
624 BYT_RT5640_MONO_SPEAKER |
625 BYT_RT5640_JD_NOT_INV |
626 BYT_RT5640_SSP0_AIF1 |
627 BYT_RT5640_MCLK_EN),
628 },
629 { /* I.T.Works TW891 */
630 .matches = {
631 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "To be filled by O.E.M."),
632 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TW891"),
633 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "To be filled by O.E.M."),
634 DMI_EXACT_MATCH(DMI_BOARD_NAME, "TW891"),
635 },
636 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
637 BYT_RT5640_MONO_SPEAKER |
638 BYT_RT5640_SSP0_AIF1 |
639 BYT_RT5640_MCLK_EN),
640 },
641 { /* Lamina I8270 / T701BR.SE */
642 .matches = {
643 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Lamina"),
644 DMI_EXACT_MATCH(DMI_BOARD_NAME, "T701BR.SE"),
645 },
646 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
647 BYT_RT5640_MONO_SPEAKER |
648 BYT_RT5640_JD_NOT_INV |
649 BYT_RT5640_SSP0_AIF1 |
650 BYT_RT5640_MCLK_EN),
651 },
652 { /* Lenovo Miix 2 8 */
653 .matches = {
654 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
655 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "20326"),
656 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Hiking"),
657 },
658 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
659 BYT_RT5640_JD_SRC_JD2_IN4N |
660 BYT_RT5640_OVCD_TH_2000UA |
661 BYT_RT5640_OVCD_SF_0P75 |
662 BYT_RT5640_MONO_SPEAKER |
663 BYT_RT5640_MCLK_EN),
664 },
665 { /* Lenovo Miix 3-830 */
666 .matches = {
667 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
668 DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 3-830"),
669 },
670 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
671 BYT_RT5640_JD_SRC_JD2_IN4N |
672 BYT_RT5640_OVCD_TH_2000UA |
673 BYT_RT5640_OVCD_SF_0P75 |
674 BYT_RT5640_MONO_SPEAKER |
675 BYT_RT5640_DIFF_MIC |
676 BYT_RT5640_SSP0_AIF1 |
677 BYT_RT5640_MCLK_EN),
678 },
679 { /* Linx Linx7 tablet */
680 .matches = {
681 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LINX"),
682 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "LINX7"),
683 },
684 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
685 BYT_RT5640_MONO_SPEAKER |
686 BYT_RT5640_JD_NOT_INV |
687 BYT_RT5640_SSP0_AIF1 |
688 BYT_RT5640_MCLK_EN),
689 },
690 { /* MPMAN Converter 9, similar hw as the I.T.Works TW891 2-in-1 */
691 .matches = {
692 DMI_MATCH(DMI_SYS_VENDOR, "MPMAN"),
693 DMI_MATCH(DMI_PRODUCT_NAME, "Converter9"),
694 },
695 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
696 BYT_RT5640_MONO_SPEAKER |
697 BYT_RT5640_SSP0_AIF1 |
698 BYT_RT5640_MCLK_EN),
699 },
700 {
701 /* MPMAN MPWIN895CL */
702 .matches = {
703 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "MPMAN"),
704 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "MPWIN8900CL"),
705 },
706 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
707 BYT_RT5640_MONO_SPEAKER |
708 BYT_RT5640_SSP0_AIF1 |
709 BYT_RT5640_MCLK_EN),
710 },
711 { /* MSI S100 tablet */
712 .matches = {
713 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Micro-Star International Co., Ltd."),
714 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "S100"),
715 },
716 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
717 BYT_RT5640_JD_SRC_JD2_IN4N |
718 BYT_RT5640_OVCD_TH_2000UA |
719 BYT_RT5640_OVCD_SF_0P75 |
720 BYT_RT5640_MONO_SPEAKER |
721 BYT_RT5640_DIFF_MIC |
722 BYT_RT5640_MCLK_EN),
723 },
724 { /* Nuvison/TMax TM800W560 */
725 .matches = {
726 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TMAX"),
727 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TM800W560L"),
728 },
729 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
730 BYT_RT5640_JD_SRC_JD2_IN4N |
731 BYT_RT5640_OVCD_TH_2000UA |
732 BYT_RT5640_OVCD_SF_0P75 |
733 BYT_RT5640_JD_NOT_INV |
734 BYT_RT5640_DIFF_MIC |
735 BYT_RT5640_SSP0_AIF1 |
736 BYT_RT5640_MCLK_EN),
737 },
738 { /* Onda v975w */
739 .matches = {
740 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
741 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
742 /* The above are too generic, also match BIOS info */
743 DMI_EXACT_MATCH(DMI_BIOS_VERSION, "5.6.5"),
744 DMI_EXACT_MATCH(DMI_BIOS_DATE, "07/25/2014"),
745 },
746 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
747 BYT_RT5640_JD_SRC_JD2_IN4N |
748 BYT_RT5640_OVCD_TH_2000UA |
749 BYT_RT5640_OVCD_SF_0P75 |
750 BYT_RT5640_DIFF_MIC |
751 BYT_RT5640_MCLK_EN),
752 },
753 { /* Pipo W4 */
754 .matches = {
755 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
756 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
757 /* The above are too generic, also match BIOS info */
758 DMI_MATCH(DMI_BIOS_VERSION, "V8L_WIN32_CHIPHD"),
759 },
760 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
761 BYT_RT5640_MONO_SPEAKER |
762 BYT_RT5640_SSP0_AIF1 |
763 BYT_RT5640_MCLK_EN),
764 },
765 { /* Point of View Mobii TAB-P800W (V2.0) */
766 .matches = {
767 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
768 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
769 /* The above are too generic, also match BIOS info */
770 DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1014"),
771 DMI_EXACT_MATCH(DMI_BIOS_DATE, "10/24/2014"),
772 },
773 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
774 BYT_RT5640_JD_SRC_JD2_IN4N |
775 BYT_RT5640_OVCD_TH_2000UA |
776 BYT_RT5640_OVCD_SF_0P75 |
777 BYT_RT5640_MONO_SPEAKER |
778 BYT_RT5640_DIFF_MIC |
779 BYT_RT5640_SSP0_AIF2 |
780 BYT_RT5640_MCLK_EN),
781 },
782 { /* Point of View Mobii TAB-P800W (V2.1) */
783 .matches = {
784 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
785 DMI_EXACT_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
786 /* The above are too generic, also match BIOS info */
787 DMI_EXACT_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
788 DMI_EXACT_MATCH(DMI_BIOS_DATE, "08/22/2014"),
789 },
790 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
791 BYT_RT5640_JD_SRC_JD2_IN4N |
792 BYT_RT5640_OVCD_TH_2000UA |
793 BYT_RT5640_OVCD_SF_0P75 |
794 BYT_RT5640_MONO_SPEAKER |
795 BYT_RT5640_DIFF_MIC |
796 BYT_RT5640_SSP0_AIF2 |
797 BYT_RT5640_MCLK_EN),
798 },
799 { /* Point of View Mobii TAB-P1005W-232 (V2.0) */
800 .matches = {
801 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "POV"),
802 DMI_EXACT_MATCH(DMI_BOARD_NAME, "I102A"),
803 },
804 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
805 BYT_RT5640_JD_SRC_JD2_IN4N |
806 BYT_RT5640_OVCD_TH_2000UA |
807 BYT_RT5640_OVCD_SF_0P75 |
808 BYT_RT5640_DIFF_MIC |
809 BYT_RT5640_SSP0_AIF1 |
810 BYT_RT5640_MCLK_EN),
811 },
812 {
813 /* Prowise PT301 */
814 .matches = {
815 DMI_MATCH(DMI_SYS_VENDOR, "Prowise"),
816 DMI_MATCH(DMI_PRODUCT_NAME, "PT301"),
817 },
818 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
819 BYT_RT5640_JD_SRC_JD2_IN4N |
820 BYT_RT5640_OVCD_TH_2000UA |
821 BYT_RT5640_OVCD_SF_0P75 |
822 BYT_RT5640_DIFF_MIC |
823 BYT_RT5640_SSP0_AIF1 |
824 BYT_RT5640_MCLK_EN),
825 },
826 {
827 /* Teclast X89 */
828 .matches = {
829 DMI_MATCH(DMI_BOARD_VENDOR, "TECLAST"),
830 DMI_MATCH(DMI_BOARD_NAME, "tPAD"),
831 },
832 .driver_data = (void *)(BYT_RT5640_IN3_MAP |
833 BYT_RT5640_JD_SRC_JD1_IN4P |
834 BYT_RT5640_OVCD_TH_2000UA |
835 BYT_RT5640_OVCD_SF_1P0 |
836 BYT_RT5640_SSP0_AIF1 |
837 BYT_RT5640_MCLK_EN),
838 },
839 { /* Toshiba Satellite Click Mini L9W-B */
840 .matches = {
841 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
842 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "SATELLITE Click Mini L9W-B"),
843 },
844 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
845 BYT_RT5640_JD_SRC_JD2_IN4N |
846 BYT_RT5640_OVCD_TH_1500UA |
847 BYT_RT5640_OVCD_SF_0P75 |
848 BYT_RT5640_SSP0_AIF1 |
849 BYT_RT5640_MCLK_EN),
850 },
851 { /* Toshiba Encore WT8-A */
852 .matches = {
853 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
854 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT8-A"),
855 },
856 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
857 BYT_RT5640_JD_SRC_JD2_IN4N |
858 BYT_RT5640_OVCD_TH_2000UA |
859 BYT_RT5640_OVCD_SF_0P75 |
860 BYT_RT5640_JD_NOT_INV |
861 BYT_RT5640_MCLK_EN),
862 },
863 { /* Toshiba Encore WT10-A */
864 .matches = {
865 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "TOSHIBA"),
866 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "TOSHIBA WT10-A-103"),
867 },
868 .driver_data = (void *)(BYT_RT5640_DMIC1_MAP |
869 BYT_RT5640_JD_SRC_JD1_IN4P |
870 BYT_RT5640_OVCD_TH_2000UA |
871 BYT_RT5640_OVCD_SF_0P75 |
872 BYT_RT5640_SSP0_AIF2 |
873 BYT_RT5640_MCLK_EN),
874 },
875 { /* Voyo Winpad A15 */
876 .matches = {
877 DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
878 DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
879 /* Above strings are too generic, also match on BIOS date */
880 DMI_MATCH(DMI_BIOS_DATE, "11/20/2014"),
881 },
882 .driver_data = (void *)(BYT_RT5640_IN1_MAP |
883 BYT_RT5640_JD_SRC_JD2_IN4N |
884 BYT_RT5640_OVCD_TH_2000UA |
885 BYT_RT5640_OVCD_SF_0P75 |
886 BYT_RT5640_DIFF_MIC |
887 BYT_RT5640_MCLK_EN),
888 },
889 { /* Catch-all for generic Insyde tablets, must be last */
890 .matches = {
891 DMI_MATCH(DMI_SYS_VENDOR, "Insyde"),
892 },
893 .driver_data = (void *)(BYTCR_INPUT_DEFAULTS |
894 BYT_RT5640_MCLK_EN |
895 BYT_RT5640_SSP0_AIF1),
896
897 },
898 {}
899 };
900
901 /*
902 * Note this MUST be called before snd_soc_register_card(), so that the props
903 * are in place before the codec component driver's probe function parses them.
904 */
byt_rt5640_add_codec_device_props(const char * i2c_dev_name)905 static int byt_rt5640_add_codec_device_props(const char *i2c_dev_name)
906 {
907 struct property_entry props[MAX_NO_PROPS] = {};
908 struct device *i2c_dev;
909 int ret, cnt = 0;
910
911 i2c_dev = bus_find_device_by_name(&i2c_bus_type, NULL, i2c_dev_name);
912 if (!i2c_dev)
913 return -EPROBE_DEFER;
914
915 switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
916 case BYT_RT5640_DMIC1_MAP:
917 props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic1-data-pin",
918 RT5640_DMIC1_DATA_PIN_IN1P);
919 break;
920 case BYT_RT5640_DMIC2_MAP:
921 props[cnt++] = PROPERTY_ENTRY_U32("realtek,dmic2-data-pin",
922 RT5640_DMIC2_DATA_PIN_IN1N);
923 break;
924 case BYT_RT5640_IN1_MAP:
925 if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
926 props[cnt++] =
927 PROPERTY_ENTRY_BOOL("realtek,in1-differential");
928 break;
929 case BYT_RT5640_IN3_MAP:
930 if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC)
931 props[cnt++] =
932 PROPERTY_ENTRY_BOOL("realtek,in3-differential");
933 break;
934 }
935
936 if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
937 props[cnt++] = PROPERTY_ENTRY_U32(
938 "realtek,jack-detect-source",
939 BYT_RT5640_JDSRC(byt_rt5640_quirk));
940
941 props[cnt++] = PROPERTY_ENTRY_U32(
942 "realtek,over-current-threshold-microamp",
943 BYT_RT5640_OVCD_TH(byt_rt5640_quirk) * 100);
944
945 props[cnt++] = PROPERTY_ENTRY_U32(
946 "realtek,over-current-scale-factor",
947 BYT_RT5640_OVCD_SF(byt_rt5640_quirk));
948 }
949
950 if (byt_rt5640_quirk & BYT_RT5640_JD_NOT_INV)
951 props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,jack-detect-not-inverted");
952
953 ret = device_add_properties(i2c_dev, props);
954 put_device(i2c_dev);
955
956 return ret;
957 }
958
byt_rt5640_init(struct snd_soc_pcm_runtime * runtime)959 static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime)
960 {
961 struct snd_soc_card *card = runtime->card;
962 struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
963 struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
964 const struct snd_soc_dapm_route *custom_map;
965 int num_routes;
966 int ret;
967
968 card->dapm.idle_bias_off = true;
969
970 /* Start with RC clk for jack-detect (we disable MCLK below) */
971 if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN)
972 snd_soc_component_update_bits(component, RT5640_GLB_CLK,
973 RT5640_SCLK_SRC_MASK, RT5640_SCLK_SRC_RCCLK);
974
975 rt5640_sel_asrc_clk_src(component,
976 RT5640_DA_STEREO_FILTER |
977 RT5640_DA_MONO_L_FILTER |
978 RT5640_DA_MONO_R_FILTER |
979 RT5640_AD_STEREO_FILTER |
980 RT5640_AD_MONO_L_FILTER |
981 RT5640_AD_MONO_R_FILTER,
982 RT5640_CLK_SEL_ASRC);
983
984 ret = snd_soc_add_card_controls(card, byt_rt5640_controls,
985 ARRAY_SIZE(byt_rt5640_controls));
986 if (ret) {
987 dev_err(card->dev, "unable to add card controls\n");
988 return ret;
989 }
990
991 switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
992 case BYT_RT5640_IN1_MAP:
993 custom_map = byt_rt5640_intmic_in1_map;
994 num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map);
995 break;
996 case BYT_RT5640_IN3_MAP:
997 custom_map = byt_rt5640_intmic_in3_map;
998 num_routes = ARRAY_SIZE(byt_rt5640_intmic_in3_map);
999 break;
1000 case BYT_RT5640_DMIC2_MAP:
1001 custom_map = byt_rt5640_intmic_dmic2_map;
1002 num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map);
1003 break;
1004 default:
1005 custom_map = byt_rt5640_intmic_dmic1_map;
1006 num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map);
1007 }
1008
1009 ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
1010 if (ret)
1011 return ret;
1012
1013 if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
1014 ret = snd_soc_dapm_add_routes(&card->dapm,
1015 byt_rt5640_ssp2_aif2_map,
1016 ARRAY_SIZE(byt_rt5640_ssp2_aif2_map));
1017 } else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
1018 ret = snd_soc_dapm_add_routes(&card->dapm,
1019 byt_rt5640_ssp0_aif1_map,
1020 ARRAY_SIZE(byt_rt5640_ssp0_aif1_map));
1021 } else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
1022 ret = snd_soc_dapm_add_routes(&card->dapm,
1023 byt_rt5640_ssp0_aif2_map,
1024 ARRAY_SIZE(byt_rt5640_ssp0_aif2_map));
1025 } else {
1026 ret = snd_soc_dapm_add_routes(&card->dapm,
1027 byt_rt5640_ssp2_aif1_map,
1028 ARRAY_SIZE(byt_rt5640_ssp2_aif1_map));
1029 }
1030 if (ret)
1031 return ret;
1032
1033 if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
1034 ret = snd_soc_dapm_add_routes(&card->dapm,
1035 byt_rt5640_mono_spk_map,
1036 ARRAY_SIZE(byt_rt5640_mono_spk_map));
1037 } else if (!(byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS)) {
1038 ret = snd_soc_dapm_add_routes(&card->dapm,
1039 byt_rt5640_stereo_spk_map,
1040 ARRAY_SIZE(byt_rt5640_stereo_spk_map));
1041 }
1042 if (ret)
1043 return ret;
1044
1045 if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
1046 /*
1047 * The firmware might enable the clock at
1048 * boot (this information may or may not
1049 * be reflected in the enable clock register).
1050 * To change the rate we must disable the clock
1051 * first to cover these cases. Due to common
1052 * clock framework restrictions that do not allow
1053 * to disable a clock that has not been enabled,
1054 * we need to enable the clock first.
1055 */
1056 ret = clk_prepare_enable(priv->mclk);
1057 if (!ret)
1058 clk_disable_unprepare(priv->mclk);
1059
1060 if (byt_rt5640_quirk & BYT_RT5640_MCLK_25MHZ)
1061 ret = clk_set_rate(priv->mclk, 25000000);
1062 else
1063 ret = clk_set_rate(priv->mclk, 19200000);
1064
1065 if (ret) {
1066 dev_err(card->dev, "unable to set MCLK rate\n");
1067 return ret;
1068 }
1069 }
1070
1071 if (BYT_RT5640_JDSRC(byt_rt5640_quirk)) {
1072 ret = snd_soc_card_jack_new(card, "Headset",
1073 SND_JACK_HEADSET | SND_JACK_BTN_0,
1074 &priv->jack, rt5640_pins,
1075 ARRAY_SIZE(rt5640_pins));
1076 if (ret) {
1077 dev_err(card->dev, "Jack creation failed %d\n", ret);
1078 return ret;
1079 }
1080 snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0,
1081 KEY_PLAYPAUSE);
1082 snd_soc_component_set_jack(component, &priv->jack, NULL);
1083 }
1084
1085 return 0;
1086 }
1087
byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)1088 static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd,
1089 struct snd_pcm_hw_params *params)
1090 {
1091 struct snd_interval *rate = hw_param_interval(params,
1092 SNDRV_PCM_HW_PARAM_RATE);
1093 struct snd_interval *channels = hw_param_interval(params,
1094 SNDRV_PCM_HW_PARAM_CHANNELS);
1095 int ret, bits;
1096
1097 /* The DSP will covert the FE rate to 48k, stereo */
1098 rate->min = rate->max = 48000;
1099 channels->min = channels->max = 2;
1100
1101 if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1102 (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
1103 /* set SSP0 to 16-bit */
1104 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
1105 bits = 16;
1106 } else {
1107 /* set SSP2 to 24-bit */
1108 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
1109 bits = 24;
1110 }
1111
1112 /*
1113 * Default mode for SSP configuration is TDM 4 slot, override config
1114 * with explicit setting to I2S 2ch. The word length is set with
1115 * dai_set_tdm_slot() since there is no other API exposed
1116 */
1117 ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0),
1118 SND_SOC_DAIFMT_I2S |
1119 SND_SOC_DAIFMT_NB_NF |
1120 SND_SOC_DAIFMT_CBS_CFS);
1121 if (ret < 0) {
1122 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
1123 return ret;
1124 }
1125
1126 ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits);
1127 if (ret < 0) {
1128 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
1129 return ret;
1130 }
1131
1132 return 0;
1133 }
1134
byt_rt5640_aif1_startup(struct snd_pcm_substream * substream)1135 static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream)
1136 {
1137 return snd_pcm_hw_constraint_single(substream->runtime,
1138 SNDRV_PCM_HW_PARAM_RATE, 48000);
1139 }
1140
1141 static const struct snd_soc_ops byt_rt5640_aif1_ops = {
1142 .startup = byt_rt5640_aif1_startup,
1143 };
1144
1145 static const struct snd_soc_ops byt_rt5640_be_ssp2_ops = {
1146 .hw_params = byt_rt5640_aif1_hw_params,
1147 };
1148
1149 SND_SOC_DAILINK_DEF(dummy,
1150 DAILINK_COMP_ARRAY(COMP_DUMMY()));
1151
1152 SND_SOC_DAILINK_DEF(media,
1153 DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
1154
1155 SND_SOC_DAILINK_DEF(deepbuffer,
1156 DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
1157
1158 SND_SOC_DAILINK_DEF(ssp2_port,
1159 /* overwritten for ssp0 routing */
1160 DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
1161 SND_SOC_DAILINK_DEF(ssp2_codec,
1162 DAILINK_COMP_ARRAY(COMP_CODEC(
1163 /* overwritten with HID */ "i2c-10EC5640:00",
1164 /* changed w/ quirk */ "rt5640-aif1")));
1165
1166 SND_SOC_DAILINK_DEF(platform,
1167 DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
1168
1169 static struct snd_soc_dai_link byt_rt5640_dais[] = {
1170 [MERR_DPCM_AUDIO] = {
1171 .name = "Baytrail Audio Port",
1172 .stream_name = "Baytrail Audio",
1173 .nonatomic = true,
1174 .dynamic = 1,
1175 .dpcm_playback = 1,
1176 .dpcm_capture = 1,
1177 .ops = &byt_rt5640_aif1_ops,
1178 SND_SOC_DAILINK_REG(media, dummy, platform),
1179 },
1180 [MERR_DPCM_DEEP_BUFFER] = {
1181 .name = "Deep-Buffer Audio Port",
1182 .stream_name = "Deep-Buffer Audio",
1183 .nonatomic = true,
1184 .dynamic = 1,
1185 .dpcm_playback = 1,
1186 .ops = &byt_rt5640_aif1_ops,
1187 SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
1188 },
1189 /* back ends */
1190 {
1191 .name = "SSP2-Codec",
1192 .id = 0,
1193 .no_pcm = 1,
1194 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
1195 | SND_SOC_DAIFMT_CBS_CFS,
1196 .be_hw_params_fixup = byt_rt5640_codec_fixup,
1197 .nonatomic = true,
1198 .dpcm_playback = 1,
1199 .dpcm_capture = 1,
1200 .init = byt_rt5640_init,
1201 .ops = &byt_rt5640_be_ssp2_ops,
1202 SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
1203 },
1204 };
1205
1206 /* SoC card */
1207 static char byt_rt5640_codec_name[SND_ACPI_I2C_ID_LEN];
1208 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1209 static char byt_rt5640_long_name[40]; /* = "bytcr-rt5640-*-spk-*-mic" */
1210 #endif
1211 static char byt_rt5640_components[32]; /* = "cfg-spk:* cfg-mic:*" */
1212
byt_rt5640_suspend(struct snd_soc_card * card)1213 static int byt_rt5640_suspend(struct snd_soc_card *card)
1214 {
1215 struct snd_soc_component *component;
1216
1217 if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1218 return 0;
1219
1220 for_each_card_components(card, component) {
1221 if (!strcmp(component->name, byt_rt5640_codec_name)) {
1222 dev_dbg(component->dev, "disabling jack detect before suspend\n");
1223 snd_soc_component_set_jack(component, NULL, NULL);
1224 break;
1225 }
1226 }
1227
1228 return 0;
1229 }
1230
byt_rt5640_resume(struct snd_soc_card * card)1231 static int byt_rt5640_resume(struct snd_soc_card *card)
1232 {
1233 struct byt_rt5640_private *priv = snd_soc_card_get_drvdata(card);
1234 struct snd_soc_component *component;
1235
1236 if (!BYT_RT5640_JDSRC(byt_rt5640_quirk))
1237 return 0;
1238
1239 for_each_card_components(card, component) {
1240 if (!strcmp(component->name, byt_rt5640_codec_name)) {
1241 dev_dbg(component->dev, "re-enabling jack detect after resume\n");
1242 snd_soc_component_set_jack(component, &priv->jack, NULL);
1243 break;
1244 }
1245 }
1246
1247 return 0;
1248 }
1249
1250 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BAYTRAIL)
1251 /* use space before codec name to simplify card ID, and simplify driver name */
1252 #define CARD_NAME "bytcht rt5640" /* card name will be 'sof-bytcht rt5640' */
1253 #define DRIVER_NAME "SOF"
1254 #else
1255 #define CARD_NAME "bytcr-rt5640"
1256 #define DRIVER_NAME NULL /* card name will be used for driver name */
1257 #endif
1258
1259 static struct snd_soc_card byt_rt5640_card = {
1260 .name = CARD_NAME,
1261 .driver_name = DRIVER_NAME,
1262 .owner = THIS_MODULE,
1263 .dai_link = byt_rt5640_dais,
1264 .num_links = ARRAY_SIZE(byt_rt5640_dais),
1265 .dapm_widgets = byt_rt5640_widgets,
1266 .num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets),
1267 .dapm_routes = byt_rt5640_audio_map,
1268 .num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map),
1269 .fully_routed = true,
1270 .suspend_pre = byt_rt5640_suspend,
1271 .resume_post = byt_rt5640_resume,
1272 };
1273
1274 struct acpi_chan_package { /* ACPICA seems to require 64 bit integers */
1275 u64 aif_value; /* 1: AIF1, 2: AIF2 */
1276 u64 mclock_value; /* usually 25MHz (0x17d7940), ignored */
1277 };
1278
snd_byt_rt5640_mc_probe(struct platform_device * pdev)1279 static int snd_byt_rt5640_mc_probe(struct platform_device *pdev)
1280 {
1281 static const char * const map_name[] = { "dmic1", "dmic2", "in1", "in3" };
1282 __maybe_unused const char *spk_type;
1283 const struct dmi_system_id *dmi_id;
1284 struct byt_rt5640_private *priv;
1285 struct snd_soc_acpi_mach *mach;
1286 const char *platform_name;
1287 struct acpi_device *adev;
1288 int ret_val = 0;
1289 int dai_index = 0;
1290 int i, cfg_spk;
1291
1292 is_bytcr = false;
1293 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1294 if (!priv)
1295 return -ENOMEM;
1296
1297 /* register the soc card */
1298 byt_rt5640_card.dev = &pdev->dev;
1299 mach = byt_rt5640_card.dev->platform_data;
1300 snd_soc_card_set_drvdata(&byt_rt5640_card, priv);
1301
1302 /* fix index of codec dai */
1303 for (i = 0; i < ARRAY_SIZE(byt_rt5640_dais); i++) {
1304 if (!strcmp(byt_rt5640_dais[i].codecs->name,
1305 "i2c-10EC5640:00")) {
1306 dai_index = i;
1307 break;
1308 }
1309 }
1310
1311 /* fixup codec name based on HID */
1312 adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
1313 if (adev) {
1314 snprintf(byt_rt5640_codec_name, sizeof(byt_rt5640_codec_name),
1315 "i2c-%s", acpi_dev_name(adev));
1316 put_device(&adev->dev);
1317 byt_rt5640_dais[dai_index].codecs->name = byt_rt5640_codec_name;
1318 }
1319
1320 /*
1321 * swap SSP0 if bytcr is detected
1322 * (will be overridden if DMI quirk is detected)
1323 */
1324 if (soc_intel_is_byt()) {
1325 if (mach->mach_params.acpi_ipc_irq_index == 0)
1326 is_bytcr = true;
1327 }
1328
1329 if (is_bytcr) {
1330 /*
1331 * Baytrail CR platforms may have CHAN package in BIOS, try
1332 * to find relevant routing quirk based as done on Windows
1333 * platforms. We have to read the information directly from the
1334 * BIOS, at this stage the card is not created and the links
1335 * with the codec driver/pdata are non-existent
1336 */
1337
1338 struct acpi_chan_package chan_package;
1339
1340 /* format specified: 2 64-bit integers */
1341 struct acpi_buffer format = {sizeof("NN"), "NN"};
1342 struct acpi_buffer state = {0, NULL};
1343 struct snd_soc_acpi_package_context pkg_ctx;
1344 bool pkg_found = false;
1345
1346 state.length = sizeof(chan_package);
1347 state.pointer = &chan_package;
1348
1349 pkg_ctx.name = "CHAN";
1350 pkg_ctx.length = 2;
1351 pkg_ctx.format = &format;
1352 pkg_ctx.state = &state;
1353 pkg_ctx.data_valid = false;
1354
1355 pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
1356 &pkg_ctx);
1357 if (pkg_found) {
1358 if (chan_package.aif_value == 1) {
1359 dev_info(&pdev->dev, "BIOS Routing: AIF1 connected\n");
1360 byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF1;
1361 } else if (chan_package.aif_value == 2) {
1362 dev_info(&pdev->dev, "BIOS Routing: AIF2 connected\n");
1363 byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1364 } else {
1365 dev_info(&pdev->dev, "BIOS Routing isn't valid, ignored\n");
1366 pkg_found = false;
1367 }
1368 }
1369
1370 if (!pkg_found) {
1371 /* no BIOS indications, assume SSP0-AIF2 connection */
1372 byt_rt5640_quirk |= BYT_RT5640_SSP0_AIF2;
1373 }
1374
1375 /* change defaults for Baytrail-CR capture */
1376 byt_rt5640_quirk |= BYTCR_INPUT_DEFAULTS;
1377 } else {
1378 byt_rt5640_quirk |= BYT_RT5640_DMIC1_MAP |
1379 BYT_RT5640_JD_SRC_JD2_IN4N |
1380 BYT_RT5640_OVCD_TH_2000UA |
1381 BYT_RT5640_OVCD_SF_0P75;
1382 }
1383
1384 /* check quirks before creating card */
1385 dmi_id = dmi_first_match(byt_rt5640_quirk_table);
1386 if (dmi_id)
1387 byt_rt5640_quirk = (unsigned long)dmi_id->driver_data;
1388 if (quirk_override != -1) {
1389 dev_info(&pdev->dev, "Overriding quirk 0x%lx => 0x%x\n",
1390 byt_rt5640_quirk, quirk_override);
1391 byt_rt5640_quirk = quirk_override;
1392 }
1393
1394 /* Must be called before register_card, also see declaration comment. */
1395 ret_val = byt_rt5640_add_codec_device_props(byt_rt5640_codec_name);
1396 if (ret_val)
1397 return ret_val;
1398
1399 log_quirks(&pdev->dev);
1400
1401 if ((byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) ||
1402 (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2))
1403 byt_rt5640_dais[dai_index].codecs->dai_name = "rt5640-aif2";
1404
1405 if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
1406 (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2))
1407 byt_rt5640_dais[dai_index].cpus->dai_name = "ssp0-port";
1408
1409 if (byt_rt5640_quirk & BYT_RT5640_MCLK_EN) {
1410 priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
1411 if (IS_ERR(priv->mclk)) {
1412 ret_val = PTR_ERR(priv->mclk);
1413
1414 dev_err(&pdev->dev,
1415 "Failed to get MCLK from pmc_plt_clk_3: %d\n",
1416 ret_val);
1417
1418 /*
1419 * Fall back to bit clock usage for -ENOENT (clock not
1420 * available likely due to missing dependencies), bail
1421 * for all other errors, including -EPROBE_DEFER
1422 */
1423 if (ret_val != -ENOENT)
1424 return ret_val;
1425 byt_rt5640_quirk &= ~BYT_RT5640_MCLK_EN;
1426 }
1427 }
1428
1429 if (byt_rt5640_quirk & BYT_RT5640_NO_SPEAKERS) {
1430 cfg_spk = 0;
1431 spk_type = "none";
1432 } else if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
1433 cfg_spk = 1;
1434 spk_type = "mono";
1435 } else {
1436 cfg_spk = 2;
1437 spk_type = "stereo";
1438 }
1439
1440 snprintf(byt_rt5640_components, sizeof(byt_rt5640_components),
1441 "cfg-spk:%d cfg-mic:%s", cfg_spk,
1442 map_name[BYT_RT5640_MAP(byt_rt5640_quirk)]);
1443 byt_rt5640_card.components = byt_rt5640_components;
1444 #if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1445 snprintf(byt_rt5640_long_name, sizeof(byt_rt5640_long_name),
1446 "bytcr-rt5640-%s-spk-%s-mic", spk_type,
1447 map_name[BYT_RT5640_MAP(byt_rt5640_quirk)]);
1448 byt_rt5640_card.long_name = byt_rt5640_long_name;
1449 #endif
1450
1451 /* override plaform name, if required */
1452 platform_name = mach->mach_params.platform;
1453
1454 ret_val = snd_soc_fixup_dai_links_platform_name(&byt_rt5640_card,
1455 platform_name);
1456 if (ret_val)
1457 return ret_val;
1458
1459 ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5640_card);
1460
1461 if (ret_val) {
1462 dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
1463 ret_val);
1464 return ret_val;
1465 }
1466 platform_set_drvdata(pdev, &byt_rt5640_card);
1467 return ret_val;
1468 }
1469
1470 static struct platform_driver snd_byt_rt5640_mc_driver = {
1471 .driver = {
1472 .name = "bytcr_rt5640",
1473 #if IS_ENABLED(CONFIG_SND_SOC_SOF_BAYTRAIL)
1474 .pm = &snd_soc_pm_ops,
1475 #endif
1476 },
1477 .probe = snd_byt_rt5640_mc_probe,
1478 };
1479
1480 module_platform_driver(snd_byt_rt5640_mc_driver);
1481
1482 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
1483 MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
1484 MODULE_LICENSE("GPL v2");
1485 MODULE_ALIAS("platform:bytcr_rt5640");
1486