1 // SPDX-License-Identifier: GPL-2.0+ 2 // 3 // Speyside audio support 4 // 5 // Copyright 2011 Wolfson Microelectronics 6 7 #include <sound/soc.h> 8 #include <sound/soc-dapm.h> 9 #include <sound/jack.h> 10 #include <linux/gpio/machine.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/module.h> 13 14 #include "../codecs/wm8996.h" 15 #include "../codecs/wm9081.h" 16 17 #define MCLK_AUDIO_RATE (512 * 48000) 18 19 static int speyside_set_bias_level(struct snd_soc_card *card, 20 struct snd_soc_dapm_context *dapm, 21 enum snd_soc_bias_level level) 22 { 23 struct snd_soc_pcm_runtime *rtd; 24 struct snd_soc_dai *codec_dai; 25 int ret; 26 27 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[1]); 28 codec_dai = snd_soc_rtd_to_codec(rtd, 0); 29 30 if (dapm->dev != codec_dai->dev) 31 return 0; 32 33 switch (level) { 34 case SND_SOC_BIAS_STANDBY: 35 ret = snd_soc_dai_set_sysclk(codec_dai, WM8996_SYSCLK_MCLK2, 36 32768, SND_SOC_CLOCK_IN); 37 if (ret < 0) 38 return ret; 39 40 ret = snd_soc_dai_set_pll(codec_dai, WM8996_FLL_MCLK2, 41 0, 0, 0); 42 if (ret < 0) { 43 pr_err("Failed to stop FLL\n"); 44 return ret; 45 } 46 break; 47 48 default: 49 break; 50 } 51 52 return 0; 53 } 54 55 static int speyside_set_bias_level_post(struct snd_soc_card *card, 56 struct snd_soc_dapm_context *dapm, 57 enum snd_soc_bias_level level) 58 { 59 struct snd_soc_pcm_runtime *rtd; 60 struct snd_soc_dai *codec_dai; 61 int ret; 62 63 rtd = snd_soc_get_pcm_runtime(card, &card->dai_link[1]); 64 codec_dai = snd_soc_rtd_to_codec(rtd, 0); 65 66 if (dapm->dev != codec_dai->dev) 67 return 0; 68 69 switch (level) { 70 case SND_SOC_BIAS_PREPARE: 71 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY) { 72 ret = snd_soc_dai_set_pll(codec_dai, 0, 73 WM8996_FLL_MCLK2, 74 32768, MCLK_AUDIO_RATE); 75 if (ret < 0) { 76 pr_err("Failed to start FLL\n"); 77 return ret; 78 } 79 80 ret = snd_soc_dai_set_sysclk(codec_dai, 81 WM8996_SYSCLK_FLL, 82 MCLK_AUDIO_RATE, 83 SND_SOC_CLOCK_IN); 84 if (ret < 0) 85 return ret; 86 } 87 break; 88 89 default: 90 break; 91 } 92 93 card->dapm.bias_level = level; 94 95 return 0; 96 } 97 98 static struct snd_soc_jack speyside_headset; 99 100 /* Headset jack detection DAPM pins */ 101 static struct snd_soc_jack_pin speyside_headset_pins[] = { 102 { 103 .pin = "Headset Mic", 104 .mask = SND_JACK_MICROPHONE, 105 }, 106 }; 107 108 static struct gpio_desc *speyside_hpsel_gpio; 109 /* Default the headphone selection to active high */ 110 static int speyside_jack_polarity; 111 112 static int speyside_get_micbias(struct snd_soc_dapm_widget *source, 113 struct snd_soc_dapm_widget *sink) 114 { 115 if (speyside_jack_polarity && (snd_soc_dapm_widget_name_cmp(source, "MICB1") == 0)) 116 return 1; 117 if (!speyside_jack_polarity && (snd_soc_dapm_widget_name_cmp(source, "MICB2") == 0)) 118 return 1; 119 120 return 0; 121 } 122 123 static void speyside_set_polarity(struct snd_soc_component *component, 124 int polarity) 125 { 126 speyside_jack_polarity = !polarity; 127 gpiod_direction_output(speyside_hpsel_gpio, speyside_jack_polarity); 128 129 /* Re-run DAPM to make sure we're using the correct mic bias */ 130 snd_soc_dapm_sync(snd_soc_component_get_dapm(component)); 131 } 132 133 static int speyside_wm0010_init(struct snd_soc_pcm_runtime *rtd) 134 { 135 struct snd_soc_dai *dai = snd_soc_rtd_to_codec(rtd, 0); 136 int ret; 137 138 ret = snd_soc_dai_set_sysclk(dai, 0, MCLK_AUDIO_RATE, 0); 139 if (ret < 0) 140 return ret; 141 142 return 0; 143 } 144 145 static int speyside_wm8996_init(struct snd_soc_pcm_runtime *rtd) 146 { 147 struct snd_soc_dai *dai = snd_soc_rtd_to_codec(rtd, 0); 148 struct snd_soc_component *component = dai->component; 149 enum gpiod_flags flags; 150 int ret; 151 152 ret = snd_soc_dai_set_sysclk(dai, WM8996_SYSCLK_MCLK2, 32768, 0); 153 if (ret < 0) 154 return ret; 155 156 if (speyside_jack_polarity) 157 flags = GPIOD_OUT_HIGH; 158 else 159 flags = GPIOD_OUT_LOW; 160 speyside_hpsel_gpio = devm_gpiod_get(rtd->card->dev, 161 "hp-sel", 162 flags); 163 if (IS_ERR(speyside_hpsel_gpio)) 164 return PTR_ERR(speyside_hpsel_gpio); 165 166 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset", 167 SND_JACK_LINEOUT | SND_JACK_HEADSET | 168 SND_JACK_BTN_0, 169 &speyside_headset, 170 speyside_headset_pins, 171 ARRAY_SIZE(speyside_headset_pins)); 172 if (ret) 173 return ret; 174 175 wm8996_detect(component, &speyside_headset, speyside_set_polarity); 176 177 return 0; 178 } 179 180 static int speyside_late_probe(struct snd_soc_card *card) 181 { 182 snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone"); 183 snd_soc_dapm_ignore_suspend(&card->dapm, "Headset Mic"); 184 snd_soc_dapm_ignore_suspend(&card->dapm, "Main AMIC"); 185 snd_soc_dapm_ignore_suspend(&card->dapm, "Main DMIC"); 186 snd_soc_dapm_ignore_suspend(&card->dapm, "Main Speaker"); 187 snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Output"); 188 snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Input"); 189 190 return 0; 191 } 192 193 static const struct snd_soc_pcm_stream dsp_codec_params = { 194 .formats = SNDRV_PCM_FMTBIT_S32_LE, 195 .rate_min = 48000, 196 .rate_max = 48000, 197 .channels_min = 2, 198 .channels_max = 2, 199 }; 200 201 SND_SOC_DAILINK_DEFS(cpu_dsp, 202 DAILINK_COMP_ARRAY(COMP_CPU("samsung-i2s.0")), 203 DAILINK_COMP_ARRAY(COMP_CODEC("spi0.0", "wm0010-sdi1")), 204 DAILINK_COMP_ARRAY(COMP_PLATFORM("samsung-i2s.0"))); 205 206 SND_SOC_DAILINK_DEFS(dsp_codec, 207 DAILINK_COMP_ARRAY(COMP_CPU("wm0010-sdi2")), 208 DAILINK_COMP_ARRAY(COMP_CODEC("wm8996.1-001a", "wm8996-aif1"))); 209 210 SND_SOC_DAILINK_DEFS(baseband, 211 DAILINK_COMP_ARRAY(COMP_CPU("wm8996-aif2")), 212 DAILINK_COMP_ARRAY(COMP_CODEC("wm1250-ev1.1-0027", "wm1250-ev1"))); 213 214 static struct snd_soc_dai_link speyside_dai[] = { 215 { 216 .name = "CPU-DSP", 217 .stream_name = "CPU-DSP", 218 .init = speyside_wm0010_init, 219 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF 220 | SND_SOC_DAIFMT_CBP_CFP, 221 SND_SOC_DAILINK_REG(cpu_dsp), 222 }, 223 { 224 .name = "DSP-CODEC", 225 .stream_name = "DSP-CODEC", 226 .init = speyside_wm8996_init, 227 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF 228 | SND_SOC_DAIFMT_CBP_CFP, 229 .c2c_params = &dsp_codec_params, 230 .num_c2c_params = 1, 231 .ignore_suspend = 1, 232 SND_SOC_DAILINK_REG(dsp_codec), 233 }, 234 { 235 .name = "Baseband", 236 .stream_name = "Baseband", 237 .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF 238 | SND_SOC_DAIFMT_CBP_CFP, 239 .ignore_suspend = 1, 240 SND_SOC_DAILINK_REG(baseband), 241 }, 242 }; 243 244 static int speyside_wm9081_init(struct snd_soc_component *component) 245 { 246 /* At any time the WM9081 is active it will have this clock */ 247 return snd_soc_component_set_sysclk(component, WM9081_SYSCLK_MCLK, 0, 248 MCLK_AUDIO_RATE, 0); 249 } 250 251 static struct snd_soc_aux_dev speyside_aux_dev[] = { 252 { 253 .dlc = COMP_AUX("wm9081.1-006c"), 254 .init = speyside_wm9081_init, 255 }, 256 }; 257 258 static struct snd_soc_codec_conf speyside_codec_conf[] = { 259 { 260 .dlc = COMP_CODEC_CONF("wm9081.1-006c"), 261 .name_prefix = "Sub", 262 }, 263 }; 264 265 static const struct snd_kcontrol_new controls[] = { 266 SOC_DAPM_PIN_SWITCH("Main Speaker"), 267 SOC_DAPM_PIN_SWITCH("Main DMIC"), 268 SOC_DAPM_PIN_SWITCH("Main AMIC"), 269 SOC_DAPM_PIN_SWITCH("WM1250 Input"), 270 SOC_DAPM_PIN_SWITCH("WM1250 Output"), 271 SOC_DAPM_PIN_SWITCH("Headphone"), 272 }; 273 274 static const struct snd_soc_dapm_widget widgets[] = { 275 SND_SOC_DAPM_HP("Headphone", NULL), 276 SND_SOC_DAPM_MIC("Headset Mic", NULL), 277 278 SND_SOC_DAPM_SPK("Main Speaker", NULL), 279 280 SND_SOC_DAPM_MIC("Main AMIC", NULL), 281 SND_SOC_DAPM_MIC("Main DMIC", NULL), 282 }; 283 284 static const struct snd_soc_dapm_route audio_paths[] = { 285 { "IN1RN", NULL, "MICB1" }, 286 { "IN1RP", NULL, "MICB1" }, 287 { "IN1RN", NULL, "MICB2" }, 288 { "IN1RP", NULL, "MICB2" }, 289 { "MICB1", NULL, "Headset Mic", speyside_get_micbias }, 290 { "MICB2", NULL, "Headset Mic", speyside_get_micbias }, 291 292 { "IN1LP", NULL, "MICB2" }, 293 { "IN1RN", NULL, "MICB1" }, 294 { "MICB2", NULL, "Main AMIC" }, 295 296 { "DMIC1DAT", NULL, "MICB1" }, 297 { "DMIC2DAT", NULL, "MICB1" }, 298 { "MICB1", NULL, "Main DMIC" }, 299 300 { "Headphone", NULL, "HPOUT1L" }, 301 { "Headphone", NULL, "HPOUT1R" }, 302 303 { "Sub IN1", NULL, "HPOUT2L" }, 304 { "Sub IN2", NULL, "HPOUT2R" }, 305 306 { "Main Speaker", NULL, "Sub SPKN" }, 307 { "Main Speaker", NULL, "Sub SPKP" }, 308 { "Main Speaker", NULL, "SPKDAT" }, 309 }; 310 311 static struct snd_soc_card speyside = { 312 .name = "Speyside", 313 .owner = THIS_MODULE, 314 .dai_link = speyside_dai, 315 .num_links = ARRAY_SIZE(speyside_dai), 316 .aux_dev = speyside_aux_dev, 317 .num_aux_devs = ARRAY_SIZE(speyside_aux_dev), 318 .codec_conf = speyside_codec_conf, 319 .num_configs = ARRAY_SIZE(speyside_codec_conf), 320 321 .set_bias_level = speyside_set_bias_level, 322 .set_bias_level_post = speyside_set_bias_level_post, 323 324 .controls = controls, 325 .num_controls = ARRAY_SIZE(controls), 326 .dapm_widgets = widgets, 327 .num_dapm_widgets = ARRAY_SIZE(widgets), 328 .dapm_routes = audio_paths, 329 .num_dapm_routes = ARRAY_SIZE(audio_paths), 330 .fully_routed = true, 331 332 .late_probe = speyside_late_probe, 333 }; 334 335 static struct gpiod_lookup_table wm8996_gpiod_table = { 336 /* Hardcoded device name in board file mach-crag6410.c */ 337 .dev_id = "speyside", 338 .table = { 339 /* 340 * This line was hardcoded to 214 in the global GPIO 341 * number space, S3C GPIO macros seems top set the 342 * wm8996 codec GPIO start offset to 212, so this will 343 * be GPIO 214 - 212 = 2 on the wm8996. 344 */ 345 GPIO_LOOKUP("wm8996", 2, "hp-sel", GPIO_ACTIVE_HIGH), 346 { }, 347 }, 348 }; 349 350 static void speyside_gpiod_table_action(void *data) 351 { 352 gpiod_remove_lookup_table(&wm8996_gpiod_table); 353 } 354 355 static int speyside_probe(struct platform_device *pdev) 356 { 357 struct snd_soc_card *card = &speyside; 358 int ret; 359 360 card->dev = &pdev->dev; 361 362 gpiod_add_lookup_table(&wm8996_gpiod_table); 363 ret = devm_add_action_or_reset(&pdev->dev, speyside_gpiod_table_action, 364 NULL); 365 if (ret) 366 return ret; 367 368 ret = devm_snd_soc_register_card(&pdev->dev, card); 369 if (ret) 370 dev_err_probe(&pdev->dev, ret, "snd_soc_register_card() failed\n"); 371 372 return ret; 373 } 374 375 static struct platform_driver speyside_driver = { 376 .driver = { 377 .name = "speyside", 378 .pm = &snd_soc_pm_ops, 379 }, 380 .probe = speyside_probe, 381 }; 382 383 module_platform_driver(speyside_driver); 384 385 MODULE_DESCRIPTION("Speyside audio support"); 386 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 387 MODULE_LICENSE("GPL"); 388 MODULE_ALIAS("platform:speyside"); 389