1 /* 2 * cht-bsw-rt5645.c - ASoc Machine driver for Intel Cherryview-based platforms 3 * Cherrytrail and Braswell, with RT5645 codec. 4 * 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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 10 * 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. 14 * 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. 19 * 20 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 21 */ 22 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" 33 34 #define CHT_PLAT_CLK_3_HZ 19200000 35 #define CHT_CODEC_DAI "rt5645-aif1" 36 37 struct cht_acpi_card { 38 char *codec_id; 39 int codec_type; 40 struct snd_soc_card *soc_card; 41 }; 42 43 struct cht_mc_private { 44 struct snd_soc_jack jack; 45 struct cht_acpi_card *acpi_card; 46 }; 47 48 static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card) 49 { 50 struct snd_soc_pcm_runtime *rtd; 51 52 list_for_each_entry(rtd, &card->rtd_list, list) { 53 if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI, 54 strlen(CHT_CODEC_DAI))) 55 return rtd->codec_dai; 56 } 57 return NULL; 58 } 59 60 static int platform_clock_control(struct snd_soc_dapm_widget *w, 61 struct snd_kcontrol *k, int event) 62 { 63 struct snd_soc_dapm_context *dapm = w->dapm; 64 struct snd_soc_card *card = dapm->card; 65 struct snd_soc_dai *codec_dai; 66 int ret; 67 68 codec_dai = cht_get_codec_dai(card); 69 if (!codec_dai) { 70 dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n"); 71 return -EIO; 72 } 73 74 if (!SND_SOC_DAPM_EVENT_OFF(event)) 75 return 0; 76 77 /* Set codec sysclk source to its internal clock because codec PLL will 78 * be off when idle and MCLK will also be off by ACPI when codec is 79 * runtime suspended. Codec needs clock for jack detection and button 80 * press. 81 */ 82 ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_RCCLK, 83 0, SND_SOC_CLOCK_IN); 84 if (ret < 0) { 85 dev_err(card->dev, "can't set codec sysclk: %d\n", ret); 86 return ret; 87 } 88 89 return 0; 90 } 91 92 static const struct snd_soc_dapm_widget cht_dapm_widgets[] = { 93 SND_SOC_DAPM_HP("Headphone", NULL), 94 SND_SOC_DAPM_MIC("Headset Mic", NULL), 95 SND_SOC_DAPM_MIC("Int Mic", NULL), 96 SND_SOC_DAPM_SPK("Ext Spk", NULL), 97 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, 98 platform_clock_control, SND_SOC_DAPM_POST_PMD), 99 }; 100 101 static const struct snd_soc_dapm_route cht_rt5645_audio_map[] = { 102 {"IN1P", NULL, "Headset Mic"}, 103 {"IN1N", NULL, "Headset Mic"}, 104 {"DMIC L1", NULL, "Int Mic"}, 105 {"DMIC R1", NULL, "Int Mic"}, 106 {"Headphone", NULL, "HPOL"}, 107 {"Headphone", NULL, "HPOR"}, 108 {"Ext Spk", NULL, "SPOL"}, 109 {"Ext Spk", NULL, "SPOR"}, 110 {"AIF1 Playback", NULL, "ssp2 Tx"}, 111 {"ssp2 Tx", NULL, "codec_out0"}, 112 {"ssp2 Tx", NULL, "codec_out1"}, 113 {"codec_in0", NULL, "ssp2 Rx" }, 114 {"codec_in1", NULL, "ssp2 Rx" }, 115 {"ssp2 Rx", NULL, "AIF1 Capture"}, 116 {"Headphone", NULL, "Platform Clock"}, 117 {"Headset Mic", NULL, "Platform Clock"}, 118 {"Int Mic", NULL, "Platform Clock"}, 119 {"Ext Spk", NULL, "Platform Clock"}, 120 }; 121 122 static const struct snd_soc_dapm_route cht_rt5650_audio_map[] = { 123 {"IN1P", NULL, "Headset Mic"}, 124 {"IN1N", NULL, "Headset Mic"}, 125 {"DMIC L2", NULL, "Int Mic"}, 126 {"DMIC R2", NULL, "Int 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 {"Ext Spk", NULL, "Platform Clock"}, 141 }; 142 143 static const struct snd_kcontrol_new cht_mc_controls[] = { 144 SOC_DAPM_PIN_SWITCH("Headphone"), 145 SOC_DAPM_PIN_SWITCH("Headset Mic"), 146 SOC_DAPM_PIN_SWITCH("Int Mic"), 147 SOC_DAPM_PIN_SWITCH("Ext Spk"), 148 }; 149 150 static int cht_aif1_hw_params(struct snd_pcm_substream *substream, 151 struct snd_pcm_hw_params *params) 152 { 153 struct snd_soc_pcm_runtime *rtd = substream->private_data; 154 struct snd_soc_dai *codec_dai = rtd->codec_dai; 155 int ret; 156 157 /* set codec PLL source to the 19.2MHz platform clock (MCLK) */ 158 ret = snd_soc_dai_set_pll(codec_dai, 0, RT5645_PLL1_S_MCLK, 159 CHT_PLAT_CLK_3_HZ, params_rate(params) * 512); 160 if (ret < 0) { 161 dev_err(rtd->dev, "can't set codec pll: %d\n", ret); 162 return ret; 163 } 164 165 ret = snd_soc_dai_set_sysclk(codec_dai, RT5645_SCLK_S_PLL1, 166 params_rate(params) * 512, SND_SOC_CLOCK_IN); 167 if (ret < 0) { 168 dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret); 169 return ret; 170 } 171 172 return 0; 173 } 174 175 static int cht_codec_init(struct snd_soc_pcm_runtime *runtime) 176 { 177 int ret; 178 int jack_type; 179 struct snd_soc_codec *codec = runtime->codec; 180 struct snd_soc_dai *codec_dai = runtime->codec_dai; 181 struct cht_mc_private *ctx = snd_soc_card_get_drvdata(runtime->card); 182 183 /* Select clk_i2s1_asrc as ASRC clock source */ 184 rt5645_sel_asrc_clk_src(codec, 185 RT5645_DA_STEREO_FILTER | 186 RT5645_DA_MONO_L_FILTER | 187 RT5645_DA_MONO_R_FILTER | 188 RT5645_AD_STEREO_FILTER, 189 RT5645_CLK_SEL_I2S1_ASRC); 190 191 /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */ 192 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24); 193 if (ret < 0) { 194 dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret); 195 return ret; 196 } 197 198 if (ctx->acpi_card->codec_type == CODEC_TYPE_RT5650) 199 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE | 200 SND_JACK_BTN_0 | SND_JACK_BTN_1 | 201 SND_JACK_BTN_2 | SND_JACK_BTN_3; 202 else 203 jack_type = SND_JACK_HEADPHONE | SND_JACK_MICROPHONE; 204 205 ret = snd_soc_card_jack_new(runtime->card, "Headset Jack", 206 jack_type, &ctx->jack, 207 NULL, 0); 208 if (ret) { 209 dev_err(runtime->dev, "Headset jack creation failed %d\n", ret); 210 return ret; 211 } 212 213 rt5645_set_jack_detect(codec, &ctx->jack, &ctx->jack, &ctx->jack); 214 215 return ret; 216 } 217 218 static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd, 219 struct snd_pcm_hw_params *params) 220 { 221 struct snd_interval *rate = hw_param_interval(params, 222 SNDRV_PCM_HW_PARAM_RATE); 223 struct snd_interval *channels = hw_param_interval(params, 224 SNDRV_PCM_HW_PARAM_CHANNELS); 225 226 /* The DSP will covert the FE rate to 48k, stereo, 24bits */ 227 rate->min = rate->max = 48000; 228 channels->min = channels->max = 2; 229 230 /* set SSP2 to 24-bit */ 231 params_set_format(params, SNDRV_PCM_FORMAT_S24_LE); 232 return 0; 233 } 234 235 static int cht_aif1_startup(struct snd_pcm_substream *substream) 236 { 237 return snd_pcm_hw_constraint_single(substream->runtime, 238 SNDRV_PCM_HW_PARAM_RATE, 48000); 239 } 240 241 static struct snd_soc_ops cht_aif1_ops = { 242 .startup = cht_aif1_startup, 243 }; 244 245 static struct snd_soc_ops cht_be_ssp2_ops = { 246 .hw_params = cht_aif1_hw_params, 247 }; 248 249 static struct snd_soc_dai_link cht_dailink[] = { 250 [MERR_DPCM_AUDIO] = { 251 .name = "Audio Port", 252 .stream_name = "Audio", 253 .cpu_dai_name = "media-cpu-dai", 254 .codec_dai_name = "snd-soc-dummy-dai", 255 .codec_name = "snd-soc-dummy", 256 .platform_name = "sst-mfld-platform", 257 .nonatomic = true, 258 .dynamic = 1, 259 .dpcm_playback = 1, 260 .dpcm_capture = 1, 261 .ops = &cht_aif1_ops, 262 }, 263 [MERR_DPCM_DEEP_BUFFER] = { 264 .name = "Deep-Buffer Audio Port", 265 .stream_name = "Deep-Buffer Audio", 266 .cpu_dai_name = "deepbuffer-cpu-dai", 267 .codec_dai_name = "snd-soc-dummy-dai", 268 .codec_name = "snd-soc-dummy", 269 .platform_name = "sst-mfld-platform", 270 .nonatomic = true, 271 .dynamic = 1, 272 .dpcm_playback = 1, 273 .ops = &cht_aif1_ops, 274 }, 275 [MERR_DPCM_COMPR] = { 276 .name = "Compressed Port", 277 .stream_name = "Compress", 278 .cpu_dai_name = "compress-cpu-dai", 279 .codec_dai_name = "snd-soc-dummy-dai", 280 .codec_name = "snd-soc-dummy", 281 .platform_name = "sst-mfld-platform", 282 }, 283 /* CODEC<->CODEC link */ 284 /* back ends */ 285 { 286 .name = "SSP2-Codec", 287 .be_id = 1, 288 .cpu_dai_name = "ssp2-port", 289 .platform_name = "sst-mfld-platform", 290 .no_pcm = 1, 291 .codec_dai_name = "rt5645-aif1", 292 .codec_name = "i2c-10EC5645:00", 293 .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF 294 | SND_SOC_DAIFMT_CBS_CFS, 295 .init = cht_codec_init, 296 .be_hw_params_fixup = cht_codec_fixup, 297 .nonatomic = true, 298 .dpcm_playback = 1, 299 .dpcm_capture = 1, 300 .ops = &cht_be_ssp2_ops, 301 }, 302 }; 303 304 /* SoC card */ 305 static struct snd_soc_card snd_soc_card_chtrt5645 = { 306 .name = "chtrt5645", 307 .owner = THIS_MODULE, 308 .dai_link = cht_dailink, 309 .num_links = ARRAY_SIZE(cht_dailink), 310 .dapm_widgets = cht_dapm_widgets, 311 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets), 312 .dapm_routes = cht_rt5645_audio_map, 313 .num_dapm_routes = ARRAY_SIZE(cht_rt5645_audio_map), 314 .controls = cht_mc_controls, 315 .num_controls = ARRAY_SIZE(cht_mc_controls), 316 }; 317 318 static struct snd_soc_card snd_soc_card_chtrt5650 = { 319 .name = "chtrt5650", 320 .owner = THIS_MODULE, 321 .dai_link = cht_dailink, 322 .num_links = ARRAY_SIZE(cht_dailink), 323 .dapm_widgets = cht_dapm_widgets, 324 .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets), 325 .dapm_routes = cht_rt5650_audio_map, 326 .num_dapm_routes = ARRAY_SIZE(cht_rt5650_audio_map), 327 .controls = cht_mc_controls, 328 .num_controls = ARRAY_SIZE(cht_mc_controls), 329 }; 330 331 static struct cht_acpi_card snd_soc_cards[] = { 332 {"10EC5645", CODEC_TYPE_RT5645, &snd_soc_card_chtrt5645}, 333 {"10EC5650", CODEC_TYPE_RT5650, &snd_soc_card_chtrt5650}, 334 }; 335 336 static acpi_status snd_acpi_codec_match(acpi_handle handle, u32 level, 337 void *context, void **ret) 338 { 339 *(bool *)context = true; 340 return AE_OK; 341 } 342 343 static int snd_cht_mc_probe(struct platform_device *pdev) 344 { 345 int ret_val = 0; 346 int i; 347 struct cht_mc_private *drv; 348 struct snd_soc_card *card = snd_soc_cards[0].soc_card; 349 bool found = false; 350 char codec_name[16]; 351 352 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_ATOMIC); 353 if (!drv) 354 return -ENOMEM; 355 356 for (i = 0; i < ARRAY_SIZE(snd_soc_cards); i++) { 357 if (ACPI_SUCCESS(acpi_get_devices( 358 snd_soc_cards[i].codec_id, 359 snd_acpi_codec_match, 360 &found, NULL)) && found) { 361 dev_dbg(&pdev->dev, 362 "found codec %s\n", snd_soc_cards[i].codec_id); 363 card = snd_soc_cards[i].soc_card; 364 drv->acpi_card = &snd_soc_cards[i]; 365 break; 366 } 367 } 368 card->dev = &pdev->dev; 369 sprintf(codec_name, "i2c-%s:00", drv->acpi_card->codec_id); 370 /* set correct codec name */ 371 strcpy((char *)card->dai_link[2].codec_name, codec_name); 372 snd_soc_card_set_drvdata(card, drv); 373 ret_val = devm_snd_soc_register_card(&pdev->dev, card); 374 if (ret_val) { 375 dev_err(&pdev->dev, 376 "snd_soc_register_card failed %d\n", ret_val); 377 return ret_val; 378 } 379 platform_set_drvdata(pdev, card); 380 return ret_val; 381 } 382 383 static struct platform_driver snd_cht_mc_driver = { 384 .driver = { 385 .name = "cht-bsw-rt5645", 386 }, 387 .probe = snd_cht_mc_probe, 388 }; 389 390 module_platform_driver(snd_cht_mc_driver) 391 392 MODULE_DESCRIPTION("ASoC Intel(R) Braswell Machine driver"); 393 MODULE_AUTHOR("Fang, Yang A,N,Harshapriya"); 394 MODULE_LICENSE("GPL v2"); 395 MODULE_ALIAS("platform:cht-bsw-rt5645"); 396