1 /* 2 * wm8960.c -- WM8960 ALSA SoC Audio driver 3 * 4 * Author: Liam Girdwood 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 2 as 8 * published by the Free Software Foundation. 9 */ 10 11 #include <linux/module.h> 12 #include <linux/moduleparam.h> 13 #include <linux/init.h> 14 #include <linux/delay.h> 15 #include <linux/pm.h> 16 #include <linux/i2c.h> 17 #include <linux/platform_device.h> 18 #include <linux/slab.h> 19 #include <sound/core.h> 20 #include <sound/pcm.h> 21 #include <sound/pcm_params.h> 22 #include <sound/soc.h> 23 #include <sound/soc-dapm.h> 24 #include <sound/initval.h> 25 #include <sound/tlv.h> 26 27 #include "wm8960.h" 28 29 #define AUDIO_NAME "wm8960" 30 31 struct snd_soc_codec_device soc_codec_dev_wm8960; 32 33 /* R25 - Power 1 */ 34 #define WM8960_VREF 0x40 35 36 /* R28 - Anti-pop 1 */ 37 #define WM8960_POBCTRL 0x80 38 #define WM8960_BUFDCOPEN 0x10 39 #define WM8960_BUFIOEN 0x08 40 #define WM8960_SOFT_ST 0x04 41 #define WM8960_HPSTBY 0x01 42 43 /* R29 - Anti-pop 2 */ 44 #define WM8960_DISOP 0x40 45 46 /* 47 * wm8960 register cache 48 * We can't read the WM8960 register space when we are 49 * using 2 wire for device control, so we cache them instead. 50 */ 51 static const u16 wm8960_reg[WM8960_CACHEREGNUM] = { 52 0x0097, 0x0097, 0x0000, 0x0000, 53 0x0000, 0x0008, 0x0000, 0x000a, 54 0x01c0, 0x0000, 0x00ff, 0x00ff, 55 0x0000, 0x0000, 0x0000, 0x0000, 56 0x0000, 0x007b, 0x0100, 0x0032, 57 0x0000, 0x00c3, 0x00c3, 0x01c0, 58 0x0000, 0x0000, 0x0000, 0x0000, 59 0x0000, 0x0000, 0x0000, 0x0000, 60 0x0100, 0x0100, 0x0050, 0x0050, 61 0x0050, 0x0050, 0x0000, 0x0000, 62 0x0000, 0x0000, 0x0040, 0x0000, 63 0x0000, 0x0050, 0x0050, 0x0000, 64 0x0002, 0x0037, 0x004d, 0x0080, 65 0x0008, 0x0031, 0x0026, 0x00e9, 66 }; 67 68 struct wm8960_priv { 69 u16 reg_cache[WM8960_CACHEREGNUM]; 70 struct snd_soc_codec codec; 71 }; 72 73 #define wm8960_reset(c) snd_soc_write(c, WM8960_RESET, 0) 74 75 /* enumerated controls */ 76 static const char *wm8960_deemph[] = {"None", "32Khz", "44.1Khz", "48Khz"}; 77 static const char *wm8960_polarity[] = {"No Inversion", "Left Inverted", 78 "Right Inverted", "Stereo Inversion"}; 79 static const char *wm8960_3d_upper_cutoff[] = {"High", "Low"}; 80 static const char *wm8960_3d_lower_cutoff[] = {"Low", "High"}; 81 static const char *wm8960_alcfunc[] = {"Off", "Right", "Left", "Stereo"}; 82 static const char *wm8960_alcmode[] = {"ALC", "Limiter"}; 83 84 static const struct soc_enum wm8960_enum[] = { 85 SOC_ENUM_SINGLE(WM8960_DACCTL1, 1, 4, wm8960_deemph), 86 SOC_ENUM_SINGLE(WM8960_DACCTL1, 5, 4, wm8960_polarity), 87 SOC_ENUM_SINGLE(WM8960_DACCTL2, 5, 4, wm8960_polarity), 88 SOC_ENUM_SINGLE(WM8960_3D, 6, 2, wm8960_3d_upper_cutoff), 89 SOC_ENUM_SINGLE(WM8960_3D, 5, 2, wm8960_3d_lower_cutoff), 90 SOC_ENUM_SINGLE(WM8960_ALC1, 7, 4, wm8960_alcfunc), 91 SOC_ENUM_SINGLE(WM8960_ALC3, 8, 2, wm8960_alcmode), 92 }; 93 94 static const DECLARE_TLV_DB_SCALE(adc_tlv, -9700, 50, 0); 95 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 50, 1); 96 static const DECLARE_TLV_DB_SCALE(bypass_tlv, -2100, 300, 0); 97 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1); 98 99 static const struct snd_kcontrol_new wm8960_snd_controls[] = { 100 SOC_DOUBLE_R_TLV("Capture Volume", WM8960_LINVOL, WM8960_RINVOL, 101 0, 63, 0, adc_tlv), 102 SOC_DOUBLE_R("Capture Volume ZC Switch", WM8960_LINVOL, WM8960_RINVOL, 103 6, 1, 0), 104 SOC_DOUBLE_R("Capture Switch", WM8960_LINVOL, WM8960_RINVOL, 105 7, 1, 0), 106 107 SOC_DOUBLE_R_TLV("Playback Volume", WM8960_LDAC, WM8960_RDAC, 108 0, 255, 0, dac_tlv), 109 110 SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8960_LOUT1, WM8960_ROUT1, 111 0, 127, 0, out_tlv), 112 SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8960_LOUT1, WM8960_ROUT1, 113 7, 1, 0), 114 115 SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8960_LOUT2, WM8960_ROUT2, 116 0, 127, 0, out_tlv), 117 SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8960_LOUT2, WM8960_ROUT2, 118 7, 1, 0), 119 SOC_SINGLE("Speaker DC Volume", WM8960_CLASSD3, 3, 5, 0), 120 SOC_SINGLE("Speaker AC Volume", WM8960_CLASSD3, 0, 5, 0), 121 122 SOC_SINGLE("PCM Playback -6dB Switch", WM8960_DACCTL1, 7, 1, 0), 123 SOC_ENUM("ADC Polarity", wm8960_enum[1]), 124 SOC_ENUM("Playback De-emphasis", wm8960_enum[0]), 125 SOC_SINGLE("ADC High Pass Filter Switch", WM8960_DACCTL1, 0, 1, 0), 126 127 SOC_ENUM("DAC Polarity", wm8960_enum[2]), 128 129 SOC_ENUM("3D Filter Upper Cut-Off", wm8960_enum[3]), 130 SOC_ENUM("3D Filter Lower Cut-Off", wm8960_enum[4]), 131 SOC_SINGLE("3D Volume", WM8960_3D, 1, 15, 0), 132 SOC_SINGLE("3D Switch", WM8960_3D, 0, 1, 0), 133 134 SOC_ENUM("ALC Function", wm8960_enum[5]), 135 SOC_SINGLE("ALC Max Gain", WM8960_ALC1, 4, 7, 0), 136 SOC_SINGLE("ALC Target", WM8960_ALC1, 0, 15, 1), 137 SOC_SINGLE("ALC Min Gain", WM8960_ALC2, 4, 7, 0), 138 SOC_SINGLE("ALC Hold Time", WM8960_ALC2, 0, 15, 0), 139 SOC_ENUM("ALC Mode", wm8960_enum[6]), 140 SOC_SINGLE("ALC Decay", WM8960_ALC3, 4, 15, 0), 141 SOC_SINGLE("ALC Attack", WM8960_ALC3, 0, 15, 0), 142 143 SOC_SINGLE("Noise Gate Threshold", WM8960_NOISEG, 3, 31, 0), 144 SOC_SINGLE("Noise Gate Switch", WM8960_NOISEG, 0, 1, 0), 145 146 SOC_DOUBLE_R("ADC PCM Capture Volume", WM8960_LINPATH, WM8960_RINPATH, 147 0, 127, 0), 148 149 SOC_SINGLE_TLV("Left Output Mixer Boost Bypass Volume", 150 WM8960_BYPASS1, 4, 7, 1, bypass_tlv), 151 SOC_SINGLE_TLV("Left Output Mixer LINPUT3 Volume", 152 WM8960_LOUTMIX, 4, 7, 1, bypass_tlv), 153 SOC_SINGLE_TLV("Right Output Mixer Boost Bypass Volume", 154 WM8960_BYPASS2, 4, 7, 1, bypass_tlv), 155 SOC_SINGLE_TLV("Right Output Mixer RINPUT3 Volume", 156 WM8960_ROUTMIX, 4, 7, 1, bypass_tlv), 157 }; 158 159 static const struct snd_kcontrol_new wm8960_lin_boost[] = { 160 SOC_DAPM_SINGLE("LINPUT2 Switch", WM8960_LINPATH, 6, 1, 0), 161 SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LINPATH, 7, 1, 0), 162 SOC_DAPM_SINGLE("LINPUT1 Switch", WM8960_LINPATH, 8, 1, 0), 163 }; 164 165 static const struct snd_kcontrol_new wm8960_lin[] = { 166 SOC_DAPM_SINGLE("Boost Switch", WM8960_LINPATH, 3, 1, 0), 167 }; 168 169 static const struct snd_kcontrol_new wm8960_rin_boost[] = { 170 SOC_DAPM_SINGLE("RINPUT2 Switch", WM8960_RINPATH, 6, 1, 0), 171 SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_RINPATH, 7, 1, 0), 172 SOC_DAPM_SINGLE("RINPUT1 Switch", WM8960_RINPATH, 8, 1, 0), 173 }; 174 175 static const struct snd_kcontrol_new wm8960_rin[] = { 176 SOC_DAPM_SINGLE("Boost Switch", WM8960_RINPATH, 3, 1, 0), 177 }; 178 179 static const struct snd_kcontrol_new wm8960_loutput_mixer[] = { 180 SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_LOUTMIX, 8, 1, 0), 181 SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LOUTMIX, 7, 1, 0), 182 SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS1, 7, 1, 0), 183 }; 184 185 static const struct snd_kcontrol_new wm8960_routput_mixer[] = { 186 SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_ROUTMIX, 8, 1, 0), 187 SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_ROUTMIX, 7, 1, 0), 188 SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS2, 7, 1, 0), 189 }; 190 191 static const struct snd_kcontrol_new wm8960_mono_out[] = { 192 SOC_DAPM_SINGLE("Left Switch", WM8960_MONOMIX1, 7, 1, 0), 193 SOC_DAPM_SINGLE("Right Switch", WM8960_MONOMIX2, 7, 1, 0), 194 }; 195 196 static const struct snd_soc_dapm_widget wm8960_dapm_widgets[] = { 197 SND_SOC_DAPM_INPUT("LINPUT1"), 198 SND_SOC_DAPM_INPUT("RINPUT1"), 199 SND_SOC_DAPM_INPUT("LINPUT2"), 200 SND_SOC_DAPM_INPUT("RINPUT2"), 201 SND_SOC_DAPM_INPUT("LINPUT3"), 202 SND_SOC_DAPM_INPUT("RINPUT3"), 203 204 SND_SOC_DAPM_MICBIAS("MICB", WM8960_POWER1, 1, 0), 205 206 SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8960_POWER1, 5, 0, 207 wm8960_lin_boost, ARRAY_SIZE(wm8960_lin_boost)), 208 SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8960_POWER1, 4, 0, 209 wm8960_rin_boost, ARRAY_SIZE(wm8960_rin_boost)), 210 211 SND_SOC_DAPM_MIXER("Left Input Mixer", WM8960_POWER3, 5, 0, 212 wm8960_lin, ARRAY_SIZE(wm8960_lin)), 213 SND_SOC_DAPM_MIXER("Right Input Mixer", WM8960_POWER3, 4, 0, 214 wm8960_rin, ARRAY_SIZE(wm8960_rin)), 215 216 SND_SOC_DAPM_ADC("Left ADC", "Capture", WM8960_POWER2, 3, 0), 217 SND_SOC_DAPM_ADC("Right ADC", "Capture", WM8960_POWER2, 2, 0), 218 219 SND_SOC_DAPM_DAC("Left DAC", "Playback", WM8960_POWER2, 8, 0), 220 SND_SOC_DAPM_DAC("Right DAC", "Playback", WM8960_POWER2, 7, 0), 221 222 SND_SOC_DAPM_MIXER("Left Output Mixer", WM8960_POWER3, 3, 0, 223 &wm8960_loutput_mixer[0], 224 ARRAY_SIZE(wm8960_loutput_mixer)), 225 SND_SOC_DAPM_MIXER("Right Output Mixer", WM8960_POWER3, 2, 0, 226 &wm8960_routput_mixer[0], 227 ARRAY_SIZE(wm8960_routput_mixer)), 228 229 SND_SOC_DAPM_MIXER("Mono Output Mixer", WM8960_POWER2, 1, 0, 230 &wm8960_mono_out[0], 231 ARRAY_SIZE(wm8960_mono_out)), 232 233 SND_SOC_DAPM_PGA("LOUT1 PGA", WM8960_POWER2, 6, 0, NULL, 0), 234 SND_SOC_DAPM_PGA("ROUT1 PGA", WM8960_POWER2, 5, 0, NULL, 0), 235 236 SND_SOC_DAPM_PGA("Left Speaker PGA", WM8960_POWER2, 4, 0, NULL, 0), 237 SND_SOC_DAPM_PGA("Right Speaker PGA", WM8960_POWER2, 3, 0, NULL, 0), 238 239 SND_SOC_DAPM_PGA("Right Speaker Output", WM8960_CLASSD1, 7, 0, NULL, 0), 240 SND_SOC_DAPM_PGA("Left Speaker Output", WM8960_CLASSD1, 6, 0, NULL, 0), 241 242 SND_SOC_DAPM_OUTPUT("SPK_LP"), 243 SND_SOC_DAPM_OUTPUT("SPK_LN"), 244 SND_SOC_DAPM_OUTPUT("HP_L"), 245 SND_SOC_DAPM_OUTPUT("HP_R"), 246 SND_SOC_DAPM_OUTPUT("SPK_RP"), 247 SND_SOC_DAPM_OUTPUT("SPK_RN"), 248 SND_SOC_DAPM_OUTPUT("OUT3"), 249 }; 250 251 static const struct snd_soc_dapm_route audio_paths[] = { 252 { "Left Boost Mixer", "LINPUT1 Switch", "LINPUT1" }, 253 { "Left Boost Mixer", "LINPUT2 Switch", "LINPUT2" }, 254 { "Left Boost Mixer", "LINPUT3 Switch", "LINPUT3" }, 255 256 { "Left Input Mixer", "Boost Switch", "Left Boost Mixer", }, 257 { "Left Input Mixer", NULL, "LINPUT1", }, /* Really Boost Switch */ 258 { "Left Input Mixer", NULL, "LINPUT2" }, 259 { "Left Input Mixer", NULL, "LINPUT3" }, 260 261 { "Right Boost Mixer", "RINPUT1 Switch", "RINPUT1" }, 262 { "Right Boost Mixer", "RINPUT2 Switch", "RINPUT2" }, 263 { "Right Boost Mixer", "RINPUT3 Switch", "RINPUT3" }, 264 265 { "Right Input Mixer", "Boost Switch", "Right Boost Mixer", }, 266 { "Right Input Mixer", NULL, "RINPUT1", }, /* Really Boost Switch */ 267 { "Right Input Mixer", NULL, "RINPUT2" }, 268 { "Right Input Mixer", NULL, "LINPUT3" }, 269 270 { "Left ADC", NULL, "Left Input Mixer" }, 271 { "Right ADC", NULL, "Right Input Mixer" }, 272 273 { "Left Output Mixer", "LINPUT3 Switch", "LINPUT3" }, 274 { "Left Output Mixer", "Boost Bypass Switch", "Left Boost Mixer"} , 275 { "Left Output Mixer", "PCM Playback Switch", "Left DAC" }, 276 277 { "Right Output Mixer", "RINPUT3 Switch", "RINPUT3" }, 278 { "Right Output Mixer", "Boost Bypass Switch", "Right Boost Mixer" } , 279 { "Right Output Mixer", "PCM Playback Switch", "Right DAC" }, 280 281 { "Mono Output Mixer", "Left Switch", "Left Output Mixer" }, 282 { "Mono Output Mixer", "Right Switch", "Right Output Mixer" }, 283 284 { "LOUT1 PGA", NULL, "Left Output Mixer" }, 285 { "ROUT1 PGA", NULL, "Right Output Mixer" }, 286 287 { "HP_L", NULL, "LOUT1 PGA" }, 288 { "HP_R", NULL, "ROUT1 PGA" }, 289 290 { "Left Speaker PGA", NULL, "Left Output Mixer" }, 291 { "Right Speaker PGA", NULL, "Right Output Mixer" }, 292 293 { "Left Speaker Output", NULL, "Left Speaker PGA" }, 294 { "Right Speaker Output", NULL, "Right Speaker PGA" }, 295 296 { "SPK_LN", NULL, "Left Speaker Output" }, 297 { "SPK_LP", NULL, "Left Speaker Output" }, 298 { "SPK_RN", NULL, "Right Speaker Output" }, 299 { "SPK_RP", NULL, "Right Speaker Output" }, 300 301 { "OUT3", NULL, "Mono Output Mixer", } 302 }; 303 304 static int wm8960_add_widgets(struct snd_soc_codec *codec) 305 { 306 snd_soc_dapm_new_controls(codec, wm8960_dapm_widgets, 307 ARRAY_SIZE(wm8960_dapm_widgets)); 308 309 snd_soc_dapm_add_routes(codec, audio_paths, ARRAY_SIZE(audio_paths)); 310 311 return 0; 312 } 313 314 static int wm8960_set_dai_fmt(struct snd_soc_dai *codec_dai, 315 unsigned int fmt) 316 { 317 struct snd_soc_codec *codec = codec_dai->codec; 318 u16 iface = 0; 319 320 /* set master/slave audio interface */ 321 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 322 case SND_SOC_DAIFMT_CBM_CFM: 323 iface |= 0x0040; 324 break; 325 case SND_SOC_DAIFMT_CBS_CFS: 326 break; 327 default: 328 return -EINVAL; 329 } 330 331 /* interface format */ 332 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 333 case SND_SOC_DAIFMT_I2S: 334 iface |= 0x0002; 335 break; 336 case SND_SOC_DAIFMT_RIGHT_J: 337 break; 338 case SND_SOC_DAIFMT_LEFT_J: 339 iface |= 0x0001; 340 break; 341 case SND_SOC_DAIFMT_DSP_A: 342 iface |= 0x0003; 343 break; 344 case SND_SOC_DAIFMT_DSP_B: 345 iface |= 0x0013; 346 break; 347 default: 348 return -EINVAL; 349 } 350 351 /* clock inversion */ 352 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 353 case SND_SOC_DAIFMT_NB_NF: 354 break; 355 case SND_SOC_DAIFMT_IB_IF: 356 iface |= 0x0090; 357 break; 358 case SND_SOC_DAIFMT_IB_NF: 359 iface |= 0x0080; 360 break; 361 case SND_SOC_DAIFMT_NB_IF: 362 iface |= 0x0010; 363 break; 364 default: 365 return -EINVAL; 366 } 367 368 /* set iface */ 369 snd_soc_write(codec, WM8960_IFACE1, iface); 370 return 0; 371 } 372 373 static int wm8960_hw_params(struct snd_pcm_substream *substream, 374 struct snd_pcm_hw_params *params, 375 struct snd_soc_dai *dai) 376 { 377 struct snd_soc_pcm_runtime *rtd = substream->private_data; 378 struct snd_soc_device *socdev = rtd->socdev; 379 struct snd_soc_codec *codec = socdev->card->codec; 380 u16 iface = snd_soc_read(codec, WM8960_IFACE1) & 0xfff3; 381 382 /* bit size */ 383 switch (params_format(params)) { 384 case SNDRV_PCM_FORMAT_S16_LE: 385 break; 386 case SNDRV_PCM_FORMAT_S20_3LE: 387 iface |= 0x0004; 388 break; 389 case SNDRV_PCM_FORMAT_S24_LE: 390 iface |= 0x0008; 391 break; 392 } 393 394 /* set iface */ 395 snd_soc_write(codec, WM8960_IFACE1, iface); 396 return 0; 397 } 398 399 static int wm8960_mute(struct snd_soc_dai *dai, int mute) 400 { 401 struct snd_soc_codec *codec = dai->codec; 402 u16 mute_reg = snd_soc_read(codec, WM8960_DACCTL1) & 0xfff7; 403 404 if (mute) 405 snd_soc_write(codec, WM8960_DACCTL1, mute_reg | 0x8); 406 else 407 snd_soc_write(codec, WM8960_DACCTL1, mute_reg); 408 return 0; 409 } 410 411 static int wm8960_set_bias_level(struct snd_soc_codec *codec, 412 enum snd_soc_bias_level level) 413 { 414 struct wm8960_data *pdata = codec->dev->platform_data; 415 u16 reg; 416 417 switch (level) { 418 case SND_SOC_BIAS_ON: 419 break; 420 421 case SND_SOC_BIAS_PREPARE: 422 /* Set VMID to 2x50k */ 423 reg = snd_soc_read(codec, WM8960_POWER1); 424 reg &= ~0x180; 425 reg |= 0x80; 426 snd_soc_write(codec, WM8960_POWER1, reg); 427 break; 428 429 case SND_SOC_BIAS_STANDBY: 430 if (codec->bias_level == SND_SOC_BIAS_OFF) { 431 /* Enable anti-pop features */ 432 snd_soc_write(codec, WM8960_APOP1, 433 WM8960_POBCTRL | WM8960_SOFT_ST | 434 WM8960_BUFDCOPEN | WM8960_BUFIOEN); 435 436 /* Discharge HP output */ 437 reg = WM8960_DISOP; 438 if (pdata) 439 reg |= pdata->dres << 4; 440 snd_soc_write(codec, WM8960_APOP2, reg); 441 442 msleep(400); 443 444 snd_soc_write(codec, WM8960_APOP2, 0); 445 446 /* Enable & ramp VMID at 2x50k */ 447 reg = snd_soc_read(codec, WM8960_POWER1); 448 reg |= 0x80; 449 snd_soc_write(codec, WM8960_POWER1, reg); 450 msleep(100); 451 452 /* Enable VREF */ 453 snd_soc_write(codec, WM8960_POWER1, reg | WM8960_VREF); 454 455 /* Disable anti-pop features */ 456 snd_soc_write(codec, WM8960_APOP1, WM8960_BUFIOEN); 457 } 458 459 /* Set VMID to 2x250k */ 460 reg = snd_soc_read(codec, WM8960_POWER1); 461 reg &= ~0x180; 462 reg |= 0x100; 463 snd_soc_write(codec, WM8960_POWER1, reg); 464 break; 465 466 case SND_SOC_BIAS_OFF: 467 /* Enable anti-pop features */ 468 snd_soc_write(codec, WM8960_APOP1, 469 WM8960_POBCTRL | WM8960_SOFT_ST | 470 WM8960_BUFDCOPEN | WM8960_BUFIOEN); 471 472 /* Disable VMID and VREF, let them discharge */ 473 snd_soc_write(codec, WM8960_POWER1, 0); 474 msleep(600); 475 476 snd_soc_write(codec, WM8960_APOP1, 0); 477 break; 478 } 479 480 codec->bias_level = level; 481 482 return 0; 483 } 484 485 /* PLL divisors */ 486 struct _pll_div { 487 u32 pre_div:1; 488 u32 n:4; 489 u32 k:24; 490 }; 491 492 /* The size in bits of the pll divide multiplied by 10 493 * to allow rounding later */ 494 #define FIXED_PLL_SIZE ((1 << 24) * 10) 495 496 static int pll_factors(unsigned int source, unsigned int target, 497 struct _pll_div *pll_div) 498 { 499 unsigned long long Kpart; 500 unsigned int K, Ndiv, Nmod; 501 502 pr_debug("WM8960 PLL: setting %dHz->%dHz\n", source, target); 503 504 /* Scale up target to PLL operating frequency */ 505 target *= 4; 506 507 Ndiv = target / source; 508 if (Ndiv < 6) { 509 source >>= 1; 510 pll_div->pre_div = 1; 511 Ndiv = target / source; 512 } else 513 pll_div->pre_div = 0; 514 515 if ((Ndiv < 6) || (Ndiv > 12)) { 516 pr_err("WM8960 PLL: Unsupported N=%d\n", Ndiv); 517 return -EINVAL; 518 } 519 520 pll_div->n = Ndiv; 521 Nmod = target % source; 522 Kpart = FIXED_PLL_SIZE * (long long)Nmod; 523 524 do_div(Kpart, source); 525 526 K = Kpart & 0xFFFFFFFF; 527 528 /* Check if we need to round */ 529 if ((K % 10) >= 5) 530 K += 5; 531 532 /* Move down to proper range now rounding is done */ 533 K /= 10; 534 535 pll_div->k = K; 536 537 pr_debug("WM8960 PLL: N=%x K=%x pre_div=%d\n", 538 pll_div->n, pll_div->k, pll_div->pre_div); 539 540 return 0; 541 } 542 543 static int wm8960_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id, 544 int source, unsigned int freq_in, unsigned int freq_out) 545 { 546 struct snd_soc_codec *codec = codec_dai->codec; 547 u16 reg; 548 static struct _pll_div pll_div; 549 int ret; 550 551 if (freq_in && freq_out) { 552 ret = pll_factors(freq_in, freq_out, &pll_div); 553 if (ret != 0) 554 return ret; 555 } 556 557 /* Disable the PLL: even if we are changing the frequency the 558 * PLL needs to be disabled while we do so. */ 559 snd_soc_write(codec, WM8960_CLOCK1, 560 snd_soc_read(codec, WM8960_CLOCK1) & ~1); 561 snd_soc_write(codec, WM8960_POWER2, 562 snd_soc_read(codec, WM8960_POWER2) & ~1); 563 564 if (!freq_in || !freq_out) 565 return 0; 566 567 reg = snd_soc_read(codec, WM8960_PLL1) & ~0x3f; 568 reg |= pll_div.pre_div << 4; 569 reg |= pll_div.n; 570 571 if (pll_div.k) { 572 reg |= 0x20; 573 574 snd_soc_write(codec, WM8960_PLL2, (pll_div.k >> 18) & 0x3f); 575 snd_soc_write(codec, WM8960_PLL3, (pll_div.k >> 9) & 0x1ff); 576 snd_soc_write(codec, WM8960_PLL4, pll_div.k & 0x1ff); 577 } 578 snd_soc_write(codec, WM8960_PLL1, reg); 579 580 /* Turn it on */ 581 snd_soc_write(codec, WM8960_POWER2, 582 snd_soc_read(codec, WM8960_POWER2) | 1); 583 msleep(250); 584 snd_soc_write(codec, WM8960_CLOCK1, 585 snd_soc_read(codec, WM8960_CLOCK1) | 1); 586 587 return 0; 588 } 589 590 static int wm8960_set_dai_clkdiv(struct snd_soc_dai *codec_dai, 591 int div_id, int div) 592 { 593 struct snd_soc_codec *codec = codec_dai->codec; 594 u16 reg; 595 596 switch (div_id) { 597 case WM8960_SYSCLKSEL: 598 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1fe; 599 snd_soc_write(codec, WM8960_CLOCK1, reg | div); 600 break; 601 case WM8960_SYSCLKDIV: 602 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1f9; 603 snd_soc_write(codec, WM8960_CLOCK1, reg | div); 604 break; 605 case WM8960_DACDIV: 606 reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1c7; 607 snd_soc_write(codec, WM8960_CLOCK1, reg | div); 608 break; 609 case WM8960_OPCLKDIV: 610 reg = snd_soc_read(codec, WM8960_PLL1) & 0x03f; 611 snd_soc_write(codec, WM8960_PLL1, reg | div); 612 break; 613 case WM8960_DCLKDIV: 614 reg = snd_soc_read(codec, WM8960_CLOCK2) & 0x03f; 615 snd_soc_write(codec, WM8960_CLOCK2, reg | div); 616 break; 617 case WM8960_TOCLKSEL: 618 reg = snd_soc_read(codec, WM8960_ADDCTL1) & 0x1fd; 619 snd_soc_write(codec, WM8960_ADDCTL1, reg | div); 620 break; 621 default: 622 return -EINVAL; 623 } 624 625 return 0; 626 } 627 628 #define WM8960_RATES SNDRV_PCM_RATE_8000_48000 629 630 #define WM8960_FORMATS \ 631 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ 632 SNDRV_PCM_FMTBIT_S24_LE) 633 634 static struct snd_soc_dai_ops wm8960_dai_ops = { 635 .hw_params = wm8960_hw_params, 636 .digital_mute = wm8960_mute, 637 .set_fmt = wm8960_set_dai_fmt, 638 .set_clkdiv = wm8960_set_dai_clkdiv, 639 .set_pll = wm8960_set_dai_pll, 640 }; 641 642 struct snd_soc_dai wm8960_dai = { 643 .name = "WM8960", 644 .playback = { 645 .stream_name = "Playback", 646 .channels_min = 1, 647 .channels_max = 2, 648 .rates = WM8960_RATES, 649 .formats = WM8960_FORMATS,}, 650 .capture = { 651 .stream_name = "Capture", 652 .channels_min = 1, 653 .channels_max = 2, 654 .rates = WM8960_RATES, 655 .formats = WM8960_FORMATS,}, 656 .ops = &wm8960_dai_ops, 657 .symmetric_rates = 1, 658 }; 659 EXPORT_SYMBOL_GPL(wm8960_dai); 660 661 static int wm8960_suspend(struct platform_device *pdev, pm_message_t state) 662 { 663 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 664 struct snd_soc_codec *codec = socdev->card->codec; 665 666 wm8960_set_bias_level(codec, SND_SOC_BIAS_OFF); 667 return 0; 668 } 669 670 static int wm8960_resume(struct platform_device *pdev) 671 { 672 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 673 struct snd_soc_codec *codec = socdev->card->codec; 674 int i; 675 u8 data[2]; 676 u16 *cache = codec->reg_cache; 677 678 /* Sync reg_cache with the hardware */ 679 for (i = 0; i < ARRAY_SIZE(wm8960_reg); i++) { 680 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001); 681 data[1] = cache[i] & 0x00ff; 682 codec->hw_write(codec->control_data, data, 2); 683 } 684 685 wm8960_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 686 wm8960_set_bias_level(codec, codec->suspend_bias_level); 687 return 0; 688 } 689 690 static struct snd_soc_codec *wm8960_codec; 691 692 static int wm8960_probe(struct platform_device *pdev) 693 { 694 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 695 struct snd_soc_codec *codec; 696 int ret = 0; 697 698 if (wm8960_codec == NULL) { 699 dev_err(&pdev->dev, "Codec device not registered\n"); 700 return -ENODEV; 701 } 702 703 socdev->card->codec = wm8960_codec; 704 codec = wm8960_codec; 705 706 /* register pcms */ 707 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); 708 if (ret < 0) { 709 dev_err(codec->dev, "failed to create pcms: %d\n", ret); 710 goto pcm_err; 711 } 712 713 snd_soc_add_controls(codec, wm8960_snd_controls, 714 ARRAY_SIZE(wm8960_snd_controls)); 715 wm8960_add_widgets(codec); 716 717 return ret; 718 719 pcm_err: 720 return ret; 721 } 722 723 /* power down chip */ 724 static int wm8960_remove(struct platform_device *pdev) 725 { 726 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 727 728 snd_soc_free_pcms(socdev); 729 snd_soc_dapm_free(socdev); 730 731 return 0; 732 } 733 734 struct snd_soc_codec_device soc_codec_dev_wm8960 = { 735 .probe = wm8960_probe, 736 .remove = wm8960_remove, 737 .suspend = wm8960_suspend, 738 .resume = wm8960_resume, 739 }; 740 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8960); 741 742 static int wm8960_register(struct wm8960_priv *wm8960, 743 enum snd_soc_control_type control) 744 { 745 struct wm8960_data *pdata = wm8960->codec.dev->platform_data; 746 struct snd_soc_codec *codec = &wm8960->codec; 747 int ret; 748 u16 reg; 749 750 if (wm8960_codec) { 751 dev_err(codec->dev, "Another WM8960 is registered\n"); 752 ret = -EINVAL; 753 goto err; 754 } 755 756 if (!pdata) { 757 dev_warn(codec->dev, "No platform data supplied\n"); 758 } else { 759 if (pdata->dres > WM8960_DRES_MAX) { 760 dev_err(codec->dev, "Invalid DRES: %d\n", pdata->dres); 761 pdata->dres = 0; 762 } 763 } 764 765 mutex_init(&codec->mutex); 766 INIT_LIST_HEAD(&codec->dapm_widgets); 767 INIT_LIST_HEAD(&codec->dapm_paths); 768 769 codec->private_data = wm8960; 770 codec->name = "WM8960"; 771 codec->owner = THIS_MODULE; 772 codec->bias_level = SND_SOC_BIAS_OFF; 773 codec->set_bias_level = wm8960_set_bias_level; 774 codec->dai = &wm8960_dai; 775 codec->num_dai = 1; 776 codec->reg_cache_size = WM8960_CACHEREGNUM; 777 codec->reg_cache = &wm8960->reg_cache; 778 779 memcpy(codec->reg_cache, wm8960_reg, sizeof(wm8960_reg)); 780 781 ret = snd_soc_codec_set_cache_io(codec, 7, 9, control); 782 if (ret < 0) { 783 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); 784 goto err; 785 } 786 787 ret = wm8960_reset(codec); 788 if (ret < 0) { 789 dev_err(codec->dev, "Failed to issue reset\n"); 790 goto err; 791 } 792 793 wm8960_dai.dev = codec->dev; 794 795 wm8960_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 796 797 /* Latch the update bits */ 798 reg = snd_soc_read(codec, WM8960_LINVOL); 799 snd_soc_write(codec, WM8960_LINVOL, reg | 0x100); 800 reg = snd_soc_read(codec, WM8960_RINVOL); 801 snd_soc_write(codec, WM8960_RINVOL, reg | 0x100); 802 reg = snd_soc_read(codec, WM8960_LADC); 803 snd_soc_write(codec, WM8960_LADC, reg | 0x100); 804 reg = snd_soc_read(codec, WM8960_RADC); 805 snd_soc_write(codec, WM8960_RADC, reg | 0x100); 806 reg = snd_soc_read(codec, WM8960_LDAC); 807 snd_soc_write(codec, WM8960_LDAC, reg | 0x100); 808 reg = snd_soc_read(codec, WM8960_RDAC); 809 snd_soc_write(codec, WM8960_RDAC, reg | 0x100); 810 reg = snd_soc_read(codec, WM8960_LOUT1); 811 snd_soc_write(codec, WM8960_LOUT1, reg | 0x100); 812 reg = snd_soc_read(codec, WM8960_ROUT1); 813 snd_soc_write(codec, WM8960_ROUT1, reg | 0x100); 814 reg = snd_soc_read(codec, WM8960_LOUT2); 815 snd_soc_write(codec, WM8960_LOUT2, reg | 0x100); 816 reg = snd_soc_read(codec, WM8960_ROUT2); 817 snd_soc_write(codec, WM8960_ROUT2, reg | 0x100); 818 819 wm8960_codec = codec; 820 821 ret = snd_soc_register_codec(codec); 822 if (ret != 0) { 823 dev_err(codec->dev, "Failed to register codec: %d\n", ret); 824 goto err; 825 } 826 827 ret = snd_soc_register_dai(&wm8960_dai); 828 if (ret != 0) { 829 dev_err(codec->dev, "Failed to register DAI: %d\n", ret); 830 goto err_codec; 831 } 832 833 return 0; 834 835 err_codec: 836 snd_soc_unregister_codec(codec); 837 err: 838 kfree(wm8960); 839 return ret; 840 } 841 842 static void wm8960_unregister(struct wm8960_priv *wm8960) 843 { 844 wm8960_set_bias_level(&wm8960->codec, SND_SOC_BIAS_OFF); 845 snd_soc_unregister_dai(&wm8960_dai); 846 snd_soc_unregister_codec(&wm8960->codec); 847 kfree(wm8960); 848 wm8960_codec = NULL; 849 } 850 851 static __devinit int wm8960_i2c_probe(struct i2c_client *i2c, 852 const struct i2c_device_id *id) 853 { 854 struct wm8960_priv *wm8960; 855 struct snd_soc_codec *codec; 856 857 wm8960 = kzalloc(sizeof(struct wm8960_priv), GFP_KERNEL); 858 if (wm8960 == NULL) 859 return -ENOMEM; 860 861 codec = &wm8960->codec; 862 863 i2c_set_clientdata(i2c, wm8960); 864 codec->control_data = i2c; 865 866 codec->dev = &i2c->dev; 867 868 return wm8960_register(wm8960, SND_SOC_I2C); 869 } 870 871 static __devexit int wm8960_i2c_remove(struct i2c_client *client) 872 { 873 struct wm8960_priv *wm8960 = i2c_get_clientdata(client); 874 wm8960_unregister(wm8960); 875 return 0; 876 } 877 878 static const struct i2c_device_id wm8960_i2c_id[] = { 879 { "wm8960", 0 }, 880 { } 881 }; 882 MODULE_DEVICE_TABLE(i2c, wm8960_i2c_id); 883 884 static struct i2c_driver wm8960_i2c_driver = { 885 .driver = { 886 .name = "WM8960 I2C Codec", 887 .owner = THIS_MODULE, 888 }, 889 .probe = wm8960_i2c_probe, 890 .remove = __devexit_p(wm8960_i2c_remove), 891 .id_table = wm8960_i2c_id, 892 }; 893 894 static int __init wm8960_modinit(void) 895 { 896 int ret; 897 898 ret = i2c_add_driver(&wm8960_i2c_driver); 899 if (ret != 0) { 900 printk(KERN_ERR "Failed to register WM8960 I2C driver: %d\n", 901 ret); 902 } 903 904 return ret; 905 } 906 module_init(wm8960_modinit); 907 908 static void __exit wm8960_exit(void) 909 { 910 i2c_del_driver(&wm8960_i2c_driver); 911 } 912 module_exit(wm8960_exit); 913 914 915 MODULE_DESCRIPTION("ASoC WM8960 driver"); 916 MODULE_AUTHOR("Liam Girdwood"); 917 MODULE_LICENSE("GPL"); 918