]>
Commit | Line | Data |
---|---|---|
996cc849 SP |
1 | /* |
2 | * byt_cr_dpcm_rt5640.c - ASoc Machine driver for Intel Byt CR platform | |
3 | * | |
4 | * Copyright (C) 2014 Intel Corp | |
5 | * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com> | |
6 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; version 2 of the License. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
18 | */ | |
19 | ||
20 | #include <linux/init.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/platform_device.h> | |
a2d5563b | 23 | #include <linux/acpi.h> |
996cc849 | 24 | #include <linux/device.h> |
a2d5563b | 25 | #include <linux/dmi.h> |
996cc849 | 26 | #include <linux/slab.h> |
996cc849 SP |
27 | #include <sound/pcm.h> |
28 | #include <sound/pcm_params.h> | |
29 | #include <sound/soc.h> | |
a2d5563b | 30 | #include <sound/jack.h> |
e56c72d5 | 31 | #include "../../codecs/rt5640.h" |
b97169da | 32 | #include "../atom/sst-atom-controls.h" |
caf94ed8 | 33 | #include "../common/sst-acpi.h" |
996cc849 | 34 | |
ab738e4e PLB |
35 | enum { |
36 | BYT_RT5640_DMIC1_MAP, | |
37 | BYT_RT5640_DMIC2_MAP, | |
38 | BYT_RT5640_IN1_MAP, | |
39 | }; | |
40 | ||
41 | #define BYT_RT5640_MAP(quirk) ((quirk) & 0xff) | |
42 | #define BYT_RT5640_DMIC_EN BIT(16) | |
68817cdb | 43 | #define BYT_RT5640_MONO_SPEAKER BIT(17) |
5d98f58f | 44 | #define BYT_RT5640_DIFF_MIC BIT(18) /* defaut is single-ended */ |
ab738e4e PLB |
45 | |
46 | static unsigned long byt_rt5640_quirk = BYT_RT5640_DMIC1_MAP | | |
47 | BYT_RT5640_DMIC_EN; | |
48 | ||
a2d5563b | 49 | static const struct snd_soc_dapm_widget byt_rt5640_widgets[] = { |
996cc849 SP |
50 | SND_SOC_DAPM_HP("Headphone", NULL), |
51 | SND_SOC_DAPM_MIC("Headset Mic", NULL), | |
e2be1da0 PLB |
52 | SND_SOC_DAPM_MIC("Internal Mic", NULL), |
53 | SND_SOC_DAPM_SPK("Speaker", NULL), | |
996cc849 SP |
54 | }; |
55 | ||
a2d5563b | 56 | static const struct snd_soc_dapm_route byt_rt5640_audio_map[] = { |
9fd57471 PLB |
57 | {"AIF1 Playback", NULL, "ssp2 Tx"}, |
58 | {"ssp2 Tx", NULL, "codec_out0"}, | |
59 | {"ssp2 Tx", NULL, "codec_out1"}, | |
60 | {"codec_in0", NULL, "ssp2 Rx"}, | |
61 | {"codec_in1", NULL, "ssp2 Rx"}, | |
62 | {"ssp2 Rx", NULL, "AIF1 Capture"}, | |
63 | ||
996cc849 | 64 | {"Headset Mic", NULL, "MICBIAS1"}, |
e2be1da0 | 65 | {"IN2P", NULL, "Headset Mic"}, |
996cc849 SP |
66 | {"Headphone", NULL, "HPOL"}, |
67 | {"Headphone", NULL, "HPOR"}, | |
996cc849 SP |
68 | }; |
69 | ||
a2d5563b PLB |
70 | static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic1_map[] = { |
71 | {"DMIC1", NULL, "Internal Mic"}, | |
72 | }; | |
73 | ||
74 | static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic2_map[] = { | |
75 | {"DMIC2", NULL, "Internal Mic"}, | |
76 | }; | |
77 | ||
78 | static const struct snd_soc_dapm_route byt_rt5640_intmic_in1_map[] = { | |
79 | {"Internal Mic", NULL, "MICBIAS1"}, | |
80 | {"IN1P", NULL, "Internal Mic"}, | |
81 | }; | |
82 | ||
68817cdb PLB |
83 | static const struct snd_soc_dapm_route byt_rt5640_stereo_spk_map[] = { |
84 | {"Speaker", NULL, "SPOLP"}, | |
85 | {"Speaker", NULL, "SPOLN"}, | |
86 | {"Speaker", NULL, "SPORP"}, | |
87 | {"Speaker", NULL, "SPORN"}, | |
88 | }; | |
89 | ||
90 | static const struct snd_soc_dapm_route byt_rt5640_mono_spk_map[] = { | |
91 | {"Speaker", NULL, "SPOLP"}, | |
92 | {"Speaker", NULL, "SPOLN"}, | |
93 | }; | |
94 | ||
a2d5563b | 95 | static const struct snd_kcontrol_new byt_rt5640_controls[] = { |
996cc849 SP |
96 | SOC_DAPM_PIN_SWITCH("Headphone"), |
97 | SOC_DAPM_PIN_SWITCH("Headset Mic"), | |
e2be1da0 PLB |
98 | SOC_DAPM_PIN_SWITCH("Internal Mic"), |
99 | SOC_DAPM_PIN_SWITCH("Speaker"), | |
996cc849 SP |
100 | }; |
101 | ||
a2d5563b | 102 | static int byt_rt5640_aif1_hw_params(struct snd_pcm_substream *substream, |
996cc849 SP |
103 | struct snd_pcm_hw_params *params) |
104 | { | |
105 | struct snd_soc_pcm_runtime *rtd = substream->private_data; | |
106 | struct snd_soc_dai *codec_dai = rtd->codec_dai; | |
107 | int ret; | |
108 | ||
109 | snd_soc_dai_set_bclk_ratio(codec_dai, 50); | |
110 | ||
111 | ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1, | |
112 | params_rate(params) * 512, | |
113 | SND_SOC_CLOCK_IN); | |
114 | if (ret < 0) { | |
115 | dev_err(rtd->dev, "can't set codec clock %d\n", ret); | |
116 | return ret; | |
117 | } | |
118 | ||
119 | ret = snd_soc_dai_set_pll(codec_dai, 0, RT5640_PLL1_S_BCLK1, | |
120 | params_rate(params) * 50, | |
121 | params_rate(params) * 512); | |
122 | if (ret < 0) { | |
123 | dev_err(rtd->dev, "can't set codec pll: %d\n", ret); | |
124 | return ret; | |
125 | } | |
126 | ||
127 | return 0; | |
128 | } | |
129 | ||
a2d5563b PLB |
130 | static int byt_rt5640_quirk_cb(const struct dmi_system_id *id) |
131 | { | |
132 | byt_rt5640_quirk = (unsigned long)id->driver_data; | |
133 | return 1; | |
134 | } | |
135 | ||
136 | static const struct dmi_system_id byt_rt5640_quirk_table[] = { | |
137 | { | |
138 | .callback = byt_rt5640_quirk_cb, | |
139 | .matches = { | |
73442e3c PLB |
140 | DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."), |
141 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"), | |
a2d5563b PLB |
142 | }, |
143 | .driver_data = (unsigned long *)BYT_RT5640_IN1_MAP, | |
144 | }, | |
145 | { | |
146 | .callback = byt_rt5640_quirk_cb, | |
147 | .matches = { | |
73442e3c PLB |
148 | DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."), |
149 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TAF"), | |
150 | }, | |
68817cdb | 151 | .driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP | |
5d98f58f PLB |
152 | BYT_RT5640_MONO_SPEAKER | |
153 | BYT_RT5640_DIFF_MIC | |
68817cdb | 154 | ), |
73442e3c PLB |
155 | }, |
156 | { | |
157 | .callback = byt_rt5640_quirk_cb, | |
158 | .matches = { | |
159 | DMI_EXACT_MATCH(DMI_SYS_VENDOR, "DellInc."), | |
160 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"), | |
a2d5563b PLB |
161 | }, |
162 | .driver_data = (unsigned long *)(BYT_RT5640_DMIC2_MAP | | |
163 | BYT_RT5640_DMIC_EN), | |
164 | }, | |
55fc2056 JFM |
165 | { |
166 | .callback = byt_rt5640_quirk_cb, | |
167 | .matches = { | |
73442e3c PLB |
168 | DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), |
169 | DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP ElitePad 1000 G2"), | |
55fc2056 JFM |
170 | }, |
171 | .driver_data = (unsigned long *)BYT_RT5640_IN1_MAP, | |
172 | }, | |
a2d5563b PLB |
173 | {} |
174 | }; | |
175 | ||
9fd57471 PLB |
176 | static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime) |
177 | { | |
178 | int ret; | |
179 | struct snd_soc_codec *codec = runtime->codec; | |
180 | struct snd_soc_card *card = runtime->card; | |
181 | const struct snd_soc_dapm_route *custom_map; | |
182 | int num_routes; | |
183 | ||
184 | card->dapm.idle_bias_off = true; | |
185 | ||
0ec66e2d PLB |
186 | rt5640_sel_asrc_clk_src(codec, |
187 | RT5640_DA_STEREO_FILTER | | |
188 | RT5640_AD_STEREO_FILTER, | |
189 | RT5640_CLK_SEL_ASRC); | |
190 | ||
9fd57471 PLB |
191 | ret = snd_soc_add_card_controls(card, byt_rt5640_controls, |
192 | ARRAY_SIZE(byt_rt5640_controls)); | |
193 | if (ret) { | |
194 | dev_err(card->dev, "unable to add card controls\n"); | |
195 | return ret; | |
196 | } | |
197 | ||
9fd57471 PLB |
198 | switch (BYT_RT5640_MAP(byt_rt5640_quirk)) { |
199 | case BYT_RT5640_IN1_MAP: | |
200 | custom_map = byt_rt5640_intmic_in1_map; | |
201 | num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map); | |
202 | break; | |
203 | case BYT_RT5640_DMIC2_MAP: | |
204 | custom_map = byt_rt5640_intmic_dmic2_map; | |
205 | num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map); | |
206 | break; | |
207 | default: | |
208 | custom_map = byt_rt5640_intmic_dmic1_map; | |
209 | num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map); | |
210 | } | |
211 | ||
212 | ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes); | |
213 | if (ret) | |
214 | return ret; | |
215 | ||
68817cdb PLB |
216 | if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) { |
217 | ret = snd_soc_dapm_add_routes(&card->dapm, | |
218 | byt_rt5640_mono_spk_map, | |
219 | ARRAY_SIZE(byt_rt5640_mono_spk_map)); | |
220 | } else { | |
221 | ret = snd_soc_dapm_add_routes(&card->dapm, | |
222 | byt_rt5640_stereo_spk_map, | |
223 | ARRAY_SIZE(byt_rt5640_stereo_spk_map)); | |
224 | } | |
225 | if (ret) | |
226 | return ret; | |
227 | ||
5d98f58f PLB |
228 | if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC) { |
229 | snd_soc_update_bits(codec, RT5640_IN1_IN2, RT5640_IN_DF1, | |
230 | RT5640_IN_DF1); | |
231 | } | |
232 | ||
9fd57471 PLB |
233 | if (byt_rt5640_quirk & BYT_RT5640_DMIC_EN) { |
234 | ret = rt5640_dmic_enable(codec, 0, 0); | |
235 | if (ret) | |
236 | return ret; | |
237 | } | |
238 | ||
239 | snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone"); | |
240 | snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker"); | |
241 | ||
242 | return ret; | |
243 | } | |
244 | ||
a2d5563b | 245 | static const struct snd_soc_pcm_stream byt_rt5640_dai_params = { |
996cc849 SP |
246 | .formats = SNDRV_PCM_FMTBIT_S24_LE, |
247 | .rate_min = 48000, | |
248 | .rate_max = 48000, | |
249 | .channels_min = 2, | |
250 | .channels_max = 2, | |
251 | }; | |
252 | ||
a2d5563b | 253 | static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd, |
996cc849 SP |
254 | struct snd_pcm_hw_params *params) |
255 | { | |
256 | struct snd_interval *rate = hw_param_interval(params, | |
257 | SNDRV_PCM_HW_PARAM_RATE); | |
258 | struct snd_interval *channels = hw_param_interval(params, | |
259 | SNDRV_PCM_HW_PARAM_CHANNELS); | |
3f27dedd | 260 | int ret; |
996cc849 SP |
261 | |
262 | /* The DSP will covert the FE rate to 48k, stereo, 24bits */ | |
263 | rate->min = rate->max = 48000; | |
264 | channels->min = channels->max = 2; | |
265 | ||
266 | /* set SSP2 to 24-bit */ | |
369a9f5f | 267 | params_set_format(params, SNDRV_PCM_FORMAT_S24_LE); |
3f27dedd SG |
268 | |
269 | /* | |
270 | * Default mode for SSP configuration is TDM 4 slot, override config | |
271 | * with explicit setting to I2S 2ch 24-bit. The word length is set with | |
272 | * dai_set_tdm_slot() since there is no other API exposed | |
273 | */ | |
274 | ret = snd_soc_dai_set_fmt(rtd->cpu_dai, | |
275 | SND_SOC_DAIFMT_I2S | | |
276 | SND_SOC_DAIFMT_NB_IF | | |
277 | SND_SOC_DAIFMT_CBS_CFS | |
278 | ); | |
279 | if (ret < 0) { | |
280 | dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret); | |
281 | return ret; | |
282 | } | |
283 | ||
284 | ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 24); | |
285 | if (ret < 0) { | |
286 | dev_err(rtd->dev, "can't set I2S config, err %d\n", ret); | |
287 | return ret; | |
288 | } | |
289 | ||
996cc849 SP |
290 | return 0; |
291 | } | |
292 | ||
a2d5563b | 293 | static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream) |
996cc849 | 294 | { |
d0a1b660 LPC |
295 | return snd_pcm_hw_constraint_single(substream->runtime, |
296 | SNDRV_PCM_HW_PARAM_RATE, 48000); | |
996cc849 SP |
297 | } |
298 | ||
a2d5563b PLB |
299 | static struct snd_soc_ops byt_rt5640_aif1_ops = { |
300 | .startup = byt_rt5640_aif1_startup, | |
996cc849 SP |
301 | }; |
302 | ||
a2d5563b PLB |
303 | static struct snd_soc_ops byt_rt5640_be_ssp2_ops = { |
304 | .hw_params = byt_rt5640_aif1_hw_params, | |
996cc849 SP |
305 | }; |
306 | ||
a2d5563b | 307 | static struct snd_soc_dai_link byt_rt5640_dais[] = { |
996cc849 SP |
308 | [MERR_DPCM_AUDIO] = { |
309 | .name = "Baytrail Audio Port", | |
310 | .stream_name = "Baytrail Audio", | |
311 | .cpu_dai_name = "media-cpu-dai", | |
312 | .codec_dai_name = "snd-soc-dummy-dai", | |
313 | .codec_name = "snd-soc-dummy", | |
314 | .platform_name = "sst-mfld-platform", | |
315 | .ignore_suspend = 1, | |
316 | .dynamic = 1, | |
317 | .dpcm_playback = 1, | |
318 | .dpcm_capture = 1, | |
a2d5563b | 319 | .ops = &byt_rt5640_aif1_ops, |
996cc849 | 320 | }, |
d35eb96a PLB |
321 | [MERR_DPCM_DEEP_BUFFER] = { |
322 | .name = "Deep-Buffer Audio Port", | |
323 | .stream_name = "Deep-Buffer Audio", | |
324 | .cpu_dai_name = "deepbuffer-cpu-dai", | |
325 | .codec_dai_name = "snd-soc-dummy-dai", | |
326 | .codec_name = "snd-soc-dummy", | |
327 | .platform_name = "sst-mfld-platform", | |
328 | .ignore_suspend = 1, | |
329 | .nonatomic = true, | |
330 | .dynamic = 1, | |
331 | .dpcm_playback = 1, | |
332 | .ops = &byt_rt5640_aif1_ops, | |
333 | }, | |
996cc849 SP |
334 | [MERR_DPCM_COMPR] = { |
335 | .name = "Baytrail Compressed Port", | |
336 | .stream_name = "Baytrail Compress", | |
337 | .cpu_dai_name = "compress-cpu-dai", | |
338 | .codec_dai_name = "snd-soc-dummy-dai", | |
339 | .codec_name = "snd-soc-dummy", | |
340 | .platform_name = "sst-mfld-platform", | |
341 | }, | |
342 | /* back ends */ | |
343 | { | |
344 | .name = "SSP2-Codec", | |
2f0ad491 | 345 | .id = 1, |
996cc849 SP |
346 | .cpu_dai_name = "ssp2-port", |
347 | .platform_name = "sst-mfld-platform", | |
348 | .no_pcm = 1, | |
349 | .codec_dai_name = "rt5640-aif1", | |
7762ef42 | 350 | .codec_name = "i2c-10EC5640:00", /* overwritten with HID */ |
996cc849 SP |
351 | .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
352 | | SND_SOC_DAIFMT_CBS_CFS, | |
a2d5563b | 353 | .be_hw_params_fixup = byt_rt5640_codec_fixup, |
996cc849 SP |
354 | .ignore_suspend = 1, |
355 | .dpcm_playback = 1, | |
356 | .dpcm_capture = 1, | |
9fd57471 | 357 | .init = byt_rt5640_init, |
a2d5563b | 358 | .ops = &byt_rt5640_be_ssp2_ops, |
996cc849 SP |
359 | }, |
360 | }; | |
361 | ||
362 | /* SoC card */ | |
9fd57471 | 363 | static struct snd_soc_card byt_rt5640_card = { |
a2d5563b | 364 | .name = "bytcr-rt5640", |
54d8697f | 365 | .owner = THIS_MODULE, |
a2d5563b PLB |
366 | .dai_link = byt_rt5640_dais, |
367 | .num_links = ARRAY_SIZE(byt_rt5640_dais), | |
368 | .dapm_widgets = byt_rt5640_widgets, | |
369 | .num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets), | |
370 | .dapm_routes = byt_rt5640_audio_map, | |
371 | .num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map), | |
9fd57471 | 372 | .fully_routed = true, |
996cc849 SP |
373 | }; |
374 | ||
caf94ed8 PLB |
375 | static char byt_rt5640_codec_name[16]; /* i2c-<HID>:00 with HID being 8 chars */ |
376 | ||
a2d5563b | 377 | static int snd_byt_rt5640_mc_probe(struct platform_device *pdev) |
996cc849 SP |
378 | { |
379 | int ret_val = 0; | |
caf94ed8 | 380 | struct sst_acpi_mach *mach; |
a232b96d PLB |
381 | const char *i2c_name = NULL; |
382 | int i; | |
383 | int dai_index; | |
996cc849 SP |
384 | |
385 | /* register the soc card */ | |
9fd57471 | 386 | byt_rt5640_card.dev = &pdev->dev; |
caf94ed8 PLB |
387 | mach = byt_rt5640_card.dev->platform_data; |
388 | ||
a232b96d PLB |
389 | /* fix index of codec dai */ |
390 | dai_index = MERR_DPCM_COMPR + 1; | |
391 | for (i = 0; i < ARRAY_SIZE(byt_rt5640_dais); i++) { | |
392 | if (!strcmp(byt_rt5640_dais[i].codec_name, "i2c-10EC5640:00")) { | |
393 | dai_index = i; | |
394 | break; | |
395 | } | |
396 | } | |
397 | ||
caf94ed8 | 398 | /* fixup codec name based on HID */ |
a232b96d PLB |
399 | i2c_name = sst_acpi_find_name_from_hid(mach->id); |
400 | if (i2c_name != NULL) { | |
401 | snprintf(byt_rt5640_codec_name, sizeof(byt_rt5640_codec_name), | |
402 | "%s%s", "i2c-", i2c_name); | |
403 | ||
404 | byt_rt5640_dais[dai_index].codec_name = byt_rt5640_codec_name; | |
405 | } | |
9fd57471 | 406 | |
ab738e4e PLB |
407 | /* check quirks before creating card */ |
408 | dmi_check_system(byt_rt5640_quirk_table); | |
409 | ||
9fd57471 | 410 | ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5640_card); |
996cc849 | 411 | |
996cc849 | 412 | if (ret_val) { |
a2d5563b PLB |
413 | dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n", |
414 | ret_val); | |
996cc849 SP |
415 | return ret_val; |
416 | } | |
9fd57471 | 417 | platform_set_drvdata(pdev, &byt_rt5640_card); |
996cc849 SP |
418 | return ret_val; |
419 | } | |
420 | ||
a2d5563b | 421 | static struct platform_driver snd_byt_rt5640_mc_driver = { |
996cc849 | 422 | .driver = { |
a2d5563b | 423 | .name = "bytcr_rt5640", |
996cc849 SP |
424 | .pm = &snd_soc_pm_ops, |
425 | }, | |
a2d5563b | 426 | .probe = snd_byt_rt5640_mc_probe, |
996cc849 SP |
427 | }; |
428 | ||
a2d5563b | 429 | module_platform_driver(snd_byt_rt5640_mc_driver); |
996cc849 SP |
430 | |
431 | MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver"); | |
432 | MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>"); | |
433 | MODULE_LICENSE("GPL v2"); | |
a2d5563b | 434 | MODULE_ALIAS("platform:bytcr_rt5640"); |