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