blob: 86ff5a9ffa7f844d802d6eec7478452d7d2ba8aa [file] [log] [blame]
Steve Sakomancc175572008-10-30 21:35:26 -07001/*
2 * ALSA SoC TWL4030 codec driver
3 *
4 * Author: Steve Sakoman, <steve@sakoman.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18 * 02110-1301 USA
19 *
20 */
21
22#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/init.h>
25#include <linux/delay.h>
26#include <linux/pm.h>
27#include <linux/i2c.h>
28#include <linux/platform_device.h>
29#include <linux/i2c/twl4030.h>
30#include <sound/core.h>
31#include <sound/pcm.h>
32#include <sound/pcm_params.h>
33#include <sound/soc.h>
34#include <sound/soc-dapm.h>
35#include <sound/initval.h>
Peter Ujfalusic10b82c2008-11-24 13:49:35 +020036#include <sound/tlv.h>
Steve Sakomancc175572008-10-30 21:35:26 -070037
38#include "twl4030.h"
39
40/*
41 * twl4030 register cache & default register settings
42 */
43static const u8 twl4030_reg[TWL4030_CACHEREGNUM] = {
44 0x00, /* this register not used */
45 0x93, /* REG_CODEC_MODE (0x1) */
46 0xc3, /* REG_OPTION (0x2) */
47 0x00, /* REG_UNKNOWN (0x3) */
48 0x00, /* REG_MICBIAS_CTL (0x4) */
Grazvydas Ignotas5920b452008-12-02 20:48:58 +020049 0x20, /* REG_ANAMICL (0x5) */
50 0x00, /* REG_ANAMICR (0x6) */
51 0x00, /* REG_AVADC_CTL (0x7) */
Steve Sakomancc175572008-10-30 21:35:26 -070052 0x00, /* REG_ADCMICSEL (0x8) */
53 0x00, /* REG_DIGMIXING (0x9) */
54 0x0c, /* REG_ATXL1PGA (0xA) */
55 0x0c, /* REG_ATXR1PGA (0xB) */
56 0x00, /* REG_AVTXL2PGA (0xC) */
57 0x00, /* REG_AVTXR2PGA (0xD) */
58 0x01, /* REG_AUDIO_IF (0xE) */
59 0x00, /* REG_VOICE_IF (0xF) */
60 0x00, /* REG_ARXR1PGA (0x10) */
61 0x00, /* REG_ARXL1PGA (0x11) */
62 0x6c, /* REG_ARXR2PGA (0x12) */
63 0x6c, /* REG_ARXL2PGA (0x13) */
64 0x00, /* REG_VRXPGA (0x14) */
65 0x00, /* REG_VSTPGA (0x15) */
66 0x00, /* REG_VRX2ARXPGA (0x16) */
67 0x0c, /* REG_AVDAC_CTL (0x17) */
68 0x00, /* REG_ARX2VTXPGA (0x18) */
69 0x00, /* REG_ARXL1_APGA_CTL (0x19) */
70 0x00, /* REG_ARXR1_APGA_CTL (0x1A) */
71 0x4b, /* REG_ARXL2_APGA_CTL (0x1B) */
72 0x4b, /* REG_ARXR2_APGA_CTL (0x1C) */
73 0x00, /* REG_ATX2ARXPGA (0x1D) */
74 0x00, /* REG_BT_IF (0x1E) */
75 0x00, /* REG_BTPGA (0x1F) */
76 0x00, /* REG_BTSTPGA (0x20) */
77 0x00, /* REG_EAR_CTL (0x21) */
78 0x24, /* REG_HS_SEL (0x22) */
79 0x0a, /* REG_HS_GAIN_SET (0x23) */
80 0x00, /* REG_HS_POPN_SET (0x24) */
81 0x00, /* REG_PREDL_CTL (0x25) */
82 0x00, /* REG_PREDR_CTL (0x26) */
83 0x00, /* REG_PRECKL_CTL (0x27) */
84 0x00, /* REG_PRECKR_CTL (0x28) */
85 0x00, /* REG_HFL_CTL (0x29) */
86 0x00, /* REG_HFR_CTL (0x2A) */
87 0x00, /* REG_ALC_CTL (0x2B) */
88 0x00, /* REG_ALC_SET1 (0x2C) */
89 0x00, /* REG_ALC_SET2 (0x2D) */
90 0x00, /* REG_BOOST_CTL (0x2E) */
Peter Ujfalusif8d05bd2008-11-24 08:25:45 +020091 0x00, /* REG_SOFTVOL_CTL (0x2F) */
Steve Sakomancc175572008-10-30 21:35:26 -070092 0x00, /* REG_DTMF_FREQSEL (0x30) */
93 0x00, /* REG_DTMF_TONEXT1H (0x31) */
94 0x00, /* REG_DTMF_TONEXT1L (0x32) */
95 0x00, /* REG_DTMF_TONEXT2H (0x33) */
96 0x00, /* REG_DTMF_TONEXT2L (0x34) */
97 0x00, /* REG_DTMF_TONOFF (0x35) */
98 0x00, /* REG_DTMF_WANONOFF (0x36) */
99 0x00, /* REG_I2S_RX_SCRAMBLE_H (0x37) */
100 0x00, /* REG_I2S_RX_SCRAMBLE_M (0x38) */
101 0x00, /* REG_I2S_RX_SCRAMBLE_L (0x39) */
102 0x16, /* REG_APLL_CTL (0x3A) */
103 0x00, /* REG_DTMF_CTL (0x3B) */
104 0x00, /* REG_DTMF_PGA_CTL2 (0x3C) */
105 0x00, /* REG_DTMF_PGA_CTL1 (0x3D) */
106 0x00, /* REG_MISC_SET_1 (0x3E) */
107 0x00, /* REG_PCMBTMUX (0x3F) */
108 0x00, /* not used (0x40) */
109 0x00, /* not used (0x41) */
110 0x00, /* not used (0x42) */
111 0x00, /* REG_RX_PATH_SEL (0x43) */
112 0x00, /* REG_VDL_APGA_CTL (0x44) */
113 0x00, /* REG_VIBRA_CTL (0x45) */
114 0x00, /* REG_VIBRA_SET (0x46) */
115 0x00, /* REG_VIBRA_PWM_SET (0x47) */
116 0x00, /* REG_ANAMIC_GAIN (0x48) */
117 0x00, /* REG_MISC_SET_2 (0x49) */
118};
119
120/*
121 * read twl4030 register cache
122 */
123static inline unsigned int twl4030_read_reg_cache(struct snd_soc_codec *codec,
124 unsigned int reg)
125{
126 u8 *cache = codec->reg_cache;
127
128 return cache[reg];
129}
130
131/*
132 * write twl4030 register cache
133 */
134static inline void twl4030_write_reg_cache(struct snd_soc_codec *codec,
135 u8 reg, u8 value)
136{
137 u8 *cache = codec->reg_cache;
138
139 if (reg >= TWL4030_CACHEREGNUM)
140 return;
141 cache[reg] = value;
142}
143
144/*
145 * write to the twl4030 register space
146 */
147static int twl4030_write(struct snd_soc_codec *codec,
148 unsigned int reg, unsigned int value)
149{
150 twl4030_write_reg_cache(codec, reg, value);
151 return twl4030_i2c_write_u8(TWL4030_MODULE_AUDIO_VOICE, value, reg);
152}
153
154static void twl4030_clear_codecpdz(struct snd_soc_codec *codec)
155{
156 u8 mode;
157
158 mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE);
159 twl4030_write(codec, TWL4030_REG_CODEC_MODE,
160 mode & ~TWL4030_CODECPDZ);
161
162 /* REVISIT: this delay is present in TI sample drivers */
163 /* but there seems to be no TRM requirement for it */
164 udelay(10);
165}
166
167static void twl4030_set_codecpdz(struct snd_soc_codec *codec)
168{
169 u8 mode;
170
171 mode = twl4030_read_reg_cache(codec, TWL4030_REG_CODEC_MODE);
172 twl4030_write(codec, TWL4030_REG_CODEC_MODE,
173 mode | TWL4030_CODECPDZ);
174
175 /* REVISIT: this delay is present in TI sample drivers */
176 /* but there seems to be no TRM requirement for it */
177 udelay(10);
178}
179
180static void twl4030_init_chip(struct snd_soc_codec *codec)
181{
182 int i;
183
184 /* clear CODECPDZ prior to setting register defaults */
185 twl4030_clear_codecpdz(codec);
186
187 /* set all audio section registers to reasonable defaults */
188 for (i = TWL4030_REG_OPTION; i <= TWL4030_REG_MISC_SET_2; i++)
189 twl4030_write(codec, i, twl4030_reg[i]);
190
191}
192
Peter Ujfalusi5e98a462008-12-09 12:35:47 +0200193/* Earpiece */
194static const char *twl4030_earpiece_texts[] =
195 {"Off", "DACL1", "DACL2", "Invalid",
196 "DACR1"};
197
198static const struct soc_enum twl4030_earpiece_enum =
199 SOC_ENUM_SINGLE(TWL4030_REG_EAR_CTL, 1,
200 ARRAY_SIZE(twl4030_earpiece_texts),
201 twl4030_earpiece_texts);
202
203static const struct snd_kcontrol_new twl4030_dapm_earpiece_control =
204SOC_DAPM_ENUM("Route", twl4030_earpiece_enum);
205
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +0200206/* PreDrive Left */
207static const char *twl4030_predrivel_texts[] =
208 {"Off", "DACL1", "DACL2", "Invalid",
209 "DACR2"};
210
211static const struct soc_enum twl4030_predrivel_enum =
212 SOC_ENUM_SINGLE(TWL4030_REG_PREDL_CTL, 1,
213 ARRAY_SIZE(twl4030_predrivel_texts),
214 twl4030_predrivel_texts);
215
216static const struct snd_kcontrol_new twl4030_dapm_predrivel_control =
217SOC_DAPM_ENUM("Route", twl4030_predrivel_enum);
218
219/* PreDrive Right */
220static const char *twl4030_predriver_texts[] =
221 {"Off", "DACR1", "DACR2", "Invalid",
222 "DACL2"};
223
224static const struct soc_enum twl4030_predriver_enum =
225 SOC_ENUM_SINGLE(TWL4030_REG_PREDR_CTL, 1,
226 ARRAY_SIZE(twl4030_predriver_texts),
227 twl4030_predriver_texts);
228
229static const struct snd_kcontrol_new twl4030_dapm_predriver_control =
230SOC_DAPM_ENUM("Route", twl4030_predriver_enum);
231
Peter Ujfalusidfad21a2008-12-09 12:35:49 +0200232/* Headset Left */
233static const char *twl4030_hsol_texts[] =
234 {"Off", "DACL1", "DACL2"};
235
236static const struct soc_enum twl4030_hsol_enum =
237 SOC_ENUM_SINGLE(TWL4030_REG_HS_SEL, 1,
238 ARRAY_SIZE(twl4030_hsol_texts),
239 twl4030_hsol_texts);
240
241static const struct snd_kcontrol_new twl4030_dapm_hsol_control =
242SOC_DAPM_ENUM("Route", twl4030_hsol_enum);
243
244/* Headset Right */
245static const char *twl4030_hsor_texts[] =
246 {"Off", "DACR1", "DACR2"};
247
248static const struct soc_enum twl4030_hsor_enum =
249 SOC_ENUM_SINGLE(TWL4030_REG_HS_SEL, 4,
250 ARRAY_SIZE(twl4030_hsor_texts),
251 twl4030_hsor_texts);
252
253static const struct snd_kcontrol_new twl4030_dapm_hsor_control =
254SOC_DAPM_ENUM("Route", twl4030_hsor_enum);
255
Peter Ujfalusie8ff9c42008-12-09 12:35:46 +0200256static int outmixer_event(struct snd_soc_dapm_widget *w,
257 struct snd_kcontrol *kcontrol, int event)
258{
259 struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
260 int ret = 0;
261 int val;
262
263 switch (e->reg) {
264 case TWL4030_REG_PREDL_CTL:
265 case TWL4030_REG_PREDR_CTL:
266 case TWL4030_REG_EAR_CTL:
267 val = w->value >> e->shift_l;
268 if (val == 3) {
269 printk(KERN_WARNING
270 "Invalid MUX setting for register 0x%02x (%d)\n",
271 e->reg, val);
272 ret = -1;
273 }
274 break;
275 }
276
277 return ret;
278}
279
Peter Ujfalusic10b82c2008-11-24 13:49:35 +0200280/*
Peter Ujfalusib0bd53a2008-11-24 13:49:38 +0200281 * Some of the gain controls in TWL (mostly those which are associated with
282 * the outputs) are implemented in an interesting way:
283 * 0x0 : Power down (mute)
284 * 0x1 : 6dB
285 * 0x2 : 0 dB
286 * 0x3 : -6 dB
287 * Inverting not going to help with these.
288 * Custom volsw and volsw_2r get/put functions to handle these gain bits.
289 */
290#define SOC_DOUBLE_TLV_TWL4030(xname, xreg, shift_left, shift_right, xmax,\
291 xinvert, tlv_array) \
292{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
293 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
294 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
295 .tlv.p = (tlv_array), \
296 .info = snd_soc_info_volsw, \
297 .get = snd_soc_get_volsw_twl4030, \
298 .put = snd_soc_put_volsw_twl4030, \
299 .private_value = (unsigned long)&(struct soc_mixer_control) \
300 {.reg = xreg, .shift = shift_left, .rshift = shift_right,\
301 .max = xmax, .invert = xinvert} }
302#define SOC_DOUBLE_R_TLV_TWL4030(xname, reg_left, reg_right, xshift, xmax,\
303 xinvert, tlv_array) \
304{ .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
305 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
306 SNDRV_CTL_ELEM_ACCESS_READWRITE,\
307 .tlv.p = (tlv_array), \
308 .info = snd_soc_info_volsw_2r, \
309 .get = snd_soc_get_volsw_r2_twl4030,\
310 .put = snd_soc_put_volsw_r2_twl4030, \
311 .private_value = (unsigned long)&(struct soc_mixer_control) \
312 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
313 .max = xmax, .invert = xinvert} }
314#define SOC_SINGLE_TLV_TWL4030(xname, xreg, xshift, xmax, xinvert, tlv_array) \
315 SOC_DOUBLE_TLV_TWL4030(xname, xreg, xshift, xshift, xmax, \
316 xinvert, tlv_array)
317
318static int snd_soc_get_volsw_twl4030(struct snd_kcontrol *kcontrol,
319 struct snd_ctl_elem_value *ucontrol)
320{
321 struct soc_mixer_control *mc =
322 (struct soc_mixer_control *)kcontrol->private_value;
323 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
324 unsigned int reg = mc->reg;
325 unsigned int shift = mc->shift;
326 unsigned int rshift = mc->rshift;
327 int max = mc->max;
328 int mask = (1 << fls(max)) - 1;
329
330 ucontrol->value.integer.value[0] =
331 (snd_soc_read(codec, reg) >> shift) & mask;
332 if (ucontrol->value.integer.value[0])
333 ucontrol->value.integer.value[0] =
334 max + 1 - ucontrol->value.integer.value[0];
335
336 if (shift != rshift) {
337 ucontrol->value.integer.value[1] =
338 (snd_soc_read(codec, reg) >> rshift) & mask;
339 if (ucontrol->value.integer.value[1])
340 ucontrol->value.integer.value[1] =
341 max + 1 - ucontrol->value.integer.value[1];
342 }
343
344 return 0;
345}
346
347static int snd_soc_put_volsw_twl4030(struct snd_kcontrol *kcontrol,
348 struct snd_ctl_elem_value *ucontrol)
349{
350 struct soc_mixer_control *mc =
351 (struct soc_mixer_control *)kcontrol->private_value;
352 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
353 unsigned int reg = mc->reg;
354 unsigned int shift = mc->shift;
355 unsigned int rshift = mc->rshift;
356 int max = mc->max;
357 int mask = (1 << fls(max)) - 1;
358 unsigned short val, val2, val_mask;
359
360 val = (ucontrol->value.integer.value[0] & mask);
361
362 val_mask = mask << shift;
363 if (val)
364 val = max + 1 - val;
365 val = val << shift;
366 if (shift != rshift) {
367 val2 = (ucontrol->value.integer.value[1] & mask);
368 val_mask |= mask << rshift;
369 if (val2)
370 val2 = max + 1 - val2;
371 val |= val2 << rshift;
372 }
373 return snd_soc_update_bits(codec, reg, val_mask, val);
374}
375
376static int snd_soc_get_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
377 struct snd_ctl_elem_value *ucontrol)
378{
379 struct soc_mixer_control *mc =
380 (struct soc_mixer_control *)kcontrol->private_value;
381 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
382 unsigned int reg = mc->reg;
383 unsigned int reg2 = mc->rreg;
384 unsigned int shift = mc->shift;
385 int max = mc->max;
386 int mask = (1<<fls(max))-1;
387
388 ucontrol->value.integer.value[0] =
389 (snd_soc_read(codec, reg) >> shift) & mask;
390 ucontrol->value.integer.value[1] =
391 (snd_soc_read(codec, reg2) >> shift) & mask;
392
393 if (ucontrol->value.integer.value[0])
394 ucontrol->value.integer.value[0] =
395 max + 1 - ucontrol->value.integer.value[0];
396 if (ucontrol->value.integer.value[1])
397 ucontrol->value.integer.value[1] =
398 max + 1 - ucontrol->value.integer.value[1];
399
400 return 0;
401}
402
403static int snd_soc_put_volsw_r2_twl4030(struct snd_kcontrol *kcontrol,
404 struct snd_ctl_elem_value *ucontrol)
405{
406 struct soc_mixer_control *mc =
407 (struct soc_mixer_control *)kcontrol->private_value;
408 struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
409 unsigned int reg = mc->reg;
410 unsigned int reg2 = mc->rreg;
411 unsigned int shift = mc->shift;
412 int max = mc->max;
413 int mask = (1 << fls(max)) - 1;
414 int err;
415 unsigned short val, val2, val_mask;
416
417 val_mask = mask << shift;
418 val = (ucontrol->value.integer.value[0] & mask);
419 val2 = (ucontrol->value.integer.value[1] & mask);
420
421 if (val)
422 val = max + 1 - val;
423 if (val2)
424 val2 = max + 1 - val2;
425
426 val = val << shift;
427 val2 = val2 << shift;
428
429 err = snd_soc_update_bits(codec, reg, val_mask, val);
430 if (err < 0)
431 return err;
432
433 err = snd_soc_update_bits(codec, reg2, val_mask, val2);
434 return err;
435}
436
Grazvydas Ignotas5920b452008-12-02 20:48:58 +0200437static int twl4030_get_left_input(struct snd_kcontrol *kcontrol,
438 struct snd_ctl_elem_value *ucontrol)
439{
440 struct snd_soc_codec *codec = kcontrol->private_data;
441 u8 reg = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICL);
442 int result = 0;
443
444 /* one bit must be set a time */
445 reg &= TWL4030_CKMIC_EN | TWL4030_AUXL_EN | TWL4030_HSMIC_EN
446 | TWL4030_MAINMIC_EN;
447 if (reg != 0) {
448 result++;
449 while ((reg & 1) == 0) {
450 result++;
451 reg >>= 1;
452 }
453 }
454
455 ucontrol->value.integer.value[0] = result;
456 return 0;
457}
458
459static int twl4030_put_left_input(struct snd_kcontrol *kcontrol,
460 struct snd_ctl_elem_value *ucontrol)
461{
462 struct snd_soc_codec *codec = kcontrol->private_data;
463 int value = ucontrol->value.integer.value[0];
464 u8 anamicl, micbias, avadc_ctl;
465
466 anamicl = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICL);
467 anamicl &= ~(TWL4030_CKMIC_EN | TWL4030_AUXL_EN | TWL4030_HSMIC_EN
468 | TWL4030_MAINMIC_EN);
469 micbias = twl4030_read_reg_cache(codec, TWL4030_REG_MICBIAS_CTL);
470 micbias &= ~(TWL4030_HSMICBIAS_EN | TWL4030_MICBIAS1_EN);
471 avadc_ctl = twl4030_read_reg_cache(codec, TWL4030_REG_AVADC_CTL);
472
473 switch (value) {
474 case 1:
475 anamicl |= TWL4030_MAINMIC_EN;
476 micbias |= TWL4030_MICBIAS1_EN;
477 break;
478 case 2:
479 anamicl |= TWL4030_HSMIC_EN;
480 micbias |= TWL4030_HSMICBIAS_EN;
481 break;
482 case 3:
483 anamicl |= TWL4030_AUXL_EN;
484 break;
485 case 4:
486 anamicl |= TWL4030_CKMIC_EN;
487 break;
488 default:
489 break;
490 }
491
492 /* If some input is selected, enable amp and ADC */
493 if (value != 0) {
494 anamicl |= TWL4030_MICAMPL_EN;
495 avadc_ctl |= TWL4030_ADCL_EN;
496 } else {
497 anamicl &= ~TWL4030_MICAMPL_EN;
498 avadc_ctl &= ~TWL4030_ADCL_EN;
499 }
500
501 twl4030_write(codec, TWL4030_REG_ANAMICL, anamicl);
502 twl4030_write(codec, TWL4030_REG_MICBIAS_CTL, micbias);
503 twl4030_write(codec, TWL4030_REG_AVADC_CTL, avadc_ctl);
504
505 return 1;
506}
507
508static int twl4030_get_right_input(struct snd_kcontrol *kcontrol,
509 struct snd_ctl_elem_value *ucontrol)
510{
511 struct snd_soc_codec *codec = kcontrol->private_data;
512 u8 reg = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICR);
513 int value = 0;
514
515 reg &= TWL4030_SUBMIC_EN|TWL4030_AUXR_EN;
516 switch (reg) {
517 case TWL4030_SUBMIC_EN:
518 value = 1;
519 break;
520 case TWL4030_AUXR_EN:
521 value = 2;
522 break;
523 default:
524 break;
525 }
526
527 ucontrol->value.integer.value[0] = value;
528 return 0;
529}
530
531static int twl4030_put_right_input(struct snd_kcontrol *kcontrol,
532 struct snd_ctl_elem_value *ucontrol)
533{
534 struct snd_soc_codec *codec = kcontrol->private_data;
535 int value = ucontrol->value.integer.value[0];
536 u8 anamicr, micbias, avadc_ctl;
537
538 anamicr = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICR);
539 anamicr &= ~(TWL4030_SUBMIC_EN|TWL4030_AUXR_EN);
540 micbias = twl4030_read_reg_cache(codec, TWL4030_REG_MICBIAS_CTL);
541 micbias &= ~TWL4030_MICBIAS2_EN;
542 avadc_ctl = twl4030_read_reg_cache(codec, TWL4030_REG_AVADC_CTL);
543
544 switch (value) {
545 case 1:
546 anamicr |= TWL4030_SUBMIC_EN;
547 micbias |= TWL4030_MICBIAS2_EN;
548 break;
549 case 2:
550 anamicr |= TWL4030_AUXR_EN;
551 break;
552 default:
553 break;
554 }
555
556 if (value != 0) {
557 anamicr |= TWL4030_MICAMPR_EN;
558 avadc_ctl |= TWL4030_ADCR_EN;
559 } else {
560 anamicr &= ~TWL4030_MICAMPR_EN;
561 avadc_ctl &= ~TWL4030_ADCR_EN;
562 }
563
564 twl4030_write(codec, TWL4030_REG_ANAMICR, anamicr);
565 twl4030_write(codec, TWL4030_REG_MICBIAS_CTL, micbias);
566 twl4030_write(codec, TWL4030_REG_AVADC_CTL, avadc_ctl);
567
568 return 1;
569}
570
571static const char *twl4030_left_in_sel[] = {
572 "None",
573 "Main Mic",
574 "Headset Mic",
575 "Line In",
576 "Carkit Mic",
577};
578
579static const char *twl4030_right_in_sel[] = {
580 "None",
581 "Sub Mic",
582 "Line In",
583};
584
585static const struct soc_enum twl4030_left_input_mux =
586 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl4030_left_in_sel),
587 twl4030_left_in_sel);
588
589static const struct soc_enum twl4030_right_input_mux =
590 SOC_ENUM_SINGLE_EXT(ARRAY_SIZE(twl4030_right_in_sel),
591 twl4030_right_in_sel);
592
Peter Ujfalusib0bd53a2008-11-24 13:49:38 +0200593/*
Peter Ujfalusic10b82c2008-11-24 13:49:35 +0200594 * FGAIN volume control:
595 * from -62 to 0 dB in 1 dB steps (mute instead of -63 dB)
596 */
Peter Ujfalusid889a722008-12-01 10:03:46 +0200597static DECLARE_TLV_DB_SCALE(digital_fine_tlv, -6300, 100, 1);
Peter Ujfalusic10b82c2008-11-24 13:49:35 +0200598
Peter Ujfalusi0d33ea02008-11-24 13:49:36 +0200599/*
600 * CGAIN volume control:
601 * 0 dB to 12 dB in 6 dB steps
602 * value 2 and 3 means 12 dB
603 */
Peter Ujfalusid889a722008-12-01 10:03:46 +0200604static DECLARE_TLV_DB_SCALE(digital_coarse_tlv, 0, 600, 0);
605
606/*
607 * Analog playback gain
608 * -24 dB to 12 dB in 2 dB steps
609 */
610static DECLARE_TLV_DB_SCALE(analog_tlv, -2400, 200, 0);
Peter Ujfalusi0d33ea02008-11-24 13:49:36 +0200611
Peter Ujfalusi381a22b2008-12-01 10:03:45 +0200612/*
Peter Ujfalusi42902392008-12-01 10:03:47 +0200613 * Gain controls tied to outputs
614 * -6 dB to 6 dB in 6 dB steps (mute instead of -12)
615 */
616static DECLARE_TLV_DB_SCALE(output_tvl, -1200, 600, 1);
617
618/*
Peter Ujfalusi381a22b2008-12-01 10:03:45 +0200619 * Capture gain after the ADCs
620 * from 0 dB to 31 dB in 1 dB steps
621 */
622static DECLARE_TLV_DB_SCALE(digital_capture_tlv, 0, 100, 0);
623
Grazvydas Ignotas5920b452008-12-02 20:48:58 +0200624/*
625 * Gain control for input amplifiers
626 * 0 dB to 30 dB in 6 dB steps
627 */
628static DECLARE_TLV_DB_SCALE(input_gain_tlv, 0, 600, 0);
629
Steve Sakomancc175572008-10-30 21:35:26 -0700630static const struct snd_kcontrol_new twl4030_snd_controls[] = {
Peter Ujfalusid889a722008-12-01 10:03:46 +0200631 /* Common playback gain controls */
632 SOC_DOUBLE_R_TLV("DAC1 Digital Fine Playback Volume",
633 TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
634 0, 0x3f, 0, digital_fine_tlv),
635 SOC_DOUBLE_R_TLV("DAC2 Digital Fine Playback Volume",
636 TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
637 0, 0x3f, 0, digital_fine_tlv),
638
639 SOC_DOUBLE_R_TLV("DAC1 Digital Coarse Playback Volume",
640 TWL4030_REG_ARXL1PGA, TWL4030_REG_ARXR1PGA,
641 6, 0x2, 0, digital_coarse_tlv),
642 SOC_DOUBLE_R_TLV("DAC2 Digital Coarse Playback Volume",
643 TWL4030_REG_ARXL2PGA, TWL4030_REG_ARXR2PGA,
644 6, 0x2, 0, digital_coarse_tlv),
645
646 SOC_DOUBLE_R_TLV("DAC1 Analog Playback Volume",
647 TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
648 3, 0x12, 1, analog_tlv),
649 SOC_DOUBLE_R_TLV("DAC2 Analog Playback Volume",
650 TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
651 3, 0x12, 1, analog_tlv),
Peter Ujfalusi44c55872008-12-09 08:45:44 +0200652 SOC_DOUBLE_R("DAC1 Analog Playback Switch",
653 TWL4030_REG_ARXL1_APGA_CTL, TWL4030_REG_ARXR1_APGA_CTL,
654 1, 1, 0),
655 SOC_DOUBLE_R("DAC2 Analog Playback Switch",
656 TWL4030_REG_ARXL2_APGA_CTL, TWL4030_REG_ARXR2_APGA_CTL,
657 1, 1, 0),
Peter Ujfalusi381a22b2008-12-01 10:03:45 +0200658
Peter Ujfalusi42902392008-12-01 10:03:47 +0200659 /* Separate output gain controls */
660 SOC_DOUBLE_R_TLV_TWL4030("PreDriv Playback Volume",
661 TWL4030_REG_PREDL_CTL, TWL4030_REG_PREDR_CTL,
662 4, 3, 0, output_tvl),
663
664 SOC_DOUBLE_TLV_TWL4030("Headset Playback Volume",
665 TWL4030_REG_HS_GAIN_SET, 0, 2, 3, 0, output_tvl),
666
667 SOC_DOUBLE_R_TLV_TWL4030("Carkit Playback Volume",
668 TWL4030_REG_PRECKL_CTL, TWL4030_REG_PRECKR_CTL,
669 4, 3, 0, output_tvl),
670
671 SOC_SINGLE_TLV_TWL4030("Earpiece Playback Volume",
672 TWL4030_REG_EAR_CTL, 4, 3, 0, output_tvl),
673
Peter Ujfalusi381a22b2008-12-01 10:03:45 +0200674 /* Common capture gain controls */
675 SOC_DOUBLE_R_TLV("Capture Volume",
676 TWL4030_REG_ATXL1PGA, TWL4030_REG_ATXR1PGA,
677 0, 0x1f, 0, digital_capture_tlv),
Grazvydas Ignotas5920b452008-12-02 20:48:58 +0200678
679 SOC_DOUBLE_TLV("Input Boost Volume", TWL4030_REG_ANAMIC_GAIN,
680 0, 3, 5, 0, input_gain_tlv),
681
682 /* Input source controls */
683 SOC_ENUM_EXT("Left Input Source", twl4030_left_input_mux,
684 twl4030_get_left_input, twl4030_put_left_input),
685 SOC_ENUM_EXT("Right Input Source", twl4030_right_input_mux,
686 twl4030_get_right_input, twl4030_put_right_input),
Steve Sakomancc175572008-10-30 21:35:26 -0700687};
688
689/* add non dapm controls */
690static int twl4030_add_controls(struct snd_soc_codec *codec)
691{
692 int err, i;
693
694 for (i = 0; i < ARRAY_SIZE(twl4030_snd_controls); i++) {
695 err = snd_ctl_add(codec->card,
696 snd_soc_cnew(&twl4030_snd_controls[i],
697 codec, NULL));
698 if (err < 0)
699 return err;
700 }
701
702 return 0;
703}
704
705static const struct snd_soc_dapm_widget twl4030_dapm_widgets[] = {
706 SND_SOC_DAPM_INPUT("INL"),
707 SND_SOC_DAPM_INPUT("INR"),
708
709 SND_SOC_DAPM_OUTPUT("OUTL"),
710 SND_SOC_DAPM_OUTPUT("OUTR"),
Peter Ujfalusi5e98a462008-12-09 12:35:47 +0200711 SND_SOC_DAPM_OUTPUT("EARPIECE"),
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +0200712 SND_SOC_DAPM_OUTPUT("PREDRIVEL"),
713 SND_SOC_DAPM_OUTPUT("PREDRIVER"),
Peter Ujfalusidfad21a2008-12-09 12:35:49 +0200714 SND_SOC_DAPM_OUTPUT("HSOL"),
715 SND_SOC_DAPM_OUTPUT("HSOR"),
Steve Sakomancc175572008-10-30 21:35:26 -0700716
Peter Ujfalusi53b50472008-12-09 08:45:43 +0200717 /* DACs */
718 SND_SOC_DAPM_DAC("DACR1", "Right Front Playback",
719 TWL4030_REG_AVDAC_CTL, 0, 0),
720 SND_SOC_DAPM_DAC("DACL1", "Left Front Playback",
721 TWL4030_REG_AVDAC_CTL, 1, 0),
722 SND_SOC_DAPM_DAC("DACR2", "Right Rear Playback",
723 TWL4030_REG_AVDAC_CTL, 2, 0),
724 SND_SOC_DAPM_DAC("DACL2", "Left Rear Playback",
725 TWL4030_REG_AVDAC_CTL, 3, 0),
Steve Sakomancc175572008-10-30 21:35:26 -0700726
Peter Ujfalusi44c55872008-12-09 08:45:44 +0200727 /* Analog PGAs */
728 SND_SOC_DAPM_PGA("ARXR1_APGA", TWL4030_REG_ARXR1_APGA_CTL,
729 0, 0, NULL, 0),
730 SND_SOC_DAPM_PGA("ARXL1_APGA", TWL4030_REG_ARXL1_APGA_CTL,
731 0, 0, NULL, 0),
732 SND_SOC_DAPM_PGA("ARXR2_APGA", TWL4030_REG_ARXR2_APGA_CTL,
733 0, 0, NULL, 0),
734 SND_SOC_DAPM_PGA("ARXL2_APGA", TWL4030_REG_ARXL2_APGA_CTL,
735 0, 0, NULL, 0),
736
Peter Ujfalusi5e98a462008-12-09 12:35:47 +0200737 /* Output MUX controls */
738 /* Earpiece */
739 SND_SOC_DAPM_MUX_E("Earpiece Mux", SND_SOC_NOPM, 0, 0,
740 &twl4030_dapm_earpiece_control, outmixer_event,
741 SND_SOC_DAPM_PRE_REG),
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +0200742 /* PreDrivL/R */
743 SND_SOC_DAPM_MUX_E("PredriveL Mux", SND_SOC_NOPM, 0, 0,
744 &twl4030_dapm_predrivel_control, outmixer_event,
745 SND_SOC_DAPM_PRE_REG),
746 SND_SOC_DAPM_MUX_E("PredriveR Mux", SND_SOC_NOPM, 0, 0,
747 &twl4030_dapm_predriver_control, outmixer_event,
748 SND_SOC_DAPM_PRE_REG),
Peter Ujfalusidfad21a2008-12-09 12:35:49 +0200749 /* HeadsetL/R */
750 SND_SOC_DAPM_MUX("HeadsetL Mux", SND_SOC_NOPM, 0, 0,
751 &twl4030_dapm_hsol_control),
752 SND_SOC_DAPM_MUX("HeadsetR Mux", SND_SOC_NOPM, 0, 0,
753 &twl4030_dapm_hsor_control),
Peter Ujfalusi5e98a462008-12-09 12:35:47 +0200754
Steve Sakomancc175572008-10-30 21:35:26 -0700755 SND_SOC_DAPM_ADC("ADCL", "Left Capture", SND_SOC_NOPM, 0, 0),
756 SND_SOC_DAPM_ADC("ADCR", "Right Capture", SND_SOC_NOPM, 0, 0),
757};
758
759static const struct snd_soc_dapm_route intercon[] = {
Peter Ujfalusi44c55872008-12-09 08:45:44 +0200760 {"ARXL1_APGA", NULL, "DACL1"},
761 {"ARXR1_APGA", NULL, "DACR1"},
762 {"ARXL2_APGA", NULL, "DACL2"},
763 {"ARXR2_APGA", NULL, "DACR2"},
764
Peter Ujfalusi5e98a462008-12-09 12:35:47 +0200765 /* Internal playback routings */
766 /* Earpiece */
767 {"Earpiece Mux", "DACL1", "ARXL1_APGA"},
768 {"Earpiece Mux", "DACL2", "ARXL2_APGA"},
769 {"Earpiece Mux", "DACR1", "ARXR1_APGA"},
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +0200770 /* PreDrivL */
771 {"PredriveL Mux", "DACL1", "ARXL1_APGA"},
772 {"PredriveL Mux", "DACL2", "ARXL2_APGA"},
773 {"PredriveL Mux", "DACR2", "ARXR2_APGA"},
774 /* PreDrivR */
775 {"PredriveR Mux", "DACR1", "ARXR1_APGA"},
776 {"PredriveR Mux", "DACR2", "ARXR2_APGA"},
777 {"PredriveR Mux", "DACL2", "ARXL2_APGA"},
Peter Ujfalusidfad21a2008-12-09 12:35:49 +0200778 /* HeadsetL */
779 {"HeadsetL Mux", "DACL1", "ARXL1_APGA"},
780 {"HeadsetL Mux", "DACL2", "ARXL2_APGA"},
781 /* HeadsetR */
782 {"HeadsetR Mux", "DACR1", "ARXR1_APGA"},
783 {"HeadsetR Mux", "DACR2", "ARXR2_APGA"},
Peter Ujfalusi5e98a462008-12-09 12:35:47 +0200784
Steve Sakomancc175572008-10-30 21:35:26 -0700785 /* outputs */
Peter Ujfalusi44c55872008-12-09 08:45:44 +0200786 {"OUTL", NULL, "ARXL2_APGA"},
787 {"OUTR", NULL, "ARXR2_APGA"},
Peter Ujfalusi5e98a462008-12-09 12:35:47 +0200788 {"EARPIECE", NULL, "Earpiece Mux"},
Peter Ujfalusi2a6f5c582008-12-09 12:35:48 +0200789 {"PREDRIVEL", NULL, "PredriveL Mux"},
790 {"PREDRIVER", NULL, "PredriveR Mux"},
Peter Ujfalusidfad21a2008-12-09 12:35:49 +0200791 {"HSOL", NULL, "HeadsetL Mux"},
792 {"HSOR", NULL, "HeadsetR Mux"},
Steve Sakomancc175572008-10-30 21:35:26 -0700793
794 /* inputs */
795 {"ADCL", NULL, "INL"},
796 {"ADCR", NULL, "INR"},
797};
798
799static int twl4030_add_widgets(struct snd_soc_codec *codec)
800{
801 snd_soc_dapm_new_controls(codec, twl4030_dapm_widgets,
802 ARRAY_SIZE(twl4030_dapm_widgets));
803
804 snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
805
806 snd_soc_dapm_new_widgets(codec);
807 return 0;
808}
809
810static void twl4030_power_up(struct snd_soc_codec *codec)
811{
812 u8 anamicl, regmisc1, byte, popn, hsgain;
813 int i = 0;
814
815 /* set CODECPDZ to turn on codec */
816 twl4030_set_codecpdz(codec);
817
818 /* initiate offset cancellation */
819 anamicl = twl4030_read_reg_cache(codec, TWL4030_REG_ANAMICL);
820 twl4030_write(codec, TWL4030_REG_ANAMICL,
821 anamicl | TWL4030_CNCL_OFFSET_START);
822
823 /* wait for offset cancellation to complete */
824 do {
825 /* this takes a little while, so don't slam i2c */
826 udelay(2000);
827 twl4030_i2c_read_u8(TWL4030_MODULE_AUDIO_VOICE, &byte,
828 TWL4030_REG_ANAMICL);
829 } while ((i++ < 100) &&
830 ((byte & TWL4030_CNCL_OFFSET_START) ==
831 TWL4030_CNCL_OFFSET_START));
832
833 /* anti-pop when changing analog gain */
834 regmisc1 = twl4030_read_reg_cache(codec, TWL4030_REG_MISC_SET_1);
835 twl4030_write(codec, TWL4030_REG_MISC_SET_1,
836 regmisc1 | TWL4030_SMOOTH_ANAVOL_EN);
837
838 /* toggle CODECPDZ as per TRM */
839 twl4030_clear_codecpdz(codec);
840 twl4030_set_codecpdz(codec);
841
842 /* program anti-pop with bias ramp delay */
843 popn = twl4030_read_reg_cache(codec, TWL4030_REG_HS_POPN_SET);
844 popn &= TWL4030_RAMP_DELAY;
845 popn |= TWL4030_RAMP_DELAY_645MS;
846 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn);
847 popn |= TWL4030_VMID_EN;
848 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn);
849
850 /* enable output stage and gain setting */
851 hsgain = TWL4030_HSR_GAIN_0DB | TWL4030_HSL_GAIN_0DB;
852 twl4030_write(codec, TWL4030_REG_HS_GAIN_SET, hsgain);
853
854 /* enable anti-pop ramp */
855 popn |= TWL4030_RAMP_EN;
856 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn);
857}
858
859static void twl4030_power_down(struct snd_soc_codec *codec)
860{
861 u8 popn, hsgain;
862
863 /* disable anti-pop ramp */
864 popn = twl4030_read_reg_cache(codec, TWL4030_REG_HS_POPN_SET);
865 popn &= ~TWL4030_RAMP_EN;
866 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn);
867
868 /* disable output stage and gain setting */
869 hsgain = TWL4030_HSR_GAIN_PWR_DOWN | TWL4030_HSL_GAIN_PWR_DOWN;
870 twl4030_write(codec, TWL4030_REG_HS_GAIN_SET, hsgain);
871
872 /* disable bias out */
873 popn &= ~TWL4030_VMID_EN;
874 twl4030_write(codec, TWL4030_REG_HS_POPN_SET, popn);
875
876 /* power down */
877 twl4030_clear_codecpdz(codec);
878}
879
880static int twl4030_set_bias_level(struct snd_soc_codec *codec,
881 enum snd_soc_bias_level level)
882{
883 switch (level) {
884 case SND_SOC_BIAS_ON:
885 twl4030_power_up(codec);
886 break;
887 case SND_SOC_BIAS_PREPARE:
888 /* TODO: develop a twl4030_prepare function */
889 break;
890 case SND_SOC_BIAS_STANDBY:
891 /* TODO: develop a twl4030_standby function */
892 twl4030_power_down(codec);
893 break;
894 case SND_SOC_BIAS_OFF:
895 twl4030_power_down(codec);
896 break;
897 }
898 codec->bias_level = level;
899
900 return 0;
901}
902
903static int twl4030_hw_params(struct snd_pcm_substream *substream,
Mark Browndee89c42008-11-18 22:11:38 +0000904 struct snd_pcm_hw_params *params,
905 struct snd_soc_dai *dai)
Steve Sakomancc175572008-10-30 21:35:26 -0700906{
907 struct snd_soc_pcm_runtime *rtd = substream->private_data;
908 struct snd_soc_device *socdev = rtd->socdev;
909 struct snd_soc_codec *codec = socdev->codec;
910 u8 mode, old_mode, format, old_format;
911
912
913 /* bit rate */
914 old_mode = twl4030_read_reg_cache(codec,
915 TWL4030_REG_CODEC_MODE) & ~TWL4030_CODECPDZ;
916 mode = old_mode & ~TWL4030_APLL_RATE;
917
918 switch (params_rate(params)) {
919 case 8000:
920 mode |= TWL4030_APLL_RATE_8000;
921 break;
922 case 11025:
923 mode |= TWL4030_APLL_RATE_11025;
924 break;
925 case 12000:
926 mode |= TWL4030_APLL_RATE_12000;
927 break;
928 case 16000:
929 mode |= TWL4030_APLL_RATE_16000;
930 break;
931 case 22050:
932 mode |= TWL4030_APLL_RATE_22050;
933 break;
934 case 24000:
935 mode |= TWL4030_APLL_RATE_24000;
936 break;
937 case 32000:
938 mode |= TWL4030_APLL_RATE_32000;
939 break;
940 case 44100:
941 mode |= TWL4030_APLL_RATE_44100;
942 break;
943 case 48000:
944 mode |= TWL4030_APLL_RATE_48000;
945 break;
946 default:
947 printk(KERN_ERR "TWL4030 hw params: unknown rate %d\n",
948 params_rate(params));
949 return -EINVAL;
950 }
951
952 if (mode != old_mode) {
953 /* change rate and set CODECPDZ */
954 twl4030_write(codec, TWL4030_REG_CODEC_MODE, mode);
955 twl4030_set_codecpdz(codec);
956 }
957
958 /* sample size */
959 old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
960 format = old_format;
961 format &= ~TWL4030_DATA_WIDTH;
962 switch (params_format(params)) {
963 case SNDRV_PCM_FORMAT_S16_LE:
964 format |= TWL4030_DATA_WIDTH_16S_16W;
965 break;
966 case SNDRV_PCM_FORMAT_S24_LE:
967 format |= TWL4030_DATA_WIDTH_32S_24W;
968 break;
969 default:
970 printk(KERN_ERR "TWL4030 hw params: unknown format %d\n",
971 params_format(params));
972 return -EINVAL;
973 }
974
975 if (format != old_format) {
976
977 /* clear CODECPDZ before changing format (codec requirement) */
978 twl4030_clear_codecpdz(codec);
979
980 /* change format */
981 twl4030_write(codec, TWL4030_REG_AUDIO_IF, format);
982
983 /* set CODECPDZ afterwards */
984 twl4030_set_codecpdz(codec);
985 }
986 return 0;
987}
988
989static int twl4030_set_dai_sysclk(struct snd_soc_dai *codec_dai,
990 int clk_id, unsigned int freq, int dir)
991{
992 struct snd_soc_codec *codec = codec_dai->codec;
993 u8 infreq;
994
995 switch (freq) {
996 case 19200000:
997 infreq = TWL4030_APLL_INFREQ_19200KHZ;
998 break;
999 case 26000000:
1000 infreq = TWL4030_APLL_INFREQ_26000KHZ;
1001 break;
1002 case 38400000:
1003 infreq = TWL4030_APLL_INFREQ_38400KHZ;
1004 break;
1005 default:
1006 printk(KERN_ERR "TWL4030 set sysclk: unknown rate %d\n",
1007 freq);
1008 return -EINVAL;
1009 }
1010
1011 infreq |= TWL4030_APLL_EN;
1012 twl4030_write(codec, TWL4030_REG_APLL_CTL, infreq);
1013
1014 return 0;
1015}
1016
1017static int twl4030_set_dai_fmt(struct snd_soc_dai *codec_dai,
1018 unsigned int fmt)
1019{
1020 struct snd_soc_codec *codec = codec_dai->codec;
1021 u8 old_format, format;
1022
1023 /* get format */
1024 old_format = twl4030_read_reg_cache(codec, TWL4030_REG_AUDIO_IF);
1025 format = old_format;
1026
1027 /* set master/slave audio interface */
1028 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1029 case SND_SOC_DAIFMT_CBM_CFM:
1030 format &= ~(TWL4030_AIF_SLAVE_EN);
Grazvydas Ignotase18c94d2008-11-05 23:51:05 +02001031 format &= ~(TWL4030_CLK256FS_EN);
Steve Sakomancc175572008-10-30 21:35:26 -07001032 break;
1033 case SND_SOC_DAIFMT_CBS_CFS:
Steve Sakomancc175572008-10-30 21:35:26 -07001034 format |= TWL4030_AIF_SLAVE_EN;
Grazvydas Ignotase18c94d2008-11-05 23:51:05 +02001035 format |= TWL4030_CLK256FS_EN;
Steve Sakomancc175572008-10-30 21:35:26 -07001036 break;
1037 default:
1038 return -EINVAL;
1039 }
1040
1041 /* interface format */
1042 format &= ~TWL4030_AIF_FORMAT;
1043 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1044 case SND_SOC_DAIFMT_I2S:
1045 format |= TWL4030_AIF_FORMAT_CODEC;
1046 break;
1047 default:
1048 return -EINVAL;
1049 }
1050
1051 if (format != old_format) {
1052
1053 /* clear CODECPDZ before changing format (codec requirement) */
1054 twl4030_clear_codecpdz(codec);
1055
1056 /* change format */
1057 twl4030_write(codec, TWL4030_REG_AUDIO_IF, format);
1058
1059 /* set CODECPDZ afterwards */
1060 twl4030_set_codecpdz(codec);
1061 }
1062
1063 return 0;
1064}
1065
Jarkko Nikulabbba9442008-11-12 17:05:41 +02001066#define TWL4030_RATES (SNDRV_PCM_RATE_8000_48000)
Steve Sakomancc175572008-10-30 21:35:26 -07001067#define TWL4030_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FORMAT_S24_LE)
1068
1069struct snd_soc_dai twl4030_dai = {
1070 .name = "twl4030",
1071 .playback = {
1072 .stream_name = "Playback",
1073 .channels_min = 2,
1074 .channels_max = 2,
1075 .rates = TWL4030_RATES,
1076 .formats = TWL4030_FORMATS,},
1077 .capture = {
1078 .stream_name = "Capture",
1079 .channels_min = 2,
1080 .channels_max = 2,
1081 .rates = TWL4030_RATES,
1082 .formats = TWL4030_FORMATS,},
1083 .ops = {
1084 .hw_params = twl4030_hw_params,
Steve Sakomancc175572008-10-30 21:35:26 -07001085 .set_sysclk = twl4030_set_dai_sysclk,
1086 .set_fmt = twl4030_set_dai_fmt,
1087 }
1088};
1089EXPORT_SYMBOL_GPL(twl4030_dai);
1090
1091static int twl4030_suspend(struct platform_device *pdev, pm_message_t state)
1092{
1093 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1094 struct snd_soc_codec *codec = socdev->codec;
1095
1096 twl4030_set_bias_level(codec, SND_SOC_BIAS_OFF);
1097
1098 return 0;
1099}
1100
1101static int twl4030_resume(struct platform_device *pdev)
1102{
1103 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1104 struct snd_soc_codec *codec = socdev->codec;
1105
1106 twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1107 twl4030_set_bias_level(codec, codec->suspend_bias_level);
1108 return 0;
1109}
1110
1111/*
1112 * initialize the driver
1113 * register the mixer and dsp interfaces with the kernel
1114 */
1115
1116static int twl4030_init(struct snd_soc_device *socdev)
1117{
1118 struct snd_soc_codec *codec = socdev->codec;
1119 int ret = 0;
1120
1121 printk(KERN_INFO "TWL4030 Audio Codec init \n");
1122
1123 codec->name = "twl4030";
1124 codec->owner = THIS_MODULE;
1125 codec->read = twl4030_read_reg_cache;
1126 codec->write = twl4030_write;
1127 codec->set_bias_level = twl4030_set_bias_level;
1128 codec->dai = &twl4030_dai;
1129 codec->num_dai = 1;
1130 codec->reg_cache_size = sizeof(twl4030_reg);
1131 codec->reg_cache = kmemdup(twl4030_reg, sizeof(twl4030_reg),
1132 GFP_KERNEL);
1133 if (codec->reg_cache == NULL)
1134 return -ENOMEM;
1135
1136 /* register pcms */
1137 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
1138 if (ret < 0) {
1139 printk(KERN_ERR "twl4030: failed to create pcms\n");
1140 goto pcm_err;
1141 }
1142
1143 twl4030_init_chip(codec);
1144
1145 /* power on device */
1146 twl4030_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1147
1148 twl4030_add_controls(codec);
1149 twl4030_add_widgets(codec);
1150
Mark Brown968a6022008-11-28 11:49:07 +00001151 ret = snd_soc_init_card(socdev);
Steve Sakomancc175572008-10-30 21:35:26 -07001152 if (ret < 0) {
1153 printk(KERN_ERR "twl4030: failed to register card\n");
1154 goto card_err;
1155 }
1156
1157 return ret;
1158
1159card_err:
1160 snd_soc_free_pcms(socdev);
1161 snd_soc_dapm_free(socdev);
1162pcm_err:
1163 kfree(codec->reg_cache);
1164 return ret;
1165}
1166
1167static struct snd_soc_device *twl4030_socdev;
1168
1169static int twl4030_probe(struct platform_device *pdev)
1170{
1171 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1172 struct snd_soc_codec *codec;
1173
1174 codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
1175 if (codec == NULL)
1176 return -ENOMEM;
1177
1178 socdev->codec = codec;
1179 mutex_init(&codec->mutex);
1180 INIT_LIST_HEAD(&codec->dapm_widgets);
1181 INIT_LIST_HEAD(&codec->dapm_paths);
1182
1183 twl4030_socdev = socdev;
1184 twl4030_init(socdev);
1185
1186 return 0;
1187}
1188
1189static int twl4030_remove(struct platform_device *pdev)
1190{
1191 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1192 struct snd_soc_codec *codec = socdev->codec;
1193
1194 printk(KERN_INFO "TWL4030 Audio Codec remove\n");
1195 kfree(codec);
1196
1197 return 0;
1198}
1199
1200struct snd_soc_codec_device soc_codec_dev_twl4030 = {
1201 .probe = twl4030_probe,
1202 .remove = twl4030_remove,
1203 .suspend = twl4030_suspend,
1204 .resume = twl4030_resume,
1205};
1206EXPORT_SYMBOL_GPL(soc_codec_dev_twl4030);
1207
1208MODULE_DESCRIPTION("ASoC TWL4030 codec driver");
1209MODULE_AUTHOR("Steve Sakoman");
1210MODULE_LICENSE("GPL");