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