blob: 9163713a030794f1a5b3305f30b1a3628f0b6747 [file] [log] [blame]
Steve Sakomancc175572008-10-30 21:35:26 -07001/*
2 * ALSA SoC TWL4030 codec driver
3 *
4 * Author: Steve Sakoman, <steve@sakoman.com>
5 *
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.
9 *
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.
14 *
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
19 *
20 */
21
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>
Peter Ujfalusic10b82c2008-11-24 13:49:35 +020036#include <sound/tlv.h>
Steve Sakomancc175572008-10-30 21:35:26 -070037
38#include "twl4030.h"
39
40/*
41 * twl4030 register cache & default register settings
42 */
43static const u8 twl4030_reg[TWL4030_CACHEREGNUM] = {
44 0x00, /* this register not used */
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +020045 0x91, /* REG_CODEC_MODE (0x1) */
Steve Sakomancc175572008-10-30 21:35:26 -070046 0xc3, /* REG_OPTION (0x2) */
47 0x00, /* REG_UNKNOWN (0x3) */
48 0x00, /* REG_MICBIAS_CTL (0x4) */
Grazvydas Ignotas5920b452008-12-02 20:48:58 +020049 0x20, /* REG_ANAMICL (0x5) */
50 0x00, /* REG_ANAMICR (0x6) */
51 0x00, /* REG_AVADC_CTL (0x7) */
Steve Sakomancc175572008-10-30 21:35:26 -070052 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) */
Peter Ujfalusif8d05bd2008-11-24 08:25:45 +020091 0x00, /* REG_SOFTVOL_CTL (0x2F) */
Steve Sakomancc175572008-10-30 21:35:26 -070092 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) */
Peter Ujfalusif3b5d302009-05-25 11:12:12 +0300118 0x00, /* REG_SW_SHADOW (0x4A) - Shadow, non HW register */
Steve Sakomancc175572008-10-30 21:35:26 -0700119};
120
Peter Ujfalusi73939582009-01-29 14:57:50 +0200121/* codec private data */
122struct twl4030_priv {
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300123 struct snd_soc_codec codec;
124
Peter Ujfalusi73939582009-01-29 14:57:50 +0200125 unsigned int codec_powered;
Peter Ujfalusi2845fa12009-10-28 10:57:05 +0200126 unsigned int apll_enabled;
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +0200127
128 struct snd_pcm_substream *master_substream;
129 struct snd_pcm_substream *slave_substream;
Peter Ujfalusi6b87a912009-04-17 15:55:08 +0300130
131 unsigned int configured;
132 unsigned int rate;
133 unsigned int sample_bits;
134 unsigned int channels;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300135
136 unsigned int sysclk;
137
138 /* Headset output state handling */
139 unsigned int hsl_enabled;
140 unsigned int hsr_enabled;
Peter Ujfalusi73939582009-01-29 14:57:50 +0200141};
142
Steve Sakomancc175572008-10-30 21:35:26 -0700143/*
144 * read twl4030 register cache
145 */
146static inline unsigned int twl4030_read_reg_cache(struct snd_soc_codec *codec,
147 unsigned int reg)
148{
Takashi Iwaid08664f2009-06-04 09:58:18 +0200149 u8 *cache = codec->reg_cache;
Steve Sakomancc175572008-10-30 21:35:26 -0700150
Ian Molton91432e92009-01-17 17:44:23 +0000151 if (reg >= TWL4030_CACHEREGNUM)
152 return -EIO;
153
Steve Sakomancc175572008-10-30 21:35:26 -0700154 return cache[reg];
155}
156
157/*
158 * write twl4030 register cache
159 */
160static inline void twl4030_write_reg_cache(struct snd_soc_codec *codec,
161 u8 reg, u8 value)
162{
163 u8 *cache = codec->reg_cache;
164
165 if (reg >= TWL4030_CACHEREGNUM)
166 return;
167 cache[reg] = value;
168}
169
170/*
171 * write to the twl4030 register space
172 */
173static int twl4030_write(struct snd_soc_codec *codec,
174 unsigned int reg, unsigned int value)
175{
176 twl4030_write_reg_cache(codec, reg, value);
Peter Ujfalusif3b5d302009-05-25 11:12:12 +0300177 if (likely(reg < TWL4030_REG_SW_SHADOW))
178 return twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, value,
179 reg);
180 else
181 return 0;
Steve Sakomancc175572008-10-30 21:35:26 -0700182}
183
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +0200184static void twl4030_codec_enable(struct snd_soc_codec *codec, int enable)
Steve Sakomancc175572008-10-30 21:35:26 -0700185{
Peter Ujfalusi73939582009-01-29 14:57:50 +0200186 struct twl4030_priv *twl4030 = codec->private_data;
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300187 int mode;
Steve Sakomancc175572008-10-30 21:35:26 -0700188
Peter Ujfalusi73939582009-01-29 14:57:50 +0200189 if (enable == twl4030->codec_powered)
190 return;
191
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +0200192 if (enable)
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300193 mode = twl4030_codec_enable_resource(TWL4030_CODEC_RES_POWER);
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +0200194 else
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300195 mode = twl4030_codec_disable_resource(TWL4030_CODEC_RES_POWER);
Steve Sakomancc175572008-10-30 21:35:26 -0700196
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300197 if (mode >= 0) {
198 twl4030_write_reg_cache(codec, TWL4030_REG_CODEC_MODE, mode);
199 twl4030->codec_powered = enable;
200 }
Steve Sakomancc175572008-10-30 21:35:26 -0700201
202 /* REVISIT: this delay is present in TI sample drivers */
203 /* but there seems to be no TRM requirement for it */
204 udelay(10);
205}
206
207static void twl4030_init_chip(struct snd_soc_codec *codec)
208{
Peter Ujfalusi16a30fb2009-05-29 09:22:37 +0300209 u8 *cache = codec->reg_cache;
Steve Sakomancc175572008-10-30 21:35:26 -0700210 int i;
211
212 /* clear CODECPDZ prior to setting register defaults */
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +0200213 twl4030_codec_enable(codec, 0);
Steve Sakomancc175572008-10-30 21:35:26 -0700214
215 /* set all audio section registers to reasonable defaults */
216 for (i = TWL4030_REG_OPTION; i <= TWL4030_REG_MISC_SET_2; i++)
Peter Ujfalusi16a30fb2009-05-29 09:22:37 +0300217 twl4030_write(codec, i, cache[i]);
Steve Sakomancc175572008-10-30 21:35:26 -0700218
219}
220
Peter Ujfalusi2845fa12009-10-28 10:57:05 +0200221static void twl4030_apll_enable(struct snd_soc_codec *codec, int enable)
Peter Ujfalusi73939582009-01-29 14:57:50 +0200222{
223 struct twl4030_priv *twl4030 = codec->private_data;
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300224 int status;
Peter Ujfalusi73939582009-01-29 14:57:50 +0200225
Peter Ujfalusi2845fa12009-10-28 10:57:05 +0200226 if (enable == twl4030->apll_enabled)
Peter Ujfalusi73939582009-01-29 14:57:50 +0200227 return;
228
Peter Ujfalusi2845fa12009-10-28 10:57:05 +0200229 if (enable)
Peter Ujfalusi73939582009-01-29 14:57:50 +0200230 /* Enable PLL */
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300231 status = twl4030_codec_enable_resource(TWL4030_CODEC_RES_APLL);
Peter Ujfalusi2845fa12009-10-28 10:57:05 +0200232 else
233 /* Disable PLL */
234 status = twl4030_codec_disable_resource(TWL4030_CODEC_RES_APLL);
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300235
236 if (status >= 0)
237 twl4030_write_reg_cache(codec, TWL4030_REG_APLL_CTL, status);
Peter Ujfalusi73939582009-01-29 14:57:50 +0200238
Peter Ujfalusi2845fa12009-10-28 10:57:05 +0200239 twl4030->apll_enabled = enable;
Peter Ujfalusi73939582009-01-29 14:57:50 +0200240}
241
Peter Ujfalusi006f367e2009-01-27 11:29:43 +0200242static void twl4030_power_up(struct snd_soc_codec *codec)
243{
Peter Ujfalusi73939582009-01-29 14:57:50 +0200244 struct twl4030_priv *twl4030 = codec->private_data;
Peter Ujfalusi006f367e2009-01-27 11:29:43 +0200245 u8 anamicl, regmisc1, byte;
246 int i = 0;
247
Peter Ujfalusi73939582009-01-29 14:57:50 +0200248 if (twl4030->codec_powered)
249 return;
250
Peter Ujfalusi006f367e2009-01-27 11:29:43 +0200251 /* set CODECPDZ to turn on codec */
252 twl4030_codec_enable(codec, 1);
253
254 /* initiate offset cancellation */
255 anamicl = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICL);
256 twl4030_write(codec, TWL4030_REG_ANAMICL,
257 anamicl | TWL4030_CNCL_OFFSET_START);
258
259 /* wait for offset cancellation to complete */
260 do {
261 /* this takes a little while, so don't slam i2c */
262 udelay(2000);
263 twl4030_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &byte,
264 TWL4030_REG_ANAMICL);
265 } while ((i++ < 100) &&
266 ((byte & TWL4030_CNCL_OFFSET_START) ==
267 TWL4030_CNCL_OFFSET_START));
268
269 /* Make sure that the reg_cache has the same value as the HW */
270 twl4030_write_reg_cache(codec, TWL4030_REG_ANAMICL, byte);
271
272 /* anti-pop when changing analog gain */
273 regmisc1 = twl4030_read_reg_cache(codec, TWL4030_REG_MISC_SET_1);
274 twl4030_write(codec, TWL4030_REG_MISC_SET_1,
275 regmisc1 | TWL4030_SMOOTH_ANAVOL_EN);
276
277 /* toggle CODECPDZ as per TRM */
278 twl4030_codec_enable(codec, 0);
279 twl4030_codec_enable(codec, 1);
280}
281
Peter Ujfalusi73939582009-01-29 14:57:50 +0200282/*
283 * Unconditional power down
284 */
Peter Ujfalusi006f367e2009-01-27 11:29:43 +0200285static void twl4030_power_down(struct snd_soc_codec *codec)
286{
287 /* power down */
288 twl4030_codec_enable(codec, 0);
289}
290
Peter Ujfalusi5e98a462008-12-09 12:35:47 +0200291/* Earpiece */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900292static const struct snd_kcontrol_new twl4030_dapm_earpiece_controls[] = {
293 SOC_DAPM_SINGLE("Voice", TWL4030_REG_EAR_CTL, 0, 1, 0),
294 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_EAR_CTL, 1, 1, 0),
295 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_EAR_CTL, 2, 1, 0),
296 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_EAR_CTL, 3, 1, 0),
297};
Peter Ujfalusi5e98a462008-12-09 12:35:47 +0200298
Peter Ujfalusi2a6f5c52008-12-09 12:35:48 +0200299/* PreDrive Left */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900300static const struct snd_kcontrol_new twl4030_dapm_predrivel_controls[] = {
301 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PREDL_CTL, 0, 1, 0),
302 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_PREDL_CTL, 1, 1, 0),
303 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PREDL_CTL, 2, 1, 0),
304 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PREDL_CTL, 3, 1, 0),
305};
Peter Ujfalusi2a6f5c52008-12-09 12:35:48 +0200306
307/* PreDrive Right */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900308static const struct snd_kcontrol_new twl4030_dapm_predriver_controls[] = {
309 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PREDR_CTL, 0, 1, 0),
310 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_PREDR_CTL, 1, 1, 0),
311 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PREDR_CTL, 2, 1, 0),
312 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PREDR_CTL, 3, 1, 0),
313};
Peter Ujfalusi2a6f5c52008-12-09 12:35:48 +0200314
Peter Ujfalusidfad21a2008-12-09 12:35:49 +0200315/* Headset Left */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900316static const struct snd_kcontrol_new twl4030_dapm_hsol_controls[] = {
317 SOC_DAPM_SINGLE("Voice", TWL4030_REG_HS_SEL, 0, 1, 0),
318 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_HS_SEL, 1, 1, 0),
319 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_HS_SEL, 2, 1, 0),
320};
Peter Ujfalusidfad21a2008-12-09 12:35:49 +0200321
322/* Headset Right */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900323static const struct snd_kcontrol_new twl4030_dapm_hsor_controls[] = {
324 SOC_DAPM_SINGLE("Voice", TWL4030_REG_HS_SEL, 3, 1, 0),
325 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_HS_SEL, 4, 1, 0),
326 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_HS_SEL, 5, 1, 0),
327};
Peter Ujfalusidfad21a2008-12-09 12:35:49 +0200328
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +0200329/* Carkit Left */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900330static const struct snd_kcontrol_new twl4030_dapm_carkitl_controls[] = {
331 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PRECKL_CTL, 0, 1, 0),
332 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_PRECKL_CTL, 1, 1, 0),
333 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PRECKL_CTL, 2, 1, 0),
334};
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +0200335
336/* Carkit Right */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900337static const struct snd_kcontrol_new twl4030_dapm_carkitr_controls[] = {
338 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PRECKR_CTL, 0, 1, 0),
339 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_PRECKR_CTL, 1, 1, 0),
340 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PRECKR_CTL, 2, 1, 0),
341};
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +0200342
Peter Ujfalusidf339802008-12-09 12:35:51 +0200343/* Handsfree Left */
344static const char *twl4030_handsfreel_texts[] =
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900345 {"Voice", "AudioL1", "AudioL2", "AudioR2"};
Peter Ujfalusidf339802008-12-09 12:35:51 +0200346
347static const struct soc_enum twl4030_handsfreel_enum =
348 SOC_ENUM_SINGLE(TWL4030_REG_HFL_CTL, 0,
349 ARRAY_SIZE(twl4030_handsfreel_texts),
350 twl4030_handsfreel_texts);
351
352static const struct snd_kcontrol_new twl4030_dapm_handsfreel_control =
353SOC_DAPM_ENUM("Route", twl4030_handsfreel_enum);
354
Peter Ujfalusi0f89bdc2009-05-25 11:12:13 +0300355/* Handsfree Left virtual mute */
356static const struct snd_kcontrol_new twl4030_dapm_handsfreelmute_control =
357 SOC_DAPM_SINGLE("Switch", TWL4030_REG_SW_SHADOW, 0, 1, 0);
358
Peter Ujfalusidf339802008-12-09 12:35:51 +0200359/* Handsfree Right */
360static const char *twl4030_handsfreer_texts[] =
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900361 {"Voice", "AudioR1", "AudioR2", "AudioL2"};
Peter Ujfalusidf339802008-12-09 12:35:51 +0200362
363static const struct soc_enum twl4030_handsfreer_enum =
364 SOC_ENUM_SINGLE(TWL4030_REG_HFR_CTL, 0,
365 ARRAY_SIZE(twl4030_handsfreer_texts),
366 twl4030_handsfreer_texts);
367
368static const struct snd_kcontrol_new twl4030_dapm_handsfreer_control =
369SOC_DAPM_ENUM("Route", twl4030_handsfreer_enum);
370
Peter Ujfalusi0f89bdc2009-05-25 11:12:13 +0300371/* Handsfree Right virtual mute */
372static const struct snd_kcontrol_new twl4030_dapm_handsfreermute_control =
373 SOC_DAPM_SINGLE("Switch", TWL4030_REG_SW_SHADOW, 1, 1, 0);
374
Peter Ujfalusi376f7832009-05-05 08:55:47 +0300375/* Vibra */
376/* Vibra audio path selection */
377static const char *twl4030_vibra_texts[] =
378 {"AudioL1", "AudioR1", "AudioL2", "AudioR2"};
379
380static const struct soc_enum twl4030_vibra_enum =
381 SOC_ENUM_SINGLE(TWL4030_REG_VIBRA_CTL, 2,
382 ARRAY_SIZE(twl4030_vibra_texts),
383 twl4030_vibra_texts);
384
385static const struct snd_kcontrol_new twl4030_dapm_vibra_control =
386SOC_DAPM_ENUM("Route", twl4030_vibra_enum);
387
388/* Vibra path selection: local vibrator (PWM) or audio driven */
389static const char *twl4030_vibrapath_texts[] =
390 {"Local vibrator", "Audio"};
391
392static const struct soc_enum twl4030_vibrapath_enum =
393 SOC_ENUM_SINGLE(TWL4030_REG_VIBRA_CTL, 4,
394 ARRAY_SIZE(twl4030_vibrapath_texts),
395 twl4030_vibrapath_texts);
396
397static const struct snd_kcontrol_new twl4030_dapm_vibrapath_control =
398SOC_DAPM_ENUM("Route", twl4030_vibrapath_enum);
399
Peter Ujfalusi276c6222008-12-31 10:08:38 +0200400/* Left analog microphone selection */
Joonyoung Shim97b80962009-05-11 20:36:08 +0900401static const struct snd_kcontrol_new twl4030_dapm_analoglmic_controls[] = {
Peter Ujfalusi90289352009-08-14 08:44:00 +0300402 SOC_DAPM_SINGLE("Main Mic Capture Switch",
403 TWL4030_REG_ANAMICL, 0, 1, 0),
404 SOC_DAPM_SINGLE("Headset Mic Capture Switch",
405 TWL4030_REG_ANAMICL, 1, 1, 0),
406 SOC_DAPM_SINGLE("AUXL Capture Switch",
407 TWL4030_REG_ANAMICL, 2, 1, 0),
408 SOC_DAPM_SINGLE("Carkit Mic Capture Switch",
409 TWL4030_REG_ANAMICL, 3, 1, 0),
Joonyoung Shim97b80962009-05-11 20:36:08 +0900410};
Peter Ujfalusi276c6222008-12-31 10:08:38 +0200411
412/* Right analog microphone selection */
Joonyoung Shim97b80962009-05-11 20:36:08 +0900413static const struct snd_kcontrol_new twl4030_dapm_analogrmic_controls[] = {
Peter Ujfalusi90289352009-08-14 08:44:00 +0300414 SOC_DAPM_SINGLE("Sub Mic Capture Switch", TWL4030_REG_ANAMICR, 0, 1, 0),
415 SOC_DAPM_SINGLE("AUXR Capture Switch", TWL4030_REG_ANAMICR, 2, 1, 0),
Joonyoung Shim97b80962009-05-11 20:36:08 +0900416};
Peter Ujfalusi276c6222008-12-31 10:08:38 +0200417
418/* TX1 L/R Analog/Digital microphone selection */
419static const char *twl4030_micpathtx1_texts[] =
420 {"Analog", "Digimic0"};
421
422static const struct soc_enum twl4030_micpathtx1_enum =
423 SOC_ENUM_SINGLE(TWL4030_REG_ADCMICSEL, 0,
424 ARRAY_SIZE(twl4030_micpathtx1_texts),
425 twl4030_micpathtx1_texts);
426
427static const struct snd_kcontrol_new twl4030_dapm_micpathtx1_control =
428SOC_DAPM_ENUM("Route", twl4030_micpathtx1_enum);
429
430/* TX2 L/R Analog/Digital microphone selection */
431static const char *twl4030_micpathtx2_texts[] =
432 {"Analog", "Digimic1"};
433
434static const struct soc_enum twl4030_micpathtx2_enum =
435 SOC_ENUM_SINGLE(TWL4030_REG_ADCMICSEL, 2,
436 ARRAY_SIZE(twl4030_micpathtx2_texts),
437 twl4030_micpathtx2_texts);
438
439static const struct snd_kcontrol_new twl4030_dapm_micpathtx2_control =
440SOC_DAPM_ENUM("Route", twl4030_micpathtx2_enum);
441
Peter Ujfalusi73939582009-01-29 14:57:50 +0200442/* Analog bypass for AudioR1 */
443static const struct snd_kcontrol_new twl4030_dapm_abypassr1_control =
444 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXR1_APGA_CTL, 2, 1, 0);
445
446/* Analog bypass for AudioL1 */
447static const struct snd_kcontrol_new twl4030_dapm_abypassl1_control =
448 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXL1_APGA_CTL, 2, 1, 0);
449
450/* Analog bypass for AudioR2 */
451static const struct snd_kcontrol_new twl4030_dapm_abypassr2_control =
452 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXR2_APGA_CTL, 2, 1, 0);
453
454/* Analog bypass for AudioL2 */
455static const struct snd_kcontrol_new twl4030_dapm_abypassl2_control =
456 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXL2_APGA_CTL, 2, 1, 0);
457
Lopez Cruz, Misaelfcd274a2009-04-30 21:47:22 -0500458/* Analog bypass for Voice */
459static const struct snd_kcontrol_new twl4030_dapm_abypassv_control =
460 SOC_DAPM_SINGLE("Switch", TWL4030_REG_VDL_APGA_CTL, 2, 1, 0);
461
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +0200462/* Digital bypass gain, 0 mutes the bypass */
463static const unsigned int twl4030_dapm_dbypass_tlv[] = {
464 TLV_DB_RANGE_HEAD(2),
465 0, 3, TLV_DB_SCALE_ITEM(-2400, 0, 1),
466 4, 7, TLV_DB_SCALE_ITEM(-1800, 600, 0),
467};
468
469/* Digital bypass left (TX1L -> RX2L) */
470static const struct snd_kcontrol_new twl4030_dapm_dbypassl_control =
471 SOC_DAPM_SINGLE_TLV("Volume",
472 TWL4030_REG_ATX2ARXPGA, 3, 7, 0,
473 twl4030_dapm_dbypass_tlv);
474
475/* Digital bypass right (TX1R -> RX2R) */
476static const struct snd_kcontrol_new twl4030_dapm_dbypassr_control =
477 SOC_DAPM_SINGLE_TLV("Volume",
478 TWL4030_REG_ATX2ARXPGA, 0, 7, 0,
479 twl4030_dapm_dbypass_tlv);
480
Lopez Cruz, Misaelee8f6892009-04-30 21:48:08 -0500481/*
482 * Voice Sidetone GAIN volume control:
483 * from -51 to -10 dB in 1 dB steps (mute instead of -51 dB)
484 */
485static DECLARE_TLV_DB_SCALE(twl4030_dapm_dbypassv_tlv, -5100, 100, 1);
486
487/* Digital bypass voice: sidetone (VUL -> VDL)*/
488static const struct snd_kcontrol_new twl4030_dapm_dbypassv_control =
489 SOC_DAPM_SINGLE_TLV("Volume",
490 TWL4030_REG_VSTPGA, 0, 0x29, 0,
491 twl4030_dapm_dbypassv_tlv);
492
Peter Ujfalusi276c6222008-12-31 10:08:38 +0200493static int micpath_event(struct snd_soc_dapm_widget *w,
494 struct snd_kcontrol *kcontrol, int event)
495{
496 struct soc_enum *e = (struct soc_enum *)w->kcontrols->private_value;
497 unsigned char adcmicsel, micbias_ctl;
498
499 adcmicsel = twl4030_read_reg_cache(w->codec, TWL4030_REG_ADCMICSEL);
500 micbias_ctl = twl4030_read_reg_cache(w->codec, TWL4030_REG_MICBIAS_CTL);
501 /* Prepare the bits for the given TX path:
502 * shift_l == 0: TX1 microphone path
503 * shift_l == 2: TX2 microphone path */
504 if (e->shift_l) {
505 /* TX2 microphone path */
506 if (adcmicsel & TWL4030_TX2IN_SEL)
507 micbias_ctl |= TWL4030_MICBIAS2_CTL; /* digimic */
508 else
509 micbias_ctl &= ~TWL4030_MICBIAS2_CTL;
510 } else {
511 /* TX1 microphone path */
512 if (adcmicsel & TWL4030_TX1IN_SEL)
513 micbias_ctl |= TWL4030_MICBIAS1_CTL; /* digimic */
514 else
515 micbias_ctl &= ~TWL4030_MICBIAS1_CTL;
516 }
517
518 twl4030_write(w->codec, TWL4030_REG_MICBIAS_CTL, micbias_ctl);
519
520 return 0;
521}
522
Peter Ujfalusi9008adf2009-08-13 15:59:34 +0300523/*
524 * Output PGA builder:
525 * Handle the muting and unmuting of the given output (turning off the
526 * amplifier associated with the output pin)
527 * On mute bypass the reg_cache and mute the volume
528 * On unmute: restore the register content
529 * Outputs handled in this way: Earpiece, PreDrivL/R, CarkitL/R
530 */
531#define TWL4030_OUTPUT_PGA(pin_name, reg, mask) \
532static int pin_name##pga_event(struct snd_soc_dapm_widget *w, \
533 struct snd_kcontrol *kcontrol, int event) \
534{ \
535 u8 reg_val; \
536 \
537 switch (event) { \
538 case SND_SOC_DAPM_POST_PMU: \
539 twl4030_write(w->codec, reg, \
540 twl4030_read_reg_cache(w->codec, reg)); \
541 break; \
542 case SND_SOC_DAPM_POST_PMD: \
543 reg_val = twl4030_read_reg_cache(w->codec, reg); \
544 twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, \
545 reg_val & (~mask), \
546 reg); \
547 break; \
548 } \
549 return 0; \
550}
551
552TWL4030_OUTPUT_PGA(earpiece, TWL4030_REG_EAR_CTL, TWL4030_EAR_GAIN);
553TWL4030_OUTPUT_PGA(predrivel, TWL4030_REG_PREDL_CTL, TWL4030_PREDL_GAIN);
554TWL4030_OUTPUT_PGA(predriver, TWL4030_REG_PREDR_CTL, TWL4030_PREDR_GAIN);
555TWL4030_OUTPUT_PGA(carkitl, TWL4030_REG_PRECKL_CTL, TWL4030_PRECKL_GAIN);
556TWL4030_OUTPUT_PGA(carkitr, TWL4030_REG_PRECKR_CTL, TWL4030_PRECKR_GAIN);
557
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300558static void handsfree_ramp(struct snd_soc_codec *codec, int reg, int ramp)
Stanley.Miao49d92c72008-12-11 23:28:10 +0800559{
Stanley.Miao49d92c72008-12-11 23:28:10 +0800560 unsigned char hs_ctl;
561
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300562 hs_ctl = twl4030_read_reg_cache(codec, reg);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800563
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300564 if (ramp) {
565 /* HF ramp-up */
566 hs_ctl |= TWL4030_HF_CTL_REF_EN;
567 twl4030_write(codec, reg, hs_ctl);
568 udelay(10);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800569 hs_ctl |= TWL4030_HF_CTL_RAMP_EN;
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300570 twl4030_write(codec, reg, hs_ctl);
571 udelay(40);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800572 hs_ctl |= TWL4030_HF_CTL_LOOP_EN;
Stanley.Miao49d92c72008-12-11 23:28:10 +0800573 hs_ctl |= TWL4030_HF_CTL_HB_EN;
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300574 twl4030_write(codec, reg, hs_ctl);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800575 } else {
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300576 /* HF ramp-down */
577 hs_ctl &= ~TWL4030_HF_CTL_LOOP_EN;
578 hs_ctl &= ~TWL4030_HF_CTL_HB_EN;
579 twl4030_write(codec, reg, hs_ctl);
580 hs_ctl &= ~TWL4030_HF_CTL_RAMP_EN;
581 twl4030_write(codec, reg, hs_ctl);
582 udelay(40);
583 hs_ctl &= ~TWL4030_HF_CTL_REF_EN;
584 twl4030_write(codec, reg, hs_ctl);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800585 }
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300586}
Stanley.Miao49d92c72008-12-11 23:28:10 +0800587
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300588static int handsfreelpga_event(struct snd_soc_dapm_widget *w,
589 struct snd_kcontrol *kcontrol, int event)
590{
591 switch (event) {
592 case SND_SOC_DAPM_POST_PMU:
593 handsfree_ramp(w->codec, TWL4030_REG_HFL_CTL, 1);
594 break;
595 case SND_SOC_DAPM_POST_PMD:
596 handsfree_ramp(w->codec, TWL4030_REG_HFL_CTL, 0);
597 break;
598 }
599 return 0;
600}
601
602static int handsfreerpga_event(struct snd_soc_dapm_widget *w,
603 struct snd_kcontrol *kcontrol, int event)
604{
605 switch (event) {
606 case SND_SOC_DAPM_POST_PMU:
607 handsfree_ramp(w->codec, TWL4030_REG_HFR_CTL, 1);
608 break;
609 case SND_SOC_DAPM_POST_PMD:
610 handsfree_ramp(w->codec, TWL4030_REG_HFR_CTL, 0);
611 break;
612 }
Stanley.Miao49d92c72008-12-11 23:28:10 +0800613 return 0;
614}
615
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300616static void headset_ramp(struct snd_soc_codec *codec, int ramp)
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200617{
Candelaria Villareal, Jorge4e49ffd2009-07-01 19:17:43 -0500618 struct snd_soc_device *socdev = codec->socdev;
619 struct twl4030_setup_data *setup = socdev->codec_data;
620
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200621 unsigned char hs_gain, hs_pop;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300622 struct twl4030_priv *twl4030 = codec->private_data;
623 /* Base values for ramp delay calculation: 2^19 - 2^26 */
624 unsigned int ramp_base[] = {524288, 1048576, 2097152, 4194304,
625 8388608, 16777216, 33554432, 67108864};
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200626
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300627 hs_gain = twl4030_read_reg_cache(codec, TWL4030_REG_HS_GAIN_SET);
628 hs_pop = twl4030_read_reg_cache(codec, TWL4030_REG_HS_POPN_SET);
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200629
Candelaria Villareal, Jorge4e49ffd2009-07-01 19:17:43 -0500630 /* Enable external mute control, this dramatically reduces
631 * the pop-noise */
632 if (setup && setup->hs_extmute) {
633 if (setup->set_hs_extmute) {
634 setup->set_hs_extmute(1);
635 } else {
636 hs_pop |= TWL4030_EXTMUTE;
637 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
638 }
639 }
640
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300641 if (ramp) {
642 /* Headset ramp-up according to the TRM */
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200643 hs_pop |= TWL4030_VMID_EN;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300644 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
645 twl4030_write(codec, TWL4030_REG_HS_GAIN_SET, hs_gain);
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200646 hs_pop |= TWL4030_RAMP_EN;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300647 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
Candelaria Villareal, Jorge4e49ffd2009-07-01 19:17:43 -0500648 /* Wait ramp delay time + 1, so the VMID can settle */
649 mdelay((ramp_base[(hs_pop & TWL4030_RAMP_DELAY) >> 2] /
650 twl4030->sysclk) + 1);
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300651 } else {
652 /* Headset ramp-down _not_ according to
653 * the TRM, but in a way that it is working */
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200654 hs_pop &= ~TWL4030_RAMP_EN;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300655 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
656 /* Wait ramp delay time + 1, so the VMID can settle */
657 mdelay((ramp_base[(hs_pop & TWL4030_RAMP_DELAY) >> 2] /
658 twl4030->sysclk) + 1);
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200659 /* Bypass the reg_cache to mute the headset */
660 twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
661 hs_gain & (~0x0f),
662 TWL4030_REG_HS_GAIN_SET);
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300663
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200664 hs_pop &= ~TWL4030_VMID_EN;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300665 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
666 }
Candelaria Villareal, Jorge4e49ffd2009-07-01 19:17:43 -0500667
668 /* Disable external mute */
669 if (setup && setup->hs_extmute) {
670 if (setup->set_hs_extmute) {
671 setup->set_hs_extmute(0);
672 } else {
673 hs_pop &= ~TWL4030_EXTMUTE;
674 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
675 }
676 }
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300677}
678
679static int headsetlpga_event(struct snd_soc_dapm_widget *w,
680 struct snd_kcontrol *kcontrol, int event)
681{
682 struct twl4030_priv *twl4030 = w->codec->private_data;
683
684 switch (event) {
685 case SND_SOC_DAPM_POST_PMU:
686 /* Do the ramp-up only once */
687 if (!twl4030->hsr_enabled)
688 headset_ramp(w->codec, 1);
689
690 twl4030->hsl_enabled = 1;
691 break;
692 case SND_SOC_DAPM_POST_PMD:
693 /* Do the ramp-down only if both headsetL/R is disabled */
694 if (!twl4030->hsr_enabled)
695 headset_ramp(w->codec, 0);
696
697 twl4030->hsl_enabled = 0;
698 break;
699 }
700 return 0;
701}
702
703static int headsetrpga_event(struct snd_soc_dapm_widget *w,
704 struct snd_kcontrol *kcontrol, int event)
705{
706 struct twl4030_priv *twl4030 = w->codec->private_data;
707
708 switch (event) {
709 case SND_SOC_DAPM_POST_PMU:
710 /* Do the ramp-up only once */
711 if (!twl4030->hsl_enabled)
712 headset_ramp(w->codec, 1);
713
714 twl4030->hsr_enabled = 1;
715 break;
716 case SND_SOC_DAPM_POST_PMD:
717 /* Do the ramp-down only if both headsetL/R is disabled */
718 if (!twl4030->hsl_enabled)
719 headset_ramp(w->codec, 0);
720
721 twl4030->hsr_enabled = 0;
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200722 break;
723 }
724 return 0;
725}
726
Peter Ujfalusic10b82c2008-11-24 13:49:35 +0200727/*
Peter Ujfalusib0bd53a2008-11-24 13:49:38 +0200728 * Some of the gain controls in TWL (mostly those which are associated with
729 * the outputs) are implemented in an interesting way:
730 * 0x0 : Power down (mute)
731 * 0x1 : 6dB
732 * 0x2 : 0 dB
733 * 0x3 : -6 dB
734 * Inverting not going to help with these.
735 * Custom volsw and volsw_2r get/put functions to handle these gain bits.
736 */
737#define SOC_DOUBLE_TLV_TWL4030(xname, xreg, shift_left, shift_right, xmax,\
738 xinvert, tlv_array) \
739{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
740 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
741 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
742 .tlv.p = (tlv_array), \
743 .info = snd_soc_info_volsw, \
744 .get = snd_soc_get_volsw_twl4030, \
745 .put = snd_soc_put_volsw_twl4030, \
746 .private_value = (unsigned long)&(struct soc_mixer_control) \
747 {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
748 .max = xmax, .invert = xinvert} }
749#define SOC_DOUBLE_R_TLV_TWL4030(xname, reg_left, reg_right, xshift, xmax,\
750 xinvert, tlv_array) \
751{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
752 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
753 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
754 .tlv.p = (tlv_array), \
755 .info = snd_soc_info_volsw_2r, \
756 .get = snd_soc_get_volsw_r2_twl4030,\
757 .put = snd_soc_put_volsw_r2_twl4030, \
758 .private_value = (unsigned long)&(struct soc_mixer_control) \
759 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
Mark Brown64089b82008-12-08 19:17:58 +0000760 .rshift = xshift, .max = xmax, .invert = xinvert} }
Peter Ujfalusib0bd53a2008-11-24 13:49:38 +0200761#define SOC_SINGLE_TLV_TWL4030(xname, xreg, xshift, xmax, xinvert, tlv_array) \
762 SOC_DOUBLE_TLV_TWL4030(xname, xreg, xshift, xshift, xmax, \
763 xinvert, tlv_array)
764
765static int snd_soc_get_volsw_twl4030(struct snd_kcontrol *kcontrol,
766 struct snd_ctl_elem_value *ucontrol)
767{
768 struct soc_mixer_control *mc =
769 (struct soc_mixer_control *)kcontrol->private_value;
770 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
771 unsigned int reg = mc->reg;
772 unsigned int shift = mc->shift;
773 unsigned int rshift = mc->rshift;
774 int max = mc->max;
775 int mask = (1 << fls(max)) - 1;
776
777 ucontrol->value.integer.value[0] =
778 (snd_soc_read(codec, reg) >> shift) & mask;
779 if (ucontrol->value.integer.value[0])
780 ucontrol->value.integer.value[0] =
781 max + 1 - ucontrol->value.integer.value[0];
782
783 if (shift != rshift) {
784 ucontrol->value.integer.value[1] =
785 (snd_soc_read(codec, reg) >> rshift) & mask;
786 if (ucontrol->value.integer.value[1])
787 ucontrol->value.integer.value[1] =
788 max + 1 - ucontrol->value.integer.value[1];
789 }
790
791 return 0;
792}
793
794static int snd_soc_put_volsw_twl4030(struct snd_kcontrol *kcontrol,
795 struct snd_ctl_elem_value *ucontrol)
796{
797 struct soc_mixer_control *mc =
798 (struct soc_mixer_control *)kcontrol->private_value;
799 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
800 unsigned int reg = mc->reg;
801 unsigned int shift = mc->shift;
802 unsigned int rshift = mc->rshift;
803 int max = mc->max;
804 int mask = (1 << fls(max)) - 1;
805 unsigned short val, val2, val_mask;
806
807 val = (ucontrol->value.integer.value[0] & mask);
808
809 val_mask = mask << shift;
810 if (val)
811 val = max + 1 - val;
812 val = val << shift;
813 if (shift != rshift) {
814 val2 = (ucontrol->value.integer.value[1] & mask);
815 val_mask |= mask << rshift;
816 if (val2)
817 val2 = max + 1 - val2;
818 val |= val2 << rshift;
819 }
820 return snd_soc_update_bits(codec, reg, val_mask, val);
821}
822
823static int snd_soc_get_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
824 struct snd_ctl_elem_value *ucontrol)
825{
826 struct soc_mixer_control *mc =
827 (struct soc_mixer_control *)kcontrol->private_value;
828 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
829 unsigned int reg = mc->reg;
830 unsigned int reg2 = mc->rreg;
831 unsigned int shift = mc->shift;
832 int max = mc->max;
833 int mask = (1<<fls(max))-1;
834
835 ucontrol->value.integer.value[0] =
836 (snd_soc_read(codec, reg) >> shift) & mask;
837 ucontrol->value.integer.value[1] =
838 (snd_soc_read(codec, reg2) >> shift) & mask;
839
840 if (ucontrol->value.integer.value[0])
841 ucontrol->value.integer.value[0] =
842 max + 1 - ucontrol->value.integer.value[0];
843 if (ucontrol->value.integer.value[1])
844 ucontrol->value.integer.value[1] =
845 max + 1 - ucontrol->value.integer.value[1];
846
847 return 0;
848}
849
850static int snd_soc_put_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
851 struct snd_ctl_elem_value *ucontrol)
852{
853 struct soc_mixer_control *mc =
854 (struct soc_mixer_control *)kcontrol->private_value;
855 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
856 unsigned int reg = mc->reg;
857 unsigned int reg2 = mc->rreg;
858 unsigned int shift = mc->shift;
859 int max = mc->max;
860 int mask = (1 << fls(max)) - 1;
861 int err;
862 unsigned short val, val2, val_mask;
863
864 val_mask = mask << shift;
865 val = (ucontrol->value.integer.value[0] & mask);
866 val2 = (ucontrol->value.integer.value[1] & mask);
867
868 if (val)
869 val = max + 1 - val;
870 if (val2)
871 val2 = max + 1 - val2;
872
873 val = val << shift;
874 val2 = val2 << shift;
875
876 err = snd_soc_update_bits(codec, reg, val_mask, val);
877 if (err < 0)
878 return err;
879
880 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
881 return err;
882}
883
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -0500884/* Codec operation modes */
885static const char *twl4030_op_modes_texts[] = {
886 "Option 2 (voice/audio)", "Option 1 (audio)"
887};
888
889static const struct soc_enum twl4030_op_modes_enum =
890 SOC_ENUM_SINGLE(TWL4030_REG_CODEC_MODE, 0,
891 ARRAY_SIZE(twl4030_op_modes_texts),
892 twl4030_op_modes_texts);
893
Mark Brown423c2382009-06-20 13:54:02 +0100894static int snd_soc_put_twl4030_opmode_enum_double(struct snd_kcontrol *kcontrol,
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -0500895 struct snd_ctl_elem_value *ucontrol)
896{
897 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
898 struct twl4030_priv *twl4030 = codec->private_data;
899 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
900 unsigned short val;
901 unsigned short mask, bitmask;
902
903 if (twl4030->configured) {
904 printk(KERN_ERR "twl4030 operation mode cannot be "
905 "changed on-the-fly\n");
906 return -EBUSY;
907 }
908
909 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
910 ;
911 if (ucontrol->value.enumerated.item[0] > e->max - 1)
912 return -EINVAL;
913
914 val = ucontrol->value.enumerated.item[0] << e->shift_l;
915 mask = (bitmask - 1) << e->shift_l;
916 if (e->shift_l != e->shift_r) {
917 if (ucontrol->value.enumerated.item[1] > e->max - 1)
918 return -EINVAL;
919 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
920 mask |= (bitmask - 1) << e->shift_r;
921 }
922
923 return snd_soc_update_bits(codec, e->reg, mask, val);
924}
925
Peter Ujfalusib0bd53a2008-11-24 13:49:38 +0200926/*
Peter Ujfalusic10b82c2008-11-24 13:49:35 +0200927 * FGAIN volume control:
928 * from -62 to 0 dB in 1 dB steps (mute instead of -63 dB)
929 */
Peter Ujfalusid889a722008-12-01 10:03:46 +0200930static DECLARE_TLV_DB_SCALE(digital_fine_tlv, -6300, 100, 1);
Peter Ujfalusic10b82c2008-11-24 13:49:35 +0200931
Peter Ujfalusi0d33ea02008-11-24 13:49:36 +0200932/*
933 * CGAIN volume control:
934 * 0 dB to 12 dB in 6 dB steps
935 * value 2 and 3 means 12 dB
936 */
Peter Ujfalusid889a722008-12-01 10:03:46 +0200937static DECLARE_TLV_DB_SCALE(digital_coarse_tlv, 0, 600, 0);
938
939/*
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900940 * Voice Downlink GAIN volume control:
941 * from -37 to 12 dB in 1 dB steps (mute instead of -37 dB)
942 */
943static DECLARE_TLV_DB_SCALE(digital_voice_downlink_tlv, -3700, 100, 1);
944
945/*
Peter Ujfalusid889a722008-12-01 10:03:46 +0200946 * Analog playback gain
947 * -24 dB to 12 dB in 2 dB steps
948 */
949static DECLARE_TLV_DB_SCALE(analog_tlv, -2400, 200, 0);
Peter Ujfalusi0d33ea02008-11-24 13:49:36 +0200950
Peter Ujfalusi381a22b2008-12-01 10:03:45 +0200951/*
Peter Ujfalusi42902392008-12-01 10:03:47 +0200952 * Gain controls tied to outputs
953 * -6 dB to 6 dB in 6 dB steps (mute instead of -12)
954 */
955static DECLARE_TLV_DB_SCALE(output_tvl, -1200, 600, 1);
956
957/*
Joonyoung Shim18cc8d82009-04-28 18:18:05 +0900958 * Gain control for earpiece amplifier
959 * 0 dB to 12 dB in 6 dB steps (mute instead of -6)
960 */
961static DECLARE_TLV_DB_SCALE(output_ear_tvl, -600, 600, 1);
962
963/*
Peter Ujfalusi381a22b2008-12-01 10:03:45 +0200964 * Capture gain after the ADCs
965 * from 0 dB to 31 dB in 1 dB steps
966 */
967static DECLARE_TLV_DB_SCALE(digital_capture_tlv, 0, 100, 0);
968
Grazvydas Ignotas5920b452008-12-02 20:48:58 +0200969/*
970 * Gain control for input amplifiers
971 * 0 dB to 30 dB in 6 dB steps
972 */
973static DECLARE_TLV_DB_SCALE(input_gain_tlv, 0, 600, 0);
974
Lopez Cruz, Misael328d0a12009-06-22 10:51:52 -0500975/* AVADC clock priority */
976static const char *twl4030_avadc_clk_priority_texts[] = {
977 "Voice high priority", "HiFi high priority"
978};
979
980static const struct soc_enum twl4030_avadc_clk_priority_enum =
981 SOC_ENUM_SINGLE(TWL4030_REG_AVADC_CTL, 2,
982 ARRAY_SIZE(twl4030_avadc_clk_priority_texts),
983 twl4030_avadc_clk_priority_texts);
984
Peter Ujfalusi89492be2009-03-05 12:48:49 +0200985static const char *twl4030_rampdelay_texts[] = {
986 "27/20/14 ms", "55/40/27 ms", "109/81/55 ms", "218/161/109 ms",
987 "437/323/218 ms", "874/645/437 ms", "1748/1291/874 ms",
988 "3495/2581/1748 ms"
989};
990
991static const struct soc_enum twl4030_rampdelay_enum =
992 SOC_ENUM_SINGLE(TWL4030_REG_HS_POPN_SET, 2,
993 ARRAY_SIZE(twl4030_rampdelay_texts),
994 twl4030_rampdelay_texts);
995
Peter Ujfalusi376f7832009-05-05 08:55:47 +0300996/* Vibra H-bridge direction mode */
997static const char *twl4030_vibradirmode_texts[] = {
998 "Vibra H-bridge direction", "Audio data MSB",
999};
1000
1001static const struct soc_enum twl4030_vibradirmode_enum =
1002 SOC_ENUM_SINGLE(TWL4030_REG_VIBRA_CTL, 5,
1003 ARRAY_SIZE(twl4030_vibradirmode_texts),
1004 twl4030_vibradirmode_texts);
1005
1006/* Vibra H-bridge direction */
1007static const char *twl4030_vibradir_texts[] = {
1008 "Positive polarity", "Negative polarity",
1009};
1010
1011static const struct soc_enum twl4030_vibradir_enum =
1012 SOC_ENUM_SINGLE(TWL4030_REG_VIBRA_CTL, 1,
1013 ARRAY_SIZE(twl4030_vibradir_texts),
1014 twl4030_vibradir_texts);
1015
Steve Sakomancc175572008-10-30 21:35:26 -07001016static const struct snd_kcontrol_new twl4030_snd_controls[] = {
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -05001017 /* Codec operation mode control */
1018 SOC_ENUM_EXT("Codec Operation Mode", twl4030_op_modes_enum,
1019 snd_soc_get_enum_double,
1020 snd_soc_put_twl4030_opmode_enum_double),
1021
Peter Ujfalusid889a722008-12-01 10:03:46 +02001022 /* Common playback gain controls */
1023 SOC_DOUBLE_R_TLV("DAC1 Digital Fine Playback Volume",
1024 TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
1025 0, 0x3f, 0, digital_fine_tlv),
1026 SOC_DOUBLE_R_TLV("DAC2 Digital Fine Playback Volume",
1027 TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
1028 0, 0x3f, 0, digital_fine_tlv),
1029
1030 SOC_DOUBLE_R_TLV("DAC1 Digital Coarse Playback Volume",
1031 TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
1032 6, 0x2, 0, digital_coarse_tlv),
1033 SOC_DOUBLE_R_TLV("DAC2 Digital Coarse Playback Volume",
1034 TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
1035 6, 0x2, 0, digital_coarse_tlv),
1036
1037 SOC_DOUBLE_R_TLV("DAC1 Analog Playback Volume",
1038 TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
1039 3, 0x12, 1, analog_tlv),
1040 SOC_DOUBLE_R_TLV("DAC2 Analog Playback Volume",
1041 TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
1042 3, 0x12, 1, analog_tlv),
Peter Ujfalusi44c55872008-12-09 08:45:44 +02001043 SOC_DOUBLE_R("DAC1 Analog Playback Switch",
1044 TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
1045 1, 1, 0),
1046 SOC_DOUBLE_R("DAC2 Analog Playback Switch",
1047 TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
1048 1, 1, 0),
Peter Ujfalusi381a22b2008-12-01 10:03:45 +02001049
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001050 /* Common voice downlink gain controls */
1051 SOC_SINGLE_TLV("DAC Voice Digital Downlink Volume",
1052 TWL4030_REG_VRXPGA, 0, 0x31, 0, digital_voice_downlink_tlv),
1053
1054 SOC_SINGLE_TLV("DAC Voice Analog Downlink Volume",
1055 TWL4030_REG_VDL_APGA_CTL, 3, 0x12, 1, analog_tlv),
1056
1057 SOC_SINGLE("DAC Voice Analog Downlink Switch",
1058 TWL4030_REG_VDL_APGA_CTL, 1, 1, 0),
1059
Peter Ujfalusi42902392008-12-01 10:03:47 +02001060 /* Separate output gain controls */
1061 SOC_DOUBLE_R_TLV_TWL4030("PreDriv Playback Volume",
1062 TWL4030_REG_PREDL_CTL, TWL4030_REG_PREDR_CTL,
1063 4, 3, 0, output_tvl),
1064
1065 SOC_DOUBLE_TLV_TWL4030("Headset Playback Volume",
1066 TWL4030_REG_HS_GAIN_SET, 0, 2, 3, 0, output_tvl),
1067
1068 SOC_DOUBLE_R_TLV_TWL4030("Carkit Playback Volume",
1069 TWL4030_REG_PRECKL_CTL, TWL4030_REG_PRECKR_CTL,
1070 4, 3, 0, output_tvl),
1071
1072 SOC_SINGLE_TLV_TWL4030("Earpiece Playback Volume",
Joonyoung Shim18cc8d82009-04-28 18:18:05 +09001073 TWL4030_REG_EAR_CTL, 4, 3, 0, output_ear_tvl),
Peter Ujfalusi42902392008-12-01 10:03:47 +02001074
Peter Ujfalusi381a22b2008-12-01 10:03:45 +02001075 /* Common capture gain controls */
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001076 SOC_DOUBLE_R_TLV("TX1 Digital Capture Volume",
Peter Ujfalusi381a22b2008-12-01 10:03:45 +02001077 TWL4030_REG_ATXL1PGA, TWL4030_REG_ATXR1PGA,
1078 0, 0x1f, 0, digital_capture_tlv),
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001079 SOC_DOUBLE_R_TLV("TX2 Digital Capture Volume",
1080 TWL4030_REG_AVTXL2PGA, TWL4030_REG_AVTXR2PGA,
1081 0, 0x1f, 0, digital_capture_tlv),
Grazvydas Ignotas5920b452008-12-02 20:48:58 +02001082
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001083 SOC_DOUBLE_TLV("Analog Capture Volume", TWL4030_REG_ANAMIC_GAIN,
Grazvydas Ignotas5920b452008-12-02 20:48:58 +02001084 0, 3, 5, 0, input_gain_tlv),
Peter Ujfalusi89492be2009-03-05 12:48:49 +02001085
Lopez Cruz, Misael328d0a12009-06-22 10:51:52 -05001086 SOC_ENUM("AVADC Clock Priority", twl4030_avadc_clk_priority_enum),
1087
Peter Ujfalusi89492be2009-03-05 12:48:49 +02001088 SOC_ENUM("HS ramp delay", twl4030_rampdelay_enum),
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001089
1090 SOC_ENUM("Vibra H-bridge mode", twl4030_vibradirmode_enum),
1091 SOC_ENUM("Vibra H-bridge direction", twl4030_vibradir_enum),
Steve Sakomancc175572008-10-30 21:35:26 -07001092};
1093
Steve Sakomancc175572008-10-30 21:35:26 -07001094static const struct snd_soc_dapm_widget twl4030_dapm_widgets[] = {
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001095 /* Left channel inputs */
1096 SND_SOC_DAPM_INPUT("MAINMIC"),
1097 SND_SOC_DAPM_INPUT("HSMIC"),
1098 SND_SOC_DAPM_INPUT("AUXL"),
1099 SND_SOC_DAPM_INPUT("CARKITMIC"),
1100 /* Right channel inputs */
1101 SND_SOC_DAPM_INPUT("SUBMIC"),
1102 SND_SOC_DAPM_INPUT("AUXR"),
1103 /* Digital microphones (Stereo) */
1104 SND_SOC_DAPM_INPUT("DIGIMIC0"),
1105 SND_SOC_DAPM_INPUT("DIGIMIC1"),
Steve Sakomancc175572008-10-30 21:35:26 -07001106
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001107 /* Outputs */
Steve Sakomancc175572008-10-30 21:35:26 -07001108 SND_SOC_DAPM_OUTPUT("OUTL"),
1109 SND_SOC_DAPM_OUTPUT("OUTR"),
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001110 SND_SOC_DAPM_OUTPUT("EARPIECE"),
Peter Ujfalusi2a6f5c52008-12-09 12:35:48 +02001111 SND_SOC_DAPM_OUTPUT("PREDRIVEL"),
1112 SND_SOC_DAPM_OUTPUT("PREDRIVER"),
Peter Ujfalusidfad21a2008-12-09 12:35:49 +02001113 SND_SOC_DAPM_OUTPUT("HSOL"),
1114 SND_SOC_DAPM_OUTPUT("HSOR"),
Peter Ujfalusi6a1bee42008-12-10 12:51:46 +02001115 SND_SOC_DAPM_OUTPUT("CARKITL"),
1116 SND_SOC_DAPM_OUTPUT("CARKITR"),
Peter Ujfalusidf339802008-12-09 12:35:51 +02001117 SND_SOC_DAPM_OUTPUT("HFL"),
1118 SND_SOC_DAPM_OUTPUT("HFR"),
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001119 SND_SOC_DAPM_OUTPUT("VIBRA"),
Steve Sakomancc175572008-10-30 21:35:26 -07001120
Peter Ujfalusi53b50472008-12-09 08:45:43 +02001121 /* DACs */
Peter Ujfalusib4852b72009-05-22 15:12:15 +03001122 SND_SOC_DAPM_DAC("DAC Right1", "Right Front HiFi Playback",
Peter Ujfalusi73939582009-01-29 14:57:50 +02001123 SND_SOC_NOPM, 0, 0),
Peter Ujfalusib4852b72009-05-22 15:12:15 +03001124 SND_SOC_DAPM_DAC("DAC Left1", "Left Front HiFi Playback",
Peter Ujfalusi73939582009-01-29 14:57:50 +02001125 SND_SOC_NOPM, 0, 0),
Peter Ujfalusib4852b72009-05-22 15:12:15 +03001126 SND_SOC_DAPM_DAC("DAC Right2", "Right Rear HiFi Playback",
Peter Ujfalusi73939582009-01-29 14:57:50 +02001127 SND_SOC_NOPM, 0, 0),
Peter Ujfalusib4852b72009-05-22 15:12:15 +03001128 SND_SOC_DAPM_DAC("DAC Left2", "Left Rear HiFi Playback",
Peter Ujfalusi73939582009-01-29 14:57:50 +02001129 SND_SOC_NOPM, 0, 0),
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001130 SND_SOC_DAPM_DAC("DAC Voice", "Voice Playback",
Lopez Cruz, Misaelfcd274a2009-04-30 21:47:22 -05001131 SND_SOC_NOPM, 0, 0),
Steve Sakomancc175572008-10-30 21:35:26 -07001132
Peter Ujfalusi73939582009-01-29 14:57:50 +02001133 /* Analog bypasses */
Peter Ujfalusi78e08e22009-10-28 10:57:04 +02001134 SND_SOC_DAPM_SWITCH("Right1 Analog Loopback", SND_SOC_NOPM, 0, 0,
1135 &twl4030_dapm_abypassr1_control),
1136 SND_SOC_DAPM_SWITCH("Left1 Analog Loopback", SND_SOC_NOPM, 0, 0,
1137 &twl4030_dapm_abypassl1_control),
1138 SND_SOC_DAPM_SWITCH("Right2 Analog Loopback", SND_SOC_NOPM, 0, 0,
1139 &twl4030_dapm_abypassr2_control),
1140 SND_SOC_DAPM_SWITCH("Left2 Analog Loopback", SND_SOC_NOPM, 0, 0,
1141 &twl4030_dapm_abypassl2_control),
1142 SND_SOC_DAPM_SWITCH("Voice Analog Loopback", SND_SOC_NOPM, 0, 0,
1143 &twl4030_dapm_abypassv_control),
1144
1145 /* Master analog loopback switch */
1146 SND_SOC_DAPM_SUPPLY("FM Loop Enable", TWL4030_REG_MISC_SET_1, 5, 0,
1147 NULL, 0),
Peter Ujfalusi73939582009-01-29 14:57:50 +02001148
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001149 /* Digital bypasses */
Peter Ujfalusi78e08e22009-10-28 10:57:04 +02001150 SND_SOC_DAPM_SWITCH("Left Digital Loopback", SND_SOC_NOPM, 0, 0,
1151 &twl4030_dapm_dbypassl_control),
1152 SND_SOC_DAPM_SWITCH("Right Digital Loopback", SND_SOC_NOPM, 0, 0,
1153 &twl4030_dapm_dbypassr_control),
1154 SND_SOC_DAPM_SWITCH("Voice Digital Loopback", SND_SOC_NOPM, 0, 0,
1155 &twl4030_dapm_dbypassv_control),
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001156
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001157 /* Digital mixers, power control for the physical DACs */
1158 SND_SOC_DAPM_MIXER("Digital R1 Playback Mixer",
1159 TWL4030_REG_AVDAC_CTL, 0, 0, NULL, 0),
1160 SND_SOC_DAPM_MIXER("Digital L1 Playback Mixer",
1161 TWL4030_REG_AVDAC_CTL, 1, 0, NULL, 0),
1162 SND_SOC_DAPM_MIXER("Digital R2 Playback Mixer",
1163 TWL4030_REG_AVDAC_CTL, 2, 0, NULL, 0),
1164 SND_SOC_DAPM_MIXER("Digital L2 Playback Mixer",
1165 TWL4030_REG_AVDAC_CTL, 3, 0, NULL, 0),
1166 SND_SOC_DAPM_MIXER("Digital Voice Playback Mixer",
1167 TWL4030_REG_AVDAC_CTL, 4, 0, NULL, 0),
1168
1169 /* Analog mixers, power control for the physical PGAs */
1170 SND_SOC_DAPM_MIXER("Analog R1 Playback Mixer",
1171 TWL4030_REG_ARXR1_APGA_CTL, 0, 0, NULL, 0),
1172 SND_SOC_DAPM_MIXER("Analog L1 Playback Mixer",
1173 TWL4030_REG_ARXL1_APGA_CTL, 0, 0, NULL, 0),
1174 SND_SOC_DAPM_MIXER("Analog R2 Playback Mixer",
1175 TWL4030_REG_ARXR2_APGA_CTL, 0, 0, NULL, 0),
1176 SND_SOC_DAPM_MIXER("Analog L2 Playback Mixer",
1177 TWL4030_REG_ARXL2_APGA_CTL, 0, 0, NULL, 0),
1178 SND_SOC_DAPM_MIXER("Analog Voice Playback Mixer",
1179 TWL4030_REG_VDL_APGA_CTL, 0, 0, NULL, 0),
Peter Ujfalusi73939582009-01-29 14:57:50 +02001180
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001181 /* Output MIXER controls */
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001182 /* Earpiece */
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001183 SND_SOC_DAPM_MIXER("Earpiece Mixer", SND_SOC_NOPM, 0, 0,
1184 &twl4030_dapm_earpiece_controls[0],
1185 ARRAY_SIZE(twl4030_dapm_earpiece_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001186 SND_SOC_DAPM_PGA_E("Earpiece PGA", SND_SOC_NOPM,
1187 0, 0, NULL, 0, earpiecepga_event,
1188 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Peter Ujfalusi2a6f5c52008-12-09 12:35:48 +02001189 /* PreDrivL/R */
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001190 SND_SOC_DAPM_MIXER("PredriveL Mixer", SND_SOC_NOPM, 0, 0,
1191 &twl4030_dapm_predrivel_controls[0],
1192 ARRAY_SIZE(twl4030_dapm_predrivel_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001193 SND_SOC_DAPM_PGA_E("PredriveL PGA", SND_SOC_NOPM,
1194 0, 0, NULL, 0, predrivelpga_event,
1195 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001196 SND_SOC_DAPM_MIXER("PredriveR Mixer", SND_SOC_NOPM, 0, 0,
1197 &twl4030_dapm_predriver_controls[0],
1198 ARRAY_SIZE(twl4030_dapm_predriver_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001199 SND_SOC_DAPM_PGA_E("PredriveR PGA", SND_SOC_NOPM,
1200 0, 0, NULL, 0, predriverpga_event,
1201 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Peter Ujfalusidfad21a2008-12-09 12:35:49 +02001202 /* HeadsetL/R */
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001203 SND_SOC_DAPM_MIXER("HeadsetL Mixer", SND_SOC_NOPM, 0, 0,
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001204 &twl4030_dapm_hsol_controls[0],
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001205 ARRAY_SIZE(twl4030_dapm_hsol_controls)),
1206 SND_SOC_DAPM_PGA_E("HeadsetL PGA", SND_SOC_NOPM,
1207 0, 0, NULL, 0, headsetlpga_event,
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001208 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
1209 SND_SOC_DAPM_MIXER("HeadsetR Mixer", SND_SOC_NOPM, 0, 0,
1210 &twl4030_dapm_hsor_controls[0],
1211 ARRAY_SIZE(twl4030_dapm_hsor_controls)),
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001212 SND_SOC_DAPM_PGA_E("HeadsetR PGA", SND_SOC_NOPM,
1213 0, 0, NULL, 0, headsetrpga_event,
1214 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +02001215 /* CarkitL/R */
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001216 SND_SOC_DAPM_MIXER("CarkitL Mixer", SND_SOC_NOPM, 0, 0,
1217 &twl4030_dapm_carkitl_controls[0],
1218 ARRAY_SIZE(twl4030_dapm_carkitl_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001219 SND_SOC_DAPM_PGA_E("CarkitL PGA", SND_SOC_NOPM,
1220 0, 0, NULL, 0, carkitlpga_event,
1221 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001222 SND_SOC_DAPM_MIXER("CarkitR Mixer", SND_SOC_NOPM, 0, 0,
1223 &twl4030_dapm_carkitr_controls[0],
1224 ARRAY_SIZE(twl4030_dapm_carkitr_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001225 SND_SOC_DAPM_PGA_E("CarkitR PGA", SND_SOC_NOPM,
1226 0, 0, NULL, 0, carkitrpga_event,
1227 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001228
1229 /* Output MUX controls */
Peter Ujfalusidf339802008-12-09 12:35:51 +02001230 /* HandsfreeL/R */
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +03001231 SND_SOC_DAPM_MUX("HandsfreeL Mux", SND_SOC_NOPM, 0, 0,
1232 &twl4030_dapm_handsfreel_control),
Lopez Cruz, Misaele3c7dbb2009-06-25 12:36:14 -05001233 SND_SOC_DAPM_SWITCH("HandsfreeL", SND_SOC_NOPM, 0, 0,
Peter Ujfalusi0f89bdc2009-05-25 11:12:13 +03001234 &twl4030_dapm_handsfreelmute_control),
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +03001235 SND_SOC_DAPM_PGA_E("HandsfreeL PGA", SND_SOC_NOPM,
1236 0, 0, NULL, 0, handsfreelpga_event,
1237 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
1238 SND_SOC_DAPM_MUX("HandsfreeR Mux", SND_SOC_NOPM, 5, 0,
1239 &twl4030_dapm_handsfreer_control),
Lopez Cruz, Misaele3c7dbb2009-06-25 12:36:14 -05001240 SND_SOC_DAPM_SWITCH("HandsfreeR", SND_SOC_NOPM, 0, 0,
Peter Ujfalusi0f89bdc2009-05-25 11:12:13 +03001241 &twl4030_dapm_handsfreermute_control),
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +03001242 SND_SOC_DAPM_PGA_E("HandsfreeR PGA", SND_SOC_NOPM,
1243 0, 0, NULL, 0, handsfreerpga_event,
1244 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001245 /* Vibra */
1246 SND_SOC_DAPM_MUX("Vibra Mux", TWL4030_REG_VIBRA_CTL, 0, 0,
1247 &twl4030_dapm_vibra_control),
1248 SND_SOC_DAPM_MUX("Vibra Route", SND_SOC_NOPM, 0, 0,
1249 &twl4030_dapm_vibrapath_control),
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001250
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001251 /* Introducing four virtual ADC, since TWL4030 have four channel for
1252 capture */
1253 SND_SOC_DAPM_ADC("ADC Virtual Left1", "Left Front Capture",
1254 SND_SOC_NOPM, 0, 0),
1255 SND_SOC_DAPM_ADC("ADC Virtual Right1", "Right Front Capture",
1256 SND_SOC_NOPM, 0, 0),
1257 SND_SOC_DAPM_ADC("ADC Virtual Left2", "Left Rear Capture",
1258 SND_SOC_NOPM, 0, 0),
1259 SND_SOC_DAPM_ADC("ADC Virtual Right2", "Right Rear Capture",
1260 SND_SOC_NOPM, 0, 0),
1261
1262 /* Analog/Digital mic path selection.
1263 TX1 Left/Right: either analog Left/Right or Digimic0
1264 TX2 Left/Right: either analog Left/Right or Digimic1 */
1265 SND_SOC_DAPM_MUX_E("TX1 Capture Route", SND_SOC_NOPM, 0, 0,
1266 &twl4030_dapm_micpathtx1_control, micpath_event,
1267 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD|
1268 SND_SOC_DAPM_POST_REG),
1269 SND_SOC_DAPM_MUX_E("TX2 Capture Route", SND_SOC_NOPM, 0, 0,
1270 &twl4030_dapm_micpathtx2_control, micpath_event,
1271 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD|
1272 SND_SOC_DAPM_POST_REG),
1273
Joonyoung Shim97b80962009-05-11 20:36:08 +09001274 /* Analog input mixers for the capture amplifiers */
Peter Ujfalusi90289352009-08-14 08:44:00 +03001275 SND_SOC_DAPM_MIXER("Analog Left",
Joonyoung Shim97b80962009-05-11 20:36:08 +09001276 TWL4030_REG_ANAMICL, 4, 0,
1277 &twl4030_dapm_analoglmic_controls[0],
1278 ARRAY_SIZE(twl4030_dapm_analoglmic_controls)),
Peter Ujfalusi90289352009-08-14 08:44:00 +03001279 SND_SOC_DAPM_MIXER("Analog Right",
Joonyoung Shim97b80962009-05-11 20:36:08 +09001280 TWL4030_REG_ANAMICR, 4, 0,
1281 &twl4030_dapm_analogrmic_controls[0],
1282 ARRAY_SIZE(twl4030_dapm_analogrmic_controls)),
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001283
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001284 SND_SOC_DAPM_PGA("ADC Physical Left",
1285 TWL4030_REG_AVADC_CTL, 3, 0, NULL, 0),
1286 SND_SOC_DAPM_PGA("ADC Physical Right",
1287 TWL4030_REG_AVADC_CTL, 1, 0, NULL, 0),
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001288
1289 SND_SOC_DAPM_PGA("Digimic0 Enable",
1290 TWL4030_REG_ADCMICSEL, 1, 0, NULL, 0),
1291 SND_SOC_DAPM_PGA("Digimic1 Enable",
1292 TWL4030_REG_ADCMICSEL, 3, 0, NULL, 0),
1293
1294 SND_SOC_DAPM_MICBIAS("Mic Bias 1", TWL4030_REG_MICBIAS_CTL, 0, 0),
1295 SND_SOC_DAPM_MICBIAS("Mic Bias 2", TWL4030_REG_MICBIAS_CTL, 1, 0),
1296 SND_SOC_DAPM_MICBIAS("Headset Mic Bias", TWL4030_REG_MICBIAS_CTL, 2, 0),
Peter Ujfalusi73939582009-01-29 14:57:50 +02001297
Steve Sakomancc175572008-10-30 21:35:26 -07001298};
1299
1300static const struct snd_soc_dapm_route intercon[] = {
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001301 {"Digital L1 Playback Mixer", NULL, "DAC Left1"},
1302 {"Digital R1 Playback Mixer", NULL, "DAC Right1"},
1303 {"Digital L2 Playback Mixer", NULL, "DAC Left2"},
1304 {"Digital R2 Playback Mixer", NULL, "DAC Right2"},
1305 {"Digital Voice Playback Mixer", NULL, "DAC Voice"},
Peter Ujfalusi73939582009-01-29 14:57:50 +02001306
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001307 {"Analog L1 Playback Mixer", NULL, "Digital L1 Playback Mixer"},
1308 {"Analog R1 Playback Mixer", NULL, "Digital R1 Playback Mixer"},
1309 {"Analog L2 Playback Mixer", NULL, "Digital L2 Playback Mixer"},
1310 {"Analog R2 Playback Mixer", NULL, "Digital R2 Playback Mixer"},
1311 {"Analog Voice Playback Mixer", NULL, "Digital Voice Playback Mixer"},
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001312
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001313 /* Internal playback routings */
1314 /* Earpiece */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001315 {"Earpiece Mixer", "Voice", "Analog Voice Playback Mixer"},
1316 {"Earpiece Mixer", "AudioL1", "Analog L1 Playback Mixer"},
1317 {"Earpiece Mixer", "AudioL2", "Analog L2 Playback Mixer"},
1318 {"Earpiece Mixer", "AudioR1", "Analog R1 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001319 {"Earpiece PGA", NULL, "Earpiece Mixer"},
Peter Ujfalusi2a6f5c52008-12-09 12:35:48 +02001320 /* PreDrivL */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001321 {"PredriveL Mixer", "Voice", "Analog Voice Playback Mixer"},
1322 {"PredriveL Mixer", "AudioL1", "Analog L1 Playback Mixer"},
1323 {"PredriveL Mixer", "AudioL2", "Analog L2 Playback Mixer"},
1324 {"PredriveL Mixer", "AudioR2", "Analog R2 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001325 {"PredriveL PGA", NULL, "PredriveL Mixer"},
Peter Ujfalusi2a6f5c52008-12-09 12:35:48 +02001326 /* PreDrivR */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001327 {"PredriveR Mixer", "Voice", "Analog Voice Playback Mixer"},
1328 {"PredriveR Mixer", "AudioR1", "Analog R1 Playback Mixer"},
1329 {"PredriveR Mixer", "AudioR2", "Analog R2 Playback Mixer"},
1330 {"PredriveR Mixer", "AudioL2", "Analog L2 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001331 {"PredriveR PGA", NULL, "PredriveR Mixer"},
Peter Ujfalusidfad21a2008-12-09 12:35:49 +02001332 /* HeadsetL */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001333 {"HeadsetL Mixer", "Voice", "Analog Voice Playback Mixer"},
1334 {"HeadsetL Mixer", "AudioL1", "Analog L1 Playback Mixer"},
1335 {"HeadsetL Mixer", "AudioL2", "Analog L2 Playback Mixer"},
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001336 {"HeadsetL PGA", NULL, "HeadsetL Mixer"},
Peter Ujfalusidfad21a2008-12-09 12:35:49 +02001337 /* HeadsetR */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001338 {"HeadsetR Mixer", "Voice", "Analog Voice Playback Mixer"},
1339 {"HeadsetR Mixer", "AudioR1", "Analog R1 Playback Mixer"},
1340 {"HeadsetR Mixer", "AudioR2", "Analog R2 Playback Mixer"},
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001341 {"HeadsetR PGA", NULL, "HeadsetR Mixer"},
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +02001342 /* CarkitL */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001343 {"CarkitL Mixer", "Voice", "Analog Voice Playback Mixer"},
1344 {"CarkitL Mixer", "AudioL1", "Analog L1 Playback Mixer"},
1345 {"CarkitL Mixer", "AudioL2", "Analog L2 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001346 {"CarkitL PGA", NULL, "CarkitL Mixer"},
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +02001347 /* CarkitR */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001348 {"CarkitR Mixer", "Voice", "Analog Voice Playback Mixer"},
1349 {"CarkitR Mixer", "AudioR1", "Analog R1 Playback Mixer"},
1350 {"CarkitR Mixer", "AudioR2", "Analog R2 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001351 {"CarkitR PGA", NULL, "CarkitR Mixer"},
Peter Ujfalusidf339802008-12-09 12:35:51 +02001352 /* HandsfreeL */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001353 {"HandsfreeL Mux", "Voice", "Analog Voice Playback Mixer"},
1354 {"HandsfreeL Mux", "AudioL1", "Analog L1 Playback Mixer"},
1355 {"HandsfreeL Mux", "AudioL2", "Analog L2 Playback Mixer"},
1356 {"HandsfreeL Mux", "AudioR2", "Analog R2 Playback Mixer"},
Lopez Cruz, Misaele3c7dbb2009-06-25 12:36:14 -05001357 {"HandsfreeL", "Switch", "HandsfreeL Mux"},
1358 {"HandsfreeL PGA", NULL, "HandsfreeL"},
Peter Ujfalusidf339802008-12-09 12:35:51 +02001359 /* HandsfreeR */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001360 {"HandsfreeR Mux", "Voice", "Analog Voice Playback Mixer"},
1361 {"HandsfreeR Mux", "AudioR1", "Analog R1 Playback Mixer"},
1362 {"HandsfreeR Mux", "AudioR2", "Analog R2 Playback Mixer"},
1363 {"HandsfreeR Mux", "AudioL2", "Analog L2 Playback Mixer"},
Lopez Cruz, Misaele3c7dbb2009-06-25 12:36:14 -05001364 {"HandsfreeR", "Switch", "HandsfreeR Mux"},
1365 {"HandsfreeR PGA", NULL, "HandsfreeR"},
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001366 /* Vibra */
1367 {"Vibra Mux", "AudioL1", "DAC Left1"},
1368 {"Vibra Mux", "AudioR1", "DAC Right1"},
1369 {"Vibra Mux", "AudioL2", "DAC Left2"},
1370 {"Vibra Mux", "AudioR2", "DAC Right2"},
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001371
Steve Sakomancc175572008-10-30 21:35:26 -07001372 /* outputs */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001373 {"OUTL", NULL, "Analog L2 Playback Mixer"},
1374 {"OUTR", NULL, "Analog R2 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001375 {"EARPIECE", NULL, "Earpiece PGA"},
1376 {"PREDRIVEL", NULL, "PredriveL PGA"},
1377 {"PREDRIVER", NULL, "PredriveR PGA"},
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001378 {"HSOL", NULL, "HeadsetL PGA"},
1379 {"HSOR", NULL, "HeadsetR PGA"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001380 {"CARKITL", NULL, "CarkitL PGA"},
1381 {"CARKITR", NULL, "CarkitR PGA"},
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +03001382 {"HFL", NULL, "HandsfreeL PGA"},
1383 {"HFR", NULL, "HandsfreeR PGA"},
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001384 {"Vibra Route", "Audio", "Vibra Mux"},
1385 {"VIBRA", NULL, "Vibra Route"},
Steve Sakomancc175572008-10-30 21:35:26 -07001386
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001387 /* Capture path */
Peter Ujfalusi90289352009-08-14 08:44:00 +03001388 {"Analog Left", "Main Mic Capture Switch", "MAINMIC"},
1389 {"Analog Left", "Headset Mic Capture Switch", "HSMIC"},
1390 {"Analog Left", "AUXL Capture Switch", "AUXL"},
1391 {"Analog Left", "Carkit Mic Capture Switch", "CARKITMIC"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001392
Peter Ujfalusi90289352009-08-14 08:44:00 +03001393 {"Analog Right", "Sub Mic Capture Switch", "SUBMIC"},
1394 {"Analog Right", "AUXR Capture Switch", "AUXR"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001395
Peter Ujfalusi90289352009-08-14 08:44:00 +03001396 {"ADC Physical Left", NULL, "Analog Left"},
1397 {"ADC Physical Right", NULL, "Analog Right"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001398
1399 {"Digimic0 Enable", NULL, "DIGIMIC0"},
1400 {"Digimic1 Enable", NULL, "DIGIMIC1"},
1401
1402 /* TX1 Left capture path */
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001403 {"TX1 Capture Route", "Analog", "ADC Physical Left"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001404 {"TX1 Capture Route", "Digimic0", "Digimic0 Enable"},
1405 /* TX1 Right capture path */
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001406 {"TX1 Capture Route", "Analog", "ADC Physical Right"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001407 {"TX1 Capture Route", "Digimic0", "Digimic0 Enable"},
1408 /* TX2 Left capture path */
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001409 {"TX2 Capture Route", "Analog", "ADC Physical Left"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001410 {"TX2 Capture Route", "Digimic1", "Digimic1 Enable"},
1411 /* TX2 Right capture path */
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001412 {"TX2 Capture Route", "Analog", "ADC Physical Right"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001413 {"TX2 Capture Route", "Digimic1", "Digimic1 Enable"},
1414
1415 {"ADC Virtual Left1", NULL, "TX1 Capture Route"},
1416 {"ADC Virtual Right1", NULL, "TX1 Capture Route"},
1417 {"ADC Virtual Left2", NULL, "TX2 Capture Route"},
1418 {"ADC Virtual Right2", NULL, "TX2 Capture Route"},
1419
Peter Ujfalusi73939582009-01-29 14:57:50 +02001420 /* Analog bypass routes */
Peter Ujfalusi90289352009-08-14 08:44:00 +03001421 {"Right1 Analog Loopback", "Switch", "Analog Right"},
1422 {"Left1 Analog Loopback", "Switch", "Analog Left"},
1423 {"Right2 Analog Loopback", "Switch", "Analog Right"},
1424 {"Left2 Analog Loopback", "Switch", "Analog Left"},
1425 {"Voice Analog Loopback", "Switch", "Analog Left"},
Peter Ujfalusi73939582009-01-29 14:57:50 +02001426
Peter Ujfalusi78e08e22009-10-28 10:57:04 +02001427 /* Supply for the Analog loopbacks */
1428 {"Right1 Analog Loopback", NULL, "FM Loop Enable"},
1429 {"Left1 Analog Loopback", NULL, "FM Loop Enable"},
1430 {"Right2 Analog Loopback", NULL, "FM Loop Enable"},
1431 {"Left2 Analog Loopback", NULL, "FM Loop Enable"},
1432 {"Voice Analog Loopback", NULL, "FM Loop Enable"},
1433
Peter Ujfalusi73939582009-01-29 14:57:50 +02001434 {"Analog R1 Playback Mixer", NULL, "Right1 Analog Loopback"},
1435 {"Analog L1 Playback Mixer", NULL, "Left1 Analog Loopback"},
1436 {"Analog R2 Playback Mixer", NULL, "Right2 Analog Loopback"},
1437 {"Analog L2 Playback Mixer", NULL, "Left2 Analog Loopback"},
Lopez Cruz, Misaelfcd274a2009-04-30 21:47:22 -05001438 {"Analog Voice Playback Mixer", NULL, "Voice Analog Loopback"},
Peter Ujfalusi73939582009-01-29 14:57:50 +02001439
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001440 /* Digital bypass routes */
1441 {"Right Digital Loopback", "Volume", "TX1 Capture Route"},
1442 {"Left Digital Loopback", "Volume", "TX1 Capture Route"},
Lopez Cruz, Misaelee8f6892009-04-30 21:48:08 -05001443 {"Voice Digital Loopback", "Volume", "TX2 Capture Route"},
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001444
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001445 {"Digital R2 Playback Mixer", NULL, "Right Digital Loopback"},
1446 {"Digital L2 Playback Mixer", NULL, "Left Digital Loopback"},
1447 {"Digital Voice Playback Mixer", NULL, "Voice Digital Loopback"},
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001448
Steve Sakomancc175572008-10-30 21:35:26 -07001449};
1450
1451static int twl4030_add_widgets(struct snd_soc_codec *codec)
1452{
1453 snd_soc_dapm_new_controls(codec, twl4030_dapm_widgets,
1454 ARRAY_SIZE(twl4030_dapm_widgets));
1455
1456 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
1457
1458 snd_soc_dapm_new_widgets(codec);
1459 return 0;
1460}
1461
Steve Sakomancc175572008-10-30 21:35:26 -07001462static int twl4030_set_bias_level(struct snd_soc_codec *codec,
1463 enum snd_soc_bias_level level)
1464{
1465 switch (level) {
1466 case SND_SOC_BIAS_ON:
Peter Ujfalusi2845fa12009-10-28 10:57:05 +02001467 twl4030_apll_enable(codec, 1);
Steve Sakomancc175572008-10-30 21:35:26 -07001468 break;
1469 case SND_SOC_BIAS_PREPARE:
Steve Sakomancc175572008-10-30 21:35:26 -07001470 break;
1471 case SND_SOC_BIAS_STANDBY:
Peter Ujfalusi78e08e22009-10-28 10:57:04 +02001472 if (codec->bias_level == SND_SOC_BIAS_OFF)
1473 twl4030_power_up(codec);
Peter Ujfalusi2845fa12009-10-28 10:57:05 +02001474 twl4030_apll_enable(codec, 0);
Steve Sakomancc175572008-10-30 21:35:26 -07001475 break;
1476 case SND_SOC_BIAS_OFF:
1477 twl4030_power_down(codec);
1478 break;
1479 }
1480 codec->bias_level = level;
1481
1482 return 0;
1483}
1484
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001485static void twl4030_constraints(struct twl4030_priv *twl4030,
1486 struct snd_pcm_substream *mst_substream)
1487{
1488 struct snd_pcm_substream *slv_substream;
1489
1490 /* Pick the stream, which need to be constrained */
1491 if (mst_substream == twl4030->master_substream)
1492 slv_substream = twl4030->slave_substream;
1493 else if (mst_substream == twl4030->slave_substream)
1494 slv_substream = twl4030->master_substream;
1495 else /* This should not happen.. */
1496 return;
1497
1498 /* Set the constraints according to the already configured stream */
1499 snd_pcm_hw_constraint_minmax(slv_substream->runtime,
1500 SNDRV_PCM_HW_PARAM_RATE,
1501 twl4030->rate,
1502 twl4030->rate);
1503
1504 snd_pcm_hw_constraint_minmax(slv_substream->runtime,
1505 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1506 twl4030->sample_bits,
1507 twl4030->sample_bits);
1508
1509 snd_pcm_hw_constraint_minmax(slv_substream->runtime,
1510 SNDRV_PCM_HW_PARAM_CHANNELS,
1511 twl4030->channels,
1512 twl4030->channels);
1513}
1514
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001515/* In case of 4 channel mode, the RX1 L/R for playback and the TX2 L/R for
1516 * capture has to be enabled/disabled. */
1517static void twl4030_tdm_enable(struct snd_soc_codec *codec, int direction,
1518 int enable)
1519{
1520 u8 reg, mask;
1521
1522 reg = twl4030_read_reg_cache(codec, TWL4030_REG_OPTION);
1523
1524 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1525 mask = TWL4030_ARXL1_VRX_EN | TWL4030_ARXR1_EN;
1526 else
1527 mask = TWL4030_ATXL2_VTXL_EN | TWL4030_ATXR2_VTXR_EN;
1528
1529 if (enable)
1530 reg |= mask;
1531 else
1532 reg &= ~mask;
1533
1534 twl4030_write(codec, TWL4030_REG_OPTION, reg);
1535}
1536
Peter Ujfalusid6648da2009-04-07 09:14:00 +03001537static int twl4030_startup(struct snd_pcm_substream *substream,
1538 struct snd_soc_dai *dai)
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001539{
1540 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1541 struct snd_soc_device *socdev = rtd->socdev;
Peter Ujfalusid6648da2009-04-07 09:14:00 +03001542 struct snd_soc_codec *codec = socdev->card->codec;
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001543 struct twl4030_priv *twl4030 = codec->private_data;
1544
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001545 if (twl4030->master_substream) {
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001546 twl4030->slave_substream = substream;
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001547 /* The DAI has one configuration for playback and capture, so
1548 * if the DAI has been already configured then constrain this
1549 * substream to match it. */
1550 if (twl4030->configured)
1551 twl4030_constraints(twl4030, twl4030->master_substream);
1552 } else {
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001553 if (!(twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE) &
1554 TWL4030_OPTION_1)) {
1555 /* In option2 4 channel is not supported, set the
1556 * constraint for the first stream for channels, the
1557 * second stream will 'inherit' this cosntraint */
1558 snd_pcm_hw_constraint_minmax(substream->runtime,
1559 SNDRV_PCM_HW_PARAM_CHANNELS,
1560 2, 2);
1561 }
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001562 twl4030->master_substream = substream;
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001563 }
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001564
1565 return 0;
1566}
1567
Peter Ujfalusid6648da2009-04-07 09:14:00 +03001568static void twl4030_shutdown(struct snd_pcm_substream *substream,
1569 struct snd_soc_dai *dai)
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001570{
1571 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1572 struct snd_soc_device *socdev = rtd->socdev;
Peter Ujfalusid6648da2009-04-07 09:14:00 +03001573 struct snd_soc_codec *codec = socdev->card->codec;
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001574 struct twl4030_priv *twl4030 = codec->private_data;
1575
1576 if (twl4030->master_substream == substream)
1577 twl4030->master_substream = twl4030->slave_substream;
1578
1579 twl4030->slave_substream = NULL;
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001580
1581 /* If all streams are closed, or the remaining stream has not yet
1582 * been configured than set the DAI as not configured. */
1583 if (!twl4030->master_substream)
1584 twl4030->configured = 0;
1585 else if (!twl4030->master_substream->runtime->channels)
1586 twl4030->configured = 0;
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001587
1588 /* If the closing substream had 4 channel, do the necessary cleanup */
1589 if (substream->runtime->channels == 4)
1590 twl4030_tdm_enable(codec, substream->stream, 0);
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001591}
1592
Steve Sakomancc175572008-10-30 21:35:26 -07001593static int twl4030_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +00001594 struct snd_pcm_hw_params *params,
1595 struct snd_soc_dai *dai)
Steve Sakomancc175572008-10-30 21:35:26 -07001596{
1597 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1598 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +00001599 struct snd_soc_codec *codec = socdev->card->codec;
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001600 struct twl4030_priv *twl4030 = codec->private_data;
Steve Sakomancc175572008-10-30 21:35:26 -07001601 u8 mode, old_mode, format, old_format;
1602
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001603 /* If the substream has 4 channel, do the necessary setup */
1604 if (params_channels(params) == 4) {
Peter Ujfalusieaf1ac82009-06-01 14:06:40 +03001605 format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1606 mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE);
1607
1608 /* Safety check: are we in the correct operating mode and
1609 * the interface is in TDM mode? */
1610 if ((mode & TWL4030_OPTION_1) &&
1611 ((format & TWL4030_AIF_FORMAT) == TWL4030_AIF_FORMAT_TDM))
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001612 twl4030_tdm_enable(codec, substream->stream, 1);
1613 else
1614 return -EINVAL;
1615 }
1616
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001617 if (twl4030->configured)
1618 /* Ignoring hw_params for already configured DAI */
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001619 return 0;
1620
Steve Sakomancc175572008-10-30 21:35:26 -07001621 /* bit rate */
1622 old_mode = twl4030_read_reg_cache(codec,
1623 TWL4030_REG_CODEC_MODE) & ~TWL4030_CODECPDZ;
1624 mode = old_mode & ~TWL4030_APLL_RATE;
1625
1626 switch (params_rate(params)) {
1627 case 8000:
1628 mode |= TWL4030_APLL_RATE_8000;
1629 break;
1630 case 11025:
1631 mode |= TWL4030_APLL_RATE_11025;
1632 break;
1633 case 12000:
1634 mode |= TWL4030_APLL_RATE_12000;
1635 break;
1636 case 16000:
1637 mode |= TWL4030_APLL_RATE_16000;
1638 break;
1639 case 22050:
1640 mode |= TWL4030_APLL_RATE_22050;
1641 break;
1642 case 24000:
1643 mode |= TWL4030_APLL_RATE_24000;
1644 break;
1645 case 32000:
1646 mode |= TWL4030_APLL_RATE_32000;
1647 break;
1648 case 44100:
1649 mode |= TWL4030_APLL_RATE_44100;
1650 break;
1651 case 48000:
1652 mode |= TWL4030_APLL_RATE_48000;
1653 break;
Peter Ujfalusi103f2112009-04-03 14:39:05 +03001654 case 96000:
1655 mode |= TWL4030_APLL_RATE_96000;
1656 break;
Steve Sakomancc175572008-10-30 21:35:26 -07001657 default:
1658 printk(KERN_ERR "TWL4030 hw params: unknown rate %d\n",
1659 params_rate(params));
1660 return -EINVAL;
1661 }
1662
1663 if (mode != old_mode) {
1664 /* change rate and set CODECPDZ */
Peter Ujfalusi73939582009-01-29 14:57:50 +02001665 twl4030_codec_enable(codec, 0);
Steve Sakomancc175572008-10-30 21:35:26 -07001666 twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode);
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +02001667 twl4030_codec_enable(codec, 1);
Steve Sakomancc175572008-10-30 21:35:26 -07001668 }
1669
1670 /* sample size */
1671 old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1672 format = old_format;
1673 format &= ~TWL4030_DATA_WIDTH;
1674 switch (params_format(params)) {
1675 case SNDRV_PCM_FORMAT_S16_LE:
1676 format |= TWL4030_DATA_WIDTH_16S_16W;
1677 break;
1678 case SNDRV_PCM_FORMAT_S24_LE:
1679 format |= TWL4030_DATA_WIDTH_32S_24W;
1680 break;
1681 default:
1682 printk(KERN_ERR "TWL4030 hw params: unknown format %d\n",
1683 params_format(params));
1684 return -EINVAL;
1685 }
1686
1687 if (format != old_format) {
1688
1689 /* clear CODECPDZ before changing format (codec requirement) */
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +02001690 twl4030_codec_enable(codec, 0);
Steve Sakomancc175572008-10-30 21:35:26 -07001691
1692 /* change format */
1693 twl4030_write(codec, TWL4030_REG_AUDIO_IF, format);
1694
1695 /* set CODECPDZ afterwards */
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +02001696 twl4030_codec_enable(codec, 1);
Steve Sakomancc175572008-10-30 21:35:26 -07001697 }
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001698
1699 /* Store the important parameters for the DAI configuration and set
1700 * the DAI as configured */
1701 twl4030->configured = 1;
1702 twl4030->rate = params_rate(params);
1703 twl4030->sample_bits = hw_param_interval(params,
1704 SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
1705 twl4030->channels = params_channels(params);
1706
1707 /* If both playback and capture streams are open, and one of them
1708 * is setting the hw parameters right now (since we are here), set
1709 * constraints to the other stream to match the current one. */
1710 if (twl4030->slave_substream)
1711 twl4030_constraints(twl4030, substream);
1712
Steve Sakomancc175572008-10-30 21:35:26 -07001713 return 0;
1714}
1715
1716static int twl4030_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1717 int clk_id, unsigned int freq, int dir)
1718{
1719 struct snd_soc_codec *codec = codec_dai->codec;
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001720 struct twl4030_priv *twl4030 = codec->private_data;
Peter Ujfalusid8707ce2009-10-19 15:42:19 +03001721 u8 apll_ctrl;
Steve Sakomancc175572008-10-30 21:35:26 -07001722
Peter Ujfalusid8707ce2009-10-19 15:42:19 +03001723 apll_ctrl = twl4030_read_reg_cache(codec, TWL4030_REG_APLL_CTL);
1724 apll_ctrl &= ~TWL4030_APLL_INFREQ;
Steve Sakomancc175572008-10-30 21:35:26 -07001725 switch (freq) {
1726 case 19200000:
Peter Ujfalusid8707ce2009-10-19 15:42:19 +03001727 apll_ctrl |= TWL4030_APLL_INFREQ_19200KHZ;
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001728 twl4030->sysclk = 19200;
Steve Sakomancc175572008-10-30 21:35:26 -07001729 break;
1730 case 26000000:
Peter Ujfalusid8707ce2009-10-19 15:42:19 +03001731 apll_ctrl |= TWL4030_APLL_INFREQ_26000KHZ;
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001732 twl4030->sysclk = 26000;
Steve Sakomancc175572008-10-30 21:35:26 -07001733 break;
1734 case 38400000:
Peter Ujfalusid8707ce2009-10-19 15:42:19 +03001735 apll_ctrl |= TWL4030_APLL_INFREQ_38400KHZ;
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001736 twl4030->sysclk = 38400;
Steve Sakomancc175572008-10-30 21:35:26 -07001737 break;
1738 default:
1739 printk(KERN_ERR "TWL4030 set sysclk: unknown rate %d\n",
1740 freq);
1741 return -EINVAL;
1742 }
1743
Peter Ujfalusid8707ce2009-10-19 15:42:19 +03001744 twl4030_write(codec, TWL4030_REG_APLL_CTL, apll_ctrl);
Steve Sakomancc175572008-10-30 21:35:26 -07001745
1746 return 0;
1747}
1748
1749static int twl4030_set_dai_fmt(struct snd_soc_dai *codec_dai,
1750 unsigned int fmt)
1751{
1752 struct snd_soc_codec *codec = codec_dai->codec;
1753 u8 old_format, format;
1754
1755 /* get format */
1756 old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1757 format = old_format;
1758
1759 /* set master/slave audio interface */
1760 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1761 case SND_SOC_DAIFMT_CBM_CFM:
1762 format &= ~(TWL4030_AIF_SLAVE_EN);
Grazvydas Ignotase18c94d2008-11-05 23:51:05 +02001763 format &= ~(TWL4030_CLK256FS_EN);
Steve Sakomancc175572008-10-30 21:35:26 -07001764 break;
1765 case SND_SOC_DAIFMT_CBS_CFS:
Steve Sakomancc175572008-10-30 21:35:26 -07001766 format |= TWL4030_AIF_SLAVE_EN;
Grazvydas Ignotase18c94d2008-11-05 23:51:05 +02001767 format |= TWL4030_CLK256FS_EN;
Steve Sakomancc175572008-10-30 21:35:26 -07001768 break;
1769 default:
1770 return -EINVAL;
1771 }
1772
1773 /* interface format */
1774 format &= ~TWL4030_AIF_FORMAT;
1775 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1776 case SND_SOC_DAIFMT_I2S:
1777 format |= TWL4030_AIF_FORMAT_CODEC;
1778 break;
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001779 case SND_SOC_DAIFMT_DSP_A:
1780 format |= TWL4030_AIF_FORMAT_TDM;
1781 break;
Steve Sakomancc175572008-10-30 21:35:26 -07001782 default:
1783 return -EINVAL;
1784 }
1785
1786 if (format != old_format) {
1787
1788 /* clear CODECPDZ before changing format (codec requirement) */
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +02001789 twl4030_codec_enable(codec, 0);
Steve Sakomancc175572008-10-30 21:35:26 -07001790
1791 /* change format */
1792 twl4030_write(codec, TWL4030_REG_AUDIO_IF, format);
1793
1794 /* set CODECPDZ afterwards */
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +02001795 twl4030_codec_enable(codec, 1);
Steve Sakomancc175572008-10-30 21:35:26 -07001796 }
1797
1798 return 0;
1799}
1800
Lopez Cruz, Misael68140442009-07-03 02:21:39 -05001801static int twl4030_set_tristate(struct snd_soc_dai *dai, int tristate)
1802{
1803 struct snd_soc_codec *codec = dai->codec;
1804 u8 reg = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1805
1806 if (tristate)
1807 reg |= TWL4030_AIF_TRI_EN;
1808 else
1809 reg &= ~TWL4030_AIF_TRI_EN;
1810
1811 return twl4030_write(codec, TWL4030_REG_AUDIO_IF, reg);
1812}
1813
Misael Lopez Cruzb7a755a2009-05-17 20:02:31 -05001814/* In case of voice mode, the RX1 L(VRX) for downlink and the TX2 L/R
1815 * (VTXL, VTXR) for uplink has to be enabled/disabled. */
1816static void twl4030_voice_enable(struct snd_soc_codec *codec, int direction,
1817 int enable)
1818{
1819 u8 reg, mask;
1820
1821 reg = twl4030_read_reg_cache(codec, TWL4030_REG_OPTION);
1822
1823 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1824 mask = TWL4030_ARXL1_VRX_EN;
1825 else
1826 mask = TWL4030_ATXL2_VTXL_EN | TWL4030_ATXR2_VTXR_EN;
1827
1828 if (enable)
1829 reg |= mask;
1830 else
1831 reg &= ~mask;
1832
1833 twl4030_write(codec, TWL4030_REG_OPTION, reg);
1834}
1835
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09001836static int twl4030_voice_startup(struct snd_pcm_substream *substream,
1837 struct snd_soc_dai *dai)
1838{
1839 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1840 struct snd_soc_device *socdev = rtd->socdev;
1841 struct snd_soc_codec *codec = socdev->card->codec;
1842 u8 infreq;
1843 u8 mode;
1844
1845 /* If the system master clock is not 26MHz, the voice PCM interface is
1846 * not avilable.
1847 */
1848 infreq = twl4030_read_reg_cache(codec, TWL4030_REG_APLL_CTL)
1849 & TWL4030_APLL_INFREQ;
1850
1851 if (infreq != TWL4030_APLL_INFREQ_26000KHZ) {
1852 printk(KERN_ERR "TWL4030 voice startup: "
1853 "MCLK is not 26MHz, call set_sysclk() on init\n");
1854 return -EINVAL;
1855 }
1856
1857 /* If the codec mode is not option2, the voice PCM interface is not
1858 * avilable.
1859 */
1860 mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE)
1861 & TWL4030_OPT_MODE;
1862
1863 if (mode != TWL4030_OPTION_2) {
1864 printk(KERN_ERR "TWL4030 voice startup: "
1865 "the codec mode is not option2\n");
1866 return -EINVAL;
1867 }
1868
1869 return 0;
1870}
1871
Misael Lopez Cruzb7a755a2009-05-17 20:02:31 -05001872static void twl4030_voice_shutdown(struct snd_pcm_substream *substream,
1873 struct snd_soc_dai *dai)
1874{
1875 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1876 struct snd_soc_device *socdev = rtd->socdev;
1877 struct snd_soc_codec *codec = socdev->card->codec;
1878
1879 /* Enable voice digital filters */
1880 twl4030_voice_enable(codec, substream->stream, 0);
1881}
1882
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09001883static int twl4030_voice_hw_params(struct snd_pcm_substream *substream,
1884 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
1885{
1886 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1887 struct snd_soc_device *socdev = rtd->socdev;
1888 struct snd_soc_codec *codec = socdev->card->codec;
1889 u8 old_mode, mode;
1890
Misael Lopez Cruzb7a755a2009-05-17 20:02:31 -05001891 /* Enable voice digital filters */
1892 twl4030_voice_enable(codec, substream->stream, 1);
1893
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09001894 /* bit rate */
1895 old_mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE)
1896 & ~(TWL4030_CODECPDZ);
1897 mode = old_mode;
1898
1899 switch (params_rate(params)) {
1900 case 8000:
1901 mode &= ~(TWL4030_SEL_16K);
1902 break;
1903 case 16000:
1904 mode |= TWL4030_SEL_16K;
1905 break;
1906 default:
1907 printk(KERN_ERR "TWL4030 voice hw params: unknown rate %d\n",
1908 params_rate(params));
1909 return -EINVAL;
1910 }
1911
1912 if (mode != old_mode) {
1913 /* change rate and set CODECPDZ */
1914 twl4030_codec_enable(codec, 0);
1915 twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode);
1916 twl4030_codec_enable(codec, 1);
1917 }
1918
1919 return 0;
1920}
1921
1922static int twl4030_voice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1923 int clk_id, unsigned int freq, int dir)
1924{
1925 struct snd_soc_codec *codec = codec_dai->codec;
Peter Ujfalusid8707ce2009-10-19 15:42:19 +03001926 u8 apll_ctrl;
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09001927
Peter Ujfalusid8707ce2009-10-19 15:42:19 +03001928 apll_ctrl = twl4030_read_reg_cache(codec, TWL4030_REG_APLL_CTL);
1929 apll_ctrl &= ~TWL4030_APLL_INFREQ;
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09001930 switch (freq) {
1931 case 26000000:
Peter Ujfalusid8707ce2009-10-19 15:42:19 +03001932 apll_ctrl |= TWL4030_APLL_INFREQ_26000KHZ;
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09001933 break;
1934 default:
1935 printk(KERN_ERR "TWL4030 voice set sysclk: unknown rate %d\n",
1936 freq);
1937 return -EINVAL;
1938 }
1939
Peter Ujfalusid8707ce2009-10-19 15:42:19 +03001940 twl4030_write(codec, TWL4030_REG_APLL_CTL, apll_ctrl);
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09001941
1942 return 0;
1943}
1944
1945static int twl4030_voice_set_dai_fmt(struct snd_soc_dai *codec_dai,
1946 unsigned int fmt)
1947{
1948 struct snd_soc_codec *codec = codec_dai->codec;
1949 u8 old_format, format;
1950
1951 /* get format */
1952 old_format = twl4030_read_reg_cache(codec, TWL4030_REG_VOICE_IF);
1953 format = old_format;
1954
1955 /* set master/slave audio interface */
1956 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
Lopez Cruz, Misaelc2643012009-06-19 03:23:42 -05001957 case SND_SOC_DAIFMT_CBM_CFM:
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09001958 format &= ~(TWL4030_VIF_SLAVE_EN);
1959 break;
1960 case SND_SOC_DAIFMT_CBS_CFS:
1961 format |= TWL4030_VIF_SLAVE_EN;
1962 break;
1963 default:
1964 return -EINVAL;
1965 }
1966
1967 /* clock inversion */
1968 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1969 case SND_SOC_DAIFMT_IB_NF:
1970 format &= ~(TWL4030_VIF_FORMAT);
1971 break;
1972 case SND_SOC_DAIFMT_NB_IF:
1973 format |= TWL4030_VIF_FORMAT;
1974 break;
1975 default:
1976 return -EINVAL;
1977 }
1978
1979 if (format != old_format) {
1980 /* change format and set CODECPDZ */
1981 twl4030_codec_enable(codec, 0);
1982 twl4030_write(codec, TWL4030_REG_VOICE_IF, format);
1983 twl4030_codec_enable(codec, 1);
1984 }
1985
1986 return 0;
1987}
1988
Lopez Cruz, Misael68140442009-07-03 02:21:39 -05001989static int twl4030_voice_set_tristate(struct snd_soc_dai *dai, int tristate)
1990{
1991 struct snd_soc_codec *codec = dai->codec;
1992 u8 reg = twl4030_read_reg_cache(codec, TWL4030_REG_VOICE_IF);
1993
1994 if (tristate)
1995 reg |= TWL4030_VIF_TRI_EN;
1996 else
1997 reg &= ~TWL4030_VIF_TRI_EN;
1998
1999 return twl4030_write(codec, TWL4030_REG_VOICE_IF, reg);
2000}
2001
Jarkko Nikulabbba9442008-11-12 17:05:41 +02002002#define TWL4030_RATES (SNDRV_PCM_RATE_8000_48000)
Steve Sakomancc175572008-10-30 21:35:26 -07002003#define TWL4030_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FORMAT_S24_LE)
2004
Joonyoung Shim10d9e3d2009-03-16 21:23:35 +09002005static struct snd_soc_dai_ops twl4030_dai_ops = {
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02002006 .startup = twl4030_startup,
2007 .shutdown = twl4030_shutdown,
Joonyoung Shim10d9e3d2009-03-16 21:23:35 +09002008 .hw_params = twl4030_hw_params,
2009 .set_sysclk = twl4030_set_dai_sysclk,
2010 .set_fmt = twl4030_set_dai_fmt,
Lopez Cruz, Misael68140442009-07-03 02:21:39 -05002011 .set_tristate = twl4030_set_tristate,
Joonyoung Shim10d9e3d2009-03-16 21:23:35 +09002012};
2013
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002014static struct snd_soc_dai_ops twl4030_dai_voice_ops = {
2015 .startup = twl4030_voice_startup,
Misael Lopez Cruzb7a755a2009-05-17 20:02:31 -05002016 .shutdown = twl4030_voice_shutdown,
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002017 .hw_params = twl4030_voice_hw_params,
2018 .set_sysclk = twl4030_voice_set_dai_sysclk,
2019 .set_fmt = twl4030_voice_set_dai_fmt,
Lopez Cruz, Misael68140442009-07-03 02:21:39 -05002020 .set_tristate = twl4030_voice_set_tristate,
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002021};
2022
2023struct snd_soc_dai twl4030_dai[] = {
2024{
Steve Sakomancc175572008-10-30 21:35:26 -07002025 .name = "twl4030",
2026 .playback = {
Peter Ujfalusib4852b72009-05-22 15:12:15 +03002027 .stream_name = "HiFi Playback",
Steve Sakomancc175572008-10-30 21:35:26 -07002028 .channels_min = 2,
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03002029 .channels_max = 4,
Peter Ujfalusi31ad0f32009-03-27 10:39:07 +02002030 .rates = TWL4030_RATES | SNDRV_PCM_RATE_96000,
Steve Sakomancc175572008-10-30 21:35:26 -07002031 .formats = TWL4030_FORMATS,},
2032 .capture = {
2033 .stream_name = "Capture",
2034 .channels_min = 2,
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03002035 .channels_max = 4,
Steve Sakomancc175572008-10-30 21:35:26 -07002036 .rates = TWL4030_RATES,
2037 .formats = TWL4030_FORMATS,},
Joonyoung Shim10d9e3d2009-03-16 21:23:35 +09002038 .ops = &twl4030_dai_ops,
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002039},
2040{
2041 .name = "twl4030 Voice",
2042 .playback = {
Peter Ujfalusib4852b72009-05-22 15:12:15 +03002043 .stream_name = "Voice Playback",
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002044 .channels_min = 1,
2045 .channels_max = 1,
2046 .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
2047 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
2048 .capture = {
2049 .stream_name = "Capture",
2050 .channels_min = 1,
2051 .channels_max = 2,
2052 .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
2053 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
2054 .ops = &twl4030_dai_voice_ops,
2055},
Steve Sakomancc175572008-10-30 21:35:26 -07002056};
2057EXPORT_SYMBOL_GPL(twl4030_dai);
2058
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002059static int twl4030_soc_suspend(struct platform_device *pdev, pm_message_t state)
Steve Sakomancc175572008-10-30 21:35:26 -07002060{
2061 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +00002062 struct snd_soc_codec *codec = socdev->card->codec;
Steve Sakomancc175572008-10-30 21:35:26 -07002063
2064 twl4030_set_bias_level(codec, SND_SOC_BIAS_OFF);
2065
2066 return 0;
2067}
2068
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002069static int twl4030_soc_resume(struct platform_device *pdev)
Steve Sakomancc175572008-10-30 21:35:26 -07002070{
2071 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +00002072 struct snd_soc_codec *codec = socdev->card->codec;
Steve Sakomancc175572008-10-30 21:35:26 -07002073
2074 twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
2075 twl4030_set_bias_level(codec, codec->suspend_bias_level);
2076 return 0;
2077}
2078
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002079static struct snd_soc_codec *twl4030_codec;
Steve Sakomancc175572008-10-30 21:35:26 -07002080
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002081static int twl4030_soc_probe(struct platform_device *pdev)
Steve Sakomancc175572008-10-30 21:35:26 -07002082{
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002083 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Peter Ujfalusi9da28c72009-05-22 10:13:15 +03002084 struct twl4030_setup_data *setup = socdev->codec_data;
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002085 struct snd_soc_codec *codec;
2086 struct twl4030_priv *twl4030;
2087 int ret;
Steve Sakomancc175572008-10-30 21:35:26 -07002088
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002089 BUG_ON(!twl4030_codec);
Steve Sakomancc175572008-10-30 21:35:26 -07002090
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002091 codec = twl4030_codec;
2092 twl4030 = codec->private_data;
2093 socdev->card->codec = codec;
Steve Sakomancc175572008-10-30 21:35:26 -07002094
Peter Ujfalusi9da28c72009-05-22 10:13:15 +03002095 /* Configuration for headset ramp delay from setup data */
2096 if (setup) {
2097 unsigned char hs_pop;
2098
2099 if (setup->sysclk)
2100 twl4030->sysclk = setup->sysclk;
2101 else
2102 twl4030->sysclk = 26000;
2103
2104 hs_pop = twl4030_read_reg_cache(codec, TWL4030_REG_HS_POPN_SET);
2105 hs_pop &= ~TWL4030_RAMP_DELAY;
2106 hs_pop |= (setup->ramp_delay_value << 2);
2107 twl4030_write_reg_cache(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
2108 } else {
2109 twl4030->sysclk = 26000;
2110 }
2111
Steve Sakomancc175572008-10-30 21:35:26 -07002112 /* register pcms */
2113 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
2114 if (ret < 0) {
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002115 dev_err(&pdev->dev, "failed to create pcms\n");
2116 return ret;
Steve Sakomancc175572008-10-30 21:35:26 -07002117 }
2118
Ian Molton3e8e1952009-01-09 00:23:21 +00002119 snd_soc_add_controls(codec, twl4030_snd_controls,
2120 ARRAY_SIZE(twl4030_snd_controls));
Steve Sakomancc175572008-10-30 21:35:26 -07002121 twl4030_add_widgets(codec);
2122
Mark Brown968a6022008-11-28 11:49:07 +00002123 ret = snd_soc_init_card(socdev);
Steve Sakomancc175572008-10-30 21:35:26 -07002124 if (ret < 0) {
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002125 dev_err(&pdev->dev, "failed to register card\n");
Steve Sakomancc175572008-10-30 21:35:26 -07002126 goto card_err;
2127 }
2128
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002129 return 0;
Steve Sakomancc175572008-10-30 21:35:26 -07002130
2131card_err:
2132 snd_soc_free_pcms(socdev);
2133 snd_soc_dapm_free(socdev);
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002134
Steve Sakomancc175572008-10-30 21:35:26 -07002135 return ret;
2136}
2137
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002138static int twl4030_soc_remove(struct platform_device *pdev)
Steve Sakomancc175572008-10-30 21:35:26 -07002139{
2140 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +00002141 struct snd_soc_codec *codec = socdev->card->codec;
Steve Sakomancc175572008-10-30 21:35:26 -07002142
Peter Ujfalusi73939582009-01-29 14:57:50 +02002143 twl4030_set_bias_level(codec, SND_SOC_BIAS_OFF);
Peter Ujfalusic6d1662b2009-01-08 15:52:43 +02002144 snd_soc_free_pcms(socdev);
2145 snd_soc_dapm_free(socdev);
Peter Ujfalusi73939582009-01-29 14:57:50 +02002146 kfree(codec->private_data);
Steve Sakomancc175572008-10-30 21:35:26 -07002147 kfree(codec);
2148
2149 return 0;
2150}
2151
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002152static int __devinit twl4030_codec_probe(struct platform_device *pdev)
2153{
2154 struct twl4030_codec_audio_data *pdata = pdev->dev.platform_data;
2155 struct snd_soc_codec *codec;
2156 struct twl4030_priv *twl4030;
2157 int ret;
2158
2159 if (!pdata || !(pdata->audio_mclk == 19200000 ||
2160 pdata->audio_mclk == 26000000 ||
2161 pdata->audio_mclk == 38400000)) {
2162 dev_err(&pdev->dev, "Invalid platform_data\n");
2163 return -EINVAL;
2164 }
2165
2166 twl4030 = kzalloc(sizeof(struct twl4030_priv), GFP_KERNEL);
2167 if (twl4030 == NULL) {
2168 dev_err(&pdev->dev, "Can not allocate memroy\n");
2169 return -ENOMEM;
2170 }
2171
2172 codec = &twl4030->codec;
2173 codec->private_data = twl4030;
2174 codec->dev = &pdev->dev;
2175 twl4030_dai[0].dev = &pdev->dev;
2176 twl4030_dai[1].dev = &pdev->dev;
2177
2178 mutex_init(&codec->mutex);
2179 INIT_LIST_HEAD(&codec->dapm_widgets);
2180 INIT_LIST_HEAD(&codec->dapm_paths);
2181
2182 codec->name = "twl4030";
2183 codec->owner = THIS_MODULE;
2184 codec->read = twl4030_read_reg_cache;
2185 codec->write = twl4030_write;
2186 codec->set_bias_level = twl4030_set_bias_level;
2187 codec->dai = twl4030_dai;
2188 codec->num_dai = ARRAY_SIZE(twl4030_dai),
2189 codec->reg_cache_size = sizeof(twl4030_reg);
2190 codec->reg_cache = kmemdup(twl4030_reg, sizeof(twl4030_reg),
2191 GFP_KERNEL);
2192 if (codec->reg_cache == NULL) {
2193 ret = -ENOMEM;
2194 goto error_cache;
2195 }
2196
2197 platform_set_drvdata(pdev, twl4030);
2198 twl4030_codec = codec;
2199
2200 /* Set the defaults, and power up the codec */
2201 twl4030_init_chip(codec);
2202 twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
2203
2204 ret = snd_soc_register_codec(codec);
2205 if (ret != 0) {
2206 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
2207 goto error_codec;
2208 }
2209
2210 ret = snd_soc_register_dais(&twl4030_dai[0], ARRAY_SIZE(twl4030_dai));
2211 if (ret != 0) {
2212 dev_err(codec->dev, "Failed to register DAIs: %d\n", ret);
2213 snd_soc_unregister_codec(codec);
2214 goto error_codec;
2215 }
2216
2217 return 0;
2218
2219error_codec:
2220 twl4030_power_down(codec);
2221 kfree(codec->reg_cache);
2222error_cache:
2223 kfree(twl4030);
2224 return ret;
2225}
2226
2227static int __devexit twl4030_codec_remove(struct platform_device *pdev)
2228{
2229 struct twl4030_priv *twl4030 = platform_get_drvdata(pdev);
2230
2231 kfree(twl4030);
2232
2233 twl4030_codec = NULL;
2234 return 0;
2235}
2236
2237MODULE_ALIAS("platform:twl4030_codec_audio");
2238
2239static struct platform_driver twl4030_codec_driver = {
2240 .probe = twl4030_codec_probe,
2241 .remove = __devexit_p(twl4030_codec_remove),
2242 .driver = {
2243 .name = "twl4030_codec_audio",
2244 .owner = THIS_MODULE,
2245 },
Steve Sakomancc175572008-10-30 21:35:26 -07002246};
Steve Sakomancc175572008-10-30 21:35:26 -07002247
Takashi Iwai24e07db2008-12-10 07:40:24 +01002248static int __init twl4030_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +00002249{
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002250 return platform_driver_register(&twl4030_codec_driver);
Mark Brown64089b82008-12-08 19:17:58 +00002251}
Takashi Iwai24e07db2008-12-10 07:40:24 +01002252module_init(twl4030_modinit);
Mark Brown64089b82008-12-08 19:17:58 +00002253
2254static void __exit twl4030_exit(void)
2255{
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002256 platform_driver_unregister(&twl4030_codec_driver);
Mark Brown64089b82008-12-08 19:17:58 +00002257}
2258module_exit(twl4030_exit);
2259
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002260struct snd_soc_codec_device soc_codec_dev_twl4030 = {
2261 .probe = twl4030_soc_probe,
2262 .remove = twl4030_soc_remove,
2263 .suspend = twl4030_soc_suspend,
2264 .resume = twl4030_soc_resume,
2265};
2266EXPORT_SYMBOL_GPL(soc_codec_dev_twl4030);
2267
Steve Sakomancc175572008-10-30 21:35:26 -07002268MODULE_DESCRIPTION("ASoC TWL4030 codec driver");
2269MODULE_AUTHOR("Steve Sakoman");
2270MODULE_LICENSE("GPL");