blob: b4fcdb01fc49a9cc261c06545c1913d5d9adac14 [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>
Santosh Shilimkarb07682b2009-12-13 20:05:51 +010029#include <linux/i2c/twl.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Steve Sakomancc175572008-10-30 21:35:26 -070031#include <sound/core.h>
32#include <sound/pcm.h>
33#include <sound/pcm_params.h>
34#include <sound/soc.h>
35#include <sound/soc-dapm.h>
36#include <sound/initval.h>
Peter Ujfalusic10b82c2008-11-24 13:49:35 +020037#include <sound/tlv.h>
Steve Sakomancc175572008-10-30 21:35:26 -070038
39#include "twl4030.h"
40
41/*
42 * twl4030 register cache & default register settings
43 */
44static const u8 twl4030_reg[TWL4030_CACHEREGNUM] = {
45 0x00, /* this register not used */
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +020046 0x91, /* REG_CODEC_MODE (0x1) */
Steve Sakomancc175572008-10-30 21:35:26 -070047 0xc3, /* REG_OPTION (0x2) */
48 0x00, /* REG_UNKNOWN (0x3) */
49 0x00, /* REG_MICBIAS_CTL (0x4) */
Grazvydas Ignotas5920b452008-12-02 20:48:58 +020050 0x20, /* REG_ANAMICL (0x5) */
51 0x00, /* REG_ANAMICR (0x6) */
52 0x00, /* REG_AVADC_CTL (0x7) */
Steve Sakomancc175572008-10-30 21:35:26 -070053 0x00, /* REG_ADCMICSEL (0x8) */
54 0x00, /* REG_DIGMIXING (0x9) */
55 0x0c, /* REG_ATXL1PGA (0xA) */
56 0x0c, /* REG_ATXR1PGA (0xB) */
57 0x00, /* REG_AVTXL2PGA (0xC) */
58 0x00, /* REG_AVTXR2PGA (0xD) */
Peter Ujfalusic42a59e2010-02-09 15:24:04 +020059 0x00, /* REG_AUDIO_IF (0xE) */
Steve Sakomancc175572008-10-30 21:35:26 -070060 0x00, /* REG_VOICE_IF (0xF) */
61 0x00, /* REG_ARXR1PGA (0x10) */
62 0x00, /* REG_ARXL1PGA (0x11) */
63 0x6c, /* REG_ARXR2PGA (0x12) */
64 0x6c, /* REG_ARXL2PGA (0x13) */
65 0x00, /* REG_VRXPGA (0x14) */
66 0x00, /* REG_VSTPGA (0x15) */
67 0x00, /* REG_VRX2ARXPGA (0x16) */
Peter Ujfalusic8124592010-01-28 15:57:04 +020068 0x00, /* REG_AVDAC_CTL (0x17) */
Steve Sakomancc175572008-10-30 21:35:26 -070069 0x00, /* REG_ARX2VTXPGA (0x18) */
70 0x00, /* REG_ARXL1_APGA_CTL (0x19) */
71 0x00, /* REG_ARXR1_APGA_CTL (0x1A) */
Peter Ujfalusic8124592010-01-28 15:57:04 +020072 0x4a, /* REG_ARXL2_APGA_CTL (0x1B) */
73 0x4a, /* REG_ARXR2_APGA_CTL (0x1C) */
Steve Sakomancc175572008-10-30 21:35:26 -070074 0x00, /* REG_ATX2ARXPGA (0x1D) */
75 0x00, /* REG_BT_IF (0x1E) */
76 0x00, /* REG_BTPGA (0x1F) */
77 0x00, /* REG_BTSTPGA (0x20) */
78 0x00, /* REG_EAR_CTL (0x21) */
Peter Ujfalusie47c7962010-02-17 09:49:54 +020079 0x00, /* REG_HS_SEL (0x22) */
80 0x00, /* REG_HS_GAIN_SET (0x23) */
Steve Sakomancc175572008-10-30 21:35:26 -070081 0x00, /* REG_HS_POPN_SET (0x24) */
82 0x00, /* REG_PREDL_CTL (0x25) */
83 0x00, /* REG_PREDR_CTL (0x26) */
84 0x00, /* REG_PRECKL_CTL (0x27) */
85 0x00, /* REG_PRECKR_CTL (0x28) */
86 0x00, /* REG_HFL_CTL (0x29) */
87 0x00, /* REG_HFR_CTL (0x2A) */
88 0x00, /* REG_ALC_CTL (0x2B) */
89 0x00, /* REG_ALC_SET1 (0x2C) */
90 0x00, /* REG_ALC_SET2 (0x2D) */
91 0x00, /* REG_BOOST_CTL (0x2E) */
Peter Ujfalusif8d05bd2008-11-24 08:25:45 +020092 0x00, /* REG_SOFTVOL_CTL (0x2F) */
Steve Sakomancc175572008-10-30 21:35:26 -070093 0x00, /* REG_DTMF_FREQSEL (0x30) */
94 0x00, /* REG_DTMF_TONEXT1H (0x31) */
95 0x00, /* REG_DTMF_TONEXT1L (0x32) */
96 0x00, /* REG_DTMF_TONEXT2H (0x33) */
97 0x00, /* REG_DTMF_TONEXT2L (0x34) */
98 0x00, /* REG_DTMF_TONOFF (0x35) */
99 0x00, /* REG_DTMF_WANONOFF (0x36) */
100 0x00, /* REG_I2S_RX_SCRAMBLE_H (0x37) */
101 0x00, /* REG_I2S_RX_SCRAMBLE_M (0x38) */
102 0x00, /* REG_I2S_RX_SCRAMBLE_L (0x39) */
Peter Ujfalusic8124592010-01-28 15:57:04 +0200103 0x06, /* REG_APLL_CTL (0x3A) */
Steve Sakomancc175572008-10-30 21:35:26 -0700104 0x00, /* REG_DTMF_CTL (0x3B) */
105 0x00, /* REG_DTMF_PGA_CTL2 (0x3C) */
106 0x00, /* REG_DTMF_PGA_CTL1 (0x3D) */
107 0x00, /* REG_MISC_SET_1 (0x3E) */
108 0x00, /* REG_PCMBTMUX (0x3F) */
109 0x00, /* not used (0x40) */
110 0x00, /* not used (0x41) */
111 0x00, /* not used (0x42) */
112 0x00, /* REG_RX_PATH_SEL (0x43) */
113 0x00, /* REG_VDL_APGA_CTL (0x44) */
114 0x00, /* REG_VIBRA_CTL (0x45) */
115 0x00, /* REG_VIBRA_SET (0x46) */
116 0x00, /* REG_VIBRA_PWM_SET (0x47) */
117 0x00, /* REG_ANAMIC_GAIN (0x48) */
118 0x00, /* REG_MISC_SET_2 (0x49) */
Peter Ujfalusif3b5d302009-05-25 11:12:12 +0300119 0x00, /* REG_SW_SHADOW (0x4A) - Shadow, non HW register */
Steve Sakomancc175572008-10-30 21:35:26 -0700120};
121
Peter Ujfalusi73939582009-01-29 14:57:50 +0200122/* codec private data */
123struct twl4030_priv {
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300124 struct snd_soc_codec codec;
125
Peter Ujfalusi73939582009-01-29 14:57:50 +0200126 unsigned int codec_powered;
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +0300127
128 /* reference counts of AIF/APLL users */
Peter Ujfalusi2845fa12009-10-28 10:57:05 +0200129 unsigned int apll_enabled;
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +0200130
131 struct snd_pcm_substream *master_substream;
132 struct snd_pcm_substream *slave_substream;
Peter Ujfalusi6b87a912009-04-17 15:55:08 +0300133
134 unsigned int configured;
135 unsigned int rate;
136 unsigned int sample_bits;
137 unsigned int channels;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300138
139 unsigned int sysclk;
140
Peter Ujfalusic96907f2010-03-22 17:46:37 +0200141 /* Output (with associated amp) states */
142 u8 hsl_enabled, hsr_enabled;
143 u8 earpiece_enabled;
144 u8 predrivel_enabled, predriver_enabled;
145 u8 carkitl_enabled, carkitr_enabled;
Peter Ujfalusi73939582009-01-29 14:57:50 +0200146};
147
Steve Sakomancc175572008-10-30 21:35:26 -0700148/*
149 * read twl4030 register cache
150 */
151static inline unsigned int twl4030_read_reg_cache(struct snd_soc_codec *codec,
152 unsigned int reg)
153{
Takashi Iwaid08664f2009-06-04 09:58:18 +0200154 u8 *cache = codec->reg_cache;
Steve Sakomancc175572008-10-30 21:35:26 -0700155
Ian Molton91432e92009-01-17 17:44:23 +0000156 if (reg >= TWL4030_CACHEREGNUM)
157 return -EIO;
158
Steve Sakomancc175572008-10-30 21:35:26 -0700159 return cache[reg];
160}
161
162/*
163 * write twl4030 register cache
164 */
165static inline void twl4030_write_reg_cache(struct snd_soc_codec *codec,
166 u8 reg, u8 value)
167{
168 u8 *cache = codec->reg_cache;
169
170 if (reg >= TWL4030_CACHEREGNUM)
171 return;
172 cache[reg] = value;
173}
174
175/*
176 * write to the twl4030 register space
177 */
178static int twl4030_write(struct snd_soc_codec *codec,
179 unsigned int reg, unsigned int value)
180{
Mark Brownb2c812e2010-04-14 15:35:19 +0900181 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic96907f2010-03-22 17:46:37 +0200182 int write_to_reg = 0;
183
Steve Sakomancc175572008-10-30 21:35:26 -0700184 twl4030_write_reg_cache(codec, reg, value);
Peter Ujfalusic96907f2010-03-22 17:46:37 +0200185 if (likely(reg < TWL4030_REG_SW_SHADOW)) {
186 /* Decide if the given register can be written */
187 switch (reg) {
188 case TWL4030_REG_EAR_CTL:
189 if (twl4030->earpiece_enabled)
190 write_to_reg = 1;
191 break;
192 case TWL4030_REG_PREDL_CTL:
193 if (twl4030->predrivel_enabled)
194 write_to_reg = 1;
195 break;
196 case TWL4030_REG_PREDR_CTL:
197 if (twl4030->predriver_enabled)
198 write_to_reg = 1;
199 break;
200 case TWL4030_REG_PRECKL_CTL:
201 if (twl4030->carkitl_enabled)
202 write_to_reg = 1;
203 break;
204 case TWL4030_REG_PRECKR_CTL:
205 if (twl4030->carkitr_enabled)
206 write_to_reg = 1;
207 break;
208 case TWL4030_REG_HS_GAIN_SET:
209 if (twl4030->hsl_enabled || twl4030->hsr_enabled)
210 write_to_reg = 1;
211 break;
212 default:
213 /* All other register can be written */
214 write_to_reg = 1;
215 break;
216 }
217 if (write_to_reg)
218 return twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
219 value, reg);
220 }
221 return 0;
Steve Sakomancc175572008-10-30 21:35:26 -0700222}
223
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +0200224static void twl4030_codec_enable(struct snd_soc_codec *codec, int enable)
Steve Sakomancc175572008-10-30 21:35:26 -0700225{
Mark Brownb2c812e2010-04-14 15:35:19 +0900226 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300227 int mode;
Steve Sakomancc175572008-10-30 21:35:26 -0700228
Peter Ujfalusi73939582009-01-29 14:57:50 +0200229 if (enable == twl4030->codec_powered)
230 return;
231
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +0200232 if (enable)
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300233 mode = twl4030_codec_enable_resource(TWL4030_CODEC_RES_POWER);
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +0200234 else
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300235 mode = twl4030_codec_disable_resource(TWL4030_CODEC_RES_POWER);
Steve Sakomancc175572008-10-30 21:35:26 -0700236
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300237 if (mode >= 0) {
238 twl4030_write_reg_cache(codec, TWL4030_REG_CODEC_MODE, mode);
239 twl4030->codec_powered = enable;
240 }
Steve Sakomancc175572008-10-30 21:35:26 -0700241
242 /* REVISIT: this delay is present in TI sample drivers */
243 /* but there seems to be no TRM requirement for it */
244 udelay(10);
245}
246
247static void twl4030_init_chip(struct snd_soc_codec *codec)
248{
Peter Ujfalusi16a30fb2009-05-29 09:22:37 +0300249 u8 *cache = codec->reg_cache;
Steve Sakomancc175572008-10-30 21:35:26 -0700250 int i;
251
252 /* clear CODECPDZ prior to setting register defaults */
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +0200253 twl4030_codec_enable(codec, 0);
Steve Sakomancc175572008-10-30 21:35:26 -0700254
255 /* set all audio section registers to reasonable defaults */
256 for (i = TWL4030_REG_OPTION; i <= TWL4030_REG_MISC_SET_2; i++)
Peter Ujfalusi68d01952009-11-04 09:58:20 +0200257 if (i != TWL4030_REG_APLL_CTL)
258 twl4030_write(codec, i, cache[i]);
Steve Sakomancc175572008-10-30 21:35:26 -0700259
260}
261
Peter Ujfalusi2845fa12009-10-28 10:57:05 +0200262static void twl4030_apll_enable(struct snd_soc_codec *codec, int enable)
Peter Ujfalusi73939582009-01-29 14:57:50 +0200263{
Mark Brownb2c812e2010-04-14 15:35:19 +0900264 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +0300265 int status = -1;
Peter Ujfalusi73939582009-01-29 14:57:50 +0200266
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +0300267 if (enable) {
268 twl4030->apll_enabled++;
269 if (twl4030->apll_enabled == 1)
270 status = twl4030_codec_enable_resource(
271 TWL4030_CODEC_RES_APLL);
272 } else {
273 twl4030->apll_enabled--;
274 if (!twl4030->apll_enabled)
275 status = twl4030_codec_disable_resource(
276 TWL4030_CODEC_RES_APLL);
277 }
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300278
279 if (status >= 0)
280 twl4030_write_reg_cache(codec, TWL4030_REG_APLL_CTL, status);
Peter Ujfalusi73939582009-01-29 14:57:50 +0200281}
282
Peter Ujfalusi006f367e2009-01-27 11:29:43 +0200283static void twl4030_power_up(struct snd_soc_codec *codec)
284{
Mark Brownb2c812e2010-04-14 15:35:19 +0900285 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusi006f367e2009-01-27 11:29:43 +0200286 u8 anamicl, regmisc1, byte;
287 int i = 0;
288
Peter Ujfalusi73939582009-01-29 14:57:50 +0200289 if (twl4030->codec_powered)
290 return;
291
Peter Ujfalusi006f367e2009-01-27 11:29:43 +0200292 /* set CODECPDZ to turn on codec */
293 twl4030_codec_enable(codec, 1);
294
295 /* initiate offset cancellation */
296 anamicl = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICL);
297 twl4030_write(codec, TWL4030_REG_ANAMICL,
298 anamicl | TWL4030_CNCL_OFFSET_START);
299
300 /* wait for offset cancellation to complete */
301 do {
302 /* this takes a little while, so don't slam i2c */
303 udelay(2000);
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100304 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &byte,
Peter Ujfalusi006f367e2009-01-27 11:29:43 +0200305 TWL4030_REG_ANAMICL);
306 } while ((i++ < 100) &&
307 ((byte & TWL4030_CNCL_OFFSET_START) ==
308 TWL4030_CNCL_OFFSET_START));
309
310 /* Make sure that the reg_cache has the same value as the HW */
311 twl4030_write_reg_cache(codec, TWL4030_REG_ANAMICL, byte);
312
313 /* anti-pop when changing analog gain */
314 regmisc1 = twl4030_read_reg_cache(codec, TWL4030_REG_MISC_SET_1);
315 twl4030_write(codec, TWL4030_REG_MISC_SET_1,
316 regmisc1 | TWL4030_SMOOTH_ANAVOL_EN);
317
318 /* toggle CODECPDZ as per TRM */
319 twl4030_codec_enable(codec, 0);
320 twl4030_codec_enable(codec, 1);
321}
322
Peter Ujfalusi73939582009-01-29 14:57:50 +0200323/*
324 * Unconditional power down
325 */
Peter Ujfalusi006f367e2009-01-27 11:29:43 +0200326static void twl4030_power_down(struct snd_soc_codec *codec)
327{
328 /* power down */
329 twl4030_codec_enable(codec, 0);
330}
331
Peter Ujfalusi5e98a462008-12-09 12:35:47 +0200332/* Earpiece */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900333static const struct snd_kcontrol_new twl4030_dapm_earpiece_controls[] = {
334 SOC_DAPM_SINGLE("Voice", TWL4030_REG_EAR_CTL, 0, 1, 0),
335 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_EAR_CTL, 1, 1, 0),
336 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_EAR_CTL, 2, 1, 0),
337 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_EAR_CTL, 3, 1, 0),
338};
Peter Ujfalusi5e98a462008-12-09 12:35:47 +0200339
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +0200340/* PreDrive Left */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900341static const struct snd_kcontrol_new twl4030_dapm_predrivel_controls[] = {
342 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PREDL_CTL, 0, 1, 0),
343 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_PREDL_CTL, 1, 1, 0),
344 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PREDL_CTL, 2, 1, 0),
345 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PREDL_CTL, 3, 1, 0),
346};
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +0200347
348/* PreDrive Right */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900349static const struct snd_kcontrol_new twl4030_dapm_predriver_controls[] = {
350 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PREDR_CTL, 0, 1, 0),
351 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_PREDR_CTL, 1, 1, 0),
352 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PREDR_CTL, 2, 1, 0),
353 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PREDR_CTL, 3, 1, 0),
354};
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +0200355
Peter Ujfalusidfad21a2008-12-09 12:35:49 +0200356/* Headset Left */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900357static const struct snd_kcontrol_new twl4030_dapm_hsol_controls[] = {
358 SOC_DAPM_SINGLE("Voice", TWL4030_REG_HS_SEL, 0, 1, 0),
359 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_HS_SEL, 1, 1, 0),
360 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_HS_SEL, 2, 1, 0),
361};
Peter Ujfalusidfad21a2008-12-09 12:35:49 +0200362
363/* Headset Right */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900364static const struct snd_kcontrol_new twl4030_dapm_hsor_controls[] = {
365 SOC_DAPM_SINGLE("Voice", TWL4030_REG_HS_SEL, 3, 1, 0),
366 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_HS_SEL, 4, 1, 0),
367 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_HS_SEL, 5, 1, 0),
368};
Peter Ujfalusidfad21a2008-12-09 12:35:49 +0200369
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +0200370/* Carkit Left */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900371static const struct snd_kcontrol_new twl4030_dapm_carkitl_controls[] = {
372 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PRECKL_CTL, 0, 1, 0),
373 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_PRECKL_CTL, 1, 1, 0),
374 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PRECKL_CTL, 2, 1, 0),
375};
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +0200376
377/* Carkit Right */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900378static const struct snd_kcontrol_new twl4030_dapm_carkitr_controls[] = {
379 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PRECKR_CTL, 0, 1, 0),
380 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_PRECKR_CTL, 1, 1, 0),
381 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PRECKR_CTL, 2, 1, 0),
382};
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +0200383
Peter Ujfalusidf339802008-12-09 12:35:51 +0200384/* Handsfree Left */
385static const char *twl4030_handsfreel_texts[] =
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900386 {"Voice", "AudioL1", "AudioL2", "AudioR2"};
Peter Ujfalusidf339802008-12-09 12:35:51 +0200387
388static const struct soc_enum twl4030_handsfreel_enum =
389 SOC_ENUM_SINGLE(TWL4030_REG_HFL_CTL, 0,
390 ARRAY_SIZE(twl4030_handsfreel_texts),
391 twl4030_handsfreel_texts);
392
393static const struct snd_kcontrol_new twl4030_dapm_handsfreel_control =
394SOC_DAPM_ENUM("Route", twl4030_handsfreel_enum);
395
Peter Ujfalusi0f89bdc2009-05-25 11:12:13 +0300396/* Handsfree Left virtual mute */
397static const struct snd_kcontrol_new twl4030_dapm_handsfreelmute_control =
398 SOC_DAPM_SINGLE("Switch", TWL4030_REG_SW_SHADOW, 0, 1, 0);
399
Peter Ujfalusidf339802008-12-09 12:35:51 +0200400/* Handsfree Right */
401static const char *twl4030_handsfreer_texts[] =
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900402 {"Voice", "AudioR1", "AudioR2", "AudioL2"};
Peter Ujfalusidf339802008-12-09 12:35:51 +0200403
404static const struct soc_enum twl4030_handsfreer_enum =
405 SOC_ENUM_SINGLE(TWL4030_REG_HFR_CTL, 0,
406 ARRAY_SIZE(twl4030_handsfreer_texts),
407 twl4030_handsfreer_texts);
408
409static const struct snd_kcontrol_new twl4030_dapm_handsfreer_control =
410SOC_DAPM_ENUM("Route", twl4030_handsfreer_enum);
411
Peter Ujfalusi0f89bdc2009-05-25 11:12:13 +0300412/* Handsfree Right virtual mute */
413static const struct snd_kcontrol_new twl4030_dapm_handsfreermute_control =
414 SOC_DAPM_SINGLE("Switch", TWL4030_REG_SW_SHADOW, 1, 1, 0);
415
Peter Ujfalusi376f7832009-05-05 08:55:47 +0300416/* Vibra */
417/* Vibra audio path selection */
418static const char *twl4030_vibra_texts[] =
419 {"AudioL1", "AudioR1", "AudioL2", "AudioR2"};
420
421static const struct soc_enum twl4030_vibra_enum =
422 SOC_ENUM_SINGLE(TWL4030_REG_VIBRA_CTL, 2,
423 ARRAY_SIZE(twl4030_vibra_texts),
424 twl4030_vibra_texts);
425
426static const struct snd_kcontrol_new twl4030_dapm_vibra_control =
427SOC_DAPM_ENUM("Route", twl4030_vibra_enum);
428
429/* Vibra path selection: local vibrator (PWM) or audio driven */
430static const char *twl4030_vibrapath_texts[] =
431 {"Local vibrator", "Audio"};
432
433static const struct soc_enum twl4030_vibrapath_enum =
434 SOC_ENUM_SINGLE(TWL4030_REG_VIBRA_CTL, 4,
435 ARRAY_SIZE(twl4030_vibrapath_texts),
436 twl4030_vibrapath_texts);
437
438static const struct snd_kcontrol_new twl4030_dapm_vibrapath_control =
439SOC_DAPM_ENUM("Route", twl4030_vibrapath_enum);
440
Peter Ujfalusi276c6222008-12-31 10:08:38 +0200441/* Left analog microphone selection */
Joonyoung Shim97b80962009-05-11 20:36:08 +0900442static const struct snd_kcontrol_new twl4030_dapm_analoglmic_controls[] = {
Peter Ujfalusi90289352009-08-14 08:44:00 +0300443 SOC_DAPM_SINGLE("Main Mic Capture Switch",
444 TWL4030_REG_ANAMICL, 0, 1, 0),
445 SOC_DAPM_SINGLE("Headset Mic Capture Switch",
446 TWL4030_REG_ANAMICL, 1, 1, 0),
447 SOC_DAPM_SINGLE("AUXL Capture Switch",
448 TWL4030_REG_ANAMICL, 2, 1, 0),
449 SOC_DAPM_SINGLE("Carkit Mic Capture Switch",
450 TWL4030_REG_ANAMICL, 3, 1, 0),
Joonyoung Shim97b80962009-05-11 20:36:08 +0900451};
Peter Ujfalusi276c6222008-12-31 10:08:38 +0200452
453/* Right analog microphone selection */
Joonyoung Shim97b80962009-05-11 20:36:08 +0900454static const struct snd_kcontrol_new twl4030_dapm_analogrmic_controls[] = {
Peter Ujfalusi90289352009-08-14 08:44:00 +0300455 SOC_DAPM_SINGLE("Sub Mic Capture Switch", TWL4030_REG_ANAMICR, 0, 1, 0),
456 SOC_DAPM_SINGLE("AUXR Capture Switch", TWL4030_REG_ANAMICR, 2, 1, 0),
Joonyoung Shim97b80962009-05-11 20:36:08 +0900457};
Peter Ujfalusi276c6222008-12-31 10:08:38 +0200458
459/* TX1 L/R Analog/Digital microphone selection */
460static const char *twl4030_micpathtx1_texts[] =
461 {"Analog", "Digimic0"};
462
463static const struct soc_enum twl4030_micpathtx1_enum =
464 SOC_ENUM_SINGLE(TWL4030_REG_ADCMICSEL, 0,
465 ARRAY_SIZE(twl4030_micpathtx1_texts),
466 twl4030_micpathtx1_texts);
467
468static const struct snd_kcontrol_new twl4030_dapm_micpathtx1_control =
469SOC_DAPM_ENUM("Route", twl4030_micpathtx1_enum);
470
471/* TX2 L/R Analog/Digital microphone selection */
472static const char *twl4030_micpathtx2_texts[] =
473 {"Analog", "Digimic1"};
474
475static const struct soc_enum twl4030_micpathtx2_enum =
476 SOC_ENUM_SINGLE(TWL4030_REG_ADCMICSEL, 2,
477 ARRAY_SIZE(twl4030_micpathtx2_texts),
478 twl4030_micpathtx2_texts);
479
480static const struct snd_kcontrol_new twl4030_dapm_micpathtx2_control =
481SOC_DAPM_ENUM("Route", twl4030_micpathtx2_enum);
482
Peter Ujfalusi73939582009-01-29 14:57:50 +0200483/* Analog bypass for AudioR1 */
484static const struct snd_kcontrol_new twl4030_dapm_abypassr1_control =
485 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXR1_APGA_CTL, 2, 1, 0);
486
487/* Analog bypass for AudioL1 */
488static const struct snd_kcontrol_new twl4030_dapm_abypassl1_control =
489 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXL1_APGA_CTL, 2, 1, 0);
490
491/* Analog bypass for AudioR2 */
492static const struct snd_kcontrol_new twl4030_dapm_abypassr2_control =
493 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXR2_APGA_CTL, 2, 1, 0);
494
495/* Analog bypass for AudioL2 */
496static const struct snd_kcontrol_new twl4030_dapm_abypassl2_control =
497 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXL2_APGA_CTL, 2, 1, 0);
498
Lopez Cruz, Misaelfcd274a2009-04-30 21:47:22 -0500499/* Analog bypass for Voice */
500static const struct snd_kcontrol_new twl4030_dapm_abypassv_control =
501 SOC_DAPM_SINGLE("Switch", TWL4030_REG_VDL_APGA_CTL, 2, 1, 0);
502
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +0200503/* Digital bypass gain, 0 mutes the bypass */
504static const unsigned int twl4030_dapm_dbypass_tlv[] = {
505 TLV_DB_RANGE_HEAD(2),
506 0, 3, TLV_DB_SCALE_ITEM(-2400, 0, 1),
507 4, 7, TLV_DB_SCALE_ITEM(-1800, 600, 0),
508};
509
510/* Digital bypass left (TX1L -> RX2L) */
511static const struct snd_kcontrol_new twl4030_dapm_dbypassl_control =
512 SOC_DAPM_SINGLE_TLV("Volume",
513 TWL4030_REG_ATX2ARXPGA, 3, 7, 0,
514 twl4030_dapm_dbypass_tlv);
515
516/* Digital bypass right (TX1R -> RX2R) */
517static const struct snd_kcontrol_new twl4030_dapm_dbypassr_control =
518 SOC_DAPM_SINGLE_TLV("Volume",
519 TWL4030_REG_ATX2ARXPGA, 0, 7, 0,
520 twl4030_dapm_dbypass_tlv);
521
Lopez Cruz, Misaelee8f6892009-04-30 21:48:08 -0500522/*
523 * Voice Sidetone GAIN volume control:
524 * from -51 to -10 dB in 1 dB steps (mute instead of -51 dB)
525 */
526static DECLARE_TLV_DB_SCALE(twl4030_dapm_dbypassv_tlv, -5100, 100, 1);
527
528/* Digital bypass voice: sidetone (VUL -> VDL)*/
529static const struct snd_kcontrol_new twl4030_dapm_dbypassv_control =
530 SOC_DAPM_SINGLE_TLV("Volume",
531 TWL4030_REG_VSTPGA, 0, 0x29, 0,
532 twl4030_dapm_dbypassv_tlv);
533
Peter Ujfalusi276c6222008-12-31 10:08:38 +0200534static int micpath_event(struct snd_soc_dapm_widget *w,
535 struct snd_kcontrol *kcontrol, int event)
536{
537 struct soc_enum *e = (struct soc_enum *)w->kcontrols->private_value;
538 unsigned char adcmicsel, micbias_ctl;
539
540 adcmicsel = twl4030_read_reg_cache(w->codec, TWL4030_REG_ADCMICSEL);
541 micbias_ctl = twl4030_read_reg_cache(w->codec, TWL4030_REG_MICBIAS_CTL);
542 /* Prepare the bits for the given TX path:
543 * shift_l == 0: TX1 microphone path
544 * shift_l == 2: TX2 microphone path */
545 if (e->shift_l) {
546 /* TX2 microphone path */
547 if (adcmicsel & TWL4030_TX2IN_SEL)
548 micbias_ctl |= TWL4030_MICBIAS2_CTL; /* digimic */
549 else
550 micbias_ctl &= ~TWL4030_MICBIAS2_CTL;
551 } else {
552 /* TX1 microphone path */
553 if (adcmicsel & TWL4030_TX1IN_SEL)
554 micbias_ctl |= TWL4030_MICBIAS1_CTL; /* digimic */
555 else
556 micbias_ctl &= ~TWL4030_MICBIAS1_CTL;
557 }
558
559 twl4030_write(w->codec, TWL4030_REG_MICBIAS_CTL, micbias_ctl);
560
561 return 0;
562}
563
Peter Ujfalusi9008adf2009-08-13 15:59:34 +0300564/*
565 * Output PGA builder:
566 * Handle the muting and unmuting of the given output (turning off the
567 * amplifier associated with the output pin)
Peter Ujfalusic96907f2010-03-22 17:46:37 +0200568 * On mute bypass the reg_cache and write 0 to the register
569 * On unmute: restore the register content from the reg_cache
Peter Ujfalusi9008adf2009-08-13 15:59:34 +0300570 * Outputs handled in this way: Earpiece, PreDrivL/R, CarkitL/R
571 */
572#define TWL4030_OUTPUT_PGA(pin_name, reg, mask) \
573static int pin_name##pga_event(struct snd_soc_dapm_widget *w, \
574 struct snd_kcontrol *kcontrol, int event) \
575{ \
Mark Brownb2c812e2010-04-14 15:35:19 +0900576 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(w->codec); \
Peter Ujfalusi9008adf2009-08-13 15:59:34 +0300577 \
578 switch (event) { \
579 case SND_SOC_DAPM_POST_PMU: \
Peter Ujfalusic96907f2010-03-22 17:46:37 +0200580 twl4030->pin_name##_enabled = 1; \
Peter Ujfalusi9008adf2009-08-13 15:59:34 +0300581 twl4030_write(w->codec, reg, \
582 twl4030_read_reg_cache(w->codec, reg)); \
583 break; \
584 case SND_SOC_DAPM_POST_PMD: \
Peter Ujfalusic96907f2010-03-22 17:46:37 +0200585 twl4030->pin_name##_enabled = 0; \
586 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, \
587 0, reg); \
Peter Ujfalusi9008adf2009-08-13 15:59:34 +0300588 break; \
589 } \
590 return 0; \
591}
592
593TWL4030_OUTPUT_PGA(earpiece, TWL4030_REG_EAR_CTL, TWL4030_EAR_GAIN);
594TWL4030_OUTPUT_PGA(predrivel, TWL4030_REG_PREDL_CTL, TWL4030_PREDL_GAIN);
595TWL4030_OUTPUT_PGA(predriver, TWL4030_REG_PREDR_CTL, TWL4030_PREDR_GAIN);
596TWL4030_OUTPUT_PGA(carkitl, TWL4030_REG_PRECKL_CTL, TWL4030_PRECKL_GAIN);
597TWL4030_OUTPUT_PGA(carkitr, TWL4030_REG_PRECKR_CTL, TWL4030_PRECKR_GAIN);
598
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300599static void handsfree_ramp(struct snd_soc_codec *codec, int reg, int ramp)
Stanley.Miao49d92c72008-12-11 23:28:10 +0800600{
Stanley.Miao49d92c72008-12-11 23:28:10 +0800601 unsigned char hs_ctl;
602
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300603 hs_ctl = twl4030_read_reg_cache(codec, reg);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800604
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300605 if (ramp) {
606 /* HF ramp-up */
607 hs_ctl |= TWL4030_HF_CTL_REF_EN;
608 twl4030_write(codec, reg, hs_ctl);
609 udelay(10);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800610 hs_ctl |= TWL4030_HF_CTL_RAMP_EN;
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300611 twl4030_write(codec, reg, hs_ctl);
612 udelay(40);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800613 hs_ctl |= TWL4030_HF_CTL_LOOP_EN;
Stanley.Miao49d92c72008-12-11 23:28:10 +0800614 hs_ctl |= TWL4030_HF_CTL_HB_EN;
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300615 twl4030_write(codec, reg, hs_ctl);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800616 } else {
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300617 /* HF ramp-down */
618 hs_ctl &= ~TWL4030_HF_CTL_LOOP_EN;
619 hs_ctl &= ~TWL4030_HF_CTL_HB_EN;
620 twl4030_write(codec, reg, hs_ctl);
621 hs_ctl &= ~TWL4030_HF_CTL_RAMP_EN;
622 twl4030_write(codec, reg, hs_ctl);
623 udelay(40);
624 hs_ctl &= ~TWL4030_HF_CTL_REF_EN;
625 twl4030_write(codec, reg, hs_ctl);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800626 }
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300627}
Stanley.Miao49d92c72008-12-11 23:28:10 +0800628
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300629static int handsfreelpga_event(struct snd_soc_dapm_widget *w,
630 struct snd_kcontrol *kcontrol, int event)
631{
632 switch (event) {
633 case SND_SOC_DAPM_POST_PMU:
634 handsfree_ramp(w->codec, TWL4030_REG_HFL_CTL, 1);
635 break;
636 case SND_SOC_DAPM_POST_PMD:
637 handsfree_ramp(w->codec, TWL4030_REG_HFL_CTL, 0);
638 break;
639 }
640 return 0;
641}
642
643static int handsfreerpga_event(struct snd_soc_dapm_widget *w,
644 struct snd_kcontrol *kcontrol, int event)
645{
646 switch (event) {
647 case SND_SOC_DAPM_POST_PMU:
648 handsfree_ramp(w->codec, TWL4030_REG_HFR_CTL, 1);
649 break;
650 case SND_SOC_DAPM_POST_PMD:
651 handsfree_ramp(w->codec, TWL4030_REG_HFR_CTL, 0);
652 break;
653 }
Stanley.Miao49d92c72008-12-11 23:28:10 +0800654 return 0;
655}
656
Jari Vanhala86139a12009-10-29 11:58:09 +0200657static int vibramux_event(struct snd_soc_dapm_widget *w,
658 struct snd_kcontrol *kcontrol, int event)
659{
660 twl4030_write(w->codec, TWL4030_REG_VIBRA_SET, 0xff);
661 return 0;
662}
663
Peter Ujfalusi7729cf72009-10-29 11:58:10 +0200664static int apll_event(struct snd_soc_dapm_widget *w,
665 struct snd_kcontrol *kcontrol, int event)
666{
667 switch (event) {
668 case SND_SOC_DAPM_PRE_PMU:
669 twl4030_apll_enable(w->codec, 1);
670 break;
671 case SND_SOC_DAPM_POST_PMD:
672 twl4030_apll_enable(w->codec, 0);
673 break;
674 }
675 return 0;
676}
677
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +0300678static int aif_event(struct snd_soc_dapm_widget *w,
679 struct snd_kcontrol *kcontrol, int event)
680{
681 u8 audio_if;
682
683 audio_if = twl4030_read_reg_cache(w->codec, TWL4030_REG_AUDIO_IF);
684 switch (event) {
685 case SND_SOC_DAPM_PRE_PMU:
686 /* Enable AIF */
687 /* enable the PLL before we use it to clock the DAI */
688 twl4030_apll_enable(w->codec, 1);
689
690 twl4030_write(w->codec, TWL4030_REG_AUDIO_IF,
691 audio_if | TWL4030_AIF_EN);
692 break;
693 case SND_SOC_DAPM_POST_PMD:
694 /* disable the DAI before we stop it's source PLL */
695 twl4030_write(w->codec, TWL4030_REG_AUDIO_IF,
696 audio_if & ~TWL4030_AIF_EN);
697 twl4030_apll_enable(w->codec, 0);
698 break;
699 }
700 return 0;
701}
702
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300703static void headset_ramp(struct snd_soc_codec *codec, int ramp)
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200704{
Candelaria Villareal, Jorge4e49ffd2009-07-01 19:17:43 -0500705 struct snd_soc_device *socdev = codec->socdev;
706 struct twl4030_setup_data *setup = socdev->codec_data;
707
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200708 unsigned char hs_gain, hs_pop;
Mark Brownb2c812e2010-04-14 15:35:19 +0900709 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300710 /* Base values for ramp delay calculation: 2^19 - 2^26 */
711 unsigned int ramp_base[] = {524288, 1048576, 2097152, 4194304,
712 8388608, 16777216, 33554432, 67108864};
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200713
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300714 hs_gain = twl4030_read_reg_cache(codec, TWL4030_REG_HS_GAIN_SET);
715 hs_pop = twl4030_read_reg_cache(codec, TWL4030_REG_HS_POPN_SET);
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200716
Candelaria Villareal, Jorge4e49ffd2009-07-01 19:17:43 -0500717 /* Enable external mute control, this dramatically reduces
718 * the pop-noise */
719 if (setup && setup->hs_extmute) {
720 if (setup->set_hs_extmute) {
721 setup->set_hs_extmute(1);
722 } else {
723 hs_pop |= TWL4030_EXTMUTE;
724 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
725 }
726 }
727
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300728 if (ramp) {
729 /* Headset ramp-up according to the TRM */
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200730 hs_pop |= TWL4030_VMID_EN;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300731 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
Peter Ujfalusic96907f2010-03-22 17:46:37 +0200732 /* Actually write to the register */
733 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
734 hs_gain,
735 TWL4030_REG_HS_GAIN_SET);
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200736 hs_pop |= TWL4030_RAMP_EN;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300737 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
Candelaria Villareal, Jorge4e49ffd2009-07-01 19:17:43 -0500738 /* Wait ramp delay time + 1, so the VMID can settle */
739 mdelay((ramp_base[(hs_pop & TWL4030_RAMP_DELAY) >> 2] /
740 twl4030->sysclk) + 1);
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300741 } else {
742 /* Headset ramp-down _not_ according to
743 * the TRM, but in a way that it is working */
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200744 hs_pop &= ~TWL4030_RAMP_EN;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300745 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
746 /* Wait ramp delay time + 1, so the VMID can settle */
747 mdelay((ramp_base[(hs_pop & TWL4030_RAMP_DELAY) >> 2] /
748 twl4030->sysclk) + 1);
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200749 /* Bypass the reg_cache to mute the headset */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100750 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200751 hs_gain & (~0x0f),
752 TWL4030_REG_HS_GAIN_SET);
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300753
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200754 hs_pop &= ~TWL4030_VMID_EN;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300755 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
756 }
Candelaria Villareal, Jorge4e49ffd2009-07-01 19:17:43 -0500757
758 /* Disable external mute */
759 if (setup && setup->hs_extmute) {
760 if (setup->set_hs_extmute) {
761 setup->set_hs_extmute(0);
762 } else {
763 hs_pop &= ~TWL4030_EXTMUTE;
764 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
765 }
766 }
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300767}
768
769static int headsetlpga_event(struct snd_soc_dapm_widget *w,
770 struct snd_kcontrol *kcontrol, int event)
771{
Mark Brownb2c812e2010-04-14 15:35:19 +0900772 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(w->codec);
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300773
774 switch (event) {
775 case SND_SOC_DAPM_POST_PMU:
776 /* Do the ramp-up only once */
777 if (!twl4030->hsr_enabled)
778 headset_ramp(w->codec, 1);
779
780 twl4030->hsl_enabled = 1;
781 break;
782 case SND_SOC_DAPM_POST_PMD:
783 /* Do the ramp-down only if both headsetL/R is disabled */
784 if (!twl4030->hsr_enabled)
785 headset_ramp(w->codec, 0);
786
787 twl4030->hsl_enabled = 0;
788 break;
789 }
790 return 0;
791}
792
793static int headsetrpga_event(struct snd_soc_dapm_widget *w,
794 struct snd_kcontrol *kcontrol, int event)
795{
Mark Brownb2c812e2010-04-14 15:35:19 +0900796 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(w->codec);
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300797
798 switch (event) {
799 case SND_SOC_DAPM_POST_PMU:
800 /* Do the ramp-up only once */
801 if (!twl4030->hsl_enabled)
802 headset_ramp(w->codec, 1);
803
804 twl4030->hsr_enabled = 1;
805 break;
806 case SND_SOC_DAPM_POST_PMD:
807 /* Do the ramp-down only if both headsetL/R is disabled */
808 if (!twl4030->hsl_enabled)
809 headset_ramp(w->codec, 0);
810
811 twl4030->hsr_enabled = 0;
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200812 break;
813 }
814 return 0;
815}
816
Peter Ujfalusic10b82c2008-11-24 13:49:35 +0200817/*
Peter Ujfalusib0bd53a2008-11-24 13:49:38 +0200818 * Some of the gain controls in TWL (mostly those which are associated with
819 * the outputs) are implemented in an interesting way:
820 * 0x0 : Power down (mute)
821 * 0x1 : 6dB
822 * 0x2 : 0 dB
823 * 0x3 : -6 dB
824 * Inverting not going to help with these.
825 * Custom volsw and volsw_2r get/put functions to handle these gain bits.
826 */
827#define SOC_DOUBLE_TLV_TWL4030(xname, xreg, shift_left, shift_right, xmax,\
828 xinvert, tlv_array) \
829{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
830 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
831 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
832 .tlv.p = (tlv_array), \
833 .info = snd_soc_info_volsw, \
834 .get = snd_soc_get_volsw_twl4030, \
835 .put = snd_soc_put_volsw_twl4030, \
836 .private_value = (unsigned long)&(struct soc_mixer_control) \
837 {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
838 .max = xmax, .invert = xinvert} }
839#define SOC_DOUBLE_R_TLV_TWL4030(xname, reg_left, reg_right, xshift, xmax,\
840 xinvert, tlv_array) \
841{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
842 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
843 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
844 .tlv.p = (tlv_array), \
845 .info = snd_soc_info_volsw_2r, \
846 .get = snd_soc_get_volsw_r2_twl4030,\
847 .put = snd_soc_put_volsw_r2_twl4030, \
848 .private_value = (unsigned long)&(struct soc_mixer_control) \
849 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
Mark Brown64089b82008-12-08 19:17:58 +0000850 .rshift = xshift, .max = xmax, .invert = xinvert} }
Peter Ujfalusib0bd53a2008-11-24 13:49:38 +0200851#define SOC_SINGLE_TLV_TWL4030(xname, xreg, xshift, xmax, xinvert, tlv_array) \
852 SOC_DOUBLE_TLV_TWL4030(xname, xreg, xshift, xshift, xmax, \
853 xinvert, tlv_array)
854
855static int snd_soc_get_volsw_twl4030(struct snd_kcontrol *kcontrol,
856 struct snd_ctl_elem_value *ucontrol)
857{
858 struct soc_mixer_control *mc =
859 (struct soc_mixer_control *)kcontrol->private_value;
860 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
861 unsigned int reg = mc->reg;
862 unsigned int shift = mc->shift;
863 unsigned int rshift = mc->rshift;
864 int max = mc->max;
865 int mask = (1 << fls(max)) - 1;
866
867 ucontrol->value.integer.value[0] =
868 (snd_soc_read(codec, reg) >> shift) & mask;
869 if (ucontrol->value.integer.value[0])
870 ucontrol->value.integer.value[0] =
871 max + 1 - ucontrol->value.integer.value[0];
872
873 if (shift != rshift) {
874 ucontrol->value.integer.value[1] =
875 (snd_soc_read(codec, reg) >> rshift) & mask;
876 if (ucontrol->value.integer.value[1])
877 ucontrol->value.integer.value[1] =
878 max + 1 - ucontrol->value.integer.value[1];
879 }
880
881 return 0;
882}
883
884static int snd_soc_put_volsw_twl4030(struct snd_kcontrol *kcontrol,
885 struct snd_ctl_elem_value *ucontrol)
886{
887 struct soc_mixer_control *mc =
888 (struct soc_mixer_control *)kcontrol->private_value;
889 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
890 unsigned int reg = mc->reg;
891 unsigned int shift = mc->shift;
892 unsigned int rshift = mc->rshift;
893 int max = mc->max;
894 int mask = (1 << fls(max)) - 1;
895 unsigned short val, val2, val_mask;
896
897 val = (ucontrol->value.integer.value[0] & mask);
898
899 val_mask = mask << shift;
900 if (val)
901 val = max + 1 - val;
902 val = val << shift;
903 if (shift != rshift) {
904 val2 = (ucontrol->value.integer.value[1] & mask);
905 val_mask |= mask << rshift;
906 if (val2)
907 val2 = max + 1 - val2;
908 val |= val2 << rshift;
909 }
910 return snd_soc_update_bits(codec, reg, val_mask, val);
911}
912
913static int snd_soc_get_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
914 struct snd_ctl_elem_value *ucontrol)
915{
916 struct soc_mixer_control *mc =
917 (struct soc_mixer_control *)kcontrol->private_value;
918 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
919 unsigned int reg = mc->reg;
920 unsigned int reg2 = mc->rreg;
921 unsigned int shift = mc->shift;
922 int max = mc->max;
923 int mask = (1<<fls(max))-1;
924
925 ucontrol->value.integer.value[0] =
926 (snd_soc_read(codec, reg) >> shift) & mask;
927 ucontrol->value.integer.value[1] =
928 (snd_soc_read(codec, reg2) >> shift) & mask;
929
930 if (ucontrol->value.integer.value[0])
931 ucontrol->value.integer.value[0] =
932 max + 1 - ucontrol->value.integer.value[0];
933 if (ucontrol->value.integer.value[1])
934 ucontrol->value.integer.value[1] =
935 max + 1 - ucontrol->value.integer.value[1];
936
937 return 0;
938}
939
940static int snd_soc_put_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
941 struct snd_ctl_elem_value *ucontrol)
942{
943 struct soc_mixer_control *mc =
944 (struct soc_mixer_control *)kcontrol->private_value;
945 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
946 unsigned int reg = mc->reg;
947 unsigned int reg2 = mc->rreg;
948 unsigned int shift = mc->shift;
949 int max = mc->max;
950 int mask = (1 << fls(max)) - 1;
951 int err;
952 unsigned short val, val2, val_mask;
953
954 val_mask = mask << shift;
955 val = (ucontrol->value.integer.value[0] & mask);
956 val2 = (ucontrol->value.integer.value[1] & mask);
957
958 if (val)
959 val = max + 1 - val;
960 if (val2)
961 val2 = max + 1 - val2;
962
963 val = val << shift;
964 val2 = val2 << shift;
965
966 err = snd_soc_update_bits(codec, reg, val_mask, val);
967 if (err < 0)
968 return err;
969
970 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
971 return err;
972}
973
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -0500974/* Codec operation modes */
975static const char *twl4030_op_modes_texts[] = {
976 "Option 2 (voice/audio)", "Option 1 (audio)"
977};
978
979static const struct soc_enum twl4030_op_modes_enum =
980 SOC_ENUM_SINGLE(TWL4030_REG_CODEC_MODE, 0,
981 ARRAY_SIZE(twl4030_op_modes_texts),
982 twl4030_op_modes_texts);
983
Mark Brown423c2382009-06-20 13:54:02 +0100984static int snd_soc_put_twl4030_opmode_enum_double(struct snd_kcontrol *kcontrol,
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -0500985 struct snd_ctl_elem_value *ucontrol)
986{
987 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +0900988 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -0500989 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
990 unsigned short val;
991 unsigned short mask, bitmask;
992
993 if (twl4030->configured) {
994 printk(KERN_ERR "twl4030 operation mode cannot be "
995 "changed on-the-fly\n");
996 return -EBUSY;
997 }
998
999 for (bitmask = 1; bitmask < e->max; bitmask <<= 1)
1000 ;
1001 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1002 return -EINVAL;
1003
1004 val = ucontrol->value.enumerated.item[0] << e->shift_l;
1005 mask = (bitmask - 1) << e->shift_l;
1006 if (e->shift_l != e->shift_r) {
1007 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1008 return -EINVAL;
1009 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
1010 mask |= (bitmask - 1) << e->shift_r;
1011 }
1012
1013 return snd_soc_update_bits(codec, e->reg, mask, val);
1014}
1015
Peter Ujfalusib0bd53a2008-11-24 13:49:38 +02001016/*
Peter Ujfalusic10b82c2008-11-24 13:49:35 +02001017 * FGAIN volume control:
1018 * from -62 to 0 dB in 1 dB steps (mute instead of -63 dB)
1019 */
Peter Ujfalusid889a722008-12-01 10:03:46 +02001020static DECLARE_TLV_DB_SCALE(digital_fine_tlv, -6300, 100, 1);
Peter Ujfalusic10b82c2008-11-24 13:49:35 +02001021
Peter Ujfalusi0d33ea02008-11-24 13:49:36 +02001022/*
1023 * CGAIN volume control:
1024 * 0 dB to 12 dB in 6 dB steps
1025 * value 2 and 3 means 12 dB
1026 */
Peter Ujfalusid889a722008-12-01 10:03:46 +02001027static DECLARE_TLV_DB_SCALE(digital_coarse_tlv, 0, 600, 0);
1028
1029/*
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001030 * Voice Downlink GAIN volume control:
1031 * from -37 to 12 dB in 1 dB steps (mute instead of -37 dB)
1032 */
1033static DECLARE_TLV_DB_SCALE(digital_voice_downlink_tlv, -3700, 100, 1);
1034
1035/*
Peter Ujfalusid889a722008-12-01 10:03:46 +02001036 * Analog playback gain
1037 * -24 dB to 12 dB in 2 dB steps
1038 */
1039static DECLARE_TLV_DB_SCALE(analog_tlv, -2400, 200, 0);
Peter Ujfalusi0d33ea02008-11-24 13:49:36 +02001040
Peter Ujfalusi381a22b2008-12-01 10:03:45 +02001041/*
Peter Ujfalusi42902392008-12-01 10:03:47 +02001042 * Gain controls tied to outputs
1043 * -6 dB to 6 dB in 6 dB steps (mute instead of -12)
1044 */
1045static DECLARE_TLV_DB_SCALE(output_tvl, -1200, 600, 1);
1046
1047/*
Joonyoung Shim18cc8d82009-04-28 18:18:05 +09001048 * Gain control for earpiece amplifier
1049 * 0 dB to 12 dB in 6 dB steps (mute instead of -6)
1050 */
1051static DECLARE_TLV_DB_SCALE(output_ear_tvl, -600, 600, 1);
1052
1053/*
Peter Ujfalusi381a22b2008-12-01 10:03:45 +02001054 * Capture gain after the ADCs
1055 * from 0 dB to 31 dB in 1 dB steps
1056 */
1057static DECLARE_TLV_DB_SCALE(digital_capture_tlv, 0, 100, 0);
1058
Grazvydas Ignotas5920b452008-12-02 20:48:58 +02001059/*
1060 * Gain control for input amplifiers
1061 * 0 dB to 30 dB in 6 dB steps
1062 */
1063static DECLARE_TLV_DB_SCALE(input_gain_tlv, 0, 600, 0);
1064
Lopez Cruz, Misael328d0a12009-06-22 10:51:52 -05001065/* AVADC clock priority */
1066static const char *twl4030_avadc_clk_priority_texts[] = {
1067 "Voice high priority", "HiFi high priority"
1068};
1069
1070static const struct soc_enum twl4030_avadc_clk_priority_enum =
1071 SOC_ENUM_SINGLE(TWL4030_REG_AVADC_CTL, 2,
1072 ARRAY_SIZE(twl4030_avadc_clk_priority_texts),
1073 twl4030_avadc_clk_priority_texts);
1074
Peter Ujfalusi89492be2009-03-05 12:48:49 +02001075static const char *twl4030_rampdelay_texts[] = {
1076 "27/20/14 ms", "55/40/27 ms", "109/81/55 ms", "218/161/109 ms",
1077 "437/323/218 ms", "874/645/437 ms", "1748/1291/874 ms",
1078 "3495/2581/1748 ms"
1079};
1080
1081static const struct soc_enum twl4030_rampdelay_enum =
1082 SOC_ENUM_SINGLE(TWL4030_REG_HS_POPN_SET, 2,
1083 ARRAY_SIZE(twl4030_rampdelay_texts),
1084 twl4030_rampdelay_texts);
1085
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001086/* Vibra H-bridge direction mode */
1087static const char *twl4030_vibradirmode_texts[] = {
1088 "Vibra H-bridge direction", "Audio data MSB",
1089};
1090
1091static const struct soc_enum twl4030_vibradirmode_enum =
1092 SOC_ENUM_SINGLE(TWL4030_REG_VIBRA_CTL, 5,
1093 ARRAY_SIZE(twl4030_vibradirmode_texts),
1094 twl4030_vibradirmode_texts);
1095
1096/* Vibra H-bridge direction */
1097static const char *twl4030_vibradir_texts[] = {
1098 "Positive polarity", "Negative polarity",
1099};
1100
1101static const struct soc_enum twl4030_vibradir_enum =
1102 SOC_ENUM_SINGLE(TWL4030_REG_VIBRA_CTL, 1,
1103 ARRAY_SIZE(twl4030_vibradir_texts),
1104 twl4030_vibradir_texts);
1105
Peter Ujfalusi36aeff62010-05-12 10:35:36 +03001106/* Digimic Left and right swapping */
1107static const char *twl4030_digimicswap_texts[] = {
1108 "Not swapped", "Swapped",
1109};
1110
1111static const struct soc_enum twl4030_digimicswap_enum =
1112 SOC_ENUM_SINGLE(TWL4030_REG_MISC_SET_1, 0,
1113 ARRAY_SIZE(twl4030_digimicswap_texts),
1114 twl4030_digimicswap_texts);
1115
Steve Sakomancc175572008-10-30 21:35:26 -07001116static const struct snd_kcontrol_new twl4030_snd_controls[] = {
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -05001117 /* Codec operation mode control */
1118 SOC_ENUM_EXT("Codec Operation Mode", twl4030_op_modes_enum,
1119 snd_soc_get_enum_double,
1120 snd_soc_put_twl4030_opmode_enum_double),
1121
Peter Ujfalusid889a722008-12-01 10:03:46 +02001122 /* Common playback gain controls */
1123 SOC_DOUBLE_R_TLV("DAC1 Digital Fine Playback Volume",
1124 TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
1125 0, 0x3f, 0, digital_fine_tlv),
1126 SOC_DOUBLE_R_TLV("DAC2 Digital Fine Playback Volume",
1127 TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
1128 0, 0x3f, 0, digital_fine_tlv),
1129
1130 SOC_DOUBLE_R_TLV("DAC1 Digital Coarse Playback Volume",
1131 TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
1132 6, 0x2, 0, digital_coarse_tlv),
1133 SOC_DOUBLE_R_TLV("DAC2 Digital Coarse Playback Volume",
1134 TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
1135 6, 0x2, 0, digital_coarse_tlv),
1136
1137 SOC_DOUBLE_R_TLV("DAC1 Analog Playback Volume",
1138 TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
1139 3, 0x12, 1, analog_tlv),
1140 SOC_DOUBLE_R_TLV("DAC2 Analog Playback Volume",
1141 TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
1142 3, 0x12, 1, analog_tlv),
Peter Ujfalusi44c55872008-12-09 08:45:44 +02001143 SOC_DOUBLE_R("DAC1 Analog Playback Switch",
1144 TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
1145 1, 1, 0),
1146 SOC_DOUBLE_R("DAC2 Analog Playback Switch",
1147 TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
1148 1, 1, 0),
Peter Ujfalusi381a22b2008-12-01 10:03:45 +02001149
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001150 /* Common voice downlink gain controls */
1151 SOC_SINGLE_TLV("DAC Voice Digital Downlink Volume",
1152 TWL4030_REG_VRXPGA, 0, 0x31, 0, digital_voice_downlink_tlv),
1153
1154 SOC_SINGLE_TLV("DAC Voice Analog Downlink Volume",
1155 TWL4030_REG_VDL_APGA_CTL, 3, 0x12, 1, analog_tlv),
1156
1157 SOC_SINGLE("DAC Voice Analog Downlink Switch",
1158 TWL4030_REG_VDL_APGA_CTL, 1, 1, 0),
1159
Peter Ujfalusi42902392008-12-01 10:03:47 +02001160 /* Separate output gain controls */
1161 SOC_DOUBLE_R_TLV_TWL4030("PreDriv Playback Volume",
1162 TWL4030_REG_PREDL_CTL, TWL4030_REG_PREDR_CTL,
1163 4, 3, 0, output_tvl),
1164
1165 SOC_DOUBLE_TLV_TWL4030("Headset Playback Volume",
1166 TWL4030_REG_HS_GAIN_SET, 0, 2, 3, 0, output_tvl),
1167
1168 SOC_DOUBLE_R_TLV_TWL4030("Carkit Playback Volume",
1169 TWL4030_REG_PRECKL_CTL, TWL4030_REG_PRECKR_CTL,
1170 4, 3, 0, output_tvl),
1171
1172 SOC_SINGLE_TLV_TWL4030("Earpiece Playback Volume",
Joonyoung Shim18cc8d82009-04-28 18:18:05 +09001173 TWL4030_REG_EAR_CTL, 4, 3, 0, output_ear_tvl),
Peter Ujfalusi42902392008-12-01 10:03:47 +02001174
Peter Ujfalusi381a22b2008-12-01 10:03:45 +02001175 /* Common capture gain controls */
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001176 SOC_DOUBLE_R_TLV("TX1 Digital Capture Volume",
Peter Ujfalusi381a22b2008-12-01 10:03:45 +02001177 TWL4030_REG_ATXL1PGA, TWL4030_REG_ATXR1PGA,
1178 0, 0x1f, 0, digital_capture_tlv),
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001179 SOC_DOUBLE_R_TLV("TX2 Digital Capture Volume",
1180 TWL4030_REG_AVTXL2PGA, TWL4030_REG_AVTXR2PGA,
1181 0, 0x1f, 0, digital_capture_tlv),
Grazvydas Ignotas5920b452008-12-02 20:48:58 +02001182
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001183 SOC_DOUBLE_TLV("Analog Capture Volume", TWL4030_REG_ANAMIC_GAIN,
Grazvydas Ignotas5920b452008-12-02 20:48:58 +02001184 0, 3, 5, 0, input_gain_tlv),
Peter Ujfalusi89492be2009-03-05 12:48:49 +02001185
Lopez Cruz, Misael328d0a12009-06-22 10:51:52 -05001186 SOC_ENUM("AVADC Clock Priority", twl4030_avadc_clk_priority_enum),
1187
Peter Ujfalusi89492be2009-03-05 12:48:49 +02001188 SOC_ENUM("HS ramp delay", twl4030_rampdelay_enum),
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001189
1190 SOC_ENUM("Vibra H-bridge mode", twl4030_vibradirmode_enum),
1191 SOC_ENUM("Vibra H-bridge direction", twl4030_vibradir_enum),
Peter Ujfalusi36aeff62010-05-12 10:35:36 +03001192
1193 SOC_ENUM("Digimic LR Swap", twl4030_digimicswap_enum),
Steve Sakomancc175572008-10-30 21:35:26 -07001194};
1195
Steve Sakomancc175572008-10-30 21:35:26 -07001196static const struct snd_soc_dapm_widget twl4030_dapm_widgets[] = {
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001197 /* Left channel inputs */
1198 SND_SOC_DAPM_INPUT("MAINMIC"),
1199 SND_SOC_DAPM_INPUT("HSMIC"),
1200 SND_SOC_DAPM_INPUT("AUXL"),
1201 SND_SOC_DAPM_INPUT("CARKITMIC"),
1202 /* Right channel inputs */
1203 SND_SOC_DAPM_INPUT("SUBMIC"),
1204 SND_SOC_DAPM_INPUT("AUXR"),
1205 /* Digital microphones (Stereo) */
1206 SND_SOC_DAPM_INPUT("DIGIMIC0"),
1207 SND_SOC_DAPM_INPUT("DIGIMIC1"),
Steve Sakomancc175572008-10-30 21:35:26 -07001208
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001209 /* Outputs */
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001210 SND_SOC_DAPM_OUTPUT("EARPIECE"),
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +02001211 SND_SOC_DAPM_OUTPUT("PREDRIVEL"),
1212 SND_SOC_DAPM_OUTPUT("PREDRIVER"),
Peter Ujfalusidfad21a2008-12-09 12:35:49 +02001213 SND_SOC_DAPM_OUTPUT("HSOL"),
1214 SND_SOC_DAPM_OUTPUT("HSOR"),
Peter Ujfalusi6a1bee42008-12-10 12:51:46 +02001215 SND_SOC_DAPM_OUTPUT("CARKITL"),
1216 SND_SOC_DAPM_OUTPUT("CARKITR"),
Peter Ujfalusidf339802008-12-09 12:35:51 +02001217 SND_SOC_DAPM_OUTPUT("HFL"),
1218 SND_SOC_DAPM_OUTPUT("HFR"),
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001219 SND_SOC_DAPM_OUTPUT("VIBRA"),
Steve Sakomancc175572008-10-30 21:35:26 -07001220
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +03001221 /* AIF and APLL clocks for running DAIs (including loopback) */
1222 SND_SOC_DAPM_OUTPUT("Virtual HiFi OUT"),
1223 SND_SOC_DAPM_INPUT("Virtual HiFi IN"),
1224 SND_SOC_DAPM_OUTPUT("Virtual Voice OUT"),
1225
Peter Ujfalusi53b50472008-12-09 08:45:43 +02001226 /* DACs */
Peter Ujfalusib4852b72009-05-22 15:12:15 +03001227 SND_SOC_DAPM_DAC("DAC Right1", "Right Front HiFi Playback",
Peter Ujfalusi73939582009-01-29 14:57:50 +02001228 SND_SOC_NOPM, 0, 0),
Peter Ujfalusib4852b72009-05-22 15:12:15 +03001229 SND_SOC_DAPM_DAC("DAC Left1", "Left Front HiFi Playback",
Peter Ujfalusi73939582009-01-29 14:57:50 +02001230 SND_SOC_NOPM, 0, 0),
Peter Ujfalusib4852b72009-05-22 15:12:15 +03001231 SND_SOC_DAPM_DAC("DAC Right2", "Right Rear HiFi Playback",
Peter Ujfalusi73939582009-01-29 14:57:50 +02001232 SND_SOC_NOPM, 0, 0),
Peter Ujfalusib4852b72009-05-22 15:12:15 +03001233 SND_SOC_DAPM_DAC("DAC Left2", "Left Rear HiFi Playback",
Peter Ujfalusi73939582009-01-29 14:57:50 +02001234 SND_SOC_NOPM, 0, 0),
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001235 SND_SOC_DAPM_DAC("DAC Voice", "Voice Playback",
Lopez Cruz, Misaelfcd274a2009-04-30 21:47:22 -05001236 SND_SOC_NOPM, 0, 0),
Steve Sakomancc175572008-10-30 21:35:26 -07001237
Peter Ujfalusi73939582009-01-29 14:57:50 +02001238 /* Analog bypasses */
Peter Ujfalusi78e08e22009-10-28 10:57:04 +02001239 SND_SOC_DAPM_SWITCH("Right1 Analog Loopback", SND_SOC_NOPM, 0, 0,
1240 &twl4030_dapm_abypassr1_control),
1241 SND_SOC_DAPM_SWITCH("Left1 Analog Loopback", SND_SOC_NOPM, 0, 0,
1242 &twl4030_dapm_abypassl1_control),
1243 SND_SOC_DAPM_SWITCH("Right2 Analog Loopback", SND_SOC_NOPM, 0, 0,
1244 &twl4030_dapm_abypassr2_control),
1245 SND_SOC_DAPM_SWITCH("Left2 Analog Loopback", SND_SOC_NOPM, 0, 0,
1246 &twl4030_dapm_abypassl2_control),
1247 SND_SOC_DAPM_SWITCH("Voice Analog Loopback", SND_SOC_NOPM, 0, 0,
1248 &twl4030_dapm_abypassv_control),
1249
1250 /* Master analog loopback switch */
1251 SND_SOC_DAPM_SUPPLY("FM Loop Enable", TWL4030_REG_MISC_SET_1, 5, 0,
1252 NULL, 0),
Peter Ujfalusi73939582009-01-29 14:57:50 +02001253
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001254 /* Digital bypasses */
Peter Ujfalusi78e08e22009-10-28 10:57:04 +02001255 SND_SOC_DAPM_SWITCH("Left Digital Loopback", SND_SOC_NOPM, 0, 0,
1256 &twl4030_dapm_dbypassl_control),
1257 SND_SOC_DAPM_SWITCH("Right Digital Loopback", SND_SOC_NOPM, 0, 0,
1258 &twl4030_dapm_dbypassr_control),
1259 SND_SOC_DAPM_SWITCH("Voice Digital Loopback", SND_SOC_NOPM, 0, 0,
1260 &twl4030_dapm_dbypassv_control),
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001261
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001262 /* Digital mixers, power control for the physical DACs */
1263 SND_SOC_DAPM_MIXER("Digital R1 Playback Mixer",
1264 TWL4030_REG_AVDAC_CTL, 0, 0, NULL, 0),
1265 SND_SOC_DAPM_MIXER("Digital L1 Playback Mixer",
1266 TWL4030_REG_AVDAC_CTL, 1, 0, NULL, 0),
1267 SND_SOC_DAPM_MIXER("Digital R2 Playback Mixer",
1268 TWL4030_REG_AVDAC_CTL, 2, 0, NULL, 0),
1269 SND_SOC_DAPM_MIXER("Digital L2 Playback Mixer",
1270 TWL4030_REG_AVDAC_CTL, 3, 0, NULL, 0),
1271 SND_SOC_DAPM_MIXER("Digital Voice Playback Mixer",
1272 TWL4030_REG_AVDAC_CTL, 4, 0, NULL, 0),
1273
1274 /* Analog mixers, power control for the physical PGAs */
1275 SND_SOC_DAPM_MIXER("Analog R1 Playback Mixer",
1276 TWL4030_REG_ARXR1_APGA_CTL, 0, 0, NULL, 0),
1277 SND_SOC_DAPM_MIXER("Analog L1 Playback Mixer",
1278 TWL4030_REG_ARXL1_APGA_CTL, 0, 0, NULL, 0),
1279 SND_SOC_DAPM_MIXER("Analog R2 Playback Mixer",
1280 TWL4030_REG_ARXR2_APGA_CTL, 0, 0, NULL, 0),
1281 SND_SOC_DAPM_MIXER("Analog L2 Playback Mixer",
1282 TWL4030_REG_ARXL2_APGA_CTL, 0, 0, NULL, 0),
1283 SND_SOC_DAPM_MIXER("Analog Voice Playback Mixer",
1284 TWL4030_REG_VDL_APGA_CTL, 0, 0, NULL, 0),
Peter Ujfalusi73939582009-01-29 14:57:50 +02001285
Peter Ujfalusi7729cf72009-10-29 11:58:10 +02001286 SND_SOC_DAPM_SUPPLY("APLL Enable", SND_SOC_NOPM, 0, 0, apll_event,
1287 SND_SOC_DAPM_PRE_PMU|SND_SOC_DAPM_POST_PMD),
1288
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +03001289 SND_SOC_DAPM_SUPPLY("AIF Enable", SND_SOC_NOPM, 0, 0, aif_event,
1290 SND_SOC_DAPM_PRE_PMU|SND_SOC_DAPM_POST_PMD),
Peter Ujfalusic42a59e2010-02-09 15:24:04 +02001291
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001292 /* Output MIXER controls */
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001293 /* Earpiece */
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001294 SND_SOC_DAPM_MIXER("Earpiece Mixer", SND_SOC_NOPM, 0, 0,
1295 &twl4030_dapm_earpiece_controls[0],
1296 ARRAY_SIZE(twl4030_dapm_earpiece_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001297 SND_SOC_DAPM_PGA_E("Earpiece PGA", SND_SOC_NOPM,
1298 0, 0, NULL, 0, earpiecepga_event,
1299 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +02001300 /* PreDrivL/R */
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001301 SND_SOC_DAPM_MIXER("PredriveL Mixer", SND_SOC_NOPM, 0, 0,
1302 &twl4030_dapm_predrivel_controls[0],
1303 ARRAY_SIZE(twl4030_dapm_predrivel_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001304 SND_SOC_DAPM_PGA_E("PredriveL PGA", SND_SOC_NOPM,
1305 0, 0, NULL, 0, predrivelpga_event,
1306 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001307 SND_SOC_DAPM_MIXER("PredriveR Mixer", SND_SOC_NOPM, 0, 0,
1308 &twl4030_dapm_predriver_controls[0],
1309 ARRAY_SIZE(twl4030_dapm_predriver_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001310 SND_SOC_DAPM_PGA_E("PredriveR PGA", SND_SOC_NOPM,
1311 0, 0, NULL, 0, predriverpga_event,
1312 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Peter Ujfalusidfad21a2008-12-09 12:35:49 +02001313 /* HeadsetL/R */
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001314 SND_SOC_DAPM_MIXER("HeadsetL Mixer", SND_SOC_NOPM, 0, 0,
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001315 &twl4030_dapm_hsol_controls[0],
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001316 ARRAY_SIZE(twl4030_dapm_hsol_controls)),
1317 SND_SOC_DAPM_PGA_E("HeadsetL PGA", SND_SOC_NOPM,
1318 0, 0, NULL, 0, headsetlpga_event,
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001319 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
1320 SND_SOC_DAPM_MIXER("HeadsetR Mixer", SND_SOC_NOPM, 0, 0,
1321 &twl4030_dapm_hsor_controls[0],
1322 ARRAY_SIZE(twl4030_dapm_hsor_controls)),
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001323 SND_SOC_DAPM_PGA_E("HeadsetR PGA", SND_SOC_NOPM,
1324 0, 0, NULL, 0, headsetrpga_event,
1325 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +02001326 /* CarkitL/R */
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001327 SND_SOC_DAPM_MIXER("CarkitL Mixer", SND_SOC_NOPM, 0, 0,
1328 &twl4030_dapm_carkitl_controls[0],
1329 ARRAY_SIZE(twl4030_dapm_carkitl_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001330 SND_SOC_DAPM_PGA_E("CarkitL PGA", SND_SOC_NOPM,
1331 0, 0, NULL, 0, carkitlpga_event,
1332 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001333 SND_SOC_DAPM_MIXER("CarkitR Mixer", SND_SOC_NOPM, 0, 0,
1334 &twl4030_dapm_carkitr_controls[0],
1335 ARRAY_SIZE(twl4030_dapm_carkitr_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001336 SND_SOC_DAPM_PGA_E("CarkitR PGA", SND_SOC_NOPM,
1337 0, 0, NULL, 0, carkitrpga_event,
1338 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001339
1340 /* Output MUX controls */
Peter Ujfalusidf339802008-12-09 12:35:51 +02001341 /* HandsfreeL/R */
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +03001342 SND_SOC_DAPM_MUX("HandsfreeL Mux", SND_SOC_NOPM, 0, 0,
1343 &twl4030_dapm_handsfreel_control),
Lopez Cruz, Misaele3c7dbb2009-06-25 12:36:14 -05001344 SND_SOC_DAPM_SWITCH("HandsfreeL", SND_SOC_NOPM, 0, 0,
Peter Ujfalusi0f89bdc2009-05-25 11:12:13 +03001345 &twl4030_dapm_handsfreelmute_control),
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +03001346 SND_SOC_DAPM_PGA_E("HandsfreeL PGA", SND_SOC_NOPM,
1347 0, 0, NULL, 0, handsfreelpga_event,
1348 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
1349 SND_SOC_DAPM_MUX("HandsfreeR Mux", SND_SOC_NOPM, 5, 0,
1350 &twl4030_dapm_handsfreer_control),
Lopez Cruz, Misaele3c7dbb2009-06-25 12:36:14 -05001351 SND_SOC_DAPM_SWITCH("HandsfreeR", SND_SOC_NOPM, 0, 0,
Peter Ujfalusi0f89bdc2009-05-25 11:12:13 +03001352 &twl4030_dapm_handsfreermute_control),
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +03001353 SND_SOC_DAPM_PGA_E("HandsfreeR PGA", SND_SOC_NOPM,
1354 0, 0, NULL, 0, handsfreerpga_event,
1355 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001356 /* Vibra */
Jari Vanhala86139a12009-10-29 11:58:09 +02001357 SND_SOC_DAPM_MUX_E("Vibra Mux", TWL4030_REG_VIBRA_CTL, 0, 0,
1358 &twl4030_dapm_vibra_control, vibramux_event,
1359 SND_SOC_DAPM_PRE_PMU),
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001360 SND_SOC_DAPM_MUX("Vibra Route", SND_SOC_NOPM, 0, 0,
1361 &twl4030_dapm_vibrapath_control),
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001362
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001363 /* Introducing four virtual ADC, since TWL4030 have four channel for
1364 capture */
1365 SND_SOC_DAPM_ADC("ADC Virtual Left1", "Left Front Capture",
1366 SND_SOC_NOPM, 0, 0),
1367 SND_SOC_DAPM_ADC("ADC Virtual Right1", "Right Front Capture",
1368 SND_SOC_NOPM, 0, 0),
1369 SND_SOC_DAPM_ADC("ADC Virtual Left2", "Left Rear Capture",
1370 SND_SOC_NOPM, 0, 0),
1371 SND_SOC_DAPM_ADC("ADC Virtual Right2", "Right Rear Capture",
1372 SND_SOC_NOPM, 0, 0),
1373
1374 /* Analog/Digital mic path selection.
1375 TX1 Left/Right: either analog Left/Right or Digimic0
1376 TX2 Left/Right: either analog Left/Right or Digimic1 */
1377 SND_SOC_DAPM_MUX_E("TX1 Capture Route", SND_SOC_NOPM, 0, 0,
1378 &twl4030_dapm_micpathtx1_control, micpath_event,
1379 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD|
1380 SND_SOC_DAPM_POST_REG),
1381 SND_SOC_DAPM_MUX_E("TX2 Capture Route", SND_SOC_NOPM, 0, 0,
1382 &twl4030_dapm_micpathtx2_control, micpath_event,
1383 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD|
1384 SND_SOC_DAPM_POST_REG),
1385
Joonyoung Shim97b80962009-05-11 20:36:08 +09001386 /* Analog input mixers for the capture amplifiers */
Peter Ujfalusi90289352009-08-14 08:44:00 +03001387 SND_SOC_DAPM_MIXER("Analog Left",
Joonyoung Shim97b80962009-05-11 20:36:08 +09001388 TWL4030_REG_ANAMICL, 4, 0,
1389 &twl4030_dapm_analoglmic_controls[0],
1390 ARRAY_SIZE(twl4030_dapm_analoglmic_controls)),
Peter Ujfalusi90289352009-08-14 08:44:00 +03001391 SND_SOC_DAPM_MIXER("Analog Right",
Joonyoung Shim97b80962009-05-11 20:36:08 +09001392 TWL4030_REG_ANAMICR, 4, 0,
1393 &twl4030_dapm_analogrmic_controls[0],
1394 ARRAY_SIZE(twl4030_dapm_analogrmic_controls)),
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001395
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001396 SND_SOC_DAPM_PGA("ADC Physical Left",
1397 TWL4030_REG_AVADC_CTL, 3, 0, NULL, 0),
1398 SND_SOC_DAPM_PGA("ADC Physical Right",
1399 TWL4030_REG_AVADC_CTL, 1, 0, NULL, 0),
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001400
1401 SND_SOC_DAPM_PGA("Digimic0 Enable",
1402 TWL4030_REG_ADCMICSEL, 1, 0, NULL, 0),
1403 SND_SOC_DAPM_PGA("Digimic1 Enable",
1404 TWL4030_REG_ADCMICSEL, 3, 0, NULL, 0),
1405
1406 SND_SOC_DAPM_MICBIAS("Mic Bias 1", TWL4030_REG_MICBIAS_CTL, 0, 0),
1407 SND_SOC_DAPM_MICBIAS("Mic Bias 2", TWL4030_REG_MICBIAS_CTL, 1, 0),
1408 SND_SOC_DAPM_MICBIAS("Headset Mic Bias", TWL4030_REG_MICBIAS_CTL, 2, 0),
Peter Ujfalusi73939582009-01-29 14:57:50 +02001409
Steve Sakomancc175572008-10-30 21:35:26 -07001410};
1411
1412static const struct snd_soc_dapm_route intercon[] = {
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001413 {"Digital L1 Playback Mixer", NULL, "DAC Left1"},
1414 {"Digital R1 Playback Mixer", NULL, "DAC Right1"},
1415 {"Digital L2 Playback Mixer", NULL, "DAC Left2"},
1416 {"Digital R2 Playback Mixer", NULL, "DAC Right2"},
1417 {"Digital Voice Playback Mixer", NULL, "DAC Voice"},
Peter Ujfalusi73939582009-01-29 14:57:50 +02001418
Peter Ujfalusi7729cf72009-10-29 11:58:10 +02001419 /* Supply for the digital part (APLL) */
Peter Ujfalusi7729cf72009-10-29 11:58:10 +02001420 {"Digital Voice Playback Mixer", NULL, "APLL Enable"},
1421
Peter Ujfalusic42a59e2010-02-09 15:24:04 +02001422 {"Digital R1 Playback Mixer", NULL, "AIF Enable"},
1423 {"Digital L1 Playback Mixer", NULL, "AIF Enable"},
1424 {"Digital R2 Playback Mixer", NULL, "AIF Enable"},
1425 {"Digital L2 Playback Mixer", NULL, "AIF Enable"},
1426
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001427 {"Analog L1 Playback Mixer", NULL, "Digital L1 Playback Mixer"},
1428 {"Analog R1 Playback Mixer", NULL, "Digital R1 Playback Mixer"},
1429 {"Analog L2 Playback Mixer", NULL, "Digital L2 Playback Mixer"},
1430 {"Analog R2 Playback Mixer", NULL, "Digital R2 Playback Mixer"},
1431 {"Analog Voice Playback Mixer", NULL, "Digital Voice Playback Mixer"},
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001432
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001433 /* Internal playback routings */
1434 /* Earpiece */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001435 {"Earpiece Mixer", "Voice", "Analog Voice Playback Mixer"},
1436 {"Earpiece Mixer", "AudioL1", "Analog L1 Playback Mixer"},
1437 {"Earpiece Mixer", "AudioL2", "Analog L2 Playback Mixer"},
1438 {"Earpiece Mixer", "AudioR1", "Analog R1 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001439 {"Earpiece PGA", NULL, "Earpiece Mixer"},
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +02001440 /* PreDrivL */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001441 {"PredriveL Mixer", "Voice", "Analog Voice Playback Mixer"},
1442 {"PredriveL Mixer", "AudioL1", "Analog L1 Playback Mixer"},
1443 {"PredriveL Mixer", "AudioL2", "Analog L2 Playback Mixer"},
1444 {"PredriveL Mixer", "AudioR2", "Analog R2 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001445 {"PredriveL PGA", NULL, "PredriveL Mixer"},
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +02001446 /* PreDrivR */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001447 {"PredriveR Mixer", "Voice", "Analog Voice Playback Mixer"},
1448 {"PredriveR Mixer", "AudioR1", "Analog R1 Playback Mixer"},
1449 {"PredriveR Mixer", "AudioR2", "Analog R2 Playback Mixer"},
1450 {"PredriveR Mixer", "AudioL2", "Analog L2 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001451 {"PredriveR PGA", NULL, "PredriveR Mixer"},
Peter Ujfalusidfad21a2008-12-09 12:35:49 +02001452 /* HeadsetL */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001453 {"HeadsetL Mixer", "Voice", "Analog Voice Playback Mixer"},
1454 {"HeadsetL Mixer", "AudioL1", "Analog L1 Playback Mixer"},
1455 {"HeadsetL Mixer", "AudioL2", "Analog L2 Playback Mixer"},
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001456 {"HeadsetL PGA", NULL, "HeadsetL Mixer"},
Peter Ujfalusidfad21a2008-12-09 12:35:49 +02001457 /* HeadsetR */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001458 {"HeadsetR Mixer", "Voice", "Analog Voice Playback Mixer"},
1459 {"HeadsetR Mixer", "AudioR1", "Analog R1 Playback Mixer"},
1460 {"HeadsetR Mixer", "AudioR2", "Analog R2 Playback Mixer"},
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001461 {"HeadsetR PGA", NULL, "HeadsetR Mixer"},
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +02001462 /* CarkitL */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001463 {"CarkitL Mixer", "Voice", "Analog Voice Playback Mixer"},
1464 {"CarkitL Mixer", "AudioL1", "Analog L1 Playback Mixer"},
1465 {"CarkitL Mixer", "AudioL2", "Analog L2 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001466 {"CarkitL PGA", NULL, "CarkitL Mixer"},
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +02001467 /* CarkitR */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001468 {"CarkitR Mixer", "Voice", "Analog Voice Playback Mixer"},
1469 {"CarkitR Mixer", "AudioR1", "Analog R1 Playback Mixer"},
1470 {"CarkitR Mixer", "AudioR2", "Analog R2 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001471 {"CarkitR PGA", NULL, "CarkitR Mixer"},
Peter Ujfalusidf339802008-12-09 12:35:51 +02001472 /* HandsfreeL */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001473 {"HandsfreeL Mux", "Voice", "Analog Voice Playback Mixer"},
1474 {"HandsfreeL Mux", "AudioL1", "Analog L1 Playback Mixer"},
1475 {"HandsfreeL Mux", "AudioL2", "Analog L2 Playback Mixer"},
1476 {"HandsfreeL Mux", "AudioR2", "Analog R2 Playback Mixer"},
Lopez Cruz, Misaele3c7dbb2009-06-25 12:36:14 -05001477 {"HandsfreeL", "Switch", "HandsfreeL Mux"},
1478 {"HandsfreeL PGA", NULL, "HandsfreeL"},
Peter Ujfalusidf339802008-12-09 12:35:51 +02001479 /* HandsfreeR */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001480 {"HandsfreeR Mux", "Voice", "Analog Voice Playback Mixer"},
1481 {"HandsfreeR Mux", "AudioR1", "Analog R1 Playback Mixer"},
1482 {"HandsfreeR Mux", "AudioR2", "Analog R2 Playback Mixer"},
1483 {"HandsfreeR Mux", "AudioL2", "Analog L2 Playback Mixer"},
Lopez Cruz, Misaele3c7dbb2009-06-25 12:36:14 -05001484 {"HandsfreeR", "Switch", "HandsfreeR Mux"},
1485 {"HandsfreeR PGA", NULL, "HandsfreeR"},
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001486 /* Vibra */
1487 {"Vibra Mux", "AudioL1", "DAC Left1"},
1488 {"Vibra Mux", "AudioR1", "DAC Right1"},
1489 {"Vibra Mux", "AudioL2", "DAC Left2"},
1490 {"Vibra Mux", "AudioR2", "DAC Right2"},
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001491
Steve Sakomancc175572008-10-30 21:35:26 -07001492 /* outputs */
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +03001493 /* Must be always connected (for AIF and APLL) */
1494 {"Virtual HiFi OUT", NULL, "Digital L1 Playback Mixer"},
1495 {"Virtual HiFi OUT", NULL, "Digital R1 Playback Mixer"},
1496 {"Virtual HiFi OUT", NULL, "Digital L2 Playback Mixer"},
1497 {"Virtual HiFi OUT", NULL, "Digital R2 Playback Mixer"},
1498 /* Must be always connected (for APLL) */
1499 {"Virtual Voice OUT", NULL, "Digital Voice Playback Mixer"},
1500 /* Physical outputs */
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001501 {"EARPIECE", NULL, "Earpiece PGA"},
1502 {"PREDRIVEL", NULL, "PredriveL PGA"},
1503 {"PREDRIVER", NULL, "PredriveR PGA"},
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001504 {"HSOL", NULL, "HeadsetL PGA"},
1505 {"HSOR", NULL, "HeadsetR PGA"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001506 {"CARKITL", NULL, "CarkitL PGA"},
1507 {"CARKITR", NULL, "CarkitR PGA"},
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +03001508 {"HFL", NULL, "HandsfreeL PGA"},
1509 {"HFR", NULL, "HandsfreeR PGA"},
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001510 {"Vibra Route", "Audio", "Vibra Mux"},
1511 {"VIBRA", NULL, "Vibra Route"},
Steve Sakomancc175572008-10-30 21:35:26 -07001512
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001513 /* Capture path */
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +03001514 /* Must be always connected (for AIF and APLL) */
1515 {"ADC Virtual Left1", NULL, "Virtual HiFi IN"},
1516 {"ADC Virtual Right1", NULL, "Virtual HiFi IN"},
1517 {"ADC Virtual Left2", NULL, "Virtual HiFi IN"},
1518 {"ADC Virtual Right2", NULL, "Virtual HiFi IN"},
1519 /* Physical inputs */
Peter Ujfalusi90289352009-08-14 08:44:00 +03001520 {"Analog Left", "Main Mic Capture Switch", "MAINMIC"},
1521 {"Analog Left", "Headset Mic Capture Switch", "HSMIC"},
1522 {"Analog Left", "AUXL Capture Switch", "AUXL"},
1523 {"Analog Left", "Carkit Mic Capture Switch", "CARKITMIC"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001524
Peter Ujfalusi90289352009-08-14 08:44:00 +03001525 {"Analog Right", "Sub Mic Capture Switch", "SUBMIC"},
1526 {"Analog Right", "AUXR Capture Switch", "AUXR"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001527
Peter Ujfalusi90289352009-08-14 08:44:00 +03001528 {"ADC Physical Left", NULL, "Analog Left"},
1529 {"ADC Physical Right", NULL, "Analog Right"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001530
1531 {"Digimic0 Enable", NULL, "DIGIMIC0"},
1532 {"Digimic1 Enable", NULL, "DIGIMIC1"},
1533
1534 /* TX1 Left capture path */
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001535 {"TX1 Capture Route", "Analog", "ADC Physical Left"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001536 {"TX1 Capture Route", "Digimic0", "Digimic0 Enable"},
1537 /* TX1 Right capture path */
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001538 {"TX1 Capture Route", "Analog", "ADC Physical Right"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001539 {"TX1 Capture Route", "Digimic0", "Digimic0 Enable"},
1540 /* TX2 Left capture path */
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001541 {"TX2 Capture Route", "Analog", "ADC Physical Left"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001542 {"TX2 Capture Route", "Digimic1", "Digimic1 Enable"},
1543 /* TX2 Right capture path */
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001544 {"TX2 Capture Route", "Analog", "ADC Physical Right"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001545 {"TX2 Capture Route", "Digimic1", "Digimic1 Enable"},
1546
1547 {"ADC Virtual Left1", NULL, "TX1 Capture Route"},
1548 {"ADC Virtual Right1", NULL, "TX1 Capture Route"},
1549 {"ADC Virtual Left2", NULL, "TX2 Capture Route"},
1550 {"ADC Virtual Right2", NULL, "TX2 Capture Route"},
1551
Peter Ujfalusic42a59e2010-02-09 15:24:04 +02001552 {"ADC Virtual Left1", NULL, "AIF Enable"},
1553 {"ADC Virtual Right1", NULL, "AIF Enable"},
1554 {"ADC Virtual Left2", NULL, "AIF Enable"},
1555 {"ADC Virtual Right2", NULL, "AIF Enable"},
1556
Peter Ujfalusi73939582009-01-29 14:57:50 +02001557 /* Analog bypass routes */
Peter Ujfalusi90289352009-08-14 08:44:00 +03001558 {"Right1 Analog Loopback", "Switch", "Analog Right"},
1559 {"Left1 Analog Loopback", "Switch", "Analog Left"},
1560 {"Right2 Analog Loopback", "Switch", "Analog Right"},
1561 {"Left2 Analog Loopback", "Switch", "Analog Left"},
1562 {"Voice Analog Loopback", "Switch", "Analog Left"},
Peter Ujfalusi73939582009-01-29 14:57:50 +02001563
Peter Ujfalusi78e08e22009-10-28 10:57:04 +02001564 /* Supply for the Analog loopbacks */
1565 {"Right1 Analog Loopback", NULL, "FM Loop Enable"},
1566 {"Left1 Analog Loopback", NULL, "FM Loop Enable"},
1567 {"Right2 Analog Loopback", NULL, "FM Loop Enable"},
1568 {"Left2 Analog Loopback", NULL, "FM Loop Enable"},
1569 {"Voice Analog Loopback", NULL, "FM Loop Enable"},
1570
Peter Ujfalusi73939582009-01-29 14:57:50 +02001571 {"Analog R1 Playback Mixer", NULL, "Right1 Analog Loopback"},
1572 {"Analog L1 Playback Mixer", NULL, "Left1 Analog Loopback"},
1573 {"Analog R2 Playback Mixer", NULL, "Right2 Analog Loopback"},
1574 {"Analog L2 Playback Mixer", NULL, "Left2 Analog Loopback"},
Lopez Cruz, Misaelfcd274a2009-04-30 21:47:22 -05001575 {"Analog Voice Playback Mixer", NULL, "Voice Analog Loopback"},
Peter Ujfalusi73939582009-01-29 14:57:50 +02001576
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001577 /* Digital bypass routes */
1578 {"Right Digital Loopback", "Volume", "TX1 Capture Route"},
1579 {"Left Digital Loopback", "Volume", "TX1 Capture Route"},
Lopez Cruz, Misaelee8f6892009-04-30 21:48:08 -05001580 {"Voice Digital Loopback", "Volume", "TX2 Capture Route"},
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001581
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001582 {"Digital R2 Playback Mixer", NULL, "Right Digital Loopback"},
1583 {"Digital L2 Playback Mixer", NULL, "Left Digital Loopback"},
1584 {"Digital Voice Playback Mixer", NULL, "Voice Digital Loopback"},
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001585
Steve Sakomancc175572008-10-30 21:35:26 -07001586};
1587
1588static int twl4030_add_widgets(struct snd_soc_codec *codec)
1589{
1590 snd_soc_dapm_new_controls(codec, twl4030_dapm_widgets,
1591 ARRAY_SIZE(twl4030_dapm_widgets));
1592
1593 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
1594
Steve Sakomancc175572008-10-30 21:35:26 -07001595 return 0;
1596}
1597
Steve Sakomancc175572008-10-30 21:35:26 -07001598static int twl4030_set_bias_level(struct snd_soc_codec *codec,
1599 enum snd_soc_bias_level level)
1600{
1601 switch (level) {
1602 case SND_SOC_BIAS_ON:
Steve Sakomancc175572008-10-30 21:35:26 -07001603 break;
1604 case SND_SOC_BIAS_PREPARE:
Steve Sakomancc175572008-10-30 21:35:26 -07001605 break;
1606 case SND_SOC_BIAS_STANDBY:
Peter Ujfalusi78e08e22009-10-28 10:57:04 +02001607 if (codec->bias_level == SND_SOC_BIAS_OFF)
1608 twl4030_power_up(codec);
Steve Sakomancc175572008-10-30 21:35:26 -07001609 break;
1610 case SND_SOC_BIAS_OFF:
1611 twl4030_power_down(codec);
1612 break;
1613 }
1614 codec->bias_level = level;
1615
1616 return 0;
1617}
1618
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001619static void twl4030_constraints(struct twl4030_priv *twl4030,
1620 struct snd_pcm_substream *mst_substream)
1621{
1622 struct snd_pcm_substream *slv_substream;
1623
1624 /* Pick the stream, which need to be constrained */
1625 if (mst_substream == twl4030->master_substream)
1626 slv_substream = twl4030->slave_substream;
1627 else if (mst_substream == twl4030->slave_substream)
1628 slv_substream = twl4030->master_substream;
1629 else /* This should not happen.. */
1630 return;
1631
1632 /* Set the constraints according to the already configured stream */
1633 snd_pcm_hw_constraint_minmax(slv_substream->runtime,
1634 SNDRV_PCM_HW_PARAM_RATE,
1635 twl4030->rate,
1636 twl4030->rate);
1637
1638 snd_pcm_hw_constraint_minmax(slv_substream->runtime,
1639 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1640 twl4030->sample_bits,
1641 twl4030->sample_bits);
1642
1643 snd_pcm_hw_constraint_minmax(slv_substream->runtime,
1644 SNDRV_PCM_HW_PARAM_CHANNELS,
1645 twl4030->channels,
1646 twl4030->channels);
1647}
1648
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001649/* In case of 4 channel mode, the RX1 L/R for playback and the TX2 L/R for
1650 * capture has to be enabled/disabled. */
1651static void twl4030_tdm_enable(struct snd_soc_codec *codec, int direction,
1652 int enable)
1653{
1654 u8 reg, mask;
1655
1656 reg = twl4030_read_reg_cache(codec, TWL4030_REG_OPTION);
1657
1658 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1659 mask = TWL4030_ARXL1_VRX_EN | TWL4030_ARXR1_EN;
1660 else
1661 mask = TWL4030_ATXL2_VTXL_EN | TWL4030_ATXR2_VTXR_EN;
1662
1663 if (enable)
1664 reg |= mask;
1665 else
1666 reg &= ~mask;
1667
1668 twl4030_write(codec, TWL4030_REG_OPTION, reg);
1669}
1670
Peter Ujfalusid6648da2009-04-07 09:14:00 +03001671static int twl4030_startup(struct snd_pcm_substream *substream,
1672 struct snd_soc_dai *dai)
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001673{
1674 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1675 struct snd_soc_device *socdev = rtd->socdev;
Peter Ujfalusid6648da2009-04-07 09:14:00 +03001676 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001677 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001678
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001679 if (twl4030->master_substream) {
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001680 twl4030->slave_substream = substream;
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001681 /* The DAI has one configuration for playback and capture, so
1682 * if the DAI has been already configured then constrain this
1683 * substream to match it. */
1684 if (twl4030->configured)
1685 twl4030_constraints(twl4030, twl4030->master_substream);
1686 } else {
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001687 if (!(twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE) &
1688 TWL4030_OPTION_1)) {
1689 /* In option2 4 channel is not supported, set the
1690 * constraint for the first stream for channels, the
1691 * second stream will 'inherit' this cosntraint */
1692 snd_pcm_hw_constraint_minmax(substream->runtime,
1693 SNDRV_PCM_HW_PARAM_CHANNELS,
1694 2, 2);
1695 }
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001696 twl4030->master_substream = substream;
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001697 }
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001698
1699 return 0;
1700}
1701
Peter Ujfalusid6648da2009-04-07 09:14:00 +03001702static void twl4030_shutdown(struct snd_pcm_substream *substream,
1703 struct snd_soc_dai *dai)
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001704{
1705 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1706 struct snd_soc_device *socdev = rtd->socdev;
Peter Ujfalusid6648da2009-04-07 09:14:00 +03001707 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001708 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001709
1710 if (twl4030->master_substream == substream)
1711 twl4030->master_substream = twl4030->slave_substream;
1712
1713 twl4030->slave_substream = NULL;
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001714
1715 /* If all streams are closed, or the remaining stream has not yet
1716 * been configured than set the DAI as not configured. */
1717 if (!twl4030->master_substream)
1718 twl4030->configured = 0;
1719 else if (!twl4030->master_substream->runtime->channels)
1720 twl4030->configured = 0;
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001721
1722 /* If the closing substream had 4 channel, do the necessary cleanup */
1723 if (substream->runtime->channels == 4)
1724 twl4030_tdm_enable(codec, substream->stream, 0);
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001725}
1726
Steve Sakomancc175572008-10-30 21:35:26 -07001727static int twl4030_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +00001728 struct snd_pcm_hw_params *params,
1729 struct snd_soc_dai *dai)
Steve Sakomancc175572008-10-30 21:35:26 -07001730{
1731 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1732 struct snd_soc_device *socdev = rtd->socdev;
Mark Brown6627a652009-01-23 22:55:23 +00001733 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001734 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Steve Sakomancc175572008-10-30 21:35:26 -07001735 u8 mode, old_mode, format, old_format;
1736
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001737 /* If the substream has 4 channel, do the necessary setup */
1738 if (params_channels(params) == 4) {
Peter Ujfalusieaf1ac82009-06-01 14:06:40 +03001739 format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1740 mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE);
1741
1742 /* Safety check: are we in the correct operating mode and
1743 * the interface is in TDM mode? */
1744 if ((mode & TWL4030_OPTION_1) &&
1745 ((format & TWL4030_AIF_FORMAT) == TWL4030_AIF_FORMAT_TDM))
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001746 twl4030_tdm_enable(codec, substream->stream, 1);
1747 else
1748 return -EINVAL;
1749 }
1750
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001751 if (twl4030->configured)
1752 /* Ignoring hw_params for already configured DAI */
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001753 return 0;
1754
Steve Sakomancc175572008-10-30 21:35:26 -07001755 /* bit rate */
1756 old_mode = twl4030_read_reg_cache(codec,
1757 TWL4030_REG_CODEC_MODE) & ~TWL4030_CODECPDZ;
1758 mode = old_mode & ~TWL4030_APLL_RATE;
1759
1760 switch (params_rate(params)) {
1761 case 8000:
1762 mode |= TWL4030_APLL_RATE_8000;
1763 break;
1764 case 11025:
1765 mode |= TWL4030_APLL_RATE_11025;
1766 break;
1767 case 12000:
1768 mode |= TWL4030_APLL_RATE_12000;
1769 break;
1770 case 16000:
1771 mode |= TWL4030_APLL_RATE_16000;
1772 break;
1773 case 22050:
1774 mode |= TWL4030_APLL_RATE_22050;
1775 break;
1776 case 24000:
1777 mode |= TWL4030_APLL_RATE_24000;
1778 break;
1779 case 32000:
1780 mode |= TWL4030_APLL_RATE_32000;
1781 break;
1782 case 44100:
1783 mode |= TWL4030_APLL_RATE_44100;
1784 break;
1785 case 48000:
1786 mode |= TWL4030_APLL_RATE_48000;
1787 break;
Peter Ujfalusi103f2112009-04-03 14:39:05 +03001788 case 96000:
1789 mode |= TWL4030_APLL_RATE_96000;
1790 break;
Steve Sakomancc175572008-10-30 21:35:26 -07001791 default:
1792 printk(KERN_ERR "TWL4030 hw params: unknown rate %d\n",
1793 params_rate(params));
1794 return -EINVAL;
1795 }
1796
1797 if (mode != old_mode) {
1798 /* change rate and set CODECPDZ */
Peter Ujfalusi73939582009-01-29 14:57:50 +02001799 twl4030_codec_enable(codec, 0);
Steve Sakomancc175572008-10-30 21:35:26 -07001800 twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode);
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +02001801 twl4030_codec_enable(codec, 1);
Steve Sakomancc175572008-10-30 21:35:26 -07001802 }
1803
1804 /* sample size */
1805 old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1806 format = old_format;
1807 format &= ~TWL4030_DATA_WIDTH;
1808 switch (params_format(params)) {
1809 case SNDRV_PCM_FORMAT_S16_LE:
1810 format |= TWL4030_DATA_WIDTH_16S_16W;
1811 break;
1812 case SNDRV_PCM_FORMAT_S24_LE:
1813 format |= TWL4030_DATA_WIDTH_32S_24W;
1814 break;
1815 default:
1816 printk(KERN_ERR "TWL4030 hw params: unknown format %d\n",
1817 params_format(params));
1818 return -EINVAL;
1819 }
1820
1821 if (format != old_format) {
1822
1823 /* clear CODECPDZ before changing format (codec requirement) */
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +02001824 twl4030_codec_enable(codec, 0);
Steve Sakomancc175572008-10-30 21:35:26 -07001825
1826 /* change format */
1827 twl4030_write(codec, TWL4030_REG_AUDIO_IF, format);
1828
1829 /* set CODECPDZ afterwards */
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +02001830 twl4030_codec_enable(codec, 1);
Steve Sakomancc175572008-10-30 21:35:26 -07001831 }
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001832
1833 /* Store the important parameters for the DAI configuration and set
1834 * the DAI as configured */
1835 twl4030->configured = 1;
1836 twl4030->rate = params_rate(params);
1837 twl4030->sample_bits = hw_param_interval(params,
1838 SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
1839 twl4030->channels = params_channels(params);
1840
1841 /* If both playback and capture streams are open, and one of them
1842 * is setting the hw parameters right now (since we are here), set
1843 * constraints to the other stream to match the current one. */
1844 if (twl4030->slave_substream)
1845 twl4030_constraints(twl4030, substream);
1846
Steve Sakomancc175572008-10-30 21:35:26 -07001847 return 0;
1848}
1849
1850static int twl4030_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1851 int clk_id, unsigned int freq, int dir)
1852{
1853 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001854 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Steve Sakomancc175572008-10-30 21:35:26 -07001855
1856 switch (freq) {
1857 case 19200000:
Steve Sakomancc175572008-10-30 21:35:26 -07001858 case 26000000:
Steve Sakomancc175572008-10-30 21:35:26 -07001859 case 38400000:
Steve Sakomancc175572008-10-30 21:35:26 -07001860 break;
1861 default:
Peter Ujfalusi68d01952009-11-04 09:58:20 +02001862 dev_err(codec->dev, "Unsupported APLL mclk: %u\n", freq);
Steve Sakomancc175572008-10-30 21:35:26 -07001863 return -EINVAL;
1864 }
1865
Peter Ujfalusi68d01952009-11-04 09:58:20 +02001866 if ((freq / 1000) != twl4030->sysclk) {
1867 dev_err(codec->dev,
1868 "Mismatch in APLL mclk: %u (configured: %u)\n",
1869 freq, twl4030->sysclk * 1000);
1870 return -EINVAL;
1871 }
Steve Sakomancc175572008-10-30 21:35:26 -07001872
1873 return 0;
1874}
1875
1876static int twl4030_set_dai_fmt(struct snd_soc_dai *codec_dai,
1877 unsigned int fmt)
1878{
1879 struct snd_soc_codec *codec = codec_dai->codec;
1880 u8 old_format, format;
1881
1882 /* get format */
1883 old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1884 format = old_format;
1885
1886 /* set master/slave audio interface */
1887 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1888 case SND_SOC_DAIFMT_CBM_CFM:
1889 format &= ~(TWL4030_AIF_SLAVE_EN);
Grazvydas Ignotase18c94d2008-11-05 23:51:05 +02001890 format &= ~(TWL4030_CLK256FS_EN);
Steve Sakomancc175572008-10-30 21:35:26 -07001891 break;
1892 case SND_SOC_DAIFMT_CBS_CFS:
Steve Sakomancc175572008-10-30 21:35:26 -07001893 format |= TWL4030_AIF_SLAVE_EN;
Grazvydas Ignotase18c94d2008-11-05 23:51:05 +02001894 format |= TWL4030_CLK256FS_EN;
Steve Sakomancc175572008-10-30 21:35:26 -07001895 break;
1896 default:
1897 return -EINVAL;
1898 }
1899
1900 /* interface format */
1901 format &= ~TWL4030_AIF_FORMAT;
1902 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1903 case SND_SOC_DAIFMT_I2S:
1904 format |= TWL4030_AIF_FORMAT_CODEC;
1905 break;
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001906 case SND_SOC_DAIFMT_DSP_A:
1907 format |= TWL4030_AIF_FORMAT_TDM;
1908 break;
Steve Sakomancc175572008-10-30 21:35:26 -07001909 default:
1910 return -EINVAL;
1911 }
1912
1913 if (format != old_format) {
1914
1915 /* clear CODECPDZ before changing format (codec requirement) */
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +02001916 twl4030_codec_enable(codec, 0);
Steve Sakomancc175572008-10-30 21:35:26 -07001917
1918 /* change format */
1919 twl4030_write(codec, TWL4030_REG_AUDIO_IF, format);
1920
1921 /* set CODECPDZ afterwards */
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +02001922 twl4030_codec_enable(codec, 1);
Steve Sakomancc175572008-10-30 21:35:26 -07001923 }
1924
1925 return 0;
1926}
1927
Lopez Cruz, Misael68140442009-07-03 02:21:39 -05001928static int twl4030_set_tristate(struct snd_soc_dai *dai, int tristate)
1929{
1930 struct snd_soc_codec *codec = dai->codec;
1931 u8 reg = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1932
1933 if (tristate)
1934 reg |= TWL4030_AIF_TRI_EN;
1935 else
1936 reg &= ~TWL4030_AIF_TRI_EN;
1937
1938 return twl4030_write(codec, TWL4030_REG_AUDIO_IF, reg);
1939}
1940
Misael Lopez Cruzb7a755a2009-05-17 20:02:31 -05001941/* In case of voice mode, the RX1 L(VRX) for downlink and the TX2 L/R
1942 * (VTXL, VTXR) for uplink has to be enabled/disabled. */
1943static void twl4030_voice_enable(struct snd_soc_codec *codec, int direction,
1944 int enable)
1945{
1946 u8 reg, mask;
1947
1948 reg = twl4030_read_reg_cache(codec, TWL4030_REG_OPTION);
1949
1950 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1951 mask = TWL4030_ARXL1_VRX_EN;
1952 else
1953 mask = TWL4030_ATXL2_VTXL_EN | TWL4030_ATXR2_VTXR_EN;
1954
1955 if (enable)
1956 reg |= mask;
1957 else
1958 reg &= ~mask;
1959
1960 twl4030_write(codec, TWL4030_REG_OPTION, reg);
1961}
1962
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09001963static int twl4030_voice_startup(struct snd_pcm_substream *substream,
1964 struct snd_soc_dai *dai)
1965{
1966 struct snd_soc_pcm_runtime *rtd = substream->private_data;
1967 struct snd_soc_device *socdev = rtd->socdev;
1968 struct snd_soc_codec *codec = socdev->card->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001969 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09001970 u8 mode;
1971
1972 /* If the system master clock is not 26MHz, the voice PCM interface is
1973 * not avilable.
1974 */
Peter Ujfalusi68d01952009-11-04 09:58:20 +02001975 if (twl4030->sysclk != 26000) {
1976 dev_err(codec->dev, "The board is configured for %u Hz, while"
1977 "the Voice interface needs 26MHz APLL mclk\n",
1978 twl4030->sysclk * 1000);
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09001979 return -EINVAL;
1980 }
1981
1982 /* If the codec mode is not option2, the voice PCM interface is not
1983 * avilable.
1984 */
1985 mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE)
1986 & TWL4030_OPT_MODE;
1987
1988 if (mode != TWL4030_OPTION_2) {
1989 printk(KERN_ERR "TWL4030 voice startup: "
1990 "the codec mode is not option2\n");
1991 return -EINVAL;
1992 }
1993
1994 return 0;
1995}
1996
Misael Lopez Cruzb7a755a2009-05-17 20:02:31 -05001997static void twl4030_voice_shutdown(struct snd_pcm_substream *substream,
1998 struct snd_soc_dai *dai)
1999{
2000 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2001 struct snd_soc_device *socdev = rtd->socdev;
2002 struct snd_soc_codec *codec = socdev->card->codec;
2003
2004 /* Enable voice digital filters */
2005 twl4030_voice_enable(codec, substream->stream, 0);
2006}
2007
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002008static int twl4030_voice_hw_params(struct snd_pcm_substream *substream,
2009 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
2010{
2011 struct snd_soc_pcm_runtime *rtd = substream->private_data;
2012 struct snd_soc_device *socdev = rtd->socdev;
2013 struct snd_soc_codec *codec = socdev->card->codec;
2014 u8 old_mode, mode;
2015
Misael Lopez Cruzb7a755a2009-05-17 20:02:31 -05002016 /* Enable voice digital filters */
2017 twl4030_voice_enable(codec, substream->stream, 1);
2018
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002019 /* bit rate */
2020 old_mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE)
2021 & ~(TWL4030_CODECPDZ);
2022 mode = old_mode;
2023
2024 switch (params_rate(params)) {
2025 case 8000:
2026 mode &= ~(TWL4030_SEL_16K);
2027 break;
2028 case 16000:
2029 mode |= TWL4030_SEL_16K;
2030 break;
2031 default:
2032 printk(KERN_ERR "TWL4030 voice hw params: unknown rate %d\n",
2033 params_rate(params));
2034 return -EINVAL;
2035 }
2036
2037 if (mode != old_mode) {
2038 /* change rate and set CODECPDZ */
2039 twl4030_codec_enable(codec, 0);
2040 twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode);
2041 twl4030_codec_enable(codec, 1);
2042 }
2043
2044 return 0;
2045}
2046
2047static int twl4030_voice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
2048 int clk_id, unsigned int freq, int dir)
2049{
2050 struct snd_soc_codec *codec = codec_dai->codec;
Takashi Iwaid4a8ca22010-04-20 08:20:31 +02002051 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002052
Peter Ujfalusi68d01952009-11-04 09:58:20 +02002053 if (freq != 26000000) {
2054 dev_err(codec->dev, "Unsupported APLL mclk: %u, the Voice"
2055 "interface needs 26MHz APLL mclk\n", freq);
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002056 return -EINVAL;
2057 }
Peter Ujfalusi68d01952009-11-04 09:58:20 +02002058 if ((freq / 1000) != twl4030->sysclk) {
2059 dev_err(codec->dev,
2060 "Mismatch in APLL mclk: %u (configured: %u)\n",
2061 freq, twl4030->sysclk * 1000);
2062 return -EINVAL;
2063 }
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002064 return 0;
2065}
2066
2067static int twl4030_voice_set_dai_fmt(struct snd_soc_dai *codec_dai,
2068 unsigned int fmt)
2069{
2070 struct snd_soc_codec *codec = codec_dai->codec;
2071 u8 old_format, format;
2072
2073 /* get format */
2074 old_format = twl4030_read_reg_cache(codec, TWL4030_REG_VOICE_IF);
2075 format = old_format;
2076
2077 /* set master/slave audio interface */
2078 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
Lopez Cruz, Misaelc2643012009-06-19 03:23:42 -05002079 case SND_SOC_DAIFMT_CBM_CFM:
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002080 format &= ~(TWL4030_VIF_SLAVE_EN);
2081 break;
2082 case SND_SOC_DAIFMT_CBS_CFS:
2083 format |= TWL4030_VIF_SLAVE_EN;
2084 break;
2085 default:
2086 return -EINVAL;
2087 }
2088
2089 /* clock inversion */
2090 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2091 case SND_SOC_DAIFMT_IB_NF:
2092 format &= ~(TWL4030_VIF_FORMAT);
2093 break;
2094 case SND_SOC_DAIFMT_NB_IF:
2095 format |= TWL4030_VIF_FORMAT;
2096 break;
2097 default:
2098 return -EINVAL;
2099 }
2100
2101 if (format != old_format) {
2102 /* change format and set CODECPDZ */
2103 twl4030_codec_enable(codec, 0);
2104 twl4030_write(codec, TWL4030_REG_VOICE_IF, format);
2105 twl4030_codec_enable(codec, 1);
2106 }
2107
2108 return 0;
2109}
2110
Lopez Cruz, Misael68140442009-07-03 02:21:39 -05002111static int twl4030_voice_set_tristate(struct snd_soc_dai *dai, int tristate)
2112{
2113 struct snd_soc_codec *codec = dai->codec;
2114 u8 reg = twl4030_read_reg_cache(codec, TWL4030_REG_VOICE_IF);
2115
2116 if (tristate)
2117 reg |= TWL4030_VIF_TRI_EN;
2118 else
2119 reg &= ~TWL4030_VIF_TRI_EN;
2120
2121 return twl4030_write(codec, TWL4030_REG_VOICE_IF, reg);
2122}
2123
Jarkko Nikulabbba9442008-11-12 17:05:41 +02002124#define TWL4030_RATES (SNDRV_PCM_RATE_8000_48000)
Steve Sakomancc175572008-10-30 21:35:26 -07002125#define TWL4030_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FORMAT_S24_LE)
2126
Joonyoung Shim10d9e3d2009-03-16 21:23:35 +09002127static struct snd_soc_dai_ops twl4030_dai_ops = {
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02002128 .startup = twl4030_startup,
2129 .shutdown = twl4030_shutdown,
Joonyoung Shim10d9e3d2009-03-16 21:23:35 +09002130 .hw_params = twl4030_hw_params,
2131 .set_sysclk = twl4030_set_dai_sysclk,
2132 .set_fmt = twl4030_set_dai_fmt,
Lopez Cruz, Misael68140442009-07-03 02:21:39 -05002133 .set_tristate = twl4030_set_tristate,
Joonyoung Shim10d9e3d2009-03-16 21:23:35 +09002134};
2135
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002136static struct snd_soc_dai_ops twl4030_dai_voice_ops = {
2137 .startup = twl4030_voice_startup,
Misael Lopez Cruzb7a755a2009-05-17 20:02:31 -05002138 .shutdown = twl4030_voice_shutdown,
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002139 .hw_params = twl4030_voice_hw_params,
2140 .set_sysclk = twl4030_voice_set_dai_sysclk,
2141 .set_fmt = twl4030_voice_set_dai_fmt,
Lopez Cruz, Misael68140442009-07-03 02:21:39 -05002142 .set_tristate = twl4030_voice_set_tristate,
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002143};
2144
2145struct snd_soc_dai twl4030_dai[] = {
2146{
Steve Sakomancc175572008-10-30 21:35:26 -07002147 .name = "twl4030",
2148 .playback = {
Peter Ujfalusib4852b72009-05-22 15:12:15 +03002149 .stream_name = "HiFi Playback",
Steve Sakomancc175572008-10-30 21:35:26 -07002150 .channels_min = 2,
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03002151 .channels_max = 4,
Peter Ujfalusi31ad0f32009-03-27 10:39:07 +02002152 .rates = TWL4030_RATES | SNDRV_PCM_RATE_96000,
Steve Sakomancc175572008-10-30 21:35:26 -07002153 .formats = TWL4030_FORMATS,},
2154 .capture = {
2155 .stream_name = "Capture",
2156 .channels_min = 2,
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03002157 .channels_max = 4,
Steve Sakomancc175572008-10-30 21:35:26 -07002158 .rates = TWL4030_RATES,
2159 .formats = TWL4030_FORMATS,},
Joonyoung Shim10d9e3d2009-03-16 21:23:35 +09002160 .ops = &twl4030_dai_ops,
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002161},
2162{
2163 .name = "twl4030 Voice",
2164 .playback = {
Peter Ujfalusib4852b72009-05-22 15:12:15 +03002165 .stream_name = "Voice Playback",
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002166 .channels_min = 1,
2167 .channels_max = 1,
2168 .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
2169 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
2170 .capture = {
2171 .stream_name = "Capture",
2172 .channels_min = 1,
2173 .channels_max = 2,
2174 .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
2175 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
2176 .ops = &twl4030_dai_voice_ops,
2177},
Steve Sakomancc175572008-10-30 21:35:26 -07002178};
2179EXPORT_SYMBOL_GPL(twl4030_dai);
2180
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002181static int twl4030_soc_suspend(struct platform_device *pdev, pm_message_t state)
Steve Sakomancc175572008-10-30 21:35:26 -07002182{
2183 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +00002184 struct snd_soc_codec *codec = socdev->card->codec;
Steve Sakomancc175572008-10-30 21:35:26 -07002185
2186 twl4030_set_bias_level(codec, SND_SOC_BIAS_OFF);
2187
2188 return 0;
2189}
2190
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002191static int twl4030_soc_resume(struct platform_device *pdev)
Steve Sakomancc175572008-10-30 21:35:26 -07002192{
2193 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +00002194 struct snd_soc_codec *codec = socdev->card->codec;
Steve Sakomancc175572008-10-30 21:35:26 -07002195
2196 twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Steve Sakomancc175572008-10-30 21:35:26 -07002197 return 0;
2198}
2199
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002200static struct snd_soc_codec *twl4030_codec;
Steve Sakomancc175572008-10-30 21:35:26 -07002201
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002202static int twl4030_soc_probe(struct platform_device *pdev)
Steve Sakomancc175572008-10-30 21:35:26 -07002203{
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002204 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Peter Ujfalusi9da28c72009-05-22 10:13:15 +03002205 struct twl4030_setup_data *setup = socdev->codec_data;
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002206 struct snd_soc_codec *codec;
2207 struct twl4030_priv *twl4030;
2208 int ret;
Steve Sakomancc175572008-10-30 21:35:26 -07002209
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002210 BUG_ON(!twl4030_codec);
Steve Sakomancc175572008-10-30 21:35:26 -07002211
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002212 codec = twl4030_codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09002213 twl4030 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002214 socdev->card->codec = codec;
Steve Sakomancc175572008-10-30 21:35:26 -07002215
Peter Ujfalusi9da28c72009-05-22 10:13:15 +03002216 /* Configuration for headset ramp delay from setup data */
2217 if (setup) {
2218 unsigned char hs_pop;
2219
Peter Ujfalusi68d01952009-11-04 09:58:20 +02002220 if (setup->sysclk != twl4030->sysclk)
2221 dev_warn(&pdev->dev,
2222 "Mismatch in APLL mclk: %u (configured: %u)\n",
2223 setup->sysclk, twl4030->sysclk);
Peter Ujfalusi9da28c72009-05-22 10:13:15 +03002224
2225 hs_pop = twl4030_read_reg_cache(codec, TWL4030_REG_HS_POPN_SET);
2226 hs_pop &= ~TWL4030_RAMP_DELAY;
2227 hs_pop |= (setup->ramp_delay_value << 2);
2228 twl4030_write_reg_cache(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
Peter Ujfalusi9da28c72009-05-22 10:13:15 +03002229 }
2230
Steve Sakomancc175572008-10-30 21:35:26 -07002231 /* register pcms */
2232 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
2233 if (ret < 0) {
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002234 dev_err(&pdev->dev, "failed to create pcms\n");
2235 return ret;
Steve Sakomancc175572008-10-30 21:35:26 -07002236 }
2237
Ian Molton3e8e1952009-01-09 00:23:21 +00002238 snd_soc_add_controls(codec, twl4030_snd_controls,
2239 ARRAY_SIZE(twl4030_snd_controls));
Steve Sakomancc175572008-10-30 21:35:26 -07002240 twl4030_add_widgets(codec);
2241
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002242 return 0;
Steve Sakomancc175572008-10-30 21:35:26 -07002243}
2244
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002245static int twl4030_soc_remove(struct platform_device *pdev)
Steve Sakomancc175572008-10-30 21:35:26 -07002246{
2247 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
Mark Brown6627a652009-01-23 22:55:23 +00002248 struct snd_soc_codec *codec = socdev->card->codec;
Steve Sakomancc175572008-10-30 21:35:26 -07002249
Peter Ujfalusi73939582009-01-29 14:57:50 +02002250 twl4030_set_bias_level(codec, SND_SOC_BIAS_OFF);
Peter Ujfalusic6d1662b2009-01-08 15:52:43 +02002251 snd_soc_free_pcms(socdev);
2252 snd_soc_dapm_free(socdev);
Steve Sakomancc175572008-10-30 21:35:26 -07002253
2254 return 0;
2255}
2256
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002257static int __devinit twl4030_codec_probe(struct platform_device *pdev)
2258{
2259 struct twl4030_codec_audio_data *pdata = pdev->dev.platform_data;
2260 struct snd_soc_codec *codec;
2261 struct twl4030_priv *twl4030;
2262 int ret;
2263
Peter Ujfalusi68d01952009-11-04 09:58:20 +02002264 if (!pdata) {
2265 dev_err(&pdev->dev, "platform_data is missing\n");
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002266 return -EINVAL;
2267 }
2268
2269 twl4030 = kzalloc(sizeof(struct twl4030_priv), GFP_KERNEL);
2270 if (twl4030 == NULL) {
2271 dev_err(&pdev->dev, "Can not allocate memroy\n");
2272 return -ENOMEM;
2273 }
2274
2275 codec = &twl4030->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09002276 snd_soc_codec_set_drvdata(codec, twl4030);
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002277 codec->dev = &pdev->dev;
2278 twl4030_dai[0].dev = &pdev->dev;
2279 twl4030_dai[1].dev = &pdev->dev;
2280
2281 mutex_init(&codec->mutex);
2282 INIT_LIST_HEAD(&codec->dapm_widgets);
2283 INIT_LIST_HEAD(&codec->dapm_paths);
2284
2285 codec->name = "twl4030";
2286 codec->owner = THIS_MODULE;
2287 codec->read = twl4030_read_reg_cache;
2288 codec->write = twl4030_write;
2289 codec->set_bias_level = twl4030_set_bias_level;
2290 codec->dai = twl4030_dai;
Peter Ujfalusifd63df22010-01-13 12:37:49 +02002291 codec->num_dai = ARRAY_SIZE(twl4030_dai);
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002292 codec->reg_cache_size = sizeof(twl4030_reg);
2293 codec->reg_cache = kmemdup(twl4030_reg, sizeof(twl4030_reg),
2294 GFP_KERNEL);
2295 if (codec->reg_cache == NULL) {
2296 ret = -ENOMEM;
2297 goto error_cache;
2298 }
2299
2300 platform_set_drvdata(pdev, twl4030);
2301 twl4030_codec = codec;
2302
2303 /* Set the defaults, and power up the codec */
Peter Ujfalusi68d01952009-11-04 09:58:20 +02002304 twl4030->sysclk = twl4030_codec_get_mclk() / 1000;
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002305 twl4030_init_chip(codec);
Peter Ujfalusib3f5a272009-11-02 14:34:54 +02002306 codec->bias_level = SND_SOC_BIAS_OFF;
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002307 twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
2308
2309 ret = snd_soc_register_codec(codec);
2310 if (ret != 0) {
2311 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
2312 goto error_codec;
2313 }
2314
2315 ret = snd_soc_register_dais(&twl4030_dai[0], ARRAY_SIZE(twl4030_dai));
2316 if (ret != 0) {
2317 dev_err(codec->dev, "Failed to register DAIs: %d\n", ret);
2318 snd_soc_unregister_codec(codec);
2319 goto error_codec;
2320 }
2321
2322 return 0;
2323
2324error_codec:
2325 twl4030_power_down(codec);
2326 kfree(codec->reg_cache);
2327error_cache:
2328 kfree(twl4030);
2329 return ret;
2330}
2331
2332static int __devexit twl4030_codec_remove(struct platform_device *pdev)
2333{
2334 struct twl4030_priv *twl4030 = platform_get_drvdata(pdev);
2335
Peter Ujfalusicb672862010-02-04 09:10:10 +02002336 snd_soc_unregister_dais(&twl4030_dai[0], ARRAY_SIZE(twl4030_dai));
2337 snd_soc_unregister_codec(&twl4030->codec);
2338 kfree(twl4030->codec.reg_cache);
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002339 kfree(twl4030);
2340
2341 twl4030_codec = NULL;
2342 return 0;
2343}
2344
2345MODULE_ALIAS("platform:twl4030_codec_audio");
2346
2347static struct platform_driver twl4030_codec_driver = {
2348 .probe = twl4030_codec_probe,
2349 .remove = __devexit_p(twl4030_codec_remove),
2350 .driver = {
2351 .name = "twl4030_codec_audio",
2352 .owner = THIS_MODULE,
2353 },
Steve Sakomancc175572008-10-30 21:35:26 -07002354};
Steve Sakomancc175572008-10-30 21:35:26 -07002355
Takashi Iwai24e07db2008-12-10 07:40:24 +01002356static int __init twl4030_modinit(void)
Mark Brown64089b82008-12-08 19:17:58 +00002357{
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002358 return platform_driver_register(&twl4030_codec_driver);
Mark Brown64089b82008-12-08 19:17:58 +00002359}
Takashi Iwai24e07db2008-12-10 07:40:24 +01002360module_init(twl4030_modinit);
Mark Brown64089b82008-12-08 19:17:58 +00002361
2362static void __exit twl4030_exit(void)
2363{
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002364 platform_driver_unregister(&twl4030_codec_driver);
Mark Brown64089b82008-12-08 19:17:58 +00002365}
2366module_exit(twl4030_exit);
2367
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002368struct snd_soc_codec_device soc_codec_dev_twl4030 = {
2369 .probe = twl4030_soc_probe,
2370 .remove = twl4030_soc_remove,
2371 .suspend = twl4030_soc_suspend,
2372 .resume = twl4030_soc_resume,
2373};
2374EXPORT_SYMBOL_GPL(soc_codec_dev_twl4030);
2375
Steve Sakomancc175572008-10-30 21:35:26 -07002376MODULE_DESCRIPTION("ASoC TWL4030 codec driver");
2377MODULE_AUTHOR("Steve Sakoman");
2378MODULE_LICENSE("GPL");