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