Added de-emphasis filter functions and speaker protection.
[aic34-eq] / kernel-2.6.28 / sound / soc / codecs / tlv320aic3x.c
1 /*
2  * ALSA SoC TLV320AIC3X codec driver
3  *
4  * Author:      Vladimir Barinov, <vbarinov@embeddedalley.com>
5  * Copyright:   (C) 2007 MontaVista Software, Inc., <source@mvista.com>
6  *
7  * Based on sound/soc/codecs/wm8753.c by Liam Girdwood
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  *
13  * Notes:
14  *  The AIC3X is a driver for a low power stereo audio
15  *  codecs aic31, aic32, aic33.
16  *
17  *  It supports full aic33 codec functionality.
18  *  The compatibility with aic32, aic31 is as follows:
19  *        aic32        |        aic31
20  *  ---------------------------------------
21  *   MONO_LOUT -> N/A  |  MONO_LOUT -> N/A
22  *                     |  IN1L -> LINE1L
23  *                     |  IN1R -> LINE1R
24  *                     |  IN2L -> LINE2L
25  *                     |  IN2R -> LINE2R
26  *                     |  MIC3L/R -> N/A
27  *   truncated internal functionality in
28  *   accordance with documentation
29  *  ---------------------------------------
30  *
31  *  Hence the machine layer should disable unsupported inputs/outputs by
32  *  snd_soc_dapm_disable_pin(codec, "MONO_LOUT"), etc.
33  */
34
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/init.h>
38 #include <linux/delay.h>
39 #include <linux/pm.h>
40 #include <linux/i2c.h>
41 #include <linux/platform_device.h>
42 #include <sound/core.h>
43 #include <sound/pcm.h>
44 #include <sound/pcm_params.h>
45 #include <sound/soc.h>
46 #include <sound/soc-dapm.h>
47 #include <sound/initval.h>
48 #include <sound/tlv.h>
49 #include <sound/hwdep.h>
50
51 #include "tlv320aic3x.h"
52
53 #define AIC3X_VERSION "0.2"
54
55 static int hp_dac_lim = 9;
56 module_param(hp_dac_lim, int, 0);
57
58 struct aic3x_dacfilter_t {
59         struct aic3x_iir_coeffs coeffs;
60         int state;
61 } aic3x_dacfilter = {
62         .coeffs = {
63           .N0 = 27619, .N1 = -27034, .N2 = 26461, .D1 = 32131, .D2 = -31506,
64       .N3 = 27619, .N4 = -27034, .N5 = 26461, .D4 = 32131, .D5 = -31506,
65         },
66         .state = 0,
67 };
68
69 /* codec private data */
70 struct aic3x_priv {
71         unsigned int sysclk;
72         int master;
73         int prepare_reset;
74         struct snd_hwdep *hwdep;
75         struct aic3x_dacfilter_t dacfilter;
76 };
77
78 /*
79  * AIC3X register cache
80  * We can't read the AIC3X register space when we are
81  * using 2 wire for device control, so we cache them instead.
82  * There is no point in caching the reset register
83  */
84 static const u8 aic3x_reg[AIC3X_CACHEREGNUM] = {
85         0x00, 0x00, 0x00, 0x10, /* 0 */
86         0x04, 0x00, 0x00, 0x00, /* 4 */
87         0x00, 0x00, 0x00, 0x01, /* 8 */
88         0x00, 0x00, 0x00, 0x80, /* 12 */
89         0x80, 0xff, 0xff, 0x78, /* 16 */
90         0x78, 0x78, 0x78, 0x78, /* 20 */
91         0x78, 0x00, 0x00, 0xfe, /* 24 */
92         0x00, 0x00, 0xfe, 0x00, /* 28 */
93         0x00, 0x00, 0x00, 0x00, /* 32 */
94         0x00, 0x00, 0x00, 0x00, /* 36 */
95         0x00, 0x00, 0x00, 0x80, /* 40 */
96         0x80, 0x00, 0x00, 0x00, /* 44 */
97         0x00, 0x00, 0x00, 0x04, /* 48 */
98         0x00, 0x00, 0x00, 0x00, /* 52 */
99         0x00, 0x00, 0x04, 0x00, /* 56 */
100         0x00, 0x00, 0x00, 0x00, /* 60 */
101         0x00, 0x04, 0x00, 0x00, /* 64 */
102         0x00, 0x00, 0x00, 0x00, /* 68 */
103         0x04, 0x00, 0x00, 0x00, /* 72 */
104         0x00, 0x00, 0x00, 0x00, /* 76 */
105         0x00, 0x00, 0x00, 0x00, /* 80 */
106         0x00, 0x00, 0x00, 0x00, /* 84 */
107         0x00, 0x00, 0x00, 0x00, /* 88 */
108         0x00, 0x00, 0x00, 0x00, /* 92 */
109         0x00, 0x00, 0x00, 0x00, /* 96 */
110         0x00, 0x00, 0x02, 0x00, /* 100 */
111         0x00, 0x00, 0x00, 0x00, /* 104 */
112         0x00, 0x00, 0x00, 0x00, /* 108 */
113         0x00, 0x00, 0x00, 0x00, /* 112 */
114         0x00, 0x00, 0x00, 0x00, /* 116 */
115         0x00, 0x00, 0x00, 0x00, /* 120 */
116         0x00, 0x00, 0x00, 0x00, /* 124 */
117         0x01, 0x6b, 0xe3, 0x96, /* 128 */
118         0x66, 0x67, 0x5d, 0x6b, /* 132 */
119         0xe3, 0x96, 0x66, 0x67, /* 136 */
120         0x5d, 0x7d, 0x83, 0x84, /* 140 */
121         0xee, 0x7d, 0x83, 0x84, /* 144 */
122         0xee, 0x39, 0x55, 0xf3, /* 148 */
123         0x2d, 0x53, 0x7e, 0x6b, /* 152 */
124         0xe3, 0x96, 0x66, 0x67, /* 156 */
125         0x5d, 0x6b, 0xe3, 0x96, /* 160 */
126         0x66, 0x67, 0x5d, 0x7d, /* 164 */
127         0x83, 0x84, 0xee, 0x7d, /* 168 */
128         0x83, 0x84, 0xee, 0x39, /* 172 */
129         0x55, 0xf3, 0x2d, 0x53, /* 176 */
130         0x7e, 0x7f, 0xff, 0x00, /* 180 */
131         0x00, 0x00, 0x00, 0x00, /* 184 */
132         0x00, 0x00, 0x00, 0x00, /* 188 */
133         0x00, 0x39, 0x55, 0xf3, /* 192 */
134         0x2d, 0x53, 0x7e, 0x39, /* 196 */
135         0x55, 0xf3, 0x2d, 0x53, /* 200 */
136         0x7e,                   /* 204 */
137 };
138
139 /*
140  * read aic3x register cache
141  */
142 static inline unsigned int aic3x_read_reg_cache(struct snd_soc_codec *codec,
143                                                 unsigned int reg)
144 {
145         u8 *cache = codec->reg_cache;
146         if (reg >= AIC3X_CACHEREGNUM)
147                 return -1;
148         return cache[reg];
149 }
150
151 static inline int aic3x_read_coeff_reg_cache
152         (struct snd_soc_codec *codec, unsigned int msbreg)
153 {
154         int val;
155         val = aic3x_read_reg_cache(codec, msbreg) << 8;
156         val |= aic3x_read_reg_cache(codec, msbreg+1);
157         if(val > 32767) val -= 65536;
158         return val;
159 }
160
161 /*
162  * write aic3x register cache
163  */
164 static inline void aic3x_write_reg_cache(struct snd_soc_codec *codec,
165                                          u8 reg, u8 value)
166 {
167         u8 *cache = codec->reg_cache;
168         if (reg >= AIC3X_CACHEREGNUM)
169                 return;
170         cache[reg] = value;
171 }
172
173 /*
174  * write to the aic3x register space
175  */
176 static int aic3x_write(struct snd_soc_codec *codec,
177                         unsigned int reg, unsigned int value)
178 {
179         static char curpage = -1;
180         u8 data[2], page = 0;
181
182         /*mutex_lock(&codec->mutex);*/
183         
184         if (reg > 127){
185                 reg -= 128;
186                 page = 1;
187         }
188     //printk("MNZ: aic3x_write(reg = %i, val = 0x%x, page = %i)\n",
189     //reg, value, page);
190         if(reg && curpage != page){
191                 data[0] = 0;
192                 data[1] = page;
193                 if (codec->hw_write(codec->control_data, data, 2) != 2){
194                         /*mutex_unlock(&codec->mutex);*/
195                         return -EIO;
196                 } else {
197                         curpage = page;
198                 }
199         }
200
201
202         /* data is
203          *   D15..D8 aic3x register offset
204          *   D7...D0 register data
205          */
206         data[0] = reg;
207         data[1] = value;
208
209         if (codec->hw_write(codec->control_data, data, 2) == 2){
210                 aic3x_write_reg_cache(codec, data[0] + page * 128, data[1]);
211                 if(!reg) curpage = value; /* for reg = 0, ie, page setting */
212                 /*mutex_unlock(&codec->mutex);*/
213                 return 0;
214         } else {
215                 /*mutex_unlock(&codec->mutex);*/
216                 return -EIO;
217         }
218
219     return -EIO;
220 }
221
222 /* Convert a value to 2s compliment and write to registers */
223 static int aic3x_write_coeff(struct snd_soc_codec *codec,
224         u8 msbreg, int value)
225 {
226     u16 outp;
227
228         if (value < 0)
229       outp = 65536 + value;
230     else
231       outp = value;
232
233     aic3x_write(codec, msbreg, outp >> 8);
234     aic3x_write(codec, msbreg+1, outp);
235
236         return 0;
237 }
238
239
240 /*
241  * read from the aic3x register space
242  */
243 static int aic3x_read(struct snd_soc_codec *codec, unsigned int reg,
244                       u8 *value)
245 {
246         *value = reg & 0xff;
247
248         /* No read access is recommended if the chip is reset after use */
249         printk(KERN_ERR "%s(): Values are may be incorrect!\n", __func__);
250
251         if (codec->hw_read(codec->control_data, value, 1) != 1)
252                 return -EIO;
253
254         aic3x_write_reg_cache(codec, reg, *value);
255         return 0;
256 }
257
258 static int aic3x_sync_hw(struct snd_soc_codec *codec)
259 {
260         u8 *cache = codec->reg_cache;
261         u8 data[2];
262         int i;
263     
264         aic3x_write(codec, AIC3X_PAGE_SELECT, 1);
265     /*mutex_lock(&codec->mutex);*/
266         for (i = 1; i < AIC3X_PAGE1REGNUM; i++) {
267                 data[0] = i;
268                 data[1] = cache[i+128];
269                 codec->hw_write(codec->control_data, data, 2);
270         }
271         /*mutex_unlock(&codec->mutex);*/
272     
273         /* We do not rewrite page select nor reset again */
274         aic3x_write(codec, AIC3X_PAGE_SELECT, 0);       
275         /*mutex_lock(&codec->mutex);*/
276     for (i = 2; i < AIC3X_PAGE0REGNUM; i++) {
277                 data[0] = i;
278                 data[1] = cache[i];
279                 codec->hw_write(codec->control_data, data, 2);
280         }
281         /*mutex_unlock(&codec->mutex);*/
282
283         return 0;
284 }
285
286 /*
287  * Reset for getting low power consumption after bypass paths
288  */
289 static void aic3x_reset(struct snd_soc_codec *codec)
290 {
291         aic3x_write(codec, AIC3X_RESET, SOFT_RESET);
292         aic3x_sync_hw(codec);
293 }
294
295 #define SOC_DAPM_SINGLE_AIC3X(xname, reg, shift, mask, invert) \
296 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
297         .info = snd_soc_info_volsw, \
298         .get = snd_soc_dapm_get_volsw, .put = snd_soc_dapm_put_volsw_aic3x, \
299         .private_value =  SOC_SINGLE_VALUE(reg, shift, mask, invert) }
300
301 /*
302  * All input lines are connected when !0xf and disconnected with 0xf bit field,
303  * so we have to use specific dapm_put call for input mixer
304  */
305 static int snd_soc_dapm_put_volsw_aic3x(struct snd_kcontrol *kcontrol,
306                                         struct snd_ctl_elem_value *ucontrol)
307 {
308         struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
309         struct soc_mixer_control *mc =
310                 (struct soc_mixer_control *)kcontrol->private_value;
311         unsigned int reg = mc->reg;
312         unsigned int shift = mc->shift;
313         int max = mc->max;
314         unsigned int mask = (1 << fls(max)) - 1;
315         unsigned int invert = mc->invert;
316         unsigned short val, val_mask;
317         int ret;
318         struct snd_soc_dapm_path *path;
319         int found = 0;
320
321         val = (ucontrol->value.integer.value[0] & mask);
322
323         mask = 0xf;
324         if (val)
325                 val = mask;
326
327         if (invert)
328                 val = mask - val;
329         val_mask = mask << shift;
330         val = val << shift;
331
332         mutex_lock(&widget->codec->mutex);
333
334         if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
335                 /* find dapm widget path assoc with kcontrol */
336                 list_for_each_entry(path, &widget->codec->dapm_paths, list) {
337                         if (path->kcontrol != kcontrol)
338                                 continue;
339
340                         /* found, now check type */
341                         found = 1;
342                         if (val)
343                                 /* new connection */
344                                 path->connect = invert ? 0 : 1;
345                         else
346                                 /* old connection must be powered down */
347                                 path->connect = invert ? 1 : 0;
348                         break;
349                 }
350
351                 if (found)
352                         snd_soc_dapm_sync(widget->codec);
353         }
354
355         ret = snd_soc_update_bits(widget->codec, reg, val_mask, val);
356
357         mutex_unlock(&widget->codec->mutex);
358         return ret;
359 }
360
361 static const char *aic3x_left_dac_mux[] = { "DAC_L1", "DAC_L3", "DAC_L2" };
362 static const char *aic3x_right_dac_mux[] = { "DAC_R1", "DAC_R3", "DAC_R2" };
363 static const char *aic3x_left_hpcom_mux[] =
364     { "differential of HPLOUT", "constant VCM", "single-ended" };
365 static const char *aic3x_right_hpcom_mux[] =
366     { "differential of HPROUT", "constant VCM", "single-ended",
367       "differential of HPLCOM", "external feedback" };
368 static const char *aic3x_linein_mode_mux[] = { "single-ended", "differential" };
369 static const char *aic3x_adc_hpf[] =
370     { "Disabled", "0.0045xFs", "0.0125xFs", "0.025xFs" };
371 static const char *aic3x_dac_filt[] =
372         { "Off", "Bass/Treble", "Custom" };
373
374 #define LDAC_ENUM       0
375 #define RDAC_ENUM       1
376 #define LHPCOM_ENUM     2
377 #define RHPCOM_ENUM     3
378 #define LINE1L_ENUM     4
379 #define LINE1R_ENUM     5
380 #define LINE2L_ENUM     6
381 #define LINE2R_ENUM     7
382 #define ADC_HPF_ENUM    8
383 #define DAC_FILT_ENUM   9
384
385 static const struct soc_enum aic3x_enum[] = {
386         SOC_ENUM_SINGLE(DAC_LINE_MUX, 6, 3, aic3x_left_dac_mux),
387         SOC_ENUM_SINGLE(DAC_LINE_MUX, 4, 3, aic3x_right_dac_mux),
388         SOC_ENUM_SINGLE(HPLCOM_CFG, 4, 3, aic3x_left_hpcom_mux),
389         SOC_ENUM_SINGLE(HPRCOM_CFG, 3, 5, aic3x_right_hpcom_mux),
390         SOC_ENUM_SINGLE(LINE1L_2_LADC_CTRL, 7, 2, aic3x_linein_mode_mux),
391         SOC_ENUM_SINGLE(LINE1R_2_RADC_CTRL, 7, 2, aic3x_linein_mode_mux),
392         SOC_ENUM_SINGLE(LINE2L_2_LADC_CTRL, 7, 2, aic3x_linein_mode_mux),
393         SOC_ENUM_SINGLE(LINE2R_2_RADC_CTRL, 7, 2, aic3x_linein_mode_mux),
394         SOC_ENUM_DOUBLE(AIC3X_CODEC_DFILT_CTRL, 6, 4, 4, aic3x_adc_hpf),
395         SOC_ENUM_DOUBLE(AIC3X_CODEC_DFILT_CTRL, 1, 3, 3, aic3x_dac_filt),
396 };
397
398 /*
399  * DAC digital volumes. From -63.5 to 0 dB in 0.5 dB steps
400  */
401 static DECLARE_TLV_DB_SCALE(dac_tlv, -6350, 50, 0);
402 /* ADC PGA gain volumes. From 0 to 59.5 dB in 0.5 dB steps */
403 static DECLARE_TLV_DB_SCALE(adc_tlv, 0, 50, 0);
404 /* HP DAC Output gain values. From 0 to 9.0 dB in 1 dB steps */
405 static DECLARE_TLV_DB_SCALE(hpout_tlv, 0, 100, 0);
406 /*
407  * Output stage volumes. From -78.3 to 0 dB. Muted below -78.3 dB.
408  * Step size is approximately 0.5 dB over most of the scale but increasing
409  * near the very low levels.
410  * Define dB scale so that it is mostly correct for range about -55 to 0 dB
411  * but having increasing dB difference below that (and where it doesn't count
412  * so much). This setting shows -50 dB (actual is -50.3 dB) for register
413  * value 100 and -58.5 dB (actual is -78.3 dB) for register value 117.
414  */
415 static DECLARE_TLV_DB_SCALE(output_stage_tlv, -5900, 50, 1);
416
417 #define SOC_DOUBLE_R_TLV_TLV320ALC3X(xname, reg_left, reg_right, xshift, xmax,\
418                                  xinvert, tlv_array) \
419 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = (xname),\
420         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
421                  SNDRV_CTL_ELEM_ACCESS_READWRITE,\
422         .tlv.p = (tlv_array), \
423         .info = tlv320alc3x_info_volsw, \
424         .get = snd_soc_get_volsw_2r,\
425         .put = snd_soc_put_volsw_2r,\
426         .private_value = (unsigned long)&(struct soc_mixer_control) \
427                 {.reg = reg_left, .rreg = reg_right, .shift = xshift, \
428                 .max = xmax, .invert = xinvert} }
429
430 static int tlv320alc3x_info_volsw(struct snd_kcontrol *kcontrol,
431         struct snd_ctl_elem_info *uinfo)
432 {
433         struct soc_mixer_control *mc =
434                 (struct soc_mixer_control *)kcontrol->private_value;
435         int max = mc->max;
436
437         if (hp_dac_lim != max && hp_dac_lim >= 2 && hp_dac_lim <= 9)
438                 max = hp_dac_lim;
439
440         if (max == 1)
441                 uinfo->type = SNDRV_CTL_ELEM_TYPE_BOOLEAN;
442         else
443                 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
444
445         uinfo->count = 2;
446         uinfo->value.integer.min = 0;
447         uinfo->value.integer.max = max;
448         return 0;
449 }
450
451 /* DAC and De-emphasis Filter Functions */
452 int aic3x_deemph_set_coeffs(struct snd_soc_codec *codec,
453                 int N0, int N1, int D1){
454     printk("MNZ: setting deemph coeffs\n");
455         snd_soc_update_bits(codec, AIC3X_CODEC_DFILT_CTRL, DEEMPH_ON, 0);
456
457         aic3x_write_coeff(codec, DEEMPH_LEFT_N0, N0);
458         aic3x_write_coeff(codec, DEEMPH_LEFT_N1, N1);
459         aic3x_write_coeff(codec, DEEMPH_LEFT_D1, D1);
460         aic3x_write_coeff(codec, DEEMPH_RIGHT_N0, N0);
461         aic3x_write_coeff(codec, DEEMPH_RIGHT_N1, N1);
462         aic3x_write_coeff(codec, DEEMPH_RIGHT_D1, D1);
463         
464         return 0;
465 }
466 EXPORT_SYMBOL_GPL(aic3x_deemph_set_coeffs);
467
468 int aic3x_deemph_set_state(struct snd_soc_codec *codec, int state){
469         printk("MNZ: Setting De-Emph filter: %i\n", state);
470         if(state) state = DEEMPH_ON;
471         else state = 0;
472         return snd_soc_update_bits(codec, AIC3X_CODEC_DFILT_CTRL,
473                 DEEMPH_ON, state);
474 }
475 EXPORT_SYMBOL_GPL(aic3x_deemph_set_state);
476
477 static int aic3x_dacfilter_write_coeffs
478     (struct snd_soc_codec *codec, struct aic3x_iir_coeffs *coeffs)
479 {
480         printk("MNZ: dacfilter_write_coeffs\n");
481         snd_soc_update_bits(codec, AIC3X_CODEC_DFILT_CTRL, EFFECTS_ON, 0);
482         aic3x_write_coeff(codec, EFFECTS_LEFT_N0, coeffs->N0);
483         aic3x_write_coeff(codec, EFFECTS_LEFT_N1, coeffs->N1);
484         aic3x_write_coeff(codec, EFFECTS_LEFT_N2, coeffs->N2);
485         aic3x_write_coeff(codec, EFFECTS_LEFT_D1, coeffs->D1);
486         aic3x_write_coeff(codec, EFFECTS_LEFT_D2, coeffs->D2);
487         aic3x_write_coeff(codec, EFFECTS_LEFT_N3, coeffs->N3);
488         aic3x_write_coeff(codec, EFFECTS_LEFT_N4, coeffs->N4);
489         aic3x_write_coeff(codec, EFFECTS_LEFT_N5, coeffs->N5);
490         aic3x_write_coeff(codec, EFFECTS_LEFT_D4, coeffs->D4);
491         aic3x_write_coeff(codec, EFFECTS_LEFT_D5, coeffs->D5);
492
493         aic3x_write_coeff(codec, EFFECTS_RIGHT_N0, coeffs->N0);
494         aic3x_write_coeff(codec, EFFECTS_RIGHT_N1, coeffs->N1);
495         aic3x_write_coeff(codec, EFFECTS_RIGHT_N2, coeffs->N2);
496         aic3x_write_coeff(codec, EFFECTS_RIGHT_D1, coeffs->D1);
497         aic3x_write_coeff(codec, EFFECTS_RIGHT_D2, coeffs->D2);
498         aic3x_write_coeff(codec, EFFECTS_RIGHT_N3, coeffs->N3);
499         aic3x_write_coeff(codec, EFFECTS_RIGHT_N4, coeffs->N4);
500         aic3x_write_coeff(codec, EFFECTS_RIGHT_N5, coeffs->N5);
501         aic3x_write_coeff(codec, EFFECTS_RIGHT_D4, coeffs->D4);
502         aic3x_write_coeff(codec, EFFECTS_RIGHT_D5, coeffs->D5);
503         
504         return 0;
505 }
506
507 int aic3x_dacfilter_set_coeffs
508     (struct snd_soc_codec *codec, struct aic3x_iir_coeffs *coeffs)
509 {
510         struct aic3x_priv *aic3x = codec->private_data;
511         memcpy((void*)&aic3x->dacfilter.coeffs, (void*)coeffs,
512                         sizeof(struct aic3x_iir_coeffs));
513         return 0;
514 }
515 EXPORT_SYMBOL_GPL(aic3x_dacfilter_set_coeffs);
516
517 int aic3x_dacfilter_set_state(struct snd_soc_codec *codec, int state)
518 {
519         struct aic3x_priv *aic3x = codec->private_data;
520
521         if(aic3x->dacfilter.state == state) return 0;
522         
523         aic3x->dacfilter.state = state;
524
525         if(state == 0)
526                 snd_soc_update_bits(codec, AIC3X_CODEC_DFILT_CTRL,
527                         EFFECTS_ON, 0);
528         else if(state == 1) {}
529                 /* FIXME MNZ. Set preset from current chosen preset */
530         else if (state == 2){
531                 aic3x_dacfilter_write_coeffs(codec,     &aic3x->dacfilter.coeffs);
532                 snd_soc_update_bits(codec, AIC3X_CODEC_DFILT_CTRL,
533                         EFFECTS_ON, EFFECTS_ON);
534         }
535
536         return 0;
537 }
538 EXPORT_SYMBOL_GPL(aic3x_dacfilter_set_state);
539
540 /* DAC Filter hwdep device callbacks */
541
542 static int snd_hwdep_dacfilter_open_aic3x(struct snd_hwdep *hw,
543         struct file *file)
544 {
545         return 0;
546 }
547
548 static int snd_hwdep_dacfilter_ioctl_aic3x(struct snd_hwdep *hw,
549         struct file *file, unsigned int cmd, unsigned long arg)
550 {
551         /* Only IOCTL command is for enabling/disabling filter, cmd = 1
552          * arg = 0 to disable, 1 to enable and set to bass/treble,
553          *       2 to enable and set to custom coeffs 
554          */
555         struct snd_soc_codec *codec = hw->private_data;
556         if (cmd != 1) return -EINVAL;
557         return aic3x_dacfilter_set_state(codec, *((int*)arg));
558         return 0;
559 }
560
561 static long snd_hwdep_dacfilter_read_aic3x(struct snd_hwdep *hw,
562         char __user *buf, long count, loff_t *offset)
563 {
564         struct aic3x_priv *aic3x = 
565                 ((struct snd_soc_codec*)hw->private_data)->private_data;
566         if(count != sizeof(struct aic3x_iir_coeffs)) return -EINVAL;
567         memcpy((void*)buf, (void*)&aic3x->dacfilter.coeffs, count);
568         return 0;
569 }
570
571 static long snd_hwdep_dacfilter_write_aic3x(struct snd_hwdep *hw,
572         const char __user *buf, long count, loff_t *offset)
573 {
574         struct snd_soc_codec *codec = hw->private_data;
575         if(count != sizeof(struct aic3x_iir_coeffs)) return -EINVAL;
576
577         ((struct aic3x_priv*)codec->private_data)->dacfilter.state = 2;
578         
579         return aic3x_dacfilter_set_coeffs(codec, 
580                                                 (struct aic3x_iir_coeffs*)buf);
581 }
582
583 /* DAC filter and 3D depth ALSA controls callbacks */
584
585 static int snd_soc_get_dacfilter_aic3x(struct snd_kcontrol *kcontrol,
586         struct snd_ctl_elem_value *ucontrol)
587 {
588         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
589         ucontrol->value.enumerated.item[0] = 
590                 ((struct aic3x_priv*)codec->private_data)->dacfilter.state;
591         return 0;
592 }
593
594 static int snd_soc_put_dacfilter_aic3x(struct snd_kcontrol *kcontrol,
595         struct snd_ctl_elem_value *ucontrol)
596 {
597         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
598         if (ucontrol->value.enumerated.item[0] > 2)
599                 return -EINVAL;
600         aic3x_dacfilter_set_state(codec, ucontrol->value.enumerated.item[0]);
601         return 1;       
602 }
603
604 static int snd_soc_get_3d_attenuation_aic3x
605         (struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
606 {
607         int val = aic3x_read_coeff_reg_cache
608                 (snd_kcontrol_chip(kcontrol), EFFECTS_3DATTEN);
609         val = ((val*100)/65530) + 50;
610         ucontrol->value.integer.value[0] = val;
611         return 0;
612 }
613
614 static int snd_soc_put_3d_attenuation_aic3x
615         (struct snd_kcontrol *kcontrol, struct snd_ctl_elem_value *ucontrol)
616 {
617         struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
618         int val = ucontrol->value.integer.value[0];
619     if(val > 100 || val < 0) return -EINVAL;
620
621         if(val){
622                 snd_soc_update_bits(codec, AIC3X_ASD_INTF_CTRLA,
623                         EFFECTS_3D_ON, EFFECTS_3D_ON);
624                 val = ((val - 50) * 65535) / 100 ;
625         } else {
626                 snd_soc_update_bits(codec, AIC3X_ASD_INTF_CTRLA,
627                         EFFECTS_3D_ON, 0);
628                 val = -32768;
629         }
630
631         aic3x_write_coeff(codec, EFFECTS_3DATTEN, val);
632
633         return 1;
634 }
635
636 static const struct snd_kcontrol_new aic3x_snd_controls[] = {
637         /* Output */
638         SOC_DOUBLE_R_TLV("PCM Playback Volume",
639                          LDAC_VOL, RDAC_VOL, 0, 0x7f, 1, dac_tlv),
640
641         SOC_DOUBLE_R_TLV("Line DAC Playback Volume",
642                          DACL1_2_LLOPM_VOL, DACR1_2_RLOPM_VOL,
643                          0, 118, 1, output_stage_tlv),
644         SOC_DOUBLE_R("Line DAC Playback Switch", LLOPM_CTRL, RLOPM_CTRL, 3,
645                      0x01, 0),
646         SOC_DOUBLE_R_TLV("Line PGA Bypass Playback Volume",
647                          PGAL_2_LLOPM_VOL, PGAR_2_RLOPM_VOL,
648                          0, 118, 1, output_stage_tlv),
649         SOC_DOUBLE_R_TLV("Line Line2 Bypass Playback Volume",
650                          LINE2L_2_LLOPM_VOL, LINE2R_2_RLOPM_VOL,
651                          0, 118, 1, output_stage_tlv),
652
653         SOC_DOUBLE_R_TLV("Mono DAC Playback Volume",
654                          DACL1_2_MONOLOPM_VOL, DACR1_2_MONOLOPM_VOL,
655                          0, 118, 1, output_stage_tlv),
656         SOC_SINGLE("Mono DAC Playback Switch", MONOLOPM_CTRL, 3, 0x01, 0),
657         SOC_DOUBLE_R_TLV("Mono PGA Bypass Playback Volume",
658                          PGAL_2_MONOLOPM_VOL, PGAR_2_MONOLOPM_VOL,
659                          0, 118, 1, output_stage_tlv),
660         SOC_DOUBLE_R_TLV("Mono Line2 Bypass Playback Volume",
661                          LINE2L_2_MONOLOPM_VOL, LINE2R_2_MONOLOPM_VOL,
662                          0, 118, 1, output_stage_tlv),
663
664         SOC_DOUBLE_R_TLV("HP DAC Playback Volume",
665                          DACL1_2_HPLOUT_VOL, DACR1_2_HPROUT_VOL,
666                          0, 118, 1, output_stage_tlv),
667         SOC_DOUBLE_R("HP DAC Playback Switch", HPLOUT_CTRL, HPROUT_CTRL, 3,
668                      0x01, 0),
669         SOC_DOUBLE_R_TLV_TLV320ALC3X("HP DAC Output Volume", HPLOUT_CTRL,
670                          HPROUT_CTRL, 4, 9, 0, hpout_tlv),
671         SOC_DOUBLE_R_TLV("HP PGA Bypass Playback Volume",
672                          PGAL_2_HPLOUT_VOL, PGAR_2_HPROUT_VOL,
673                          0, 118, 1, output_stage_tlv),
674         SOC_DOUBLE_R_TLV("HP Line2 Bypass Playback Volume",
675                          LINE2L_2_HPLOUT_VOL, LINE2R_2_HPROUT_VOL,
676                          0, 118, 1, output_stage_tlv),
677
678         SOC_DOUBLE_R_TLV("HPCOM DAC Playback Volume",
679                          DACL1_2_HPLCOM_VOL, DACR1_2_HPRCOM_VOL,
680                          0, 118, 1, output_stage_tlv),
681         SOC_DOUBLE_R("HPCOM DAC Playback Switch", HPLCOM_CTRL, HPRCOM_CTRL, 3,
682                      0x01, 0),
683         SOC_DOUBLE_R_TLV_TLV320ALC3X("HPCOM DAC Output Volume", HPLCOM_CTRL,
684                          HPRCOM_CTRL, 4, 9, 0, hpout_tlv),
685         SOC_DOUBLE_R_TLV("HPCOM PGA Bypass Playback Volume",
686                          PGAL_2_HPLCOM_VOL, PGAR_2_HPRCOM_VOL,
687                          0, 118, 1, output_stage_tlv),
688         SOC_DOUBLE_R_TLV("HPCOM Line2 Bypass Playback Volume",
689                          LINE2L_2_HPLCOM_VOL, LINE2R_2_HPRCOM_VOL,
690                          0, 118, 1, output_stage_tlv),
691
692         /*
693          * Note: enable Automatic input Gain Controller with care. It can
694          * adjust PGA to max value when ADC is on and will never go back.
695         */
696         SOC_DOUBLE_R("AGC Switch", LAGC_CTRL_A, RAGC_CTRL_A, 7, 0x01, 0),
697
698         /* Input */
699         SOC_DOUBLE_R_TLV("PGA Capture Volume", LADC_VOL, RADC_VOL,
700                          0, 119, 0, adc_tlv),
701         SOC_DOUBLE_R("PGA Capture Switch", LADC_VOL, RADC_VOL, 7, 0x01, 1),
702
703         SOC_ENUM("ADC HPF Cut-off", aic3x_enum[ADC_HPF_ENUM]),
704
705         SOC_ENUM_EXT("Hardware EQ", aic3x_enum[DAC_FILT_ENUM],
706                 snd_soc_get_dacfilter_aic3x, snd_soc_put_dacfilter_aic3x),
707
708         SOC_SINGLE_EXT("3D Control - Depth", EFFECTS_3DATTEN, 0, 100, 0,
709                 snd_soc_get_3d_attenuation_aic3x, snd_soc_put_3d_attenuation_aic3x),
710 };
711
712 /* add non dapm controls */
713 static int aic3x_add_controls(struct snd_soc_codec *codec)
714 {
715         int err, i;
716
717         for (i = 0; i < ARRAY_SIZE(aic3x_snd_controls); i++) {
718                 err = snd_ctl_add(codec->card,
719                                   snd_soc_cnew(&aic3x_snd_controls[i],
720                                                codec, NULL));
721                 if (err < 0)
722                         return err;
723         }
724
725         return 0;
726 }
727
728 static int reset_after_bypass(struct snd_soc_dapm_widget *w,
729                         struct snd_kcontrol *kcontrol, int event)
730 {
731         struct aic3x_priv *aic3x = w->codec->private_data;
732         struct soc_mixer_control *mc = NULL;
733         unsigned int reg = 0;
734
735         if (kcontrol)
736                 mc = (struct soc_mixer_control *)kcontrol->private_value;
737         if (mc)
738                 reg = mc->reg;
739
740         if (reg == PGAL_2_LLOPM_VOL || reg == PGAR_2_RLOPM_VOL ||
741             reg == PGAL_2_HPLOUT_VOL || reg == PGAR_2_HPROUT_VOL) {
742                 if (w->value & 0x80) {
743                         /* Prepare reset on the chip */
744                         if (reg == PGAL_2_LLOPM_VOL)
745                                 aic3x->prepare_reset |= 0x01;
746                         else if (reg == PGAR_2_RLOPM_VOL)
747                                 aic3x->prepare_reset |= 0x02;
748                         else if (reg == PGAL_2_HPLOUT_VOL)
749                                 aic3x->prepare_reset |= 0x04;
750                         else if (reg == PGAR_2_HPROUT_VOL)
751                                 aic3x->prepare_reset |= 0x08;
752                 } else {
753                         if (aic3x->prepare_reset) {
754                                 if (reg == PGAL_2_LLOPM_VOL)
755                                         aic3x->prepare_reset &= ~0x01;
756                                 else if (reg == PGAR_2_RLOPM_VOL)
757                                         aic3x->prepare_reset &= ~0x02;
758                                 else if (reg == PGAL_2_HPLOUT_VOL)
759                                         aic3x->prepare_reset &= ~0x04;
760                                 else if (reg == PGAR_2_HPROUT_VOL)
761                                         aic3x->prepare_reset &= ~0x08;
762                                 /*
763                                  * Controls may have now been turned off,
764                                  * once they were on, so schedule or
765                                  * issue a reset on the chip.
766                                  */
767                                 if (!aic3x->prepare_reset) {
768                                         if (!((w->codec->bias_level ==
769                                                 SND_SOC_BIAS_ON) ||
770                                                 (w->codec->bias_level ==
771                                                 SND_SOC_BIAS_PREPARE)))
772                                                 aic3x_reset(w->codec);
773                                 }
774                         }
775                 }
776         }
777
778         return 0;
779 }
780
781 /* Left DAC Mux */
782 static const struct snd_kcontrol_new aic3x_left_dac_mux_controls =
783 SOC_DAPM_ENUM("Route", aic3x_enum[LDAC_ENUM]);
784
785 /* Right DAC Mux */
786 static const struct snd_kcontrol_new aic3x_right_dac_mux_controls =
787 SOC_DAPM_ENUM("Route", aic3x_enum[RDAC_ENUM]);
788
789 /* Left HPCOM Mux */
790 static const struct snd_kcontrol_new aic3x_left_hpcom_mux_controls =
791 SOC_DAPM_ENUM("Route", aic3x_enum[LHPCOM_ENUM]);
792
793 /* Right HPCOM Mux */
794 static const struct snd_kcontrol_new aic3x_right_hpcom_mux_controls =
795 SOC_DAPM_ENUM("Route", aic3x_enum[RHPCOM_ENUM]);
796
797 /* Left DAC_L1 Mixer */
798 static const struct snd_kcontrol_new aic3x_left_dac_mixer_controls[] = {
799         SOC_DAPM_SINGLE("Line Switch", DACL1_2_LLOPM_VOL, 7, 1, 0),
800         SOC_DAPM_SINGLE("Mono Switch", DACL1_2_MONOLOPM_VOL, 7, 1, 0),
801         SOC_DAPM_SINGLE("HP Switch", DACL1_2_HPLOUT_VOL, 7, 1, 0),
802         SOC_DAPM_SINGLE("HPCOM Switch", DACL1_2_HPLCOM_VOL, 7, 1, 0),
803 };
804
805 /* Right DAC_R1 Mixer */
806 static const struct snd_kcontrol_new aic3x_right_dac_mixer_controls[] = {
807         SOC_DAPM_SINGLE("Line Switch", DACR1_2_RLOPM_VOL, 7, 1, 0),
808         SOC_DAPM_SINGLE("Mono Switch", DACR1_2_MONOLOPM_VOL, 7, 1, 0),
809         SOC_DAPM_SINGLE("HP Switch", DACR1_2_HPROUT_VOL, 7, 1, 0),
810         SOC_DAPM_SINGLE("HPCOM Switch", DACR1_2_HPRCOM_VOL, 7, 1, 0),
811 };
812
813 /* Left PGA Mixer */
814 static const struct snd_kcontrol_new aic3x_left_pga_mixer_controls[] = {
815         SOC_DAPM_SINGLE_AIC3X("Line1L Switch", LINE1L_2_LADC_CTRL, 3, 1, 1),
816         SOC_DAPM_SINGLE_AIC3X("Line2L Switch", LINE2L_2_LADC_CTRL, 3, 1, 1),
817         SOC_DAPM_SINGLE_AIC3X("Mic3L Switch", MIC3LR_2_LADC_CTRL, 4, 1, 1),
818 };
819
820 /* Right PGA Mixer */
821 static const struct snd_kcontrol_new aic3x_right_pga_mixer_controls[] = {
822         SOC_DAPM_SINGLE_AIC3X("Line1R Switch", LINE1R_2_RADC_CTRL, 3, 1, 1),
823         SOC_DAPM_SINGLE_AIC3X("Line1L Switch", LINE1L_2_RADC_CTRL, 3, 1, 1),
824         SOC_DAPM_SINGLE_AIC3X("Line2R Switch", LINE2R_2_RADC_CTRL, 3, 1, 1),
825         SOC_DAPM_SINGLE_AIC3X("Mic3R Switch", MIC3LR_2_RADC_CTRL, 0, 1, 1),
826 };
827
828 /* Left Line1 Mux */
829 static const struct snd_kcontrol_new aic3x_left_line1_mux_controls =
830 SOC_DAPM_ENUM("Route", aic3x_enum[LINE1L_ENUM]);
831
832 /* Right Line1 Mux */
833 static const struct snd_kcontrol_new aic3x_right_line1_mux_controls =
834 SOC_DAPM_ENUM("Route", aic3x_enum[LINE1R_ENUM]);
835
836 /* Left Line2 Mux */
837 static const struct snd_kcontrol_new aic3x_left_line2_mux_controls =
838 SOC_DAPM_ENUM("Route", aic3x_enum[LINE2L_ENUM]);
839
840 /* Right Line2 Mux */
841 static const struct snd_kcontrol_new aic3x_right_line2_mux_controls =
842 SOC_DAPM_ENUM("Route", aic3x_enum[LINE2R_ENUM]);
843
844 /* Left PGA Bypass Mixer */
845 static const struct snd_kcontrol_new aic3x_left_pga_bp_mixer_controls[] = {
846         SOC_DAPM_SINGLE("Line Switch", PGAL_2_LLOPM_VOL, 7, 1, 0),
847         SOC_DAPM_SINGLE("Mono Switch", PGAL_2_MONOLOPM_VOL, 7, 1, 0),
848         SOC_DAPM_SINGLE("HP Switch", PGAL_2_HPLOUT_VOL, 7, 1, 0),
849         SOC_DAPM_SINGLE("HPCOM Switch", PGAL_2_HPLCOM_VOL, 7, 1, 0),
850 };
851
852 /* Right PGA Bypass Mixer */
853 static const struct snd_kcontrol_new aic3x_right_pga_bp_mixer_controls[] = {
854         SOC_DAPM_SINGLE("Line Switch", PGAR_2_RLOPM_VOL, 7, 1, 0),
855         SOC_DAPM_SINGLE("Mono Switch", PGAR_2_MONOLOPM_VOL, 7, 1, 0),
856         SOC_DAPM_SINGLE("HP Switch", PGAR_2_HPROUT_VOL, 7, 1, 0),
857         SOC_DAPM_SINGLE("HPCOM Switch", PGAR_2_HPRCOM_VOL, 7, 1, 0),
858 };
859
860 /* Left Line2 Bypass Mixer */
861 static const struct snd_kcontrol_new aic3x_left_line2_bp_mixer_controls[] = {
862         SOC_DAPM_SINGLE("Line Switch", LINE2L_2_LLOPM_VOL, 7, 1, 0),
863         SOC_DAPM_SINGLE("Mono Switch", LINE2L_2_MONOLOPM_VOL, 7, 1, 0),
864         SOC_DAPM_SINGLE("HP Switch", LINE2L_2_HPLOUT_VOL, 7, 1, 0),
865         SOC_DAPM_SINGLE("HPCOM Switch", LINE2L_2_HPLCOM_VOL, 7, 1, 0),
866 };
867
868 /* Right Line2 Bypass Mixer */
869 static const struct snd_kcontrol_new aic3x_right_line2_bp_mixer_controls[] = {
870         SOC_DAPM_SINGLE("Line Switch", LINE2R_2_RLOPM_VOL, 7, 1, 0),
871         SOC_DAPM_SINGLE("Mono Switch", LINE2R_2_MONOLOPM_VOL, 7, 1, 0),
872         SOC_DAPM_SINGLE("HP Switch", LINE2R_2_HPROUT_VOL, 7, 1, 0),
873         SOC_DAPM_SINGLE("HPCOM Switch", LINE2R_2_HPRCOM_VOL, 7, 1, 0),
874 };
875
876 static const struct snd_soc_dapm_widget aic3x_dapm_widgets[] = {
877         /* Left DAC to Left Outputs */
878         SND_SOC_DAPM_DAC("Left DAC", "Left Playback", DAC_PWR, 7, 0),
879         SND_SOC_DAPM_MUX("Left DAC Mux", SND_SOC_NOPM, 0, 0,
880                          &aic3x_left_dac_mux_controls),
881         SND_SOC_DAPM_MIXER("Left DAC_L1 Mixer", SND_SOC_NOPM, 0, 0,
882                            &aic3x_left_dac_mixer_controls[0],
883                            ARRAY_SIZE(aic3x_left_dac_mixer_controls)),
884         SND_SOC_DAPM_MUX("Left HPCOM Mux", SND_SOC_NOPM, 0, 0,
885                          &aic3x_left_hpcom_mux_controls),
886         SND_SOC_DAPM_PGA("Left Line Out", LLOPM_CTRL, 0, 0, NULL, 0),
887         SND_SOC_DAPM_PGA("Left HP Out", HPLOUT_CTRL, 0, 0, NULL, 0),
888         SND_SOC_DAPM_PGA("Left HP Com", HPLCOM_CTRL, 0, 0, NULL, 0),
889
890         /* Right DAC to Right Outputs */
891         SND_SOC_DAPM_DAC("Right DAC", "Right Playback", DAC_PWR, 6, 0),
892         SND_SOC_DAPM_MUX("Right DAC Mux", SND_SOC_NOPM, 0, 0,
893                          &aic3x_right_dac_mux_controls),
894         SND_SOC_DAPM_MIXER("Right DAC_R1 Mixer", SND_SOC_NOPM, 0, 0,
895                            &aic3x_right_dac_mixer_controls[0],
896                            ARRAY_SIZE(aic3x_right_dac_mixer_controls)),
897         SND_SOC_DAPM_MUX("Right HPCOM Mux", SND_SOC_NOPM, 0, 0,
898                          &aic3x_right_hpcom_mux_controls),
899         SND_SOC_DAPM_PGA("Right Line Out", RLOPM_CTRL, 0, 0, NULL, 0),
900         SND_SOC_DAPM_PGA("Right HP Out", HPROUT_CTRL, 0, 0, NULL, 0),
901         SND_SOC_DAPM_PGA("Right HP Com", HPRCOM_CTRL, 0, 0, NULL, 0),
902
903         /* Mono Output */
904         SND_SOC_DAPM_PGA("Mono Out", MONOLOPM_CTRL, 0, 0, NULL, 0),
905
906         /* Left Inputs to Left ADC */
907         SND_SOC_DAPM_ADC("Left ADC", "Left Capture", LINE1L_2_LADC_CTRL, 2, 0),
908         SND_SOC_DAPM_MIXER("Left PGA Mixer", SND_SOC_NOPM, 0, 0,
909                            &aic3x_left_pga_mixer_controls[0],
910                            ARRAY_SIZE(aic3x_left_pga_mixer_controls)),
911         SND_SOC_DAPM_MUX("Left Line1L Mux", SND_SOC_NOPM, 0, 0,
912                          &aic3x_left_line1_mux_controls),
913         SND_SOC_DAPM_MUX("Left Line2L Mux", SND_SOC_NOPM, 0, 0,
914                          &aic3x_left_line2_mux_controls),
915
916         /* Right Inputs to Right ADC */
917         SND_SOC_DAPM_ADC("Right ADC", "Right Capture",
918                          LINE1R_2_RADC_CTRL, 2, 0),
919         SND_SOC_DAPM_MIXER("Right PGA Mixer", SND_SOC_NOPM, 0, 0,
920                            &aic3x_right_pga_mixer_controls[0],
921                            ARRAY_SIZE(aic3x_right_pga_mixer_controls)),
922         SND_SOC_DAPM_MUX("Right Line1L Mux", SND_SOC_NOPM, 0, 0,
923                          &aic3x_right_line1_mux_controls),
924         SND_SOC_DAPM_MUX("Right Line1R Mux", SND_SOC_NOPM, 0, 0,
925                          &aic3x_right_line1_mux_controls),
926         SND_SOC_DAPM_MUX("Right Line2R Mux", SND_SOC_NOPM, 0, 0,
927                          &aic3x_right_line2_mux_controls),
928
929         /*
930          * Not a real mic bias widget but similar function. This is for dynamic
931          * control of GPIO1 digital mic modulator clock output function when
932          * using digital mic.
933          */
934         SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "GPIO1 dmic modclk",
935                          AIC3X_GPIO1_REG, 4, 0xf,
936                          AIC3X_GPIO1_FUNC_DIGITAL_MIC_MODCLK,
937                          AIC3X_GPIO1_FUNC_DISABLED),
938
939         /*
940          * Also similar function like mic bias. Selects digital mic with
941          * configurable oversampling rate instead of ADC converter.
942          */
943         SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "DMic Rate 128",
944                          AIC3X_ASD_INTF_CTRLA, 0, 3, 1, 0),
945         SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "DMic Rate 64",
946                          AIC3X_ASD_INTF_CTRLA, 0, 3, 2, 0),
947         SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "DMic Rate 32",
948                          AIC3X_ASD_INTF_CTRLA, 0, 3, 3, 0),
949
950         /* Mic Bias */
951         SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "Mic Bias 2V",
952                          MICBIAS_CTRL, 6, 3, 1, 0),
953         SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "Mic Bias 2.5V",
954                          MICBIAS_CTRL, 6, 3, 2, 0),
955         SND_SOC_DAPM_REG(snd_soc_dapm_micbias, "Mic Bias AVDD",
956                          MICBIAS_CTRL, 6, 3, 3, 0),
957
958         /* Left PGA to Left Output bypass */
959         SND_SOC_DAPM_MIXER_E("Left PGA Bypass Mixer", SND_SOC_NOPM, 0, 0,
960                            &aic3x_left_pga_bp_mixer_controls[0],
961                            ARRAY_SIZE(aic3x_left_pga_bp_mixer_controls),
962                            reset_after_bypass, SND_SOC_DAPM_POST_REG),
963
964         /* Right PGA to Right Output bypass */
965         SND_SOC_DAPM_MIXER_E("Right PGA Bypass Mixer", SND_SOC_NOPM, 0, 0,
966                            &aic3x_right_pga_bp_mixer_controls[0],
967                            ARRAY_SIZE(aic3x_right_pga_bp_mixer_controls),
968                            reset_after_bypass, SND_SOC_DAPM_POST_REG),
969
970         /* Left Line2 to Left Output bypass */
971         SND_SOC_DAPM_MIXER("Left Line2 Bypass Mixer", SND_SOC_NOPM, 0, 0,
972                            &aic3x_left_line2_bp_mixer_controls[0],
973                            ARRAY_SIZE(aic3x_left_line2_bp_mixer_controls)),
974
975         /* Right Line2 to Right Output bypass */
976         SND_SOC_DAPM_MIXER("Right Line2 Bypass Mixer", SND_SOC_NOPM, 0, 0,
977                            &aic3x_right_line2_bp_mixer_controls[0],
978                            ARRAY_SIZE(aic3x_right_line2_bp_mixer_controls)),
979
980         SND_SOC_DAPM_OUTPUT("LLOUT"),
981         SND_SOC_DAPM_OUTPUT("RLOUT"),
982         SND_SOC_DAPM_OUTPUT("MONO_LOUT"),
983         SND_SOC_DAPM_OUTPUT("HPLOUT"),
984         SND_SOC_DAPM_OUTPUT("HPROUT"),
985         SND_SOC_DAPM_OUTPUT("HPLCOM"),
986         SND_SOC_DAPM_OUTPUT("HPRCOM"),
987
988         SND_SOC_DAPM_INPUT("MIC3L"),
989         SND_SOC_DAPM_INPUT("MIC3R"),
990         SND_SOC_DAPM_INPUT("LINE1L"),
991         SND_SOC_DAPM_INPUT("LINE1R"),
992         SND_SOC_DAPM_INPUT("LINE2L"),
993         SND_SOC_DAPM_INPUT("LINE2R"),
994 };
995
996 static const struct snd_soc_dapm_route intercon[] = {
997         /* Left Output */
998         {"Left DAC Mux", "DAC_L1", "Left DAC"},
999         {"Left DAC Mux", "DAC_L2", "Left DAC"},
1000         {"Left DAC Mux", "DAC_L3", "Left DAC"},
1001
1002         {"Left DAC_L1 Mixer", "Line Switch", "Left DAC Mux"},
1003         {"Left DAC_L1 Mixer", "Mono Switch", "Left DAC Mux"},
1004         {"Left DAC_L1 Mixer", "HP Switch", "Left DAC Mux"},
1005         {"Left DAC_L1 Mixer", "HPCOM Switch", "Left DAC Mux"},
1006         {"Left Line Out", NULL, "Left DAC Mux"},
1007         {"Left HP Out", NULL, "Left DAC Mux"},
1008
1009         {"Left HPCOM Mux", "differential of HPLOUT", "Left DAC_L1 Mixer"},
1010         {"Left HPCOM Mux", "constant VCM", "Left DAC_L1 Mixer"},
1011         {"Left HPCOM Mux", "single-ended", "Left DAC_L1 Mixer"},
1012
1013         {"Left Line Out", NULL, "Left DAC_L1 Mixer"},
1014         {"Mono Out", NULL, "Left DAC_L1 Mixer"},
1015         {"Left HP Out", NULL, "Left DAC_L1 Mixer"},
1016         {"Left HP Com", NULL, "Left HPCOM Mux"},
1017
1018         {"LLOUT", NULL, "Left Line Out"},
1019         {"LLOUT", NULL, "Left Line Out"},
1020         {"HPLOUT", NULL, "Left HP Out"},
1021         {"HPLCOM", NULL, "Left HP Com"},
1022
1023         /* Right Output */
1024         {"Right DAC Mux", "DAC_R1", "Right DAC"},
1025         {"Right DAC Mux", "DAC_R2", "Right DAC"},
1026         {"Right DAC Mux", "DAC_R3", "Right DAC"},
1027
1028         {"Right DAC_R1 Mixer", "Line Switch", "Right DAC Mux"},
1029         {"Right DAC_R1 Mixer", "Mono Switch", "Right DAC Mux"},
1030         {"Right DAC_R1 Mixer", "HP Switch", "Right DAC Mux"},
1031         {"Right DAC_R1 Mixer", "HPCOM Switch", "Right DAC Mux"},
1032         {"Right Line Out", NULL, "Right DAC Mux"},
1033         {"Right HP Out", NULL, "Right DAC Mux"},
1034
1035         {"Right HPCOM Mux", "differential of HPROUT", "Right DAC_R1 Mixer"},
1036         {"Right HPCOM Mux", "constant VCM", "Right DAC_R1 Mixer"},
1037         {"Right HPCOM Mux", "single-ended", "Right DAC_R1 Mixer"},
1038         {"Right HPCOM Mux", "differential of HPLCOM", "Right DAC_R1 Mixer"},
1039         {"Right HPCOM Mux", "external feedback", "Right DAC_R1 Mixer"},
1040
1041         {"Right Line Out", NULL, "Right DAC_R1 Mixer"},
1042         {"Mono Out", NULL, "Right DAC_R1 Mixer"},
1043         {"Right HP Out", NULL, "Right DAC_R1 Mixer"},
1044         {"Right HP Com", NULL, "Right HPCOM Mux"},
1045
1046         {"RLOUT", NULL, "Right Line Out"},
1047         {"RLOUT", NULL, "Right Line Out"},
1048         {"HPROUT", NULL, "Right HP Out"},
1049         {"HPRCOM", NULL, "Right HP Com"},
1050
1051         /* Mono Output */
1052         {"MONO_LOUT", NULL, "Mono Out"},
1053         {"MONO_LOUT", NULL, "Mono Out"},
1054
1055         /* Left Input */
1056         {"Left Line1L Mux", "single-ended", "LINE1L"},
1057         {"Left Line1L Mux", "differential", "LINE1L"},
1058
1059         {"Left Line2L Mux", "single-ended", "LINE2L"},
1060         {"Left Line2L Mux", "differential", "LINE2L"},
1061
1062         {"Left PGA Mixer", "Line1L Switch", "Left Line1L Mux"},
1063         {"Left PGA Mixer", "Line2L Switch", "Left Line2L Mux"},
1064         {"Left PGA Mixer", "Mic3L Switch", "MIC3L"},
1065
1066         {"Left ADC", NULL, "Left PGA Mixer"},
1067         {"Left ADC", NULL, "GPIO1 dmic modclk"},
1068
1069         /* Right Input */
1070         {"Right Line1L Mux", "single-ended", "LINE1L"},
1071         {"Right Line1L Mux", "differential", "LINE1L"},
1072
1073         {"Right Line1R Mux", "single-ended", "LINE1R"},
1074         {"Right Line1R Mux", "differential", "LINE1R"},
1075
1076         {"Right Line2R Mux", "single-ended", "LINE2R"},
1077         {"Right Line2R Mux", "differential", "LINE2R"},
1078
1079         {"Right PGA Mixer", "Line1L Switch", "Right Line1L Mux"},
1080         {"Right PGA Mixer", "Line1R Switch", "Right Line1R Mux"},
1081         {"Right PGA Mixer", "Line2R Switch", "Right Line2R Mux"},
1082         {"Right PGA Mixer", "Mic3R Switch", "MIC3R"},
1083
1084         {"Right ADC", NULL, "Right PGA Mixer"},
1085         {"Right ADC", NULL, "GPIO1 dmic modclk"},
1086
1087         /* Left PGA Bypass */
1088         {"Left PGA Bypass Mixer", "Line Switch", "Left PGA Mixer"},
1089         {"Left PGA Bypass Mixer", "Mono Switch", "Left PGA Mixer"},
1090         {"Left PGA Bypass Mixer", "HP Switch", "Left PGA Mixer"},
1091         {"Left PGA Bypass Mixer", "HPCOM Switch", "Left PGA Mixer"},
1092
1093         {"Left HPCOM Mux", "differential of HPLOUT", "Left PGA Bypass Mixer"},
1094         {"Left HPCOM Mux", "constant VCM", "Left PGA Bypass Mixer"},
1095         {"Left HPCOM Mux", "single-ended", "Left PGA Bypass Mixer"},
1096
1097         {"Left Line Out", NULL, "Left PGA Bypass Mixer"},
1098         {"Mono Out", NULL, "Left PGA Bypass Mixer"},
1099         {"Left HP Out", NULL, "Left PGA Bypass Mixer"},
1100
1101         /* Right PGA Bypass */
1102         {"Right PGA Bypass Mixer", "Line Switch", "Right PGA Mixer"},
1103         {"Right PGA Bypass Mixer", "Mono Switch", "Right PGA Mixer"},
1104         {"Right PGA Bypass Mixer", "HP Switch", "Right PGA Mixer"},
1105         {"Right PGA Bypass Mixer", "HPCOM Switch", "Right PGA Mixer"},
1106
1107         {"Right HPCOM Mux", "differential of HPROUT", "Right PGA Bypass Mixer"},
1108         {"Right HPCOM Mux", "constant VCM", "Right PGA Bypass Mixer"},
1109         {"Right HPCOM Mux", "single-ended", "Right PGA Bypass Mixer"},
1110         {"Right HPCOM Mux", "differential of HPLCOM", "Right PGA Bypass Mixer"},
1111         {"Right HPCOM Mux", "external feedback", "Right PGA Bypass Mixer"},
1112
1113         {"Right Line Out", NULL, "Right PGA Bypass Mixer"},
1114         {"Mono Out", NULL, "Right PGA Bypass Mixer"},
1115         {"Right HP Out", NULL, "Right PGA Bypass Mixer"},
1116
1117         /* Left Line2 Bypass */
1118         {"Left Line2 Bypass Mixer", "Line Switch", "Left Line2L Mux"},
1119         {"Left Line2 Bypass Mixer", "Mono Switch", "Left Line2L Mux"},
1120         {"Left Line2 Bypass Mixer", "HP Switch", "Left Line2L Mux"},
1121         {"Left Line2 Bypass Mixer", "HPCOM Switch", "Left Line2L Mux"},
1122
1123         {"Left HPCOM Mux", "differential of HPLOUT", "Left Line2 Bypass Mixer"},
1124         {"Left HPCOM Mux", "constant VCM", "Left Line2 Bypass Mixer"},
1125         {"Left HPCOM Mux", "single-ended", "Left Line2 Bypass Mixer"},
1126
1127         {"Left Line Out", NULL, "Left Line2 Bypass Mixer"},
1128         {"Mono Out", NULL, "Left Line2 Bypass Mixer"},
1129         {"Left HP Out", NULL, "Left Line2 Bypass Mixer"},
1130
1131         /* Right Line2 Bypass */
1132         {"Right Line2 Bypass Mixer", "Line Switch", "Right Line2R Mux"},
1133         {"Right Line2 Bypass Mixer", "Mono Switch", "Right Line2R Mux"},
1134         {"Right Line2 Bypass Mixer", "HP Switch", "Right Line2R Mux"},
1135         {"Right Line2 Bypass Mixer", "HPCOM Switch", "Right Line2R Mux"},
1136
1137         {"Right HPCOM Mux", "differential of HPROUT", "Right Line2 Bypass Mixer"},
1138         {"Right HPCOM Mux", "constant VCM", "Right Line2 Bypass Mixer"},
1139         {"Right HPCOM Mux", "single-ended", "Right Line2 Bypass Mixer"},
1140         {"Right HPCOM Mux", "differential of HPLCOM", "Right Line2 Bypass Mixer"},
1141         {"Right HPCOM Mux", "external feedback", "Right Line2 Bypass Mixer"},
1142
1143         {"Right Line Out", NULL, "Right Line2 Bypass Mixer"},
1144         {"Mono Out", NULL, "Right Line2 Bypass Mixer"},
1145         {"Right HP Out", NULL, "Right Line2 Bypass Mixer"},
1146
1147         /*
1148          * Logical path between digital mic enable and GPIO1 modulator clock
1149          * output function
1150          */
1151         {"GPIO1 dmic modclk", NULL, "DMic Rate 128"},
1152         {"GPIO1 dmic modclk", NULL, "DMic Rate 64"},
1153         {"GPIO1 dmic modclk", NULL, "DMic Rate 32"},
1154 };
1155
1156 static int aic3x_add_widgets(struct snd_soc_codec *codec)
1157 {
1158         snd_soc_dapm_new_controls(codec, aic3x_dapm_widgets,
1159                                   ARRAY_SIZE(aic3x_dapm_widgets));
1160
1161         /* set up audio path interconnects */
1162         snd_soc_dapm_add_routes(codec, intercon, ARRAY_SIZE(intercon));
1163
1164         snd_soc_dapm_new_widgets(codec);
1165         return 0;
1166 }
1167
1168 static int aic3x_hw_params(struct snd_pcm_substream *substream,
1169                            struct snd_pcm_hw_params *params)
1170 {
1171         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1172         struct snd_soc_device *socdev = rtd->socdev;
1173         struct snd_soc_codec *codec = socdev->codec;
1174         struct aic3x_priv *aic3x = codec->private_data;
1175         int codec_clk = 0, bypass_pll = 0, fsref, last_clk = 0;
1176         u8 data, r, p, pll_q, pll_p = 1, pll_r = 1, pll_j = 1;
1177         u16 pll_d = 1;
1178
1179         /* select data word length */
1180         data =
1181             aic3x_read_reg_cache(codec, AIC3X_ASD_INTF_CTRLB) & (~(0x3 << 4));
1182         switch (params_format(params)) {
1183         case SNDRV_PCM_FORMAT_S16_LE:
1184                 break;
1185         case SNDRV_PCM_FORMAT_S20_3LE:
1186                 data |= (0x01 << 4);
1187                 break;
1188         case SNDRV_PCM_FORMAT_S24_LE:
1189                 data |= (0x02 << 4);
1190                 break;
1191         case SNDRV_PCM_FORMAT_S32_LE:
1192                 data |= (0x03 << 4);
1193                 break;
1194         }
1195         aic3x_write(codec, AIC3X_ASD_INTF_CTRLB, data);
1196
1197         /* Fsref can be 44100 or 48000 */
1198         fsref = (params_rate(params) % 11025 == 0) ? 44100 : 48000;
1199
1200         /* Try to find a value for Q which allows us to bypass the PLL and
1201          * generate CODEC_CLK directly. */
1202         for (pll_q = 2; pll_q < 18; pll_q++)
1203                 if (aic3x->sysclk / (128 * pll_q) == fsref) {
1204                         bypass_pll = 1;
1205                         break;
1206                 }
1207
1208         if (bypass_pll) {
1209                 pll_q &= 0xf;
1210                 aic3x_write(codec, AIC3X_PLL_PROGA_REG, pll_q << PLLQ_SHIFT);
1211                 aic3x_write(codec, AIC3X_GPIOB_REG, CODEC_CLKIN_CLKDIV);
1212         } else
1213                 aic3x_write(codec, AIC3X_GPIOB_REG, CODEC_CLKIN_PLLDIV);
1214
1215         /* Route Left DAC to left channel input and
1216          * right DAC to right channel input */
1217         data = (LDAC2LCH | RDAC2RCH);
1218         data |= (fsref == 44100) ? FSREF_44100 : FSREF_48000;
1219         if (params_rate(params) >= 64000)
1220                 data |= DUAL_RATE_MODE;
1221         aic3x_write(codec, AIC3X_CODEC_DATAPATH_REG, data);
1222
1223         /* codec sample rate select */
1224         data = (fsref * 20) / params_rate(params);
1225         if (params_rate(params) < 64000)
1226                 data /= 2;
1227         data /= 5;
1228         data -= 2;
1229         data |= (data << 4);
1230         aic3x_write(codec, AIC3X_SAMPLE_RATE_SEL_REG, data);
1231
1232         if (bypass_pll)
1233                 return 0;
1234
1235         /* Use PLL
1236          * find an apropriate setup for j, d, r and p by iterating over
1237          * p and r - j and d are calculated for each fraction.
1238          * Up to 128 values are probed, the closest one wins the game.
1239          * The sysclk is divided by 1000 to prevent integer overflows.
1240          */
1241         codec_clk = (2048 * fsref) / (aic3x->sysclk / 1000);
1242
1243         for (r = 1; r <= 16; r++)
1244                 for (p = 1; p <= 8; p++) {
1245                         int clk, tmp = (codec_clk * pll_r * 10) / pll_p;
1246                         u8 j = tmp / 10000;
1247                         u16 d = tmp % 10000;
1248
1249                         if (j > 63)
1250                                 continue;
1251
1252                         if (d != 0 && aic3x->sysclk < 10000000)
1253                                 continue;
1254
1255                         /* This is actually 1000 * ((j + (d/10000)) * r) / p
1256                          * The term had to be converted to get rid of the
1257                          * division by 10000 */
1258                         clk = ((10000 * j * r) + (d * r)) / (10 * p);
1259
1260                         /* check whether this values get closer than the best
1261                          * ones we had before */
1262                         if (abs(codec_clk - clk) < abs(codec_clk - last_clk)) {
1263                                 pll_j = j; pll_d = d; pll_r = r; pll_p = p;
1264                                 last_clk = clk;
1265                         }
1266
1267                         /* Early exit for exact matches */
1268                         if (clk == codec_clk)
1269                                 break;
1270                 }
1271
1272         if (last_clk == 0) {
1273                 printk(KERN_ERR "%s(): unable to setup PLL\n", __func__);
1274                 return -EINVAL;
1275         }
1276
1277         data = aic3x_read_reg_cache(codec, AIC3X_PLL_PROGA_REG);
1278         aic3x_write(codec, AIC3X_PLL_PROGA_REG, data | (pll_p << PLLP_SHIFT));
1279         aic3x_write(codec, AIC3X_OVRF_STATUS_AND_PLLR_REG, pll_r << PLLR_SHIFT);
1280         aic3x_write(codec, AIC3X_PLL_PROGB_REG, pll_j << PLLJ_SHIFT);
1281         aic3x_write(codec, AIC3X_PLL_PROGC_REG, (pll_d >> 6) << PLLD_MSB_SHIFT);
1282         aic3x_write(codec, AIC3X_PLL_PROGD_REG,
1283                     (pll_d & 0x3F) << PLLD_LSB_SHIFT);
1284
1285         return 0;
1286 }
1287
1288 static int aic3x_mute(struct snd_soc_dai *dai, int mute)
1289 {
1290         struct snd_soc_codec *codec = dai->codec;
1291         u8 ldac_reg = aic3x_read_reg_cache(codec, LDAC_VOL) & ~MUTE_ON;
1292         u8 rdac_reg = aic3x_read_reg_cache(codec, RDAC_VOL) & ~MUTE_ON;
1293
1294         if (mute) {
1295                 aic3x_write(codec, LDAC_VOL, ldac_reg | MUTE_ON);
1296                 aic3x_write(codec, RDAC_VOL, rdac_reg | MUTE_ON);
1297         } else {
1298                 aic3x_write(codec, LDAC_VOL, ldac_reg);
1299                 aic3x_write(codec, RDAC_VOL, rdac_reg);
1300         }
1301
1302         return 0;
1303 }
1304
1305 static int aic3x_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1306                                 int clk_id, unsigned int freq, int dir)
1307 {
1308         struct snd_soc_codec *codec = codec_dai->codec;
1309         struct aic3x_priv *aic3x = codec->private_data;
1310
1311         aic3x->sysclk = freq;
1312         return 0;
1313 }
1314
1315 static int aic3x_set_dai_fmt(struct snd_soc_dai *codec_dai,
1316                              unsigned int fmt)
1317 {
1318         struct snd_soc_codec *codec = codec_dai->codec;
1319         struct aic3x_priv *aic3x = codec->private_data;
1320         u8 iface_areg, iface_breg;
1321         int delay = 0;
1322
1323         iface_areg = aic3x_read_reg_cache(codec, AIC3X_ASD_INTF_CTRLA) & 0x3f;
1324         iface_breg = aic3x_read_reg_cache(codec, AIC3X_ASD_INTF_CTRLB) & 0x3f;
1325
1326         /* set master/slave audio interface */
1327         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1328         case SND_SOC_DAIFMT_CBM_CFM:
1329                 aic3x->master = 1;
1330                 iface_areg |= BIT_CLK_MASTER | WORD_CLK_MASTER;
1331                 break;
1332         case SND_SOC_DAIFMT_CBS_CFS:
1333                 aic3x->master = 0;
1334                 break;
1335         default:
1336                 return -EINVAL;
1337         }
1338
1339         /*
1340          * match both interface format and signal polarities since they
1341          * are fixed
1342          */
1343         switch (fmt & (SND_SOC_DAIFMT_FORMAT_MASK |
1344                        SND_SOC_DAIFMT_INV_MASK)) {
1345         case (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF):
1346                 break;
1347         case (SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_IB_NF):
1348                 delay = 1;
1349         case (SND_SOC_DAIFMT_DSP_B | SND_SOC_DAIFMT_IB_NF):
1350                 iface_breg |= (0x01 << 6);
1351                 break;
1352         case (SND_SOC_DAIFMT_RIGHT_J | SND_SOC_DAIFMT_NB_NF):
1353                 iface_breg |= (0x02 << 6);
1354                 break;
1355         case (SND_SOC_DAIFMT_LEFT_J | SND_SOC_DAIFMT_NB_NF):
1356                 iface_breg |= (0x03 << 6);
1357                 break;
1358         default:
1359                 return -EINVAL;
1360         }
1361
1362         /* set iface */
1363         aic3x_write(codec, AIC3X_ASD_INTF_CTRLA, iface_areg);
1364         aic3x_write(codec, AIC3X_ASD_INTF_CTRLB, iface_breg);
1365         aic3x_write(codec, AIC3X_ASD_INTF_CTRLC, delay);
1366
1367         return 0;
1368 }
1369
1370 static int aic3x_set_bias_level(struct snd_soc_codec *codec,
1371                                 enum snd_soc_bias_level level)
1372 {
1373         struct aic3x_priv *aic3x = codec->private_data;
1374         u8 reg;
1375
1376         switch (level) {
1377         case SND_SOC_BIAS_ON:
1378                 /* all power is driven by DAPM system */
1379                 if (aic3x->master) {
1380                         /* enable pll */
1381                         reg = aic3x_read_reg_cache(codec, AIC3X_PLL_PROGA_REG);
1382                         aic3x_write(codec, AIC3X_PLL_PROGA_REG,
1383                                     reg | PLL_ENABLE);
1384                         /*
1385                          * ensure that bit and word clocks are running also if
1386                          * DAC and ADC are shutdown
1387                          */
1388                         reg = aic3x_read_reg_cache(codec, AIC3X_ASD_INTF_CTRLA);
1389                         aic3x_write(codec, AIC3X_ASD_INTF_CTRLA, reg | 0x10);
1390                 }
1391                 break;
1392         case SND_SOC_BIAS_PREPARE:
1393                 break;
1394         case SND_SOC_BIAS_STANDBY:
1395                 /*
1396                  * all power is driven by DAPM system,
1397                  * so output power is safe if bypass was set
1398                  */
1399                 if (aic3x->master) {
1400                         reg = aic3x_read_reg_cache(codec, AIC3X_ASD_INTF_CTRLA);
1401                         aic3x_write(codec, AIC3X_ASD_INTF_CTRLA, reg & ~0x10);
1402                         /* disable pll */
1403                         reg = aic3x_read_reg_cache(codec, AIC3X_PLL_PROGA_REG);
1404                         aic3x_write(codec, AIC3X_PLL_PROGA_REG,
1405                                     reg & ~PLL_ENABLE);
1406                 }
1407                 /* Reset cannot be issued, if bypass paths are in use */
1408                 if (!aic3x->prepare_reset)
1409                         aic3x_reset(codec);
1410                 break;
1411         case SND_SOC_BIAS_OFF:
1412                 /* force all power off */
1413                 reg = aic3x_read_reg_cache(codec, LINE1L_2_LADC_CTRL);
1414                 aic3x_write(codec, LINE1L_2_LADC_CTRL, reg & ~LADC_PWR_ON);
1415                 reg = aic3x_read_reg_cache(codec, LINE1R_2_RADC_CTRL);
1416                 aic3x_write(codec, LINE1R_2_RADC_CTRL, reg & ~RADC_PWR_ON);
1417
1418                 reg = aic3x_read_reg_cache(codec, DAC_PWR);
1419                 aic3x_write(codec, DAC_PWR, reg & ~(LDAC_PWR_ON | RDAC_PWR_ON));
1420
1421                 reg = aic3x_read_reg_cache(codec, HPLOUT_CTRL);
1422                 aic3x_write(codec, HPLOUT_CTRL, reg & ~HPLOUT_PWR_ON);
1423                 reg = aic3x_read_reg_cache(codec, HPROUT_CTRL);
1424                 aic3x_write(codec, HPROUT_CTRL, reg & ~HPROUT_PWR_ON);
1425
1426                 reg = aic3x_read_reg_cache(codec, HPLCOM_CTRL);
1427                 aic3x_write(codec, HPLCOM_CTRL, reg & ~HPLCOM_PWR_ON);
1428                 reg = aic3x_read_reg_cache(codec, HPRCOM_CTRL);
1429                 aic3x_write(codec, HPRCOM_CTRL, reg & ~HPRCOM_PWR_ON);
1430
1431                 reg = aic3x_read_reg_cache(codec, MONOLOPM_CTRL);
1432                 aic3x_write(codec, MONOLOPM_CTRL, reg & ~MONOLOPM_PWR_ON);
1433
1434                 reg = aic3x_read_reg_cache(codec, LLOPM_CTRL);
1435                 aic3x_write(codec, LLOPM_CTRL, reg & ~LLOPM_PWR_ON);
1436                 reg = aic3x_read_reg_cache(codec, RLOPM_CTRL);
1437                 aic3x_write(codec, RLOPM_CTRL, reg & ~RLOPM_PWR_ON);
1438
1439                 if (aic3x->master) {
1440                         reg = aic3x_read_reg_cache(codec, AIC3X_ASD_INTF_CTRLA);
1441                         aic3x_write(codec, AIC3X_ASD_INTF_CTRLA, reg & ~0x10);
1442                         /* disable pll */
1443                         reg = aic3x_read_reg_cache(codec, AIC3X_PLL_PROGA_REG);
1444                         aic3x_write(codec, AIC3X_PLL_PROGA_REG,
1445                                     reg & ~PLL_ENABLE);
1446                 }
1447                 break;
1448         }
1449         codec->bias_level = level;
1450
1451         return 0;
1452 }
1453
1454 void aic3x_set_gpio(struct snd_soc_codec *codec, int gpio, int state)
1455 {
1456         u8 reg = gpio ? AIC3X_GPIO2_REG : AIC3X_GPIO1_REG;
1457         u8 bit = gpio ? 3: 0;
1458         u8 val = aic3x_read_reg_cache(codec, reg) & ~(1 << bit);
1459         aic3x_write(codec, reg, val | (!!state << bit));
1460 }
1461 EXPORT_SYMBOL_GPL(aic3x_set_gpio);
1462
1463 int aic3x_get_gpio(struct snd_soc_codec *codec, int gpio)
1464 {
1465         u8 reg = gpio ? AIC3X_GPIO2_REG : AIC3X_GPIO1_REG;
1466         u8 val, bit = gpio ? 2: 1;
1467
1468         aic3x_read(codec, reg, &val);
1469         return (val >> bit) & 1;
1470 }
1471 EXPORT_SYMBOL_GPL(aic3x_get_gpio);
1472
1473 int aic3x_headset_detected(struct snd_soc_codec *codec)
1474 {
1475         u8 val;
1476         aic3x_read(codec, AIC3X_RT_IRQ_FLAGS_REG, &val);
1477         return (val >> 2) & 1;
1478 }
1479 EXPORT_SYMBOL_GPL(aic3x_headset_detected);
1480
1481 #define AIC3X_RATES     SNDRV_PCM_RATE_8000_96000
1482 #define AIC3X_FORMATS   (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
1483                          SNDRV_PCM_FMTBIT_S24_3LE | SNDRV_PCM_FMTBIT_S32_LE)
1484
1485 struct snd_soc_dai aic3x_dai = {
1486         .name = "tlv320aic3x",
1487         .playback = {
1488                 .stream_name = "Playback",
1489                 .channels_min = 1,
1490                 .channels_max = 2,
1491                 .rates = AIC3X_RATES,
1492                 .formats = AIC3X_FORMATS,},
1493         .capture = {
1494                 .stream_name = "Capture",
1495                 .channels_min = 1,
1496                 .channels_max = 2,
1497                 .rates = AIC3X_RATES,
1498                 .formats = AIC3X_FORMATS,},
1499         .ops = {
1500                 .hw_params = aic3x_hw_params,
1501         },
1502         .dai_ops = {
1503                 .digital_mute = aic3x_mute,
1504                 .set_sysclk = aic3x_set_dai_sysclk,
1505                 .set_fmt = aic3x_set_dai_fmt,
1506         }
1507 };
1508 EXPORT_SYMBOL_GPL(aic3x_dai);
1509
1510 static int aic3x_suspend(struct platform_device *pdev, pm_message_t state)
1511 {
1512         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1513         struct snd_soc_codec *codec = socdev->codec;
1514
1515         aic3x_write(codec, AIC3X_RESET, SOFT_RESET);
1516
1517         return 0;
1518 }
1519
1520 static int aic3x_resume(struct platform_device *pdev)
1521 {
1522         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1523         aic3x_sync_hw(socdev->codec);
1524         aic3x_set_bias_level(socdev->codec,
1525                         socdev->codec->suspend_bias_level);
1526         return 0;
1527 }
1528
1529
1530 /*
1531  * initialise the AIC3X driver
1532  * register the mixer and dsp interfaces with the kernel
1533  */
1534 static int aic3x_init(struct snd_soc_device *socdev)
1535 {
1536         struct snd_soc_codec *codec = socdev->codec;
1537         struct aic3x_setup_data *setup = socdev->codec_data;
1538         struct snd_hwdep *hwdep;
1539     char hwdepid[] = "IIR Filter";
1540         int reg, ret = 0;
1541
1542         printk("MNZ: BEGIN aic3x_init\n");
1543         codec->name = "tlv320aic3x";
1544         codec->owner = THIS_MODULE;
1545         codec->read = aic3x_read_reg_cache;
1546         codec->write = aic3x_write;
1547         codec->set_bias_level = aic3x_set_bias_level;
1548         codec->dai = &aic3x_dai;
1549         codec->num_dai = 1;
1550         codec->reg_cache_size = ARRAY_SIZE(aic3x_reg);
1551         codec->reg_cache = kmemdup(aic3x_reg, sizeof(aic3x_reg), GFP_KERNEL);
1552         if (codec->reg_cache == NULL)
1553                 return -ENOMEM;
1554
1555         aic3x_write(codec, AIC3X_PAGE_SELECT, PAGE0_SELECT);
1556         aic3x_write(codec, AIC3X_RESET, SOFT_RESET);
1557
1558         /* register pcms */
1559         ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
1560         if (ret < 0) {
1561                 printk(KERN_ERR "aic3x: failed to create pcms\n");
1562                 goto pcm_err;
1563         }
1564
1565         /* DAC default volume and mute */
1566         aic3x_write(codec, LDAC_VOL, DEFAULT_VOL | MUTE_ON);
1567         aic3x_write(codec, RDAC_VOL, DEFAULT_VOL | MUTE_ON);
1568
1569         /* DAC to HP default volume and route to Output mixer */
1570         aic3x_write(codec, DACL1_2_HPLOUT_VOL, DEFAULT_VOL | ROUTE_ON);
1571         aic3x_write(codec, DACR1_2_HPROUT_VOL, DEFAULT_VOL | ROUTE_ON);
1572         aic3x_write(codec, DACL1_2_HPLCOM_VOL, DEFAULT_VOL | ROUTE_ON);
1573         aic3x_write(codec, DACR1_2_HPRCOM_VOL, DEFAULT_VOL | ROUTE_ON);
1574         /* DAC to Line Out default volume and route to Output mixer */
1575         aic3x_write(codec, DACL1_2_LLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
1576         aic3x_write(codec, DACR1_2_RLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
1577         /* DAC to Mono Line Out default volume and route to Output mixer */
1578         aic3x_write(codec, DACL1_2_MONOLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
1579         aic3x_write(codec, DACR1_2_MONOLOPM_VOL, DEFAULT_VOL | ROUTE_ON);
1580
1581         /* unmute all outputs */
1582         reg = aic3x_read_reg_cache(codec, LLOPM_CTRL);
1583         aic3x_write(codec, LLOPM_CTRL, reg | UNMUTE);
1584         reg = aic3x_read_reg_cache(codec, RLOPM_CTRL);
1585         aic3x_write(codec, RLOPM_CTRL, reg | UNMUTE);
1586         reg = aic3x_read_reg_cache(codec, MONOLOPM_CTRL);
1587         aic3x_write(codec, MONOLOPM_CTRL, reg | UNMUTE);
1588         reg = aic3x_read_reg_cache(codec, HPLOUT_CTRL);
1589         aic3x_write(codec, HPLOUT_CTRL, reg | UNMUTE);
1590         reg = aic3x_read_reg_cache(codec, HPROUT_CTRL);
1591         aic3x_write(codec, HPROUT_CTRL, reg | UNMUTE);
1592         reg = aic3x_read_reg_cache(codec, HPLCOM_CTRL);
1593         aic3x_write(codec, HPLCOM_CTRL, reg | UNMUTE);
1594         reg = aic3x_read_reg_cache(codec, HPRCOM_CTRL);
1595         aic3x_write(codec, HPRCOM_CTRL, reg | UNMUTE);
1596
1597         /* ADC default volume and unmute */
1598         aic3x_write(codec, LADC_VOL, DEFAULT_GAIN);
1599         aic3x_write(codec, RADC_VOL, DEFAULT_GAIN);
1600         /* By default route Line1 to ADC PGA mixer */
1601         aic3x_write(codec, LINE1L_2_LADC_CTRL, 0x0);
1602         aic3x_write(codec, LINE1R_2_RADC_CTRL, 0x0);
1603
1604         /* PGA to HP Bypass default volume, disconnect from Output Mixer */
1605         aic3x_write(codec, PGAL_2_HPLOUT_VOL, DEFAULT_VOL);
1606         aic3x_write(codec, PGAR_2_HPROUT_VOL, DEFAULT_VOL);
1607         aic3x_write(codec, PGAL_2_HPLCOM_VOL, DEFAULT_VOL);
1608         aic3x_write(codec, PGAR_2_HPRCOM_VOL, DEFAULT_VOL);
1609         /* PGA to Line Out default volume, disconnect from Output Mixer */
1610         aic3x_write(codec, PGAL_2_LLOPM_VOL, DEFAULT_VOL);
1611         aic3x_write(codec, PGAR_2_RLOPM_VOL, DEFAULT_VOL);
1612         /* PGA to Mono Line Out default volume, disconnect from Output Mixer */
1613         aic3x_write(codec, PGAL_2_MONOLOPM_VOL, DEFAULT_VOL);
1614         aic3x_write(codec, PGAR_2_MONOLOPM_VOL, DEFAULT_VOL);
1615
1616         /* Line2 to HP Bypass default volume, disconnect from Output Mixer */
1617         aic3x_write(codec, LINE2L_2_HPLOUT_VOL, DEFAULT_VOL);
1618         aic3x_write(codec, LINE2R_2_HPROUT_VOL, DEFAULT_VOL);
1619         aic3x_write(codec, LINE2L_2_HPLCOM_VOL, DEFAULT_VOL);
1620         aic3x_write(codec, LINE2R_2_HPRCOM_VOL, DEFAULT_VOL);
1621         /* Line2 Line Out default volume, disconnect from Output Mixer */
1622         aic3x_write(codec, LINE2L_2_LLOPM_VOL, DEFAULT_VOL);
1623         aic3x_write(codec, LINE2R_2_RLOPM_VOL, DEFAULT_VOL);
1624         /* Line2 to Mono Out default volume, disconnect from Output Mixer */
1625         aic3x_write(codec, LINE2L_2_MONOLOPM_VOL, DEFAULT_VOL);
1626         aic3x_write(codec, LINE2R_2_MONOLOPM_VOL, DEFAULT_VOL);
1627
1628         /* off, with power on */
1629         aic3x_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1630
1631         /* setup GPIO functions */
1632         aic3x_write(codec, AIC3X_GPIO1_REG, (setup->gpio_func[0] & 0xf) << 4);
1633         aic3x_write(codec, AIC3X_GPIO2_REG, (setup->gpio_func[1] & 0xf) << 4);
1634
1635         aic3x_add_controls(codec);
1636         aic3x_add_widgets(codec);
1637
1638         if(snd_hwdep_new(codec->card, hwdepid, 0, &hwdep) == 0){
1639                 hwdep->private_data = codec;
1640                 sprintf(hwdep->name, hwdepid);
1641                 hwdep->ops.open = snd_hwdep_dacfilter_open_aic3x;
1642                 hwdep->ops.ioctl = snd_hwdep_dacfilter_ioctl_aic3x;
1643                 hwdep->ops.read = snd_hwdep_dacfilter_read_aic3x;
1644                 hwdep->ops.write = snd_hwdep_dacfilter_write_aic3x;
1645                 ((struct aic3x_priv*)codec->private_data)->hwdep = hwdep;
1646         }
1647
1648         ret = snd_soc_register_card(socdev);
1649
1650         if (ret < 0) {
1651                 printk(KERN_ERR "aic3x: failed to register card\n");
1652                 goto card_err;
1653         }
1654
1655         /* Set some defaults for coefficients */
1656         aic3x_write_coeff(codec, EFFECTS_3DATTEN, -32768);
1657         printk("MNZ: END aic3x_init\n");
1658
1659         return ret;
1660
1661 card_err:
1662         snd_soc_free_pcms(socdev);
1663         snd_soc_dapm_free(socdev);
1664 pcm_err:
1665         kfree(codec->reg_cache);
1666         return ret;
1667 }
1668
1669 static struct snd_soc_device *aic3x_socdev;
1670
1671 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1672 /*
1673  * AIC3X 2 wire address can be up to 4 devices with device addresses
1674  * 0x18, 0x19, 0x1A, 0x1B
1675  */
1676
1677 /*
1678  * If the i2c layer weren't so broken, we could pass this kind of data
1679  * around
1680  */
1681 static int aic3x_i2c_probe(struct i2c_client *i2c,
1682                            const struct i2c_device_id *id)
1683 {
1684         struct snd_soc_device *socdev = aic3x_socdev;
1685         struct snd_soc_codec *codec = socdev->codec;
1686         int ret;
1687
1688         i2c_set_clientdata(i2c, codec);
1689         codec->control_data = i2c;
1690
1691         ret = aic3x_init(socdev);
1692         if (ret < 0)
1693                 printk(KERN_ERR "aic3x: failed to initialise AIC3X\n");
1694         return ret;
1695 }
1696
1697 static int aic3x_i2c_remove(struct i2c_client *client)
1698 {
1699         struct snd_soc_codec *codec = i2c_get_clientdata(client);
1700         kfree(codec->reg_cache);
1701         return 0;
1702 }
1703
1704 static const struct i2c_device_id aic3x_i2c_id[] = {
1705         { "tlv320aic3x", 0 },
1706         { }
1707 };
1708 MODULE_DEVICE_TABLE(i2c, aic3x_i2c_id);
1709
1710 /* machine i2c codec control layer */
1711 static struct i2c_driver aic3x_i2c_driver = {
1712         .driver = {
1713                 .name = "aic3x I2C Codec",
1714                 .owner = THIS_MODULE,
1715         },
1716         .probe = aic3x_i2c_probe,
1717         .remove = aic3x_i2c_remove,
1718         .id_table = aic3x_i2c_id,
1719 };
1720
1721 static int aic3x_i2c_read(struct i2c_client *client, u8 *value, int len)
1722 {
1723         value[0] = i2c_smbus_read_byte_data(client, value[0]);
1724         return (len == 1);
1725 }
1726
1727 static int aic3x_add_i2c_device(struct platform_device *pdev,
1728                                  const struct aic3x_setup_data *setup)
1729 {
1730         struct i2c_board_info info;
1731         struct i2c_adapter *adapter;
1732         struct i2c_client *client;
1733         int ret;
1734
1735         ret = i2c_add_driver(&aic3x_i2c_driver);
1736         if (ret != 0) {
1737                 dev_err(&pdev->dev, "can't add i2c driver\n");
1738                 return ret;
1739         }
1740
1741         memset(&info, 0, sizeof(struct i2c_board_info));
1742         info.addr = setup->i2c_address;
1743         strlcpy(info.type, "tlv320aic3x", I2C_NAME_SIZE);
1744
1745         adapter = i2c_get_adapter(setup->i2c_bus);
1746         if (!adapter) {
1747                 dev_err(&pdev->dev, "can't get i2c adapter %d\n",
1748                         setup->i2c_bus);
1749                 goto err_driver;
1750         }
1751
1752         client = i2c_new_device(adapter, &info);
1753         i2c_put_adapter(adapter);
1754         if (!client) {
1755                 dev_err(&pdev->dev, "can't add i2c device at 0x%x\n",
1756                         (unsigned int)info.addr);
1757                 goto err_driver;
1758         }
1759
1760         return 0;
1761
1762 err_driver:
1763         i2c_del_driver(&aic3x_i2c_driver);
1764         return -ENODEV;
1765 }
1766 #endif
1767
1768 static int aic3x_probe(struct platform_device *pdev)
1769 {
1770         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1771         struct aic3x_setup_data *setup;
1772         struct snd_soc_codec *codec;
1773         struct aic3x_priv *aic3x;
1774         int ret = 0;
1775
1776         printk(KERN_INFO "AIC3X Audio Codec %s\n", AIC3X_VERSION);
1777
1778         setup = socdev->codec_data;
1779         codec = kzalloc(sizeof(struct snd_soc_codec), GFP_KERNEL);
1780         if (codec == NULL)
1781                 return -ENOMEM;
1782
1783         aic3x = kzalloc(sizeof(struct aic3x_priv), GFP_KERNEL);
1784         memcpy(&aic3x->dacfilter, &aic3x_dacfilter, sizeof(aic3x_dacfilter));
1785
1786         if (aic3x == NULL) {
1787                 kfree(codec);
1788                 return -ENOMEM;
1789         }
1790
1791         codec->private_data = aic3x;
1792         socdev->codec = codec;
1793         mutex_init(&codec->mutex);
1794         INIT_LIST_HEAD(&codec->dapm_widgets);
1795         INIT_LIST_HEAD(&codec->dapm_paths);
1796
1797         aic3x_socdev = socdev;
1798 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1799         if (setup->i2c_address) {
1800                 codec->hw_write = (hw_write_t) i2c_master_send;
1801                 codec->hw_read = (hw_read_t) aic3x_i2c_read;
1802                 ret = aic3x_add_i2c_device(pdev, setup);
1803         }
1804 #else
1805         /* Add other interfaces here */
1806 #endif
1807
1808         if (ret != 0) {
1809                 kfree(codec->private_data);
1810                 kfree(codec);
1811         }
1812         return ret;
1813 }
1814
1815 static int aic3x_remove(struct platform_device *pdev)
1816 {
1817         struct snd_soc_device *socdev = platform_get_drvdata(pdev);
1818         struct snd_soc_codec *codec = socdev->codec;
1819
1820         /* power down chip */
1821         if (codec->control_data)
1822                 aic3x_set_bias_level(codec, SND_SOC_BIAS_OFF);
1823
1824         snd_soc_free_pcms(socdev);
1825         snd_soc_dapm_free(socdev);
1826 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1827         if (codec->control_data)
1828                 i2c_unregister_device(codec->control_data);
1829         i2c_del_driver(&aic3x_i2c_driver);
1830 #endif
1831         kfree(codec->private_data);
1832         kfree(codec);
1833
1834         return 0;
1835 }
1836
1837 struct snd_soc_codec_device soc_codec_dev_aic3x = {
1838         .probe = aic3x_probe,
1839         .remove = aic3x_remove,
1840         .suspend = aic3x_suspend,
1841         .resume = aic3x_resume,
1842 };
1843 EXPORT_SYMBOL_GPL(soc_codec_dev_aic3x);
1844
1845 MODULE_DESCRIPTION("ASoC TLV320AIC3X codec driver");
1846 MODULE_AUTHOR("Vladimir Barinov");
1847 MODULE_LICENSE("GPL");