1 /* 2 * cht-bsw-max98090.c - ASoc Machine driver for Intel Cherryview-based 3 * platforms Cherrytrail and Braswell, with max98090 & TI codec. 4 * 5 * Copyright (C) 2015 Intel Corp 6 * Author: Fang, Yang A <yang.a.fang@intel.com> 7 * This file is modified from cht_bsw_rt5645.c 8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation; version 2 of the License. 13 * 14 * This program is distributed in the hope that it will be useful, but 15 * WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 * General Public License for more details. 18 * 19 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 20 */ 21 22 #include <linux/module.h> 23 #include <linux/platform_device.h> 24 #include <linux/slab.h> 25 #include <linux/acpi.h> 26 #include <linux/clk.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/max98090.h" 32 #include "../atom/sst-atom-controls.h" 33 #include "../../codecs/ts3a227e.h" 34 35 #define CHT_PLAT_CLK_3_HZ 19200000 36 #define CHT_CODEC_DAI "HiFi" 37 38 struct cht_mc_private { 39 struct clk *mclk; 40 struct snd_soc_jack jack; 41 bool ts3a227e_present; 42 }; 43 44 static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card) 45 { 46 struct snd_soc_pcm_runtime *rtd; 47 48 list_for_each_entry(rtd, &card->rtd_list, list) { 49 if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI, 50 strlen(CHT_CODEC_DAI))) 51 return rtd->codec_dai; 52 } 53 return NULL; 54 } 55 56 static int platform_clock_control(struct snd_soc_dapm_widget *w, 57 struct snd_kcontrol *k, int event) 58 { 59 struct snd_soc_dapm_context *dapm = w->dapm; 60 struct snd_soc_card *card = dapm->card; 61 struct snd_soc_dai *codec_dai; 62 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card); 63 int ret; 64 65 codec_dai = cht_get_codec_dai(card); 66 if (!codec_dai) { 67 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n"); 68 return -EIO; 69 } 70 71 if (SND_SOC_DAPM_EVENT_ON(event)) { 72 ret = clk_prepare_enable(ctx->mclk); 73 if (ret < 0) { 74 dev_err(card->dev, 75 "could not configure MCLK state"); 76 return ret; 77 } 78 } else { 79 clk_disable_unprepare(ctx->mclk); 80 } 81 82 return 0; 83 } 84 85 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = { 86 SND_SOC_DAPM_HP("Headphone", NULL), 87 SND_SOC_DAPM_MIC("Headset Mic", NULL), 88 SND_SOC_DAPM_MIC("Int Mic", NULL), 89 SND_SOC_DAPM_SPK("Ext Spk", NULL), 90 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, 91 platform_clock_control, SND_SOC_DAPM_PRE_PMU | 92 SND_SOC_DAPM_POST_PMD), 93 }; 94 95 static const struct snd_soc_dapm_route cht_audio_map[] = { 96 {"IN34", NULL, "Headset Mic"}, 97 {"Headset Mic", NULL, "MICBIAS"}, 98 {"DMICL", NULL, "Int Mic"}, 99 {"Headphone", NULL, "HPL"}, 100 {"Headphone", NULL, "HPR"}, 101 {"Ext Spk", NULL, "SPKL"}, 102 {"Ext Spk", NULL, "SPKR"}, 103 {"HiFi Playback", NULL, "ssp2 Tx"}, 104 {"ssp2 Tx", NULL, "codec_out0"}, 105 {"ssp2 Tx", NULL, "codec_out1"}, 106 {"codec_in0", NULL, "ssp2 Rx" }, 107 {"codec_in1", NULL, "ssp2 Rx" }, 108 {"ssp2 Rx", NULL, "HiFi Capture"}, 109 {"Headphone", NULL, "Platform Clock"}, 110 {"Headset Mic", NULL, "Platform Clock"}, 111 {"Int Mic", NULL, "Platform Clock"}, 112 {"Ext Spk", NULL, "Platform Clock"}, 113 }; 114 115 static const struct snd_kcontrol_new cht_mc_controls[] = { 116 SOC_DAPM_PIN_SWITCH("Headphone"), 117 SOC_DAPM_PIN_SWITCH("Headset Mic"), 118 SOC_DAPM_PIN_SWITCH("Int Mic"), 119 SOC_DAPM_PIN_SWITCH("Ext Spk"), 120 }; 121 122 static int cht_aif1_hw_params(struct snd_pcm_substream *substream, 123 struct snd_pcm_hw_params *params) 124 { 125 struct snd_soc_pcm_runtime *rtd = substream->private_data; 126 struct snd_soc_dai *codec_dai = rtd->codec_dai; 127 int ret; 128 129 ret = snd_soc_dai_set_sysclk(codec_dai, M98090_REG_SYSTEM_CLOCK, 130 CHT_PLAT_CLK_3_HZ, SND_SOC_CLOCK_IN); 131 if (ret < 0) { 132 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret); 133 return ret; 134 } 135 136 return 0; 137 } 138 139 static int cht_ti_jack_event(struct notifier_block *nb, 140 unsigned long event, void *data) 141 { 142 struct snd_soc_jack *jack = (struct snd_soc_jack *)data; 143 struct snd_soc_dapm_context *dapm = &jack->card->dapm; 144 145 if (event & SND_JACK_MICROPHONE) { 146 snd_soc_dapm_force_enable_pin(dapm, "SHDN"); 147 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS"); 148 snd_soc_dapm_sync(dapm); 149 } else { 150 snd_soc_dapm_disable_pin(dapm, "MICBIAS"); 151 snd_soc_dapm_disable_pin(dapm, "SHDN"); 152 snd_soc_dapm_sync(dapm); 153 } 154 155 return 0; 156 } 157 158 static struct notifier_block cht_jack_nb = { 159 .notifier_call = cht_ti_jack_event, 160 }; 161 162 static struct snd_soc_jack_pin hs_jack_pins[] = { 163 { 164 .pin = "Headphone", 165 .mask = SND_JACK_HEADPHONE, 166 }, 167 { 168 .pin = "Headset Mic", 169 .mask = SND_JACK_MICROPHONE, 170 }, 171 }; 172 173 static struct snd_soc_jack_gpio hs_jack_gpios[] = { 174 { 175 .name = "hp", 176 .report = SND_JACK_HEADPHONE | SND_JACK_LINEOUT, 177 .debounce_time = 200, 178 }, 179 { 180 .name = "mic", 181 .invert = 1, 182 .report = SND_JACK_MICROPHONE, 183 .debounce_time = 200, 184 }, 185 }; 186 187 static const struct acpi_gpio_params hp_gpios = { 0, 0, false }; 188 static const struct acpi_gpio_params mic_gpios = { 1, 0, false }; 189 190 static const struct acpi_gpio_mapping acpi_max98090_gpios[] = { 191 { "hp-gpios", &hp_gpios, 1 }, 192 { "mic-gpios", &mic_gpios, 1 }, 193 {}, 194 }; 195 196 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime) 197 { 198 int ret; 199 int jack_type; 200 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card); 201 struct snd_soc_jack *jack = &ctx->jack; 202 203 if (ctx->ts3a227e_present) { 204 /* 205 * The jack has already been created in the 206 * cht_max98090_headset_init() function. 207 */ 208 snd_soc_jack_notifier_register(jack, &cht_jack_nb); 209 return 0; 210 } 211 212 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE; 213 214 ret = snd_soc_card_jack_new(runtime->card, "Headset Jack", 215 jack_type, jack, 216 hs_jack_pins, ARRAY_SIZE(hs_jack_pins)); 217 if (ret) { 218 dev_err(runtime->dev, "Headset Jack creation failed %d\n", ret); 219 return ret; 220 } 221 222 ret = snd_soc_jack_add_gpiods(runtime->card->dev->parent, jack, 223 ARRAY_SIZE(hs_jack_gpios), 224 hs_jack_gpios); 225 if (ret) { 226 /* 227 * flag error but don't bail if jack detect is broken 228 * due to platform issues or bad BIOS/configuration 229 */ 230 dev_err(runtime->dev, 231 "jack detection gpios not added, error %d\n", ret); 232 } 233 234 /* 235 * The firmware might enable the clock at 236 * boot (this information may or may not 237 * be reflected in the enable clock register). 238 * To change the rate we must disable the clock 239 * first to cover these cases. Due to common 240 * clock framework restrictions that do not allow 241 * to disable a clock that has not been enabled, 242 * we need to enable the clock first. 243 */ 244 ret = clk_prepare_enable(ctx->mclk); 245 if (!ret) 246 clk_disable_unprepare(ctx->mclk); 247 248 ret = clk_set_rate(ctx->mclk, CHT_PLAT_CLK_3_HZ); 249 250 if (ret) 251 dev_err(runtime->dev, "unable to set MCLK rate\n"); 252 253 return ret; 254 } 255 256 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd, 257 struct snd_pcm_hw_params *params) 258 { 259 struct snd_interval *rate = hw_param_interval(params, 260 SNDRV_PCM_HW_PARAM_RATE); 261 struct snd_interval *channels = hw_param_interval(params, 262 SNDRV_PCM_HW_PARAM_CHANNELS); 263 int ret = 0; 264 unsigned int fmt = 0; 265 266 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2, 16); 267 if (ret < 0) { 268 dev_err(rtd->dev, "can't set cpu_dai slot fmt: %d\n", ret); 269 return ret; 270 } 271 272 fmt = SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF 273 | SND_SOC_DAIFMT_CBS_CFS; 274 275 ret = snd_soc_dai_set_fmt(rtd->cpu_dai, fmt); 276 if (ret < 0) { 277 dev_err(rtd->dev, "can't set cpu_dai set fmt: %d\n", ret); 278 return ret; 279 } 280 281 /* The DSP will covert the FE rate to 48k, stereo, 24bits */ 282 rate->min = rate->max = 48000; 283 channels->min = channels->max = 2; 284 285 /* set SSP2 to 24-bit */ 286 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE); 287 return 0; 288 } 289 290 static int cht_aif1_startup(struct snd_pcm_substream *substream) 291 { 292 return snd_pcm_hw_constraint_single(substream->runtime, 293 SNDRV_PCM_HW_PARAM_RATE, 48000); 294 } 295 296 static int cht_max98090_headset_init(struct snd_soc_component *component) 297 { 298 struct snd_soc_card *card = component->card; 299 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(card); 300 struct snd_soc_jack *jack = &ctx->jack; 301 int jack_type; 302 int ret; 303 304 /* 305 * TI supports 4 butons headset detection 306 * KEY_MEDIA 307 * KEY_VOICECOMMAND 308 * KEY_VOLUMEUP 309 * KEY_VOLUMEDOWN 310 */ 311 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE | 312 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 313 SND_JACK_BTN_2 | SND_JACK_BTN_3; 314 315 ret = snd_soc_card_jack_new(card, "Headset Jack", jack_type, 316 jack, NULL, 0); 317 if (ret) { 318 dev_err(card->dev, "Headset Jack creation failed %d\n", ret); 319 return ret; 320 } 321 322 return ts3a227e_enable_jack_detect(component, jack); 323 } 324 325 static const struct snd_soc_ops cht_aif1_ops = { 326 .startup = cht_aif1_startup, 327 }; 328 329 static const struct snd_soc_ops cht_be_ssp2_ops = { 330 .hw_params = cht_aif1_hw_params, 331 }; 332 333 static struct snd_soc_aux_dev cht_max98090_headset_dev = { 334 .name = "Headset Chip", 335 .init = cht_max98090_headset_init, 336 .codec_name = "i2c-104C227E:00", 337 }; 338 339 static struct snd_soc_dai_link cht_dailink[] = { 340 [MERR_DPCM_AUDIO] = { 341 .name = "Audio Port", 342 .stream_name = "Audio", 343 .cpu_dai_name = "media-cpu-dai", 344 .codec_dai_name = "snd-soc-dummy-dai", 345 .codec_name = "snd-soc-dummy", 346 .platform_name = "sst-mfld-platform", 347 .nonatomic = true, 348 .dynamic = 1, 349 .dpcm_playback = 1, 350 .dpcm_capture = 1, 351 .ops = &cht_aif1_ops, 352 }, 353 [MERR_DPCM_DEEP_BUFFER] = { 354 .name = "Deep-Buffer Audio Port", 355 .stream_name = "Deep-Buffer Audio", 356 .cpu_dai_name = "deepbuffer-cpu-dai", 357 .codec_dai_name = "snd-soc-dummy-dai", 358 .codec_name = "snd-soc-dummy", 359 .platform_name = "sst-mfld-platform", 360 .nonatomic = true, 361 .dynamic = 1, 362 .dpcm_playback = 1, 363 .ops = &cht_aif1_ops, 364 }, 365 [MERR_DPCM_COMPR] = { 366 .name = "Compressed Port", 367 .stream_name = "Compress", 368 .cpu_dai_name = "compress-cpu-dai", 369 .codec_dai_name = "snd-soc-dummy-dai", 370 .codec_name = "snd-soc-dummy", 371 .platform_name = "sst-mfld-platform", 372 }, 373 /* back ends */ 374 { 375 .name = "SSP2-Codec", 376 .id = 1, 377 .cpu_dai_name = "ssp2-port", 378 .platform_name = "sst-mfld-platform", 379 .no_pcm = 1, 380 .codec_dai_name = "HiFi", 381 .codec_name = "i2c-193C9890:00", 382 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF 383 | SND_SOC_DAIFMT_CBS_CFS, 384 .init = cht_codec_init, 385 .be_hw_params_fixup = cht_codec_fixup, 386 .dpcm_playback = 1, 387 .dpcm_capture = 1, 388 .ops = &cht_be_ssp2_ops, 389 }, 390 }; 391 392 /* SoC card */ 393 static struct snd_soc_card snd_soc_card_cht = { 394 .name = "chtmax98090", 395 .owner = THIS_MODULE, 396 .dai_link = cht_dailink, 397 .num_links = ARRAY_SIZE(cht_dailink), 398 .aux_dev = &cht_max98090_headset_dev, 399 .num_aux_devs = 1, 400 .dapm_widgets = cht_dapm_widgets, 401 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets), 402 .dapm_routes = cht_audio_map, 403 .num_dapm_routes = ARRAY_SIZE(cht_audio_map), 404 .controls = cht_mc_controls, 405 .num_controls = ARRAY_SIZE(cht_mc_controls), 406 }; 407 408 static int snd_cht_mc_probe(struct platform_device *pdev) 409 { 410 struct device *dev = &pdev->dev; 411 int ret_val = 0; 412 struct cht_mc_private *drv; 413 414 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC); 415 if (!drv) 416 return -ENOMEM; 417 418 drv->ts3a227e_present = acpi_dev_found("104C227E"); 419 if (!drv->ts3a227e_present) { 420 /* no need probe TI jack detection chip */ 421 snd_soc_card_cht.aux_dev = NULL; 422 snd_soc_card_cht.num_aux_devs = 0; 423 424 ret_val = devm_acpi_dev_add_driver_gpios(dev->parent, 425 acpi_max98090_gpios); 426 if (ret_val) 427 dev_dbg(dev, "Unable to add GPIO mapping table\n"); 428 } 429 430 /* register the soc card */ 431 snd_soc_card_cht.dev = &pdev->dev; 432 snd_soc_card_set_drvdata(&snd_soc_card_cht, drv); 433 434 drv->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3"); 435 if (IS_ERR(drv->mclk)) { 436 dev_err(&pdev->dev, 437 "Failed to get MCLK from pmc_plt_clk_3: %ld\n", 438 PTR_ERR(drv->mclk)); 439 return PTR_ERR(drv->mclk); 440 } 441 442 ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht); 443 if (ret_val) { 444 dev_err(&pdev->dev, 445 "snd_soc_register_card failed %d\n", ret_val); 446 return ret_val; 447 } 448 platform_set_drvdata(pdev, &snd_soc_card_cht); 449 return ret_val; 450 } 451 452 static struct platform_driver snd_cht_mc_driver = { 453 .driver = { 454 .name = "cht-bsw-max98090", 455 }, 456 .probe = snd_cht_mc_probe, 457 }; 458 459 module_platform_driver(snd_cht_mc_driver) 460 461 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver"); 462 MODULE_AUTHOR("Fang, Yang A <yang.a.fang@intel.com>"); 463 MODULE_LICENSE("GPL v2"); 464 MODULE_ALIAS("platform:cht-bsw-max98090"); 465