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/dmi.h> 10 #include <linux/module.h> 11 #include <sound/jack.h> 12 #include <sound/pcm.h> 13 #include <sound/pcm_params.h> 14 #include <sound/soc.h> 15 #include <sound/soc-acpi.h> 16 #include "../../../codecs/rt298.h" 17 18 #define RT298_CODEC_DAI "rt298-aif1" 19 20 static const struct dmi_system_id kblr_dmi_table[] = { 21 { 22 .matches = { 23 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"), 24 DMI_MATCH(DMI_BOARD_NAME, "Kabylake R DDR4 RVP"), 25 }, 26 }, 27 {} 28 }; 29 30 static const struct snd_kcontrol_new card_controls[] = { 31 SOC_DAPM_PIN_SWITCH("Headphone Jack"), 32 SOC_DAPM_PIN_SWITCH("Mic Jack"), 33 SOC_DAPM_PIN_SWITCH("Speaker"), 34 }; 35 36 static const struct snd_soc_dapm_widget card_widgets[] = { 37 SND_SOC_DAPM_HP("Headphone Jack", NULL), 38 SND_SOC_DAPM_MIC("Mic Jack", NULL), 39 SND_SOC_DAPM_SPK("Speaker", NULL), 40 }; 41 42 static const struct snd_soc_dapm_route card_base_routes[] = { 43 /* HP jack connectors - unknown if we have jack detect */ 44 {"Headphone Jack", NULL, "HPO Pin"}, 45 {"MIC1", NULL, "Mic Jack"}, 46 47 {"Speaker", NULL, "SPOR"}, 48 {"Speaker", NULL, "SPOL"}, 49 }; 50 51 static struct snd_soc_jack_pin card_headset_pins[] = { 52 { 53 .pin = "Headphone Jack", 54 .mask = SND_JACK_HEADPHONE, 55 }, 56 { 57 .pin = "Mic Jack", 58 .mask = SND_JACK_MICROPHONE, 59 }, 60 }; 61 62 static int avs_rt298_codec_init(struct snd_soc_pcm_runtime *runtime) 63 { 64 struct snd_soc_card *card = runtime->card; 65 struct snd_soc_jack_pin *pins; 66 struct snd_soc_jack *jack; 67 int num_pins, ret; 68 69 jack = snd_soc_card_get_drvdata(card); 70 num_pins = ARRAY_SIZE(card_headset_pins); 71 72 pins = devm_kmemdup(card->dev, card_headset_pins, sizeof(*pins) * num_pins, GFP_KERNEL); 73 if (!pins) 74 return -ENOMEM; 75 76 ret = snd_soc_card_jack_new_pins(card, "Headset", SND_JACK_HEADSET | SND_JACK_BTN_0, jack, 77 pins, num_pins); 78 if (ret) 79 return ret; 80 81 return snd_soc_component_set_jack(asoc_rtd_to_codec(runtime, 0)->component, jack, NULL); 82 } 83 84 static void avs_rt298_codec_exit(struct snd_soc_pcm_runtime *rtd) 85 { 86 snd_soc_component_set_jack(asoc_rtd_to_codec(rtd, 0)->component, NULL, NULL); 87 } 88 89 static int avs_rt298_be_fixup(struct snd_soc_pcm_runtime *runtime, struct snd_pcm_hw_params *params) 90 { 91 struct snd_interval *rate, *channels; 92 struct snd_mask *fmt; 93 94 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 95 channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 96 fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 97 98 /* The ADSP will convert the FE rate to 48k, stereo */ 99 rate->min = rate->max = 48000; 100 channels->min = channels->max = 2; 101 102 /* set SSP0 to 24 bit */ 103 snd_mask_none(fmt); 104 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE); 105 106 return 0; 107 } 108 109 static int 110 avs_rt298_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params) 111 { 112 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); 113 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0); 114 unsigned int clk_freq; 115 int ret; 116 117 if (dmi_first_match(kblr_dmi_table)) 118 clk_freq = 24000000; 119 else 120 clk_freq = 19200000; 121 122 ret = snd_soc_dai_set_sysclk(codec_dai, RT298_SCLK_S_PLL, clk_freq, SND_SOC_CLOCK_IN); 123 if (ret < 0) 124 dev_err(rtd->dev, "Set codec sysclk failed: %d\n", ret); 125 126 return ret; 127 } 128 129 static const struct snd_soc_ops avs_rt298_ops = { 130 .hw_params = avs_rt298_hw_params, 131 }; 132 133 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port, 134 struct snd_soc_dai_link **dai_link) 135 { 136 struct snd_soc_dai_link_component *platform; 137 struct snd_soc_dai_link *dl; 138 139 dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL); 140 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL); 141 if (!dl || !platform) 142 return -ENOMEM; 143 144 platform->name = platform_name; 145 146 dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port); 147 dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL); 148 dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL); 149 if (!dl->name || !dl->cpus || !dl->codecs) 150 return -ENOMEM; 151 152 dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port); 153 dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-INT343A:00"); 154 dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, RT298_CODEC_DAI); 155 if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name) 156 return -ENOMEM; 157 158 dl->num_cpus = 1; 159 dl->num_codecs = 1; 160 dl->platforms = platform; 161 dl->num_platforms = 1; 162 dl->id = 0; 163 if (dmi_first_match(kblr_dmi_table)) 164 dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS; 165 else 166 dl->dai_fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS; 167 dl->init = avs_rt298_codec_init; 168 dl->exit = avs_rt298_codec_exit; 169 dl->be_hw_params_fixup = avs_rt298_be_fixup; 170 dl->ops = &avs_rt298_ops; 171 dl->nonatomic = 1; 172 dl->no_pcm = 1; 173 dl->dpcm_capture = 1; 174 dl->dpcm_playback = 1; 175 176 *dai_link = dl; 177 178 return 0; 179 } 180 181 static int avs_card_suspend_pre(struct snd_soc_card *card) 182 { 183 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, RT298_CODEC_DAI); 184 185 return snd_soc_component_set_jack(codec_dai->component, NULL, NULL); 186 } 187 188 static int avs_card_resume_post(struct snd_soc_card *card) 189 { 190 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, RT298_CODEC_DAI); 191 struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card); 192 193 return snd_soc_component_set_jack(codec_dai->component, jack, NULL); 194 } 195 196 static int avs_rt298_probe(struct platform_device *pdev) 197 { 198 struct snd_soc_dai_link *dai_link; 199 struct snd_soc_acpi_mach *mach; 200 struct snd_soc_card *card; 201 struct snd_soc_jack *jack; 202 struct device *dev = &pdev->dev; 203 const char *pname; 204 int ssp_port, ret; 205 206 mach = dev_get_platdata(dev); 207 pname = mach->mach_params.platform; 208 ssp_port = __ffs(mach->mach_params.i2s_link_mask); 209 210 ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link); 211 if (ret) { 212 dev_err(dev, "Failed to create dai link: %d", ret); 213 return ret; 214 } 215 216 jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL); 217 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL); 218 if (!jack || !card) 219 return -ENOMEM; 220 221 card->name = "avs_rt298"; 222 card->dev = dev; 223 card->owner = THIS_MODULE; 224 card->suspend_pre = avs_card_suspend_pre; 225 card->resume_post = avs_card_resume_post; 226 card->dai_link = dai_link; 227 card->num_links = 1; 228 card->controls = card_controls; 229 card->num_controls = ARRAY_SIZE(card_controls); 230 card->dapm_widgets = card_widgets; 231 card->num_dapm_widgets = ARRAY_SIZE(card_widgets); 232 card->dapm_routes = card_base_routes; 233 card->num_dapm_routes = ARRAY_SIZE(card_base_routes); 234 card->fully_routed = true; 235 snd_soc_card_set_drvdata(card, jack); 236 237 ret = snd_soc_fixup_dai_links_platform_name(card, pname); 238 if (ret) 239 return ret; 240 241 return devm_snd_soc_register_card(dev, card); 242 } 243 244 static struct platform_driver avs_rt298_driver = { 245 .probe = avs_rt298_probe, 246 .driver = { 247 .name = "avs_rt298", 248 .pm = &snd_soc_pm_ops, 249 }, 250 }; 251 252 module_platform_driver(avs_rt298_driver); 253 254 MODULE_LICENSE("GPL"); 255 MODULE_ALIAS("platform:avs_rt298"); 256