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 #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 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", SND_JACK_HEADSET | SND_JACK_BTN_0, jack, 78 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 dl->dpcm_capture = 1; 177 dl->dpcm_playback = 1; 178 179 *dai_link = dl; 180 181 return 0; 182 } 183 184 static int avs_card_suspend_pre(struct snd_soc_card *card) 185 { 186 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, RT298_CODEC_DAI); 187 188 return snd_soc_component_set_jack(codec_dai->component, NULL, NULL); 189 } 190 191 static int avs_card_resume_post(struct snd_soc_card *card) 192 { 193 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, RT298_CODEC_DAI); 194 struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card); 195 196 return snd_soc_component_set_jack(codec_dai->component, jack, NULL); 197 } 198 199 static int avs_rt298_probe(struct platform_device *pdev) 200 { 201 struct snd_soc_dai_link *dai_link; 202 struct snd_soc_acpi_mach *mach; 203 struct snd_soc_card *card; 204 struct snd_soc_jack *jack; 205 struct device *dev = &pdev->dev; 206 const char *pname; 207 int ssp_port, tdm_slot, ret; 208 209 mach = dev_get_platdata(dev); 210 pname = mach->mach_params.platform; 211 212 ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot); 213 if (ret) 214 return ret; 215 216 ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link); 217 if (ret) { 218 dev_err(dev, "Failed to create dai link: %d", ret); 219 return ret; 220 } 221 222 jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL); 223 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL); 224 if (!jack || !card) 225 return -ENOMEM; 226 227 card->name = "avs_rt298"; 228 card->dev = dev; 229 card->owner = THIS_MODULE; 230 card->suspend_pre = avs_card_suspend_pre; 231 card->resume_post = avs_card_resume_post; 232 card->dai_link = dai_link; 233 card->num_links = 1; 234 card->controls = card_controls; 235 card->num_controls = ARRAY_SIZE(card_controls); 236 card->dapm_widgets = card_widgets; 237 card->num_dapm_widgets = ARRAY_SIZE(card_widgets); 238 card->dapm_routes = card_base_routes; 239 card->num_dapm_routes = ARRAY_SIZE(card_base_routes); 240 card->fully_routed = true; 241 snd_soc_card_set_drvdata(card, jack); 242 243 ret = snd_soc_fixup_dai_links_platform_name(card, pname); 244 if (ret) 245 return ret; 246 247 return devm_snd_soc_register_card(dev, card); 248 } 249 250 static const struct platform_device_id avs_rt298_driver_ids[] = { 251 { 252 .name = "avs_rt298", 253 }, 254 {}, 255 }; 256 MODULE_DEVICE_TABLE(platform, avs_rt298_driver_ids); 257 258 static struct platform_driver avs_rt298_driver = { 259 .probe = avs_rt298_probe, 260 .driver = { 261 .name = "avs_rt298", 262 .pm = &snd_soc_pm_ops, 263 }, 264 .id_table = avs_rt298_driver_ids, 265 }; 266 267 module_platform_driver(avs_rt298_driver); 268 269 MODULE_DESCRIPTION("Intel rt298 machine driver"); 270 MODULE_LICENSE("GPL"); 271