1 /* 2 * wm9705.c -- ALSA Soc WM9705 codec support 3 * 4 * Copyright 2008 Ian Molton <spyro@f2s.com> 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; Version 2 of the License only. 9 * 10 */ 11 12 #include <linux/init.h> 13 #include <linux/slab.h> 14 #include <linux/mfd/wm97xx.h> 15 #include <linux/module.h> 16 #include <linux/kernel.h> 17 #include <linux/device.h> 18 #include <linux/regmap.h> 19 #include <sound/core.h> 20 #include <sound/pcm.h> 21 #include <sound/ac97_codec.h> 22 #include <sound/ac97/codec.h> 23 #include <sound/ac97/compat.h> 24 #include <sound/initval.h> 25 #include <sound/soc.h> 26 27 #define WM9705_VENDOR_ID 0x574d4c05 28 #define WM9705_VENDOR_ID_MASK 0xffffffff 29 30 struct wm9705_priv { 31 struct snd_ac97 *ac97; 32 struct wm97xx_platform_data *mfd_pdata; 33 }; 34 35 static const struct reg_default wm9705_reg_defaults[] = { 36 { 0x02, 0x8000 }, 37 { 0x04, 0x8000 }, 38 { 0x06, 0x8000 }, 39 { 0x0a, 0x8000 }, 40 { 0x0c, 0x8008 }, 41 { 0x0e, 0x8008 }, 42 { 0x10, 0x8808 }, 43 { 0x12, 0x8808 }, 44 { 0x14, 0x8808 }, 45 { 0x16, 0x8808 }, 46 { 0x18, 0x8808 }, 47 { 0x1a, 0x0000 }, 48 { 0x1c, 0x8000 }, 49 { 0x20, 0x0000 }, 50 { 0x22, 0x0000 }, 51 { 0x26, 0x000f }, 52 { 0x28, 0x0605 }, 53 { 0x2a, 0x0000 }, 54 { 0x2c, 0xbb80 }, 55 { 0x32, 0xbb80 }, 56 { 0x34, 0x2000 }, 57 { 0x5a, 0x0000 }, 58 { 0x5c, 0x0000 }, 59 { 0x72, 0x0808 }, 60 { 0x74, 0x0000 }, 61 { 0x76, 0x0006 }, 62 { 0x78, 0x0000 }, 63 { 0x7a, 0x0000 }, 64 }; 65 66 static const struct regmap_config wm9705_regmap_config = { 67 .reg_bits = 16, 68 .reg_stride = 2, 69 .val_bits = 16, 70 .max_register = 0x7e, 71 .cache_type = REGCACHE_RBTREE, 72 73 .volatile_reg = regmap_ac97_default_volatile, 74 75 .reg_defaults = wm9705_reg_defaults, 76 .num_reg_defaults = ARRAY_SIZE(wm9705_reg_defaults), 77 }; 78 79 static const struct snd_kcontrol_new wm9705_snd_ac97_controls[] = { 80 SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1), 81 SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1), 82 SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1), 83 SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1), 84 SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1), 85 SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1), 86 SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1), 87 SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1), 88 SOC_SINGLE("PCBeep Playback Volume", AC97_PC_BEEP, 1, 15, 1), 89 SOC_SINGLE("Phone Playback Volume", AC97_PHONE, 0, 31, 1), 90 SOC_DOUBLE("Line Playback Volume", AC97_LINE, 8, 0, 31, 1), 91 SOC_DOUBLE("CD Playback Volume", AC97_CD, 8, 0, 31, 1), 92 SOC_SINGLE("Mic Playback Volume", AC97_MIC, 0, 31, 1), 93 SOC_SINGLE("Mic 20dB Boost Switch", AC97_MIC, 6, 1, 0), 94 SOC_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0), 95 SOC_SINGLE("Capture Switch", AC97_REC_GAIN, 15, 1, 1), 96 }; 97 98 static const char *wm9705_mic[] = {"Mic 1", "Mic 2"}; 99 static const char *wm9705_rec_sel[] = {"Mic", "CD", "NC", "NC", 100 "Line", "Stereo Mix", "Mono Mix", "Phone"}; 101 102 static SOC_ENUM_SINGLE_DECL(wm9705_enum_mic, 103 AC97_GENERAL_PURPOSE, 8, wm9705_mic); 104 static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_l, 105 AC97_REC_SEL, 8, wm9705_rec_sel); 106 static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_r, 107 AC97_REC_SEL, 0, wm9705_rec_sel); 108 109 /* Headphone Mixer */ 110 static const struct snd_kcontrol_new wm9705_hp_mixer_controls[] = { 111 SOC_DAPM_SINGLE("PCBeep Playback Switch", AC97_PC_BEEP, 15, 1, 1), 112 SOC_DAPM_SINGLE("CD Playback Switch", AC97_CD, 15, 1, 1), 113 SOC_DAPM_SINGLE("Mic Playback Switch", AC97_MIC, 15, 1, 1), 114 SOC_DAPM_SINGLE("Phone Playback Switch", AC97_PHONE, 15, 1, 1), 115 SOC_DAPM_SINGLE("Line Playback Switch", AC97_LINE, 15, 1, 1), 116 }; 117 118 /* Mic source */ 119 static const struct snd_kcontrol_new wm9705_mic_src_controls = 120 SOC_DAPM_ENUM("Route", wm9705_enum_mic); 121 122 /* Capture source */ 123 static const struct snd_kcontrol_new wm9705_capture_selectl_controls = 124 SOC_DAPM_ENUM("Route", wm9705_enum_rec_l); 125 static const struct snd_kcontrol_new wm9705_capture_selectr_controls = 126 SOC_DAPM_ENUM("Route", wm9705_enum_rec_r); 127 128 /* DAPM widgets */ 129 static const struct snd_soc_dapm_widget wm9705_dapm_widgets[] = { 130 SND_SOC_DAPM_MUX("Mic Source", SND_SOC_NOPM, 0, 0, 131 &wm9705_mic_src_controls), 132 SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0, 133 &wm9705_capture_selectl_controls), 134 SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0, 135 &wm9705_capture_selectr_controls), 136 SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback", 137 SND_SOC_NOPM, 0, 0), 138 SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback", 139 SND_SOC_NOPM, 0, 0), 140 SND_SOC_DAPM_MIXER_NAMED_CTL("HP Mixer", SND_SOC_NOPM, 0, 0, 141 &wm9705_hp_mixer_controls[0], 142 ARRAY_SIZE(wm9705_hp_mixer_controls)), 143 SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0, NULL, 0), 144 SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0), 145 SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0), 146 SND_SOC_DAPM_PGA("Headphone PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 147 SND_SOC_DAPM_PGA("Speaker PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 148 SND_SOC_DAPM_PGA("Line PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 149 SND_SOC_DAPM_PGA("Line out PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 150 SND_SOC_DAPM_PGA("Mono PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 151 SND_SOC_DAPM_PGA("Phone PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 152 SND_SOC_DAPM_PGA("Mic PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 153 SND_SOC_DAPM_PGA("PCBEEP PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 154 SND_SOC_DAPM_PGA("CD PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 155 SND_SOC_DAPM_PGA("ADC PGA", SND_SOC_NOPM, 0, 0, NULL, 0), 156 SND_SOC_DAPM_OUTPUT("HPOUTL"), 157 SND_SOC_DAPM_OUTPUT("HPOUTR"), 158 SND_SOC_DAPM_OUTPUT("LOUT"), 159 SND_SOC_DAPM_OUTPUT("ROUT"), 160 SND_SOC_DAPM_OUTPUT("MONOOUT"), 161 SND_SOC_DAPM_INPUT("PHONE"), 162 SND_SOC_DAPM_INPUT("LINEINL"), 163 SND_SOC_DAPM_INPUT("LINEINR"), 164 SND_SOC_DAPM_INPUT("CDINL"), 165 SND_SOC_DAPM_INPUT("CDINR"), 166 SND_SOC_DAPM_INPUT("PCBEEP"), 167 SND_SOC_DAPM_INPUT("MIC1"), 168 SND_SOC_DAPM_INPUT("MIC2"), 169 }; 170 171 /* Audio map 172 * WM9705 has no switches to disable the route from the inputs to the HP mixer 173 * so in order to prevent active inputs from forcing the audio outputs to be 174 * constantly enabled, we use the mutes on those inputs to simulate such 175 * controls. 176 */ 177 static const struct snd_soc_dapm_route wm9705_audio_map[] = { 178 /* HP mixer */ 179 {"HP Mixer", "PCBeep Playback Switch", "PCBEEP PGA"}, 180 {"HP Mixer", "CD Playback Switch", "CD PGA"}, 181 {"HP Mixer", "Mic Playback Switch", "Mic PGA"}, 182 {"HP Mixer", "Phone Playback Switch", "Phone PGA"}, 183 {"HP Mixer", "Line Playback Switch", "Line PGA"}, 184 {"HP Mixer", NULL, "Left DAC"}, 185 {"HP Mixer", NULL, "Right DAC"}, 186 187 /* mono mixer */ 188 {"Mono Mixer", NULL, "HP Mixer"}, 189 190 /* outputs */ 191 {"Headphone PGA", NULL, "HP Mixer"}, 192 {"HPOUTL", NULL, "Headphone PGA"}, 193 {"HPOUTR", NULL, "Headphone PGA"}, 194 {"Line out PGA", NULL, "HP Mixer"}, 195 {"LOUT", NULL, "Line out PGA"}, 196 {"ROUT", NULL, "Line out PGA"}, 197 {"Mono PGA", NULL, "Mono Mixer"}, 198 {"MONOOUT", NULL, "Mono PGA"}, 199 200 /* inputs */ 201 {"CD PGA", NULL, "CDINL"}, 202 {"CD PGA", NULL, "CDINR"}, 203 {"Line PGA", NULL, "LINEINL"}, 204 {"Line PGA", NULL, "LINEINR"}, 205 {"Phone PGA", NULL, "PHONE"}, 206 {"Mic Source", "Mic 1", "MIC1"}, 207 {"Mic Source", "Mic 2", "MIC2"}, 208 {"Mic PGA", NULL, "Mic Source"}, 209 {"PCBEEP PGA", NULL, "PCBEEP"}, 210 211 /* Left capture selector */ 212 {"Left Capture Source", "Mic", "Mic Source"}, 213 {"Left Capture Source", "CD", "CDINL"}, 214 {"Left Capture Source", "Line", "LINEINL"}, 215 {"Left Capture Source", "Stereo Mix", "HP Mixer"}, 216 {"Left Capture Source", "Mono Mix", "HP Mixer"}, 217 {"Left Capture Source", "Phone", "PHONE"}, 218 219 /* Right capture source */ 220 {"Right Capture Source", "Mic", "Mic Source"}, 221 {"Right Capture Source", "CD", "CDINR"}, 222 {"Right Capture Source", "Line", "LINEINR"}, 223 {"Right Capture Source", "Stereo Mix", "HP Mixer"}, 224 {"Right Capture Source", "Mono Mix", "HP Mixer"}, 225 {"Right Capture Source", "Phone", "PHONE"}, 226 227 {"ADC PGA", NULL, "Left Capture Source"}, 228 {"ADC PGA", NULL, "Right Capture Source"}, 229 230 /* ADC's */ 231 {"Left ADC", NULL, "ADC PGA"}, 232 {"Right ADC", NULL, "ADC PGA"}, 233 }; 234 235 static int ac97_prepare(struct snd_pcm_substream *substream, 236 struct snd_soc_dai *dai) 237 { 238 struct snd_soc_codec *codec = dai->codec; 239 int reg; 240 241 snd_soc_update_bits(codec, AC97_EXTENDED_STATUS, 0x1, 0x1); 242 243 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) 244 reg = AC97_PCM_FRONT_DAC_RATE; 245 else 246 reg = AC97_PCM_LR_ADC_RATE; 247 248 return snd_soc_write(codec, reg, substream->runtime->rate); 249 } 250 251 #define WM9705_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | \ 252 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \ 253 SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \ 254 SNDRV_PCM_RATE_48000) 255 256 static const struct snd_soc_dai_ops wm9705_dai_ops = { 257 .prepare = ac97_prepare, 258 }; 259 260 static struct snd_soc_dai_driver wm9705_dai[] = { 261 { 262 .name = "wm9705-hifi", 263 .playback = { 264 .stream_name = "HiFi Playback", 265 .channels_min = 1, 266 .channels_max = 2, 267 .rates = WM9705_AC97_RATES, 268 .formats = SND_SOC_STD_AC97_FMTS, 269 }, 270 .capture = { 271 .stream_name = "HiFi Capture", 272 .channels_min = 1, 273 .channels_max = 2, 274 .rates = WM9705_AC97_RATES, 275 .formats = SND_SOC_STD_AC97_FMTS, 276 }, 277 .ops = &wm9705_dai_ops, 278 }, 279 { 280 .name = "wm9705-aux", 281 .playback = { 282 .stream_name = "Aux Playback", 283 .channels_min = 1, 284 .channels_max = 1, 285 .rates = WM9705_AC97_RATES, 286 .formats = SNDRV_PCM_FMTBIT_S16_LE, 287 }, 288 } 289 }; 290 291 #ifdef CONFIG_PM 292 static int wm9705_soc_suspend(struct snd_soc_codec *codec) 293 { 294 regcache_cache_bypass(codec->component.regmap, true); 295 snd_soc_write(codec, AC97_POWERDOWN, 0xffff); 296 regcache_cache_bypass(codec->component.regmap, false); 297 298 return 0; 299 } 300 301 static int wm9705_soc_resume(struct snd_soc_codec *codec) 302 { 303 struct wm9705_priv *wm9705 = snd_soc_codec_get_drvdata(codec); 304 int ret; 305 306 ret = snd_ac97_reset(wm9705->ac97, true, WM9705_VENDOR_ID, 307 WM9705_VENDOR_ID_MASK); 308 if (ret < 0) 309 return ret; 310 311 regcache_sync(codec->component.regmap); 312 313 return 0; 314 } 315 #else 316 #define wm9705_soc_suspend NULL 317 #define wm9705_soc_resume NULL 318 #endif 319 320 static int wm9705_soc_probe(struct snd_soc_codec *codec) 321 { 322 struct wm9705_priv *wm9705 = snd_soc_codec_get_drvdata(codec); 323 struct regmap *regmap; 324 325 if (wm9705->mfd_pdata) { 326 wm9705->ac97 = wm9705->mfd_pdata->ac97; 327 regmap = wm9705->mfd_pdata->regmap; 328 } else { 329 #ifdef CONFIG_SND_SOC_AC97_BUS 330 wm9705->ac97 = snd_soc_new_ac97_codec(codec, WM9705_VENDOR_ID, 331 WM9705_VENDOR_ID_MASK); 332 if (IS_ERR(wm9705->ac97)) { 333 dev_err(codec->dev, "Failed to register AC97 codec\n"); 334 return PTR_ERR(wm9705->ac97); 335 } 336 337 regmap = regmap_init_ac97(wm9705->ac97, &wm9705_regmap_config); 338 if (IS_ERR(regmap)) { 339 snd_soc_free_ac97_codec(wm9705->ac97); 340 return PTR_ERR(regmap); 341 } 342 #endif 343 } 344 345 snd_soc_codec_set_drvdata(codec, wm9705->ac97); 346 snd_soc_codec_init_regmap(codec, regmap); 347 348 return 0; 349 } 350 351 static int wm9705_soc_remove(struct snd_soc_codec *codec) 352 { 353 #ifdef CONFIG_SND_SOC_AC97_BUS 354 struct wm9705_priv *wm9705 = snd_soc_codec_get_drvdata(codec); 355 356 if (!wm9705->mfd_pdata) { 357 snd_soc_codec_exit_regmap(codec); 358 snd_soc_free_ac97_codec(wm9705->ac97); 359 } 360 #endif 361 return 0; 362 } 363 364 static const struct snd_soc_codec_driver soc_codec_dev_wm9705 = { 365 .probe = wm9705_soc_probe, 366 .remove = wm9705_soc_remove, 367 .suspend = wm9705_soc_suspend, 368 .resume = wm9705_soc_resume, 369 370 .component_driver = { 371 .controls = wm9705_snd_ac97_controls, 372 .num_controls = ARRAY_SIZE(wm9705_snd_ac97_controls), 373 .dapm_widgets = wm9705_dapm_widgets, 374 .num_dapm_widgets = ARRAY_SIZE(wm9705_dapm_widgets), 375 .dapm_routes = wm9705_audio_map, 376 .num_dapm_routes = ARRAY_SIZE(wm9705_audio_map), 377 }, 378 }; 379 380 static int wm9705_probe(struct platform_device *pdev) 381 { 382 struct wm9705_priv *wm9705; 383 384 wm9705 = devm_kzalloc(&pdev->dev, sizeof(*wm9705), GFP_KERNEL); 385 if (wm9705 == NULL) 386 return -ENOMEM; 387 388 wm9705->mfd_pdata = dev_get_platdata(&pdev->dev); 389 platform_set_drvdata(pdev, wm9705); 390 391 return snd_soc_register_codec(&pdev->dev, 392 &soc_codec_dev_wm9705, wm9705_dai, ARRAY_SIZE(wm9705_dai)); 393 } 394 395 static int wm9705_remove(struct platform_device *pdev) 396 { 397 snd_soc_unregister_codec(&pdev->dev); 398 return 0; 399 } 400 401 static struct platform_driver wm9705_codec_driver = { 402 .driver = { 403 .name = "wm9705-codec", 404 }, 405 406 .probe = wm9705_probe, 407 .remove = wm9705_remove, 408 }; 409 410 module_platform_driver(wm9705_codec_driver); 411 412 MODULE_DESCRIPTION("ASoC WM9705 driver"); 413 MODULE_AUTHOR("Ian Molton"); 414 MODULE_LICENSE("GPL v2"); 415