Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 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 <sound/pcm.h> |
| 23 | #include <sound/pcm_params.h> |
| 24 | #include <sound/soc.h> |
Jin Yao | 6b3b58d | 2015-03-17 10:23:31 +0800 | [diff] [blame] | 25 | #include <sound/jack.h> |
Jie Yang | e56c72d | 2015-04-02 15:37:02 +0800 | [diff] [blame] | 26 | #include "../../codecs/rt5670.h" |
Jie Yang | b97169d | 2015-04-02 15:37:04 +0800 | [diff] [blame] | 27 | #include "../atom/sst-atom-controls.h" |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 28 | |
| 29 | /* The platform clock #3 outputs 19.2Mhz clock to codec as I2S MCLK */ |
| 30 | #define CHT_PLAT_CLK_3_HZ 19200000 |
| 31 | #define CHT_CODEC_DAI "rt5670-aif1" |
| 32 | |
Jin Yao | 6b3b58d | 2015-03-17 10:23:31 +0800 | [diff] [blame] | 33 | static struct snd_soc_jack cht_bsw_headset; |
| 34 | |
| 35 | /* Headset jack detection DAPM pins */ |
| 36 | static struct snd_soc_jack_pin cht_bsw_headset_pins[] = { |
| 37 | { |
| 38 | .pin = "Headset Mic", |
| 39 | .mask = SND_JACK_MICROPHONE, |
| 40 | }, |
| 41 | { |
| 42 | .pin = "Headphone", |
| 43 | .mask = SND_JACK_HEADPHONE, |
| 44 | }, |
| 45 | }; |
| 46 | |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 47 | static inline struct snd_soc_dai *cht_get_codec_dai(struct snd_soc_card *card) |
| 48 | { |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame^] | 49 | struct snd_soc_pcm_runtime *rtd; |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 50 | |
Mengdong Lin | 1a49798 | 2015-11-18 02:34:11 -0500 | [diff] [blame^] | 51 | list_for_each_entry(rtd, &card->rtd_list, list) { |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 52 | if (!strncmp(rtd->codec_dai->name, CHT_CODEC_DAI, |
| 53 | strlen(CHT_CODEC_DAI))) |
| 54 | return rtd->codec_dai; |
| 55 | } |
| 56 | return NULL; |
| 57 | } |
| 58 | |
| 59 | static int platform_clock_control(struct snd_soc_dapm_widget *w, |
| 60 | struct snd_kcontrol *k, int event) |
| 61 | { |
| 62 | struct snd_soc_dapm_context *dapm = w->dapm; |
| 63 | struct snd_soc_card *card = dapm->card; |
| 64 | struct snd_soc_dai *codec_dai; |
Jin Yao | 8d0c38a | 2015-03-10 09:05:38 +0800 | [diff] [blame] | 65 | int ret; |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 66 | |
| 67 | codec_dai = cht_get_codec_dai(card); |
| 68 | if (!codec_dai) { |
| 69 | dev_err(card->dev, "Codec dai not found; Unable to set platform clock\n"); |
| 70 | return -EIO; |
| 71 | } |
| 72 | |
Jin Yao | 8d0c38a | 2015-03-10 09:05:38 +0800 | [diff] [blame] | 73 | if (SND_SOC_DAPM_EVENT_ON(event)) { |
| 74 | /* set codec PLL source to the 19.2MHz platform clock (MCLK) */ |
| 75 | ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK, |
| 76 | CHT_PLAT_CLK_3_HZ, 48000 * 512); |
| 77 | if (ret < 0) { |
| 78 | dev_err(card->dev, "can't set codec pll: %d\n", ret); |
| 79 | return ret; |
| 80 | } |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 81 | |
Jin Yao | 8d0c38a | 2015-03-10 09:05:38 +0800 | [diff] [blame] | 82 | /* set codec sysclk source to PLL */ |
| 83 | ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1, |
| 84 | 48000 * 512, SND_SOC_CLOCK_IN); |
| 85 | if (ret < 0) { |
| 86 | dev_err(card->dev, "can't set codec sysclk: %d\n", ret); |
| 87 | return ret; |
| 88 | } |
| 89 | } else { |
| 90 | /* Set codec sysclk source to its internal clock because codec |
| 91 | * PLL will be off when idle and MCLK will also be off by ACPI |
| 92 | * when codec is runtime suspended. Codec needs clock for jack |
| 93 | * detection and button press. |
| 94 | */ |
| 95 | snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_RCCLK, |
| 96 | 48000 * 512, SND_SOC_CLOCK_IN); |
| 97 | } |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static const struct snd_soc_dapm_widget cht_dapm_widgets[] = { |
| 102 | SND_SOC_DAPM_HP("Headphone", NULL), |
| 103 | SND_SOC_DAPM_MIC("Headset Mic", NULL), |
| 104 | SND_SOC_DAPM_MIC("Int Mic", NULL), |
| 105 | SND_SOC_DAPM_SPK("Ext Spk", NULL), |
| 106 | SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, |
Jin Yao | 8d0c38a | 2015-03-10 09:05:38 +0800 | [diff] [blame] | 107 | platform_clock_control, SND_SOC_DAPM_PRE_PMU | |
| 108 | SND_SOC_DAPM_POST_PMD), |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | static const struct snd_soc_dapm_route cht_audio_map[] = { |
| 112 | {"IN1P", NULL, "Headset Mic"}, |
| 113 | {"IN1N", NULL, "Headset Mic"}, |
| 114 | {"DMIC L1", NULL, "Int Mic"}, |
| 115 | {"DMIC R1", NULL, "Int Mic"}, |
| 116 | {"Headphone", NULL, "HPOL"}, |
| 117 | {"Headphone", NULL, "HPOR"}, |
| 118 | {"Ext Spk", NULL, "SPOLP"}, |
| 119 | {"Ext Spk", NULL, "SPOLN"}, |
| 120 | {"Ext Spk", NULL, "SPORP"}, |
| 121 | {"Ext Spk", NULL, "SPORN"}, |
| 122 | {"AIF1 Playback", NULL, "ssp2 Tx"}, |
| 123 | {"ssp2 Tx", NULL, "codec_out0"}, |
| 124 | {"ssp2 Tx", NULL, "codec_out1"}, |
| 125 | {"codec_in0", NULL, "ssp2 Rx"}, |
| 126 | {"codec_in1", NULL, "ssp2 Rx"}, |
| 127 | {"ssp2 Rx", NULL, "AIF1 Capture"}, |
| 128 | {"Headphone", NULL, "Platform Clock"}, |
| 129 | {"Headset Mic", NULL, "Platform Clock"}, |
| 130 | {"Int Mic", NULL, "Platform Clock"}, |
| 131 | {"Ext Spk", NULL, "Platform Clock"}, |
| 132 | }; |
| 133 | |
| 134 | static const struct snd_kcontrol_new cht_mc_controls[] = { |
| 135 | SOC_DAPM_PIN_SWITCH("Headphone"), |
| 136 | SOC_DAPM_PIN_SWITCH("Headset Mic"), |
| 137 | SOC_DAPM_PIN_SWITCH("Int Mic"), |
| 138 | SOC_DAPM_PIN_SWITCH("Ext Spk"), |
| 139 | }; |
| 140 | |
| 141 | static int cht_aif1_hw_params(struct snd_pcm_substream *substream, |
| 142 | struct snd_pcm_hw_params *params) |
| 143 | { |
| 144 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 145 | struct snd_soc_dai *codec_dai = rtd->codec_dai; |
| 146 | int ret; |
| 147 | |
| 148 | /* set codec PLL source to the 19.2MHz platform clock (MCLK) */ |
| 149 | ret = snd_soc_dai_set_pll(codec_dai, 0, RT5670_PLL1_S_MCLK, |
| 150 | CHT_PLAT_CLK_3_HZ, params_rate(params) * 512); |
| 151 | if (ret < 0) { |
| 152 | dev_err(rtd->dev, "can't set codec pll: %d\n", ret); |
| 153 | return ret; |
| 154 | } |
| 155 | |
| 156 | /* set codec sysclk source to PLL */ |
| 157 | ret = snd_soc_dai_set_sysclk(codec_dai, RT5670_SCLK_S_PLL1, |
| 158 | params_rate(params) * 512, |
| 159 | SND_SOC_CLOCK_IN); |
| 160 | if (ret < 0) { |
| 161 | dev_err(rtd->dev, "can't set codec sysclk: %d\n", ret); |
| 162 | return ret; |
| 163 | } |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int cht_codec_init(struct snd_soc_pcm_runtime *runtime) |
| 168 | { |
| 169 | int ret; |
| 170 | struct snd_soc_dai *codec_dai = runtime->codec_dai; |
Mengdong Lin | eb55fab | 2015-01-07 10:19:23 +0800 | [diff] [blame] | 171 | struct snd_soc_codec *codec = codec_dai->codec; |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 172 | |
| 173 | /* TDM 4 slots 24 bit, set Rx & Tx bitmask to 4 active slots */ |
| 174 | ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xF, 0xF, 4, 24); |
| 175 | if (ret < 0) { |
| 176 | dev_err(runtime->dev, "can't set codec TDM slot %d\n", ret); |
| 177 | return ret; |
| 178 | } |
| 179 | |
Mengdong Lin | eb55fab | 2015-01-07 10:19:23 +0800 | [diff] [blame] | 180 | /* Select codec ASRC clock source to track I2S1 clock, because codec |
| 181 | * is in slave mode and 100fs I2S format (BCLK = 100 * LRCLK) cannot |
| 182 | * be supported by RT5672. Otherwise, ASRC will be disabled and cause |
| 183 | * noise. |
| 184 | */ |
| 185 | rt5670_sel_asrc_clk_src(codec, |
| 186 | RT5670_DA_STEREO_FILTER |
| 187 | | RT5670_DA_MONO_L_FILTER |
| 188 | | RT5670_DA_MONO_R_FILTER |
| 189 | | RT5670_AD_STEREO_FILTER |
| 190 | | RT5670_AD_MONO_L_FILTER |
| 191 | | RT5670_AD_MONO_R_FILTER, |
| 192 | RT5670_CLK_SEL_I2S1_ASRC); |
Jin Yao | 6b3b58d | 2015-03-17 10:23:31 +0800 | [diff] [blame] | 193 | |
| 194 | ret = snd_soc_card_jack_new(runtime->card, "Headset", |
| 195 | SND_JACK_HEADSET | SND_JACK_BTN_0 | |
| 196 | SND_JACK_BTN_1 | SND_JACK_BTN_2, &cht_bsw_headset, |
| 197 | cht_bsw_headset_pins, ARRAY_SIZE(cht_bsw_headset_pins)); |
| 198 | if (ret) |
| 199 | return ret; |
| 200 | |
| 201 | rt5670_set_jack_detect(codec, &cht_bsw_headset); |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int cht_codec_fixup(struct snd_soc_pcm_runtime *rtd, |
| 206 | struct snd_pcm_hw_params *params) |
| 207 | { |
| 208 | struct snd_interval *rate = hw_param_interval(params, |
| 209 | SNDRV_PCM_HW_PARAM_RATE); |
| 210 | struct snd_interval *channels = hw_param_interval(params, |
| 211 | SNDRV_PCM_HW_PARAM_CHANNELS); |
| 212 | |
| 213 | /* The DSP will covert the FE rate to 48k, stereo, 24bits */ |
| 214 | rate->min = rate->max = 48000; |
| 215 | channels->min = channels->max = 2; |
| 216 | |
| 217 | /* set SSP2 to 24-bit */ |
Fang, Yang A | 369a9f5 | 2015-02-09 00:18:12 -0800 | [diff] [blame] | 218 | params_set_format(params, SNDRV_PCM_FORMAT_S24_LE); |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 222 | static int cht_aif1_startup(struct snd_pcm_substream *substream) |
| 223 | { |
Lars-Peter Clausen | a5be88f | 2015-10-18 15:39:33 +0200 | [diff] [blame] | 224 | return snd_pcm_hw_constraint_single(substream->runtime, |
| 225 | SNDRV_PCM_HW_PARAM_RATE, 48000); |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static struct snd_soc_ops cht_aif1_ops = { |
| 229 | .startup = cht_aif1_startup, |
| 230 | }; |
| 231 | |
| 232 | static struct snd_soc_ops cht_be_ssp2_ops = { |
| 233 | .hw_params = cht_aif1_hw_params, |
| 234 | }; |
| 235 | |
| 236 | static struct snd_soc_dai_link cht_dailink[] = { |
| 237 | /* Front End DAI links */ |
| 238 | [MERR_DPCM_AUDIO] = { |
| 239 | .name = "Audio Port", |
| 240 | .stream_name = "Audio", |
| 241 | .cpu_dai_name = "media-cpu-dai", |
| 242 | .codec_dai_name = "snd-soc-dummy-dai", |
| 243 | .codec_name = "snd-soc-dummy", |
| 244 | .platform_name = "sst-mfld-platform", |
Vinod Koul | 76ca1c2 | 2015-02-12 09:59:54 +0530 | [diff] [blame] | 245 | .nonatomic = true, |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 246 | .dynamic = 1, |
| 247 | .dpcm_playback = 1, |
| 248 | .dpcm_capture = 1, |
| 249 | .ops = &cht_aif1_ops, |
| 250 | }, |
| 251 | [MERR_DPCM_COMPR] = { |
| 252 | .name = "Compressed Port", |
| 253 | .stream_name = "Compress", |
| 254 | .cpu_dai_name = "compress-cpu-dai", |
| 255 | .codec_dai_name = "snd-soc-dummy-dai", |
| 256 | .codec_name = "snd-soc-dummy", |
| 257 | .platform_name = "sst-mfld-platform", |
| 258 | }, |
| 259 | |
| 260 | /* Back End DAI links */ |
| 261 | { |
| 262 | /* SSP2 - Codec */ |
| 263 | .name = "SSP2-Codec", |
| 264 | .be_id = 1, |
| 265 | .cpu_dai_name = "ssp2-port", |
| 266 | .platform_name = "sst-mfld-platform", |
| 267 | .no_pcm = 1, |
Vinod Koul | 76ca1c2 | 2015-02-12 09:59:54 +0530 | [diff] [blame] | 268 | .nonatomic = true, |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 269 | .codec_dai_name = "rt5670-aif1", |
| 270 | .codec_name = "i2c-10EC5670:00", |
| 271 | .dai_fmt = SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF |
| 272 | | SND_SOC_DAIFMT_CBS_CFS, |
| 273 | .init = cht_codec_init, |
| 274 | .be_hw_params_fixup = cht_codec_fixup, |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 275 | .dpcm_playback = 1, |
| 276 | .dpcm_capture = 1, |
| 277 | .ops = &cht_be_ssp2_ops, |
| 278 | }, |
| 279 | }; |
| 280 | |
Jin Yao | 066d7b8 | 2015-03-17 10:23:30 +0800 | [diff] [blame] | 281 | static int cht_suspend_pre(struct snd_soc_card *card) |
| 282 | { |
| 283 | struct snd_soc_codec *codec; |
| 284 | |
| 285 | list_for_each_entry(codec, &card->codec_dev_list, card_list) { |
| 286 | if (!strcmp(codec->component.name, "i2c-10EC5670:00")) { |
| 287 | dev_dbg(codec->dev, "disabling jack detect before going to suspend.\n"); |
| 288 | rt5670_jack_suspend(codec); |
| 289 | break; |
| 290 | } |
| 291 | } |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | static int cht_resume_post(struct snd_soc_card *card) |
| 296 | { |
| 297 | struct snd_soc_codec *codec; |
| 298 | |
| 299 | list_for_each_entry(codec, &card->codec_dev_list, card_list) { |
| 300 | if (!strcmp(codec->component.name, "i2c-10EC5670:00")) { |
| 301 | dev_dbg(codec->dev, "enabling jack detect for resume.\n"); |
| 302 | rt5670_jack_resume(codec); |
| 303 | break; |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | return 0; |
| 308 | } |
| 309 | |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 310 | /* SoC card */ |
| 311 | static struct snd_soc_card snd_soc_card_cht = { |
| 312 | .name = "cherrytrailcraudio", |
Axel Lin | 54d8697 | 2015-08-21 20:59:21 +0800 | [diff] [blame] | 313 | .owner = THIS_MODULE, |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 314 | .dai_link = cht_dailink, |
| 315 | .num_links = ARRAY_SIZE(cht_dailink), |
| 316 | .dapm_widgets = cht_dapm_widgets, |
| 317 | .num_dapm_widgets = ARRAY_SIZE(cht_dapm_widgets), |
| 318 | .dapm_routes = cht_audio_map, |
| 319 | .num_dapm_routes = ARRAY_SIZE(cht_audio_map), |
| 320 | .controls = cht_mc_controls, |
| 321 | .num_controls = ARRAY_SIZE(cht_mc_controls), |
Jin Yao | 066d7b8 | 2015-03-17 10:23:30 +0800 | [diff] [blame] | 322 | .suspend_pre = cht_suspend_pre, |
| 323 | .resume_post = cht_resume_post, |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | static int snd_cht_mc_probe(struct platform_device *pdev) |
| 327 | { |
| 328 | int ret_val = 0; |
| 329 | |
| 330 | /* register the soc card */ |
| 331 | snd_soc_card_cht.dev = &pdev->dev; |
| 332 | ret_val = devm_snd_soc_register_card(&pdev->dev, &snd_soc_card_cht); |
| 333 | if (ret_val) { |
| 334 | dev_err(&pdev->dev, |
| 335 | "snd_soc_register_card failed %d\n", ret_val); |
| 336 | return ret_val; |
| 337 | } |
| 338 | platform_set_drvdata(pdev, &snd_soc_card_cht); |
| 339 | return ret_val; |
| 340 | } |
| 341 | |
| 342 | static struct platform_driver snd_cht_mc_driver = { |
| 343 | .driver = { |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 344 | .name = "cht-bsw-rt5672", |
Mengdong Lin | 026da22 | 2014-11-21 16:08:59 +0800 | [diff] [blame] | 345 | }, |
| 346 | .probe = snd_cht_mc_probe, |
| 347 | }; |
| 348 | |
| 349 | module_platform_driver(snd_cht_mc_driver); |
| 350 | |
| 351 | MODULE_DESCRIPTION("ASoC Intel(R) Baytrail CR Machine driver"); |
| 352 | MODULE_AUTHOR("Subhransu S. Prusty, Mengdong Lin"); |
| 353 | MODULE_LICENSE("GPL v2"); |
| 354 | MODULE_ALIAS("platform:cht-bsw-rt5672"); |