1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ASoC machine driver for Intel Broadwell platforms with RT5677 codec 4 * 5 * Copyright (c) 2014, The Chromium OS Authors. All rights reserved. 6 */ 7 8 #include <linux/acpi.h> 9 #include <linux/module.h> 10 #include <linux/platform_device.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/delay.h> 13 #include <sound/core.h> 14 #include <sound/pcm.h> 15 #include <sound/soc.h> 16 #include <sound/pcm_params.h> 17 #include <sound/jack.h> 18 #include <sound/soc-acpi.h> 19 20 #include "../common/sst-dsp.h" 21 #include "../haswell/sst-haswell-ipc.h" 22 23 #include "../../codecs/rt5677.h" 24 25 struct bdw_rt5677_priv { 26 struct gpio_desc *gpio_hp_en; 27 struct snd_soc_component *component; 28 }; 29 30 static int bdw_rt5677_event_hp(struct snd_soc_dapm_widget *w, 31 struct snd_kcontrol *k, int event) 32 { 33 struct snd_soc_dapm_context *dapm = w->dapm; 34 struct snd_soc_card *card = dapm->card; 35 struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card); 36 37 if (SND_SOC_DAPM_EVENT_ON(event)) 38 msleep(70); 39 40 gpiod_set_value_cansleep(bdw_rt5677->gpio_hp_en, 41 SND_SOC_DAPM_EVENT_ON(event)); 42 43 return 0; 44 } 45 46 static const struct snd_soc_dapm_widget bdw_rt5677_widgets[] = { 47 SND_SOC_DAPM_HP("Headphone", bdw_rt5677_event_hp), 48 SND_SOC_DAPM_SPK("Speaker", NULL), 49 SND_SOC_DAPM_MIC("Headset Mic", NULL), 50 SND_SOC_DAPM_MIC("Local DMICs", NULL), 51 SND_SOC_DAPM_MIC("Remote DMICs", NULL), 52 }; 53 54 static const struct snd_soc_dapm_route bdw_rt5677_map[] = { 55 /* Speakers */ 56 {"Speaker", NULL, "PDM1L"}, 57 {"Speaker", NULL, "PDM1R"}, 58 59 /* Headset jack connectors */ 60 {"Headphone", NULL, "LOUT1"}, 61 {"Headphone", NULL, "LOUT2"}, 62 {"IN1P", NULL, "Headset Mic"}, 63 {"IN1N", NULL, "Headset Mic"}, 64 65 /* Digital MICs 66 * Local DMICs: the two DMICs on the mainboard 67 * Remote DMICs: the two DMICs on the camera module 68 */ 69 {"DMIC L1", NULL, "Remote DMICs"}, 70 {"DMIC R1", NULL, "Remote DMICs"}, 71 {"DMIC L2", NULL, "Local DMICs"}, 72 {"DMIC R2", NULL, "Local DMICs"}, 73 74 /* CODEC BE connections */ 75 {"SSP0 CODEC IN", NULL, "AIF1 Capture"}, 76 {"AIF1 Playback", NULL, "SSP0 CODEC OUT"}, 77 }; 78 79 static const struct snd_kcontrol_new bdw_rt5677_controls[] = { 80 SOC_DAPM_PIN_SWITCH("Speaker"), 81 SOC_DAPM_PIN_SWITCH("Headphone"), 82 SOC_DAPM_PIN_SWITCH("Headset Mic"), 83 SOC_DAPM_PIN_SWITCH("Local DMICs"), 84 SOC_DAPM_PIN_SWITCH("Remote DMICs"), 85 }; 86 87 88 static struct snd_soc_jack headphone_jack; 89 static struct snd_soc_jack mic_jack; 90 91 static struct snd_soc_jack_pin headphone_jack_pin = { 92 .pin = "Headphone", 93 .mask = SND_JACK_HEADPHONE, 94 }; 95 96 static struct snd_soc_jack_pin mic_jack_pin = { 97 .pin = "Headset Mic", 98 .mask = SND_JACK_MICROPHONE, 99 }; 100 101 static struct snd_soc_jack_gpio headphone_jack_gpio = { 102 .name = "plug-det", 103 .report = SND_JACK_HEADPHONE, 104 .debounce_time = 200, 105 }; 106 107 static struct snd_soc_jack_gpio mic_jack_gpio = { 108 .name = "mic-present", 109 .report = SND_JACK_MICROPHONE, 110 .debounce_time = 200, 111 .invert = 1, 112 }; 113 114 /* GPIO indexes defined by ACPI */ 115 enum { 116 RT5677_GPIO_PLUG_DET = 0, 117 RT5677_GPIO_MIC_PRESENT_L = 1, 118 RT5677_GPIO_HOTWORD_DET_L = 2, 119 RT5677_GPIO_DSP_INT = 3, 120 RT5677_GPIO_HP_AMP_SHDN_L = 4, 121 }; 122 123 static const struct acpi_gpio_params plug_det_gpio = { RT5677_GPIO_PLUG_DET, 0, false }; 124 static const struct acpi_gpio_params mic_present_gpio = { RT5677_GPIO_MIC_PRESENT_L, 0, false }; 125 static const struct acpi_gpio_params headphone_enable_gpio = { RT5677_GPIO_HP_AMP_SHDN_L, 0, false }; 126 127 static const struct acpi_gpio_mapping bdw_rt5677_gpios[] = { 128 { "plug-det-gpios", &plug_det_gpio, 1 }, 129 { "mic-present-gpios", &mic_present_gpio, 1 }, 130 { "headphone-enable-gpios", &headphone_enable_gpio, 1 }, 131 { NULL }, 132 }; 133 134 static int broadwell_ssp0_fixup(struct snd_soc_pcm_runtime *rtd, 135 struct snd_pcm_hw_params *params) 136 { 137 struct snd_interval *rate = hw_param_interval(params, 138 SNDRV_PCM_HW_PARAM_RATE); 139 struct snd_interval *channels = hw_param_interval(params, 140 SNDRV_PCM_HW_PARAM_CHANNELS); 141 142 /* The ADSP will covert the FE rate to 48k, stereo */ 143 rate->min = rate->max = 48000; 144 channels->min = channels->max = 2; 145 146 /* set SSP0 to 16 bit */ 147 params_set_format(params, SNDRV_PCM_FORMAT_S16_LE); 148 return 0; 149 } 150 151 static int bdw_rt5677_hw_params(struct snd_pcm_substream *substream, 152 struct snd_pcm_hw_params *params) 153 { 154 struct snd_soc_pcm_runtime *rtd = substream->private_data; 155 struct snd_soc_dai *codec_dai = rtd->codec_dai; 156 int ret; 157 158 ret = snd_soc_dai_set_sysclk(codec_dai, RT5677_SCLK_S_MCLK, 24576000, 159 SND_SOC_CLOCK_IN); 160 if (ret < 0) { 161 dev_err(rtd->dev, "can't set codec sysclk configuration\n"); 162 return ret; 163 } 164 165 return ret; 166 } 167 168 static const struct snd_soc_ops bdw_rt5677_ops = { 169 .hw_params = bdw_rt5677_hw_params, 170 }; 171 172 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL) 173 static int bdw_rt5677_rtd_init(struct snd_soc_pcm_runtime *rtd) 174 { 175 struct snd_soc_component *component = snd_soc_rtdcom_lookup(rtd, DRV_NAME); 176 struct sst_pdata *pdata = dev_get_platdata(component->dev); 177 struct sst_hsw *broadwell = pdata->dsp; 178 int ret; 179 180 /* Set ADSP SSP port settings */ 181 ret = sst_hsw_device_set_config(broadwell, SST_HSW_DEVICE_SSP_0, 182 SST_HSW_DEVICE_MCLK_FREQ_24_MHZ, 183 SST_HSW_DEVICE_CLOCK_MASTER, 9); 184 if (ret < 0) { 185 dev_err(rtd->dev, "error: failed to set device config\n"); 186 return ret; 187 } 188 189 return 0; 190 } 191 #endif 192 193 static int bdw_rt5677_init(struct snd_soc_pcm_runtime *rtd) 194 { 195 struct bdw_rt5677_priv *bdw_rt5677 = 196 snd_soc_card_get_drvdata(rtd->card); 197 struct snd_soc_component *component = rtd->codec_dai->component; 198 struct snd_soc_dapm_context *dapm = snd_soc_component_get_dapm(component); 199 int ret; 200 201 ret = devm_acpi_dev_add_driver_gpios(component->dev, bdw_rt5677_gpios); 202 if (ret) 203 dev_warn(component->dev, "Failed to add driver gpios\n"); 204 205 /* Enable codec ASRC function for Stereo DAC/Stereo1 ADC/DMIC/I2S1. 206 * The ASRC clock source is clk_i2s1_asrc. 207 */ 208 rt5677_sel_asrc_clk_src(component, RT5677_DA_STEREO_FILTER | 209 RT5677_AD_STEREO1_FILTER | RT5677_I2S1_SOURCE, 210 RT5677_CLK_SEL_I2S1_ASRC); 211 212 /* Request rt5677 GPIO for headphone amp control */ 213 bdw_rt5677->gpio_hp_en = devm_gpiod_get(component->dev, "headphone-enable", 214 GPIOD_OUT_LOW); 215 if (IS_ERR(bdw_rt5677->gpio_hp_en)) { 216 dev_err(component->dev, "Can't find HP_AMP_SHDN_L gpio\n"); 217 return PTR_ERR(bdw_rt5677->gpio_hp_en); 218 } 219 220 /* Create and initialize headphone jack */ 221 if (!snd_soc_card_jack_new(rtd->card, "Headphone Jack", 222 SND_JACK_HEADPHONE, &headphone_jack, 223 &headphone_jack_pin, 1)) { 224 headphone_jack_gpio.gpiod_dev = component->dev; 225 if (snd_soc_jack_add_gpios(&headphone_jack, 1, 226 &headphone_jack_gpio)) 227 dev_err(component->dev, "Can't add headphone jack gpio\n"); 228 } else { 229 dev_err(component->dev, "Can't create headphone jack\n"); 230 } 231 232 /* Create and initialize mic jack */ 233 if (!snd_soc_card_jack_new(rtd->card, "Mic Jack", 234 SND_JACK_MICROPHONE, &mic_jack, 235 &mic_jack_pin, 1)) { 236 mic_jack_gpio.gpiod_dev = component->dev; 237 if (snd_soc_jack_add_gpios(&mic_jack, 1, &mic_jack_gpio)) 238 dev_err(component->dev, "Can't add mic jack gpio\n"); 239 } else { 240 dev_err(component->dev, "Can't create mic jack\n"); 241 } 242 bdw_rt5677->component = component; 243 244 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1"); 245 return 0; 246 } 247 248 /* broadwell digital audio interface glue - connects codec <--> CPU */ 249 static struct snd_soc_dai_link bdw_rt5677_dais[] = { 250 /* Front End DAI links */ 251 { 252 .name = "System PCM", 253 .stream_name = "System Playback/Capture", 254 .cpu_dai_name = "System Pin", 255 .platform_name = "haswell-pcm-audio", 256 .dynamic = 1, 257 .codec_name = "snd-soc-dummy", 258 .codec_dai_name = "snd-soc-dummy-dai", 259 #if !IS_ENABLED(CONFIG_SND_SOC_SOF_BROADWELL) 260 .init = bdw_rt5677_rtd_init, 261 #endif 262 .trigger = { 263 SND_SOC_DPCM_TRIGGER_POST, 264 SND_SOC_DPCM_TRIGGER_POST 265 }, 266 .dpcm_capture = 1, 267 .dpcm_playback = 1, 268 }, 269 270 /* Back End DAI links */ 271 { 272 /* SSP0 - Codec */ 273 .name = "Codec", 274 .id = 0, 275 .cpu_dai_name = "snd-soc-dummy-dai", 276 .platform_name = "snd-soc-dummy", 277 .no_pcm = 1, 278 .codec_name = "i2c-RT5677CE:00", 279 .codec_dai_name = "rt5677-aif1", 280 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | 281 SND_SOC_DAIFMT_CBS_CFS, 282 .ignore_suspend = 1, 283 .ignore_pmdown_time = 1, 284 .be_hw_params_fixup = broadwell_ssp0_fixup, 285 .ops = &bdw_rt5677_ops, 286 .dpcm_playback = 1, 287 .dpcm_capture = 1, 288 .init = bdw_rt5677_init, 289 }, 290 }; 291 292 static int bdw_rt5677_suspend_pre(struct snd_soc_card *card) 293 { 294 struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card); 295 struct snd_soc_dapm_context *dapm; 296 297 if (bdw_rt5677->component) { 298 dapm = snd_soc_component_get_dapm(bdw_rt5677->component); 299 snd_soc_dapm_disable_pin(dapm, "MICBIAS1"); 300 } 301 return 0; 302 } 303 304 static int bdw_rt5677_resume_post(struct snd_soc_card *card) 305 { 306 struct bdw_rt5677_priv *bdw_rt5677 = snd_soc_card_get_drvdata(card); 307 struct snd_soc_dapm_context *dapm; 308 309 if (bdw_rt5677->component) { 310 dapm = snd_soc_component_get_dapm(bdw_rt5677->component); 311 snd_soc_dapm_force_enable_pin(dapm, "MICBIAS1"); 312 } 313 return 0; 314 } 315 316 /* ASoC machine driver for Broadwell DSP + RT5677 */ 317 static struct snd_soc_card bdw_rt5677_card = { 318 .name = "bdw-rt5677", 319 .owner = THIS_MODULE, 320 .dai_link = bdw_rt5677_dais, 321 .num_links = ARRAY_SIZE(bdw_rt5677_dais), 322 .dapm_widgets = bdw_rt5677_widgets, 323 .num_dapm_widgets = ARRAY_SIZE(bdw_rt5677_widgets), 324 .dapm_routes = bdw_rt5677_map, 325 .num_dapm_routes = ARRAY_SIZE(bdw_rt5677_map), 326 .controls = bdw_rt5677_controls, 327 .num_controls = ARRAY_SIZE(bdw_rt5677_controls), 328 .fully_routed = true, 329 .suspend_pre = bdw_rt5677_suspend_pre, 330 .resume_post = bdw_rt5677_resume_post, 331 }; 332 333 static int bdw_rt5677_probe(struct platform_device *pdev) 334 { 335 struct bdw_rt5677_priv *bdw_rt5677; 336 struct snd_soc_acpi_mach *mach; 337 const char *platform_name = NULL; 338 int ret; 339 340 bdw_rt5677_card.dev = &pdev->dev; 341 342 /* Allocate driver private struct */ 343 bdw_rt5677 = devm_kzalloc(&pdev->dev, sizeof(struct bdw_rt5677_priv), 344 GFP_KERNEL); 345 if (!bdw_rt5677) { 346 dev_err(&pdev->dev, "Can't allocate bdw_rt5677\n"); 347 return -ENOMEM; 348 } 349 350 /* override plaform name, if required */ 351 mach = (&pdev->dev)->platform_data; 352 if (mach) /* extra check since legacy does not pass parameters */ 353 platform_name = mach->mach_params.platform; 354 355 ret = snd_soc_fixup_dai_links_platform_name(&bdw_rt5677_card, 356 platform_name); 357 if (ret) 358 return ret; 359 360 snd_soc_card_set_drvdata(&bdw_rt5677_card, bdw_rt5677); 361 362 return devm_snd_soc_register_card(&pdev->dev, &bdw_rt5677_card); 363 } 364 365 static struct platform_driver bdw_rt5677_audio = { 366 .probe = bdw_rt5677_probe, 367 .driver = { 368 .name = "bdw-rt5677", 369 }, 370 }; 371 372 module_platform_driver(bdw_rt5677_audio) 373 374 /* Module information */ 375 MODULE_AUTHOR("Ben Zhang"); 376 MODULE_DESCRIPTION("Intel Broadwell RT5677 machine driver"); 377 MODULE_LICENSE("GPL v2"); 378 MODULE_ALIAS("platform:bdw-rt5677"); 379