2 * cht_bsw_rt5672.c - ASoc Machine driver for Intel Cherryview-based platforms
3 * Cherrytrail and Braswell, with RT5672 codec.
5 * Copyright (C) 2014 Intel Corp
6 * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
7 * Mengdong Lin <mengdong.lin@intel.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include "../codecs/rt5670.h"
26 #include "sst-atom-controls.h"
28 /* The platform clock #3 outputs 19.2Mhz clock to codec as I2S MCLK */
29 #define CHT_PLAT_CLK_3_HZ 19200000
30 #define CHT_CODEC_DAI "rt5670-aif1"
32 static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
36 for (i = 0; i < card->num_rtd; i++) {
37 struct snd_soc_pcm_runtime *rtd;
40 if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
41 strlen(CHT_CODEC_DAI)))
42 return rtd->codec_dai;
47 static int platform_clock_control(struct snd_soc_dapm_widget *w,
48 struct snd_kcontrol *k, int event)
50 struct snd_soc_dapm_context *dapm = w->dapm;
51 struct snd_soc_card *card = dapm->card;
52 struct snd_soc_dai *codec_dai;
54 codec_dai = cht_get_codec_dai(card);
56 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
60 if (!SND_SOC_DAPM_EVENT_OFF(event))
63 /* Set codec sysclk source to its internal clock because codec PLL will
64 * be off when idle and MCLK will also be off by ACPI when codec is
65 * runtime suspended. Codec needs clock for jack detection and button
68 snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_RCCLK,
74 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
75 SND_SOC_DAPM_HP("Headphone", NULL),
76 SND_SOC_DAPM_MIC("Headset Mic", NULL),
77 SND_SOC_DAPM_MIC("Int Mic", NULL),
78 SND_SOC_DAPM_SPK("Ext Spk", NULL),
79 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
80 platform_clock_control, SND_SOC_DAPM_POST_PMD),
83 static const struct snd_soc_dapm_route cht_audio_map[] = {
84 {"IN1P", NULL, "Headset Mic"},
85 {"IN1N", NULL, "Headset Mic"},
86 {"DMIC L1", NULL, "Int Mic"},
87 {"DMIC R1", NULL, "Int Mic"},
88 {"Headphone", NULL, "HPOL"},
89 {"Headphone", NULL, "HPOR"},
90 {"Ext Spk", NULL, "SPOLP"},
91 {"Ext Spk", NULL, "SPOLN"},
92 {"Ext Spk", NULL, "SPORP"},
93 {"Ext Spk", NULL, "SPORN"},
94 {"AIF1 Playback", NULL, "ssp2 Tx"},
95 {"ssp2 Tx", NULL, "codec_out0"},
96 {"ssp2 Tx", NULL, "codec_out1"},
97 {"codec_in0", NULL, "ssp2 Rx"},
98 {"codec_in1", NULL, "ssp2 Rx"},
99 {"ssp2 Rx", NULL, "AIF1 Capture"},
100 {"Headphone", NULL, "Platform Clock"},
101 {"Headset Mic", NULL, "Platform Clock"},
102 {"Int Mic", NULL, "Platform Clock"},
103 {"Ext Spk", NULL, "Platform Clock"},
106 static const struct snd_kcontrol_new cht_mc_controls[] = {
107 SOC_DAPM_PIN_SWITCH("Headphone"),
108 SOC_DAPM_PIN_SWITCH("Headset Mic"),
109 SOC_DAPM_PIN_SWITCH("Int Mic"),
110 SOC_DAPM_PIN_SWITCH("Ext Spk"),
113 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
114 struct snd_pcm_hw_params *params)
116 struct snd_soc_pcm_runtime *rtd = substream->private_data;
117 struct snd_soc_dai *codec_dai = rtd->codec_dai;
120 /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
121 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK,
122 CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
124 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
128 /* set codec sysclk source to PLL */
129 ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1,
130 params_rate(params) * 512,
133 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
139 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
142 struct snd_soc_dai *codec_dai = runtime->codec_dai;
143 struct snd_soc_codec *codec = codec_dai->codec;
145 /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
146 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
148 dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
152 /* Select codec ASRC clock source to track I2S1 clock, because codec
153 * is in slave mode and 100fs I2S format (BCLK = 100 * LRCLK) cannot
154 * be supported by RT5672. Otherwise, ASRC will be disabled and cause
157 rt5670_sel_asrc_clk_src(codec,
158 RT5670_DA_STEREO_FILTER
159 | RT5670_DA_MONO_L_FILTER
160 | RT5670_DA_MONO_R_FILTER
161 | RT5670_AD_STEREO_FILTER
162 | RT5670_AD_MONO_L_FILTER
163 | RT5670_AD_MONO_R_FILTER,
164 RT5670_CLK_SEL_I2S1_ASRC);
168 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
169 struct snd_pcm_hw_params *params)
171 struct snd_interval *rate = hw_param_interval(params,
172 SNDRV_PCM_HW_PARAM_RATE);
173 struct snd_interval *channels = hw_param_interval(params,
174 SNDRV_PCM_HW_PARAM_CHANNELS);
176 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
177 rate->min = rate->max = 48000;
178 channels->min = channels->max = 2;
180 /* set SSP2 to 24-bit */
181 snd_mask_set(¶ms->masks[SNDRV_PCM_HW_PARAM_FORMAT -
182 SNDRV_PCM_HW_PARAM_FIRST_MASK],
183 SNDRV_PCM_FORMAT_S24_LE);
187 static unsigned int rates_48000[] = {
191 static struct snd_pcm_hw_constraint_list constraints_48000 = {
192 .count = ARRAY_SIZE(rates_48000),
196 static int cht_aif1_startup(struct snd_pcm_substream *substream)
198 return snd_pcm_hw_constraint_list(substream->runtime, 0,
199 SNDRV_PCM_HW_PARAM_RATE,
203 static struct snd_soc_ops cht_aif1_ops = {
204 .startup = cht_aif1_startup,
207 static struct snd_soc_ops cht_be_ssp2_ops = {
208 .hw_params = cht_aif1_hw_params,
211 static struct snd_soc_dai_link cht_dailink[] = {
212 /* Front End DAI links */
213 [MERR_DPCM_AUDIO] = {
214 .name = "Audio Port",
215 .stream_name = "Audio",
216 .cpu_dai_name = "media-cpu-dai",
217 .codec_dai_name = "snd-soc-dummy-dai",
218 .codec_name = "snd-soc-dummy",
219 .platform_name = "sst-mfld-platform",
224 .ops = &cht_aif1_ops,
226 [MERR_DPCM_COMPR] = {
227 .name = "Compressed Port",
228 .stream_name = "Compress",
229 .cpu_dai_name = "compress-cpu-dai",
230 .codec_dai_name = "snd-soc-dummy-dai",
231 .codec_name = "snd-soc-dummy",
232 .platform_name = "sst-mfld-platform",
235 /* Back End DAI links */
238 .name = "SSP2-Codec",
240 .cpu_dai_name = "ssp2-port",
241 .platform_name = "sst-mfld-platform",
243 .codec_dai_name = "rt5670-aif1",
244 .codec_name = "i2c-10EC5670:00",
245 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
246 | SND_SOC_DAIFMT_CBS_CFS,
247 .init = cht_codec_init,
248 .be_hw_params_fixup = cht_codec_fixup,
252 .ops = &cht_be_ssp2_ops,
257 static struct snd_soc_card snd_soc_card_cht = {
258 .name = "cherrytrailcraudio",
259 .dai_link = cht_dailink,
260 .num_links = ARRAY_SIZE(cht_dailink),
261 .dapm_widgets = cht_dapm_widgets,
262 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
263 .dapm_routes = cht_audio_map,
264 .num_dapm_routes = ARRAY_SIZE(cht_audio_map),
265 .controls = cht_mc_controls,
266 .num_controls = ARRAY_SIZE(cht_mc_controls),
269 static int snd_cht_mc_probe(struct platform_device *pdev)
273 /* register the soc card */
274 snd_soc_card_cht.dev = &pdev->dev;
275 ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht);
278 "snd_soc_register_card failed %d\n", ret_val);
281 platform_set_drvdata(pdev, &snd_soc_card_cht);
285 static struct platform_driver snd_cht_mc_driver = {
287 .name = "cht-bsw-rt5672",
288 .pm = &snd_soc_pm_ops,
290 .probe = snd_cht_mc_probe,
293 module_platform_driver(snd_cht_mc_driver);
295 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
296 MODULE_AUTHOR("Subhransu S. Prusty, Mengdong Lin");
297 MODULE_LICENSE("GPL v2");
298 MODULE_ALIAS("platform:cht-bsw-rt5672");