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
;
144 /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
145 ret
= snd_soc_dai_set_tdm_slot(codec_dai
, 0xF, 0xF, 4, 24);
147 dev_err(runtime
->dev
, "can't set codec TDM slot %d\n", ret
);
154 static int cht_codec_fixup(struct snd_soc_pcm_runtime
*rtd
,
155 struct snd_pcm_hw_params
*params
)
157 struct snd_interval
*rate
= hw_param_interval(params
,
158 SNDRV_PCM_HW_PARAM_RATE
);
159 struct snd_interval
*channels
= hw_param_interval(params
,
160 SNDRV_PCM_HW_PARAM_CHANNELS
);
162 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
163 rate
->min
= rate
->max
= 48000;
164 channels
->min
= channels
->max
= 2;
166 /* set SSP2 to 24-bit */
167 snd_mask_set(¶ms
->masks
[SNDRV_PCM_HW_PARAM_FORMAT
-
168 SNDRV_PCM_HW_PARAM_FIRST_MASK
],
169 SNDRV_PCM_FORMAT_S24_LE
);
173 static unsigned int rates_48000
[] = {
177 static struct snd_pcm_hw_constraint_list constraints_48000
= {
178 .count
= ARRAY_SIZE(rates_48000
),
182 static int cht_aif1_startup(struct snd_pcm_substream
*substream
)
184 return snd_pcm_hw_constraint_list(substream
->runtime
, 0,
185 SNDRV_PCM_HW_PARAM_RATE
,
189 static struct snd_soc_ops cht_aif1_ops
= {
190 .startup
= cht_aif1_startup
,
193 static struct snd_soc_ops cht_be_ssp2_ops
= {
194 .hw_params
= cht_aif1_hw_params
,
197 static struct snd_soc_dai_link cht_dailink
[] = {
198 /* Front End DAI links */
199 [MERR_DPCM_AUDIO
] = {
200 .name
= "Audio Port",
201 .stream_name
= "Audio",
202 .cpu_dai_name
= "media-cpu-dai",
203 .codec_dai_name
= "snd-soc-dummy-dai",
204 .codec_name
= "snd-soc-dummy",
205 .platform_name
= "sst-mfld-platform",
210 .ops
= &cht_aif1_ops
,
212 [MERR_DPCM_COMPR
] = {
213 .name
= "Compressed Port",
214 .stream_name
= "Compress",
215 .cpu_dai_name
= "compress-cpu-dai",
216 .codec_dai_name
= "snd-soc-dummy-dai",
217 .codec_name
= "snd-soc-dummy",
218 .platform_name
= "sst-mfld-platform",
221 /* Back End DAI links */
224 .name
= "SSP2-Codec",
226 .cpu_dai_name
= "ssp2-port",
227 .platform_name
= "sst-mfld-platform",
229 .codec_dai_name
= "rt5670-aif1",
230 .codec_name
= "i2c-10EC5670:00",
231 .dai_fmt
= SND_SOC_DAIFMT_DSP_B
| SND_SOC_DAIFMT_IB_NF
232 | SND_SOC_DAIFMT_CBS_CFS
,
233 .init
= cht_codec_init
,
234 .be_hw_params_fixup
= cht_codec_fixup
,
238 .ops
= &cht_be_ssp2_ops
,
243 static struct snd_soc_card snd_soc_card_cht
= {
244 .name
= "cherrytrailcraudio",
245 .dai_link
= cht_dailink
,
246 .num_links
= ARRAY_SIZE(cht_dailink
),
247 .dapm_widgets
= cht_dapm_widgets
,
248 .num_dapm_widgets
= ARRAY_SIZE(cht_dapm_widgets
),
249 .dapm_routes
= cht_audio_map
,
250 .num_dapm_routes
= ARRAY_SIZE(cht_audio_map
),
251 .controls
= cht_mc_controls
,
252 .num_controls
= ARRAY_SIZE(cht_mc_controls
),
255 static int snd_cht_mc_probe(struct platform_device
*pdev
)
259 /* register the soc card */
260 snd_soc_card_cht
.dev
= &pdev
->dev
;
261 ret_val
= devm_snd_soc_register_card(&pdev
->dev
, &snd_soc_card_cht
);
264 "snd_soc_register_card failed %d\n", ret_val
);
267 platform_set_drvdata(pdev
, &snd_soc_card_cht
);
271 static struct platform_driver snd_cht_mc_driver
= {
273 .owner
= THIS_MODULE
,
274 .name
= "cht-bsw-rt5672",
275 .pm
= &snd_soc_pm_ops
,
277 .probe
= snd_cht_mc_probe
,
280 module_platform_driver(snd_cht_mc_driver
);
282 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver");
283 MODULE_AUTHOR("Subhransu S. Prusty, Mengdong Lin");
284 MODULE_LICENSE("GPL v2");
285 MODULE_ALIAS("platform:cht-bsw-rt5672");