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 ret
= snd_soc_dai_set_sysclk(codec_dai
, RT5640_SCLK_S_PLL1
,
142 params_rate(params
) * 512,
145 dev_err(rtd
->dev
, "can't set codec clock %d\n", ret
);
149 if ((byt_rt5640_quirk
& BYT_RT5640_SSP0_AIF1
) ||
150 (byt_rt5640_quirk
& BYT_RT5640_SSP0_AIF2
)) {
152 ret
= snd_soc_dai_set_pll(codec_dai
, 0, RT5640_PLL1_S_BCLK1
,
153 params_rate(params
) * 32, /* FIXME */
154 params_rate(params
) * 512);
156 ret
= snd_soc_dai_set_pll(codec_dai
, 0, RT5640_PLL1_S_BCLK1
,
157 params_rate(params
) * 50,
158 params_rate(params
) * 512);
162 dev_err(rtd
->dev
, "can't set codec pll: %d\n", ret
);
169 static int byt_rt5640_quirk_cb(const struct dmi_system_id
*id
)
171 byt_rt5640_quirk
= (unsigned long)id
->driver_data
;
175 static const struct dmi_system_id byt_rt5640_quirk_table
[] = {
177 .callback
= byt_rt5640_quirk_cb
,
179 DMI_EXACT_MATCH(DMI_SYS_VENDOR
, "ASUSTeK COMPUTER INC."),
180 DMI_EXACT_MATCH(DMI_PRODUCT_NAME
, "T100TA"),
182 .driver_data
= (unsigned long *)BYT_RT5640_IN1_MAP
,
185 .callback
= byt_rt5640_quirk_cb
,
187 DMI_EXACT_MATCH(DMI_SYS_VENDOR
, "ASUSTeK COMPUTER INC."),
188 DMI_EXACT_MATCH(DMI_PRODUCT_NAME
, "T100TAF"),
190 .driver_data
= (unsigned long *)(BYT_RT5640_IN1_MAP
|
191 BYT_RT5640_MONO_SPEAKER
|
196 .callback
= byt_rt5640_quirk_cb
,
198 DMI_EXACT_MATCH(DMI_SYS_VENDOR
, "DellInc."),
199 DMI_EXACT_MATCH(DMI_PRODUCT_NAME
, "Venue 8 Pro 5830"),
201 .driver_data
= (unsigned long *)(BYT_RT5640_DMIC2_MAP
|
205 .callback
= byt_rt5640_quirk_cb
,
207 DMI_EXACT_MATCH(DMI_SYS_VENDOR
, "Hewlett-Packard"),
208 DMI_EXACT_MATCH(DMI_PRODUCT_NAME
, "HP ElitePad 1000 G2"),
210 .driver_data
= (unsigned long *)BYT_RT5640_IN1_MAP
,
215 static int byt_rt5640_init(struct snd_soc_pcm_runtime
*runtime
)
218 struct snd_soc_codec
*codec
= runtime
->codec
;
219 struct snd_soc_card
*card
= runtime
->card
;
220 const struct snd_soc_dapm_route
*custom_map
;
223 card
->dapm
.idle_bias_off
= true;
225 rt5640_sel_asrc_clk_src(codec
,
226 RT5640_DA_STEREO_FILTER
|
227 RT5640_AD_STEREO_FILTER
,
228 RT5640_CLK_SEL_ASRC
);
230 ret
= snd_soc_add_card_controls(card
, byt_rt5640_controls
,
231 ARRAY_SIZE(byt_rt5640_controls
));
233 dev_err(card
->dev
, "unable to add card controls\n");
237 switch (BYT_RT5640_MAP(byt_rt5640_quirk
)) {
238 case BYT_RT5640_IN1_MAP
:
239 custom_map
= byt_rt5640_intmic_in1_map
;
240 num_routes
= ARRAY_SIZE(byt_rt5640_intmic_in1_map
);
242 case BYT_RT5640_DMIC2_MAP
:
243 custom_map
= byt_rt5640_intmic_dmic2_map
;
244 num_routes
= ARRAY_SIZE(byt_rt5640_intmic_dmic2_map
);
247 custom_map
= byt_rt5640_intmic_dmic1_map
;
248 num_routes
= ARRAY_SIZE(byt_rt5640_intmic_dmic1_map
);
251 ret
= snd_soc_dapm_add_routes(&card
->dapm
, custom_map
, num_routes
);
255 if (byt_rt5640_quirk
& BYT_RT5640_SSP2_AIF2
) {
256 ret
= snd_soc_dapm_add_routes(&card
->dapm
,
257 byt_rt5640_ssp2_aif2_map
,
258 ARRAY_SIZE(byt_rt5640_ssp2_aif2_map
));
259 } else if (byt_rt5640_quirk
& BYT_RT5640_SSP0_AIF1
) {
260 ret
= snd_soc_dapm_add_routes(&card
->dapm
,
261 byt_rt5640_ssp0_aif1_map
,
262 ARRAY_SIZE(byt_rt5640_ssp0_aif1_map
));
263 } else if (byt_rt5640_quirk
& BYT_RT5640_SSP0_AIF2
) {
264 ret
= snd_soc_dapm_add_routes(&card
->dapm
,
265 byt_rt5640_ssp0_aif2_map
,
266 ARRAY_SIZE(byt_rt5640_ssp0_aif2_map
));
268 ret
= snd_soc_dapm_add_routes(&card
->dapm
,
269 byt_rt5640_ssp2_aif1_map
,
270 ARRAY_SIZE(byt_rt5640_ssp2_aif1_map
));
275 if (byt_rt5640_quirk
& BYT_RT5640_MONO_SPEAKER
) {
276 ret
= snd_soc_dapm_add_routes(&card
->dapm
,
277 byt_rt5640_mono_spk_map
,
278 ARRAY_SIZE(byt_rt5640_mono_spk_map
));
280 ret
= snd_soc_dapm_add_routes(&card
->dapm
,
281 byt_rt5640_stereo_spk_map
,
282 ARRAY_SIZE(byt_rt5640_stereo_spk_map
));
287 if (byt_rt5640_quirk
& BYT_RT5640_DIFF_MIC
) {
288 snd_soc_update_bits(codec
, RT5640_IN1_IN2
, RT5640_IN_DF1
,
292 if (byt_rt5640_quirk
& BYT_RT5640_DMIC_EN
) {
293 ret
= rt5640_dmic_enable(codec
, 0, 0);
298 snd_soc_dapm_ignore_suspend(&card
->dapm
, "Headphone");
299 snd_soc_dapm_ignore_suspend(&card
->dapm
, "Speaker");
304 static const struct snd_soc_pcm_stream byt_rt5640_dai_params
= {
305 .formats
= SNDRV_PCM_FMTBIT_S24_LE
,
312 static int byt_rt5640_codec_fixup(struct snd_soc_pcm_runtime
*rtd
,
313 struct snd_pcm_hw_params
*params
)
315 struct snd_interval
*rate
= hw_param_interval(params
,
316 SNDRV_PCM_HW_PARAM_RATE
);
317 struct snd_interval
*channels
= hw_param_interval(params
,
318 SNDRV_PCM_HW_PARAM_CHANNELS
);
321 /* The DSP will covert the FE rate to 48k, stereo */
322 rate
->min
= rate
->max
= 48000;
323 channels
->min
= channels
->max
= 2;
325 if ((byt_rt5640_quirk
& BYT_RT5640_SSP0_AIF1
) ||
326 (byt_rt5640_quirk
& BYT_RT5640_SSP0_AIF2
)) {
328 /* set SSP2 to 16-bit */
329 params_set_format(params
, SNDRV_PCM_FORMAT_S16_LE
);
332 * Default mode for SSP configuration is TDM 4 slot, override config
333 * with explicit setting to I2S 2ch 16-bit. The word length is set with
334 * dai_set_tdm_slot() since there is no other API exposed
336 ret
= snd_soc_dai_set_fmt(rtd
->cpu_dai
,
338 SND_SOC_DAIFMT_NB_IF
|
339 SND_SOC_DAIFMT_CBS_CFS
342 dev_err(rtd
->dev
, "can't set format to I2S, err %d\n", ret
);
346 ret
= snd_soc_dai_set_tdm_slot(rtd
->cpu_dai
, 0x3, 0x3, 2, 16);
348 dev_err(rtd
->dev
, "can't set I2S config, err %d\n", ret
);
354 /* set SSP2 to 24-bit */
355 params_set_format(params
, SNDRV_PCM_FORMAT_S24_LE
);
358 * Default mode for SSP configuration is TDM 4 slot, override config
359 * with explicit setting to I2S 2ch 24-bit. The word length is set with
360 * dai_set_tdm_slot() since there is no other API exposed
362 ret
= snd_soc_dai_set_fmt(rtd
->cpu_dai
,
364 SND_SOC_DAIFMT_NB_IF
|
365 SND_SOC_DAIFMT_CBS_CFS
368 dev_err(rtd
->dev
, "can't set format to I2S, err %d\n", ret
);
372 ret
= snd_soc_dai_set_tdm_slot(rtd
->cpu_dai
, 0x3, 0x3, 2, 24);
374 dev_err(rtd
->dev
, "can't set I2S config, err %d\n", ret
);
381 static int byt_rt5640_aif1_startup(struct snd_pcm_substream
*substream
)
383 return snd_pcm_hw_constraint_single(substream
->runtime
,
384 SNDRV_PCM_HW_PARAM_RATE
, 48000);
387 static struct snd_soc_ops byt_rt5640_aif1_ops
= {
388 .startup
= byt_rt5640_aif1_startup
,
391 static struct snd_soc_ops byt_rt5640_be_ssp2_ops
= {
392 .hw_params
= byt_rt5640_aif1_hw_params
,
395 static struct snd_soc_dai_link byt_rt5640_dais
[] = {
396 [MERR_DPCM_AUDIO
] = {
397 .name
= "Baytrail Audio Port",
398 .stream_name
= "Baytrail Audio",
399 .cpu_dai_name
= "media-cpu-dai",
400 .codec_dai_name
= "snd-soc-dummy-dai",
401 .codec_name
= "snd-soc-dummy",
402 .platform_name
= "sst-mfld-platform",
407 .ops
= &byt_rt5640_aif1_ops
,
409 [MERR_DPCM_DEEP_BUFFER
] = {
410 .name
= "Deep-Buffer Audio Port",
411 .stream_name
= "Deep-Buffer Audio",
412 .cpu_dai_name
= "deepbuffer-cpu-dai",
413 .codec_dai_name
= "snd-soc-dummy-dai",
414 .codec_name
= "snd-soc-dummy",
415 .platform_name
= "sst-mfld-platform",
420 .ops
= &byt_rt5640_aif1_ops
,
422 [MERR_DPCM_COMPR
] = {
423 .name
= "Baytrail Compressed Port",
424 .stream_name
= "Baytrail Compress",
425 .cpu_dai_name
= "compress-cpu-dai",
426 .codec_dai_name
= "snd-soc-dummy-dai",
427 .codec_name
= "snd-soc-dummy",
428 .platform_name
= "sst-mfld-platform",
432 .name
= "SSP2-Codec",
434 .cpu_dai_name
= "ssp2-port", /* overwritten for ssp0 routing */
435 .platform_name
= "sst-mfld-platform",
437 .codec_dai_name
= "rt5640-aif1", /* changed w/ quirk */
438 .codec_name
= "i2c-10EC5640:00", /* overwritten with HID */
439 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
440 | SND_SOC_DAIFMT_CBS_CFS
,
441 .be_hw_params_fixup
= byt_rt5640_codec_fixup
,
445 .init
= byt_rt5640_init
,
446 .ops
= &byt_rt5640_be_ssp2_ops
,
451 static struct snd_soc_card byt_rt5640_card
= {
452 .name
= "bytcr-rt5640",
453 .owner
= THIS_MODULE
,
454 .dai_link
= byt_rt5640_dais
,
455 .num_links
= ARRAY_SIZE(byt_rt5640_dais
),
456 .dapm_widgets
= byt_rt5640_widgets
,
457 .num_dapm_widgets
= ARRAY_SIZE(byt_rt5640_widgets
),
458 .dapm_routes
= byt_rt5640_audio_map
,
459 .num_dapm_routes
= ARRAY_SIZE(byt_rt5640_audio_map
),
460 .fully_routed
= true,
463 static char byt_rt5640_codec_name
[16]; /* i2c-<HID>:00 with HID being 8 chars */
464 static char byt_rt5640_codec_aif_name
[12]; /* = "rt5640-aif[1|2]" */
465 static char byt_rt5640_cpu_dai_name
[10]; /* = "ssp[0|2]-port" */
467 static int snd_byt_rt5640_mc_probe(struct platform_device
*pdev
)
470 struct sst_acpi_mach
*mach
;
471 const char *i2c_name
= NULL
;
475 /* register the soc card */
476 byt_rt5640_card
.dev
= &pdev
->dev
;
477 mach
= byt_rt5640_card
.dev
->platform_data
;
479 /* fix index of codec dai */
480 dai_index
= MERR_DPCM_COMPR
+ 1;
481 for (i
= 0; i
< ARRAY_SIZE(byt_rt5640_dais
); i
++) {
482 if (!strcmp(byt_rt5640_dais
[i
].codec_name
, "i2c-10EC5640:00")) {
488 /* fixup codec name based on HID */
489 i2c_name
= sst_acpi_find_name_from_hid(mach
->id
);
490 if (i2c_name
!= NULL
) {
491 snprintf(byt_rt5640_codec_name
, sizeof(byt_rt5640_codec_name
),
492 "%s%s", "i2c-", i2c_name
);
494 byt_rt5640_dais
[dai_index
].codec_name
= byt_rt5640_codec_name
;
497 /* check quirks before creating card */
498 dmi_check_system(byt_rt5640_quirk_table
);
500 if ((byt_rt5640_quirk
& BYT_RT5640_SSP2_AIF2
) ||
501 (byt_rt5640_quirk
& BYT_RT5640_SSP0_AIF2
)) {
503 /* fixup codec aif name */
504 snprintf(byt_rt5640_codec_aif_name
,
505 sizeof(byt_rt5640_codec_aif_name
),
506 "%s", "rt5640-aif2");
508 byt_rt5640_dais
[dai_index
].codec_dai_name
=
509 byt_rt5640_codec_aif_name
;
512 if ((byt_rt5640_quirk
& BYT_RT5640_SSP0_AIF1
) ||
513 (byt_rt5640_quirk
& BYT_RT5640_SSP0_AIF2
)) {
515 /* fixup cpu dai name name */
516 snprintf(byt_rt5640_cpu_dai_name
,
517 sizeof(byt_rt5640_cpu_dai_name
),
520 byt_rt5640_dais
[dai_index
].cpu_dai_name
=
521 byt_rt5640_cpu_dai_name
;
524 ret_val
= devm_snd_soc_register_card(&pdev
->dev
, &byt_rt5640_card
);
527 dev_err(&pdev
->dev
, "devm_snd_soc_register_card failed %d\n",
531 platform_set_drvdata(pdev
, &byt_rt5640_card
);
535 static struct platform_driver snd_byt_rt5640_mc_driver
= {
537 .name
= "bytcr_rt5640",
538 .pm
= &snd_soc_pm_ops
,
540 .probe
= snd_byt_rt5640_mc_probe
,
543 module_platform_driver(snd_byt_rt5640_mc_driver
);
545 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
546 MODULE_AUTHOR("Subhransu S. Prusty <subhransu.s.prusty@intel.com>");
547 MODULE_LICENSE("GPL v2");
548 MODULE_ALIAS("platform:bytcr_rt5640");