Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 1 | /* |
| 2 | * wm8510.c -- WM8510 ALSA Soc Audio driver |
| 3 | * |
| 4 | * Copyright 2006 Wolfson Microelectronics PLC. |
| 5 | * |
| 6 | * Author: Liam Girdwood <liam.girdwood@wolfsonmicro.com> |
| 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 <sound/core.h> |
| 22 | #include <sound/pcm.h> |
| 23 | #include <sound/pcm_params.h> |
| 24 | #include <sound/soc.h> |
| 25 | #include <sound/soc-dapm.h> |
| 26 | #include <sound/initval.h> |
| 27 | |
| 28 | #include "wm8510.h" |
| 29 | |
| 30 | #define AUDIO_NAME "wm8510" |
| 31 | #define WM8510_VERSION "0.6" |
| 32 | |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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, |
Mark Brown | 05a076d | 2008-06-06 17:13:53 +0100 | [diff] [blame] | 55 | 0x0001, |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | /* |
| 59 | * read wm8510 register cache |
| 60 | */ |
| 61 | static inline unsigned int wm8510_read_reg_cache(struct snd_soc_codec *codec, |
| 62 | unsigned int reg) |
| 63 | { |
| 64 | u16 *cache = codec->reg_cache; |
| 65 | if (reg == WM8510_RESET) |
| 66 | return 0; |
| 67 | if (reg >= WM8510_CACHEREGNUM) |
| 68 | return -1; |
| 69 | return cache[reg]; |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * write wm8510 register cache |
| 74 | */ |
| 75 | static inline void wm8510_write_reg_cache(struct snd_soc_codec *codec, |
| 76 | u16 reg, unsigned int value) |
| 77 | { |
| 78 | u16 *cache = codec->reg_cache; |
| 79 | if (reg >= WM8510_CACHEREGNUM) |
| 80 | return; |
| 81 | cache[reg] = value; |
| 82 | } |
| 83 | |
| 84 | /* |
| 85 | * write to the WM8510 register space |
| 86 | */ |
| 87 | static int wm8510_write(struct snd_soc_codec *codec, unsigned int reg, |
| 88 | unsigned int value) |
| 89 | { |
| 90 | u8 data[2]; |
| 91 | |
| 92 | /* data is |
| 93 | * D15..D9 WM8510 register offset |
| 94 | * D8...D0 register data |
| 95 | */ |
| 96 | data[0] = (reg << 1) | ((value >> 8) & 0x0001); |
| 97 | data[1] = value & 0x00ff; |
| 98 | |
| 99 | wm8510_write_reg_cache(codec, reg, value); |
| 100 | if (codec->hw_write(codec->control_data, data, 2) == 2) |
| 101 | return 0; |
| 102 | else |
| 103 | return -EIO; |
| 104 | } |
| 105 | |
| 106 | #define wm8510_reset(c) wm8510_write(c, WM8510_RESET, 0) |
| 107 | |
| 108 | static const char *wm8510_companding[] = { "Off", "NC", "u-law", "A-law" }; |
| 109 | static const char *wm8510_deemp[] = { "None", "32kHz", "44.1kHz", "48kHz" }; |
| 110 | static const char *wm8510_alc[] = { "ALC", "Limiter" }; |
| 111 | |
| 112 | static const struct soc_enum wm8510_enum[] = { |
| 113 | SOC_ENUM_SINGLE(WM8510_COMP, 1, 4, wm8510_companding), /* adc */ |
| 114 | SOC_ENUM_SINGLE(WM8510_COMP, 3, 4, wm8510_companding), /* dac */ |
| 115 | SOC_ENUM_SINGLE(WM8510_DAC, 4, 4, wm8510_deemp), |
| 116 | SOC_ENUM_SINGLE(WM8510_ALC3, 8, 2, wm8510_alc), |
| 117 | }; |
| 118 | |
| 119 | static const struct snd_kcontrol_new wm8510_snd_controls[] = { |
| 120 | |
| 121 | SOC_SINGLE("Digital Loopback Switch", WM8510_COMP, 0, 1, 0), |
| 122 | |
| 123 | SOC_ENUM("DAC Companding", wm8510_enum[1]), |
| 124 | SOC_ENUM("ADC Companding", wm8510_enum[0]), |
| 125 | |
| 126 | SOC_ENUM("Playback De-emphasis", wm8510_enum[2]), |
| 127 | SOC_SINGLE("DAC Inversion Switch", WM8510_DAC, 0, 1, 0), |
| 128 | |
| 129 | SOC_SINGLE("Master Playback Volume", WM8510_DACVOL, 0, 127, 0), |
| 130 | |
| 131 | SOC_SINGLE("High Pass Filter Switch", WM8510_ADC, 8, 1, 0), |
| 132 | SOC_SINGLE("High Pass Cut Off", WM8510_ADC, 4, 7, 0), |
| 133 | SOC_SINGLE("ADC Inversion Switch", WM8510_COMP, 0, 1, 0), |
| 134 | |
| 135 | SOC_SINGLE("Capture Volume", WM8510_ADCVOL, 0, 127, 0), |
| 136 | |
| 137 | SOC_SINGLE("DAC Playback Limiter Switch", WM8510_DACLIM1, 8, 1, 0), |
| 138 | SOC_SINGLE("DAC Playback Limiter Decay", WM8510_DACLIM1, 4, 15, 0), |
| 139 | SOC_SINGLE("DAC Playback Limiter Attack", WM8510_DACLIM1, 0, 15, 0), |
| 140 | |
| 141 | SOC_SINGLE("DAC Playback Limiter Threshold", WM8510_DACLIM2, 4, 7, 0), |
| 142 | SOC_SINGLE("DAC Playback Limiter Boost", WM8510_DACLIM2, 0, 15, 0), |
| 143 | |
| 144 | SOC_SINGLE("ALC Enable Switch", WM8510_ALC1, 8, 1, 0), |
| 145 | SOC_SINGLE("ALC Capture Max Gain", WM8510_ALC1, 3, 7, 0), |
| 146 | SOC_SINGLE("ALC Capture Min Gain", WM8510_ALC1, 0, 7, 0), |
| 147 | |
| 148 | SOC_SINGLE("ALC Capture ZC Switch", WM8510_ALC2, 8, 1, 0), |
| 149 | SOC_SINGLE("ALC Capture Hold", WM8510_ALC2, 4, 7, 0), |
| 150 | SOC_SINGLE("ALC Capture Target", WM8510_ALC2, 0, 15, 0), |
| 151 | |
| 152 | SOC_ENUM("ALC Capture Mode", wm8510_enum[3]), |
| 153 | SOC_SINGLE("ALC Capture Decay", WM8510_ALC3, 4, 15, 0), |
| 154 | SOC_SINGLE("ALC Capture Attack", WM8510_ALC3, 0, 15, 0), |
| 155 | |
| 156 | SOC_SINGLE("ALC Capture Noise Gate Switch", WM8510_NGATE, 3, 1, 0), |
| 157 | SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8510_NGATE, 0, 7, 0), |
| 158 | |
| 159 | SOC_SINGLE("Capture PGA ZC Switch", WM8510_INPPGA, 7, 1, 0), |
| 160 | SOC_SINGLE("Capture PGA Volume", WM8510_INPPGA, 0, 63, 0), |
| 161 | |
| 162 | SOC_SINGLE("Speaker Playback ZC Switch", WM8510_SPKVOL, 7, 1, 0), |
| 163 | SOC_SINGLE("Speaker Playback Switch", WM8510_SPKVOL, 6, 1, 1), |
| 164 | SOC_SINGLE("Speaker Playback Volume", WM8510_SPKVOL, 0, 63, 0), |
| 165 | SOC_SINGLE("Speaker Boost", WM8510_OUTPUT, 2, 1, 0), |
| 166 | |
| 167 | SOC_SINGLE("Capture Boost(+20dB)", WM8510_ADCBOOST, 8, 1, 0), |
| 168 | SOC_SINGLE("Mono Playback Switch", WM8510_MONOMIX, 6, 1, 1), |
| 169 | }; |
| 170 | |
| 171 | /* add non dapm controls */ |
| 172 | static int wm8510_add_controls(struct snd_soc_codec *codec) |
| 173 | { |
| 174 | int err, i; |
| 175 | |
| 176 | for (i = 0; i < ARRAY_SIZE(wm8510_snd_controls); i++) { |
| 177 | err = snd_ctl_add(codec->card, |
| 178 | snd_soc_cnew(&wm8510_snd_controls[i], codec, |
| 179 | NULL)); |
| 180 | if (err < 0) |
| 181 | return err; |
| 182 | } |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /* Speaker Output Mixer */ |
| 188 | static const struct snd_kcontrol_new wm8510_speaker_mixer_controls[] = { |
| 189 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_SPKMIX, 1, 1, 0), |
| 190 | SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_SPKMIX, 5, 1, 0), |
| 191 | SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_SPKMIX, 0, 1, 0), |
| 192 | }; |
| 193 | |
| 194 | /* Mono Output Mixer */ |
| 195 | static const struct snd_kcontrol_new wm8510_mono_mixer_controls[] = { |
| 196 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8510_MONOMIX, 1, 1, 0), |
| 197 | SOC_DAPM_SINGLE("Aux Playback Switch", WM8510_MONOMIX, 2, 1, 0), |
| 198 | SOC_DAPM_SINGLE("PCM Playback Switch", WM8510_MONOMIX, 0, 1, 0), |
| 199 | }; |
| 200 | |
| 201 | static const struct snd_kcontrol_new wm8510_boost_controls[] = { |
| 202 | SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA, 6, 1, 0), |
| 203 | SOC_DAPM_SINGLE("Aux Volume", WM8510_ADCBOOST, 0, 7, 0), |
| 204 | SOC_DAPM_SINGLE("Mic Volume", WM8510_ADCBOOST, 4, 7, 0), |
| 205 | }; |
| 206 | |
| 207 | static const struct snd_kcontrol_new wm8510_micpga_controls[] = { |
| 208 | SOC_DAPM_SINGLE("MICP Switch", WM8510_INPUT, 0, 1, 0), |
| 209 | SOC_DAPM_SINGLE("MICN Switch", WM8510_INPUT, 1, 1, 0), |
| 210 | SOC_DAPM_SINGLE("AUX Switch", WM8510_INPUT, 2, 1, 0), |
| 211 | }; |
| 212 | |
| 213 | static const struct snd_soc_dapm_widget wm8510_dapm_widgets[] = { |
| 214 | SND_SOC_DAPM_MIXER("Speaker Mixer", WM8510_POWER3, 2, 0, |
| 215 | &wm8510_speaker_mixer_controls[0], |
| 216 | ARRAY_SIZE(wm8510_speaker_mixer_controls)), |
| 217 | SND_SOC_DAPM_MIXER("Mono Mixer", WM8510_POWER3, 3, 0, |
| 218 | &wm8510_mono_mixer_controls[0], |
| 219 | ARRAY_SIZE(wm8510_mono_mixer_controls)), |
| 220 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8510_POWER3, 0, 0), |
| 221 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8510_POWER2, 0, 0), |
| 222 | SND_SOC_DAPM_PGA("Aux Input", WM8510_POWER1, 6, 0, NULL, 0), |
| 223 | SND_SOC_DAPM_PGA("SpkN Out", WM8510_POWER3, 5, 0, NULL, 0), |
| 224 | SND_SOC_DAPM_PGA("SpkP Out", WM8510_POWER3, 6, 0, NULL, 0), |
| 225 | SND_SOC_DAPM_PGA("Mono Out", WM8510_POWER3, 7, 0, NULL, 0), |
| 226 | |
| 227 | SND_SOC_DAPM_PGA("Mic PGA", WM8510_POWER2, 2, 0, |
| 228 | &wm8510_micpga_controls[0], |
| 229 | ARRAY_SIZE(wm8510_micpga_controls)), |
| 230 | SND_SOC_DAPM_MIXER("Boost Mixer", WM8510_POWER2, 4, 0, |
| 231 | &wm8510_boost_controls[0], |
| 232 | ARRAY_SIZE(wm8510_boost_controls)), |
| 233 | |
| 234 | SND_SOC_DAPM_MICBIAS("Mic Bias", WM8510_POWER1, 4, 0), |
| 235 | |
| 236 | SND_SOC_DAPM_INPUT("MICN"), |
| 237 | SND_SOC_DAPM_INPUT("MICP"), |
| 238 | SND_SOC_DAPM_INPUT("AUX"), |
| 239 | SND_SOC_DAPM_OUTPUT("MONOOUT"), |
| 240 | SND_SOC_DAPM_OUTPUT("SPKOUTP"), |
| 241 | SND_SOC_DAPM_OUTPUT("SPKOUTN"), |
| 242 | }; |
| 243 | |
| 244 | static const struct snd_soc_dapm_route audio_map[] = { |
| 245 | /* Mono output mixer */ |
| 246 | {"Mono Mixer", "PCM Playback Switch", "DAC"}, |
| 247 | {"Mono Mixer", "Aux Playback Switch", "Aux Input"}, |
| 248 | {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"}, |
| 249 | |
| 250 | /* Speaker output mixer */ |
| 251 | {"Speaker Mixer", "PCM Playback Switch", "DAC"}, |
| 252 | {"Speaker Mixer", "Aux Playback Switch", "Aux Input"}, |
| 253 | {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"}, |
| 254 | |
| 255 | /* Outputs */ |
| 256 | {"Mono Out", NULL, "Mono Mixer"}, |
| 257 | {"MONOOUT", NULL, "Mono Out"}, |
| 258 | {"SpkN Out", NULL, "Speaker Mixer"}, |
| 259 | {"SpkP Out", NULL, "Speaker Mixer"}, |
| 260 | {"SPKOUTN", NULL, "SpkN Out"}, |
| 261 | {"SPKOUTP", NULL, "SpkP Out"}, |
| 262 | |
| 263 | /* Microphone PGA */ |
| 264 | {"Mic PGA", "MICN Switch", "MICN"}, |
| 265 | {"Mic PGA", "MICP Switch", "MICP"}, |
| 266 | { "Mic PGA", "AUX Switch", "Aux Input" }, |
| 267 | |
| 268 | /* Boost Mixer */ |
| 269 | {"Boost Mixer", "Mic PGA Switch", "Mic PGA"}, |
| 270 | {"Boost Mixer", "Mic Volume", "MICP"}, |
| 271 | {"Boost Mixer", "Aux Volume", "Aux Input"}, |
| 272 | |
| 273 | {"ADC", NULL, "Boost Mixer"}, |
| 274 | }; |
| 275 | |
| 276 | static int wm8510_add_widgets(struct snd_soc_codec *codec) |
| 277 | { |
| 278 | snd_soc_dapm_new_controls(codec, wm8510_dapm_widgets, |
| 279 | ARRAY_SIZE(wm8510_dapm_widgets)); |
| 280 | |
| 281 | snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map)); |
| 282 | |
| 283 | snd_soc_dapm_new_widgets(codec); |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | struct pll_ { |
| 288 | unsigned int pre_div:4; /* prescale - 1 */ |
| 289 | unsigned int n:4; |
| 290 | unsigned int k; |
| 291 | }; |
| 292 | |
| 293 | static struct pll_ pll_div; |
| 294 | |
| 295 | /* The size in bits of the pll divide multiplied by 10 |
| 296 | * to allow rounding later */ |
| 297 | #define FIXED_PLL_SIZE ((1 << 24) * 10) |
| 298 | |
| 299 | static void pll_factors(unsigned int target, unsigned int source) |
| 300 | { |
| 301 | unsigned long long Kpart; |
| 302 | unsigned int K, Ndiv, Nmod; |
| 303 | |
| 304 | Ndiv = target / source; |
| 305 | if (Ndiv < 6) { |
| 306 | source >>= 1; |
| 307 | pll_div.pre_div = 1; |
| 308 | Ndiv = target / source; |
| 309 | } else |
| 310 | pll_div.pre_div = 0; |
| 311 | |
| 312 | if ((Ndiv < 6) || (Ndiv > 12)) |
| 313 | printk(KERN_WARNING |
| 314 | "WM8510 N value %d outwith recommended range!d\n", |
| 315 | Ndiv); |
| 316 | |
| 317 | pll_div.n = Ndiv; |
| 318 | Nmod = target % source; |
| 319 | Kpart = FIXED_PLL_SIZE * (long long)Nmod; |
| 320 | |
| 321 | do_div(Kpart, source); |
| 322 | |
| 323 | K = Kpart & 0xFFFFFFFF; |
| 324 | |
| 325 | /* Check if we need to round */ |
| 326 | if ((K % 10) >= 5) |
| 327 | K += 5; |
| 328 | |
| 329 | /* Move down to proper range now rounding is done */ |
| 330 | K /= 10; |
| 331 | |
| 332 | pll_div.k = K; |
| 333 | } |
| 334 | |
| 335 | static int wm8510_set_dai_pll(struct snd_soc_codec_dai *codec_dai, |
| 336 | int pll_id, unsigned int freq_in, unsigned int freq_out) |
| 337 | { |
| 338 | struct snd_soc_codec *codec = codec_dai->codec; |
| 339 | u16 reg; |
| 340 | |
| 341 | if (freq_in == 0 || freq_out == 0) { |
| 342 | /* Clock CODEC directly from MCLK */ |
| 343 | reg = wm8510_read_reg_cache(codec, WM8510_CLOCK); |
| 344 | wm8510_write(codec, WM8510_CLOCK, reg & 0x0ff); |
| 345 | |
| 346 | /* Turn off PLL */ |
| 347 | reg = wm8510_read_reg_cache(codec, WM8510_POWER1); |
| 348 | wm8510_write(codec, WM8510_POWER1, reg & 0x1df); |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | pll_factors(freq_out*8, freq_in); |
| 353 | |
| 354 | wm8510_write(codec, WM8510_PLLN, (pll_div.pre_div << 4) | pll_div.n); |
| 355 | wm8510_write(codec, WM8510_PLLK1, pll_div.k >> 18); |
| 356 | wm8510_write(codec, WM8510_PLLK2, (pll_div.k >> 9) & 0x1ff); |
| 357 | wm8510_write(codec, WM8510_PLLK3, pll_div.k & 0x1ff); |
| 358 | reg = wm8510_read_reg_cache(codec, WM8510_POWER1); |
| 359 | wm8510_write(codec, WM8510_POWER1, reg | 0x020); |
| 360 | |
| 361 | /* Run CODEC from PLL instead of MCLK */ |
| 362 | reg = wm8510_read_reg_cache(codec, WM8510_CLOCK); |
| 363 | wm8510_write(codec, WM8510_CLOCK, reg | 0x100); |
| 364 | |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | /* |
| 369 | * Configure WM8510 clock dividers. |
| 370 | */ |
| 371 | static int wm8510_set_dai_clkdiv(struct snd_soc_codec_dai *codec_dai, |
| 372 | int div_id, int div) |
| 373 | { |
| 374 | struct snd_soc_codec *codec = codec_dai->codec; |
| 375 | u16 reg; |
| 376 | |
| 377 | switch (div_id) { |
| 378 | case WM8510_OPCLKDIV: |
| 379 | reg = wm8510_read_reg_cache(codec, WM8510_GPIO) & 0x1cf; |
| 380 | wm8510_write(codec, WM8510_GPIO, reg | div); |
| 381 | break; |
| 382 | case WM8510_MCLKDIV: |
| 383 | reg = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x1f; |
| 384 | wm8510_write(codec, WM8510_CLOCK, reg | div); |
| 385 | break; |
| 386 | case WM8510_ADCCLK: |
| 387 | reg = wm8510_read_reg_cache(codec, WM8510_ADC) & 0x1f7; |
| 388 | wm8510_write(codec, WM8510_ADC, reg | div); |
| 389 | break; |
| 390 | case WM8510_DACCLK: |
| 391 | reg = wm8510_read_reg_cache(codec, WM8510_DAC) & 0x1f7; |
| 392 | wm8510_write(codec, WM8510_DAC, reg | div); |
| 393 | break; |
| 394 | case WM8510_BCLKDIV: |
| 395 | reg = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x1e3; |
| 396 | wm8510_write(codec, WM8510_CLOCK, reg | div); |
| 397 | break; |
| 398 | default: |
| 399 | return -EINVAL; |
| 400 | } |
| 401 | |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static int wm8510_set_dai_fmt(struct snd_soc_codec_dai *codec_dai, |
| 406 | unsigned int fmt) |
| 407 | { |
| 408 | struct snd_soc_codec *codec = codec_dai->codec; |
| 409 | u16 iface = 0; |
| 410 | u16 clk = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x1fe; |
| 411 | |
| 412 | /* set master/slave audio interface */ |
| 413 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 414 | case SND_SOC_DAIFMT_CBM_CFM: |
| 415 | clk |= 0x0001; |
| 416 | break; |
| 417 | case SND_SOC_DAIFMT_CBS_CFS: |
| 418 | break; |
| 419 | default: |
| 420 | return -EINVAL; |
| 421 | } |
| 422 | |
| 423 | /* interface format */ |
| 424 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 425 | case SND_SOC_DAIFMT_I2S: |
| 426 | iface |= 0x0010; |
| 427 | break; |
| 428 | case SND_SOC_DAIFMT_RIGHT_J: |
| 429 | break; |
| 430 | case SND_SOC_DAIFMT_LEFT_J: |
| 431 | iface |= 0x0008; |
| 432 | break; |
| 433 | case SND_SOC_DAIFMT_DSP_A: |
| 434 | iface |= 0x00018; |
| 435 | break; |
| 436 | default: |
| 437 | return -EINVAL; |
| 438 | } |
| 439 | |
| 440 | /* clock inversion */ |
| 441 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 442 | case SND_SOC_DAIFMT_NB_NF: |
| 443 | break; |
| 444 | case SND_SOC_DAIFMT_IB_IF: |
| 445 | iface |= 0x0180; |
| 446 | break; |
| 447 | case SND_SOC_DAIFMT_IB_NF: |
| 448 | iface |= 0x0100; |
| 449 | break; |
| 450 | case SND_SOC_DAIFMT_NB_IF: |
| 451 | iface |= 0x0080; |
| 452 | break; |
| 453 | default: |
| 454 | return -EINVAL; |
| 455 | } |
| 456 | |
| 457 | wm8510_write(codec, WM8510_IFACE, iface); |
| 458 | wm8510_write(codec, WM8510_CLOCK, clk); |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static int wm8510_pcm_hw_params(struct snd_pcm_substream *substream, |
| 463 | struct snd_pcm_hw_params *params) |
| 464 | { |
| 465 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 466 | struct snd_soc_device *socdev = rtd->socdev; |
| 467 | struct snd_soc_codec *codec = socdev->codec; |
| 468 | u16 iface = wm8510_read_reg_cache(codec, WM8510_IFACE) & 0x19f; |
| 469 | u16 adn = wm8510_read_reg_cache(codec, WM8510_ADD) & 0x1f1; |
| 470 | |
| 471 | /* bit size */ |
| 472 | switch (params_format(params)) { |
| 473 | case SNDRV_PCM_FORMAT_S16_LE: |
| 474 | break; |
| 475 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 476 | iface |= 0x0020; |
| 477 | break; |
| 478 | case SNDRV_PCM_FORMAT_S24_LE: |
| 479 | iface |= 0x0040; |
| 480 | break; |
| 481 | case SNDRV_PCM_FORMAT_S32_LE: |
| 482 | iface |= 0x0060; |
| 483 | break; |
| 484 | } |
| 485 | |
| 486 | /* filter coefficient */ |
| 487 | switch (params_rate(params)) { |
| 488 | case SNDRV_PCM_RATE_8000: |
| 489 | adn |= 0x5 << 1; |
| 490 | break; |
| 491 | case SNDRV_PCM_RATE_11025: |
| 492 | adn |= 0x4 << 1; |
| 493 | break; |
| 494 | case SNDRV_PCM_RATE_16000: |
| 495 | adn |= 0x3 << 1; |
| 496 | break; |
| 497 | case SNDRV_PCM_RATE_22050: |
| 498 | adn |= 0x2 << 1; |
| 499 | break; |
| 500 | case SNDRV_PCM_RATE_32000: |
| 501 | adn |= 0x1 << 1; |
| 502 | break; |
| 503 | case SNDRV_PCM_RATE_44100: |
| 504 | case SNDRV_PCM_RATE_48000: |
| 505 | break; |
| 506 | } |
| 507 | |
| 508 | wm8510_write(codec, WM8510_IFACE, iface); |
| 509 | wm8510_write(codec, WM8510_ADD, adn); |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static int wm8510_mute(struct snd_soc_codec_dai *dai, int mute) |
| 514 | { |
| 515 | struct snd_soc_codec *codec = dai->codec; |
| 516 | u16 mute_reg = wm8510_read_reg_cache(codec, WM8510_DAC) & 0xffbf; |
| 517 | |
| 518 | if (mute) |
| 519 | wm8510_write(codec, WM8510_DAC, mute_reg | 0x40); |
| 520 | else |
| 521 | wm8510_write(codec, WM8510_DAC, mute_reg); |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | /* liam need to make this lower power with dapm */ |
| 526 | static int wm8510_set_bias_level(struct snd_soc_codec *codec, |
| 527 | enum snd_soc_bias_level level) |
| 528 | { |
| 529 | |
| 530 | switch (level) { |
| 531 | case SND_SOC_BIAS_ON: |
| 532 | wm8510_write(codec, WM8510_POWER1, 0x1ff); |
| 533 | wm8510_write(codec, WM8510_POWER2, 0x1ff); |
| 534 | wm8510_write(codec, WM8510_POWER3, 0x1ff); |
| 535 | break; |
| 536 | case SND_SOC_BIAS_PREPARE: |
| 537 | case SND_SOC_BIAS_STANDBY: |
| 538 | break; |
| 539 | case SND_SOC_BIAS_OFF: |
| 540 | /* everything off, dac mute, inactive */ |
| 541 | wm8510_write(codec, WM8510_POWER1, 0x0); |
| 542 | wm8510_write(codec, WM8510_POWER2, 0x0); |
| 543 | wm8510_write(codec, WM8510_POWER3, 0x0); |
| 544 | break; |
| 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 | struct snd_soc_codec_dai wm8510_dai = { |
| 558 | .name = "WM8510 HiFi", |
| 559 | .playback = { |
| 560 | .stream_name = "Playback", |
| 561 | .channels_min = 2, |
| 562 | .channels_max = 2, |
| 563 | .rates = WM8510_RATES, |
| 564 | .formats = WM8510_FORMATS,}, |
| 565 | .capture = { |
| 566 | .stream_name = "Capture", |
| 567 | .channels_min = 2, |
| 568 | .channels_max = 2, |
| 569 | .rates = WM8510_RATES, |
| 570 | .formats = WM8510_FORMATS,}, |
| 571 | .ops = { |
| 572 | .hw_params = wm8510_pcm_hw_params, |
| 573 | }, |
| 574 | .dai_ops = { |
| 575 | .digital_mute = wm8510_mute, |
| 576 | .set_fmt = wm8510_set_dai_fmt, |
| 577 | .set_clkdiv = wm8510_set_dai_clkdiv, |
| 578 | .set_pll = wm8510_set_dai_pll, |
| 579 | }, |
| 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->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->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->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 | wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 644 | wm8510_add_controls(codec); |
| 645 | wm8510_add_widgets(codec); |
| 646 | ret = snd_soc_register_card(socdev); |
| 647 | if (ret < 0) { |
| 648 | printk(KERN_ERR "wm8510: failed to register card\n"); |
| 649 | goto card_err; |
| 650 | } |
| 651 | return ret; |
| 652 | |
| 653 | card_err: |
| 654 | snd_soc_free_pcms(socdev); |
| 655 | snd_soc_dapm_free(socdev); |
| 656 | pcm_err: |
| 657 | kfree(codec->reg_cache); |
| 658 | return ret; |
| 659 | } |
| 660 | |
| 661 | static struct snd_soc_device *wm8510_socdev; |
| 662 | |
| 663 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 664 | |
| 665 | /* |
| 666 | * WM8510 2 wire address is 0x1a |
| 667 | */ |
| 668 | #define I2C_DRIVERID_WM8510 0xfefe /* liam - need a proper id */ |
| 669 | |
| 670 | static unsigned short normal_i2c[] = { 0, I2C_CLIENT_END }; |
| 671 | |
| 672 | /* Magic definition of all other variables and things */ |
| 673 | I2C_CLIENT_INSMOD; |
| 674 | |
| 675 | static struct i2c_driver wm8510_i2c_driver; |
| 676 | static struct i2c_client client_template; |
| 677 | |
| 678 | /* If the i2c layer weren't so broken, we could pass this kind of data |
| 679 | around */ |
| 680 | |
| 681 | static int wm8510_codec_probe(struct i2c_adapter *adap, int addr, int kind) |
| 682 | { |
| 683 | struct snd_soc_device *socdev = wm8510_socdev; |
| 684 | struct wm8510_setup_data *setup = socdev->codec_data; |
| 685 | struct snd_soc_codec *codec = socdev->codec; |
| 686 | struct i2c_client *i2c; |
| 687 | int ret; |
| 688 | |
| 689 | if (addr != setup->i2c_address) |
| 690 | return -ENODEV; |
| 691 | |
| 692 | client_template.adapter = adap; |
| 693 | client_template.addr = addr; |
| 694 | |
| 695 | i2c = kmemdup(&client_template, sizeof(client_template), GFP_KERNEL); |
| 696 | if (i2c == NULL) { |
| 697 | kfree(codec); |
| 698 | return -ENOMEM; |
| 699 | } |
| 700 | i2c_set_clientdata(i2c, codec); |
| 701 | codec->control_data = i2c; |
| 702 | |
| 703 | ret = i2c_attach_client(i2c); |
| 704 | if (ret < 0) { |
Mark Brown | a5c95e9 | 2008-06-23 14:51:29 +0100 | [diff] [blame^] | 705 | pr_err("failed to attach codec at addr %x\n", addr); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 706 | goto err; |
| 707 | } |
| 708 | |
| 709 | ret = wm8510_init(socdev); |
| 710 | if (ret < 0) { |
Mark Brown | a5c95e9 | 2008-06-23 14:51:29 +0100 | [diff] [blame^] | 711 | pr_err("failed to initialise WM8510\n"); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 712 | goto err; |
| 713 | } |
| 714 | return ret; |
| 715 | |
| 716 | err: |
| 717 | kfree(codec); |
| 718 | kfree(i2c); |
| 719 | return ret; |
| 720 | } |
| 721 | |
| 722 | static int wm8510_i2c_detach(struct i2c_client *client) |
| 723 | { |
| 724 | struct snd_soc_codec *codec = i2c_get_clientdata(client); |
| 725 | i2c_detach_client(client); |
| 726 | kfree(codec->reg_cache); |
| 727 | kfree(client); |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | static int wm8510_i2c_attach(struct i2c_adapter *adap) |
| 732 | { |
| 733 | return i2c_probe(adap, &addr_data, wm8510_codec_probe); |
| 734 | } |
| 735 | |
| 736 | /* corgi i2c codec control layer */ |
| 737 | static struct i2c_driver wm8510_i2c_driver = { |
| 738 | .driver = { |
| 739 | .name = "WM8510 I2C Codec", |
| 740 | .owner = THIS_MODULE, |
| 741 | }, |
| 742 | .id = I2C_DRIVERID_WM8510, |
| 743 | .attach_adapter = wm8510_i2c_attach, |
| 744 | .detach_client = wm8510_i2c_detach, |
| 745 | .command = NULL, |
| 746 | }; |
| 747 | |
| 748 | static struct i2c_client client_template = { |
| 749 | .name = "WM8510", |
| 750 | .driver = &wm8510_i2c_driver, |
| 751 | }; |
| 752 | #endif |
| 753 | |
| 754 | static int wm8510_probe(struct platform_device *pdev) |
| 755 | { |
| 756 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 757 | struct wm8510_setup_data *setup; |
| 758 | struct snd_soc_codec *codec; |
| 759 | int ret = 0; |
| 760 | |
Mark Brown | a5c95e9 | 2008-06-23 14:51:29 +0100 | [diff] [blame^] | 761 | pr_info("WM8510 Audio Codec %s", WM8510_VERSION); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 762 | |
| 763 | setup = socdev->codec_data; |
| 764 | codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); |
| 765 | if (codec == NULL) |
| 766 | return -ENOMEM; |
| 767 | |
| 768 | socdev->codec = codec; |
| 769 | mutex_init(&codec->mutex); |
| 770 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 771 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 772 | |
| 773 | wm8510_socdev = socdev; |
| 774 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 775 | if (setup->i2c_address) { |
| 776 | normal_i2c[0] = setup->i2c_address; |
| 777 | codec->hw_write = (hw_write_t)i2c_master_send; |
| 778 | ret = i2c_add_driver(&wm8510_i2c_driver); |
| 779 | if (ret != 0) |
| 780 | printk(KERN_ERR "can't add i2c driver"); |
| 781 | } |
| 782 | #else |
| 783 | /* Add other interfaces here */ |
| 784 | #endif |
| 785 | return ret; |
| 786 | } |
| 787 | |
| 788 | /* power down chip */ |
| 789 | static int wm8510_remove(struct platform_device *pdev) |
| 790 | { |
| 791 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 792 | struct snd_soc_codec *codec = socdev->codec; |
| 793 | |
| 794 | if (codec->control_data) |
| 795 | wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 796 | |
| 797 | snd_soc_free_pcms(socdev); |
| 798 | snd_soc_dapm_free(socdev); |
| 799 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 800 | i2c_del_driver(&wm8510_i2c_driver); |
| 801 | #endif |
| 802 | kfree(codec); |
| 803 | |
| 804 | return 0; |
| 805 | } |
| 806 | |
| 807 | struct snd_soc_codec_device soc_codec_dev_wm8510 = { |
| 808 | .probe = wm8510_probe, |
| 809 | .remove = wm8510_remove, |
| 810 | .suspend = wm8510_suspend, |
| 811 | .resume = wm8510_resume, |
| 812 | }; |
| 813 | EXPORT_SYMBOL_GPL(soc_codec_dev_wm8510); |
| 814 | |
| 815 | MODULE_DESCRIPTION("ASoC WM8510 driver"); |
| 816 | MODULE_AUTHOR("Liam Girdwood"); |
| 817 | MODULE_LICENSE("GPL"); |