ASoC: ac108: Fix changed interface
[platform/kernel/linux-rpi.git] / sound / soc / codecs / ac108.c
1 /*
2  * ac10x.c  --  ac10x ALSA SoC Audio driver
3  *
4  *
5  * Author: Baozhu Zuo<zuobaozhu@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11
12 /* #undef DEBUG
13  * use 'make DEBUG=1' to enable debugging
14  */
15 #include <linux/module.h>
16 #include <linux/moduleparam.h>
17 #include <linux/init.h>
18 #include <linux/delay.h>
19 #include <linux/pm.h>
20 #include <linux/clk.h>
21 #include <linux/i2c.h>
22 #include <linux/slab.h>
23 #include <linux/workqueue.h>
24 #include <linux/regmap.h>
25 #include <linux/gpio/consumer.h>
26 #include <sound/core.h>
27 #include <sound/pcm.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/initval.h>
31 #include <sound/tlv.h>
32 #include "ac108.h"
33 #include "ac10x.h"
34
35 #define _USE_CAPTURE    1
36 #define _MASTER_INDEX   0
37
38 /**
39  * TODO:
40  * 1, add PM API:  ac108_suspend,ac108_resume
41  * 2,0x65-0x6a
42  * 3,0x76-0x79 high 4bit
43  */
44 struct pll_div {
45         unsigned int freq_in;
46         unsigned int freq_out;
47         unsigned int m1;
48         unsigned int m2;
49         unsigned int n;
50         unsigned int k1;
51         unsigned int k2;
52 };
53
54 static struct ac10x_priv *ac10x;
55
56 struct real_val_to_reg_val {
57         unsigned int real_val;
58         unsigned int reg_val;
59 };
60
61 static const struct real_val_to_reg_val ac108_sample_rate[] = {
62         { 8000,  0 },
63         { 11025, 1 },
64         { 12000, 2 },
65         { 16000, 3 },
66         { 22050, 4 },
67         { 24000, 5 },
68         { 32000, 6 },
69         { 44100, 7 },
70         { 48000, 8 },
71         { 96000, 9 },
72 };
73
74 /* Sample resolution */
75 static const struct real_val_to_reg_val ac108_samp_res[] = {
76         { 8,  1 },
77         { 12, 2 },
78         { 16, 3 },
79         { 20, 4 },
80         { 24, 5 },
81         { 28, 6 },
82         { 32, 7 },
83 };
84
85 static const unsigned ac108_bclkdivs[] = {
86          0,   1,   2,   4,
87          6,   8,  12,  16,
88         24,  32,  48,  64,
89         96, 128, 176, 192,
90 };
91
92 /* FOUT =(FIN * N) / [(M1+1) * (M2+1)*(K1+1)*(K2+1)] ;  M1[0,31],  M2[0,1],  N[0,1023],  K1[0,31],  K2[0,1] */
93 static const struct pll_div ac108_pll_div_list[] = {
94         { 400000,   _FREQ_24_576K, 0,  0, 614, 4, 1 },
95         { 512000,   _FREQ_24_576K, 0,  0, 960, 9, 1 }, //_FREQ_24_576K/48
96         { 768000,   _FREQ_24_576K, 0,  0, 640, 9, 1 }, //_FREQ_24_576K/32
97         { 800000,   _FREQ_24_576K, 0,  0, 614, 9, 1 },
98         { 1024000,  _FREQ_24_576K, 0,  0, 480, 9, 1 }, //_FREQ_24_576K/24
99         { 1600000,  _FREQ_24_576K, 0,  0, 307, 9, 1 },
100         { 2048000,  _FREQ_24_576K, 0,  0, 240, 9, 1 }, /* accurate,  8000 * 256 */
101         { 3072000,  _FREQ_24_576K, 0,  0, 160, 9, 1 }, /* accurate, 12000 * 256 */
102         { 4096000,  _FREQ_24_576K, 2,  0, 360, 9, 1 }, /* accurate, 16000 * 256 */
103         { 6000000,  _FREQ_24_576K, 4,  0, 410, 9, 1 },
104         { 12000000, _FREQ_24_576K, 9,  0, 410, 9, 1 },
105         { 13000000, _FREQ_24_576K, 8,  0, 340, 9, 1 },
106         { 15360000, _FREQ_24_576K, 12, 0, 415, 9, 1 },
107         { 16000000, _FREQ_24_576K, 12, 0, 400, 9, 1 },
108         { 19200000, _FREQ_24_576K, 15, 0, 410, 9, 1 },
109         { 19680000, _FREQ_24_576K, 15, 0, 400, 9, 1 },
110         { 24000000, _FREQ_24_576K, 4,  0, 128,24, 0 }, // accurate, 24M -> 24.576M */
111
112         { 400000,   _FREQ_22_579K, 0,  0, 566, 4, 1 },
113         { 512000,   _FREQ_22_579K, 0,  0, 880, 9, 1 },
114         { 768000,   _FREQ_22_579K, 0,  0, 587, 9, 1 },
115         { 800000,   _FREQ_22_579K, 0,  0, 567, 9, 1 },
116         { 1024000,  _FREQ_22_579K, 0,  0, 440, 9, 1 },
117         { 1600000,  _FREQ_22_579K, 1,  0, 567, 9, 1 },
118         { 2048000,  _FREQ_22_579K, 0,  0, 220, 9, 1 },
119         { 3072000,  _FREQ_22_579K, 0,  0, 148, 9, 1 },
120         { 4096000,  _FREQ_22_579K, 2,  0, 330, 9, 1 },
121         { 6000000,  _FREQ_22_579K, 2,  0, 227, 9, 1 },
122         { 12000000, _FREQ_22_579K, 8,  0, 340, 9, 1 },
123         { 13000000, _FREQ_22_579K, 9,  0, 350, 9, 1 },
124         { 15360000, _FREQ_22_579K, 10, 0, 325, 9, 1 },
125         { 16000000, _FREQ_22_579K, 11, 0, 340, 9, 1 },
126         { 19200000, _FREQ_22_579K, 13, 0, 330, 9, 1 },
127         { 19680000, _FREQ_22_579K, 14, 0, 345, 9, 1 },
128         { 24000000, _FREQ_22_579K, 24, 0, 588,24, 0 }, // accurate, 24M -> 22.5792M */
129
130
131         { _FREQ_24_576K / 1,   _FREQ_24_576K, 9,  0, 200, 9, 1 }, //_FREQ_24_576K
132         { _FREQ_24_576K / 2,   _FREQ_24_576K, 9,  0, 400, 9, 1 }, /*12288000,accurate, 48000 * 256 */
133         { _FREQ_24_576K / 4,   _FREQ_24_576K, 4,  0, 400, 9, 1 }, /*6144000, accurate, 24000 * 256 */
134         { _FREQ_24_576K / 16,  _FREQ_24_576K, 0,  0, 320, 9, 1 }, //1536000
135         { _FREQ_24_576K / 64,  _FREQ_24_576K, 0,  0, 640, 4, 1 }, //384000
136         { _FREQ_24_576K / 96,  _FREQ_24_576K, 0,  0, 960, 4, 1 }, //256000
137         { _FREQ_24_576K / 128, _FREQ_24_576K, 0,  0, 512, 1, 1 }, //192000
138         { _FREQ_24_576K / 176, _FREQ_24_576K, 0,  0, 880, 4, 0 }, //140000
139         { _FREQ_24_576K / 192, _FREQ_24_576K, 0,  0, 960, 4, 0 }, //128000
140
141         { _FREQ_22_579K / 1,   _FREQ_22_579K, 9,  0, 200, 9, 1 }, //_FREQ_22_579K
142         { _FREQ_22_579K / 2,   _FREQ_22_579K, 9,  0, 400, 9, 1 }, /*11289600,accurate, 44100 * 256 */
143         { _FREQ_22_579K / 4,   _FREQ_22_579K, 4,  0, 400, 9, 1 }, /*5644800, accurate, 22050 * 256 */
144         { _FREQ_22_579K / 16,  _FREQ_22_579K, 0,  0, 320, 9, 1 }, //1411200
145         { _FREQ_22_579K / 64,  _FREQ_22_579K, 0,  0, 640, 4, 1 }, //352800
146         { _FREQ_22_579K / 96,  _FREQ_22_579K, 0,  0, 960, 4, 1 }, //235200
147         { _FREQ_22_579K / 128, _FREQ_22_579K, 0,  0, 512, 1, 1 }, //176400
148         { _FREQ_22_579K / 176, _FREQ_22_579K, 0,  0, 880, 4, 0 }, //128290
149         { _FREQ_22_579K / 192, _FREQ_22_579K, 0,  0, 960, 4, 0 }, //117600
150
151         { _FREQ_22_579K / 6,   _FREQ_22_579K, 2,  0, 360, 9, 1 }, //3763200
152         { _FREQ_22_579K / 8,   _FREQ_22_579K, 0,  0, 160, 9, 1 }, /*2822400, accurate, 11025 * 256 */
153         { _FREQ_22_579K / 12,  _FREQ_22_579K, 0,  0, 240, 9, 1 }, //1881600
154         { _FREQ_22_579K / 24,  _FREQ_22_579K, 0,  0, 480, 9, 1 }, //940800
155         { _FREQ_22_579K / 32,  _FREQ_22_579K, 0,  0, 640, 9, 1 }, //705600
156         { _FREQ_22_579K / 48,  _FREQ_22_579K, 0,  0, 960, 9, 1 }, //470400
157 };
158
159
160 /* AC108 definition */
161 #define AC108_CHANNELS_MAX              8               /* range[1, 16] */
162 #define AC108_RATES                     (SNDRV_PCM_RATE_8000_96000 &            \
163                                         ~(SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_64000 | \
164                                         SNDRV_PCM_RATE_88200 | SNDRV_PCM_RATE_96000))
165 #define AC108_FORMATS                   (/*SNDRV_PCM_FMTBIT_S16_LE | \
166                                         SNDRV_PCM_FMTBIT_S20_3LE |   \
167                                         SNDRV_PCM_FMTBIT_S24_LE |*/  \
168                                         SNDRV_PCM_FMTBIT_S32_LE)
169
170 static const DECLARE_TLV_DB_SCALE(tlv_adc_pga_gain, 0, 100, 0);
171 static const DECLARE_TLV_DB_SCALE(tlv_ch_digital_vol, -11925,75,0);
172
173 int ac10x_read(u8 reg, u8* rt_val, struct regmap* i2cm) {
174         int r, v = 0;
175
176         if ((r = regmap_read(i2cm, reg, &v)) < 0) {
177                 pr_err("ac10x_read error->[REG-0x%02x]\n", reg);
178         } else {
179                 *rt_val = v;
180         }
181         return r;
182 }
183
184 int ac10x_write(u8 reg, u8 val, struct regmap* i2cm) {
185         int r;
186
187         if ((r = regmap_write(i2cm, reg, val)) < 0) {
188                 pr_err("ac10x_write error->[REG-0x%02x,val-0x%02x]\n", reg, val);
189         }
190         return r;
191 }
192
193 int ac10x_update_bits(u8 reg, u8 mask, u8 val, struct regmap* i2cm) {
194         int r;
195
196         if ((r = regmap_update_bits(i2cm, reg, mask, val)) < 0) {
197                 pr_err("%s() error->[REG-0x%02x,val-0x%02x]\n", __func__, reg, val);
198         }
199         return r;
200 }
201
202 /**
203  * snd_ac108_get_volsw - single mixer get callback
204  * @kcontrol: mixer control
205  * @ucontrol: control element information
206  *
207  * Callback to get the value of a single mixer control, or a double mixer
208  * control that spans 2 registers.
209  *
210  * Returns 0 for success.
211  */
212 static int snd_ac108_get_volsw(struct snd_kcontrol *kcontrol,
213         struct snd_ctl_elem_value *ucontrol
214 ){
215         struct soc_mixer_control *mc =
216                 (struct soc_mixer_control *)kcontrol->private_value;
217         unsigned int mask = (1 << fls(mc->max)) - 1;
218         unsigned int invert = mc->invert;
219         int ret, chip = mc->autodisable;
220         u8 val;
221
222         if ((ret = ac10x_read(mc->reg, &val, ac10x->i2cmap[chip])) < 0)
223                 return ret;
224
225         val = ((val >> mc->shift) & mask) - mc->min;
226         if (invert) {
227                 val = mc->max - val;
228         }
229         ucontrol->value.integer.value[0] = val;
230         return 0;
231 }
232
233 /**
234  * snd_ac108_put_volsw - single mixer put callback
235  * @kcontrol: mixer control
236  * @ucontrol: control element information
237  *
238  * Callback to set the value of a single mixer control, or a double mixer
239  * control that spans 2 registers.
240  *
241  * Returns 0 for success.
242  */
243 static int snd_ac108_put_volsw(struct snd_kcontrol *kcontrol,
244         struct snd_ctl_elem_value *ucontrol
245 ){
246         struct soc_mixer_control *mc =
247                 (struct soc_mixer_control *)kcontrol->private_value;
248         unsigned int sign_bit = mc->sign_bit;
249         unsigned int val, mask = (1 << fls(mc->max)) - 1;
250         unsigned int invert = mc->invert;
251         int ret, chip = mc->autodisable;
252
253         if (sign_bit)
254                 mask = BIT(sign_bit + 1) - 1;
255
256         val = ((ucontrol->value.integer.value[0] + mc->min) & mask);
257         if (invert) {
258                 val = mc->max - val;
259         }
260
261         mask = mask << mc->shift;
262         val = val << mc->shift;
263
264         ret = ac10x_update_bits(mc->reg, mask, val, ac10x->i2cmap[chip]);
265         return ret;
266 }
267
268 #define SOC_AC108_SINGLE_TLV(xname, reg, shift, max, invert, chip, tlv_array) \
269 {       .iface = SNDRV_CTL_ELEM_IFACE_MIXER, .name = xname, \
270         .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\
271                  SNDRV_CTL_ELEM_ACCESS_READWRITE,\
272         .tlv.p = (tlv_array), \
273         .info = snd_soc_info_volsw, .get = snd_ac108_get_volsw,\
274         .put = snd_ac108_put_volsw, \
275         .private_value = SOC_SINGLE_VALUE(reg, shift, max, invert, chip) }
276
277 /* single ac108 */
278 static const struct snd_kcontrol_new ac108_snd_controls[] = {
279         /* ### chip 0 ### */
280         /*0x70: ADC1 Digital Channel Volume Control Register*/
281         SOC_AC108_SINGLE_TLV("CH1 digital volume", ADC1_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
282         /*0x71: ADC2 Digital Channel Volume Control Register*/
283         SOC_AC108_SINGLE_TLV("CH2 digital volume", ADC2_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
284         /*0x72: ADC3 Digital Channel Volume Control Register*/
285         SOC_AC108_SINGLE_TLV("CH3 digital volume", ADC3_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
286         /*0x73: ADC4 Digital Channel Volume Control Register*/
287         SOC_AC108_SINGLE_TLV("CH4 digital volume", ADC4_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
288
289         /*0x90: Analog PGA1 Control Register*/
290         SOC_AC108_SINGLE_TLV("ADC1 PGA gain", ANA_PGA1_CTRL, ADC1_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
291         /*0x91: Analog PGA2 Control Register*/
292         SOC_AC108_SINGLE_TLV("ADC2 PGA gain", ANA_PGA2_CTRL, ADC2_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
293         /*0x92: Analog PGA3 Control Register*/
294         SOC_AC108_SINGLE_TLV("ADC3 PGA gain", ANA_PGA3_CTRL, ADC3_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
295         /*0x93: Analog PGA4 Control Register*/
296         SOC_AC108_SINGLE_TLV("ADC4 PGA gain", ANA_PGA4_CTRL, ADC4_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
297 };
298 /* multiple ac108s */
299 static const struct snd_kcontrol_new ac108tdm_snd_controls[] = {
300         /* ### chip 1 ### */
301         /*0x70: ADC1 Digital Channel Volume Control Register*/
302         SOC_AC108_SINGLE_TLV("CH1 digital volume", ADC1_DVOL_CTRL, 0, 0xff, 0, 1, tlv_ch_digital_vol),
303         /*0x71: ADC2 Digital Channel Volume Control Register*/
304         SOC_AC108_SINGLE_TLV("CH2 digital volume", ADC2_DVOL_CTRL, 0, 0xff, 0, 1, tlv_ch_digital_vol),
305         /*0x72: ADC3 Digital Channel Volume Control Register*/
306         SOC_AC108_SINGLE_TLV("CH3 digital volume", ADC3_DVOL_CTRL, 0, 0xff, 0, 1, tlv_ch_digital_vol),
307         /*0x73: ADC4 Digital Channel Volume Control Register*/
308         SOC_AC108_SINGLE_TLV("CH4 digital volume", ADC4_DVOL_CTRL, 0, 0xff, 0, 1, tlv_ch_digital_vol),
309
310         /*0x90: Analog PGA1 Control Register*/
311         SOC_AC108_SINGLE_TLV("ADC1 PGA gain", ANA_PGA1_CTRL, ADC1_ANALOG_PGA, 0x1f, 0, 1, tlv_adc_pga_gain),
312         /*0x91: Analog PGA2 Control Register*/
313         SOC_AC108_SINGLE_TLV("ADC2 PGA gain", ANA_PGA2_CTRL, ADC2_ANALOG_PGA, 0x1f, 0, 1, tlv_adc_pga_gain),
314         /*0x92: Analog PGA3 Control Register*/
315         SOC_AC108_SINGLE_TLV("ADC3 PGA gain", ANA_PGA3_CTRL, ADC3_ANALOG_PGA, 0x1f, 0, 1, tlv_adc_pga_gain),
316         /*0x93: Analog PGA4 Control Register*/
317         SOC_AC108_SINGLE_TLV("ADC4 PGA gain", ANA_PGA4_CTRL, ADC4_ANALOG_PGA, 0x1f, 0, 1, tlv_adc_pga_gain),
318
319         /* ### chip 0 ### */
320         /*0x70: ADC1 Digital Channel Volume Control Register*/
321         SOC_AC108_SINGLE_TLV("CH5 digital volume", ADC1_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
322         /*0x71: ADC2 Digital Channel Volume Control Register*/
323         SOC_AC108_SINGLE_TLV("CH6 digital volume", ADC2_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
324         /*0x72: ADC3 Digital Channel Volume Control Register*/
325         SOC_AC108_SINGLE_TLV("CH7 digital volume", ADC3_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
326         /*0x73: ADC4 Digital Channel Volume Control Register*/
327         SOC_AC108_SINGLE_TLV("CH8 digital volume", ADC4_DVOL_CTRL, 0, 0xff, 0, 0, tlv_ch_digital_vol),
328
329         /*0x90: Analog PGA1 Control Register*/
330         SOC_AC108_SINGLE_TLV("ADC5 PGA gain", ANA_PGA1_CTRL, ADC1_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
331         /*0x91: Analog PGA2 Control Register*/
332         SOC_AC108_SINGLE_TLV("ADC6 PGA gain", ANA_PGA2_CTRL, ADC2_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
333         /*0x92: Analog PGA3 Control Register*/
334         SOC_AC108_SINGLE_TLV("ADC7 PGA gain", ANA_PGA3_CTRL, ADC3_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
335         /*0x93: Analog PGA4 Control Register*/
336         SOC_AC108_SINGLE_TLV("ADC8 PGA gain", ANA_PGA4_CTRL, ADC4_ANALOG_PGA, 0x1f, 0, 0, tlv_adc_pga_gain),
337 };
338
339
340 static const struct snd_soc_dapm_widget ac108_dapm_widgets[] = {
341         //input widgets
342         SND_SOC_DAPM_INPUT("MIC1P"),
343         SND_SOC_DAPM_INPUT("MIC1N"),
344
345         SND_SOC_DAPM_INPUT("MIC2P"),
346         SND_SOC_DAPM_INPUT("MIC2N"),
347
348         SND_SOC_DAPM_INPUT("MIC3P"),
349         SND_SOC_DAPM_INPUT("MIC3N"),
350
351         SND_SOC_DAPM_INPUT("MIC4P"),
352         SND_SOC_DAPM_INPUT("MIC4N"),
353
354         SND_SOC_DAPM_INPUT("DMIC1"),
355         SND_SOC_DAPM_INPUT("DMIC2"),
356
357         /*0xa0: ADC1 Analog Control 1 Register*/
358         /*0xa1-0xa6:use the defualt value*/
359         SND_SOC_DAPM_AIF_IN("Channel 1 AAF", "Capture", 0, ANA_ADC1_CTRL1, ADC1_DSM_ENABLE, 1),
360         SND_SOC_DAPM_SUPPLY("Channel 1 EN", ANA_ADC1_CTRL1, ADC1_PGA_ENABLE, 1, NULL, 0),
361         SND_SOC_DAPM_MICBIAS("MIC1BIAS", ANA_ADC1_CTRL1, ADC1_MICBIAS_EN, 1),
362
363         /*0xa7: ADC2 Analog Control 1 Register*/
364         /*0xa8-0xad:use the defualt value*/
365         SND_SOC_DAPM_AIF_IN("Channel 2 AAF", "Capture", 0, ANA_ADC2_CTRL1, ADC2_DSM_ENABLE, 1),
366         SND_SOC_DAPM_SUPPLY("Channel 2 EN", ANA_ADC2_CTRL1, ADC2_PGA_ENABLE, 1, NULL, 0),
367         SND_SOC_DAPM_MICBIAS("MIC2BIAS", ANA_ADC2_CTRL1, ADC2_MICBIAS_EN, 1),
368
369         /*0xae: ADC3 Analog Control 1 Register*/
370         /*0xaf-0xb4:use the defualt value*/
371         SND_SOC_DAPM_AIF_IN("Channel 3 AAF", "Capture", 0, ANA_ADC3_CTRL1, ADC3_DSM_ENABLE, 1),
372         SND_SOC_DAPM_SUPPLY("Channel 3 EN", ANA_ADC3_CTRL1, ADC3_PGA_ENABLE, 1, NULL, 0),
373         SND_SOC_DAPM_MICBIAS("MIC3BIAS", ANA_ADC3_CTRL1, ADC3_MICBIAS_EN, 1),
374
375         /*0xb5: ADC4 Analog Control 1 Register*/
376         /*0xb6-0xbb:use the defualt value*/
377         SND_SOC_DAPM_AIF_IN("Channel 4 AAF", "Capture", 0, ANA_ADC4_CTRL1, ADC4_DSM_ENABLE, 1),
378         SND_SOC_DAPM_SUPPLY("Channel 4 EN", ANA_ADC4_CTRL1, ADC4_PGA_ENABLE, 1, NULL, 0),
379         SND_SOC_DAPM_MICBIAS("MIC4BIAS", ANA_ADC4_CTRL1, ADC4_MICBIAS_EN, 1),
380
381
382         /*0x61: ADC Digital Part Enable Register*/
383         SND_SOC_DAPM_SUPPLY("ADC EN", ADC_DIG_EN, 4,  1, NULL, 0),
384         SND_SOC_DAPM_ADC("ADC1", "Capture", ADC_DIG_EN, 0,  1),
385         SND_SOC_DAPM_ADC("ADC2", "Capture", ADC_DIG_EN, 1,  1),
386         SND_SOC_DAPM_ADC("ADC3", "Capture", ADC_DIG_EN, 2,  1),
387         SND_SOC_DAPM_ADC("ADC4", "Capture", ADC_DIG_EN, 3,  1),
388
389         SND_SOC_DAPM_SUPPLY("ADC1 CLK", ANA_ADC4_CTRL7, ADC1_CLK_GATING, 1, NULL, 0),
390         SND_SOC_DAPM_SUPPLY("ADC2 CLK", ANA_ADC4_CTRL7, ADC2_CLK_GATING, 1, NULL, 0),
391         SND_SOC_DAPM_SUPPLY("ADC3 CLK", ANA_ADC4_CTRL7, ADC3_CLK_GATING, 1, NULL, 0),
392         SND_SOC_DAPM_SUPPLY("ADC4 CLK", ANA_ADC4_CTRL7, ADC4_CLK_GATING, 1, NULL, 0),
393
394         SND_SOC_DAPM_SUPPLY("DSM EN", ANA_ADC4_CTRL6, DSM_DEMOFF, 1, NULL, 0),
395
396         /*0x62:Digital MIC Enable Register*/
397         SND_SOC_DAPM_MICBIAS("DMIC1 enable", DMIC_EN, 0, 0),
398         SND_SOC_DAPM_MICBIAS("DMIC2 enable", DMIC_EN, 1, 0),
399 };
400
401 static const struct snd_soc_dapm_route ac108_dapm_routes[] = {
402
403         { "ADC1", NULL, "Channel 1 AAF" },
404         { "ADC2", NULL, "Channel 2 AAF" },
405         { "ADC3", NULL, "Channel 3 AAF" },
406         { "ADC4", NULL, "Channel 4 AAF" },
407
408         { "Channel 1 AAF", NULL, "MIC1BIAS" },
409         { "Channel 2 AAF", NULL, "MIC2BIAS" },
410         { "Channel 3 AAF", NULL, "MIC3BIAS" },
411         { "Channel 4 AAF", NULL, "MIC4BIAS" },
412
413         { "MIC1BIAS", NULL, "ADC1 CLK" },
414         { "MIC2BIAS", NULL, "ADC2 CLK" },
415         { "MIC3BIAS", NULL, "ADC3 CLK" },
416         { "MIC4BIAS", NULL, "ADC4 CLK" },
417
418
419         { "ADC1 CLK", NULL, "DSM EN" },
420         { "ADC2 CLK", NULL, "DSM EN" },
421         { "ADC3 CLK", NULL, "DSM EN" },
422         { "ADC4 CLK", NULL, "DSM EN" },
423
424
425         { "DSM EN", NULL, "ADC EN" },
426
427         { "Channel 1 EN", NULL, "DSM EN" },
428         { "Channel 2 EN", NULL, "DSM EN" },
429         { "Channel 3 EN", NULL, "DSM EN" },
430         { "Channel 4 EN", NULL, "DSM EN" },
431
432
433         { "MIC1P", NULL, "Channel 1 EN" },
434         { "MIC1N", NULL, "Channel 1 EN" },
435
436         { "MIC2P", NULL, "Channel 2 EN" },
437         { "MIC2N", NULL, "Channel 2 EN" },
438
439         { "MIC3P", NULL, "Channel 3 EN" },
440         { "MIC3N", NULL, "Channel 3 EN" },
441
442         { "MIC4P", NULL, "Channel 4 EN" },
443         { "MIC4N", NULL, "Channel 4 EN" },
444
445 };
446
447 static int ac108_multi_write(u8 reg, u8 val, struct ac10x_priv *ac10x) {
448         u8 i;
449         for (i = 0; i < ac10x->codec_cnt; i++) {
450                 ac10x_write(reg, val, ac10x->i2cmap[i]);
451         }
452         return 0;
453 }
454
455 static int ac108_multi_update_bits(u8 reg, u8 mask, u8 val, struct ac10x_priv *ac10x) {
456         int r = 0;
457         u8 i;
458
459         for (i = 0; i < ac10x->codec_cnt; i++) {
460                 r |= ac10x_update_bits(reg, mask, val, ac10x->i2cmap[i]);
461         }
462         return r;
463 }
464
465 static unsigned int ac108_codec_read(struct snd_soc_codec *codec, unsigned int reg) {
466         unsigned char val_r;
467         struct ac10x_priv *ac10x = dev_get_drvdata(codec->dev);
468         /*read one chip is fine*/
469         ac10x_read(reg, &val_r, ac10x->i2cmap[_MASTER_INDEX]);
470         return val_r;
471 }
472
473 static int ac108_codec_write(struct snd_soc_codec *codec, unsigned int reg, unsigned int val) {
474         struct ac10x_priv *ac10x = dev_get_drvdata(codec->dev);
475         ac108_multi_write(reg, val, ac10x);
476         return 0;
477 }
478
479 /**
480  * The Power management related registers are Reg01h~Reg09h
481  * 0x01-0x05,0x08,use the default value
482  * @author baozhu (17-6-21)
483  *
484  * @param ac10x
485  */
486 static void ac108_configure_power(struct ac10x_priv *ac10x) {
487         /**
488          * 0x06:Enable Analog LDO
489          */
490         ac108_multi_update_bits(PWR_CTRL6, 0x01 << LDO33ANA_ENABLE, 0x01 << LDO33ANA_ENABLE, ac10x);
491         /**
492          * 0x07:
493          * Control VREF output and micbias voltage ?
494          * REF faststart disable, enable Enable VREF (needed for Analog
495          * LDO and MICBIAS)
496          */
497         ac108_multi_update_bits(PWR_CTRL7, 0x1f << VREF_SEL | 0x01 << VREF_FASTSTART_ENABLE | 0x01 << VREF_ENABLE,
498                                            0x13 << VREF_SEL | 0x00 << VREF_FASTSTART_ENABLE | 0x01 << VREF_ENABLE, ac10x);
499         /**
500          * 0x09:
501          * Disable fast-start circuit on VREFP
502          * VREFP_RESCTRL=00=1 MOhm
503          * IGEN_TRIM=100=+25%
504          * Enable VREFP (needed by all audio input channels)
505          */
506         ac108_multi_update_bits(PWR_CTRL9, 0x01 << VREFP_FASTSTART_ENABLE | 0x03 << VREFP_RESCTRL | 0x07 << IGEN_TRIM | 0x01 << VREFP_ENABLE,
507                                            0x00 << VREFP_FASTSTART_ENABLE | 0x00 << VREFP_RESCTRL | 0x04 << IGEN_TRIM | 0x01 << VREFP_ENABLE,
508                                            ac10x);
509 }
510
511 /**
512  * The clock management related registers are Reg20h~Reg25h
513  * The PLL management related registers are Reg10h~Reg18h.
514  * @author baozhu (17-6-20)
515  *
516  * @param ac10x
517  * @param rate : sample rate
518  *
519  * @return int : fail or success
520  */
521 static int ac108_config_pll(struct ac10x_priv *ac10x, unsigned rate, unsigned lrck_ratio) {
522         unsigned int i = 0;
523         struct pll_div ac108_pll_div = { 0 };
524
525         if (ac10x->clk_id == SYSCLK_SRC_PLL) {
526                 unsigned pll_src, pll_freq_in;
527
528                 if (lrck_ratio == 0) {
529                         /* PLL clock source from MCLK */
530                         pll_freq_in = ac10x->sysclk;
531                         pll_src = 0x0;
532                 } else {
533                         /* PLL clock source from BCLK */
534                         pll_freq_in = rate * lrck_ratio;
535                         pll_src = 0x1;
536                 }
537
538                 /* FOUT =(FIN * N) / [(M1+1) * (M2+1)*(K1+1)*(K2+1)] */
539                 for (i = 0; i < ARRAY_SIZE(ac108_pll_div_list); i++) {
540                         if (ac108_pll_div_list[i].freq_in == pll_freq_in && ac108_pll_div_list[i].freq_out % rate == 0) {
541                                 ac108_pll_div = ac108_pll_div_list[i];
542                                 dev_dbg(&ac10x->i2c[_MASTER_INDEX]->dev, "AC108 PLL freq_in match:%u, freq_out:%u\n\n",
543                                                                 ac108_pll_div.freq_in, ac108_pll_div.freq_out);
544                                 break;
545                         }
546                 }
547                 /* 0x11,0x12,0x13,0x14: Config PLL DIV param M1/M2/N/K1/K2 */
548                 ac108_multi_update_bits(PLL_CTRL5, 0x1f << PLL_POSTDIV1 | 0x01 << PLL_POSTDIV2,
549                                                    ac108_pll_div.k1 << PLL_POSTDIV1 | ac108_pll_div.k2 << PLL_POSTDIV2, ac10x);
550                 ac108_multi_update_bits(PLL_CTRL4, 0xff << PLL_LOOPDIV_LSB, (unsigned char)ac108_pll_div.n << PLL_LOOPDIV_LSB, ac10x);
551                 ac108_multi_update_bits(PLL_CTRL3, 0x03 << PLL_LOOPDIV_MSB, (ac108_pll_div.n >> 8) << PLL_LOOPDIV_MSB, ac10x);
552                 ac108_multi_update_bits(PLL_CTRL2, 0x1f << PLL_PREDIV1 | 0x01 << PLL_PREDIV2,
553                                                     ac108_pll_div.m1 << PLL_PREDIV1 | ac108_pll_div.m2 << PLL_PREDIV2, ac10x);
554
555                 /*0x18: PLL clk lock enable*/
556                 ac108_multi_update_bits(PLL_LOCK_CTRL, 0x1 << PLL_LOCK_EN, 0x1 << PLL_LOCK_EN, ac10x);
557
558                 /**
559                  * 0x20: enable pll, pll source from mclk/bclk, sysclk source from pll, enable sysclk
560                  */
561                 ac108_multi_update_bits(SYSCLK_CTRL, 0x01 << PLLCLK_EN | 0x03  << PLLCLK_SRC | 0x01 << SYSCLK_SRC | 0x01 << SYSCLK_EN,
562                                                      0x01 << PLLCLK_EN |pll_src<< PLLCLK_SRC | 0x01 << SYSCLK_SRC | 0x01 << SYSCLK_EN, ac10x);
563                 ac10x->mclk = ac108_pll_div.freq_out;
564         }
565         if (ac10x->clk_id == SYSCLK_SRC_MCLK) {
566                 /**
567                  *0x20: sysclk source from mclk, enable sysclk
568                  */
569                 ac108_multi_update_bits(SYSCLK_CTRL, 0x01 << PLLCLK_EN | 0x01 << SYSCLK_SRC | 0x01 << SYSCLK_EN,
570                                                      0x00 << PLLCLK_EN | 0x00 << SYSCLK_SRC | 0x01 << SYSCLK_EN, ac10x);
571                 ac10x->mclk = ac10x->sysclk;
572         }
573
574         return 0;
575 }
576
577 /*
578  * support no more than 16 slots.
579  */
580 static int ac108_multi_chips_slots(struct ac10x_priv *ac, int slots) {
581         int i;
582
583         /*
584          * codec0 enable slots 2,3,0,1 when 1 codec
585          *
586          * codec0 enable slots 6,7,0,1 when 2 codec
587          * codec1 enable slots 2,3,4,5
588          *
589          * ...
590          */
591         for (i = 0; i < ac->codec_cnt; i++) {
592                 /* rotate map, due to channels rotated by CPU_DAI */
593                 const unsigned vec_mask[] = {
594                         0x3 << 6 | 0x3, // slots 6,7,0,1
595                         0xF << 2,       // slots 2,3,4,5
596                         0,
597                         0,
598                 };
599                 const unsigned vec_maps[] = {
600                         /*
601                          * chip 0,
602                          * mic 0 sample -> slot 6
603                          * mic 1 sample -> slot 7
604                          * mic 2 sample -> slot 0
605                          * mic 3 sample -> slot 1
606                          */
607                         0x0 << 12 | 0x1 << 14 | 0x2 << 0 | 0x3 << 2,
608                         /*
609                          * chip 1,
610                          * mic 0 sample -> slot 2
611                          * mic 1 sample -> slot 3
612                          * mic 2 sample -> slot 4
613                          * mic 3 sample -> slot 5
614                          */
615                         0x0 << 4  | 0x1 << 6  | 0x2 << 8 | 0x3 << 10,
616                         0,
617                         0,
618                 };
619                 unsigned vec;
620
621                 /* 0x38-0x3A I2S_TX1_CTRLx */
622                 if (ac->codec_cnt == 1) {
623                         vec = 0xFUL;
624                 } else {
625                         vec = vec_mask[i];
626                 }
627                 ac10x_write(I2S_TX1_CTRL1, slots - 1, ac->i2cmap[i]);
628                 ac10x_write(I2S_TX1_CTRL2, (vec >> 0) & 0xFF, ac->i2cmap[i]);
629                 ac10x_write(I2S_TX1_CTRL3, (vec >> 8) & 0xFF, ac->i2cmap[i]);
630
631                 /* 0x3C-0x3F I2S_TX1_CHMP_CTRLx */
632                 if (ac->codec_cnt == 1) {
633                         vec = (0x2 << 0 | 0x3 << 2 | 0x0 << 4 | 0x1 << 6);
634                 } else if (ac->codec_cnt == 2) {
635                         vec = vec_maps[i];
636                 }
637
638                 ac10x_write(I2S_TX1_CHMP_CTRL1, (vec >>  0) & 0xFF, ac->i2cmap[i]);
639                 ac10x_write(I2S_TX1_CHMP_CTRL2, (vec >>  8) & 0xFF, ac->i2cmap[i]);
640                 ac10x_write(I2S_TX1_CHMP_CTRL3, (vec >> 16) & 0xFF, ac->i2cmap[i]);
641                 ac10x_write(I2S_TX1_CHMP_CTRL4, (vec >> 24) & 0xFF, ac->i2cmap[i]);
642         }
643         return 0;
644 }
645
646 static int ac108_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) {
647         unsigned int i, channels, samp_res, rate;
648         struct snd_soc_codec *codec = dai->codec;
649         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
650         unsigned bclkdiv;
651         int ret = 0;
652         u8 v;
653
654         dev_dbg(dai->dev, "%s() stream=%s play:%d capt:%d +++\n", __func__,
655                         snd_pcm_stream_str(substream),
656                         dai->stream_active[SNDRV_PCM_STREAM_PLAYBACK],
657                         dai->stream_active[SNDRV_PCM_STREAM_CAPTURE]);
658
659         if (ac10x->i2c101) {
660                 ret = ac101_hw_params(substream, params, dai);
661                 if (ret > 0) {
662                         dev_dbg(dai->dev, "%s() L%d returned\n", __func__, __LINE__);
663                         /* not configure hw_param twice */
664                         return 0;
665                 }
666         }
667
668         if ((substream->stream == SNDRV_PCM_STREAM_CAPTURE &&
669             dai->stream_active[SNDRV_PCM_STREAM_PLAYBACK])
670             || (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
671             dai->stream_active[SNDRV_PCM_STREAM_CAPTURE])) {
672                 /* not configure hw_param twice */
673                 /* return 0; */
674         }
675
676         channels = params_channels(params);
677
678         /* Master mode, to clear cpu_dai fifos, output bclk without lrck */
679         ac10x_read(I2S_CTRL, &v, ac10x->i2cmap[_MASTER_INDEX]);
680         if (v & (0x01 << BCLK_IOEN)) {
681                 ac10x_update_bits(I2S_CTRL, 0x1 << LRCK_IOEN, 0x0 << LRCK_IOEN, ac10x->i2cmap[_MASTER_INDEX]);
682         }
683
684         switch (params_format(params)) {
685         case SNDRV_PCM_FORMAT_S8:
686                 samp_res = 0;
687                 break;
688         case SNDRV_PCM_FORMAT_S16_LE:
689                 samp_res = 2;
690                 break;
691         case SNDRV_PCM_FORMAT_S20_3LE:
692                 samp_res = 3;
693                 break;
694         case SNDRV_PCM_FORMAT_S24_LE:
695                 samp_res = 4;
696                 break;
697         case SNDRV_PCM_FORMAT_S32_LE:
698                 samp_res = 6;
699                 break;
700         default:
701                 pr_err("AC108 don't supported the sample resolution: %u\n", params_format(params));
702                 return -EINVAL;
703         }
704
705         for (i = 0; i < ARRAY_SIZE(ac108_sample_rate); i++) {
706                 if (ac108_sample_rate[i].real_val == params_rate(params) / (ac10x->data_protocol + 1UL)) {
707                         rate = i;
708                         break;
709                 }
710         }
711         if (i >= ARRAY_SIZE(ac108_sample_rate)) {
712                 return -EINVAL;
713         }
714
715         if (channels == 8 && ac108_sample_rate[rate].real_val == 96000) {
716                 /* 24.576M bit clock is not support by ac108 */
717                 return -EINVAL;
718         }
719
720         dev_dbg(dai->dev, "rate: %d , channels: %d , samp_res: %d",
721                         ac108_sample_rate[rate].real_val,
722                         channels,
723                         ac108_samp_res[samp_res].real_val);
724
725         /**
726          * 0x33:
727          *  The 8-Low bit of LRCK period value. It is used to program
728          *  the number of BCLKs per channel of sample frame. This value
729          *  is interpreted as follow:
730          *  The 8-Low bit of LRCK period value. It is used to program
731          *  the number of BCLKs per channel of sample frame. This value
732          *  is interpreted as follow: PCM mode: Number of BCLKs within
733          *  (Left + Right) channel width I2S / Left-Justified /
734          *  Right-Justified mode: Number of BCLKs within each individual
735          *  channel width (Left or Right) N+1
736          *  For example:
737          *  n = 7: 8 BCLK width
738          *  â€¦
739          *  n = 1023: 1024 BCLKs width
740          *  0X32[0:1]:
741          *  The 2-High bit of LRCK period value.
742          */
743         if (ac10x->i2s_mode != PCM_FORMAT) {
744                 if (ac10x->data_protocol) {
745                         ac108_multi_write(I2S_LRCK_CTRL2, ac108_samp_res[samp_res].real_val - 1, ac10x);
746                         /*encoding mode, the max LRCK period value < 32,so the 2-High bit is zero*/
747                         ac108_multi_update_bits(I2S_LRCK_CTRL1, 0x03 << 0, 0x00, ac10x);
748                 } else {
749                         /*TDM mode or normal mode*/
750                         ac108_multi_update_bits(I2S_LRCK_CTRL1, 0x03 << 0, 0x00, ac10x);
751                 }
752
753         } else {
754                 unsigned div;
755
756                 /*TDM mode or normal mode*/
757                 div = ac108_samp_res[samp_res].real_val * channels - 1;
758                 ac108_multi_write(I2S_LRCK_CTRL2, (div & 0xFF), ac10x);
759                 ac108_multi_update_bits(I2S_LRCK_CTRL1, 0x03 << 0, (div >> 8) << 0, ac10x);
760         }
761
762         /**
763          * 0x35:
764          * TX Encoding mode will add  4bits to mark channel number
765          * TODO: need a chat to explain this
766          */
767         ac108_multi_update_bits(I2S_FMT_CTRL2, 0x07 << SAMPLE_RESOLUTION | 0x07 << SLOT_WIDTH_SEL,
768                                                 ac108_samp_res[samp_res].reg_val << SAMPLE_RESOLUTION
769                                                   | ac108_samp_res[samp_res].reg_val << SLOT_WIDTH_SEL, ac10x);
770
771         /**
772          * 0x60:
773          * ADC Sample Rate synchronised with I2S1 clock zone
774          */
775         ac108_multi_update_bits(ADC_SPRC, 0x0f << ADC_FS_I2S1, ac108_sample_rate[rate].reg_val << ADC_FS_I2S1, ac10x);
776         ac108_multi_write(HPF_EN, 0x0F, ac10x);
777
778         if (ac10x->i2c101 && _MASTER_MULTI_CODEC == _MASTER_AC101) {
779                 ac108_config_pll(ac10x, ac108_sample_rate[rate].real_val, ac108_samp_res[samp_res].real_val * channels);
780         } else {
781                 ac108_config_pll(ac10x, ac108_sample_rate[rate].real_val, 0);
782         }
783
784         /*
785          * master mode only
786          */
787         bclkdiv = ac10x->mclk / (ac108_sample_rate[rate].real_val * channels * ac108_samp_res[samp_res].real_val);
788         for (i = 0; i < ARRAY_SIZE(ac108_bclkdivs) - 1; i++) {
789                 if (ac108_bclkdivs[i] >= bclkdiv) {
790                         break;
791                 }
792         }
793         ac108_multi_update_bits(I2S_BCLK_CTRL, 0x0F << BCLKDIV, i << BCLKDIV, ac10x);
794
795         /*
796          * slots allocation for each chip
797          */
798         ac108_multi_chips_slots(ac10x, channels);
799
800         /*0x21: Module clock enable<I2S, ADC digital, MIC offset Calibration, ADC analog>*/
801         ac108_multi_write(MOD_CLK_EN, 1 << I2S | 1 << ADC_DIGITAL | 1 << MIC_OFFSET_CALIBRATION | 1 << ADC_ANALOG, ac10x);
802         /*0x22: Module reset de-asserted<I2S, ADC digital, MIC offset Calibration, ADC analog>*/
803         ac108_multi_write(MOD_RST_CTRL, 1 << I2S | 1 << ADC_DIGITAL | 1 << MIC_OFFSET_CALIBRATION | 1 << ADC_ANALOG, ac10x);
804
805
806         dev_dbg(dai->dev, "%s() stream=%s ---\n", __func__,
807                         snd_pcm_stream_str(substream));
808
809         return 0;
810 }
811
812 static int ac108_set_sysclk(struct snd_soc_dai *dai, int clk_id, unsigned int freq, int dir) {
813
814         struct ac10x_priv *ac10x = snd_soc_dai_get_drvdata(dai);
815
816         freq = 24000000;
817         clk_id = SYSCLK_SRC_PLL;
818
819         pr_info("%s  :%d\n", __FUNCTION__, freq);
820
821         switch (clk_id) {
822         case SYSCLK_SRC_MCLK:
823                 ac108_multi_update_bits(SYSCLK_CTRL, 0x1 << SYSCLK_SRC, SYSCLK_SRC_MCLK << SYSCLK_SRC, ac10x);
824                 break;
825         case SYSCLK_SRC_PLL:
826                 ac108_multi_update_bits(SYSCLK_CTRL, 0x1 << SYSCLK_SRC, SYSCLK_SRC_PLL << SYSCLK_SRC, ac10x);
827                 break;
828         default:
829                 return -EINVAL;
830         }
831         ac10x->sysclk = freq;
832         ac10x->clk_id = clk_id;
833
834         return 0;
835 }
836
837 /**
838  *  The i2s format management related registers are Reg
839  *  30h~Reg36h
840  *  33h,35h will be set in ac108_hw_params, It's BCLK width and
841  *  Sample Resolution.
842  * @author baozhu (17-6-20)
843  *
844  * @param dai
845  * @param fmt
846  *
847  * @return int
848  */
849 static int ac108_set_fmt(struct snd_soc_dai *dai, unsigned int fmt) {
850         unsigned char tx_offset, lrck_polarity, brck_polarity;
851         struct ac10x_priv *ac10x = dev_get_drvdata(dai->dev);
852
853         dev_dbg(dai->dev, "%s\n", __FUNCTION__);
854
855         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
856         case SND_SOC_DAIFMT_CBM_CFM:    /*AC108 Master*/
857                 if (! ac10x->i2c101 || _MASTER_MULTI_CODEC == _MASTER_AC108) {
858                         dev_dbg(dai->dev, "AC108 set to work as Master\n");
859                         /**
860                          * 0x30:chip is master mode ,BCLK & LRCK output
861                          */
862                         ac108_multi_update_bits(I2S_CTRL, 0x03 << LRCK_IOEN | 0x03 << SDO1_EN | 0x1 << TXEN | 0x1 << GEN,
863                                                           0x00 << LRCK_IOEN | 0x03 << SDO1_EN | 0x1 << TXEN | 0x1 << GEN, ac10x);
864                         /* multi_chips: only one chip set as Master, and the others also need to set as Slave */
865                         ac10x_update_bits(I2S_CTRL, 0x3 << LRCK_IOEN, 0x01 << BCLK_IOEN, ac10x->i2cmap[_MASTER_INDEX]);
866                         break;
867                 } else {
868                         /* TODO: Both cpu_dai and codec_dai(AC108) be set as slave in DTS */
869                         dev_dbg(dai->dev, "used as slave when AC101 is master\n");
870                 }
871                 /* fall through */
872         case SND_SOC_DAIFMT_CBS_CFS:    /*AC108 Slave*/
873                 dev_dbg(dai->dev, "AC108 set to work as Slave\n");
874                 /**
875                  * 0x30:chip is slave mode, BCLK & LRCK input,enable SDO1_EN and
876                  *  SDO2_EN, Transmitter Block Enable, Globe Enable
877                  */
878                 ac108_multi_update_bits(I2S_CTRL, 0x03 << LRCK_IOEN | 0x03 << SDO1_EN | 0x1 << TXEN | 0x1 << GEN,
879                                                   0x00 << LRCK_IOEN | 0x03 << SDO1_EN | 0x0 << TXEN | 0x0 << GEN, ac10x);
880                 break;
881         default:
882                 pr_err("AC108 Master/Slave mode config error:%u\n\n", (fmt & SND_SOC_DAIFMT_MASTER_MASK) >> 12);
883                 return -EINVAL;
884         }
885
886         /*AC108 config I2S/LJ/RJ/PCM format*/
887         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
888         case SND_SOC_DAIFMT_I2S:
889                 dev_dbg(dai->dev, "AC108 config I2S format\n");
890                 ac10x->i2s_mode = LEFT_JUSTIFIED_FORMAT;
891                 tx_offset = 1;
892                 break;
893         case SND_SOC_DAIFMT_RIGHT_J:
894                 dev_dbg(dai->dev, "AC108 config RIGHT-JUSTIFIED format\n");
895                 ac10x->i2s_mode = RIGHT_JUSTIFIED_FORMAT;
896                 tx_offset = 0;
897                 break;
898         case SND_SOC_DAIFMT_LEFT_J:
899                 dev_dbg(dai->dev, "AC108 config LEFT-JUSTIFIED format\n");
900                 ac10x->i2s_mode = LEFT_JUSTIFIED_FORMAT;
901                 tx_offset = 0;
902                 break;
903         case SND_SOC_DAIFMT_DSP_A:
904                 dev_dbg(dai->dev, "AC108 config PCM-A format\n");
905                 ac10x->i2s_mode = PCM_FORMAT;
906                 tx_offset = 1;
907                 break;
908         case SND_SOC_DAIFMT_DSP_B:
909                 dev_dbg(dai->dev, "AC108 config PCM-B format\n");
910                 ac10x->i2s_mode = PCM_FORMAT;
911                 tx_offset = 0;
912                 break;
913         default:
914                 pr_err("AC108 I2S format config error:%u\n\n", fmt & SND_SOC_DAIFMT_FORMAT_MASK);
915                 return -EINVAL;
916         }
917
918         /*AC108 config BCLK&LRCK polarity*/
919         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
920         case SND_SOC_DAIFMT_NB_NF:
921                 dev_dbg(dai->dev, "AC108 config BCLK&LRCK polarity: BCLK_normal,LRCK_normal\n");
922                 brck_polarity = BCLK_NORMAL_DRIVE_N_SAMPLE_P;
923                 lrck_polarity = LRCK_LEFT_HIGH_RIGHT_LOW;
924                 break;
925         case SND_SOC_DAIFMT_NB_IF:
926                 dev_dbg(dai->dev, "AC108 config BCLK&LRCK polarity: BCLK_normal,LRCK_invert\n");
927                 brck_polarity = BCLK_NORMAL_DRIVE_N_SAMPLE_P;
928                 lrck_polarity = LRCK_LEFT_LOW_RIGHT_HIGH;
929                 break;
930         case SND_SOC_DAIFMT_IB_NF:
931                 dev_dbg(dai->dev, "AC108 config BCLK&LRCK polarity: BCLK_invert,LRCK_normal\n");
932                 brck_polarity = BCLK_INVERT_DRIVE_P_SAMPLE_N;
933                 lrck_polarity = LRCK_LEFT_HIGH_RIGHT_LOW;
934                 break;
935         case SND_SOC_DAIFMT_IB_IF:
936                 dev_dbg(dai->dev, "AC108 config BCLK&LRCK polarity: BCLK_invert,LRCK_invert\n");
937                 brck_polarity = BCLK_INVERT_DRIVE_P_SAMPLE_N;
938                 lrck_polarity = LRCK_LEFT_LOW_RIGHT_HIGH;
939                 break;
940         default:
941                 pr_err("AC108 config BCLK/LRCLK polarity error:%u\n\n", (fmt & SND_SOC_DAIFMT_INV_MASK) >> 8);
942                 return -EINVAL;
943         }
944
945         ac108_configure_power(ac10x);
946
947         /**
948          *0x31: 0: normal mode, negative edge drive and positive edge sample
949                 1: invert mode, positive edge drive and negative edge sample
950          */
951         ac108_multi_update_bits(I2S_BCLK_CTRL,  0x01 << BCLK_POLARITY, brck_polarity << BCLK_POLARITY, ac10x);
952         /**
953          * 0x32: same as 0x31
954          */
955         ac108_multi_update_bits(I2S_LRCK_CTRL1, 0x01 << LRCK_POLARITY, lrck_polarity << LRCK_POLARITY, ac10x);
956         /**
957          * 0x34:Encoding Mode Selection,Mode
958          * Selection,data is offset by 1 BCLKs to LRCK
959          * normal mode for the last half cycle of BCLK in the slot ?
960          * turn to hi-z state (TDM) when not transferring slot ?
961          */
962         ac108_multi_update_bits(I2S_FMT_CTRL1,  0x01 << ENCD_SEL | 0x03 << MODE_SEL | 0x01 << TX2_OFFSET |
963                                                 0x01 << TX1_OFFSET | 0x01 << TX_SLOT_HIZ | 0x01 << TX_STATE,
964                                                                   ac10x->data_protocol << ENCD_SEL      |
965                                                                   ac10x->i2s_mode << MODE_SEL           |
966                                                                   tx_offset << TX2_OFFSET                       |
967                                                                   tx_offset << TX1_OFFSET                       |
968                                                                   0x00 << TX_SLOT_HIZ                           |
969                                                                   0x01 << TX_STATE, ac10x);
970
971         /**
972          * 0x60:
973          * MSB / LSB First Select: This driver only support MSB First Select .
974          * OUT2_MUTE,OUT1_MUTE shoule be set in widget.
975          * LRCK = 1 BCLK width
976          * Linear PCM
977          *
978          * TODO:pcm mode, bit[0:1] and bit[2] is special
979          */
980         ac108_multi_update_bits(I2S_FMT_CTRL3,  0x01 << TX_MLS | 0x03 << SEXT  | 0x01 << LRCK_WIDTH | 0x03 << TX_PDM,
981                                                 0x00 << TX_MLS | 0x03 << SEXT  | 0x00 << LRCK_WIDTH | 0x00 << TX_PDM, ac10x);
982
983         ac108_multi_write(HPF_EN, 0x00, ac10x);
984
985         if (ac10x->i2c101) {
986                 return ac101_set_dai_fmt(dai, fmt);
987         }
988         return 0;
989 }
990
991 /*
992  * due to miss channels order in cpu_dai, we meed defer the clock starting.
993  */
994 static int ac108_set_clock(int y_start_n_stop) {
995         u8 reg;
996         int ret = 0;
997
998         dev_dbg(ac10x->codec->dev, "%s() L%d cmd:%d\n", __func__, __LINE__, y_start_n_stop);
999
1000         /* spin_lock move to machine trigger */
1001
1002         if (y_start_n_stop && ac10x->sysclk_en == 0) {
1003                 /* enable lrck clock */
1004                 ac10x_read(I2S_CTRL, &reg, ac10x->i2cmap[_MASTER_INDEX]);
1005                 if (reg & (0x01 << BCLK_IOEN)) {
1006                         ret = ret || ac10x_update_bits(I2S_CTRL, 0x03 << LRCK_IOEN, 0x03 << LRCK_IOEN, ac10x->i2cmap[_MASTER_INDEX]);
1007                 }
1008
1009                 /*0x10: PLL Common voltage enable, PLL enable */
1010                 ret = ret || ac108_multi_update_bits(PLL_CTRL1, 0x01 << PLL_EN | 0x01 << PLL_COM_EN,
1011                                                    0x01 << PLL_EN | 0x01 << PLL_COM_EN, ac10x);
1012                 /* enable global clock */
1013                 ret = ret || ac108_multi_update_bits(I2S_CTRL, 0x1 << TXEN | 0x1 << GEN, 0x1 << TXEN | 0x1 << GEN, ac10x);
1014
1015                 ac10x->sysclk_en = 1UL;
1016         } else if (!y_start_n_stop && ac10x->sysclk_en != 0) {
1017                 /* disable global clock */
1018                 ret = ret || ac108_multi_update_bits(I2S_CTRL, 0x1 << TXEN | 0x1 << GEN, 0x0 << TXEN | 0x0 << GEN, ac10x);
1019
1020                 /*0x10: PLL Common voltage disable, PLL disable */
1021                 ret = ret || ac108_multi_update_bits(PLL_CTRL1, 0x01 << PLL_EN | 0x01 << PLL_COM_EN,
1022                                                    0x00 << PLL_EN | 0x00 << PLL_COM_EN, ac10x);
1023
1024                 /* disable lrck clock if it's enabled */
1025                 ac10x_read(I2S_CTRL, &reg, ac10x->i2cmap[_MASTER_INDEX]);
1026                 if (reg & (0x01 << LRCK_IOEN)) {
1027                         ret = ret || ac10x_update_bits(I2S_CTRL, 0x03 << LRCK_IOEN, 0x01 << BCLK_IOEN, ac10x->i2cmap[_MASTER_INDEX]);
1028                 }
1029                 if (!ret) {
1030                         ac10x->sysclk_en = 0UL;
1031                 }
1032         }
1033
1034         return ret;
1035 }
1036
1037 static int ac108_prepare(struct snd_pcm_substream *substream,
1038                                         struct snd_soc_dai *dai)
1039 {
1040         struct snd_soc_codec *codec = dai->codec;
1041         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1042         u8 r;
1043
1044         dev_dbg(dai->dev, "%s() stream=%s\n",
1045                 __func__,
1046                 snd_pcm_stream_str(substream));
1047
1048         /* disable global clock if lrck disabled */
1049         ac10x_read(I2S_CTRL, &r, ac10x->i2cmap[_MASTER_INDEX]);
1050         if ((r & (0x01 << BCLK_IOEN)) && (r & (0x01 << LRCK_IOEN)) == 0) {
1051                 dev_err(dai->dev, "%s() jcsing ac108 multi update\n", __FUNCTION__);
1052                 /* disable global clock */
1053                 ac108_multi_update_bits(I2S_CTRL, 0x1 << TXEN | 0x1 << GEN, 0x0 << TXEN | 0x0 << GEN, ac10x);
1054         }
1055
1056         return 0;
1057 }
1058
1059 int ac108_audio_startup(struct snd_pcm_substream *substream,
1060         struct snd_soc_dai *dai
1061 ) {
1062         struct snd_soc_codec *codec = dai->codec;
1063         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1064
1065         if (ac10x->i2c101) {
1066                 return ac101_audio_startup(substream, dai);
1067         }
1068         return 0;
1069 }
1070
1071 void ac108_aif_shutdown(struct snd_pcm_substream *substream,
1072         struct snd_soc_dai *dai
1073 ) {
1074         struct snd_soc_codec *codec = dai->codec;
1075         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1076
1077         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
1078                 /*0x21: Module clock disable <I2S, ADC digital, MIC offset Calibration, ADC analog>*/
1079                 ac108_multi_write(MOD_CLK_EN, 0x0, ac10x);
1080                 /*0x22: Module reset asserted <I2S, ADC digital, MIC offset Calibration, ADC analog>*/
1081                 ac108_multi_write(MOD_RST_CTRL, 0x0, ac10x);
1082         }
1083
1084         if (ac10x->i2c101) {
1085                 ac101_aif_shutdown(substream, dai);
1086         }
1087 }
1088
1089 int ac108_aif_mute(struct snd_soc_dai *dai, int mute, int direction) {
1090         struct snd_soc_codec *codec = dai->codec;
1091         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1092
1093         if (ac10x->i2c101) {
1094                 return ac101_aif_mute(dai, mute);
1095         }
1096         return 0;
1097 }
1098
1099 static const struct snd_soc_dai_ops ac108_dai_ops = {
1100         .startup        = ac108_audio_startup,
1101         .shutdown       = ac108_aif_shutdown,
1102
1103         /*DAI clocking configuration*/
1104         .set_sysclk     = ac108_set_sysclk,
1105
1106         /*ALSA PCM audio operations*/
1107         .hw_params      = ac108_hw_params,
1108         .prepare        = ac108_prepare,
1109         .mute_stream    = ac108_aif_mute,
1110
1111         /*DAI format configuration*/
1112         .set_fmt        = ac108_set_fmt,
1113
1114         // .hw_free = ac108_hw_free,
1115 };
1116
1117 static  struct snd_soc_dai_driver ac108_dai0 = {
1118         .name = "ac10x-codec0",
1119         #if _USE_CAPTURE
1120         .playback = {
1121                 .stream_name = "Playback",
1122                 .channels_min = 1,
1123                 .channels_max = AC108_CHANNELS_MAX,
1124                 .rates = AC108_RATES,
1125                 .formats = AC108_FORMATS,
1126         },
1127         #endif
1128         .capture = {
1129                 .stream_name = "Capture",
1130                 .channels_min = 1,
1131                 .channels_max = AC108_CHANNELS_MAX,
1132                 .rates = AC108_RATES,
1133                 .formats = AC108_FORMATS,
1134         },
1135         .ops = &ac108_dai_ops,
1136 };
1137
1138 static  struct snd_soc_dai_driver ac108_dai1 = {
1139         .name = "ac10x-codec1",
1140         #if _USE_CAPTURE
1141         .playback = {
1142                 .stream_name = "Playback",
1143                 .channels_min = 1,
1144                 .channels_max = AC108_CHANNELS_MAX,
1145                 .rates = AC108_RATES,
1146                 .formats = AC108_FORMATS,
1147         },
1148         #endif
1149         .capture = {
1150                 .stream_name = "Capture",
1151                 .channels_min = 1,
1152                 .channels_max = AC108_CHANNELS_MAX,
1153                 .rates = AC108_RATES,
1154                 .formats = AC108_FORMATS,
1155         },
1156         .ops = &ac108_dai_ops,
1157 };
1158
1159 static  struct snd_soc_dai_driver *ac108_dai[] = {
1160         &ac108_dai0,
1161         &ac108_dai1,
1162 };
1163
1164 static int ac108_add_widgets(struct snd_soc_codec *codec) {
1165         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1166         struct snd_soc_dapm_context *dapm = snd_soc_codec_get_dapm(codec);
1167         const struct snd_kcontrol_new* snd_kcntl = ac108_snd_controls;
1168         int ctrl_cnt = ARRAY_SIZE(ac108_snd_controls);
1169
1170         /* only register controls correspond to exist chips */
1171         if (ac10x->tdm_chips_cnt >= 2) {
1172                 snd_kcntl = ac108tdm_snd_controls;
1173                 ctrl_cnt = ARRAY_SIZE(ac108tdm_snd_controls);
1174         }
1175         snd_soc_add_codec_controls(codec, snd_kcntl, ctrl_cnt);
1176
1177         snd_soc_dapm_new_controls(dapm, ac108_dapm_widgets,ARRAY_SIZE(ac108_dapm_widgets));
1178         snd_soc_dapm_add_routes(dapm, ac108_dapm_routes, ARRAY_SIZE(ac108_dapm_routes));
1179
1180         return 0;
1181 }
1182
1183 static int ac108_codec_probe(struct snd_soc_codec *codec) {
1184         spin_lock_init(&ac10x->lock);
1185
1186         ac10x->codec = codec;
1187         dev_set_drvdata(codec->dev, ac10x);
1188         ac108_add_widgets(codec);
1189
1190         if (ac10x->i2c101) {
1191                 ac101_codec_probe(codec);
1192         }
1193
1194         return 0;
1195 }
1196
1197 static int ac108_set_bias_level(struct snd_soc_codec *codec, enum snd_soc_bias_level level) {
1198         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1199
1200         dev_dbg(codec->dev, "AC108 level:%d\n", level);
1201
1202         switch (level) {
1203         case SND_SOC_BIAS_ON:
1204                 ac108_multi_update_bits(ANA_ADC1_CTRL1, 0x01 << ADC1_MICBIAS_EN,  0x01 << ADC1_MICBIAS_EN, ac10x);
1205                 ac108_multi_update_bits(ANA_ADC2_CTRL1, 0x01 << ADC2_MICBIAS_EN,  0x01 << ADC2_MICBIAS_EN, ac10x);
1206                 ac108_multi_update_bits(ANA_ADC3_CTRL1, 0x01 << ADC3_MICBIAS_EN,  0x01 << ADC3_MICBIAS_EN, ac10x);
1207                 ac108_multi_update_bits(ANA_ADC4_CTRL1, 0x01 << ADC4_MICBIAS_EN,  0x01 << ADC4_MICBIAS_EN, ac10x);
1208                 break;
1209
1210         case SND_SOC_BIAS_PREPARE:
1211                 /* Put the MICBIASes into regulating mode */
1212                 break;
1213
1214         case SND_SOC_BIAS_STANDBY:
1215                 break;
1216
1217         case SND_SOC_BIAS_OFF:
1218                 ac108_multi_update_bits(ANA_ADC1_CTRL1, 0x01 << ADC1_MICBIAS_EN,  0x00 << ADC1_MICBIAS_EN, ac10x);
1219                 ac108_multi_update_bits(ANA_ADC2_CTRL1, 0x01 << ADC2_MICBIAS_EN,  0x00 << ADC2_MICBIAS_EN, ac10x);
1220                 ac108_multi_update_bits(ANA_ADC3_CTRL1, 0x01 << ADC3_MICBIAS_EN,  0x00 << ADC3_MICBIAS_EN, ac10x);
1221                 ac108_multi_update_bits(ANA_ADC4_CTRL1, 0x01 << ADC4_MICBIAS_EN,  0x00 << ADC4_MICBIAS_EN, ac10x);
1222                 break;
1223         }
1224
1225         if (ac10x->i2c101) {
1226                 ac101_set_bias_level(codec, level);
1227         }
1228         return 0;
1229 }
1230
1231 int ac108_codec_remove(struct snd_soc_codec *codec) {
1232         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1233
1234         if (! ac10x->i2c101) {
1235                 return 0;
1236         }
1237         return ac101_codec_remove(codec);
1238 }
1239 #if __NO_SND_SOC_CODEC_DRV
1240 void ac108_codec_remove_void(struct snd_soc_codec *codec) {
1241         ac108_codec_remove(codec);
1242 }
1243 #define ac108_codec_remove ac108_codec_remove_void
1244 #endif
1245
1246 int ac108_codec_suspend(struct snd_soc_codec *codec) {
1247         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1248         int i;
1249
1250         for (i = 0; i < ac10x->codec_cnt; i++) {
1251                 regcache_cache_only(ac10x->i2cmap[i], true);
1252         }
1253
1254         if (! ac10x->i2c101) {
1255                 return 0;
1256         }
1257         return ac101_codec_suspend(codec);
1258 }
1259
1260 int ac108_codec_resume(struct snd_soc_codec *codec) {
1261         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1262         int i, ret;
1263
1264         /* Sync reg_cache with the hardware */
1265         for (i = 0; i < ac10x->codec_cnt; i++) {
1266                 regcache_cache_only(ac10x->i2cmap[i], false);
1267                 ret = regcache_sync(ac10x->i2cmap[i]);
1268                 if (ret != 0) {
1269                         dev_err(codec->dev, "Failed to sync i2cmap%d register cache: %d\n", i, ret);
1270                         regcache_cache_only(ac10x->i2cmap[i], true);
1271                 }
1272         }
1273
1274         if (! ac10x->i2c101) {
1275                 return 0;
1276         }
1277         return ac101_codec_resume(codec);
1278 }
1279
1280 static struct snd_soc_codec_driver ac10x_soc_codec_driver = {
1281         .probe          = ac108_codec_probe,
1282         .remove         = ac108_codec_remove,
1283         .suspend        = ac108_codec_suspend,
1284         .resume         = ac108_codec_resume,
1285         .set_bias_level = ac108_set_bias_level,
1286         .read           = ac108_codec_read,
1287         .write          = ac108_codec_write,
1288 };
1289
1290 static ssize_t ac108_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count) {
1291         int val = 0, flag = 0;
1292         u8 i = 0, reg, num, value_w, value_r[4];
1293
1294         val = simple_strtol(buf, NULL, 16);
1295         flag = (val >> 16) & 0xF;
1296
1297         if (flag) {
1298                 reg = (val >> 8) & 0xFF;
1299                 value_w = val & 0xFF;
1300                 ac108_multi_write(reg, value_w, ac10x);
1301                 printk("Write 0x%02x to REG:0x%02x\n", value_w, reg);
1302         } else {
1303                 int k;
1304
1305                 reg = (val >> 8) & 0xFF;
1306                 num = val & 0xff;
1307                 printk("\nRead: start REG:0x%02x,count:0x%02x\n", reg, num);
1308
1309                 for (k = 0; k < ac10x->codec_cnt; k++) {
1310                         regcache_cache_bypass(ac10x->i2cmap[k], true);
1311                 }
1312                 do {
1313
1314                         memset(value_r, 0, sizeof value_r);
1315
1316                         for (k = 0; k < ac10x->codec_cnt; k++) {
1317                                 ac10x_read(reg, &value_r[k], ac10x->i2cmap[k]);
1318                         }
1319                         if (ac10x->codec_cnt >= 2) {
1320                                 printk("REG[0x%02x]: 0x%02x 0x%02x", reg, value_r[0], value_r[1]);
1321                         } else {
1322                                 printk("REG[0x%02x]: 0x%02x", reg, value_r[0]);
1323                         }
1324                         reg++;
1325
1326                         if ((++i == num) || (i % 4 == 0)) {
1327                                 printk("\n");
1328                         }
1329                 } while (i < num);
1330                 for (k = 0; k < ac10x->codec_cnt; k++) {
1331                         regcache_cache_bypass(ac10x->i2cmap[k], false);
1332                 }
1333         }
1334
1335         return count;
1336 }
1337
1338 static ssize_t ac108_show(struct device *dev, struct device_attribute *attr, char *buf) {
1339 #if 1
1340         printk("echo flag|reg|val > ac108\n");
1341         printk("eg read star addres=0x06,count 0x10:echo 0610 >ac108\n");
1342         printk("eg write value:0xfe to address:0x06 :echo 106fe > ac108\n");
1343         return 0;
1344 #else
1345         return snprintf(buf, PAGE_SIZE,"echo flag|reg|val > ac108\n"
1346                                         "eg read star addres=0x06,count 0x10:echo 0610 >ac108\n"
1347                                         "eg write value:0xfe to address:0x06 :echo 106fe > ac108\n");
1348 #endif
1349 }
1350
1351 static DEVICE_ATTR(ac108, 0644, ac108_show, ac108_store);
1352 static struct attribute *ac108_debug_attrs[] = {
1353         &dev_attr_ac108.attr,
1354         NULL,
1355 };
1356 static struct attribute_group ac108_debug_attr_group = {
1357         .name   = "ac108_debug",
1358         .attrs  = ac108_debug_attrs,
1359 };
1360
1361 static const struct regmap_config ac108_regmap = {
1362         .reg_bits = 8,
1363         .val_bits = 8,
1364         .reg_stride = 1,
1365         .max_register = 0xDF,
1366         .cache_type = REGCACHE_FLAT,
1367 };
1368 static int ac108_i2c_probe(struct i2c_client *i2c, const struct i2c_device_id *i2c_id) {
1369         struct device_node *np = i2c->dev.of_node;
1370         unsigned int val = 0;
1371         int ret = 0, index;
1372
1373         if (ac10x == NULL) {
1374                 ac10x = kzalloc(sizeof(struct ac10x_priv), GFP_KERNEL);
1375                 if (ac10x == NULL) {
1376                         dev_err(&i2c->dev, "Unable to allocate ac10x private data\n");
1377                         return -ENOMEM;
1378                 }
1379         }
1380
1381         index = (int)i2c_id->driver_data;
1382         if (index == AC101_I2C_ID) {
1383                 ac10x->i2c101 = i2c;
1384                 i2c_set_clientdata(i2c, ac10x);
1385                 ret = ac101_probe(i2c, i2c_id);
1386                 if (ret) {
1387                         ac10x->i2c101 = NULL;
1388                         return ret;
1389                 }
1390                 goto __ret;
1391         }
1392
1393         ret = of_property_read_u32(np, "data-protocol", &val);
1394         if (ret) {
1395                 pr_err("Please set data-protocol.\n");
1396                 return -EINVAL;
1397         }
1398         ac10x->data_protocol = val;
1399
1400         if (of_property_read_u32(np, "tdm-chips-count", &val)) val = 1;
1401         ac10x->tdm_chips_cnt = val;
1402
1403         pr_err(" ac10x i2c_id number: %d\n", index);
1404         pr_err(" ac10x data protocol: %d\n", ac10x->data_protocol);
1405
1406         ac10x->i2c[index] = i2c;
1407         ac10x->i2cmap[index] = devm_regmap_init_i2c(i2c, &ac108_regmap);
1408         if (IS_ERR(ac10x->i2cmap[index])) {
1409                 ret = PTR_ERR(ac10x->i2cmap[index]);
1410                 dev_err(&i2c->dev, "Fail to initialize i2cmap%d I/O: %d\n", index, ret);
1411                 return ret;
1412         }
1413
1414         /*
1415          * Writing this register with 0x12
1416          * will resets all register to their default state.
1417          */
1418         regcache_cache_only(ac10x->i2cmap[index], false);
1419         ret = regmap_write(ac10x->i2cmap[index], CHIP_RST, CHIP_RST_VAL);
1420         msleep(1);
1421
1422         if (regmap_write(ac10x->i2cmap[index], ADC1_DVOL_CTRL, 0xff)) {
1423                 dev_err(&i2c->dev, "fail to write adc1 volume register\n");
1424                 return -ENODEV;
1425         }
1426
1427         if (regmap_write(ac10x->i2cmap[index], ADC2_DVOL_CTRL, 0xff)) {
1428                 dev_err(&i2c->dev, "fail to write adc2 volume register\n");
1429                 return -ENODEV;
1430         }
1431
1432         if (regmap_write(ac10x->i2cmap[index], ADC3_DVOL_CTRL, 0xff)) {
1433                 dev_err(&i2c->dev, "fail to write adc3 volume register\n");
1434                 return -ENODEV;
1435         }
1436
1437         if (regmap_write(ac10x->i2cmap[index], ADC4_DVOL_CTRL, 0xff)) {
1438                 dev_err(&i2c->dev, "fail to write adc4 volume register\n");
1439                 return -ENODEV;
1440         }
1441
1442         /* sync regcache for FLAT type */
1443         ac10x_fill_regcache(&i2c->dev, ac10x->i2cmap[index]);
1444
1445         ac10x->codec_cnt++;
1446         pr_err(" ac10x codec count  : %d\n", ac10x->codec_cnt);
1447
1448         ret = sysfs_create_group(&i2c->dev.kobj, &ac108_debug_attr_group);
1449         if (ret) {
1450                 pr_err("failed to create attr group\n");
1451         }
1452
1453 __ret:
1454         /* It's time to bind codec to i2c[_MASTER_INDEX] when all i2c are ready */
1455         if ((ac10x->codec_cnt != 0 && ac10x->tdm_chips_cnt < 2)
1456         || (ac10x->i2c[0] && ac10x->i2c[1] && ac10x->i2c101)) {
1457                 seeed_voice_card_register_set_clock(SNDRV_PCM_STREAM_CAPTURE, ac108_set_clock);
1458                 /* no playback stream */
1459                 if (! ac10x->i2c101) {
1460                         memset(&ac108_dai[_MASTER_INDEX]->playback, '\0', sizeof ac108_dai[_MASTER_INDEX]->playback);
1461                 }
1462                 ret = snd_soc_register_codec(&ac10x->i2c[_MASTER_INDEX]->dev, &ac10x_soc_codec_driver,
1463                                                 ac108_dai[_MASTER_INDEX], 1);
1464                 if (ret < 0) {
1465                         dev_err(&i2c->dev, "Failed to register ac10x codec: %d\n", ret);
1466                 }
1467         }
1468         return ret;
1469 }
1470
1471 static int ac108_i2c_remove(struct i2c_client *i2c) {
1472         if (ac10x->codec != NULL) {
1473                 snd_soc_unregister_codec(&ac10x->i2c[_MASTER_INDEX]->dev);
1474                 ac10x->codec = NULL;
1475         }
1476         if (i2c == ac10x->i2c101) {
1477                 ac101_remove(ac10x->i2c101);
1478                 ac10x->i2c101 = NULL;
1479                 goto __ret;
1480         }
1481
1482         if (i2c == ac10x->i2c[0]) {
1483                 ac10x->i2c[0] = NULL;
1484         }
1485         if (i2c == ac10x->i2c[1]) {
1486                 ac10x->i2c[1] = NULL;
1487         }
1488
1489         sysfs_remove_group(&i2c->dev.kobj, &ac108_debug_attr_group);
1490
1491 __ret:
1492         if (!ac10x->i2c[0] && !ac10x->i2c[1] && !ac10x->i2c101) {
1493                 kfree(ac10x);
1494                 ac10x = NULL;
1495         }
1496         return 0;
1497 }
1498
1499 static const struct i2c_device_id ac108_i2c_id[] = {
1500         { "ac108_0", 0 },
1501         { "ac108_1", 1 },
1502         { "ac108_2", 2 },
1503         { "ac108_3", 3 },
1504         { "ac101", AC101_I2C_ID },
1505         { }
1506 };
1507 MODULE_DEVICE_TABLE(i2c, ac108_i2c_id);
1508
1509 static const struct of_device_id ac108_of_match[] = {
1510         { .compatible = "x-power,ac108_0", },
1511         { .compatible = "x-power,ac108_1", },
1512         { .compatible = "x-power,ac108_2", },
1513         { .compatible = "x-power,ac108_3", },
1514         { .compatible = "x-power,ac101",   },
1515         { }
1516 };
1517 MODULE_DEVICE_TABLE(of, ac108_of_match);
1518
1519 static struct i2c_driver ac108_i2c_driver = {
1520         .driver = {
1521                 .name = "ac10x-codec",
1522                 .of_match_table = ac108_of_match,
1523         },
1524         .probe =    ac108_i2c_probe,
1525         .remove =   ac108_i2c_remove,
1526         .id_table = ac108_i2c_id,
1527 };
1528
1529 module_i2c_driver(ac108_i2c_driver);
1530
1531 MODULE_DESCRIPTION("ASoC AC108 driver");
1532 MODULE_AUTHOR("Baozhu Zuo<zuobaozhu@gmail.com>");
1533 MODULE_LICENSE("GPL");