2 * bytcr_rt5651.c - ASoc Machine driver for Intel Byt CR platform
3 * (derived from bytcr_rt5640.c)
5 * Copyright (C) 2015 Intel Corp
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/clk.h>
25 #include <linux/device.h>
26 #include <linux/dmi.h>
27 #include <linux/slab.h>
28 #include <asm/platform_sst_audio.h>
29 #include <sound/pcm.h>
30 #include <sound/pcm_params.h>
31 #include <sound/soc.h>
32 #include <sound/jack.h>
33 #include <sound/soc-acpi.h>
34 #include "../../codecs/rt5651.h"
35 #include "../atom/sst-atom-controls.h"
43 #define BYT_RT5651_MAP(quirk) ((quirk) & GENMASK(7, 0))
44 #define BYT_RT5651_DMIC_EN BIT(16)
45 #define BYT_RT5651_MCLK_EN BIT(17)
46 #define BYT_RT5651_MCLK_25MHZ BIT(18)
48 struct byt_rt5651_private
{
50 struct snd_soc_jack jack
;
53 static unsigned long byt_rt5651_quirk
= BYT_RT5651_DMIC_MAP
|
57 static void log_quirks(struct device
*dev
)
59 if (BYT_RT5651_MAP(byt_rt5651_quirk
) == BYT_RT5651_DMIC_MAP
)
60 dev_info(dev
, "quirk DMIC_MAP enabled");
61 if (BYT_RT5651_MAP(byt_rt5651_quirk
) == BYT_RT5651_IN1_MAP
)
62 dev_info(dev
, "quirk IN1_MAP enabled");
63 if (BYT_RT5651_MAP(byt_rt5651_quirk
) == BYT_RT5651_IN2_MAP
)
64 dev_info(dev
, "quirk IN2_MAP enabled");
65 if (byt_rt5651_quirk
& BYT_RT5651_DMIC_EN
)
66 dev_info(dev
, "quirk DMIC enabled");
67 if (byt_rt5651_quirk
& BYT_RT5651_MCLK_EN
)
68 dev_info(dev
, "quirk MCLK_EN enabled");
69 if (byt_rt5651_quirk
& BYT_RT5651_MCLK_25MHZ
)
70 dev_info(dev
, "quirk MCLK_25MHZ enabled");
73 #define BYT_CODEC_DAI1 "rt5651-aif1"
75 static int platform_clock_control(struct snd_soc_dapm_widget
*w
,
76 struct snd_kcontrol
*k
, int event
)
78 struct snd_soc_dapm_context
*dapm
= w
->dapm
;
79 struct snd_soc_card
*card
= dapm
->card
;
80 struct snd_soc_dai
*codec_dai
;
81 struct byt_rt5651_private
*priv
= snd_soc_card_get_drvdata(card
);
84 codec_dai
= snd_soc_card_get_codec_dai(card
, BYT_CODEC_DAI1
);
87 "Codec dai not found; Unable to set platform clock\n");
91 if (SND_SOC_DAPM_EVENT_ON(event
)) {
92 if (byt_rt5651_quirk
& BYT_RT5651_MCLK_EN
) {
93 ret
= clk_prepare_enable(priv
->mclk
);
96 "could not configure MCLK state");
100 ret
= snd_soc_dai_set_sysclk(codec_dai
, RT5651_SCLK_S_PLL1
,
105 * Set codec clock source to internal clock before
106 * turning off the platform clock. Codec needs clock
107 * for Jack detection and button press
109 ret
= snd_soc_dai_set_sysclk(codec_dai
, RT5651_SCLK_S_RCCLK
,
113 if (byt_rt5651_quirk
& BYT_RT5651_MCLK_EN
)
114 clk_disable_unprepare(priv
->mclk
);
118 dev_err(card
->dev
, "can't set codec sysclk: %d\n", ret
);
125 static const struct snd_soc_dapm_widget byt_rt5651_widgets
[] = {
126 SND_SOC_DAPM_HP("Headphone", NULL
),
127 SND_SOC_DAPM_MIC("Headset Mic", NULL
),
128 SND_SOC_DAPM_MIC("Internal Mic", NULL
),
129 SND_SOC_DAPM_SPK("Speaker", NULL
),
130 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM
, 0, 0,
131 platform_clock_control
, SND_SOC_DAPM_PRE_PMU
|
132 SND_SOC_DAPM_POST_PMD
),
136 static const struct snd_soc_dapm_route byt_rt5651_audio_map
[] = {
137 {"Headphone", NULL
, "Platform Clock"},
138 {"Headset Mic", NULL
, "Platform Clock"},
139 {"Internal Mic", NULL
, "Platform Clock"},
140 {"Speaker", NULL
, "Platform Clock"},
142 {"AIF1 Playback", NULL
, "ssp2 Tx"},
143 {"ssp2 Tx", NULL
, "codec_out0"},
144 {"ssp2 Tx", NULL
, "codec_out1"},
145 {"codec_in0", NULL
, "ssp2 Rx"},
146 {"codec_in1", NULL
, "ssp2 Rx"},
147 {"ssp2 Rx", NULL
, "AIF1 Capture"},
149 {"Headset Mic", NULL
, "micbias1"}, /* lowercase for rt5651 */
150 {"Headphone", NULL
, "HPOL"},
151 {"Headphone", NULL
, "HPOR"},
152 {"Speaker", NULL
, "LOUTL"},
153 {"Speaker", NULL
, "LOUTR"},
156 static const struct snd_soc_dapm_route byt_rt5651_intmic_dmic_map
[] = {
157 {"IN2P", NULL
, "Headset Mic"},
158 {"DMIC L1", NULL
, "Internal Mic"},
159 {"DMIC R1", NULL
, "Internal Mic"},
162 static const struct snd_soc_dapm_route byt_rt5651_intmic_in1_map
[] = {
163 {"Internal Mic", NULL
, "micbias1"},
164 {"IN2P", NULL
, "Headset Mic"},
165 {"IN1P", NULL
, "Internal Mic"},
168 static const struct snd_soc_dapm_route byt_rt5651_intmic_in2_map
[] = {
169 {"Internal Mic", NULL
, "micbias1"},
170 {"IN1P", NULL
, "Headset Mic"},
171 {"IN2P", NULL
, "Internal Mic"},
174 static const struct snd_kcontrol_new byt_rt5651_controls
[] = {
175 SOC_DAPM_PIN_SWITCH("Headphone"),
176 SOC_DAPM_PIN_SWITCH("Headset Mic"),
177 SOC_DAPM_PIN_SWITCH("Internal Mic"),
178 SOC_DAPM_PIN_SWITCH("Speaker"),
181 static struct snd_soc_jack_pin bytcr_jack_pins
[] = {
184 .mask
= SND_JACK_HEADPHONE
,
187 .pin
= "Headset Mic",
188 .mask
= SND_JACK_MICROPHONE
,
192 static int byt_rt5651_aif1_hw_params(struct snd_pcm_substream
*substream
,
193 struct snd_pcm_hw_params
*params
)
195 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
196 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
199 snd_soc_dai_set_bclk_ratio(codec_dai
, 50);
201 ret
= snd_soc_dai_set_sysclk(codec_dai
, RT5651_SCLK_S_PLL1
,
202 params_rate(params
) * 512,
205 dev_err(rtd
->dev
, "can't set codec clock %d\n", ret
);
209 if (!(byt_rt5651_quirk
& BYT_RT5651_MCLK_EN
)) {
210 /* 2x25 bit slots on SSP2 */
211 ret
= snd_soc_dai_set_pll(codec_dai
, 0,
213 params_rate(params
) * 50,
214 params_rate(params
) * 512);
216 if (byt_rt5651_quirk
& BYT_RT5651_MCLK_25MHZ
) {
217 ret
= snd_soc_dai_set_pll(codec_dai
, 0,
220 params_rate(params
) * 512);
222 ret
= snd_soc_dai_set_pll(codec_dai
, 0,
225 params_rate(params
) * 512);
230 dev_err(rtd
->dev
, "can't set codec pll: %d\n", ret
);
237 static int byt_rt5651_quirk_cb(const struct dmi_system_id
*id
)
239 byt_rt5651_quirk
= (unsigned long)id
->driver_data
;
243 static const struct dmi_system_id byt_rt5651_quirk_table
[] = {
245 .callback
= byt_rt5651_quirk_cb
,
247 DMI_MATCH(DMI_SYS_VENDOR
, "Circuitco"),
248 DMI_MATCH(DMI_PRODUCT_NAME
, "Minnowboard Max B3 PLATFORM"),
250 .driver_data
= (void *)(BYT_RT5651_DMIC_MAP
|
254 .callback
= byt_rt5651_quirk_cb
,
256 DMI_MATCH(DMI_SYS_VENDOR
, "KIANO"),
257 DMI_MATCH(DMI_PRODUCT_NAME
, "KIANO SlimNote 14.2"),
259 .driver_data
= (void *)(BYT_RT5651_IN2_MAP
),
264 static int byt_rt5651_init(struct snd_soc_pcm_runtime
*runtime
)
266 struct snd_soc_card
*card
= runtime
->card
;
267 struct snd_soc_codec
*codec
= runtime
->codec
;
268 struct byt_rt5651_private
*priv
= snd_soc_card_get_drvdata(card
);
269 const struct snd_soc_dapm_route
*custom_map
;
273 card
->dapm
.idle_bias_off
= true;
275 switch (BYT_RT5651_MAP(byt_rt5651_quirk
)) {
276 case BYT_RT5651_IN1_MAP
:
277 custom_map
= byt_rt5651_intmic_in1_map
;
278 num_routes
= ARRAY_SIZE(byt_rt5651_intmic_in1_map
);
280 case BYT_RT5651_IN2_MAP
:
281 custom_map
= byt_rt5651_intmic_in2_map
;
282 num_routes
= ARRAY_SIZE(byt_rt5651_intmic_in2_map
);
285 custom_map
= byt_rt5651_intmic_dmic_map
;
286 num_routes
= ARRAY_SIZE(byt_rt5651_intmic_dmic_map
);
288 ret
= snd_soc_dapm_add_routes(&card
->dapm
, custom_map
, num_routes
);
292 ret
= snd_soc_add_card_controls(card
, byt_rt5651_controls
,
293 ARRAY_SIZE(byt_rt5651_controls
));
295 dev_err(card
->dev
, "unable to add card controls\n");
298 snd_soc_dapm_ignore_suspend(&card
->dapm
, "Headphone");
299 snd_soc_dapm_ignore_suspend(&card
->dapm
, "Speaker");
301 if (byt_rt5651_quirk
& BYT_RT5651_MCLK_EN
) {
303 * The firmware might enable the clock at
304 * boot (this information may or may not
305 * be reflected in the enable clock register).
306 * To change the rate we must disable the clock
307 * first to cover these cases. Due to common
308 * clock framework restrictions that do not allow
309 * to disable a clock that has not been enabled,
310 * we need to enable the clock first.
312 ret
= clk_prepare_enable(priv
->mclk
);
314 clk_disable_unprepare(priv
->mclk
);
316 if (byt_rt5651_quirk
& BYT_RT5651_MCLK_25MHZ
)
317 ret
= clk_set_rate(priv
->mclk
, 25000000);
319 ret
= clk_set_rate(priv
->mclk
, 19200000);
322 dev_err(card
->dev
, "unable to set MCLK rate\n");
325 ret
= snd_soc_card_jack_new(runtime
->card
, "Headset",
326 SND_JACK_HEADSET
, &priv
->jack
,
327 bytcr_jack_pins
, ARRAY_SIZE(bytcr_jack_pins
));
329 dev_err(runtime
->dev
, "Headset jack creation failed %d\n", ret
);
333 rt5651_set_jack_detect(codec
, &priv
->jack
);
338 static const struct snd_soc_pcm_stream byt_rt5651_dai_params
= {
339 .formats
= SNDRV_PCM_FMTBIT_S24_LE
,
346 static int byt_rt5651_codec_fixup(struct snd_soc_pcm_runtime
*rtd
,
347 struct snd_pcm_hw_params
*params
)
349 struct snd_interval
*rate
= hw_param_interval(params
,
350 SNDRV_PCM_HW_PARAM_RATE
);
351 struct snd_interval
*channels
= hw_param_interval(params
,
352 SNDRV_PCM_HW_PARAM_CHANNELS
);
355 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
356 rate
->min
= rate
->max
= 48000;
357 channels
->min
= channels
->max
= 2;
359 /* set SSP2 to 24-bit */
360 params_set_format(params
, SNDRV_PCM_FORMAT_S24_LE
);
363 * Default mode for SSP configuration is TDM 4 slot, override config
364 * with explicit setting to I2S 2ch 24-bit. The word length is set with
365 * dai_set_tdm_slot() since there is no other API exposed
367 ret
= snd_soc_dai_set_fmt(rtd
->cpu_dai
,
369 SND_SOC_DAIFMT_NB_NF
|
370 SND_SOC_DAIFMT_CBS_CFS
374 dev_err(rtd
->dev
, "can't set format to I2S, err %d\n", ret
);
378 ret
= snd_soc_dai_set_tdm_slot(rtd
->cpu_dai
, 0x3, 0x3, 2, 24);
380 dev_err(rtd
->dev
, "can't set I2S config, err %d\n", ret
);
387 static const unsigned int rates_48000
[] = {
391 static const struct snd_pcm_hw_constraint_list constraints_48000
= {
392 .count
= ARRAY_SIZE(rates_48000
),
396 static int byt_rt5651_aif1_startup(struct snd_pcm_substream
*substream
)
398 return snd_pcm_hw_constraint_list(substream
->runtime
, 0,
399 SNDRV_PCM_HW_PARAM_RATE
,
403 static const struct snd_soc_ops byt_rt5651_aif1_ops
= {
404 .startup
= byt_rt5651_aif1_startup
,
407 static const struct snd_soc_ops byt_rt5651_be_ssp2_ops
= {
408 .hw_params
= byt_rt5651_aif1_hw_params
,
411 static struct snd_soc_dai_link byt_rt5651_dais
[] = {
412 [MERR_DPCM_AUDIO
] = {
413 .name
= "Audio Port",
414 .stream_name
= "Audio",
415 .cpu_dai_name
= "media-cpu-dai",
416 .codec_dai_name
= "snd-soc-dummy-dai",
417 .codec_name
= "snd-soc-dummy",
418 .platform_name
= "sst-mfld-platform",
423 .ops
= &byt_rt5651_aif1_ops
,
425 [MERR_DPCM_DEEP_BUFFER
] = {
426 .name
= "Deep-Buffer Audio Port",
427 .stream_name
= "Deep-Buffer Audio",
428 .cpu_dai_name
= "deepbuffer-cpu-dai",
429 .codec_dai_name
= "snd-soc-dummy-dai",
430 .codec_name
= "snd-soc-dummy",
431 .platform_name
= "sst-mfld-platform",
435 .ops
= &byt_rt5651_aif1_ops
,
437 /* CODEC<->CODEC link */
440 .name
= "SSP2-Codec",
442 .cpu_dai_name
= "ssp2-port",
443 .platform_name
= "sst-mfld-platform",
445 .codec_dai_name
= "rt5651-aif1",
446 .codec_name
= "i2c-10EC5651:00",
447 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
448 | SND_SOC_DAIFMT_CBS_CFS
,
449 .be_hw_params_fixup
= byt_rt5651_codec_fixup
,
454 .init
= byt_rt5651_init
,
455 .ops
= &byt_rt5651_be_ssp2_ops
,
460 static struct snd_soc_card byt_rt5651_card
= {
461 .name
= "bytcr-rt5651",
462 .owner
= THIS_MODULE
,
463 .dai_link
= byt_rt5651_dais
,
464 .num_links
= ARRAY_SIZE(byt_rt5651_dais
),
465 .dapm_widgets
= byt_rt5651_widgets
,
466 .num_dapm_widgets
= ARRAY_SIZE(byt_rt5651_widgets
),
467 .dapm_routes
= byt_rt5651_audio_map
,
468 .num_dapm_routes
= ARRAY_SIZE(byt_rt5651_audio_map
),
469 .fully_routed
= true,
472 static char byt_rt5651_codec_name
[16]; /* i2c-<HID>:00 with HID being 8 chars */
474 static int snd_byt_rt5651_mc_probe(struct platform_device
*pdev
)
476 struct byt_rt5651_private
*priv
;
477 struct snd_soc_acpi_mach
*mach
;
478 const char *i2c_name
= NULL
;
483 priv
= devm_kzalloc(&pdev
->dev
, sizeof(*priv
), GFP_ATOMIC
);
487 /* register the soc card */
488 byt_rt5651_card
.dev
= &pdev
->dev
;
490 mach
= byt_rt5651_card
.dev
->platform_data
;
491 snd_soc_card_set_drvdata(&byt_rt5651_card
, priv
);
493 /* fix index of codec dai */
494 for (i
= 0; i
< ARRAY_SIZE(byt_rt5651_dais
); i
++) {
495 if (!strcmp(byt_rt5651_dais
[i
].codec_name
, "i2c-10EC5651:00")) {
501 /* fixup codec name based on HID */
502 i2c_name
= snd_soc_acpi_find_name_from_hid(mach
->id
);
504 snprintf(byt_rt5651_codec_name
, sizeof(byt_rt5651_codec_name
),
505 "%s%s", "i2c-", i2c_name
);
507 byt_rt5651_dais
[dai_index
].codec_name
= byt_rt5651_codec_name
;
510 /* check quirks before creating card */
511 dmi_check_system(byt_rt5651_quirk_table
);
512 log_quirks(&pdev
->dev
);
514 if (byt_rt5651_quirk
& BYT_RT5651_MCLK_EN
) {
515 priv
->mclk
= devm_clk_get(&pdev
->dev
, "pmc_plt_clk_3");
516 if (IS_ERR(priv
->mclk
)) {
518 "Failed to get MCLK from pmc_plt_clk_3: %ld\n",
519 PTR_ERR(priv
->mclk
));
521 * Fall back to bit clock usage for -ENOENT (clock not
522 * available likely due to missing dependencies), bail
523 * for all other errors, including -EPROBE_DEFER
525 if (ret_val
!= -ENOENT
)
527 byt_rt5651_quirk
&= ~BYT_RT5651_MCLK_EN
;
531 ret_val
= devm_snd_soc_register_card(&pdev
->dev
, &byt_rt5651_card
);
534 dev_err(&pdev
->dev
, "devm_snd_soc_register_card failed %d\n",
538 platform_set_drvdata(pdev
, &byt_rt5651_card
);
542 static struct platform_driver snd_byt_rt5651_mc_driver
= {
544 .name
= "bytcr_rt5651",
546 .probe
= snd_byt_rt5651_mc_probe
,
549 module_platform_driver(snd_byt_rt5651_mc_driver
);
551 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver for RT5651");
552 MODULE_AUTHOR("Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>");
553 MODULE_LICENSE("GPL v2");
554 MODULE_ALIAS("platform:bytcr_rt5651");