1 /* 2 * wm8940.c -- WM8940 ALSA Soc Audio driver 3 * 4 * Author: Jonathan Cameron <jic23@cam.ac.uk> 5 * 6 * Based on wm8510.c 7 * Copyright 2006 Wolfson Microelectronics PLC. 8 * Author: Liam Girdwood <lrg@slimlogic.co.uk> 9 * 10 * This program is free software; you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License version 2 as 12 * published by the Free Software Foundation. 13 * 14 * Not currently handled: 15 * Notch filter control 16 * AUXMode (inverting vs mixer) 17 * No means to obtain current gain if alc enabled. 18 * No use made of gpio 19 * Fast VMID discharge for power down 20 * Soft Start 21 * DLR and ALR Swaps not enabled 22 * Digital Sidetone not supported 23 */ 24 #include <linux/module.h> 25 #include <linux/moduleparam.h> 26 #include <linux/kernel.h> 27 #include <linux/init.h> 28 #include <linux/delay.h> 29 #include <linux/pm.h> 30 #include <linux/i2c.h> 31 #include <linux/platform_device.h> 32 #include <linux/spi/spi.h> 33 #include <sound/core.h> 34 #include <sound/pcm.h> 35 #include <sound/pcm_params.h> 36 #include <sound/soc.h> 37 #include <sound/soc-dapm.h> 38 #include <sound/initval.h> 39 #include <sound/tlv.h> 40 41 #include "wm8940.h" 42 43 struct wm8940_priv { 44 unsigned int sysclk; 45 u16 reg_cache[WM8940_CACHEREGNUM]; 46 struct snd_soc_codec codec; 47 }; 48 49 static u16 wm8940_reg_defaults[] = { 50 0x8940, /* Soft Reset */ 51 0x0000, /* Power 1 */ 52 0x0000, /* Power 2 */ 53 0x0000, /* Power 3 */ 54 0x0010, /* Interface Control */ 55 0x0000, /* Companding Control */ 56 0x0140, /* Clock Control */ 57 0x0000, /* Additional Controls */ 58 0x0000, /* GPIO Control */ 59 0x0002, /* Auto Increment Control */ 60 0x0000, /* DAC Control */ 61 0x00FF, /* DAC Volume */ 62 0, 63 0, 64 0x0100, /* ADC Control */ 65 0x00FF, /* ADC Volume */ 66 0x0000, /* Notch Filter 1 Control 1 */ 67 0x0000, /* Notch Filter 1 Control 2 */ 68 0x0000, /* Notch Filter 2 Control 1 */ 69 0x0000, /* Notch Filter 2 Control 2 */ 70 0x0000, /* Notch Filter 3 Control 1 */ 71 0x0000, /* Notch Filter 3 Control 2 */ 72 0x0000, /* Notch Filter 4 Control 1 */ 73 0x0000, /* Notch Filter 4 Control 2 */ 74 0x0032, /* DAC Limit Control 1 */ 75 0x0000, /* DAC Limit Control 2 */ 76 0, 77 0, 78 0, 79 0, 80 0, 81 0, 82 0x0038, /* ALC Control 1 */ 83 0x000B, /* ALC Control 2 */ 84 0x0032, /* ALC Control 3 */ 85 0x0000, /* Noise Gate */ 86 0x0041, /* PLLN */ 87 0x000C, /* PLLK1 */ 88 0x0093, /* PLLK2 */ 89 0x00E9, /* PLLK3 */ 90 0, 91 0, 92 0x0030, /* ALC Control 4 */ 93 0, 94 0x0002, /* Input Control */ 95 0x0050, /* PGA Gain */ 96 0, 97 0x0002, /* ADC Boost Control */ 98 0, 99 0x0002, /* Output Control */ 100 0x0000, /* Speaker Mixer Control */ 101 0, 102 0, 103 0, 104 0x0079, /* Speaker Volume */ 105 0, 106 0x0000, /* Mono Mixer Control */ 107 }; 108 109 static const char *wm8940_companding[] = { "Off", "NC", "u-law", "A-law" }; 110 static const struct soc_enum wm8940_adc_companding_enum 111 = SOC_ENUM_SINGLE(WM8940_COMPANDINGCTL, 1, 4, wm8940_companding); 112 static const struct soc_enum wm8940_dac_companding_enum 113 = SOC_ENUM_SINGLE(WM8940_COMPANDINGCTL, 3, 4, wm8940_companding); 114 115 static const char *wm8940_alc_mode_text[] = {"ALC", "Limiter"}; 116 static const struct soc_enum wm8940_alc_mode_enum 117 = SOC_ENUM_SINGLE(WM8940_ALC3, 8, 2, wm8940_alc_mode_text); 118 119 static const char *wm8940_mic_bias_level_text[] = {"0.9", "0.65"}; 120 static const struct soc_enum wm8940_mic_bias_level_enum 121 = SOC_ENUM_SINGLE(WM8940_INPUTCTL, 8, 2, wm8940_mic_bias_level_text); 122 123 static const char *wm8940_filter_mode_text[] = {"Audio", "Application"}; 124 static const struct soc_enum wm8940_filter_mode_enum 125 = SOC_ENUM_SINGLE(WM8940_ADC, 7, 2, wm8940_filter_mode_text); 126 127 static DECLARE_TLV_DB_SCALE(wm8940_spk_vol_tlv, -5700, 100, 1); 128 static DECLARE_TLV_DB_SCALE(wm8940_att_tlv, -1000, 1000, 0); 129 static DECLARE_TLV_DB_SCALE(wm8940_pga_vol_tlv, -1200, 75, 0); 130 static DECLARE_TLV_DB_SCALE(wm8940_alc_min_tlv, -1200, 600, 0); 131 static DECLARE_TLV_DB_SCALE(wm8940_alc_max_tlv, 675, 600, 0); 132 static DECLARE_TLV_DB_SCALE(wm8940_alc_tar_tlv, -2250, 50, 0); 133 static DECLARE_TLV_DB_SCALE(wm8940_lim_boost_tlv, 0, 100, 0); 134 static DECLARE_TLV_DB_SCALE(wm8940_lim_thresh_tlv, -600, 100, 0); 135 static DECLARE_TLV_DB_SCALE(wm8940_adc_tlv, -12750, 50, 1); 136 static DECLARE_TLV_DB_SCALE(wm8940_capture_boost_vol_tlv, 0, 2000, 0); 137 138 static const struct snd_kcontrol_new wm8940_snd_controls[] = { 139 SOC_SINGLE("Digital Loopback Switch", WM8940_COMPANDINGCTL, 140 6, 1, 0), 141 SOC_ENUM("DAC Companding", wm8940_dac_companding_enum), 142 SOC_ENUM("ADC Companding", wm8940_adc_companding_enum), 143 144 SOC_ENUM("ALC Mode", wm8940_alc_mode_enum), 145 SOC_SINGLE("ALC Switch", WM8940_ALC1, 8, 1, 0), 146 SOC_SINGLE_TLV("ALC Capture Max Gain", WM8940_ALC1, 147 3, 7, 1, wm8940_alc_max_tlv), 148 SOC_SINGLE_TLV("ALC Capture Min Gain", WM8940_ALC1, 149 0, 7, 0, wm8940_alc_min_tlv), 150 SOC_SINGLE_TLV("ALC Capture Target", WM8940_ALC2, 151 0, 14, 0, wm8940_alc_tar_tlv), 152 SOC_SINGLE("ALC Capture Hold", WM8940_ALC2, 4, 10, 0), 153 SOC_SINGLE("ALC Capture Decay", WM8940_ALC3, 4, 10, 0), 154 SOC_SINGLE("ALC Capture Attach", WM8940_ALC3, 0, 10, 0), 155 SOC_SINGLE("ALC ZC Switch", WM8940_ALC4, 1, 1, 0), 156 SOC_SINGLE("ALC Capture Noise Gate Switch", WM8940_NOISEGATE, 157 3, 1, 0), 158 SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8940_NOISEGATE, 159 0, 7, 0), 160 161 SOC_SINGLE("DAC Playback Limiter Switch", WM8940_DACLIM1, 8, 1, 0), 162 SOC_SINGLE("DAC Playback Limiter Attack", WM8940_DACLIM1, 0, 9, 0), 163 SOC_SINGLE("DAC Playback Limiter Decay", WM8940_DACLIM1, 4, 11, 0), 164 SOC_SINGLE_TLV("DAC Playback Limiter Threshold", WM8940_DACLIM2, 165 4, 9, 1, wm8940_lim_thresh_tlv), 166 SOC_SINGLE_TLV("DAC Playback Limiter Boost", WM8940_DACLIM2, 167 0, 12, 0, wm8940_lim_boost_tlv), 168 169 SOC_SINGLE("Capture PGA ZC Switch", WM8940_PGAGAIN, 7, 1, 0), 170 SOC_SINGLE_TLV("Capture PGA Volume", WM8940_PGAGAIN, 171 0, 63, 0, wm8940_pga_vol_tlv), 172 SOC_SINGLE_TLV("Digital Playback Volume", WM8940_DACVOL, 173 0, 255, 0, wm8940_adc_tlv), 174 SOC_SINGLE_TLV("Digital Capture Volume", WM8940_ADCVOL, 175 0, 255, 0, wm8940_adc_tlv), 176 SOC_ENUM("Mic Bias Level", wm8940_mic_bias_level_enum), 177 SOC_SINGLE_TLV("Capture Boost Volue", WM8940_ADCBOOST, 178 8, 1, 0, wm8940_capture_boost_vol_tlv), 179 SOC_SINGLE_TLV("Speaker Playback Volume", WM8940_SPKVOL, 180 0, 63, 0, wm8940_spk_vol_tlv), 181 SOC_SINGLE("Speaker Playback Switch", WM8940_SPKVOL, 6, 1, 1), 182 183 SOC_SINGLE_TLV("Speaker Mixer Line Bypass Volume", WM8940_SPKVOL, 184 8, 1, 1, wm8940_att_tlv), 185 SOC_SINGLE("Speaker Playback ZC Switch", WM8940_SPKVOL, 7, 1, 0), 186 187 SOC_SINGLE("Mono Out Switch", WM8940_MONOMIX, 6, 1, 1), 188 SOC_SINGLE_TLV("Mono Mixer Line Bypass Volume", WM8940_MONOMIX, 189 7, 1, 1, wm8940_att_tlv), 190 191 SOC_SINGLE("High Pass Filter Switch", WM8940_ADC, 8, 1, 0), 192 SOC_ENUM("High Pass Filter Mode", wm8940_filter_mode_enum), 193 SOC_SINGLE("High Pass Filter Cut Off", WM8940_ADC, 4, 7, 0), 194 SOC_SINGLE("ADC Inversion Switch", WM8940_ADC, 0, 1, 0), 195 SOC_SINGLE("DAC Inversion Switch", WM8940_DAC, 0, 1, 0), 196 SOC_SINGLE("DAC Auto Mute Switch", WM8940_DAC, 2, 1, 0), 197 SOC_SINGLE("ZC Timeout Clock Switch", WM8940_ADDCNTRL, 0, 1, 0), 198 }; 199 200 static const struct snd_kcontrol_new wm8940_speaker_mixer_controls[] = { 201 SOC_DAPM_SINGLE("Line Bypass Switch", WM8940_SPKMIX, 1, 1, 0), 202 SOC_DAPM_SINGLE("Aux Playback Switch", WM8940_SPKMIX, 5, 1, 0), 203 SOC_DAPM_SINGLE("PCM Playback Switch", WM8940_SPKMIX, 0, 1, 0), 204 }; 205 206 static const struct snd_kcontrol_new wm8940_mono_mixer_controls[] = { 207 SOC_DAPM_SINGLE("Line Bypass Switch", WM8940_MONOMIX, 1, 1, 0), 208 SOC_DAPM_SINGLE("Aux Playback Switch", WM8940_MONOMIX, 2, 1, 0), 209 SOC_DAPM_SINGLE("PCM Playback Switch", WM8940_MONOMIX, 0, 1, 0), 210 }; 211 212 static DECLARE_TLV_DB_SCALE(wm8940_boost_vol_tlv, -1500, 300, 1); 213 static const struct snd_kcontrol_new wm8940_input_boost_controls[] = { 214 SOC_DAPM_SINGLE("Mic PGA Switch", WM8940_PGAGAIN, 6, 1, 1), 215 SOC_DAPM_SINGLE_TLV("Aux Volume", WM8940_ADCBOOST, 216 0, 7, 0, wm8940_boost_vol_tlv), 217 SOC_DAPM_SINGLE_TLV("Mic Volume", WM8940_ADCBOOST, 218 4, 7, 0, wm8940_boost_vol_tlv), 219 }; 220 221 static const struct snd_kcontrol_new wm8940_micpga_controls[] = { 222 SOC_DAPM_SINGLE("AUX Switch", WM8940_INPUTCTL, 2, 1, 0), 223 SOC_DAPM_SINGLE("MICP Switch", WM8940_INPUTCTL, 0, 1, 0), 224 SOC_DAPM_SINGLE("MICN Switch", WM8940_INPUTCTL, 1, 1, 0), 225 }; 226 227 static const struct snd_soc_dapm_widget wm8940_dapm_widgets[] = { 228 SND_SOC_DAPM_MIXER("Speaker Mixer", WM8940_POWER3, 2, 0, 229 &wm8940_speaker_mixer_controls[0], 230 ARRAY_SIZE(wm8940_speaker_mixer_controls)), 231 SND_SOC_DAPM_MIXER("Mono Mixer", WM8940_POWER3, 3, 0, 232 &wm8940_mono_mixer_controls[0], 233 ARRAY_SIZE(wm8940_mono_mixer_controls)), 234 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8940_POWER3, 0, 0), 235 236 SND_SOC_DAPM_PGA("SpkN Out", WM8940_POWER3, 5, 0, NULL, 0), 237 SND_SOC_DAPM_PGA("SpkP Out", WM8940_POWER3, 6, 0, NULL, 0), 238 SND_SOC_DAPM_PGA("Mono Out", WM8940_POWER3, 7, 0, NULL, 0), 239 SND_SOC_DAPM_OUTPUT("MONOOUT"), 240 SND_SOC_DAPM_OUTPUT("SPKOUTP"), 241 SND_SOC_DAPM_OUTPUT("SPKOUTN"), 242 243 SND_SOC_DAPM_PGA("Aux Input", WM8940_POWER1, 6, 0, NULL, 0), 244 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8940_POWER2, 0, 0), 245 SND_SOC_DAPM_MIXER("Mic PGA", WM8940_POWER2, 2, 0, 246 &wm8940_micpga_controls[0], 247 ARRAY_SIZE(wm8940_micpga_controls)), 248 SND_SOC_DAPM_MIXER("Boost Mixer", WM8940_POWER2, 4, 0, 249 &wm8940_input_boost_controls[0], 250 ARRAY_SIZE(wm8940_input_boost_controls)), 251 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8940_POWER1, 4, 0), 252 253 SND_SOC_DAPM_INPUT("MICN"), 254 SND_SOC_DAPM_INPUT("MICP"), 255 SND_SOC_DAPM_INPUT("AUX"), 256 }; 257 258 static const struct snd_soc_dapm_route audio_map[] = { 259 /* Mono output mixer */ 260 {"Mono Mixer", "PCM Playback Switch", "DAC"}, 261 {"Mono Mixer", "Aux Playback Switch", "Aux Input"}, 262 {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"}, 263 264 /* Speaker output mixer */ 265 {"Speaker Mixer", "PCM Playback Switch", "DAC"}, 266 {"Speaker Mixer", "Aux Playback Switch", "Aux Input"}, 267 {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"}, 268 269 /* Outputs */ 270 {"Mono Out", NULL, "Mono Mixer"}, 271 {"MONOOUT", NULL, "Mono Out"}, 272 {"SpkN Out", NULL, "Speaker Mixer"}, 273 {"SpkP Out", NULL, "Speaker Mixer"}, 274 {"SPKOUTN", NULL, "SpkN Out"}, 275 {"SPKOUTP", NULL, "SpkP Out"}, 276 277 /* Microphone PGA */ 278 {"Mic PGA", "MICN Switch", "MICN"}, 279 {"Mic PGA", "MICP Switch", "MICP"}, 280 {"Mic PGA", "AUX Switch", "AUX"}, 281 282 /* Boost Mixer */ 283 {"Boost Mixer", "Mic PGA Switch", "Mic PGA"}, 284 {"Boost Mixer", "Mic Volume", "MICP"}, 285 {"Boost Mixer", "Aux Volume", "Aux Input"}, 286 287 {"ADC", NULL, "Boost Mixer"}, 288 }; 289 290 static int wm8940_add_widgets(struct snd_soc_codec *codec) 291 { 292 int ret; 293 294 ret = snd_soc_dapm_new_controls(codec, wm8940_dapm_widgets, 295 ARRAY_SIZE(wm8940_dapm_widgets)); 296 if (ret) 297 goto error_ret; 298 ret = snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map)); 299 if (ret) 300 goto error_ret; 301 302 error_ret: 303 return ret; 304 } 305 306 #define wm8940_reset(c) snd_soc_write(c, WM8940_SOFTRESET, 0); 307 308 static int wm8940_set_dai_fmt(struct snd_soc_dai *codec_dai, 309 unsigned int fmt) 310 { 311 struct snd_soc_codec *codec = codec_dai->codec; 312 u16 iface = snd_soc_read(codec, WM8940_IFACE) & 0xFE67; 313 u16 clk = snd_soc_read(codec, WM8940_CLOCK) & 0x1fe; 314 315 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 316 case SND_SOC_DAIFMT_CBM_CFM: 317 clk |= 1; 318 break; 319 case SND_SOC_DAIFMT_CBS_CFS: 320 break; 321 default: 322 return -EINVAL; 323 } 324 snd_soc_write(codec, WM8940_CLOCK, clk); 325 326 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 327 case SND_SOC_DAIFMT_I2S: 328 iface |= (2 << 3); 329 break; 330 case SND_SOC_DAIFMT_LEFT_J: 331 iface |= (1 << 3); 332 break; 333 case SND_SOC_DAIFMT_RIGHT_J: 334 break; 335 case SND_SOC_DAIFMT_DSP_A: 336 iface |= (3 << 3); 337 break; 338 case SND_SOC_DAIFMT_DSP_B: 339 iface |= (3 << 3) | (1 << 7); 340 break; 341 } 342 343 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 344 case SND_SOC_DAIFMT_NB_NF: 345 break; 346 case SND_SOC_DAIFMT_NB_IF: 347 iface |= (1 << 7); 348 break; 349 case SND_SOC_DAIFMT_IB_NF: 350 iface |= (1 << 8); 351 break; 352 case SND_SOC_DAIFMT_IB_IF: 353 iface |= (1 << 8) | (1 << 7); 354 break; 355 } 356 357 snd_soc_write(codec, WM8940_IFACE, iface); 358 359 return 0; 360 } 361 362 static int wm8940_i2s_hw_params(struct snd_pcm_substream *substream, 363 struct snd_pcm_hw_params *params, 364 struct snd_soc_dai *dai) 365 { 366 struct snd_soc_pcm_runtime *rtd = substream->private_data; 367 struct snd_soc_device *socdev = rtd->socdev; 368 struct snd_soc_codec *codec = socdev->card->codec; 369 u16 iface = snd_soc_read(codec, WM8940_IFACE) & 0xFD9F; 370 u16 addcntrl = snd_soc_read(codec, WM8940_ADDCNTRL) & 0xFFF1; 371 u16 companding = snd_soc_read(codec, 372 WM8940_COMPANDINGCTL) & 0xFFDF; 373 int ret; 374 375 /* LoutR control */ 376 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE 377 && params_channels(params) == 2) 378 iface |= (1 << 9); 379 380 switch (params_rate(params)) { 381 case 8000: 382 addcntrl |= (0x5 << 1); 383 break; 384 case 11025: 385 addcntrl |= (0x4 << 1); 386 break; 387 case 16000: 388 addcntrl |= (0x3 << 1); 389 break; 390 case 22050: 391 addcntrl |= (0x2 << 1); 392 break; 393 case 32000: 394 addcntrl |= (0x1 << 1); 395 break; 396 case 44100: 397 case 48000: 398 break; 399 } 400 ret = snd_soc_write(codec, WM8940_ADDCNTRL, addcntrl); 401 if (ret) 402 goto error_ret; 403 404 switch (params_format(params)) { 405 case SNDRV_PCM_FORMAT_S8: 406 companding = companding | (1 << 5); 407 break; 408 case SNDRV_PCM_FORMAT_S16_LE: 409 break; 410 case SNDRV_PCM_FORMAT_S20_3LE: 411 iface |= (1 << 5); 412 break; 413 case SNDRV_PCM_FORMAT_S24_LE: 414 iface |= (2 << 5); 415 break; 416 case SNDRV_PCM_FORMAT_S32_LE: 417 iface |= (3 << 5); 418 break; 419 } 420 ret = snd_soc_write(codec, WM8940_COMPANDINGCTL, companding); 421 if (ret) 422 goto error_ret; 423 ret = snd_soc_write(codec, WM8940_IFACE, iface); 424 425 error_ret: 426 return ret; 427 } 428 429 static int wm8940_mute(struct snd_soc_dai *dai, int mute) 430 { 431 struct snd_soc_codec *codec = dai->codec; 432 u16 mute_reg = snd_soc_read(codec, WM8940_DAC) & 0xffbf; 433 434 if (mute) 435 mute_reg |= 0x40; 436 437 return snd_soc_write(codec, WM8940_DAC, mute_reg); 438 } 439 440 static int wm8940_set_bias_level(struct snd_soc_codec *codec, 441 enum snd_soc_bias_level level) 442 { 443 u16 val; 444 u16 pwr_reg = snd_soc_read(codec, WM8940_POWER1) & 0x1F0; 445 int ret = 0; 446 447 switch (level) { 448 case SND_SOC_BIAS_ON: 449 /* ensure bufioen and biasen */ 450 pwr_reg |= (1 << 2) | (1 << 3); 451 /* Enable thermal shutdown */ 452 val = snd_soc_read(codec, WM8940_OUTPUTCTL); 453 ret = snd_soc_write(codec, WM8940_OUTPUTCTL, val | 0x2); 454 if (ret) 455 break; 456 /* set vmid to 75k */ 457 ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x1); 458 break; 459 case SND_SOC_BIAS_PREPARE: 460 /* ensure bufioen and biasen */ 461 pwr_reg |= (1 << 2) | (1 << 3); 462 ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x1); 463 break; 464 case SND_SOC_BIAS_STANDBY: 465 /* ensure bufioen and biasen */ 466 pwr_reg |= (1 << 2) | (1 << 3); 467 /* set vmid to 300k for standby */ 468 ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x2); 469 break; 470 case SND_SOC_BIAS_OFF: 471 ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg); 472 break; 473 } 474 475 return ret; 476 } 477 478 struct pll_ { 479 unsigned int pre_scale:2; 480 unsigned int n:4; 481 unsigned int k; 482 }; 483 484 static struct pll_ pll_div; 485 486 /* The size in bits of the pll divide multiplied by 10 487 * to allow rounding later */ 488 #define FIXED_PLL_SIZE ((1 << 24) * 10) 489 static void pll_factors(unsigned int target, unsigned int source) 490 { 491 unsigned long long Kpart; 492 unsigned int K, Ndiv, Nmod; 493 /* The left shift ist to avoid accuracy loss when right shifting */ 494 Ndiv = target / source; 495 496 if (Ndiv > 12) { 497 source <<= 1; 498 /* Multiply by 2 */ 499 pll_div.pre_scale = 0; 500 Ndiv = target / source; 501 } else if (Ndiv < 3) { 502 source >>= 2; 503 /* Divide by 4 */ 504 pll_div.pre_scale = 3; 505 Ndiv = target / source; 506 } else if (Ndiv < 6) { 507 source >>= 1; 508 /* divide by 2 */ 509 pll_div.pre_scale = 2; 510 Ndiv = target / source; 511 } else 512 pll_div.pre_scale = 1; 513 514 if ((Ndiv < 6) || (Ndiv > 12)) 515 printk(KERN_WARNING 516 "WM8940 N value %d outwith recommended range!d\n", 517 Ndiv); 518 519 pll_div.n = Ndiv; 520 Nmod = target % source; 521 Kpart = FIXED_PLL_SIZE * (long long)Nmod; 522 523 do_div(Kpart, source); 524 525 K = Kpart & 0xFFFFFFFF; 526 527 /* Check if we need to round */ 528 if ((K % 10) >= 5) 529 K += 5; 530 531 /* Move down to proper range now rounding is done */ 532 K /= 10; 533 534 pll_div.k = K; 535 } 536 537 /* Untested at the moment */ 538 static int wm8940_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id, 539 int source, unsigned int freq_in, unsigned int freq_out) 540 { 541 struct snd_soc_codec *codec = codec_dai->codec; 542 u16 reg; 543 544 /* Turn off PLL */ 545 reg = snd_soc_read(codec, WM8940_POWER1); 546 snd_soc_write(codec, WM8940_POWER1, reg & 0x1df); 547 548 if (freq_in == 0 || freq_out == 0) { 549 /* Clock CODEC directly from MCLK */ 550 reg = snd_soc_read(codec, WM8940_CLOCK); 551 snd_soc_write(codec, WM8940_CLOCK, reg & 0x0ff); 552 /* Pll power down */ 553 snd_soc_write(codec, WM8940_PLLN, (1 << 7)); 554 return 0; 555 } 556 557 /* Pll is followed by a frequency divide by 4 */ 558 pll_factors(freq_out*4, freq_in); 559 if (pll_div.k) 560 snd_soc_write(codec, WM8940_PLLN, 561 (pll_div.pre_scale << 4) | pll_div.n | (1 << 6)); 562 else /* No factional component */ 563 snd_soc_write(codec, WM8940_PLLN, 564 (pll_div.pre_scale << 4) | pll_div.n); 565 snd_soc_write(codec, WM8940_PLLK1, pll_div.k >> 18); 566 snd_soc_write(codec, WM8940_PLLK2, (pll_div.k >> 9) & 0x1ff); 567 snd_soc_write(codec, WM8940_PLLK3, pll_div.k & 0x1ff); 568 /* Enable the PLL */ 569 reg = snd_soc_read(codec, WM8940_POWER1); 570 snd_soc_write(codec, WM8940_POWER1, reg | 0x020); 571 572 /* Run CODEC from PLL instead of MCLK */ 573 reg = snd_soc_read(codec, WM8940_CLOCK); 574 snd_soc_write(codec, WM8940_CLOCK, reg | 0x100); 575 576 return 0; 577 } 578 579 static int wm8940_set_dai_sysclk(struct snd_soc_dai *codec_dai, 580 int clk_id, unsigned int freq, int dir) 581 { 582 struct snd_soc_codec *codec = codec_dai->codec; 583 struct wm8940_priv *wm8940 = snd_soc_codec_get_drvdata(codec); 584 585 switch (freq) { 586 case 11289600: 587 case 12000000: 588 case 12288000: 589 case 16934400: 590 case 18432000: 591 wm8940->sysclk = freq; 592 return 0; 593 } 594 return -EINVAL; 595 } 596 597 static int wm8940_set_dai_clkdiv(struct snd_soc_dai *codec_dai, 598 int div_id, int div) 599 { 600 struct snd_soc_codec *codec = codec_dai->codec; 601 u16 reg; 602 int ret = 0; 603 604 switch (div_id) { 605 case WM8940_BCLKDIV: 606 reg = snd_soc_read(codec, WM8940_CLOCK) & 0xFFEF3; 607 ret = snd_soc_write(codec, WM8940_CLOCK, reg | (div << 2)); 608 break; 609 case WM8940_MCLKDIV: 610 reg = snd_soc_read(codec, WM8940_CLOCK) & 0xFF1F; 611 ret = snd_soc_write(codec, WM8940_CLOCK, reg | (div << 5)); 612 break; 613 case WM8940_OPCLKDIV: 614 reg = snd_soc_read(codec, WM8940_ADDCNTRL) & 0xFFCF; 615 ret = snd_soc_write(codec, WM8940_ADDCNTRL, reg | (div << 4)); 616 break; 617 } 618 return ret; 619 } 620 621 #define WM8940_RATES SNDRV_PCM_RATE_8000_48000 622 623 #define WM8940_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ 624 SNDRV_PCM_FMTBIT_S16_LE | \ 625 SNDRV_PCM_FMTBIT_S20_3LE | \ 626 SNDRV_PCM_FMTBIT_S24_LE | \ 627 SNDRV_PCM_FMTBIT_S32_LE) 628 629 static struct snd_soc_dai_ops wm8940_dai_ops = { 630 .hw_params = wm8940_i2s_hw_params, 631 .set_sysclk = wm8940_set_dai_sysclk, 632 .digital_mute = wm8940_mute, 633 .set_fmt = wm8940_set_dai_fmt, 634 .set_clkdiv = wm8940_set_dai_clkdiv, 635 .set_pll = wm8940_set_dai_pll, 636 }; 637 638 struct snd_soc_dai wm8940_dai = { 639 .name = "WM8940", 640 .playback = { 641 .stream_name = "Playback", 642 .channels_min = 1, 643 .channels_max = 2, 644 .rates = WM8940_RATES, 645 .formats = WM8940_FORMATS, 646 }, 647 .capture = { 648 .stream_name = "Capture", 649 .channels_min = 1, 650 .channels_max = 2, 651 .rates = WM8940_RATES, 652 .formats = WM8940_FORMATS, 653 }, 654 .ops = &wm8940_dai_ops, 655 .symmetric_rates = 1, 656 }; 657 EXPORT_SYMBOL_GPL(wm8940_dai); 658 659 static int wm8940_suspend(struct platform_device *pdev, pm_message_t state) 660 { 661 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 662 struct snd_soc_codec *codec = socdev->card->codec; 663 664 return wm8940_set_bias_level(codec, SND_SOC_BIAS_OFF); 665 } 666 667 static int wm8940_resume(struct platform_device *pdev) 668 { 669 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 670 struct snd_soc_codec *codec = socdev->card->codec; 671 int i; 672 int ret; 673 u8 data[3]; 674 u16 *cache = codec->reg_cache; 675 676 /* Sync reg_cache with the hardware 677 * Could use auto incremented writes to speed this up 678 */ 679 for (i = 0; i < ARRAY_SIZE(wm8940_reg_defaults); i++) { 680 data[0] = i; 681 data[1] = (cache[i] & 0xFF00) >> 8; 682 data[2] = cache[i] & 0x00FF; 683 ret = codec->hw_write(codec->control_data, data, 3); 684 if (ret < 0) 685 goto error_ret; 686 else if (ret != 3) { 687 ret = -EIO; 688 goto error_ret; 689 } 690 } 691 ret = wm8940_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 692 if (ret) 693 goto error_ret; 694 ret = wm8940_set_bias_level(codec, codec->suspend_bias_level); 695 696 error_ret: 697 return ret; 698 } 699 700 static struct snd_soc_codec *wm8940_codec; 701 702 static int wm8940_probe(struct platform_device *pdev) 703 { 704 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 705 struct snd_soc_codec *codec; 706 707 int ret = 0; 708 709 if (wm8940_codec == NULL) { 710 dev_err(&pdev->dev, "Codec device not registered\n"); 711 return -ENODEV; 712 } 713 714 socdev->card->codec = wm8940_codec; 715 codec = wm8940_codec; 716 717 mutex_init(&codec->mutex); 718 /* register pcms */ 719 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); 720 if (ret < 0) { 721 dev_err(codec->dev, "failed to create pcms: %d\n", ret); 722 goto pcm_err; 723 } 724 725 ret = snd_soc_add_controls(codec, wm8940_snd_controls, 726 ARRAY_SIZE(wm8940_snd_controls)); 727 if (ret) 728 goto error_free_pcms; 729 ret = wm8940_add_widgets(codec); 730 if (ret) 731 goto error_free_pcms; 732 733 return ret; 734 735 error_free_pcms: 736 snd_soc_free_pcms(socdev); 737 snd_soc_dapm_free(socdev); 738 pcm_err: 739 return ret; 740 } 741 742 static int wm8940_remove(struct platform_device *pdev) 743 { 744 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 745 746 snd_soc_free_pcms(socdev); 747 snd_soc_dapm_free(socdev); 748 749 return 0; 750 } 751 752 struct snd_soc_codec_device soc_codec_dev_wm8940 = { 753 .probe = wm8940_probe, 754 .remove = wm8940_remove, 755 .suspend = wm8940_suspend, 756 .resume = wm8940_resume, 757 }; 758 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8940); 759 760 static int wm8940_register(struct wm8940_priv *wm8940, 761 enum snd_soc_control_type control) 762 { 763 struct wm8940_setup_data *pdata = wm8940->codec.dev->platform_data; 764 struct snd_soc_codec *codec = &wm8940->codec; 765 int ret; 766 u16 reg; 767 if (wm8940_codec) { 768 dev_err(codec->dev, "Another WM8940 is registered\n"); 769 return -EINVAL; 770 } 771 772 INIT_LIST_HEAD(&codec->dapm_widgets); 773 INIT_LIST_HEAD(&codec->dapm_paths); 774 775 snd_soc_codec_set_drvdata(codec, wm8940); 776 codec->name = "WM8940"; 777 codec->owner = THIS_MODULE; 778 codec->bias_level = SND_SOC_BIAS_OFF; 779 codec->set_bias_level = wm8940_set_bias_level; 780 codec->dai = &wm8940_dai; 781 codec->num_dai = 1; 782 codec->reg_cache_size = ARRAY_SIZE(wm8940_reg_defaults); 783 codec->reg_cache = &wm8940->reg_cache; 784 785 ret = snd_soc_codec_set_cache_io(codec, 8, 16, control); 786 if (ret < 0) { 787 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); 788 return ret; 789 } 790 791 memcpy(codec->reg_cache, wm8940_reg_defaults, 792 sizeof(wm8940_reg_defaults)); 793 794 ret = wm8940_reset(codec); 795 if (ret < 0) { 796 dev_err(codec->dev, "Failed to issue reset\n"); 797 return ret; 798 } 799 800 wm8940_dai.dev = codec->dev; 801 802 wm8940_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 803 804 ret = snd_soc_write(codec, WM8940_POWER1, 0x180); 805 if (ret < 0) 806 return ret; 807 808 if (!pdata) 809 dev_warn(codec->dev, "No platform data supplied\n"); 810 else { 811 reg = snd_soc_read(codec, WM8940_OUTPUTCTL); 812 ret = snd_soc_write(codec, WM8940_OUTPUTCTL, reg | pdata->vroi); 813 if (ret < 0) 814 return ret; 815 } 816 817 818 wm8940_codec = codec; 819 820 ret = snd_soc_register_codec(codec); 821 if (ret) { 822 dev_err(codec->dev, "Failed to register codec: %d\n", ret); 823 return ret; 824 } 825 826 ret = snd_soc_register_dai(&wm8940_dai); 827 if (ret) { 828 dev_err(codec->dev, "Failed to register DAI: %d\n", ret); 829 snd_soc_unregister_codec(codec); 830 return ret; 831 } 832 833 return 0; 834 } 835 836 static void wm8940_unregister(struct wm8940_priv *wm8940) 837 { 838 wm8940_set_bias_level(&wm8940->codec, SND_SOC_BIAS_OFF); 839 snd_soc_unregister_dai(&wm8940_dai); 840 snd_soc_unregister_codec(&wm8940->codec); 841 kfree(wm8940); 842 wm8940_codec = NULL; 843 } 844 845 static int wm8940_i2c_probe(struct i2c_client *i2c, 846 const struct i2c_device_id *id) 847 { 848 struct wm8940_priv *wm8940; 849 struct snd_soc_codec *codec; 850 851 wm8940 = kzalloc(sizeof *wm8940, GFP_KERNEL); 852 if (wm8940 == NULL) 853 return -ENOMEM; 854 855 codec = &wm8940->codec; 856 codec->hw_write = (hw_write_t)i2c_master_send; 857 i2c_set_clientdata(i2c, wm8940); 858 codec->control_data = i2c; 859 codec->dev = &i2c->dev; 860 861 return wm8940_register(wm8940, SND_SOC_I2C); 862 } 863 864 static int __devexit wm8940_i2c_remove(struct i2c_client *client) 865 { 866 struct wm8940_priv *wm8940 = i2c_get_clientdata(client); 867 868 wm8940_unregister(wm8940); 869 870 return 0; 871 } 872 873 static const struct i2c_device_id wm8940_i2c_id[] = { 874 { "wm8940", 0 }, 875 { } 876 }; 877 MODULE_DEVICE_TABLE(i2c, wm8940_i2c_id); 878 879 static struct i2c_driver wm8940_i2c_driver = { 880 .driver = { 881 .name = "WM8940 I2C Codec", 882 .owner = THIS_MODULE, 883 }, 884 .probe = wm8940_i2c_probe, 885 .remove = __devexit_p(wm8940_i2c_remove), 886 .id_table = wm8940_i2c_id, 887 }; 888 889 static int __init wm8940_modinit(void) 890 { 891 int ret; 892 893 ret = i2c_add_driver(&wm8940_i2c_driver); 894 if (ret) 895 printk(KERN_ERR "Failed to register WM8940 I2C driver: %d\n", 896 ret); 897 return ret; 898 } 899 module_init(wm8940_modinit); 900 901 static void __exit wm8940_exit(void) 902 { 903 i2c_del_driver(&wm8940_i2c_driver); 904 } 905 module_exit(wm8940_exit); 906 907 MODULE_DESCRIPTION("ASoC WM8940 driver"); 908 MODULE_AUTHOR("Jonathan Cameron"); 909 MODULE_LICENSE("GPL"); 910