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