Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * wm8960.c -- WM8960 ALSA SoC Audio driver |
| 3 | * |
| 4 | * Author: Liam Girdwood |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 as |
| 8 | * published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/moduleparam.h> |
| 13 | #include <linux/init.h> |
| 14 | #include <linux/delay.h> |
| 15 | #include <linux/pm.h> |
| 16 | #include <linux/i2c.h> |
| 17 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 19 | #include <sound/core.h> |
| 20 | #include <sound/pcm.h> |
| 21 | #include <sound/pcm_params.h> |
| 22 | #include <sound/soc.h> |
| 23 | #include <sound/soc-dapm.h> |
| 24 | #include <sound/initval.h> |
| 25 | #include <sound/tlv.h> |
Mark Brown | b6877a4 | 2010-03-03 11:43:38 +0000 | [diff] [blame] | 26 | #include <sound/wm8960.h> |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 27 | |
| 28 | #include "wm8960.h" |
| 29 | |
| 30 | #define AUDIO_NAME "wm8960" |
| 31 | |
| 32 | struct snd_soc_codec_device soc_codec_dev_wm8960; |
| 33 | |
| 34 | /* R25 - Power 1 */ |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 35 | #define WM8960_VMID_MASK 0x180 |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 36 | #define WM8960_VREF 0x40 |
| 37 | |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 38 | /* R26 - Power 2 */ |
| 39 | #define WM8960_PWR2_LOUT1 0x40 |
| 40 | #define WM8960_PWR2_ROUT1 0x20 |
| 41 | #define WM8960_PWR2_OUT3 0x02 |
| 42 | |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 43 | /* R28 - Anti-pop 1 */ |
| 44 | #define WM8960_POBCTRL 0x80 |
| 45 | #define WM8960_BUFDCOPEN 0x10 |
| 46 | #define WM8960_BUFIOEN 0x08 |
| 47 | #define WM8960_SOFT_ST 0x04 |
| 48 | #define WM8960_HPSTBY 0x01 |
| 49 | |
| 50 | /* R29 - Anti-pop 2 */ |
| 51 | #define WM8960_DISOP 0x40 |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 52 | #define WM8960_DRES_MASK 0x30 |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 53 | |
| 54 | /* |
| 55 | * wm8960 register cache |
| 56 | * We can't read the WM8960 register space when we are |
| 57 | * using 2 wire for device control, so we cache them instead. |
| 58 | */ |
| 59 | static const u16 wm8960_reg[WM8960_CACHEREGNUM] = { |
| 60 | 0x0097, 0x0097, 0x0000, 0x0000, |
| 61 | 0x0000, 0x0008, 0x0000, 0x000a, |
| 62 | 0x01c0, 0x0000, 0x00ff, 0x00ff, |
| 63 | 0x0000, 0x0000, 0x0000, 0x0000, |
| 64 | 0x0000, 0x007b, 0x0100, 0x0032, |
| 65 | 0x0000, 0x00c3, 0x00c3, 0x01c0, |
| 66 | 0x0000, 0x0000, 0x0000, 0x0000, |
| 67 | 0x0000, 0x0000, 0x0000, 0x0000, |
| 68 | 0x0100, 0x0100, 0x0050, 0x0050, |
| 69 | 0x0050, 0x0050, 0x0000, 0x0000, |
| 70 | 0x0000, 0x0000, 0x0040, 0x0000, |
| 71 | 0x0000, 0x0050, 0x0050, 0x0000, |
| 72 | 0x0002, 0x0037, 0x004d, 0x0080, |
| 73 | 0x0008, 0x0031, 0x0026, 0x00e9, |
| 74 | }; |
| 75 | |
| 76 | struct wm8960_priv { |
| 77 | u16 reg_cache[WM8960_CACHEREGNUM]; |
| 78 | struct snd_soc_codec codec; |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 79 | struct snd_soc_dapm_widget *lout1; |
| 80 | struct snd_soc_dapm_widget *rout1; |
| 81 | struct snd_soc_dapm_widget *out3; |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 82 | }; |
| 83 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 84 | #define wm8960_reset(c) snd_soc_write(c, WM8960_RESET, 0) |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 85 | |
| 86 | /* enumerated controls */ |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 87 | static const char *wm8960_polarity[] = {"No Inversion", "Left Inverted", |
| 88 | "Right Inverted", "Stereo Inversion"}; |
| 89 | static const char *wm8960_3d_upper_cutoff[] = {"High", "Low"}; |
| 90 | static const char *wm8960_3d_lower_cutoff[] = {"Low", "High"}; |
| 91 | static const char *wm8960_alcfunc[] = {"Off", "Right", "Left", "Stereo"}; |
| 92 | static const char *wm8960_alcmode[] = {"ALC", "Limiter"}; |
| 93 | |
| 94 | static const struct soc_enum wm8960_enum[] = { |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 95 | SOC_ENUM_SINGLE(WM8960_DACCTL1, 5, 4, wm8960_polarity), |
| 96 | SOC_ENUM_SINGLE(WM8960_DACCTL2, 5, 4, wm8960_polarity), |
| 97 | SOC_ENUM_SINGLE(WM8960_3D, 6, 2, wm8960_3d_upper_cutoff), |
| 98 | SOC_ENUM_SINGLE(WM8960_3D, 5, 2, wm8960_3d_lower_cutoff), |
| 99 | SOC_ENUM_SINGLE(WM8960_ALC1, 7, 4, wm8960_alcfunc), |
| 100 | SOC_ENUM_SINGLE(WM8960_ALC3, 8, 2, wm8960_alcmode), |
| 101 | }; |
| 102 | |
| 103 | static const DECLARE_TLV_DB_SCALE(adc_tlv, -9700, 50, 0); |
| 104 | static const DECLARE_TLV_DB_SCALE(dac_tlv, -12700, 50, 1); |
| 105 | static const DECLARE_TLV_DB_SCALE(bypass_tlv, -2100, 300, 0); |
| 106 | static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1); |
| 107 | |
| 108 | static const struct snd_kcontrol_new wm8960_snd_controls[] = { |
| 109 | SOC_DOUBLE_R_TLV("Capture Volume", WM8960_LINVOL, WM8960_RINVOL, |
| 110 | 0, 63, 0, adc_tlv), |
| 111 | SOC_DOUBLE_R("Capture Volume ZC Switch", WM8960_LINVOL, WM8960_RINVOL, |
| 112 | 6, 1, 0), |
| 113 | SOC_DOUBLE_R("Capture Switch", WM8960_LINVOL, WM8960_RINVOL, |
| 114 | 7, 1, 0), |
| 115 | |
| 116 | SOC_DOUBLE_R_TLV("Playback Volume", WM8960_LDAC, WM8960_RDAC, |
| 117 | 0, 255, 0, dac_tlv), |
| 118 | |
| 119 | SOC_DOUBLE_R_TLV("Headphone Playback Volume", WM8960_LOUT1, WM8960_ROUT1, |
| 120 | 0, 127, 0, out_tlv), |
| 121 | SOC_DOUBLE_R("Headphone Playback ZC Switch", WM8960_LOUT1, WM8960_ROUT1, |
| 122 | 7, 1, 0), |
| 123 | |
| 124 | SOC_DOUBLE_R_TLV("Speaker Playback Volume", WM8960_LOUT2, WM8960_ROUT2, |
| 125 | 0, 127, 0, out_tlv), |
| 126 | SOC_DOUBLE_R("Speaker Playback ZC Switch", WM8960_LOUT2, WM8960_ROUT2, |
| 127 | 7, 1, 0), |
| 128 | SOC_SINGLE("Speaker DC Volume", WM8960_CLASSD3, 3, 5, 0), |
| 129 | SOC_SINGLE("Speaker AC Volume", WM8960_CLASSD3, 0, 5, 0), |
| 130 | |
| 131 | SOC_SINGLE("PCM Playback -6dB Switch", WM8960_DACCTL1, 7, 1, 0), |
Mark Brown | 4faaa8d | 2010-07-05 13:54:32 +0900 | [diff] [blame^] | 132 | SOC_ENUM("ADC Polarity", wm8960_enum[0]), |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 133 | SOC_SINGLE("ADC High Pass Filter Switch", WM8960_DACCTL1, 0, 1, 0), |
| 134 | |
| 135 | SOC_ENUM("DAC Polarity", wm8960_enum[2]), |
| 136 | |
Mark Brown | 4faaa8d | 2010-07-05 13:54:32 +0900 | [diff] [blame^] | 137 | SOC_ENUM("3D Filter Upper Cut-Off", wm8960_enum[2]), |
| 138 | SOC_ENUM("3D Filter Lower Cut-Off", wm8960_enum[3]), |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 139 | SOC_SINGLE("3D Volume", WM8960_3D, 1, 15, 0), |
| 140 | SOC_SINGLE("3D Switch", WM8960_3D, 0, 1, 0), |
| 141 | |
Mark Brown | 4faaa8d | 2010-07-05 13:54:32 +0900 | [diff] [blame^] | 142 | SOC_ENUM("ALC Function", wm8960_enum[4]), |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 143 | SOC_SINGLE("ALC Max Gain", WM8960_ALC1, 4, 7, 0), |
| 144 | SOC_SINGLE("ALC Target", WM8960_ALC1, 0, 15, 1), |
| 145 | SOC_SINGLE("ALC Min Gain", WM8960_ALC2, 4, 7, 0), |
| 146 | SOC_SINGLE("ALC Hold Time", WM8960_ALC2, 0, 15, 0), |
Mark Brown | 4faaa8d | 2010-07-05 13:54:32 +0900 | [diff] [blame^] | 147 | SOC_ENUM("ALC Mode", wm8960_enum[5]), |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 148 | SOC_SINGLE("ALC Decay", WM8960_ALC3, 4, 15, 0), |
| 149 | SOC_SINGLE("ALC Attack", WM8960_ALC3, 0, 15, 0), |
| 150 | |
| 151 | SOC_SINGLE("Noise Gate Threshold", WM8960_NOISEG, 3, 31, 0), |
| 152 | SOC_SINGLE("Noise Gate Switch", WM8960_NOISEG, 0, 1, 0), |
| 153 | |
| 154 | SOC_DOUBLE_R("ADC PCM Capture Volume", WM8960_LINPATH, WM8960_RINPATH, |
| 155 | 0, 127, 0), |
| 156 | |
| 157 | SOC_SINGLE_TLV("Left Output Mixer Boost Bypass Volume", |
| 158 | WM8960_BYPASS1, 4, 7, 1, bypass_tlv), |
| 159 | SOC_SINGLE_TLV("Left Output Mixer LINPUT3 Volume", |
| 160 | WM8960_LOUTMIX, 4, 7, 1, bypass_tlv), |
| 161 | SOC_SINGLE_TLV("Right Output Mixer Boost Bypass Volume", |
| 162 | WM8960_BYPASS2, 4, 7, 1, bypass_tlv), |
| 163 | SOC_SINGLE_TLV("Right Output Mixer RINPUT3 Volume", |
| 164 | WM8960_ROUTMIX, 4, 7, 1, bypass_tlv), |
| 165 | }; |
| 166 | |
| 167 | static const struct snd_kcontrol_new wm8960_lin_boost[] = { |
| 168 | SOC_DAPM_SINGLE("LINPUT2 Switch", WM8960_LINPATH, 6, 1, 0), |
| 169 | SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LINPATH, 7, 1, 0), |
| 170 | SOC_DAPM_SINGLE("LINPUT1 Switch", WM8960_LINPATH, 8, 1, 0), |
| 171 | }; |
| 172 | |
| 173 | static const struct snd_kcontrol_new wm8960_lin[] = { |
| 174 | SOC_DAPM_SINGLE("Boost Switch", WM8960_LINPATH, 3, 1, 0), |
| 175 | }; |
| 176 | |
| 177 | static const struct snd_kcontrol_new wm8960_rin_boost[] = { |
| 178 | SOC_DAPM_SINGLE("RINPUT2 Switch", WM8960_RINPATH, 6, 1, 0), |
| 179 | SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_RINPATH, 7, 1, 0), |
| 180 | SOC_DAPM_SINGLE("RINPUT1 Switch", WM8960_RINPATH, 8, 1, 0), |
| 181 | }; |
| 182 | |
| 183 | static const struct snd_kcontrol_new wm8960_rin[] = { |
| 184 | SOC_DAPM_SINGLE("Boost Switch", WM8960_RINPATH, 3, 1, 0), |
| 185 | }; |
| 186 | |
| 187 | static const struct snd_kcontrol_new wm8960_loutput_mixer[] = { |
| 188 | SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_LOUTMIX, 8, 1, 0), |
| 189 | SOC_DAPM_SINGLE("LINPUT3 Switch", WM8960_LOUTMIX, 7, 1, 0), |
| 190 | SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS1, 7, 1, 0), |
| 191 | }; |
| 192 | |
| 193 | static const struct snd_kcontrol_new wm8960_routput_mixer[] = { |
| 194 | SOC_DAPM_SINGLE("PCM Playback Switch", WM8960_ROUTMIX, 8, 1, 0), |
| 195 | SOC_DAPM_SINGLE("RINPUT3 Switch", WM8960_ROUTMIX, 7, 1, 0), |
| 196 | SOC_DAPM_SINGLE("Boost Bypass Switch", WM8960_BYPASS2, 7, 1, 0), |
| 197 | }; |
| 198 | |
| 199 | static const struct snd_kcontrol_new wm8960_mono_out[] = { |
| 200 | SOC_DAPM_SINGLE("Left Switch", WM8960_MONOMIX1, 7, 1, 0), |
| 201 | SOC_DAPM_SINGLE("Right Switch", WM8960_MONOMIX2, 7, 1, 0), |
| 202 | }; |
| 203 | |
| 204 | static const struct snd_soc_dapm_widget wm8960_dapm_widgets[] = { |
| 205 | SND_SOC_DAPM_INPUT("LINPUT1"), |
| 206 | SND_SOC_DAPM_INPUT("RINPUT1"), |
| 207 | SND_SOC_DAPM_INPUT("LINPUT2"), |
| 208 | SND_SOC_DAPM_INPUT("RINPUT2"), |
| 209 | SND_SOC_DAPM_INPUT("LINPUT3"), |
| 210 | SND_SOC_DAPM_INPUT("RINPUT3"), |
| 211 | |
| 212 | SND_SOC_DAPM_MICBIAS("MICB", WM8960_POWER1, 1, 0), |
| 213 | |
| 214 | SND_SOC_DAPM_MIXER("Left Boost Mixer", WM8960_POWER1, 5, 0, |
| 215 | wm8960_lin_boost, ARRAY_SIZE(wm8960_lin_boost)), |
| 216 | SND_SOC_DAPM_MIXER("Right Boost Mixer", WM8960_POWER1, 4, 0, |
| 217 | wm8960_rin_boost, ARRAY_SIZE(wm8960_rin_boost)), |
| 218 | |
| 219 | SND_SOC_DAPM_MIXER("Left Input Mixer", WM8960_POWER3, 5, 0, |
| 220 | wm8960_lin, ARRAY_SIZE(wm8960_lin)), |
| 221 | SND_SOC_DAPM_MIXER("Right Input Mixer", WM8960_POWER3, 4, 0, |
| 222 | wm8960_rin, ARRAY_SIZE(wm8960_rin)), |
| 223 | |
| 224 | SND_SOC_DAPM_ADC("Left ADC", "Capture", WM8960_POWER2, 3, 0), |
| 225 | SND_SOC_DAPM_ADC("Right ADC", "Capture", WM8960_POWER2, 2, 0), |
| 226 | |
| 227 | SND_SOC_DAPM_DAC("Left DAC", "Playback", WM8960_POWER2, 8, 0), |
| 228 | SND_SOC_DAPM_DAC("Right DAC", "Playback", WM8960_POWER2, 7, 0), |
| 229 | |
| 230 | SND_SOC_DAPM_MIXER("Left Output Mixer", WM8960_POWER3, 3, 0, |
| 231 | &wm8960_loutput_mixer[0], |
| 232 | ARRAY_SIZE(wm8960_loutput_mixer)), |
| 233 | SND_SOC_DAPM_MIXER("Right Output Mixer", WM8960_POWER3, 2, 0, |
| 234 | &wm8960_routput_mixer[0], |
| 235 | ARRAY_SIZE(wm8960_routput_mixer)), |
| 236 | |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 237 | SND_SOC_DAPM_PGA("LOUT1 PGA", WM8960_POWER2, 6, 0, NULL, 0), |
| 238 | SND_SOC_DAPM_PGA("ROUT1 PGA", WM8960_POWER2, 5, 0, NULL, 0), |
| 239 | |
| 240 | SND_SOC_DAPM_PGA("Left Speaker PGA", WM8960_POWER2, 4, 0, NULL, 0), |
| 241 | SND_SOC_DAPM_PGA("Right Speaker PGA", WM8960_POWER2, 3, 0, NULL, 0), |
| 242 | |
| 243 | SND_SOC_DAPM_PGA("Right Speaker Output", WM8960_CLASSD1, 7, 0, NULL, 0), |
| 244 | SND_SOC_DAPM_PGA("Left Speaker Output", WM8960_CLASSD1, 6, 0, NULL, 0), |
| 245 | |
| 246 | SND_SOC_DAPM_OUTPUT("SPK_LP"), |
| 247 | SND_SOC_DAPM_OUTPUT("SPK_LN"), |
| 248 | SND_SOC_DAPM_OUTPUT("HP_L"), |
| 249 | SND_SOC_DAPM_OUTPUT("HP_R"), |
| 250 | SND_SOC_DAPM_OUTPUT("SPK_RP"), |
| 251 | SND_SOC_DAPM_OUTPUT("SPK_RN"), |
| 252 | SND_SOC_DAPM_OUTPUT("OUT3"), |
| 253 | }; |
| 254 | |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 255 | static const struct snd_soc_dapm_widget wm8960_dapm_widgets_out3[] = { |
| 256 | SND_SOC_DAPM_MIXER("Mono Output Mixer", WM8960_POWER2, 1, 0, |
| 257 | &wm8960_mono_out[0], |
| 258 | ARRAY_SIZE(wm8960_mono_out)), |
| 259 | }; |
| 260 | |
| 261 | /* Represent OUT3 as a PGA so that it gets turned on with LOUT1/ROUT1 */ |
| 262 | static const struct snd_soc_dapm_widget wm8960_dapm_widgets_capless[] = { |
| 263 | SND_SOC_DAPM_PGA("OUT3 VMID", WM8960_POWER2, 1, 0, NULL, 0), |
| 264 | }; |
| 265 | |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 266 | static const struct snd_soc_dapm_route audio_paths[] = { |
| 267 | { "Left Boost Mixer", "LINPUT1 Switch", "LINPUT1" }, |
| 268 | { "Left Boost Mixer", "LINPUT2 Switch", "LINPUT2" }, |
| 269 | { "Left Boost Mixer", "LINPUT3 Switch", "LINPUT3" }, |
| 270 | |
| 271 | { "Left Input Mixer", "Boost Switch", "Left Boost Mixer", }, |
| 272 | { "Left Input Mixer", NULL, "LINPUT1", }, /* Really Boost Switch */ |
| 273 | { "Left Input Mixer", NULL, "LINPUT2" }, |
| 274 | { "Left Input Mixer", NULL, "LINPUT3" }, |
| 275 | |
| 276 | { "Right Boost Mixer", "RINPUT1 Switch", "RINPUT1" }, |
| 277 | { "Right Boost Mixer", "RINPUT2 Switch", "RINPUT2" }, |
| 278 | { "Right Boost Mixer", "RINPUT3 Switch", "RINPUT3" }, |
| 279 | |
| 280 | { "Right Input Mixer", "Boost Switch", "Right Boost Mixer", }, |
| 281 | { "Right Input Mixer", NULL, "RINPUT1", }, /* Really Boost Switch */ |
| 282 | { "Right Input Mixer", NULL, "RINPUT2" }, |
| 283 | { "Right Input Mixer", NULL, "LINPUT3" }, |
| 284 | |
| 285 | { "Left ADC", NULL, "Left Input Mixer" }, |
| 286 | { "Right ADC", NULL, "Right Input Mixer" }, |
| 287 | |
| 288 | { "Left Output Mixer", "LINPUT3 Switch", "LINPUT3" }, |
| 289 | { "Left Output Mixer", "Boost Bypass Switch", "Left Boost Mixer"} , |
| 290 | { "Left Output Mixer", "PCM Playback Switch", "Left DAC" }, |
| 291 | |
| 292 | { "Right Output Mixer", "RINPUT3 Switch", "RINPUT3" }, |
| 293 | { "Right Output Mixer", "Boost Bypass Switch", "Right Boost Mixer" } , |
| 294 | { "Right Output Mixer", "PCM Playback Switch", "Right DAC" }, |
| 295 | |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 296 | { "LOUT1 PGA", NULL, "Left Output Mixer" }, |
| 297 | { "ROUT1 PGA", NULL, "Right Output Mixer" }, |
| 298 | |
| 299 | { "HP_L", NULL, "LOUT1 PGA" }, |
| 300 | { "HP_R", NULL, "ROUT1 PGA" }, |
| 301 | |
| 302 | { "Left Speaker PGA", NULL, "Left Output Mixer" }, |
| 303 | { "Right Speaker PGA", NULL, "Right Output Mixer" }, |
| 304 | |
| 305 | { "Left Speaker Output", NULL, "Left Speaker PGA" }, |
| 306 | { "Right Speaker Output", NULL, "Right Speaker PGA" }, |
| 307 | |
| 308 | { "SPK_LN", NULL, "Left Speaker Output" }, |
| 309 | { "SPK_LP", NULL, "Left Speaker Output" }, |
| 310 | { "SPK_RN", NULL, "Right Speaker Output" }, |
| 311 | { "SPK_RP", NULL, "Right Speaker Output" }, |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | static const struct snd_soc_dapm_route audio_paths_out3[] = { |
| 315 | { "Mono Output Mixer", "Left Switch", "Left Output Mixer" }, |
| 316 | { "Mono Output Mixer", "Right Switch", "Right Output Mixer" }, |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 317 | |
| 318 | { "OUT3", NULL, "Mono Output Mixer", } |
| 319 | }; |
| 320 | |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 321 | static const struct snd_soc_dapm_route audio_paths_capless[] = { |
| 322 | { "HP_L", NULL, "OUT3 VMID" }, |
| 323 | { "HP_R", NULL, "OUT3 VMID" }, |
| 324 | |
| 325 | { "OUT3 VMID", NULL, "Left Output Mixer" }, |
| 326 | { "OUT3 VMID", NULL, "Right Output Mixer" }, |
| 327 | }; |
| 328 | |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 329 | static int wm8960_add_widgets(struct snd_soc_codec *codec) |
| 330 | { |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 331 | struct wm8960_data *pdata = codec->dev->platform_data; |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 332 | struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 333 | struct snd_soc_dapm_widget *w; |
| 334 | |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 335 | snd_soc_dapm_new_controls(codec, wm8960_dapm_widgets, |
| 336 | ARRAY_SIZE(wm8960_dapm_widgets)); |
| 337 | |
| 338 | snd_soc_dapm_add_routes(codec, audio_paths, ARRAY_SIZE(audio_paths)); |
| 339 | |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 340 | /* In capless mode OUT3 is used to provide VMID for the |
| 341 | * headphone outputs, otherwise it is used as a mono mixer. |
| 342 | */ |
| 343 | if (pdata && pdata->capless) { |
| 344 | snd_soc_dapm_new_controls(codec, wm8960_dapm_widgets_capless, |
| 345 | ARRAY_SIZE(wm8960_dapm_widgets_capless)); |
| 346 | |
| 347 | snd_soc_dapm_add_routes(codec, audio_paths_capless, |
| 348 | ARRAY_SIZE(audio_paths_capless)); |
| 349 | } else { |
| 350 | snd_soc_dapm_new_controls(codec, wm8960_dapm_widgets_out3, |
| 351 | ARRAY_SIZE(wm8960_dapm_widgets_out3)); |
| 352 | |
| 353 | snd_soc_dapm_add_routes(codec, audio_paths_out3, |
| 354 | ARRAY_SIZE(audio_paths_out3)); |
| 355 | } |
| 356 | |
| 357 | /* We need to power up the headphone output stage out of |
| 358 | * sequence for capless mode. To save scanning the widget |
| 359 | * list each time to find the desired power state do so now |
| 360 | * and save the result. |
| 361 | */ |
| 362 | list_for_each_entry(w, &codec->dapm_widgets, list) { |
| 363 | if (strcmp(w->name, "LOUT1 PGA") == 0) |
| 364 | wm8960->lout1 = w; |
| 365 | if (strcmp(w->name, "ROUT1 PGA") == 0) |
| 366 | wm8960->rout1 = w; |
| 367 | if (strcmp(w->name, "OUT3 VMID") == 0) |
| 368 | wm8960->out3 = w; |
| 369 | } |
| 370 | |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static int wm8960_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 375 | unsigned int fmt) |
| 376 | { |
| 377 | struct snd_soc_codec *codec = codec_dai->codec; |
| 378 | u16 iface = 0; |
| 379 | |
| 380 | /* set master/slave audio interface */ |
| 381 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 382 | case SND_SOC_DAIFMT_CBM_CFM: |
| 383 | iface |= 0x0040; |
| 384 | break; |
| 385 | case SND_SOC_DAIFMT_CBS_CFS: |
| 386 | break; |
| 387 | default: |
| 388 | return -EINVAL; |
| 389 | } |
| 390 | |
| 391 | /* interface format */ |
| 392 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 393 | case SND_SOC_DAIFMT_I2S: |
| 394 | iface |= 0x0002; |
| 395 | break; |
| 396 | case SND_SOC_DAIFMT_RIGHT_J: |
| 397 | break; |
| 398 | case SND_SOC_DAIFMT_LEFT_J: |
| 399 | iface |= 0x0001; |
| 400 | break; |
| 401 | case SND_SOC_DAIFMT_DSP_A: |
| 402 | iface |= 0x0003; |
| 403 | break; |
| 404 | case SND_SOC_DAIFMT_DSP_B: |
| 405 | iface |= 0x0013; |
| 406 | break; |
| 407 | default: |
| 408 | return -EINVAL; |
| 409 | } |
| 410 | |
| 411 | /* clock inversion */ |
| 412 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 413 | case SND_SOC_DAIFMT_NB_NF: |
| 414 | break; |
| 415 | case SND_SOC_DAIFMT_IB_IF: |
| 416 | iface |= 0x0090; |
| 417 | break; |
| 418 | case SND_SOC_DAIFMT_IB_NF: |
| 419 | iface |= 0x0080; |
| 420 | break; |
| 421 | case SND_SOC_DAIFMT_NB_IF: |
| 422 | iface |= 0x0010; |
| 423 | break; |
| 424 | default: |
| 425 | return -EINVAL; |
| 426 | } |
| 427 | |
| 428 | /* set iface */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 429 | snd_soc_write(codec, WM8960_IFACE1, iface); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | static int wm8960_hw_params(struct snd_pcm_substream *substream, |
| 434 | struct snd_pcm_hw_params *params, |
| 435 | struct snd_soc_dai *dai) |
| 436 | { |
| 437 | struct snd_soc_pcm_runtime *rtd = substream->private_data; |
| 438 | struct snd_soc_device *socdev = rtd->socdev; |
| 439 | struct snd_soc_codec *codec = socdev->card->codec; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 440 | u16 iface = snd_soc_read(codec, WM8960_IFACE1) & 0xfff3; |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 441 | |
| 442 | /* bit size */ |
| 443 | switch (params_format(params)) { |
| 444 | case SNDRV_PCM_FORMAT_S16_LE: |
| 445 | break; |
| 446 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 447 | iface |= 0x0004; |
| 448 | break; |
| 449 | case SNDRV_PCM_FORMAT_S24_LE: |
| 450 | iface |= 0x0008; |
| 451 | break; |
| 452 | } |
| 453 | |
| 454 | /* set iface */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 455 | snd_soc_write(codec, WM8960_IFACE1, iface); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 456 | return 0; |
| 457 | } |
| 458 | |
| 459 | static int wm8960_mute(struct snd_soc_dai *dai, int mute) |
| 460 | { |
| 461 | struct snd_soc_codec *codec = dai->codec; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 462 | u16 mute_reg = snd_soc_read(codec, WM8960_DACCTL1) & 0xfff7; |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 463 | |
| 464 | if (mute) |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 465 | snd_soc_write(codec, WM8960_DACCTL1, mute_reg | 0x8); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 466 | else |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 467 | snd_soc_write(codec, WM8960_DACCTL1, mute_reg); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 468 | return 0; |
| 469 | } |
| 470 | |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 471 | static int wm8960_set_bias_level_out3(struct snd_soc_codec *codec, |
| 472 | enum snd_soc_bias_level level) |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 473 | { |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 474 | u16 reg; |
| 475 | |
| 476 | switch (level) { |
| 477 | case SND_SOC_BIAS_ON: |
| 478 | break; |
| 479 | |
| 480 | case SND_SOC_BIAS_PREPARE: |
| 481 | /* Set VMID to 2x50k */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 482 | reg = snd_soc_read(codec, WM8960_POWER1); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 483 | reg &= ~0x180; |
| 484 | reg |= 0x80; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 485 | snd_soc_write(codec, WM8960_POWER1, reg); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 486 | break; |
| 487 | |
| 488 | case SND_SOC_BIAS_STANDBY: |
| 489 | if (codec->bias_level == SND_SOC_BIAS_OFF) { |
| 490 | /* Enable anti-pop features */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 491 | snd_soc_write(codec, WM8960_APOP1, |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 492 | WM8960_POBCTRL | WM8960_SOFT_ST | |
| 493 | WM8960_BUFDCOPEN | WM8960_BUFIOEN); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 494 | |
| 495 | /* Enable & ramp VMID at 2x50k */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 496 | reg = snd_soc_read(codec, WM8960_POWER1); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 497 | reg |= 0x80; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 498 | snd_soc_write(codec, WM8960_POWER1, reg); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 499 | msleep(100); |
| 500 | |
| 501 | /* Enable VREF */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 502 | snd_soc_write(codec, WM8960_POWER1, reg | WM8960_VREF); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 503 | |
| 504 | /* Disable anti-pop features */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 505 | snd_soc_write(codec, WM8960_APOP1, WM8960_BUFIOEN); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* Set VMID to 2x250k */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 509 | reg = snd_soc_read(codec, WM8960_POWER1); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 510 | reg &= ~0x180; |
| 511 | reg |= 0x100; |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 512 | snd_soc_write(codec, WM8960_POWER1, reg); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 513 | break; |
| 514 | |
| 515 | case SND_SOC_BIAS_OFF: |
| 516 | /* Enable anti-pop features */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 517 | snd_soc_write(codec, WM8960_APOP1, |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 518 | WM8960_POBCTRL | WM8960_SOFT_ST | |
| 519 | WM8960_BUFDCOPEN | WM8960_BUFIOEN); |
| 520 | |
| 521 | /* Disable VMID and VREF, let them discharge */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 522 | snd_soc_write(codec, WM8960_POWER1, 0); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 523 | msleep(600); |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 524 | break; |
| 525 | } |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 526 | |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 527 | codec->bias_level = level; |
| 528 | |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int wm8960_set_bias_level_capless(struct snd_soc_codec *codec, |
| 533 | enum snd_soc_bias_level level) |
| 534 | { |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 535 | struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 536 | int reg; |
| 537 | |
| 538 | switch (level) { |
| 539 | case SND_SOC_BIAS_ON: |
| 540 | break; |
| 541 | |
| 542 | case SND_SOC_BIAS_PREPARE: |
| 543 | switch (codec->bias_level) { |
| 544 | case SND_SOC_BIAS_STANDBY: |
| 545 | /* Enable anti pop mode */ |
| 546 | snd_soc_update_bits(codec, WM8960_APOP1, |
| 547 | WM8960_POBCTRL | WM8960_SOFT_ST | |
| 548 | WM8960_BUFDCOPEN, |
| 549 | WM8960_POBCTRL | WM8960_SOFT_ST | |
| 550 | WM8960_BUFDCOPEN); |
| 551 | |
| 552 | /* Enable LOUT1, ROUT1 and OUT3 if they're enabled */ |
| 553 | reg = 0; |
| 554 | if (wm8960->lout1 && wm8960->lout1->power) |
| 555 | reg |= WM8960_PWR2_LOUT1; |
| 556 | if (wm8960->rout1 && wm8960->rout1->power) |
| 557 | reg |= WM8960_PWR2_ROUT1; |
| 558 | if (wm8960->out3 && wm8960->out3->power) |
| 559 | reg |= WM8960_PWR2_OUT3; |
| 560 | snd_soc_update_bits(codec, WM8960_POWER2, |
| 561 | WM8960_PWR2_LOUT1 | |
| 562 | WM8960_PWR2_ROUT1 | |
| 563 | WM8960_PWR2_OUT3, reg); |
| 564 | |
| 565 | /* Enable VMID at 2*50k */ |
| 566 | snd_soc_update_bits(codec, WM8960_POWER1, |
| 567 | WM8960_VMID_MASK, 0x80); |
| 568 | |
| 569 | /* Ramp */ |
| 570 | msleep(100); |
| 571 | |
| 572 | /* Enable VREF */ |
| 573 | snd_soc_update_bits(codec, WM8960_POWER1, |
| 574 | WM8960_VREF, WM8960_VREF); |
| 575 | |
| 576 | msleep(100); |
| 577 | break; |
| 578 | |
| 579 | case SND_SOC_BIAS_ON: |
| 580 | /* Enable anti-pop mode */ |
| 581 | snd_soc_update_bits(codec, WM8960_APOP1, |
| 582 | WM8960_POBCTRL | WM8960_SOFT_ST | |
| 583 | WM8960_BUFDCOPEN, |
| 584 | WM8960_POBCTRL | WM8960_SOFT_ST | |
| 585 | WM8960_BUFDCOPEN); |
| 586 | |
| 587 | /* Disable VMID and VREF */ |
| 588 | snd_soc_update_bits(codec, WM8960_POWER1, |
| 589 | WM8960_VREF | WM8960_VMID_MASK, 0); |
| 590 | break; |
| 591 | |
| 592 | default: |
| 593 | break; |
| 594 | } |
| 595 | break; |
| 596 | |
| 597 | case SND_SOC_BIAS_STANDBY: |
| 598 | switch (codec->bias_level) { |
| 599 | case SND_SOC_BIAS_PREPARE: |
| 600 | /* Disable HP discharge */ |
| 601 | snd_soc_update_bits(codec, WM8960_APOP2, |
| 602 | WM8960_DISOP | WM8960_DRES_MASK, |
| 603 | 0); |
| 604 | |
| 605 | /* Disable anti-pop features */ |
| 606 | snd_soc_update_bits(codec, WM8960_APOP1, |
| 607 | WM8960_POBCTRL | WM8960_SOFT_ST | |
| 608 | WM8960_BUFDCOPEN, |
| 609 | WM8960_POBCTRL | WM8960_SOFT_ST | |
| 610 | WM8960_BUFDCOPEN); |
| 611 | break; |
| 612 | |
| 613 | default: |
| 614 | break; |
| 615 | } |
| 616 | break; |
| 617 | |
| 618 | case SND_SOC_BIAS_OFF: |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 619 | break; |
| 620 | } |
| 621 | |
| 622 | codec->bias_level = level; |
| 623 | |
| 624 | return 0; |
| 625 | } |
| 626 | |
| 627 | /* PLL divisors */ |
| 628 | struct _pll_div { |
| 629 | u32 pre_div:1; |
| 630 | u32 n:4; |
| 631 | u32 k:24; |
| 632 | }; |
| 633 | |
| 634 | /* The size in bits of the pll divide multiplied by 10 |
| 635 | * to allow rounding later */ |
| 636 | #define FIXED_PLL_SIZE ((1 << 24) * 10) |
| 637 | |
| 638 | static int pll_factors(unsigned int source, unsigned int target, |
| 639 | struct _pll_div *pll_div) |
| 640 | { |
| 641 | unsigned long long Kpart; |
| 642 | unsigned int K, Ndiv, Nmod; |
| 643 | |
| 644 | pr_debug("WM8960 PLL: setting %dHz->%dHz\n", source, target); |
| 645 | |
| 646 | /* Scale up target to PLL operating frequency */ |
| 647 | target *= 4; |
| 648 | |
| 649 | Ndiv = target / source; |
| 650 | if (Ndiv < 6) { |
| 651 | source >>= 1; |
| 652 | pll_div->pre_div = 1; |
| 653 | Ndiv = target / source; |
| 654 | } else |
| 655 | pll_div->pre_div = 0; |
| 656 | |
| 657 | if ((Ndiv < 6) || (Ndiv > 12)) { |
| 658 | pr_err("WM8960 PLL: Unsupported N=%d\n", Ndiv); |
| 659 | return -EINVAL; |
| 660 | } |
| 661 | |
| 662 | pll_div->n = Ndiv; |
| 663 | Nmod = target % source; |
| 664 | Kpart = FIXED_PLL_SIZE * (long long)Nmod; |
| 665 | |
| 666 | do_div(Kpart, source); |
| 667 | |
| 668 | K = Kpart & 0xFFFFFFFF; |
| 669 | |
| 670 | /* Check if we need to round */ |
| 671 | if ((K % 10) >= 5) |
| 672 | K += 5; |
| 673 | |
| 674 | /* Move down to proper range now rounding is done */ |
| 675 | K /= 10; |
| 676 | |
| 677 | pll_div->k = K; |
| 678 | |
| 679 | pr_debug("WM8960 PLL: N=%x K=%x pre_div=%d\n", |
| 680 | pll_div->n, pll_div->k, pll_div->pre_div); |
| 681 | |
| 682 | return 0; |
| 683 | } |
| 684 | |
Mark Brown | 8548803 | 2009-09-05 18:52:16 +0100 | [diff] [blame] | 685 | static int wm8960_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id, |
| 686 | int source, unsigned int freq_in, unsigned int freq_out) |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 687 | { |
| 688 | struct snd_soc_codec *codec = codec_dai->codec; |
| 689 | u16 reg; |
| 690 | static struct _pll_div pll_div; |
| 691 | int ret; |
| 692 | |
| 693 | if (freq_in && freq_out) { |
| 694 | ret = pll_factors(freq_in, freq_out, &pll_div); |
| 695 | if (ret != 0) |
| 696 | return ret; |
| 697 | } |
| 698 | |
| 699 | /* Disable the PLL: even if we are changing the frequency the |
| 700 | * PLL needs to be disabled while we do so. */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 701 | snd_soc_write(codec, WM8960_CLOCK1, |
| 702 | snd_soc_read(codec, WM8960_CLOCK1) & ~1); |
| 703 | snd_soc_write(codec, WM8960_POWER2, |
| 704 | snd_soc_read(codec, WM8960_POWER2) & ~1); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 705 | |
| 706 | if (!freq_in || !freq_out) |
| 707 | return 0; |
| 708 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 709 | reg = snd_soc_read(codec, WM8960_PLL1) & ~0x3f; |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 710 | reg |= pll_div.pre_div << 4; |
| 711 | reg |= pll_div.n; |
| 712 | |
| 713 | if (pll_div.k) { |
| 714 | reg |= 0x20; |
| 715 | |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 716 | snd_soc_write(codec, WM8960_PLL2, (pll_div.k >> 18) & 0x3f); |
| 717 | snd_soc_write(codec, WM8960_PLL3, (pll_div.k >> 9) & 0x1ff); |
| 718 | snd_soc_write(codec, WM8960_PLL4, pll_div.k & 0x1ff); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 719 | } |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 720 | snd_soc_write(codec, WM8960_PLL1, reg); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 721 | |
| 722 | /* Turn it on */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 723 | snd_soc_write(codec, WM8960_POWER2, |
| 724 | snd_soc_read(codec, WM8960_POWER2) | 1); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 725 | msleep(250); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 726 | snd_soc_write(codec, WM8960_CLOCK1, |
| 727 | snd_soc_read(codec, WM8960_CLOCK1) | 1); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 728 | |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | static int wm8960_set_dai_clkdiv(struct snd_soc_dai *codec_dai, |
| 733 | int div_id, int div) |
| 734 | { |
| 735 | struct snd_soc_codec *codec = codec_dai->codec; |
| 736 | u16 reg; |
| 737 | |
| 738 | switch (div_id) { |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 739 | case WM8960_SYSCLKDIV: |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 740 | reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1f9; |
| 741 | snd_soc_write(codec, WM8960_CLOCK1, reg | div); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 742 | break; |
| 743 | case WM8960_DACDIV: |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 744 | reg = snd_soc_read(codec, WM8960_CLOCK1) & 0x1c7; |
| 745 | snd_soc_write(codec, WM8960_CLOCK1, reg | div); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 746 | break; |
| 747 | case WM8960_OPCLKDIV: |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 748 | reg = snd_soc_read(codec, WM8960_PLL1) & 0x03f; |
| 749 | snd_soc_write(codec, WM8960_PLL1, reg | div); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 750 | break; |
| 751 | case WM8960_DCLKDIV: |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 752 | reg = snd_soc_read(codec, WM8960_CLOCK2) & 0x03f; |
| 753 | snd_soc_write(codec, WM8960_CLOCK2, reg | div); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 754 | break; |
| 755 | case WM8960_TOCLKSEL: |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 756 | reg = snd_soc_read(codec, WM8960_ADDCTL1) & 0x1fd; |
| 757 | snd_soc_write(codec, WM8960_ADDCTL1, reg | div); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 758 | break; |
| 759 | default: |
| 760 | return -EINVAL; |
| 761 | } |
| 762 | |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | #define WM8960_RATES SNDRV_PCM_RATE_8000_48000 |
| 767 | |
| 768 | #define WM8960_FORMATS \ |
| 769 | (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \ |
| 770 | SNDRV_PCM_FMTBIT_S24_LE) |
| 771 | |
| 772 | static struct snd_soc_dai_ops wm8960_dai_ops = { |
| 773 | .hw_params = wm8960_hw_params, |
| 774 | .digital_mute = wm8960_mute, |
| 775 | .set_fmt = wm8960_set_dai_fmt, |
| 776 | .set_clkdiv = wm8960_set_dai_clkdiv, |
| 777 | .set_pll = wm8960_set_dai_pll, |
| 778 | }; |
| 779 | |
| 780 | struct snd_soc_dai wm8960_dai = { |
| 781 | .name = "WM8960", |
| 782 | .playback = { |
| 783 | .stream_name = "Playback", |
| 784 | .channels_min = 1, |
| 785 | .channels_max = 2, |
| 786 | .rates = WM8960_RATES, |
| 787 | .formats = WM8960_FORMATS,}, |
| 788 | .capture = { |
| 789 | .stream_name = "Capture", |
| 790 | .channels_min = 1, |
| 791 | .channels_max = 2, |
| 792 | .rates = WM8960_RATES, |
| 793 | .formats = WM8960_FORMATS,}, |
| 794 | .ops = &wm8960_dai_ops, |
| 795 | .symmetric_rates = 1, |
| 796 | }; |
| 797 | EXPORT_SYMBOL_GPL(wm8960_dai); |
| 798 | |
| 799 | static int wm8960_suspend(struct platform_device *pdev, pm_message_t state) |
| 800 | { |
| 801 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 802 | struct snd_soc_codec *codec = socdev->card->codec; |
| 803 | |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 804 | codec->set_bias_level(codec, SND_SOC_BIAS_OFF); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | static int wm8960_resume(struct platform_device *pdev) |
| 809 | { |
| 810 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 811 | struct snd_soc_codec *codec = socdev->card->codec; |
| 812 | int i; |
| 813 | u8 data[2]; |
| 814 | u16 *cache = codec->reg_cache; |
| 815 | |
| 816 | /* Sync reg_cache with the hardware */ |
| 817 | for (i = 0; i < ARRAY_SIZE(wm8960_reg); i++) { |
| 818 | data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001); |
| 819 | data[1] = cache[i] & 0x00ff; |
| 820 | codec->hw_write(codec->control_data, data, 2); |
| 821 | } |
| 822 | |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 823 | codec->set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
Mark Brown | 29e189c | 2010-05-07 20:30:00 +0100 | [diff] [blame] | 824 | |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | static struct snd_soc_codec *wm8960_codec; |
| 829 | |
| 830 | static int wm8960_probe(struct platform_device *pdev) |
| 831 | { |
| 832 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 833 | struct snd_soc_codec *codec; |
| 834 | int ret = 0; |
| 835 | |
| 836 | if (wm8960_codec == NULL) { |
| 837 | dev_err(&pdev->dev, "Codec device not registered\n"); |
| 838 | return -ENODEV; |
| 839 | } |
| 840 | |
| 841 | socdev->card->codec = wm8960_codec; |
| 842 | codec = wm8960_codec; |
| 843 | |
| 844 | /* register pcms */ |
| 845 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); |
| 846 | if (ret < 0) { |
| 847 | dev_err(codec->dev, "failed to create pcms: %d\n", ret); |
| 848 | goto pcm_err; |
| 849 | } |
| 850 | |
| 851 | snd_soc_add_controls(codec, wm8960_snd_controls, |
| 852 | ARRAY_SIZE(wm8960_snd_controls)); |
| 853 | wm8960_add_widgets(codec); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 854 | |
| 855 | return ret; |
| 856 | |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 857 | pcm_err: |
| 858 | return ret; |
| 859 | } |
| 860 | |
| 861 | /* power down chip */ |
| 862 | static int wm8960_remove(struct platform_device *pdev) |
| 863 | { |
| 864 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 865 | |
| 866 | snd_soc_free_pcms(socdev); |
| 867 | snd_soc_dapm_free(socdev); |
| 868 | |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | struct snd_soc_codec_device soc_codec_dev_wm8960 = { |
| 873 | .probe = wm8960_probe, |
| 874 | .remove = wm8960_remove, |
| 875 | .suspend = wm8960_suspend, |
| 876 | .resume = wm8960_resume, |
| 877 | }; |
| 878 | EXPORT_SYMBOL_GPL(soc_codec_dev_wm8960); |
| 879 | |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 880 | static int wm8960_register(struct wm8960_priv *wm8960, |
| 881 | enum snd_soc_control_type control) |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 882 | { |
| 883 | struct wm8960_data *pdata = wm8960->codec.dev->platform_data; |
| 884 | struct snd_soc_codec *codec = &wm8960->codec; |
| 885 | int ret; |
| 886 | u16 reg; |
| 887 | |
| 888 | if (wm8960_codec) { |
| 889 | dev_err(codec->dev, "Another WM8960 is registered\n"); |
Mark Brown | 1a01417 | 2009-07-05 15:47:03 +0100 | [diff] [blame] | 890 | ret = -EINVAL; |
| 891 | goto err; |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 892 | } |
| 893 | |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 894 | codec->set_bias_level = wm8960_set_bias_level_out3; |
| 895 | |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 896 | if (!pdata) { |
| 897 | dev_warn(codec->dev, "No platform data supplied\n"); |
| 898 | } else { |
| 899 | if (pdata->dres > WM8960_DRES_MAX) { |
| 900 | dev_err(codec->dev, "Invalid DRES: %d\n", pdata->dres); |
| 901 | pdata->dres = 0; |
| 902 | } |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 903 | |
| 904 | if (pdata->capless) |
| 905 | codec->set_bias_level = wm8960_set_bias_level_capless; |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 906 | } |
| 907 | |
| 908 | mutex_init(&codec->mutex); |
| 909 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 910 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 911 | |
Mark Brown | b2c812e | 2010-04-14 15:35:19 +0900 | [diff] [blame] | 912 | snd_soc_codec_set_drvdata(codec, wm8960); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 913 | codec->name = "WM8960"; |
| 914 | codec->owner = THIS_MODULE; |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 915 | codec->bias_level = SND_SOC_BIAS_OFF; |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 916 | codec->dai = &wm8960_dai; |
| 917 | codec->num_dai = 1; |
| 918 | codec->reg_cache_size = WM8960_CACHEREGNUM; |
| 919 | codec->reg_cache = &wm8960->reg_cache; |
| 920 | |
| 921 | memcpy(codec->reg_cache, wm8960_reg, sizeof(wm8960_reg)); |
| 922 | |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 923 | ret = snd_soc_codec_set_cache_io(codec, 7, 9, control); |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 924 | if (ret < 0) { |
| 925 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); |
| 926 | goto err; |
| 927 | } |
| 928 | |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 929 | ret = wm8960_reset(codec); |
| 930 | if (ret < 0) { |
| 931 | dev_err(codec->dev, "Failed to issue reset\n"); |
Mark Brown | 1a01417 | 2009-07-05 15:47:03 +0100 | [diff] [blame] | 932 | goto err; |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | wm8960_dai.dev = codec->dev; |
| 936 | |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 937 | codec->set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 938 | |
| 939 | /* Latch the update bits */ |
Mark Brown | 17a52fd | 2009-07-05 17:24:50 +0100 | [diff] [blame] | 940 | reg = snd_soc_read(codec, WM8960_LINVOL); |
| 941 | snd_soc_write(codec, WM8960_LINVOL, reg | 0x100); |
| 942 | reg = snd_soc_read(codec, WM8960_RINVOL); |
| 943 | snd_soc_write(codec, WM8960_RINVOL, reg | 0x100); |
| 944 | reg = snd_soc_read(codec, WM8960_LADC); |
| 945 | snd_soc_write(codec, WM8960_LADC, reg | 0x100); |
| 946 | reg = snd_soc_read(codec, WM8960_RADC); |
| 947 | snd_soc_write(codec, WM8960_RADC, reg | 0x100); |
| 948 | reg = snd_soc_read(codec, WM8960_LDAC); |
| 949 | snd_soc_write(codec, WM8960_LDAC, reg | 0x100); |
| 950 | reg = snd_soc_read(codec, WM8960_RDAC); |
| 951 | snd_soc_write(codec, WM8960_RDAC, reg | 0x100); |
| 952 | reg = snd_soc_read(codec, WM8960_LOUT1); |
| 953 | snd_soc_write(codec, WM8960_LOUT1, reg | 0x100); |
| 954 | reg = snd_soc_read(codec, WM8960_ROUT1); |
| 955 | snd_soc_write(codec, WM8960_ROUT1, reg | 0x100); |
| 956 | reg = snd_soc_read(codec, WM8960_LOUT2); |
| 957 | snd_soc_write(codec, WM8960_LOUT2, reg | 0x100); |
| 958 | reg = snd_soc_read(codec, WM8960_ROUT2); |
| 959 | snd_soc_write(codec, WM8960_ROUT2, reg | 0x100); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 960 | |
| 961 | wm8960_codec = codec; |
| 962 | |
| 963 | ret = snd_soc_register_codec(codec); |
| 964 | if (ret != 0) { |
| 965 | dev_err(codec->dev, "Failed to register codec: %d\n", ret); |
Mark Brown | 1a01417 | 2009-07-05 15:47:03 +0100 | [diff] [blame] | 966 | goto err; |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | ret = snd_soc_register_dai(&wm8960_dai); |
| 970 | if (ret != 0) { |
| 971 | dev_err(codec->dev, "Failed to register DAI: %d\n", ret); |
Mark Brown | 1a01417 | 2009-07-05 15:47:03 +0100 | [diff] [blame] | 972 | goto err_codec; |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | return 0; |
Mark Brown | 1a01417 | 2009-07-05 15:47:03 +0100 | [diff] [blame] | 976 | |
| 977 | err_codec: |
| 978 | snd_soc_unregister_codec(codec); |
| 979 | err: |
| 980 | kfree(wm8960); |
| 981 | return ret; |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | static void wm8960_unregister(struct wm8960_priv *wm8960) |
| 985 | { |
Mark Brown | 913d7b4 | 2010-03-03 13:47:03 +0000 | [diff] [blame] | 986 | wm8960->codec.set_bias_level(&wm8960->codec, SND_SOC_BIAS_OFF); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 987 | snd_soc_unregister_dai(&wm8960_dai); |
| 988 | snd_soc_unregister_codec(&wm8960->codec); |
| 989 | kfree(wm8960); |
| 990 | wm8960_codec = NULL; |
| 991 | } |
| 992 | |
| 993 | static __devinit int wm8960_i2c_probe(struct i2c_client *i2c, |
| 994 | const struct i2c_device_id *id) |
| 995 | { |
| 996 | struct wm8960_priv *wm8960; |
| 997 | struct snd_soc_codec *codec; |
| 998 | |
| 999 | wm8960 = kzalloc(sizeof(struct wm8960_priv), GFP_KERNEL); |
| 1000 | if (wm8960 == NULL) |
| 1001 | return -ENOMEM; |
| 1002 | |
| 1003 | codec = &wm8960->codec; |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 1004 | |
| 1005 | i2c_set_clientdata(i2c, wm8960); |
| 1006 | codec->control_data = i2c; |
| 1007 | |
| 1008 | codec->dev = &i2c->dev; |
| 1009 | |
Mark Brown | 7084a42 | 2009-07-10 22:24:27 +0100 | [diff] [blame] | 1010 | return wm8960_register(wm8960, SND_SOC_I2C); |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | static __devexit int wm8960_i2c_remove(struct i2c_client *client) |
| 1014 | { |
| 1015 | struct wm8960_priv *wm8960 = i2c_get_clientdata(client); |
| 1016 | wm8960_unregister(wm8960); |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | static const struct i2c_device_id wm8960_i2c_id[] = { |
| 1021 | { "wm8960", 0 }, |
| 1022 | { } |
| 1023 | }; |
| 1024 | MODULE_DEVICE_TABLE(i2c, wm8960_i2c_id); |
| 1025 | |
| 1026 | static struct i2c_driver wm8960_i2c_driver = { |
| 1027 | .driver = { |
Mark Brown | a24d62d | 2010-03-01 20:00:04 +0000 | [diff] [blame] | 1028 | .name = "wm8960", |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 1029 | .owner = THIS_MODULE, |
| 1030 | }, |
| 1031 | .probe = wm8960_i2c_probe, |
| 1032 | .remove = __devexit_p(wm8960_i2c_remove), |
Mark Brown | f2644a2 | 2009-04-07 19:20:14 +0100 | [diff] [blame] | 1033 | .id_table = wm8960_i2c_id, |
| 1034 | }; |
| 1035 | |
| 1036 | static int __init wm8960_modinit(void) |
| 1037 | { |
| 1038 | int ret; |
| 1039 | |
| 1040 | ret = i2c_add_driver(&wm8960_i2c_driver); |
| 1041 | if (ret != 0) { |
| 1042 | printk(KERN_ERR "Failed to register WM8960 I2C driver: %d\n", |
| 1043 | ret); |
| 1044 | } |
| 1045 | |
| 1046 | return ret; |
| 1047 | } |
| 1048 | module_init(wm8960_modinit); |
| 1049 | |
| 1050 | static void __exit wm8960_exit(void) |
| 1051 | { |
| 1052 | i2c_del_driver(&wm8960_i2c_driver); |
| 1053 | } |
| 1054 | module_exit(wm8960_exit); |
| 1055 | |
| 1056 | |
| 1057 | MODULE_DESCRIPTION("ASoC WM8960 driver"); |
| 1058 | MODULE_AUTHOR("Liam Girdwood"); |
| 1059 | MODULE_LICENSE("GPL"); |