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 <asm/cpu_device_id.h>
28 #include <asm/platform_sst_audio.h>
29 #include <linux/clk.h>
30 #include <sound/pcm.h>
31 #include <sound/pcm_params.h>
32 #include <sound/soc.h>
33 #include <sound/jack.h>
34 #include "../../codecs/rt5645.h"
35 #include "../atom/sst-atom-controls.h"
36 #include "../common/sst-acpi.h"
38 #define CHT_PLAT_CLK_3_HZ 19200000
39 #define CHT_CODEC_DAI "rt5645-aif1"
41 struct cht_acpi_card {
44 struct snd_soc_card *soc_card;
47 struct cht_mc_private {
48 struct snd_soc_jack jack;
49 struct cht_acpi_card *acpi_card;
54 static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card)
56 struct snd_soc_pcm_runtime *rtd;
58 list_for_each_entry(rtd, &card->rtd_list, list) {
59 if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI,
60 strlen(CHT_CODEC_DAI)))
61 return rtd->codec_dai;
66 static int platform_clock_control(struct snd_soc_dapm_widget *w,
67 struct snd_kcontrol *k, int event)
69 struct snd_soc_dapm_context *dapm = w->dapm;
70 struct snd_soc_card *card = dapm->card;
71 struct snd_soc_dai *codec_dai;
72 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card);
75 codec_dai = cht_get_codec_dai(card);
77 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n");
81 if (SND_SOC_DAPM_EVENT_ON(event)) {
83 ret = clk_prepare_enable(ctx->mclk);
86 "could not configure MCLK state");
91 /* Set codec sysclk source to its internal clock because codec PLL will
92 * be off when idle and MCLK will also be off when codec is
93 * runtime suspended. Codec needs clock for jack detection and button
94 * press. MCLK is turned off with clock framework or ACPI.
96 ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_RCCLK,
97 48000 * 512, SND_SOC_CLOCK_IN);
99 dev_err(card->dev, "can't set codec sysclk: %d\n", ret);
104 clk_disable_unprepare(ctx->mclk);
110 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = {
111 SND_SOC_DAPM_HP("Headphone", NULL),
112 SND_SOC_DAPM_MIC("Headset Mic", NULL),
113 SND_SOC_DAPM_MIC("Int Mic", NULL),
114 SND_SOC_DAPM_MIC("Int Analog Mic", NULL),
115 SND_SOC_DAPM_SPK("Ext Spk", NULL),
116 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
117 platform_clock_control, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
120 static const struct snd_soc_dapm_route cht_rt5645_audio_map[] = {
121 {"IN1P", NULL, "Headset Mic"},
122 {"IN1N", NULL, "Headset Mic"},
123 {"DMIC L1", NULL, "Int Mic"},
124 {"DMIC R1", NULL, "Int Mic"},
125 {"IN2P", NULL, "Int Analog Mic"},
126 {"IN2N", NULL, "Int Analog Mic"},
127 {"Headphone", NULL, "HPOL"},
128 {"Headphone", NULL, "HPOR"},
129 {"Ext Spk", NULL, "SPOL"},
130 {"Ext Spk", NULL, "SPOR"},
131 {"AIF1 Playback", NULL, "ssp2 Tx"},
132 {"ssp2 Tx", NULL, "codec_out0"},
133 {"ssp2 Tx", NULL, "codec_out1"},
134 {"codec_in0", NULL, "ssp2 Rx" },
135 {"codec_in1", NULL, "ssp2 Rx" },
136 {"ssp2 Rx", NULL, "AIF1 Capture"},
137 {"Headphone", NULL, "Platform Clock"},
138 {"Headset Mic", NULL, "Platform Clock"},
139 {"Int Mic", NULL, "Platform Clock"},
140 {"Int Analog Mic", NULL, "Platform Clock"},
141 {"Int Analog Mic", NULL, "micbias1"},
142 {"Int Analog Mic", NULL, "micbias2"},
143 {"Ext Spk", NULL, "Platform Clock"},
146 static const struct snd_soc_dapm_route cht_rt5650_audio_map[] = {
147 {"IN1P", NULL, "Headset Mic"},
148 {"IN1N", NULL, "Headset Mic"},
149 {"DMIC L2", NULL, "Int Mic"},
150 {"DMIC R2", NULL, "Int Mic"},
151 {"Headphone", NULL, "HPOL"},
152 {"Headphone", NULL, "HPOR"},
153 {"Ext Spk", NULL, "SPOL"},
154 {"Ext Spk", NULL, "SPOR"},
155 {"AIF1 Playback", NULL, "ssp2 Tx"},
156 {"ssp2 Tx", NULL, "codec_out0"},
157 {"ssp2 Tx", NULL, "codec_out1"},
158 {"codec_in0", NULL, "ssp2 Rx" },
159 {"codec_in1", NULL, "ssp2 Rx" },
160 {"ssp2 Rx", NULL, "AIF1 Capture"},
161 {"Headphone", NULL, "Platform Clock"},
162 {"Headset Mic", NULL, "Platform Clock"},
163 {"Int Mic", NULL, "Platform Clock"},
164 {"Ext Spk", NULL, "Platform Clock"},
167 static const struct snd_kcontrol_new cht_mc_controls[] = {
168 SOC_DAPM_PIN_SWITCH("Headphone"),
169 SOC_DAPM_PIN_SWITCH("Headset Mic"),
170 SOC_DAPM_PIN_SWITCH("Int Mic"),
171 SOC_DAPM_PIN_SWITCH("Int Analog Mic"),
172 SOC_DAPM_PIN_SWITCH("Ext Spk"),
175 static struct snd_soc_jack_pin cht_bsw_jack_pins[] = {
178 .mask = SND_JACK_HEADPHONE,
181 .pin = "Headset Mic",
182 .mask = SND_JACK_MICROPHONE,
186 static int cht_aif1_hw_params(struct snd_pcm_substream *substream,
187 struct snd_pcm_hw_params *params)
189 struct snd_soc_pcm_runtime *rtd = substream->private_data;
190 struct snd_soc_dai *codec_dai = rtd->codec_dai;
193 /* set codec PLL source to the 19.2MHz platform clock (MCLK) */
194 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK,
195 CHT_PLAT_CLK_3_HZ, params_rate(params) * 512);
197 dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
201 ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1,
202 params_rate(params) * 512, SND_SOC_CLOCK_IN);
204 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret);
211 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime)
215 struct snd_soc_codec *codec = runtime->codec;
216 struct snd_soc_dai *codec_dai = runtime->codec_dai;
217 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
219 /* Select clk_i2s1_asrc as ASRC clock source */
220 rt5645_sel_asrc_clk_src(codec,
221 RT5645_DA_STEREO_FILTER |
222 RT5645_DA_MONO_L_FILTER |
223 RT5645_DA_MONO_R_FILTER |
224 RT5645_AD_STEREO_FILTER,
225 RT5645_CLK_SEL_I2S1_ASRC);
227 /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */
228 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24);
230 dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret);
234 if (ctx->acpi_card->codec_type == CODEC_TYPE_RT5650)
235 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE |
236 SND_JACK_BTN_0 | SND_JACK_BTN_1 |
237 SND_JACK_BTN_2 | SND_JACK_BTN_3;
239 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE;
241 ret = snd_soc_card_jack_new(runtime->card, "Headset",
242 jack_type, &ctx->jack,
243 cht_bsw_jack_pins, ARRAY_SIZE(cht_bsw_jack_pins));
245 dev_err(runtime->dev, "Headset jack creation failed %d\n", ret);
249 rt5645_set_jack_detect(codec, &ctx->jack, &ctx->jack, &ctx->jack);
253 * The firmware might enable the clock at
254 * boot (this information may or may not
255 * be reflected in the enable clock register).
256 * To change the rate we must disable the clock
257 * first to cover these cases. Due to common
258 * clock framework restrictions that do not allow
259 * to disable a clock that has not been enabled,
260 * we need to enable the clock first.
262 ret = clk_prepare_enable(ctx->mclk);
264 clk_disable_unprepare(ctx->mclk);
266 ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ);
269 dev_err(runtime->dev, "unable to set MCLK rate\n");
274 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd,
275 struct snd_pcm_hw_params *params)
277 struct snd_interval *rate = hw_param_interval(params,
278 SNDRV_PCM_HW_PARAM_RATE);
279 struct snd_interval *channels = hw_param_interval(params,
280 SNDRV_PCM_HW_PARAM_CHANNELS);
282 /* The DSP will covert the FE rate to 48k, stereo, 24bits */
283 rate->min = rate->max = 48000;
284 channels->min = channels->max = 2;
286 /* set SSP2 to 24-bit */
287 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
291 static int cht_aif1_startup(struct snd_pcm_substream *substream)
293 return snd_pcm_hw_constraint_single(substream->runtime,
294 SNDRV_PCM_HW_PARAM_RATE, 48000);
297 static struct snd_soc_ops cht_aif1_ops = {
298 .startup = cht_aif1_startup,
301 static struct snd_soc_ops cht_be_ssp2_ops = {
302 .hw_params = cht_aif1_hw_params,
305 static struct snd_soc_dai_link cht_dailink[] = {
306 [MERR_DPCM_AUDIO] = {
307 .name = "Audio Port",
308 .stream_name = "Audio",
309 .cpu_dai_name = "media-cpu-dai",
310 .codec_dai_name = "snd-soc-dummy-dai",
311 .codec_name = "snd-soc-dummy",
312 .platform_name = "sst-mfld-platform",
317 .ops = &cht_aif1_ops,
319 [MERR_DPCM_DEEP_BUFFER] = {
320 .name = "Deep-Buffer Audio Port",
321 .stream_name = "Deep-Buffer Audio",
322 .cpu_dai_name = "deepbuffer-cpu-dai",
323 .codec_dai_name = "snd-soc-dummy-dai",
324 .codec_name = "snd-soc-dummy",
325 .platform_name = "sst-mfld-platform",
329 .ops = &cht_aif1_ops,
331 [MERR_DPCM_COMPR] = {
332 .name = "Compressed Port",
333 .stream_name = "Compress",
334 .cpu_dai_name = "compress-cpu-dai",
335 .codec_dai_name = "snd-soc-dummy-dai",
336 .codec_name = "snd-soc-dummy",
337 .platform_name = "sst-mfld-platform",
339 /* CODEC<->CODEC link */
342 .name = "SSP2-Codec",
344 .cpu_dai_name = "ssp2-port",
345 .platform_name = "sst-mfld-platform",
347 .codec_dai_name = "rt5645-aif1",
348 .codec_name = "i2c-10EC5645:00",
349 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF
350 | SND_SOC_DAIFMT_CBS_CFS,
351 .init = cht_codec_init,
352 .be_hw_params_fixup = cht_codec_fixup,
356 .ops = &cht_be_ssp2_ops,
361 static struct snd_soc_card snd_soc_card_chtrt5645 = {
363 .owner = THIS_MODULE,
364 .dai_link = cht_dailink,
365 .num_links = ARRAY_SIZE(cht_dailink),
366 .dapm_widgets = cht_dapm_widgets,
367 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
368 .dapm_routes = cht_rt5645_audio_map,
369 .num_dapm_routes = ARRAY_SIZE(cht_rt5645_audio_map),
370 .controls = cht_mc_controls,
371 .num_controls = ARRAY_SIZE(cht_mc_controls),
374 static struct snd_soc_card snd_soc_card_chtrt5650 = {
376 .owner = THIS_MODULE,
377 .dai_link = cht_dailink,
378 .num_links = ARRAY_SIZE(cht_dailink),
379 .dapm_widgets = cht_dapm_widgets,
380 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets),
381 .dapm_routes = cht_rt5650_audio_map,
382 .num_dapm_routes = ARRAY_SIZE(cht_rt5650_audio_map),
383 .controls = cht_mc_controls,
384 .num_controls = ARRAY_SIZE(cht_mc_controls),
387 static struct cht_acpi_card snd_soc_cards[] = {
388 {"10EC5640", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
389 {"10EC5645", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645},
390 {"10EC5650", CODEC_TYPE_RT5650, &snd_soc_card_chtrt5650},
393 static char cht_rt5640_codec_name[16]; /* i2c-<HID>:00 with HID being 8 chars */
395 static bool is_valleyview(void)
397 static const struct x86_cpu_id cpu_ids[] = {
398 { X86_VENDOR_INTEL, 6, 55 }, /* Valleyview, Bay Trail */
402 if (!x86_match_cpu(cpu_ids))
407 static int snd_cht_mc_probe(struct platform_device *pdev)
411 struct cht_mc_private *drv;
412 struct snd_soc_card *card = snd_soc_cards[0].soc_card;
413 struct sst_acpi_mach *mach;
414 const char *i2c_name = NULL;
418 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC);
422 mach = (&pdev->dev)->platform_data;
424 for (i = 0; i < ARRAY_SIZE(snd_soc_cards); i++) {
425 if (acpi_dev_found(snd_soc_cards[i].codec_id) &&
426 (!strncmp(snd_soc_cards[i].codec_id, mach->id, 8))) {
428 "found codec %s\n", snd_soc_cards[i].codec_id);
429 card = snd_soc_cards[i].soc_card;
430 drv->acpi_card = &snd_soc_cards[i];
437 dev_err(&pdev->dev, "No matching HID found in supported list\n");
441 card->dev = &pdev->dev;
442 sprintf(drv->codec_name, "i2c-%s:00", drv->acpi_card->codec_id);
444 /* set correct codec name */
445 for (i = 0; i < ARRAY_SIZE(cht_dailink); i++)
446 if (!strcmp(card->dai_link[i].codec_name, "i2c-10EC5645:00")) {
447 card->dai_link[i].codec_name = drv->codec_name;
451 /* fixup codec name based on HID */
452 i2c_name = sst_acpi_find_name_from_hid(mach->id);
453 if (i2c_name != NULL) {
454 snprintf(cht_rt5640_codec_name, sizeof(cht_rt5640_codec_name),
455 "%s%s", "i2c-", i2c_name);
456 cht_dailink[dai_index].codec_name = cht_rt5640_codec_name;
459 if (is_valleyview()) {
460 drv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
461 if (IS_ERR(drv->mclk)) {
463 "Failed to get MCLK from pmc_plt_clk_3: %ld\n",
465 return PTR_ERR(drv->mclk);
469 snd_soc_card_set_drvdata(card, drv);
470 ret_val = devm_snd_soc_register_card(&pdev->dev, card);
473 "snd_soc_register_card failed %d\n", ret_val);
476 platform_set_drvdata(pdev, card);
480 static struct platform_driver snd_cht_mc_driver = {
482 .name = "cht-bsw-rt5645",
484 .probe = snd_cht_mc_probe,
487 module_platform_driver(snd_cht_mc_driver)
489 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver");
490 MODULE_AUTHOR("Fang, Yang A,N,Harshapriya");
491 MODULE_LICENSE("GPL v2");
492 MODULE_ALIAS("platform:cht-bsw-rt5645");