blob: aa96788c8673807411de3cc863dc8a2433123122 [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>
Peter Ujfalusi281ecd12012-09-10 13:46:27 +030031#include <linux/gpio.h>
Steve Sakomancc175572008-10-30 21:35:26 -070032#include <sound/core.h>
33#include <sound/pcm.h>
34#include <sound/pcm_params.h>
35#include <sound/soc.h>
Steve Sakomancc175572008-10-30 21:35:26 -070036#include <sound/initval.h>
Peter Ujfalusic10b82c2008-11-24 13:49:35 +020037#include <sound/tlv.h>
Steve Sakomancc175572008-10-30 21:35:26 -070038
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000039/* Register descriptions are here */
Peter Ujfalusi57fe7252011-05-31 12:02:49 +030040#include <linux/mfd/twl4030-audio.h>
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +000041
42/* Shadow register used by the audio driver */
43#define TWL4030_REG_SW_SHADOW 0x4A
44#define TWL4030_CACHEREGNUM (TWL4030_REG_SW_SHADOW + 1)
45
46/* TWL4030_REG_SW_SHADOW (0x4A) Fields */
47#define TWL4030_HFL_EN 0x01
48#define TWL4030_HFR_EN 0x02
Steve Sakomancc175572008-10-30 21:35:26 -070049
50/*
51 * twl4030 register cache & default register settings
52 */
53static const u8 twl4030_reg[TWL4030_CACHEREGNUM] = {
54 0x00, /* this register not used */
Peter Ujfalusi33f92ed2010-05-26 11:38:14 +030055 0x00, /* REG_CODEC_MODE (0x1) */
Peter Ujfalusiee4ccac72010-05-26 11:38:17 +030056 0x00, /* REG_OPTION (0x2) */
Steve Sakomancc175572008-10-30 21:35:26 -070057 0x00, /* REG_UNKNOWN (0x3) */
58 0x00, /* REG_MICBIAS_CTL (0x4) */
Peter Ujfalusi979bb1f2010-05-26 11:38:16 +030059 0x00, /* REG_ANAMICL (0x5) */
Grazvydas Ignotas5920b452008-12-02 20:48:58 +020060 0x00, /* REG_ANAMICR (0x6) */
61 0x00, /* REG_AVADC_CTL (0x7) */
Steve Sakomancc175572008-10-30 21:35:26 -070062 0x00, /* REG_ADCMICSEL (0x8) */
63 0x00, /* REG_DIGMIXING (0x9) */
Peter Ujfalusi33f92ed2010-05-26 11:38:14 +030064 0x0f, /* REG_ATXL1PGA (0xA) */
65 0x0f, /* REG_ATXR1PGA (0xB) */
66 0x0f, /* REG_AVTXL2PGA (0xC) */
67 0x0f, /* REG_AVTXR2PGA (0xD) */
Peter Ujfalusic42a59e2010-02-09 15:24:04 +020068 0x00, /* REG_AUDIO_IF (0xE) */
Steve Sakomancc175572008-10-30 21:35:26 -070069 0x00, /* REG_VOICE_IF (0xF) */
Peter Ujfalusi33f92ed2010-05-26 11:38:14 +030070 0x3f, /* REG_ARXR1PGA (0x10) */
71 0x3f, /* REG_ARXL1PGA (0x11) */
72 0x3f, /* REG_ARXR2PGA (0x12) */
73 0x3f, /* REG_ARXL2PGA (0x13) */
74 0x25, /* REG_VRXPGA (0x14) */
Steve Sakomancc175572008-10-30 21:35:26 -070075 0x00, /* REG_VSTPGA (0x15) */
76 0x00, /* REG_VRX2ARXPGA (0x16) */
Peter Ujfalusic8124592010-01-28 15:57:04 +020077 0x00, /* REG_AVDAC_CTL (0x17) */
Steve Sakomancc175572008-10-30 21:35:26 -070078 0x00, /* REG_ARX2VTXPGA (0x18) */
Peter Ujfalusi33f92ed2010-05-26 11:38:14 +030079 0x32, /* REG_ARXL1_APGA_CTL (0x19) */
80 0x32, /* REG_ARXR1_APGA_CTL (0x1A) */
81 0x32, /* REG_ARXL2_APGA_CTL (0x1B) */
82 0x32, /* REG_ARXR2_APGA_CTL (0x1C) */
Steve Sakomancc175572008-10-30 21:35:26 -070083 0x00, /* REG_ATX2ARXPGA (0x1D) */
84 0x00, /* REG_BT_IF (0x1E) */
Peter Ujfalusi33f92ed2010-05-26 11:38:14 +030085 0x55, /* REG_BTPGA (0x1F) */
Steve Sakomancc175572008-10-30 21:35:26 -070086 0x00, /* REG_BTSTPGA (0x20) */
87 0x00, /* REG_EAR_CTL (0x21) */
Peter Ujfalusie47c7962010-02-17 09:49:54 +020088 0x00, /* REG_HS_SEL (0x22) */
89 0x00, /* REG_HS_GAIN_SET (0x23) */
Steve Sakomancc175572008-10-30 21:35:26 -070090 0x00, /* REG_HS_POPN_SET (0x24) */
91 0x00, /* REG_PREDL_CTL (0x25) */
92 0x00, /* REG_PREDR_CTL (0x26) */
93 0x00, /* REG_PRECKL_CTL (0x27) */
94 0x00, /* REG_PRECKR_CTL (0x28) */
95 0x00, /* REG_HFL_CTL (0x29) */
96 0x00, /* REG_HFR_CTL (0x2A) */
Peter Ujfalusi33f92ed2010-05-26 11:38:14 +030097 0x05, /* REG_ALC_CTL (0x2B) */
Steve Sakomancc175572008-10-30 21:35:26 -070098 0x00, /* REG_ALC_SET1 (0x2C) */
99 0x00, /* REG_ALC_SET2 (0x2D) */
100 0x00, /* REG_BOOST_CTL (0x2E) */
Peter Ujfalusif8d05bd2008-11-24 08:25:45 +0200101 0x00, /* REG_SOFTVOL_CTL (0x2F) */
Peter Ujfalusi33f92ed2010-05-26 11:38:14 +0300102 0x13, /* REG_DTMF_FREQSEL (0x30) */
Steve Sakomancc175572008-10-30 21:35:26 -0700103 0x00, /* REG_DTMF_TONEXT1H (0x31) */
104 0x00, /* REG_DTMF_TONEXT1L (0x32) */
105 0x00, /* REG_DTMF_TONEXT2H (0x33) */
106 0x00, /* REG_DTMF_TONEXT2L (0x34) */
Peter Ujfalusi33f92ed2010-05-26 11:38:14 +0300107 0x79, /* REG_DTMF_TONOFF (0x35) */
108 0x11, /* REG_DTMF_WANONOFF (0x36) */
Steve Sakomancc175572008-10-30 21:35:26 -0700109 0x00, /* REG_I2S_RX_SCRAMBLE_H (0x37) */
110 0x00, /* REG_I2S_RX_SCRAMBLE_M (0x38) */
111 0x00, /* REG_I2S_RX_SCRAMBLE_L (0x39) */
Peter Ujfalusic8124592010-01-28 15:57:04 +0200112 0x06, /* REG_APLL_CTL (0x3A) */
Steve Sakomancc175572008-10-30 21:35:26 -0700113 0x00, /* REG_DTMF_CTL (0x3B) */
Peter Ujfalusi33f92ed2010-05-26 11:38:14 +0300114 0x44, /* REG_DTMF_PGA_CTL2 (0x3C) */
115 0x69, /* REG_DTMF_PGA_CTL1 (0x3D) */
Steve Sakomancc175572008-10-30 21:35:26 -0700116 0x00, /* REG_MISC_SET_1 (0x3E) */
117 0x00, /* REG_PCMBTMUX (0x3F) */
118 0x00, /* not used (0x40) */
119 0x00, /* not used (0x41) */
120 0x00, /* not used (0x42) */
121 0x00, /* REG_RX_PATH_SEL (0x43) */
Peter Ujfalusi33f92ed2010-05-26 11:38:14 +0300122 0x32, /* REG_VDL_APGA_CTL (0x44) */
Steve Sakomancc175572008-10-30 21:35:26 -0700123 0x00, /* REG_VIBRA_CTL (0x45) */
124 0x00, /* REG_VIBRA_SET (0x46) */
125 0x00, /* REG_VIBRA_PWM_SET (0x47) */
126 0x00, /* REG_ANAMIC_GAIN (0x48) */
127 0x00, /* REG_MISC_SET_2 (0x49) */
Peter Ujfalusif3b5d302009-05-25 11:12:12 +0300128 0x00, /* REG_SW_SHADOW (0x4A) - Shadow, non HW register */
Steve Sakomancc175572008-10-30 21:35:26 -0700129};
130
Peter Ujfalusi73939582009-01-29 14:57:50 +0200131/* codec private data */
132struct twl4030_priv {
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300133 struct snd_soc_codec codec;
134
Peter Ujfalusi73939582009-01-29 14:57:50 +0200135 unsigned int codec_powered;
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +0300136
137 /* reference counts of AIF/APLL users */
Peter Ujfalusi2845fa12009-10-28 10:57:05 +0200138 unsigned int apll_enabled;
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +0200139
140 struct snd_pcm_substream *master_substream;
141 struct snd_pcm_substream *slave_substream;
Peter Ujfalusi6b87a912009-04-17 15:55:08 +0300142
143 unsigned int configured;
144 unsigned int rate;
145 unsigned int sample_bits;
146 unsigned int channels;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300147
148 unsigned int sysclk;
149
Peter Ujfalusic96907f2010-03-22 17:46:37 +0200150 /* Output (with associated amp) states */
151 u8 hsl_enabled, hsr_enabled;
152 u8 earpiece_enabled;
153 u8 predrivel_enabled, predriver_enabled;
154 u8 carkitl_enabled, carkitr_enabled;
Peter Ujfalusi01ea6ba2010-07-20 15:49:09 +0300155
156 /* Delay needed after enabling the digimic interface */
157 unsigned int digimic_delay;
Peter Ujfalusi73939582009-01-29 14:57:50 +0200158};
159
Steve Sakomancc175572008-10-30 21:35:26 -0700160/*
161 * read twl4030 register cache
162 */
163static inline unsigned int twl4030_read_reg_cache(struct snd_soc_codec *codec,
164 unsigned int reg)
165{
Takashi Iwaid08664f2009-06-04 09:58:18 +0200166 u8 *cache = codec->reg_cache;
Steve Sakomancc175572008-10-30 21:35:26 -0700167
Ian Molton91432e92009-01-17 17:44:23 +0000168 if (reg >= TWL4030_CACHEREGNUM)
169 return -EIO;
170
Steve Sakomancc175572008-10-30 21:35:26 -0700171 return cache[reg];
172}
173
174/*
175 * write twl4030 register cache
176 */
177static inline void twl4030_write_reg_cache(struct snd_soc_codec *codec,
178 u8 reg, u8 value)
179{
180 u8 *cache = codec->reg_cache;
181
182 if (reg >= TWL4030_CACHEREGNUM)
183 return;
184 cache[reg] = value;
185}
186
187/*
188 * write to the twl4030 register space
189 */
190static int twl4030_write(struct snd_soc_codec *codec,
191 unsigned int reg, unsigned int value)
192{
Mark Brownb2c812e2010-04-14 15:35:19 +0900193 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusic96907f2010-03-22 17:46:37 +0200194 int write_to_reg = 0;
195
Steve Sakomancc175572008-10-30 21:35:26 -0700196 twl4030_write_reg_cache(codec, reg, value);
Peter Ujfalusic96907f2010-03-22 17:46:37 +0200197 if (likely(reg < TWL4030_REG_SW_SHADOW)) {
198 /* Decide if the given register can be written */
199 switch (reg) {
200 case TWL4030_REG_EAR_CTL:
201 if (twl4030->earpiece_enabled)
202 write_to_reg = 1;
203 break;
204 case TWL4030_REG_PREDL_CTL:
205 if (twl4030->predrivel_enabled)
206 write_to_reg = 1;
207 break;
208 case TWL4030_REG_PREDR_CTL:
209 if (twl4030->predriver_enabled)
210 write_to_reg = 1;
211 break;
212 case TWL4030_REG_PRECKL_CTL:
213 if (twl4030->carkitl_enabled)
214 write_to_reg = 1;
215 break;
216 case TWL4030_REG_PRECKR_CTL:
217 if (twl4030->carkitr_enabled)
218 write_to_reg = 1;
219 break;
220 case TWL4030_REG_HS_GAIN_SET:
221 if (twl4030->hsl_enabled || twl4030->hsr_enabled)
222 write_to_reg = 1;
223 break;
224 default:
225 /* All other register can be written */
226 write_to_reg = 1;
227 break;
228 }
229 if (write_to_reg)
230 return twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
231 value, reg);
232 }
233 return 0;
Steve Sakomancc175572008-10-30 21:35:26 -0700234}
235
Peter Ujfalusi7e6120c2010-10-25 11:34:23 +0300236static inline void twl4030_wait_ms(int time)
237{
238 if (time < 60) {
239 time *= 1000;
240 usleep_range(time, time + 500);
241 } else {
242 msleep(time);
243 }
244}
245
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +0200246static void twl4030_codec_enable(struct snd_soc_codec *codec, int enable)
Steve Sakomancc175572008-10-30 21:35:26 -0700247{
Mark Brownb2c812e2010-04-14 15:35:19 +0900248 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300249 int mode;
Steve Sakomancc175572008-10-30 21:35:26 -0700250
Peter Ujfalusi73939582009-01-29 14:57:50 +0200251 if (enable == twl4030->codec_powered)
252 return;
253
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +0200254 if (enable)
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300255 mode = twl4030_audio_enable_resource(TWL4030_AUDIO_RES_POWER);
Peter Ujfalusidb04e2c2009-01-27 11:29:40 +0200256 else
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300257 mode = twl4030_audio_disable_resource(TWL4030_AUDIO_RES_POWER);
Steve Sakomancc175572008-10-30 21:35:26 -0700258
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300259 if (mode >= 0) {
260 twl4030_write_reg_cache(codec, TWL4030_REG_CODEC_MODE, mode);
261 twl4030->codec_powered = enable;
262 }
Steve Sakomancc175572008-10-30 21:35:26 -0700263
264 /* REVISIT: this delay is present in TI sample drivers */
265 /* but there seems to be no TRM requirement for it */
266 udelay(10);
267}
268
Peter Ujfalusi9fdcc0f2010-05-26 11:38:18 +0300269static inline void twl4030_check_defaults(struct snd_soc_codec *codec)
Steve Sakomancc175572008-10-30 21:35:26 -0700270{
Peter Ujfalusi9fdcc0f2010-05-26 11:38:18 +0300271 int i, difference = 0;
272 u8 val;
Steve Sakomancc175572008-10-30 21:35:26 -0700273
Peter Ujfalusi9fdcc0f2010-05-26 11:38:18 +0300274 dev_dbg(codec->dev, "Checking TWL audio default configuration\n");
275 for (i = 1; i <= TWL4030_REG_MISC_SET_2; i++) {
276 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &val, i);
277 if (val != twl4030_reg[i]) {
278 difference++;
279 dev_dbg(codec->dev,
280 "Reg 0x%02x: chip: 0x%02x driver: 0x%02x\n",
281 i, val, twl4030_reg[i]);
282 }
283 }
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300284 dev_dbg(codec->dev, "Found %d non-matching registers. %s\n",
Peter Ujfalusi9fdcc0f2010-05-26 11:38:18 +0300285 difference, difference ? "Not OK" : "OK");
286}
287
Peter Ujfalusia3a29b52010-05-26 11:38:21 +0300288static inline void twl4030_reset_registers(struct snd_soc_codec *codec)
289{
290 int i;
Steve Sakomancc175572008-10-30 21:35:26 -0700291
292 /* set all audio section registers to reasonable defaults */
293 for (i = TWL4030_REG_OPTION; i <= TWL4030_REG_MISC_SET_2; i++)
Peter Ujfalusi68d01952009-11-04 09:58:20 +0200294 if (i != TWL4030_REG_APLL_CTL)
Peter Ujfalusia3a29b52010-05-26 11:38:21 +0300295 twl4030_write(codec, i, twl4030_reg[i]);
Steve Sakomancc175572008-10-30 21:35:26 -0700296
297}
298
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000299static void twl4030_init_chip(struct snd_soc_codec *codec)
Steve Sakomancc175572008-10-30 21:35:26 -0700300{
Peter Ujfalusi4ae6df5e2011-05-31 15:21:13 +0300301 struct twl4030_codec_data *pdata = dev_get_platdata(codec->dev);
Peter Ujfalusiee4ccac72010-05-26 11:38:17 +0300302 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
303 u8 reg, byte;
304 int i = 0;
Steve Sakomancc175572008-10-30 21:35:26 -0700305
Peter Ujfalusi281ecd12012-09-10 13:46:27 +0300306 if (pdata && pdata->hs_extmute &&
307 gpio_is_valid(pdata->hs_extmute_gpio)) {
308 int ret;
309
310 if (!pdata->hs_extmute_gpio)
311 dev_warn(codec->dev,
312 "Extmute GPIO is 0 is this correct?\n");
313
314 ret = gpio_request_one(pdata->hs_extmute_gpio,
315 GPIOF_OUT_INIT_LOW, "hs_extmute");
316 if (ret) {
317 dev_err(codec->dev, "Failed to get hs_extmute GPIO\n");
318 pdata->hs_extmute_gpio = -1;
319 }
320 }
321
Peter Ujfalusi9fdcc0f2010-05-26 11:38:18 +0300322 /* Check defaults, if instructed before anything else */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000323 if (pdata && pdata->check_defaults)
Peter Ujfalusi9fdcc0f2010-05-26 11:38:18 +0300324 twl4030_check_defaults(codec);
325
Peter Ujfalusia3a29b52010-05-26 11:38:21 +0300326 /* Reset registers, if no setup data or if instructed to do so */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000327 if (!pdata || (pdata && pdata->reset_registers))
Peter Ujfalusia3a29b52010-05-26 11:38:21 +0300328 twl4030_reset_registers(codec);
329
Peter Ujfalusiee4ccac72010-05-26 11:38:17 +0300330 /* Refresh APLL_CTL register from HW */
Peter Ujfalusi9fdcc0f2010-05-26 11:38:18 +0300331 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &byte,
Peter Ujfalusiee4ccac72010-05-26 11:38:17 +0300332 TWL4030_REG_APLL_CTL);
333 twl4030_write_reg_cache(codec, TWL4030_REG_APLL_CTL, byte);
334
335 /* anti-pop when changing analog gain */
336 reg = twl4030_read_reg_cache(codec, TWL4030_REG_MISC_SET_1);
337 twl4030_write(codec, TWL4030_REG_MISC_SET_1,
338 reg | TWL4030_SMOOTH_ANAVOL_EN);
339
340 twl4030_write(codec, TWL4030_REG_OPTION,
341 TWL4030_ATXL1_EN | TWL4030_ATXR1_EN |
342 TWL4030_ARXL2_EN | TWL4030_ARXR2_EN);
343
Peter Ujfalusi3c36cc62010-05-26 11:38:19 +0300344 /* REG_ARXR2_APGA_CTL reset according to the TRM: 0dB, DA_EN */
345 twl4030_write(codec, TWL4030_REG_ARXR2_APGA_CTL, 0x32);
346
Peter Ujfalusiee4ccac72010-05-26 11:38:17 +0300347 /* Machine dependent setup */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000348 if (!pdata)
Peter Ujfalusiee4ccac72010-05-26 11:38:17 +0300349 return;
350
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000351 twl4030->digimic_delay = pdata->digimic_delay;
Peter Ujfalusiee4ccac72010-05-26 11:38:17 +0300352
353 reg = twl4030_read_reg_cache(codec, TWL4030_REG_HS_POPN_SET);
354 reg &= ~TWL4030_RAMP_DELAY;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000355 reg |= (pdata->ramp_delay_value << 2);
Peter Ujfalusiee4ccac72010-05-26 11:38:17 +0300356 twl4030_write_reg_cache(codec, TWL4030_REG_HS_POPN_SET, reg);
357
358 /* initiate offset cancellation */
359 twl4030_codec_enable(codec, 1);
360
361 reg = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICL);
362 reg &= ~TWL4030_OFFSET_CNCL_SEL;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000363 reg |= pdata->offset_cncl_path;
Peter Ujfalusiee4ccac72010-05-26 11:38:17 +0300364 twl4030_write(codec, TWL4030_REG_ANAMICL,
365 reg | TWL4030_CNCL_OFFSET_START);
366
Peter Ujfalusi7e6120c2010-10-25 11:34:23 +0300367 /*
368 * Wait for offset cancellation to complete.
369 * Since this takes a while, do not slam the i2c.
370 * Start polling the status after ~20ms.
371 */
372 msleep(20);
Peter Ujfalusiee4ccac72010-05-26 11:38:17 +0300373 do {
Peter Ujfalusi7e6120c2010-10-25 11:34:23 +0300374 usleep_range(1000, 2000);
Peter Ujfalusiee4ccac72010-05-26 11:38:17 +0300375 twl_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &byte,
376 TWL4030_REG_ANAMICL);
377 } while ((i++ < 100) &&
378 ((byte & TWL4030_CNCL_OFFSET_START) ==
379 TWL4030_CNCL_OFFSET_START));
380
381 /* Make sure that the reg_cache has the same value as the HW */
382 twl4030_write_reg_cache(codec, TWL4030_REG_ANAMICL, byte);
383
Steve Sakomancc175572008-10-30 21:35:26 -0700384 twl4030_codec_enable(codec, 0);
Steve Sakomancc175572008-10-30 21:35:26 -0700385}
386
Peter Ujfalusi2845fa12009-10-28 10:57:05 +0200387static void twl4030_apll_enable(struct snd_soc_codec *codec, int enable)
Peter Ujfalusi73939582009-01-29 14:57:50 +0200388{
Mark Brownb2c812e2010-04-14 15:35:19 +0900389 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +0300390 int status = -1;
Peter Ujfalusi73939582009-01-29 14:57:50 +0200391
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +0300392 if (enable) {
393 twl4030->apll_enabled++;
394 if (twl4030->apll_enabled == 1)
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300395 status = twl4030_audio_enable_resource(
396 TWL4030_AUDIO_RES_APLL);
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +0300397 } else {
398 twl4030->apll_enabled--;
399 if (!twl4030->apll_enabled)
Peter Ujfalusi57fe7252011-05-31 12:02:49 +0300400 status = twl4030_audio_disable_resource(
401 TWL4030_AUDIO_RES_APLL);
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +0300402 }
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +0300403
404 if (status >= 0)
405 twl4030_write_reg_cache(codec, TWL4030_REG_APLL_CTL, status);
Peter Ujfalusi73939582009-01-29 14:57:50 +0200406}
407
Peter Ujfalusi5e98a462008-12-09 12:35:47 +0200408/* Earpiece */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900409static const struct snd_kcontrol_new twl4030_dapm_earpiece_controls[] = {
410 SOC_DAPM_SINGLE("Voice", TWL4030_REG_EAR_CTL, 0, 1, 0),
411 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_EAR_CTL, 1, 1, 0),
412 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_EAR_CTL, 2, 1, 0),
413 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_EAR_CTL, 3, 1, 0),
414};
Peter Ujfalusi5e98a462008-12-09 12:35:47 +0200415
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +0200416/* PreDrive Left */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900417static const struct snd_kcontrol_new twl4030_dapm_predrivel_controls[] = {
418 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PREDL_CTL, 0, 1, 0),
419 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_PREDL_CTL, 1, 1, 0),
420 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PREDL_CTL, 2, 1, 0),
421 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PREDL_CTL, 3, 1, 0),
422};
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +0200423
424/* PreDrive Right */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900425static const struct snd_kcontrol_new twl4030_dapm_predriver_controls[] = {
426 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PREDR_CTL, 0, 1, 0),
427 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_PREDR_CTL, 1, 1, 0),
428 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PREDR_CTL, 2, 1, 0),
429 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PREDR_CTL, 3, 1, 0),
430};
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +0200431
Peter Ujfalusidfad21a2008-12-09 12:35:49 +0200432/* Headset Left */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900433static const struct snd_kcontrol_new twl4030_dapm_hsol_controls[] = {
434 SOC_DAPM_SINGLE("Voice", TWL4030_REG_HS_SEL, 0, 1, 0),
435 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_HS_SEL, 1, 1, 0),
436 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_HS_SEL, 2, 1, 0),
437};
Peter Ujfalusidfad21a2008-12-09 12:35:49 +0200438
439/* Headset Right */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900440static const struct snd_kcontrol_new twl4030_dapm_hsor_controls[] = {
441 SOC_DAPM_SINGLE("Voice", TWL4030_REG_HS_SEL, 3, 1, 0),
442 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_HS_SEL, 4, 1, 0),
443 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_HS_SEL, 5, 1, 0),
444};
Peter Ujfalusidfad21a2008-12-09 12:35:49 +0200445
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +0200446/* Carkit Left */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900447static const struct snd_kcontrol_new twl4030_dapm_carkitl_controls[] = {
448 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PRECKL_CTL, 0, 1, 0),
449 SOC_DAPM_SINGLE("AudioL1", TWL4030_REG_PRECKL_CTL, 1, 1, 0),
450 SOC_DAPM_SINGLE("AudioL2", TWL4030_REG_PRECKL_CTL, 2, 1, 0),
451};
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +0200452
453/* Carkit Right */
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900454static const struct snd_kcontrol_new twl4030_dapm_carkitr_controls[] = {
455 SOC_DAPM_SINGLE("Voice", TWL4030_REG_PRECKR_CTL, 0, 1, 0),
456 SOC_DAPM_SINGLE("AudioR1", TWL4030_REG_PRECKR_CTL, 1, 1, 0),
457 SOC_DAPM_SINGLE("AudioR2", TWL4030_REG_PRECKR_CTL, 2, 1, 0),
458};
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +0200459
Peter Ujfalusidf339802008-12-09 12:35:51 +0200460/* Handsfree Left */
461static const char *twl4030_handsfreel_texts[] =
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900462 {"Voice", "AudioL1", "AudioL2", "AudioR2"};
Peter Ujfalusidf339802008-12-09 12:35:51 +0200463
464static const struct soc_enum twl4030_handsfreel_enum =
465 SOC_ENUM_SINGLE(TWL4030_REG_HFL_CTL, 0,
466 ARRAY_SIZE(twl4030_handsfreel_texts),
467 twl4030_handsfreel_texts);
468
469static const struct snd_kcontrol_new twl4030_dapm_handsfreel_control =
470SOC_DAPM_ENUM("Route", twl4030_handsfreel_enum);
471
Peter Ujfalusi0f89bdc2009-05-25 11:12:13 +0300472/* Handsfree Left virtual mute */
473static const struct snd_kcontrol_new twl4030_dapm_handsfreelmute_control =
474 SOC_DAPM_SINGLE("Switch", TWL4030_REG_SW_SHADOW, 0, 1, 0);
475
Peter Ujfalusidf339802008-12-09 12:35:51 +0200476/* Handsfree Right */
477static const char *twl4030_handsfreer_texts[] =
Joonyoung Shim1a787e72009-04-22 13:13:34 +0900478 {"Voice", "AudioR1", "AudioR2", "AudioL2"};
Peter Ujfalusidf339802008-12-09 12:35:51 +0200479
480static const struct soc_enum twl4030_handsfreer_enum =
481 SOC_ENUM_SINGLE(TWL4030_REG_HFR_CTL, 0,
482 ARRAY_SIZE(twl4030_handsfreer_texts),
483 twl4030_handsfreer_texts);
484
485static const struct snd_kcontrol_new twl4030_dapm_handsfreer_control =
486SOC_DAPM_ENUM("Route", twl4030_handsfreer_enum);
487
Peter Ujfalusi0f89bdc2009-05-25 11:12:13 +0300488/* Handsfree Right virtual mute */
489static const struct snd_kcontrol_new twl4030_dapm_handsfreermute_control =
490 SOC_DAPM_SINGLE("Switch", TWL4030_REG_SW_SHADOW, 1, 1, 0);
491
Peter Ujfalusi376f7832009-05-05 08:55:47 +0300492/* Vibra */
493/* Vibra audio path selection */
494static const char *twl4030_vibra_texts[] =
495 {"AudioL1", "AudioR1", "AudioL2", "AudioR2"};
496
497static const struct soc_enum twl4030_vibra_enum =
498 SOC_ENUM_SINGLE(TWL4030_REG_VIBRA_CTL, 2,
499 ARRAY_SIZE(twl4030_vibra_texts),
500 twl4030_vibra_texts);
501
502static const struct snd_kcontrol_new twl4030_dapm_vibra_control =
503SOC_DAPM_ENUM("Route", twl4030_vibra_enum);
504
505/* Vibra path selection: local vibrator (PWM) or audio driven */
506static const char *twl4030_vibrapath_texts[] =
507 {"Local vibrator", "Audio"};
508
509static const struct soc_enum twl4030_vibrapath_enum =
510 SOC_ENUM_SINGLE(TWL4030_REG_VIBRA_CTL, 4,
511 ARRAY_SIZE(twl4030_vibrapath_texts),
512 twl4030_vibrapath_texts);
513
514static const struct snd_kcontrol_new twl4030_dapm_vibrapath_control =
515SOC_DAPM_ENUM("Route", twl4030_vibrapath_enum);
516
Peter Ujfalusi276c6222008-12-31 10:08:38 +0200517/* Left analog microphone selection */
Joonyoung Shim97b80962009-05-11 20:36:08 +0900518static const struct snd_kcontrol_new twl4030_dapm_analoglmic_controls[] = {
Peter Ujfalusi90289352009-08-14 08:44:00 +0300519 SOC_DAPM_SINGLE("Main Mic Capture Switch",
520 TWL4030_REG_ANAMICL, 0, 1, 0),
521 SOC_DAPM_SINGLE("Headset Mic Capture Switch",
522 TWL4030_REG_ANAMICL, 1, 1, 0),
523 SOC_DAPM_SINGLE("AUXL Capture Switch",
524 TWL4030_REG_ANAMICL, 2, 1, 0),
525 SOC_DAPM_SINGLE("Carkit Mic Capture Switch",
526 TWL4030_REG_ANAMICL, 3, 1, 0),
Joonyoung Shim97b80962009-05-11 20:36:08 +0900527};
Peter Ujfalusi276c6222008-12-31 10:08:38 +0200528
529/* Right analog microphone selection */
Joonyoung Shim97b80962009-05-11 20:36:08 +0900530static const struct snd_kcontrol_new twl4030_dapm_analogrmic_controls[] = {
Peter Ujfalusi90289352009-08-14 08:44:00 +0300531 SOC_DAPM_SINGLE("Sub Mic Capture Switch", TWL4030_REG_ANAMICR, 0, 1, 0),
532 SOC_DAPM_SINGLE("AUXR Capture Switch", TWL4030_REG_ANAMICR, 2, 1, 0),
Joonyoung Shim97b80962009-05-11 20:36:08 +0900533};
Peter Ujfalusi276c6222008-12-31 10:08:38 +0200534
535/* TX1 L/R Analog/Digital microphone selection */
536static const char *twl4030_micpathtx1_texts[] =
537 {"Analog", "Digimic0"};
538
539static const struct soc_enum twl4030_micpathtx1_enum =
540 SOC_ENUM_SINGLE(TWL4030_REG_ADCMICSEL, 0,
541 ARRAY_SIZE(twl4030_micpathtx1_texts),
542 twl4030_micpathtx1_texts);
543
544static const struct snd_kcontrol_new twl4030_dapm_micpathtx1_control =
545SOC_DAPM_ENUM("Route", twl4030_micpathtx1_enum);
546
547/* TX2 L/R Analog/Digital microphone selection */
548static const char *twl4030_micpathtx2_texts[] =
549 {"Analog", "Digimic1"};
550
551static const struct soc_enum twl4030_micpathtx2_enum =
552 SOC_ENUM_SINGLE(TWL4030_REG_ADCMICSEL, 2,
553 ARRAY_SIZE(twl4030_micpathtx2_texts),
554 twl4030_micpathtx2_texts);
555
556static const struct snd_kcontrol_new twl4030_dapm_micpathtx2_control =
557SOC_DAPM_ENUM("Route", twl4030_micpathtx2_enum);
558
Peter Ujfalusi73939582009-01-29 14:57:50 +0200559/* Analog bypass for AudioR1 */
560static const struct snd_kcontrol_new twl4030_dapm_abypassr1_control =
561 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXR1_APGA_CTL, 2, 1, 0);
562
563/* Analog bypass for AudioL1 */
564static const struct snd_kcontrol_new twl4030_dapm_abypassl1_control =
565 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXL1_APGA_CTL, 2, 1, 0);
566
567/* Analog bypass for AudioR2 */
568static const struct snd_kcontrol_new twl4030_dapm_abypassr2_control =
569 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXR2_APGA_CTL, 2, 1, 0);
570
571/* Analog bypass for AudioL2 */
572static const struct snd_kcontrol_new twl4030_dapm_abypassl2_control =
573 SOC_DAPM_SINGLE("Switch", TWL4030_REG_ARXL2_APGA_CTL, 2, 1, 0);
574
Lopez Cruz, Misaelfcd274a2009-04-30 21:47:22 -0500575/* Analog bypass for Voice */
576static const struct snd_kcontrol_new twl4030_dapm_abypassv_control =
577 SOC_DAPM_SINGLE("Switch", TWL4030_REG_VDL_APGA_CTL, 2, 1, 0);
578
Peter Ujfalusi8b0d3152010-07-12 11:50:06 +0300579/* Digital bypass gain, mute instead of -30dB */
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +0200580static const unsigned int twl4030_dapm_dbypass_tlv[] = {
Peter Ujfalusi8b0d3152010-07-12 11:50:06 +0300581 TLV_DB_RANGE_HEAD(3),
582 0, 1, TLV_DB_SCALE_ITEM(-3000, 600, 1),
583 2, 3, TLV_DB_SCALE_ITEM(-2400, 0, 0),
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +0200584 4, 7, TLV_DB_SCALE_ITEM(-1800, 600, 0),
585};
586
587/* Digital bypass left (TX1L -> RX2L) */
588static const struct snd_kcontrol_new twl4030_dapm_dbypassl_control =
589 SOC_DAPM_SINGLE_TLV("Volume",
590 TWL4030_REG_ATX2ARXPGA, 3, 7, 0,
591 twl4030_dapm_dbypass_tlv);
592
593/* Digital bypass right (TX1R -> RX2R) */
594static const struct snd_kcontrol_new twl4030_dapm_dbypassr_control =
595 SOC_DAPM_SINGLE_TLV("Volume",
596 TWL4030_REG_ATX2ARXPGA, 0, 7, 0,
597 twl4030_dapm_dbypass_tlv);
598
Lopez Cruz, Misaelee8f6892009-04-30 21:48:08 -0500599/*
600 * Voice Sidetone GAIN volume control:
601 * from -51 to -10 dB in 1 dB steps (mute instead of -51 dB)
602 */
603static DECLARE_TLV_DB_SCALE(twl4030_dapm_dbypassv_tlv, -5100, 100, 1);
604
605/* Digital bypass voice: sidetone (VUL -> VDL)*/
606static const struct snd_kcontrol_new twl4030_dapm_dbypassv_control =
607 SOC_DAPM_SINGLE_TLV("Volume",
608 TWL4030_REG_VSTPGA, 0, 0x29, 0,
609 twl4030_dapm_dbypassv_tlv);
610
Peter Ujfalusi9008adf2009-08-13 15:59:34 +0300611/*
612 * Output PGA builder:
613 * Handle the muting and unmuting of the given output (turning off the
614 * amplifier associated with the output pin)
Peter Ujfalusic96907f2010-03-22 17:46:37 +0200615 * On mute bypass the reg_cache and write 0 to the register
616 * On unmute: restore the register content from the reg_cache
Peter Ujfalusi9008adf2009-08-13 15:59:34 +0300617 * Outputs handled in this way: Earpiece, PreDrivL/R, CarkitL/R
618 */
619#define TWL4030_OUTPUT_PGA(pin_name, reg, mask) \
620static int pin_name##pga_event(struct snd_soc_dapm_widget *w, \
621 struct snd_kcontrol *kcontrol, int event) \
622{ \
Mark Brownb2c812e2010-04-14 15:35:19 +0900623 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(w->codec); \
Peter Ujfalusi9008adf2009-08-13 15:59:34 +0300624 \
625 switch (event) { \
626 case SND_SOC_DAPM_POST_PMU: \
Peter Ujfalusic96907f2010-03-22 17:46:37 +0200627 twl4030->pin_name##_enabled = 1; \
Peter Ujfalusi9008adf2009-08-13 15:59:34 +0300628 twl4030_write(w->codec, reg, \
629 twl4030_read_reg_cache(w->codec, reg)); \
630 break; \
631 case SND_SOC_DAPM_POST_PMD: \
Peter Ujfalusic96907f2010-03-22 17:46:37 +0200632 twl4030->pin_name##_enabled = 0; \
633 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, \
634 0, reg); \
Peter Ujfalusi9008adf2009-08-13 15:59:34 +0300635 break; \
636 } \
637 return 0; \
638}
639
640TWL4030_OUTPUT_PGA(earpiece, TWL4030_REG_EAR_CTL, TWL4030_EAR_GAIN);
641TWL4030_OUTPUT_PGA(predrivel, TWL4030_REG_PREDL_CTL, TWL4030_PREDL_GAIN);
642TWL4030_OUTPUT_PGA(predriver, TWL4030_REG_PREDR_CTL, TWL4030_PREDR_GAIN);
643TWL4030_OUTPUT_PGA(carkitl, TWL4030_REG_PRECKL_CTL, TWL4030_PRECKL_GAIN);
644TWL4030_OUTPUT_PGA(carkitr, TWL4030_REG_PRECKR_CTL, TWL4030_PRECKR_GAIN);
645
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300646static void handsfree_ramp(struct snd_soc_codec *codec, int reg, int ramp)
Stanley.Miao49d92c72008-12-11 23:28:10 +0800647{
Stanley.Miao49d92c72008-12-11 23:28:10 +0800648 unsigned char hs_ctl;
649
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300650 hs_ctl = twl4030_read_reg_cache(codec, reg);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800651
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300652 if (ramp) {
653 /* HF ramp-up */
654 hs_ctl |= TWL4030_HF_CTL_REF_EN;
655 twl4030_write(codec, reg, hs_ctl);
656 udelay(10);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800657 hs_ctl |= TWL4030_HF_CTL_RAMP_EN;
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300658 twl4030_write(codec, reg, hs_ctl);
659 udelay(40);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800660 hs_ctl |= TWL4030_HF_CTL_LOOP_EN;
Stanley.Miao49d92c72008-12-11 23:28:10 +0800661 hs_ctl |= TWL4030_HF_CTL_HB_EN;
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300662 twl4030_write(codec, reg, hs_ctl);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800663 } else {
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300664 /* HF ramp-down */
665 hs_ctl &= ~TWL4030_HF_CTL_LOOP_EN;
666 hs_ctl &= ~TWL4030_HF_CTL_HB_EN;
667 twl4030_write(codec, reg, hs_ctl);
668 hs_ctl &= ~TWL4030_HF_CTL_RAMP_EN;
669 twl4030_write(codec, reg, hs_ctl);
670 udelay(40);
671 hs_ctl &= ~TWL4030_HF_CTL_REF_EN;
672 twl4030_write(codec, reg, hs_ctl);
Stanley.Miao49d92c72008-12-11 23:28:10 +0800673 }
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300674}
Stanley.Miao49d92c72008-12-11 23:28:10 +0800675
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +0300676static int handsfreelpga_event(struct snd_soc_dapm_widget *w,
677 struct snd_kcontrol *kcontrol, int event)
678{
679 switch (event) {
680 case SND_SOC_DAPM_POST_PMU:
681 handsfree_ramp(w->codec, TWL4030_REG_HFL_CTL, 1);
682 break;
683 case SND_SOC_DAPM_POST_PMD:
684 handsfree_ramp(w->codec, TWL4030_REG_HFL_CTL, 0);
685 break;
686 }
687 return 0;
688}
689
690static int handsfreerpga_event(struct snd_soc_dapm_widget *w,
691 struct snd_kcontrol *kcontrol, int event)
692{
693 switch (event) {
694 case SND_SOC_DAPM_POST_PMU:
695 handsfree_ramp(w->codec, TWL4030_REG_HFR_CTL, 1);
696 break;
697 case SND_SOC_DAPM_POST_PMD:
698 handsfree_ramp(w->codec, TWL4030_REG_HFR_CTL, 0);
699 break;
700 }
Stanley.Miao49d92c72008-12-11 23:28:10 +0800701 return 0;
702}
703
Jari Vanhala86139a12009-10-29 11:58:09 +0200704static int vibramux_event(struct snd_soc_dapm_widget *w,
705 struct snd_kcontrol *kcontrol, int event)
706{
707 twl4030_write(w->codec, TWL4030_REG_VIBRA_SET, 0xff);
708 return 0;
709}
710
Peter Ujfalusi7729cf72009-10-29 11:58:10 +0200711static int apll_event(struct snd_soc_dapm_widget *w,
712 struct snd_kcontrol *kcontrol, int event)
713{
714 switch (event) {
715 case SND_SOC_DAPM_PRE_PMU:
716 twl4030_apll_enable(w->codec, 1);
717 break;
718 case SND_SOC_DAPM_POST_PMD:
719 twl4030_apll_enable(w->codec, 0);
720 break;
721 }
722 return 0;
723}
724
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +0300725static int aif_event(struct snd_soc_dapm_widget *w,
726 struct snd_kcontrol *kcontrol, int event)
727{
728 u8 audio_if;
729
730 audio_if = twl4030_read_reg_cache(w->codec, TWL4030_REG_AUDIO_IF);
731 switch (event) {
732 case SND_SOC_DAPM_PRE_PMU:
733 /* Enable AIF */
734 /* enable the PLL before we use it to clock the DAI */
735 twl4030_apll_enable(w->codec, 1);
736
737 twl4030_write(w->codec, TWL4030_REG_AUDIO_IF,
738 audio_if | TWL4030_AIF_EN);
739 break;
740 case SND_SOC_DAPM_POST_PMD:
741 /* disable the DAI before we stop it's source PLL */
742 twl4030_write(w->codec, TWL4030_REG_AUDIO_IF,
743 audio_if & ~TWL4030_AIF_EN);
744 twl4030_apll_enable(w->codec, 0);
745 break;
746 }
747 return 0;
748}
749
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300750static void headset_ramp(struct snd_soc_codec *codec, int ramp)
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200751{
Peter Ujfalusi4ae6df5e2011-05-31 15:21:13 +0300752 struct twl4030_codec_data *pdata = codec->dev->platform_data;
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200753 unsigned char hs_gain, hs_pop;
Mark Brownb2c812e2010-04-14 15:35:19 +0900754 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300755 /* Base values for ramp delay calculation: 2^19 - 2^26 */
756 unsigned int ramp_base[] = {524288, 1048576, 2097152, 4194304,
757 8388608, 16777216, 33554432, 67108864};
Peter Ujfalusi7e6120c2010-10-25 11:34:23 +0300758 unsigned int delay;
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200759
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300760 hs_gain = twl4030_read_reg_cache(codec, TWL4030_REG_HS_GAIN_SET);
761 hs_pop = twl4030_read_reg_cache(codec, TWL4030_REG_HS_POPN_SET);
Peter Ujfalusi7e6120c2010-10-25 11:34:23 +0300762 delay = (ramp_base[(hs_pop & TWL4030_RAMP_DELAY) >> 2] /
763 twl4030->sysclk) + 1;
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200764
Candelaria Villareal, Jorge4e49ffd2009-07-01 19:17:43 -0500765 /* Enable external mute control, this dramatically reduces
766 * the pop-noise */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000767 if (pdata && pdata->hs_extmute) {
Peter Ujfalusi281ecd12012-09-10 13:46:27 +0300768 if (gpio_is_valid(pdata->hs_extmute_gpio)) {
769 gpio_set_value(pdata->hs_extmute_gpio, 1);
Candelaria Villareal, Jorge4e49ffd2009-07-01 19:17:43 -0500770 } else {
771 hs_pop |= TWL4030_EXTMUTE;
772 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
773 }
774 }
775
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300776 if (ramp) {
777 /* Headset ramp-up according to the TRM */
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200778 hs_pop |= TWL4030_VMID_EN;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300779 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
Peter Ujfalusic96907f2010-03-22 17:46:37 +0200780 /* Actually write to the register */
781 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
782 hs_gain,
783 TWL4030_REG_HS_GAIN_SET);
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200784 hs_pop |= TWL4030_RAMP_EN;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300785 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
Candelaria Villareal, Jorge4e49ffd2009-07-01 19:17:43 -0500786 /* Wait ramp delay time + 1, so the VMID can settle */
Peter Ujfalusi7e6120c2010-10-25 11:34:23 +0300787 twl4030_wait_ms(delay);
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300788 } else {
789 /* Headset ramp-down _not_ according to
790 * the TRM, but in a way that it is working */
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200791 hs_pop &= ~TWL4030_RAMP_EN;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300792 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
793 /* Wait ramp delay time + 1, so the VMID can settle */
Peter Ujfalusi7e6120c2010-10-25 11:34:23 +0300794 twl4030_wait_ms(delay);
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200795 /* Bypass the reg_cache to mute the headset */
Balaji T Kfc7b92f2009-12-13 21:23:33 +0100796 twl_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE,
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200797 hs_gain & (~0x0f),
798 TWL4030_REG_HS_GAIN_SET);
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300799
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200800 hs_pop &= ~TWL4030_VMID_EN;
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300801 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
802 }
Candelaria Villareal, Jorge4e49ffd2009-07-01 19:17:43 -0500803
804 /* Disable external mute */
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +0000805 if (pdata && pdata->hs_extmute) {
Peter Ujfalusi281ecd12012-09-10 13:46:27 +0300806 if (gpio_is_valid(pdata->hs_extmute_gpio)) {
807 gpio_set_value(pdata->hs_extmute_gpio, 0);
Candelaria Villareal, Jorge4e49ffd2009-07-01 19:17:43 -0500808 } else {
809 hs_pop &= ~TWL4030_EXTMUTE;
810 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, hs_pop);
811 }
812 }
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300813}
814
815static int headsetlpga_event(struct snd_soc_dapm_widget *w,
816 struct snd_kcontrol *kcontrol, int event)
817{
Mark Brownb2c812e2010-04-14 15:35:19 +0900818 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(w->codec);
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300819
820 switch (event) {
821 case SND_SOC_DAPM_POST_PMU:
822 /* Do the ramp-up only once */
823 if (!twl4030->hsr_enabled)
824 headset_ramp(w->codec, 1);
825
826 twl4030->hsl_enabled = 1;
827 break;
828 case SND_SOC_DAPM_POST_PMD:
829 /* Do the ramp-down only if both headsetL/R is disabled */
830 if (!twl4030->hsr_enabled)
831 headset_ramp(w->codec, 0);
832
833 twl4030->hsl_enabled = 0;
834 break;
835 }
836 return 0;
837}
838
839static int headsetrpga_event(struct snd_soc_dapm_widget *w,
840 struct snd_kcontrol *kcontrol, int event)
841{
Mark Brownb2c812e2010-04-14 15:35:19 +0900842 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(w->codec);
Peter Ujfalusi6943c922009-05-18 16:02:05 +0300843
844 switch (event) {
845 case SND_SOC_DAPM_POST_PMU:
846 /* Do the ramp-up only once */
847 if (!twl4030->hsl_enabled)
848 headset_ramp(w->codec, 1);
849
850 twl4030->hsr_enabled = 1;
851 break;
852 case SND_SOC_DAPM_POST_PMD:
853 /* Do the ramp-down only if both headsetL/R is disabled */
854 if (!twl4030->hsl_enabled)
855 headset_ramp(w->codec, 0);
856
857 twl4030->hsr_enabled = 0;
Peter Ujfalusiaad749e2009-01-27 11:29:41 +0200858 break;
859 }
860 return 0;
861}
862
Peter Ujfalusi01ea6ba2010-07-20 15:49:09 +0300863static int digimic_event(struct snd_soc_dapm_widget *w,
864 struct snd_kcontrol *kcontrol, int event)
865{
866 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(w->codec);
867
868 if (twl4030->digimic_delay)
Peter Ujfalusi7e6120c2010-10-25 11:34:23 +0300869 twl4030_wait_ms(twl4030->digimic_delay);
Peter Ujfalusi01ea6ba2010-07-20 15:49:09 +0300870 return 0;
871}
872
Peter Ujfalusic10b82c2008-11-24 13:49:35 +0200873/*
Peter Ujfalusib0bd53a2008-11-24 13:49:38 +0200874 * Some of the gain controls in TWL (mostly those which are associated with
875 * the outputs) are implemented in an interesting way:
876 * 0x0 : Power down (mute)
877 * 0x1 : 6dB
878 * 0x2 : 0 dB
879 * 0x3 : -6 dB
880 * Inverting not going to help with these.
881 * Custom volsw and volsw_2r get/put functions to handle these gain bits.
882 */
Peter Ujfalusib0bd53a2008-11-24 13:49:38 +0200883static int snd_soc_get_volsw_twl4030(struct snd_kcontrol *kcontrol,
884 struct snd_ctl_elem_value *ucontrol)
885{
886 struct soc_mixer_control *mc =
887 (struct soc_mixer_control *)kcontrol->private_value;
888 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
889 unsigned int reg = mc->reg;
890 unsigned int shift = mc->shift;
891 unsigned int rshift = mc->rshift;
892 int max = mc->max;
893 int mask = (1 << fls(max)) - 1;
894
895 ucontrol->value.integer.value[0] =
896 (snd_soc_read(codec, reg) >> shift) & mask;
897 if (ucontrol->value.integer.value[0])
898 ucontrol->value.integer.value[0] =
899 max + 1 - ucontrol->value.integer.value[0];
900
901 if (shift != rshift) {
902 ucontrol->value.integer.value[1] =
903 (snd_soc_read(codec, reg) >> rshift) & mask;
904 if (ucontrol->value.integer.value[1])
905 ucontrol->value.integer.value[1] =
906 max + 1 - ucontrol->value.integer.value[1];
907 }
908
909 return 0;
910}
911
912static int snd_soc_put_volsw_twl4030(struct snd_kcontrol *kcontrol,
913 struct snd_ctl_elem_value *ucontrol)
914{
915 struct soc_mixer_control *mc =
916 (struct soc_mixer_control *)kcontrol->private_value;
917 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
918 unsigned int reg = mc->reg;
919 unsigned int shift = mc->shift;
920 unsigned int rshift = mc->rshift;
921 int max = mc->max;
922 int mask = (1 << fls(max)) - 1;
923 unsigned short val, val2, val_mask;
924
925 val = (ucontrol->value.integer.value[0] & mask);
926
927 val_mask = mask << shift;
928 if (val)
929 val = max + 1 - val;
930 val = val << shift;
931 if (shift != rshift) {
932 val2 = (ucontrol->value.integer.value[1] & mask);
933 val_mask |= mask << rshift;
934 if (val2)
935 val2 = max + 1 - val2;
936 val |= val2 << rshift;
937 }
938 return snd_soc_update_bits(codec, reg, val_mask, val);
939}
940
941static int snd_soc_get_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
942 struct snd_ctl_elem_value *ucontrol)
943{
944 struct soc_mixer_control *mc =
945 (struct soc_mixer_control *)kcontrol->private_value;
946 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
947 unsigned int reg = mc->reg;
948 unsigned int reg2 = mc->rreg;
949 unsigned int shift = mc->shift;
950 int max = mc->max;
951 int mask = (1<<fls(max))-1;
952
953 ucontrol->value.integer.value[0] =
954 (snd_soc_read(codec, reg) >> shift) & mask;
955 ucontrol->value.integer.value[1] =
956 (snd_soc_read(codec, reg2) >> shift) & mask;
957
958 if (ucontrol->value.integer.value[0])
959 ucontrol->value.integer.value[0] =
960 max + 1 - ucontrol->value.integer.value[0];
961 if (ucontrol->value.integer.value[1])
962 ucontrol->value.integer.value[1] =
963 max + 1 - ucontrol->value.integer.value[1];
964
965 return 0;
966}
967
968static int snd_soc_put_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
969 struct snd_ctl_elem_value *ucontrol)
970{
971 struct soc_mixer_control *mc =
972 (struct soc_mixer_control *)kcontrol->private_value;
973 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
974 unsigned int reg = mc->reg;
975 unsigned int reg2 = mc->rreg;
976 unsigned int shift = mc->shift;
977 int max = mc->max;
978 int mask = (1 << fls(max)) - 1;
979 int err;
980 unsigned short val, val2, val_mask;
981
982 val_mask = mask << shift;
983 val = (ucontrol->value.integer.value[0] & mask);
984 val2 = (ucontrol->value.integer.value[1] & mask);
985
986 if (val)
987 val = max + 1 - val;
988 if (val2)
989 val2 = max + 1 - val2;
990
991 val = val << shift;
992 val2 = val2 << shift;
993
994 err = snd_soc_update_bits(codec, reg, val_mask, val);
995 if (err < 0)
996 return err;
997
998 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
999 return err;
1000}
1001
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -05001002/* Codec operation modes */
1003static const char *twl4030_op_modes_texts[] = {
1004 "Option 2 (voice/audio)", "Option 1 (audio)"
1005};
1006
1007static const struct soc_enum twl4030_op_modes_enum =
1008 SOC_ENUM_SINGLE(TWL4030_REG_CODEC_MODE, 0,
1009 ARRAY_SIZE(twl4030_op_modes_texts),
1010 twl4030_op_modes_texts);
1011
Mark Brown423c2382009-06-20 13:54:02 +01001012static int snd_soc_put_twl4030_opmode_enum_double(struct snd_kcontrol *kcontrol,
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -05001013 struct snd_ctl_elem_value *ucontrol)
1014{
1015 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
Mark Brownb2c812e2010-04-14 15:35:19 +09001016 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -05001017 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
1018 unsigned short val;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02001019 unsigned short mask;
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -05001020
1021 if (twl4030->configured) {
Peter Ujfalusi3b8a0792012-02-21 09:34:20 +02001022 dev_err(codec->dev,
1023 "operation mode cannot be changed on-the-fly\n");
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -05001024 return -EBUSY;
1025 }
1026
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -05001027 if (ucontrol->value.enumerated.item[0] > e->max - 1)
1028 return -EINVAL;
1029
1030 val = ucontrol->value.enumerated.item[0] << e->shift_l;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02001031 mask = e->mask << e->shift_l;
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -05001032 if (e->shift_l != e->shift_r) {
1033 if (ucontrol->value.enumerated.item[1] > e->max - 1)
1034 return -EINVAL;
1035 val |= ucontrol->value.enumerated.item[1] << e->shift_r;
Lars-Peter Clausen86767b72012-09-14 13:57:27 +02001036 mask |= e->mask << e->shift_r;
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -05001037 }
1038
1039 return snd_soc_update_bits(codec, e->reg, mask, val);
1040}
1041
Peter Ujfalusib0bd53a2008-11-24 13:49:38 +02001042/*
Peter Ujfalusic10b82c2008-11-24 13:49:35 +02001043 * FGAIN volume control:
1044 * from -62 to 0 dB in 1 dB steps (mute instead of -63 dB)
1045 */
Peter Ujfalusid889a722008-12-01 10:03:46 +02001046static DECLARE_TLV_DB_SCALE(digital_fine_tlv, -6300, 100, 1);
Peter Ujfalusic10b82c2008-11-24 13:49:35 +02001047
Peter Ujfalusi0d33ea02008-11-24 13:49:36 +02001048/*
1049 * CGAIN volume control:
1050 * 0 dB to 12 dB in 6 dB steps
1051 * value 2 and 3 means 12 dB
1052 */
Peter Ujfalusid889a722008-12-01 10:03:46 +02001053static DECLARE_TLV_DB_SCALE(digital_coarse_tlv, 0, 600, 0);
1054
1055/*
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001056 * Voice Downlink GAIN volume control:
1057 * from -37 to 12 dB in 1 dB steps (mute instead of -37 dB)
1058 */
1059static DECLARE_TLV_DB_SCALE(digital_voice_downlink_tlv, -3700, 100, 1);
1060
1061/*
Peter Ujfalusid889a722008-12-01 10:03:46 +02001062 * Analog playback gain
1063 * -24 dB to 12 dB in 2 dB steps
1064 */
1065static DECLARE_TLV_DB_SCALE(analog_tlv, -2400, 200, 0);
Peter Ujfalusi0d33ea02008-11-24 13:49:36 +02001066
Peter Ujfalusi381a22b2008-12-01 10:03:45 +02001067/*
Peter Ujfalusi42902392008-12-01 10:03:47 +02001068 * Gain controls tied to outputs
1069 * -6 dB to 6 dB in 6 dB steps (mute instead of -12)
1070 */
1071static DECLARE_TLV_DB_SCALE(output_tvl, -1200, 600, 1);
1072
1073/*
Joonyoung Shim18cc8d82009-04-28 18:18:05 +09001074 * Gain control for earpiece amplifier
1075 * 0 dB to 12 dB in 6 dB steps (mute instead of -6)
1076 */
1077static DECLARE_TLV_DB_SCALE(output_ear_tvl, -600, 600, 1);
1078
1079/*
Peter Ujfalusi381a22b2008-12-01 10:03:45 +02001080 * Capture gain after the ADCs
1081 * from 0 dB to 31 dB in 1 dB steps
1082 */
1083static DECLARE_TLV_DB_SCALE(digital_capture_tlv, 0, 100, 0);
1084
Grazvydas Ignotas5920b452008-12-02 20:48:58 +02001085/*
1086 * Gain control for input amplifiers
1087 * 0 dB to 30 dB in 6 dB steps
1088 */
1089static DECLARE_TLV_DB_SCALE(input_gain_tlv, 0, 600, 0);
1090
Lopez Cruz, Misael328d0a12009-06-22 10:51:52 -05001091/* AVADC clock priority */
1092static const char *twl4030_avadc_clk_priority_texts[] = {
1093 "Voice high priority", "HiFi high priority"
1094};
1095
1096static const struct soc_enum twl4030_avadc_clk_priority_enum =
1097 SOC_ENUM_SINGLE(TWL4030_REG_AVADC_CTL, 2,
1098 ARRAY_SIZE(twl4030_avadc_clk_priority_texts),
1099 twl4030_avadc_clk_priority_texts);
1100
Peter Ujfalusi89492be2009-03-05 12:48:49 +02001101static const char *twl4030_rampdelay_texts[] = {
1102 "27/20/14 ms", "55/40/27 ms", "109/81/55 ms", "218/161/109 ms",
1103 "437/323/218 ms", "874/645/437 ms", "1748/1291/874 ms",
1104 "3495/2581/1748 ms"
1105};
1106
1107static const struct soc_enum twl4030_rampdelay_enum =
1108 SOC_ENUM_SINGLE(TWL4030_REG_HS_POPN_SET, 2,
1109 ARRAY_SIZE(twl4030_rampdelay_texts),
1110 twl4030_rampdelay_texts);
1111
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001112/* Vibra H-bridge direction mode */
1113static const char *twl4030_vibradirmode_texts[] = {
1114 "Vibra H-bridge direction", "Audio data MSB",
1115};
1116
1117static const struct soc_enum twl4030_vibradirmode_enum =
1118 SOC_ENUM_SINGLE(TWL4030_REG_VIBRA_CTL, 5,
1119 ARRAY_SIZE(twl4030_vibradirmode_texts),
1120 twl4030_vibradirmode_texts);
1121
1122/* Vibra H-bridge direction */
1123static const char *twl4030_vibradir_texts[] = {
1124 "Positive polarity", "Negative polarity",
1125};
1126
1127static const struct soc_enum twl4030_vibradir_enum =
1128 SOC_ENUM_SINGLE(TWL4030_REG_VIBRA_CTL, 1,
1129 ARRAY_SIZE(twl4030_vibradir_texts),
1130 twl4030_vibradir_texts);
1131
Peter Ujfalusi36aeff62010-05-12 10:35:36 +03001132/* Digimic Left and right swapping */
1133static const char *twl4030_digimicswap_texts[] = {
1134 "Not swapped", "Swapped",
1135};
1136
1137static const struct soc_enum twl4030_digimicswap_enum =
1138 SOC_ENUM_SINGLE(TWL4030_REG_MISC_SET_1, 0,
1139 ARRAY_SIZE(twl4030_digimicswap_texts),
1140 twl4030_digimicswap_texts);
1141
Steve Sakomancc175572008-10-30 21:35:26 -07001142static const struct snd_kcontrol_new twl4030_snd_controls[] = {
Lopez Cruz, Misaelb74bd402009-05-18 11:52:55 -05001143 /* Codec operation mode control */
1144 SOC_ENUM_EXT("Codec Operation Mode", twl4030_op_modes_enum,
1145 snd_soc_get_enum_double,
1146 snd_soc_put_twl4030_opmode_enum_double),
1147
Peter Ujfalusid889a722008-12-01 10:03:46 +02001148 /* Common playback gain controls */
1149 SOC_DOUBLE_R_TLV("DAC1 Digital Fine Playback Volume",
1150 TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
1151 0, 0x3f, 0, digital_fine_tlv),
1152 SOC_DOUBLE_R_TLV("DAC2 Digital Fine Playback Volume",
1153 TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
1154 0, 0x3f, 0, digital_fine_tlv),
1155
1156 SOC_DOUBLE_R_TLV("DAC1 Digital Coarse Playback Volume",
1157 TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
1158 6, 0x2, 0, digital_coarse_tlv),
1159 SOC_DOUBLE_R_TLV("DAC2 Digital Coarse Playback Volume",
1160 TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
1161 6, 0x2, 0, digital_coarse_tlv),
1162
1163 SOC_DOUBLE_R_TLV("DAC1 Analog Playback Volume",
1164 TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
1165 3, 0x12, 1, analog_tlv),
1166 SOC_DOUBLE_R_TLV("DAC2 Analog Playback Volume",
1167 TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
1168 3, 0x12, 1, analog_tlv),
Peter Ujfalusi44c55872008-12-09 08:45:44 +02001169 SOC_DOUBLE_R("DAC1 Analog Playback Switch",
1170 TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
1171 1, 1, 0),
1172 SOC_DOUBLE_R("DAC2 Analog Playback Switch",
1173 TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
1174 1, 1, 0),
Peter Ujfalusi381a22b2008-12-01 10:03:45 +02001175
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001176 /* Common voice downlink gain controls */
1177 SOC_SINGLE_TLV("DAC Voice Digital Downlink Volume",
1178 TWL4030_REG_VRXPGA, 0, 0x31, 0, digital_voice_downlink_tlv),
1179
1180 SOC_SINGLE_TLV("DAC Voice Analog Downlink Volume",
1181 TWL4030_REG_VDL_APGA_CTL, 3, 0x12, 1, analog_tlv),
1182
1183 SOC_SINGLE("DAC Voice Analog Downlink Switch",
1184 TWL4030_REG_VDL_APGA_CTL, 1, 1, 0),
1185
Peter Ujfalusi42902392008-12-01 10:03:47 +02001186 /* Separate output gain controls */
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +03001187 SOC_DOUBLE_R_EXT_TLV("PreDriv Playback Volume",
Peter Ujfalusi42902392008-12-01 10:03:47 +02001188 TWL4030_REG_PREDL_CTL, TWL4030_REG_PREDR_CTL,
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +03001189 4, 3, 0, snd_soc_get_volsw_r2_twl4030,
1190 snd_soc_put_volsw_r2_twl4030, output_tvl),
Peter Ujfalusi42902392008-12-01 10:03:47 +02001191
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +03001192 SOC_DOUBLE_EXT_TLV("Headset Playback Volume",
1193 TWL4030_REG_HS_GAIN_SET, 0, 2, 3, 0, snd_soc_get_volsw_twl4030,
1194 snd_soc_put_volsw_twl4030, output_tvl),
Peter Ujfalusi42902392008-12-01 10:03:47 +02001195
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +03001196 SOC_DOUBLE_R_EXT_TLV("Carkit Playback Volume",
Peter Ujfalusi42902392008-12-01 10:03:47 +02001197 TWL4030_REG_PRECKL_CTL, TWL4030_REG_PRECKR_CTL,
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +03001198 4, 3, 0, snd_soc_get_volsw_r2_twl4030,
1199 snd_soc_put_volsw_r2_twl4030, output_tvl),
Peter Ujfalusi42902392008-12-01 10:03:47 +02001200
Peter Ujfalusi0f9887d2011-10-05 10:29:19 +03001201 SOC_SINGLE_EXT_TLV("Earpiece Playback Volume",
1202 TWL4030_REG_EAR_CTL, 4, 3, 0, snd_soc_get_volsw_twl4030,
1203 snd_soc_put_volsw_twl4030, output_ear_tvl),
Peter Ujfalusi42902392008-12-01 10:03:47 +02001204
Peter Ujfalusi381a22b2008-12-01 10:03:45 +02001205 /* Common capture gain controls */
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001206 SOC_DOUBLE_R_TLV("TX1 Digital Capture Volume",
Peter Ujfalusi381a22b2008-12-01 10:03:45 +02001207 TWL4030_REG_ATXL1PGA, TWL4030_REG_ATXR1PGA,
1208 0, 0x1f, 0, digital_capture_tlv),
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001209 SOC_DOUBLE_R_TLV("TX2 Digital Capture Volume",
1210 TWL4030_REG_AVTXL2PGA, TWL4030_REG_AVTXR2PGA,
1211 0, 0x1f, 0, digital_capture_tlv),
Grazvydas Ignotas5920b452008-12-02 20:48:58 +02001212
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001213 SOC_DOUBLE_TLV("Analog Capture Volume", TWL4030_REG_ANAMIC_GAIN,
Grazvydas Ignotas5920b452008-12-02 20:48:58 +02001214 0, 3, 5, 0, input_gain_tlv),
Peter Ujfalusi89492be2009-03-05 12:48:49 +02001215
Lopez Cruz, Misael328d0a12009-06-22 10:51:52 -05001216 SOC_ENUM("AVADC Clock Priority", twl4030_avadc_clk_priority_enum),
1217
Peter Ujfalusi89492be2009-03-05 12:48:49 +02001218 SOC_ENUM("HS ramp delay", twl4030_rampdelay_enum),
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001219
1220 SOC_ENUM("Vibra H-bridge mode", twl4030_vibradirmode_enum),
1221 SOC_ENUM("Vibra H-bridge direction", twl4030_vibradir_enum),
Peter Ujfalusi36aeff62010-05-12 10:35:36 +03001222
1223 SOC_ENUM("Digimic LR Swap", twl4030_digimicswap_enum),
Steve Sakomancc175572008-10-30 21:35:26 -07001224};
1225
Steve Sakomancc175572008-10-30 21:35:26 -07001226static const struct snd_soc_dapm_widget twl4030_dapm_widgets[] = {
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001227 /* Left channel inputs */
1228 SND_SOC_DAPM_INPUT("MAINMIC"),
1229 SND_SOC_DAPM_INPUT("HSMIC"),
1230 SND_SOC_DAPM_INPUT("AUXL"),
1231 SND_SOC_DAPM_INPUT("CARKITMIC"),
1232 /* Right channel inputs */
1233 SND_SOC_DAPM_INPUT("SUBMIC"),
1234 SND_SOC_DAPM_INPUT("AUXR"),
1235 /* Digital microphones (Stereo) */
1236 SND_SOC_DAPM_INPUT("DIGIMIC0"),
1237 SND_SOC_DAPM_INPUT("DIGIMIC1"),
Steve Sakomancc175572008-10-30 21:35:26 -07001238
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001239 /* Outputs */
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001240 SND_SOC_DAPM_OUTPUT("EARPIECE"),
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +02001241 SND_SOC_DAPM_OUTPUT("PREDRIVEL"),
1242 SND_SOC_DAPM_OUTPUT("PREDRIVER"),
Peter Ujfalusidfad21a2008-12-09 12:35:49 +02001243 SND_SOC_DAPM_OUTPUT("HSOL"),
1244 SND_SOC_DAPM_OUTPUT("HSOR"),
Peter Ujfalusi6a1bee42008-12-10 12:51:46 +02001245 SND_SOC_DAPM_OUTPUT("CARKITL"),
1246 SND_SOC_DAPM_OUTPUT("CARKITR"),
Peter Ujfalusidf339802008-12-09 12:35:51 +02001247 SND_SOC_DAPM_OUTPUT("HFL"),
1248 SND_SOC_DAPM_OUTPUT("HFR"),
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001249 SND_SOC_DAPM_OUTPUT("VIBRA"),
Steve Sakomancc175572008-10-30 21:35:26 -07001250
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +03001251 /* AIF and APLL clocks for running DAIs (including loopback) */
1252 SND_SOC_DAPM_OUTPUT("Virtual HiFi OUT"),
1253 SND_SOC_DAPM_INPUT("Virtual HiFi IN"),
1254 SND_SOC_DAPM_OUTPUT("Virtual Voice OUT"),
1255
Peter Ujfalusi53b50472008-12-09 08:45:43 +02001256 /* DACs */
Peter Ujfalusi7f51e7d2012-09-20 16:32:02 +03001257 SND_SOC_DAPM_DAC("DAC Right1", NULL, SND_SOC_NOPM, 0, 0),
1258 SND_SOC_DAPM_DAC("DAC Left1", NULL, SND_SOC_NOPM, 0, 0),
1259 SND_SOC_DAPM_DAC("DAC Right2", NULL, SND_SOC_NOPM, 0, 0),
1260 SND_SOC_DAPM_DAC("DAC Left2", NULL, SND_SOC_NOPM, 0, 0),
1261 SND_SOC_DAPM_DAC("DAC Voice", NULL, SND_SOC_NOPM, 0, 0),
Steve Sakomancc175572008-10-30 21:35:26 -07001262
Peter Ujfalusi73939582009-01-29 14:57:50 +02001263 /* Analog bypasses */
Peter Ujfalusi78e08e22009-10-28 10:57:04 +02001264 SND_SOC_DAPM_SWITCH("Right1 Analog Loopback", SND_SOC_NOPM, 0, 0,
1265 &twl4030_dapm_abypassr1_control),
1266 SND_SOC_DAPM_SWITCH("Left1 Analog Loopback", SND_SOC_NOPM, 0, 0,
1267 &twl4030_dapm_abypassl1_control),
1268 SND_SOC_DAPM_SWITCH("Right2 Analog Loopback", SND_SOC_NOPM, 0, 0,
1269 &twl4030_dapm_abypassr2_control),
1270 SND_SOC_DAPM_SWITCH("Left2 Analog Loopback", SND_SOC_NOPM, 0, 0,
1271 &twl4030_dapm_abypassl2_control),
1272 SND_SOC_DAPM_SWITCH("Voice Analog Loopback", SND_SOC_NOPM, 0, 0,
1273 &twl4030_dapm_abypassv_control),
1274
1275 /* Master analog loopback switch */
1276 SND_SOC_DAPM_SUPPLY("FM Loop Enable", TWL4030_REG_MISC_SET_1, 5, 0,
1277 NULL, 0),
Peter Ujfalusi73939582009-01-29 14:57:50 +02001278
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001279 /* Digital bypasses */
Peter Ujfalusi78e08e22009-10-28 10:57:04 +02001280 SND_SOC_DAPM_SWITCH("Left Digital Loopback", SND_SOC_NOPM, 0, 0,
1281 &twl4030_dapm_dbypassl_control),
1282 SND_SOC_DAPM_SWITCH("Right Digital Loopback", SND_SOC_NOPM, 0, 0,
1283 &twl4030_dapm_dbypassr_control),
1284 SND_SOC_DAPM_SWITCH("Voice Digital Loopback", SND_SOC_NOPM, 0, 0,
1285 &twl4030_dapm_dbypassv_control),
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001286
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001287 /* Digital mixers, power control for the physical DACs */
1288 SND_SOC_DAPM_MIXER("Digital R1 Playback Mixer",
1289 TWL4030_REG_AVDAC_CTL, 0, 0, NULL, 0),
1290 SND_SOC_DAPM_MIXER("Digital L1 Playback Mixer",
1291 TWL4030_REG_AVDAC_CTL, 1, 0, NULL, 0),
1292 SND_SOC_DAPM_MIXER("Digital R2 Playback Mixer",
1293 TWL4030_REG_AVDAC_CTL, 2, 0, NULL, 0),
1294 SND_SOC_DAPM_MIXER("Digital L2 Playback Mixer",
1295 TWL4030_REG_AVDAC_CTL, 3, 0, NULL, 0),
1296 SND_SOC_DAPM_MIXER("Digital Voice Playback Mixer",
1297 TWL4030_REG_AVDAC_CTL, 4, 0, NULL, 0),
1298
1299 /* Analog mixers, power control for the physical PGAs */
1300 SND_SOC_DAPM_MIXER("Analog R1 Playback Mixer",
1301 TWL4030_REG_ARXR1_APGA_CTL, 0, 0, NULL, 0),
1302 SND_SOC_DAPM_MIXER("Analog L1 Playback Mixer",
1303 TWL4030_REG_ARXL1_APGA_CTL, 0, 0, NULL, 0),
1304 SND_SOC_DAPM_MIXER("Analog R2 Playback Mixer",
1305 TWL4030_REG_ARXR2_APGA_CTL, 0, 0, NULL, 0),
1306 SND_SOC_DAPM_MIXER("Analog L2 Playback Mixer",
1307 TWL4030_REG_ARXL2_APGA_CTL, 0, 0, NULL, 0),
1308 SND_SOC_DAPM_MIXER("Analog Voice Playback Mixer",
1309 TWL4030_REG_VDL_APGA_CTL, 0, 0, NULL, 0),
Peter Ujfalusi73939582009-01-29 14:57:50 +02001310
Peter Ujfalusi7729cf72009-10-29 11:58:10 +02001311 SND_SOC_DAPM_SUPPLY("APLL Enable", SND_SOC_NOPM, 0, 0, apll_event,
1312 SND_SOC_DAPM_PRE_PMU|SND_SOC_DAPM_POST_PMD),
1313
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +03001314 SND_SOC_DAPM_SUPPLY("AIF Enable", SND_SOC_NOPM, 0, 0, aif_event,
1315 SND_SOC_DAPM_PRE_PMU|SND_SOC_DAPM_POST_PMD),
Peter Ujfalusic42a59e2010-02-09 15:24:04 +02001316
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001317 /* Output MIXER controls */
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001318 /* Earpiece */
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001319 SND_SOC_DAPM_MIXER("Earpiece Mixer", SND_SOC_NOPM, 0, 0,
1320 &twl4030_dapm_earpiece_controls[0],
1321 ARRAY_SIZE(twl4030_dapm_earpiece_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001322 SND_SOC_DAPM_PGA_E("Earpiece PGA", SND_SOC_NOPM,
1323 0, 0, NULL, 0, earpiecepga_event,
1324 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +02001325 /* PreDrivL/R */
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001326 SND_SOC_DAPM_MIXER("PredriveL Mixer", SND_SOC_NOPM, 0, 0,
1327 &twl4030_dapm_predrivel_controls[0],
1328 ARRAY_SIZE(twl4030_dapm_predrivel_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001329 SND_SOC_DAPM_PGA_E("PredriveL PGA", SND_SOC_NOPM,
1330 0, 0, NULL, 0, predrivelpga_event,
1331 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001332 SND_SOC_DAPM_MIXER("PredriveR Mixer", SND_SOC_NOPM, 0, 0,
1333 &twl4030_dapm_predriver_controls[0],
1334 ARRAY_SIZE(twl4030_dapm_predriver_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001335 SND_SOC_DAPM_PGA_E("PredriveR PGA", SND_SOC_NOPM,
1336 0, 0, NULL, 0, predriverpga_event,
1337 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Peter Ujfalusidfad21a2008-12-09 12:35:49 +02001338 /* HeadsetL/R */
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001339 SND_SOC_DAPM_MIXER("HeadsetL Mixer", SND_SOC_NOPM, 0, 0,
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001340 &twl4030_dapm_hsol_controls[0],
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001341 ARRAY_SIZE(twl4030_dapm_hsol_controls)),
1342 SND_SOC_DAPM_PGA_E("HeadsetL PGA", SND_SOC_NOPM,
1343 0, 0, NULL, 0, headsetlpga_event,
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001344 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
1345 SND_SOC_DAPM_MIXER("HeadsetR Mixer", SND_SOC_NOPM, 0, 0,
1346 &twl4030_dapm_hsor_controls[0],
1347 ARRAY_SIZE(twl4030_dapm_hsor_controls)),
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001348 SND_SOC_DAPM_PGA_E("HeadsetR PGA", SND_SOC_NOPM,
1349 0, 0, NULL, 0, headsetrpga_event,
1350 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +02001351 /* CarkitL/R */
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001352 SND_SOC_DAPM_MIXER("CarkitL Mixer", SND_SOC_NOPM, 0, 0,
1353 &twl4030_dapm_carkitl_controls[0],
1354 ARRAY_SIZE(twl4030_dapm_carkitl_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001355 SND_SOC_DAPM_PGA_E("CarkitL PGA", SND_SOC_NOPM,
1356 0, 0, NULL, 0, carkitlpga_event,
1357 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001358 SND_SOC_DAPM_MIXER("CarkitR Mixer", SND_SOC_NOPM, 0, 0,
1359 &twl4030_dapm_carkitr_controls[0],
1360 ARRAY_SIZE(twl4030_dapm_carkitr_controls)),
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001361 SND_SOC_DAPM_PGA_E("CarkitR PGA", SND_SOC_NOPM,
1362 0, 0, NULL, 0, carkitrpga_event,
1363 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001364
1365 /* Output MUX controls */
Peter Ujfalusidf339802008-12-09 12:35:51 +02001366 /* HandsfreeL/R */
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +03001367 SND_SOC_DAPM_MUX("HandsfreeL Mux", SND_SOC_NOPM, 0, 0,
1368 &twl4030_dapm_handsfreel_control),
Lopez Cruz, Misaele3c7dbb2009-06-25 12:36:14 -05001369 SND_SOC_DAPM_SWITCH("HandsfreeL", SND_SOC_NOPM, 0, 0,
Peter Ujfalusi0f89bdc2009-05-25 11:12:13 +03001370 &twl4030_dapm_handsfreelmute_control),
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +03001371 SND_SOC_DAPM_PGA_E("HandsfreeL PGA", SND_SOC_NOPM,
1372 0, 0, NULL, 0, handsfreelpga_event,
1373 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
1374 SND_SOC_DAPM_MUX("HandsfreeR Mux", SND_SOC_NOPM, 5, 0,
1375 &twl4030_dapm_handsfreer_control),
Lopez Cruz, Misaele3c7dbb2009-06-25 12:36:14 -05001376 SND_SOC_DAPM_SWITCH("HandsfreeR", SND_SOC_NOPM, 0, 0,
Peter Ujfalusi0f89bdc2009-05-25 11:12:13 +03001377 &twl4030_dapm_handsfreermute_control),
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +03001378 SND_SOC_DAPM_PGA_E("HandsfreeR PGA", SND_SOC_NOPM,
1379 0, 0, NULL, 0, handsfreerpga_event,
1380 SND_SOC_DAPM_POST_PMU|SND_SOC_DAPM_POST_PMD),
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001381 /* Vibra */
Jari Vanhala86139a12009-10-29 11:58:09 +02001382 SND_SOC_DAPM_MUX_E("Vibra Mux", TWL4030_REG_VIBRA_CTL, 0, 0,
1383 &twl4030_dapm_vibra_control, vibramux_event,
1384 SND_SOC_DAPM_PRE_PMU),
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001385 SND_SOC_DAPM_MUX("Vibra Route", SND_SOC_NOPM, 0, 0,
1386 &twl4030_dapm_vibrapath_control),
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001387
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001388 /* Introducing four virtual ADC, since TWL4030 have four channel for
1389 capture */
Peter Ujfalusi7f51e7d2012-09-20 16:32:02 +03001390 SND_SOC_DAPM_ADC("ADC Virtual Left1", NULL, SND_SOC_NOPM, 0, 0),
1391 SND_SOC_DAPM_ADC("ADC Virtual Right1", NULL, SND_SOC_NOPM, 0, 0),
1392 SND_SOC_DAPM_ADC("ADC Virtual Left2", NULL, SND_SOC_NOPM, 0, 0),
1393 SND_SOC_DAPM_ADC("ADC Virtual Right2", NULL, SND_SOC_NOPM, 0, 0),
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001394
1395 /* Analog/Digital mic path selection.
1396 TX1 Left/Right: either analog Left/Right or Digimic0
1397 TX2 Left/Right: either analog Left/Right or Digimic1 */
Peter Ujfalusibda7d2a2010-08-03 12:01:01 +03001398 SND_SOC_DAPM_MUX("TX1 Capture Route", SND_SOC_NOPM, 0, 0,
1399 &twl4030_dapm_micpathtx1_control),
1400 SND_SOC_DAPM_MUX("TX2 Capture Route", SND_SOC_NOPM, 0, 0,
1401 &twl4030_dapm_micpathtx2_control),
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001402
Joonyoung Shim97b80962009-05-11 20:36:08 +09001403 /* Analog input mixers for the capture amplifiers */
Peter Ujfalusi90289352009-08-14 08:44:00 +03001404 SND_SOC_DAPM_MIXER("Analog Left",
Joonyoung Shim97b80962009-05-11 20:36:08 +09001405 TWL4030_REG_ANAMICL, 4, 0,
1406 &twl4030_dapm_analoglmic_controls[0],
1407 ARRAY_SIZE(twl4030_dapm_analoglmic_controls)),
Peter Ujfalusi90289352009-08-14 08:44:00 +03001408 SND_SOC_DAPM_MIXER("Analog Right",
Joonyoung Shim97b80962009-05-11 20:36:08 +09001409 TWL4030_REG_ANAMICR, 4, 0,
1410 &twl4030_dapm_analogrmic_controls[0],
1411 ARRAY_SIZE(twl4030_dapm_analogrmic_controls)),
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001412
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001413 SND_SOC_DAPM_PGA("ADC Physical Left",
1414 TWL4030_REG_AVADC_CTL, 3, 0, NULL, 0),
1415 SND_SOC_DAPM_PGA("ADC Physical Right",
1416 TWL4030_REG_AVADC_CTL, 1, 0, NULL, 0),
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001417
Peter Ujfalusi01ea6ba2010-07-20 15:49:09 +03001418 SND_SOC_DAPM_PGA_E("Digimic0 Enable",
1419 TWL4030_REG_ADCMICSEL, 1, 0, NULL, 0,
1420 digimic_event, SND_SOC_DAPM_POST_PMU),
1421 SND_SOC_DAPM_PGA_E("Digimic1 Enable",
1422 TWL4030_REG_ADCMICSEL, 3, 0, NULL, 0,
1423 digimic_event, SND_SOC_DAPM_POST_PMU),
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001424
Peter Ujfalusibda7d2a2010-08-03 12:01:01 +03001425 SND_SOC_DAPM_SUPPLY("micbias1 select", TWL4030_REG_MICBIAS_CTL, 5, 0,
1426 NULL, 0),
1427 SND_SOC_DAPM_SUPPLY("micbias2 select", TWL4030_REG_MICBIAS_CTL, 6, 0,
1428 NULL, 0),
1429
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001430 SND_SOC_DAPM_MICBIAS("Mic Bias 1", TWL4030_REG_MICBIAS_CTL, 0, 0),
1431 SND_SOC_DAPM_MICBIAS("Mic Bias 2", TWL4030_REG_MICBIAS_CTL, 1, 0),
1432 SND_SOC_DAPM_MICBIAS("Headset Mic Bias", TWL4030_REG_MICBIAS_CTL, 2, 0),
Peter Ujfalusi73939582009-01-29 14:57:50 +02001433
Steve Sakomancc175572008-10-30 21:35:26 -07001434};
1435
1436static const struct snd_soc_dapm_route intercon[] = {
Peter Ujfalusi7f51e7d2012-09-20 16:32:02 +03001437 /* Stream -> DAC mapping */
1438 {"DAC Right1", NULL, "HiFi Playback"},
1439 {"DAC Left1", NULL, "HiFi Playback"},
1440 {"DAC Right2", NULL, "HiFi Playback"},
1441 {"DAC Left2", NULL, "HiFi Playback"},
1442 {"DAC Voice", NULL, "Voice Playback"},
1443
1444 /* ADC -> Stream mapping */
1445 {"HiFi Capture", NULL, "ADC Virtual Left1"},
1446 {"HiFi Capture", NULL, "ADC Virtual Right1"},
1447 {"HiFi Capture", NULL, "ADC Virtual Left2"},
1448 {"HiFi Capture", NULL, "ADC Virtual Right2"},
1449 {"Voice Capture", NULL, "ADC Virtual Left1"},
1450 {"Voice Capture", NULL, "ADC Virtual Right1"},
1451 {"Voice Capture", NULL, "ADC Virtual Left2"},
1452 {"Voice Capture", NULL, "ADC Virtual Right2"},
1453
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001454 {"Digital L1 Playback Mixer", NULL, "DAC Left1"},
1455 {"Digital R1 Playback Mixer", NULL, "DAC Right1"},
1456 {"Digital L2 Playback Mixer", NULL, "DAC Left2"},
1457 {"Digital R2 Playback Mixer", NULL, "DAC Right2"},
1458 {"Digital Voice Playback Mixer", NULL, "DAC Voice"},
Peter Ujfalusi73939582009-01-29 14:57:50 +02001459
Peter Ujfalusi7729cf72009-10-29 11:58:10 +02001460 /* Supply for the digital part (APLL) */
Peter Ujfalusi7729cf72009-10-29 11:58:10 +02001461 {"Digital Voice Playback Mixer", NULL, "APLL Enable"},
1462
Peter Ujfalusi27eeb1f2010-07-13 12:07:44 +03001463 {"DAC Left1", NULL, "AIF Enable"},
1464 {"DAC Right1", NULL, "AIF Enable"},
1465 {"DAC Left2", NULL, "AIF Enable"},
1466 {"DAC Right1", NULL, "AIF Enable"},
1467
Peter Ujfalusic42a59e2010-02-09 15:24:04 +02001468 {"Digital R2 Playback Mixer", NULL, "AIF Enable"},
1469 {"Digital L2 Playback Mixer", NULL, "AIF Enable"},
1470
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001471 {"Analog L1 Playback Mixer", NULL, "Digital L1 Playback Mixer"},
1472 {"Analog R1 Playback Mixer", NULL, "Digital R1 Playback Mixer"},
1473 {"Analog L2 Playback Mixer", NULL, "Digital L2 Playback Mixer"},
1474 {"Analog R2 Playback Mixer", NULL, "Digital R2 Playback Mixer"},
1475 {"Analog Voice Playback Mixer", NULL, "Digital Voice Playback Mixer"},
Joonyoung Shim1a787e72009-04-22 13:13:34 +09001476
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001477 /* Internal playback routings */
1478 /* Earpiece */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001479 {"Earpiece Mixer", "Voice", "Analog Voice Playback Mixer"},
1480 {"Earpiece Mixer", "AudioL1", "Analog L1 Playback Mixer"},
1481 {"Earpiece Mixer", "AudioL2", "Analog L2 Playback Mixer"},
1482 {"Earpiece Mixer", "AudioR1", "Analog R1 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001483 {"Earpiece PGA", NULL, "Earpiece Mixer"},
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +02001484 /* PreDrivL */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001485 {"PredriveL Mixer", "Voice", "Analog Voice Playback Mixer"},
1486 {"PredriveL Mixer", "AudioL1", "Analog L1 Playback Mixer"},
1487 {"PredriveL Mixer", "AudioL2", "Analog L2 Playback Mixer"},
1488 {"PredriveL Mixer", "AudioR2", "Analog R2 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001489 {"PredriveL PGA", NULL, "PredriveL Mixer"},
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +02001490 /* PreDrivR */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001491 {"PredriveR Mixer", "Voice", "Analog Voice Playback Mixer"},
1492 {"PredriveR Mixer", "AudioR1", "Analog R1 Playback Mixer"},
1493 {"PredriveR Mixer", "AudioR2", "Analog R2 Playback Mixer"},
1494 {"PredriveR Mixer", "AudioL2", "Analog L2 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001495 {"PredriveR PGA", NULL, "PredriveR Mixer"},
Peter Ujfalusidfad21a2008-12-09 12:35:49 +02001496 /* HeadsetL */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001497 {"HeadsetL Mixer", "Voice", "Analog Voice Playback Mixer"},
1498 {"HeadsetL Mixer", "AudioL1", "Analog L1 Playback Mixer"},
1499 {"HeadsetL Mixer", "AudioL2", "Analog L2 Playback Mixer"},
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001500 {"HeadsetL PGA", NULL, "HeadsetL Mixer"},
Peter Ujfalusidfad21a2008-12-09 12:35:49 +02001501 /* HeadsetR */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001502 {"HeadsetR Mixer", "Voice", "Analog Voice Playback Mixer"},
1503 {"HeadsetR Mixer", "AudioR1", "Analog R1 Playback Mixer"},
1504 {"HeadsetR Mixer", "AudioR2", "Analog R2 Playback Mixer"},
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001505 {"HeadsetR PGA", NULL, "HeadsetR Mixer"},
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +02001506 /* CarkitL */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001507 {"CarkitL Mixer", "Voice", "Analog Voice Playback Mixer"},
1508 {"CarkitL Mixer", "AudioL1", "Analog L1 Playback Mixer"},
1509 {"CarkitL Mixer", "AudioL2", "Analog L2 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001510 {"CarkitL PGA", NULL, "CarkitL Mixer"},
Peter Ujfalusi5152d8c2008-12-09 12:35:50 +02001511 /* CarkitR */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001512 {"CarkitR Mixer", "Voice", "Analog Voice Playback Mixer"},
1513 {"CarkitR Mixer", "AudioR1", "Analog R1 Playback Mixer"},
1514 {"CarkitR Mixer", "AudioR2", "Analog R2 Playback Mixer"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001515 {"CarkitR PGA", NULL, "CarkitR Mixer"},
Peter Ujfalusidf339802008-12-09 12:35:51 +02001516 /* HandsfreeL */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001517 {"HandsfreeL Mux", "Voice", "Analog Voice Playback Mixer"},
1518 {"HandsfreeL Mux", "AudioL1", "Analog L1 Playback Mixer"},
1519 {"HandsfreeL Mux", "AudioL2", "Analog L2 Playback Mixer"},
1520 {"HandsfreeL Mux", "AudioR2", "Analog R2 Playback Mixer"},
Lopez Cruz, Misaele3c7dbb2009-06-25 12:36:14 -05001521 {"HandsfreeL", "Switch", "HandsfreeL Mux"},
1522 {"HandsfreeL PGA", NULL, "HandsfreeL"},
Peter Ujfalusidf339802008-12-09 12:35:51 +02001523 /* HandsfreeR */
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001524 {"HandsfreeR Mux", "Voice", "Analog Voice Playback Mixer"},
1525 {"HandsfreeR Mux", "AudioR1", "Analog R1 Playback Mixer"},
1526 {"HandsfreeR Mux", "AudioR2", "Analog R2 Playback Mixer"},
1527 {"HandsfreeR Mux", "AudioL2", "Analog L2 Playback Mixer"},
Lopez Cruz, Misaele3c7dbb2009-06-25 12:36:14 -05001528 {"HandsfreeR", "Switch", "HandsfreeR Mux"},
1529 {"HandsfreeR PGA", NULL, "HandsfreeR"},
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001530 /* Vibra */
1531 {"Vibra Mux", "AudioL1", "DAC Left1"},
1532 {"Vibra Mux", "AudioR1", "DAC Right1"},
1533 {"Vibra Mux", "AudioL2", "DAC Left2"},
1534 {"Vibra Mux", "AudioR2", "DAC Right2"},
Peter Ujfalusi5e98a462008-12-09 12:35:47 +02001535
Steve Sakomancc175572008-10-30 21:35:26 -07001536 /* outputs */
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +03001537 /* Must be always connected (for AIF and APLL) */
Peter Ujfalusi27eeb1f2010-07-13 12:07:44 +03001538 {"Virtual HiFi OUT", NULL, "DAC Left1"},
1539 {"Virtual HiFi OUT", NULL, "DAC Right1"},
1540 {"Virtual HiFi OUT", NULL, "DAC Left2"},
1541 {"Virtual HiFi OUT", NULL, "DAC Right2"},
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +03001542 /* Must be always connected (for APLL) */
1543 {"Virtual Voice OUT", NULL, "Digital Voice Playback Mixer"},
1544 /* Physical outputs */
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001545 {"EARPIECE", NULL, "Earpiece PGA"},
1546 {"PREDRIVEL", NULL, "PredriveL PGA"},
1547 {"PREDRIVER", NULL, "PredriveR PGA"},
Peter Ujfalusi6943c922009-05-18 16:02:05 +03001548 {"HSOL", NULL, "HeadsetL PGA"},
1549 {"HSOR", NULL, "HeadsetR PGA"},
Peter Ujfalusi9008adf2009-08-13 15:59:34 +03001550 {"CARKITL", NULL, "CarkitL PGA"},
1551 {"CARKITR", NULL, "CarkitR PGA"},
Peter Ujfalusi5a2e9a42009-05-25 11:12:11 +03001552 {"HFL", NULL, "HandsfreeL PGA"},
1553 {"HFR", NULL, "HandsfreeR PGA"},
Peter Ujfalusi376f7832009-05-05 08:55:47 +03001554 {"Vibra Route", "Audio", "Vibra Mux"},
1555 {"VIBRA", NULL, "Vibra Route"},
Steve Sakomancc175572008-10-30 21:35:26 -07001556
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001557 /* Capture path */
Peter Ujfalusi7b4c7342010-04-29 10:58:08 +03001558 /* Must be always connected (for AIF and APLL) */
1559 {"ADC Virtual Left1", NULL, "Virtual HiFi IN"},
1560 {"ADC Virtual Right1", NULL, "Virtual HiFi IN"},
1561 {"ADC Virtual Left2", NULL, "Virtual HiFi IN"},
1562 {"ADC Virtual Right2", NULL, "Virtual HiFi IN"},
1563 /* Physical inputs */
Peter Ujfalusi90289352009-08-14 08:44:00 +03001564 {"Analog Left", "Main Mic Capture Switch", "MAINMIC"},
1565 {"Analog Left", "Headset Mic Capture Switch", "HSMIC"},
1566 {"Analog Left", "AUXL Capture Switch", "AUXL"},
1567 {"Analog Left", "Carkit Mic Capture Switch", "CARKITMIC"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001568
Peter Ujfalusi90289352009-08-14 08:44:00 +03001569 {"Analog Right", "Sub Mic Capture Switch", "SUBMIC"},
1570 {"Analog Right", "AUXR Capture Switch", "AUXR"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001571
Peter Ujfalusi90289352009-08-14 08:44:00 +03001572 {"ADC Physical Left", NULL, "Analog Left"},
1573 {"ADC Physical Right", NULL, "Analog Right"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001574
1575 {"Digimic0 Enable", NULL, "DIGIMIC0"},
1576 {"Digimic1 Enable", NULL, "DIGIMIC1"},
1577
Peter Ujfalusibda7d2a2010-08-03 12:01:01 +03001578 {"DIGIMIC0", NULL, "micbias1 select"},
1579 {"DIGIMIC1", NULL, "micbias2 select"},
1580
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001581 /* TX1 Left capture path */
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001582 {"TX1 Capture Route", "Analog", "ADC Physical Left"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001583 {"TX1 Capture Route", "Digimic0", "Digimic0 Enable"},
1584 /* TX1 Right capture path */
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001585 {"TX1 Capture Route", "Analog", "ADC Physical Right"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001586 {"TX1 Capture Route", "Digimic0", "Digimic0 Enable"},
1587 /* TX2 Left capture path */
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001588 {"TX2 Capture Route", "Analog", "ADC Physical Left"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001589 {"TX2 Capture Route", "Digimic1", "Digimic1 Enable"},
1590 /* TX2 Right capture path */
Peter Ujfalusifb2a2f82009-01-27 11:29:42 +02001591 {"TX2 Capture Route", "Analog", "ADC Physical Right"},
Peter Ujfalusi276c6222008-12-31 10:08:38 +02001592 {"TX2 Capture Route", "Digimic1", "Digimic1 Enable"},
1593
1594 {"ADC Virtual Left1", NULL, "TX1 Capture Route"},
1595 {"ADC Virtual Right1", NULL, "TX1 Capture Route"},
1596 {"ADC Virtual Left2", NULL, "TX2 Capture Route"},
1597 {"ADC Virtual Right2", NULL, "TX2 Capture Route"},
1598
Peter Ujfalusic42a59e2010-02-09 15:24:04 +02001599 {"ADC Virtual Left1", NULL, "AIF Enable"},
1600 {"ADC Virtual Right1", NULL, "AIF Enable"},
1601 {"ADC Virtual Left2", NULL, "AIF Enable"},
1602 {"ADC Virtual Right2", NULL, "AIF Enable"},
1603
Peter Ujfalusi73939582009-01-29 14:57:50 +02001604 /* Analog bypass routes */
Peter Ujfalusi90289352009-08-14 08:44:00 +03001605 {"Right1 Analog Loopback", "Switch", "Analog Right"},
1606 {"Left1 Analog Loopback", "Switch", "Analog Left"},
1607 {"Right2 Analog Loopback", "Switch", "Analog Right"},
1608 {"Left2 Analog Loopback", "Switch", "Analog Left"},
1609 {"Voice Analog Loopback", "Switch", "Analog Left"},
Peter Ujfalusi73939582009-01-29 14:57:50 +02001610
Peter Ujfalusi78e08e22009-10-28 10:57:04 +02001611 /* Supply for the Analog loopbacks */
1612 {"Right1 Analog Loopback", NULL, "FM Loop Enable"},
1613 {"Left1 Analog Loopback", NULL, "FM Loop Enable"},
1614 {"Right2 Analog Loopback", NULL, "FM Loop Enable"},
1615 {"Left2 Analog Loopback", NULL, "FM Loop Enable"},
1616 {"Voice Analog Loopback", NULL, "FM Loop Enable"},
1617
Peter Ujfalusi73939582009-01-29 14:57:50 +02001618 {"Analog R1 Playback Mixer", NULL, "Right1 Analog Loopback"},
1619 {"Analog L1 Playback Mixer", NULL, "Left1 Analog Loopback"},
1620 {"Analog R2 Playback Mixer", NULL, "Right2 Analog Loopback"},
1621 {"Analog L2 Playback Mixer", NULL, "Left2 Analog Loopback"},
Lopez Cruz, Misaelfcd274a2009-04-30 21:47:22 -05001622 {"Analog Voice Playback Mixer", NULL, "Voice Analog Loopback"},
Peter Ujfalusi73939582009-01-29 14:57:50 +02001623
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001624 /* Digital bypass routes */
1625 {"Right Digital Loopback", "Volume", "TX1 Capture Route"},
1626 {"Left Digital Loopback", "Volume", "TX1 Capture Route"},
Lopez Cruz, Misaelee8f6892009-04-30 21:48:08 -05001627 {"Voice Digital Loopback", "Volume", "TX2 Capture Route"},
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001628
Peter Ujfalusi4005d392009-05-18 16:02:04 +03001629 {"Digital R2 Playback Mixer", NULL, "Right Digital Loopback"},
1630 {"Digital L2 Playback Mixer", NULL, "Left Digital Loopback"},
1631 {"Digital Voice Playback Mixer", NULL, "Voice Digital Loopback"},
Peter Ujfalusi6bab83f2009-02-18 14:39:05 +02001632
Steve Sakomancc175572008-10-30 21:35:26 -07001633};
1634
Steve Sakomancc175572008-10-30 21:35:26 -07001635static int twl4030_set_bias_level(struct snd_soc_codec *codec,
1636 enum snd_soc_bias_level level)
1637{
1638 switch (level) {
1639 case SND_SOC_BIAS_ON:
Steve Sakomancc175572008-10-30 21:35:26 -07001640 break;
1641 case SND_SOC_BIAS_PREPARE:
Steve Sakomancc175572008-10-30 21:35:26 -07001642 break;
1643 case SND_SOC_BIAS_STANDBY:
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001644 if (codec->dapm.bias_level == SND_SOC_BIAS_OFF)
Peter Ujfalusiee4ccac72010-05-26 11:38:17 +03001645 twl4030_codec_enable(codec, 1);
Steve Sakomancc175572008-10-30 21:35:26 -07001646 break;
1647 case SND_SOC_BIAS_OFF:
Peter Ujfalusicbd2db12010-05-26 11:38:15 +03001648 twl4030_codec_enable(codec, 0);
Steve Sakomancc175572008-10-30 21:35:26 -07001649 break;
1650 }
Liam Girdwoodce6120c2010-11-05 15:53:46 +02001651 codec->dapm.bias_level = level;
Steve Sakomancc175572008-10-30 21:35:26 -07001652
1653 return 0;
1654}
1655
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001656static void twl4030_constraints(struct twl4030_priv *twl4030,
1657 struct snd_pcm_substream *mst_substream)
1658{
1659 struct snd_pcm_substream *slv_substream;
1660
1661 /* Pick the stream, which need to be constrained */
1662 if (mst_substream == twl4030->master_substream)
1663 slv_substream = twl4030->slave_substream;
1664 else if (mst_substream == twl4030->slave_substream)
1665 slv_substream = twl4030->master_substream;
1666 else /* This should not happen.. */
1667 return;
1668
1669 /* Set the constraints according to the already configured stream */
1670 snd_pcm_hw_constraint_minmax(slv_substream->runtime,
1671 SNDRV_PCM_HW_PARAM_RATE,
1672 twl4030->rate,
1673 twl4030->rate);
1674
1675 snd_pcm_hw_constraint_minmax(slv_substream->runtime,
1676 SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
1677 twl4030->sample_bits,
1678 twl4030->sample_bits);
1679
1680 snd_pcm_hw_constraint_minmax(slv_substream->runtime,
1681 SNDRV_PCM_HW_PARAM_CHANNELS,
1682 twl4030->channels,
1683 twl4030->channels);
1684}
1685
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001686/* In case of 4 channel mode, the RX1 L/R for playback and the TX2 L/R for
1687 * capture has to be enabled/disabled. */
1688static void twl4030_tdm_enable(struct snd_soc_codec *codec, int direction,
1689 int enable)
1690{
1691 u8 reg, mask;
1692
1693 reg = twl4030_read_reg_cache(codec, TWL4030_REG_OPTION);
1694
1695 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1696 mask = TWL4030_ARXL1_VRX_EN | TWL4030_ARXR1_EN;
1697 else
1698 mask = TWL4030_ATXL2_VTXL_EN | TWL4030_ATXR2_VTXR_EN;
1699
1700 if (enable)
1701 reg |= mask;
1702 else
1703 reg &= ~mask;
1704
1705 twl4030_write(codec, TWL4030_REG_OPTION, reg);
1706}
1707
Peter Ujfalusid6648da2009-04-07 09:14:00 +03001708static int twl4030_startup(struct snd_pcm_substream *substream,
1709 struct snd_soc_dai *dai)
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001710{
Mark Browne6968a12012-04-04 15:58:16 +01001711 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001712 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001713
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001714 if (twl4030->master_substream) {
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001715 twl4030->slave_substream = substream;
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001716 /* The DAI has one configuration for playback and capture, so
1717 * if the DAI has been already configured then constrain this
1718 * substream to match it. */
1719 if (twl4030->configured)
1720 twl4030_constraints(twl4030, twl4030->master_substream);
1721 } else {
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001722 if (!(twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE) &
1723 TWL4030_OPTION_1)) {
1724 /* In option2 4 channel is not supported, set the
1725 * constraint for the first stream for channels, the
1726 * second stream will 'inherit' this cosntraint */
1727 snd_pcm_hw_constraint_minmax(substream->runtime,
1728 SNDRV_PCM_HW_PARAM_CHANNELS,
1729 2, 2);
1730 }
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001731 twl4030->master_substream = substream;
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001732 }
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001733
1734 return 0;
1735}
1736
Peter Ujfalusid6648da2009-04-07 09:14:00 +03001737static void twl4030_shutdown(struct snd_pcm_substream *substream,
1738 struct snd_soc_dai *dai)
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001739{
Mark Browne6968a12012-04-04 15:58:16 +01001740 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001741 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001742
1743 if (twl4030->master_substream == substream)
1744 twl4030->master_substream = twl4030->slave_substream;
1745
1746 twl4030->slave_substream = NULL;
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001747
1748 /* If all streams are closed, or the remaining stream has not yet
1749 * been configured than set the DAI as not configured. */
1750 if (!twl4030->master_substream)
1751 twl4030->configured = 0;
1752 else if (!twl4030->master_substream->runtime->channels)
1753 twl4030->configured = 0;
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001754
1755 /* If the closing substream had 4 channel, do the necessary cleanup */
1756 if (substream->runtime->channels == 4)
1757 twl4030_tdm_enable(codec, substream->stream, 0);
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001758}
1759
Steve Sakomancc175572008-10-30 21:35:26 -07001760static int twl4030_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +00001761 struct snd_pcm_hw_params *params,
1762 struct snd_soc_dai *dai)
Steve Sakomancc175572008-10-30 21:35:26 -07001763{
Mark Browne6968a12012-04-04 15:58:16 +01001764 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001765 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Steve Sakomancc175572008-10-30 21:35:26 -07001766 u8 mode, old_mode, format, old_format;
1767
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001768 /* If the substream has 4 channel, do the necessary setup */
1769 if (params_channels(params) == 4) {
Peter Ujfalusieaf1ac82009-06-01 14:06:40 +03001770 format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1771 mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE);
1772
1773 /* Safety check: are we in the correct operating mode and
1774 * the interface is in TDM mode? */
1775 if ((mode & TWL4030_OPTION_1) &&
1776 ((format & TWL4030_AIF_FORMAT) == TWL4030_AIF_FORMAT_TDM))
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001777 twl4030_tdm_enable(codec, substream->stream, 1);
1778 else
1779 return -EINVAL;
1780 }
1781
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001782 if (twl4030->configured)
1783 /* Ignoring hw_params for already configured DAI */
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02001784 return 0;
1785
Steve Sakomancc175572008-10-30 21:35:26 -07001786 /* bit rate */
1787 old_mode = twl4030_read_reg_cache(codec,
1788 TWL4030_REG_CODEC_MODE) & ~TWL4030_CODECPDZ;
1789 mode = old_mode & ~TWL4030_APLL_RATE;
1790
1791 switch (params_rate(params)) {
1792 case 8000:
1793 mode |= TWL4030_APLL_RATE_8000;
1794 break;
1795 case 11025:
1796 mode |= TWL4030_APLL_RATE_11025;
1797 break;
1798 case 12000:
1799 mode |= TWL4030_APLL_RATE_12000;
1800 break;
1801 case 16000:
1802 mode |= TWL4030_APLL_RATE_16000;
1803 break;
1804 case 22050:
1805 mode |= TWL4030_APLL_RATE_22050;
1806 break;
1807 case 24000:
1808 mode |= TWL4030_APLL_RATE_24000;
1809 break;
1810 case 32000:
1811 mode |= TWL4030_APLL_RATE_32000;
1812 break;
1813 case 44100:
1814 mode |= TWL4030_APLL_RATE_44100;
1815 break;
1816 case 48000:
1817 mode |= TWL4030_APLL_RATE_48000;
1818 break;
Peter Ujfalusi103f2112009-04-03 14:39:05 +03001819 case 96000:
1820 mode |= TWL4030_APLL_RATE_96000;
1821 break;
Steve Sakomancc175572008-10-30 21:35:26 -07001822 default:
Peter Ujfalusi3b8a0792012-02-21 09:34:20 +02001823 dev_err(codec->dev, "%s: unknown rate %d\n", __func__,
Steve Sakomancc175572008-10-30 21:35:26 -07001824 params_rate(params));
1825 return -EINVAL;
1826 }
1827
Steve Sakomancc175572008-10-30 21:35:26 -07001828 /* sample size */
1829 old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1830 format = old_format;
1831 format &= ~TWL4030_DATA_WIDTH;
1832 switch (params_format(params)) {
1833 case SNDRV_PCM_FORMAT_S16_LE:
1834 format |= TWL4030_DATA_WIDTH_16S_16W;
1835 break;
Peter Ujfalusidcdeda42010-12-14 13:45:29 +02001836 case SNDRV_PCM_FORMAT_S32_LE:
Steve Sakomancc175572008-10-30 21:35:26 -07001837 format |= TWL4030_DATA_WIDTH_32S_24W;
1838 break;
1839 default:
Peter Ujfalusi3b8a0792012-02-21 09:34:20 +02001840 dev_err(codec->dev, "%s: unknown format %d\n", __func__,
Steve Sakomancc175572008-10-30 21:35:26 -07001841 params_format(params));
1842 return -EINVAL;
1843 }
1844
Peter Ujfalusi2046f172010-05-26 11:38:20 +03001845 if (format != old_format || mode != old_mode) {
1846 if (twl4030->codec_powered) {
1847 /*
1848 * If the codec is powered, than we need to toggle the
1849 * codec power.
1850 */
1851 twl4030_codec_enable(codec, 0);
1852 twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode);
1853 twl4030_write(codec, TWL4030_REG_AUDIO_IF, format);
1854 twl4030_codec_enable(codec, 1);
1855 } else {
1856 twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode);
1857 twl4030_write(codec, TWL4030_REG_AUDIO_IF, format);
1858 }
Steve Sakomancc175572008-10-30 21:35:26 -07001859 }
Peter Ujfalusi6b87a912009-04-17 15:55:08 +03001860
1861 /* Store the important parameters for the DAI configuration and set
1862 * the DAI as configured */
1863 twl4030->configured = 1;
1864 twl4030->rate = params_rate(params);
1865 twl4030->sample_bits = hw_param_interval(params,
1866 SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
1867 twl4030->channels = params_channels(params);
1868
1869 /* If both playback and capture streams are open, and one of them
1870 * is setting the hw parameters right now (since we are here), set
1871 * constraints to the other stream to match the current one. */
1872 if (twl4030->slave_substream)
1873 twl4030_constraints(twl4030, substream);
1874
Steve Sakomancc175572008-10-30 21:35:26 -07001875 return 0;
1876}
1877
1878static int twl4030_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1879 int clk_id, unsigned int freq, int dir)
1880{
1881 struct snd_soc_codec *codec = codec_dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001882 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Steve Sakomancc175572008-10-30 21:35:26 -07001883
1884 switch (freq) {
1885 case 19200000:
Steve Sakomancc175572008-10-30 21:35:26 -07001886 case 26000000:
Steve Sakomancc175572008-10-30 21:35:26 -07001887 case 38400000:
Steve Sakomancc175572008-10-30 21:35:26 -07001888 break;
1889 default:
Peter Ujfalusi3b8a0792012-02-21 09:34:20 +02001890 dev_err(codec->dev, "Unsupported HFCLKIN: %u\n", freq);
Steve Sakomancc175572008-10-30 21:35:26 -07001891 return -EINVAL;
1892 }
1893
Peter Ujfalusi68d01952009-11-04 09:58:20 +02001894 if ((freq / 1000) != twl4030->sysclk) {
1895 dev_err(codec->dev,
Peter Ujfalusi3b8a0792012-02-21 09:34:20 +02001896 "Mismatch in HFCLKIN: %u (configured: %u)\n",
Peter Ujfalusi68d01952009-11-04 09:58:20 +02001897 freq, twl4030->sysclk * 1000);
1898 return -EINVAL;
1899 }
Steve Sakomancc175572008-10-30 21:35:26 -07001900
1901 return 0;
1902}
1903
1904static int twl4030_set_dai_fmt(struct snd_soc_dai *codec_dai,
1905 unsigned int fmt)
1906{
1907 struct snd_soc_codec *codec = codec_dai->codec;
Peter Ujfalusi2046f172010-05-26 11:38:20 +03001908 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Steve Sakomancc175572008-10-30 21:35:26 -07001909 u8 old_format, format;
1910
1911 /* get format */
1912 old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1913 format = old_format;
1914
1915 /* set master/slave audio interface */
1916 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1917 case SND_SOC_DAIFMT_CBM_CFM:
1918 format &= ~(TWL4030_AIF_SLAVE_EN);
Grazvydas Ignotase18c94d2008-11-05 23:51:05 +02001919 format &= ~(TWL4030_CLK256FS_EN);
Steve Sakomancc175572008-10-30 21:35:26 -07001920 break;
1921 case SND_SOC_DAIFMT_CBS_CFS:
Steve Sakomancc175572008-10-30 21:35:26 -07001922 format |= TWL4030_AIF_SLAVE_EN;
Grazvydas Ignotase18c94d2008-11-05 23:51:05 +02001923 format |= TWL4030_CLK256FS_EN;
Steve Sakomancc175572008-10-30 21:35:26 -07001924 break;
1925 default:
1926 return -EINVAL;
1927 }
1928
1929 /* interface format */
1930 format &= ~TWL4030_AIF_FORMAT;
1931 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1932 case SND_SOC_DAIFMT_I2S:
1933 format |= TWL4030_AIF_FORMAT_CODEC;
1934 break;
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03001935 case SND_SOC_DAIFMT_DSP_A:
1936 format |= TWL4030_AIF_FORMAT_TDM;
1937 break;
Steve Sakomancc175572008-10-30 21:35:26 -07001938 default:
1939 return -EINVAL;
1940 }
1941
1942 if (format != old_format) {
Peter Ujfalusi2046f172010-05-26 11:38:20 +03001943 if (twl4030->codec_powered) {
1944 /*
1945 * If the codec is powered, than we need to toggle the
1946 * codec power.
1947 */
1948 twl4030_codec_enable(codec, 0);
1949 twl4030_write(codec, TWL4030_REG_AUDIO_IF, format);
1950 twl4030_codec_enable(codec, 1);
1951 } else {
1952 twl4030_write(codec, TWL4030_REG_AUDIO_IF, format);
1953 }
Steve Sakomancc175572008-10-30 21:35:26 -07001954 }
1955
1956 return 0;
1957}
1958
Lopez Cruz, Misael68140442009-07-03 02:21:39 -05001959static int twl4030_set_tristate(struct snd_soc_dai *dai, int tristate)
1960{
1961 struct snd_soc_codec *codec = dai->codec;
1962 u8 reg = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1963
1964 if (tristate)
1965 reg |= TWL4030_AIF_TRI_EN;
1966 else
1967 reg &= ~TWL4030_AIF_TRI_EN;
1968
1969 return twl4030_write(codec, TWL4030_REG_AUDIO_IF, reg);
1970}
1971
Misael Lopez Cruzb7a755a2009-05-17 20:02:31 -05001972/* In case of voice mode, the RX1 L(VRX) for downlink and the TX2 L/R
1973 * (VTXL, VTXR) for uplink has to be enabled/disabled. */
1974static void twl4030_voice_enable(struct snd_soc_codec *codec, int direction,
1975 int enable)
1976{
1977 u8 reg, mask;
1978
1979 reg = twl4030_read_reg_cache(codec, TWL4030_REG_OPTION);
1980
1981 if (direction == SNDRV_PCM_STREAM_PLAYBACK)
1982 mask = TWL4030_ARXL1_VRX_EN;
1983 else
1984 mask = TWL4030_ATXL2_VTXL_EN | TWL4030_ATXR2_VTXR_EN;
1985
1986 if (enable)
1987 reg |= mask;
1988 else
1989 reg &= ~mask;
1990
1991 twl4030_write(codec, TWL4030_REG_OPTION, reg);
1992}
1993
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09001994static int twl4030_voice_startup(struct snd_pcm_substream *substream,
1995 struct snd_soc_dai *dai)
1996{
Mark Browne6968a12012-04-04 15:58:16 +01001997 struct snd_soc_codec *codec = dai->codec;
Mark Brownb2c812e2010-04-14 15:35:19 +09001998 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09001999 u8 mode;
2000
2001 /* If the system master clock is not 26MHz, the voice PCM interface is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002002 * not available.
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002003 */
Peter Ujfalusi68d01952009-11-04 09:58:20 +02002004 if (twl4030->sysclk != 26000) {
Peter Ujfalusi3b8a0792012-02-21 09:34:20 +02002005 dev_err(codec->dev,
2006 "%s: HFCLKIN is %u KHz, voice interface needs 26MHz\n",
2007 __func__, twl4030->sysclk);
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002008 return -EINVAL;
2009 }
2010
2011 /* If the codec mode is not option2, the voice PCM interface is not
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002012 * available.
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002013 */
2014 mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE)
2015 & TWL4030_OPT_MODE;
2016
2017 if (mode != TWL4030_OPTION_2) {
Peter Ujfalusi3b8a0792012-02-21 09:34:20 +02002018 dev_err(codec->dev, "%s: the codec mode is not option2\n",
2019 __func__);
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002020 return -EINVAL;
2021 }
2022
2023 return 0;
2024}
2025
Misael Lopez Cruzb7a755a2009-05-17 20:02:31 -05002026static void twl4030_voice_shutdown(struct snd_pcm_substream *substream,
2027 struct snd_soc_dai *dai)
2028{
Mark Browne6968a12012-04-04 15:58:16 +01002029 struct snd_soc_codec *codec = dai->codec;
Misael Lopez Cruzb7a755a2009-05-17 20:02:31 -05002030
2031 /* Enable voice digital filters */
2032 twl4030_voice_enable(codec, substream->stream, 0);
2033}
2034
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002035static int twl4030_voice_hw_params(struct snd_pcm_substream *substream,
2036 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
2037{
Mark Browne6968a12012-04-04 15:58:16 +01002038 struct snd_soc_codec *codec = dai->codec;
Peter Ujfalusi2046f172010-05-26 11:38:20 +03002039 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002040 u8 old_mode, mode;
2041
Misael Lopez Cruzb7a755a2009-05-17 20:02:31 -05002042 /* Enable voice digital filters */
2043 twl4030_voice_enable(codec, substream->stream, 1);
2044
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002045 /* bit rate */
2046 old_mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE)
2047 & ~(TWL4030_CODECPDZ);
2048 mode = old_mode;
2049
2050 switch (params_rate(params)) {
2051 case 8000:
2052 mode &= ~(TWL4030_SEL_16K);
2053 break;
2054 case 16000:
2055 mode |= TWL4030_SEL_16K;
2056 break;
2057 default:
Peter Ujfalusi3b8a0792012-02-21 09:34:20 +02002058 dev_err(codec->dev, "%s: unknown rate %d\n", __func__,
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002059 params_rate(params));
2060 return -EINVAL;
2061 }
2062
2063 if (mode != old_mode) {
Peter Ujfalusi2046f172010-05-26 11:38:20 +03002064 if (twl4030->codec_powered) {
2065 /*
2066 * If the codec is powered, than we need to toggle the
2067 * codec power.
2068 */
2069 twl4030_codec_enable(codec, 0);
2070 twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode);
2071 twl4030_codec_enable(codec, 1);
2072 } else {
2073 twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode);
2074 }
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002075 }
2076
2077 return 0;
2078}
2079
2080static int twl4030_voice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
2081 int clk_id, unsigned int freq, int dir)
2082{
2083 struct snd_soc_codec *codec = codec_dai->codec;
Takashi Iwaid4a8ca22010-04-20 08:20:31 +02002084 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002085
Peter Ujfalusi68d01952009-11-04 09:58:20 +02002086 if (freq != 26000000) {
Peter Ujfalusi3b8a0792012-02-21 09:34:20 +02002087 dev_err(codec->dev,
2088 "%s: HFCLKIN is %u KHz, voice interface needs 26MHz\n",
2089 __func__, freq / 1000);
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002090 return -EINVAL;
2091 }
Peter Ujfalusi68d01952009-11-04 09:58:20 +02002092 if ((freq / 1000) != twl4030->sysclk) {
2093 dev_err(codec->dev,
Peter Ujfalusi3b8a0792012-02-21 09:34:20 +02002094 "Mismatch in HFCLKIN: %u (configured: %u)\n",
Peter Ujfalusi68d01952009-11-04 09:58:20 +02002095 freq, twl4030->sysclk * 1000);
2096 return -EINVAL;
2097 }
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002098 return 0;
2099}
2100
2101static int twl4030_voice_set_dai_fmt(struct snd_soc_dai *codec_dai,
2102 unsigned int fmt)
2103{
2104 struct snd_soc_codec *codec = codec_dai->codec;
Peter Ujfalusi2046f172010-05-26 11:38:20 +03002105 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002106 u8 old_format, format;
2107
2108 /* get format */
2109 old_format = twl4030_read_reg_cache(codec, TWL4030_REG_VOICE_IF);
2110 format = old_format;
2111
2112 /* set master/slave audio interface */
2113 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
Lopez Cruz, Misaelc2643012009-06-19 03:23:42 -05002114 case SND_SOC_DAIFMT_CBM_CFM:
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002115 format &= ~(TWL4030_VIF_SLAVE_EN);
2116 break;
2117 case SND_SOC_DAIFMT_CBS_CFS:
2118 format |= TWL4030_VIF_SLAVE_EN;
2119 break;
2120 default:
2121 return -EINVAL;
2122 }
2123
2124 /* clock inversion */
2125 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
2126 case SND_SOC_DAIFMT_IB_NF:
2127 format &= ~(TWL4030_VIF_FORMAT);
2128 break;
2129 case SND_SOC_DAIFMT_NB_IF:
2130 format |= TWL4030_VIF_FORMAT;
2131 break;
2132 default:
2133 return -EINVAL;
2134 }
2135
2136 if (format != old_format) {
Peter Ujfalusi2046f172010-05-26 11:38:20 +03002137 if (twl4030->codec_powered) {
2138 /*
2139 * If the codec is powered, than we need to toggle the
2140 * codec power.
2141 */
2142 twl4030_codec_enable(codec, 0);
2143 twl4030_write(codec, TWL4030_REG_VOICE_IF, format);
2144 twl4030_codec_enable(codec, 1);
2145 } else {
2146 twl4030_write(codec, TWL4030_REG_VOICE_IF, format);
2147 }
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002148 }
2149
2150 return 0;
2151}
2152
Lopez Cruz, Misael68140442009-07-03 02:21:39 -05002153static int twl4030_voice_set_tristate(struct snd_soc_dai *dai, int tristate)
2154{
2155 struct snd_soc_codec *codec = dai->codec;
2156 u8 reg = twl4030_read_reg_cache(codec, TWL4030_REG_VOICE_IF);
2157
2158 if (tristate)
2159 reg |= TWL4030_VIF_TRI_EN;
2160 else
2161 reg &= ~TWL4030_VIF_TRI_EN;
2162
2163 return twl4030_write(codec, TWL4030_REG_VOICE_IF, reg);
2164}
2165
Jarkko Nikulabbba9442008-11-12 17:05:41 +02002166#define TWL4030_RATES (SNDRV_PCM_RATE_8000_48000)
Peter Ujfalusidcdeda42010-12-14 13:45:29 +02002167#define TWL4030_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S32_LE)
Steve Sakomancc175572008-10-30 21:35:26 -07002168
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01002169static const struct snd_soc_dai_ops twl4030_dai_hifi_ops = {
Peter Ujfalusi7220b9f2009-03-27 10:39:08 +02002170 .startup = twl4030_startup,
2171 .shutdown = twl4030_shutdown,
Joonyoung Shim10d9e3d2009-03-16 21:23:35 +09002172 .hw_params = twl4030_hw_params,
2173 .set_sysclk = twl4030_set_dai_sysclk,
2174 .set_fmt = twl4030_set_dai_fmt,
Lopez Cruz, Misael68140442009-07-03 02:21:39 -05002175 .set_tristate = twl4030_set_tristate,
Joonyoung Shim10d9e3d2009-03-16 21:23:35 +09002176};
2177
Lars-Peter Clausen85e76522011-11-23 11:40:40 +01002178static const struct snd_soc_dai_ops twl4030_dai_voice_ops = {
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002179 .startup = twl4030_voice_startup,
Misael Lopez Cruzb7a755a2009-05-17 20:02:31 -05002180 .shutdown = twl4030_voice_shutdown,
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002181 .hw_params = twl4030_voice_hw_params,
2182 .set_sysclk = twl4030_voice_set_dai_sysclk,
2183 .set_fmt = twl4030_voice_set_dai_fmt,
Lopez Cruz, Misael68140442009-07-03 02:21:39 -05002184 .set_tristate = twl4030_voice_set_tristate,
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002185};
2186
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002187static struct snd_soc_dai_driver twl4030_dai[] = {
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002188{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002189 .name = "twl4030-hifi",
Steve Sakomancc175572008-10-30 21:35:26 -07002190 .playback = {
Peter Ujfalusib4852b72009-05-22 15:12:15 +03002191 .stream_name = "HiFi Playback",
Steve Sakomancc175572008-10-30 21:35:26 -07002192 .channels_min = 2,
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03002193 .channels_max = 4,
Peter Ujfalusi31ad0f32009-03-27 10:39:07 +02002194 .rates = TWL4030_RATES | SNDRV_PCM_RATE_96000,
Peter Ujfalusi8819f652012-01-18 12:18:26 +01002195 .formats = TWL4030_FORMATS,
2196 .sig_bits = 24,},
Steve Sakomancc175572008-10-30 21:35:26 -07002197 .capture = {
Peter Ujfalusi7f51e7d2012-09-20 16:32:02 +03002198 .stream_name = "HiFi Capture",
Steve Sakomancc175572008-10-30 21:35:26 -07002199 .channels_min = 2,
Peter Ujfalusi8a1f9362009-04-23 14:36:49 +03002200 .channels_max = 4,
Steve Sakomancc175572008-10-30 21:35:26 -07002201 .rates = TWL4030_RATES,
Peter Ujfalusi8819f652012-01-18 12:18:26 +01002202 .formats = TWL4030_FORMATS,
2203 .sig_bits = 24,},
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002204 .ops = &twl4030_dai_hifi_ops,
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002205},
2206{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002207 .name = "twl4030-voice",
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002208 .playback = {
Peter Ujfalusib4852b72009-05-22 15:12:15 +03002209 .stream_name = "Voice Playback",
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002210 .channels_min = 1,
2211 .channels_max = 1,
2212 .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
2213 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
2214 .capture = {
Peter Ujfalusi7f51e7d2012-09-20 16:32:02 +03002215 .stream_name = "Voice Capture",
Joonyoung Shim7154b3e2009-04-20 19:21:35 +09002216 .channels_min = 1,
2217 .channels_max = 2,
2218 .rates = SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000,
2219 .formats = SNDRV_PCM_FMTBIT_S16_LE,},
2220 .ops = &twl4030_dai_voice_ops,
2221},
Steve Sakomancc175572008-10-30 21:35:26 -07002222};
Steve Sakomancc175572008-10-30 21:35:26 -07002223
Lars-Peter Clausen84b315e2011-12-02 10:18:28 +01002224static int twl4030_soc_suspend(struct snd_soc_codec *codec)
Steve Sakomancc175572008-10-30 21:35:26 -07002225{
Steve Sakomancc175572008-10-30 21:35:26 -07002226 twl4030_set_bias_level(codec, SND_SOC_BIAS_OFF);
Steve Sakomancc175572008-10-30 21:35:26 -07002227 return 0;
2228}
2229
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002230static int twl4030_soc_resume(struct snd_soc_codec *codec)
Steve Sakomancc175572008-10-30 21:35:26 -07002231{
Steve Sakomancc175572008-10-30 21:35:26 -07002232 twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
Steve Sakomancc175572008-10-30 21:35:26 -07002233 return 0;
2234}
2235
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002236static int twl4030_soc_probe(struct snd_soc_codec *codec)
Steve Sakomancc175572008-10-30 21:35:26 -07002237{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002238 struct twl4030_priv *twl4030;
Steve Sakomancc175572008-10-30 21:35:26 -07002239
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002240 twl4030 = kzalloc(sizeof(struct twl4030_priv), GFP_KERNEL);
2241 if (twl4030 == NULL) {
Peter Ujfalusi3b8a0792012-02-21 09:34:20 +02002242 dev_err(codec->dev, "Can not allocate memory\n");
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002243 return -ENOMEM;
Steve Sakomancc175572008-10-30 21:35:26 -07002244 }
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002245 snd_soc_codec_set_drvdata(codec, twl4030);
2246 /* Set the defaults, and power up the codec */
Peter Ujfalusi57fe7252011-05-31 12:02:49 +03002247 twl4030->sysclk = twl4030_audio_get_mclk() / 1000;
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002248
2249 twl4030_init_chip(codec);
Steve Sakomancc175572008-10-30 21:35:26 -07002250
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002251 return 0;
Steve Sakomancc175572008-10-30 21:35:26 -07002252}
2253
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002254static int twl4030_soc_remove(struct snd_soc_codec *codec)
Steve Sakomancc175572008-10-30 21:35:26 -07002255{
Peter Ujfalusi281ecd12012-09-10 13:46:27 +03002256 struct twl4030_codec_data *pdata = dev_get_platdata(codec->dev);
Axel Lin5b3b0fa2010-11-19 17:31:08 +08002257 struct twl4030_priv *twl4030 = snd_soc_codec_get_drvdata(codec);
2258
Peter Ujfalusi5dcba5d2010-08-12 09:29:52 +03002259 /* Reset registers to their chip default before leaving */
2260 twl4030_reset_registers(codec);
Peter Ujfalusi73939582009-01-29 14:57:50 +02002261 twl4030_set_bias_level(codec, SND_SOC_BIAS_OFF);
Axel Lin5b3b0fa2010-11-19 17:31:08 +08002262 kfree(twl4030);
Peter Ujfalusi281ecd12012-09-10 13:46:27 +03002263
2264 if (pdata && pdata->hs_extmute && gpio_is_valid(pdata->hs_extmute_gpio))
2265 gpio_free(pdata->hs_extmute_gpio);
2266
Steve Sakomancc175572008-10-30 21:35:26 -07002267 return 0;
2268}
2269
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002270static struct snd_soc_codec_driver soc_codec_dev_twl4030 = {
2271 .probe = twl4030_soc_probe,
2272 .remove = twl4030_soc_remove,
2273 .suspend = twl4030_soc_suspend,
2274 .resume = twl4030_soc_resume,
2275 .read = twl4030_read_reg_cache,
2276 .write = twl4030_write,
2277 .set_bias_level = twl4030_set_bias_level,
Axel Lineb3032f2012-01-27 18:02:09 +08002278 .idle_bias_off = true,
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002279 .reg_cache_size = sizeof(twl4030_reg),
2280 .reg_word_size = sizeof(u8),
2281 .reg_cache_default = twl4030_reg,
Peter Ujfalusif7c93f02011-10-11 13:11:32 +03002282
2283 .controls = twl4030_snd_controls,
2284 .num_controls = ARRAY_SIZE(twl4030_snd_controls),
2285 .dapm_widgets = twl4030_dapm_widgets,
2286 .num_dapm_widgets = ARRAY_SIZE(twl4030_dapm_widgets),
2287 .dapm_routes = intercon,
2288 .num_dapm_routes = ARRAY_SIZE(intercon),
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002289};
2290
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002291static int __devinit twl4030_codec_probe(struct platform_device *pdev)
2292{
Peter Ujfalusi4ae6df5e2011-05-31 15:21:13 +03002293 struct twl4030_codec_data *pdata = pdev->dev.platform_data;
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002294
Peter Ujfalusi68d01952009-11-04 09:58:20 +02002295 if (!pdata) {
2296 dev_err(&pdev->dev, "platform_data is missing\n");
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002297 return -EINVAL;
2298 }
2299
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002300 return snd_soc_register_codec(&pdev->dev, &soc_codec_dev_twl4030,
2301 twl4030_dai, ARRAY_SIZE(twl4030_dai));
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002302}
2303
2304static int __devexit twl4030_codec_remove(struct platform_device *pdev)
2305{
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002306 snd_soc_unregister_codec(&pdev->dev);
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002307 return 0;
2308}
2309
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002310MODULE_ALIAS("platform:twl4030-codec");
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002311
2312static struct platform_driver twl4030_codec_driver = {
2313 .probe = twl4030_codec_probe,
2314 .remove = __devexit_p(twl4030_codec_remove),
2315 .driver = {
Liam Girdwoodf0fba2a2010-03-17 20:15:21 +00002316 .name = "twl4030-codec",
Peter Ujfalusi7a1fecf2009-10-22 13:26:48 +03002317 .owner = THIS_MODULE,
2318 },
Steve Sakomancc175572008-10-30 21:35:26 -07002319};
Steve Sakomancc175572008-10-30 21:35:26 -07002320
Mark Brown5bbcc3c2011-11-23 22:52:08 +00002321module_platform_driver(twl4030_codec_driver);
Mark Brown64089b82008-12-08 19:17:58 +00002322
Steve Sakomancc175572008-10-30 21:35:26 -07002323MODULE_DESCRIPTION("ASoC TWL4030 codec driver");
2324MODULE_AUTHOR("Steve Sakoman");
2325MODULE_LICENSE("GPL");