Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * bytcr_rt5651.c - ASoc Machine driver for Intel Byt CR platform
4 * (derived from bytcr_rt5640.c)
5 *
6 * Copyright (C) 2015 Intel Corp
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 *
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 */
11
12#include <linux/init.h>
13#include <linux/i2c.h>
14#include <linux/module.h>
15#include <linux/platform_device.h>
16#include <linux/property.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/gpio/consumer.h>
23#include <linux/gpio/machine.h>
24#include <linux/slab.h>
25#include <sound/pcm.h>
26#include <sound/pcm_params.h>
27#include <sound/soc.h>
28#include <sound/jack.h>
29#include <sound/soc-acpi.h>
30#include "../../codecs/rt5651.h"
31#include "../atom/sst-atom-controls.h"
32#include "../common/soc-intel-quirks.h"
33
34enum {
35 BYT_RT5651_DMIC_MAP,
36 BYT_RT5651_IN1_MAP,
37 BYT_RT5651_IN2_MAP,
38 BYT_RT5651_IN1_IN2_MAP,
39};
40
41enum {
42 BYT_RT5651_JD_NULL = (RT5651_JD_NULL << 4),
43 BYT_RT5651_JD1_1 = (RT5651_JD1_1 << 4),
44 BYT_RT5651_JD1_2 = (RT5651_JD1_2 << 4),
45 BYT_RT5651_JD2 = (RT5651_JD2 << 4),
46};
47
48enum {
49 BYT_RT5651_OVCD_TH_600UA = (6 << 8),
50 BYT_RT5651_OVCD_TH_1500UA = (15 << 8),
51 BYT_RT5651_OVCD_TH_2000UA = (20 << 8),
52};
53
54enum {
55 BYT_RT5651_OVCD_SF_0P5 = (RT5651_OVCD_SF_0P5 << 13),
56 BYT_RT5651_OVCD_SF_0P75 = (RT5651_OVCD_SF_0P75 << 13),
57 BYT_RT5651_OVCD_SF_1P0 = (RT5651_OVCD_SF_1P0 << 13),
58 BYT_RT5651_OVCD_SF_1P5 = (RT5651_OVCD_SF_1P5 << 13),
59};
60
61#define BYT_RT5651_MAP(quirk) ((quirk) & GENMASK(3, 0))
62#define BYT_RT5651_JDSRC(quirk) (((quirk) & GENMASK(7, 4)) >> 4)
63#define BYT_RT5651_OVCD_TH(quirk) (((quirk) & GENMASK(12, 8)) >> 8)
64#define BYT_RT5651_OVCD_SF(quirk) (((quirk) & GENMASK(14, 13)) >> 13)
65#define BYT_RT5651_DMIC_EN BIT(16)
66#define BYT_RT5651_MCLK_EN BIT(17)
67#define BYT_RT5651_MCLK_25MHZ BIT(18)
68#define BYT_RT5651_SSP2_AIF2 BIT(19) /* default is using AIF1 */
69#define BYT_RT5651_SSP0_AIF1 BIT(20)
70#define BYT_RT5651_SSP0_AIF2 BIT(21)
71#define BYT_RT5651_HP_LR_SWAPPED BIT(22)
72#define BYT_RT5651_MONO_SPEAKER BIT(23)
73#define BYT_RT5651_JD_NOT_INV BIT(24)
74
75#define BYT_RT5651_DEFAULT_QUIRKS (BYT_RT5651_MCLK_EN | \
76 BYT_RT5651_JD1_1 | \
77 BYT_RT5651_OVCD_TH_2000UA | \
78 BYT_RT5651_OVCD_SF_0P75)
79
80/* jack-detect-source + inv + dmic-en + ovcd-th + -sf + terminating entry */
81#define MAX_NO_PROPS 6
82
83struct byt_rt5651_private {
84 struct clk *mclk;
85 struct gpio_desc *ext_amp_gpio;
86 struct gpio_desc *hp_detect;
87 struct snd_soc_jack jack;
88 struct device *codec_dev;
89};
90
91static const struct acpi_gpio_mapping *byt_rt5651_gpios;
92
93/* Default: jack-detect on JD1_1, internal mic on in2, headsetmic on in3 */
94static unsigned long byt_rt5651_quirk = BYT_RT5651_DEFAULT_QUIRKS |
95 BYT_RT5651_IN2_MAP;
96
97static int quirk_override = -1;
98module_param_named(quirk, quirk_override, int, 0444);
99MODULE_PARM_DESC(quirk, "Board-specific quirk override");
100
101static void log_quirks(struct device *dev)
102{
103 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_DMIC_MAP)
104 dev_info(dev, "quirk DMIC_MAP enabled");
105 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_MAP)
106 dev_info(dev, "quirk IN1_MAP enabled");
107 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN2_MAP)
108 dev_info(dev, "quirk IN2_MAP enabled");
109 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_IN2_MAP)
110 dev_info(dev, "quirk IN1_IN2_MAP enabled");
111 if (BYT_RT5651_JDSRC(byt_rt5651_quirk)) {
112 dev_info(dev, "quirk realtek,jack-detect-source %ld\n",
113 BYT_RT5651_JDSRC(byt_rt5651_quirk));
114 dev_info(dev, "quirk realtek,over-current-threshold-microamp %ld\n",
115 BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100);
116 dev_info(dev, "quirk realtek,over-current-scale-factor %ld\n",
117 BYT_RT5651_OVCD_SF(byt_rt5651_quirk));
118 }
119 if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN)
120 dev_info(dev, "quirk DMIC enabled");
121 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
122 dev_info(dev, "quirk MCLK_EN enabled");
123 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
124 dev_info(dev, "quirk MCLK_25MHZ enabled");
125 if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2)
126 dev_info(dev, "quirk SSP2_AIF2 enabled\n");
127 if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1)
128 dev_info(dev, "quirk SSP0_AIF1 enabled\n");
129 if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)
130 dev_info(dev, "quirk SSP0_AIF2 enabled\n");
131 if (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER)
132 dev_info(dev, "quirk MONO_SPEAKER enabled\n");
133 if (byt_rt5651_quirk & BYT_RT5651_JD_NOT_INV)
134 dev_info(dev, "quirk JD_NOT_INV enabled\n");
135}
136
137#define BYT_CODEC_DAI1 "rt5651-aif1"
138#define BYT_CODEC_DAI2 "rt5651-aif2"
139
140static int byt_rt5651_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai,
141 int rate, int bclk_ratio)
142{
143 int clk_id, clk_freq, ret;
144
145 /* Configure the PLL before selecting it */
146 if (!(byt_rt5651_quirk & BYT_RT5651_MCLK_EN)) {
147 clk_id = RT5651_PLL1_S_BCLK1;
148 clk_freq = rate * bclk_ratio;
149 } else {
150 clk_id = RT5651_PLL1_S_MCLK;
151 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
152 clk_freq = 25000000;
153 else
154 clk_freq = 19200000;
155 }
156 ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, rate * 512);
157 if (ret < 0) {
158 dev_err(codec_dai->component->dev, "can't set pll: %d\n", ret);
159 return ret;
160 }
161
162 ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1,
163 rate * 512, SND_SOC_CLOCK_IN);
164 if (ret < 0) {
165 dev_err(codec_dai->component->dev, "can't set clock %d\n", ret);
166 return ret;
167 }
168
169 return 0;
170}
171
172static int platform_clock_control(struct snd_soc_dapm_widget *w,
173 struct snd_kcontrol *k, int event)
174{
175 struct snd_soc_dapm_context *dapm = w->dapm;
176 struct snd_soc_card *card = dapm->card;
177 struct snd_soc_dai *codec_dai;
178 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
179 int ret;
180
181 codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1);
182 if (!codec_dai)
183 codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI2);
184 if (!codec_dai) {
185 dev_err(card->dev,
186 "Codec dai not found; Unable to set platform clock\n");
187 return -EIO;
188 }
189
190 if (SND_SOC_DAPM_EVENT_ON(event)) {
191 ret = clk_prepare_enable(priv->mclk);
192 if (ret < 0) {
193 dev_err(card->dev, "could not configure MCLK state");
194 return ret;
195 }
196 ret = byt_rt5651_prepare_and_enable_pll1(codec_dai, 48000, 50);
197 } else {
198 /*
199 * Set codec clock source to internal clock before
200 * turning off the platform clock. Codec needs clock
201 * for Jack detection and button press
202 */
203 ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_RCCLK,
204 48000 * 512,
205 SND_SOC_CLOCK_IN);
206 if (!ret)
207 clk_disable_unprepare(priv->mclk);
208 }
209
210 if (ret < 0) {
211 dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
212 return ret;
213 }
214
215 return 0;
216}
217
218static int rt5651_ext_amp_power_event(struct snd_soc_dapm_widget *w,
219 struct snd_kcontrol *kcontrol, int event)
220{
221 struct snd_soc_card *card = w->dapm->card;
222 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
223
224 if (SND_SOC_DAPM_EVENT_ON(event))
225 gpiod_set_value_cansleep(priv->ext_amp_gpio, 1);
226 else
227 gpiod_set_value_cansleep(priv->ext_amp_gpio, 0);
228
229 return 0;
230}
231
232static const struct snd_soc_dapm_widget byt_rt5651_widgets[] = {
233 SND_SOC_DAPM_HP("Headphone", NULL),
234 SND_SOC_DAPM_MIC("Headset Mic", NULL),
235 SND_SOC_DAPM_MIC("Internal Mic", NULL),
236 SND_SOC_DAPM_SPK("Speaker", NULL),
237 SND_SOC_DAPM_LINE("Line In", NULL),
238 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
239 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
240 SND_SOC_DAPM_POST_PMD),
241 SND_SOC_DAPM_SUPPLY("Ext Amp Power", SND_SOC_NOPM, 0, 0,
242 rt5651_ext_amp_power_event,
243 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
244};
245
246static const struct snd_soc_dapm_route byt_rt5651_audio_map[] = {
247 {"Headphone", NULL, "Platform Clock"},
248 {"Headset Mic", NULL, "Platform Clock"},
249 {"Internal Mic", NULL, "Platform Clock"},
250 {"Speaker", NULL, "Platform Clock"},
251 {"Speaker", NULL, "Ext Amp Power"},
252 {"Line In", NULL, "Platform Clock"},
253
254 {"Headset Mic", NULL, "micbias1"}, /* lowercase for rt5651 */
255 {"Headphone", NULL, "HPOL"},
256 {"Headphone", NULL, "HPOR"},
257 {"Speaker", NULL, "LOUTL"},
258 {"Speaker", NULL, "LOUTR"},
259 {"IN2P", NULL, "Line In"},
260 {"IN2N", NULL, "Line In"},
261
262};
263
264static const struct snd_soc_dapm_route byt_rt5651_intmic_dmic_map[] = {
265 {"DMIC L1", NULL, "Internal Mic"},
266 {"DMIC R1", NULL, "Internal Mic"},
267 {"IN2P", NULL, "Headset Mic"},
268};
269
270static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_map[] = {
271 {"Internal Mic", NULL, "micbias1"},
272 {"IN1P", NULL, "Internal Mic"},
273 {"IN3P", NULL, "Headset Mic"},
274};
275
276static const struct snd_soc_dapm_route byt_rt5651_intmic_in2_map[] = {
277 {"Internal Mic", NULL, "micbias1"},
278 {"IN2P", NULL, "Internal Mic"},
279 {"IN3P", NULL, "Headset Mic"},
280};
281
282static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_in2_map[] = {
283 {"Internal Mic", NULL, "micbias1"},
284 {"IN1P", NULL, "Internal Mic"},
285 {"IN2P", NULL, "Internal Mic"},
286 {"IN3P", NULL, "Headset Mic"},
287};
288
289static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif1_map[] = {
290 {"ssp0 Tx", NULL, "modem_out"},
291 {"modem_in", NULL, "ssp0 Rx"},
292
293 {"AIF1 Playback", NULL, "ssp0 Tx"},
294 {"ssp0 Rx", NULL, "AIF1 Capture"},
295};
296
297static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif2_map[] = {
298 {"ssp0 Tx", NULL, "modem_out"},
299 {"modem_in", NULL, "ssp0 Rx"},
300
301 {"AIF2 Playback", NULL, "ssp0 Tx"},
302 {"ssp0 Rx", NULL, "AIF2 Capture"},
303};
304
305static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif1_map[] = {
306 {"ssp2 Tx", NULL, "codec_out0"},
307 {"ssp2 Tx", NULL, "codec_out1"},
308 {"codec_in0", NULL, "ssp2 Rx"},
309 {"codec_in1", NULL, "ssp2 Rx"},
310
311 {"AIF1 Playback", NULL, "ssp2 Tx"},
312 {"ssp2 Rx", NULL, "AIF1 Capture"},
313};
314
315static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif2_map[] = {
316 {"ssp2 Tx", NULL, "codec_out0"},
317 {"ssp2 Tx", NULL, "codec_out1"},
318 {"codec_in0", NULL, "ssp2 Rx"},
319 {"codec_in1", NULL, "ssp2 Rx"},
320
321 {"AIF2 Playback", NULL, "ssp2 Tx"},
322 {"ssp2 Rx", NULL, "AIF2 Capture"},
323};
324
325static const struct snd_kcontrol_new byt_rt5651_controls[] = {
326 SOC_DAPM_PIN_SWITCH("Headphone"),
327 SOC_DAPM_PIN_SWITCH("Headset Mic"),
328 SOC_DAPM_PIN_SWITCH("Internal Mic"),
329 SOC_DAPM_PIN_SWITCH("Speaker"),
330 SOC_DAPM_PIN_SWITCH("Line In"),
331};
332
333static struct snd_soc_jack_pin bytcr_jack_pins[] = {
334 {
335 .pin = "Headphone",
336 .mask = SND_JACK_HEADPHONE,
337 },
338 {
339 .pin = "Headset Mic",
340 .mask = SND_JACK_MICROPHONE,
341 },
342};
343
344static int byt_rt5651_aif1_hw_params(struct snd_pcm_substream *substream,
345 struct snd_pcm_hw_params *params)
346{
347 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
348 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
349 snd_pcm_format_t format = params_format(params);
350 int rate = params_rate(params);
351 int bclk_ratio;
352
353 if (format == SNDRV_PCM_FORMAT_S16_LE)
354 bclk_ratio = 32;
355 else
356 bclk_ratio = 50;
357
358 return byt_rt5651_prepare_and_enable_pll1(codec_dai, rate, bclk_ratio);
359}
360
361static const struct acpi_gpio_params pov_p1006w_hp_detect = { 1, 0, false };
362static const struct acpi_gpio_params pov_p1006w_ext_amp_en = { 2, 0, true };
363
364static const struct acpi_gpio_mapping byt_rt5651_pov_p1006w_gpios[] = {
365 { "hp-detect-gpios", &pov_p1006w_hp_detect, 1, },
366 { "ext-amp-enable-gpios", &pov_p1006w_ext_amp_en, 1, },
367 { },
368};
369
370static int byt_rt5651_pov_p1006w_quirk_cb(const struct dmi_system_id *id)
371{
372 byt_rt5651_quirk = (unsigned long)id->driver_data;
373 byt_rt5651_gpios = byt_rt5651_pov_p1006w_gpios;
374 return 1;
375}
376
377static int byt_rt5651_quirk_cb(const struct dmi_system_id *id)
378{
379 byt_rt5651_quirk = (unsigned long)id->driver_data;
380 return 1;
381}
382
383static const struct dmi_system_id byt_rt5651_quirk_table[] = {
384 {
385 /* Chuwi Hi8 Pro (CWI513) */
386 .callback = byt_rt5651_quirk_cb,
387 .matches = {
388 DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"),
389 DMI_MATCH(DMI_PRODUCT_NAME, "X1D3_C806N"),
390 },
391 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
392 BYT_RT5651_IN2_MAP |
393 BYT_RT5651_HP_LR_SWAPPED |
394 BYT_RT5651_MONO_SPEAKER),
395 },
396 {
397 /* Chuwi Vi8 Plus (CWI519) */
398 .callback = byt_rt5651_quirk_cb,
399 .matches = {
400 DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"),
401 DMI_MATCH(DMI_PRODUCT_NAME, "D2D3_Vi8A1"),
402 },
403 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
404 BYT_RT5651_IN2_MAP |
405 BYT_RT5651_HP_LR_SWAPPED |
406 BYT_RT5651_MONO_SPEAKER),
407 },
408 {
409 /* Complet Electro Serv MY8307 */
410 .callback = byt_rt5651_quirk_cb,
411 .matches = {
412 DMI_MATCH(DMI_SYS_VENDOR, "Complet Electro Serv"),
413 DMI_MATCH(DMI_PRODUCT_NAME, "MY8307"),
414 },
415 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
416 BYT_RT5651_IN2_MAP |
417 BYT_RT5651_MONO_SPEAKER |
418 BYT_RT5651_JD_NOT_INV),
419 },
420 {
421 /* I.T.Works TW701, Ployer Momo7w and Trekstor ST70416-6
422 * (these all use the same mainboard) */
423 .callback = byt_rt5651_quirk_cb,
424 .matches = {
425 DMI_MATCH(DMI_BIOS_VENDOR, "INSYDE Corp."),
426 /* Partial match for all of itWORKS.G.WI71C.JGBMRBA,
427 * TREK.G.WI71C.JGBMRBA0x and MOMO.G.WI71C.MABMRBA02 */
428 DMI_MATCH(DMI_BIOS_VERSION, ".G.WI71C."),
429 },
430 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
431 BYT_RT5651_IN2_MAP |
432 BYT_RT5651_SSP0_AIF1 |
433 BYT_RT5651_MONO_SPEAKER),
434 },
435 {
436 /* Jumper EZpad 7 */
437 .callback = byt_rt5651_quirk_cb,
438 .matches = {
439 DMI_MATCH(DMI_SYS_VENDOR, "Jumper"),
440 DMI_MATCH(DMI_PRODUCT_NAME, "EZpad"),
441 /* Jumper12x.WJ2012.bsBKRCP05 with the version dropped */
442 DMI_MATCH(DMI_BIOS_VERSION, "Jumper12x.WJ2012.bsBKRCP"),
443 },
444 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
445 BYT_RT5651_IN2_MAP |
446 BYT_RT5651_JD_NOT_INV),
447 },
448 {
449 /* KIANO SlimNote 14.2 */
450 .callback = byt_rt5651_quirk_cb,
451 .matches = {
452 DMI_MATCH(DMI_SYS_VENDOR, "KIANO"),
453 DMI_MATCH(DMI_PRODUCT_NAME, "KIANO SlimNote 14.2"),
454 },
455 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
456 BYT_RT5651_IN1_IN2_MAP),
457 },
458 {
459 /* Minnowboard Max B3 */
460 .callback = byt_rt5651_quirk_cb,
461 .matches = {
462 DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
463 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
464 },
465 .driver_data = (void *)(BYT_RT5651_IN1_MAP),
466 },
467 {
468 /* Minnowboard Turbot */
469 .callback = byt_rt5651_quirk_cb,
470 .matches = {
471 DMI_MATCH(DMI_SYS_VENDOR, "ADI"),
472 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Turbot"),
473 },
474 .driver_data = (void *)(BYT_RT5651_MCLK_EN |
475 BYT_RT5651_IN1_MAP),
476 },
477 {
478 /* Point of View mobii wintab p1006w (v1.0) */
479 .callback = byt_rt5651_pov_p1006w_quirk_cb,
480 .matches = {
481 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
482 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "BayTrail"),
483 /* Note 105b is Foxcon's USB/PCI vendor id */
484 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "105B"),
485 DMI_EXACT_MATCH(DMI_BOARD_NAME, "0E57"),
486 },
487 .driver_data = (void *)(BYT_RT5651_DMIC_MAP |
488 BYT_RT5651_OVCD_TH_2000UA |
489 BYT_RT5651_OVCD_SF_0P75 |
490 BYT_RT5651_DMIC_EN |
491 BYT_RT5651_MCLK_EN |
492 BYT_RT5651_SSP0_AIF1),
493 },
494 {
495 /* VIOS LTH17 */
496 .callback = byt_rt5651_quirk_cb,
497 .matches = {
498 DMI_MATCH(DMI_SYS_VENDOR, "VIOS"),
499 DMI_MATCH(DMI_PRODUCT_NAME, "LTH17"),
500 },
501 .driver_data = (void *)(BYT_RT5651_IN1_IN2_MAP |
502 BYT_RT5651_JD1_1 |
503 BYT_RT5651_OVCD_TH_2000UA |
504 BYT_RT5651_OVCD_SF_1P0 |
505 BYT_RT5651_MCLK_EN),
506 },
507 {
508 /* Yours Y8W81 (and others using the same mainboard) */
509 .callback = byt_rt5651_quirk_cb,
510 .matches = {
511 DMI_MATCH(DMI_BIOS_VENDOR, "INSYDE Corp."),
512 /* Partial match for all devs with a W86C mainboard */
513 DMI_MATCH(DMI_BIOS_VERSION, ".F.W86C."),
514 },
515 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
516 BYT_RT5651_IN2_MAP |
517 BYT_RT5651_SSP0_AIF1 |
518 BYT_RT5651_MONO_SPEAKER),
519 },
520 {}
521};
522
523/*
524 * Note this MUST be called before snd_soc_register_card(), so that the props
525 * are in place before the codec component driver's probe function parses them.
526 */
527static int byt_rt5651_add_codec_device_props(struct device *i2c_dev,
528 struct byt_rt5651_private *priv)
529{
530 struct property_entry props[MAX_NO_PROPS] = {};
531 struct fwnode_handle *fwnode;
532 int cnt = 0;
533 int ret;
534
535 props[cnt++] = PROPERTY_ENTRY_U32("realtek,jack-detect-source",
536 BYT_RT5651_JDSRC(byt_rt5651_quirk));
537
538 props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-threshold-microamp",
539 BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100);
540
541 props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-scale-factor",
542 BYT_RT5651_OVCD_SF(byt_rt5651_quirk));
543
544 if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN)
545 props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,dmic-en");
546
547 if (byt_rt5651_quirk & BYT_RT5651_JD_NOT_INV)
548 props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,jack-detect-not-inverted");
549
550 fwnode = fwnode_create_software_node(props, NULL);
551 if (IS_ERR(fwnode)) {
552 /* put_device(i2c_dev) is handled in caller */
553 return PTR_ERR(fwnode);
554 }
555
556 ret = device_add_software_node(i2c_dev, to_software_node(fwnode));
557
558 fwnode_handle_put(fwnode);
559
560 return ret;
561}
562
563static int byt_rt5651_init(struct snd_soc_pcm_runtime *runtime)
564{
565 struct snd_soc_card *card = runtime->card;
566 struct snd_soc_component *codec = snd_soc_rtd_to_codec(runtime, 0)->component;
567 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
568 const struct snd_soc_dapm_route *custom_map;
569 int num_routes;
570 int report;
571 int ret;
572
573 card->dapm.idle_bias_off = true;
574
575 /* Start with RC clk for jack-detect (we disable MCLK below) */
576 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
577 snd_soc_component_update_bits(codec, RT5651_GLB_CLK,
578 RT5651_SCLK_SRC_MASK, RT5651_SCLK_SRC_RCCLK);
579
580 switch (BYT_RT5651_MAP(byt_rt5651_quirk)) {
581 case BYT_RT5651_IN1_MAP:
582 custom_map = byt_rt5651_intmic_in1_map;
583 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_map);
584 break;
585 case BYT_RT5651_IN2_MAP:
586 custom_map = byt_rt5651_intmic_in2_map;
587 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in2_map);
588 break;
589 case BYT_RT5651_IN1_IN2_MAP:
590 custom_map = byt_rt5651_intmic_in1_in2_map;
591 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_in2_map);
592 break;
593 default:
594 custom_map = byt_rt5651_intmic_dmic_map;
595 num_routes = ARRAY_SIZE(byt_rt5651_intmic_dmic_map);
596 }
597 ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
598 if (ret)
599 return ret;
600
601 if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) {
602 ret = snd_soc_dapm_add_routes(&card->dapm,
603 byt_rt5651_ssp2_aif2_map,
604 ARRAY_SIZE(byt_rt5651_ssp2_aif2_map));
605 } else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) {
606 ret = snd_soc_dapm_add_routes(&card->dapm,
607 byt_rt5651_ssp0_aif1_map,
608 ARRAY_SIZE(byt_rt5651_ssp0_aif1_map));
609 } else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2) {
610 ret = snd_soc_dapm_add_routes(&card->dapm,
611 byt_rt5651_ssp0_aif2_map,
612 ARRAY_SIZE(byt_rt5651_ssp0_aif2_map));
613 } else {
614 ret = snd_soc_dapm_add_routes(&card->dapm,
615 byt_rt5651_ssp2_aif1_map,
616 ARRAY_SIZE(byt_rt5651_ssp2_aif1_map));
617 }
618 if (ret)
619 return ret;
620
621 ret = snd_soc_add_card_controls(card, byt_rt5651_controls,
622 ARRAY_SIZE(byt_rt5651_controls));
623 if (ret) {
624 dev_err(card->dev, "unable to add card controls\n");
625 return ret;
626 }
627
628 /*
629 * The firmware might enable the clock at boot (this information
630 * may or may not be reflected in the enable clock register).
631 * To change the rate we must disable the clock first to cover
632 * these cases. Due to common clock framework restrictions that
633 * do not allow to disable a clock that has not been enabled,
634 * we need to enable the clock first.
635 */
636 ret = clk_prepare_enable(priv->mclk);
637 if (!ret)
638 clk_disable_unprepare(priv->mclk);
639
640 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
641 ret = clk_set_rate(priv->mclk, 25000000);
642 else
643 ret = clk_set_rate(priv->mclk, 19200000);
644
645 if (ret)
646 dev_err(card->dev, "unable to set MCLK rate\n");
647
648 report = 0;
649 if (BYT_RT5651_JDSRC(byt_rt5651_quirk))
650 report = SND_JACK_HEADSET | SND_JACK_BTN_0;
651 else if (priv->hp_detect)
652 report = SND_JACK_HEADSET;
653
654 if (report) {
655 ret = snd_soc_card_jack_new_pins(runtime->card, "Headset",
656 report, &priv->jack,
657 bytcr_jack_pins,
658 ARRAY_SIZE(bytcr_jack_pins));
659 if (ret) {
660 dev_err(runtime->dev, "jack creation failed %d\n", ret);
661 return ret;
662 }
663
664 if (report & SND_JACK_BTN_0)
665 snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0,
666 KEY_PLAYPAUSE);
667
668 ret = snd_soc_component_set_jack(codec, &priv->jack,
669 priv->hp_detect);
670 if (ret)
671 return ret;
672 }
673
674 return 0;
675}
676
677static int byt_rt5651_codec_fixup(struct snd_soc_pcm_runtime *rtd,
678 struct snd_pcm_hw_params *params)
679{
680 struct snd_interval *rate = hw_param_interval(params,
681 SNDRV_PCM_HW_PARAM_RATE);
682 struct snd_interval *channels = hw_param_interval(params,
683 SNDRV_PCM_HW_PARAM_CHANNELS);
684 int ret, bits;
685
686 /* The DSP will convert the FE rate to 48k, stereo */
687 rate->min = rate->max = 48000;
688 channels->min = channels->max = 2;
689
690 if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) ||
691 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
692 /* set SSP0 to 16-bit */
693 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
694 bits = 16;
695 } else {
696 /* set SSP2 to 24-bit */
697 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
698 bits = 24;
699 }
700
701 /*
702 * Default mode for SSP configuration is TDM 4 slot, override config
703 * with explicit setting to I2S 2ch. The word length is set with
704 * dai_set_tdm_slot() since there is no other API exposed
705 */
706 ret = snd_soc_dai_set_fmt(snd_soc_rtd_to_cpu(rtd, 0),
707 SND_SOC_DAIFMT_I2S |
708 SND_SOC_DAIFMT_NB_NF |
709 SND_SOC_DAIFMT_BP_FP
710 );
711
712 if (ret < 0) {
713 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
714 return ret;
715 }
716
717 ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits);
718 if (ret < 0) {
719 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
720 return ret;
721 }
722
723 return 0;
724}
725
726static const unsigned int rates_48000[] = {
727 48000,
728};
729
730static const struct snd_pcm_hw_constraint_list constraints_48000 = {
731 .count = ARRAY_SIZE(rates_48000),
732 .list = rates_48000,
733};
734
735static int byt_rt5651_aif1_startup(struct snd_pcm_substream *substream)
736{
737 return snd_pcm_hw_constraint_list(substream->runtime, 0,
738 SNDRV_PCM_HW_PARAM_RATE,
739 &constraints_48000);
740}
741
742static const struct snd_soc_ops byt_rt5651_aif1_ops = {
743 .startup = byt_rt5651_aif1_startup,
744};
745
746static const struct snd_soc_ops byt_rt5651_be_ssp2_ops = {
747 .hw_params = byt_rt5651_aif1_hw_params,
748};
749
750SND_SOC_DAILINK_DEF(dummy,
751 DAILINK_COMP_ARRAY(COMP_DUMMY()));
752
753SND_SOC_DAILINK_DEF(media,
754 DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
755
756SND_SOC_DAILINK_DEF(deepbuffer,
757 DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
758
759SND_SOC_DAILINK_DEF(ssp2_port,
760 DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
761SND_SOC_DAILINK_DEF(ssp2_codec,
762 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5651:00", "rt5651-aif1")));
763
764SND_SOC_DAILINK_DEF(platform,
765 DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
766
767static struct snd_soc_dai_link byt_rt5651_dais[] = {
768 [MERR_DPCM_AUDIO] = {
769 .name = "Audio Port",
770 .stream_name = "Audio",
771 .nonatomic = true,
772 .dynamic = 1,
773 .ops = &byt_rt5651_aif1_ops,
774 SND_SOC_DAILINK_REG(media, dummy, platform),
775 },
776 [MERR_DPCM_DEEP_BUFFER] = {
777 .name = "Deep-Buffer Audio Port",
778 .stream_name = "Deep-Buffer Audio",
779 .nonatomic = true,
780 .dynamic = 1,
781 .playback_only = 1,
782 .ops = &byt_rt5651_aif1_ops,
783 SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
784 },
785 /* CODEC<->CODEC link */
786 /* back ends */
787 {
788 .name = "SSP2-Codec",
789 .id = 0,
790 .no_pcm = 1,
791 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
792 | SND_SOC_DAIFMT_CBC_CFC,
793 .be_hw_params_fixup = byt_rt5651_codec_fixup,
794 .init = byt_rt5651_init,
795 .ops = &byt_rt5651_be_ssp2_ops,
796 SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
797 },
798};
799
800/* SoC card */
801static char byt_rt5651_codec_name[SND_ACPI_I2C_ID_LEN];
802#if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
803static char byt_rt5651_long_name[50]; /* = "bytcr-rt5651-*-spk-*-mic[-swapped-hp]" */
804#endif
805static char byt_rt5651_components[50]; /* = "cfg-spk:* cfg-mic:*" */
806
807static int byt_rt5651_suspend(struct snd_soc_card *card)
808{
809 struct snd_soc_component *component;
810
811 if (!BYT_RT5651_JDSRC(byt_rt5651_quirk))
812 return 0;
813
814 for_each_card_components(card, component) {
815 if (!strcmp(component->name, byt_rt5651_codec_name)) {
816 dev_dbg(component->dev, "disabling jack detect before suspend\n");
817 snd_soc_component_set_jack(component, NULL, NULL);
818 break;
819 }
820 }
821
822 return 0;
823}
824
825static int byt_rt5651_resume(struct snd_soc_card *card)
826{
827 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
828 struct snd_soc_component *component;
829
830 if (!BYT_RT5651_JDSRC(byt_rt5651_quirk))
831 return 0;
832
833 for_each_card_components(card, component) {
834 if (!strcmp(component->name, byt_rt5651_codec_name)) {
835 dev_dbg(component->dev, "re-enabling jack detect after resume\n");
836 snd_soc_component_set_jack(component, &priv->jack,
837 priv->hp_detect);
838 break;
839 }
840 }
841
842 return 0;
843}
844
845/* use space before codec name to simplify card ID, and simplify driver name */
846#define SOF_CARD_NAME "bytcht rt5651" /* card name will be 'sof-bytcht rt5651' */
847#define SOF_DRIVER_NAME "SOF"
848
849#define CARD_NAME "bytcr-rt5651"
850#define DRIVER_NAME NULL /* card name will be used for driver name */
851
852static struct snd_soc_card byt_rt5651_card = {
853 .name = CARD_NAME,
854 .driver_name = DRIVER_NAME,
855 .owner = THIS_MODULE,
856 .dai_link = byt_rt5651_dais,
857 .num_links = ARRAY_SIZE(byt_rt5651_dais),
858 .dapm_widgets = byt_rt5651_widgets,
859 .num_dapm_widgets = ARRAY_SIZE(byt_rt5651_widgets),
860 .dapm_routes = byt_rt5651_audio_map,
861 .num_dapm_routes = ARRAY_SIZE(byt_rt5651_audio_map),
862 .fully_routed = true,
863 .suspend_pre = byt_rt5651_suspend,
864 .resume_post = byt_rt5651_resume,
865};
866
867static const struct acpi_gpio_params ext_amp_enable_gpios = { 0, 0, false };
868
869static const struct acpi_gpio_mapping cht_rt5651_gpios[] = {
870 /*
871 * Some boards have I2cSerialBusV2, GpioIo, GpioInt as ACPI resources,
872 * other boards may have I2cSerialBusV2, GpioInt, GpioIo instead.
873 * We want the GpioIo one for the ext-amp-enable-gpio.
874 */
875 { "ext-amp-enable-gpios", &ext_amp_enable_gpios, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO },
876 { },
877};
878
879struct acpi_chan_package { /* ACPICA seems to require 64 bit integers */
880 u64 aif_value; /* 1: AIF1, 2: AIF2 */
881 u64 mclock_value; /* usually 25MHz (0x17d7940), ignored */
882};
883
884static int snd_byt_rt5651_mc_probe(struct platform_device *pdev)
885{
886 struct device *dev = &pdev->dev;
887 static const char * const mic_name[] = { "dmic", "in1", "in2", "in12" };
888 struct snd_soc_acpi_mach *mach = dev_get_platdata(dev);
889 struct byt_rt5651_private *priv;
890 const char *platform_name;
891 struct acpi_device *adev;
892 struct device *codec_dev;
893 bool sof_parent;
894 bool is_bytcr = false;
895 int ret_val = 0;
896 int dai_index = 0;
897 int i;
898
899 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
900 if (!priv)
901 return -ENOMEM;
902
903 /* register the soc card */
904 byt_rt5651_card.dev = dev;
905 snd_soc_card_set_drvdata(&byt_rt5651_card, priv);
906
907 /* fix index of codec dai */
908 for (i = 0; i < ARRAY_SIZE(byt_rt5651_dais); i++) {
909 if (byt_rt5651_dais[i].num_codecs &&
910 !strcmp(byt_rt5651_dais[i].codecs->name,
911 "i2c-10EC5651:00")) {
912 dai_index = i;
913 break;
914 }
915 }
916
917 /* fixup codec name based on HID */
918 adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
919 if (adev) {
920 snprintf(byt_rt5651_codec_name, sizeof(byt_rt5651_codec_name),
921 "i2c-%s", acpi_dev_name(adev));
922 byt_rt5651_dais[dai_index].codecs->name = byt_rt5651_codec_name;
923 } else {
924 dev_err(dev, "Error cannot find '%s' dev\n", mach->id);
925 return -ENOENT;
926 }
927
928 codec_dev = acpi_get_first_physical_node(adev);
929 acpi_dev_put(adev);
930 if (!codec_dev)
931 return -EPROBE_DEFER;
932 priv->codec_dev = get_device(codec_dev);
933
934 /*
935 * swap SSP0 if bytcr is detected
936 * (will be overridden if DMI quirk is detected)
937 */
938 if (soc_intel_is_byt()) {
939 if (mach->mach_params.acpi_ipc_irq_index == 0)
940 is_bytcr = true;
941 }
942
943 if (is_bytcr) {
944 /*
945 * Baytrail CR platforms may have CHAN package in BIOS, try
946 * to find relevant routing quirk based as done on Windows
947 * platforms. We have to read the information directly from the
948 * BIOS, at this stage the card is not created and the links
949 * with the codec driver/pdata are non-existent
950 */
951
952 struct acpi_chan_package chan_package = { 0 };
953
954 /* format specified: 2 64-bit integers */
955 struct acpi_buffer format = {sizeof("NN"), "NN"};
956 struct acpi_buffer state = {0, NULL};
957 struct snd_soc_acpi_package_context pkg_ctx;
958 bool pkg_found = false;
959
960 state.length = sizeof(chan_package);
961 state.pointer = &chan_package;
962
963 pkg_ctx.name = "CHAN";
964 pkg_ctx.length = 2;
965 pkg_ctx.format = &format;
966 pkg_ctx.state = &state;
967 pkg_ctx.data_valid = false;
968
969 pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
970 &pkg_ctx);
971 if (pkg_found) {
972 if (chan_package.aif_value == 1) {
973 dev_info(dev, "BIOS Routing: AIF1 connected\n");
974 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF1;
975 } else if (chan_package.aif_value == 2) {
976 dev_info(dev, "BIOS Routing: AIF2 connected\n");
977 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2;
978 } else {
979 dev_info(dev, "BIOS Routing isn't valid, ignored\n");
980 pkg_found = false;
981 }
982 }
983
984 if (!pkg_found) {
985 /* no BIOS indications, assume SSP0-AIF2 connection */
986 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2;
987 }
988 }
989
990 /* check quirks before creating card */
991 dmi_check_system(byt_rt5651_quirk_table);
992
993 if (quirk_override != -1) {
994 dev_info(dev, "Overriding quirk 0x%lx => 0x%x\n",
995 byt_rt5651_quirk, quirk_override);
996 byt_rt5651_quirk = quirk_override;
997 }
998
999 /* Must be called before register_card, also see declaration comment. */
1000 ret_val = byt_rt5651_add_codec_device_props(codec_dev, priv);
1001 if (ret_val)
1002 goto err_device;
1003
1004 /* Cherry Trail devices use an external amplifier enable gpio */
1005 if (soc_intel_is_cht() && !byt_rt5651_gpios)
1006 byt_rt5651_gpios = cht_rt5651_gpios;
1007
1008 if (byt_rt5651_gpios) {
1009 devm_acpi_dev_add_driver_gpios(codec_dev, byt_rt5651_gpios);
1010 priv->ext_amp_gpio = devm_fwnode_gpiod_get(dev, codec_dev->fwnode,
1011 "ext-amp-enable",
1012 GPIOD_OUT_LOW,
1013 "speaker-amp");
1014 if (IS_ERR(priv->ext_amp_gpio)) {
1015 ret_val = PTR_ERR(priv->ext_amp_gpio);
1016 switch (ret_val) {
1017 case -ENOENT:
1018 priv->ext_amp_gpio = NULL;
1019 break;
1020 default:
1021 dev_err(dev, "Failed to get ext-amp-enable GPIO: %d\n", ret_val);
1022 fallthrough;
1023 case -EPROBE_DEFER:
1024 goto err;
1025 }
1026 }
1027 priv->hp_detect = devm_fwnode_gpiod_get(dev, codec_dev->fwnode,
1028 "hp-detect",
1029 GPIOD_IN,
1030 "hp-detect");
1031 if (IS_ERR(priv->hp_detect)) {
1032 ret_val = PTR_ERR(priv->hp_detect);
1033 switch (ret_val) {
1034 case -ENOENT:
1035 priv->hp_detect = NULL;
1036 break;
1037 default:
1038 dev_err(dev, "Failed to get hp-detect GPIO: %d\n", ret_val);
1039 fallthrough;
1040 case -EPROBE_DEFER:
1041 goto err;
1042 }
1043 }
1044 }
1045
1046 log_quirks(dev);
1047
1048 if ((byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) ||
1049 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2))
1050 byt_rt5651_dais[dai_index].codecs->dai_name = "rt5651-aif2";
1051
1052 if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) ||
1053 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2))
1054 byt_rt5651_dais[dai_index].cpus->dai_name = "ssp0-port";
1055
1056 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
1057 priv->mclk = devm_clk_get_optional(dev, "pmc_plt_clk_3");
1058 if (IS_ERR(priv->mclk)) {
1059 ret_val = dev_err_probe(dev, PTR_ERR(priv->mclk),
1060 "Failed to get MCLK from pmc_plt_clk_3\n");
1061 goto err;
1062 }
1063 /*
1064 * Fall back to bit clock usage when clock is not
1065 * available likely due to missing dependencies.
1066 */
1067 if (!priv->mclk)
1068 byt_rt5651_quirk &= ~BYT_RT5651_MCLK_EN;
1069 }
1070
1071 snprintf(byt_rt5651_components, sizeof(byt_rt5651_components),
1072 "cfg-spk:%s cfg-mic:%s%s",
1073 (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER) ? "1" : "2",
1074 mic_name[BYT_RT5651_MAP(byt_rt5651_quirk)],
1075 (byt_rt5651_quirk & BYT_RT5651_HP_LR_SWAPPED) ?
1076 " cfg-hp:lrswap" : "");
1077 byt_rt5651_card.components = byt_rt5651_components;
1078#if !IS_ENABLED(CONFIG_SND_SOC_INTEL_USER_FRIENDLY_LONG_NAMES)
1079 snprintf(byt_rt5651_long_name, sizeof(byt_rt5651_long_name),
1080 "bytcr-rt5651-%s-spk-%s-mic%s",
1081 (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER) ?
1082 "mono" : "stereo",
1083 mic_name[BYT_RT5651_MAP(byt_rt5651_quirk)],
1084 (byt_rt5651_quirk & BYT_RT5651_HP_LR_SWAPPED) ?
1085 "-hp-swapped" : "");
1086 byt_rt5651_card.long_name = byt_rt5651_long_name;
1087#endif
1088
1089 /* override platform name, if required */
1090 platform_name = mach->mach_params.platform;
1091
1092 ret_val = snd_soc_fixup_dai_links_platform_name(&byt_rt5651_card,
1093 platform_name);
1094 if (ret_val)
1095 goto err;
1096
1097 sof_parent = snd_soc_acpi_sof_parent(dev);
1098
1099 /* set card and driver name */
1100 if (sof_parent) {
1101 byt_rt5651_card.name = SOF_CARD_NAME;
1102 byt_rt5651_card.driver_name = SOF_DRIVER_NAME;
1103 } else {
1104 byt_rt5651_card.name = CARD_NAME;
1105 byt_rt5651_card.driver_name = DRIVER_NAME;
1106 }
1107
1108 /* set pm ops */
1109 if (sof_parent)
1110 dev->driver->pm = &snd_soc_pm_ops;
1111
1112 ret_val = devm_snd_soc_register_card(dev, &byt_rt5651_card);
1113 if (ret_val) {
1114 dev_err(dev, "devm_snd_soc_register_card failed %d\n", ret_val);
1115 goto err;
1116 }
1117 platform_set_drvdata(pdev, &byt_rt5651_card);
1118 return ret_val;
1119
1120err:
1121 device_remove_software_node(priv->codec_dev);
1122err_device:
1123 put_device(priv->codec_dev);
1124 return ret_val;
1125}
1126
1127static void snd_byt_rt5651_mc_remove(struct platform_device *pdev)
1128{
1129 struct snd_soc_card *card = platform_get_drvdata(pdev);
1130 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
1131
1132 device_remove_software_node(priv->codec_dev);
1133 put_device(priv->codec_dev);
1134}
1135
1136static struct platform_driver snd_byt_rt5651_mc_driver = {
1137 .driver = {
1138 .name = "bytcr_rt5651",
1139 },
1140 .probe = snd_byt_rt5651_mc_probe,
1141 .remove = snd_byt_rt5651_mc_remove,
1142};
1143
1144module_platform_driver(snd_byt_rt5651_mc_driver);
1145
1146MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver for RT5651");
1147MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
1148MODULE_LICENSE("GPL v2");
1149MODULE_ALIAS("platform:bytcr_rt5651");
1/*
2 * bytcr_rt5651.c - ASoc Machine driver for Intel Byt CR platform
3 * (derived from bytcr_rt5640.c)
4 *
5 * Copyright (C) 2015 Intel Corp
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/i2c.h>
22#include <linux/module.h>
23#include <linux/platform_device.h>
24#include <linux/property.h>
25#include <linux/acpi.h>
26#include <linux/clk.h>
27#include <linux/device.h>
28#include <linux/dmi.h>
29#include <linux/slab.h>
30#include <asm/cpu_device_id.h>
31#include <asm/platform_sst_audio.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/soc.h>
35#include <sound/jack.h>
36#include <sound/soc-acpi.h>
37#include "../../codecs/rt5651.h"
38#include "../atom/sst-atom-controls.h"
39
40enum {
41 BYT_RT5651_DMIC_MAP,
42 BYT_RT5651_IN1_MAP,
43 BYT_RT5651_IN2_MAP,
44 BYT_RT5651_IN1_IN2_MAP,
45 BYT_RT5651_IN1_HS_IN3_MAP,
46 BYT_RT5651_IN2_HS_IN3_MAP,
47};
48
49enum {
50 BYT_RT5651_JD_NULL = (RT5651_JD_NULL << 4),
51 BYT_RT5651_JD1_1 = (RT5651_JD1_1 << 4),
52 BYT_RT5651_JD1_2 = (RT5651_JD1_2 << 4),
53 BYT_RT5651_JD2 = (RT5651_JD2 << 4),
54};
55
56enum {
57 BYT_RT5651_OVCD_TH_600UA = (6 << 8),
58 BYT_RT5651_OVCD_TH_1500UA = (15 << 8),
59 BYT_RT5651_OVCD_TH_2000UA = (20 << 8),
60};
61
62enum {
63 BYT_RT5651_OVCD_SF_0P5 = (RT5651_OVCD_SF_0P5 << 13),
64 BYT_RT5651_OVCD_SF_0P75 = (RT5651_OVCD_SF_0P75 << 13),
65 BYT_RT5651_OVCD_SF_1P0 = (RT5651_OVCD_SF_1P0 << 13),
66 BYT_RT5651_OVCD_SF_1P5 = (RT5651_OVCD_SF_1P5 << 13),
67};
68
69#define BYT_RT5651_MAP(quirk) ((quirk) & GENMASK(3, 0))
70#define BYT_RT5651_JDSRC(quirk) (((quirk) & GENMASK(7, 4)) >> 4)
71#define BYT_RT5651_OVCD_TH(quirk) (((quirk) & GENMASK(12, 8)) >> 8)
72#define BYT_RT5651_OVCD_SF(quirk) (((quirk) & GENMASK(14, 13)) >> 13)
73#define BYT_RT5651_DMIC_EN BIT(16)
74#define BYT_RT5651_MCLK_EN BIT(17)
75#define BYT_RT5651_MCLK_25MHZ BIT(18)
76#define BYT_RT5651_SSP2_AIF2 BIT(19) /* default is using AIF1 */
77#define BYT_RT5651_SSP0_AIF1 BIT(20)
78#define BYT_RT5651_SSP0_AIF2 BIT(21)
79
80/* jack-detect-source + dmic-en + ovcd-th + -sf + terminating empty entry */
81#define MAX_NO_PROPS 5
82
83struct byt_rt5651_private {
84 struct clk *mclk;
85 struct snd_soc_jack jack;
86};
87
88/* Default: jack-detect on JD1_1, internal mic on in2, headsetmic on in3 */
89static unsigned long byt_rt5651_quirk = BYT_RT5651_MCLK_EN |
90 BYT_RT5651_JD1_1 |
91 BYT_RT5651_OVCD_TH_2000UA |
92 BYT_RT5651_OVCD_SF_0P75 |
93 BYT_RT5651_IN2_HS_IN3_MAP;
94
95static void log_quirks(struct device *dev)
96{
97 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_DMIC_MAP)
98 dev_info(dev, "quirk DMIC_MAP enabled");
99 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_MAP)
100 dev_info(dev, "quirk IN1_MAP enabled");
101 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN2_MAP)
102 dev_info(dev, "quirk IN2_MAP enabled");
103 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_HS_IN3_MAP)
104 dev_info(dev, "quirk IN1_HS_IN3_MAP enabled");
105 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN2_HS_IN3_MAP)
106 dev_info(dev, "quirk IN2_HS_IN3_MAP enabled");
107 if (BYT_RT5651_JDSRC(byt_rt5651_quirk)) {
108 dev_info(dev, "quirk realtek,jack-detect-source %ld\n",
109 BYT_RT5651_JDSRC(byt_rt5651_quirk));
110 dev_info(dev, "quirk realtek,over-current-threshold-microamp %ld\n",
111 BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100);
112 dev_info(dev, "quirk realtek,over-current-scale-factor %ld\n",
113 BYT_RT5651_OVCD_SF(byt_rt5651_quirk));
114 }
115 if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN)
116 dev_info(dev, "quirk DMIC enabled");
117 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
118 dev_info(dev, "quirk MCLK_EN enabled");
119 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
120 dev_info(dev, "quirk MCLK_25MHZ enabled");
121 if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2)
122 dev_info(dev, "quirk SSP2_AIF2 enabled\n");
123 if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1)
124 dev_info(dev, "quirk SSP0_AIF1 enabled\n");
125 if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)
126 dev_info(dev, "quirk SSP0_AIF2 enabled\n");
127}
128
129#define BYT_CODEC_DAI1 "rt5651-aif1"
130#define BYT_CODEC_DAI2 "rt5651-aif2"
131
132static int byt_rt5651_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai,
133 int rate, int bclk_ratio)
134{
135 int clk_id, clk_freq, ret;
136
137 /* Configure the PLL before selecting it */
138 if (!(byt_rt5651_quirk & BYT_RT5651_MCLK_EN)) {
139 clk_id = RT5651_PLL1_S_BCLK1,
140 clk_freq = rate * bclk_ratio;
141 } else {
142 clk_id = RT5651_PLL1_S_MCLK;
143 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
144 clk_freq = 25000000;
145 else
146 clk_freq = 19200000;
147 }
148 ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, rate * 512);
149 if (ret < 0) {
150 dev_err(codec_dai->component->dev, "can't set pll: %d\n", ret);
151 return ret;
152 }
153
154 ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1,
155 rate * 512, SND_SOC_CLOCK_IN);
156 if (ret < 0) {
157 dev_err(codec_dai->component->dev, "can't set clock %d\n", ret);
158 return ret;
159 }
160
161 return 0;
162}
163
164static int platform_clock_control(struct snd_soc_dapm_widget *w,
165 struct snd_kcontrol *k, int event)
166{
167 struct snd_soc_dapm_context *dapm = w->dapm;
168 struct snd_soc_card *card = dapm->card;
169 struct snd_soc_dai *codec_dai;
170 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
171 int ret;
172
173 codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1);
174 if (!codec_dai)
175 codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI2);
176 if (!codec_dai) {
177 dev_err(card->dev,
178 "Codec dai not found; Unable to set platform clock\n");
179 return -EIO;
180 }
181
182 if (SND_SOC_DAPM_EVENT_ON(event)) {
183 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
184 ret = clk_prepare_enable(priv->mclk);
185 if (ret < 0) {
186 dev_err(card->dev,
187 "could not configure MCLK state");
188 return ret;
189 }
190 }
191 ret = byt_rt5651_prepare_and_enable_pll1(codec_dai, 48000, 50);
192 } else {
193 /*
194 * Set codec clock source to internal clock before
195 * turning off the platform clock. Codec needs clock
196 * for Jack detection and button press
197 */
198 ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_RCCLK,
199 48000 * 512,
200 SND_SOC_CLOCK_IN);
201 if (!ret)
202 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
203 clk_disable_unprepare(priv->mclk);
204 }
205
206 if (ret < 0) {
207 dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
208 return ret;
209 }
210
211 return 0;
212}
213
214static const struct snd_soc_dapm_widget byt_rt5651_widgets[] = {
215 SND_SOC_DAPM_HP("Headphone", NULL),
216 SND_SOC_DAPM_MIC("Headset Mic", NULL),
217 SND_SOC_DAPM_MIC("Internal Mic", NULL),
218 SND_SOC_DAPM_SPK("Speaker", NULL),
219 SND_SOC_DAPM_LINE("Line In", NULL),
220 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
221 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
222 SND_SOC_DAPM_POST_PMD),
223
224};
225
226static const struct snd_soc_dapm_route byt_rt5651_audio_map[] = {
227 {"Headphone", NULL, "Platform Clock"},
228 {"Headset Mic", NULL, "Platform Clock"},
229 {"Internal Mic", NULL, "Platform Clock"},
230 {"Speaker", NULL, "Platform Clock"},
231 {"Line In", NULL, "Platform Clock"},
232
233 {"Headset Mic", NULL, "micbias1"}, /* lowercase for rt5651 */
234 {"Headphone", NULL, "HPOL"},
235 {"Headphone", NULL, "HPOR"},
236 {"Speaker", NULL, "LOUTL"},
237 {"Speaker", NULL, "LOUTR"},
238 {"IN2P", NULL, "Line In"},
239 {"IN2N", NULL, "Line In"},
240
241};
242
243static const struct snd_soc_dapm_route byt_rt5651_intmic_dmic_map[] = {
244 {"IN2P", NULL, "Headset Mic"},
245 {"DMIC L1", NULL, "Internal Mic"},
246 {"DMIC R1", NULL, "Internal Mic"},
247};
248
249static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_map[] = {
250 {"Internal Mic", NULL, "micbias1"},
251 {"IN1P", NULL, "Internal Mic"},
252 {"IN2P", NULL, "Headset Mic"},
253};
254
255static const struct snd_soc_dapm_route byt_rt5651_intmic_in2_map[] = {
256 {"Internal Mic", NULL, "micbias1"},
257 {"IN1P", NULL, "Headset Mic"},
258 {"IN2P", NULL, "Internal Mic"},
259};
260
261static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_in2_map[] = {
262 {"Internal Mic", NULL, "micbias1"},
263 {"IN1P", NULL, "Internal Mic"},
264 {"IN2P", NULL, "Internal Mic"},
265 {"IN3P", NULL, "Headset Mic"},
266};
267
268static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_hs_in3_map[] = {
269 {"Internal Mic", NULL, "micbias1"},
270 {"IN1P", NULL, "Internal Mic"},
271 {"IN3P", NULL, "Headset Mic"},
272};
273
274static const struct snd_soc_dapm_route byt_rt5651_intmic_in2_hs_in3_map[] = {
275 {"Internal Mic", NULL, "micbias1"},
276 {"IN2P", NULL, "Internal Mic"},
277 {"IN3P", NULL, "Headset Mic"},
278};
279
280static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif1_map[] = {
281 {"ssp0 Tx", NULL, "modem_out"},
282 {"modem_in", NULL, "ssp0 Rx"},
283
284 {"AIF1 Playback", NULL, "ssp0 Tx"},
285 {"ssp0 Rx", NULL, "AIF1 Capture"},
286};
287
288static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif2_map[] = {
289 {"ssp0 Tx", NULL, "modem_out"},
290 {"modem_in", NULL, "ssp0 Rx"},
291
292 {"AIF2 Playback", NULL, "ssp0 Tx"},
293 {"ssp0 Rx", NULL, "AIF2 Capture"},
294};
295
296static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif1_map[] = {
297 {"ssp2 Tx", NULL, "codec_out0"},
298 {"ssp2 Tx", NULL, "codec_out1"},
299 {"codec_in0", NULL, "ssp2 Rx"},
300 {"codec_in1", NULL, "ssp2 Rx"},
301
302 {"AIF1 Playback", NULL, "ssp2 Tx"},
303 {"ssp2 Rx", NULL, "AIF1 Capture"},
304};
305
306static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif2_map[] = {
307 {"ssp2 Tx", NULL, "codec_out0"},
308 {"ssp2 Tx", NULL, "codec_out1"},
309 {"codec_in0", NULL, "ssp2 Rx"},
310 {"codec_in1", NULL, "ssp2 Rx"},
311
312 {"AIF2 Playback", NULL, "ssp2 Tx"},
313 {"ssp2 Rx", NULL, "AIF2 Capture"},
314};
315
316static const struct snd_kcontrol_new byt_rt5651_controls[] = {
317 SOC_DAPM_PIN_SWITCH("Headphone"),
318 SOC_DAPM_PIN_SWITCH("Headset Mic"),
319 SOC_DAPM_PIN_SWITCH("Internal Mic"),
320 SOC_DAPM_PIN_SWITCH("Speaker"),
321 SOC_DAPM_PIN_SWITCH("Line In"),
322};
323
324static struct snd_soc_jack_pin bytcr_jack_pins[] = {
325 {
326 .pin = "Headphone",
327 .mask = SND_JACK_HEADPHONE,
328 },
329 {
330 .pin = "Headset Mic",
331 .mask = SND_JACK_MICROPHONE,
332 },
333};
334
335static int byt_rt5651_aif1_hw_params(struct snd_pcm_substream *substream,
336 struct snd_pcm_hw_params *params)
337{
338 struct snd_soc_pcm_runtime *rtd = substream->private_data;
339 struct snd_soc_dai *codec_dai = rtd->codec_dai;
340 snd_pcm_format_t format = params_format(params);
341 int rate = params_rate(params);
342 int bclk_ratio;
343
344 if (format == SNDRV_PCM_FORMAT_S16_LE)
345 bclk_ratio = 32;
346 else
347 bclk_ratio = 50;
348
349 return byt_rt5651_prepare_and_enable_pll1(codec_dai, rate, bclk_ratio);
350}
351
352static int byt_rt5651_quirk_cb(const struct dmi_system_id *id)
353{
354 byt_rt5651_quirk = (unsigned long)id->driver_data;
355 return 1;
356}
357
358static const struct dmi_system_id byt_rt5651_quirk_table[] = {
359 {
360 .callback = byt_rt5651_quirk_cb,
361 .matches = {
362 DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
363 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
364 },
365 .driver_data = (void *)(BYT_RT5651_IN1_HS_IN3_MAP),
366 },
367 {
368 .callback = byt_rt5651_quirk_cb,
369 .matches = {
370 DMI_MATCH(DMI_SYS_VENDOR, "ADI"),
371 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Turbot"),
372 },
373 .driver_data = (void *)(BYT_RT5651_MCLK_EN |
374 BYT_RT5651_IN1_HS_IN3_MAP),
375 },
376 {
377 .callback = byt_rt5651_quirk_cb,
378 .matches = {
379 DMI_MATCH(DMI_SYS_VENDOR, "KIANO"),
380 DMI_MATCH(DMI_PRODUCT_NAME, "KIANO SlimNote 14.2"),
381 },
382 .driver_data = (void *)(BYT_RT5651_MCLK_EN |
383 BYT_RT5651_JD1_1 |
384 BYT_RT5651_OVCD_TH_2000UA |
385 BYT_RT5651_OVCD_SF_0P75 |
386 BYT_RT5651_IN1_IN2_MAP),
387 },
388 {
389 /* Chuwi Vi8 Plus (CWI519) */
390 .callback = byt_rt5651_quirk_cb,
391 .matches = {
392 DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"),
393 DMI_MATCH(DMI_PRODUCT_NAME, "D2D3_Vi8A1"),
394 },
395 .driver_data = (void *)(BYT_RT5651_MCLK_EN |
396 BYT_RT5651_JD1_1 |
397 BYT_RT5651_OVCD_TH_2000UA |
398 BYT_RT5651_OVCD_SF_0P75 |
399 BYT_RT5651_IN2_HS_IN3_MAP),
400 },
401 {
402 /* VIOS LTH17 */
403 .callback = byt_rt5651_quirk_cb,
404 .matches = {
405 DMI_MATCH(DMI_SYS_VENDOR, "VIOS"),
406 DMI_MATCH(DMI_PRODUCT_NAME, "LTH17"),
407 },
408 .driver_data = (void *)(BYT_RT5651_MCLK_EN |
409 BYT_RT5651_JD1_1 |
410 BYT_RT5651_OVCD_TH_2000UA |
411 BYT_RT5651_OVCD_SF_1P0 |
412 BYT_RT5651_IN1_IN2_MAP),
413 },
414 {}
415};
416
417/*
418 * Note this MUST be called before snd_soc_register_card(), so that the props
419 * are in place before the codec component driver's probe function parses them.
420 */
421static int byt_rt5651_add_codec_device_props(const char *i2c_dev_name)
422{
423 struct property_entry props[MAX_NO_PROPS] = {};
424 struct device *i2c_dev;
425 int ret, cnt = 0;
426
427 i2c_dev = bus_find_device_by_name(&i2c_bus_type, NULL, i2c_dev_name);
428 if (!i2c_dev)
429 return -EPROBE_DEFER;
430
431 props[cnt++] = PROPERTY_ENTRY_U32("realtek,jack-detect-source",
432 BYT_RT5651_JDSRC(byt_rt5651_quirk));
433
434 props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-threshold-microamp",
435 BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100);
436
437 props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-scale-factor",
438 BYT_RT5651_OVCD_SF(byt_rt5651_quirk));
439
440 if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN)
441 props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,dmic-en");
442
443 ret = device_add_properties(i2c_dev, props);
444 put_device(i2c_dev);
445
446 return ret;
447}
448
449static int byt_rt5651_init(struct snd_soc_pcm_runtime *runtime)
450{
451 struct snd_soc_card *card = runtime->card;
452 struct snd_soc_component *codec = runtime->codec_dai->component;
453 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
454 const struct snd_soc_dapm_route *custom_map;
455 int num_routes;
456 int ret;
457
458 card->dapm.idle_bias_off = true;
459
460 /* Start with RC clk for jack-detect (we disable MCLK below) */
461 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
462 snd_soc_component_update_bits(codec, RT5651_GLB_CLK,
463 RT5651_SCLK_SRC_MASK, RT5651_SCLK_SRC_RCCLK);
464
465 switch (BYT_RT5651_MAP(byt_rt5651_quirk)) {
466 case BYT_RT5651_IN1_MAP:
467 custom_map = byt_rt5651_intmic_in1_map;
468 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_map);
469 break;
470 case BYT_RT5651_IN2_MAP:
471 custom_map = byt_rt5651_intmic_in2_map;
472 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in2_map);
473 break;
474 case BYT_RT5651_IN1_IN2_MAP:
475 custom_map = byt_rt5651_intmic_in1_in2_map;
476 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_in2_map);
477 break;
478 case BYT_RT5651_IN1_HS_IN3_MAP:
479 custom_map = byt_rt5651_intmic_in1_hs_in3_map;
480 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_hs_in3_map);
481 break;
482 case BYT_RT5651_IN2_HS_IN3_MAP:
483 custom_map = byt_rt5651_intmic_in2_hs_in3_map;
484 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in2_hs_in3_map);
485 break;
486 default:
487 custom_map = byt_rt5651_intmic_dmic_map;
488 num_routes = ARRAY_SIZE(byt_rt5651_intmic_dmic_map);
489 }
490 ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
491 if (ret)
492 return ret;
493
494 if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) {
495 ret = snd_soc_dapm_add_routes(&card->dapm,
496 byt_rt5651_ssp2_aif2_map,
497 ARRAY_SIZE(byt_rt5651_ssp2_aif2_map));
498 } else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) {
499 ret = snd_soc_dapm_add_routes(&card->dapm,
500 byt_rt5651_ssp0_aif1_map,
501 ARRAY_SIZE(byt_rt5651_ssp0_aif1_map));
502 } else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2) {
503 ret = snd_soc_dapm_add_routes(&card->dapm,
504 byt_rt5651_ssp0_aif2_map,
505 ARRAY_SIZE(byt_rt5651_ssp0_aif2_map));
506 } else {
507 ret = snd_soc_dapm_add_routes(&card->dapm,
508 byt_rt5651_ssp2_aif1_map,
509 ARRAY_SIZE(byt_rt5651_ssp2_aif1_map));
510 }
511 if (ret)
512 return ret;
513
514 ret = snd_soc_add_card_controls(card, byt_rt5651_controls,
515 ARRAY_SIZE(byt_rt5651_controls));
516 if (ret) {
517 dev_err(card->dev, "unable to add card controls\n");
518 return ret;
519 }
520 snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
521 snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
522
523 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
524 /*
525 * The firmware might enable the clock at
526 * boot (this information may or may not
527 * be reflected in the enable clock register).
528 * To change the rate we must disable the clock
529 * first to cover these cases. Due to common
530 * clock framework restrictions that do not allow
531 * to disable a clock that has not been enabled,
532 * we need to enable the clock first.
533 */
534 ret = clk_prepare_enable(priv->mclk);
535 if (!ret)
536 clk_disable_unprepare(priv->mclk);
537
538 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
539 ret = clk_set_rate(priv->mclk, 25000000);
540 else
541 ret = clk_set_rate(priv->mclk, 19200000);
542
543 if (ret)
544 dev_err(card->dev, "unable to set MCLK rate\n");
545 }
546
547 if (BYT_RT5651_JDSRC(byt_rt5651_quirk)) {
548 ret = snd_soc_card_jack_new(runtime->card, "Headset",
549 SND_JACK_HEADSET, &priv->jack,
550 bytcr_jack_pins, ARRAY_SIZE(bytcr_jack_pins));
551 if (ret) {
552 dev_err(runtime->dev, "jack creation failed %d\n", ret);
553 return ret;
554 }
555
556 ret = snd_soc_component_set_jack(codec, &priv->jack, NULL);
557 if (ret)
558 return ret;
559 }
560
561 return 0;
562}
563
564static const struct snd_soc_pcm_stream byt_rt5651_dai_params = {
565 .formats = SNDRV_PCM_FMTBIT_S24_LE,
566 .rate_min = 48000,
567 .rate_max = 48000,
568 .channels_min = 2,
569 .channels_max = 2,
570};
571
572static int byt_rt5651_codec_fixup(struct snd_soc_pcm_runtime *rtd,
573 struct snd_pcm_hw_params *params)
574{
575 struct snd_interval *rate = hw_param_interval(params,
576 SNDRV_PCM_HW_PARAM_RATE);
577 struct snd_interval *channels = hw_param_interval(params,
578 SNDRV_PCM_HW_PARAM_CHANNELS);
579 int ret, bits;
580
581 /* The DSP will covert the FE rate to 48k, stereo */
582 rate->min = rate->max = 48000;
583 channels->min = channels->max = 2;
584
585 if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) ||
586 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
587 /* set SSP0 to 16-bit */
588 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
589 bits = 16;
590 } else {
591 /* set SSP2 to 24-bit */
592 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
593 bits = 24;
594 }
595
596 /*
597 * Default mode for SSP configuration is TDM 4 slot, override config
598 * with explicit setting to I2S 2ch. The word length is set with
599 * dai_set_tdm_slot() since there is no other API exposed
600 */
601 ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
602 SND_SOC_DAIFMT_I2S |
603 SND_SOC_DAIFMT_NB_NF |
604 SND_SOC_DAIFMT_CBS_CFS
605 );
606
607 if (ret < 0) {
608 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
609 return ret;
610 }
611
612 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, bits);
613 if (ret < 0) {
614 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
615 return ret;
616 }
617
618 return 0;
619}
620
621static const unsigned int rates_48000[] = {
622 48000,
623};
624
625static const struct snd_pcm_hw_constraint_list constraints_48000 = {
626 .count = ARRAY_SIZE(rates_48000),
627 .list = rates_48000,
628};
629
630static int byt_rt5651_aif1_startup(struct snd_pcm_substream *substream)
631{
632 return snd_pcm_hw_constraint_list(substream->runtime, 0,
633 SNDRV_PCM_HW_PARAM_RATE,
634 &constraints_48000);
635}
636
637static const struct snd_soc_ops byt_rt5651_aif1_ops = {
638 .startup = byt_rt5651_aif1_startup,
639};
640
641static const struct snd_soc_ops byt_rt5651_be_ssp2_ops = {
642 .hw_params = byt_rt5651_aif1_hw_params,
643};
644
645static struct snd_soc_dai_link byt_rt5651_dais[] = {
646 [MERR_DPCM_AUDIO] = {
647 .name = "Audio Port",
648 .stream_name = "Audio",
649 .cpu_dai_name = "media-cpu-dai",
650 .codec_dai_name = "snd-soc-dummy-dai",
651 .codec_name = "snd-soc-dummy",
652 .platform_name = "sst-mfld-platform",
653 .nonatomic = true,
654 .dynamic = 1,
655 .dpcm_playback = 1,
656 .dpcm_capture = 1,
657 .ops = &byt_rt5651_aif1_ops,
658 },
659 [MERR_DPCM_DEEP_BUFFER] = {
660 .name = "Deep-Buffer Audio Port",
661 .stream_name = "Deep-Buffer Audio",
662 .cpu_dai_name = "deepbuffer-cpu-dai",
663 .codec_dai_name = "snd-soc-dummy-dai",
664 .codec_name = "snd-soc-dummy",
665 .platform_name = "sst-mfld-platform",
666 .nonatomic = true,
667 .dynamic = 1,
668 .dpcm_playback = 1,
669 .ops = &byt_rt5651_aif1_ops,
670 },
671 /* CODEC<->CODEC link */
672 /* back ends */
673 {
674 .name = "SSP2-Codec",
675 .id = 0,
676 .cpu_dai_name = "ssp2-port",
677 .platform_name = "sst-mfld-platform",
678 .no_pcm = 1,
679 .codec_dai_name = "rt5651-aif1",
680 .codec_name = "i2c-10EC5651:00",
681 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
682 | SND_SOC_DAIFMT_CBS_CFS,
683 .be_hw_params_fixup = byt_rt5651_codec_fixup,
684 .ignore_suspend = 1,
685 .nonatomic = true,
686 .dpcm_playback = 1,
687 .dpcm_capture = 1,
688 .init = byt_rt5651_init,
689 .ops = &byt_rt5651_be_ssp2_ops,
690 },
691};
692
693/* SoC card */
694static struct snd_soc_card byt_rt5651_card = {
695 .name = "bytcr-rt5651",
696 .owner = THIS_MODULE,
697 .dai_link = byt_rt5651_dais,
698 .num_links = ARRAY_SIZE(byt_rt5651_dais),
699 .dapm_widgets = byt_rt5651_widgets,
700 .num_dapm_widgets = ARRAY_SIZE(byt_rt5651_widgets),
701 .dapm_routes = byt_rt5651_audio_map,
702 .num_dapm_routes = ARRAY_SIZE(byt_rt5651_audio_map),
703 .fully_routed = true,
704};
705
706static char byt_rt5651_codec_name[SND_ACPI_I2C_ID_LEN];
707static char byt_rt5651_codec_aif_name[12]; /* = "rt5651-aif[1|2]" */
708static char byt_rt5651_cpu_dai_name[10]; /* = "ssp[0|2]-port" */
709
710static bool is_valleyview(void)
711{
712 static const struct x86_cpu_id cpu_ids[] = {
713 { X86_VENDOR_INTEL, 6, 55 }, /* Valleyview, Bay Trail */
714 {}
715 };
716
717 if (!x86_match_cpu(cpu_ids))
718 return false;
719 return true;
720}
721
722struct acpi_chan_package { /* ACPICA seems to require 64 bit integers */
723 u64 aif_value; /* 1: AIF1, 2: AIF2 */
724 u64 mclock_value; /* usually 25MHz (0x17d7940), ignored */
725};
726
727static int snd_byt_rt5651_mc_probe(struct platform_device *pdev)
728{
729 struct byt_rt5651_private *priv;
730 struct snd_soc_acpi_mach *mach;
731 const char *i2c_name = NULL;
732 bool is_bytcr = false;
733 int ret_val = 0;
734 int dai_index = 0;
735 int i;
736
737 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_ATOMIC);
738 if (!priv)
739 return -ENOMEM;
740
741 /* register the soc card */
742 byt_rt5651_card.dev = &pdev->dev;
743
744 mach = byt_rt5651_card.dev->platform_data;
745 snd_soc_card_set_drvdata(&byt_rt5651_card, priv);
746
747 /* fix index of codec dai */
748 for (i = 0; i < ARRAY_SIZE(byt_rt5651_dais); i++) {
749 if (!strcmp(byt_rt5651_dais[i].codec_name, "i2c-10EC5651:00")) {
750 dai_index = i;
751 break;
752 }
753 }
754
755 /* fixup codec name based on HID */
756 i2c_name = acpi_dev_get_first_match_name(mach->id, NULL, -1);
757 if (!i2c_name) {
758 dev_err(&pdev->dev, "Error cannot find '%s' dev\n", mach->id);
759 return -ENODEV;
760 }
761 snprintf(byt_rt5651_codec_name, sizeof(byt_rt5651_codec_name),
762 "%s%s", "i2c-", i2c_name);
763 byt_rt5651_dais[dai_index].codec_name = byt_rt5651_codec_name;
764
765 /*
766 * swap SSP0 if bytcr is detected
767 * (will be overridden if DMI quirk is detected)
768 */
769 if (is_valleyview()) {
770 struct sst_platform_info *p_info = mach->pdata;
771 const struct sst_res_info *res_info = p_info->res_info;
772
773 if (res_info->acpi_ipc_irq_index == 0)
774 is_bytcr = true;
775 }
776
777 if (is_bytcr) {
778 /*
779 * Baytrail CR platforms may have CHAN package in BIOS, try
780 * to find relevant routing quirk based as done on Windows
781 * platforms. We have to read the information directly from the
782 * BIOS, at this stage the card is not created and the links
783 * with the codec driver/pdata are non-existent
784 */
785
786 struct acpi_chan_package chan_package;
787
788 /* format specified: 2 64-bit integers */
789 struct acpi_buffer format = {sizeof("NN"), "NN"};
790 struct acpi_buffer state = {0, NULL};
791 struct snd_soc_acpi_package_context pkg_ctx;
792 bool pkg_found = false;
793
794 state.length = sizeof(chan_package);
795 state.pointer = &chan_package;
796
797 pkg_ctx.name = "CHAN";
798 pkg_ctx.length = 2;
799 pkg_ctx.format = &format;
800 pkg_ctx.state = &state;
801 pkg_ctx.data_valid = false;
802
803 pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
804 &pkg_ctx);
805 if (pkg_found) {
806 if (chan_package.aif_value == 1) {
807 dev_info(&pdev->dev, "BIOS Routing: AIF1 connected\n");
808 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF1;
809 } else if (chan_package.aif_value == 2) {
810 dev_info(&pdev->dev, "BIOS Routing: AIF2 connected\n");
811 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2;
812 } else {
813 dev_info(&pdev->dev, "BIOS Routing isn't valid, ignored\n");
814 pkg_found = false;
815 }
816 }
817
818 if (!pkg_found) {
819 /* no BIOS indications, assume SSP0-AIF2 connection */
820 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2;
821 }
822 }
823
824 /* check quirks before creating card */
825 dmi_check_system(byt_rt5651_quirk_table);
826
827 /* Must be called before register_card, also see declaration comment. */
828 ret_val = byt_rt5651_add_codec_device_props(byt_rt5651_codec_name);
829 if (ret_val)
830 return ret_val;
831
832 log_quirks(&pdev->dev);
833
834 if ((byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) ||
835 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
836 /* fixup codec aif name */
837 snprintf(byt_rt5651_codec_aif_name,
838 sizeof(byt_rt5651_codec_aif_name),
839 "%s", "rt5651-aif2");
840
841 byt_rt5651_dais[dai_index].codec_dai_name =
842 byt_rt5651_codec_aif_name;
843 }
844
845 if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) ||
846 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
847 /* fixup cpu dai name name */
848 snprintf(byt_rt5651_cpu_dai_name,
849 sizeof(byt_rt5651_cpu_dai_name),
850 "%s", "ssp0-port");
851
852 byt_rt5651_dais[dai_index].cpu_dai_name =
853 byt_rt5651_cpu_dai_name;
854 }
855
856 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
857 priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
858 if (IS_ERR(priv->mclk)) {
859 dev_err(&pdev->dev,
860 "Failed to get MCLK from pmc_plt_clk_3: %ld\n",
861 PTR_ERR(priv->mclk));
862 /*
863 * Fall back to bit clock usage for -ENOENT (clock not
864 * available likely due to missing dependencies), bail
865 * for all other errors, including -EPROBE_DEFER
866 */
867 if (ret_val != -ENOENT)
868 return ret_val;
869 byt_rt5651_quirk &= ~BYT_RT5651_MCLK_EN;
870 }
871 }
872
873 ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5651_card);
874
875 if (ret_val) {
876 dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
877 ret_val);
878 return ret_val;
879 }
880 platform_set_drvdata(pdev, &byt_rt5651_card);
881 return ret_val;
882}
883
884static struct platform_driver snd_byt_rt5651_mc_driver = {
885 .driver = {
886 .name = "bytcr_rt5651",
887 },
888 .probe = snd_byt_rt5651_mc_probe,
889};
890
891module_platform_driver(snd_byt_rt5651_mc_driver);
892
893MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver for RT5651");
894MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
895MODULE_LICENSE("GPL v2");
896MODULE_ALIAS("platform:bytcr_rt5651");