2 * cht-bsw-rt5645.c - ASoc Machine driver for Intel Cherryview-based platforms
3 * Cherrytrail and Braswell, with RT5645 codec.
5 * Copyright (C) 2015 Intel Corp
6 * Author: Fang, Yang A <yang.a.fang@intel.com>
7 * N,Harshapriya <harshapriya.n@intel.com>
8 * This file is modified from cht_bsw_rt5672.c
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23 #include <linux/module.h>
24 #include <linux/acpi.h>
25 #include <linux/platform_device.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/rt5645.h"
32 #include "../atom/sst-atom-controls.h"
34 #define CHT_PLAT_CLK_3_HZ 19200000
35 #define CHT_CODEC_DAI "rt5645-aif1"
37 struct cht_acpi_card {
40 struct snd_soc_card *soc_card;
43 struct cht_mc_private {
44 struct snd_soc_jack jack;
45 struct cht_acpi_card *acpi_card;
48 static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
52 for (i = 0; i < card->num_rtd; i++) {
53 struct snd_soc_pcm_runtime *rtd;
56 if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
57 strlen(CHT_CODEC_DAI)))
58 return rtd->codec_dai;
63 static int platform_clock_control(struct snd_soc_dapm_widget *w,
64 struct snd_kcontrol *k, int event)
66 struct snd_soc_dapm_context *dapm = w->dapm;
67 struct snd_soc_card *card = dapm->card;
68 struct snd_soc_dai *codec_dai;
71 codec_dai = cht_get_codec_dai(card);
73 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
77 if (!SND_SOC_DAPM_EVENT_OFF(event))
80 /* Set codec sysclk source to its internal clock because codec PLL will
81 * be off when idle and MCLK will also be off by ACPI when codec is
82 * runtime suspended. Codec needs clock for jack detection and button
85 ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_RCCLK,
88 dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
95 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
96 SND_SOC_DAPM_HP("Headphone", NULL),
97 SND_SOC_DAPM_MIC("Headset Mic", NULL),
98 SND_SOC_DAPM_MIC("Int Mic", NULL),
99 SND_SOC_DAPM_MIC("Int Analog Mic", NULL),
100 SND_SOC_DAPM_SPK("Ext Spk", NULL),
101 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
102 platform_clock_control, SND_SOC_DAPM_POST_PMD),
105 static const struct snd_soc_dapm_route cht_rt5645_audio_map[] = {
106 {"IN1P", NULL, "Headset Mic"},
107 {"IN1N", NULL, "Headset Mic"},
108 {"DMIC L1", NULL, "Int Mic"},
109 {"DMIC R1", NULL, "Int Mic"},
110 {"IN2P", NULL, "Int Analog Mic"},
111 {"IN2N", NULL, "Int Analog Mic"},
112 {"Headphone", NULL, "HPOL"},
113 {"Headphone", NULL, "HPOR"},
114 {"Ext Spk", NULL, "SPOL"},
115 {"Ext Spk", NULL, "SPOR"},
116 {"AIF1 Playback", NULL, "ssp2 Tx"},
117 {"ssp2 Tx", NULL, "codec_out0"},
118 {"ssp2 Tx", NULL, "codec_out1"},
119 {"codec_in0", NULL, "ssp2 Rx" },
120 {"codec_in1", NULL, "ssp2 Rx" },
121 {"ssp2 Rx", NULL, "AIF1 Capture"},
122 {"Headphone", NULL, "Platform Clock"},
123 {"Headset Mic", NULL, "Platform Clock"},
124 {"Int Mic", NULL, "Platform Clock"},
125 {"Int Analog Mic", NULL, "Platform Clock"},
126 {"Int Analog Mic", NULL, "micbias1"},
127 {"Int Analog Mic", NULL, "micbias2"},
128 {"Ext Spk", NULL, "Platform Clock"},
131 static const struct snd_soc_dapm_route cht_rt5650_audio_map[] = {
132 {"IN1P", NULL, "Headset Mic"},
133 {"IN1N", NULL, "Headset Mic"},
134 {"DMIC L2", NULL, "Int Mic"},
135 {"DMIC R2", NULL, "Int Mic"},
136 {"Headphone", NULL, "HPOL"},
137 {"Headphone", NULL, "HPOR"},
138 {"Ext Spk", NULL, "SPOL"},
139 {"Ext Spk", NULL, "SPOR"},
140 {"AIF1 Playback", NULL, "ssp2 Tx"},
141 {"ssp2 Tx", NULL, "codec_out0"},
142 {"ssp2 Tx", NULL, "codec_out1"},
143 {"codec_in0", NULL, "ssp2 Rx" },
144 {"codec_in1", NULL, "ssp2 Rx" },
145 {"ssp2 Rx", NULL, "AIF1 Capture"},
146 {"Headphone", NULL, "Platform Clock"},
147 {"Headset Mic", NULL, "Platform Clock"},
148 {"Int Mic", NULL, "Platform Clock"},
149 {"Ext Spk", NULL, "Platform Clock"},
152 static const struct snd_kcontrol_new cht_mc_controls[] = {
153 SOC_DAPM_PIN_SWITCH("Headphone"),
154 SOC_DAPM_PIN_SWITCH("Headset Mic"),
155 SOC_DAPM_PIN_SWITCH("Int Mic"),
156 SOC_DAPM_PIN_SWITCH("Int Analog Mic"),
157 SOC_DAPM_PIN_SWITCH("Ext Spk"),
160 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
161 struct snd_pcm_hw_params *params)
163 struct snd_soc_pcm_runtime *rtd = substream->private_data;
164 struct snd_soc_dai *codec_dai = rtd->codec_dai;
167 /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
168 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
169 CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
171 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
175 ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
176 params_rate(params) * 512, SND_SOC_CLOCK_IN);
178 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
185 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
189 struct snd_soc_codec *codec = runtime->codec;
190 struct snd_soc_dai *codec_dai = runtime->codec_dai;
191 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
193 /* Select clk_i2s1_asrc as ASRC clock source */
194 rt5645_sel_asrc_clk_src(codec,
195 RT5645_DA_STEREO_FILTER |
196 RT5645_DA_MONO_L_FILTER |
197 RT5645_DA_MONO_R_FILTER |
198 RT5645_AD_STEREO_FILTER,
199 RT5645_CLK_SEL_I2S1_ASRC);
201 /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
202 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
204 dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
208 if (ctx->acpi_card->codec_type == CODEC_TYPE_RT5650)
209 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
210 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
211 SND_JACK_BTN_2 | SND_JACK_BTN_3;
213 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
215 ret = snd_soc_card_jack_new(runtime->card, "Headset Jack",
216 jack_type, &ctx->jack,
219 dev_err(runtime->dev, "Headset jack creation failed %d\n", ret);
223 rt5645_set_jack_detect(codec, &ctx->jack, &ctx->jack, &ctx->jack);
228 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
229 struct snd_pcm_hw_params *params)
231 struct snd_interval *rate = hw_param_interval(params,
232 SNDRV_PCM_HW_PARAM_RATE);
233 struct snd_interval *channels = hw_param_interval(params,
234 SNDRV_PCM_HW_PARAM_CHANNELS);
236 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
237 rate->min = rate->max = 48000;
238 channels->min = channels->max = 2;
240 /* set SSP2 to 24-bit */
241 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
245 static int cht_aif1_startup(struct snd_pcm_substream *substream)
247 return snd_pcm_hw_constraint_single(substream->runtime,
248 SNDRV_PCM_HW_PARAM_RATE, 48000);
251 static struct snd_soc_ops cht_aif1_ops = {
252 .startup = cht_aif1_startup,
255 static struct snd_soc_ops cht_be_ssp2_ops = {
256 .hw_params = cht_aif1_hw_params,
259 static struct snd_soc_dai_link cht_dailink[] = {
260 [MERR_DPCM_AUDIO] = {
261 .name = "Audio Port",
262 .stream_name = "Audio",
263 .cpu_dai_name = "media-cpu-dai",
264 .codec_dai_name = "snd-soc-dummy-dai",
265 .codec_name = "snd-soc-dummy",
266 .platform_name = "sst-mfld-platform",
271 .ops = &cht_aif1_ops,
273 [MERR_DPCM_COMPR] = {
274 .name = "Compressed Port",
275 .stream_name = "Compress",
276 .cpu_dai_name = "compress-cpu-dai",
277 .codec_dai_name = "snd-soc-dummy-dai",
278 .codec_name = "snd-soc-dummy",
279 .platform_name = "sst-mfld-platform",
281 /* CODEC<->CODEC link */
284 .name = "SSP2-Codec",
286 .cpu_dai_name = "ssp2-port",
287 .platform_name = "sst-mfld-platform",
289 .codec_dai_name = "rt5645-aif1",
290 .codec_name = "i2c-10EC5645:00",
291 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
292 | SND_SOC_DAIFMT_CBS_CFS,
293 .init = cht_codec_init,
294 .be_hw_params_fixup = cht_codec_fixup,
298 .ops = &cht_be_ssp2_ops,
303 static struct snd_soc_card snd_soc_card_chtrt5645 = {
305 .owner = THIS_MODULE,
306 .dai_link = cht_dailink,
307 .num_links = ARRAY_SIZE(cht_dailink),
308 .dapm_widgets = cht_dapm_widgets,
309 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
310 .dapm_routes = cht_rt5645_audio_map,
311 .num_dapm_routes = ARRAY_SIZE(cht_rt5645_audio_map),
312 .controls = cht_mc_controls,
313 .num_controls = ARRAY_SIZE(cht_mc_controls),
316 static struct snd_soc_card snd_soc_card_chtrt5650 = {
318 .owner = THIS_MODULE,
319 .dai_link = cht_dailink,
320 .num_links = ARRAY_SIZE(cht_dailink),
321 .dapm_widgets = cht_dapm_widgets,
322 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
323 .dapm_routes = cht_rt5650_audio_map,
324 .num_dapm_routes = ARRAY_SIZE(cht_rt5650_audio_map),
325 .controls = cht_mc_controls,
326 .num_controls = ARRAY_SIZE(cht_mc_controls),
329 static struct cht_acpi_card snd_soc_cards[] = {
330 {"10EC5645", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
331 {"10EC5650", CODEC_TYPE_RT5650, &snd_soc_card_chtrt5650},
334 static acpi_status snd_acpi_codec_match(acpi_handle handle, u32 level,
335 void *context, void **ret)
337 *(bool *)context = true;
341 static int snd_cht_mc_probe(struct platform_device *pdev)
345 struct cht_mc_private *drv;
346 struct snd_soc_card *card = snd_soc_cards[0].soc_card;
350 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
354 for (i = 0; i < ARRAY_SIZE(snd_soc_cards); i++) {
355 if (ACPI_SUCCESS(acpi_get_devices(
356 snd_soc_cards[i].codec_id,
357 snd_acpi_codec_match,
358 &found, NULL)) && found) {
360 "found codec %s\n", snd_soc_cards[i].codec_id);
361 card = snd_soc_cards[i].soc_card;
362 drv->acpi_card = &snd_soc_cards[i];
366 card->dev = &pdev->dev;
367 sprintf(codec_name, "i2c-%s:00", drv->acpi_card->codec_id);
368 /* set correct codec name */
369 strcpy((char *)card->dai_link[2].codec_name, codec_name);
370 snd_soc_card_set_drvdata(card, drv);
371 ret_val = devm_snd_soc_register_card(&pdev->dev, card);
374 "snd_soc_register_card failed %d\n", ret_val);
377 platform_set_drvdata(pdev, card);
381 static struct platform_driver snd_cht_mc_driver = {
383 .name = "cht-bsw-rt5645",
385 .probe = snd_cht_mc_probe,
388 module_platform_driver(snd_cht_mc_driver)
390 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
391 MODULE_AUTHOR("Fang, Yang A,N,Harshapriya");
392 MODULE_LICENSE("GPL v2");
393 MODULE_ALIAS("platform:cht-bsw-rt5645");