1 /* 2 * Speyside audio support 3 * 4 * Copyright 2011 Wolfson Microelectronics 5 * 6 * This program is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License as published by the 8 * Free Software Foundation; either version 2 of the License, or (at your 9 * option) any later version. 10 */ 11 12 #include <sound/soc.h> 13 #include <sound/soc-dapm.h> 14 #include <sound/jack.h> 15 #include <linux/gpio.h> 16 #include <linux/module.h> 17 18 #include "../codecs/wm8996.h" 19 #include "../codecs/wm9081.h" 20 21 #define WM8996_HPSEL_GPIO 214 22 23 static int speyside_set_bias_level(struct snd_soc_card *card, 24 struct snd_soc_dapm_context *dapm, 25 enum snd_soc_bias_level level) 26 { 27 struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai; 28 int ret; 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_dai *codec_dai = card->rtd[0].codec_dai; 60 int ret; 61 62 if (dapm->dev != codec_dai->dev) 63 return 0; 64 65 switch (level) { 66 case SND_SOC_BIAS_PREPARE: 67 if (card->dapm.bias_level == SND_SOC_BIAS_STANDBY) { 68 ret = snd_soc_dai_set_pll(codec_dai, 0, 69 WM8996_FLL_MCLK2, 70 32768, 48000 * 256); 71 if (ret < 0) { 72 pr_err("Failed to start FLL\n"); 73 return ret; 74 } 75 76 ret = snd_soc_dai_set_sysclk(codec_dai, 77 WM8996_SYSCLK_FLL, 78 48000 * 256, 79 SND_SOC_CLOCK_IN); 80 if (ret < 0) 81 return ret; 82 } 83 break; 84 85 default: 86 break; 87 } 88 89 card->dapm.bias_level = level; 90 91 return 0; 92 } 93 94 static int speyside_hw_params(struct snd_pcm_substream *substream, 95 struct snd_pcm_hw_params *params) 96 { 97 struct snd_soc_pcm_runtime *rtd = substream->private_data; 98 struct snd_soc_dai *cpu_dai = rtd->cpu_dai; 99 struct snd_soc_dai *codec_dai = rtd->codec_dai; 100 int ret; 101 102 ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S 103 | SND_SOC_DAIFMT_NB_NF 104 | SND_SOC_DAIFMT_CBM_CFM); 105 if (ret < 0) 106 return ret; 107 108 ret = snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_I2S 109 | SND_SOC_DAIFMT_NB_NF 110 | SND_SOC_DAIFMT_CBM_CFM); 111 if (ret < 0) 112 return ret; 113 114 return 0; 115 } 116 117 static struct snd_soc_ops speyside_ops = { 118 .hw_params = speyside_hw_params, 119 }; 120 121 static struct snd_soc_jack speyside_headset; 122 123 /* Headset jack detection DAPM pins */ 124 static struct snd_soc_jack_pin speyside_headset_pins[] = { 125 { 126 .pin = "Headset Mic", 127 .mask = SND_JACK_MICROPHONE, 128 }, 129 }; 130 131 /* Default the headphone selection to active high */ 132 static int speyside_jack_polarity; 133 134 static int speyside_get_micbias(struct snd_soc_dapm_widget *source, 135 struct snd_soc_dapm_widget *sink) 136 { 137 if (speyside_jack_polarity && (strcmp(source->name, "MICB1") == 0)) 138 return 1; 139 if (!speyside_jack_polarity && (strcmp(source->name, "MICB2") == 0)) 140 return 1; 141 142 return 0; 143 } 144 145 static void speyside_set_polarity(struct snd_soc_codec *codec, 146 int polarity) 147 { 148 speyside_jack_polarity = !polarity; 149 gpio_direction_output(WM8996_HPSEL_GPIO, speyside_jack_polarity); 150 151 /* Re-run DAPM to make sure we're using the correct mic bias */ 152 snd_soc_dapm_sync(&codec->dapm); 153 } 154 155 static int speyside_wm8996_init(struct snd_soc_pcm_runtime *rtd) 156 { 157 struct snd_soc_dai *dai = rtd->codec_dai; 158 struct snd_soc_codec *codec = rtd->codec; 159 int ret; 160 161 ret = snd_soc_dai_set_sysclk(dai, WM8996_SYSCLK_MCLK2, 32768, 0); 162 if (ret < 0) 163 return ret; 164 165 ret = gpio_request(WM8996_HPSEL_GPIO, "HP_SEL"); 166 if (ret != 0) 167 pr_err("Failed to request HP_SEL GPIO: %d\n", ret); 168 gpio_direction_output(WM8996_HPSEL_GPIO, speyside_jack_polarity); 169 170 ret = snd_soc_jack_new(codec, "Headset", 171 SND_JACK_LINEOUT | SND_JACK_HEADSET | 172 SND_JACK_BTN_0, 173 &speyside_headset); 174 if (ret) 175 return ret; 176 177 ret = snd_soc_jack_add_pins(&speyside_headset, 178 ARRAY_SIZE(speyside_headset_pins), 179 speyside_headset_pins); 180 if (ret) 181 return ret; 182 183 wm8996_detect(codec, &speyside_headset, speyside_set_polarity); 184 185 return 0; 186 } 187 188 static int speyside_late_probe(struct snd_soc_card *card) 189 { 190 snd_soc_dapm_ignore_suspend(&card->dapm, "Headphone"); 191 snd_soc_dapm_ignore_suspend(&card->dapm, "Headset Mic"); 192 snd_soc_dapm_ignore_suspend(&card->dapm, "Main AMIC"); 193 snd_soc_dapm_ignore_suspend(&card->dapm, "Main DMIC"); 194 snd_soc_dapm_ignore_suspend(&card->dapm, "Speaker"); 195 snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Output"); 196 snd_soc_dapm_ignore_suspend(&card->dapm, "WM1250 Input"); 197 198 return 0; 199 } 200 201 static struct snd_soc_dai_link speyside_dai[] = { 202 { 203 .name = "CPU", 204 .stream_name = "CPU", 205 .cpu_dai_name = "samsung-i2s.0", 206 .codec_dai_name = "wm8996-aif1", 207 .platform_name = "samsung-audio", 208 .codec_name = "wm8996.1-001a", 209 .init = speyside_wm8996_init, 210 .ops = &speyside_ops, 211 }, 212 { 213 .name = "Baseband", 214 .stream_name = "Baseband", 215 .cpu_dai_name = "wm8996-aif2", 216 .codec_dai_name = "wm1250-ev1", 217 .codec_name = "wm1250-ev1.1-0027", 218 .ops = &speyside_ops, 219 .ignore_suspend = 1, 220 }, 221 }; 222 223 static int speyside_wm9081_init(struct snd_soc_dapm_context *dapm) 224 { 225 snd_soc_dapm_nc_pin(dapm, "LINEOUT"); 226 227 /* At any time the WM9081 is active it will have this clock */ 228 return snd_soc_codec_set_sysclk(dapm->codec, WM9081_SYSCLK_MCLK, 0, 229 48000 * 256, 0); 230 } 231 232 static struct snd_soc_aux_dev speyside_aux_dev[] = { 233 { 234 .name = "wm9081", 235 .codec_name = "wm9081.1-006c", 236 .init = speyside_wm9081_init, 237 }, 238 }; 239 240 static struct snd_soc_codec_conf speyside_codec_conf[] = { 241 { 242 .dev_name = "wm9081.1-006c", 243 .name_prefix = "Sub", 244 }, 245 }; 246 247 static const struct snd_kcontrol_new controls[] = { 248 SOC_DAPM_PIN_SWITCH("Main Speaker"), 249 SOC_DAPM_PIN_SWITCH("Main DMIC"), 250 SOC_DAPM_PIN_SWITCH("Main AMIC"), 251 SOC_DAPM_PIN_SWITCH("WM1250 Input"), 252 SOC_DAPM_PIN_SWITCH("WM1250 Output"), 253 SOC_DAPM_PIN_SWITCH("Headphone"), 254 }; 255 256 static struct snd_soc_dapm_widget widgets[] = { 257 SND_SOC_DAPM_HP("Headphone", NULL), 258 SND_SOC_DAPM_MIC("Headset Mic", NULL), 259 260 SND_SOC_DAPM_SPK("Main Speaker", NULL), 261 262 SND_SOC_DAPM_MIC("Main AMIC", NULL), 263 SND_SOC_DAPM_MIC("Main DMIC", NULL), 264 }; 265 266 static struct snd_soc_dapm_route audio_paths[] = { 267 { "IN1RN", NULL, "MICB1" }, 268 { "IN1RP", NULL, "MICB1" }, 269 { "IN1RN", NULL, "MICB2" }, 270 { "IN1RP", NULL, "MICB2" }, 271 { "MICB1", NULL, "Headset Mic", speyside_get_micbias }, 272 { "MICB2", NULL, "Headset Mic", speyside_get_micbias }, 273 274 { "IN1LP", NULL, "MICB2" }, 275 { "IN1RN", NULL, "MICB1" }, 276 { "MICB2", NULL, "Main AMIC" }, 277 278 { "DMIC1DAT", NULL, "MICB1" }, 279 { "DMIC2DAT", NULL, "MICB1" }, 280 { "MICB1", NULL, "Main DMIC" }, 281 282 { "Headphone", NULL, "HPOUT1L" }, 283 { "Headphone", NULL, "HPOUT1R" }, 284 285 { "Sub IN1", NULL, "HPOUT2L" }, 286 { "Sub IN2", NULL, "HPOUT2R" }, 287 288 { "Main Speaker", NULL, "Sub SPKN" }, 289 { "Main Speaker", NULL, "Sub SPKP" }, 290 { "Main Speaker", NULL, "SPKDAT" }, 291 }; 292 293 static struct snd_soc_card speyside = { 294 .name = "Speyside", 295 .dai_link = speyside_dai, 296 .num_links = ARRAY_SIZE(speyside_dai), 297 .aux_dev = speyside_aux_dev, 298 .num_aux_devs = ARRAY_SIZE(speyside_aux_dev), 299 .codec_conf = speyside_codec_conf, 300 .num_configs = ARRAY_SIZE(speyside_codec_conf), 301 302 .set_bias_level = speyside_set_bias_level, 303 .set_bias_level_post = speyside_set_bias_level_post, 304 305 .controls = controls, 306 .num_controls = ARRAY_SIZE(controls), 307 .dapm_widgets = widgets, 308 .num_dapm_widgets = ARRAY_SIZE(widgets), 309 .dapm_routes = audio_paths, 310 .num_dapm_routes = ARRAY_SIZE(audio_paths), 311 312 .late_probe = speyside_late_probe, 313 }; 314 315 static __devinit int speyside_probe(struct platform_device *pdev) 316 { 317 struct snd_soc_card *card = &speyside; 318 int ret; 319 320 card->dev = &pdev->dev; 321 322 ret = snd_soc_register_card(card); 323 if (ret) { 324 dev_err(&pdev->dev, "snd_soc_register_card() failed: %d\n", 325 ret); 326 return ret; 327 } 328 329 return 0; 330 } 331 332 static int __devexit speyside_remove(struct platform_device *pdev) 333 { 334 struct snd_soc_card *card = platform_get_drvdata(pdev); 335 336 snd_soc_unregister_card(card); 337 338 return 0; 339 } 340 341 static struct platform_driver speyside_driver = { 342 .driver = { 343 .name = "speyside", 344 .owner = THIS_MODULE, 345 .pm = &snd_soc_pm_ops, 346 }, 347 .probe = speyside_probe, 348 .remove = __devexit_p(speyside_remove), 349 }; 350 351 static int __init speyside_audio_init(void) 352 { 353 return platform_driver_register(&speyside_driver); 354 } 355 module_init(speyside_audio_init); 356 357 static void __exit speyside_audio_exit(void) 358 { 359 platform_driver_unregister(&speyside_driver); 360 } 361 module_exit(speyside_audio_exit); 362 363 MODULE_DESCRIPTION("Speyside audio support"); 364 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); 365 MODULE_LICENSE("GPL"); 366 MODULE_ALIAS("platform:speyside"); 367