Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 554 lines 16 kB view raw
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * cht_bsw_rt5672.c - ASoc Machine driver for Intel Cherryview-based platforms 4 * Cherrytrail and Braswell, with RT5672 codec. 5 * 6 * Copyright (C) 2014 Intel Corp 7 * Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com> 8 * Mengdong Lin <mengdong.lin@intel.com> 9 */ 10 11#include <linux/gpio/consumer.h> 12#include <linux/input.h> 13#include <linux/module.h> 14#include <linux/platform_device.h> 15#include <linux/slab.h> 16#include <linux/clk.h> 17#include <linux/string.h> 18#include <sound/pcm.h> 19#include <sound/pcm_params.h> 20#include <sound/soc.h> 21#include <sound/jack.h> 22#include <sound/soc-acpi.h> 23#include "../../codecs/rt5670.h" 24#include "../atom/sst-atom-controls.h" 25#include "../common/soc-intel-quirks.h" 26 27 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" 31 32struct cht_mc_private { 33 struct snd_soc_jack headset; 34 char codec_name[SND_ACPI_I2C_ID_LEN]; 35 struct clk *mclk; 36 bool use_ssp0; 37}; 38 39/* Headset jack detection DAPM pins */ 40static struct snd_soc_jack_pin cht_bsw_headset_pins[] = { 41 { 42 .pin = "Headset Mic", 43 .mask = SND_JACK_MICROPHONE, 44 }, 45 { 46 .pin = "Headphone", 47 .mask = SND_JACK_HEADPHONE, 48 }, 49}; 50 51static int platform_clock_control(struct snd_soc_dapm_widget *w, 52 struct snd_kcontrol *k, int event) 53{ 54 struct snd_soc_card *card = snd_soc_dapm_to_card(w->dapm); 55 struct snd_soc_dai *codec_dai; 56 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card); 57 int ret; 58 59 codec_dai = snd_soc_card_get_codec_dai(card, CHT_CODEC_DAI); 60 if (!codec_dai) { 61 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n"); 62 return -EIO; 63 } 64 65 if (SND_SOC_DAPM_EVENT_ON(event)) { 66 if (ctx->mclk) { 67 ret = clk_prepare_enable(ctx->mclk); 68 if (ret < 0) { 69 dev_err(card->dev, 70 "could not configure MCLK state: %d\n", ret); 71 return ret; 72 } 73 } 74 75 /* set codec PLL source to the 19.2MHz platform clock (MCLK) */ 76 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK, 77 CHT_PLAT_CLK_3_HZ, 48000 * 512); 78 if (ret < 0) { 79 dev_err(card->dev, "can't set codec pll: %d\n", ret); 80 if (ctx->mclk) 81 clk_disable_unprepare(ctx->mclk); 82 return ret; 83 } 84 85 /* set codec sysclk source to PLL */ 86 ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1, 87 48000 * 512, SND_SOC_CLOCK_IN); 88 if (ret < 0) { 89 dev_err(card->dev, "can't set codec sysclk: %d\n", ret); 90 if (ctx->mclk) 91 clk_disable_unprepare(ctx->mclk); 92 return ret; 93 } 94 } else { 95 /* Set codec sysclk source to its internal clock because codec 96 * PLL will be off when idle and MCLK will also be off by ACPI 97 * when codec is runtime suspended. Codec needs clock for jack 98 * detection and button press. 99 */ 100 ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_RCCLK, 101 48000 * 512, SND_SOC_CLOCK_IN); 102 if (ret < 0) { 103 dev_err(card->dev, "failed to set codec sysclk: %d\n", ret); 104 return ret; 105 } 106 107 if (ctx->mclk) 108 clk_disable_unprepare(ctx->mclk); 109 } 110 return 0; 111} 112 113static const struct snd_soc_dapm_widget cht_dapm_widgets[] = { 114 SND_SOC_DAPM_HP("Headphone", NULL), 115 SND_SOC_DAPM_MIC("Headset Mic", NULL), 116 SND_SOC_DAPM_MIC("Int Mic", NULL), 117 SND_SOC_DAPM_SPK("Ext Spk", NULL), 118 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, 119 platform_clock_control, SND_SOC_DAPM_PRE_PMU | 120 SND_SOC_DAPM_POST_PMD), 121}; 122 123static const struct snd_soc_dapm_route cht_audio_map[] = { 124 {"IN1P", NULL, "Headset Mic"}, 125 {"IN1N", NULL, "Headset Mic"}, 126 {"DMIC L1", NULL, "Int Mic"}, 127 {"DMIC R1", NULL, "Int Mic"}, 128 {"Headphone", NULL, "HPOL"}, 129 {"Headphone", NULL, "HPOR"}, 130 {"Ext Spk", NULL, "SPOLP"}, 131 {"Ext Spk", NULL, "SPOLN"}, 132 {"Ext Spk", NULL, "SPORP"}, 133 {"Ext Spk", NULL, "SPORN"}, 134 {"Headphone", NULL, "Platform Clock"}, 135 {"Headset Mic", NULL, "Platform Clock"}, 136 {"Int Mic", NULL, "Platform Clock"}, 137 {"Ext Spk", NULL, "Platform Clock"}, 138}; 139 140static const struct snd_soc_dapm_route cht_audio_ssp0_map[] = { 141 {"AIF1 Playback", NULL, "ssp0 Tx"}, 142 {"ssp0 Tx", NULL, "modem_out"}, 143 {"modem_in", NULL, "ssp0 Rx"}, 144 {"ssp0 Rx", NULL, "AIF1 Capture"}, 145}; 146 147static const struct snd_soc_dapm_route cht_audio_ssp2_map[] = { 148 {"AIF1 Playback", NULL, "ssp2 Tx"}, 149 {"ssp2 Tx", NULL, "codec_out0"}, 150 {"ssp2 Tx", NULL, "codec_out1"}, 151 {"codec_in0", NULL, "ssp2 Rx"}, 152 {"codec_in1", NULL, "ssp2 Rx"}, 153 {"ssp2 Rx", NULL, "AIF1 Capture"}, 154}; 155 156static const struct snd_kcontrol_new cht_mc_controls[] = { 157 SOC_DAPM_PIN_SWITCH("Headphone"), 158 SOC_DAPM_PIN_SWITCH("Headset Mic"), 159 SOC_DAPM_PIN_SWITCH("Int Mic"), 160 SOC_DAPM_PIN_SWITCH("Ext Spk"), 161}; 162 163static int cht_aif1_hw_params(struct snd_pcm_substream *substream, 164 struct snd_pcm_hw_params *params) 165{ 166 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream); 167 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0); 168 int ret; 169 170 /* set codec PLL source to the 19.2MHz platform clock (MCLK) */ 171 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK, 172 CHT_PLAT_CLK_3_HZ, params_rate(params) * 512); 173 if (ret < 0) { 174 dev_err(rtd->dev, "can't set codec pll: %d\n", ret); 175 return ret; 176 } 177 178 /* set codec sysclk source to PLL */ 179 ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1, 180 params_rate(params) * 512, 181 SND_SOC_CLOCK_IN); 182 if (ret < 0) { 183 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret); 184 return ret; 185 } 186 return 0; 187} 188 189static const struct acpi_gpio_params headset_gpios = { 0, 0, false }; 190 191static const struct acpi_gpio_mapping cht_rt5672_gpios[] = { 192 { "headset-gpios", &headset_gpios, 1 }, 193 {}, 194}; 195 196static int cht_codec_init(struct snd_soc_pcm_runtime *runtime) 197{ 198 int ret; 199 struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(runtime->card); 200 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0); 201 struct snd_soc_component *component = codec_dai->component; 202 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card); 203 204 if (devm_acpi_dev_add_driver_gpios(component->dev, cht_rt5672_gpios)) 205 dev_warn(runtime->dev, "Unable to add GPIO mapping table\n"); 206 207 /* Select codec ASRC clock source to track I2S1 clock, because codec 208 * is in slave mode and 100fs I2S format (BCLK = 100 * LRCLK) cannot 209 * be supported by RT5672. Otherwise, ASRC will be disabled and cause 210 * noise. 211 */ 212 rt5670_sel_asrc_clk_src(component, 213 RT5670_DA_STEREO_FILTER 214 | RT5670_DA_MONO_L_FILTER 215 | RT5670_DA_MONO_R_FILTER 216 | RT5670_AD_STEREO_FILTER 217 | RT5670_AD_MONO_L_FILTER 218 | RT5670_AD_MONO_R_FILTER, 219 RT5670_CLK_SEL_I2S1_ASRC); 220 221 if (ctx->use_ssp0) { 222 ret = snd_soc_dapm_add_routes(dapm, 223 cht_audio_ssp0_map, 224 ARRAY_SIZE(cht_audio_ssp0_map)); 225 } else { 226 ret = snd_soc_dapm_add_routes(dapm, 227 cht_audio_ssp2_map, 228 ARRAY_SIZE(cht_audio_ssp2_map)); 229 } 230 if (ret) 231 return ret; 232 233 ret = snd_soc_card_jack_new_pins(runtime->card, "Headset", 234 SND_JACK_HEADSET | SND_JACK_BTN_0 | 235 SND_JACK_BTN_1 | SND_JACK_BTN_2, 236 &ctx->headset, 237 cht_bsw_headset_pins, 238 ARRAY_SIZE(cht_bsw_headset_pins)); 239 if (ret) 240 return ret; 241 242 snd_jack_set_key(ctx->headset.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); 243 snd_jack_set_key(ctx->headset.jack, SND_JACK_BTN_1, KEY_VOLUMEUP); 244 snd_jack_set_key(ctx->headset.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN); 245 246 rt5670_set_jack_detect(component, &ctx->headset); 247 if (ctx->mclk) { 248 /* 249 * The firmware might enable the clock at 250 * boot (this information may or may not 251 * be reflected in the enable clock register). 252 * To change the rate we must disable the clock 253 * first to cover these cases. Due to common 254 * clock framework restrictions that do not allow 255 * to disable a clock that has not been enabled, 256 * we need to enable the clock first. 257 */ 258 ret = clk_prepare_enable(ctx->mclk); 259 if (!ret) 260 clk_disable_unprepare(ctx->mclk); 261 262 ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ); 263 264 if (ret) { 265 dev_err(runtime->dev, "unable to set MCLK rate\n"); 266 return ret; 267 } 268 } 269 return 0; 270} 271 272static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd, 273 struct snd_pcm_hw_params *params) 274{ 275 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(rtd->card); 276 struct snd_interval *rate = hw_param_interval(params, 277 SNDRV_PCM_HW_PARAM_RATE); 278 struct snd_interval *channels = hw_param_interval(params, 279 SNDRV_PCM_HW_PARAM_CHANNELS); 280 int ret, bits; 281 282 /* The DSP will convert the FE rate to 48k, stereo, 24bits */ 283 rate->min = rate->max = 48000; 284 channels->min = channels->max = 2; 285 286 if (ctx->use_ssp0) { 287 /* set SSP0 to 16-bit */ 288 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE); 289 bits = 16; 290 } else { 291 /* set SSP2 to 24-bit */ 292 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE); 293 bits = 24; 294 } 295 296 /* 297 * The default mode for the cpu-dai is TDM 4 slot. The default mode 298 * for the codec-dai is I2S. So we need to either set the cpu-dai to 299 * I2S mode to match the codec-dai, or set the codec-dai to TDM 4 slot 300 * (or program both to yet another mode). 301 * One board, the Lenovo Miix 2 10, uses not 1 but 2 codecs connected 302 * to SSP2. The second piggy-backed, output-only codec is inside the 303 * keyboard-dock (which has extra speakers). Unlike the main rt5672 304 * codec, we cannot configure this codec, it is hard coded to use 305 * 2 channel 24 bit I2S. For this to work we must use I2S mode on this 306 * board. Since we only support 2 channels anyways, there is no need 307 * for TDM on any cht-bsw-rt5672 designs. So we use I2S 2ch everywhere. 308 */ 309 ret = snd_soc_dai_set_fmt(snd_soc_rtd_to_cpu(rtd, 0), 310 SND_SOC_DAIFMT_I2S | 311 SND_SOC_DAIFMT_NB_NF | 312 SND_SOC_DAIFMT_BP_FP); 313 if (ret < 0) { 314 dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret); 315 return ret; 316 } 317 318 ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, bits); 319 if (ret < 0) { 320 dev_err(rtd->dev, "can't set I2S config, err %d\n", ret); 321 return ret; 322 } 323 324 return 0; 325} 326 327static int cht_aif1_startup(struct snd_pcm_substream *substream) 328{ 329 return snd_pcm_hw_constraint_single(substream->runtime, 330 SNDRV_PCM_HW_PARAM_RATE, 48000); 331} 332 333static const struct snd_soc_ops cht_aif1_ops = { 334 .startup = cht_aif1_startup, 335}; 336 337static const struct snd_soc_ops cht_be_ssp2_ops = { 338 .hw_params = cht_aif1_hw_params, 339}; 340 341SND_SOC_DAILINK_DEF(dummy, 342 DAILINK_COMP_ARRAY(COMP_DUMMY())); 343 344SND_SOC_DAILINK_DEF(media, 345 DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai"))); 346 347SND_SOC_DAILINK_DEF(deepbuffer, 348 DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai"))); 349 350SND_SOC_DAILINK_DEF(ssp2_port, 351 DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port"))); 352SND_SOC_DAILINK_DEF(ssp2_codec, 353 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5670:00", 354 "rt5670-aif1"))); 355 356SND_SOC_DAILINK_DEF(platform, 357 DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform"))); 358 359static struct snd_soc_dai_link cht_dailink[] = { 360 /* Front End DAI links */ 361 [MERR_DPCM_AUDIO] = { 362 .name = "Audio Port", 363 .stream_name = "Audio", 364 .nonatomic = true, 365 .dynamic = 1, 366 .ops = &cht_aif1_ops, 367 SND_SOC_DAILINK_REG(media, dummy, platform), 368 }, 369 [MERR_DPCM_DEEP_BUFFER] = { 370 .name = "Deep-Buffer Audio Port", 371 .stream_name = "Deep-Buffer Audio", 372 .nonatomic = true, 373 .dynamic = 1, 374 .playback_only = 1, 375 .ops = &cht_aif1_ops, 376 SND_SOC_DAILINK_REG(deepbuffer, dummy, platform), 377 }, 378 379 /* Back End DAI links */ 380 { 381 /* SSP2 - Codec */ 382 .name = "SSP2-Codec", 383 .id = 0, 384 .no_pcm = 1, 385 .init = cht_codec_init, 386 .be_hw_params_fixup = cht_codec_fixup, 387 .ops = &cht_be_ssp2_ops, 388 SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform), 389 }, 390}; 391 392static int cht_suspend_pre(struct snd_soc_card *card) 393{ 394 struct snd_soc_component *component; 395 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card); 396 397 for_each_card_components(card, component) { 398 if (!strncmp(component->name, 399 ctx->codec_name, sizeof(ctx->codec_name))) { 400 401 dev_dbg(component->dev, "disabling jack detect before going to suspend.\n"); 402 rt5670_jack_suspend(component); 403 break; 404 } 405 } 406 return 0; 407} 408 409static int cht_resume_post(struct snd_soc_card *card) 410{ 411 struct snd_soc_component *component; 412 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card); 413 414 for_each_card_components(card, component) { 415 if (!strncmp(component->name, 416 ctx->codec_name, sizeof(ctx->codec_name))) { 417 418 dev_dbg(component->dev, "enabling jack detect for resume.\n"); 419 rt5670_jack_resume(component); 420 break; 421 } 422 } 423 424 return 0; 425} 426 427/* use space before codec name to simplify card ID, and simplify driver name */ 428#define SOF_CARD_NAME "bytcht rt5672" /* card name will be 'sof-bytcht rt5672' */ 429#define SOF_DRIVER_NAME "SOF" 430 431#define CARD_NAME "cht-bsw-rt5672" 432#define DRIVER_NAME NULL /* card name will be used for driver name */ 433 434/* SoC card */ 435static struct snd_soc_card snd_soc_card_cht = { 436 .owner = THIS_MODULE, 437 .dai_link = cht_dailink, 438 .num_links = ARRAY_SIZE(cht_dailink), 439 .dapm_widgets = cht_dapm_widgets, 440 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets), 441 .dapm_routes = cht_audio_map, 442 .num_dapm_routes = ARRAY_SIZE(cht_audio_map), 443 .controls = cht_mc_controls, 444 .num_controls = ARRAY_SIZE(cht_mc_controls), 445 .suspend_pre = cht_suspend_pre, 446 .resume_post = cht_resume_post, 447}; 448 449#define RT5672_I2C_DEFAULT "i2c-10EC5670:00" 450 451static int snd_cht_mc_probe(struct platform_device *pdev) 452{ 453 int ret_val = 0; 454 struct cht_mc_private *drv; 455 struct snd_soc_acpi_mach *mach = pdev->dev.platform_data; 456 const char *platform_name; 457 struct acpi_device *adev; 458 bool sof_parent; 459 int dai_index = 0; 460 int i; 461 462 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL); 463 if (!drv) 464 return -ENOMEM; 465 466 strscpy(drv->codec_name, RT5672_I2C_DEFAULT, sizeof(drv->codec_name)); 467 468 /* find index of codec dai */ 469 for (i = 0; i < ARRAY_SIZE(cht_dailink); i++) { 470 if (cht_dailink[i].num_codecs && 471 !strcmp(cht_dailink[i].codecs->name, RT5672_I2C_DEFAULT)) { 472 dai_index = i; 473 break; 474 } 475 } 476 477 /* fixup codec name based on HID */ 478 adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1); 479 if (adev) { 480 snprintf(drv->codec_name, sizeof(drv->codec_name), 481 "i2c-%s", acpi_dev_name(adev)); 482 cht_dailink[dai_index].codecs->name = drv->codec_name; 483 } else { 484 dev_err(&pdev->dev, "Error cannot find '%s' dev\n", mach->id); 485 return -ENOENT; 486 } 487 488 acpi_dev_put(adev); 489 490 /* Use SSP0 on Bay Trail CR devices */ 491 if (soc_intel_is_byt() && mach->mach_params.acpi_ipc_irq_index == 0) { 492 cht_dailink[dai_index].cpus->dai_name = "ssp0-port"; 493 drv->use_ssp0 = true; 494 } 495 496 /* override platform name, if required */ 497 snd_soc_card_cht.dev = &pdev->dev; 498 platform_name = mach->mach_params.platform; 499 500 ret_val = snd_soc_fixup_dai_links_platform_name(&snd_soc_card_cht, 501 platform_name); 502 if (ret_val) 503 return ret_val; 504 505 snd_soc_card_cht.components = rt5670_components(); 506 507 drv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3"); 508 if (IS_ERR(drv->mclk)) { 509 dev_err(&pdev->dev, 510 "Failed to get MCLK from pmc_plt_clk_3: %ld\n", 511 PTR_ERR(drv->mclk)); 512 return PTR_ERR(drv->mclk); 513 } 514 snd_soc_card_set_drvdata(&snd_soc_card_cht, drv); 515 516 sof_parent = snd_soc_acpi_sof_parent(&pdev->dev); 517 518 /* set card and driver name */ 519 if (sof_parent) { 520 snd_soc_card_cht.name = SOF_CARD_NAME; 521 snd_soc_card_cht.driver_name = SOF_DRIVER_NAME; 522 } else { 523 snd_soc_card_cht.name = CARD_NAME; 524 snd_soc_card_cht.driver_name = DRIVER_NAME; 525 } 526 527 /* set pm ops */ 528 if (sof_parent) 529 pdev->dev.driver->pm = &snd_soc_pm_ops; 530 531 /* register the soc card */ 532 ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht); 533 if (ret_val) { 534 dev_err(&pdev->dev, 535 "snd_soc_register_card failed %d\n", ret_val); 536 return ret_val; 537 } 538 platform_set_drvdata(pdev, &snd_soc_card_cht); 539 return ret_val; 540} 541 542static struct platform_driver snd_cht_mc_driver = { 543 .driver = { 544 .name = "cht-bsw-rt5672", 545 }, 546 .probe = snd_cht_mc_probe, 547}; 548 549module_platform_driver(snd_cht_mc_driver); 550 551MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver"); 552MODULE_AUTHOR("Subhransu S. Prusty, Mengdong Lin"); 553MODULE_LICENSE("GPL v2"); 554MODULE_ALIAS("platform:cht-bsw-rt5672");