2 * byt_cr_dpcm_rt5640.c - ASoc Machine driver for Intel Byt CR platform
4 * Copyright (C) 2014 Intel Corp
5 * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 #include <linux/init.h>
21 #include <linux/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"
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)
49 static unsigned long byt_rt5640_quirk
= BYT_RT5640_DMIC1_MAP
|
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
),
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"},
66 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic1_map
[] = {
67 {"DMIC1", NULL
, "Internal Mic"},
70 static const struct snd_soc_dapm_route byt_rt5640_intmic_dmic2_map
[] = {
71 {"DMIC2", NULL
, "Internal Mic"},
74 static const struct snd_soc_dapm_route byt_rt5640_intmic_in1_map
[] = {
75 {"Internal Mic", NULL
, "MICBIAS1"},
76 {"IN1P", NULL
, "Internal Mic"},
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"},
85 {"AIF1 Playback", NULL
, "ssp2 Tx"},
86 {"ssp2 Rx", NULL
, "AIF1 Capture"},
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"},
95 {"AIF2 Playback", NULL
, "ssp2 Tx"},
96 {"ssp2 Rx", NULL
, "AIF2 Capture"},
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"},
103 {"AIF1 Playback", NULL
, "ssp0 Tx"},
104 {"ssp0 Rx", NULL
, "AIF1 Capture"},
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"},
111 {"AIF2 Playback", NULL
, "ssp0 Tx"},
112 {"ssp0 Rx", NULL
, "AIF2 Capture"},
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"},
122 static const struct snd_soc_dapm_route byt_rt5640_mono_spk_map
[] = {
123 {"Speaker", NULL
, "SPOLP"},
124 {"Speaker", NULL
, "SPOLN"},
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"),
134 static int byt_rt5640_aif1_hw_params(struct snd_pcm_substream
*substream
,
135 struct snd_pcm_hw_params
*params
)
137 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
138 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
141 snd_soc_dai_set_bclk_ratio(codec_dai
, 50);
143 ret
= snd_soc_dai_set_sysclk(codec_dai
, RT5640_SCLK_S_PLL1
,
144 params_rate(params
) * 512,
147 dev_err(rtd
->dev
, "can't set codec clock %d\n", ret
);
151 ret
= snd_soc_dai_set_pll(codec_dai
, 0, RT5640_PLL1_S_BCLK1
,
152 params_rate(params
) * 50,
153 params_rate(params
) * 512);
155 dev_err(rtd
->dev
, "can't set codec pll: %d\n", ret
);
162 static int byt_rt5640_quirk_cb(const struct dmi_system_id
*id
)
164 byt_rt5640_quirk
= (unsigned long)id
->driver_data
;
168 static const struct dmi_system_id byt_rt5640_quirk_table
[] = {
170 .callback
= byt_rt5640_quirk_cb
,
172 DMI_EXACT_MATCH(DMI_SYS_VENDOR
, "ASUSTeK COMPUTER INC."),
173 DMI_EXACT_MATCH(DMI_PRODUCT_NAME
, "T100TA"),
175 .driver_data
= (unsigned long *)BYT_RT5640_IN1_MAP
,
178 .callback
= byt_rt5640_quirk_cb
,
180 DMI_EXACT_MATCH(DMI_SYS_VENDOR
, "ASUSTeK COMPUTER INC."),
181 DMI_EXACT_MATCH(DMI_PRODUCT_NAME
, "T100TAF"),
183 .driver_data
= (unsigned long *)(BYT_RT5640_IN1_MAP
|
184 BYT_RT5640_MONO_SPEAKER
|
189 .callback
= byt_rt5640_quirk_cb
,
191 DMI_EXACT_MATCH(DMI_SYS_VENDOR
, "DellInc."),
192 DMI_EXACT_MATCH(DMI_PRODUCT_NAME
, "Venue 8 Pro 5830"),
194 .driver_data
= (unsigned long *)(BYT_RT5640_DMIC2_MAP
|
198 .callback
= byt_rt5640_quirk_cb
,
200 DMI_EXACT_MATCH(DMI_SYS_VENDOR
, "Hewlett-Packard"),
201 DMI_EXACT_MATCH(DMI_PRODUCT_NAME
, "HP ElitePad 1000 G2"),
203 .driver_data
= (unsigned long *)BYT_RT5640_IN1_MAP
,
208 static int byt_rt5640_init(struct snd_soc_pcm_runtime
*runtime
)
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
;
216 card
->dapm
.idle_bias_off
= true;
218 rt5640_sel_asrc_clk_src(codec
,
219 RT5640_DA_STEREO_FILTER
|
220 RT5640_AD_STEREO_FILTER
,
221 RT5640_CLK_SEL_ASRC
);
223 ret
= snd_soc_add_card_controls(card
, byt_rt5640_controls
,
224 ARRAY_SIZE(byt_rt5640_controls
));
226 dev_err(card
->dev
, "unable to add card controls\n");
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
);
235 case BYT_RT5640_DMIC2_MAP
:
236 custom_map
= byt_rt5640_intmic_dmic2_map
;
237 num_routes
= ARRAY_SIZE(byt_rt5640_intmic_dmic2_map
);
240 custom_map
= byt_rt5640_intmic_dmic1_map
;
241 num_routes
= ARRAY_SIZE(byt_rt5640_intmic_dmic1_map
);
244 ret
= snd_soc_dapm_add_routes(&card
->dapm
, custom_map
, num_routes
);
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
));
261 ret
= snd_soc_dapm_add_routes(&card
->dapm
,
262 byt_rt5640_ssp2_aif1_map
,
263 ARRAY_SIZE(byt_rt5640_ssp2_aif1_map
));
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
));
273 ret
= snd_soc_dapm_add_routes(&card
->dapm
,
274 byt_rt5640_stereo_spk_map
,
275 ARRAY_SIZE(byt_rt5640_stereo_spk_map
));
280 if (byt_rt5640_quirk
& BYT_RT5640_DIFF_MIC
) {
281 snd_soc_update_bits(codec
, RT5640_IN1_IN2
, RT5640_IN_DF1
,
285 if (byt_rt5640_quirk
& BYT_RT5640_DMIC_EN
) {
286 ret
= rt5640_dmic_enable(codec
, 0, 0);
291 snd_soc_dapm_ignore_suspend(&card
->dapm
, "Headphone");
292 snd_soc_dapm_ignore_suspend(&card
->dapm
, "Speaker");
297 static const struct snd_soc_pcm_stream byt_rt5640_dai_params
= {
298 .formats
= SNDRV_PCM_FMTBIT_S24_LE
,
305 static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime
*rtd
,
306 struct snd_pcm_hw_params
*params
)
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
);
314 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
315 rate
->min
= rate
->max
= 48000;
316 channels
->min
= channels
->max
= 2;
318 /* set SSP2 to 24-bit */
319 params_set_format(params
, SNDRV_PCM_FORMAT_S24_LE
);
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
326 ret
= snd_soc_dai_set_fmt(rtd
->cpu_dai
,
328 SND_SOC_DAIFMT_NB_IF
|
329 SND_SOC_DAIFMT_CBS_CFS
332 dev_err(rtd
->dev
, "can't set format to I2S, err %d\n", ret
);
336 ret
= snd_soc_dai_set_tdm_slot(rtd
->cpu_dai
, 0x3, 0x3, 2, 24);
338 dev_err(rtd
->dev
, "can't set I2S config, err %d\n", ret
);
345 static int byt_rt5640_aif1_startup(struct snd_pcm_substream
*substream
)
347 return snd_pcm_hw_constraint_single(substream
->runtime
,
348 SNDRV_PCM_HW_PARAM_RATE
, 48000);
351 static struct snd_soc_ops byt_rt5640_aif1_ops
= {
352 .startup
= byt_rt5640_aif1_startup
,
355 static struct snd_soc_ops byt_rt5640_be_ssp2_ops
= {
356 .hw_params
= byt_rt5640_aif1_hw_params
,
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",
371 .ops
= &byt_rt5640_aif1_ops
,
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",
384 .ops
= &byt_rt5640_aif1_ops
,
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",
396 .name
= "SSP2-Codec",
398 .cpu_dai_name
= "ssp2-port", /* overwritten for ssp0 routing */
399 .platform_name
= "sst-mfld-platform",
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
,
409 .init
= byt_rt5640_init
,
410 .ops
= &byt_rt5640_be_ssp2_ops
,
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,
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" */
431 static int snd_byt_rt5640_mc_probe(struct platform_device
*pdev
)
434 struct sst_acpi_mach
*mach
;
435 const char *i2c_name
= NULL
;
439 /* register the soc card */
440 byt_rt5640_card
.dev
= &pdev
->dev
;
441 mach
= byt_rt5640_card
.dev
->platform_data
;
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")) {
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
);
458 byt_rt5640_dais
[dai_index
].codec_name
= byt_rt5640_codec_name
;
461 /* check quirks before creating card */
462 dmi_check_system(byt_rt5640_quirk_table
);
464 if ((byt_rt5640_quirk
& BYT_RT5640_SSP2_AIF2
) ||
465 (byt_rt5640_quirk
& BYT_RT5640_SSP0_AIF2
)) {
467 /* fixup codec aif name */
468 snprintf(byt_rt5640_codec_aif_name
,
469 sizeof(byt_rt5640_codec_aif_name
),
470 "%s", "rt5640-aif2");
472 byt_rt5640_dais
[dai_index
].codec_dai_name
=
473 byt_rt5640_codec_aif_name
;
476 if ((byt_rt5640_quirk
& BYT_RT5640_SSP0_AIF1
) ||
477 (byt_rt5640_quirk
& BYT_RT5640_SSP0_AIF2
)) {
479 /* fixup cpu dai name name */
480 snprintf(byt_rt5640_cpu_dai_name
,
481 sizeof(byt_rt5640_cpu_dai_name
),
484 byt_rt5640_dais
[dai_index
].cpu_dai_name
=
485 byt_rt5640_cpu_dai_name
;
488 ret_val
= devm_snd_soc_register_card(&pdev
->dev
, &byt_rt5640_card
);
491 dev_err(&pdev
->dev
, "devm_snd_soc_register_card failed %d\n",
495 platform_set_drvdata(pdev
, &byt_rt5640_card
);
499 static struct platform_driver snd_byt_rt5640_mc_driver
= {
501 .name
= "bytcr_rt5640",
502 .pm
= &snd_soc_pm_ops
,
504 .probe
= snd_byt_rt5640_mc_probe
,
507 module_platform_driver(snd_byt_rt5640_mc_driver
);
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");