Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * wm8974.c -- WM8974 ALSA Soc Audio driver |
| 3 | * |
Mark Brown | 8b83a19 | 2009-06-30 19:37:02 +0100 | [diff] [blame] | 4 | * Copyright 2006-2009 Wolfson Microelectronics PLC. |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 5 | * |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 6 | * Author: Liam Girdwood <linux@wolfsonmicro.com> |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +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> |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 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> |
| 21 | #include <sound/core.h> |
| 22 | #include <sound/pcm.h> |
| 23 | #include <sound/pcm_params.h> |
| 24 | #include <sound/soc.h> |
| 25 | #include <sound/soc-dapm.h> |
| 26 | #include <sound/initval.h> |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 27 | #include <sound/tlv.h> |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 28 | |
| 29 | #include "wm8974.h" |
| 30 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 31 | static const u16 wm8974_reg[WM8974_CACHEREGNUM] = { |
Mark Brown | 1a55b3f | 2009-05-23 11:31:40 +0100 | [diff] [blame] | 32 | 0x0000, 0x0000, 0x0000, 0x0000, |
| 33 | 0x0050, 0x0000, 0x0140, 0x0000, |
| 34 | 0x0000, 0x0000, 0x0000, 0x00ff, |
| 35 | 0x0000, 0x0000, 0x0100, 0x00ff, |
| 36 | 0x0000, 0x0000, 0x012c, 0x002c, |
| 37 | 0x002c, 0x002c, 0x002c, 0x0000, |
| 38 | 0x0032, 0x0000, 0x0000, 0x0000, |
| 39 | 0x0000, 0x0000, 0x0000, 0x0000, |
| 40 | 0x0038, 0x000b, 0x0032, 0x0000, |
| 41 | 0x0008, 0x000c, 0x0093, 0x00e9, |
| 42 | 0x0000, 0x0000, 0x0000, 0x0000, |
| 43 | 0x0003, 0x0010, 0x0000, 0x0000, |
| 44 | 0x0000, 0x0002, 0x0000, 0x0000, |
| 45 | 0x0000, 0x0000, 0x0039, 0x0000, |
| 46 | 0x0000, |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 47 | }; |
| 48 | |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 49 | #define WM8974_POWER1_BIASEN 0x08 |
| 50 | #define WM8974_POWER1_BUFIOEN 0x10 |
| 51 | |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 52 | struct wm8974_priv { |
| 53 | struct snd_soc_codec codec; |
| 54 | u16 reg_cache[WM8974_CACHEREGNUM]; |
| 55 | }; |
| 56 | |
| 57 | static struct snd_soc_codec *wm8974_codec; |
| 58 | |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 59 | #define wm8974_reset(c) snd_soc_write(c, WM8974_RESET, 0) |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 60 | |
| 61 | static const char *wm8974_companding[] = {"Off", "NC", "u-law", "A-law" }; |
| 62 | static const char *wm8974_deemp[] = {"None", "32kHz", "44.1kHz", "48kHz" }; |
| 63 | static const char *wm8974_eqmode[] = {"Capture", "Playback" }; |
| 64 | static const char *wm8974_bw[] = {"Narrow", "Wide" }; |
| 65 | static const char *wm8974_eq1[] = {"80Hz", "105Hz", "135Hz", "175Hz" }; |
| 66 | static const char *wm8974_eq2[] = {"230Hz", "300Hz", "385Hz", "500Hz" }; |
| 67 | static const char *wm8974_eq3[] = {"650Hz", "850Hz", "1.1kHz", "1.4kHz" }; |
| 68 | static const char *wm8974_eq4[] = {"1.8kHz", "2.4kHz", "3.2kHz", "4.1kHz" }; |
| 69 | static const char *wm8974_eq5[] = {"5.3kHz", "6.9kHz", "9kHz", "11.7kHz" }; |
| 70 | static const char *wm8974_alc[] = {"ALC", "Limiter" }; |
| 71 | |
| 72 | static const struct soc_enum wm8974_enum[] = { |
| 73 | SOC_ENUM_SINGLE(WM8974_COMP, 1, 4, wm8974_companding), /* adc */ |
| 74 | SOC_ENUM_SINGLE(WM8974_COMP, 3, 4, wm8974_companding), /* dac */ |
| 75 | SOC_ENUM_SINGLE(WM8974_DAC, 4, 4, wm8974_deemp), |
| 76 | SOC_ENUM_SINGLE(WM8974_EQ1, 8, 2, wm8974_eqmode), |
| 77 | |
| 78 | SOC_ENUM_SINGLE(WM8974_EQ1, 5, 4, wm8974_eq1), |
| 79 | SOC_ENUM_SINGLE(WM8974_EQ2, 8, 2, wm8974_bw), |
| 80 | SOC_ENUM_SINGLE(WM8974_EQ2, 5, 4, wm8974_eq2), |
| 81 | SOC_ENUM_SINGLE(WM8974_EQ3, 8, 2, wm8974_bw), |
| 82 | |
| 83 | SOC_ENUM_SINGLE(WM8974_EQ3, 5, 4, wm8974_eq3), |
| 84 | SOC_ENUM_SINGLE(WM8974_EQ4, 8, 2, wm8974_bw), |
| 85 | SOC_ENUM_SINGLE(WM8974_EQ4, 5, 4, wm8974_eq4), |
| 86 | SOC_ENUM_SINGLE(WM8974_EQ5, 8, 2, wm8974_bw), |
| 87 | |
| 88 | SOC_ENUM_SINGLE(WM8974_EQ5, 5, 4, wm8974_eq5), |
| 89 | SOC_ENUM_SINGLE(WM8974_ALC3, 8, 2, wm8974_alc), |
| 90 | }; |
| 91 | |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 92 | static const char *wm8974_auxmode_text[] = { "Buffer", "Mixer" }; |
| 93 | |
| 94 | static const struct soc_enum wm8974_auxmode = |
| 95 | SOC_ENUM_SINGLE(WM8974_INPUT, 3, 2, wm8974_auxmode_text); |
| 96 | |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 97 | static const DECLARE_TLV_DB_SCALE(digital_tlv, -12750, 50, 1); |
| 98 | static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0); |
| 99 | static const DECLARE_TLV_DB_SCALE(inpga_tlv, -1200, 75, 0); |
| 100 | static const DECLARE_TLV_DB_SCALE(spk_tlv, -5700, 100, 0); |
| 101 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 102 | static const struct snd_kcontrol_new wm8974_snd_controls[] = { |
| 103 | |
| 104 | SOC_SINGLE("Digital Loopback Switch", WM8974_COMP, 0, 1, 0), |
| 105 | |
| 106 | SOC_ENUM("DAC Companding", wm8974_enum[1]), |
| 107 | SOC_ENUM("ADC Companding", wm8974_enum[0]), |
| 108 | |
| 109 | SOC_ENUM("Playback De-emphasis", wm8974_enum[2]), |
| 110 | SOC_SINGLE("DAC Inversion Switch", WM8974_DAC, 0, 1, 0), |
| 111 | |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 112 | SOC_SINGLE_TLV("PCM Volume", WM8974_DACVOL, 0, 255, 0, digital_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 113 | |
| 114 | SOC_SINGLE("High Pass Filter Switch", WM8974_ADC, 8, 1, 0), |
| 115 | SOC_SINGLE("High Pass Cut Off", WM8974_ADC, 4, 7, 0), |
javier Martin | 25cbf46 | 2009-07-21 11:15:06 +0200 | [diff] [blame] | 116 | SOC_SINGLE("ADC Inversion Switch", WM8974_ADC, 0, 1, 0), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 117 | |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 118 | SOC_SINGLE_TLV("Capture Volume", WM8974_ADCVOL, 0, 255, 0, digital_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 119 | |
| 120 | SOC_ENUM("Equaliser Function", wm8974_enum[3]), |
| 121 | SOC_ENUM("EQ1 Cut Off", wm8974_enum[4]), |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 122 | SOC_SINGLE_TLV("EQ1 Volume", WM8974_EQ1, 0, 24, 1, eq_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 123 | |
| 124 | SOC_ENUM("Equaliser EQ2 Bandwith", wm8974_enum[5]), |
| 125 | SOC_ENUM("EQ2 Cut Off", wm8974_enum[6]), |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 126 | SOC_SINGLE_TLV("EQ2 Volume", WM8974_EQ2, 0, 24, 1, eq_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 127 | |
| 128 | SOC_ENUM("Equaliser EQ3 Bandwith", wm8974_enum[7]), |
| 129 | SOC_ENUM("EQ3 Cut Off", wm8974_enum[8]), |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 130 | SOC_SINGLE_TLV("EQ3 Volume", WM8974_EQ3, 0, 24, 1, eq_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 131 | |
| 132 | SOC_ENUM("Equaliser EQ4 Bandwith", wm8974_enum[9]), |
| 133 | SOC_ENUM("EQ4 Cut Off", wm8974_enum[10]), |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 134 | SOC_SINGLE_TLV("EQ4 Volume", WM8974_EQ4, 0, 24, 1, eq_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 135 | |
| 136 | SOC_ENUM("Equaliser EQ5 Bandwith", wm8974_enum[11]), |
| 137 | SOC_ENUM("EQ5 Cut Off", wm8974_enum[12]), |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 138 | SOC_SINGLE_TLV("EQ5 Volume", WM8974_EQ5, 0, 24, 1, eq_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 139 | |
| 140 | SOC_SINGLE("DAC Playback Limiter Switch", WM8974_DACLIM1, 8, 1, 0), |
| 141 | SOC_SINGLE("DAC Playback Limiter Decay", WM8974_DACLIM1, 4, 15, 0), |
| 142 | SOC_SINGLE("DAC Playback Limiter Attack", WM8974_DACLIM1, 0, 15, 0), |
| 143 | |
| 144 | SOC_SINGLE("DAC Playback Limiter Threshold", WM8974_DACLIM2, 4, 7, 0), |
| 145 | SOC_SINGLE("DAC Playback Limiter Boost", WM8974_DACLIM2, 0, 15, 0), |
| 146 | |
| 147 | SOC_SINGLE("ALC Enable Switch", WM8974_ALC1, 8, 1, 0), |
| 148 | SOC_SINGLE("ALC Capture Max Gain", WM8974_ALC1, 3, 7, 0), |
| 149 | SOC_SINGLE("ALC Capture Min Gain", WM8974_ALC1, 0, 7, 0), |
| 150 | |
| 151 | SOC_SINGLE("ALC Capture ZC Switch", WM8974_ALC2, 8, 1, 0), |
| 152 | SOC_SINGLE("ALC Capture Hold", WM8974_ALC2, 4, 7, 0), |
| 153 | SOC_SINGLE("ALC Capture Target", WM8974_ALC2, 0, 15, 0), |
| 154 | |
| 155 | SOC_ENUM("ALC Capture Mode", wm8974_enum[13]), |
| 156 | SOC_SINGLE("ALC Capture Decay", WM8974_ALC3, 4, 15, 0), |
| 157 | SOC_SINGLE("ALC Capture Attack", WM8974_ALC3, 0, 15, 0), |
| 158 | |
| 159 | SOC_SINGLE("ALC Capture Noise Gate Switch", WM8974_NGATE, 3, 1, 0), |
| 160 | SOC_SINGLE("ALC Capture Noise Gate Threshold", WM8974_NGATE, 0, 7, 0), |
| 161 | |
| 162 | SOC_SINGLE("Capture PGA ZC Switch", WM8974_INPPGA, 7, 1, 0), |
Mark Brown | a5f8d2f | 2009-06-30 19:30:33 +0100 | [diff] [blame] | 163 | SOC_SINGLE_TLV("Capture PGA Volume", WM8974_INPPGA, 0, 63, 0, inpga_tlv), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 164 | |
| 165 | SOC_SINGLE("Speaker Playback ZC Switch", WM8974_SPKVOL, 7, 1, 0), |
| 166 | SOC_SINGLE("Speaker Playback Switch", WM8974_SPKVOL, 6, 1, 1), |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 167 | SOC_SINGLE_TLV("Speaker Playback Volume", WM8974_SPKVOL, 0, 63, 0, spk_tlv), |
| 168 | |
| 169 | SOC_ENUM("Aux Mode", wm8974_auxmode), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 170 | |
| 171 | SOC_SINGLE("Capture Boost(+20dB)", WM8974_ADCBOOST, 8, 1, 0), |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 172 | SOC_SINGLE("Mono Playback Switch", WM8974_MONOMIX, 6, 1, 1), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 173 | }; |
| 174 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 175 | /* Speaker Output Mixer */ |
| 176 | static const struct snd_kcontrol_new wm8974_speaker_mixer_controls[] = { |
| 177 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8974_SPKMIX, 1, 1, 0), |
| 178 | SOC_DAPM_SINGLE("Aux Playback Switch", WM8974_SPKMIX, 5, 1, 0), |
| 179 | SOC_DAPM_SINGLE("PCM Playback Switch", WM8974_SPKMIX, 0, 1, 1), |
| 180 | }; |
| 181 | |
| 182 | /* Mono Output Mixer */ |
| 183 | static const struct snd_kcontrol_new wm8974_mono_mixer_controls[] = { |
| 184 | SOC_DAPM_SINGLE("Line Bypass Switch", WM8974_MONOMIX, 1, 1, 0), |
| 185 | SOC_DAPM_SINGLE("Aux Playback Switch", WM8974_MONOMIX, 2, 1, 0), |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 186 | SOC_DAPM_SINGLE("PCM Playback Switch", WM8974_MONOMIX, 0, 1, 0), |
| 187 | }; |
| 188 | |
| 189 | /* Boost mixer */ |
| 190 | static const struct snd_kcontrol_new wm8974_boost_mixer[] = { |
| 191 | SOC_DAPM_SINGLE("Aux Switch", WM8974_INPPGA, 6, 1, 0), |
| 192 | }; |
| 193 | |
| 194 | /* Input PGA */ |
| 195 | static const struct snd_kcontrol_new wm8974_inpga[] = { |
| 196 | SOC_DAPM_SINGLE("Aux Switch", WM8974_INPUT, 2, 1, 0), |
| 197 | SOC_DAPM_SINGLE("MicN Switch", WM8974_INPUT, 1, 1, 0), |
| 198 | SOC_DAPM_SINGLE("MicP Switch", WM8974_INPUT, 0, 1, 0), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | /* AUX Input boost vol */ |
| 202 | static const struct snd_kcontrol_new wm8974_aux_boost_controls = |
| 203 | SOC_DAPM_SINGLE("Aux Volume", WM8974_ADCBOOST, 0, 7, 0); |
| 204 | |
| 205 | /* Mic Input boost vol */ |
| 206 | static const struct snd_kcontrol_new wm8974_mic_boost_controls = |
| 207 | SOC_DAPM_SINGLE("Mic Volume", WM8974_ADCBOOST, 4, 7, 0); |
| 208 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 209 | static const struct snd_soc_dapm_widget wm8974_dapm_widgets[] = { |
| 210 | SND_SOC_DAPM_MIXER("Speaker Mixer", WM8974_POWER3, 2, 0, |
| 211 | &wm8974_speaker_mixer_controls[0], |
| 212 | ARRAY_SIZE(wm8974_speaker_mixer_controls)), |
| 213 | SND_SOC_DAPM_MIXER("Mono Mixer", WM8974_POWER3, 3, 0, |
| 214 | &wm8974_mono_mixer_controls[0], |
| 215 | ARRAY_SIZE(wm8974_mono_mixer_controls)), |
| 216 | SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8974_POWER3, 0, 0), |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 217 | SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8974_POWER2, 0, 0), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 218 | SND_SOC_DAPM_PGA("Aux Input", WM8974_POWER1, 6, 0, NULL, 0), |
| 219 | SND_SOC_DAPM_PGA("SpkN Out", WM8974_POWER3, 5, 0, NULL, 0), |
| 220 | SND_SOC_DAPM_PGA("SpkP Out", WM8974_POWER3, 6, 0, NULL, 0), |
| 221 | SND_SOC_DAPM_PGA("Mono Out", WM8974_POWER3, 7, 0, NULL, 0), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 222 | |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 223 | SND_SOC_DAPM_MIXER("Input PGA", WM8974_POWER2, 2, 0, wm8974_inpga, |
| 224 | ARRAY_SIZE(wm8974_inpga)), |
| 225 | SND_SOC_DAPM_MIXER("Boost Mixer", WM8974_POWER2, 4, 0, |
| 226 | wm8974_boost_mixer, ARRAY_SIZE(wm8974_boost_mixer)), |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 227 | |
| 228 | SND_SOC_DAPM_MICBIAS("Mic Bias", WM8974_POWER1, 4, 0), |
| 229 | |
| 230 | SND_SOC_DAPM_INPUT("MICN"), |
| 231 | SND_SOC_DAPM_INPUT("MICP"), |
| 232 | SND_SOC_DAPM_INPUT("AUX"), |
| 233 | SND_SOC_DAPM_OUTPUT("MONOOUT"), |
| 234 | SND_SOC_DAPM_OUTPUT("SPKOUTP"), |
| 235 | SND_SOC_DAPM_OUTPUT("SPKOUTN"), |
| 236 | }; |
| 237 | |
| 238 | static const struct snd_soc_dapm_route audio_map[] = { |
| 239 | /* Mono output mixer */ |
| 240 | {"Mono Mixer", "PCM Playback Switch", "DAC"}, |
| 241 | {"Mono Mixer", "Aux Playback Switch", "Aux Input"}, |
| 242 | {"Mono Mixer", "Line Bypass Switch", "Boost Mixer"}, |
| 243 | |
| 244 | /* Speaker output mixer */ |
| 245 | {"Speaker Mixer", "PCM Playback Switch", "DAC"}, |
| 246 | {"Speaker Mixer", "Aux Playback Switch", "Aux Input"}, |
| 247 | {"Speaker Mixer", "Line Bypass Switch", "Boost Mixer"}, |
| 248 | |
| 249 | /* Outputs */ |
| 250 | {"Mono Out", NULL, "Mono Mixer"}, |
| 251 | {"MONOOUT", NULL, "Mono Out"}, |
| 252 | {"SpkN Out", NULL, "Speaker Mixer"}, |
| 253 | {"SpkP Out", NULL, "Speaker Mixer"}, |
| 254 | {"SPKOUTN", NULL, "SpkN Out"}, |
| 255 | {"SPKOUTP", NULL, "SpkP Out"}, |
| 256 | |
| 257 | /* Boost Mixer */ |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 258 | {"ADC", NULL, "Boost Mixer"}, |
| 259 | {"Boost Mixer", "Aux Switch", "Aux Input"}, |
| 260 | {"Boost Mixer", NULL, "Input PGA"}, |
| 261 | {"Boost Mixer", NULL, "MICP"}, |
| 262 | |
| 263 | /* Input PGA */ |
| 264 | {"Input PGA", "Aux Switch", "Aux Input"}, |
| 265 | {"Input PGA", "MicN Switch", "MICN"}, |
| 266 | {"Input PGA", "MicP Switch", "MICP"}, |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 267 | |
| 268 | /* Inputs */ |
Mark Brown | 8a123ee | 2009-06-30 21:10:34 +0100 | [diff] [blame] | 269 | {"Aux Input", NULL, "AUX"}, |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 270 | }; |
| 271 | |
| 272 | static int wm8974_add_widgets(struct snd_soc_codec *codec) |
| 273 | { |
| 274 | snd_soc_dapm_new_controls(codec, wm8974_dapm_widgets, |
| 275 | ARRAY_SIZE(wm8974_dapm_widgets)); |
| 276 | |
| 277 | snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map)); |
| 278 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | struct pll_ { |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 283 | unsigned int pre_div:1; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 284 | unsigned int n:4; |
| 285 | unsigned int k; |
| 286 | }; |
| 287 | |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 288 | /* The size in bits of the pll divide multiplied by 10 |
| 289 | * to allow rounding later */ |
| 290 | #define FIXED_PLL_SIZE ((1 << 24) * 10) |
| 291 | |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 292 | static void pll_factors(struct pll_ *pll_div, |
| 293 | unsigned int target, unsigned int source) |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 294 | { |
| 295 | unsigned long long Kpart; |
| 296 | unsigned int K, Ndiv, Nmod; |
| 297 | |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 298 | /* There is a fixed divide by 4 in the output path */ |
| 299 | target *= 4; |
| 300 | |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 301 | Ndiv = target / source; |
| 302 | if (Ndiv < 6) { |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 303 | source /= 2; |
| 304 | pll_div->pre_div = 1; |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 305 | Ndiv = target / source; |
| 306 | } else |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 307 | pll_div->pre_div = 0; |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 308 | |
| 309 | if ((Ndiv < 6) || (Ndiv > 12)) |
| 310 | printk(KERN_WARNING |
Mark Brown | 8b83a19 | 2009-06-30 19:37:02 +0100 | [diff] [blame] | 311 | "WM8974 N value %u outwith recommended range!\n", |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 312 | Ndiv); |
| 313 | |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 314 | pll_div->n = Ndiv; |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 315 | Nmod = target % source; |
| 316 | Kpart = FIXED_PLL_SIZE * (long long)Nmod; |
| 317 | |
| 318 | do_div(Kpart, source); |
| 319 | |
| 320 | K = Kpart & 0xFFFFFFFF; |
| 321 | |
| 322 | /* Check if we need to round */ |
| 323 | if ((K % 10) >= 5) |
| 324 | K += 5; |
| 325 | |
| 326 | /* Move down to proper range now rounding is done */ |
| 327 | K /= 10; |
| 328 | |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 329 | pll_div->k = K; |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 330 | } |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 331 | |
Mark Brown | 8548803 | 2009-09-05 18:52:16 +0100 | [diff] [blame] | 332 | static int wm8974_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id, |
| 333 | int source, unsigned int freq_in, unsigned int freq_out) |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 334 | { |
| 335 | struct snd_soc_codec *codec = codec_dai->codec; |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 336 | struct pll_ pll_div; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 337 | u16 reg; |
| 338 | |
Mark Brown | 1a55b3f | 2009-05-23 11:31:40 +0100 | [diff] [blame] | 339 | if (freq_in == 0 || freq_out == 0) { |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 340 | /* Clock CODEC directly from MCLK */ |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 341 | reg = snd_soc_read(codec, WM8974_CLOCK); |
| 342 | snd_soc_write(codec, WM8974_CLOCK, reg & 0x0ff); |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 343 | |
| 344 | /* Turn off PLL */ |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 345 | reg = snd_soc_read(codec, WM8974_POWER1); |
| 346 | snd_soc_write(codec, WM8974_POWER1, reg & 0x1df); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
Mark Brown | c36b2fc | 2009-09-30 14:31:38 +0100 | [diff] [blame] | 350 | pll_factors(&pll_div, freq_out, freq_in); |
Mark Brown | 1a55b3f | 2009-05-23 11:31:40 +0100 | [diff] [blame] | 351 | |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 352 | snd_soc_write(codec, WM8974_PLLN, (pll_div.pre_div << 4) | pll_div.n); |
| 353 | snd_soc_write(codec, WM8974_PLLK1, pll_div.k >> 18); |
| 354 | snd_soc_write(codec, WM8974_PLLK2, (pll_div.k >> 9) & 0x1ff); |
| 355 | snd_soc_write(codec, WM8974_PLLK3, pll_div.k & 0x1ff); |
| 356 | reg = snd_soc_read(codec, WM8974_POWER1); |
| 357 | snd_soc_write(codec, WM8974_POWER1, reg | 0x020); |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 358 | |
| 359 | /* Run CODEC from PLL instead of MCLK */ |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 360 | reg = snd_soc_read(codec, WM8974_CLOCK); |
| 361 | snd_soc_write(codec, WM8974_CLOCK, reg | 0x100); |
Mark Brown | 91d0c3e | 2009-06-30 19:02:32 +0100 | [diff] [blame] | 362 | |
| 363 | return 0; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | /* |
| 367 | * Configure WM8974 clock dividers. |
| 368 | */ |
| 369 | static int wm8974_set_dai_clkdiv(struct snd_soc_dai *codec_dai, |
| 370 | int div_id, int div) |
| 371 | { |
| 372 | struct snd_soc_codec *codec = codec_dai->codec; |
| 373 | u16 reg; |
| 374 | |
| 375 | switch (div_id) { |
| 376 | case WM8974_OPCLKDIV: |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 377 | reg = snd_soc_read(codec, WM8974_GPIO) & 0x1cf; |
| 378 | snd_soc_write(codec, WM8974_GPIO, reg | div); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 379 | break; |
| 380 | case WM8974_MCLKDIV: |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 381 | reg = snd_soc_read(codec, WM8974_CLOCK) & 0x11f; |
| 382 | snd_soc_write(codec, WM8974_CLOCK, reg | div); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 383 | break; |
| 384 | case WM8974_ADCCLK: |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 385 | reg = snd_soc_read(codec, WM8974_ADC) & 0x1f7; |
| 386 | snd_soc_write(codec, WM8974_ADC, reg | div); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 387 | break; |
| 388 | case WM8974_DACCLK: |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 389 | reg = snd_soc_read(codec, WM8974_DAC) & 0x1f7; |
| 390 | snd_soc_write(codec, WM8974_DAC, reg | div); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 391 | break; |
| 392 | case WM8974_BCLKDIV: |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 393 | reg = snd_soc_read(codec, WM8974_CLOCK) & 0x1e3; |
| 394 | snd_soc_write(codec, WM8974_CLOCK, reg | div); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 395 | break; |
| 396 | default: |
| 397 | return -EINVAL; |
| 398 | } |
| 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | static int wm8974_set_dai_fmt(struct snd_soc_dai *codec_dai, |
| 404 | unsigned int fmt) |
| 405 | { |
| 406 | struct snd_soc_codec *codec = codec_dai->codec; |
| 407 | u16 iface = 0; |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 408 | u16 clk = snd_soc_read(codec, WM8974_CLOCK) & 0x1fe; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 409 | |
| 410 | /* set master/slave audio interface */ |
| 411 | switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) { |
| 412 | case SND_SOC_DAIFMT_CBM_CFM: |
| 413 | clk |= 0x0001; |
| 414 | break; |
| 415 | case SND_SOC_DAIFMT_CBS_CFS: |
| 416 | break; |
| 417 | default: |
| 418 | return -EINVAL; |
| 419 | } |
| 420 | |
| 421 | /* interface format */ |
| 422 | switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) { |
| 423 | case SND_SOC_DAIFMT_I2S: |
| 424 | iface |= 0x0010; |
| 425 | break; |
| 426 | case SND_SOC_DAIFMT_RIGHT_J: |
| 427 | break; |
| 428 | case SND_SOC_DAIFMT_LEFT_J: |
| 429 | iface |= 0x0008; |
| 430 | break; |
| 431 | case SND_SOC_DAIFMT_DSP_A: |
| 432 | iface |= 0x00018; |
| 433 | break; |
| 434 | default: |
| 435 | return -EINVAL; |
| 436 | } |
| 437 | |
| 438 | /* clock inversion */ |
| 439 | switch (fmt & SND_SOC_DAIFMT_INV_MASK) { |
| 440 | case SND_SOC_DAIFMT_NB_NF: |
| 441 | break; |
| 442 | case SND_SOC_DAIFMT_IB_IF: |
| 443 | iface |= 0x0180; |
| 444 | break; |
| 445 | case SND_SOC_DAIFMT_IB_NF: |
| 446 | iface |= 0x0100; |
| 447 | break; |
| 448 | case SND_SOC_DAIFMT_NB_IF: |
| 449 | iface |= 0x0080; |
| 450 | break; |
| 451 | default: |
| 452 | return -EINVAL; |
| 453 | } |
| 454 | |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 455 | snd_soc_write(codec, WM8974_IFACE, iface); |
| 456 | snd_soc_write(codec, WM8974_CLOCK, clk); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | static int wm8974_pcm_hw_params(struct snd_pcm_substream *substream, |
| 461 | struct snd_pcm_hw_params *params, |
| 462 | struct snd_soc_dai *dai) |
| 463 | { |
| 464 | struct snd_soc_codec *codec = dai->codec; |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 465 | u16 iface = snd_soc_read(codec, WM8974_IFACE) & 0x19f; |
| 466 | u16 adn = snd_soc_read(codec, WM8974_ADD) & 0x1f1; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 467 | |
| 468 | /* bit size */ |
| 469 | switch (params_format(params)) { |
| 470 | case SNDRV_PCM_FORMAT_S16_LE: |
| 471 | break; |
| 472 | case SNDRV_PCM_FORMAT_S20_3LE: |
| 473 | iface |= 0x0020; |
| 474 | break; |
| 475 | case SNDRV_PCM_FORMAT_S24_LE: |
| 476 | iface |= 0x0040; |
| 477 | break; |
| 478 | case SNDRV_PCM_FORMAT_S32_LE: |
| 479 | iface |= 0x0060; |
| 480 | break; |
| 481 | } |
| 482 | |
| 483 | /* filter coefficient */ |
| 484 | switch (params_rate(params)) { |
| 485 | case SNDRV_PCM_RATE_8000: |
| 486 | adn |= 0x5 << 1; |
| 487 | break; |
| 488 | case SNDRV_PCM_RATE_11025: |
| 489 | adn |= 0x4 << 1; |
| 490 | break; |
| 491 | case SNDRV_PCM_RATE_16000: |
| 492 | adn |= 0x3 << 1; |
| 493 | break; |
| 494 | case SNDRV_PCM_RATE_22050: |
| 495 | adn |= 0x2 << 1; |
| 496 | break; |
| 497 | case SNDRV_PCM_RATE_32000: |
| 498 | adn |= 0x1 << 1; |
| 499 | break; |
| 500 | case SNDRV_PCM_RATE_44100: |
Mark Brown | 8b83a19 | 2009-06-30 19:37:02 +0100 | [diff] [blame] | 501 | case SNDRV_PCM_RATE_48000: |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 502 | break; |
| 503 | } |
| 504 | |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 505 | snd_soc_write(codec, WM8974_IFACE, iface); |
| 506 | snd_soc_write(codec, WM8974_ADD, adn); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | static int wm8974_mute(struct snd_soc_dai *dai, int mute) |
| 511 | { |
| 512 | struct snd_soc_codec *codec = dai->codec; |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 513 | u16 mute_reg = snd_soc_read(codec, WM8974_DAC) & 0xffbf; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 514 | |
Mark Brown | 1a55b3f | 2009-05-23 11:31:40 +0100 | [diff] [blame] | 515 | if (mute) |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 516 | snd_soc_write(codec, WM8974_DAC, mute_reg | 0x40); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 517 | else |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 518 | snd_soc_write(codec, WM8974_DAC, mute_reg); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | /* liam need to make this lower power with dapm */ |
| 523 | static int wm8974_set_bias_level(struct snd_soc_codec *codec, |
| 524 | enum snd_soc_bias_level level) |
| 525 | { |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 526 | u16 power1 = snd_soc_read(codec, WM8974_POWER1) & ~0x3; |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 527 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 528 | switch (level) { |
| 529 | case SND_SOC_BIAS_ON: |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 530 | case SND_SOC_BIAS_PREPARE: |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 531 | power1 |= 0x1; /* VMID 50k */ |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 532 | snd_soc_write(codec, WM8974_POWER1, power1); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 533 | break; |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 534 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 535 | case SND_SOC_BIAS_STANDBY: |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 536 | power1 |= WM8974_POWER1_BIASEN | WM8974_POWER1_BUFIOEN; |
| 537 | |
| 538 | if (codec->bias_level == SND_SOC_BIAS_OFF) { |
| 539 | /* Initial cap charge at VMID 5k */ |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 540 | snd_soc_write(codec, WM8974_POWER1, power1 | 0x3); |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 541 | mdelay(100); |
| 542 | } |
| 543 | |
| 544 | power1 |= 0x2; /* VMID 500k */ |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 545 | snd_soc_write(codec, WM8974_POWER1, power1); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 546 | break; |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 547 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 548 | case SND_SOC_BIAS_OFF: |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 549 | snd_soc_write(codec, WM8974_POWER1, 0); |
| 550 | snd_soc_write(codec, WM8974_POWER2, 0); |
| 551 | snd_soc_write(codec, WM8974_POWER3, 0); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 552 | break; |
| 553 | } |
Mark Brown | df1ef7a | 2009-06-30 19:01:09 +0100 | [diff] [blame] | 554 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 555 | codec->bias_level = level; |
| 556 | return 0; |
| 557 | } |
| 558 | |
Mark Brown | 1a55b3f | 2009-05-23 11:31:40 +0100 | [diff] [blame] | 559 | #define WM8974_RATES (SNDRV_PCM_RATE_8000_48000) |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 560 | |
| 561 | #define WM8974_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\ |
| 562 | SNDRV_PCM_FMTBIT_S24_LE) |
| 563 | |
| 564 | static struct snd_soc_dai_ops wm8974_ops = { |
| 565 | .hw_params = wm8974_pcm_hw_params, |
| 566 | .digital_mute = wm8974_mute, |
| 567 | .set_fmt = wm8974_set_dai_fmt, |
| 568 | .set_clkdiv = wm8974_set_dai_clkdiv, |
| 569 | .set_pll = wm8974_set_dai_pll, |
| 570 | }; |
| 571 | |
| 572 | struct snd_soc_dai wm8974_dai = { |
| 573 | .name = "WM8974 HiFi", |
| 574 | .playback = { |
| 575 | .stream_name = "Playback", |
| 576 | .channels_min = 1, |
Mark Brown | 33d81af | 2009-06-30 19:01:52 +0100 | [diff] [blame] | 577 | .channels_max = 2, /* Only 1 channel of data */ |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 578 | .rates = WM8974_RATES, |
| 579 | .formats = WM8974_FORMATS,}, |
| 580 | .capture = { |
| 581 | .stream_name = "Capture", |
| 582 | .channels_min = 1, |
Mark Brown | 33d81af | 2009-06-30 19:01:52 +0100 | [diff] [blame] | 583 | .channels_max = 2, /* Only 1 channel of data */ |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 584 | .rates = WM8974_RATES, |
| 585 | .formats = WM8974_FORMATS,}, |
| 586 | .ops = &wm8974_ops, |
Mark Brown | cb11d39 | 2009-06-30 19:36:39 +0100 | [diff] [blame] | 587 | .symmetric_rates = 1, |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 588 | }; |
| 589 | EXPORT_SYMBOL_GPL(wm8974_dai); |
| 590 | |
| 591 | static int wm8974_suspend(struct platform_device *pdev, pm_message_t state) |
| 592 | { |
| 593 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 594 | struct snd_soc_codec *codec = socdev->card->codec; |
| 595 | |
| 596 | wm8974_set_bias_level(codec, SND_SOC_BIAS_OFF); |
| 597 | return 0; |
| 598 | } |
| 599 | |
| 600 | static int wm8974_resume(struct platform_device *pdev) |
| 601 | { |
| 602 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 603 | struct snd_soc_codec *codec = socdev->card->codec; |
| 604 | int i; |
| 605 | u8 data[2]; |
| 606 | u16 *cache = codec->reg_cache; |
| 607 | |
| 608 | /* Sync reg_cache with the hardware */ |
| 609 | for (i = 0; i < ARRAY_SIZE(wm8974_reg); i++) { |
| 610 | data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001); |
| 611 | data[1] = cache[i] & 0x00ff; |
| 612 | codec->hw_write(codec->control_data, data, 2); |
| 613 | } |
| 614 | wm8974_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 615 | wm8974_set_bias_level(codec, codec->suspend_bias_level); |
| 616 | return 0; |
| 617 | } |
| 618 | |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 619 | static int wm8974_probe(struct platform_device *pdev) |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 620 | { |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 621 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
| 622 | struct snd_soc_codec *codec; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 623 | int ret = 0; |
| 624 | |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 625 | if (wm8974_codec == NULL) { |
| 626 | dev_err(&pdev->dev, "Codec device not registered\n"); |
| 627 | return -ENODEV; |
| 628 | } |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 629 | |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 630 | socdev->card->codec = wm8974_codec; |
| 631 | codec = wm8974_codec; |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 632 | |
| 633 | /* register pcms */ |
| 634 | ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1); |
Mark Brown | 1a55b3f | 2009-05-23 11:31:40 +0100 | [diff] [blame] | 635 | if (ret < 0) { |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 636 | dev_err(codec->dev, "failed to create pcms: %d\n", ret); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 637 | goto pcm_err; |
| 638 | } |
| 639 | |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 640 | snd_soc_add_controls(codec, wm8974_snd_controls, |
| 641 | ARRAY_SIZE(wm8974_snd_controls)); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 642 | wm8974_add_widgets(codec); |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 643 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 644 | return ret; |
| 645 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 646 | pcm_err: |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 647 | return ret; |
| 648 | } |
| 649 | |
| 650 | /* power down chip */ |
| 651 | static int wm8974_remove(struct platform_device *pdev) |
| 652 | { |
| 653 | struct snd_soc_device *socdev = platform_get_drvdata(pdev); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 654 | |
| 655 | snd_soc_free_pcms(socdev); |
| 656 | snd_soc_dapm_free(socdev); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | struct snd_soc_codec_device soc_codec_dev_wm8974 = { |
| 662 | .probe = wm8974_probe, |
| 663 | .remove = wm8974_remove, |
| 664 | .suspend = wm8974_suspend, |
| 665 | .resume = wm8974_resume, |
| 666 | }; |
| 667 | EXPORT_SYMBOL_GPL(soc_codec_dev_wm8974); |
| 668 | |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 669 | static __devinit int wm8974_register(struct wm8974_priv *wm8974) |
| 670 | { |
| 671 | int ret; |
| 672 | struct snd_soc_codec *codec = &wm8974->codec; |
| 673 | |
| 674 | if (wm8974_codec) { |
| 675 | dev_err(codec->dev, "Another WM8974 is registered\n"); |
| 676 | return -EINVAL; |
| 677 | } |
| 678 | |
| 679 | mutex_init(&codec->mutex); |
| 680 | INIT_LIST_HEAD(&codec->dapm_widgets); |
| 681 | INIT_LIST_HEAD(&codec->dapm_paths); |
| 682 | |
| 683 | codec->private_data = wm8974; |
| 684 | codec->name = "WM8974"; |
| 685 | codec->owner = THIS_MODULE; |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 686 | codec->bias_level = SND_SOC_BIAS_OFF; |
| 687 | codec->set_bias_level = wm8974_set_bias_level; |
| 688 | codec->dai = &wm8974_dai; |
| 689 | codec->num_dai = 1; |
| 690 | codec->reg_cache_size = WM8974_CACHEREGNUM; |
| 691 | codec->reg_cache = &wm8974->reg_cache; |
| 692 | |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 693 | ret = snd_soc_codec_set_cache_io(codec, 7, 9, SND_SOC_I2C); |
| 694 | if (ret < 0) { |
| 695 | dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret); |
| 696 | goto err; |
| 697 | } |
| 698 | |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 699 | memcpy(codec->reg_cache, wm8974_reg, sizeof(wm8974_reg)); |
| 700 | |
| 701 | ret = wm8974_reset(codec); |
| 702 | if (ret < 0) { |
| 703 | dev_err(codec->dev, "Failed to issue reset\n"); |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 704 | goto err; |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | wm8974_dai.dev = codec->dev; |
| 708 | |
| 709 | wm8974_set_bias_level(codec, SND_SOC_BIAS_STANDBY); |
| 710 | |
| 711 | wm8974_codec = codec; |
| 712 | |
| 713 | ret = snd_soc_register_codec(codec); |
| 714 | if (ret != 0) { |
| 715 | dev_err(codec->dev, "Failed to register codec: %d\n", ret); |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 716 | goto err; |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | ret = snd_soc_register_dai(&wm8974_dai); |
| 720 | if (ret != 0) { |
| 721 | dev_err(codec->dev, "Failed to register DAI: %d\n", ret); |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 722 | goto err_codec; |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | return 0; |
Mark Brown | 1e97f50 | 2009-08-15 12:15:10 +0100 | [diff] [blame] | 726 | |
| 727 | err_codec: |
| 728 | snd_soc_unregister_codec(codec); |
| 729 | err: |
| 730 | kfree(wm8974); |
| 731 | return ret; |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | static __devexit void wm8974_unregister(struct wm8974_priv *wm8974) |
| 735 | { |
| 736 | wm8974_set_bias_level(&wm8974->codec, SND_SOC_BIAS_OFF); |
| 737 | snd_soc_unregister_dai(&wm8974_dai); |
| 738 | snd_soc_unregister_codec(&wm8974->codec); |
| 739 | kfree(wm8974); |
| 740 | wm8974_codec = NULL; |
| 741 | } |
| 742 | |
| 743 | static __devinit int wm8974_i2c_probe(struct i2c_client *i2c, |
| 744 | const struct i2c_device_id *id) |
| 745 | { |
| 746 | struct wm8974_priv *wm8974; |
| 747 | struct snd_soc_codec *codec; |
| 748 | |
| 749 | wm8974 = kzalloc(sizeof(struct wm8974_priv), GFP_KERNEL); |
| 750 | if (wm8974 == NULL) |
| 751 | return -ENOMEM; |
| 752 | |
| 753 | codec = &wm8974->codec; |
| 754 | codec->hw_write = (hw_write_t)i2c_master_send; |
| 755 | |
| 756 | i2c_set_clientdata(i2c, wm8974); |
| 757 | codec->control_data = i2c; |
| 758 | |
| 759 | codec->dev = &i2c->dev; |
| 760 | |
| 761 | return wm8974_register(wm8974); |
| 762 | } |
| 763 | |
| 764 | static __devexit int wm8974_i2c_remove(struct i2c_client *client) |
| 765 | { |
| 766 | struct wm8974_priv *wm8974 = i2c_get_clientdata(client); |
| 767 | wm8974_unregister(wm8974); |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | static const struct i2c_device_id wm8974_i2c_id[] = { |
| 772 | { "wm8974", 0 }, |
| 773 | { } |
| 774 | }; |
| 775 | MODULE_DEVICE_TABLE(i2c, wm8974_i2c_id); |
| 776 | |
| 777 | static struct i2c_driver wm8974_i2c_driver = { |
| 778 | .driver = { |
Mark Brown | 8b83a19 | 2009-06-30 19:37:02 +0100 | [diff] [blame] | 779 | .name = "WM8974", |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 780 | .owner = THIS_MODULE, |
| 781 | }, |
| 782 | .probe = wm8974_i2c_probe, |
| 783 | .remove = __devexit_p(wm8974_i2c_remove), |
| 784 | .id_table = wm8974_i2c_id, |
| 785 | }; |
| 786 | |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 787 | static int __init wm8974_modinit(void) |
| 788 | { |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 789 | return i2c_add_driver(&wm8974_i2c_driver); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 790 | } |
| 791 | module_init(wm8974_modinit); |
| 792 | |
| 793 | static void __exit wm8974_exit(void) |
| 794 | { |
Mark Brown | 4fcbbb6 | 2009-05-23 12:27:03 +0100 | [diff] [blame] | 795 | i2c_del_driver(&wm8974_i2c_driver); |
Mark Brown | 0a1bf55 | 2009-05-23 11:18:41 +0100 | [diff] [blame] | 796 | } |
| 797 | module_exit(wm8974_exit); |
| 798 | |
| 799 | MODULE_DESCRIPTION("ASoC WM8974 driver"); |
| 800 | MODULE_AUTHOR("Liam Girdwood"); |
| 801 | MODULE_LICENSE("GPL"); |