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