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