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 gpio_desc *hp_detect;
95 struct snd_soc_jack jack;
98 static const struct acpi_gpio_mapping *byt_rt5651_gpios;
100 /* Default: jack-detect on JD1_1, internal mic on in2, headsetmic on in3 */
101 static unsigned long byt_rt5651_quirk = BYT_RT5651_DEFAULT_QUIRKS |
104 static unsigned int quirk_override;
105 module_param_named(quirk, quirk_override, uint, 0444);
106 MODULE_PARM_DESC(quirk, "Board-specific quirk override");
108 static void log_quirks(struct device *dev)
110 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_DMIC_MAP)
111 dev_info(dev, "quirk DMIC_MAP enabled");
112 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_MAP)
113 dev_info(dev, "quirk IN1_MAP enabled");
114 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN2_MAP)
115 dev_info(dev, "quirk IN2_MAP enabled");
116 if (BYT_RT5651_MAP(byt_rt5651_quirk) == BYT_RT5651_IN1_IN2_MAP)
117 dev_info(dev, "quirk IN1_IN2_MAP enabled");
118 if (BYT_RT5651_JDSRC(byt_rt5651_quirk)) {
119 dev_info(dev, "quirk realtek,jack-detect-source %ld\n",
120 BYT_RT5651_JDSRC(byt_rt5651_quirk));
121 dev_info(dev, "quirk realtek,over-current-threshold-microamp %ld\n",
122 BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100);
123 dev_info(dev, "quirk realtek,over-current-scale-factor %ld\n",
124 BYT_RT5651_OVCD_SF(byt_rt5651_quirk));
126 if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN)
127 dev_info(dev, "quirk DMIC enabled");
128 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
129 dev_info(dev, "quirk MCLK_EN enabled");
130 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
131 dev_info(dev, "quirk MCLK_25MHZ enabled");
132 if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2)
133 dev_info(dev, "quirk SSP2_AIF2 enabled\n");
134 if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1)
135 dev_info(dev, "quirk SSP0_AIF1 enabled\n");
136 if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)
137 dev_info(dev, "quirk SSP0_AIF2 enabled\n");
138 if (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER)
139 dev_info(dev, "quirk MONO_SPEAKER enabled\n");
142 #define BYT_CODEC_DAI1 "rt5651-aif1"
143 #define BYT_CODEC_DAI2 "rt5651-aif2"
145 static int byt_rt5651_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai,
146 int rate, int bclk_ratio)
148 int clk_id, clk_freq, ret;
150 /* Configure the PLL before selecting it */
151 if (!(byt_rt5651_quirk & BYT_RT5651_MCLK_EN)) {
152 clk_id = RT5651_PLL1_S_BCLK1,
153 clk_freq = rate * bclk_ratio;
155 clk_id = RT5651_PLL1_S_MCLK;
156 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
161 ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, rate * 512);
163 dev_err(codec_dai->component->dev, "can't set pll: %d\n", ret);
167 ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_PLL1,
168 rate * 512, SND_SOC_CLOCK_IN);
170 dev_err(codec_dai->component->dev, "can't set clock %d\n", ret);
177 static int platform_clock_control(struct snd_soc_dapm_widget *w,
178 struct snd_kcontrol *k, int event)
180 struct snd_soc_dapm_context *dapm = w->dapm;
181 struct snd_soc_card *card = dapm->card;
182 struct snd_soc_dai *codec_dai;
183 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
186 codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI1);
188 codec_dai = snd_soc_card_get_codec_dai(card, BYT_CODEC_DAI2);
191 "Codec dai not found; Unable to set platform clock\n");
195 if (SND_SOC_DAPM_EVENT_ON(event)) {
196 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
197 ret = clk_prepare_enable(priv->mclk);
200 "could not configure MCLK state");
204 ret = byt_rt5651_prepare_and_enable_pll1(codec_dai, 48000, 50);
207 * Set codec clock source to internal clock before
208 * turning off the platform clock. Codec needs clock
209 * for Jack detection and button press
211 ret = snd_soc_dai_set_sysclk(codec_dai, RT5651_SCLK_S_RCCLK,
215 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
216 clk_disable_unprepare(priv->mclk);
220 dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
227 static int rt5651_ext_amp_power_event(struct snd_soc_dapm_widget *w,
228 struct snd_kcontrol *kcontrol, int event)
230 struct snd_soc_card *card = w->dapm->card;
231 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
233 if (SND_SOC_DAPM_EVENT_ON(event))
234 gpiod_set_value_cansleep(priv->ext_amp_gpio, 1);
236 gpiod_set_value_cansleep(priv->ext_amp_gpio, 0);
241 static const struct snd_soc_dapm_widget byt_rt5651_widgets[] = {
242 SND_SOC_DAPM_HP("Headphone", NULL),
243 SND_SOC_DAPM_MIC("Headset Mic", NULL),
244 SND_SOC_DAPM_MIC("Internal Mic", NULL),
245 SND_SOC_DAPM_SPK("Speaker", NULL),
246 SND_SOC_DAPM_LINE("Line In", NULL),
247 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
248 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
249 SND_SOC_DAPM_POST_PMD),
250 SND_SOC_DAPM_SUPPLY("Ext Amp Power", SND_SOC_NOPM, 0, 0,
251 rt5651_ext_amp_power_event,
252 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
255 static const struct snd_soc_dapm_route byt_rt5651_audio_map[] = {
256 {"Headphone", NULL, "Platform Clock"},
257 {"Headset Mic", NULL, "Platform Clock"},
258 {"Internal Mic", NULL, "Platform Clock"},
259 {"Speaker", NULL, "Platform Clock"},
260 {"Speaker", NULL, "Ext Amp Power"},
261 {"Line In", NULL, "Platform Clock"},
263 {"Headset Mic", NULL, "micbias1"}, /* lowercase for rt5651 */
264 {"Headphone", NULL, "HPOL"},
265 {"Headphone", NULL, "HPOR"},
266 {"Speaker", NULL, "LOUTL"},
267 {"Speaker", NULL, "LOUTR"},
268 {"IN2P", NULL, "Line In"},
269 {"IN2N", NULL, "Line In"},
273 static const struct snd_soc_dapm_route byt_rt5651_intmic_dmic_map[] = {
274 {"DMIC L1", NULL, "Internal Mic"},
275 {"DMIC R1", NULL, "Internal Mic"},
276 {"IN2P", NULL, "Headset Mic"},
279 static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_map[] = {
280 {"Internal Mic", NULL, "micbias1"},
281 {"IN1P", NULL, "Internal Mic"},
282 {"IN3P", NULL, "Headset Mic"},
285 static const struct snd_soc_dapm_route byt_rt5651_intmic_in2_map[] = {
286 {"Internal Mic", NULL, "micbias1"},
287 {"IN2P", NULL, "Internal Mic"},
288 {"IN3P", NULL, "Headset Mic"},
291 static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_in2_map[] = {
292 {"Internal Mic", NULL, "micbias1"},
293 {"IN1P", NULL, "Internal Mic"},
294 {"IN2P", NULL, "Internal Mic"},
295 {"IN3P", NULL, "Headset Mic"},
298 static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif1_map[] = {
299 {"ssp0 Tx", NULL, "modem_out"},
300 {"modem_in", NULL, "ssp0 Rx"},
302 {"AIF1 Playback", NULL, "ssp0 Tx"},
303 {"ssp0 Rx", NULL, "AIF1 Capture"},
306 static const struct snd_soc_dapm_route byt_rt5651_ssp0_aif2_map[] = {
307 {"ssp0 Tx", NULL, "modem_out"},
308 {"modem_in", NULL, "ssp0 Rx"},
310 {"AIF2 Playback", NULL, "ssp0 Tx"},
311 {"ssp0 Rx", NULL, "AIF2 Capture"},
314 static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif1_map[] = {
315 {"ssp2 Tx", NULL, "codec_out0"},
316 {"ssp2 Tx", NULL, "codec_out1"},
317 {"codec_in0", NULL, "ssp2 Rx"},
318 {"codec_in1", NULL, "ssp2 Rx"},
320 {"AIF1 Playback", NULL, "ssp2 Tx"},
321 {"ssp2 Rx", NULL, "AIF1 Capture"},
324 static const struct snd_soc_dapm_route byt_rt5651_ssp2_aif2_map[] = {
325 {"ssp2 Tx", NULL, "codec_out0"},
326 {"ssp2 Tx", NULL, "codec_out1"},
327 {"codec_in0", NULL, "ssp2 Rx"},
328 {"codec_in1", NULL, "ssp2 Rx"},
330 {"AIF2 Playback", NULL, "ssp2 Tx"},
331 {"ssp2 Rx", NULL, "AIF2 Capture"},
334 static const struct snd_kcontrol_new byt_rt5651_controls[] = {
335 SOC_DAPM_PIN_SWITCH("Headphone"),
336 SOC_DAPM_PIN_SWITCH("Headset Mic"),
337 SOC_DAPM_PIN_SWITCH("Internal Mic"),
338 SOC_DAPM_PIN_SWITCH("Speaker"),
339 SOC_DAPM_PIN_SWITCH("Line In"),
342 static struct snd_soc_jack_pin bytcr_jack_pins[] = {
345 .mask = SND_JACK_HEADPHONE,
348 .pin = "Headset Mic",
349 .mask = SND_JACK_MICROPHONE,
353 static int byt_rt5651_aif1_hw_params(struct snd_pcm_substream *substream,
354 struct snd_pcm_hw_params *params)
356 struct snd_soc_pcm_runtime *rtd = substream->private_data;
357 struct snd_soc_dai *codec_dai = rtd->codec_dai;
358 snd_pcm_format_t format = params_format(params);
359 int rate = params_rate(params);
362 if (format == SNDRV_PCM_FORMAT_S16_LE)
367 return byt_rt5651_prepare_and_enable_pll1(codec_dai, rate, bclk_ratio);
370 static const struct acpi_gpio_params pov_p1006w_hp_detect = { 1, 0, false };
371 static const struct acpi_gpio_params pov_p1006w_ext_amp_en = { 2, 0, true };
373 static const struct acpi_gpio_mapping byt_rt5651_pov_p1006w_gpios[] = {
374 { "hp-detect-gpios", &pov_p1006w_hp_detect, 1, },
375 { "ext-amp-enable-gpios", &pov_p1006w_ext_amp_en, 1, },
379 static int byt_rt5651_pov_p1006w_quirk_cb(const struct dmi_system_id *id)
381 byt_rt5651_quirk = (unsigned long)id->driver_data;
382 byt_rt5651_gpios = byt_rt5651_pov_p1006w_gpios;
386 static int byt_rt5651_quirk_cb(const struct dmi_system_id *id)
388 byt_rt5651_quirk = (unsigned long)id->driver_data;
392 static const struct dmi_system_id byt_rt5651_quirk_table[] = {
394 /* Chuwi Hi8 Pro (CWI513) */
395 .callback = byt_rt5651_quirk_cb,
397 DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"),
398 DMI_MATCH(DMI_PRODUCT_NAME, "X1D3_C806N"),
400 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
402 BYT_RT5651_HP_LR_SWAPPED |
403 BYT_RT5651_MONO_SPEAKER),
406 /* Chuwi Vi8 Plus (CWI519) */
407 .callback = byt_rt5651_quirk_cb,
409 DMI_MATCH(DMI_SYS_VENDOR, "Hampoo"),
410 DMI_MATCH(DMI_PRODUCT_NAME, "D2D3_Vi8A1"),
412 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
414 BYT_RT5651_HP_LR_SWAPPED |
415 BYT_RT5651_MONO_SPEAKER),
418 /* I.T.Works TW701, Ployer Momo7w and Trekstor ST70416-6
419 * (these all use the same mainboard) */
420 .callback = byt_rt5651_quirk_cb,
422 DMI_MATCH(DMI_BIOS_VENDOR, "INSYDE Corp."),
423 /* Partial match for all of itWORKS.G.WI71C.JGBMRBA,
424 * TREK.G.WI71C.JGBMRBA0x and MOMO.G.WI71C.MABMRBA02 */
425 DMI_MATCH(DMI_BIOS_VERSION, ".G.WI71C."),
427 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
429 BYT_RT5651_SSP0_AIF1 |
430 BYT_RT5651_MONO_SPEAKER),
433 /* KIANO SlimNote 14.2 */
434 .callback = byt_rt5651_quirk_cb,
436 DMI_MATCH(DMI_SYS_VENDOR, "KIANO"),
437 DMI_MATCH(DMI_PRODUCT_NAME, "KIANO SlimNote 14.2"),
439 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
440 BYT_RT5651_IN1_IN2_MAP),
443 /* Minnowboard Max B3 */
444 .callback = byt_rt5651_quirk_cb,
446 DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
447 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max B3 PLATFORM"),
449 .driver_data = (void *)(BYT_RT5651_IN1_MAP),
452 /* Minnowboard Turbot */
453 .callback = byt_rt5651_quirk_cb,
455 DMI_MATCH(DMI_SYS_VENDOR, "ADI"),
456 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Turbot"),
458 .driver_data = (void *)(BYT_RT5651_MCLK_EN |
462 /* Point of View mobii wintab p1006w (v1.0) */
463 .callback = byt_rt5651_pov_p1006w_quirk_cb,
465 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
466 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "BayTrail"),
467 /* Note 105b is Foxcon's USB/PCI vendor id */
468 DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "105B"),
469 DMI_EXACT_MATCH(DMI_BOARD_NAME, "0E57"),
471 .driver_data = (void *)(BYT_RT5651_DMIC_MAP |
472 BYT_RT5651_OVCD_TH_2000UA |
473 BYT_RT5651_OVCD_SF_0P75 |
476 BYT_RT5651_SSP0_AIF1),
480 .callback = byt_rt5651_quirk_cb,
482 DMI_MATCH(DMI_SYS_VENDOR, "VIOS"),
483 DMI_MATCH(DMI_PRODUCT_NAME, "LTH17"),
485 .driver_data = (void *)(BYT_RT5651_IN1_IN2_MAP |
487 BYT_RT5651_OVCD_TH_2000UA |
488 BYT_RT5651_OVCD_SF_1P0 |
492 /* Yours Y8W81 (and others using the same mainboard) */
493 .callback = byt_rt5651_quirk_cb,
495 DMI_MATCH(DMI_BIOS_VENDOR, "INSYDE Corp."),
496 /* Partial match for all devs with a W86C mainboard */
497 DMI_MATCH(DMI_BIOS_VERSION, ".F.W86C."),
499 .driver_data = (void *)(BYT_RT5651_DEFAULT_QUIRKS |
501 BYT_RT5651_SSP0_AIF1 |
502 BYT_RT5651_MONO_SPEAKER),
508 * Note this MUST be called before snd_soc_register_card(), so that the props
509 * are in place before the codec component driver's probe function parses them.
511 static int byt_rt5651_add_codec_device_props(struct device *i2c_dev)
513 struct property_entry props[MAX_NO_PROPS] = {};
516 props[cnt++] = PROPERTY_ENTRY_U32("realtek,jack-detect-source",
517 BYT_RT5651_JDSRC(byt_rt5651_quirk));
519 props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-threshold-microamp",
520 BYT_RT5651_OVCD_TH(byt_rt5651_quirk) * 100);
522 props[cnt++] = PROPERTY_ENTRY_U32("realtek,over-current-scale-factor",
523 BYT_RT5651_OVCD_SF(byt_rt5651_quirk));
525 if (byt_rt5651_quirk & BYT_RT5651_DMIC_EN)
526 props[cnt++] = PROPERTY_ENTRY_BOOL("realtek,dmic-en");
528 return device_add_properties(i2c_dev, props);
531 static int byt_rt5651_init(struct snd_soc_pcm_runtime *runtime)
533 struct snd_soc_card *card = runtime->card;
534 struct snd_soc_component *codec = runtime->codec_dai->component;
535 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
536 const struct snd_soc_dapm_route *custom_map;
541 card->dapm.idle_bias_off = true;
543 /* Start with RC clk for jack-detect (we disable MCLK below) */
544 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN)
545 snd_soc_component_update_bits(codec, RT5651_GLB_CLK,
546 RT5651_SCLK_SRC_MASK, RT5651_SCLK_SRC_RCCLK);
548 switch (BYT_RT5651_MAP(byt_rt5651_quirk)) {
549 case BYT_RT5651_IN1_MAP:
550 custom_map = byt_rt5651_intmic_in1_map;
551 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_map);
553 case BYT_RT5651_IN2_MAP:
554 custom_map = byt_rt5651_intmic_in2_map;
555 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in2_map);
557 case BYT_RT5651_IN1_IN2_MAP:
558 custom_map = byt_rt5651_intmic_in1_in2_map;
559 num_routes = ARRAY_SIZE(byt_rt5651_intmic_in1_in2_map);
562 custom_map = byt_rt5651_intmic_dmic_map;
563 num_routes = ARRAY_SIZE(byt_rt5651_intmic_dmic_map);
565 ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
569 if (byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) {
570 ret = snd_soc_dapm_add_routes(&card->dapm,
571 byt_rt5651_ssp2_aif2_map,
572 ARRAY_SIZE(byt_rt5651_ssp2_aif2_map));
573 } else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) {
574 ret = snd_soc_dapm_add_routes(&card->dapm,
575 byt_rt5651_ssp0_aif1_map,
576 ARRAY_SIZE(byt_rt5651_ssp0_aif1_map));
577 } else if (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2) {
578 ret = snd_soc_dapm_add_routes(&card->dapm,
579 byt_rt5651_ssp0_aif2_map,
580 ARRAY_SIZE(byt_rt5651_ssp0_aif2_map));
582 ret = snd_soc_dapm_add_routes(&card->dapm,
583 byt_rt5651_ssp2_aif1_map,
584 ARRAY_SIZE(byt_rt5651_ssp2_aif1_map));
589 ret = snd_soc_add_card_controls(card, byt_rt5651_controls,
590 ARRAY_SIZE(byt_rt5651_controls));
592 dev_err(card->dev, "unable to add card controls\n");
595 snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
596 snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
598 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
600 * The firmware might enable the clock at
601 * boot (this information may or may not
602 * be reflected in the enable clock register).
603 * To change the rate we must disable the clock
604 * first to cover these cases. Due to common
605 * clock framework restrictions that do not allow
606 * to disable a clock that has not been enabled,
607 * we need to enable the clock first.
609 ret = clk_prepare_enable(priv->mclk);
611 clk_disable_unprepare(priv->mclk);
613 if (byt_rt5651_quirk & BYT_RT5651_MCLK_25MHZ)
614 ret = clk_set_rate(priv->mclk, 25000000);
616 ret = clk_set_rate(priv->mclk, 19200000);
619 dev_err(card->dev, "unable to set MCLK rate\n");
623 if (BYT_RT5651_JDSRC(byt_rt5651_quirk))
624 report = SND_JACK_HEADSET | SND_JACK_BTN_0;
625 else if (priv->hp_detect)
626 report = SND_JACK_HEADSET;
629 ret = snd_soc_card_jack_new(runtime->card, "Headset",
630 report, &priv->jack, bytcr_jack_pins,
631 ARRAY_SIZE(bytcr_jack_pins));
633 dev_err(runtime->dev, "jack creation failed %d\n", ret);
637 if (report & SND_JACK_BTN_0)
638 snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0,
641 ret = snd_soc_component_set_jack(codec, &priv->jack,
650 static const struct snd_soc_pcm_stream byt_rt5651_dai_params = {
651 .formats = SNDRV_PCM_FMTBIT_S24_LE,
658 static int byt_rt5651_codec_fixup(struct snd_soc_pcm_runtime *rtd,
659 struct snd_pcm_hw_params *params)
661 struct snd_interval *rate = hw_param_interval(params,
662 SNDRV_PCM_HW_PARAM_RATE);
663 struct snd_interval *channels = hw_param_interval(params,
664 SNDRV_PCM_HW_PARAM_CHANNELS);
667 /* The DSP will covert the FE rate to 48k, stereo */
668 rate->min = rate->max = 48000;
669 channels->min = channels->max = 2;
671 if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) ||
672 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
673 /* set SSP0 to 16-bit */
674 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
677 /* set SSP2 to 24-bit */
678 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
683 * Default mode for SSP configuration is TDM 4 slot, override config
684 * with explicit setting to I2S 2ch. The word length is set with
685 * dai_set_tdm_slot() since there is no other API exposed
687 ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
689 SND_SOC_DAIFMT_NB_NF |
690 SND_SOC_DAIFMT_CBS_CFS
694 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
698 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, bits);
700 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
707 static const unsigned int rates_48000[] = {
711 static const struct snd_pcm_hw_constraint_list constraints_48000 = {
712 .count = ARRAY_SIZE(rates_48000),
716 static int byt_rt5651_aif1_startup(struct snd_pcm_substream *substream)
718 return snd_pcm_hw_constraint_list(substream->runtime, 0,
719 SNDRV_PCM_HW_PARAM_RATE,
723 static const struct snd_soc_ops byt_rt5651_aif1_ops = {
724 .startup = byt_rt5651_aif1_startup,
727 static const struct snd_soc_ops byt_rt5651_be_ssp2_ops = {
728 .hw_params = byt_rt5651_aif1_hw_params,
731 static struct snd_soc_dai_link byt_rt5651_dais[] = {
732 [MERR_DPCM_AUDIO] = {
733 .name = "Audio Port",
734 .stream_name = "Audio",
735 .cpu_dai_name = "media-cpu-dai",
736 .codec_dai_name = "snd-soc-dummy-dai",
737 .codec_name = "snd-soc-dummy",
738 .platform_name = "sst-mfld-platform",
743 .ops = &byt_rt5651_aif1_ops,
745 [MERR_DPCM_DEEP_BUFFER] = {
746 .name = "Deep-Buffer Audio Port",
747 .stream_name = "Deep-Buffer Audio",
748 .cpu_dai_name = "deepbuffer-cpu-dai",
749 .codec_dai_name = "snd-soc-dummy-dai",
750 .codec_name = "snd-soc-dummy",
751 .platform_name = "sst-mfld-platform",
755 .ops = &byt_rt5651_aif1_ops,
757 /* CODEC<->CODEC link */
760 .name = "SSP2-Codec",
762 .cpu_dai_name = "ssp2-port",
763 .platform_name = "sst-mfld-platform",
765 .codec_dai_name = "rt5651-aif1",
766 .codec_name = "i2c-10EC5651:00",
767 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
768 | SND_SOC_DAIFMT_CBS_CFS,
769 .be_hw_params_fixup = byt_rt5651_codec_fixup,
774 .init = byt_rt5651_init,
775 .ops = &byt_rt5651_be_ssp2_ops,
780 static char byt_rt5651_codec_name[SND_ACPI_I2C_ID_LEN];
781 static char byt_rt5651_codec_aif_name[12]; /* = "rt5651-aif[1|2]" */
782 static char byt_rt5651_cpu_dai_name[10]; /* = "ssp[0|2]-port" */
783 static char byt_rt5651_long_name[50]; /* = "bytcr-rt5651-*-spk-*-mic[-swapped-hp]" */
785 static int byt_rt5651_suspend(struct snd_soc_card *card)
787 struct snd_soc_component *component;
789 if (!BYT_RT5651_JDSRC(byt_rt5651_quirk))
792 for_each_card_components(card, component) {
793 if (!strcmp(component->name, byt_rt5651_codec_name)) {
794 dev_dbg(component->dev, "disabling jack detect before suspend\n");
795 snd_soc_component_set_jack(component, NULL, NULL);
803 static int byt_rt5651_resume(struct snd_soc_card *card)
805 struct byt_rt5651_private *priv = snd_soc_card_get_drvdata(card);
806 struct snd_soc_component *component;
808 if (!BYT_RT5651_JDSRC(byt_rt5651_quirk))
811 for_each_card_components(card, component) {
812 if (!strcmp(component->name, byt_rt5651_codec_name)) {
813 dev_dbg(component->dev, "re-enabling jack detect after resume\n");
814 snd_soc_component_set_jack(component, &priv->jack,
823 static struct snd_soc_card byt_rt5651_card = {
824 .name = "bytcr-rt5651",
825 .owner = THIS_MODULE,
826 .dai_link = byt_rt5651_dais,
827 .num_links = ARRAY_SIZE(byt_rt5651_dais),
828 .dapm_widgets = byt_rt5651_widgets,
829 .num_dapm_widgets = ARRAY_SIZE(byt_rt5651_widgets),
830 .dapm_routes = byt_rt5651_audio_map,
831 .num_dapm_routes = ARRAY_SIZE(byt_rt5651_audio_map),
832 .fully_routed = true,
833 .suspend_pre = byt_rt5651_suspend,
834 .resume_post = byt_rt5651_resume,
837 static const struct x86_cpu_id baytrail_cpu_ids[] = {
838 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_SILVERMONT }, /* Valleyview */
842 static const struct x86_cpu_id cherrytrail_cpu_ids[] = {
843 { X86_VENDOR_INTEL, 6, INTEL_FAM6_ATOM_AIRMONT }, /* Braswell */
847 static const struct acpi_gpio_params first_gpio = { 0, 0, false };
848 static const struct acpi_gpio_params second_gpio = { 1, 0, false };
850 static const struct acpi_gpio_mapping byt_rt5651_amp_en_first[] = {
851 { "ext-amp-enable-gpios", &first_gpio, 1 },
855 static const struct acpi_gpio_mapping byt_rt5651_amp_en_second[] = {
856 { "ext-amp-enable-gpios", &second_gpio, 1 },
861 * Some boards have I2cSerialBusV2, GpioIo, GpioInt as ACPI resources, other
862 * boards may have I2cSerialBusV2, GpioInt, GpioIo instead. We want the
863 * GpioIo one for the ext-amp-enable-gpio and both count for the index in
864 * acpi_gpio_params index. So we have 2 different mappings and the code
865 * below figures out which one to use.
867 struct byt_rt5651_acpi_resource_data {
872 static int snd_byt_rt5651_acpi_resource(struct acpi_resource *ares, void *arg)
874 struct byt_rt5651_acpi_resource_data *data = arg;
876 if (ares->type != ACPI_RESOURCE_TYPE_GPIO)
879 if (ares->data.gpio.connection_type == ACPI_RESOURCE_GPIO_TYPE_INT)
880 data->gpio_int_idx = data->gpio_count;
886 static void snd_byt_rt5651_mc_pick_amp_en_gpio_mapping(struct device *codec)
888 struct byt_rt5651_acpi_resource_data data = { 0, -1 };
889 LIST_HEAD(resources);
892 ret = acpi_dev_get_resources(ACPI_COMPANION(codec), &resources,
893 snd_byt_rt5651_acpi_resource, &data);
895 dev_warn(codec, "Failed to get ACPI resources, not adding external amplifier GPIO mapping\n");
899 /* All info we need is gathered during the walk */
900 acpi_dev_free_resource_list(&resources);
902 switch (data.gpio_int_idx) {
904 byt_rt5651_gpios = byt_rt5651_amp_en_second;
907 byt_rt5651_gpios = byt_rt5651_amp_en_first;
910 dev_warn(codec, "Unknown GpioInt index %d, not adding external amplifier GPIO mapping\n",
915 struct acpi_chan_package { /* ACPICA seems to require 64 bit integers */
916 u64 aif_value; /* 1: AIF1, 2: AIF2 */
917 u64 mclock_value; /* usually 25MHz (0x17d7940), ignored */
920 static int snd_byt_rt5651_mc_probe(struct platform_device *pdev)
922 static const char * const mic_name[] = { "dmic", "in1", "in2", "in12" };
923 struct byt_rt5651_private *priv;
924 struct snd_soc_acpi_mach *mach;
925 const char *platform_name;
926 struct device *codec_dev;
927 const char *i2c_name = NULL;
928 const char *hp_swapped;
929 bool is_bytcr = false;
934 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
938 /* register the soc card */
939 byt_rt5651_card.dev = &pdev->dev;
941 mach = byt_rt5651_card.dev->platform_data;
942 snd_soc_card_set_drvdata(&byt_rt5651_card, priv);
944 /* fix index of codec dai */
945 for (i = 0; i < ARRAY_SIZE(byt_rt5651_dais); i++) {
946 if (!strcmp(byt_rt5651_dais[i].codec_name, "i2c-10EC5651:00")) {
952 /* fixup codec name based on HID */
953 i2c_name = acpi_dev_get_first_match_name(mach->id, NULL, -1);
955 dev_err(&pdev->dev, "Error cannot find '%s' dev\n", mach->id);
958 snprintf(byt_rt5651_codec_name, sizeof(byt_rt5651_codec_name),
959 "%s%s", "i2c-", i2c_name);
960 byt_rt5651_dais[dai_index].codec_name = byt_rt5651_codec_name;
962 codec_dev = bus_find_device_by_name(&i2c_bus_type, NULL,
963 byt_rt5651_codec_name);
965 return -EPROBE_DEFER;
968 * swap SSP0 if bytcr is detected
969 * (will be overridden if DMI quirk is detected)
971 if (x86_match_cpu(baytrail_cpu_ids)) {
972 if (mach->mach_params.acpi_ipc_irq_index == 0)
978 * Baytrail CR platforms may have CHAN package in BIOS, try
979 * to find relevant routing quirk based as done on Windows
980 * platforms. We have to read the information directly from the
981 * BIOS, at this stage the card is not created and the links
982 * with the codec driver/pdata are non-existent
985 struct acpi_chan_package chan_package;
987 /* format specified: 2 64-bit integers */
988 struct acpi_buffer format = {sizeof("NN"), "NN"};
989 struct acpi_buffer state = {0, NULL};
990 struct snd_soc_acpi_package_context pkg_ctx;
991 bool pkg_found = false;
993 state.length = sizeof(chan_package);
994 state.pointer = &chan_package;
996 pkg_ctx.name = "CHAN";
998 pkg_ctx.format = &format;
999 pkg_ctx.state = &state;
1000 pkg_ctx.data_valid = false;
1002 pkg_found = snd_soc_acpi_find_package_from_hid(mach->id,
1005 if (chan_package.aif_value == 1) {
1006 dev_info(&pdev->dev, "BIOS Routing: AIF1 connected\n");
1007 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF1;
1008 } else if (chan_package.aif_value == 2) {
1009 dev_info(&pdev->dev, "BIOS Routing: AIF2 connected\n");
1010 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2;
1012 dev_info(&pdev->dev, "BIOS Routing isn't valid, ignored\n");
1018 /* no BIOS indications, assume SSP0-AIF2 connection */
1019 byt_rt5651_quirk |= BYT_RT5651_SSP0_AIF2;
1023 /* check quirks before creating card */
1024 dmi_check_system(byt_rt5651_quirk_table);
1026 if (quirk_override) {
1027 dev_info(&pdev->dev, "Overriding quirk 0x%x => 0x%x\n",
1028 (unsigned int)byt_rt5651_quirk, quirk_override);
1029 byt_rt5651_quirk = quirk_override;
1032 /* Must be called before register_card, also see declaration comment. */
1033 ret_val = byt_rt5651_add_codec_device_props(codec_dev);
1035 put_device(codec_dev);
1039 /* Cherry Trail devices use an external amplifier enable gpio */
1040 if (x86_match_cpu(cherrytrail_cpu_ids) && !byt_rt5651_gpios)
1041 snd_byt_rt5651_mc_pick_amp_en_gpio_mapping(codec_dev);
1043 if (byt_rt5651_gpios) {
1044 devm_acpi_dev_add_driver_gpios(codec_dev, byt_rt5651_gpios);
1045 priv->ext_amp_gpio = devm_fwnode_get_index_gpiod_from_child(
1046 &pdev->dev, "ext-amp-enable", 0,
1048 GPIOD_OUT_LOW, "speaker-amp");
1049 if (IS_ERR(priv->ext_amp_gpio)) {
1050 ret_val = PTR_ERR(priv->ext_amp_gpio);
1053 priv->ext_amp_gpio = NULL;
1056 dev_err(&pdev->dev, "Failed to get ext-amp-enable GPIO: %d\n",
1060 put_device(codec_dev);
1064 priv->hp_detect = devm_fwnode_get_index_gpiod_from_child(
1065 &pdev->dev, "hp-detect", 0,
1067 GPIOD_IN, "hp-detect");
1068 if (IS_ERR(priv->hp_detect)) {
1069 ret_val = PTR_ERR(priv->hp_detect);
1072 priv->hp_detect = NULL;
1075 dev_err(&pdev->dev, "Failed to get hp-detect GPIO: %d\n",
1079 put_device(codec_dev);
1085 put_device(codec_dev);
1087 log_quirks(&pdev->dev);
1089 if ((byt_rt5651_quirk & BYT_RT5651_SSP2_AIF2) ||
1090 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
1091 /* fixup codec aif name */
1092 snprintf(byt_rt5651_codec_aif_name,
1093 sizeof(byt_rt5651_codec_aif_name),
1094 "%s", "rt5651-aif2");
1096 byt_rt5651_dais[dai_index].codec_dai_name =
1097 byt_rt5651_codec_aif_name;
1100 if ((byt_rt5651_quirk & BYT_RT5651_SSP0_AIF1) ||
1101 (byt_rt5651_quirk & BYT_RT5651_SSP0_AIF2)) {
1102 /* fixup cpu dai name name */
1103 snprintf(byt_rt5651_cpu_dai_name,
1104 sizeof(byt_rt5651_cpu_dai_name),
1107 byt_rt5651_dais[dai_index].cpu_dai_name =
1108 byt_rt5651_cpu_dai_name;
1111 if (byt_rt5651_quirk & BYT_RT5651_MCLK_EN) {
1112 priv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
1113 if (IS_ERR(priv->mclk)) {
1114 ret_val = PTR_ERR(priv->mclk);
1116 "Failed to get MCLK from pmc_plt_clk_3: %d\n",
1119 * Fall back to bit clock usage for -ENOENT (clock not
1120 * available likely due to missing dependencies), bail
1121 * for all other errors, including -EPROBE_DEFER
1123 if (ret_val != -ENOENT)
1125 byt_rt5651_quirk &= ~BYT_RT5651_MCLK_EN;
1129 if (byt_rt5651_quirk & BYT_RT5651_HP_LR_SWAPPED)
1130 hp_swapped = "-hp-swapped";
1134 snprintf(byt_rt5651_long_name, sizeof(byt_rt5651_long_name),
1135 "bytcr-rt5651-%s-spk-%s-mic%s",
1136 (byt_rt5651_quirk & BYT_RT5651_MONO_SPEAKER) ?
1138 mic_name[BYT_RT5651_MAP(byt_rt5651_quirk)], hp_swapped);
1139 byt_rt5651_card.long_name = byt_rt5651_long_name;
1141 /* override plaform name, if required */
1142 platform_name = mach->mach_params.platform;
1144 ret_val = snd_soc_fixup_dai_links_platform_name(&byt_rt5651_card,
1149 ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5651_card);
1152 dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
1156 platform_set_drvdata(pdev, &byt_rt5651_card);
1160 static struct platform_driver snd_byt_rt5651_mc_driver = {
1162 .name = "bytcr_rt5651",
1164 .probe = snd_byt_rt5651_mc_probe,
1167 module_platform_driver(snd_byt_rt5651_mc_driver);
1169 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver for RT5651");
1170 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
1171 MODULE_LICENSE("GPL v2");
1172 MODULE_ALIAS("platform:bytcr_rt5651");