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