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