2 * bytcr_rt5651.c - ASoc Machine driver for Intel Byt CR platform
3 * (derived from bytcr_rt5640.c)
5 * Copyright (C) 2015 Intel Corp
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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/input.h>
30 #include <linux/gpio/consumer.h>
31 #include <linux/gpio/machine.h>
32 #include <linux/slab.h>
33 #include <asm/cpu_device_id.h>
34 #include <asm/intel-family.h>
35 #include <sound/pcm.h>
36 #include <sound/pcm_params.h>
37 #include <sound/soc.h>
38 #include <sound/jack.h>
39 #include <sound/soc-acpi.h>
40 #include "../../codecs/rt5651.h"
41 #include "../atom/sst-atom-controls.h"
47 BYT_RT5651_IN1_IN2_MAP,
51 BYT_RT5651_JD_NULL = (RT5651_JD_NULL << 4),
52 BYT_RT5651_JD1_1 = (RT5651_JD1_1 << 4),
53 BYT_RT5651_JD1_2 = (RT5651_JD1_2 << 4),
54 BYT_RT5651_JD2 = (RT5651_JD2 << 4),
58 BYT_RT5651_OVCD_TH_600UA = (6 << 8),
59 BYT_RT5651_OVCD_TH_1500UA = (15 << 8),
60 BYT_RT5651_OVCD_TH_2000UA = (20 << 8),
64 BYT_RT5651_OVCD_SF_0P5 = (RT5651_OVCD_SF_0P5 << 13),
65 BYT_RT5651_OVCD_SF_0P75 = (RT5651_OVCD_SF_0P75 << 13),
66 BYT_RT5651_OVCD_SF_1P0 = (RT5651_OVCD_SF_1P0 << 13),
67 BYT_RT5651_OVCD_SF_1P5 = (RT5651_OVCD_SF_1P5 << 13),
70 #define BYT_RT5651_MAP(quirk) ((quirk) & GENMASK(3, 0))
71 #define BYT_RT5651_JDSRC(quirk) (((quirk) & GENMASK(7, 4)) >> 4)
72 #define BYT_RT5651_OVCD_TH(quirk) (((quirk) & GENMASK(12, 8)) >> 8)
73 #define BYT_RT5651_OVCD_SF(quirk) (((quirk) & GENMASK(14, 13)) >> 13)
74 #define BYT_RT5651_DMIC_EN BIT(16)
75 #define BYT_RT5651_MCLK_EN BIT(17)
76 #define BYT_RT5651_MCLK_25MHZ BIT(18)
77 #define BYT_RT5651_SSP2_AIF2 BIT(19) /* default is using AIF1 */
78 #define BYT_RT5651_SSP0_AIF1 BIT(20)
79 #define BYT_RT5651_SSP0_AIF2 BIT(21)
80 #define BYT_RT5651_HP_LR_SWAPPED BIT(22)
81 #define BYT_RT5651_MONO_SPEAKER BIT(23)
83 #define BYT_RT5651_DEFAULT_QUIRKS (BYT_RT5651_MCLK_EN | \
85 BYT_RT5651_OVCD_TH_2000UA | \
86 BYT_RT5651_OVCD_SF_0P75)
88 /* jack-detect-source + dmic-en + ovcd-th + -sf + terminating empty entry */
89 #define MAX_NO_PROPS 5
91 struct byt_rt5651_private {
93 struct gpio_desc *ext_amp_gpio;
94 struct snd_soc_jack jack;
97 /* Default: jack-detect on JD1_1, internal mic on in2, headsetmic on in3 */
98 static unsigned long byt_rt5651_quirk = BYT_RT5651_DEFAULT_QUIRKS |
101 static void log_quirks(struct device *dev)
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));
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");
135 #define BYT_CODEC_DAI1 "rt5651-aif1"
136 #define BYT_CODEC_DAI2 "rt5651-aif2"
138 static int byt_rt5651_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai,
139 int rate, int bclk_ratio)
141 int clk_id, clk_freq, ret;
143 /* Configure the PLL before selecting it */
144 if (!(byt_rt5651_quirk & BYT_RT5651_MCLK_EN)) {
145 clk_id = RT5651_PLL1_S_BCLK1,
146 clk_freq = rate * bclk_ratio;
148 clk_id = RT5651_PLL1_S_MCLK;
149 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
154 ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, rate * 512);
156 dev_err(codec_dai->component->dev, "can't set pll: %d\n", ret);
160 ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1,
161 rate * 512, SND_SOC_CLOCK_IN);
163 dev_err(codec_dai->component->dev, "can't set clock %d\n", ret);
170 static int platform_clock_control(struct snd_soc_dapm_widget *w,
171 struct snd_kcontrol *k, int event)
173 struct snd_soc_dapm_context *dapm = w->dapm;
174 struct snd_soc_card *card = dapm->card;
175 struct snd_soc_dai *codec_dai;
176 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
179 codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1);
181 codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI2);
184 "Codec dai not found; Unable to set platform clock\n");
188 if (SND_SOC_DAPM_EVENT_ON(event)) {
189 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
190 ret = clk_prepare_enable(priv->mclk);
193 "could not configure MCLK state");
197 ret = byt_rt5651_prepare_and_enable_pll1(codec_dai, 48000, 50);
200 * Set codec clock source to internal clock before
201 * turning off the platform clock. Codec needs clock
202 * for Jack detection and button press
204 ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_RCCLK,
208 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
209 clk_disable_unprepare(priv->mclk);
213 dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
220 static int rt5651_ext_amp_power_event(struct snd_soc_dapm_widget *w,
221 struct snd_kcontrol *kcontrol, int event)
223 struct snd_soc_card *card = w->dapm->card;
224 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
226 if (SND_SOC_DAPM_EVENT_ON(event))
227 gpiod_set_value_cansleep(priv->ext_amp_gpio, 1);
229 gpiod_set_value_cansleep(priv->ext_amp_gpio, 0);
234 static const struct snd_soc_dapm_widget byt_rt5651_widgets[] = {
235 SND_SOC_DAPM_HP("Headphone", NULL),
236 SND_SOC_DAPM_MIC("Headset Mic", NULL),
237 SND_SOC_DAPM_MIC("Internal Mic", NULL),
238 SND_SOC_DAPM_SPK("Speaker", NULL),
239 SND_SOC_DAPM_LINE("Line In", NULL),
240 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
241 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
242 SND_SOC_DAPM_POST_PMD),
243 SND_SOC_DAPM_SUPPLY("Ext Amp Power", SND_SOC_NOPM, 0, 0,
244 rt5651_ext_amp_power_event,
245 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
248 static const struct snd_soc_dapm_route byt_rt5651_audio_map[] = {
249 {"Headphone", NULL, "Platform Clock"},
250 {"Headset Mic", NULL, "Platform Clock"},
251 {"Internal Mic", NULL, "Platform Clock"},
252 {"Speaker", NULL, "Platform Clock"},
253 {"Speaker", NULL, "Ext Amp Power"},
254 {"Line In", NULL, "Platform Clock"},
256 {"Headset Mic", NULL, "micbias1"}, /* lowercase for rt5651 */
257 {"Headphone", NULL, "HPOL"},
258 {"Headphone", NULL, "HPOR"},
259 {"Speaker", NULL, "LOUTL"},
260 {"Speaker", NULL, "LOUTR"},
261 {"IN2P", NULL, "Line In"},
262 {"IN2N", NULL, "Line In"},
266 static const struct snd_soc_dapm_route byt_rt5651_intmic_dmic_map[] = {
267 {"DMIC L1", NULL, "Internal Mic"},
268 {"DMIC R1", NULL, "Internal Mic"},
269 {"IN3P", NULL, "Headset Mic"},
272 static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_map[] = {
273 {"Internal Mic", NULL, "micbias1"},
274 {"IN1P", NULL, "Internal Mic"},
275 {"IN3P", NULL, "Headset Mic"},
278 static const struct snd_soc_dapm_route byt_rt5651_intmic_in2_map[] = {
279 {"Internal Mic", NULL, "micbias1"},
280 {"IN2P", NULL, "Internal Mic"},
281 {"IN3P", NULL, "Headset Mic"},
284 static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_in2_map[] = {
285 {"Internal Mic", NULL, "micbias1"},
286 {"IN1P", NULL, "Internal Mic"},
287 {"IN2P", NULL, "Internal Mic"},
288 {"IN3P", NULL, "Headset Mic"},
291 static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif1_map[] = {
292 {"ssp0 Tx", NULL, "modem_out"},
293 {"modem_in", NULL, "ssp0 Rx"},
295 {"AIF1 Playback", NULL, "ssp0 Tx"},
296 {"ssp0 Rx", NULL, "AIF1 Capture"},
299 static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif2_map[] = {
300 {"ssp0 Tx", NULL, "modem_out"},
301 {"modem_in", NULL, "ssp0 Rx"},
303 {"AIF2 Playback", NULL, "ssp0 Tx"},
304 {"ssp0 Rx", NULL, "AIF2 Capture"},
307 static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif1_map[] = {
308 {"ssp2 Tx", NULL, "codec_out0"},
309 {"ssp2 Tx", NULL, "codec_out1"},
310 {"codec_in0", NULL, "ssp2 Rx"},
311 {"codec_in1", NULL, "ssp2 Rx"},
313 {"AIF1 Playback", NULL, "ssp2 Tx"},
314 {"ssp2 Rx", NULL, "AIF1 Capture"},
317 static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif2_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"},
323 {"AIF2 Playback", NULL, "ssp2 Tx"},
324 {"ssp2 Rx", NULL, "AIF2 Capture"},
327 static const struct snd_kcontrol_new byt_rt5651_controls[] = {
328 SOC_DAPM_PIN_SWITCH("Headphone"),
329 SOC_DAPM_PIN_SWITCH("Headset Mic"),
330 SOC_DAPM_PIN_SWITCH("Internal Mic"),
331 SOC_DAPM_PIN_SWITCH("Speaker"),
332 SOC_DAPM_PIN_SWITCH("Line In"),
335 static struct snd_soc_jack_pin bytcr_jack_pins[] = {
338 .mask = SND_JACK_HEADPHONE,
341 .pin = "Headset Mic",
342 .mask = SND_JACK_MICROPHONE,
346 static int byt_rt5651_aif1_hw_params(struct snd_pcm_substream *substream,
347 struct snd_pcm_hw_params *params)
349 struct snd_soc_pcm_runtime *rtd = substream->private_data;
350 struct snd_soc_dai *codec_dai = rtd->codec_dai;
351 snd_pcm_format_t format = params_format(params);
352 int rate = params_rate(params);
355 if (format == SNDRV_PCM_FORMAT_S16_LE)
360 return byt_rt5651_prepare_and_enable_pll1(codec_dai, rate, bclk_ratio);
363 static int byt_rt5651_quirk_cb(const struct dmi_system_id *id)
365 byt_rt5651_quirk = (unsigned long)id->driver_data;
369 static const struct dmi_system_id byt_rt5651_quirk_table[] = {
371 /* Chuwi Hi8 Pro (CWI513) */
372 .callback = byt_rt5651_quirk_cb,
374 DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"),
375 DMI_MATCH(DMI_PRODUCT_NAME, "X1D3_C806N"),
377 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
379 BYT_RT5651_HP_LR_SWAPPED |
380 BYT_RT5651_MONO_SPEAKER),
383 /* Chuwi Vi8 Plus (CWI519) */
384 .callback = byt_rt5651_quirk_cb,
386 DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"),
387 DMI_MATCH(DMI_PRODUCT_NAME, "D2D3_Vi8A1"),
389 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
391 BYT_RT5651_HP_LR_SWAPPED |
392 BYT_RT5651_MONO_SPEAKER),
395 /* I.T.Works TW701, Ployer Momo7w and Trekstor ST70416-6
396 * (these all use the same mainboard) */
397 .callback = byt_rt5651_quirk_cb,
399 DMI_MATCH(DMI_BIOS_VENDOR, "INSYDE Corp."),
400 /* Partial match for all of itWORKS.G.WI71C.JGBMRBA,
401 * TREK.G.WI71C.JGBMRBA0x and MOMO.G.WI71C.MABMRBA02 */
402 DMI_MATCH(DMI_BIOS_VERSION, ".G.WI71C."),
404 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
406 BYT_RT5651_SSP0_AIF1 |
407 BYT_RT5651_MONO_SPEAKER),
410 /* KIANO SlimNote 14.2 */
411 .callback = byt_rt5651_quirk_cb,
413 DMI_MATCH(DMI_SYS_VENDOR, "KIANO"),
414 DMI_MATCH(DMI_PRODUCT_NAME, "KIANO SlimNote 14.2"),
416 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
417 BYT_RT5651_IN1_IN2_MAP),
420 /* Minnowboard Max B3 */
421 .callback = byt_rt5651_quirk_cb,
423 DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
424 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
426 .driver_data = (void *)(BYT_RT5651_IN1_MAP),
429 /* Minnowboard Turbot */
430 .callback = byt_rt5651_quirk_cb,
432 DMI_MATCH(DMI_SYS_VENDOR, "ADI"),
433 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Turbot"),
435 .driver_data = (void *)(BYT_RT5651_MCLK_EN |
440 .callback = byt_rt5651_quirk_cb,
442 DMI_MATCH(DMI_SYS_VENDOR, "VIOS"),
443 DMI_MATCH(DMI_PRODUCT_NAME, "LTH17"),
445 .driver_data = (void *)(BYT_RT5651_IN1_IN2_MAP |
447 BYT_RT5651_OVCD_TH_2000UA |
448 BYT_RT5651_OVCD_SF_1P0 |
452 /* Yours Y8W81 (and others using the same mainboard) */
453 .callback = byt_rt5651_quirk_cb,
455 DMI_MATCH(DMI_BIOS_VENDOR, "INSYDE Corp."),
456 /* Partial match for all devs with a W86C mainboard */
457 DMI_MATCH(DMI_BIOS_VERSION, ".F.W86C."),
459 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
461 BYT_RT5651_SSP0_AIF1 |
462 BYT_RT5651_MONO_SPEAKER),
468 * Note this MUST be called before snd_soc_register_card(), so that the props
469 * are in place before the codec component driver's probe function parses them.
471 static int byt_rt5651_add_codec_device_props(struct device *i2c_dev)
473 struct property_entry props[MAX_NO_PROPS] = {};
476 props[cnt++] = PROPERTY_ENTRY_U32("realtek,jack-detect-source",
477 BYT_RT5651_JDSRC(byt_rt5651_quirk));
479 props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-threshold-microamp",
480 BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100);
482 props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-scale-factor",
483 BYT_RT5651_OVCD_SF(byt_rt5651_quirk));
485 if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN)
486 props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,dmic-en");
488 return device_add_properties(i2c_dev, props);
491 static int byt_rt5651_init(struct snd_soc_pcm_runtime *runtime)
493 struct snd_soc_card *card = runtime->card;
494 struct snd_soc_component *codec = runtime->codec_dai->component;
495 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
496 const struct snd_soc_dapm_route *custom_map;
500 card->dapm.idle_bias_off = true;
502 /* Start with RC clk for jack-detect (we disable MCLK below) */
503 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
504 snd_soc_component_update_bits(codec, RT5651_GLB_CLK,
505 RT5651_SCLK_SRC_MASK, RT5651_SCLK_SRC_RCCLK);
507 switch (BYT_RT5651_MAP(byt_rt5651_quirk)) {
508 case BYT_RT5651_IN1_MAP:
509 custom_map = byt_rt5651_intmic_in1_map;
510 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_map);
512 case BYT_RT5651_IN2_MAP:
513 custom_map = byt_rt5651_intmic_in2_map;
514 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in2_map);
516 case BYT_RT5651_IN1_IN2_MAP:
517 custom_map = byt_rt5651_intmic_in1_in2_map;
518 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_in2_map);
521 custom_map = byt_rt5651_intmic_dmic_map;
522 num_routes = ARRAY_SIZE(byt_rt5651_intmic_dmic_map);
524 ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
528 if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) {
529 ret = snd_soc_dapm_add_routes(&card->dapm,
530 byt_rt5651_ssp2_aif2_map,
531 ARRAY_SIZE(byt_rt5651_ssp2_aif2_map));
532 } else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) {
533 ret = snd_soc_dapm_add_routes(&card->dapm,
534 byt_rt5651_ssp0_aif1_map,
535 ARRAY_SIZE(byt_rt5651_ssp0_aif1_map));
536 } else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2) {
537 ret = snd_soc_dapm_add_routes(&card->dapm,
538 byt_rt5651_ssp0_aif2_map,
539 ARRAY_SIZE(byt_rt5651_ssp0_aif2_map));
541 ret = snd_soc_dapm_add_routes(&card->dapm,
542 byt_rt5651_ssp2_aif1_map,
543 ARRAY_SIZE(byt_rt5651_ssp2_aif1_map));
548 ret = snd_soc_add_card_controls(card, byt_rt5651_controls,
549 ARRAY_SIZE(byt_rt5651_controls));
551 dev_err(card->dev, "unable to add card controls\n");
554 snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
555 snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
557 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
559 * The firmware might enable the clock at
560 * boot (this information may or may not
561 * be reflected in the enable clock register).
562 * To change the rate we must disable the clock
563 * first to cover these cases. Due to common
564 * clock framework restrictions that do not allow
565 * to disable a clock that has not been enabled,
566 * we need to enable the clock first.
568 ret = clk_prepare_enable(priv->mclk);
570 clk_disable_unprepare(priv->mclk);
572 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
573 ret = clk_set_rate(priv->mclk, 25000000);
575 ret = clk_set_rate(priv->mclk, 19200000);
578 dev_err(card->dev, "unable to set MCLK rate\n");
581 if (BYT_RT5651_JDSRC(byt_rt5651_quirk)) {
582 ret = snd_soc_card_jack_new(runtime->card, "Headset",
583 SND_JACK_HEADSET | SND_JACK_BTN_0,
584 &priv->jack, bytcr_jack_pins,
585 ARRAY_SIZE(bytcr_jack_pins));
587 dev_err(runtime->dev, "jack creation failed %d\n", ret);
591 snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0,
594 ret = snd_soc_component_set_jack(codec, &priv->jack, NULL);
602 static const struct snd_soc_pcm_stream byt_rt5651_dai_params = {
603 .formats = SNDRV_PCM_FMTBIT_S24_LE,
610 static int byt_rt5651_codec_fixup(struct snd_soc_pcm_runtime *rtd,
611 struct snd_pcm_hw_params *params)
613 struct snd_interval *rate = hw_param_interval(params,
614 SNDRV_PCM_HW_PARAM_RATE);
615 struct snd_interval *channels = hw_param_interval(params,
616 SNDRV_PCM_HW_PARAM_CHANNELS);
619 /* The DSP will covert the FE rate to 48k, stereo */
620 rate->min = rate->max = 48000;
621 channels->min = channels->max = 2;
623 if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) ||
624 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
625 /* set SSP0 to 16-bit */
626 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
629 /* set SSP2 to 24-bit */
630 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
635 * Default mode for SSP configuration is TDM 4 slot, override config
636 * with explicit setting to I2S 2ch. The word length is set with
637 * dai_set_tdm_slot() since there is no other API exposed
639 ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
641 SND_SOC_DAIFMT_NB_NF |
642 SND_SOC_DAIFMT_CBS_CFS
646 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
650 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, bits);
652 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
659 static const unsigned int rates_48000[] = {
663 static const struct snd_pcm_hw_constraint_list constraints_48000 = {
664 .count = ARRAY_SIZE(rates_48000),
668 static int byt_rt5651_aif1_startup(struct snd_pcm_substream *substream)
670 return snd_pcm_hw_constraint_list(substream->runtime, 0,
671 SNDRV_PCM_HW_PARAM_RATE,
675 static const struct snd_soc_ops byt_rt5651_aif1_ops = {
676 .startup = byt_rt5651_aif1_startup,
679 static const struct snd_soc_ops byt_rt5651_be_ssp2_ops = {
680 .hw_params = byt_rt5651_aif1_hw_params,
683 static struct snd_soc_dai_link byt_rt5651_dais[] = {
684 [MERR_DPCM_AUDIO] = {
685 .name = "Audio Port",
686 .stream_name = "Audio",
687 .cpu_dai_name = "media-cpu-dai",
688 .codec_dai_name = "snd-soc-dummy-dai",
689 .codec_name = "snd-soc-dummy",
690 .platform_name = "sst-mfld-platform",
695 .ops = &byt_rt5651_aif1_ops,
697 [MERR_DPCM_DEEP_BUFFER] = {
698 .name = "Deep-Buffer Audio Port",
699 .stream_name = "Deep-Buffer Audio",
700 .cpu_dai_name = "deepbuffer-cpu-dai",
701 .codec_dai_name = "snd-soc-dummy-dai",
702 .codec_name = "snd-soc-dummy",
703 .platform_name = "sst-mfld-platform",
707 .ops = &byt_rt5651_aif1_ops,
709 /* CODEC<->CODEC link */
712 .name = "SSP2-Codec",
714 .cpu_dai_name = "ssp2-port",
715 .platform_name = "sst-mfld-platform",
717 .codec_dai_name = "rt5651-aif1",
718 .codec_name = "i2c-10EC5651:00",
719 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
720 | SND_SOC_DAIFMT_CBS_CFS,
721 .be_hw_params_fixup = byt_rt5651_codec_fixup,
726 .init = byt_rt5651_init,
727 .ops = &byt_rt5651_be_ssp2_ops,
732 static char byt_rt5651_codec_name[SND_ACPI_I2C_ID_LEN];
733 static char byt_rt5651_codec_aif_name[12]; /* = "rt5651-aif[1|2]" */
734 static char byt_rt5651_cpu_dai_name[10]; /* = "ssp[0|2]-port" */
735 static char byt_rt5651_long_name[50]; /* = "bytcr-rt5651-*-spk-*-mic[-swapped-hp]" */
737 static int byt_rt5651_suspend(struct snd_soc_card *card)
739 struct snd_soc_component *component;
741 if (!BYT_RT5651_JDSRC(byt_rt5651_quirk))
744 for_each_card_components(card, component) {
745 if (!strcmp(component->name, byt_rt5651_codec_name)) {
746 dev_dbg(component->dev, "disabling jack detect before suspend\n");
747 snd_soc_component_set_jack(component, NULL, NULL);
755 static int byt_rt5651_resume(struct snd_soc_card *card)
757 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
758 struct snd_soc_component *component;
760 if (!BYT_RT5651_JDSRC(byt_rt5651_quirk))
763 for_each_card_components(card, component) {
764 if (!strcmp(component->name, byt_rt5651_codec_name)) {
765 dev_dbg(component->dev, "re-enabling jack detect after resume\n");
766 snd_soc_component_set_jack(component, &priv->jack, NULL);
774 static struct snd_soc_card byt_rt5651_card = {
775 .name = "bytcr-rt5651",
776 .owner = THIS_MODULE,
777 .dai_link = byt_rt5651_dais,
778 .num_links = ARRAY_SIZE(byt_rt5651_dais),
779 .dapm_widgets = byt_rt5651_widgets,
780 .num_dapm_widgets = ARRAY_SIZE(byt_rt5651_widgets),
781 .dapm_routes = byt_rt5651_audio_map,
782 .num_dapm_routes = ARRAY_SIZE(byt_rt5651_audio_map),
783 .fully_routed = true,
784 .suspend_pre = byt_rt5651_suspend,
785 .resume_post = byt_rt5651_resume,
788 static const struct x86_cpu_id baytrail_cpu_ids[] = {
789 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT }, /* Valleyview */
793 static const struct x86_cpu_id cherrytrail_cpu_ids[] = {
794 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT }, /* Braswell */
798 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
799 static const struct acpi_gpio_params second_gpio = { 1, 0, false };
801 static const struct acpi_gpio_mapping byt_rt5651_amp_en_first[] = {
802 { "ext-amp-enable-gpios", &first_gpio, 1 },
806 static const struct acpi_gpio_mapping byt_rt5651_amp_en_second[] = {
807 { "ext-amp-enable-gpios", &second_gpio, 1 },
812 * Some boards have I2cSerialBusV2, GpioIo, GpioInt as ACPI resources, other
813 * boards may have I2cSerialBusV2, GpioInt, GpioIo instead. We want the
814 * GpioIo one for the ext-amp-enable-gpio and both count for the index in
815 * acpi_gpio_params index. So we have 2 different mappings and the code
816 * below figures out which one to use.
818 struct byt_rt5651_acpi_resource_data {
823 static int snd_byt_rt5651_acpi_resource(struct acpi_resource *ares, void *arg)
825 struct byt_rt5651_acpi_resource_data *data = arg;
827 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
830 if (ares->data.gpio.connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
831 data->gpio_int_idx = data->gpio_count;
837 static void snd_byt_rt5651_mc_add_amp_en_gpio_mapping(struct device *codec)
839 struct byt_rt5651_acpi_resource_data data = { 0, -1 };
840 LIST_HEAD(resources);
843 ret = acpi_dev_get_resources(ACPI_COMPANION(codec), &resources,
844 snd_byt_rt5651_acpi_resource, &data);
846 dev_warn(codec, "Failed to get ACPI resources, not adding external amplifier GPIO mapping\n");
850 /* All info we need is gathered during the walk */
851 acpi_dev_free_resource_list(&resources);
853 switch (data.gpio_int_idx) {
855 devm_acpi_dev_add_driver_gpios(codec, byt_rt5651_amp_en_second);
858 devm_acpi_dev_add_driver_gpios(codec, byt_rt5651_amp_en_first);
861 dev_warn(codec, "Unknown GpioInt index %d, not adding external amplifier GPIO mapping\n",
866 struct acpi_chan_package { /* ACPICA seems to require 64 bit integers */
867 u64 aif_value; /* 1: AIF1, 2: AIF2 */
868 u64 mclock_value; /* usually 25MHz (0x17d7940), ignored */
871 static int snd_byt_rt5651_mc_probe(struct platform_device *pdev)
873 const char * const mic_name[] = { "dmic", "in1", "in2", "in12" };
874 struct byt_rt5651_private *priv;
875 struct snd_soc_acpi_mach *mach;
876 struct device *codec_dev;
877 const char *i2c_name = NULL;
878 const char *hp_swapped;
879 bool is_bytcr = false;
884 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
888 /* register the soc card */
889 byt_rt5651_card.dev = &pdev->dev;
891 mach = byt_rt5651_card.dev->platform_data;
892 snd_soc_card_set_drvdata(&byt_rt5651_card, priv);
894 /* fix index of codec dai */
895 for (i = 0; i < ARRAY_SIZE(byt_rt5651_dais); i++) {
896 if (!strcmp(byt_rt5651_dais[i].codec_name, "i2c-10EC5651:00")) {
902 /* fixup codec name based on HID */
903 i2c_name = acpi_dev_get_first_match_name(mach->id, NULL, -1);
905 dev_err(&pdev->dev, "Error cannot find '%s' dev\n", mach->id);
908 snprintf(byt_rt5651_codec_name, sizeof(byt_rt5651_codec_name),
909 "%s%s", "i2c-", i2c_name);
910 byt_rt5651_dais[dai_index].codec_name = byt_rt5651_codec_name;
912 codec_dev = bus_find_device_by_name(&i2c_bus_type, NULL,
913 byt_rt5651_codec_name);
915 return -EPROBE_DEFER;
918 * swap SSP0 if bytcr is detected
919 * (will be overridden if DMI quirk is detected)
921 if (x86_match_cpu(baytrail_cpu_ids)) {
922 if (mach->mach_params.acpi_ipc_irq_index == 0)
928 * Baytrail CR platforms may have CHAN package in BIOS, try
929 * to find relevant routing quirk based as done on Windows
930 * platforms. We have to read the information directly from the
931 * BIOS, at this stage the card is not created and the links
932 * with the codec driver/pdata are non-existent
935 struct acpi_chan_package chan_package;
937 /* format specified: 2 64-bit integers */
938 struct acpi_buffer format = {sizeof("NN"), "NN"};
939 struct acpi_buffer state = {0, NULL};
940 struct snd_soc_acpi_package_context pkg_ctx;
941 bool pkg_found = false;
943 state.length = sizeof(chan_package);
944 state.pointer = &chan_package;
946 pkg_ctx.name = "CHAN";
948 pkg_ctx.format = &format;
949 pkg_ctx.state = &state;
950 pkg_ctx.data_valid = false;
952 pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
955 if (chan_package.aif_value == 1) {
956 dev_info(&pdev->dev, "BIOS Routing: AIF1 connected\n");
957 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF1;
958 } else if (chan_package.aif_value == 2) {
959 dev_info(&pdev->dev, "BIOS Routing: AIF2 connected\n");
960 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2;
962 dev_info(&pdev->dev, "BIOS Routing isn't valid, ignored\n");
968 /* no BIOS indications, assume SSP0-AIF2 connection */
969 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2;
973 /* check quirks before creating card */
974 dmi_check_system(byt_rt5651_quirk_table);
976 /* Must be called before register_card, also see declaration comment. */
977 ret_val = byt_rt5651_add_codec_device_props(codec_dev);
979 put_device(codec_dev);
983 /* Cherry Trail devices use an external amplifier enable gpio */
984 if (x86_match_cpu(cherrytrail_cpu_ids)) {
985 snd_byt_rt5651_mc_add_amp_en_gpio_mapping(codec_dev);
986 priv->ext_amp_gpio = devm_fwnode_get_index_gpiod_from_child(
987 &pdev->dev, "ext-amp-enable", 0,
989 GPIOD_OUT_LOW, "speaker-amp");
990 if (IS_ERR(priv->ext_amp_gpio)) {
991 ret_val = PTR_ERR(priv->ext_amp_gpio);
994 priv->ext_amp_gpio = NULL;
997 dev_err(&pdev->dev, "Failed to get ext-amp-enable GPIO: %d\n",
1001 put_device(codec_dev);
1007 put_device(codec_dev);
1009 log_quirks(&pdev->dev);
1011 if ((byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) ||
1012 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
1013 /* fixup codec aif name */
1014 snprintf(byt_rt5651_codec_aif_name,
1015 sizeof(byt_rt5651_codec_aif_name),
1016 "%s", "rt5651-aif2");
1018 byt_rt5651_dais[dai_index].codec_dai_name =
1019 byt_rt5651_codec_aif_name;
1022 if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) ||
1023 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
1024 /* fixup cpu dai name name */
1025 snprintf(byt_rt5651_cpu_dai_name,
1026 sizeof(byt_rt5651_cpu_dai_name),
1029 byt_rt5651_dais[dai_index].cpu_dai_name =
1030 byt_rt5651_cpu_dai_name;
1033 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
1034 priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
1035 if (IS_ERR(priv->mclk)) {
1036 ret_val = PTR_ERR(priv->mclk);
1038 "Failed to get MCLK from pmc_plt_clk_3: %d\n",
1041 * Fall back to bit clock usage for -ENOENT (clock not
1042 * available likely due to missing dependencies), bail
1043 * for all other errors, including -EPROBE_DEFER
1045 if (ret_val != -ENOENT)
1047 byt_rt5651_quirk &= ~BYT_RT5651_MCLK_EN;
1051 if (byt_rt5651_quirk & BYT_RT5651_HP_LR_SWAPPED)
1052 hp_swapped = "-hp-swapped";
1056 snprintf(byt_rt5651_long_name, sizeof(byt_rt5651_long_name),
1057 "bytcr-rt5651-%s-spk-%s-mic%s",
1058 (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER) ?
1060 mic_name[BYT_RT5651_MAP(byt_rt5651_quirk)], hp_swapped);
1061 byt_rt5651_card.long_name = byt_rt5651_long_name;
1063 ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5651_card);
1066 dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
1070 platform_set_drvdata(pdev, &byt_rt5651_card);
1074 static struct platform_driver snd_byt_rt5651_mc_driver = {
1076 .name = "bytcr_rt5651",
1078 .probe = snd_byt_rt5651_mc_probe,
1081 module_platform_driver(snd_byt_rt5651_mc_driver);
1083 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver for RT5651");
1084 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
1085 MODULE_LICENSE("GPL v2");
1086 MODULE_ALIAS("platform:bytcr_rt5651");