Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * wm8940.c -- WM8940 ALSA Soc Audio driver |
| 3 | * |
| 4 | * Author: Jonathan Cameron <jic23@cam.ac.uk> |
| 5 | * |
| 6 | * Based on wm8510.c |
| 7 | * Copyright 2006 Wolfson Microelectronics PLC. |
| 8 | * Author: Liam Girdwood <lrg@slimlogic.co.uk> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | * Not currently handled: |
| 15 | * Notch filter control |
| 16 | * AUXMode (inverting vs mixer) |
| 17 | * No means to obtain current gain if alc enabled. |
| 18 | * No use made of gpio |
| 19 | * Fast VMID discharge for power down |
| 20 | * Soft Start |
| 21 | * DLR and ALR Swaps not enabled |
| 22 | * Digital Sidetone not supported |
| 23 | */ |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/moduleparam.h> |
| 26 | #include <linux/kernel.h> |
| 27 | #include <linux/init.h> |
| 28 | #include <linux/delay.h> |
| 29 | #include <linux/pm.h> |
| 30 | #include <linux/i2c.h> |
| 31 | #include <linux/platform_device.h> |
| 32 | #include <linux/spi/spi.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 33 | #include <linux/slab.h> |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 34 | #include <sound/core.h> |
| 35 | #include <sound/pcm.h> |
| 36 | #include <sound/pcm_params.h> |
| 37 | #include <sound/soc.h> |
| 38 | #include <sound/soc-dapm.h> |
| 39 | #include <sound/initval.h> |
| 40 | #include <sound/tlv.h> |
| 41 | |
| 42 | #include "wm8940.h" |
| 43 | |
| 44 | struct wm8940_priv { |
| 45 | unsigned int sysclk; |
| 46 | u16 reg_cache[WM8940_CACHEREGNUM]; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 47 | enum snd_soc_control_type control_type; |
| 48 | void *control_data; |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | static u16 wm8940_reg_defaults[] = { |
| 52 | 0x8940, /* Soft Reset */ |
| 53 | 0x0000, /* Power 1 */ |
| 54 | 0x0000, /* Power 2 */ |
| 55 | 0x0000, /* Power 3 */ |
| 56 | 0x0010, /* Interface Control */ |
| 57 | 0x0000, /* Companding Control */ |
| 58 | 0x0140, /* Clock Control */ |
| 59 | 0x0000, /* Additional Controls */ |
| 60 | 0x0000, /* GPIO Control */ |
| 61 | 0x0002, /* Auto Increment Control */ |
| 62 | 0x0000, /* DAC Control */ |
| 63 | 0x00FF, /* DAC Volume */ |
| 64 | 0, |
| 65 | 0, |
| 66 | 0x0100, /* ADC Control */ |
| 67 | 0x00FF, /* ADC Volume */ |
| 68 | 0x0000, /* Notch Filter 1 Control 1 */ |
| 69 | 0x0000, /* Notch Filter 1 Control 2 */ |
| 70 | 0x0000, /* Notch Filter 2 Control 1 */ |
| 71 | 0x0000, /* Notch Filter 2 Control 2 */ |
| 72 | 0x0000, /* Notch Filter 3 Control 1 */ |
| 73 | 0x0000, /* Notch Filter 3 Control 2 */ |
| 74 | 0x0000, /* Notch Filter 4 Control 1 */ |
| 75 | 0x0000, /* Notch Filter 4 Control 2 */ |
| 76 | 0x0032, /* DAC Limit Control 1 */ |
| 77 | 0x0000, /* DAC Limit Control 2 */ |
| 78 | 0, |
| 79 | 0, |
| 80 | 0, |
| 81 | 0, |
| 82 | 0, |
| 83 | 0, |
| 84 | 0x0038, /* ALC Control 1 */ |
| 85 | 0x000B, /* ALC Control 2 */ |
| 86 | 0x0032, /* ALC Control 3 */ |
| 87 | 0x0000, /* Noise Gate */ |
| 88 | 0x0041, /* PLLN */ |
| 89 | 0x000C, /* PLLK1 */ |
| 90 | 0x0093, /* PLLK2 */ |
| 91 | 0x00E9, /* PLLK3 */ |
| 92 | 0, |
| 93 | 0, |
| 94 | 0x0030, /* ALC Control 4 */ |
| 95 | 0, |
| 96 | 0x0002, /* Input Control */ |
| 97 | 0x0050, /* PGA Gain */ |
| 98 | 0, |
| 99 | 0x0002, /* ADC Boost Control */ |
| 100 | 0, |
| 101 | 0x0002, /* Output Control */ |
| 102 | 0x0000, /* Speaker Mixer Control */ |
| 103 | 0, |
| 104 | 0, |
| 105 | 0, |
| 106 | 0x0079, /* Speaker Volume */ |
| 107 | 0, |
| 108 | 0x0000, /* Mono Mixer Control */ |
| 109 | }; |
| 110 | |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 111 | static const char *wm8940_companding[] = { "Off", "NC", "u-law", "A-law" }; |
| 112 | static const struct soc_enum wm8940_adc_companding_enum |
| 113 | = SOC_ENUM_SINGLE(WM8940_COMPANDINGCTL, 1, 4, wm8940_companding); |
| 114 | static const struct soc_enum wm8940_dac_companding_enum |
| 115 | = SOC_ENUM_SINGLE(WM8940_COMPANDINGCTL, 3, 4, wm8940_companding); |
| 116 | |
| 117 | static const char *wm8940_alc_mode_text[] = {"ALC", "Limiter"}; |
| 118 | static const struct soc_enum wm8940_alc_mode_enum |
| 119 | = SOC_ENUM_SINGLE(WM8940_ALC3, 8, 2, wm8940_alc_mode_text); |
| 120 | |
| 121 | static const char *wm8940_mic_bias_level_text[] = {"0.9", "0.65"}; |
| 122 | static const struct soc_enum wm8940_mic_bias_level_enum |
| 123 | = SOC_ENUM_SINGLE(WM8940_INPUTCTL, 8, 2, wm8940_mic_bias_level_text); |
| 124 | |
| 125 | static const char *wm8940_filter_mode_text[] = {"Audio", "Application"}; |
| 126 | static const struct soc_enum wm8940_filter_mode_enum |
| 127 | = SOC_ENUM_SINGLE(WM8940_ADC, 7, 2, wm8940_filter_mode_text); |
| 128 | |
Mark Brown | 6be01cf | 2009-04-27 20:57:42 +0100 | [diff] [blame] | 129 | static DECLARE_TLV_DB_SCALE(wm8940_spk_vol_tlv, -5700, 100, 1); |
| 130 | static DECLARE_TLV_DB_SCALE(wm8940_att_tlv, -1000, 1000, 0); |
| 131 | static DECLARE_TLV_DB_SCALE(wm8940_pga_vol_tlv, -1200, 75, 0); |
| 132 | static DECLARE_TLV_DB_SCALE(wm8940_alc_min_tlv, -1200, 600, 0); |
| 133 | static DECLARE_TLV_DB_SCALE(wm8940_alc_max_tlv, 675, 600, 0); |
| 134 | static DECLARE_TLV_DB_SCALE(wm8940_alc_tar_tlv, -2250, 50, 0); |
| 135 | static DECLARE_TLV_DB_SCALE(wm8940_lim_boost_tlv, 0, 100, 0); |
| 136 | static DECLARE_TLV_DB_SCALE(wm8940_lim_thresh_tlv, -600, 100, 0); |
| 137 | static DECLARE_TLV_DB_SCALE(wm8940_adc_tlv, -12750, 50, 1); |
| 138 | static DECLARE_TLV_DB_SCALE(wm8940_capture_boost_vol_tlv, 0, 2000, 0); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 139 | |
| 140 | static const struct snd_kcontrol_new wm8940_snd_controls[] = { |
| 141 | SOC_SINGLE("Digital Loopback Switch", WM8940_COMPANDINGCTL, |
| 142 | 6, 1, 0), |
| 143 | SOC_ENUM("DAC Companding", wm8940_dac_companding_enum), |
| 144 | SOC_ENUM("ADC Companding", wm8940_adc_companding_enum), |
| 145 | |
| 146 | SOC_ENUM("ALC Mode", wm8940_alc_mode_enum), |
| 147 | SOC_SINGLE("ALC Switch", WM8940_ALC1, 8, 1, 0), |
| 148 | SOC_SINGLE_TLV("ALC Capture Max Gain", WM8940_ALC1, |
| 149 | 3, 7, 1, wm8940_alc_max_tlv), |
| 150 | SOC_SINGLE_TLV("ALC Capture Min Gain", WM8940_ALC1, |
| 151 | 0, 7, 0, wm8940_alc_min_tlv), |
| 152 | SOC_SINGLE_TLV("ALC Capture Target", WM8940_ALC2, |
| 153 | 0, 14, 0, wm8940_alc_tar_tlv), |
| 154 | SOC_SINGLE("ALC Capture Hold", WM8940_ALC2, 4, 10, 0), |
| 155 | SOC_SINGLE("ALC Capture Decay", WM8940_ALC3, 4, 10, 0), |
| 156 | SOC_SINGLE("ALC Capture Attach", WM8940_ALC3, 0, 10, 0), |
| 157 | SOC_SINGLE("ALC ZC Switch", WM8940_ALC4, 1, 1, 0), |
| 158 | SOC_SINGLE("ALC Capture Noise Gate Switch", WM8940_NOISEGATE, |
| 159 | 3, 1, 0), |
| 160 | SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8940_NOISEGATE, |
| 161 | 0, 7, 0), |
| 162 | |
| 163 | SOC_SINGLE("DAC Playback Limiter Switch", WM8940_DACLIM1, 8, 1, 0), |
| 164 | SOC_SINGLE("DAC Playback Limiter Attack", WM8940_DACLIM1, 0, 9, 0), |
| 165 | SOC_SINGLE("DAC Playback Limiter Decay", WM8940_DACLIM1, 4, 11, 0), |
| 166 | SOC_SINGLE_TLV("DAC Playback Limiter Threshold", WM8940_DACLIM2, |
| 167 | 4, 9, 1, wm8940_lim_thresh_tlv), |
| 168 | SOC_SINGLE_TLV("DAC Playback Limiter Boost", WM8940_DACLIM2, |
| 169 | 0, 12, 0, wm8940_lim_boost_tlv), |
| 170 | |
| 171 | SOC_SINGLE("Capture PGA ZC Switch", WM8940_PGAGAIN, 7, 1, 0), |
| 172 | SOC_SINGLE_TLV("Capture PGA Volume", WM8940_PGAGAIN, |
| 173 | 0, 63, 0, wm8940_pga_vol_tlv), |
| 174 | SOC_SINGLE_TLV("Digital Playback Volume", WM8940_DACVOL, |
| 175 | 0, 255, 0, wm8940_adc_tlv), |
| 176 | SOC_SINGLE_TLV("Digital Capture Volume", WM8940_ADCVOL, |
| 177 | 0, 255, 0, wm8940_adc_tlv), |
| 178 | SOC_ENUM("Mic Bias Level", wm8940_mic_bias_level_enum), |
| 179 | SOC_SINGLE_TLV("Capture Boost Volue", WM8940_ADCBOOST, |
| 180 | 8, 1, 0, wm8940_capture_boost_vol_tlv), |
| 181 | SOC_SINGLE_TLV("Speaker Playback Volume", WM8940_SPKVOL, |
| 182 | 0, 63, 0, wm8940_spk_vol_tlv), |
| 183 | SOC_SINGLE("Speaker Playback Switch", WM8940_SPKVOL, 6, 1, 1), |
| 184 | |
| 185 | SOC_SINGLE_TLV("Speaker Mixer Line Bypass Volume", WM8940_SPKVOL, |
| 186 | 8, 1, 1, wm8940_att_tlv), |
| 187 | SOC_SINGLE("Speaker Playback ZC Switch", WM8940_SPKVOL, 7, 1, 0), |
| 188 | |
| 189 | SOC_SINGLE("Mono Out Switch", WM8940_MONOMIX, 6, 1, 1), |
| 190 | SOC_SINGLE_TLV("Mono Mixer Line Bypass Volume", WM8940_MONOMIX, |
| 191 | 7, 1, 1, wm8940_att_tlv), |
| 192 | |
| 193 | SOC_SINGLE("High Pass Filter Switch", WM8940_ADC, 8, 1, 0), |
| 194 | SOC_ENUM("High Pass Filter Mode", wm8940_filter_mode_enum), |
| 195 | SOC_SINGLE("High Pass Filter Cut Off", WM8940_ADC, 4, 7, 0), |
| 196 | SOC_SINGLE("ADC Inversion Switch", WM8940_ADC, 0, 1, 0), |
| 197 | SOC_SINGLE("DAC Inversion Switch", WM8940_DAC, 0, 1, 0), |
| 198 | SOC_SINGLE("DAC Auto Mute Switch", WM8940_DAC, 2, 1, 0), |
| 199 | SOC_SINGLE("ZC Timeout Clock Switch", WM8940_ADDCNTRL, 0, 1, 0), |
| 200 | }; |
| 201 | |
| 202 | static const struct snd_kcontrol_new wm8940_speaker_mixer_controls[] = { |
| 203 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8940_SPKMIX, 1, 1, 0), |
| 204 | SOC_DAPM_SINGLE("Aux Playback Switch", WM8940_SPKMIX, 5, 1, 0), |
| 205 | SOC_DAPM_SINGLE("PCM Playback Switch", WM8940_SPKMIX, 0, 1, 0), |
| 206 | }; |
| 207 | |
| 208 | static const struct snd_kcontrol_new wm8940_mono_mixer_controls[] = { |
| 209 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8940_MONOMIX, 1, 1, 0), |
| 210 | SOC_DAPM_SINGLE("Aux Playback Switch", WM8940_MONOMIX, 2, 1, 0), |
| 211 | SOC_DAPM_SINGLE("PCM Playback Switch", WM8940_MONOMIX, 0, 1, 0), |
| 212 | }; |
| 213 | |
Mark Brown | 6be01cf | 2009-04-27 20:57:42 +0100 | [diff] [blame] | 214 | static DECLARE_TLV_DB_SCALE(wm8940_boost_vol_tlv, -1500, 300, 1); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 215 | static const struct snd_kcontrol_new wm8940_input_boost_controls[] = { |
| 216 | SOC_DAPM_SINGLE("Mic PGA Switch", WM8940_PGAGAIN, 6, 1, 1), |
| 217 | SOC_DAPM_SINGLE_TLV("Aux Volume", WM8940_ADCBOOST, |
| 218 | 0, 7, 0, wm8940_boost_vol_tlv), |
| 219 | SOC_DAPM_SINGLE_TLV("Mic Volume", WM8940_ADCBOOST, |
| 220 | 4, 7, 0, wm8940_boost_vol_tlv), |
| 221 | }; |
| 222 | |
| 223 | static const struct snd_kcontrol_new wm8940_micpga_controls[] = { |
| 224 | SOC_DAPM_SINGLE("AUX Switch", WM8940_INPUTCTL, 2, 1, 0), |
| 225 | SOC_DAPM_SINGLE("MICP Switch", WM8940_INPUTCTL, 0, 1, 0), |
| 226 | SOC_DAPM_SINGLE("MICN Switch", WM8940_INPUTCTL, 1, 1, 0), |
| 227 | }; |
| 228 | |
| 229 | static const struct snd_soc_dapm_widget wm8940_dapm_widgets[] = { |
| 230 | SND_SOC_DAPM_MIXER("Speaker Mixer", WM8940_POWER3, 2, 0, |
| 231 | &wm8940_speaker_mixer_controls[0], |
| 232 | ARRAY_SIZE(wm8940_speaker_mixer_controls)), |
| 233 | SND_SOC_DAPM_MIXER("Mono Mixer", WM8940_POWER3, 3, 0, |
| 234 | &wm8940_mono_mixer_controls[0], |
| 235 | ARRAY_SIZE(wm8940_mono_mixer_controls)), |
| 236 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8940_POWER3, 0, 0), |
| 237 | |
| 238 | SND_SOC_DAPM_PGA("SpkN Out", WM8940_POWER3, 5, 0, NULL, 0), |
| 239 | SND_SOC_DAPM_PGA("SpkP Out", WM8940_POWER3, 6, 0, NULL, 0), |
| 240 | SND_SOC_DAPM_PGA("Mono Out", WM8940_POWER3, 7, 0, NULL, 0), |
| 241 | SND_SOC_DAPM_OUTPUT("MONOOUT"), |
| 242 | SND_SOC_DAPM_OUTPUT("SPKOUTP"), |
| 243 | SND_SOC_DAPM_OUTPUT("SPKOUTN"), |
| 244 | |
| 245 | SND_SOC_DAPM_PGA("Aux Input", WM8940_POWER1, 6, 0, NULL, 0), |
| 246 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8940_POWER2, 0, 0), |
| 247 | SND_SOC_DAPM_MIXER("Mic PGA", WM8940_POWER2, 2, 0, |
| 248 | &wm8940_micpga_controls[0], |
| 249 | ARRAY_SIZE(wm8940_micpga_controls)), |
| 250 | SND_SOC_DAPM_MIXER("Boost Mixer", WM8940_POWER2, 4, 0, |
| 251 | &wm8940_input_boost_controls[0], |
| 252 | ARRAY_SIZE(wm8940_input_boost_controls)), |
| 253 | SND_SOC_DAPM_MICBIAS("Mic Bias", WM8940_POWER1, 4, 0), |
| 254 | |
| 255 | SND_SOC_DAPM_INPUT("MICN"), |
| 256 | SND_SOC_DAPM_INPUT("MICP"), |
| 257 | SND_SOC_DAPM_INPUT("AUX"), |
| 258 | }; |
| 259 | |
| 260 | static const struct snd_soc_dapm_route audio_map[] = { |
| 261 | /* Mono output mixer */ |
| 262 | {"Mono Mixer", "PCM Playback Switch", "DAC"}, |
| 263 | {"Mono Mixer", "Aux Playback Switch", "Aux Input"}, |
| 264 | {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"}, |
| 265 | |
| 266 | /* Speaker output mixer */ |
| 267 | {"Speaker Mixer", "PCM Playback Switch", "DAC"}, |
| 268 | {"Speaker Mixer", "Aux Playback Switch", "Aux Input"}, |
| 269 | {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"}, |
| 270 | |
| 271 | /* Outputs */ |
| 272 | {"Mono Out", NULL, "Mono Mixer"}, |
| 273 | {"MONOOUT", NULL, "Mono Out"}, |
| 274 | {"SpkN Out", NULL, "Speaker Mixer"}, |
| 275 | {"SpkP Out", NULL, "Speaker Mixer"}, |
| 276 | {"SPKOUTN", NULL, "SpkN Out"}, |
| 277 | {"SPKOUTP", NULL, "SpkP Out"}, |
| 278 | |
| 279 | /* Microphone PGA */ |
| 280 | {"Mic PGA", "MICN Switch", "MICN"}, |
| 281 | {"Mic PGA", "MICP Switch", "MICP"}, |
| 282 | {"Mic PGA", "AUX Switch", "AUX"}, |
| 283 | |
| 284 | /* Boost Mixer */ |
| 285 | {"Boost Mixer", "Mic PGA Switch", "Mic PGA"}, |
| 286 | {"Boost Mixer", "Mic Volume", "MICP"}, |
| 287 | {"Boost Mixer", "Aux Volume", "Aux Input"}, |
| 288 | |
| 289 | {"ADC", NULL, "Boost Mixer"}, |
| 290 | }; |
| 291 | |
| 292 | static int wm8940_add_widgets(struct snd_soc_codec *codec) |
| 293 | { |
| 294 | int ret; |
| 295 | |
| 296 | ret = snd_soc_dapm_new_controls(codec, wm8940_dapm_widgets, |
| 297 | ARRAY_SIZE(wm8940_dapm_widgets)); |
| 298 | if (ret) |
| 299 | goto error_ret; |
| 300 | ret = snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map)); |
| 301 | if (ret) |
| 302 | goto error_ret; |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 303 | |
| 304 | error_ret: |
| 305 | return ret; |
| 306 | } |
| 307 | |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 308 | #define wm8940_reset(c) snd_soc_write(c, WM8940_SOFTRESET, 0); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 309 | |
| 310 | static int wm8940_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 311 | unsigned int fmt) |
| 312 | { |
| 313 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 314 | u16 iface = snd_soc_read(codec, WM8940_IFACE) & 0xFE67; |
| 315 | u16 clk = snd_soc_read(codec, WM8940_CLOCK) & 0x1fe; |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 316 | |
| 317 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 318 | case SND_SOC_DAIFMT_CBM_CFM: |
| 319 | clk |= 1; |
| 320 | break; |
| 321 | case SND_SOC_DAIFMT_CBS_CFS: |
| 322 | break; |
| 323 | default: |
| 324 | return -EINVAL; |
| 325 | } |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 326 | snd_soc_write(codec, WM8940_CLOCK, clk); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 327 | |
| 328 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 329 | case SND_SOC_DAIFMT_I2S: |
| 330 | iface |= (2 << 3); |
| 331 | break; |
| 332 | case SND_SOC_DAIFMT_LEFT_J: |
| 333 | iface |= (1 << 3); |
| 334 | break; |
| 335 | case SND_SOC_DAIFMT_RIGHT_J: |
| 336 | break; |
| 337 | case SND_SOC_DAIFMT_DSP_A: |
| 338 | iface |= (3 << 3); |
| 339 | break; |
| 340 | case SND_SOC_DAIFMT_DSP_B: |
| 341 | iface |= (3 << 3) | (1 << 7); |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 346 | case SND_SOC_DAIFMT_NB_NF: |
| 347 | break; |
| 348 | case SND_SOC_DAIFMT_NB_IF: |
| 349 | iface |= (1 << 7); |
| 350 | break; |
| 351 | case SND_SOC_DAIFMT_IB_NF: |
| 352 | iface |= (1 << 8); |
| 353 | break; |
| 354 | case SND_SOC_DAIFMT_IB_IF: |
| 355 | iface |= (1 << 8) | (1 << 7); |
| 356 | break; |
| 357 | } |
| 358 | |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 359 | snd_soc_write(codec, WM8940_IFACE, iface); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int wm8940_i2s_hw_params(struct snd_pcm_substream *substream, |
| 365 | struct snd_pcm_hw_params *params, |
| 366 | struct snd_soc_dai *dai) |
| 367 | { |
| 368 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 369 | struct snd_soc_codec *codec = rtd->codec; |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 370 | u16 iface = snd_soc_read(codec, WM8940_IFACE) & 0xFD9F; |
| 371 | u16 addcntrl = snd_soc_read(codec, WM8940_ADDCNTRL) & 0xFFF1; |
| 372 | u16 companding = snd_soc_read(codec, |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 373 | WM8940_COMPANDINGCTL) & 0xFFDF; |
| 374 | int ret; |
| 375 | |
| 376 | /* LoutR control */ |
| 377 | if (substream->stream == SNDRV_PCM_STREAM_CAPTURE |
| 378 | && params_channels(params) == 2) |
| 379 | iface |= (1 << 9); |
| 380 | |
| 381 | switch (params_rate(params)) { |
Guennadi Liakhovetski | b3172f2 | 2009-12-24 01:13:51 +0100 | [diff] [blame] | 382 | case 8000: |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 383 | addcntrl |= (0x5 << 1); |
| 384 | break; |
Guennadi Liakhovetski | b3172f2 | 2009-12-24 01:13:51 +0100 | [diff] [blame] | 385 | case 11025: |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 386 | addcntrl |= (0x4 << 1); |
| 387 | break; |
Guennadi Liakhovetski | b3172f2 | 2009-12-24 01:13:51 +0100 | [diff] [blame] | 388 | case 16000: |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 389 | addcntrl |= (0x3 << 1); |
| 390 | break; |
Guennadi Liakhovetski | b3172f2 | 2009-12-24 01:13:51 +0100 | [diff] [blame] | 391 | case 22050: |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 392 | addcntrl |= (0x2 << 1); |
| 393 | break; |
Guennadi Liakhovetski | b3172f2 | 2009-12-24 01:13:51 +0100 | [diff] [blame] | 394 | case 32000: |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 395 | addcntrl |= (0x1 << 1); |
| 396 | break; |
Guennadi Liakhovetski | b3172f2 | 2009-12-24 01:13:51 +0100 | [diff] [blame] | 397 | case 44100: |
| 398 | case 48000: |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 399 | break; |
| 400 | } |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 401 | ret = snd_soc_write(codec, WM8940_ADDCNTRL, addcntrl); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 402 | if (ret) |
| 403 | goto error_ret; |
| 404 | |
| 405 | switch (params_format(params)) { |
| 406 | case SNDRV_PCM_FORMAT_S8: |
| 407 | companding = companding | (1 << 5); |
| 408 | break; |
| 409 | case SNDRV_PCM_FORMAT_S16_LE: |
| 410 | break; |
| 411 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 412 | iface |= (1 << 5); |
| 413 | break; |
| 414 | case SNDRV_PCM_FORMAT_S24_LE: |
| 415 | iface |= (2 << 5); |
| 416 | break; |
| 417 | case SNDRV_PCM_FORMAT_S32_LE: |
| 418 | iface |= (3 << 5); |
| 419 | break; |
| 420 | } |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 421 | ret = snd_soc_write(codec, WM8940_COMPANDINGCTL, companding); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 422 | if (ret) |
| 423 | goto error_ret; |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 424 | ret = snd_soc_write(codec, WM8940_IFACE, iface); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 425 | |
| 426 | error_ret: |
| 427 | return ret; |
| 428 | } |
| 429 | |
| 430 | static int wm8940_mute(struct snd_soc_dai *dai, int mute) |
| 431 | { |
| 432 | struct snd_soc_codec *codec = dai->codec; |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 433 | u16 mute_reg = snd_soc_read(codec, WM8940_DAC) & 0xffbf; |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 434 | |
| 435 | if (mute) |
| 436 | mute_reg |= 0x40; |
| 437 | |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 438 | return snd_soc_write(codec, WM8940_DAC, mute_reg); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static int wm8940_set_bias_level(struct snd_soc_codec *codec, |
| 442 | enum snd_soc_bias_level level) |
| 443 | { |
| 444 | u16 val; |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 445 | u16 pwr_reg = snd_soc_read(codec, WM8940_POWER1) & 0x1F0; |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 446 | int ret = 0; |
| 447 | |
| 448 | switch (level) { |
| 449 | case SND_SOC_BIAS_ON: |
| 450 | /* ensure bufioen and biasen */ |
| 451 | pwr_reg |= (1 << 2) | (1 << 3); |
| 452 | /* Enable thermal shutdown */ |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 453 | val = snd_soc_read(codec, WM8940_OUTPUTCTL); |
| 454 | ret = snd_soc_write(codec, WM8940_OUTPUTCTL, val | 0x2); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 455 | if (ret) |
| 456 | break; |
| 457 | /* set vmid to 75k */ |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 458 | ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x1); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 459 | break; |
| 460 | case SND_SOC_BIAS_PREPARE: |
| 461 | /* ensure bufioen and biasen */ |
| 462 | pwr_reg |= (1 << 2) | (1 << 3); |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 463 | ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x1); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 464 | break; |
| 465 | case SND_SOC_BIAS_STANDBY: |
| 466 | /* ensure bufioen and biasen */ |
| 467 | pwr_reg |= (1 << 2) | (1 << 3); |
| 468 | /* set vmid to 300k for standby */ |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 469 | ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg | 0x2); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 470 | break; |
| 471 | case SND_SOC_BIAS_OFF: |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 472 | ret = snd_soc_write(codec, WM8940_POWER1, pwr_reg); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 473 | break; |
| 474 | } |
| 475 | |
| 476 | return ret; |
| 477 | } |
| 478 | |
| 479 | struct pll_ { |
| 480 | unsigned int pre_scale:2; |
| 481 | unsigned int n:4; |
| 482 | unsigned int k; |
| 483 | }; |
| 484 | |
| 485 | static struct pll_ pll_div; |
| 486 | |
| 487 | /* The size in bits of the pll divide multiplied by 10 |
| 488 | * to allow rounding later */ |
| 489 | #define FIXED_PLL_SIZE ((1 << 24) * 10) |
| 490 | static void pll_factors(unsigned int target, unsigned int source) |
| 491 | { |
| 492 | unsigned long long Kpart; |
| 493 | unsigned int K, Ndiv, Nmod; |
| 494 | /* The left shift ist to avoid accuracy loss when right shifting */ |
| 495 | Ndiv = target / source; |
| 496 | |
| 497 | if (Ndiv > 12) { |
| 498 | source <<= 1; |
| 499 | /* Multiply by 2 */ |
| 500 | pll_div.pre_scale = 0; |
| 501 | Ndiv = target / source; |
| 502 | } else if (Ndiv < 3) { |
| 503 | source >>= 2; |
| 504 | /* Divide by 4 */ |
| 505 | pll_div.pre_scale = 3; |
| 506 | Ndiv = target / source; |
| 507 | } else if (Ndiv < 6) { |
| 508 | source >>= 1; |
| 509 | /* divide by 2 */ |
| 510 | pll_div.pre_scale = 2; |
| 511 | Ndiv = target / source; |
| 512 | } else |
| 513 | pll_div.pre_scale = 1; |
| 514 | |
| 515 | if ((Ndiv < 6) || (Ndiv > 12)) |
| 516 | printk(KERN_WARNING |
| 517 | "WM8940 N value %d outwith recommended range!d\n", |
| 518 | Ndiv); |
| 519 | |
| 520 | pll_div.n = Ndiv; |
| 521 | Nmod = target % source; |
| 522 | Kpart = FIXED_PLL_SIZE * (long long)Nmod; |
| 523 | |
| 524 | do_div(Kpart, source); |
| 525 | |
| 526 | K = Kpart & 0xFFFFFFFF; |
| 527 | |
| 528 | /* Check if we need to round */ |
| 529 | if ((K % 10) >= 5) |
| 530 | K += 5; |
| 531 | |
| 532 | /* Move down to proper range now rounding is done */ |
| 533 | K /= 10; |
| 534 | |
| 535 | pll_div.k = K; |
| 536 | } |
| 537 | |
| 538 | /* Untested at the moment */ |
Mark Brown | 8548803 | 2009-09-05 18:52:16 +0100 | [diff] [blame] | 539 | static int wm8940_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id, |
| 540 | int source, unsigned int freq_in, unsigned int freq_out) |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 541 | { |
| 542 | struct snd_soc_codec *codec = codec_dai->codec; |
| 543 | u16 reg; |
| 544 | |
| 545 | /* Turn off PLL */ |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 546 | reg = snd_soc_read(codec, WM8940_POWER1); |
| 547 | snd_soc_write(codec, WM8940_POWER1, reg & 0x1df); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 548 | |
| 549 | if (freq_in == 0 || freq_out == 0) { |
| 550 | /* Clock CODEC directly from MCLK */ |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 551 | reg = snd_soc_read(codec, WM8940_CLOCK); |
| 552 | snd_soc_write(codec, WM8940_CLOCK, reg & 0x0ff); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 553 | /* Pll power down */ |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 554 | snd_soc_write(codec, WM8940_PLLN, (1 << 7)); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | /* Pll is followed by a frequency divide by 4 */ |
| 559 | pll_factors(freq_out*4, freq_in); |
| 560 | if (pll_div.k) |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 561 | snd_soc_write(codec, WM8940_PLLN, |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 562 | (pll_div.pre_scale << 4) | pll_div.n | (1 << 6)); |
| 563 | else /* No factional component */ |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 564 | snd_soc_write(codec, WM8940_PLLN, |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 565 | (pll_div.pre_scale << 4) | pll_div.n); |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 566 | snd_soc_write(codec, WM8940_PLLK1, pll_div.k >> 18); |
| 567 | snd_soc_write(codec, WM8940_PLLK2, (pll_div.k >> 9) & 0x1ff); |
| 568 | snd_soc_write(codec, WM8940_PLLK3, pll_div.k & 0x1ff); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 569 | /* Enable the PLL */ |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 570 | reg = snd_soc_read(codec, WM8940_POWER1); |
| 571 | snd_soc_write(codec, WM8940_POWER1, reg | 0x020); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 572 | |
| 573 | /* Run CODEC from PLL instead of MCLK */ |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 574 | reg = snd_soc_read(codec, WM8940_CLOCK); |
| 575 | snd_soc_write(codec, WM8940_CLOCK, reg | 0x100); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 576 | |
| 577 | return 0; |
| 578 | } |
| 579 | |
| 580 | static int wm8940_set_dai_sysclk(struct snd_soc_dai *codec_dai, |
| 581 | int clk_id, unsigned int freq, int dir) |
| 582 | { |
| 583 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 584 | struct wm8940_priv *wm8940 = snd_soc_codec_get_drvdata(codec); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 585 | |
| 586 | switch (freq) { |
| 587 | case 11289600: |
| 588 | case 12000000: |
| 589 | case 12288000: |
| 590 | case 16934400: |
| 591 | case 18432000: |
| 592 | wm8940->sysclk = freq; |
| 593 | return 0; |
| 594 | } |
| 595 | return -EINVAL; |
| 596 | } |
| 597 | |
| 598 | static int wm8940_set_dai_clkdiv(struct snd_soc_dai *codec_dai, |
| 599 | int div_id, int div) |
| 600 | { |
| 601 | struct snd_soc_codec *codec = codec_dai->codec; |
| 602 | u16 reg; |
| 603 | int ret = 0; |
| 604 | |
| 605 | switch (div_id) { |
| 606 | case WM8940_BCLKDIV: |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 607 | reg = snd_soc_read(codec, WM8940_CLOCK) & 0xFFEF3; |
| 608 | ret = snd_soc_write(codec, WM8940_CLOCK, reg | (div << 2)); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 609 | break; |
| 610 | case WM8940_MCLKDIV: |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 611 | reg = snd_soc_read(codec, WM8940_CLOCK) & 0xFF1F; |
| 612 | ret = snd_soc_write(codec, WM8940_CLOCK, reg | (div << 5)); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 613 | break; |
| 614 | case WM8940_OPCLKDIV: |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 615 | reg = snd_soc_read(codec, WM8940_ADDCNTRL) & 0xFFCF; |
| 616 | ret = snd_soc_write(codec, WM8940_ADDCNTRL, reg | (div << 4)); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 617 | break; |
| 618 | } |
| 619 | return ret; |
| 620 | } |
| 621 | |
| 622 | #define WM8940_RATES SNDRV_PCM_RATE_8000_48000 |
| 623 | |
| 624 | #define WM8940_FORMATS (SNDRV_PCM_FMTBIT_S8 | \ |
| 625 | SNDRV_PCM_FMTBIT_S16_LE | \ |
| 626 | SNDRV_PCM_FMTBIT_S20_3LE | \ |
| 627 | SNDRV_PCM_FMTBIT_S24_LE | \ |
| 628 | SNDRV_PCM_FMTBIT_S32_LE) |
| 629 | |
| 630 | static struct snd_soc_dai_ops wm8940_dai_ops = { |
| 631 | .hw_params = wm8940_i2s_hw_params, |
| 632 | .set_sysclk = wm8940_set_dai_sysclk, |
| 633 | .digital_mute = wm8940_mute, |
| 634 | .set_fmt = wm8940_set_dai_fmt, |
| 635 | .set_clkdiv = wm8940_set_dai_clkdiv, |
| 636 | .set_pll = wm8940_set_dai_pll, |
| 637 | }; |
| 638 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 639 | static struct snd_soc_dai_driver wm8940_dai = { |
| 640 | .name = "wm8940-hifi", |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 641 | .playback = { |
| 642 | .stream_name = "Playback", |
| 643 | .channels_min = 1, |
| 644 | .channels_max = 2, |
| 645 | .rates = WM8940_RATES, |
| 646 | .formats = WM8940_FORMATS, |
| 647 | }, |
| 648 | .capture = { |
| 649 | .stream_name = "Capture", |
| 650 | .channels_min = 1, |
| 651 | .channels_max = 2, |
| 652 | .rates = WM8940_RATES, |
| 653 | .formats = WM8940_FORMATS, |
| 654 | }, |
| 655 | .ops = &wm8940_dai_ops, |
| 656 | .symmetric_rates = 1, |
| 657 | }; |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 658 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 659 | static int wm8940_suspend(struct snd_soc_codec *codec, pm_message_t state) |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 660 | { |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 661 | return wm8940_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 662 | } |
| 663 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 664 | static int wm8940_resume(struct snd_soc_codec *codec) |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 665 | { |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 666 | int i; |
| 667 | int ret; |
| 668 | u8 data[3]; |
| 669 | u16 *cache = codec->reg_cache; |
| 670 | |
| 671 | /* Sync reg_cache with the hardware |
| 672 | * Could use auto incremented writes to speed this up |
| 673 | */ |
| 674 | for (i = 0; i < ARRAY_SIZE(wm8940_reg_defaults); i++) { |
| 675 | data[0] = i; |
| 676 | data[1] = (cache[i] & 0xFF00) >> 8; |
| 677 | data[2] = cache[i] & 0x00FF; |
| 678 | ret = codec->hw_write(codec->control_data, data, 3); |
| 679 | if (ret < 0) |
| 680 | goto error_ret; |
| 681 | else if (ret != 3) { |
| 682 | ret = -EIO; |
| 683 | goto error_ret; |
| 684 | } |
| 685 | } |
| 686 | ret = wm8940_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 687 | if (ret) |
| 688 | goto error_ret; |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 689 | |
| 690 | error_ret: |
| 691 | return ret; |
| 692 | } |
| 693 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 694 | static int wm8940_probe(struct snd_soc_codec *codec) |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 695 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 696 | struct wm8940_priv *wm8940 = snd_soc_codec_get_drvdata(codec); |
| 697 | struct wm8940_setup_data *pdata = codec->dev->platform_data; |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 698 | int ret; |
| 699 | u16 reg; |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 700 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 701 | codec->control_data = wm8940->control_data; |
| 702 | ret = snd_soc_codec_set_cache_io(codec, 8, 16, wm8940->control_type); |
Jonathan Cameron | e655a43 | 2009-10-02 16:09:49 +0100 | [diff] [blame] | 703 | if (ret < 0) { |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 704 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); |
| 705 | return ret; |
| 706 | } |
| 707 | |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 708 | ret = wm8940_reset(codec); |
| 709 | if (ret < 0) { |
| 710 | dev_err(codec->dev, "Failed to issue reset\n"); |
| 711 | return ret; |
| 712 | } |
| 713 | |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 714 | wm8940_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 715 | |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 716 | ret = snd_soc_write(codec, WM8940_POWER1, 0x180); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 717 | if (ret < 0) |
| 718 | return ret; |
| 719 | |
| 720 | if (!pdata) |
| 721 | dev_warn(codec->dev, "No platform data supplied\n"); |
| 722 | else { |
Mark Brown | 8d50e44 | 2009-07-10 23:12:01 +0100 | [diff] [blame] | 723 | reg = snd_soc_read(codec, WM8940_OUTPUTCTL); |
| 724 | ret = snd_soc_write(codec, WM8940_OUTPUTCTL, reg | pdata->vroi); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 725 | if (ret < 0) |
| 726 | return ret; |
| 727 | } |
| 728 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 729 | ret = snd_soc_add_controls(codec, wm8940_snd_controls, |
| 730 | ARRAY_SIZE(wm8940_snd_controls)); |
| 731 | if (ret) |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 732 | return ret; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 733 | ret = wm8940_add_widgets(codec); |
| 734 | if (ret) |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 735 | return ret; |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 736 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 737 | return ret; |
| 738 | ; |
| 739 | } |
| 740 | |
| 741 | static int wm8940_remove(struct snd_soc_codec *codec) |
| 742 | { |
| 743 | wm8940_set_bias_level(codec, SND_SOC_BIAS_OFF); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 744 | return 0; |
| 745 | } |
| 746 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 747 | static struct snd_soc_codec_driver soc_codec_dev_wm8940 = { |
| 748 | .probe = wm8940_probe, |
| 749 | .remove = wm8940_remove, |
| 750 | .suspend = wm8940_suspend, |
| 751 | .resume = wm8940_resume, |
| 752 | .set_bias_level = wm8940_set_bias_level, |
| 753 | .reg_cache_size = sizeof(wm8940_reg_defaults), |
| 754 | .reg_word_size = sizeof(u16), |
| 755 | .reg_cache_default = wm8940_reg_defaults, |
| 756 | }; |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 757 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 758 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
| 759 | static __devinit int wm8940_i2c_probe(struct i2c_client *i2c, |
| 760 | const struct i2c_device_id *id) |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 761 | { |
| 762 | struct wm8940_priv *wm8940; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 763 | int ret; |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 764 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 765 | wm8940 = kzalloc(sizeof(struct wm8940_priv), GFP_KERNEL); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 766 | if (wm8940 == NULL) |
| 767 | return -ENOMEM; |
| 768 | |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 769 | i2c_set_clientdata(i2c, wm8940); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 770 | wm8940->control_data = i2c; |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 771 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 772 | ret = snd_soc_register_codec(&i2c->dev, |
| 773 | &soc_codec_dev_wm8940, &wm8940_dai, 1); |
Axel Lin | db1e18d | 2010-07-23 05:53:49 +0000 | [diff] [blame] | 774 | if (ret < 0) |
| 775 | kfree(wm8940); |
Axel Lin | db1e18d | 2010-07-23 05:53:49 +0000 | [diff] [blame] | 776 | return ret; |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 779 | static __devexit int wm8940_i2c_remove(struct i2c_client *client) |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 780 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 781 | snd_soc_unregister_codec(&client->dev); |
| 782 | kfree(i2c_get_clientdata(client)); |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | static const struct i2c_device_id wm8940_i2c_id[] = { |
| 787 | { "wm8940", 0 }, |
| 788 | { } |
| 789 | }; |
| 790 | MODULE_DEVICE_TABLE(i2c, wm8940_i2c_id); |
| 791 | |
| 792 | static struct i2c_driver wm8940_i2c_driver = { |
| 793 | .driver = { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 794 | .name = "wm8940-codec", |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 795 | .owner = THIS_MODULE, |
| 796 | }, |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 797 | .probe = wm8940_i2c_probe, |
| 798 | .remove = __devexit_p(wm8940_i2c_remove), |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 799 | .id_table = wm8940_i2c_id, |
| 800 | }; |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 801 | #endif |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 802 | |
| 803 | static int __init wm8940_modinit(void) |
| 804 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 805 | int ret = 0; |
| 806 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 807 | ret = i2c_add_driver(&wm8940_i2c_driver); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 808 | if (ret != 0) { |
| 809 | printk(KERN_ERR "Failed to register wm8940 I2C driver: %d\n", |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 810 | ret); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 811 | } |
| 812 | #endif |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 813 | return ret; |
| 814 | } |
| 815 | module_init(wm8940_modinit); |
| 816 | |
| 817 | static void __exit wm8940_exit(void) |
| 818 | { |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 819 | #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE) |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 820 | i2c_del_driver(&wm8940_i2c_driver); |
Liam Girdwood | f0fba2a | 2010-03-17 20:15:21 +0000 | [diff] [blame] | 821 | #endif |
Jonathan Cameron | 0b5e92c | 2009-04-27 13:49:44 +0000 | [diff] [blame] | 822 | } |
| 823 | module_exit(wm8940_exit); |
| 824 | |
| 825 | MODULE_DESCRIPTION("ASoC WM8940 driver"); |
| 826 | MODULE_AUTHOR("Jonathan Cameron"); |
| 827 | MODULE_LICENSE("GPL"); |