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