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