1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved. 4 // 5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com> 6 // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com> 7 // 8 9 #include <linux/clk.h> 10 #include <linux/dmi.h> 11 #include <linux/i2c.h> 12 #include <linux/input.h> 13 #include <linux/module.h> 14 #include <linux/platform_device.h> 15 #include <sound/core.h> 16 #include <sound/jack.h> 17 #include <sound/pcm.h> 18 #include <sound/pcm_params.h> 19 #include <sound/rt5682.h> 20 #include <sound/soc.h> 21 #include <sound/soc-acpi.h> 22 #include "../../common/soc-intel-quirks.h" 23 #include "../../../codecs/rt5682.h" 24 25 #define AVS_RT5682_SSP_CODEC(quirk) ((quirk) & GENMASK(2, 0)) 26 #define AVS_RT5682_SSP_CODEC_MASK (GENMASK(2, 0)) 27 #define AVS_RT5682_MCLK_EN BIT(3) 28 #define AVS_RT5682_MCLK_24MHZ BIT(4) 29 #define AVS_RT5682_CODEC_DAI_NAME "rt5682-aif1" 30 31 /* Default: MCLK on, MCLK 19.2M, SSP0 */ 32 static unsigned long avs_rt5682_quirk = AVS_RT5682_MCLK_EN | AVS_RT5682_SSP_CODEC(0); 33 34 static int avs_rt5682_quirk_cb(const struct dmi_system_id *id) 35 { 36 avs_rt5682_quirk = (unsigned long)id->driver_data; 37 return 1; 38 } 39 40 static const struct dmi_system_id avs_rt5682_quirk_table[] = { 41 { 42 .callback = avs_rt5682_quirk_cb, 43 .matches = { 44 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), 45 DMI_MATCH(DMI_PRODUCT_NAME, "WhiskeyLake Client"), 46 }, 47 .driver_data = (void *)(AVS_RT5682_MCLK_EN | 48 AVS_RT5682_MCLK_24MHZ | 49 AVS_RT5682_SSP_CODEC(1)), 50 }, 51 { 52 .callback = avs_rt5682_quirk_cb, 53 .matches = { 54 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), 55 DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"), 56 }, 57 .driver_data = (void *)(AVS_RT5682_MCLK_EN | 58 AVS_RT5682_SSP_CODEC(0)), 59 }, 60 {} 61 }; 62 63 static const struct snd_kcontrol_new card_controls[] = { 64 SOC_DAPM_PIN_SWITCH("Headphone Jack"), 65 SOC_DAPM_PIN_SWITCH("Headset Mic"), 66 }; 67 68 static const struct snd_soc_dapm_widget card_widgets[] = { 69 SND_SOC_DAPM_HP("Headphone Jack", NULL), 70 SND_SOC_DAPM_MIC("Headset Mic", NULL), 71 }; 72 73 static const struct snd_soc_dapm_route card_base_routes[] = { 74 /* HP jack connectors - unknown if we have jack detect */ 75 { "Headphone Jack", NULL, "HPOL" }, 76 { "Headphone Jack", NULL, "HPOR" }, 77 78 /* other jacks */ 79 { "IN1P", NULL, "Headset Mic" }, 80 }; 81 82 static int avs_rt5682_codec_init(struct snd_soc_pcm_runtime *runtime) 83 { 84 struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component; 85 struct snd_soc_jack *jack; 86 struct snd_soc_card *card = runtime->card; 87 int ret; 88 89 jack = snd_soc_card_get_drvdata(card); 90 91 /* Need to enable ASRC function for 24MHz mclk rate */ 92 if ((avs_rt5682_quirk & AVS_RT5682_MCLK_EN) && 93 (avs_rt5682_quirk & AVS_RT5682_MCLK_24MHZ)) { 94 rt5682_sel_asrc_clk_src(component, RT5682_DA_STEREO1_FILTER | 95 RT5682_AD_STEREO1_FILTER, RT5682_CLK_SEL_I2S1_ASRC); 96 } 97 98 /* 99 * Headset buttons map to the google Reference headset. 100 * These can be configured by userspace. 101 */ 102 ret = snd_soc_card_jack_new(card, "Headset", SND_JACK_HEADSET | SND_JACK_BTN_0 | 103 SND_JACK_BTN_1 | SND_JACK_BTN_2 | SND_JACK_BTN_3, jack); 104 if (ret) { 105 dev_err(card->dev, "Headset Jack creation failed: %d\n", ret); 106 return ret; 107 } 108 109 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); 110 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND); 111 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP); 112 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN); 113 114 ret = snd_soc_component_set_jack(component, jack, NULL); 115 if (ret) { 116 dev_err(card->dev, "Headset Jack call-back failed: %d\n", ret); 117 return ret; 118 } 119 120 return 0; 121 }; 122 123 static void avs_rt5682_codec_exit(struct snd_soc_pcm_runtime *rtd) 124 { 125 snd_soc_component_set_jack(asoc_rtd_to_codec(rtd, 0)->component, NULL, NULL); 126 } 127 128 static int 129 avs_rt5682_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params) 130 { 131 struct snd_soc_pcm_runtime *runtime = asoc_substream_to_rtd(substream); 132 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(runtime, 0); 133 int clk_id, clk_freq; 134 int pll_out, ret; 135 136 if (avs_rt5682_quirk & AVS_RT5682_MCLK_EN) { 137 clk_id = RT5682_PLL1_S_MCLK; 138 if (avs_rt5682_quirk & AVS_RT5682_MCLK_24MHZ) 139 clk_freq = 24000000; 140 else 141 clk_freq = 19200000; 142 } else { 143 clk_id = RT5682_PLL1_S_BCLK1; 144 clk_freq = params_rate(params) * 50; 145 } 146 147 pll_out = params_rate(params) * 512; 148 149 ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out); 150 if (ret < 0) 151 dev_err(runtime->dev, "snd_soc_dai_set_pll err = %d\n", ret); 152 153 /* Configure sysclk for codec */ 154 ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL1, pll_out, SND_SOC_CLOCK_IN); 155 if (ret < 0) 156 dev_err(runtime->dev, "snd_soc_dai_set_sysclk err = %d\n", ret); 157 158 /* slot_width should equal or large than data length, set them be the same */ 159 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2, params_width(params)); 160 if (ret < 0) { 161 dev_err(runtime->dev, "set TDM slot err:%d\n", ret); 162 return ret; 163 } 164 165 return 0; 166 } 167 168 static const struct snd_soc_ops avs_rt5682_ops = { 169 .hw_params = avs_rt5682_hw_params, 170 }; 171 172 static int 173 avs_rt5682_be_fixup(struct snd_soc_pcm_runtime *runtime, struct snd_pcm_hw_params *params) 174 { 175 struct snd_interval *rate, *channels; 176 struct snd_mask *fmt; 177 178 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 179 channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 180 fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 181 182 /* The ADSP will convert the FE rate to 48k, stereo */ 183 rate->min = rate->max = 48000; 184 channels->min = channels->max = 2; 185 186 /* set SSPN to 24 bit */ 187 snd_mask_none(fmt); 188 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE); 189 190 return 0; 191 } 192 193 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port, 194 struct snd_soc_dai_link **dai_link) 195 { 196 struct snd_soc_dai_link_component *platform; 197 struct snd_soc_dai_link *dl; 198 199 dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL); 200 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL); 201 if (!dl || !platform) 202 return -ENOMEM; 203 204 platform->name = platform_name; 205 206 dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port); 207 dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL); 208 dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL); 209 if (!dl->name || !dl->cpus || !dl->codecs) 210 return -ENOMEM; 211 212 dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port); 213 dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-10EC5682:00"); 214 dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, AVS_RT5682_CODEC_DAI_NAME); 215 if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name) 216 return -ENOMEM; 217 218 dl->num_cpus = 1; 219 dl->num_codecs = 1; 220 dl->platforms = platform; 221 dl->num_platforms = 1; 222 dl->id = 0; 223 dl->init = avs_rt5682_codec_init; 224 dl->exit = avs_rt5682_codec_exit; 225 dl->be_hw_params_fixup = avs_rt5682_be_fixup; 226 dl->ops = &avs_rt5682_ops; 227 dl->nonatomic = 1; 228 dl->no_pcm = 1; 229 dl->dpcm_capture = 1; 230 dl->dpcm_playback = 1; 231 232 *dai_link = dl; 233 234 return 0; 235 } 236 237 static int avs_create_dapm_routes(struct device *dev, int ssp_port, 238 struct snd_soc_dapm_route **routes, int *num_routes) 239 { 240 struct snd_soc_dapm_route *dr; 241 const int num_base = ARRAY_SIZE(card_base_routes); 242 const int num_dr = num_base + 2; 243 int idx; 244 245 dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL); 246 if (!dr) 247 return -ENOMEM; 248 249 memcpy(dr, card_base_routes, num_base * sizeof(*dr)); 250 251 idx = num_base; 252 dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "AIF1 Playback"); 253 dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port); 254 if (!dr[idx].sink || !dr[idx].source) 255 return -ENOMEM; 256 257 idx++; 258 dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port); 259 dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "AIF1 Capture"); 260 if (!dr[idx].sink || !dr[idx].source) 261 return -ENOMEM; 262 263 *routes = dr; 264 *num_routes = num_dr; 265 266 return 0; 267 } 268 269 static int avs_card_suspend_pre(struct snd_soc_card *card) 270 { 271 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, AVS_RT5682_CODEC_DAI_NAME); 272 273 return snd_soc_component_set_jack(codec_dai->component, NULL, NULL); 274 } 275 276 static int avs_card_resume_post(struct snd_soc_card *card) 277 { 278 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, AVS_RT5682_CODEC_DAI_NAME); 279 struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card); 280 281 return snd_soc_component_set_jack(codec_dai->component, jack, NULL); 282 } 283 284 static int avs_rt5682_probe(struct platform_device *pdev) 285 { 286 struct snd_soc_dapm_route *routes; 287 struct snd_soc_dai_link *dai_link; 288 struct snd_soc_acpi_mach *mach; 289 struct snd_soc_card *card; 290 struct snd_soc_jack *jack; 291 struct device *dev = &pdev->dev; 292 const char *pname; 293 int num_routes, ssp_port, ret; 294 295 if (pdev->id_entry && pdev->id_entry->driver_data) 296 avs_rt5682_quirk = (unsigned long)pdev->id_entry->driver_data; 297 298 dmi_check_system(avs_rt5682_quirk_table); 299 dev_dbg(dev, "avs_rt5682_quirk = %lx\n", avs_rt5682_quirk); 300 301 mach = dev_get_platdata(dev); 302 pname = mach->mach_params.platform; 303 ssp_port = __ffs(mach->mach_params.i2s_link_mask); 304 305 ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link); 306 if (ret) { 307 dev_err(dev, "Failed to create dai link: %d", ret); 308 return ret; 309 } 310 311 ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes); 312 if (ret) { 313 dev_err(dev, "Failed to create dapm routes: %d", ret); 314 return ret; 315 } 316 317 jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL); 318 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL); 319 if (!jack || !card) 320 return -ENOMEM; 321 322 card->name = "avs_rt5682"; 323 card->dev = dev; 324 card->owner = THIS_MODULE; 325 card->suspend_pre = avs_card_suspend_pre; 326 card->resume_post = avs_card_resume_post; 327 card->dai_link = dai_link; 328 card->num_links = 1; 329 card->controls = card_controls; 330 card->num_controls = ARRAY_SIZE(card_controls); 331 card->dapm_widgets = card_widgets; 332 card->num_dapm_widgets = ARRAY_SIZE(card_widgets); 333 card->dapm_routes = routes; 334 card->num_dapm_routes = num_routes; 335 card->fully_routed = true; 336 snd_soc_card_set_drvdata(card, jack); 337 338 ret = snd_soc_fixup_dai_links_platform_name(card, pname); 339 if (ret) 340 return ret; 341 342 return devm_snd_soc_register_card(dev, card); 343 } 344 345 static struct platform_driver avs_rt5682_driver = { 346 .probe = avs_rt5682_probe, 347 .driver = { 348 .name = "avs_rt5682", 349 .pm = &snd_soc_pm_ops, 350 }, 351 }; 352 353 module_platform_driver(avs_rt5682_driver) 354 355 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>"); 356 MODULE_LICENSE("GPL"); 357 MODULE_ALIAS("platform:avs_rt5682"); 358