]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - sound/soc/intel/boards/bytcr_rt5640.c
4337b5e85921c9e4836c075983390adc828ae8ed
[mirror_ubuntu-bionic-kernel.git] / sound / soc / intel / boards / bytcr_rt5640.c
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>
23 #include <linux/acpi.h>
24 #include <linux/device.h>
25 #include <linux/dmi.h>
26 #include <linux/slab.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/jack.h>
31 #include "../../codecs/rt5640.h"
32 #include "../atom/sst-atom-controls.h"
33 #include "../common/sst-acpi.h"
34
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)
43 #define BYT_RT5640_MONO_SPEAKER BIT(17)
44 #define BYT_RT5640_DIFF_MIC BIT(18) /* defaut is single-ended */
45 #define BYT_RT5640_SSP2_AIF2 BIT(19) /* default is using AIF1 */
46 #define BYT_RT5640_SSP0_AIF1 BIT(20)
47 #define BYT_RT5640_SSP0_AIF2 BIT(21)
48
49 static unsigned long byt_rt5640_quirk = BYT_RT5640_DMIC1_MAP |
50 BYT_RT5640_DMIC_EN;
51
52 static const struct snd_soc_dapm_widget byt_rt5640_widgets[] = {
53 SND_SOC_DAPM_HP("Headphone", NULL),
54 SND_SOC_DAPM_MIC("Headset Mic", NULL),
55 SND_SOC_DAPM_MIC("Internal Mic", NULL),
56 SND_SOC_DAPM_SPK("Speaker", NULL),
57 };
58
59 static const struct snd_soc_dapm_route byt_rt5640_audio_map[] = {
60 {"Headset Mic", NULL, "MICBIAS1"},
61 {"IN2P", NULL, "Headset Mic"},
62 {"Headphone", NULL, "HPOL"},
63 {"Headphone", NULL, "HPOR"},
64 };
65
66 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic1_map[] = {
67 {"DMIC1", NULL, "Internal Mic"},
68 };
69
70 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic2_map[] = {
71 {"DMIC2", NULL, "Internal Mic"},
72 };
73
74 static const struct snd_soc_dapm_route byt_rt5640_intmic_in1_map[] = {
75 {"Internal Mic", NULL, "MICBIAS1"},
76 {"IN1P", NULL, "Internal Mic"},
77 };
78
79 static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif1_map[] = {
80 {"ssp2 Tx", NULL, "codec_out0"},
81 {"ssp2 Tx", NULL, "codec_out1"},
82 {"codec_in0", NULL, "ssp2 Rx"},
83 {"codec_in1", NULL, "ssp2 Rx"},
84
85 {"AIF1 Playback", NULL, "ssp2 Tx"},
86 {"ssp2 Rx", NULL, "AIF1 Capture"},
87 };
88
89 static const struct snd_soc_dapm_route byt_rt5640_ssp2_aif2_map[] = {
90 {"ssp2 Tx", NULL, "codec_out0"},
91 {"ssp2 Tx", NULL, "codec_out1"},
92 {"codec_in0", NULL, "ssp2 Rx"},
93 {"codec_in1", NULL, "ssp2 Rx"},
94
95 {"AIF2 Playback", NULL, "ssp2 Tx"},
96 {"ssp2 Rx", NULL, "AIF2 Capture"},
97 };
98
99 static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif1_map[] = {
100 {"ssp0 Tx", NULL, "modem_out"},
101 {"modem_in", NULL, "ssp0 Rx"},
102
103 {"AIF1 Playback", NULL, "ssp0 Tx"},
104 {"ssp0 Rx", NULL, "AIF1 Capture"},
105 };
106
107 static const struct snd_soc_dapm_route byt_rt5640_ssp0_aif2_map[] = {
108 {"ssp0 Tx", NULL, "modem_out"},
109 {"modem_in", NULL, "ssp0 Rx"},
110
111 {"AIF2 Playback", NULL, "ssp0 Tx"},
112 {"ssp0 Rx", NULL, "AIF2 Capture"},
113 };
114
115 static const struct snd_soc_dapm_route byt_rt5640_stereo_spk_map[] = {
116 {"Speaker", NULL, "SPOLP"},
117 {"Speaker", NULL, "SPOLN"},
118 {"Speaker", NULL, "SPORP"},
119 {"Speaker", NULL, "SPORN"},
120 };
121
122 static const struct snd_soc_dapm_route byt_rt5640_mono_spk_map[] = {
123 {"Speaker", NULL, "SPOLP"},
124 {"Speaker", NULL, "SPOLN"},
125 };
126
127 static const struct snd_kcontrol_new byt_rt5640_controls[] = {
128 SOC_DAPM_PIN_SWITCH("Headphone"),
129 SOC_DAPM_PIN_SWITCH("Headset Mic"),
130 SOC_DAPM_PIN_SWITCH("Internal Mic"),
131 SOC_DAPM_PIN_SWITCH("Speaker"),
132 };
133
134 static int byt_rt5640_aif1_hw_params(struct snd_pcm_substream *substream,
135 struct snd_pcm_hw_params *params)
136 {
137 struct snd_soc_pcm_runtime *rtd = substream->private_data;
138 struct snd_soc_dai *codec_dai = rtd->codec_dai;
139 int ret;
140
141 snd_soc_dai_set_bclk_ratio(codec_dai, 50);
142
143 ret = snd_soc_dai_set_sysclk(codec_dai, RT5640_SCLK_S_PLL1,
144 params_rate(params) * 512,
145 SND_SOC_CLOCK_IN);
146 if (ret < 0) {
147 dev_err(rtd->dev, "can't set codec clock %d\n", ret);
148 return ret;
149 }
150
151 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5640_PLL1_S_BCLK1,
152 params_rate(params) * 50,
153 params_rate(params) * 512);
154 if (ret < 0) {
155 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
156 return ret;
157 }
158
159 return 0;
160 }
161
162 static int byt_rt5640_quirk_cb(const struct dmi_system_id *id)
163 {
164 byt_rt5640_quirk = (unsigned long)id->driver_data;
165 return 1;
166 }
167
168 static const struct dmi_system_id byt_rt5640_quirk_table[] = {
169 {
170 .callback = byt_rt5640_quirk_cb,
171 .matches = {
172 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
173 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TA"),
174 },
175 .driver_data = (unsigned long *)BYT_RT5640_IN1_MAP,
176 },
177 {
178 .callback = byt_rt5640_quirk_cb,
179 .matches = {
180 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
181 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "T100TAF"),
182 },
183 .driver_data = (unsigned long *)(BYT_RT5640_IN1_MAP |
184 BYT_RT5640_MONO_SPEAKER |
185 BYT_RT5640_DIFF_MIC
186 ),
187 },
188 {
189 .callback = byt_rt5640_quirk_cb,
190 .matches = {
191 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "DellInc."),
192 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Venue 8 Pro 5830"),
193 },
194 .driver_data = (unsigned long *)(BYT_RT5640_DMIC2_MAP |
195 BYT_RT5640_DMIC_EN),
196 },
197 {
198 .callback = byt_rt5640_quirk_cb,
199 .matches = {
200 DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
201 DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "HP ElitePad 1000 G2"),
202 },
203 .driver_data = (unsigned long *)BYT_RT5640_IN1_MAP,
204 },
205 {}
206 };
207
208 static int byt_rt5640_init(struct snd_soc_pcm_runtime *runtime)
209 {
210 int ret;
211 struct snd_soc_codec *codec = runtime->codec;
212 struct snd_soc_card *card = runtime->card;
213 const struct snd_soc_dapm_route *custom_map;
214 int num_routes;
215
216 card->dapm.idle_bias_off = true;
217
218 rt5640_sel_asrc_clk_src(codec,
219 RT5640_DA_STEREO_FILTER |
220 RT5640_AD_STEREO_FILTER,
221 RT5640_CLK_SEL_ASRC);
222
223 ret = snd_soc_add_card_controls(card, byt_rt5640_controls,
224 ARRAY_SIZE(byt_rt5640_controls));
225 if (ret) {
226 dev_err(card->dev, "unable to add card controls\n");
227 return ret;
228 }
229
230 switch (BYT_RT5640_MAP(byt_rt5640_quirk)) {
231 case BYT_RT5640_IN1_MAP:
232 custom_map = byt_rt5640_intmic_in1_map;
233 num_routes = ARRAY_SIZE(byt_rt5640_intmic_in1_map);
234 break;
235 case BYT_RT5640_DMIC2_MAP:
236 custom_map = byt_rt5640_intmic_dmic2_map;
237 num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic2_map);
238 break;
239 default:
240 custom_map = byt_rt5640_intmic_dmic1_map;
241 num_routes = ARRAY_SIZE(byt_rt5640_intmic_dmic1_map);
242 }
243
244 ret = snd_soc_dapm_add_routes(&card->dapm, custom_map, num_routes);
245 if (ret)
246 return ret;
247
248 if (byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) {
249 ret = snd_soc_dapm_add_routes(&card->dapm,
250 byt_rt5640_ssp2_aif2_map,
251 ARRAY_SIZE(byt_rt5640_ssp2_aif2_map));
252 } else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) {
253 ret = snd_soc_dapm_add_routes(&card->dapm,
254 byt_rt5640_ssp0_aif1_map,
255 ARRAY_SIZE(byt_rt5640_ssp0_aif1_map));
256 } else if (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2) {
257 ret = snd_soc_dapm_add_routes(&card->dapm,
258 byt_rt5640_ssp0_aif2_map,
259 ARRAY_SIZE(byt_rt5640_ssp0_aif2_map));
260 } else {
261 ret = snd_soc_dapm_add_routes(&card->dapm,
262 byt_rt5640_ssp2_aif1_map,
263 ARRAY_SIZE(byt_rt5640_ssp2_aif1_map));
264 }
265 if (ret)
266 return ret;
267
268 if (byt_rt5640_quirk & BYT_RT5640_MONO_SPEAKER) {
269 ret = snd_soc_dapm_add_routes(&card->dapm,
270 byt_rt5640_mono_spk_map,
271 ARRAY_SIZE(byt_rt5640_mono_spk_map));
272 } else {
273 ret = snd_soc_dapm_add_routes(&card->dapm,
274 byt_rt5640_stereo_spk_map,
275 ARRAY_SIZE(byt_rt5640_stereo_spk_map));
276 }
277 if (ret)
278 return ret;
279
280 if (byt_rt5640_quirk & BYT_RT5640_DIFF_MIC) {
281 snd_soc_update_bits(codec, RT5640_IN1_IN2, RT5640_IN_DF1,
282 RT5640_IN_DF1);
283 }
284
285 if (byt_rt5640_quirk & BYT_RT5640_DMIC_EN) {
286 ret = rt5640_dmic_enable(codec, 0, 0);
287 if (ret)
288 return ret;
289 }
290
291 snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone");
292 snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker");
293
294 return ret;
295 }
296
297 static const struct snd_soc_pcm_stream byt_rt5640_dai_params = {
298 .formats = SNDRV_PCM_FMTBIT_S24_LE,
299 .rate_min = 48000,
300 .rate_max = 48000,
301 .channels_min = 2,
302 .channels_max = 2,
303 };
304
305 static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime *rtd,
306 struct snd_pcm_hw_params *params)
307 {
308 struct snd_interval *rate = hw_param_interval(params,
309 SNDRV_PCM_HW_PARAM_RATE);
310 struct snd_interval *channels = hw_param_interval(params,
311 SNDRV_PCM_HW_PARAM_CHANNELS);
312 int ret;
313
314 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
315 rate->min = rate->max = 48000;
316 channels->min = channels->max = 2;
317
318 /* set SSP2 to 24-bit */
319 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
320
321 /*
322 * Default mode for SSP configuration is TDM 4 slot, override config
323 * with explicit setting to I2S 2ch 24-bit. The word length is set with
324 * dai_set_tdm_slot() since there is no other API exposed
325 */
326 ret = snd_soc_dai_set_fmt(rtd->cpu_dai,
327 SND_SOC_DAIFMT_I2S |
328 SND_SOC_DAIFMT_NB_IF |
329 SND_SOC_DAIFMT_CBS_CFS
330 );
331 if (ret < 0) {
332 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
333 return ret;
334 }
335
336 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 24);
337 if (ret < 0) {
338 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
339 return ret;
340 }
341
342 return 0;
343 }
344
345 static int byt_rt5640_aif1_startup(struct snd_pcm_substream *substream)
346 {
347 return snd_pcm_hw_constraint_single(substream->runtime,
348 SNDRV_PCM_HW_PARAM_RATE, 48000);
349 }
350
351 static struct snd_soc_ops byt_rt5640_aif1_ops = {
352 .startup = byt_rt5640_aif1_startup,
353 };
354
355 static struct snd_soc_ops byt_rt5640_be_ssp2_ops = {
356 .hw_params = byt_rt5640_aif1_hw_params,
357 };
358
359 static struct snd_soc_dai_link byt_rt5640_dais[] = {
360 [MERR_DPCM_AUDIO] = {
361 .name = "Baytrail Audio Port",
362 .stream_name = "Baytrail Audio",
363 .cpu_dai_name = "media-cpu-dai",
364 .codec_dai_name = "snd-soc-dummy-dai",
365 .codec_name = "snd-soc-dummy",
366 .platform_name = "sst-mfld-platform",
367 .ignore_suspend = 1,
368 .dynamic = 1,
369 .dpcm_playback = 1,
370 .dpcm_capture = 1,
371 .ops = &byt_rt5640_aif1_ops,
372 },
373 [MERR_DPCM_DEEP_BUFFER] = {
374 .name = "Deep-Buffer Audio Port",
375 .stream_name = "Deep-Buffer Audio",
376 .cpu_dai_name = "deepbuffer-cpu-dai",
377 .codec_dai_name = "snd-soc-dummy-dai",
378 .codec_name = "snd-soc-dummy",
379 .platform_name = "sst-mfld-platform",
380 .ignore_suspend = 1,
381 .nonatomic = true,
382 .dynamic = 1,
383 .dpcm_playback = 1,
384 .ops = &byt_rt5640_aif1_ops,
385 },
386 [MERR_DPCM_COMPR] = {
387 .name = "Baytrail Compressed Port",
388 .stream_name = "Baytrail Compress",
389 .cpu_dai_name = "compress-cpu-dai",
390 .codec_dai_name = "snd-soc-dummy-dai",
391 .codec_name = "snd-soc-dummy",
392 .platform_name = "sst-mfld-platform",
393 },
394 /* back ends */
395 {
396 .name = "SSP2-Codec",
397 .id = 1,
398 .cpu_dai_name = "ssp2-port", /* overwritten for ssp0 routing */
399 .platform_name = "sst-mfld-platform",
400 .no_pcm = 1,
401 .codec_dai_name = "rt5640-aif1", /* changed w/ quirk */
402 .codec_name = "i2c-10EC5640:00", /* overwritten with HID */
403 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
404 | SND_SOC_DAIFMT_CBS_CFS,
405 .be_hw_params_fixup = byt_rt5640_codec_fixup,
406 .ignore_suspend = 1,
407 .dpcm_playback = 1,
408 .dpcm_capture = 1,
409 .init = byt_rt5640_init,
410 .ops = &byt_rt5640_be_ssp2_ops,
411 },
412 };
413
414 /* SoC card */
415 static struct snd_soc_card byt_rt5640_card = {
416 .name = "bytcr-rt5640",
417 .owner = THIS_MODULE,
418 .dai_link = byt_rt5640_dais,
419 .num_links = ARRAY_SIZE(byt_rt5640_dais),
420 .dapm_widgets = byt_rt5640_widgets,
421 .num_dapm_widgets = ARRAY_SIZE(byt_rt5640_widgets),
422 .dapm_routes = byt_rt5640_audio_map,
423 .num_dapm_routes = ARRAY_SIZE(byt_rt5640_audio_map),
424 .fully_routed = true,
425 };
426
427 static char byt_rt5640_codec_name[16]; /* i2c-<HID>:00 with HID being 8 chars */
428 static char byt_rt5640_codec_aif_name[12]; /* = "rt5640-aif[1|2]" */
429 static char byt_rt5640_cpu_dai_name[10]; /* = "ssp[0|2]-port" */
430
431 static int snd_byt_rt5640_mc_probe(struct platform_device *pdev)
432 {
433 int ret_val = 0;
434 struct sst_acpi_mach *mach;
435 const char *i2c_name = NULL;
436 int i;
437 int dai_index;
438
439 /* register the soc card */
440 byt_rt5640_card.dev = &pdev->dev;
441 mach = byt_rt5640_card.dev->platform_data;
442
443 /* fix index of codec dai */
444 dai_index = MERR_DPCM_COMPR + 1;
445 for (i = 0; i < ARRAY_SIZE(byt_rt5640_dais); i++) {
446 if (!strcmp(byt_rt5640_dais[i].codec_name, "i2c-10EC5640:00")) {
447 dai_index = i;
448 break;
449 }
450 }
451
452 /* fixup codec name based on HID */
453 i2c_name = sst_acpi_find_name_from_hid(mach->id);
454 if (i2c_name != NULL) {
455 snprintf(byt_rt5640_codec_name, sizeof(byt_rt5640_codec_name),
456 "%s%s", "i2c-", i2c_name);
457
458 byt_rt5640_dais[dai_index].codec_name = byt_rt5640_codec_name;
459 }
460
461 /* check quirks before creating card */
462 dmi_check_system(byt_rt5640_quirk_table);
463
464 if ((byt_rt5640_quirk & BYT_RT5640_SSP2_AIF2) ||
465 (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
466
467 /* fixup codec aif name */
468 snprintf(byt_rt5640_codec_aif_name,
469 sizeof(byt_rt5640_codec_aif_name),
470 "%s", "rt5640-aif2");
471
472 byt_rt5640_dais[dai_index].codec_dai_name =
473 byt_rt5640_codec_aif_name;
474 }
475
476 if ((byt_rt5640_quirk & BYT_RT5640_SSP0_AIF1) ||
477 (byt_rt5640_quirk & BYT_RT5640_SSP0_AIF2)) {
478
479 /* fixup cpu dai name name */
480 snprintf(byt_rt5640_cpu_dai_name,
481 sizeof(byt_rt5640_cpu_dai_name),
482 "%s", "ssp0-port");
483
484 byt_rt5640_dais[dai_index].cpu_dai_name =
485 byt_rt5640_cpu_dai_name;
486 }
487
488 ret_val = devm_snd_soc_register_card(&pdev->dev, &byt_rt5640_card);
489
490 if (ret_val) {
491 dev_err(&pdev->dev, "devm_snd_soc_register_card failed %d\n",
492 ret_val);
493 return ret_val;
494 }
495 platform_set_drvdata(pdev, &byt_rt5640_card);
496 return ret_val;
497 }
498
499 static struct platform_driver snd_byt_rt5640_mc_driver = {
500 .driver = {
501 .name = "bytcr_rt5640",
502 .pm = &snd_soc_pm_ops,
503 },
504 .probe = snd_byt_rt5640_mc_probe,
505 };
506
507 module_platform_driver(snd_byt_rt5640_mc_driver);
508
509 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
510 MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
511 MODULE_LICENSE("GPL v2");
512 MODULE_ALIAS("platform:bytcr_rt5640");