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