rk3168_86v: basic function is ok
[firefly-linux-kernel-4.4.55.git] / sound / soc / codecs / es8323.c
1 /*
2  * es8323.c -- es8323 ALSA SoC audio driver
3  *
4  * Copyright 2009 Wolfson Microelectronics plc
5  * Copyright 2005 Openedhand Ltd.
6  *
7  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License version 2 as
11  * published by the Free Software Foundation.
12  */
13
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/pm.h>
19 #include <linux/i2c.h>
20 #include <linux/spi/spi.h>
21 #include <linux/platform_device.h>
22 #include <linux/slab.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
31 #include <mach/iomux.h>
32 #include <mach/board.h>
33 #include <mach/gpio.h>
34
35 //#include <linux/tchip_sysinf.h>
36
37 #include "es8323.h"
38
39 #include <linux/proc_fs.h>
40 #include <linux/gpio.h>
41
42
43 #include <linux/interrupt.h>
44 #include <linux/irq.h>
45
46 #if 0
47 #define DBG(x...) printk(KERN_INFO x)
48 #else
49 #define DBG(x...) do { } while (0)
50 #endif
51 #define alsa_dbg DBG
52
53 static int set_spk = 1;                     // add by xhc when insert hdmi 0, no insert hdmi 1
54 #define RT5633_SPK_TIMER        0       //if enable this, MUST enable RT5633_EQ_FUNC_ENA and RT5633_EQ_FUNC_SEL==RT5633_EQ_FOR_MANUAL first!
55 #if (RT5633_SPK_TIMER == 1)
56 static struct timer_list spk_timer;
57 struct work_struct  spk_work;
58 static bool last_is_spk = false;
59 //#define ES8323_HP_PIN  RK30_PIN0_PC7  // need modify.
60 #endif
61 //#define SPK_CTL               RK29_PIN6_PB7
62 //#define SPK_CON               RK29_PIN6_PB6
63     #undef SPK_CTL
64 //    #undef SPK_CON
65 #define SPK_CON                 RK30_PIN2_PD7 //RK30_PIN4_PC5
66 #define HP_DET          RK30_PIN0_PB5
67 static int HP_IRQ=0;
68 static int hp_irq_flag = 0;
69 //#define SPK_CTL             RK29_PIN6_PB6
70 //#define EAR_CON_PIN             RK29_PIN6_PB5
71 #undef EAR_CON_PIN
72
73 #ifndef es8323_DEF_VOL
74 #define es8323_DEF_VOL                  0x1e
75 #endif
76
77 static int es8323_set_bias_level(struct snd_soc_codec *codec,enum snd_soc_bias_level level);
78 extern int es8323_dapm_pre_event(struct snd_soc_dapm_widget* widget, struct snd_kcontrol * null, int event);
79 extern int es8323_dapm_post_event(struct snd_soc_dapm_widget* widget, struct snd_kcontrol * null, int event);                                
80 /*
81  * es8323 register cache
82  * We can't read the es8323 register space when we
83  * are using 2 wire for device control, so we cache them instead.
84  */
85 static u16 es8323_reg[] = {
86         0x06, 0x1C, 0xC3, 0xFC,  /*  0 *////0x0100 0x0180
87         0xC0, 0x00, 0x00, 0x7C,  /*  4 */
88         0x80, 0x00, 0x00, 0x06,  /*  8 */
89         0x00, 0x06, 0x30, 0x30,  /* 12 */
90         0xC0, 0xC0, 0x38, 0xB0,  /* 16 */
91         0x32, 0x06, 0x00, 0x00,  /* 20 */
92         0x06, 0x30, 0xC0, 0xC0,  /* 24 */
93         0x08, 0x06, 0x1F, 0xF7,  /* 28 */
94         0xFD, 0xFF, 0x1F, 0xF7,  /* 32 */
95         0xFD, 0xFF, 0x00, 0x38,  /* 36 */
96         0x38, 0x38, 0x38, 0x38,  /* 40 */
97         0x38, 0x00, 0x00, 0x00,  /* 44 */
98         0x00, 0x00, 0x00, 0x00,  /* 48 */
99         0x00, 0x00, 0x00, 0x00,  /* 52 */
100 };
101
102 /* codec private data */
103 struct es8323_priv {
104         unsigned int sysclk;
105         enum snd_soc_control_type control_type;
106         struct snd_pcm_hw_constraint_list *sysclk_constraints;
107         int is_startup;         // gModify.Add
108         int is_biason;
109 };
110
111 static void hp_detect_do_switch(struct work_struct *work)
112 {
113         int ret;
114         int irq = gpio_to_irq(HP_DET);
115         unsigned int type;
116
117         //rk28_send_wakeup_key();
118         printk("hjc:%s,irq=%d\n",__func__,irq);
119         type = gpio_get_value(HP_DET) ? IRQ_TYPE_EDGE_FALLING : IRQ_TYPE_EDGE_RISING;
120         ret = irq_set_irq_type(irq, type);
121         if (ret < 0) {
122                 pr_err("%s: irq_set_irq_type(%d, %d) failed\n", __func__, irq, type);
123         }
124
125         hp_irq_flag = 1;
126
127         if(0 == gpio_get_value(HP_DET)){
128                 printk("hp_det = 0,insert hp\n");
129                 gpio_set_value(SPK_CON,0);
130         }else if(1 == gpio_get_value(HP_DET)){
131                 printk("hp_det = 1,deinsert hp\n");
132                 gpio_set_value(SPK_CON,1);
133         }       
134         enable_irq(irq);
135 }
136
137
138 static DECLARE_DELAYED_WORK(wakeup_work, hp_detect_do_switch);
139
140
141 static irqreturn_t hp_det_irq_handler(int irq, void *dev_id)
142 {
143 #if 0
144     printk("%s=%d,%d\n",__FUNCTION__,HP_IRQ,HP_DET);
145     //disable_irq_nosync(ts->client->irq);      
146         //queue_work(gt801_wq, &ts->work);
147         if(0 == gpio_get_value(HP_DET)){
148                 printk("hp_det = 0,insert hp\n");
149                 gpio_set_value(SPK_CON,0);
150         }else if(1 == gpio_get_value(HP_DET)){
151                 printk("hp_det = 1,insert hp\n");
152                 gpio_set_value(SPK_CON,1);
153         }       
154     return IRQ_HANDLED;
155 #endif
156         printk("hjc:%s>>>>\n",__func__);
157         disable_irq_nosync(irq); // for irq debounce
158         //wake_lock_timeout(&usb_wakelock, WAKE_LOCK_TIMEOUT);
159         schedule_delayed_work(&wakeup_work, HZ / 10);
160         return IRQ_HANDLED;
161
162 }
163
164
165
166 static unsigned int es8323_read_reg_cache(struct snd_soc_codec *codec,
167                                      unsigned int reg)
168 {
169         //u16 *cache = codec->reg_cache;
170         if (reg >= ARRAY_SIZE(es8323_reg))
171                 return -1;
172         return es8323_reg[reg];
173 }
174
175 static int es8323_write(struct snd_soc_codec *codec, unsigned int reg,
176                              unsigned int value)
177 {
178         //u16 *cache = codec->reg_cache;
179         u8 data[2];
180         int ret;
181
182         BUG_ON(codec->volatile_register);
183
184         data[0] = reg;
185         data[1] = value & 0x00ff;
186
187         if (reg < ARRAY_SIZE(es8323_reg))
188                 es8323_reg[reg] = value;
189         ret = codec->hw_write(codec->control_data, data, 2);
190         if (ret == 2)
191                 return 0;
192         if (ret < 0)
193                 return ret;
194         else
195                 return -EIO;
196 }
197
198 //#define es8323_reset(c)       snd_soc_write(c, es8323_RESET, 0)
199  static int es8323_reset(struct snd_soc_codec *codec)
200  {
201         snd_soc_write(codec, ES8323_CONTROL1, 0x80);
202   return snd_soc_write(codec, ES8323_CONTROL1, 0x00);
203  }
204
205 static const char *es8323_line_texts[] = {
206         "Line 1", "Line 2", "PGA"};
207
208 static const unsigned int es8323_line_values[] = {
209         0, 1, 3};
210 static const char *es8323_pga_sel[] = {"Line 1", "Line 2", "Differential"};
211 static const char *stereo_3d_txt[] = {"No 3D  ", "Level 1","Level 2","Level 3","Level 4","Level 5","Level 6","Level 7"};
212 static const char *alc_func_txt[] = {"Off", "Right", "Left", "Stereo"};
213 static const char *ng_type_txt[] = {"Constant PGA Gain","Mute ADC Output"};
214 static const char *deemph_txt[] = {"None", "32Khz", "44.1Khz", "48Khz"};
215 static const char *adcpol_txt[] = {"Normal", "L Invert", "R Invert","L + R Invert"};
216 static const char *es8323_mono_mux[] = {"Stereo", "Mono (Left)","Mono (Right)"};
217 static const char *es8323_diff_sel[] = {"Line 1", "Line 2"};
218                                    
219 static const struct soc_enum es8323_enum[]={    
220         SOC_VALUE_ENUM_SINGLE(ES8323_DACCONTROL16, 3, 7, ARRAY_SIZE(es8323_line_texts), es8323_line_texts, es8323_line_values),/* LLINE */
221         SOC_VALUE_ENUM_SINGLE(ES8323_DACCONTROL16, 0, 7, ARRAY_SIZE(es8323_line_texts), es8323_line_texts, es8323_line_values),/* rline */
222         SOC_VALUE_ENUM_SINGLE(ES8323_ADCCONTROL2, 6, 3, ARRAY_SIZE(es8323_pga_sel), es8323_line_texts, es8323_line_values),/* Left PGA Mux */
223         SOC_VALUE_ENUM_SINGLE(ES8323_ADCCONTROL2, 4, 3, ARRAY_SIZE(es8323_pga_sel), es8323_line_texts, es8323_line_values),/* Right PGA Mux */
224         SOC_ENUM_SINGLE(ES8323_DACCONTROL7, 2, 8, stereo_3d_txt),/* stereo-3d */
225         SOC_ENUM_SINGLE(ES8323_ADCCONTROL10, 6, 4, alc_func_txt),/*alc func*/
226         SOC_ENUM_SINGLE(ES8323_ADCCONTROL14, 1, 2, ng_type_txt),/*noise gate type*/
227         SOC_ENUM_SINGLE(ES8323_DACCONTROL6, 6, 4, deemph_txt),/*Playback De-emphasis*/
228         SOC_ENUM_SINGLE(ES8323_ADCCONTROL6, 6, 4, adcpol_txt),
229         SOC_ENUM_SINGLE(ES8323_ADCCONTROL3, 3, 3, es8323_mono_mux),
230         SOC_ENUM_SINGLE(ES8323_ADCCONTROL3, 7, 2, es8323_diff_sel),
231         };
232                 
233
234 static const DECLARE_TLV_DB_SCALE(pga_tlv, 0, 300, 0);
235 static const DECLARE_TLV_DB_SCALE(adc_tlv, -9600, 50, 1);
236 static const DECLARE_TLV_DB_SCALE(dac_tlv, -9600, 50, 1);
237 static const DECLARE_TLV_DB_SCALE(out_tlv, -4500, 150, 0);
238 static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
239
240 static const struct snd_kcontrol_new es8323_snd_controls[] = {
241 SOC_ENUM("3D Mode", es8323_enum[4]),
242 SOC_SINGLE("ALC Capture Target Volume", ES8323_ADCCONTROL11, 4, 15, 0),
243 SOC_SINGLE("ALC Capture Max PGA", ES8323_ADCCONTROL10, 3, 7, 0),
244 SOC_SINGLE("ALC Capture Min PGA", ES8323_ADCCONTROL10, 0, 7, 0),
245 SOC_ENUM("ALC Capture Function", es8323_enum[5]),
246 SOC_SINGLE("ALC Capture ZC Switch", ES8323_ADCCONTROL13, 6, 1, 0),
247 SOC_SINGLE("ALC Capture Hold Time", ES8323_ADCCONTROL11, 0, 15, 0),
248 SOC_SINGLE("ALC Capture Decay Time", ES8323_ADCCONTROL12, 4, 15, 0),
249 SOC_SINGLE("ALC Capture Attack Time", ES8323_ADCCONTROL12, 0, 15, 0),
250 SOC_SINGLE("ALC Capture NG Threshold", ES8323_ADCCONTROL14, 3, 31, 0),
251 SOC_ENUM("ALC Capture NG Type",es8323_enum[6]),
252 SOC_SINGLE("ALC Capture NG Switch", ES8323_ADCCONTROL14, 0, 1, 0),
253 SOC_SINGLE("ZC Timeout Switch", ES8323_ADCCONTROL13, 6, 1, 0),
254 SOC_DOUBLE_R_TLV("Capture Digital Volume", ES8323_ADCCONTROL8, ES8323_ADCCONTROL9,0, 255, 1, adc_tlv),           
255 SOC_SINGLE("Capture Mute", ES8323_ADCCONTROL7, 2, 1, 0),                
256 SOC_SINGLE_TLV("Left Channel Capture Volume",   ES8323_ADCCONTROL1, 4, 15, 0, bypass_tlv),
257 SOC_SINGLE_TLV("Right Channel Capture Volume",  ES8323_ADCCONTROL1, 0, 15, 0, bypass_tlv),
258 SOC_ENUM("Playback De-emphasis", es8323_enum[7]),
259 SOC_ENUM("Capture Polarity", es8323_enum[8]),
260 SOC_DOUBLE_R_TLV("PCM Volume", ES8323_DACCONTROL4, ES8323_DACCONTROL5, 0, 255, 1, dac_tlv),
261 SOC_SINGLE_TLV("Left Mixer Left Bypass Volume", ES8323_DACCONTROL17, 3, 7, 1, bypass_tlv),
262 SOC_SINGLE_TLV("Right Mixer Right Bypass Volume", ES8323_DACCONTROL20, 3, 7, 1, bypass_tlv),
263 SOC_DOUBLE_R_TLV("Output 1 Playback Volume", ES8323_DACCONTROL24, ES8323_DACCONTROL25, 0, 64, 0, out_tlv),
264 SOC_DOUBLE_R_TLV("Output 2 Playback Volume", ES8323_DACCONTROL26, ES8323_DACCONTROL27, 0, 64, 0, out_tlv),
265 };
266
267
268 static const struct snd_kcontrol_new es8323_left_line_controls =
269         SOC_DAPM_VALUE_ENUM("Route", es8323_enum[0]);
270
271 static const struct snd_kcontrol_new es8323_right_line_controls =
272         SOC_DAPM_VALUE_ENUM("Route", es8323_enum[1]);
273
274 /* Left PGA Mux */
275 static const struct snd_kcontrol_new es8323_left_pga_controls =
276         SOC_DAPM_VALUE_ENUM("Route", es8323_enum[2]);
277 /* Right PGA Mux */
278 static const struct snd_kcontrol_new es8323_right_pga_controls =
279         SOC_DAPM_VALUE_ENUM("Route", es8323_enum[3]);
280
281 /* Left Mixer */
282 static const struct snd_kcontrol_new es8323_left_mixer_controls[] = {
283         SOC_DAPM_SINGLE("Left Playback Switch", ES8323_DACCONTROL17, 7, 1, 0),
284         SOC_DAPM_SINGLE("Left Bypass Switch", ES8323_DACCONTROL17, 6, 1, 0),    
285 };
286
287 /* Right Mixer */
288 static const struct snd_kcontrol_new es8323_right_mixer_controls[] = {
289         SOC_DAPM_SINGLE("Right Playback Switch", ES8323_DACCONTROL20, 7, 1, 0),
290         SOC_DAPM_SINGLE("Right Bypass Switch", ES8323_DACCONTROL20, 6, 1, 0),
291 };
292
293 /* Differential Mux */
294 //static const char *es8323_diff_sel[] = {"Line 1", "Line 2"};
295 static const struct snd_kcontrol_new es8323_diffmux_controls =
296         SOC_DAPM_ENUM("Route", es8323_enum[10]);
297
298 /* Mono ADC Mux */
299 static const struct snd_kcontrol_new es8323_monomux_controls =
300         SOC_DAPM_ENUM("Route", es8323_enum[9]);
301
302 static const struct snd_soc_dapm_widget es8323_dapm_widgets[] = {
303 #if 1
304         SND_SOC_DAPM_INPUT("LINPUT1"),
305         SND_SOC_DAPM_INPUT("LINPUT2"),
306         SND_SOC_DAPM_INPUT("RINPUT1"),
307         SND_SOC_DAPM_INPUT("RINPUT2"),
308         
309         SND_SOC_DAPM_MICBIAS("Mic Bias", ES8323_ADCPOWER, 3, 1),
310
311         SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
312                 &es8323_diffmux_controls),
313                 
314         SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
315                 &es8323_monomux_controls),
316         SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
317                 &es8323_monomux_controls),
318  
319         SND_SOC_DAPM_MUX("Left PGA Mux", ES8323_ADCPOWER, 7, 1,
320                 &es8323_left_pga_controls),
321         SND_SOC_DAPM_MUX("Right PGA Mux", ES8323_ADCPOWER, 6, 1,
322                 &es8323_right_pga_controls),
323
324         SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
325                 &es8323_left_line_controls),
326         SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
327                 &es8323_right_line_controls),
328
329         SND_SOC_DAPM_ADC("Right ADC", "Right Capture", ES8323_ADCPOWER, 4, 1),
330         SND_SOC_DAPM_ADC("Left ADC", "Left Capture", ES8323_ADCPOWER, 5, 1),
331
332         /* gModify.Cmmt Implement when suspend/startup */
333         SND_SOC_DAPM_DAC("Right DAC", "Right Playback", ES8323_DACPOWER, 7, 0),
334         SND_SOC_DAPM_DAC("Left DAC", "Left Playback", ES8323_DACPOWER, 8, 0),
335
336         SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
337                 &es8323_left_mixer_controls[0],
338                 ARRAY_SIZE(es8323_left_mixer_controls)),
339         SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
340                 &es8323_right_mixer_controls[0],
341                 ARRAY_SIZE(es8323_right_mixer_controls)),
342
343         SND_SOC_DAPM_PGA("Right Out 2", ES8323_DACPOWER, 2, 0, NULL, 0),
344         SND_SOC_DAPM_PGA("Left Out 2", ES8323_DACPOWER, 3, 0, NULL, 0),
345         SND_SOC_DAPM_PGA("Right Out 1", ES8323_DACPOWER, 4, 0, NULL, 0),
346         SND_SOC_DAPM_PGA("Left Out 1", ES8323_DACPOWER, 5, 0, NULL, 0),
347         SND_SOC_DAPM_PGA("LAMP", ES8323_ADCCONTROL1, 4, 0, NULL, 0),
348         SND_SOC_DAPM_PGA("RAMP", ES8323_ADCCONTROL1, 0, 0, NULL, 0),
349
350         SND_SOC_DAPM_OUTPUT("LOUT1"),
351         SND_SOC_DAPM_OUTPUT("ROUT1"),
352         SND_SOC_DAPM_OUTPUT("LOUT2"),
353         SND_SOC_DAPM_OUTPUT("ROUT2"),
354         SND_SOC_DAPM_OUTPUT("VREF"),
355
356         SND_SOC_DAPM_PRE("PRE", es8323_dapm_pre_event), 
357   SND_SOC_DAPM_POST("POST", es8323_dapm_post_event),
358 #endif
359 };
360
361 static const struct snd_soc_dapm_route audio_map[] = {
362
363         { "Left Line Mux", "NULL", "LINPUT1" },
364         { "Left Line Mux", "NULL", "LINPUT2" },
365         { "Left Line Mux", "NULL", "Left PGA Mux" },
366         
367         { "Right Line Mux", "NULL", "RINPUT1" },
368         { "Right Line Mux", "NULL", "RINPUT2" },
369         { "Right Line Mux", "NULL", "Right PGA Mux" },  
370
371         { "Left PGA Mux", "LAMP", "LINPUT1" },
372         { "Left PGA Mux", "LAMP", "LINPUT2" },
373         { "Left PGA Mux", "LAMP", "Differential Mux" },
374
375         { "Right PGA Mux", "RAMP", "RINPUT1" },
376         { "Right PGA Mux", "RAMP", "RINPUT2" },
377         { "Right PGA Mux", "RAMP", "Differential Mux" },
378
379         { "Differential Mux", "LAMP", "LINPUT1" },
380         { "Differential Mux", "RAMP", "RINPUT1" },
381         { "Differential Mux", "LAMP", "LINPUT2" },
382         { "Differential Mux", "RAMP", "RINPUT2" },
383
384         { "Left ADC Mux", "Stereo", "Left PGA Mux" },
385         { "Left ADC Mux", "Mono (Left)", "Left PGA Mux" },
386         //{ "Left ADC Mux", "Digital Mono", "Left PGA Mux" },
387
388         { "Right ADC Mux", "Stereo", "Right PGA Mux" },
389         { "Right ADC Mux", "Mono (Right)", "Right PGA Mux" },
390         //{ "Right ADC Mux", "Digital Mono", "Right PGA Mux" },
391
392         { "Left ADC", NULL, "Left ADC Mux" },
393         { "Right ADC", NULL, "Right ADC Mux" },
394
395         { "Left Line Mux", "LAMP", "LINPUT1" },
396         { "Left Line Mux", "LAMP", "LINPUT2" },
397         { "Left Line Mux", "LAMP", "Left PGA Mux" },
398
399         { "Right Line Mux", "RAMP", "RINPUT1" },
400         { "Right Line Mux", "RAMP", "RINPUT2" },
401         { "Right Line Mux", "RAMP", "Right PGA Mux" },  
402
403         { "Left Mixer", "Left Playback Switch", "Left DAC" },
404         { "Left Mixer", "Left Bypass Switch", "Left Line Mux" },
405
406         { "Right Mixer", "Right Playback Switch", "Right DAC" },
407         { "Right Mixer", "Right Bypass Switch", "Right Line Mux" },
408
409         { "Left Out 1", NULL, "Left Mixer" },
410         { "LOUT1", NULL, "Left Out 1" },
411         { "Right Out 1", NULL, "Right Mixer" },
412         { "ROUT1", NULL, "Right Out 1" },
413
414         { "Left Out 2", NULL, "Left Mixer" },
415         { "LOUT2", NULL, "Left Out 2" },
416         { "Right Out 2", NULL, "Right Mixer" },
417         { "ROUT2", NULL, "Right Out 2" },
418 };
419
420 int es8323_dapm_pre_event(struct snd_soc_dapm_widget* widget, struct snd_kcontrol * null, int event)
421 {
422 //      printk("fun:%s, event:%d\r\n", __FUNCTION__, event);
423         if (event==1)
424         { 
425                 widget->dapm->dev_power = 1;
426                 es8323_set_bias_level(widget->codec, SND_SOC_BIAS_PREPARE);
427                 }               
428         return 0;
429 }
430 int es8323_dapm_post_event(struct snd_soc_dapm_widget* widget, struct snd_kcontrol * null, int event)
431 {
432 //      printk("fun:%s, event:%d\r\n", __FUNCTION__, event);
433         if (event==8)
434         {
435                 widget->dapm->dev_power = 0;
436                 es8323_set_bias_level(widget->codec, SND_SOC_BIAS_STANDBY);
437         }
438         return 0;
439 }
440
441 struct _coeff_div {
442         u32 mclk;
443         u32 rate;
444         u16 fs;
445         u8 sr:4;
446         u8 usb:1;
447 };
448
449 /* codec hifi mclk clock divider coefficients */
450 static const struct _coeff_div coeff_div[] = {
451         /* 8k */
452         {12288000, 8000, 1536, 0xa, 0x0},
453         {11289600, 8000, 1408, 0x9, 0x0},
454         {18432000, 8000, 2304, 0xc, 0x0},
455         {16934400, 8000, 2112, 0xb, 0x0},
456         {12000000, 8000, 1500, 0xb, 0x1},
457
458         /* 11.025k */
459         {11289600, 11025, 1024, 0x7, 0x0},
460         {16934400, 11025, 1536, 0xa, 0x0},
461         {12000000, 11025, 1088, 0x9, 0x1},
462
463         /* 16k */
464         {12288000, 16000, 768, 0x6, 0x0},
465         {18432000, 16000, 1152, 0x8, 0x0},
466         {12000000, 16000, 750, 0x7, 0x1},
467
468         /* 22.05k */
469         {11289600, 22050, 512, 0x4, 0x0},
470         {16934400, 22050, 768, 0x6, 0x0},
471         {12000000, 22050, 544, 0x6, 0x1},
472
473         /* 32k */
474         {12288000, 32000, 384, 0x3, 0x0},
475         {18432000, 32000, 576, 0x5, 0x0},
476         {12000000, 32000, 375, 0x4, 0x1},
477
478         /* 44.1k */
479         {11289600, 44100, 256, 0x2, 0x0},
480         {16934400, 44100, 384, 0x3, 0x0},
481         {12000000, 44100, 272, 0x3, 0x1},
482
483         /* 48k */
484         {12288000, 48000, 256, 0x2, 0x0},
485         {18432000, 48000, 384, 0x3, 0x0},
486         {12000000, 48000, 250, 0x2, 0x1},
487
488         /* 88.2k */
489         {11289600, 88200, 128, 0x0, 0x0},
490         {16934400, 88200, 192, 0x1, 0x0},
491         {12000000, 88200, 136, 0x1, 0x1},
492
493         /* 96k */
494         {12288000, 96000, 128, 0x0, 0x0},
495         {18432000, 96000, 192, 0x1, 0x0},
496         {12000000, 96000, 125, 0x0, 0x1},
497 };
498
499 static inline int get_coeff(int mclk, int rate)
500 {
501         int i;
502
503         for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
504                 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
505                         return i;
506         }
507
508         return -EINVAL;
509 }
510
511 /* The set of rates we can generate from the above for each SYSCLK */
512
513 static unsigned int rates_12288[] = {
514         8000, 12000, 16000, 24000, 24000, 32000, 48000, 96000,
515 };
516
517 static struct snd_pcm_hw_constraint_list constraints_12288 = {
518         .count  = ARRAY_SIZE(rates_12288),
519         .list   = rates_12288,
520 };
521
522 static unsigned int rates_112896[] = {
523         8000, 11025, 22050, 44100,
524 };
525
526 static struct snd_pcm_hw_constraint_list constraints_112896 = {
527         .count  = ARRAY_SIZE(rates_112896),
528         .list   = rates_112896,
529 };
530
531 static unsigned int rates_12[] = {
532         8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000,
533         48000, 88235, 96000,
534 };
535
536 static struct snd_pcm_hw_constraint_list constraints_12 = {
537         .count  = ARRAY_SIZE(rates_12),
538         .list   = rates_12,
539 };
540
541 static void on_off_ext_amp(int i)
542 {
543     // struct snd_soc_codec *codec;
544     if (set_spk == 0) {
545             return;
546     }
547    if(hp_irq_flag == 0) 
548          gpio_set_value(SPK_CON, i);  //delete by hjc
549
550     DBG("*** %s() SPEAKER set SPK_CON %d\n", __FUNCTION__, i);
551     mdelay(50);
552     #ifdef SPK_CTL
553     //gpio_direction_output(SPK_CTL, GPIO_LOW);
554     gpio_set_value(SPK_CTL, i);
555     DBG("*** %s() SPEAKER set as %d\n", __FUNCTION__, i);
556     #endif
557     #ifdef EAR_CON_PIN
558     //gpio_direction_output(EAR_CON_PIN, GPIO_LOW);
559     gpio_set_value(EAR_CON_PIN, i);
560     DBG("*** %s() HEADPHONE set as %d\n", __FUNCTION__, i);
561     mdelay(50);
562     #endif
563 }
564
565 #if 0
566 static void es8323_codec_set_spk(bool on)
567 {
568         on_off_ext_amp(on);
569 }
570 #endif
571
572
573
574 /*
575  * Note that this should be called from init rather than from hw_params.
576  */
577 static int es8323_set_dai_sysclk(struct snd_soc_dai *codec_dai,
578                 int clk_id, unsigned int freq, int dir)
579 {
580         struct snd_soc_codec *codec = codec_dai->codec;
581         struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
582
583     DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
584                 
585         switch (freq) {
586         case 11289600:
587         case 18432000:
588         case 22579200:
589         case 36864000:
590                 es8323->sysclk_constraints = &constraints_112896;
591                 es8323->sysclk = freq;
592                 return 0;
593
594         case 12288000:
595         case 16934400:
596         case 24576000:
597         case 33868800:
598                 es8323->sysclk_constraints = &constraints_12288;
599                 es8323->sysclk = freq;
600                 return 0;
601
602         case 12000000:
603         case 24000000:
604                 es8323->sysclk_constraints = &constraints_12;
605                 es8323->sysclk = freq;
606                 return 0;
607         }
608         return -EINVAL;
609 }
610
611 static int es8323_set_dai_fmt(struct snd_soc_dai *codec_dai,
612                 unsigned int fmt)
613 {
614   struct snd_soc_codec *codec = codec_dai->codec;
615     u8 iface = 0;
616     u8 adciface = 0;
617     u8 daciface = 0;
618     alsa_dbg("%s----%d, fmt[%02x]\n",__FUNCTION__,__LINE__,fmt);
619
620     iface    = snd_soc_read(codec, ES8323_IFACE);
621     adciface = snd_soc_read(codec, ES8323_ADC_IFACE);
622     daciface = snd_soc_read(codec, ES8323_DAC_IFACE);
623
624     /* set master/slave audio interface */
625     switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
626         case SND_SOC_DAIFMT_CBM_CFM:    // MASTER MODE
627                   alsa_dbg("es8323 in master mode");
628             iface |= 0x80;
629             break;
630         case SND_SOC_DAIFMT_CBS_CFS:    // SLAVE MODE
631                   alsa_dbg("es8323 in slave mode");
632             iface &= 0x7F;
633             break;
634         default:
635             return -EINVAL;
636     }
637
638
639     /* interface format */
640     switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
641         case SND_SOC_DAIFMT_I2S:
642             adciface &= 0xFC;
643             //daciface &= 0xF9;  //updated by david-everest,5-25           
644             daciface &= 0xF9;
645             break;
646         case SND_SOC_DAIFMT_RIGHT_J:
647             break;
648         case SND_SOC_DAIFMT_LEFT_J:
649             break;
650         case SND_SOC_DAIFMT_DSP_A:
651             break;
652         case SND_SOC_DAIFMT_DSP_B:
653             break;
654         default:
655             return -EINVAL;
656     }
657
658     /* clock inversion */
659     switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
660         case SND_SOC_DAIFMT_NB_NF:
661             iface    &= 0xDF;
662             adciface &= 0xDF;
663             //daciface &= 0xDF;    //UPDATED BY david-everest,5-25        
664             daciface &= 0xBF;
665             break;
666         case SND_SOC_DAIFMT_IB_IF:
667             iface    |= 0x20;
668             //adciface &= 0xDF;    //UPDATED BY david-everest,5-25
669             adciface |= 0x20;
670             //daciface &= 0xDF;   //UPDATED BY david-everest,5-25
671             daciface |= 0x40;
672             break;
673         case SND_SOC_DAIFMT_IB_NF:
674             iface    |= 0x20;
675            // adciface |= 0x40;  //UPDATED BY david-everest,5-25
676             adciface &= 0xDF;
677             //daciface |= 0x40;  //UPDATED BY david-everest,5-25
678             daciface &= 0xBF;
679             break;
680         case SND_SOC_DAIFMT_NB_IF:
681             iface    &= 0xDF;
682             adciface |= 0x20;
683             //daciface |= 0x20;  //UPDATED BY david-everest,5-25
684             daciface |= 0x40;
685             break;
686         default:
687             return -EINVAL;
688     }
689
690     snd_soc_write(codec, ES8323_IFACE    , iface);
691     snd_soc_write(codec, ES8323_ADC_IFACE, adciface);
692     snd_soc_write(codec, ES8323_DAC_IFACE, daciface);
693
694     return 0;
695 }
696
697 static int es8323_pcm_startup(struct snd_pcm_substream *substream,
698                               struct snd_soc_dai *dai)
699 {
700         struct snd_soc_codec *codec = dai->codec;
701         struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
702         // u16 i;
703         if (!es8323->is_startup) {
704                 es8323->is_startup = 1;
705                 //on_off_ext_amp(0);
706     /*
707                 snd_soc_write(codec, ES8323_CONTROL1, 0x06);
708                 snd_soc_write(codec, ES8323_CONTROL2, 0x72);
709                 snd_soc_write(codec, ES8323_DACPOWER, 0x00);
710                 mdelay(30);
711           //snd_soc_write(codec, ES8323_CHIPPOWER, 0xf3);
712                 snd_soc_write(codec, ES8323_DACCONTROL21, 0x80);
713                 */
714                 snd_soc_write(codec, ES8323_ADCPOWER, 0x59);
715                 snd_soc_write(codec, ES8323_DACPOWER, 0x3c);
716                 snd_soc_write(codec, ES8323_CHIPPOWER, 0x00);
717                 //on_off_ext_amp(1);
718         }
719
720         DBG("Enter::%s----%d  es8323->sysclk=%d\n",__FUNCTION__,__LINE__,es8323->sysclk);
721
722         /* The set of sample rates that can be supported depends on the
723          * MCLK supplied to the CODEC - enforce this.
724          */
725         if (!es8323->sysclk) {
726                 dev_err(codec->dev,
727                         "No MCLK configured, call set_sysclk() on init\n");
728                 return -EINVAL;
729         }
730
731         snd_pcm_hw_constraint_list(substream->runtime, 0,
732                                    SNDRV_PCM_HW_PARAM_RATE,
733                                    es8323->sysclk_constraints);
734
735         return 0;
736 }
737
738 static int es8323_pcm_hw_params(struct snd_pcm_substream *substream,
739                                 struct snd_pcm_hw_params *params,
740                                 struct snd_soc_dai *dai)
741
742
743         static int codecfirstuse=0;
744         struct snd_soc_pcm_runtime *rtd = substream->private_data;
745         struct snd_soc_codec *codec = rtd->codec;
746         struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
747         //u16 iface = snd_soc_read(codec, es8323_IFACE) & 0x1f3;
748         //u16 srate = snd_soc_read(codec, es8323_SRATE) & 0x180;
749         
750         u16 srate    = snd_soc_read(codec, ES8323_IFACE) & 0x80;
751         u16 adciface = snd_soc_read(codec, ES8323_ADC_IFACE) & 0xE3;
752         u16 daciface = snd_soc_read(codec, ES8323_DAC_IFACE) & 0xC7;
753         
754         int coeff;
755
756         coeff = get_coeff(es8323->sysclk, params_rate(params));
757         if (coeff < 0) {
758                 coeff = get_coeff(es8323->sysclk / 2, params_rate(params));
759                 srate |= 0x40;
760         }
761         if (coeff < 0) {
762                 dev_err(codec->dev,
763                         "Unable to configure sample rate %dHz with %dHz MCLK\n",
764                         params_rate(params), es8323->sysclk);
765                 return coeff;
766         }
767
768         /* bit size */
769  switch (params_format(params)) {
770   case SNDRV_PCM_FORMAT_S16_LE:
771       adciface |= 0x000C;
772       daciface |= 0x0018;
773       break;
774   case SNDRV_PCM_FORMAT_S20_3LE:
775       adciface |= 0x0004;
776       daciface |= 0x0008;
777       break;
778   case SNDRV_PCM_FORMAT_S24_LE:
779       break;
780   case SNDRV_PCM_FORMAT_S32_LE:
781       adciface |= 0x0010;
782       daciface |= 0x0020;
783       break;
784   }
785
786   /* set iface & srate*/
787   snd_soc_write(codec, ES8323_DAC_IFACE, daciface); //dac bits length
788   snd_soc_write(codec, ES8323_ADC_IFACE, adciface); //adc bits length
789
790         if (coeff >= 0)
791                 {
792                  snd_soc_write(codec, ES8323_IFACE, srate);  //bclk div,mclkdiv2
793                  snd_soc_write(codec, ES8323_ADCCONTROL5, coeff_div[coeff].sr | (coeff_div[coeff].usb) << 4);
794                  snd_soc_write(codec, ES8323_DACCONTROL2, coeff_div[coeff].sr | (coeff_div[coeff].usb) << 4);
795                 }
796         if (codecfirstuse == 0)
797                 {
798                         snd_soc_write(codec, ES8323_LOUT2_VOL, es8323_DEF_VOL);//0x1c);   // 
799                   snd_soc_write(codec, ES8323_ROUT2_VOL, es8323_DEF_VOL);//0x1c);   // 
800                   codecfirstuse=1;
801                         }
802
803         return 0;
804 }
805
806 static int es8323_mute(struct snd_soc_dai *dai, int mute)
807 {
808         struct snd_soc_codec *codec = dai->codec;
809         // u16 mute_reg = snd_soc_read(codec, ES8323_DACCONTROL3) & 0xfb;
810
811         DBG("Enter::%s----%d--mute=%d\n",__FUNCTION__,__LINE__,mute);
812
813         if (mute)
814                 //snd_soc_write(codec, ES8323_DACCONTROL3, mute_reg | 0x4);
815          {
816   snd_soc_write(codec, ES8323_DACCONTROL3, 0x06);//0xe6);
817
818          }
819         else
820         {
821
822         snd_soc_write(codec, ES8323_DACCONTROL3, 0x02);//0xe2);
823                 snd_soc_write(codec, 0x30,0x1e);
824                 snd_soc_write(codec, 0x31,0x1e);
825          
826
827         }
828     on_off_ext_amp(!mute);
829     
830         return 0;
831 }
832
833 /////////////////////////////////////////////////////////////////
834 static int es8323_set_bias_level(struct snd_soc_codec *codec,
835                                  enum snd_soc_bias_level level)
836 {
837         struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
838         // u16 OUT_VOL = snd_soc_read(codec, ES8323_LOUT1_VOL);
839         // u16 i;
840         
841         DBG("Enter::%s----%d level =%d\n",__FUNCTION__,__LINE__,level);
842         switch (level) {
843         case SND_SOC_BIAS_ON:
844                 es8323->is_biason = 1;
845                 break;
846         case SND_SOC_BIAS_PREPARE:
847         snd_soc_write(codec, ES8323_ANAVOLMANAG, 0x7C);
848         snd_soc_write(codec, ES8323_CHIPLOPOW1, 0x00);
849         snd_soc_write(codec, ES8323_CHIPLOPOW2, 0xFF);                                                  
850                 snd_soc_write(codec, ES8323_CHIPPOWER, 0x00);   
851                 snd_soc_write(codec, ES8323_ADCPOWER, 0x59);
852                 break;
853         case SND_SOC_BIAS_STANDBY:              
854         snd_soc_write(codec, ES8323_ANAVOLMANAG, 0x7C);
855         snd_soc_write(codec, ES8323_CHIPLOPOW1, 0x00);
856         snd_soc_write(codec, ES8323_CHIPLOPOW2, 0xFF);                                                  
857                 snd_soc_write(codec, ES8323_CHIPPOWER, 0x00);   
858                 snd_soc_write(codec, ES8323_ADCPOWER, 0x59);
859                 break;
860         case SND_SOC_BIAS_OFF:  
861                 snd_soc_write(codec, ES8323_ANAVOLMANAG, 0x7B);
862         snd_soc_write(codec, ES8323_CHIPLOPOW1, 0xFF);
863         snd_soc_write(codec, ES8323_CHIPLOPOW2, 0xFF);          
864                 snd_soc_write(codec, ES8323_ADCPOWER, 0xFF);                                    
865         snd_soc_write(codec, ES8323_CHIPPOWER, 0xAA);
866
867         //snd_soc_write(codec, 0x2b, 0x90);
868                 break;
869         }
870         codec->dapm.bias_level = level;
871         return 0;
872 }
873
874
875
876 #define es8323_RATES SNDRV_PCM_RATE_8000_96000
877
878 #define es8323_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
879         SNDRV_PCM_FMTBIT_S24_LE)
880
881 static struct snd_soc_dai_ops es8323_ops = {
882         .startup = es8323_pcm_startup,
883         .hw_params = es8323_pcm_hw_params,
884         .set_fmt = es8323_set_dai_fmt,
885         .set_sysclk = es8323_set_dai_sysclk,
886         .digital_mute = es8323_mute,
887 };
888
889 static struct snd_soc_dai_driver es8323_dai = {
890         .name = "ES8323 HiFi",
891         .playback = {
892                 .stream_name = "Playback",
893                 .channels_min = 1,
894                 .channels_max = 2,
895                 .rates = es8323_RATES,
896                 .formats = es8323_FORMATS,
897         },
898         .capture = {
899                 .stream_name = "Capture",
900                 .channels_min = 1,
901                 .channels_max = 2,
902                 .rates = es8323_RATES,
903                 .formats = es8323_FORMATS,
904          },
905         .ops = &es8323_ops,
906         .symmetric_rates = 1,
907 };
908
909 static int es8323_suspend(struct snd_soc_codec *codec, pm_message_t state)
910 {
911         // u16 i;
912         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
913
914 #if 0
915         snd_soc_write(codec, 0x19, 0x06);
916         snd_soc_write(codec, 0x07, 0x7B);
917         snd_soc_write(codec, 0x06, 0xFF);
918         snd_soc_write(codec, 0x05, 0xFF);
919 #endif
920
921         snd_soc_write(codec, 0x19, 0x06);
922         snd_soc_write(codec, 0x30, 0x00);
923         snd_soc_write(codec, 0x31, 0x00);
924                                 snd_soc_write(codec, ES8323_ADCPOWER, 0xFF);                                    
925                                 snd_soc_write(codec, ES8323_DACPOWER, 0xc0);    
926                                 snd_soc_write(codec, ES8323_CHIPPOWER, 0xF3);
927                                 snd_soc_write(codec, 0x00, 0x00);
928                                 snd_soc_write(codec, 0x01, 0x58);
929                                 snd_soc_write(codec, 0x2b, 0x9c);       
930                                 msleep(50);
931                                 gpio_set_value(SPK_CON, 0);
932                                 return 0;
933 }
934
935 static int es8323_resume(struct snd_soc_codec *codec)
936 {
937         // u16 i;
938         // u8 data[2];
939         // u16 *cache = codec->reg_cache;       
940         snd_soc_write(codec, 0x2b, 0x80);       
941   snd_soc_write(codec, 0x01, 0x50);
942   snd_soc_write(codec, 0x00, 0x32);
943         snd_soc_write(codec, ES8323_CHIPPOWER, 0x00);   
944         snd_soc_write(codec, ES8323_DACPOWER, 0x0c);    
945         snd_soc_write(codec, ES8323_ADCPOWER, 0x59);
946   snd_soc_write(codec, 0x31, es8323_DEF_VOL);
947   snd_soc_write(codec, 0x30, es8323_DEF_VOL);
948         snd_soc_write(codec, 0x19, 0x02);                       
949         gpio_set_value(SPK_CON, 1);
950         return 0;
951 }
952
953 static u32 cur_reg=0;
954 static struct snd_soc_codec *es8323_codec;
955 static int entry_read(char *page, char **start, off_t off,
956                 int count, int *eof, void *data)
957 {
958         int len;
959
960         snd_soc_write(es8323_codec, ES8323_ADCPOWER, 0xff);
961         snd_soc_write(es8323_codec, ES8323_DACPOWER, 0xf0);
962         snd_soc_write(es8323_codec, ES8323_DACPOWER, 0xc0);
963         snd_soc_write(es8323_codec, ES8323_CHIPPOWER, 0xf3);
964
965         len = sprintf(page, "es8323 suspend...\n");
966
967         return len ;
968 }
969
970 #if (RT5633_SPK_TIMER == 1)
971 static void spk_work_handler(struct work_struct *work)
972 {
973         //if(!gpio_get_value(ES8323_HP_PIN)){
974                 //gpio_direction_output(SPK_CON,0);
975  //               gpio_set_value(SPK_CON, 0);
976 //      }else{
977                 //gpio_direction_output(SPK_CON,1);     
978  //               gpio_set_value(SPK_CON, 1);
979 //      }
980 }
981 void spk_timer_callback(unsigned long data )
982 {       
983         int ret = 0;
984         schedule_work(&spk_work);
985   ret = mod_timer(&spk_timer, jiffies + msecs_to_jiffies(1000));
986   if (ret) printk("Error in mod_timer\n");
987 }
988 #endif
989
990 static int es8323_probe(struct snd_soc_codec *codec)
991 {
992         // struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
993         struct snd_soc_dapm_context *dapm = &codec->dapm;
994         int ret = 0;
995         unsigned long flags=0;
996         // u16 reg,i;
997
998     printk("%s\n", __func__);
999 #if 0
1000     ret = gpio_request(RK30_PIN0_PC7, NULL);
1001     if (ret != 0) {
1002         printk("%s request RK30_PIN0_PC7 error", __func__);
1003         return ret;
1004     }
1005     gpio_direction_input(RK30_PIN0_PC7);
1006 #endif
1007     ret = gpio_request(SPK_CON, NULL);
1008     if (ret != 0) {
1009             printk("%s request SPK_CON error", __func__);
1010             return ret;
1011     }
1012     //gpio_set_value(SPK_CON, 1);
1013     gpio_direction_output(SPK_CON,0);
1014
1015
1016                 ret = gpio_request(HP_DET, NULL);
1017                 if (ret != 0) {
1018                                 printk("%s request HP_DET error", __func__);
1019                                 return ret;
1020                 }
1021                 gpio_direction_input(HP_DET);
1022                 
1023                 flags = gpio_get_value(HP_DET) ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1024                 
1025                 HP_IRQ = gpio_to_irq(HP_DET);
1026     if (HP_IRQ){
1027         ret = request_irq(HP_IRQ, hp_det_irq_handler, flags, "ES8323", NULL);
1028         if(ret == 0){
1029             printk("%s:register ISR (irq=%d)\n", __FUNCTION__,HP_IRQ);
1030         }
1031         else 
1032                         printk("request_irq HP_IRQ failed\n");
1033     }
1034     
1035         if (codec == NULL) {
1036                 dev_err(codec->dev, "Codec device not registered\n");
1037                 return -ENODEV;
1038         }
1039     codec->read  = es8323_read_reg_cache;
1040     codec->write = es8323_write;
1041     codec->hw_write = (hw_write_t)i2c_master_send;
1042         codec->control_data = container_of(codec->dev, struct i2c_client, dev);
1043
1044         es8323_codec = codec;
1045         ret = es8323_reset(codec);
1046         if (ret < 0) {
1047                 dev_err(codec->dev, "Failed to issue reset\n");
1048                 return ret;
1049         }
1050         #if (RT5633_SPK_TIMER == 1)
1051         setup_timer( &spk_timer, spk_timer_callback, 0 );
1052         ret = mod_timer( &spk_timer, jiffies + msecs_to_jiffies(5000) );
1053         if (ret)
1054                 printk("Error in mod_timer\n");
1055         INIT_WORK(&spk_work, spk_work_handler);
1056         es8323_ANVOL=1;
1057 #endif
1058         
1059   //es8323_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1060   
1061 #if 1   
1062     //snd_soc_write(codec, 0x35  , 0xa0); 
1063     //snd_soc_write(codec, 0x36  , 0x08); //for 1.8V VDD
1064 snd_soc_write(codec, 0x02,0xf3);
1065 snd_soc_write(codec, 0x2B,0x80);
1066 snd_soc_write(codec, 0x08,0x00);   //ES8388 salve  
1067 snd_soc_write(codec, 0x00,0x32);   //
1068 snd_soc_write(codec, 0x01,0x72);   //PLAYBACK & RECORD Mode,EnRefr=1
1069 snd_soc_write(codec, 0x03,0x59);   //pdn_ana=0,ibiasgen_pdn=0
1070 snd_soc_write(codec, 0x05,0x00);   //pdn_ana=0,ibiasgen_pdn=0
1071 snd_soc_write(codec, 0x06,0xc3);   //pdn_ana=0,ibiasgen_pdn=0 
1072 snd_soc_write(codec, 0x09,0x88);  //ADC L/R PGA =  +24dB
1073 //----------------------------------------------------------------------------------------------------------------
1074 snd_soc_write(codec, 0x0a,0xf0);  //ADC INPUT=LIN2/RIN2
1075 snd_soc_write(codec, 0x0b,0x82);  //ADC INPUT=LIN2/RIN2 //82
1076 //-----------------------------------------------------------------------------------------------------------------
1077 snd_soc_write(codec, 0x0C,0x4c);  //I2S-24BIT
1078 snd_soc_write(codec, 0x0d,0x02);  //MCLK/LRCK=256 
1079 snd_soc_write(codec, 0x10,0x00);  //ADC Left Volume=0db
1080 snd_soc_write(codec, 0x11,0x00);  //ADC Right Volume=0db
1081 snd_soc_write(codec, 0x12,0xea); // ALC stereo MAXGAIN: 35.5dB,  MINGAIN: +6dB (Record Volume increased!)
1082 snd_soc_write(codec, 0x13,0xc0);
1083 snd_soc_write(codec, 0x14,0x05);
1084 snd_soc_write(codec, 0x15,0x06);
1085 snd_soc_write(codec, 0x16,0x53);  
1086 snd_soc_write(codec, 0x17,0x18);  //I2S-16BIT
1087 snd_soc_write(codec, 0x18,0x02);
1088 snd_soc_write(codec, 0x1A,0x00);  //DAC VOLUME=0DB
1089 snd_soc_write(codec, 0x1B,0x00);
1090                 /*
1091                 snd_soc_write(codec, 0x1E,0x01);    //for 47uF capacitors ,15db Bass@90Hz,Fs=44100
1092                 snd_soc_write(codec, 0x1F,0x84);
1093                 snd_soc_write(codec, 0x20,0xED);
1094                 snd_soc_write(codec, 0x21,0xAF);
1095                 snd_soc_write(codec, 0x22,0x20);
1096                 snd_soc_write(codec, 0x23,0x6C);
1097                 snd_soc_write(codec, 0x24,0xE9);
1098                 snd_soc_write(codec, 0x25,0xBE);
1099                 */
1100 snd_soc_write(codec, 0x26,0x12);  //Left DAC TO Left IXER
1101 snd_soc_write(codec, 0x27,0xb8);  //Left DAC TO Left MIXER
1102 snd_soc_write(codec, 0x28,0x38);
1103 snd_soc_write(codec, 0x29,0x38);
1104 snd_soc_write(codec, 0x2A,0xb8);
1105 snd_soc_write(codec, 0x02,0x00); //aa //START DLL and state-machine,START DSM 
1106 snd_soc_write(codec, 0x19,0x02);  //SOFT RAMP RATE=32LRCKS/STEP,Enable ZERO-CROSS CHECK,DAC MUTE
1107 snd_soc_write(codec, 0x04,0x0c);   //pdn_ana=0,ibiasgen_pdn=0  
1108 msleep(100);
1109 snd_soc_write(codec, 0x2e,0x00); 
1110 snd_soc_write(codec, 0x2f,0x00);
1111 snd_soc_write(codec, 0x30,0x08); 
1112 snd_soc_write(codec, 0x31,0x08);
1113 msleep(200);
1114 snd_soc_write(codec, 0x30,0x0f); 
1115 snd_soc_write(codec, 0x31,0x0f);
1116 msleep(200);
1117 snd_soc_write(codec, 0x30,0x18); 
1118 snd_soc_write(codec, 0x31,0x18);
1119 msleep(100);
1120 snd_soc_write(codec, 0x04,0x2c);   //pdn_ana=0,ibiasgen_pdn=0 
1121 #endif  
1122         
1123   //s8323_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1124   //codec->dapm.bias_level = SND_SOC_BIAS_STANDBY;
1125       
1126         snd_soc_add_controls(codec, es8323_snd_controls,
1127                                 ARRAY_SIZE(es8323_snd_controls));
1128         snd_soc_dapm_new_controls(dapm, es8323_dapm_widgets,
1129                                   ARRAY_SIZE(es8323_dapm_widgets));
1130         snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
1131           
1132         create_proc_read_entry("es8323_suspend", 0644, NULL, entry_read, NULL);
1133
1134         return 0;
1135 }
1136
1137 static int es8323_remove(struct snd_soc_codec *codec)
1138 {
1139         es8323_set_bias_level(codec, SND_SOC_BIAS_OFF);
1140         return 0;
1141 }
1142
1143 void codec_set_spk(bool on)
1144 {
1145         DBG("Enter::%s----%d--, on = %d\n",__FUNCTION__,__LINE__, on);
1146
1147         set_spk = on;
1148         gpio_set_value(SPK_CON, on);
1149         //return;
1150 }
1151
1152 static struct snd_soc_codec_driver soc_codec_dev_es8323 = {
1153         .probe =        es8323_probe,
1154         .remove =       es8323_remove,
1155         .suspend =      es8323_suspend,
1156         .resume =       es8323_resume,
1157         .set_bias_level = es8323_set_bias_level,
1158         .reg_cache_size = ARRAY_SIZE(es8323_reg),
1159         .reg_word_size = sizeof(u16),
1160         .reg_cache_default = es8323_reg,
1161         //------------------------------------------
1162         //.volatile_register = es8323_volatile_register,
1163         //.readable_register = es8323_readable_register,
1164         .reg_cache_step = 1,
1165 #if 0
1166         .controls = es8323_snd_controls,
1167         .num_controls = ARRAY_SIZE(es8323_snd_controls),        
1168   .dapm_routes = audio_map,  
1169   .num_dapm_routes = ARRAY_SIZE(audio_map), 
1170   .dapm_widgets = es8323_dapm_widgets,  
1171   .num_dapm_widgets = ARRAY_SIZE(es8323_dapm_widgets),   
1172         
1173         //--------------------------------------------------    
1174         .read   = es8323_read_reg_cache,
1175         .write = es8323_write,  
1176 #endif
1177 };
1178
1179 #if defined(CONFIG_SPI_MASTER)
1180 static int __devinit es8323_spi_probe(struct spi_device *spi)
1181 {
1182         struct es8323_priv *es8323;
1183         int ret;
1184
1185         es8323 = kzalloc(sizeof(struct es8323_priv), GFP_KERNEL);
1186         if (es8323 == NULL)
1187                 return -ENOMEM;
1188
1189         es8323->control_type = SND_SOC_SPI;
1190         spi_set_drvdata(spi, es8323);
1191
1192         ret = snd_soc_register_codec(&spi->dev,
1193                         &soc_codec_dev_es8323, &es8323_dai, 1);
1194         if (ret < 0)
1195                 kfree(es8323);
1196         return ret;
1197 }
1198
1199 static int __devexit es8323_spi_remove(struct spi_device *spi)
1200 {
1201         snd_soc_unregister_codec(&spi->dev);
1202         kfree(spi_get_drvdata(spi));
1203         return 0;
1204 }
1205
1206 static struct spi_driver es8323_spi_driver = {
1207         .driver = {
1208                 .name   = "ES8323",
1209                 .owner  = THIS_MODULE,
1210         },
1211         .probe          = es8323_spi_probe,
1212         .remove         = __devexit_p(es8323_spi_remove),
1213 };
1214 #endif /* CONFIG_SPI_MASTER */
1215
1216 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1217 static ssize_t es8323_show(struct device *dev, struct device_attribute *attr, char *_buf)
1218 {
1219         return sprintf(_buf, "%s(): get 0x%04x=0x%04x\n", __FUNCTION__, cur_reg, 
1220                 snd_soc_read(es8323_codec, cur_reg));
1221 }
1222
1223 static u32 strtol(const char *nptr, int base)
1224 {
1225         u32 ret;
1226         if(!nptr || (base!=16 && base!=10 && base!=8))
1227         {
1228
1229                 printk("%s(): NULL pointer input\n", __FUNCTION__);
1230                 return -1;
1231         }
1232         for(ret=0; *nptr; nptr++)
1233         {
1234
1235
1236                 if((base==16 && *nptr>='A' && *nptr<='F') || 
1237                         (base==16 && *nptr>='a' && *nptr<='f') || 
1238                         (base>=10 && *nptr>='0' && *nptr<='9') ||
1239                         (base>=8 && *nptr>='0' && *nptr<='7') )
1240                 {
1241                         ret *= base;
1242                         if(base==16 && *nptr>='A' && *nptr<='F')
1243                                 ret += *nptr-'A'+10;
1244                         else if(base==16 && *nptr>='a' && *nptr<='f')
1245                                 ret += *nptr-'a'+10;
1246                         else if(base>=10 && *nptr>='0' && *nptr<='9')
1247                                 ret += *nptr-'0';
1248                         else if(base>=8 && *nptr>='0' && *nptr<='7')
1249                                 ret += *nptr-'0';
1250                 }
1251                 else
1252                         return ret;
1253         }
1254         return ret;
1255 }
1256
1257 static ssize_t es8323_store(struct device *dev,
1258                                         struct device_attribute *attr,
1259                                         const char *_buf, size_t _count)
1260 {
1261         const char * p=_buf;
1262         u32 reg, val;
1263         
1264         if(!strncmp(_buf, "get", strlen("get")))
1265         {
1266                 p+=strlen("get");
1267                 cur_reg=(u32)strtol(p, 16);
1268                 val=snd_soc_read(es8323_codec, cur_reg);
1269                 printk("%s(): get 0x%04x=0x%04x\n", __FUNCTION__, cur_reg, val);
1270         }
1271         else if(!strncmp(_buf, "put", strlen("put")))
1272         {
1273                 p+=strlen("put");
1274                 reg=strtol(p, 16);
1275                 p=strchr(_buf, '=');
1276                 if(p)
1277                 {
1278                         ++ p;
1279                         val=strtol(p, 16);
1280                         snd_soc_write(es8323_codec, reg, val);
1281                         printk("%s(): set 0x%04x=0x%04x\n", __FUNCTION__, reg, val);
1282                 }
1283                 else
1284                         printk("%s(): Bad string format input!\n", __FUNCTION__);
1285         }
1286         else
1287                 printk("%s(): Bad string format input!\n", __FUNCTION__);
1288         
1289         return _count;
1290
1291
1292 static struct device *es8323_dev = NULL;
1293 static struct class *es8323_class = NULL;
1294 static DEVICE_ATTR(es8323, 0664, es8323_show, es8323_store);
1295 static __devinit int es8323_i2c_probe(struct i2c_client *i2c,
1296                                       const struct i2c_device_id *id)
1297 {
1298         
1299         struct es8323_priv *es8323;
1300         int ret = -1;
1301         struct i2c_adapter *adapter = to_i2c_adapter(i2c->dev.parent);
1302         char reg;
1303         char tmp;
1304
1305          if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
1306         dev_warn(&adapter->dev,
1307                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
1308         return -EIO;
1309     }
1310
1311         es8323 = kzalloc(sizeof(struct es8323_priv), GFP_KERNEL);
1312         if (es8323 == NULL)
1313                 return -ENOMEM;
1314
1315         i2c_set_clientdata(i2c, es8323);
1316         es8323->control_type = SND_SOC_I2C;
1317         
1318         reg = ES8323_DACCONTROL18;
1319         ret = i2c_master_reg8_recv(i2c, reg, &tmp, 1 ,200 * 1000);
1320         //ret =i2c_master_reg8_recv(client, 0x00, buf, 2, 200*1000);//i2c_write_bytes(client, &test_data, 1);   //Test I2C connection.
1321         if (ret < 0){
1322                                 printk("es8323 probe error\n");
1323                                 kfree(es8323);
1324                                 return ret;
1325         }
1326         
1327         printk("es8323 probe i2c recv ok\n");
1328
1329         ret =  snd_soc_register_codec(&i2c->dev,
1330                         &soc_codec_dev_es8323, &es8323_dai, 1);
1331         if (ret < 0) {
1332                 kfree(es8323);
1333                 return ret;
1334         }
1335         es8323_class = class_create(THIS_MODULE, "es8323");
1336         if (IS_ERR(es8323_class))
1337         {
1338                 printk("Create class audio_es8323.\n");
1339                 return -ENOMEM;
1340         }
1341         es8323_dev = device_create(es8323_class, NULL, MKDEV(0, 1), NULL, "dev");
1342         ret = device_create_file(es8323_dev, &dev_attr_es8323);
1343         if (ret < 0)
1344                 printk("failed to add dev_attr_es8323 file\n");
1345
1346         return ret;
1347 }
1348
1349 static __devexit int es8323_i2c_remove(struct i2c_client *client)
1350 {
1351         snd_soc_unregister_codec(&client->dev);
1352         kfree(i2c_get_clientdata(client));
1353         return 0;
1354 }
1355
1356 static const struct i2c_device_id es8323_i2c_id[] = {
1357         { "es8323", 0 },
1358         { }
1359 };
1360 MODULE_DEVICE_TABLE(i2c, es8323_i2c_id);
1361
1362 void es8323_i2c_shutdown(struct i2c_client *client)
1363 {
1364         printk("Chenzy-------hkw-------%s\n", __func__);
1365         gpio_direction_output(SPK_CON,0);
1366
1367         snd_soc_write(es8323_codec, ES8323_CONTROL2, 0x58);     
1368         snd_soc_write(es8323_codec, ES8323_CONTROL1, 0x32);                                     
1369         snd_soc_write(es8323_codec, ES8323_CHIPPOWER, 0xf3);
1370         snd_soc_write(es8323_codec, ES8323_DACPOWER, 0xc0);
1371
1372         snd_soc_write(es8323_codec, ES8323_DACCONTROL26, 0x00);
1373         snd_soc_write(es8323_codec, ES8323_DACCONTROL27, 0x00);
1374
1375         snd_soc_write(es8323_codec, ES8323_CONTROL1, 0x30);                                     
1376         snd_soc_write(es8323_codec, ES8323_CONTROL1, 0x34);                                     
1377
1378         mdelay(100);
1379 }
1380 #define  I2C_CLK_NAME  GPIO0B0_I2S8CHCLK_NAME
1381 #define  I2C_CLK_GPIO_MODE  GPIO0B_GPIO0B0
1382 #define  I2C_GPIO_OUTPUT  GPIO_LOW
1383 #define  I2C_CLK_CLK_MODE   GPIO0B_I2S_8CH_CLK
1384 #define  I2C_CLK_GPIO   RK30_PIN0_PB0
1385
1386 #define  I2C_MCLK_NAME  GPIO0B1_I2S8CHSCLK_NAME
1387 #define  I2C_MCLK_GPIO_MODE  GPIO0B_GPIO0B1
1388 #define  I2C_MGPIO_OUTPUT  GPIO_LOW
1389 #define  I2C_MCLK_CLK_MODE   GPIO0B_I2S_8CH_SCLK
1390 #define  I2C_MCLK_GPIO   RK30_PIN0_PB1
1391 static int   es8323_i2c_suspend (struct i2c_client *client, pm_message_t mesg)
1392 {
1393 #if 0
1394         rk30_mux_api_set(I2C_CLK_NAME,I2C_CLK_GPIO_MODE);
1395         if (gpio_request(I2C_CLK_GPIO, NULL)) {
1396                 printk("func %s, line %d: request gpio fail\n", __FUNCTION__, __LINE__);
1397                 return -1;
1398         }
1399
1400         gpio_direction_output(I2C_CLK_GPIO,I2C_GPIO_OUTPUT);
1401
1402         rk30_mux_api_set(I2C_MCLK_NAME,I2C_MCLK_GPIO_MODE);
1403         if (gpio_request(I2C_MCLK_GPIO, NULL)) {
1404                 printk("func %s, line %d: request gpio fail\n", __FUNCTION__, __LINE__);
1405                 return -1;
1406         }
1407
1408         gpio_direction_output(I2C_MCLK_GPIO,I2C_MGPIO_OUTPUT);
1409 #endif
1410         iomux_set(GPIO1_C1);
1411         if (gpio_request(RK30_PIN1_PC1, NULL)) {
1412                 printk("func %s, line %d: request gpio fail\n", __FUNCTION__, __LINE__);
1413                 return -1;
1414         }
1415         gpio_direction_input(RK30_PIN1_PC1);
1416         gpio_pull_updown(RK30_PIN1_PC1, PullDisable);
1417
1418 #if 0
1419         iomux_set(GPIO1_C2);
1420         gpio_direction_input(RK30_PIN1_PC2);
1421         gpio_pull_updown(RK30_PIN1_PC2, PullDisable);
1422
1423         iomux_set(GPIO1_C3);
1424         gpio_direction_input(RK30_PIN1_PC3);
1425         gpio_pull_updown(RK30_PIN1_PC3, PullDisable);
1426
1427         iomux_set(GPIO1_C4);
1428         gpio_direction_input(RK30_PIN1_PC4);
1429         gpio_pull_updown(RK30_PIN1_PC4, PullDisable);
1430
1431         iomux_set(GPIO1_C5);
1432         gpio_direction_input(RK30_PIN1_PC5);
1433         gpio_pull_updown(RK30_PIN1_PC5, PullDisable);
1434 #endif
1435
1436         return 0;
1437 }
1438
1439 static int   es8323_i2c_resume(struct i2c_client *client)
1440 {
1441 #if 0
1442         gpio_free(I2C_MCLK_GPIO);
1443         gpio_free(I2C_CLK_GPIO);
1444
1445         rk30_mux_api_set(I2C_MCLK_NAME,I2C_MCLK_CLK_MODE);
1446         rk30_mux_api_set(I2C_CLK_NAME,I2C_CLK_CLK_MODE);
1447 #endif
1448
1449         gpio_free(RK30_PIN1_PC1);
1450         iomux_set(I2S0_SCLK);
1451
1452         return 0;
1453 }
1454
1455 static struct i2c_driver es8323_i2c_driver = {
1456         .driver = {
1457                 .name = "ES8323",
1458                 .owner = THIS_MODULE,
1459         },
1460         .probe =    es8323_i2c_probe,
1461         .remove =   __devexit_p(es8323_i2c_remove),
1462         .shutdown = es8323_i2c_shutdown,
1463         .suspend  = es8323_i2c_suspend,
1464         .resume = es8323_i2c_resume,
1465         .id_table = es8323_i2c_id,
1466 };
1467 #endif
1468
1469 static int __init es8323_modinit(void)
1470 {
1471         return i2c_add_driver(&es8323_i2c_driver);
1472 }
1473 module_init(es8323_modinit);
1474
1475 static void __exit es8323_exit(void)
1476 {
1477
1478 //      if(0 == tcsi_get_value(TCSI_CODEC_ES8323))
1479 //              return;
1480 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1481         i2c_del_driver(&es8323_i2c_driver);
1482 #endif
1483 #if defined(CONFIG_SPI_MASTER)
1484         spi_unregister_driver(&es8323_spi_driver);
1485 #endif
1486 }
1487 module_exit(es8323_exit);
1488
1489
1490 MODULE_DESCRIPTION("ASoC es8323 driver");
1491 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1492 MODULE_LICENSE("GPL");
1493