1 /* 2 * cht_bsw_rt5672.c - ASoc Machine driver for Intel Cherryview-based platforms 3 * Cherrytrail and Braswell, with RT5672 codec. 4 * 5 * Copyright (C) 2014 Intel Corp 6 * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com> 7 * Mengdong Lin <mengdong.lin@intel.com> 8 * 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. 12 * 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. 17 */ 18 19 #include <linux/module.h> 20 #include <linux/platform_device.h> 21 #include <linux/slab.h> 22 #include <linux/clk.h> 23 #include <sound/pcm.h> 24 #include <sound/pcm_params.h> 25 #include <sound/soc.h> 26 #include <sound/jack.h> 27 #include "../../codecs/rt5670.h" 28 #include "../atom/sst-atom-controls.h" 29 #include "../common/sst-acpi.h" 30 31 /* The platform clock #3 outputs 19.2Mhz clock to codec as I2S MCLK */ 32 #define CHT_PLAT_CLK_3_HZ 19200000 33 #define CHT_CODEC_DAI "rt5670-aif1" 34 35 struct cht_mc_private { 36 struct snd_soc_jack headset; 37 char codec_name[16]; 38 struct clk *mclk; 39 }; 40 41 /* Headset jack detection DAPM pins */ 42 static struct snd_soc_jack_pin cht_bsw_headset_pins[] = { 43 { 44 .pin = "Headset Mic", 45 .mask = SND_JACK_MICROPHONE, 46 }, 47 { 48 .pin = "Headphone", 49 .mask = SND_JACK_HEADPHONE, 50 }, 51 }; 52 53 static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card) 54 { 55 struct snd_soc_pcm_runtime *rtd; 56 57 list_for_each_entry(rtd, &card->rtd_list, list) { 58 if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI, 59 strlen(CHT_CODEC_DAI))) 60 return rtd->codec_dai; 61 } 62 return NULL; 63 } 64 65 static int platform_clock_control(struct snd_soc_dapm_widget *w, 66 struct snd_kcontrol *k, int event) 67 { 68 struct snd_soc_dapm_context *dapm = w->dapm; 69 struct snd_soc_card *card = dapm->card; 70 struct snd_soc_dai *codec_dai; 71 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card); 72 int ret; 73 74 codec_dai = cht_get_codec_dai(card); 75 if (!codec_dai) { 76 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n"); 77 return -EIO; 78 } 79 80 if (SND_SOC_DAPM_EVENT_ON(event)) { 81 if (ctx->mclk) { 82 ret = clk_prepare_enable(ctx->mclk); 83 if (ret < 0) { 84 dev_err(card->dev, 85 "could not configure MCLK state"); 86 return ret; 87 } 88 } 89 90 /* set codec PLL source to the 19.2MHz platform clock (MCLK) */ 91 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK, 92 CHT_PLAT_CLK_3_HZ, 48000 * 512); 93 if (ret < 0) { 94 dev_err(card->dev, "can't set codec pll: %d\n", ret); 95 return ret; 96 } 97 98 /* set codec sysclk source to PLL */ 99 ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1, 100 48000 * 512, SND_SOC_CLOCK_IN); 101 if (ret < 0) { 102 dev_err(card->dev, "can't set codec sysclk: %d\n", ret); 103 return ret; 104 } 105 } else { 106 /* Set codec sysclk source to its internal clock because codec 107 * PLL will be off when idle and MCLK will also be off by ACPI 108 * when codec is runtime suspended. Codec needs clock for jack 109 * detection and button press. 110 */ 111 snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_RCCLK, 112 48000 * 512, SND_SOC_CLOCK_IN); 113 114 if (ctx->mclk) 115 clk_disable_unprepare(ctx->mclk); 116 } 117 return 0; 118 } 119 120 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = { 121 SND_SOC_DAPM_HP("Headphone", NULL), 122 SND_SOC_DAPM_MIC("Headset Mic", NULL), 123 SND_SOC_DAPM_MIC("Int Mic", NULL), 124 SND_SOC_DAPM_SPK("Ext Spk", NULL), 125 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, 126 platform_clock_control, SND_SOC_DAPM_PRE_PMU | 127 SND_SOC_DAPM_POST_PMD), 128 }; 129 130 static const struct snd_soc_dapm_route cht_audio_map[] = { 131 {"IN1P", NULL, "Headset Mic"}, 132 {"IN1N", NULL, "Headset Mic"}, 133 {"DMIC L1", NULL, "Int Mic"}, 134 {"DMIC R1", NULL, "Int Mic"}, 135 {"Headphone", NULL, "HPOL"}, 136 {"Headphone", NULL, "HPOR"}, 137 {"Ext Spk", NULL, "SPOLP"}, 138 {"Ext Spk", NULL, "SPOLN"}, 139 {"Ext Spk", NULL, "SPORP"}, 140 {"Ext Spk", NULL, "SPORN"}, 141 {"AIF1 Playback", NULL, "ssp2 Tx"}, 142 {"ssp2 Tx", NULL, "codec_out0"}, 143 {"ssp2 Tx", NULL, "codec_out1"}, 144 {"codec_in0", NULL, "ssp2 Rx"}, 145 {"codec_in1", NULL, "ssp2 Rx"}, 146 {"ssp2 Rx", NULL, "AIF1 Capture"}, 147 {"Headphone", NULL, "Platform Clock"}, 148 {"Headset Mic", NULL, "Platform Clock"}, 149 {"Int Mic", NULL, "Platform Clock"}, 150 {"Ext Spk", NULL, "Platform Clock"}, 151 }; 152 153 static const struct snd_kcontrol_new cht_mc_controls[] = { 154 SOC_DAPM_PIN_SWITCH("Headphone"), 155 SOC_DAPM_PIN_SWITCH("Headset Mic"), 156 SOC_DAPM_PIN_SWITCH("Int Mic"), 157 SOC_DAPM_PIN_SWITCH("Ext Spk"), 158 }; 159 160 static int cht_aif1_hw_params(struct snd_pcm_substream *substream, 161 struct snd_pcm_hw_params *params) 162 { 163 struct snd_soc_pcm_runtime *rtd = substream->private_data; 164 struct snd_soc_dai *codec_dai = rtd->codec_dai; 165 int ret; 166 167 /* set codec PLL source to the 19.2MHz platform clock (MCLK) */ 168 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK, 169 CHT_PLAT_CLK_3_HZ, params_rate(params) * 512); 170 if (ret < 0) { 171 dev_err(rtd->dev, "can't set codec pll: %d\n", ret); 172 return ret; 173 } 174 175 /* set codec sysclk source to PLL */ 176 ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1, 177 params_rate(params) * 512, 178 SND_SOC_CLOCK_IN); 179 if (ret < 0) { 180 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret); 181 return ret; 182 } 183 return 0; 184 } 185 186 static const struct acpi_gpio_params headset_gpios = { 0, 0, false }; 187 188 static const struct acpi_gpio_mapping cht_rt5672_gpios[] = { 189 { "headset-gpios", &headset_gpios, 1 }, 190 {}, 191 }; 192 193 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime) 194 { 195 int ret; 196 struct snd_soc_dai *codec_dai = runtime->codec_dai; 197 struct snd_soc_codec *codec = codec_dai->codec; 198 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card); 199 200 if (devm_acpi_dev_add_driver_gpios(codec->dev, cht_rt5672_gpios)) 201 dev_warn(runtime->dev, "Unable to add GPIO mapping table\n"); 202 203 /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */ 204 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24); 205 if (ret < 0) { 206 dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret); 207 return ret; 208 } 209 210 /* Select codec ASRC clock source to track I2S1 clock, because codec 211 * is in slave mode and 100fs I2S format (BCLK = 100 * LRCLK) cannot 212 * be supported by RT5672. Otherwise, ASRC will be disabled and cause 213 * noise. 214 */ 215 rt5670_sel_asrc_clk_src(codec, 216 RT5670_DA_STEREO_FILTER 217 | RT5670_DA_MONO_L_FILTER 218 | RT5670_DA_MONO_R_FILTER 219 | RT5670_AD_STEREO_FILTER 220 | RT5670_AD_MONO_L_FILTER 221 | RT5670_AD_MONO_R_FILTER, 222 RT5670_CLK_SEL_I2S1_ASRC); 223 224 ret = snd_soc_card_jack_new(runtime->card, "Headset", 225 SND_JACK_HEADSET | SND_JACK_BTN_0 | 226 SND_JACK_BTN_1 | SND_JACK_BTN_2, 227 &ctx->headset, 228 cht_bsw_headset_pins, 229 ARRAY_SIZE(cht_bsw_headset_pins)); 230 if (ret) 231 return ret; 232 233 rt5670_set_jack_detect(codec, &ctx->headset); 234 if (ctx->mclk) { 235 /* 236 * The firmware might enable the clock at 237 * boot (this information may or may not 238 * be reflected in the enable clock register). 239 * To change the rate we must disable the clock 240 * first to cover these cases. Due to common 241 * clock framework restrictions that do not allow 242 * to disable a clock that has not been enabled, 243 * we need to enable the clock first. 244 */ 245 ret = clk_prepare_enable(ctx->mclk); 246 if (!ret) 247 clk_disable_unprepare(ctx->mclk); 248 249 ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ); 250 251 if (ret) { 252 dev_err(runtime->dev, "unable to set MCLK rate\n"); 253 return ret; 254 } 255 } 256 return 0; 257 } 258 259 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd, 260 struct snd_pcm_hw_params *params) 261 { 262 struct snd_interval *rate = hw_param_interval(params, 263 SNDRV_PCM_HW_PARAM_RATE); 264 struct snd_interval *channels = hw_param_interval(params, 265 SNDRV_PCM_HW_PARAM_CHANNELS); 266 267 /* The DSP will covert the FE rate to 48k, stereo, 24bits */ 268 rate->min = rate->max = 48000; 269 channels->min = channels->max = 2; 270 271 /* set SSP2 to 24-bit */ 272 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE); 273 return 0; 274 } 275 276 static int cht_aif1_startup(struct snd_pcm_substream *substream) 277 { 278 return snd_pcm_hw_constraint_single(substream->runtime, 279 SNDRV_PCM_HW_PARAM_RATE, 48000); 280 } 281 282 static const struct snd_soc_ops cht_aif1_ops = { 283 .startup = cht_aif1_startup, 284 }; 285 286 static const struct snd_soc_ops cht_be_ssp2_ops = { 287 .hw_params = cht_aif1_hw_params, 288 }; 289 290 static struct snd_soc_dai_link cht_dailink[] = { 291 /* Front End DAI links */ 292 [MERR_DPCM_AUDIO] = { 293 .name = "Audio Port", 294 .stream_name = "Audio", 295 .cpu_dai_name = "media-cpu-dai", 296 .codec_dai_name = "snd-soc-dummy-dai", 297 .codec_name = "snd-soc-dummy", 298 .platform_name = "sst-mfld-platform", 299 .nonatomic = true, 300 .dynamic = 1, 301 .dpcm_playback = 1, 302 .dpcm_capture = 1, 303 .ops = &cht_aif1_ops, 304 }, 305 [MERR_DPCM_DEEP_BUFFER] = { 306 .name = "Deep-Buffer Audio Port", 307 .stream_name = "Deep-Buffer Audio", 308 .cpu_dai_name = "deepbuffer-cpu-dai", 309 .codec_dai_name = "snd-soc-dummy-dai", 310 .codec_name = "snd-soc-dummy", 311 .platform_name = "sst-mfld-platform", 312 .nonatomic = true, 313 .dynamic = 1, 314 .dpcm_playback = 1, 315 .ops = &cht_aif1_ops, 316 }, 317 [MERR_DPCM_COMPR] = { 318 .name = "Compressed Port", 319 .stream_name = "Compress", 320 .cpu_dai_name = "compress-cpu-dai", 321 .codec_dai_name = "snd-soc-dummy-dai", 322 .codec_name = "snd-soc-dummy", 323 .platform_name = "sst-mfld-platform", 324 }, 325 326 /* Back End DAI links */ 327 { 328 /* SSP2 - Codec */ 329 .name = "SSP2-Codec", 330 .id = 1, 331 .cpu_dai_name = "ssp2-port", 332 .platform_name = "sst-mfld-platform", 333 .no_pcm = 1, 334 .nonatomic = true, 335 .codec_dai_name = "rt5670-aif1", 336 .codec_name = "i2c-10EC5670:00", 337 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF 338 | SND_SOC_DAIFMT_CBS_CFS, 339 .init = cht_codec_init, 340 .be_hw_params_fixup = cht_codec_fixup, 341 .dpcm_playback = 1, 342 .dpcm_capture = 1, 343 .ops = &cht_be_ssp2_ops, 344 }, 345 }; 346 347 static int cht_suspend_pre(struct snd_soc_card *card) 348 { 349 struct snd_soc_component *component; 350 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card); 351 352 list_for_each_entry(component, &card->component_dev_list, card_list) { 353 if (!strncmp(component->name, 354 ctx->codec_name, sizeof(ctx->codec_name))) { 355 struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 356 357 dev_dbg(codec->dev, "disabling jack detect before going to suspend.\n"); 358 rt5670_jack_suspend(codec); 359 break; 360 } 361 } 362 return 0; 363 } 364 365 static int cht_resume_post(struct snd_soc_card *card) 366 { 367 struct snd_soc_component *component; 368 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card); 369 370 list_for_each_entry(component, &card->component_dev_list, card_list) { 371 if (!strncmp(component->name, 372 ctx->codec_name, sizeof(ctx->codec_name))) { 373 struct snd_soc_codec *codec = snd_soc_component_to_codec(component); 374 375 dev_dbg(codec->dev, "enabling jack detect for resume.\n"); 376 rt5670_jack_resume(codec); 377 break; 378 } 379 } 380 381 return 0; 382 } 383 384 /* SoC card */ 385 static struct snd_soc_card snd_soc_card_cht = { 386 .name = "cherrytrailcraudio", 387 .owner = THIS_MODULE, 388 .dai_link = cht_dailink, 389 .num_links = ARRAY_SIZE(cht_dailink), 390 .dapm_widgets = cht_dapm_widgets, 391 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets), 392 .dapm_routes = cht_audio_map, 393 .num_dapm_routes = ARRAY_SIZE(cht_audio_map), 394 .controls = cht_mc_controls, 395 .num_controls = ARRAY_SIZE(cht_mc_controls), 396 .suspend_pre = cht_suspend_pre, 397 .resume_post = cht_resume_post, 398 }; 399 400 #define RT5672_I2C_DEFAULT "i2c-10EC5670:00" 401 402 static int snd_cht_mc_probe(struct platform_device *pdev) 403 { 404 int ret_val = 0; 405 struct cht_mc_private *drv; 406 struct sst_acpi_mach *mach = pdev->dev.platform_data; 407 const char *i2c_name; 408 int i; 409 410 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC); 411 if (!drv) 412 return -ENOMEM; 413 414 strcpy(drv->codec_name, RT5672_I2C_DEFAULT); 415 416 /* fixup codec name based on HID */ 417 if (mach) { 418 i2c_name = sst_acpi_find_name_from_hid(mach->id); 419 if (i2c_name) { 420 snprintf(drv->codec_name, sizeof(drv->codec_name), 421 "i2c-%s", i2c_name); 422 for (i = 0; i < ARRAY_SIZE(cht_dailink); i++) { 423 if (!strcmp(cht_dailink[i].codec_name, 424 RT5672_I2C_DEFAULT)) { 425 cht_dailink[i].codec_name = 426 drv->codec_name; 427 break; 428 } 429 } 430 } 431 } 432 433 drv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3"); 434 if (IS_ERR(drv->mclk)) { 435 dev_err(&pdev->dev, 436 "Failed to get MCLK from pmc_plt_clk_3: %ld\n", 437 PTR_ERR(drv->mclk)); 438 return PTR_ERR(drv->mclk); 439 } 440 snd_soc_card_set_drvdata(&snd_soc_card_cht, drv); 441 442 /* register the soc card */ 443 snd_soc_card_cht.dev = &pdev->dev; 444 ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht); 445 if (ret_val) { 446 dev_err(&pdev->dev, 447 "snd_soc_register_card failed %d\n", ret_val); 448 return ret_val; 449 } 450 platform_set_drvdata(pdev, &snd_soc_card_cht); 451 return ret_val; 452 } 453 454 static struct platform_driver snd_cht_mc_driver = { 455 .driver = { 456 .name = "cht-bsw-rt5672", 457 }, 458 .probe = snd_cht_mc_probe, 459 }; 460 461 module_platform_driver(snd_cht_mc_driver); 462 463 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver"); 464 MODULE_AUTHOR("Subhransu S. Prusty, Mengdong Lin"); 465 MODULE_LICENSE("GPL v2"); 466 MODULE_ALIAS("platform:cht-bsw-rt5672"); 467