1 /* 2 * wm8510.c -- WM8510 ALSA Soc Audio driver 3 * 4 * Copyright 2006 Wolfson Microelectronics PLC. 5 * 6 * Author: Liam Girdwood <lrg@slimlogic.co.uk> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 */ 12 13 #include <linux/module.h> 14 #include <linux/moduleparam.h> 15 #include <linux/kernel.h> 16 #include <linux/init.h> 17 #include <linux/delay.h> 18 #include <linux/pm.h> 19 #include <linux/i2c.h> 20 #include <linux/platform_device.h> 21 #include <linux/spi/spi.h> 22 #include <sound/core.h> 23 #include <sound/pcm.h> 24 #include <sound/pcm_params.h> 25 #include <sound/soc.h> 26 #include <sound/soc-dapm.h> 27 #include <sound/initval.h> 28 29 #include "wm8510.h" 30 31 #define WM8510_VERSION "0.6" 32 33 struct snd_soc_codec_device soc_codec_dev_wm8510; 34 35 /* 36 * wm8510 register cache 37 * We can't read the WM8510 register space when we are 38 * using 2 wire for device control, so we cache them instead. 39 */ 40 static const u16 wm8510_reg[WM8510_CACHEREGNUM] = { 41 0x0000, 0x0000, 0x0000, 0x0000, 42 0x0050, 0x0000, 0x0140, 0x0000, 43 0x0000, 0x0000, 0x0000, 0x00ff, 44 0x0000, 0x0000, 0x0100, 0x00ff, 45 0x0000, 0x0000, 0x012c, 0x002c, 46 0x002c, 0x002c, 0x002c, 0x0000, 47 0x0032, 0x0000, 0x0000, 0x0000, 48 0x0000, 0x0000, 0x0000, 0x0000, 49 0x0038, 0x000b, 0x0032, 0x0000, 50 0x0008, 0x000c, 0x0093, 0x00e9, 51 0x0000, 0x0000, 0x0000, 0x0000, 52 0x0003, 0x0010, 0x0000, 0x0000, 53 0x0000, 0x0002, 0x0001, 0x0000, 54 0x0000, 0x0000, 0x0039, 0x0000, 55 0x0001, 56 }; 57 58 #define WM8510_POWER1_BIASEN 0x08 59 #define WM8510_POWER1_BUFIOEN 0x10 60 61 /* 62 * read wm8510 register cache 63 */ 64 static inline unsigned int wm8510_read_reg_cache(struct snd_soc_codec *codec, 65 unsigned int reg) 66 { 67 u16 *cache = codec->reg_cache; 68 if (reg == WM8510_RESET) 69 return 0; 70 if (reg >= WM8510_CACHEREGNUM) 71 return -1; 72 return cache[reg]; 73 } 74 75 /* 76 * write wm8510 register cache 77 */ 78 static inline void wm8510_write_reg_cache(struct snd_soc_codec *codec, 79 u16 reg, unsigned int value) 80 { 81 u16 *cache = codec->reg_cache; 82 if (reg >= WM8510_CACHEREGNUM) 83 return; 84 cache[reg] = value; 85 } 86 87 /* 88 * write to the WM8510 register space 89 */ 90 static int wm8510_write(struct snd_soc_codec *codec, unsigned int reg, 91 unsigned int value) 92 { 93 u8 data[2]; 94 95 /* data is 96 * D15..D9 WM8510 register offset 97 * D8...D0 register data 98 */ 99 data[0] = (reg << 1) | ((value >> 8) & 0x0001); 100 data[1] = value & 0x00ff; 101 102 wm8510_write_reg_cache(codec, reg, value); 103 if (codec->hw_write(codec->control_data, data, 2) == 2) 104 return 0; 105 else 106 return -EIO; 107 } 108 109 #define wm8510_reset(c) wm8510_write(c, WM8510_RESET, 0) 110 111 static const char *wm8510_companding[] = { "Off", "NC", "u-law", "A-law" }; 112 static const char *wm8510_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" }; 113 static const char *wm8510_alc[] = { "ALC", "Limiter" }; 114 115 static const struct soc_enum wm8510_enum[] = { 116 SOC_ENUM_SINGLE(WM8510_COMP, 1, 4, wm8510_companding), /* adc */ 117 SOC_ENUM_SINGLE(WM8510_COMP, 3, 4, wm8510_companding), /* dac */ 118 SOC_ENUM_SINGLE(WM8510_DAC, 4, 4, wm8510_deemp), 119 SOC_ENUM_SINGLE(WM8510_ALC3, 8, 2, wm8510_alc), 120 }; 121 122 static const struct snd_kcontrol_new wm8510_snd_controls[] = { 123 124 SOC_SINGLE("Digital Loopback Switch", WM8510_COMP, 0, 1, 0), 125 126 SOC_ENUM("DAC Companding", wm8510_enum[1]), 127 SOC_ENUM("ADC Companding", wm8510_enum[0]), 128 129 SOC_ENUM("Playback De-emphasis", wm8510_enum[2]), 130 SOC_SINGLE("DAC Inversion Switch", WM8510_DAC, 0, 1, 0), 131 132 SOC_SINGLE("Master Playback Volume", WM8510_DACVOL, 0, 127, 0), 133 134 SOC_SINGLE("High Pass Filter Switch", WM8510_ADC, 8, 1, 0), 135 SOC_SINGLE("High Pass Cut Off", WM8510_ADC, 4, 7, 0), 136 SOC_SINGLE("ADC Inversion Switch", WM8510_COMP, 0, 1, 0), 137 138 SOC_SINGLE("Capture Volume", WM8510_ADCVOL, 0, 127, 0), 139 140 SOC_SINGLE("DAC Playback Limiter Switch", WM8510_DACLIM1, 8, 1, 0), 141 SOC_SINGLE("DAC Playback Limiter Decay", WM8510_DACLIM1, 4, 15, 0), 142 SOC_SINGLE("DAC Playback Limiter Attack", WM8510_DACLIM1, 0, 15, 0), 143 144 SOC_SINGLE("DAC Playback Limiter Threshold", WM8510_DACLIM2, 4, 7, 0), 145 SOC_SINGLE("DAC Playback Limiter Boost", WM8510_DACLIM2, 0, 15, 0), 146 147 SOC_SINGLE("ALC Enable Switch", WM8510_ALC1, 8, 1, 0), 148 SOC_SINGLE("ALC Capture Max Gain", WM8510_ALC1, 3, 7, 0), 149 SOC_SINGLE("ALC Capture Min Gain", WM8510_ALC1, 0, 7, 0), 150 151 SOC_SINGLE("ALC Capture ZC Switch", WM8510_ALC2, 8, 1, 0), 152 SOC_SINGLE("ALC Capture Hold", WM8510_ALC2, 4, 7, 0), 153 SOC_SINGLE("ALC Capture Target", WM8510_ALC2, 0, 15, 0), 154 155 SOC_ENUM("ALC Capture Mode", wm8510_enum[3]), 156 SOC_SINGLE("ALC Capture Decay", WM8510_ALC3, 4, 15, 0), 157 SOC_SINGLE("ALC Capture Attack", WM8510_ALC3, 0, 15, 0), 158 159 SOC_SINGLE("ALC Capture Noise Gate Switch", WM8510_NGATE, 3, 1, 0), 160 SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8510_NGATE, 0, 7, 0), 161 162 SOC_SINGLE("Capture PGA ZC Switch", WM8510_INPPGA, 7, 1, 0), 163 SOC_SINGLE("Capture PGA Volume", WM8510_INPPGA, 0, 63, 0), 164 165 SOC_SINGLE("Speaker Playback ZC Switch", WM8510_SPKVOL, 7, 1, 0), 166 SOC_SINGLE("Speaker Playback Switch", WM8510_SPKVOL, 6, 1, 1), 167 SOC_SINGLE("Speaker Playback Volume", WM8510_SPKVOL, 0, 63, 0), 168 SOC_SINGLE("Speaker Boost", WM8510_OUTPUT, 2, 1, 0), 169 170 SOC_SINGLE("Capture Boost(+20dB)", WM8510_ADCBOOST, 8, 1, 0), 171 SOC_SINGLE("Mono Playback Switch", WM8510_MONOMIX, 6, 1, 1), 172 }; 173 174 /* Speaker Output Mixer */ 175 static const struct snd_kcontrol_new wm8510_speaker_mixer_controls[] = { 176 SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_SPKMIX, 1, 1, 0), 177 SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_SPKMIX, 5, 1, 0), 178 SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_SPKMIX, 0, 1, 0), 179 }; 180 181 /* Mono Output Mixer */ 182 static const struct snd_kcontrol_new wm8510_mono_mixer_controls[] = { 183 SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_MONOMIX, 1, 1, 0), 184 SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_MONOMIX, 2, 1, 0), 185 SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_MONOMIX, 0, 1, 0), 186 }; 187 188 static const struct snd_kcontrol_new wm8510_boost_controls[] = { 189 SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA, 6, 1, 1), 190 SOC_DAPM_SINGLE("Aux Volume", WM8510_ADCBOOST, 0, 7, 0), 191 SOC_DAPM_SINGLE("Mic Volume", WM8510_ADCBOOST, 4, 7, 0), 192 }; 193 194 static const struct snd_kcontrol_new wm8510_micpga_controls[] = { 195 SOC_DAPM_SINGLE("MICP Switch", WM8510_INPUT, 0, 1, 0), 196 SOC_DAPM_SINGLE("MICN Switch", WM8510_INPUT, 1, 1, 0), 197 SOC_DAPM_SINGLE("AUX Switch", WM8510_INPUT, 2, 1, 0), 198 }; 199 200 static const struct snd_soc_dapm_widget wm8510_dapm_widgets[] = { 201 SND_SOC_DAPM_MIXER("Speaker Mixer", WM8510_POWER3, 2, 0, 202 &wm8510_speaker_mixer_controls[0], 203 ARRAY_SIZE(wm8510_speaker_mixer_controls)), 204 SND_SOC_DAPM_MIXER("Mono Mixer", WM8510_POWER3, 3, 0, 205 &wm8510_mono_mixer_controls[0], 206 ARRAY_SIZE(wm8510_mono_mixer_controls)), 207 SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8510_POWER3, 0, 0), 208 SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8510_POWER2, 0, 0), 209 SND_SOC_DAPM_PGA("Aux Input", WM8510_POWER1, 6, 0, NULL, 0), 210 SND_SOC_DAPM_PGA("SpkN Out", WM8510_POWER3, 5, 0, NULL, 0), 211 SND_SOC_DAPM_PGA("SpkP Out", WM8510_POWER3, 6, 0, NULL, 0), 212 SND_SOC_DAPM_PGA("Mono Out", WM8510_POWER3, 7, 0, NULL, 0), 213 214 SND_SOC_DAPM_MIXER("Mic PGA", WM8510_POWER2, 2, 0, 215 &wm8510_micpga_controls[0], 216 ARRAY_SIZE(wm8510_micpga_controls)), 217 SND_SOC_DAPM_MIXER("Boost Mixer", WM8510_POWER2, 4, 0, 218 &wm8510_boost_controls[0], 219 ARRAY_SIZE(wm8510_boost_controls)), 220 221 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8510_POWER1, 4, 0), 222 223 SND_SOC_DAPM_INPUT("MICN"), 224 SND_SOC_DAPM_INPUT("MICP"), 225 SND_SOC_DAPM_INPUT("AUX"), 226 SND_SOC_DAPM_OUTPUT("MONOOUT"), 227 SND_SOC_DAPM_OUTPUT("SPKOUTP"), 228 SND_SOC_DAPM_OUTPUT("SPKOUTN"), 229 }; 230 231 static const struct snd_soc_dapm_route audio_map[] = { 232 /* Mono output mixer */ 233 {"Mono Mixer", "PCM Playback Switch", "DAC"}, 234 {"Mono Mixer", "Aux Playback Switch", "Aux Input"}, 235 {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"}, 236 237 /* Speaker output mixer */ 238 {"Speaker Mixer", "PCM Playback Switch", "DAC"}, 239 {"Speaker Mixer", "Aux Playback Switch", "Aux Input"}, 240 {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"}, 241 242 /* Outputs */ 243 {"Mono Out", NULL, "Mono Mixer"}, 244 {"MONOOUT", NULL, "Mono Out"}, 245 {"SpkN Out", NULL, "Speaker Mixer"}, 246 {"SpkP Out", NULL, "Speaker Mixer"}, 247 {"SPKOUTN", NULL, "SpkN Out"}, 248 {"SPKOUTP", NULL, "SpkP Out"}, 249 250 /* Microphone PGA */ 251 {"Mic PGA", "MICN Switch", "MICN"}, 252 {"Mic PGA", "MICP Switch", "MICP"}, 253 { "Mic PGA", "AUX Switch", "Aux Input" }, 254 255 /* Boost Mixer */ 256 {"Boost Mixer", "Mic PGA Switch", "Mic PGA"}, 257 {"Boost Mixer", "Mic Volume", "MICP"}, 258 {"Boost Mixer", "Aux Volume", "Aux Input"}, 259 260 {"ADC", NULL, "Boost Mixer"}, 261 }; 262 263 static int wm8510_add_widgets(struct snd_soc_codec *codec) 264 { 265 snd_soc_dapm_new_controls(codec, wm8510_dapm_widgets, 266 ARRAY_SIZE(wm8510_dapm_widgets)); 267 268 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map)); 269 270 snd_soc_dapm_new_widgets(codec); 271 return 0; 272 } 273 274 struct pll_ { 275 unsigned int pre_div:4; /* prescale - 1 */ 276 unsigned int n:4; 277 unsigned int k; 278 }; 279 280 static struct pll_ pll_div; 281 282 /* The size in bits of the pll divide multiplied by 10 283 * to allow rounding later */ 284 #define FIXED_PLL_SIZE ((1 << 24) * 10) 285 286 static void pll_factors(unsigned int target, unsigned int source) 287 { 288 unsigned long long Kpart; 289 unsigned int K, Ndiv, Nmod; 290 291 Ndiv = target / source; 292 if (Ndiv < 6) { 293 source >>= 1; 294 pll_div.pre_div = 1; 295 Ndiv = target / source; 296 } else 297 pll_div.pre_div = 0; 298 299 if ((Ndiv < 6) || (Ndiv > 12)) 300 printk(KERN_WARNING 301 "WM8510 N value %d outwith recommended range!d\n", 302 Ndiv); 303 304 pll_div.n = Ndiv; 305 Nmod = target % source; 306 Kpart = FIXED_PLL_SIZE * (long long)Nmod; 307 308 do_div(Kpart, source); 309 310 K = Kpart & 0xFFFFFFFF; 311 312 /* Check if we need to round */ 313 if ((K % 10) >= 5) 314 K += 5; 315 316 /* Move down to proper range now rounding is done */ 317 K /= 10; 318 319 pll_div.k = K; 320 } 321 322 static int wm8510_set_dai_pll(struct snd_soc_dai *codec_dai, 323 int pll_id, unsigned int freq_in, unsigned int freq_out) 324 { 325 struct snd_soc_codec *codec = codec_dai->codec; 326 u16 reg; 327 328 if (freq_in == 0 || freq_out == 0) { 329 /* Clock CODEC directly from MCLK */ 330 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK); 331 wm8510_write(codec, WM8510_CLOCK, reg & 0x0ff); 332 333 /* Turn off PLL */ 334 reg = wm8510_read_reg_cache(codec, WM8510_POWER1); 335 wm8510_write(codec, WM8510_POWER1, reg & 0x1df); 336 return 0; 337 } 338 339 pll_factors(freq_out*4, freq_in); 340 341 wm8510_write(codec, WM8510_PLLN, (pll_div.pre_div << 4) | pll_div.n); 342 wm8510_write(codec, WM8510_PLLK1, pll_div.k >> 18); 343 wm8510_write(codec, WM8510_PLLK2, (pll_div.k >> 9) & 0x1ff); 344 wm8510_write(codec, WM8510_PLLK3, pll_div.k & 0x1ff); 345 reg = wm8510_read_reg_cache(codec, WM8510_POWER1); 346 wm8510_write(codec, WM8510_POWER1, reg | 0x020); 347 348 /* Run CODEC from PLL instead of MCLK */ 349 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK); 350 wm8510_write(codec, WM8510_CLOCK, reg | 0x100); 351 352 return 0; 353 } 354 355 /* 356 * Configure WM8510 clock dividers. 357 */ 358 static int wm8510_set_dai_clkdiv(struct snd_soc_dai *codec_dai, 359 int div_id, int div) 360 { 361 struct snd_soc_codec *codec = codec_dai->codec; 362 u16 reg; 363 364 switch (div_id) { 365 case WM8510_OPCLKDIV: 366 reg = wm8510_read_reg_cache(codec, WM8510_GPIO) & 0x1cf; 367 wm8510_write(codec, WM8510_GPIO, reg | div); 368 break; 369 case WM8510_MCLKDIV: 370 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x11f; 371 wm8510_write(codec, WM8510_CLOCK, reg | div); 372 break; 373 case WM8510_ADCCLK: 374 reg = wm8510_read_reg_cache(codec, WM8510_ADC) & 0x1f7; 375 wm8510_write(codec, WM8510_ADC, reg | div); 376 break; 377 case WM8510_DACCLK: 378 reg = wm8510_read_reg_cache(codec, WM8510_DAC) & 0x1f7; 379 wm8510_write(codec, WM8510_DAC, reg | div); 380 break; 381 case WM8510_BCLKDIV: 382 reg = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x1e3; 383 wm8510_write(codec, WM8510_CLOCK, reg | div); 384 break; 385 default: 386 return -EINVAL; 387 } 388 389 return 0; 390 } 391 392 static int wm8510_set_dai_fmt(struct snd_soc_dai *codec_dai, 393 unsigned int fmt) 394 { 395 struct snd_soc_codec *codec = codec_dai->codec; 396 u16 iface = 0; 397 u16 clk = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x1fe; 398 399 /* set master/slave audio interface */ 400 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { 401 case SND_SOC_DAIFMT_CBM_CFM: 402 clk |= 0x0001; 403 break; 404 case SND_SOC_DAIFMT_CBS_CFS: 405 break; 406 default: 407 return -EINVAL; 408 } 409 410 /* interface format */ 411 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { 412 case SND_SOC_DAIFMT_I2S: 413 iface |= 0x0010; 414 break; 415 case SND_SOC_DAIFMT_RIGHT_J: 416 break; 417 case SND_SOC_DAIFMT_LEFT_J: 418 iface |= 0x0008; 419 break; 420 case SND_SOC_DAIFMT_DSP_A: 421 iface |= 0x00018; 422 break; 423 default: 424 return -EINVAL; 425 } 426 427 /* clock inversion */ 428 switch (fmt & SND_SOC_DAIFMT_INV_MASK) { 429 case SND_SOC_DAIFMT_NB_NF: 430 break; 431 case SND_SOC_DAIFMT_IB_IF: 432 iface |= 0x0180; 433 break; 434 case SND_SOC_DAIFMT_IB_NF: 435 iface |= 0x0100; 436 break; 437 case SND_SOC_DAIFMT_NB_IF: 438 iface |= 0x0080; 439 break; 440 default: 441 return -EINVAL; 442 } 443 444 wm8510_write(codec, WM8510_IFACE, iface); 445 wm8510_write(codec, WM8510_CLOCK, clk); 446 return 0; 447 } 448 449 static int wm8510_pcm_hw_params(struct snd_pcm_substream *substream, 450 struct snd_pcm_hw_params *params, 451 struct snd_soc_dai *dai) 452 { 453 struct snd_soc_pcm_runtime *rtd = substream->private_data; 454 struct snd_soc_device *socdev = rtd->socdev; 455 struct snd_soc_codec *codec = socdev->card->codec; 456 u16 iface = wm8510_read_reg_cache(codec, WM8510_IFACE) & 0x19f; 457 u16 adn = wm8510_read_reg_cache(codec, WM8510_ADD) & 0x1f1; 458 459 /* bit size */ 460 switch (params_format(params)) { 461 case SNDRV_PCM_FORMAT_S16_LE: 462 break; 463 case SNDRV_PCM_FORMAT_S20_3LE: 464 iface |= 0x0020; 465 break; 466 case SNDRV_PCM_FORMAT_S24_LE: 467 iface |= 0x0040; 468 break; 469 case SNDRV_PCM_FORMAT_S32_LE: 470 iface |= 0x0060; 471 break; 472 } 473 474 /* filter coefficient */ 475 switch (params_rate(params)) { 476 case SNDRV_PCM_RATE_8000: 477 adn |= 0x5 << 1; 478 break; 479 case SNDRV_PCM_RATE_11025: 480 adn |= 0x4 << 1; 481 break; 482 case SNDRV_PCM_RATE_16000: 483 adn |= 0x3 << 1; 484 break; 485 case SNDRV_PCM_RATE_22050: 486 adn |= 0x2 << 1; 487 break; 488 case SNDRV_PCM_RATE_32000: 489 adn |= 0x1 << 1; 490 break; 491 case SNDRV_PCM_RATE_44100: 492 case SNDRV_PCM_RATE_48000: 493 break; 494 } 495 496 wm8510_write(codec, WM8510_IFACE, iface); 497 wm8510_write(codec, WM8510_ADD, adn); 498 return 0; 499 } 500 501 static int wm8510_mute(struct snd_soc_dai *dai, int mute) 502 { 503 struct snd_soc_codec *codec = dai->codec; 504 u16 mute_reg = wm8510_read_reg_cache(codec, WM8510_DAC) & 0xffbf; 505 506 if (mute) 507 wm8510_write(codec, WM8510_DAC, mute_reg | 0x40); 508 else 509 wm8510_write(codec, WM8510_DAC, mute_reg); 510 return 0; 511 } 512 513 /* liam need to make this lower power with dapm */ 514 static int wm8510_set_bias_level(struct snd_soc_codec *codec, 515 enum snd_soc_bias_level level) 516 { 517 u16 power1 = wm8510_read_reg_cache(codec, WM8510_POWER1) & ~0x3; 518 519 switch (level) { 520 case SND_SOC_BIAS_ON: 521 case SND_SOC_BIAS_PREPARE: 522 power1 |= 0x1; /* VMID 50k */ 523 wm8510_write(codec, WM8510_POWER1, power1); 524 break; 525 526 case SND_SOC_BIAS_STANDBY: 527 power1 |= WM8510_POWER1_BIASEN | WM8510_POWER1_BUFIOEN; 528 529 if (codec->bias_level == SND_SOC_BIAS_OFF) { 530 /* Initial cap charge at VMID 5k */ 531 wm8510_write(codec, WM8510_POWER1, power1 | 0x3); 532 mdelay(100); 533 } 534 535 power1 |= 0x2; /* VMID 500k */ 536 wm8510_write(codec, WM8510_POWER1, power1); 537 break; 538 539 case SND_SOC_BIAS_OFF: 540 wm8510_write(codec, WM8510_POWER1, 0); 541 wm8510_write(codec, WM8510_POWER2, 0); 542 wm8510_write(codec, WM8510_POWER3, 0); 543 break; 544 } 545 546 codec->bias_level = level; 547 return 0; 548 } 549 550 #define WM8510_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 |\ 551 SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 |\ 552 SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000) 553 554 #define WM8510_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ 555 SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE) 556 557 static struct snd_soc_dai_ops wm8510_dai_ops = { 558 .hw_params = wm8510_pcm_hw_params, 559 .digital_mute = wm8510_mute, 560 .set_fmt = wm8510_set_dai_fmt, 561 .set_clkdiv = wm8510_set_dai_clkdiv, 562 .set_pll = wm8510_set_dai_pll, 563 }; 564 565 struct snd_soc_dai wm8510_dai = { 566 .name = "WM8510 HiFi", 567 .playback = { 568 .stream_name = "Playback", 569 .channels_min = 2, 570 .channels_max = 2, 571 .rates = WM8510_RATES, 572 .formats = WM8510_FORMATS,}, 573 .capture = { 574 .stream_name = "Capture", 575 .channels_min = 2, 576 .channels_max = 2, 577 .rates = WM8510_RATES, 578 .formats = WM8510_FORMATS,}, 579 .ops = &wm8510_dai_ops, 580 }; 581 EXPORT_SYMBOL_GPL(wm8510_dai); 582 583 static int wm8510_suspend(struct platform_device *pdev, pm_message_t state) 584 { 585 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 586 struct snd_soc_codec *codec = socdev->card->codec; 587 588 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF); 589 return 0; 590 } 591 592 static int wm8510_resume(struct platform_device *pdev) 593 { 594 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 595 struct snd_soc_codec *codec = socdev->card->codec; 596 int i; 597 u8 data[2]; 598 u16 *cache = codec->reg_cache; 599 600 /* Sync reg_cache with the hardware */ 601 for (i = 0; i < ARRAY_SIZE(wm8510_reg); i++) { 602 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001); 603 data[1] = cache[i] & 0x00ff; 604 codec->hw_write(codec->control_data, data, 2); 605 } 606 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 607 wm8510_set_bias_level(codec, codec->suspend_bias_level); 608 return 0; 609 } 610 611 /* 612 * initialise the WM8510 driver 613 * register the mixer and dsp interfaces with the kernel 614 */ 615 static int wm8510_init(struct snd_soc_device *socdev) 616 { 617 struct snd_soc_codec *codec = socdev->card->codec; 618 int ret = 0; 619 620 codec->name = "WM8510"; 621 codec->owner = THIS_MODULE; 622 codec->read = wm8510_read_reg_cache; 623 codec->write = wm8510_write; 624 codec->set_bias_level = wm8510_set_bias_level; 625 codec->dai = &wm8510_dai; 626 codec->num_dai = 1; 627 codec->reg_cache_size = ARRAY_SIZE(wm8510_reg); 628 codec->reg_cache = kmemdup(wm8510_reg, sizeof(wm8510_reg), GFP_KERNEL); 629 630 if (codec->reg_cache == NULL) 631 return -ENOMEM; 632 633 wm8510_reset(codec); 634 635 /* register pcms */ 636 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); 637 if (ret < 0) { 638 printk(KERN_ERR "wm8510: failed to create pcms\n"); 639 goto pcm_err; 640 } 641 642 /* power on device */ 643 codec->bias_level = SND_SOC_BIAS_OFF; 644 wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY); 645 snd_soc_add_controls(codec, wm8510_snd_controls, 646 ARRAY_SIZE(wm8510_snd_controls)); 647 wm8510_add_widgets(codec); 648 ret = snd_soc_init_card(socdev); 649 if (ret < 0) { 650 printk(KERN_ERR "wm8510: failed to register card\n"); 651 goto card_err; 652 } 653 return ret; 654 655 card_err: 656 snd_soc_free_pcms(socdev); 657 snd_soc_dapm_free(socdev); 658 pcm_err: 659 kfree(codec->reg_cache); 660 return ret; 661 } 662 663 static struct snd_soc_device *wm8510_socdev; 664 665 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 666 667 /* 668 * WM8510 2 wire address is 0x1a 669 */ 670 671 static int wm8510_i2c_probe(struct i2c_client *i2c, 672 const struct i2c_device_id *id) 673 { 674 struct snd_soc_device *socdev = wm8510_socdev; 675 struct snd_soc_codec *codec = socdev->card->codec; 676 int ret; 677 678 i2c_set_clientdata(i2c, codec); 679 codec->control_data = i2c; 680 681 ret = wm8510_init(socdev); 682 if (ret < 0) 683 pr_err("failed to initialise WM8510\n"); 684 685 return ret; 686 } 687 688 static int wm8510_i2c_remove(struct i2c_client *client) 689 { 690 struct snd_soc_codec *codec = i2c_get_clientdata(client); 691 kfree(codec->reg_cache); 692 return 0; 693 } 694 695 static const struct i2c_device_id wm8510_i2c_id[] = { 696 { "wm8510", 0 }, 697 { } 698 }; 699 MODULE_DEVICE_TABLE(i2c, wm8510_i2c_id); 700 701 static struct i2c_driver wm8510_i2c_driver = { 702 .driver = { 703 .name = "WM8510 I2C Codec", 704 .owner = THIS_MODULE, 705 }, 706 .probe = wm8510_i2c_probe, 707 .remove = wm8510_i2c_remove, 708 .id_table = wm8510_i2c_id, 709 }; 710 711 static int wm8510_add_i2c_device(struct platform_device *pdev, 712 const struct wm8510_setup_data *setup) 713 { 714 struct i2c_board_info info; 715 struct i2c_adapter *adapter; 716 struct i2c_client *client; 717 int ret; 718 719 ret = i2c_add_driver(&wm8510_i2c_driver); 720 if (ret != 0) { 721 dev_err(&pdev->dev, "can't add i2c driver\n"); 722 return ret; 723 } 724 725 memset(&info, 0, sizeof(struct i2c_board_info)); 726 info.addr = setup->i2c_address; 727 strlcpy(info.type, "wm8510", I2C_NAME_SIZE); 728 729 adapter = i2c_get_adapter(setup->i2c_bus); 730 if (!adapter) { 731 dev_err(&pdev->dev, "can't get i2c adapter %d\n", 732 setup->i2c_bus); 733 goto err_driver; 734 } 735 736 client = i2c_new_device(adapter, &info); 737 i2c_put_adapter(adapter); 738 if (!client) { 739 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n", 740 (unsigned int)info.addr); 741 goto err_driver; 742 } 743 744 return 0; 745 746 err_driver: 747 i2c_del_driver(&wm8510_i2c_driver); 748 return -ENODEV; 749 } 750 #endif 751 752 #if defined(CONFIG_SPI_MASTER) 753 static int __devinit wm8510_spi_probe(struct spi_device *spi) 754 { 755 struct snd_soc_device *socdev = wm8510_socdev; 756 struct snd_soc_codec *codec = socdev->card->codec; 757 int ret; 758 759 codec->control_data = spi; 760 761 ret = wm8510_init(socdev); 762 if (ret < 0) 763 dev_err(&spi->dev, "failed to initialise WM8510\n"); 764 765 return ret; 766 } 767 768 static int __devexit wm8510_spi_remove(struct spi_device *spi) 769 { 770 return 0; 771 } 772 773 static struct spi_driver wm8510_spi_driver = { 774 .driver = { 775 .name = "wm8510", 776 .bus = &spi_bus_type, 777 .owner = THIS_MODULE, 778 }, 779 .probe = wm8510_spi_probe, 780 .remove = __devexit_p(wm8510_spi_remove), 781 }; 782 783 static int wm8510_spi_write(struct spi_device *spi, const char *data, int len) 784 { 785 struct spi_transfer t; 786 struct spi_message m; 787 u8 msg[2]; 788 789 if (len <= 0) 790 return 0; 791 792 msg[0] = data[0]; 793 msg[1] = data[1]; 794 795 spi_message_init(&m); 796 memset(&t, 0, (sizeof t)); 797 798 t.tx_buf = &msg[0]; 799 t.len = len; 800 801 spi_message_add_tail(&t, &m); 802 spi_sync(spi, &m); 803 804 return len; 805 } 806 #endif /* CONFIG_SPI_MASTER */ 807 808 static int wm8510_probe(struct platform_device *pdev) 809 { 810 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 811 struct wm8510_setup_data *setup; 812 struct snd_soc_codec *codec; 813 int ret = 0; 814 815 pr_info("WM8510 Audio Codec %s", WM8510_VERSION); 816 817 setup = socdev->codec_data; 818 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); 819 if (codec == NULL) 820 return -ENOMEM; 821 822 socdev->card->codec = codec; 823 mutex_init(&codec->mutex); 824 INIT_LIST_HEAD(&codec->dapm_widgets); 825 INIT_LIST_HEAD(&codec->dapm_paths); 826 827 wm8510_socdev = socdev; 828 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 829 if (setup->i2c_address) { 830 codec->hw_write = (hw_write_t)i2c_master_send; 831 ret = wm8510_add_i2c_device(pdev, setup); 832 } 833 #endif 834 #if defined(CONFIG_SPI_MASTER) 835 if (setup->spi) { 836 codec->hw_write = (hw_write_t)wm8510_spi_write; 837 ret = spi_register_driver(&wm8510_spi_driver); 838 if (ret != 0) 839 printk(KERN_ERR "can't add spi driver"); 840 } 841 #endif 842 843 if (ret != 0) 844 kfree(codec); 845 return ret; 846 } 847 848 /* power down chip */ 849 static int wm8510_remove(struct platform_device *pdev) 850 { 851 struct snd_soc_device *socdev = platform_get_drvdata(pdev); 852 struct snd_soc_codec *codec = socdev->card->codec; 853 854 if (codec->control_data) 855 wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF); 856 857 snd_soc_free_pcms(socdev); 858 snd_soc_dapm_free(socdev); 859 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) 860 i2c_unregister_device(codec->control_data); 861 i2c_del_driver(&wm8510_i2c_driver); 862 #endif 863 #if defined(CONFIG_SPI_MASTER) 864 spi_unregister_driver(&wm8510_spi_driver); 865 #endif 866 kfree(codec); 867 868 return 0; 869 } 870 871 struct snd_soc_codec_device soc_codec_dev_wm8510 = { 872 .probe = wm8510_probe, 873 .remove = wm8510_remove, 874 .suspend = wm8510_suspend, 875 .resume = wm8510_resume, 876 }; 877 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8510); 878 879 static int __init wm8510_modinit(void) 880 { 881 return snd_soc_register_dai(&wm8510_dai); 882 } 883 module_init(wm8510_modinit); 884 885 static void __exit wm8510_exit(void) 886 { 887 snd_soc_unregister_dai(&wm8510_dai); 888 } 889 module_exit(wm8510_exit); 890 891 MODULE_DESCRIPTION("ASoC WM8510 driver"); 892 MODULE_AUTHOR("Liam Girdwood"); 893 MODULE_LICENSE("GPL"); 894