1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved. 4 // 5 // Author: Cezary Rojewski <cezary.rojewski@intel.com> 6 // 7 8 #include <linux/module.h> 9 #include <linux/platform_data/x86/soc.h> 10 #include <linux/platform_device.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 <sound/soc-dapm.h> 17 #include <uapi/linux/input-event-codes.h> 18 #include "../../../codecs/da7219.h" 19 20 #define DA7219_DAI_NAME "da7219-hifi" 21 22 static const struct snd_kcontrol_new card_controls[] = { 23 SOC_DAPM_PIN_SWITCH("Headphone Jack"), 24 SOC_DAPM_PIN_SWITCH("Headset Mic"), 25 }; 26 27 static int platform_clock_control(struct snd_soc_dapm_widget *w, 28 struct snd_kcontrol *k, int event) 29 { 30 struct snd_soc_dapm_context *dapm = w->dapm; 31 struct snd_soc_card *card = dapm->card; 32 struct snd_soc_dai *codec_dai; 33 int ret = 0; 34 35 codec_dai = snd_soc_card_get_codec_dai(card, DA7219_DAI_NAME); 36 if (!codec_dai) { 37 dev_err(card->dev, "Codec dai not found. Unable to set/unset codec pll\n"); 38 return -EIO; 39 } 40 41 if (SND_SOC_DAPM_EVENT_OFF(event)) { 42 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0); 43 if (ret) 44 dev_err(card->dev, "failed to stop PLL: %d\n", ret); 45 } else if (SND_SOC_DAPM_EVENT_ON(event)) { 46 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_PLL_SRM, 47 0, DA7219_PLL_FREQ_OUT_98304); 48 if (ret) 49 dev_err(card->dev, "failed to start PLL: %d\n", ret); 50 } 51 52 return ret; 53 } 54 55 static const struct snd_soc_dapm_widget card_widgets[] = { 56 SND_SOC_DAPM_HP("Headphone Jack", NULL), 57 SND_SOC_DAPM_MIC("Headset Mic", NULL), 58 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0, platform_clock_control, 59 SND_SOC_DAPM_POST_PMD | SND_SOC_DAPM_PRE_PMU), 60 }; 61 62 static const struct snd_soc_dapm_route card_base_routes[] = { 63 /* HP jack connectors - unknown if we have jack detection */ 64 {"Headphone Jack", NULL, "HPL"}, 65 {"Headphone Jack", NULL, "HPR"}, 66 67 {"MIC", NULL, "Headset Mic"}, 68 69 { "Headphone Jack", NULL, "Platform Clock" }, 70 { "Headset Mic", NULL, "Platform Clock" }, 71 }; 72 73 static int avs_da7219_codec_init(struct snd_soc_pcm_runtime *runtime) 74 { 75 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(runtime, 0); 76 struct snd_soc_component *component = codec_dai->component; 77 struct snd_soc_card *card = runtime->card; 78 struct snd_soc_jack *jack; 79 int clk_freq; 80 int ret; 81 82 jack = snd_soc_card_get_drvdata(card); 83 if (soc_intel_is_apl()) 84 clk_freq = 19200000; 85 else /* kbl */ 86 clk_freq = 24576000; 87 88 ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK, clk_freq, SND_SOC_CLOCK_IN); 89 if (ret) { 90 dev_err(card->dev, "can't set codec sysclk configuration\n"); 91 return ret; 92 } 93 94 /* 95 * Headset buttons map to the google Reference headset. 96 * These can be configured by userspace. 97 */ 98 ret = snd_soc_card_jack_new(card, "Headset Jack", 99 SND_JACK_HEADSET | SND_JACK_BTN_0 | 100 SND_JACK_BTN_1 | SND_JACK_BTN_2 | 101 SND_JACK_BTN_3 | SND_JACK_LINEOUT, jack); 102 if (ret) { 103 dev_err(card->dev, "Headset Jack creation failed: %d\n", ret); 104 return ret; 105 } 106 107 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); 108 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP); 109 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN); 110 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND); 111 112 return snd_soc_component_set_jack(component, jack, NULL); 113 } 114 115 static void avs_da7219_codec_exit(struct snd_soc_pcm_runtime *rtd) 116 { 117 snd_soc_component_set_jack(asoc_rtd_to_codec(rtd, 0)->component, NULL, NULL); 118 } 119 120 static int 121 avs_da7219_be_fixup(struct snd_soc_pcm_runtime *runrime, struct snd_pcm_hw_params *params) 122 { 123 struct snd_interval *rate, *channels; 124 struct snd_mask *fmt; 125 126 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 127 channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 128 fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 129 130 /* The ADSP will convert the FE rate to 48k, stereo */ 131 rate->min = rate->max = 48000; 132 channels->min = channels->max = 2; 133 134 /* set SSP0 to 24 bit */ 135 snd_mask_none(fmt); 136 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_LE); 137 return 0; 138 } 139 140 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port, 141 struct snd_soc_dai_link **dai_link) 142 { 143 struct snd_soc_dai_link_component *platform; 144 struct snd_soc_dai_link *dl; 145 146 dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL); 147 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL); 148 if (!dl || !platform) 149 return -ENOMEM; 150 151 platform->name = platform_name; 152 153 dl->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_port); 154 dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL); 155 dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL); 156 if (!dl->name || !dl->cpus || !dl->codecs) 157 return -ENOMEM; 158 159 dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_port); 160 dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-DLGS7219:00"); 161 dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, DA7219_DAI_NAME); 162 if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name) 163 return -ENOMEM; 164 165 dl->num_cpus = 1; 166 dl->num_codecs = 1; 167 dl->platforms = platform; 168 dl->num_platforms = 1; 169 dl->id = 0; 170 dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBS_CFS; 171 dl->be_hw_params_fixup = avs_da7219_be_fixup; 172 dl->init = avs_da7219_codec_init; 173 dl->exit = avs_da7219_codec_exit; 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_create_dapm_routes(struct device *dev, int ssp_port, 185 struct snd_soc_dapm_route **routes, int *num_routes) 186 { 187 struct snd_soc_dapm_route *dr; 188 const int num_base = ARRAY_SIZE(card_base_routes); 189 const int num_dr = num_base + 2; 190 int idx; 191 192 dr = devm_kcalloc(dev, num_dr, sizeof(*dr), GFP_KERNEL); 193 if (!dr) 194 return -ENOMEM; 195 196 memcpy(dr, card_base_routes, num_base * sizeof(*dr)); 197 198 idx = num_base; 199 dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "Playback"); 200 dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Tx", ssp_port); 201 if (!dr[idx].sink || !dr[idx].source) 202 return -ENOMEM; 203 204 idx++; 205 dr[idx].sink = devm_kasprintf(dev, GFP_KERNEL, "ssp%d Rx", ssp_port); 206 dr[idx].source = devm_kasprintf(dev, GFP_KERNEL, "Capture"); 207 if (!dr[idx].sink || !dr[idx].source) 208 return -ENOMEM; 209 210 *routes = dr; 211 *num_routes = num_dr; 212 213 return 0; 214 } 215 216 static int avs_card_suspend_pre(struct snd_soc_card *card) 217 { 218 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, DA7219_DAI_NAME); 219 220 return snd_soc_component_set_jack(codec_dai->component, NULL, NULL); 221 } 222 223 static int avs_card_resume_post(struct snd_soc_card *card) 224 { 225 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, DA7219_DAI_NAME); 226 struct snd_soc_jack *jack = snd_soc_card_get_drvdata(card); 227 228 return snd_soc_component_set_jack(codec_dai->component, jack, NULL); 229 } 230 231 static int avs_da7219_probe(struct platform_device *pdev) 232 { 233 struct snd_soc_dapm_route *routes; 234 struct snd_soc_dai_link *dai_link; 235 struct snd_soc_acpi_mach *mach; 236 struct snd_soc_card *card; 237 struct snd_soc_jack *jack; 238 struct device *dev = &pdev->dev; 239 const char *pname; 240 int num_routes, ssp_port, ret; 241 242 mach = dev_get_platdata(dev); 243 pname = mach->mach_params.platform; 244 ssp_port = __ffs(mach->mach_params.i2s_link_mask); 245 246 ret = avs_create_dai_link(dev, pname, ssp_port, &dai_link); 247 if (ret) { 248 dev_err(dev, "Failed to create dai link: %d", ret); 249 return ret; 250 } 251 252 ret = avs_create_dapm_routes(dev, ssp_port, &routes, &num_routes); 253 if (ret) { 254 dev_err(dev, "Failed to create dapm routes: %d", ret); 255 return ret; 256 } 257 258 jack = devm_kzalloc(dev, sizeof(*jack), GFP_KERNEL); 259 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL); 260 if (!jack || !card) 261 return -ENOMEM; 262 263 card->name = "avs_da7219"; 264 card->dev = dev; 265 card->owner = THIS_MODULE; 266 card->suspend_pre = avs_card_suspend_pre; 267 card->resume_post = avs_card_resume_post; 268 card->dai_link = dai_link; 269 card->num_links = 1; 270 card->controls = card_controls; 271 card->num_controls = ARRAY_SIZE(card_controls); 272 card->dapm_widgets = card_widgets; 273 card->num_dapm_widgets = ARRAY_SIZE(card_widgets); 274 card->dapm_routes = routes; 275 card->num_dapm_routes = num_routes; 276 card->fully_routed = true; 277 snd_soc_card_set_drvdata(card, jack); 278 279 ret = snd_soc_fixup_dai_links_platform_name(card, pname); 280 if (ret) 281 return ret; 282 283 return devm_snd_soc_register_card(dev, card); 284 } 285 286 static struct platform_driver avs_da7219_driver = { 287 .probe = avs_da7219_probe, 288 .driver = { 289 .name = "avs_da7219", 290 .pm = &snd_soc_pm_ops, 291 }, 292 }; 293 294 module_platform_driver(avs_da7219_driver); 295 296 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>"); 297 MODULE_LICENSE("GPL"); 298 MODULE_ALIAS("platform:avs_da7219"); 299