ASoC: TWL4030: Add VDL path support
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / sound / soc / codecs / twl4030.c
blobfdf88dfbcff95c4b3fe54928142231ea9881752d
1 /*
2 * ALSA SoC TWL4030 codec driver
4 * Author: Steve Sakoman, <steve@sakoman.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
22 #include <linux/module.h>
23 #include <linux/moduleparam.h>
24 #include <linux/init.h>
25 #include <linux/delay.h>
26 #include <linux/pm.h>
27 #include <linux/i2c.h>
28 #include <linux/platform_device.h>
29 #include <linux/i2c/twl4030.h>
30 #include <sound/core.h>
31 #include <sound/pcm.h>
32 #include <sound/pcm_params.h>
33 #include <sound/soc.h>
34 #include <sound/soc-dapm.h>
35 #include <sound/initval.h>
36 #include <sound/tlv.h>
38 #include "twl4030.h"
41 * twl4030 register cache & default register settings
43 static const u8 twl4030_reg[TWL4030_CACHEREGNUM] = {
44 0x00, /* this register not used */
45 0x91, /* REG_CODEC_MODE (0x1) */
46 0xc3, /* REG_OPTION (0x2) */
47 0x00, /* REG_UNKNOWN (0x3) */
48 0x00, /* REG_MICBIAS_CTL (0x4) */
49 0x20, /* REG_ANAMICL (0x5) */
50 0x00, /* REG_ANAMICR (0x6) */
51 0x00, /* REG_AVADC_CTL (0x7) */
52 0x00, /* REG_ADCMICSEL (0x8) */
53 0x00, /* REG_DIGMIXING (0x9) */
54 0x0c, /* REG_ATXL1PGA (0xA) */
55 0x0c, /* REG_ATXR1PGA (0xB) */
56 0x00, /* REG_AVTXL2PGA (0xC) */
57 0x00, /* REG_AVTXR2PGA (0xD) */
58 0x01, /* REG_AUDIO_IF (0xE) */
59 0x00, /* REG_VOICE_IF (0xF) */
60 0x00, /* REG_ARXR1PGA (0x10) */
61 0x00, /* REG_ARXL1PGA (0x11) */
62 0x6c, /* REG_ARXR2PGA (0x12) */
63 0x6c, /* REG_ARXL2PGA (0x13) */
64 0x00, /* REG_VRXPGA (0x14) */
65 0x00, /* REG_VSTPGA (0x15) */
66 0x00, /* REG_VRX2ARXPGA (0x16) */
67 0x0c, /* REG_AVDAC_CTL (0x17) */
68 0x00, /* REG_ARX2VTXPGA (0x18) */
69 0x00, /* REG_ARXL1_APGA_CTL (0x19) */
70 0x00, /* REG_ARXR1_APGA_CTL (0x1A) */
71 0x4b, /* REG_ARXL2_APGA_CTL (0x1B) */
72 0x4b, /* REG_ARXR2_APGA_CTL (0x1C) */
73 0x00, /* REG_ATX2ARXPGA (0x1D) */
74 0x00, /* REG_BT_IF (0x1E) */
75 0x00, /* REG_BTPGA (0x1F) */
76 0x00, /* REG_BTSTPGA (0x20) */
77 0x00, /* REG_EAR_CTL (0x21) */
78 0x24, /* REG_HS_SEL (0x22) */
79 0x0a, /* REG_HS_GAIN_SET (0x23) */
80 0x00, /* REG_HS_POPN_SET (0x24) */
81 0x00, /* REG_PREDL_CTL (0x25) */
82 0x00, /* REG_PREDR_CTL (0x26) */
83 0x00, /* REG_PRECKL_CTL (0x27) */
84 0x00, /* REG_PRECKR_CTL (0x28) */
85 0x00, /* REG_HFL_CTL (0x29) */
86 0x00, /* REG_HFR_CTL (0x2A) */
87 0x00, /* REG_ALC_CTL (0x2B) */
88 0x00, /* REG_ALC_SET1 (0x2C) */
89 0x00, /* REG_ALC_SET2 (0x2D) */
90 0x00, /* REG_BOOST_CTL (0x2E) */
91 0x00, /* REG_SOFTVOL_CTL (0x2F) */
92 0x00, /* REG_DTMF_FREQSEL (0x30) */
93 0x00, /* REG_DTMF_TONEXT1H (0x31) */
94 0x00, /* REG_DTMF_TONEXT1L (0x32) */
95 0x00, /* REG_DTMF_TONEXT2H (0x33) */
96 0x00, /* REG_DTMF_TONEXT2L (0x34) */
97 0x00, /* REG_DTMF_TONOFF (0x35) */
98 0x00, /* REG_DTMF_WANONOFF (0x36) */
99 0x00, /* REG_I2S_RX_SCRAMBLE_H (0x37) */
100 0x00, /* REG_I2S_RX_SCRAMBLE_M (0x38) */
101 0x00, /* REG_I2S_RX_SCRAMBLE_L (0x39) */
102 0x16, /* REG_APLL_CTL (0x3A) */
103 0x00, /* REG_DTMF_CTL (0x3B) */
104 0x00, /* REG_DTMF_PGA_CTL2 (0x3C) */
105 0x00, /* REG_DTMF_PGA_CTL1 (0x3D) */
106 0x00, /* REG_MISC_SET_1 (0x3E) */
107 0x00, /* REG_PCMBTMUX (0x3F) */
108 0x00, /* not used (0x40) */
109 0x00, /* not used (0x41) */
110 0x00, /* not used (0x42) */
111 0x00, /* REG_RX_PATH_SEL (0x43) */
112 0x00, /* REG_VDL_APGA_CTL (0x44) */
113 0x00, /* REG_VIBRA_CTL (0x45) */
114 0x00, /* REG_VIBRA_SET (0x46) */
115 0x00, /* REG_VIBRA_PWM_SET (0x47) */
116 0x00, /* REG_ANAMIC_GAIN (0x48) */
117 0x00, /* REG_MISC_SET_2 (0x49) */
120 /* codec private data */
121 struct twl4030_priv {
122 unsigned int bypass_state;
123 unsigned int codec_powered;
124 unsigned int codec_muted;
126 struct snd_pcm_substream *master_substream;
127 struct snd_pcm_substream *slave_substream;
129 unsigned int configured;
130 unsigned int rate;
131 unsigned int sample_bits;
132 unsigned int channels;
136 * read twl4030 register cache
138 static inline unsigned int twl4030_read_reg_cache(struct snd_soc_codec *codec,
139 unsigned int reg)
141 u8 *cache = codec->reg_cache;
143 if (reg >= TWL4030_CACHEREGNUM)
144 return -EIO;
146 return cache[reg];
150 * write twl4030 register cache
152 static inline void twl4030_write_reg_cache(struct snd_soc_codec *codec,
153 u8 reg, u8 value)
155 u8 *cache = codec->reg_cache;
157 if (reg >= TWL4030_CACHEREGNUM)
158 return;
159 cache[reg] = value;
163 * write to the twl4030 register space
165 static int twl4030_write(struct snd_soc_codec *codec,
166 unsigned int reg, unsigned int value)
168 twl4030_write_reg_cache(codec, reg, value);
169 return twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, value, reg);
172 static void twl4030_codec_enable(struct snd_soc_codec *codec, int enable)
174 struct twl4030_priv *twl4030 = codec->private_data;
175 u8 mode;
177 if (enable == twl4030->codec_powered)
178 return;
180 mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE);
181 if (enable)
182 mode |= TWL4030_CODECPDZ;
183 else
184 mode &= ~TWL4030_CODECPDZ;
186 twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode);
187 twl4030->codec_powered = enable;
189 /* REVISIT: this delay is present in TI sample drivers */
190 /* but there seems to be no TRM requirement for it */
191 udelay(10);
194 static void twl4030_init_chip(struct snd_soc_codec *codec)
196 int i;
198 /* clear CODECPDZ prior to setting register defaults */
199 twl4030_codec_enable(codec, 0);
201 /* set all audio section registers to reasonable defaults */
202 for (i = TWL4030_REG_OPTION; i <= TWL4030_REG_MISC_SET_2; i++)
203 twl4030_write(codec, i, twl4030_reg[i]);
207 static void twl4030_codec_mute(struct snd_soc_codec *codec, int mute)
209 struct twl4030_priv *twl4030 = codec->private_data;
210 u8 reg_val;
212 if (mute == twl4030->codec_muted)
213 return;
215 if (mute) {
216 /* Bypass the reg_cache and mute the volumes
217 * Headset mute is done in it's own event handler
218 * Things to mute: Earpiece, PreDrivL/R, CarkitL/R
220 reg_val = twl4030_read_reg_cache(codec, TWL4030_REG_EAR_CTL);
221 twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
222 reg_val & (~TWL4030_EAR_GAIN),
223 TWL4030_REG_EAR_CTL);
225 reg_val = twl4030_read_reg_cache(codec, TWL4030_REG_PREDL_CTL);
226 twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
227 reg_val & (~TWL4030_PREDL_GAIN),
228 TWL4030_REG_PREDL_CTL);
229 reg_val = twl4030_read_reg_cache(codec, TWL4030_REG_PREDR_CTL);
230 twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
231 reg_val & (~TWL4030_PREDR_GAIN),
232 TWL4030_REG_PREDL_CTL);
234 reg_val = twl4030_read_reg_cache(codec, TWL4030_REG_PRECKL_CTL);
235 twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
236 reg_val & (~TWL4030_PRECKL_GAIN),
237 TWL4030_REG_PRECKL_CTL);
238 reg_val = twl4030_read_reg_cache(codec, TWL4030_REG_PRECKR_CTL);
239 twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
240 reg_val & (~TWL4030_PRECKL_GAIN),
241 TWL4030_REG_PRECKR_CTL);
243 /* Disable PLL */
244 reg_val = twl4030_read_reg_cache(codec, TWL4030_REG_APLL_CTL);
245 reg_val &= ~TWL4030_APLL_EN;
246 twl4030_write(codec, TWL4030_REG_APLL_CTL, reg_val);
247 } else {
248 /* Restore the volumes
249 * Headset mute is done in it's own event handler
250 * Things to restore: Earpiece, PreDrivL/R, CarkitL/R
252 twl4030_write(codec, TWL4030_REG_EAR_CTL,
253 twl4030_read_reg_cache(codec, TWL4030_REG_EAR_CTL));
255 twl4030_write(codec, TWL4030_REG_PREDL_CTL,
256 twl4030_read_reg_cache(codec, TWL4030_REG_PREDL_CTL));
257 twl4030_write(codec, TWL4030_REG_PREDR_CTL,
258 twl4030_read_reg_cache(codec, TWL4030_REG_PREDR_CTL));
260 twl4030_write(codec, TWL4030_REG_PRECKL_CTL,
261 twl4030_read_reg_cache(codec, TWL4030_REG_PRECKL_CTL));
262 twl4030_write(codec, TWL4030_REG_PRECKR_CTL,
263 twl4030_read_reg_cache(codec, TWL4030_REG_PRECKR_CTL));
265 /* Enable PLL */
266 reg_val = twl4030_read_reg_cache(codec, TWL4030_REG_APLL_CTL);
267 reg_val |= TWL4030_APLL_EN;
268 twl4030_write(codec, TWL4030_REG_APLL_CTL, reg_val);
271 twl4030->codec_muted = mute;
274 static void twl4030_power_up(struct snd_soc_codec *codec)
276 struct twl4030_priv *twl4030 = codec->private_data;
277 u8 anamicl, regmisc1, byte;
278 int i = 0;
280 if (twl4030->codec_powered)
281 return;
283 /* set CODECPDZ to turn on codec */
284 twl4030_codec_enable(codec, 1);
286 /* initiate offset cancellation */
287 anamicl = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICL);
288 twl4030_write(codec, TWL4030_REG_ANAMICL,
289 anamicl | TWL4030_CNCL_OFFSET_START);
291 /* wait for offset cancellation to complete */
292 do {
293 /* this takes a little while, so don't slam i2c */
294 udelay(2000);
295 twl4030_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &byte,
296 TWL4030_REG_ANAMICL);
297 } while ((i++ < 100) &&
298 ((byte & TWL4030_CNCL_OFFSET_START) ==
299 TWL4030_CNCL_OFFSET_START));
301 /* Make sure that the reg_cache has the same value as the HW */
302 twl4030_write_reg_cache(codec, TWL4030_REG_ANAMICL, byte);
304 /* anti-pop when changing analog gain */
305 regmisc1 = twl4030_read_reg_cache(codec, TWL4030_REG_MISC_SET_1);
306 twl4030_write(codec, TWL4030_REG_MISC_SET_1,
307 regmisc1 | TWL4030_SMOOTH_ANAVOL_EN);
309 /* toggle CODECPDZ as per TRM */
310 twl4030_codec_enable(codec, 0);
311 twl4030_codec_enable(codec, 1);
315 * Unconditional power down
317 static void twl4030_power_down(struct snd_soc_codec *codec)
319 /* power down */
320 twl4030_codec_enable(codec, 0);
323 /* Earpiece */
324 static const struct snd_kcontrol_new twl4030_dapm_earpiece_controls[] = {
325 SOC_DAPM_SINGLE("Voice", TWL4030_REG_EAR_CTL, 0, 1, 0),
326 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_EAR_CTL, 1, 1, 0),
327 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_EAR_CTL, 2, 1, 0),
328 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_EAR_CTL, 3, 1, 0),
331 /* PreDrive Left */
332 static const struct snd_kcontrol_new twl4030_dapm_predrivel_controls[] = {
333 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PREDL_CTL, 0, 1, 0),
334 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_PREDL_CTL, 1, 1, 0),
335 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PREDL_CTL, 2, 1, 0),
336 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PREDL_CTL, 3, 1, 0),
339 /* PreDrive Right */
340 static const struct snd_kcontrol_new twl4030_dapm_predriver_controls[] = {
341 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PREDR_CTL, 0, 1, 0),
342 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_PREDR_CTL, 1, 1, 0),
343 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PREDR_CTL, 2, 1, 0),
344 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PREDR_CTL, 3, 1, 0),
347 /* Headset Left */
348 static const struct snd_kcontrol_new twl4030_dapm_hsol_controls[] = {
349 SOC_DAPM_SINGLE("Voice", TWL4030_REG_HS_SEL, 0, 1, 0),
350 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_HS_SEL, 1, 1, 0),
351 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_HS_SEL, 2, 1, 0),
354 /* Headset Right */
355 static const struct snd_kcontrol_new twl4030_dapm_hsor_controls[] = {
356 SOC_DAPM_SINGLE("Voice", TWL4030_REG_HS_SEL, 3, 1, 0),
357 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_HS_SEL, 4, 1, 0),
358 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_HS_SEL, 5, 1, 0),
361 /* Carkit Left */
362 static const struct snd_kcontrol_new twl4030_dapm_carkitl_controls[] = {
363 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PRECKL_CTL, 0, 1, 0),
364 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_PRECKL_CTL, 1, 1, 0),
365 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PRECKL_CTL, 2, 1, 0),
368 /* Carkit Right */
369 static const struct snd_kcontrol_new twl4030_dapm_carkitr_controls[] = {
370 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PRECKR_CTL, 0, 1, 0),
371 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_PRECKR_CTL, 1, 1, 0),
372 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PRECKR_CTL, 2, 1, 0),
375 /* Handsfree Left */
376 static const char *twl4030_handsfreel_texts[] =
377 {"Voice", "AudioL1", "AudioL2", "AudioR2"};
379 static const struct soc_enum twl4030_handsfreel_enum =
380 SOC_ENUM_SINGLE(TWL4030_REG_HFL_CTL, 0,
381 ARRAY_SIZE(twl4030_handsfreel_texts),
382 twl4030_handsfreel_texts);
384 static const struct snd_kcontrol_new twl4030_dapm_handsfreel_control =
385 SOC_DAPM_ENUM("Route", twl4030_handsfreel_enum);
387 /* Handsfree Right */
388 static const char *twl4030_handsfreer_texts[] =
389 {"Voice", "AudioR1", "AudioR2", "AudioL2"};
391 static const struct soc_enum twl4030_handsfreer_enum =
392 SOC_ENUM_SINGLE(TWL4030_REG_HFR_CTL, 0,
393 ARRAY_SIZE(twl4030_handsfreer_texts),
394 twl4030_handsfreer_texts);
396 static const struct snd_kcontrol_new twl4030_dapm_handsfreer_control =
397 SOC_DAPM_ENUM("Route", twl4030_handsfreer_enum);
399 /* Left analog microphone selection */
400 static const char *twl4030_analoglmic_texts[] =
401 {"Off", "Main mic", "Headset mic", "AUXL", "Carkit mic"};
403 static const unsigned int twl4030_analoglmic_values[] =
404 {0x0, 0x1, 0x2, 0x4, 0x8};
406 static const struct soc_enum twl4030_analoglmic_enum =
407 SOC_VALUE_ENUM_SINGLE(TWL4030_REG_ANAMICL, 0, 0xf,
408 ARRAY_SIZE(twl4030_analoglmic_texts),
409 twl4030_analoglmic_texts,
410 twl4030_analoglmic_values);
412 static const struct snd_kcontrol_new twl4030_dapm_analoglmic_control =
413 SOC_DAPM_VALUE_ENUM("Route", twl4030_analoglmic_enum);
415 /* Right analog microphone selection */
416 static const char *twl4030_analogrmic_texts[] =
417 {"Off", "Sub mic", "AUXR"};
419 static const unsigned int twl4030_analogrmic_values[] =
420 {0x0, 0x1, 0x4};
422 static const struct soc_enum twl4030_analogrmic_enum =
423 SOC_VALUE_ENUM_SINGLE(TWL4030_REG_ANAMICR, 0, 0x5,
424 ARRAY_SIZE(twl4030_analogrmic_texts),
425 twl4030_analogrmic_texts,
426 twl4030_analogrmic_values);
428 static const struct snd_kcontrol_new twl4030_dapm_analogrmic_control =
429 SOC_DAPM_VALUE_ENUM("Route", twl4030_analogrmic_enum);
431 /* TX1 L/R Analog/Digital microphone selection */
432 static const char *twl4030_micpathtx1_texts[] =
433 {"Analog", "Digimic0"};
435 static const struct soc_enum twl4030_micpathtx1_enum =
436 SOC_ENUM_SINGLE(TWL4030_REG_ADCMICSEL, 0,
437 ARRAY_SIZE(twl4030_micpathtx1_texts),
438 twl4030_micpathtx1_texts);
440 static const struct snd_kcontrol_new twl4030_dapm_micpathtx1_control =
441 SOC_DAPM_ENUM("Route", twl4030_micpathtx1_enum);
443 /* TX2 L/R Analog/Digital microphone selection */
444 static const char *twl4030_micpathtx2_texts[] =
445 {"Analog", "Digimic1"};
447 static const struct soc_enum twl4030_micpathtx2_enum =
448 SOC_ENUM_SINGLE(TWL4030_REG_ADCMICSEL, 2,
449 ARRAY_SIZE(twl4030_micpathtx2_texts),
450 twl4030_micpathtx2_texts);
452 static const struct snd_kcontrol_new twl4030_dapm_micpathtx2_control =
453 SOC_DAPM_ENUM("Route", twl4030_micpathtx2_enum);
455 /* Analog bypass for AudioR1 */
456 static const struct snd_kcontrol_new twl4030_dapm_abypassr1_control =
457 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXR1_APGA_CTL, 2, 1, 0);
459 /* Analog bypass for AudioL1 */
460 static const struct snd_kcontrol_new twl4030_dapm_abypassl1_control =
461 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXL1_APGA_CTL, 2, 1, 0);
463 /* Analog bypass for AudioR2 */
464 static const struct snd_kcontrol_new twl4030_dapm_abypassr2_control =
465 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXR2_APGA_CTL, 2, 1, 0);
467 /* Analog bypass for AudioL2 */
468 static const struct snd_kcontrol_new twl4030_dapm_abypassl2_control =
469 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXL2_APGA_CTL, 2, 1, 0);
471 /* Digital bypass gain, 0 mutes the bypass */
472 static const unsigned int twl4030_dapm_dbypass_tlv[] = {
473 TLV_DB_RANGE_HEAD(2),
474 0, 3, TLV_DB_SCALE_ITEM(-2400, 0, 1),
475 4, 7, TLV_DB_SCALE_ITEM(-1800, 600, 0),
478 /* Digital bypass left (TX1L -> RX2L) */
479 static const struct snd_kcontrol_new twl4030_dapm_dbypassl_control =
480 SOC_DAPM_SINGLE_TLV("Volume",
481 TWL4030_REG_ATX2ARXPGA, 3, 7, 0,
482 twl4030_dapm_dbypass_tlv);
484 /* Digital bypass right (TX1R -> RX2R) */
485 static const struct snd_kcontrol_new twl4030_dapm_dbypassr_control =
486 SOC_DAPM_SINGLE_TLV("Volume",
487 TWL4030_REG_ATX2ARXPGA, 0, 7, 0,
488 twl4030_dapm_dbypass_tlv);
490 static int micpath_event(struct snd_soc_dapm_widget *w,
491 struct snd_kcontrol *kcontrol, int event)
493 struct soc_enum *e = (struct soc_enum *)w->kcontrols->private_value;
494 unsigned char adcmicsel, micbias_ctl;
496 adcmicsel = twl4030_read_reg_cache(w->codec, TWL4030_REG_ADCMICSEL);
497 micbias_ctl = twl4030_read_reg_cache(w->codec, TWL4030_REG_MICBIAS_CTL);
498 /* Prepare the bits for the given TX path:
499 * shift_l == 0: TX1 microphone path
500 * shift_l == 2: TX2 microphone path */
501 if (e->shift_l) {
502 /* TX2 microphone path */
503 if (adcmicsel & TWL4030_TX2IN_SEL)
504 micbias_ctl |= TWL4030_MICBIAS2_CTL; /* digimic */
505 else
506 micbias_ctl &= ~TWL4030_MICBIAS2_CTL;
507 } else {
508 /* TX1 microphone path */
509 if (adcmicsel & TWL4030_TX1IN_SEL)
510 micbias_ctl |= TWL4030_MICBIAS1_CTL; /* digimic */
511 else
512 micbias_ctl &= ~TWL4030_MICBIAS1_CTL;
515 twl4030_write(w->codec, TWL4030_REG_MICBIAS_CTL, micbias_ctl);
517 return 0;
520 static int handsfree_event(struct snd_soc_dapm_widget *w,
521 struct snd_kcontrol *kcontrol, int event)
523 struct soc_enum *e = (struct soc_enum *)w->kcontrols->private_value;
524 unsigned char hs_ctl;
526 hs_ctl = twl4030_read_reg_cache(w->codec, e->reg);
528 if (hs_ctl & TWL4030_HF_CTL_REF_EN) {
529 hs_ctl |= TWL4030_HF_CTL_RAMP_EN;
530 twl4030_write(w->codec, e->reg, hs_ctl);
531 hs_ctl |= TWL4030_HF_CTL_LOOP_EN;
532 twl4030_write(w->codec, e->reg, hs_ctl);
533 hs_ctl |= TWL4030_HF_CTL_HB_EN;
534 twl4030_write(w->codec, e->reg, hs_ctl);
535 } else {
536 hs_ctl &= ~(TWL4030_HF_CTL_RAMP_EN | TWL4030_HF_CTL_LOOP_EN
537 | TWL4030_HF_CTL_HB_EN);
538 twl4030_write(w->codec, e->reg, hs_ctl);
541 return 0;
544 static int headsetl_event(struct snd_soc_dapm_widget *w,
545 struct snd_kcontrol *kcontrol, int event)
547 unsigned char hs_gain, hs_pop;
549 /* Save the current volume */
550 hs_gain = twl4030_read_reg_cache(w->codec, TWL4030_REG_HS_GAIN_SET);
551 hs_pop = twl4030_read_reg_cache(w->codec, TWL4030_REG_HS_POPN_SET);
553 switch (event) {
554 case SND_SOC_DAPM_POST_PMU:
555 /* Do the anti-pop/bias ramp enable according to the TRM */
556 hs_pop |= TWL4030_VMID_EN;
557 twl4030_write(w->codec, TWL4030_REG_HS_POPN_SET, hs_pop);
558 /* Is this needed? Can we just use whatever gain here? */
559 twl4030_write(w->codec, TWL4030_REG_HS_GAIN_SET,
560 (hs_gain & (~0x0f)) | 0x0a);
561 hs_pop |= TWL4030_RAMP_EN;
562 twl4030_write(w->codec, TWL4030_REG_HS_POPN_SET, hs_pop);
564 /* Restore the original volume */
565 twl4030_write(w->codec, TWL4030_REG_HS_GAIN_SET, hs_gain);
566 break;
567 case SND_SOC_DAPM_POST_PMD:
568 /* Do the anti-pop/bias ramp disable according to the TRM */
569 hs_pop &= ~TWL4030_RAMP_EN;
570 twl4030_write(w->codec, TWL4030_REG_HS_POPN_SET, hs_pop);
571 /* Bypass the reg_cache to mute the headset */
572 twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
573 hs_gain & (~0x0f),
574 TWL4030_REG_HS_GAIN_SET);
575 hs_pop &= ~TWL4030_VMID_EN;
576 twl4030_write(w->codec, TWL4030_REG_HS_POPN_SET, hs_pop);
577 break;
579 return 0;
582 static int bypass_event(struct snd_soc_dapm_widget *w,
583 struct snd_kcontrol *kcontrol, int event)
585 struct soc_mixer_control *m =
586 (struct soc_mixer_control *)w->kcontrols->private_value;
587 struct twl4030_priv *twl4030 = w->codec->private_data;
588 unsigned char reg;
590 reg = twl4030_read_reg_cache(w->codec, m->reg);
592 if (m->reg <= TWL4030_REG_ARXR2_APGA_CTL) {
593 /* Analog bypass */
594 if (reg & (1 << m->shift))
595 twl4030->bypass_state |=
596 (1 << (m->reg - TWL4030_REG_ARXL1_APGA_CTL));
597 else
598 twl4030->bypass_state &=
599 ~(1 << (m->reg - TWL4030_REG_ARXL1_APGA_CTL));
600 } else {
601 /* Digital bypass */
602 if (reg & (0x7 << m->shift))
603 twl4030->bypass_state |= (1 << (m->shift ? 5 : 4));
604 else
605 twl4030->bypass_state &= ~(1 << (m->shift ? 5 : 4));
608 if (w->codec->bias_level == SND_SOC_BIAS_STANDBY) {
609 if (twl4030->bypass_state)
610 twl4030_codec_mute(w->codec, 0);
611 else
612 twl4030_codec_mute(w->codec, 1);
614 return 0;
618 * Some of the gain controls in TWL (mostly those which are associated with
619 * the outputs) are implemented in an interesting way:
620 * 0x0 : Power down (mute)
621 * 0x1 : 6dB
622 * 0x2 : 0 dB
623 * 0x3 : -6 dB
624 * Inverting not going to help with these.
625 * Custom volsw and volsw_2r get/put functions to handle these gain bits.
627 #define SOC_DOUBLE_TLV_TWL4030(xname, xreg, shift_left, shift_right, xmax,\
628 xinvert, tlv_array) \
629 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
630 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
631 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
632 .tlv.p = (tlv_array), \
633 .info = snd_soc_info_volsw, \
634 .get = snd_soc_get_volsw_twl4030, \
635 .put = snd_soc_put_volsw_twl4030, \
636 .private_value = (unsigned long)&(struct soc_mixer_control) \
637 {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
638 .max = xmax, .invert = xinvert} }
639 #define SOC_DOUBLE_R_TLV_TWL4030(xname, reg_left, reg_right, xshift, xmax,\
640 xinvert, tlv_array) \
641 { .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
642 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
643 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
644 .tlv.p = (tlv_array), \
645 .info = snd_soc_info_volsw_2r, \
646 .get = snd_soc_get_volsw_r2_twl4030,\
647 .put = snd_soc_put_volsw_r2_twl4030, \
648 .private_value = (unsigned long)&(struct soc_mixer_control) \
649 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
650 .rshift = xshift, .max = xmax, .invert = xinvert} }
651 #define SOC_SINGLE_TLV_TWL4030(xname, xreg, xshift, xmax, xinvert, tlv_array) \
652 SOC_DOUBLE_TLV_TWL4030(xname, xreg, xshift, xshift, xmax, \
653 xinvert, tlv_array)
655 static int snd_soc_get_volsw_twl4030(struct snd_kcontrol *kcontrol,
656 struct snd_ctl_elem_value *ucontrol)
658 struct soc_mixer_control *mc =
659 (struct soc_mixer_control *)kcontrol->private_value;
660 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
661 unsigned int reg = mc->reg;
662 unsigned int shift = mc->shift;
663 unsigned int rshift = mc->rshift;
664 int max = mc->max;
665 int mask = (1 << fls(max)) - 1;
667 ucontrol->value.integer.value[0] =
668 (snd_soc_read(codec, reg) >> shift) & mask;
669 if (ucontrol->value.integer.value[0])
670 ucontrol->value.integer.value[0] =
671 max + 1 - ucontrol->value.integer.value[0];
673 if (shift != rshift) {
674 ucontrol->value.integer.value[1] =
675 (snd_soc_read(codec, reg) >> rshift) & mask;
676 if (ucontrol->value.integer.value[1])
677 ucontrol->value.integer.value[1] =
678 max + 1 - ucontrol->value.integer.value[1];
681 return 0;
684 static int snd_soc_put_volsw_twl4030(struct snd_kcontrol *kcontrol,
685 struct snd_ctl_elem_value *ucontrol)
687 struct soc_mixer_control *mc =
688 (struct soc_mixer_control *)kcontrol->private_value;
689 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
690 unsigned int reg = mc->reg;
691 unsigned int shift = mc->shift;
692 unsigned int rshift = mc->rshift;
693 int max = mc->max;
694 int mask = (1 << fls(max)) - 1;
695 unsigned short val, val2, val_mask;
697 val = (ucontrol->value.integer.value[0] & mask);
699 val_mask = mask << shift;
700 if (val)
701 val = max + 1 - val;
702 val = val << shift;
703 if (shift != rshift) {
704 val2 = (ucontrol->value.integer.value[1] & mask);
705 val_mask |= mask << rshift;
706 if (val2)
707 val2 = max + 1 - val2;
708 val |= val2 << rshift;
710 return snd_soc_update_bits(codec, reg, val_mask, val);
713 static int snd_soc_get_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
714 struct snd_ctl_elem_value *ucontrol)
716 struct soc_mixer_control *mc =
717 (struct soc_mixer_control *)kcontrol->private_value;
718 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
719 unsigned int reg = mc->reg;
720 unsigned int reg2 = mc->rreg;
721 unsigned int shift = mc->shift;
722 int max = mc->max;
723 int mask = (1<<fls(max))-1;
725 ucontrol->value.integer.value[0] =
726 (snd_soc_read(codec, reg) >> shift) & mask;
727 ucontrol->value.integer.value[1] =
728 (snd_soc_read(codec, reg2) >> shift) & mask;
730 if (ucontrol->value.integer.value[0])
731 ucontrol->value.integer.value[0] =
732 max + 1 - ucontrol->value.integer.value[0];
733 if (ucontrol->value.integer.value[1])
734 ucontrol->value.integer.value[1] =
735 max + 1 - ucontrol->value.integer.value[1];
737 return 0;
740 static int snd_soc_put_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
741 struct snd_ctl_elem_value *ucontrol)
743 struct soc_mixer_control *mc =
744 (struct soc_mixer_control *)kcontrol->private_value;
745 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
746 unsigned int reg = mc->reg;
747 unsigned int reg2 = mc->rreg;
748 unsigned int shift = mc->shift;
749 int max = mc->max;
750 int mask = (1 << fls(max)) - 1;
751 int err;
752 unsigned short val, val2, val_mask;
754 val_mask = mask << shift;
755 val = (ucontrol->value.integer.value[0] & mask);
756 val2 = (ucontrol->value.integer.value[1] & mask);
758 if (val)
759 val = max + 1 - val;
760 if (val2)
761 val2 = max + 1 - val2;
763 val = val << shift;
764 val2 = val2 << shift;
766 err = snd_soc_update_bits(codec, reg, val_mask, val);
767 if (err < 0)
768 return err;
770 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
771 return err;
775 * FGAIN volume control:
776 * from -62 to 0 dB in 1 dB steps (mute instead of -63 dB)
778 static DECLARE_TLV_DB_SCALE(digital_fine_tlv, -6300, 100, 1);
781 * CGAIN volume control:
782 * 0 dB to 12 dB in 6 dB steps
783 * value 2 and 3 means 12 dB
785 static DECLARE_TLV_DB_SCALE(digital_coarse_tlv, 0, 600, 0);
788 * Voice Downlink GAIN volume control:
789 * from -37 to 12 dB in 1 dB steps (mute instead of -37 dB)
791 static DECLARE_TLV_DB_SCALE(digital_voice_downlink_tlv, -3700, 100, 1);
794 * Analog playback gain
795 * -24 dB to 12 dB in 2 dB steps
797 static DECLARE_TLV_DB_SCALE(analog_tlv, -2400, 200, 0);
800 * Gain controls tied to outputs
801 * -6 dB to 6 dB in 6 dB steps (mute instead of -12)
803 static DECLARE_TLV_DB_SCALE(output_tvl, -1200, 600, 1);
806 * Capture gain after the ADCs
807 * from 0 dB to 31 dB in 1 dB steps
809 static DECLARE_TLV_DB_SCALE(digital_capture_tlv, 0, 100, 0);
812 * Gain control for input amplifiers
813 * 0 dB to 30 dB in 6 dB steps
815 static DECLARE_TLV_DB_SCALE(input_gain_tlv, 0, 600, 0);
817 static const char *twl4030_rampdelay_texts[] = {
818 "27/20/14 ms", "55/40/27 ms", "109/81/55 ms", "218/161/109 ms",
819 "437/323/218 ms", "874/645/437 ms", "1748/1291/874 ms",
820 "3495/2581/1748 ms"
823 static const struct soc_enum twl4030_rampdelay_enum =
824 SOC_ENUM_SINGLE(TWL4030_REG_HS_POPN_SET, 2,
825 ARRAY_SIZE(twl4030_rampdelay_texts),
826 twl4030_rampdelay_texts);
828 static const struct snd_kcontrol_new twl4030_snd_controls[] = {
829 /* Common playback gain controls */
830 SOC_DOUBLE_R_TLV("DAC1 Digital Fine Playback Volume",
831 TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
832 0, 0x3f, 0, digital_fine_tlv),
833 SOC_DOUBLE_R_TLV("DAC2 Digital Fine Playback Volume",
834 TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
835 0, 0x3f, 0, digital_fine_tlv),
837 SOC_DOUBLE_R_TLV("DAC1 Digital Coarse Playback Volume",
838 TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
839 6, 0x2, 0, digital_coarse_tlv),
840 SOC_DOUBLE_R_TLV("DAC2 Digital Coarse Playback Volume",
841 TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
842 6, 0x2, 0, digital_coarse_tlv),
844 SOC_DOUBLE_R_TLV("DAC1 Analog Playback Volume",
845 TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
846 3, 0x12, 1, analog_tlv),
847 SOC_DOUBLE_R_TLV("DAC2 Analog Playback Volume",
848 TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
849 3, 0x12, 1, analog_tlv),
850 SOC_DOUBLE_R("DAC1 Analog Playback Switch",
851 TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
852 1, 1, 0),
853 SOC_DOUBLE_R("DAC2 Analog Playback Switch",
854 TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
855 1, 1, 0),
857 /* Common voice downlink gain controls */
858 SOC_SINGLE_TLV("DAC Voice Digital Downlink Volume",
859 TWL4030_REG_VRXPGA, 0, 0x31, 0, digital_voice_downlink_tlv),
861 SOC_SINGLE_TLV("DAC Voice Analog Downlink Volume",
862 TWL4030_REG_VDL_APGA_CTL, 3, 0x12, 1, analog_tlv),
864 SOC_SINGLE("DAC Voice Analog Downlink Switch",
865 TWL4030_REG_VDL_APGA_CTL, 1, 1, 0),
867 /* Separate output gain controls */
868 SOC_DOUBLE_R_TLV_TWL4030("PreDriv Playback Volume",
869 TWL4030_REG_PREDL_CTL, TWL4030_REG_PREDR_CTL,
870 4, 3, 0, output_tvl),
872 SOC_DOUBLE_TLV_TWL4030("Headset Playback Volume",
873 TWL4030_REG_HS_GAIN_SET, 0, 2, 3, 0, output_tvl),
875 SOC_DOUBLE_R_TLV_TWL4030("Carkit Playback Volume",
876 TWL4030_REG_PRECKL_CTL, TWL4030_REG_PRECKR_CTL,
877 4, 3, 0, output_tvl),
879 SOC_SINGLE_TLV_TWL4030("Earpiece Playback Volume",
880 TWL4030_REG_EAR_CTL, 4, 3, 0, output_tvl),
882 /* Common capture gain controls */
883 SOC_DOUBLE_R_TLV("TX1 Digital Capture Volume",
884 TWL4030_REG_ATXL1PGA, TWL4030_REG_ATXR1PGA,
885 0, 0x1f, 0, digital_capture_tlv),
886 SOC_DOUBLE_R_TLV("TX2 Digital Capture Volume",
887 TWL4030_REG_AVTXL2PGA, TWL4030_REG_AVTXR2PGA,
888 0, 0x1f, 0, digital_capture_tlv),
890 SOC_DOUBLE_TLV("Analog Capture Volume", TWL4030_REG_ANAMIC_GAIN,
891 0, 3, 5, 0, input_gain_tlv),
893 SOC_ENUM("HS ramp delay", twl4030_rampdelay_enum),
896 static const struct snd_soc_dapm_widget twl4030_dapm_widgets[] = {
897 /* Left channel inputs */
898 SND_SOC_DAPM_INPUT("MAINMIC"),
899 SND_SOC_DAPM_INPUT("HSMIC"),
900 SND_SOC_DAPM_INPUT("AUXL"),
901 SND_SOC_DAPM_INPUT("CARKITMIC"),
902 /* Right channel inputs */
903 SND_SOC_DAPM_INPUT("SUBMIC"),
904 SND_SOC_DAPM_INPUT("AUXR"),
905 /* Digital microphones (Stereo) */
906 SND_SOC_DAPM_INPUT("DIGIMIC0"),
907 SND_SOC_DAPM_INPUT("DIGIMIC1"),
909 /* Outputs */
910 SND_SOC_DAPM_OUTPUT("OUTL"),
911 SND_SOC_DAPM_OUTPUT("OUTR"),
912 SND_SOC_DAPM_OUTPUT("EARPIECE"),
913 SND_SOC_DAPM_OUTPUT("PREDRIVEL"),
914 SND_SOC_DAPM_OUTPUT("PREDRIVER"),
915 SND_SOC_DAPM_OUTPUT("HSOL"),
916 SND_SOC_DAPM_OUTPUT("HSOR"),
917 SND_SOC_DAPM_OUTPUT("CARKITL"),
918 SND_SOC_DAPM_OUTPUT("CARKITR"),
919 SND_SOC_DAPM_OUTPUT("HFL"),
920 SND_SOC_DAPM_OUTPUT("HFR"),
922 /* DACs */
923 SND_SOC_DAPM_DAC("DAC Right1", "Right Front Playback",
924 SND_SOC_NOPM, 0, 0),
925 SND_SOC_DAPM_DAC("DAC Left1", "Left Front Playback",
926 SND_SOC_NOPM, 0, 0),
927 SND_SOC_DAPM_DAC("DAC Right2", "Right Rear Playback",
928 SND_SOC_NOPM, 0, 0),
929 SND_SOC_DAPM_DAC("DAC Left2", "Left Rear Playback",
930 SND_SOC_NOPM, 0, 0),
931 SND_SOC_DAPM_DAC("DAC Voice", "Voice Playback",
932 TWL4030_REG_AVDAC_CTL, 4, 0),
934 /* Analog PGAs */
935 SND_SOC_DAPM_PGA("ARXR1_APGA", TWL4030_REG_ARXR1_APGA_CTL,
936 0, 0, NULL, 0),
937 SND_SOC_DAPM_PGA("ARXL1_APGA", TWL4030_REG_ARXL1_APGA_CTL,
938 0, 0, NULL, 0),
939 SND_SOC_DAPM_PGA("ARXR2_APGA", TWL4030_REG_ARXR2_APGA_CTL,
940 0, 0, NULL, 0),
941 SND_SOC_DAPM_PGA("ARXL2_APGA", TWL4030_REG_ARXL2_APGA_CTL,
942 0, 0, NULL, 0),
943 SND_SOC_DAPM_PGA("VDL_APGA", TWL4030_REG_VDL_APGA_CTL,
944 0, 0, NULL, 0),
946 /* Analog bypasses */
947 SND_SOC_DAPM_SWITCH_E("Right1 Analog Loopback", SND_SOC_NOPM, 0, 0,
948 &twl4030_dapm_abypassr1_control, bypass_event,
949 SND_SOC_DAPM_POST_REG),
950 SND_SOC_DAPM_SWITCH_E("Left1 Analog Loopback", SND_SOC_NOPM, 0, 0,
951 &twl4030_dapm_abypassl1_control,
952 bypass_event, SND_SOC_DAPM_POST_REG),
953 SND_SOC_DAPM_SWITCH_E("Right2 Analog Loopback", SND_SOC_NOPM, 0, 0,
954 &twl4030_dapm_abypassr2_control,
955 bypass_event, SND_SOC_DAPM_POST_REG),
956 SND_SOC_DAPM_SWITCH_E("Left2 Analog Loopback", SND_SOC_NOPM, 0, 0,
957 &twl4030_dapm_abypassl2_control,
958 bypass_event, SND_SOC_DAPM_POST_REG),
960 /* Digital bypasses */
961 SND_SOC_DAPM_SWITCH_E("Left Digital Loopback", SND_SOC_NOPM, 0, 0,
962 &twl4030_dapm_dbypassl_control, bypass_event,
963 SND_SOC_DAPM_POST_REG),
964 SND_SOC_DAPM_SWITCH_E("Right Digital Loopback", SND_SOC_NOPM, 0, 0,
965 &twl4030_dapm_dbypassr_control, bypass_event,
966 SND_SOC_DAPM_POST_REG),
968 SND_SOC_DAPM_MIXER("Analog R1 Playback Mixer", TWL4030_REG_AVDAC_CTL,
969 0, 0, NULL, 0),
970 SND_SOC_DAPM_MIXER("Analog L1 Playback Mixer", TWL4030_REG_AVDAC_CTL,
971 1, 0, NULL, 0),
972 SND_SOC_DAPM_MIXER("Analog R2 Playback Mixer", TWL4030_REG_AVDAC_CTL,
973 2, 0, NULL, 0),
974 SND_SOC_DAPM_MIXER("Analog L2 Playback Mixer", TWL4030_REG_AVDAC_CTL,
975 3, 0, NULL, 0),
977 /* Output MIXER controls */
978 /* Earpiece */
979 SND_SOC_DAPM_MIXER("Earpiece Mixer", SND_SOC_NOPM, 0, 0,
980 &twl4030_dapm_earpiece_controls[0],
981 ARRAY_SIZE(twl4030_dapm_earpiece_controls)),
982 /* PreDrivL/R */
983 SND_SOC_DAPM_MIXER("PredriveL Mixer", SND_SOC_NOPM, 0, 0,
984 &twl4030_dapm_predrivel_controls[0],
985 ARRAY_SIZE(twl4030_dapm_predrivel_controls)),
986 SND_SOC_DAPM_MIXER("PredriveR Mixer", SND_SOC_NOPM, 0, 0,
987 &twl4030_dapm_predriver_controls[0],
988 ARRAY_SIZE(twl4030_dapm_predriver_controls)),
989 /* HeadsetL/R */
990 SND_SOC_DAPM_MIXER_E("HeadsetL Mixer", SND_SOC_NOPM, 0, 0,
991 &twl4030_dapm_hsol_controls[0],
992 ARRAY_SIZE(twl4030_dapm_hsol_controls), headsetl_event,
993 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
994 SND_SOC_DAPM_MIXER("HeadsetR Mixer", SND_SOC_NOPM, 0, 0,
995 &twl4030_dapm_hsor_controls[0],
996 ARRAY_SIZE(twl4030_dapm_hsor_controls)),
997 /* CarkitL/R */
998 SND_SOC_DAPM_MIXER("CarkitL Mixer", SND_SOC_NOPM, 0, 0,
999 &twl4030_dapm_carkitl_controls[0],
1000 ARRAY_SIZE(twl4030_dapm_carkitl_controls)),
1001 SND_SOC_DAPM_MIXER("CarkitR Mixer", SND_SOC_NOPM, 0, 0,
1002 &twl4030_dapm_carkitr_controls[0],
1003 ARRAY_SIZE(twl4030_dapm_carkitr_controls)),
1005 /* Output MUX controls */
1006 /* HandsfreeL/R */
1007 SND_SOC_DAPM_MUX_E("HandsfreeL Mux", TWL4030_REG_HFL_CTL, 5, 0,
1008 &twl4030_dapm_handsfreel_control, handsfree_event,
1009 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
1010 SND_SOC_DAPM_MUX_E("HandsfreeR Mux", TWL4030_REG_HFR_CTL, 5, 0,
1011 &twl4030_dapm_handsfreer_control, handsfree_event,
1012 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
1014 /* Introducing four virtual ADC, since TWL4030 have four channel for
1015 capture */
1016 SND_SOC_DAPM_ADC("ADC Virtual Left1", "Left Front Capture",
1017 SND_SOC_NOPM, 0, 0),
1018 SND_SOC_DAPM_ADC("ADC Virtual Right1", "Right Front Capture",
1019 SND_SOC_NOPM, 0, 0),
1020 SND_SOC_DAPM_ADC("ADC Virtual Left2", "Left Rear Capture",
1021 SND_SOC_NOPM, 0, 0),
1022 SND_SOC_DAPM_ADC("ADC Virtual Right2", "Right Rear Capture",
1023 SND_SOC_NOPM, 0, 0),
1025 /* Analog/Digital mic path selection.
1026 TX1 Left/Right: either analog Left/Right or Digimic0
1027 TX2 Left/Right: either analog Left/Right or Digimic1 */
1028 SND_SOC_DAPM_MUX_E("TX1 Capture Route", SND_SOC_NOPM, 0, 0,
1029 &twl4030_dapm_micpathtx1_control, micpath_event,
1030 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD|
1031 SND_SOC_DAPM_POST_REG),
1032 SND_SOC_DAPM_MUX_E("TX2 Capture Route", SND_SOC_NOPM, 0, 0,
1033 &twl4030_dapm_micpathtx2_control, micpath_event,
1034 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD|
1035 SND_SOC_DAPM_POST_REG),
1037 /* Analog input muxes with switch for the capture amplifiers */
1038 SND_SOC_DAPM_VALUE_MUX("Analog Left Capture Route",
1039 TWL4030_REG_ANAMICL, 4, 0, &twl4030_dapm_analoglmic_control),
1040 SND_SOC_DAPM_VALUE_MUX("Analog Right Capture Route",
1041 TWL4030_REG_ANAMICR, 4, 0, &twl4030_dapm_analogrmic_control),
1043 SND_SOC_DAPM_PGA("ADC Physical Left",
1044 TWL4030_REG_AVADC_CTL, 3, 0, NULL, 0),
1045 SND_SOC_DAPM_PGA("ADC Physical Right",
1046 TWL4030_REG_AVADC_CTL, 1, 0, NULL, 0),
1048 SND_SOC_DAPM_PGA("Digimic0 Enable",
1049 TWL4030_REG_ADCMICSEL, 1, 0, NULL, 0),
1050 SND_SOC_DAPM_PGA("Digimic1 Enable",
1051 TWL4030_REG_ADCMICSEL, 3, 0, NULL, 0),
1053 SND_SOC_DAPM_MICBIAS("Mic Bias 1", TWL4030_REG_MICBIAS_CTL, 0, 0),
1054 SND_SOC_DAPM_MICBIAS("Mic Bias 2", TWL4030_REG_MICBIAS_CTL, 1, 0),
1055 SND_SOC_DAPM_MICBIAS("Headset Mic Bias", TWL4030_REG_MICBIAS_CTL, 2, 0),
1059 static const struct snd_soc_dapm_route intercon[] = {
1060 {"Analog L1 Playback Mixer", NULL, "DAC Left1"},
1061 {"Analog R1 Playback Mixer", NULL, "DAC Right1"},
1062 {"Analog L2 Playback Mixer", NULL, "DAC Left2"},
1063 {"Analog R2 Playback Mixer", NULL, "DAC Right2"},
1065 {"ARXL1_APGA", NULL, "Analog L1 Playback Mixer"},
1066 {"ARXR1_APGA", NULL, "Analog R1 Playback Mixer"},
1067 {"ARXL2_APGA", NULL, "Analog L2 Playback Mixer"},
1068 {"ARXR2_APGA", NULL, "Analog R2 Playback Mixer"},
1070 {"VDL_APGA", NULL, "DAC Voice"},
1072 /* Internal playback routings */
1073 /* Earpiece */
1074 {"Earpiece Mixer", "Voice", "VDL_APGA"},
1075 {"Earpiece Mixer", "AudioL1", "ARXL1_APGA"},
1076 {"Earpiece Mixer", "AudioL2", "ARXL2_APGA"},
1077 {"Earpiece Mixer", "AudioR1", "ARXR1_APGA"},
1078 /* PreDrivL */
1079 {"PredriveL Mixer", "Voice", "VDL_APGA"},
1080 {"PredriveL Mixer", "AudioL1", "ARXL1_APGA"},
1081 {"PredriveL Mixer", "AudioL2", "ARXL2_APGA"},
1082 {"PredriveL Mixer", "AudioR2", "ARXR2_APGA"},
1083 /* PreDrivR */
1084 {"PredriveR Mixer", "Voice", "VDL_APGA"},
1085 {"PredriveR Mixer", "AudioR1", "ARXR1_APGA"},
1086 {"PredriveR Mixer", "AudioR2", "ARXR2_APGA"},
1087 {"PredriveR Mixer", "AudioL2", "ARXL2_APGA"},
1088 /* HeadsetL */
1089 {"HeadsetL Mixer", "Voice", "VDL_APGA"},
1090 {"HeadsetL Mixer", "AudioL1", "ARXL1_APGA"},
1091 {"HeadsetL Mixer", "AudioL2", "ARXL2_APGA"},
1092 /* HeadsetR */
1093 {"HeadsetR Mixer", "Voice", "VDL_APGA"},
1094 {"HeadsetR Mixer", "AudioR1", "ARXR1_APGA"},
1095 {"HeadsetR Mixer", "AudioR2", "ARXR2_APGA"},
1096 /* CarkitL */
1097 {"CarkitL Mixer", "Voice", "VDL_APGA"},
1098 {"CarkitL Mixer", "AudioL1", "ARXL1_APGA"},
1099 {"CarkitL Mixer", "AudioL2", "ARXL2_APGA"},
1100 /* CarkitR */
1101 {"CarkitR Mixer", "Voice", "VDL_APGA"},
1102 {"CarkitR Mixer", "AudioR1", "ARXR1_APGA"},
1103 {"CarkitR Mixer", "AudioR2", "ARXR2_APGA"},
1104 /* HandsfreeL */
1105 {"HandsfreeL Mux", "Voice", "VDL_APGA"},
1106 {"HandsfreeL Mux", "AudioL1", "ARXL1_APGA"},
1107 {"HandsfreeL Mux", "AudioL2", "ARXL2_APGA"},
1108 {"HandsfreeL Mux", "AudioR2", "ARXR2_APGA"},
1109 /* HandsfreeR */
1110 {"HandsfreeR Mux", "Voice", "VDL_APGA"},
1111 {"HandsfreeR Mux", "AudioR1", "ARXR1_APGA"},
1112 {"HandsfreeR Mux", "AudioR2", "ARXR2_APGA"},
1113 {"HandsfreeR Mux", "AudioL2", "ARXL2_APGA"},
1115 /* outputs */
1116 {"OUTL", NULL, "ARXL2_APGA"},
1117 {"OUTR", NULL, "ARXR2_APGA"},
1118 {"EARPIECE", NULL, "Earpiece Mixer"},
1119 {"PREDRIVEL", NULL, "PredriveL Mixer"},
1120 {"PREDRIVER", NULL, "PredriveR Mixer"},
1121 {"HSOL", NULL, "HeadsetL Mixer"},
1122 {"HSOR", NULL, "HeadsetR Mixer"},
1123 {"CARKITL", NULL, "CarkitL Mixer"},
1124 {"CARKITR", NULL, "CarkitR Mixer"},
1125 {"HFL", NULL, "HandsfreeL Mux"},
1126 {"HFR", NULL, "HandsfreeR Mux"},
1128 /* Capture path */
1129 {"Analog Left Capture Route", "Main mic", "MAINMIC"},
1130 {"Analog Left Capture Route", "Headset mic", "HSMIC"},
1131 {"Analog Left Capture Route", "AUXL", "AUXL"},
1132 {"Analog Left Capture Route", "Carkit mic", "CARKITMIC"},
1134 {"Analog Right Capture Route", "Sub mic", "SUBMIC"},
1135 {"Analog Right Capture Route", "AUXR", "AUXR"},
1137 {"ADC Physical Left", NULL, "Analog Left Capture Route"},
1138 {"ADC Physical Right", NULL, "Analog Right Capture Route"},
1140 {"Digimic0 Enable", NULL, "DIGIMIC0"},
1141 {"Digimic1 Enable", NULL, "DIGIMIC1"},
1143 /* TX1 Left capture path */
1144 {"TX1 Capture Route", "Analog", "ADC Physical Left"},
1145 {"TX1 Capture Route", "Digimic0", "Digimic0 Enable"},
1146 /* TX1 Right capture path */
1147 {"TX1 Capture Route", "Analog", "ADC Physical Right"},
1148 {"TX1 Capture Route", "Digimic0", "Digimic0 Enable"},
1149 /* TX2 Left capture path */
1150 {"TX2 Capture Route", "Analog", "ADC Physical Left"},
1151 {"TX2 Capture Route", "Digimic1", "Digimic1 Enable"},
1152 /* TX2 Right capture path */
1153 {"TX2 Capture Route", "Analog", "ADC Physical Right"},
1154 {"TX2 Capture Route", "Digimic1", "Digimic1 Enable"},
1156 {"ADC Virtual Left1", NULL, "TX1 Capture Route"},
1157 {"ADC Virtual Right1", NULL, "TX1 Capture Route"},
1158 {"ADC Virtual Left2", NULL, "TX2 Capture Route"},
1159 {"ADC Virtual Right2", NULL, "TX2 Capture Route"},
1161 /* Analog bypass routes */
1162 {"Right1 Analog Loopback", "Switch", "Analog Right Capture Route"},
1163 {"Left1 Analog Loopback", "Switch", "Analog Left Capture Route"},
1164 {"Right2 Analog Loopback", "Switch", "Analog Right Capture Route"},
1165 {"Left2 Analog Loopback", "Switch", "Analog Left Capture Route"},
1167 {"Analog R1 Playback Mixer", NULL, "Right1 Analog Loopback"},
1168 {"Analog L1 Playback Mixer", NULL, "Left1 Analog Loopback"},
1169 {"Analog R2 Playback Mixer", NULL, "Right2 Analog Loopback"},
1170 {"Analog L2 Playback Mixer", NULL, "Left2 Analog Loopback"},
1172 /* Digital bypass routes */
1173 {"Right Digital Loopback", "Volume", "TX1 Capture Route"},
1174 {"Left Digital Loopback", "Volume", "TX1 Capture Route"},
1176 {"Analog R2 Playback Mixer", NULL, "Right Digital Loopback"},
1177 {"Analog L2 Playback Mixer", NULL, "Left Digital Loopback"},
1181 static int twl4030_add_widgets(struct snd_soc_codec *codec)
1183 snd_soc_dapm_new_controls(codec, twl4030_dapm_widgets,
1184 ARRAY_SIZE(twl4030_dapm_widgets));
1186 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
1188 snd_soc_dapm_new_widgets(codec);
1189 return 0;
1192 static int twl4030_set_bias_level(struct snd_soc_codec *codec,
1193 enum snd_soc_bias_level level)
1195 struct twl4030_priv *twl4030 = codec->private_data;
1197 switch (level) {
1198 case SND_SOC_BIAS_ON:
1199 twl4030_codec_mute(codec, 0);
1200 break;
1201 case SND_SOC_BIAS_PREPARE:
1202 twl4030_power_up(codec);
1203 if (twl4030->bypass_state)
1204 twl4030_codec_mute(codec, 0);
1205 else
1206 twl4030_codec_mute(codec, 1);
1207 break;
1208 case SND_SOC_BIAS_STANDBY:
1209 twl4030_power_up(codec);
1210 if (twl4030->bypass_state)
1211 twl4030_codec_mute(codec, 0);
1212 else
1213 twl4030_codec_mute(codec, 1);
1214 break;
1215 case SND_SOC_BIAS_OFF:
1216 twl4030_power_down(codec);
1217 break;
1219 codec->bias_level = level;
1221 return 0;
1224 static void twl4030_constraints(struct twl4030_priv *twl4030,
1225 struct snd_pcm_substream *mst_substream)
1227 struct snd_pcm_substream *slv_substream;
1229 /* Pick the stream, which need to be constrained */
1230 if (mst_substream == twl4030->master_substream)
1231 slv_substream = twl4030->slave_substream;
1232 else if (mst_substream == twl4030->slave_substream)
1233 slv_substream = twl4030->master_substream;
1234 else /* This should not happen.. */
1235 return;
1237 /* Set the constraints according to the already configured stream */
1238 snd_pcm_hw_constraint_minmax(slv_substream->runtime,
1239 SNDRV_PCM_HW_PARAM_RATE,
1240 twl4030->rate,
1241 twl4030->rate);
1243 snd_pcm_hw_constraint_minmax(slv_substream->runtime,
1244 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1245 twl4030->sample_bits,
1246 twl4030->sample_bits);
1248 snd_pcm_hw_constraint_minmax(slv_substream->runtime,
1249 SNDRV_PCM_HW_PARAM_CHANNELS,
1250 twl4030->channels,
1251 twl4030->channels);
1254 static int twl4030_startup(struct snd_pcm_substream *substream,
1255 struct snd_soc_dai *dai)
1257 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1258 struct snd_soc_device *socdev = rtd->socdev;
1259 struct snd_soc_codec *codec = socdev->card->codec;
1260 struct twl4030_priv *twl4030 = codec->private_data;
1262 if (twl4030->master_substream) {
1263 twl4030->slave_substream = substream;
1264 /* The DAI has one configuration for playback and capture, so
1265 * if the DAI has been already configured then constrain this
1266 * substream to match it. */
1267 if (twl4030->configured)
1268 twl4030_constraints(twl4030, twl4030->master_substream);
1269 } else {
1270 twl4030->master_substream = substream;
1273 return 0;
1276 static void twl4030_shutdown(struct snd_pcm_substream *substream,
1277 struct snd_soc_dai *dai)
1279 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1280 struct snd_soc_device *socdev = rtd->socdev;
1281 struct snd_soc_codec *codec = socdev->card->codec;
1282 struct twl4030_priv *twl4030 = codec->private_data;
1284 if (twl4030->master_substream == substream)
1285 twl4030->master_substream = twl4030->slave_substream;
1287 twl4030->slave_substream = NULL;
1289 /* If all streams are closed, or the remaining stream has not yet
1290 * been configured than set the DAI as not configured. */
1291 if (!twl4030->master_substream)
1292 twl4030->configured = 0;
1293 else if (!twl4030->master_substream->runtime->channels)
1294 twl4030->configured = 0;
1297 static int twl4030_hw_params(struct snd_pcm_substream *substream,
1298 struct snd_pcm_hw_params *params,
1299 struct snd_soc_dai *dai)
1301 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1302 struct snd_soc_device *socdev = rtd->socdev;
1303 struct snd_soc_codec *codec = socdev->card->codec;
1304 struct twl4030_priv *twl4030 = codec->private_data;
1305 u8 mode, old_mode, format, old_format;
1307 if (twl4030->configured)
1308 /* Ignoring hw_params for already configured DAI */
1309 return 0;
1311 /* bit rate */
1312 old_mode = twl4030_read_reg_cache(codec,
1313 TWL4030_REG_CODEC_MODE) & ~TWL4030_CODECPDZ;
1314 mode = old_mode & ~TWL4030_APLL_RATE;
1316 switch (params_rate(params)) {
1317 case 8000:
1318 mode |= TWL4030_APLL_RATE_8000;
1319 break;
1320 case 11025:
1321 mode |= TWL4030_APLL_RATE_11025;
1322 break;
1323 case 12000:
1324 mode |= TWL4030_APLL_RATE_12000;
1325 break;
1326 case 16000:
1327 mode |= TWL4030_APLL_RATE_16000;
1328 break;
1329 case 22050:
1330 mode |= TWL4030_APLL_RATE_22050;
1331 break;
1332 case 24000:
1333 mode |= TWL4030_APLL_RATE_24000;
1334 break;
1335 case 32000:
1336 mode |= TWL4030_APLL_RATE_32000;
1337 break;
1338 case 44100:
1339 mode |= TWL4030_APLL_RATE_44100;
1340 break;
1341 case 48000:
1342 mode |= TWL4030_APLL_RATE_48000;
1343 break;
1344 case 96000:
1345 mode |= TWL4030_APLL_RATE_96000;
1346 break;
1347 default:
1348 printk(KERN_ERR "TWL4030 hw params: unknown rate %d\n",
1349 params_rate(params));
1350 return -EINVAL;
1353 if (mode != old_mode) {
1354 /* change rate and set CODECPDZ */
1355 twl4030_codec_enable(codec, 0);
1356 twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode);
1357 twl4030_codec_enable(codec, 1);
1360 /* sample size */
1361 old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1362 format = old_format;
1363 format &= ~TWL4030_DATA_WIDTH;
1364 switch (params_format(params)) {
1365 case SNDRV_PCM_FORMAT_S16_LE:
1366 format |= TWL4030_DATA_WIDTH_16S_16W;
1367 break;
1368 case SNDRV_PCM_FORMAT_S24_LE:
1369 format |= TWL4030_DATA_WIDTH_32S_24W;
1370 break;
1371 default:
1372 printk(KERN_ERR "TWL4030 hw params: unknown format %d\n",
1373 params_format(params));
1374 return -EINVAL;
1377 if (format != old_format) {
1379 /* clear CODECPDZ before changing format (codec requirement) */
1380 twl4030_codec_enable(codec, 0);
1382 /* change format */
1383 twl4030_write(codec, TWL4030_REG_AUDIO_IF, format);
1385 /* set CODECPDZ afterwards */
1386 twl4030_codec_enable(codec, 1);
1389 /* Store the important parameters for the DAI configuration and set
1390 * the DAI as configured */
1391 twl4030->configured = 1;
1392 twl4030->rate = params_rate(params);
1393 twl4030->sample_bits = hw_param_interval(params,
1394 SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
1395 twl4030->channels = params_channels(params);
1397 /* If both playback and capture streams are open, and one of them
1398 * is setting the hw parameters right now (since we are here), set
1399 * constraints to the other stream to match the current one. */
1400 if (twl4030->slave_substream)
1401 twl4030_constraints(twl4030, substream);
1403 return 0;
1406 static int twl4030_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1407 int clk_id, unsigned int freq, int dir)
1409 struct snd_soc_codec *codec = codec_dai->codec;
1410 u8 infreq;
1412 switch (freq) {
1413 case 19200000:
1414 infreq = TWL4030_APLL_INFREQ_19200KHZ;
1415 break;
1416 case 26000000:
1417 infreq = TWL4030_APLL_INFREQ_26000KHZ;
1418 break;
1419 case 38400000:
1420 infreq = TWL4030_APLL_INFREQ_38400KHZ;
1421 break;
1422 default:
1423 printk(KERN_ERR "TWL4030 set sysclk: unknown rate %d\n",
1424 freq);
1425 return -EINVAL;
1428 infreq |= TWL4030_APLL_EN;
1429 twl4030_write(codec, TWL4030_REG_APLL_CTL, infreq);
1431 return 0;
1434 static int twl4030_set_dai_fmt(struct snd_soc_dai *codec_dai,
1435 unsigned int fmt)
1437 struct snd_soc_codec *codec = codec_dai->codec;
1438 u8 old_format, format;
1440 /* get format */
1441 old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1442 format = old_format;
1444 /* set master/slave audio interface */
1445 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1446 case SND_SOC_DAIFMT_CBM_CFM:
1447 format &= ~(TWL4030_AIF_SLAVE_EN);
1448 format &= ~(TWL4030_CLK256FS_EN);
1449 break;
1450 case SND_SOC_DAIFMT_CBS_CFS:
1451 format |= TWL4030_AIF_SLAVE_EN;
1452 format |= TWL4030_CLK256FS_EN;
1453 break;
1454 default:
1455 return -EINVAL;
1458 /* interface format */
1459 format &= ~TWL4030_AIF_FORMAT;
1460 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1461 case SND_SOC_DAIFMT_I2S:
1462 format |= TWL4030_AIF_FORMAT_CODEC;
1463 break;
1464 default:
1465 return -EINVAL;
1468 if (format != old_format) {
1470 /* clear CODECPDZ before changing format (codec requirement) */
1471 twl4030_codec_enable(codec, 0);
1473 /* change format */
1474 twl4030_write(codec, TWL4030_REG_AUDIO_IF, format);
1476 /* set CODECPDZ afterwards */
1477 twl4030_codec_enable(codec, 1);
1480 return 0;
1483 static int twl4030_voice_startup(struct snd_pcm_substream *substream,
1484 struct snd_soc_dai *dai)
1486 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1487 struct snd_soc_device *socdev = rtd->socdev;
1488 struct snd_soc_codec *codec = socdev->card->codec;
1489 u8 infreq;
1490 u8 mode;
1492 /* If the system master clock is not 26MHz, the voice PCM interface is
1493 * not avilable.
1495 infreq = twl4030_read_reg_cache(codec, TWL4030_REG_APLL_CTL)
1496 & TWL4030_APLL_INFREQ;
1498 if (infreq != TWL4030_APLL_INFREQ_26000KHZ) {
1499 printk(KERN_ERR "TWL4030 voice startup: "
1500 "MCLK is not 26MHz, call set_sysclk() on init\n");
1501 return -EINVAL;
1504 /* If the codec mode is not option2, the voice PCM interface is not
1505 * avilable.
1507 mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE)
1508 & TWL4030_OPT_MODE;
1510 if (mode != TWL4030_OPTION_2) {
1511 printk(KERN_ERR "TWL4030 voice startup: "
1512 "the codec mode is not option2\n");
1513 return -EINVAL;
1516 return 0;
1519 static int twl4030_voice_hw_params(struct snd_pcm_substream *substream,
1520 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
1522 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1523 struct snd_soc_device *socdev = rtd->socdev;
1524 struct snd_soc_codec *codec = socdev->card->codec;
1525 u8 old_mode, mode;
1527 /* bit rate */
1528 old_mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE)
1529 & ~(TWL4030_CODECPDZ);
1530 mode = old_mode;
1532 switch (params_rate(params)) {
1533 case 8000:
1534 mode &= ~(TWL4030_SEL_16K);
1535 break;
1536 case 16000:
1537 mode |= TWL4030_SEL_16K;
1538 break;
1539 default:
1540 printk(KERN_ERR "TWL4030 voice hw params: unknown rate %d\n",
1541 params_rate(params));
1542 return -EINVAL;
1545 if (mode != old_mode) {
1546 /* change rate and set CODECPDZ */
1547 twl4030_codec_enable(codec, 0);
1548 twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode);
1549 twl4030_codec_enable(codec, 1);
1552 return 0;
1555 static int twl4030_voice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1556 int clk_id, unsigned int freq, int dir)
1558 struct snd_soc_codec *codec = codec_dai->codec;
1559 u8 infreq;
1561 switch (freq) {
1562 case 26000000:
1563 infreq = TWL4030_APLL_INFREQ_26000KHZ;
1564 break;
1565 default:
1566 printk(KERN_ERR "TWL4030 voice set sysclk: unknown rate %d\n",
1567 freq);
1568 return -EINVAL;
1571 infreq |= TWL4030_APLL_EN;
1572 twl4030_write(codec, TWL4030_REG_APLL_CTL, infreq);
1574 return 0;
1577 static int twl4030_voice_set_dai_fmt(struct snd_soc_dai *codec_dai,
1578 unsigned int fmt)
1580 struct snd_soc_codec *codec = codec_dai->codec;
1581 u8 old_format, format;
1583 /* get format */
1584 old_format = twl4030_read_reg_cache(codec, TWL4030_REG_VOICE_IF);
1585 format = old_format;
1587 /* set master/slave audio interface */
1588 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1589 case SND_SOC_DAIFMT_CBS_CFM:
1590 format &= ~(TWL4030_VIF_SLAVE_EN);
1591 break;
1592 case SND_SOC_DAIFMT_CBS_CFS:
1593 format |= TWL4030_VIF_SLAVE_EN;
1594 break;
1595 default:
1596 return -EINVAL;
1599 /* clock inversion */
1600 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1601 case SND_SOC_DAIFMT_IB_NF:
1602 format &= ~(TWL4030_VIF_FORMAT);
1603 break;
1604 case SND_SOC_DAIFMT_NB_IF:
1605 format |= TWL4030_VIF_FORMAT;
1606 break;
1607 default:
1608 return -EINVAL;
1611 if (format != old_format) {
1612 /* change format and set CODECPDZ */
1613 twl4030_codec_enable(codec, 0);
1614 twl4030_write(codec, TWL4030_REG_VOICE_IF, format);
1615 twl4030_codec_enable(codec, 1);
1618 return 0;
1621 #define TWL4030_RATES (SNDRV_PCM_RATE_8000_48000)
1622 #define TWL4030_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FORMAT_S24_LE)
1624 static struct snd_soc_dai_ops twl4030_dai_ops = {
1625 .startup = twl4030_startup,
1626 .shutdown = twl4030_shutdown,
1627 .hw_params = twl4030_hw_params,
1628 .set_sysclk = twl4030_set_dai_sysclk,
1629 .set_fmt = twl4030_set_dai_fmt,
1632 static struct snd_soc_dai_ops twl4030_dai_voice_ops = {
1633 .startup = twl4030_voice_startup,
1634 .hw_params = twl4030_voice_hw_params,
1635 .set_sysclk = twl4030_voice_set_dai_sysclk,
1636 .set_fmt = twl4030_voice_set_dai_fmt,
1639 struct snd_soc_dai twl4030_dai[] = {
1641 .name = "twl4030",
1642 .playback = {
1643 .stream_name = "Playback",
1644 .channels_min = 2,
1645 .channels_max = 2,
1646 .rates = TWL4030_RATES | SNDRV_PCM_RATE_96000,
1647 .formats = TWL4030_FORMATS,},
1648 .capture = {
1649 .stream_name = "Capture",
1650 .channels_min = 2,
1651 .channels_max = 2,
1652 .rates = TWL4030_RATES,
1653 .formats = TWL4030_FORMATS,},
1654 .ops = &twl4030_dai_ops,
1657 .name = "twl4030 Voice",
1658 .playback = {
1659 .stream_name = "Playback",
1660 .channels_min = 1,
1661 .channels_max = 1,
1662 .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
1663 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
1664 .capture = {
1665 .stream_name = "Capture",
1666 .channels_min = 1,
1667 .channels_max = 2,
1668 .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
1669 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
1670 .ops = &twl4030_dai_voice_ops,
1673 EXPORT_SYMBOL_GPL(twl4030_dai);
1675 static int twl4030_suspend(struct platform_device *pdev, pm_message_t state)
1677 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1678 struct snd_soc_codec *codec = socdev->card->codec;
1680 twl4030_set_bias_level(codec, SND_SOC_BIAS_OFF);
1682 return 0;
1685 static int twl4030_resume(struct platform_device *pdev)
1687 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1688 struct snd_soc_codec *codec = socdev->card->codec;
1690 twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1691 twl4030_set_bias_level(codec, codec->suspend_bias_level);
1692 return 0;
1696 * initialize the driver
1697 * register the mixer and dsp interfaces with the kernel
1700 static int twl4030_init(struct snd_soc_device *socdev)
1702 struct snd_soc_codec *codec = socdev->card->codec;
1703 int ret = 0;
1705 printk(KERN_INFO "TWL4030 Audio Codec init \n");
1707 codec->name = "twl4030";
1708 codec->owner = THIS_MODULE;
1709 codec->read = twl4030_read_reg_cache;
1710 codec->write = twl4030_write;
1711 codec->set_bias_level = twl4030_set_bias_level;
1712 codec->dai = twl4030_dai;
1713 codec->num_dai = ARRAY_SIZE(twl4030_dai),
1714 codec->reg_cache_size = sizeof(twl4030_reg);
1715 codec->reg_cache = kmemdup(twl4030_reg, sizeof(twl4030_reg),
1716 GFP_KERNEL);
1717 if (codec->reg_cache == NULL)
1718 return -ENOMEM;
1720 /* register pcms */
1721 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
1722 if (ret < 0) {
1723 printk(KERN_ERR "twl4030: failed to create pcms\n");
1724 goto pcm_err;
1727 twl4030_init_chip(codec);
1729 /* power on device */
1730 twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1732 snd_soc_add_controls(codec, twl4030_snd_controls,
1733 ARRAY_SIZE(twl4030_snd_controls));
1734 twl4030_add_widgets(codec);
1736 ret = snd_soc_init_card(socdev);
1737 if (ret < 0) {
1738 printk(KERN_ERR "twl4030: failed to register card\n");
1739 goto card_err;
1742 return ret;
1744 card_err:
1745 snd_soc_free_pcms(socdev);
1746 snd_soc_dapm_free(socdev);
1747 pcm_err:
1748 kfree(codec->reg_cache);
1749 return ret;
1752 static struct snd_soc_device *twl4030_socdev;
1754 static int twl4030_probe(struct platform_device *pdev)
1756 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1757 struct snd_soc_codec *codec;
1758 struct twl4030_priv *twl4030;
1760 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
1761 if (codec == NULL)
1762 return -ENOMEM;
1764 twl4030 = kzalloc(sizeof(struct twl4030_priv), GFP_KERNEL);
1765 if (twl4030 == NULL) {
1766 kfree(codec);
1767 return -ENOMEM;
1770 codec->private_data = twl4030;
1771 socdev->card->codec = codec;
1772 mutex_init(&codec->mutex);
1773 INIT_LIST_HEAD(&codec->dapm_widgets);
1774 INIT_LIST_HEAD(&codec->dapm_paths);
1776 twl4030_socdev = socdev;
1777 twl4030_init(socdev);
1779 return 0;
1782 static int twl4030_remove(struct platform_device *pdev)
1784 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1785 struct snd_soc_codec *codec = socdev->card->codec;
1787 printk(KERN_INFO "TWL4030 Audio Codec remove\n");
1788 twl4030_set_bias_level(codec, SND_SOC_BIAS_OFF);
1789 snd_soc_free_pcms(socdev);
1790 snd_soc_dapm_free(socdev);
1791 kfree(codec->private_data);
1792 kfree(codec);
1794 return 0;
1797 struct snd_soc_codec_device soc_codec_dev_twl4030 = {
1798 .probe = twl4030_probe,
1799 .remove = twl4030_remove,
1800 .suspend = twl4030_suspend,
1801 .resume = twl4030_resume,
1803 EXPORT_SYMBOL_GPL(soc_codec_dev_twl4030);
1805 static int __init twl4030_modinit(void)
1807 return snd_soc_register_dais(&twl4030_dai[0], ARRAY_SIZE(twl4030_dai));
1809 module_init(twl4030_modinit);
1811 static void __exit twl4030_exit(void)
1813 snd_soc_unregister_dais(&twl4030_dai[0], ARRAY_SIZE(twl4030_dai));
1815 module_exit(twl4030_exit);
1817 MODULE_DESCRIPTION("ASoC TWL4030 codec driver");
1818 MODULE_AUTHOR("Steve Sakoman");
1819 MODULE_LICENSE("GPL");