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 | * |
Liam Girdwood | d331124 | 2008-10-12 13:17:36 +0100 | [diff] [blame] | 6 | * Author: Liam Girdwood <lrg@slimlogic.co.uk> |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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> |
Mark Brown | 5e35795 | 2008-10-07 11:56:20 +0100 | [diff] [blame] | 21 | #include <linux/spi/spi.h> |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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 | |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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 | |
Mark Brown | 09af98b | 2008-10-07 13:04:58 +0100 | [diff] [blame] | 58 | #define WM8510_POWER1_BIASEN 0x08 |
| 59 | #define WM8510_POWER1_BUFIOEN 0x10 |
| 60 | |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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 | |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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[] = { |
Mark Brown | 8ae23ec | 2008-10-06 11:33:21 +0100 | [diff] [blame] | 189 | SOC_DAPM_SINGLE("Mic PGA Switch", WM8510_INPPGA, 6, 1, 1), |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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 | |
Mark Brown | 2b5f34c | 2008-10-07 16:13:50 +0100 | [diff] [blame] | 214 | SND_SOC_DAPM_MIXER("Mic PGA", WM8510_POWER2, 2, 0, |
| 215 | &wm8510_micpga_controls[0], |
| 216 | ARRAY_SIZE(wm8510_micpga_controls)), |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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 |
Roel Kluin | 449bd54 | 2009-05-27 17:08:39 -0700 | [diff] [blame] | 301 | "WM8510 N value %u outwith recommended range!d\n", |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 322 | static int wm8510_set_dai_pll(struct snd_soc_dai *codec_dai, |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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 | |
Jonas Andersson | 86027ae | 2009-03-04 08:24:26 +0100 | [diff] [blame] | 339 | pll_factors(freq_out*4, freq_in); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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 | */ |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 358 | static int wm8510_set_dai_clkdiv(struct snd_soc_dai *codec_dai, |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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: |
Jonas Andersson | 86027ae | 2009-03-04 08:24:26 +0100 | [diff] [blame] | 370 | reg = wm8510_read_reg_cache(codec, WM8510_CLOCK) & 0x11f; |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 392 | static int wm8510_set_dai_fmt(struct snd_soc_dai *codec_dai, |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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, |
Mark Brown | dee89c4 | 2008-11-18 22:11:38 +0000 | [diff] [blame] | 450 | struct snd_pcm_hw_params *params, |
| 451 | struct snd_soc_dai *dai) |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 452 | { |
| 453 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 454 | struct snd_soc_device *socdev = rtd->socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 455 | struct snd_soc_codec *codec = socdev->card->codec; |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 501 | static int wm8510_mute(struct snd_soc_dai *dai, int mute) |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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 | { |
Mark Brown | 09af98b | 2008-10-07 13:04:58 +0100 | [diff] [blame] | 517 | u16 power1 = wm8510_read_reg_cache(codec, WM8510_POWER1) & ~0x3; |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 518 | |
| 519 | switch (level) { |
| 520 | case SND_SOC_BIAS_ON: |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 521 | case SND_SOC_BIAS_PREPARE: |
Mark Brown | 09af98b | 2008-10-07 13:04:58 +0100 | [diff] [blame] | 522 | power1 |= 0x1; /* VMID 50k */ |
| 523 | wm8510_write(codec, WM8510_POWER1, power1); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 524 | break; |
Mark Brown | 09af98b | 2008-10-07 13:04:58 +0100 | [diff] [blame] | 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 | |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 539 | case SND_SOC_BIAS_OFF: |
Mark Brown | 09af98b | 2008-10-07 13:04:58 +0100 | [diff] [blame] | 540 | wm8510_write(codec, WM8510_POWER1, 0); |
| 541 | wm8510_write(codec, WM8510_POWER2, 0); |
| 542 | wm8510_write(codec, WM8510_POWER3, 0); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 543 | break; |
| 544 | } |
Mark Brown | 09af98b | 2008-10-07 13:04:58 +0100 | [diff] [blame] | 545 | |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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 | |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 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 | |
Liam Girdwood | e550e17 | 2008-07-07 16:07:52 +0100 | [diff] [blame] | 565 | struct snd_soc_dai wm8510_dai = { |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 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,}, |
Eric Miao | 6335d05 | 2009-03-03 09:41:00 +0800 | [diff] [blame] | 579 | .ops = &wm8510_dai_ops, |
Mark Brown | cc369cf | 2009-07-09 11:28:07 +0100 | [diff] [blame^] | 580 | .symmetric_rates = 1, |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 581 | }; |
| 582 | EXPORT_SYMBOL_GPL(wm8510_dai); |
| 583 | |
| 584 | static int wm8510_suspend(struct platform_device *pdev, pm_message_t state) |
| 585 | { |
| 586 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 587 | struct snd_soc_codec *codec = socdev->card->codec; |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 588 | |
| 589 | wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 590 | return 0; |
| 591 | } |
| 592 | |
| 593 | static int wm8510_resume(struct platform_device *pdev) |
| 594 | { |
| 595 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 596 | struct snd_soc_codec *codec = socdev->card->codec; |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 597 | int i; |
| 598 | u8 data[2]; |
| 599 | u16 *cache = codec->reg_cache; |
| 600 | |
| 601 | /* Sync reg_cache with the hardware */ |
| 602 | for (i = 0; i < ARRAY_SIZE(wm8510_reg); i++) { |
| 603 | data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001); |
| 604 | data[1] = cache[i] & 0x00ff; |
| 605 | codec->hw_write(codec->control_data, data, 2); |
| 606 | } |
| 607 | wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 608 | wm8510_set_bias_level(codec, codec->suspend_bias_level); |
| 609 | return 0; |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * initialise the WM8510 driver |
| 614 | * register the mixer and dsp interfaces with the kernel |
| 615 | */ |
| 616 | static int wm8510_init(struct snd_soc_device *socdev) |
| 617 | { |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 618 | struct snd_soc_codec *codec = socdev->card->codec; |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 619 | int ret = 0; |
| 620 | |
| 621 | codec->name = "WM8510"; |
| 622 | codec->owner = THIS_MODULE; |
| 623 | codec->read = wm8510_read_reg_cache; |
| 624 | codec->write = wm8510_write; |
| 625 | codec->set_bias_level = wm8510_set_bias_level; |
| 626 | codec->dai = &wm8510_dai; |
| 627 | codec->num_dai = 1; |
| 628 | codec->reg_cache_size = ARRAY_SIZE(wm8510_reg); |
| 629 | codec->reg_cache = kmemdup(wm8510_reg, sizeof(wm8510_reg), GFP_KERNEL); |
| 630 | |
| 631 | if (codec->reg_cache == NULL) |
| 632 | return -ENOMEM; |
| 633 | |
| 634 | wm8510_reset(codec); |
| 635 | |
| 636 | /* register pcms */ |
| 637 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); |
| 638 | if (ret < 0) { |
| 639 | printk(KERN_ERR "wm8510: failed to create pcms\n"); |
| 640 | goto pcm_err; |
| 641 | } |
| 642 | |
| 643 | /* power on device */ |
Mark Brown | 09af98b | 2008-10-07 13:04:58 +0100 | [diff] [blame] | 644 | codec->bias_level = SND_SOC_BIAS_OFF; |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 645 | wm8510_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
Ian Molton | 3e8e195 | 2009-01-09 00:23:21 +0000 | [diff] [blame] | 646 | snd_soc_add_controls(codec, wm8510_snd_controls, |
| 647 | ARRAY_SIZE(wm8510_snd_controls)); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 648 | wm8510_add_widgets(codec); |
Mark Brown | 968a602 | 2008-11-28 11:49:07 +0000 | [diff] [blame] | 649 | ret = snd_soc_init_card(socdev); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 650 | if (ret < 0) { |
| 651 | printk(KERN_ERR "wm8510: failed to register card\n"); |
| 652 | goto card_err; |
| 653 | } |
| 654 | return ret; |
| 655 | |
| 656 | card_err: |
| 657 | snd_soc_free_pcms(socdev); |
| 658 | snd_soc_dapm_free(socdev); |
| 659 | pcm_err: |
| 660 | kfree(codec->reg_cache); |
| 661 | return ret; |
| 662 | } |
| 663 | |
| 664 | static struct snd_soc_device *wm8510_socdev; |
| 665 | |
| 666 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 667 | |
| 668 | /* |
| 669 | * WM8510 2 wire address is 0x1a |
| 670 | */ |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 671 | |
Jean Delvare | 41759c2 | 2008-09-02 17:07:30 +0200 | [diff] [blame] | 672 | static int wm8510_i2c_probe(struct i2c_client *i2c, |
| 673 | const struct i2c_device_id *id) |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 674 | { |
| 675 | struct snd_soc_device *socdev = wm8510_socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 676 | struct snd_soc_codec *codec = socdev->card->codec; |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 677 | int ret; |
| 678 | |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 679 | i2c_set_clientdata(i2c, codec); |
| 680 | codec->control_data = i2c; |
| 681 | |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 682 | ret = wm8510_init(socdev); |
Jean Delvare | 41759c2 | 2008-09-02 17:07:30 +0200 | [diff] [blame] | 683 | if (ret < 0) |
Mark Brown | a5c95e9 | 2008-06-23 14:51:29 +0100 | [diff] [blame] | 684 | pr_err("failed to initialise WM8510\n"); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 685 | |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 686 | return ret; |
| 687 | } |
| 688 | |
Jean Delvare | 41759c2 | 2008-09-02 17:07:30 +0200 | [diff] [blame] | 689 | static int wm8510_i2c_remove(struct i2c_client *client) |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 690 | { |
| 691 | struct snd_soc_codec *codec = i2c_get_clientdata(client); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 692 | kfree(codec->reg_cache); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 693 | return 0; |
| 694 | } |
| 695 | |
Jean Delvare | 41759c2 | 2008-09-02 17:07:30 +0200 | [diff] [blame] | 696 | static const struct i2c_device_id wm8510_i2c_id[] = { |
| 697 | { "wm8510", 0 }, |
| 698 | { } |
| 699 | }; |
| 700 | MODULE_DEVICE_TABLE(i2c, wm8510_i2c_id); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 701 | |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 702 | static struct i2c_driver wm8510_i2c_driver = { |
| 703 | .driver = { |
| 704 | .name = "WM8510 I2C Codec", |
| 705 | .owner = THIS_MODULE, |
| 706 | }, |
Jean Delvare | 41759c2 | 2008-09-02 17:07:30 +0200 | [diff] [blame] | 707 | .probe = wm8510_i2c_probe, |
| 708 | .remove = wm8510_i2c_remove, |
| 709 | .id_table = wm8510_i2c_id, |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 710 | }; |
| 711 | |
Jean Delvare | 41759c2 | 2008-09-02 17:07:30 +0200 | [diff] [blame] | 712 | static int wm8510_add_i2c_device(struct platform_device *pdev, |
| 713 | const struct wm8510_setup_data *setup) |
| 714 | { |
| 715 | struct i2c_board_info info; |
| 716 | struct i2c_adapter *adapter; |
| 717 | struct i2c_client *client; |
| 718 | int ret; |
| 719 | |
| 720 | ret = i2c_add_driver(&wm8510_i2c_driver); |
| 721 | if (ret != 0) { |
| 722 | dev_err(&pdev->dev, "can't add i2c driver\n"); |
| 723 | return ret; |
| 724 | } |
| 725 | |
| 726 | memset(&info, 0, sizeof(struct i2c_board_info)); |
| 727 | info.addr = setup->i2c_address; |
| 728 | strlcpy(info.type, "wm8510", I2C_NAME_SIZE); |
| 729 | |
| 730 | adapter = i2c_get_adapter(setup->i2c_bus); |
| 731 | if (!adapter) { |
| 732 | dev_err(&pdev->dev, "can't get i2c adapter %d\n", |
| 733 | setup->i2c_bus); |
| 734 | goto err_driver; |
| 735 | } |
| 736 | |
| 737 | client = i2c_new_device(adapter, &info); |
| 738 | i2c_put_adapter(adapter); |
| 739 | if (!client) { |
| 740 | dev_err(&pdev->dev, "can't add i2c device at 0x%x\n", |
| 741 | (unsigned int)info.addr); |
| 742 | goto err_driver; |
| 743 | } |
| 744 | |
| 745 | return 0; |
| 746 | |
| 747 | err_driver: |
| 748 | i2c_del_driver(&wm8510_i2c_driver); |
| 749 | return -ENODEV; |
| 750 | } |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 751 | #endif |
| 752 | |
Mark Brown | 5e35795 | 2008-10-07 11:56:20 +0100 | [diff] [blame] | 753 | #if defined(CONFIG_SPI_MASTER) |
| 754 | static int __devinit wm8510_spi_probe(struct spi_device *spi) |
| 755 | { |
| 756 | struct snd_soc_device *socdev = wm8510_socdev; |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 757 | struct snd_soc_codec *codec = socdev->card->codec; |
Mark Brown | 5e35795 | 2008-10-07 11:56:20 +0100 | [diff] [blame] | 758 | int ret; |
| 759 | |
| 760 | codec->control_data = spi; |
| 761 | |
| 762 | ret = wm8510_init(socdev); |
| 763 | if (ret < 0) |
| 764 | dev_err(&spi->dev, "failed to initialise WM8510\n"); |
| 765 | |
| 766 | return ret; |
| 767 | } |
| 768 | |
| 769 | static int __devexit wm8510_spi_remove(struct spi_device *spi) |
| 770 | { |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | static struct spi_driver wm8510_spi_driver = { |
| 775 | .driver = { |
| 776 | .name = "wm8510", |
| 777 | .bus = &spi_bus_type, |
| 778 | .owner = THIS_MODULE, |
| 779 | }, |
| 780 | .probe = wm8510_spi_probe, |
| 781 | .remove = __devexit_p(wm8510_spi_remove), |
| 782 | }; |
| 783 | |
| 784 | static int wm8510_spi_write(struct spi_device *spi, const char *data, int len) |
| 785 | { |
| 786 | struct spi_transfer t; |
| 787 | struct spi_message m; |
| 788 | u8 msg[2]; |
| 789 | |
| 790 | if (len <= 0) |
| 791 | return 0; |
| 792 | |
| 793 | msg[0] = data[0]; |
| 794 | msg[1] = data[1]; |
| 795 | |
| 796 | spi_message_init(&m); |
| 797 | memset(&t, 0, (sizeof t)); |
| 798 | |
| 799 | t.tx_buf = &msg[0]; |
| 800 | t.len = len; |
| 801 | |
| 802 | spi_message_add_tail(&t, &m); |
| 803 | spi_sync(spi, &m); |
| 804 | |
| 805 | return len; |
| 806 | } |
| 807 | #endif /* CONFIG_SPI_MASTER */ |
| 808 | |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 809 | static int wm8510_probe(struct platform_device *pdev) |
| 810 | { |
| 811 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 812 | struct wm8510_setup_data *setup; |
| 813 | struct snd_soc_codec *codec; |
| 814 | int ret = 0; |
| 815 | |
Mark Brown | a5c95e9 | 2008-06-23 14:51:29 +0100 | [diff] [blame] | 816 | pr_info("WM8510 Audio Codec %s", WM8510_VERSION); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 817 | |
| 818 | setup = socdev->codec_data; |
| 819 | codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL); |
| 820 | if (codec == NULL) |
| 821 | return -ENOMEM; |
| 822 | |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 823 | socdev->card->codec = codec; |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 824 | mutex_init(&codec->mutex); |
| 825 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 826 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 827 | |
| 828 | wm8510_socdev = socdev; |
| 829 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 830 | if (setup->i2c_address) { |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 831 | codec->hw_write = (hw_write_t)i2c_master_send; |
Jean Delvare | 41759c2 | 2008-09-02 17:07:30 +0200 | [diff] [blame] | 832 | ret = wm8510_add_i2c_device(pdev, setup); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 833 | } |
Mark Brown | 5e35795 | 2008-10-07 11:56:20 +0100 | [diff] [blame] | 834 | #endif |
| 835 | #if defined(CONFIG_SPI_MASTER) |
| 836 | if (setup->spi) { |
| 837 | codec->hw_write = (hw_write_t)wm8510_spi_write; |
| 838 | ret = spi_register_driver(&wm8510_spi_driver); |
| 839 | if (ret != 0) |
| 840 | printk(KERN_ERR "can't add spi driver"); |
| 841 | } |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 842 | #endif |
Jean Delvare | 3051e41 | 2008-08-25 11:49:20 +0100 | [diff] [blame] | 843 | |
| 844 | if (ret != 0) |
| 845 | kfree(codec); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 846 | return ret; |
| 847 | } |
| 848 | |
| 849 | /* power down chip */ |
| 850 | static int wm8510_remove(struct platform_device *pdev) |
| 851 | { |
| 852 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 6627a65 | 2009-01-23 22:55:23 +0000 | [diff] [blame] | 853 | struct snd_soc_codec *codec = socdev->card->codec; |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 854 | |
| 855 | if (codec->control_data) |
| 856 | wm8510_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 857 | |
| 858 | snd_soc_free_pcms(socdev); |
| 859 | snd_soc_dapm_free(socdev); |
| 860 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Jean Delvare | 41759c2 | 2008-09-02 17:07:30 +0200 | [diff] [blame] | 861 | i2c_unregister_device(codec->control_data); |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 862 | i2c_del_driver(&wm8510_i2c_driver); |
| 863 | #endif |
Mark Brown | 5e35795 | 2008-10-07 11:56:20 +0100 | [diff] [blame] | 864 | #if defined(CONFIG_SPI_MASTER) |
| 865 | spi_unregister_driver(&wm8510_spi_driver); |
| 866 | #endif |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 867 | kfree(codec); |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | struct snd_soc_codec_device soc_codec_dev_wm8510 = { |
| 873 | .probe = wm8510_probe, |
| 874 | .remove = wm8510_remove, |
| 875 | .suspend = wm8510_suspend, |
| 876 | .resume = wm8510_resume, |
| 877 | }; |
| 878 | EXPORT_SYMBOL_GPL(soc_codec_dev_wm8510); |
| 879 | |
Takashi Iwai | c9b3a40 | 2008-12-10 07:47:22 +0100 | [diff] [blame] | 880 | static int __init wm8510_modinit(void) |
Mark Brown | 64089b8 | 2008-12-08 19:17:58 +0000 | [diff] [blame] | 881 | { |
| 882 | return snd_soc_register_dai(&wm8510_dai); |
| 883 | } |
| 884 | module_init(wm8510_modinit); |
| 885 | |
| 886 | static void __exit wm8510_exit(void) |
| 887 | { |
| 888 | snd_soc_unregister_dai(&wm8510_dai); |
| 889 | } |
| 890 | module_exit(wm8510_exit); |
| 891 | |
Mark Brown | 5d42151 | 2008-06-05 13:49:32 +0100 | [diff] [blame] | 892 | MODULE_DESCRIPTION("ASoC WM8510 driver"); |
| 893 | MODULE_AUTHOR("Liam Girdwood"); |
| 894 | MODULE_LICENSE("GPL"); |