rk3368 codec : add support for codec es8316
[firefly-linux-kernel-4.4.55.git] / sound / soc / codecs / es8316.c
1 /*
2  * es8316.c -- es8316 ALSA SoC audio driver
3  * Copyright Everest Semiconductor Co.,Ltd
4  *
5  * Author: David Yang <yangxiaohua@everest-semi.com>
6  *
7  * Based on es8316.c
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <linux/pm.h>
18 #include <linux/i2c.h>
19 #include <linux/spi/spi.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/of_gpio.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/tlv.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
29 #include <sound/initval.h>
30 #include <linux/proc_fs.h>
31 #include <linux/gpio.h>
32 #include <linux/interrupt.h>
33 #include <linux/irq.h>
34 #include <linux/regmap.h>
35 #include "es8316.h"
36
37 #if 1
38 #define DBG(x...) printk(x)
39 #else
40 #define DBG(x...) do { } while (0)
41 #endif
42 #define alsa_dbg DBG
43
44 #define dmic_used  1
45 #define amic_used  0
46
47 #define INVALID_GPIO -1
48 int es8316_spk_con_gpio = INVALID_GPIO;
49 int es8316_hp_con_gpio = INVALID_GPIO;
50 int es8316_hp_det_gpio = INVALID_GPIO;
51 int HP_IRQ = 0;
52 int hp_irq_flag = 0;
53 int es8316_init_reg = 0;
54
55 #define GPIO_LOW  0
56 #define GPIO_HIGH 1
57 #ifndef es8316_DEF_VOL
58 #define es8316_DEF_VOL                  0x1e
59 #endif
60
61 struct snd_soc_codec *es8316_codec;
62 static int es8316_init_regs(struct snd_soc_codec *codec);
63 static int es8316_set_bias_level(struct snd_soc_codec *codec,
64                                  enum snd_soc_bias_level level);
65
66 static const struct reg_default es8316_reg_defaults[] = {
67         {0x00, 0x03}, {0x01, 0x03}, {0x02, 0x00}, {0x03, 0x20},
68         {0x04, 0x11}, {0x05, 0x00}, {0x06, 0x11}, {0x07, 0x00},
69         {0x08, 0x00}, {0x09, 0x01}, {0x0a, 0x00}, {0x0b, 0x00},
70         {0x0c, 0xf8}, {0x0d, 0x3f}, {0x0e, 0x00}, {0x0f, 0x00},
71         {0x10, 0x01}, {0x11, 0xfc}, {0x12, 0x28}, {0x13, 0x00},
72         {0x14, 0x00}, {0x15, 0x33}, {0x16, 0x00}, {0x17, 0x00},
73         {0x18, 0x88}, {0x19, 0x06}, {0x1a, 0x22}, {0x1b, 0x03},
74         {0x1c, 0x0f}, {0x1d, 0x00}, {0x1e, 0x80}, {0x1f, 0x80},
75         {0x20, 0x00}, {0x21, 0x00}, {0x22, 0xc0}, {0x23, 0x00},
76         {0x24, 0x01}, {0x25, 0x08}, {0x26, 0x10}, {0x27, 0xc0},
77         {0x28, 0x00}, {0x29, 0x1c}, {0x2a, 0x00}, {0x2b, 0xb0},
78         {0x2c, 0x32}, {0x2d, 0x03}, {0x2e, 0x00}, {0x2f, 0x11},
79         {0x30, 0x10}, {0x31, 0x00}, {0x32, 0x00}, {0x33, 0xc0},
80         {0x34, 0xc0}, {0x35, 0x1f}, {0x36, 0xf7}, {0x37, 0xfd},
81         {0x38, 0xff}, {0x39, 0x1f}, {0x3a, 0xf7}, {0x3b, 0xfd},
82         {0x3c, 0xff}, {0x3d, 0x1f}, {0x3e, 0xf7}, {0x3f, 0xfd},
83         {0x40, 0xff}, {0x41, 0x1f}, {0x42, 0xf7}, {0x43, 0xfd},
84         {0x44, 0xff}, {0x45, 0x1f}, {0x46, 0xf7}, {0x47, 0xfd},
85         {0x48, 0xff}, {0x49, 0x1f}, {0x4a, 0xf7}, {0x4b, 0xfd},
86         {0x4c, 0xff}, {0x4d, 0x00}, {0x4e, 0x00}, {0x4f, 0xff},
87         {0x50, 0x00}, {0x51, 0x00}, {0x52, 0x00}, {0x53, 0x00},
88 };
89
90 static inline unsigned int es8316_readable(struct device *dev, unsigned int reg)
91 {
92         if (reg <= 90)
93                 return 0;
94         else
95                 return -1;
96 }
97 static inline unsigned int es8316_volatile(struct device *dev, unsigned int reg)
98 {
99         if (reg <= 90)
100                 return 0;
101         else
102                 return -1;
103 }
104 /* codec private data */
105 struct es8316_priv {
106         struct regmap *regmap;
107         unsigned int dmic_amic;
108         unsigned int sysclk;
109         struct snd_pcm_hw_constraint_list *sysclk_constraints;
110 };
111
112 /*
113 * es8316S Controls
114 */
115 /*#define DECLARE_TLV_DB_SCALE(name, min, step, mute) */
116 /*static const DECLARE_TLV_DB_SCALE(hpout_vol_tlv, -4800, 1200, 0);*/
117 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -9600, 50, 1);
118 static const DECLARE_TLV_DB_SCALE(adc_vol_tlv, -9600, 50, 1);
119 static const DECLARE_TLV_DB_SCALE(hpmixer_gain_tlv, -1200, 150, 0);
120 static const DECLARE_TLV_DB_SCALE(mic_bst_tlv, 0, 1200, 0);
121 /*static const DECLARE_TLV_DB_SCALE(linin_pga_tlv, 0, 300, 0);*/
122 /* {0, +3, +6, +9, +12, +15, +18, +21, +24,+27,+30,+33} dB */
123 static unsigned int linin_pga_tlv[] = {
124         TLV_DB_RANGE_HEAD(12),
125         0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
126         1, 1, TLV_DB_SCALE_ITEM(300, 0, 0),
127         2, 2, TLV_DB_SCALE_ITEM(600, 0, 0),
128         3, 3, TLV_DB_SCALE_ITEM(900, 0, 0),
129         4, 4, TLV_DB_SCALE_ITEM(1200, 0, 0),
130         5, 5, TLV_DB_SCALE_ITEM(1500, 0, 0),
131         6, 6, TLV_DB_SCALE_ITEM(1800, 0, 0),
132         7, 7, TLV_DB_SCALE_ITEM(2100, 0, 0),
133         8, 8, TLV_DB_SCALE_ITEM(2400, 0, 0),
134 };
135 static unsigned int hpout_vol_tlv[] = {
136         TLV_DB_RANGE_HEAD(1),
137         0, 3, TLV_DB_SCALE_ITEM(-4800, 1200, 0),
138 };
139 static const char *const alc_func_txt[] = {"Off", "On"};
140 static const struct soc_enum alc_func =
141         SOC_ENUM_SINGLE(ES8316_ADC_ALC1_REG29, 6, 2, alc_func_txt);
142
143 static const char *const ng_type_txt[] = {"Constant PGA Gain",
144                                     "Mute ADC Output"};
145 static const struct soc_enum ng_type =
146         SOC_ENUM_SINGLE(ES8316_ADC_ALC6_REG2E, 6, 2, ng_type_txt);
147
148 static const char *const adcpol_txt[] = {"Normal", "Invert"};
149 static const struct soc_enum adcpol =
150         SOC_ENUM_SINGLE(ES8316_ADC_MUTE_REG26, 1, 2, adcpol_txt);
151 static const char *const dacpol_txt[] = {"Normal", "R Invert", "L Invert",
152                                    "L + R Invert"};
153 static const struct soc_enum dacpol =
154         SOC_ENUM_SINGLE(ES8316_DAC_SET1_REG30, 0, 4, dacpol_txt);
155
156 static const struct snd_kcontrol_new es8316_snd_controls[] = {
157         /* HP OUT VOLUME */
158         SOC_DOUBLE_TLV("HP Playback Volume", ES8316_CPHP_ICAL_VOL_REG18,
159                        4, 0, 0, 1, hpout_vol_tlv),
160         /* HPMIXER VOLUME Control */
161         SOC_DOUBLE_TLV("HPMixer Gain", ES8316_HPMIX_VOL_REG16,
162                        0, 4, 7, 0, hpmixer_gain_tlv),
163
164         /* DAC Digital controls */
165         SOC_DOUBLE_R_TLV("DAC Playback Volume", ES8316_DAC_VOLL_REG33,
166                          ES8316_DAC_VOLR_REG34, 0, 0xC0, 1, dac_vol_tlv),
167
168         SOC_SINGLE("Enable DAC Soft Ramp", ES8316_DAC_SET1_REG30, 4, 1, 1),
169         SOC_SINGLE("DAC Soft Ramp Rate", ES8316_DAC_SET1_REG30, 2, 4, 0),
170
171         SOC_ENUM("Playback Polarity", dacpol),
172         SOC_SINGLE("DAC Notch Filter", ES8316_DAC_SET2_REG31, 6, 1, 0),
173         SOC_SINGLE("DAC Double Fs Mode", ES8316_DAC_SET2_REG31, 7, 1, 0),
174         SOC_SINGLE("DAC Volume Control-LeR", ES8316_DAC_SET2_REG31, 2, 1, 0),
175         SOC_SINGLE("DAC Stereo Enhancement", ES8316_DAC_SET3_REG32, 0, 7, 0),
176
177         /* +20dB D2SE PGA Control */
178         SOC_SINGLE_TLV("MIC Boost", ES8316_ADC_D2SEPGA_REG24,
179                        0, 1, 0, mic_bst_tlv),
180         /* 0-+24dB Lineinput PGA Control */
181         SOC_SINGLE_TLV("Input PGA", ES8316_ADC_PGAGAIN_REG23,
182                        4, 8, 0, linin_pga_tlv),
183
184         /* ADC Digital  Control */
185         SOC_SINGLE_TLV("ADC Capture Volume", ES8316_ADC_VOLUME_REG27,
186                        0, 0xC0, 1, adc_vol_tlv),
187         SOC_SINGLE("ADC Soft Ramp", ES8316_ADC_MUTE_REG26, 4, 1, 0),
188         SOC_ENUM("Capture Polarity", adcpol),
189         SOC_SINGLE("ADC Double FS Mode", ES8316_ADC_DMIC_REG25, 4, 1, 0),
190         /* ADC ALC  Control */
191         SOC_SINGLE("ALC Capture Target Volume",
192                    ES8316_ADC_ALC3_REG2B, 4, 10, 0),
193         SOC_SINGLE("ALC Capture Max PGA", ES8316_ADC_ALC1_REG29, 0, 28, 0),
194         SOC_SINGLE("ALC Capture Min PGA", ES8316_ADC_ALC2_REG2A, 0, 28, 0),
195         SOC_ENUM("ALC Capture Function", alc_func),
196         SOC_SINGLE("ALC Capture Hold Time", ES8316_ADC_ALC3_REG2B, 0, 10, 0),
197         SOC_SINGLE("ALC Capture Decay Time", ES8316_ADC_ALC4_REG2C, 4, 10, 0),
198         SOC_SINGLE("ALC Capture Attack Time", ES8316_ADC_ALC4_REG2C, 0, 10, 0),
199         SOC_SINGLE("ALC Capture NG Threshold", ES8316_ADC_ALC6_REG2E, 0, 31, 0),
200         SOC_ENUM("ALC Capture NG Type", ng_type),
201         SOC_SINGLE("ALC Capture NG Switch", ES8316_ADC_ALC6_REG2E, 5, 1, 0),
202 };
203
204 /* Analog Input MUX */
205 static const char * const es8316_analog_in_txt[] = {
206                 "lin1-rin1",
207                 "lin2-rin2",
208                 "lin1-rin1 with 20db Boost",
209                 "lin2-rin2 with 20db Boost"
210 };
211 static const unsigned int es8316_analog_in_values[] = {
212                 0,/*1,*/
213                 1,
214                 2,
215                 3
216 };
217 static const struct soc_enum es8316_analog_input_enum =
218         SOC_VALUE_ENUM_SINGLE(ES8316_ADC_PDN_LINSEL_REG22, 4, 3,
219                               ARRAY_SIZE(es8316_analog_in_txt),
220                               es8316_analog_in_txt,
221                               es8316_analog_in_values);
222 static const struct snd_kcontrol_new es8316_analog_in_mux_controls =
223         SOC_DAPM_ENUM("Route", es8316_analog_input_enum);
224
225 /* Dmic MUX */
226 static const char * const es8316_dmic_txt[] = {
227                 "dmic disable",
228                 "dmic data at high level",
229                 "dmic data at low level",
230 };
231 static const unsigned int es8316_dmic_values[] = {
232                 0,/*1,*/
233                 1,
234                 2
235 };
236 static const struct soc_enum es8316_dmic_src_enum =
237         SOC_VALUE_ENUM_SINGLE(ES8316_ADC_DMIC_REG25, 0, 3,
238                               ARRAY_SIZE(es8316_dmic_txt),
239                               es8316_dmic_txt,
240                               es8316_dmic_values);
241 static const struct snd_kcontrol_new es8316_dmic_src_controls =
242         SOC_DAPM_ENUM("Route", es8316_dmic_src_enum);
243
244 /* hp mixer mux */
245 static const char *const es8316_hpmux_texts[] = {
246         "lin1-rin1",
247         "lin2-rin2",
248         "lin-rin with Boost",
249         "lin-rin with Boost and PGA"
250 };
251
252 static const unsigned int es8316_hpmux_values[] = {0, 1, 2, 3};
253
254 static const struct soc_enum es8316_left_hpmux_enum =
255         SOC_VALUE_ENUM_SINGLE(ES8316_HPMIX_SEL_REG13, 4, 7,
256                               ARRAY_SIZE(es8316_hpmux_texts),
257                               es8316_hpmux_texts,
258                               es8316_hpmux_values);
259 static const struct snd_kcontrol_new es8316_left_hpmux_controls =
260         SOC_DAPM_VALUE_ENUM("Route", es8316_left_hpmux_enum);
261
262 static const struct soc_enum es8316_right_hpmux_enum =
263         SOC_VALUE_ENUM_SINGLE(ES8316_HPMIX_SEL_REG13, 0, 7,
264                               ARRAY_SIZE(es8316_hpmux_texts),
265                               es8316_hpmux_texts,
266                               es8316_hpmux_values);
267 static const struct snd_kcontrol_new es8316_right_hpmux_controls =
268         SOC_DAPM_VALUE_ENUM("Route", es8316_right_hpmux_enum);
269
270 /* headphone Output Mixer */
271 static const struct snd_kcontrol_new es8316_out_left_mix[] = {
272         SOC_DAPM_SINGLE("LLIN Switch", ES8316_HPMIX_SWITCH_REG14,
273                         6, 1, 0),
274         SOC_DAPM_SINGLE("Left DAC Switch", ES8316_HPMIX_SWITCH_REG14,
275                         7, 1, 0),
276 };
277 static const struct snd_kcontrol_new es8316_out_right_mix[] = {
278         SOC_DAPM_SINGLE("RLIN Switch", ES8316_HPMIX_SWITCH_REG14,
279                         2, 1, 0),
280         SOC_DAPM_SINGLE("Right DAC Switch", ES8316_HPMIX_SWITCH_REG14,
281                         3, 1, 0),
282 };
283
284 /* DAC data source mux */
285 static const char * const es8316_dacsrc_texts[] = {
286         "LDATA TO LDAC, RDATA TO RDAC",
287         "LDATA TO LDAC, LDATA TO RDAC",
288         "RDATA TO LDAC, RDATA TO RDAC",
289         "RDATA TO LDAC, LDATA TO RDAC",
290 };
291
292 static const unsigned int es8316_dacsrc_values[] = {
293         0, 1, 2, 3};
294
295 static const struct soc_enum es8316_dacsrc_mux_enum =
296         SOC_VALUE_ENUM_SINGLE(ES8316_DAC_SET1_REG30, 6, 4,
297                               ARRAY_SIZE(es8316_dacsrc_texts),
298                               es8316_dacsrc_texts,
299                               es8316_dacsrc_values);
300 static const struct snd_kcontrol_new es8316_dacsrc_mux_controls =
301         SOC_DAPM_VALUE_ENUM("Route", es8316_dacsrc_mux_enum);
302
303
304 static const struct snd_soc_dapm_widget es8316_dapm_widgets[] = {
305         /* Input Lines */
306         SND_SOC_DAPM_INPUT("DMIC"),
307         SND_SOC_DAPM_INPUT("MIC1"),
308         SND_SOC_DAPM_INPUT("MIC2"),
309
310         SND_SOC_DAPM_MICBIAS("micbias", SND_SOC_NOPM,
311                              0, 0),
312         /* Input MUX */
313         SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
314                          &es8316_analog_in_mux_controls),
315         SND_SOC_DAPM_PGA("Line input PGA", SND_SOC_NOPM,
316                          0, 0, NULL, 0),
317
318         /* ADCs */
319         SND_SOC_DAPM_ADC("Mono ADC", NULL, SND_SOC_NOPM, 0, 0),
320
321         /* Dmic MUX */
322         SND_SOC_DAPM_MUX("Digital Mic Mux", SND_SOC_NOPM, 0, 0,
323                          &es8316_dmic_src_controls),
324
325         /* Digital Interface */
326         SND_SOC_DAPM_AIF_OUT("I2S OUT", "I2S1 Capture", 1,
327                              SND_SOC_NOPM, 0, 0),
328         SND_SOC_DAPM_AIF_IN("I2S IN", "I2S1 Playback", 0,
329                             SND_SOC_NOPM, 0, 0),
330
331         /*  DACs DATA SRC MUX */
332         SND_SOC_DAPM_MUX("DAC SRC Mux", SND_SOC_NOPM, 0, 0,
333                          &es8316_dacsrc_mux_controls),
334         /*  DACs  */
335         SND_SOC_DAPM_DAC("Right DAC", NULL, SND_SOC_NOPM, 0, 0),
336
337         /*SND_SOC_DAPM_DAC("Left DAC", NULL, ES8316_DAC_PDN_REG2F, 4, 0),*/
338         SND_SOC_DAPM_DAC("Left DAC", NULL, SND_SOC_NOPM, 0, 0),
339
340         /* Headphone Output Side */
341         /* hpmux for hp mixer */
342         SND_SOC_DAPM_MUX("Left Hp mux", SND_SOC_NOPM, 0, 0,
343                          &es8316_left_hpmux_controls),
344         SND_SOC_DAPM_MUX("Right Hp mux", SND_SOC_NOPM, 0, 0,
345                          &es8316_right_hpmux_controls),
346         /* Output mixer  */
347         SND_SOC_DAPM_MIXER("Left Hp mixer", ES8316_HPMIX_PDN_REG15,
348                            4, 0, &es8316_out_left_mix[0],
349                            ARRAY_SIZE(es8316_out_left_mix)),
350         SND_SOC_DAPM_MIXER("Right Hp mixer", ES8316_HPMIX_PDN_REG15,
351                            0, 0, &es8316_out_right_mix[0],
352                            ARRAY_SIZE(es8316_out_right_mix)),
353
354
355         /* Ouput charge pump */
356
357         SND_SOC_DAPM_PGA("HPCP L", SND_SOC_NOPM,
358                          0, 0, NULL, 0),
359         SND_SOC_DAPM_PGA("HPCP R", SND_SOC_NOPM,
360                          0, 0, NULL, 0),
361
362         /* Ouput Driver */
363         SND_SOC_DAPM_PGA("HPVOL L", SND_SOC_NOPM,
364                          0, 0, NULL, 0),
365         SND_SOC_DAPM_PGA("HPVOL R", SND_SOC_NOPM,
366                          0, 0, NULL, 0),
367
368         /* Output Lines */
369         SND_SOC_DAPM_OUTPUT("HPOL"),
370         SND_SOC_DAPM_OUTPUT("HPOR"),
371 };
372
373 static const struct snd_soc_dapm_route es8316_dapm_routes[] = {
374         /*
375          * record route map
376          */
377         {"MIC1", NULL, "micbias"},
378         {"MIC2", NULL, "micbias"},
379         {"DMIC", NULL, "micbias"},
380
381         {"Differential Mux", "lin1-rin1", "MIC1"},
382         {"Differential Mux", "lin2-rin2", "MIC2"},
383         {"Line input PGA", NULL, "Differential Mux"},
384
385         {"Mono ADC", NULL, "Line input PGA"},
386
387         {"Digital Mic Mux", "dmic disable", "Mono ADC"},
388         {"Digital Mic Mux", "dmic data at high level", "DMIC"},
389         {"Digital Mic Mux", "dmic data at low level", "DMIC"},
390
391         {"I2S OUT", NULL, "Digital Mic Mux"},
392         /*
393          * playback route map
394          */
395         {"DAC SRC Mux", "LDATA TO LDAC, RDATA TO RDAC", "I2S IN"},
396         {"DAC SRC Mux", "LDATA TO LDAC, LDATA TO RDAC", "I2S IN"},
397         {"DAC SRC Mux", "RDATA TO LDAC, RDATA TO RDAC", "I2S IN"},
398         {"DAC SRC Mux", "RDATA TO LDAC, LDATA TO RDAC", "I2S IN"},
399
400         {"Left DAC", NULL, "DAC SRC Mux"},
401         {"Right DAC", NULL, "DAC SRC Mux"},
402
403         {"Left Hp mux", "lin1-rin1", "MIC1"},
404         {"Left Hp mux", "lin2-rin2", "MIC2"},
405         {"Left Hp mux", "lin-rin with Boost", "Differential Mux"},
406         {"Left Hp mux", "lin-rin with Boost and PGA", "Line input PGA"},
407
408         {"Right Hp mux", "lin1-rin1", "MIC1"},
409         {"Right Hp mux", "lin2-rin2", "MIC2"},
410         {"Right Hp mux", "lin-rin with Boost", "Differential Mux"},
411         {"Right Hp mux", "lin-rin with Boost and PGA", "Line input PGA"},
412
413         {"Left Hp mixer", "LLIN Switch", "Left Hp mux"},
414         {"Left Hp mixer", "Left DAC Switch", "Left DAC"},
415
416         {"Right Hp mixer", "RLIN Switch", "Right Hp mux"},
417         {"Right Hp mixer", "Right DAC Switch", "Right DAC"},
418
419         {"HPCP L", NULL, "Left Hp mixer"},
420         {"HPCP R", NULL, "Right Hp mixer"},
421
422         {"HPVOL L", NULL, "HPCP L"},
423         {"HPVOL R", NULL, "HPCP R"},
424
425         {"HPOL", NULL, "HPVOL L"},
426         {"HPOR", NULL, "HPVOL R"},
427 };
428
429 struct _coeff_div {
430         u32 mclk;       /*mclk frequency*/
431         u32 rate;       /*sample rate*/
432         u8 div;         /*adcclk and dacclk divider*/
433         u8 lrck_h;      /*adclrck divider and daclrck divider*/
434         u8 lrck_l;
435         u8 sr;          /*sclk divider*/
436         u8 osr;         /*adc osr*/
437 };
438
439
440 /* codec hifi mclk clock divider coefficients */
441 static const struct _coeff_div coeff_div[] = {
442         /* 8k */
443         {12288000, 8000 , 6 , 0x06, 0x00, 21, 32},
444         {11289600, 8000 , 6 , 0x05, 0x83, 20, 29},
445         {18432000, 8000 , 9 , 0x09, 0x00, 27, 32},
446         {16934400, 8000 , 8 , 0x08, 0x44, 25, 33},
447         {12000000, 8000 , 7 , 0x05, 0xdc, 21, 25},
448         {19200000, 8000 , 12, 0x09, 0x60, 27, 25},
449
450         /* 11.025k */
451         {11289600, 11025, 4 , 0x04, 0x00, 16, 32},
452         {16934400, 11025, 6 , 0x06, 0x00, 21, 32},
453         {12000000, 11025, 4 , 0x04, 0x40, 17, 34},
454
455         /* 16k */
456         {12288000, 16000, 3 , 0x03, 0x00, 12, 32},
457         {18432000, 16000, 5 , 0x04, 0x80, 18, 25},
458         {12000000, 16000, 3 , 0x02, 0xee, 12, 31},
459         {19200000, 16000, 6 , 0x04, 0xb0, 18, 25},
460
461         /* 22.05k */
462         {11289600, 22050, 2 , 0x02, 0x00, 8 , 32},
463         {16934400, 22050, 3 , 0x03, 0x00, 12, 32},
464         {12000000, 22050, 2 , 0x02, 0x20, 8 , 34},
465
466         /* 32k */
467         {12288000, 32000, 1 , 0x01, 0x80, 6 , 48},
468         {18432000, 32000, 2 , 0x02, 0x40, 9 , 32},
469         {12000000, 32000, 1 , 0x01, 0x77, 6 , 31},
470         {19200000, 32000, 3 , 0x02, 0x58, 10, 25},
471
472         /* 44.1k */
473         {11289600, 44100, 1 , 0x01, 0x00, 4 , 32},
474         {16934400, 44100, 1 , 0x01, 0x80, 6 , 32},
475         {12000000, 44100, 1 , 0x01, 0x10, 4 , 34},
476
477         /* 48k */
478         {12288000, 48000, 1 , 0x01, 0x00, 4 , 32},
479         {18432000, 48000, 1 , 0x01, 0x80, 6 , 32},
480         {12000000, 48000, 1 , 0x00, 0xfa, 4 , 31},
481         {19200000, 48000, 2 , 0x01, 0x90, 6, 25},
482
483         /* 88.2k */
484         {11289600, 88200, 1 , 0x00, 0x80, 2 , 32},
485         {16934400, 88200, 1 , 0x00, 0xc0, 3 , 48},
486         {12000000, 88200, 1 , 0x00, 0x88, 2 , 34},
487
488         /* 96k */
489         {12288000, 96000, 1 , 0x00, 0x80, 2 , 32},
490         {18432000, 96000, 1 , 0x00, 0xc0, 3 , 48},
491         {12000000, 96000, 1 , 0x00, 0x7d, 1 , 31},
492         {19200000, 96000, 1 , 0x00, 0xc8, 3 , 25},
493 };
494 static inline int get_coeff(int mclk, int rate)
495 {
496         int i;
497
498         for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
499                 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
500                         return i;
501         }
502
503         return -EINVAL;
504 }
505
506 /* The set of rates we can generate from the above for each SYSCLK */
507
508 static unsigned int rates_12288[] = {
509         8000, 12000, 16000, 24000, 24000, 32000, 48000, 96000,
510 };
511
512 static struct snd_pcm_hw_constraint_list constraints_12288 = {
513         .count  = ARRAY_SIZE(rates_12288),
514         .list   = rates_12288,
515 };
516
517 static unsigned int rates_112896[] = {
518         8000, 11025, 22050, 44100,
519 };
520
521 static struct snd_pcm_hw_constraint_list constraints_112896 = {
522         .count  = ARRAY_SIZE(rates_112896),
523         .list   = rates_112896,
524 };
525
526 static unsigned int rates_12[] = {
527         8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
528         48000, 88235, 96000,
529 };
530
531 static struct snd_pcm_hw_constraint_list constraints_12 = {
532         .count  = ARRAY_SIZE(rates_12),
533         .list   = rates_12,
534 };
535
536 /*
537 * Note that this should be called from init rather than from hw_params.
538 */
539 static int es8316_set_dai_sysclk(struct snd_soc_dai *codec_dai,
540                                  int clk_id, unsigned int freq, int dir)
541 {
542         struct snd_soc_codec *codec = codec_dai->codec;
543         struct es8316_priv *es8316 = snd_soc_codec_get_drvdata(codec);
544
545         switch (freq) {
546         case 11289600:
547         case 18432000:
548         case 22579200:
549         case 36864000:
550                 es8316->sysclk_constraints = &constraints_112896;
551                 es8316->sysclk = freq;
552                 return 0;
553         case 12288000:
554         case 19200000:
555         case 16934400:
556         case 24576000:
557         case 33868800:
558                 es8316->sysclk_constraints = &constraints_12288;
559                 es8316->sysclk = freq;
560                 return 0;
561         case 12000000:
562         case 24000000:
563                 es8316->sysclk_constraints = &constraints_12;
564                 es8316->sysclk = freq;
565                 return 0;
566         }
567         return -EINVAL;
568 }
569
570 static int es8316_set_dai_fmt(struct snd_soc_dai *codec_dai,
571                               unsigned int fmt)
572 {
573         struct snd_soc_codec *codec = codec_dai->codec;
574         u8 iface = 0;
575         u8 adciface = 0;
576         u8 daciface = 0;
577
578         alsa_dbg("%s----%d, fmt[%02x]\n", __func__, __LINE__, fmt);
579
580         iface    = snd_soc_read(codec, ES8316_IFACE);
581         adciface = snd_soc_read(codec, ES8316_ADC_IFACE);
582         daciface = snd_soc_read(codec, ES8316_DAC_IFACE);
583
584         /* set master/slave audio interface */
585         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
586         case SND_SOC_DAIFMT_CBM_CFM:
587                 alsa_dbg("es8316 in master mode");
588                 iface |= 0x80;
589                 break;
590         case SND_SOC_DAIFMT_CBS_CFS:
591                 alsa_dbg("es8316 in slave mode");
592                 iface &= 0x7F;
593                 break;
594         default:
595                 return -EINVAL;
596         }
597
598
599         /* interface format */
600
601         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
602         case SND_SOC_DAIFMT_I2S:
603                 adciface &= 0xFC;
604                 daciface &= 0xFC;
605                 break;
606         case SND_SOC_DAIFMT_RIGHT_J:
607                 return -EINVAL;
608         case SND_SOC_DAIFMT_LEFT_J:
609                 adciface &= 0xFC;
610                 daciface &= 0xFC;
611                 adciface |= 0x01;
612                 daciface |= 0x01;
613                 break;
614         case SND_SOC_DAIFMT_DSP_A:
615                 adciface &= 0xDC;
616                 daciface &= 0xDC;
617                 adciface |= 0x03;
618                 daciface |= 0x03;
619                 break;
620         case SND_SOC_DAIFMT_DSP_B:
621                 adciface &= 0xDC;
622                 daciface &= 0xDC;
623                 adciface |= 0x23;
624                 daciface |= 0x23;
625                 break;
626         default:
627                 return -EINVAL;
628         }
629
630
631         /* clock inversion */
632         switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
633         case SND_SOC_DAIFMT_NB_NF:
634                 iface    &= 0xDF;
635                 adciface &= 0xDF;
636                 daciface &= 0xDF;
637                 break;
638         case SND_SOC_DAIFMT_IB_IF:
639                 iface    |= 0x20;
640                 adciface |= 0x20;
641                 daciface |= 0x20;
642                 break;
643         case SND_SOC_DAIFMT_IB_NF:
644                 iface    |= 0x20;
645                 adciface &= 0xDF;
646                 daciface &= 0xDF;
647                 break;
648         case SND_SOC_DAIFMT_NB_IF:
649                 iface    &= 0xDF;
650                 adciface |= 0x20;
651                 daciface |= 0x20;
652                 break;
653         default:
654                 return -EINVAL;
655         }
656         snd_soc_write(codec, ES8316_IFACE, iface);
657         snd_soc_write(codec, ES8316_ADC_IFACE, adciface);
658         snd_soc_write(codec, ES8316_DAC_IFACE, daciface);
659         return 0;
660 }
661
662 static int es8316_pcm_startup(struct snd_pcm_substream *substream,
663                               struct snd_soc_dai *dai)
664 {
665         struct snd_soc_codec *codec = dai->codec;
666         struct es8316_priv *es8316 = snd_soc_codec_get_drvdata(codec);
667
668         DBG("Enter::%s----%d  es8316->sysclk=%d\n",
669             __func__, __LINE__, es8316->sysclk);
670
671         /* The set of sample rates that can be supported depends on the
672          * MCLK supplied to the CODEC - enforce this.
673          */
674         if (!es8316->sysclk) {
675                 dev_err(codec->dev,
676                         "No MCLK configured, call set_sysclk() on init\n");
677                 return -EINVAL;
678         }
679
680         snd_pcm_hw_constraint_list(substream->runtime, 0,
681                                    SNDRV_PCM_HW_PARAM_RATE,
682                                    es8316->sysclk_constraints);
683
684         return 0;
685 }
686 static int es8316_pcm_hw_params(struct snd_pcm_substream *substream,
687                                 struct snd_pcm_hw_params *params,
688                                 struct snd_soc_dai *dai)
689 {
690         struct snd_soc_pcm_runtime *rtd = substream->private_data;
691         struct snd_soc_codec *codec = rtd->codec;
692         struct es8316_priv *es8316 = snd_soc_codec_get_drvdata(codec);
693         int retv;
694
695         u16 osrate  =  snd_soc_read(codec,
696                                     ES8316_CLKMGR_ADCOSR_REG03) & 0xc0;
697         u16 mclkdiv = snd_soc_read(codec,
698                                    ES8316_CLKMGR_CLKSW_REG01) & 0x7f;
699         u16 srate = snd_soc_read(codec,
700                                  ES8316_SDP_MS_BCKDIV_REG09) & 0xE0;
701         u16 adciface = snd_soc_read(codec,
702                                     ES8316_SDP_ADCFMT_REG0A) & 0xE3;
703         u16 daciface = snd_soc_read(codec,
704                                     ES8316_SDP_DACFMT_REG0B) & 0xE3;
705         u16 adcdiv   = snd_soc_read(codec,
706                                     ES8316_CLKMGR_ADCDIV1_REG04);
707         u16 adclrckdiv_l = snd_soc_read(codec,
708                                         ES8316_CLKMGR_ADCDIV2_REG05) & 0x00;
709         u16 dacdiv   = snd_soc_read(codec, ES8316_CLKMGR_DACDIV1_REG06);
710         u16 daclrckdiv_l = snd_soc_read(codec,
711                                         ES8316_CLKMGR_DACDIV2_REG07) & 0x00;
712         int coeff;
713         u16 adclrckdiv_h = adcdiv & 0xf0;
714         u16 daclrckdiv_h = dacdiv & 0xf0;
715
716         adcdiv &= 0x0f;
717         dacdiv &= 0x0f;
718
719
720         coeff = get_coeff(es8316->sysclk, params_rate(params));
721         if (coeff < 0) {
722                 coeff = get_coeff(es8316->sysclk / 2, params_rate(params));
723                 mclkdiv |= 0x80;
724         }
725         if (coeff < 0) {
726                 dev_err(codec->dev,
727                         "Unable to configure sample rate %dHz with %dHz MCLK\n",
728                         params_rate(params), es8316->sysclk);
729                 return coeff;
730         }
731
732         /* bit size */
733         switch (params_format(params)) {
734         case SNDRV_PCM_FORMAT_S16_LE:
735                 adciface |= 0x000C;
736                 daciface |= 0x000C;
737                 break;
738         case SNDRV_PCM_FORMAT_S20_3LE:
739                 adciface |= 0x0004;
740                 daciface |= 0x0004;
741                 break;
742         case SNDRV_PCM_FORMAT_S24_LE:
743                 break;
744         case SNDRV_PCM_FORMAT_S32_LE:
745                 adciface |= 0x0010;
746                 daciface |= 0x0010;
747                 break;
748         }
749
750         /* set iface & srate*/
751         snd_soc_update_bits(codec, ES8316_SDP_DACFMT_REG0B, 0xe3, daciface);
752         snd_soc_update_bits(codec, ES8316_SDP_ADCFMT_REG0A, 0xe3, adciface);
753         snd_soc_update_bits(codec, ES8316_CLKMGR_CLKSW_REG01, 0x80, mclkdiv);
754         if (coeff >= 0) {
755                 osrate = coeff_div[coeff].osr;
756                 osrate &= 0x3f;
757
758                 srate |= coeff_div[coeff].sr;
759                 srate &= 0x1f;
760
761                 adcdiv |= (coeff_div[coeff].div << 4);
762                 adclrckdiv_h |= coeff_div[coeff].lrck_h;
763                 adcdiv &= 0xf0;
764                 adclrckdiv_h &= 0x0f;
765                 adcdiv |= adclrckdiv_h;
766                 adclrckdiv_l = coeff_div[coeff].lrck_l;
767
768                 dacdiv |= (coeff_div[coeff].div << 4);
769                 daclrckdiv_h |= coeff_div[coeff].lrck_h;
770                 dacdiv &= 0xf0;
771                 daclrckdiv_h &= 0x0f;
772                 dacdiv |= daclrckdiv_h;
773                 daclrckdiv_l = coeff_div[coeff].lrck_l;
774
775                 retv = snd_soc_read(codec, ES8316_CLKMGR_ADCDIV2_REG05);
776                 if (retv == 0) {
777                         es8316_init_regs(codec);
778                         if (es8316->dmic_amic ==  dmic_used)
779                                 snd_soc_write(codec, ES8316_GPIO_SEL_REG4D,
780                                               0x02);
781                         else
782                                 snd_soc_write(codec, ES8316_GPIO_SEL_REG4D,
783                                               0x00);
784                         snd_soc_write(codec, ES8316_GPIO_DEBUNCE_INT_REG4E,
785                                       0xf3);
786                         es8316_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
787                         es8316_init_reg = 1;
788                 }
789         }
790
791         retv = snd_soc_read(codec, ES8316_GPIO_FLAG);
792         return 0;
793 }
794
795 static int es8316_mute(struct snd_soc_dai *dai, int mute)
796 {
797         struct snd_soc_codec *codec = dai->codec;
798
799         dev_dbg(codec->dev, "%s %d\n", __func__, mute);
800         if (mute)
801                 snd_soc_write(codec, ES8316_DAC_SET1_REG30, 0x20);
802         else if (dai->playback_active)
803                 snd_soc_write(codec, ES8316_DAC_SET1_REG30, 0x00);
804         return 0;
805 }
806
807 static int es8316_set_bias_level(struct snd_soc_codec *codec,
808                                  enum snd_soc_bias_level level)
809 {
810         return 0;
811         switch (level) {
812         case SND_SOC_BIAS_ON:
813                 dev_dbg(codec->dev, "%s on\n", __func__);
814                 break;
815         case SND_SOC_BIAS_PREPARE:
816                 dev_dbg(codec->dev, "%s prepare\n", __func__);
817                 if (es8316_init_reg > 0) {
818                         snd_soc_write(codec, ES8316_CLKMGR_CLKSW_REG01, 0x7F);
819                         snd_soc_write(codec, ES8316_SYS_PDN_REG0D, 0x00);
820                         snd_soc_write(codec, ES8316_ADC_PDN_LINSEL_REG22, 0x00);
821                         snd_soc_write(codec, ES8316_DAC_PDN_REG2F, 0x00);
822                         snd_soc_write(codec, ES8316_HPMIX_SWITCH_REG14, 0x88);
823                         snd_soc_write(codec, ES8316_HPMIX_PDN_REG15, 0x88);
824                         snd_soc_write(codec, ES8316_HPMIX_VOL_REG16, 0xBB);
825                         snd_soc_write(codec, ES8316_CPHP_PDN2_REG1A, 0x10);
826                         snd_soc_write(codec, ES8316_CPHP_LDOCTL_REG1B, 0x30);
827                         snd_soc_write(codec, ES8316_CPHP_PDN1_REG19, 0x02);
828                         snd_soc_write(codec, ES8316_CPHP_ICAL_VOL_REG18, 0x00);
829                         snd_soc_write(codec, ES8316_RESET_REG00, 0xC0);
830                         snd_soc_write(codec, ES8316_CPHP_OUTEN_REG17, 0x66);
831                 }
832                 break;
833         case SND_SOC_BIAS_STANDBY:
834                 dev_dbg(codec->dev, "%s standby\n", __func__);
835                 if (es8316_init_reg > 0) {
836                         snd_soc_write(codec,
837                                       ES8316_CLKMGR_CLKSW_REG01, 0x7F);
838                         snd_soc_write(codec, ES8316_SYS_PDN_REG0D, 0x00);
839                         snd_soc_write(codec,
840                                       ES8316_ADC_PDN_LINSEL_REG22, 0x00);
841                         snd_soc_write(codec, ES8316_DAC_PDN_REG2F, 0x00);
842                         snd_soc_write(codec,
843                                       ES8316_HPMIX_SWITCH_REG14, 0x88);
844                         snd_soc_write(codec,
845                                       ES8316_HPMIX_PDN_REG15, 0x88);
846                         snd_soc_write(codec,
847                                       ES8316_HPMIX_VOL_REG16, 0xBB);
848                         snd_soc_write(codec, ES8316_CPHP_PDN2_REG1A, 0x10);
849                         snd_soc_write(codec, ES8316_CPHP_LDOCTL_REG1B, 0x30);
850                         snd_soc_write(codec, ES8316_CPHP_PDN1_REG19, 0x02);
851                         snd_soc_write(codec, ES8316_CPHP_ICAL_VOL_REG18, 0x00);
852                         snd_soc_write(codec, ES8316_RESET_REG00, 0xC0);
853                         snd_soc_write(codec, ES8316_CPHP_OUTEN_REG17, 0x66);
854                 }
855                 break;
856         case SND_SOC_BIAS_OFF:
857                 dev_dbg(codec->dev, "%s off\n", __func__);
858                 snd_soc_write(codec, ES8316_CPHP_ICAL_VOL_REG18, 0x33);
859                 snd_soc_write(codec, ES8316_CPHP_OUTEN_REG17, 0x00);
860                 snd_soc_write(codec, ES8316_CPHP_LDOCTL_REG1B, 0x03);
861                 snd_soc_write(codec, ES8316_CPHP_PDN2_REG1A, 0x22);
862                 snd_soc_write(codec, ES8316_CPHP_PDN1_REG19, 0x06);
863                 snd_soc_write(codec, ES8316_HPMIX_SWITCH_REG14, 0x00);
864                 snd_soc_write(codec, ES8316_HPMIX_PDN_REG15, 0x33);
865                 snd_soc_write(codec, ES8316_HPMIX_VOL_REG16, 0x00);
866                 snd_soc_write(codec, ES8316_ADC_PDN_LINSEL_REG22, 0xC0);
867                 snd_soc_write(codec, ES8316_DAC_PDN_REG2F, 0x11);
868                 snd_soc_write(codec, ES8316_SYS_PDN_REG0D, 0x3F);
869                 snd_soc_write(codec, ES8316_CLKMGR_CLKSW_REG01, 0x03);
870                 snd_soc_write(codec, ES8316_RESET_REG00, 0x7F);
871                 break;
872         }
873         codec->dapm.bias_level = level;
874
875         return 0;
876 }
877
878 #define es8316_RATES SNDRV_PCM_RATE_8000_96000
879
880 #define es8316_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
881         SNDRV_PCM_FMTBIT_S24_LE)
882
883 static struct snd_soc_dai_ops es8316_ops = {
884         .startup = es8316_pcm_startup,
885         .hw_params = es8316_pcm_hw_params,
886         .set_fmt = es8316_set_dai_fmt,
887         .set_sysclk = es8316_set_dai_sysclk,
888         .digital_mute = es8316_mute,
889 };
890
891 static struct snd_soc_dai_driver es8316_dai = {
892         .name = "ES8316 HiFi",
893         .playback = {
894                 .stream_name = "Playback",
895                 .channels_min = 1,
896                 .channels_max = 2,
897                 .rates = es8316_RATES,
898                 .formats = es8316_FORMATS,
899         },
900         .capture = {
901                 .stream_name = "Capture",
902                 .channels_min = 1,
903                 .channels_max = 2,
904                 .rates = es8316_RATES,
905                 .formats = es8316_FORMATS,
906         },
907         .ops = &es8316_ops,
908         .symmetric_rates = 1,
909 };
910
911
912 static int es8316_init_regs(struct snd_soc_codec *codec)
913 {
914         dev_dbg(codec->dev, "%s\n", __func__);
915         snd_soc_write(codec, ES8316_ADC_VOLUME_REG27, 0x00);
916         snd_soc_write(codec, ES8316_DAC_SET2_REG31, 0x00);
917         snd_soc_write(codec, ES8316_DAC_VOLL_REG33, 0x00);
918         snd_soc_write(codec, ES8316_DAC_VOLR_REG34, 0x00);
919         snd_soc_write(codec, ES8316_SDP_ADCFMT_REG0A, 0x0C);
920         snd_soc_write(codec, ES8316_SDP_DACFMT_REG0B, 0x0C);
921         snd_soc_write(codec, ES8316_CLKMGR_CLKSEL_REG02, 0x09);
922         snd_soc_write(codec, 0x03, 0x19);
923         snd_soc_write(codec, 0x04, 0x21);
924         snd_soc_write(codec, 0x05, 0x90);
925         snd_soc_write(codec, 0x06, 0x21);
926         snd_soc_write(codec, 0x07, 0x90);
927         snd_soc_write(codec, 0x09, 0x06);
928         snd_soc_write(codec, ES8316_CLKMGR_CPDIV_REG08, 0x00);
929         snd_soc_write(codec, ES8316_CLKMGR_CLKSW_REG01, 0x7F);
930         snd_soc_write(codec, ES8316_SYS_VMIDSEL_REG0C, 0xFa);
931         snd_soc_write(codec, ES8316_SYS_PDN_REG0D, 0x00);
932         snd_soc_write(codec, ES8316_RESET_REG00, 0xC0);
933         msleep(50);
934         snd_soc_write(codec, ES8316_ADC_PDN_LINSEL_REG22, 0x20);
935         snd_soc_write(codec, ES8316_DAC_PDN_REG2F, 0x00);
936         snd_soc_write(codec, ES8316_HPMIX_SEL_REG13, 0x00);
937         snd_soc_write(codec, ES8316_HPMIX_SWITCH_REG14, 0x88);
938         snd_soc_write(codec, ES8316_HPMIX_PDN_REG15, 0x00);
939         snd_soc_write(codec, ES8316_HPMIX_VOL_REG16, 0xBB);
940         snd_soc_write(codec, ES8316_CPHP_PDN2_REG1A, 0x10);
941         snd_soc_write(codec, ES8316_CPHP_LDOCTL_REG1B, 0x00);
942         snd_soc_write(codec, ES8316_CPHP_PDN1_REG19, 0x03);
943         snd_soc_write(codec, ES8316_CPHP_ICAL_VOL_REG18, 0x00);
944         snd_soc_write(codec, ES8316_SYS_VMIDLOW_REG10, 0x09);
945         msleep(20);
946         snd_soc_write(codec, ES8316_CPHP_OUTEN_REG17, 0x66);
947
948         snd_soc_write(codec, ES8316_GPIO_DEBUNCE_INT_REG4E, 0xf3);
949         snd_soc_write(codec, ES8316_TESTMODE_REG50, 0xA0);
950         snd_soc_write(codec, ES8316_TEST2_REG52, 0x03);
951         /*alc set*/
952         snd_soc_write(codec, ES8316_ADC_PGAGAIN_REG23, 0xa0);
953         snd_soc_write(codec, ES8316_ADC_D2SEPGA_REG24, 0x01);
954         /*adc ds mode, HPF enable*/
955         snd_soc_write(codec, ES8316_ADC_DMIC_REG25, 0x08);
956         snd_soc_write(codec, ES8316_ADC_ALC1_REG29, 0xcd);
957         snd_soc_write(codec, ES8316_ADC_ALC2_REG2A, 0x08);
958         snd_soc_write(codec, ES8316_ADC_ALC3_REG2B, 0xa0);
959         snd_soc_write(codec, ES8316_ADC_ALC4_REG2C, 0x05);
960         snd_soc_write(codec, ES8316_ADC_ALC5_REG2D, 0x06);
961         snd_soc_write(codec, ES8316_ADC_ALC6_REG2E, 0x61);
962         return 0;
963 }
964
965 static int es8316_suspend(struct snd_soc_codec *codec)
966 {
967         snd_soc_write(codec, ES8316_CPHP_ICAL_VOL_REG18, 0x33);
968         snd_soc_write(codec, ES8316_CPHP_OUTEN_REG17, 0x00);
969         snd_soc_write(codec, ES8316_CPHP_LDOCTL_REG1B, 0x03);
970         snd_soc_write(codec, ES8316_CPHP_PDN2_REG1A, 0x22);
971         snd_soc_write(codec, ES8316_CPHP_PDN1_REG19, 0x06);
972         snd_soc_write(codec, ES8316_HPMIX_SWITCH_REG14, 0x00);
973         snd_soc_write(codec, ES8316_HPMIX_PDN_REG15, 0x33);
974         snd_soc_write(codec, ES8316_HPMIX_VOL_REG16, 0x00);
975         snd_soc_write(codec, ES8316_ADC_PDN_LINSEL_REG22, 0xC0);
976         snd_soc_write(codec, ES8316_CLKMGR_CLKSW_REG01, 0x03);
977         return 0;
978 }
979
980 static int es8316_resume(struct snd_soc_codec *codec)
981 {
982         snd_soc_write(codec, ES8316_CPHP_ICAL_VOL_REG18, 0x00);
983         snd_soc_write(codec, ES8316_CPHP_LDOCTL_REG1B, 0x30);
984         snd_soc_write(codec, ES8316_CPHP_PDN2_REG1A, 0x10);
985         snd_soc_write(codec, ES8316_CPHP_PDN1_REG19, 0x02);
986         snd_soc_write(codec, ES8316_HPMIX_SWITCH_REG14, 0x88);
987         snd_soc_write(codec, ES8316_HPMIX_PDN_REG15, 0x88);
988         snd_soc_write(codec, ES8316_HPMIX_VOL_REG16, 0xbb);
989         snd_soc_write(codec, ES8316_ADC_PDN_LINSEL_REG22, 0x20);
990         snd_soc_write(codec, ES8316_CLKMGR_CLKSW_REG01, 0x7f);
991         snd_soc_write(codec, ES8316_CPHP_OUTEN_REG17, 0x66);
992         return 0;
993 }
994
995 static int es8316_probe(struct snd_soc_codec *codec)
996 {
997         int ret = 0, retv;
998         struct es8316_priv *es8316 = snd_soc_codec_get_drvdata(codec);
999
1000         DBG("---%s--start--\n", __func__);
1001
1002         ret = snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_I2C);
1003
1004         if (ret < 0) {
1005                 dev_err(codec->dev, "fail to reset audio (%d)\n", ret);
1006                 goto err;
1007         }
1008         retv = snd_soc_read(codec, ES8316_CLKMGR_ADCDIV2_REG05);
1009         if (retv == 0) {
1010                 snd_soc_write(codec, ES8316_GPIO_DEBUNCE_INT_REG4E, 0xf0);
1011                 snd_soc_write(codec, 0x01, 0x7f);
1012                 snd_soc_write(codec, 0x00, 0xc3);
1013                 msleep(100);
1014                 if (es8316->dmic_amic ==  dmic_used)
1015                         snd_soc_write(codec, ES8316_GPIO_SEL_REG4D, 0x02);
1016                 else
1017                         snd_soc_write(codec, ES8316_GPIO_SEL_REG4D, 0x00);
1018                 snd_soc_write(codec, ES8316_GPIO_DEBUNCE_INT_REG4E, 0xf3);
1019         }
1020         codec->dapm.idle_bias_off = 0;
1021 #if defined(HS_IRQ)
1022         det_initalize();
1023 #elif defined(HS_TIMER)
1024         hsdet_init();
1025 #endif
1026 err:
1027         return ret;
1028 }
1029
1030 static int es8316_remove(struct snd_soc_codec *codec)
1031 {
1032         es8316_set_bias_level(codec, SND_SOC_BIAS_OFF);
1033         return 0;
1034 }
1035
1036 static struct snd_soc_codec_driver soc_codec_dev_es8316 = {
1037         .probe =        es8316_probe,
1038         .remove =       es8316_remove,
1039         .suspend =      es8316_suspend,
1040         .resume =       es8316_resume,
1041         .set_bias_level = es8316_set_bias_level,
1042         .reg_cache_size = ARRAY_SIZE(es8316_reg_defaults),
1043         .reg_word_size = sizeof(u8),
1044         .reg_cache_default = es8316_reg_defaults,
1045
1046         .controls = es8316_snd_controls,
1047         .num_controls = ARRAY_SIZE(es8316_snd_controls),
1048         .dapm_widgets = es8316_dapm_widgets,
1049         .num_dapm_widgets = ARRAY_SIZE(es8316_dapm_widgets),
1050         .dapm_routes = es8316_dapm_routes,
1051         .num_dapm_routes = ARRAY_SIZE(es8316_dapm_routes),
1052 };
1053
1054 #if defined(CONFIG_SPI_MASTER)
1055 static int es8316_spi_probe(struct spi_device *spi)
1056 {
1057         struct es8316_priv *es8316;
1058         int ret;
1059
1060         es8316 = kzalloc(sizeof(*es8316), GFP_KERNEL);
1061         if (es8316 == NULL)
1062                 return -ENOMEM;
1063
1064         spi_set_drvdata(spi, es8316);
1065         ret = snd_soc_register_codec(&spi->dev,
1066                                      &soc_codec_dev_es8316,
1067                                      &es8316_dai, 1);
1068         if (ret < 0)
1069                 kfree(es8316);
1070         return ret;
1071 }
1072
1073 static int es8316_spi_remove(struct spi_device *spi)
1074 {
1075         snd_soc_unregister_codec(&spi->dev);
1076         kfree(spi_get_drvdata(spi));
1077         return 0;
1078 }
1079
1080 static struct spi_driver es8316_spi_driver = {
1081         .driver = {
1082                 .name   = "es8316",
1083                 .owner  = THIS_MODULE,
1084         },
1085         .probe          = es8316_spi_probe,
1086         .remove         = es8316_spi_remove,
1087 };
1088 #endif /* CONFIG_SPI_MASTER */
1089
1090 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1091
1092 static void es8316_i2c_shutdown(struct i2c_client *i2c)
1093 {
1094         struct snd_soc_codec *codec;
1095
1096         if (!es8316_codec)
1097                 goto err;
1098         codec = es8316_codec;
1099         snd_soc_write(codec, ES8316_CPHP_ICAL_VOL_REG18, 0x33);
1100         snd_soc_write(codec, ES8316_CPHP_OUTEN_REG17, 0x00);
1101         snd_soc_write(codec, ES8316_CPHP_LDOCTL_REG1B, 0x03);
1102         snd_soc_write(codec, ES8316_CPHP_PDN2_REG1A, 0x22);
1103         snd_soc_write(codec, ES8316_CPHP_PDN1_REG19, 0x06);
1104         snd_soc_write(codec, ES8316_HPMIX_SWITCH_REG14, 0x00);
1105         snd_soc_write(codec, ES8316_HPMIX_PDN_REG15, 0x33);
1106         snd_soc_write(codec, ES8316_HPMIX_VOL_REG16, 0x00);
1107         snd_soc_write(codec, ES8316_ADC_PDN_LINSEL_REG22, 0xC0);
1108         snd_soc_write(codec, ES8316_DAC_PDN_REG2F, 0x11);
1109         snd_soc_write(codec, ES8316_SYS_PDN_REG0D, 0x3F);
1110         snd_soc_write(codec, ES8316_CLKMGR_CLKSW_REG01, 0x03);
1111         snd_soc_write(codec, ES8316_RESET_REG00, 0x7F);
1112 err:
1113         return;
1114 }
1115
1116 static int es8316_i2c_probe(struct i2c_client *i2c_client,
1117                             const struct i2c_device_id *id)
1118 {
1119         struct es8316_priv *es8316;
1120         int ret = -1;
1121
1122         DBG("---%s---probe start\n", __func__);
1123
1124         es8316 = kzalloc(sizeof(*es8316), GFP_KERNEL);
1125         if (es8316 == NULL)
1126                 return -ENOMEM;
1127         es8316->dmic_amic = amic_used;     /*if internal mic is amic*/
1128         i2c_set_clientdata(i2c_client, es8316);
1129         ret = snd_soc_register_codec(&i2c_client->dev,
1130                                      &soc_codec_dev_es8316,
1131                                      &es8316_dai, 1);
1132         if (ret < 0) {
1133                 kfree(es8316);
1134                 return ret;
1135         }
1136
1137         return ret;
1138 }
1139
1140 static  int es8316_i2c_remove(struct i2c_client *client)
1141 {
1142         snd_soc_unregister_codec(&client->dev);
1143         kfree(i2c_get_clientdata(client));
1144         return 0;
1145 }
1146
1147 static const unsigned short normal_i2c[] = {0x10, I2C_CLIENT_END};
1148 static const struct i2c_device_id es8316_i2c_id[] = {
1149         {"es8316", 0},
1150         {"10ES8316:00", 0},
1151         {"10ES8316", 0},
1152         { }
1153 };
1154 MODULE_DEVICE_TABLE(i2c, es8316_i2c_id);
1155
1156 static struct i2c_driver es8316_i2c_driver = {
1157         .driver = {
1158                 .name = "es8316",
1159                 .owner = THIS_MODULE,
1160         },
1161         .shutdown = es8316_i2c_shutdown,
1162         .probe = es8316_i2c_probe,
1163         .remove = es8316_i2c_remove,
1164         .id_table = es8316_i2c_id,
1165         .class = I2C_CLASS_HWMON,
1166         .address_list = normal_i2c,
1167 };
1168 #endif
1169
1170 static int __init es8316_init(void)
1171 {
1172         DBG("--%s--start--\n", __func__);
1173 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1174         return i2c_add_driver(&es8316_i2c_driver);
1175 #endif
1176 #if defined(CONFIG_SPI_MASTER)
1177         return spi_register_driver(&es8316_spi_driver);
1178 #endif
1179 }
1180
1181 static void __exit es8316_exit(void)
1182 {
1183 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1184         return i2c_del_driver(&es8316_i2c_driver);
1185 #endif
1186 #if defined(CONFIG_SPI_MASTER)
1187         return spi_unregister_driver(&es8316_spi_driver);
1188 #endif
1189 }
1190
1191
1192 module_init(es8316_init);
1193 module_exit(es8316_exit);
1194
1195 MODULE_DESCRIPTION("ASoC es8316 driver");
1196 MODULE_AUTHOR("Will <will@everset-semi.com>");
1197 MODULE_LICENSE("GPL");