1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // Copyright(c) 2023 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/device.h> 10 #include <linux/gpio/consumer.h> 11 #include <linux/input.h> 12 #include <linux/module.h> 13 #include <linux/platform_device.h> 14 #include <linux/processor.h> 15 #include <linux/slab.h> 16 #include <sound/jack.h> 17 #include <sound/pcm.h> 18 #include <sound/pcm_params.h> 19 #include <sound/soc.h> 20 #include <sound/soc-acpi.h> 21 #include <asm/intel-family.h> 22 #include "../utils.h" 23 24 #define ES8336_CODEC_DAI "ES8316 HiFi" 25 26 struct avs_card_drvdata { 27 struct snd_soc_jack jack; 28 struct gpio_desc *gpiod; 29 }; 30 31 static const struct acpi_gpio_params enable_gpio = { 0, 0, true }; 32 33 static const struct acpi_gpio_mapping speaker_gpios[] = { 34 { "speaker-enable-gpios", &enable_gpio, 1, ACPI_GPIO_QUIRK_ONLY_GPIOIO }, 35 { } 36 }; 37 38 static int avs_es8336_speaker_power_event(struct snd_soc_dapm_widget *w, 39 struct snd_kcontrol *kcontrol, int event) 40 { 41 struct snd_soc_card *card = w->dapm->card; 42 struct avs_card_drvdata *data; 43 bool speaker_en; 44 45 data = snd_soc_card_get_drvdata(card); 46 /* As enable_gpio has active_low=true, logic is inverted. */ 47 speaker_en = !SND_SOC_DAPM_EVENT_ON(event); 48 49 gpiod_set_value_cansleep(data->gpiod, speaker_en); 50 return 0; 51 } 52 53 static const struct snd_soc_dapm_widget card_widgets[] = { 54 SND_SOC_DAPM_SPK("Speaker", NULL), 55 SND_SOC_DAPM_HP("Headphone", NULL), 56 SND_SOC_DAPM_MIC("Headset Mic", NULL), 57 SND_SOC_DAPM_MIC("Internal Mic", NULL), 58 59 SND_SOC_DAPM_SUPPLY("Speaker Power", SND_SOC_NOPM, 0, 0, 60 avs_es8336_speaker_power_event, 61 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU), 62 }; 63 64 static const struct snd_soc_dapm_route card_routes[] = { 65 {"Headphone", NULL, "HPOL"}, 66 {"Headphone", NULL, "HPOR"}, 67 68 /* 69 * There is no separate speaker output instead the speakers are muxed to 70 * the HP outputs. The mux is controlled by the "Speaker Power" widget. 71 */ 72 {"Speaker", NULL, "HPOL"}, 73 {"Speaker", NULL, "HPOR"}, 74 {"Speaker", NULL, "Speaker Power"}, 75 76 /* Mic route map */ 77 {"MIC1", NULL, "Internal Mic"}, 78 {"MIC2", NULL, "Headset Mic"}, 79 }; 80 81 static const struct snd_kcontrol_new card_controls[] = { 82 SOC_DAPM_PIN_SWITCH("Speaker"), 83 SOC_DAPM_PIN_SWITCH("Headphone"), 84 SOC_DAPM_PIN_SWITCH("Headset Mic"), 85 SOC_DAPM_PIN_SWITCH("Internal Mic"), 86 }; 87 88 static struct snd_soc_jack_pin card_headset_pins[] = { 89 { 90 .pin = "Headphone", 91 .mask = SND_JACK_HEADPHONE, 92 }, 93 { 94 .pin = "Headset Mic", 95 .mask = SND_JACK_MICROPHONE, 96 }, 97 }; 98 99 static int avs_es8336_codec_init(struct snd_soc_pcm_runtime *runtime) 100 { 101 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0); 102 struct snd_soc_component *component = codec_dai->component; 103 struct snd_soc_card *card = runtime->card; 104 struct snd_soc_jack_pin *pins; 105 struct avs_card_drvdata *data; 106 struct gpio_desc *gpiod; 107 int num_pins, ret; 108 109 data = snd_soc_card_get_drvdata(card); 110 num_pins = ARRAY_SIZE(card_headset_pins); 111 112 pins = devm_kmemdup(card->dev, card_headset_pins, sizeof(*pins) * num_pins, GFP_KERNEL); 113 if (!pins) 114 return -ENOMEM; 115 116 ret = snd_soc_card_jack_new_pins(card, "Headset", SND_JACK_HEADSET | SND_JACK_BTN_0, 117 &data->jack, pins, num_pins); 118 if (ret) 119 return ret; 120 121 ret = devm_acpi_dev_add_driver_gpios(codec_dai->dev, speaker_gpios); 122 if (ret) 123 dev_warn(codec_dai->dev, "Unable to add GPIO mapping table\n"); 124 125 gpiod = gpiod_get_optional(codec_dai->dev, "speaker-enable", GPIOD_OUT_LOW); 126 if (IS_ERR(gpiod)) 127 return dev_err_probe(codec_dai->dev, PTR_ERR(gpiod), "Get gpiod failed: %ld\n", 128 PTR_ERR(gpiod)); 129 130 data->gpiod = gpiod; 131 snd_jack_set_key(data->jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE); 132 snd_soc_component_set_jack(component, &data->jack, NULL); 133 134 card->dapm.idle_bias_off = true; 135 136 return 0; 137 } 138 139 static void avs_es8336_codec_exit(struct snd_soc_pcm_runtime *runtime) 140 { 141 struct avs_card_drvdata *data = snd_soc_card_get_drvdata(runtime->card); 142 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0); 143 144 snd_soc_component_set_jack(codec_dai->component, NULL, NULL); 145 gpiod_put(data->gpiod); 146 } 147 148 static int avs_es8336_hw_params(struct snd_pcm_substream *substream, 149 struct snd_pcm_hw_params *params) 150 { 151 struct snd_soc_pcm_runtime *runtime = snd_soc_substream_to_rtd(substream); 152 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, 0); 153 int clk_freq; 154 int ret; 155 156 switch (boot_cpu_data.x86_model) { 157 case INTEL_FAM6_KABYLAKE_L: 158 case INTEL_FAM6_KABYLAKE: 159 clk_freq = 24000000; 160 break; 161 default: 162 clk_freq = 19200000; 163 break; 164 } 165 166 ret = snd_soc_dai_set_sysclk(codec_dai, 1, clk_freq, SND_SOC_CLOCK_OUT); 167 if (ret < 0) 168 dev_err(runtime->dev, "Set codec sysclk failed: %d\n", ret); 169 170 return ret; 171 } 172 173 static const struct snd_soc_ops avs_es8336_ops = { 174 .hw_params = avs_es8336_hw_params, 175 }; 176 177 static int avs_es8336_be_fixup(struct snd_soc_pcm_runtime *runtime, 178 struct snd_pcm_hw_params *params) 179 { 180 struct snd_interval *rate, *channels; 181 struct snd_mask *fmt; 182 183 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE); 184 channels = hw_param_interval(params, SNDRV_PCM_HW_PARAM_CHANNELS); 185 fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT); 186 187 /* The ADSP will convert the FE rate to 48k, stereo */ 188 rate->min = rate->max = 48000; 189 channels->min = channels->max = 2; 190 191 /* set SSPN to 24 bit */ 192 snd_mask_none(fmt); 193 snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S24_3LE); 194 195 return 0; 196 } 197 static int avs_create_dai_link(struct device *dev, const char *platform_name, int ssp_port, 198 int tdm_slot, struct snd_soc_dai_link **dai_link) 199 { 200 struct snd_soc_dai_link_component *platform; 201 struct snd_soc_dai_link *dl; 202 203 dl = devm_kzalloc(dev, sizeof(*dl), GFP_KERNEL); 204 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL); 205 if (!dl || !platform) 206 return -ENOMEM; 207 208 platform->name = platform_name; 209 210 dl->name = devm_kasprintf(dev, GFP_KERNEL, 211 AVS_STRING_FMT("SSP", "-Codec", ssp_port, tdm_slot)); 212 dl->cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL); 213 dl->codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL); 214 if (!dl->name || !dl->cpus || !dl->codecs) 215 return -ENOMEM; 216 217 dl->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, 218 AVS_STRING_FMT("SSP", " Pin", ssp_port, tdm_slot)); 219 dl->codecs->name = devm_kasprintf(dev, GFP_KERNEL, "i2c-ESSX8336:00"); 220 dl->codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL, ES8336_CODEC_DAI); 221 if (!dl->cpus->dai_name || !dl->codecs->name || !dl->codecs->dai_name) 222 return -ENOMEM; 223 224 dl->num_cpus = 1; 225 dl->num_codecs = 1; 226 dl->platforms = platform; 227 dl->num_platforms = 1; 228 dl->id = 0; 229 dl->dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | SND_SOC_DAIFMT_CBC_CFC; 230 dl->init = avs_es8336_codec_init; 231 dl->exit = avs_es8336_codec_exit; 232 dl->be_hw_params_fixup = avs_es8336_be_fixup; 233 dl->ops = &avs_es8336_ops; 234 dl->nonatomic = 1; 235 dl->no_pcm = 1; 236 dl->dpcm_capture = 1; 237 dl->dpcm_playback = 1; 238 239 *dai_link = dl; 240 241 return 0; 242 } 243 244 static int avs_card_suspend_pre(struct snd_soc_card *card) 245 { 246 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, ES8336_CODEC_DAI); 247 248 return snd_soc_component_set_jack(codec_dai->component, NULL, NULL); 249 } 250 251 static int avs_card_resume_post(struct snd_soc_card *card) 252 { 253 struct snd_soc_dai *codec_dai = snd_soc_card_get_codec_dai(card, ES8336_CODEC_DAI); 254 struct avs_card_drvdata *data = snd_soc_card_get_drvdata(card); 255 256 return snd_soc_component_set_jack(codec_dai->component, &data->jack, NULL); 257 } 258 259 static int avs_es8336_probe(struct platform_device *pdev) 260 { 261 struct snd_soc_dai_link *dai_link; 262 struct snd_soc_acpi_mach *mach; 263 struct avs_card_drvdata *data; 264 struct snd_soc_card *card; 265 struct device *dev = &pdev->dev; 266 const char *pname; 267 int ssp_port, tdm_slot, ret; 268 269 mach = dev_get_platdata(dev); 270 pname = mach->mach_params.platform; 271 272 ret = avs_mach_get_ssp_tdm(dev, mach, &ssp_port, &tdm_slot); 273 if (ret) 274 return ret; 275 276 ret = avs_create_dai_link(dev, pname, ssp_port, tdm_slot, &dai_link); 277 if (ret) { 278 dev_err(dev, "Failed to create dai link: %d", ret); 279 return ret; 280 } 281 282 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 283 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL); 284 if (!data || !card) 285 return -ENOMEM; 286 287 card->name = "avs_es8336"; 288 card->dev = dev; 289 card->owner = THIS_MODULE; 290 card->suspend_pre = avs_card_suspend_pre; 291 card->resume_post = avs_card_resume_post; 292 card->dai_link = dai_link; 293 card->num_links = 1; 294 card->controls = card_controls; 295 card->num_controls = ARRAY_SIZE(card_controls); 296 card->dapm_widgets = card_widgets; 297 card->num_dapm_widgets = ARRAY_SIZE(card_widgets); 298 card->dapm_routes = card_routes; 299 card->num_dapm_routes = ARRAY_SIZE(card_routes); 300 card->fully_routed = true; 301 snd_soc_card_set_drvdata(card, data); 302 303 ret = snd_soc_fixup_dai_links_platform_name(card, pname); 304 if (ret) 305 return ret; 306 307 return devm_snd_soc_register_card(dev, card); 308 } 309 310 static struct platform_driver avs_es8336_driver = { 311 .probe = avs_es8336_probe, 312 .driver = { 313 .name = "avs_es8336", 314 .pm = &snd_soc_pm_ops, 315 }, 316 }; 317 318 module_platform_driver(avs_es8336_driver); 319 320 MODULE_LICENSE("GPL"); 321 MODULE_ALIAS("platform:avs_es8336"); 322