Merge remote-tracking branch 'origin/upstream/linux-linaro-lsk-v3.10-android+android...
[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 #ifdef CONFIG_MACH_RK_FAC 
66 int es8323_hdmi_ctrl=0;
67 #endif
68 #define SPK_CON                 RK30_PIN2_PD7 //RK30_PIN4_PC5
69 #define HP_DET          RK30_PIN0_PB5
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(HP_DET);
118         unsigned int type;
119
120         //rk28_send_wakeup_key();
121         printk("hjc:%s,irq=%d\n",__func__,irq);
122         type = gpio_get_value(HP_DET) ? 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(HP_DET)){
131                 printk("hp_det = 0,insert hp\n");
132                 gpio_set_value(SPK_CON,0);
133         }else if(1 == gpio_get_value(HP_DET)){
134                 printk("hp_det = 1,deinsert hp\n");
135                 gpio_set_value(SPK_CON,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(SPK_CON, 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 #if 0
569 static void es8323_codec_set_spk(bool on)
570 {
571         on_off_ext_amp(on);
572 }
573 #endif
574
575
576
577 /*
578  * Note that this should be called from init rather than from hw_params.
579  */
580 static int es8323_set_dai_sysclk(struct snd_soc_dai *codec_dai,
581                 int clk_id, unsigned int freq, int dir)
582 {
583         struct snd_soc_codec *codec = codec_dai->codec;
584         struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
585
586     DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
587                 
588         switch (freq) {
589         case 11289600:
590         case 18432000:
591         case 22579200:
592         case 36864000:
593                 es8323->sysclk_constraints = &constraints_112896;
594                 es8323->sysclk = freq;
595                 return 0;
596
597         case 12288000:
598         case 16934400:
599         case 24576000:
600         case 33868800:
601                 es8323->sysclk_constraints = &constraints_12288;
602                 es8323->sysclk = freq;
603                 return 0;
604
605         case 12000000:
606         case 24000000:
607                 es8323->sysclk_constraints = &constraints_12;
608                 es8323->sysclk = freq;
609                 return 0;
610         }
611         return -EINVAL;
612 }
613
614 static int es8323_set_dai_fmt(struct snd_soc_dai *codec_dai,
615                 unsigned int fmt)
616 {
617   struct snd_soc_codec *codec = codec_dai->codec;
618     u8 iface = 0;
619     u8 adciface = 0;
620     u8 daciface = 0;
621     alsa_dbg("%s----%d, fmt[%02x]\n",__FUNCTION__,__LINE__,fmt);
622
623     iface    = snd_soc_read(codec, ES8323_IFACE);
624     adciface = snd_soc_read(codec, ES8323_ADC_IFACE);
625     daciface = snd_soc_read(codec, ES8323_DAC_IFACE);
626
627     /* set master/slave audio interface */
628     switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
629         case SND_SOC_DAIFMT_CBM_CFM:    // MASTER MODE
630                   alsa_dbg("es8323 in master mode");
631             iface |= 0x80;
632             break;
633         case SND_SOC_DAIFMT_CBS_CFS:    // SLAVE MODE
634                   alsa_dbg("es8323 in slave mode");
635             iface &= 0x7F;
636             break;
637         default:
638             return -EINVAL;
639     }
640
641
642     /* interface format */
643     switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
644         case SND_SOC_DAIFMT_I2S:
645             adciface &= 0xFC;
646             //daciface &= 0xF9;  //updated by david-everest,5-25           
647             daciface &= 0xF9;
648             break;
649         case SND_SOC_DAIFMT_RIGHT_J:
650             break;
651         case SND_SOC_DAIFMT_LEFT_J:
652             break;
653         case SND_SOC_DAIFMT_DSP_A:
654             break;
655         case SND_SOC_DAIFMT_DSP_B:
656             break;
657         default:
658             return -EINVAL;
659     }
660
661     /* clock inversion */
662     switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
663         case SND_SOC_DAIFMT_NB_NF:
664             iface    &= 0xDF;
665             adciface &= 0xDF;
666             //daciface &= 0xDF;    //UPDATED BY david-everest,5-25        
667             daciface &= 0xBF;
668             break;
669         case SND_SOC_DAIFMT_IB_IF:
670             iface    |= 0x20;
671             //adciface &= 0xDF;    //UPDATED BY david-everest,5-25
672             adciface |= 0x20;
673             //daciface &= 0xDF;   //UPDATED BY david-everest,5-25
674             daciface |= 0x40;
675             break;
676         case SND_SOC_DAIFMT_IB_NF:
677             iface    |= 0x20;
678            // adciface |= 0x40;  //UPDATED BY david-everest,5-25
679             adciface &= 0xDF;
680             //daciface |= 0x40;  //UPDATED BY david-everest,5-25
681             daciface &= 0xBF;
682             break;
683         case SND_SOC_DAIFMT_NB_IF:
684             iface    &= 0xDF;
685             adciface |= 0x20;
686             //daciface |= 0x20;  //UPDATED BY david-everest,5-25
687             daciface |= 0x40;
688             break;
689         default:
690             return -EINVAL;
691     }
692
693     snd_soc_write(codec, ES8323_IFACE    , iface);
694     snd_soc_write(codec, ES8323_ADC_IFACE, adciface);
695     snd_soc_write(codec, ES8323_DAC_IFACE, daciface);
696
697     return 0;
698 }
699
700 static int es8323_pcm_startup(struct snd_pcm_substream *substream,
701                               struct snd_soc_dai *dai)
702 {
703         struct snd_soc_codec *codec = dai->codec;
704         struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
705         // u16 i;
706         if (!es8323->is_startup) {
707                 es8323->is_startup = 1;
708                 //on_off_ext_amp(0);
709     /*
710                 snd_soc_write(codec, ES8323_CONTROL1, 0x06);
711                 snd_soc_write(codec, ES8323_CONTROL2, 0x72);
712                 snd_soc_write(codec, ES8323_DACPOWER, 0x00);
713                 mdelay(30);
714           //snd_soc_write(codec, ES8323_CHIPPOWER, 0xf3);
715                 snd_soc_write(codec, ES8323_DACCONTROL21, 0x80);
716                 */
717                 snd_soc_write(codec, ES8323_ADCPOWER, 0x59);
718                 snd_soc_write(codec, ES8323_DACPOWER, 0x3c);
719                 snd_soc_write(codec, ES8323_CHIPPOWER, 0x00);
720                 //on_off_ext_amp(1);
721         }
722
723         DBG("Enter::%s----%d  es8323->sysclk=%d\n",__FUNCTION__,__LINE__,es8323->sysclk);
724
725         /* The set of sample rates that can be supported depends on the
726          * MCLK supplied to the CODEC - enforce this.
727          */
728         if (!es8323->sysclk) {
729                 dev_err(codec->dev,
730                         "No MCLK configured, call set_sysclk() on init\n");
731                 return -EINVAL;
732         }
733
734         snd_pcm_hw_constraint_list(substream->runtime, 0,
735                                    SNDRV_PCM_HW_PARAM_RATE,
736                                    es8323->sysclk_constraints);
737
738         return 0;
739 }
740
741 static int es8323_pcm_hw_params(struct snd_pcm_substream *substream,
742                                 struct snd_pcm_hw_params *params,
743                                 struct snd_soc_dai *dai)
744
745
746         static int codecfirstuse=0;
747         struct snd_soc_pcm_runtime *rtd = substream->private_data;
748         struct snd_soc_codec *codec = rtd->codec;
749         struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
750         //u16 iface = snd_soc_read(codec, es8323_IFACE) & 0x1f3;
751         //u16 srate = snd_soc_read(codec, es8323_SRATE) & 0x180;
752         
753         u16 srate    = snd_soc_read(codec, ES8323_IFACE) & 0x80;
754         u16 adciface = snd_soc_read(codec, ES8323_ADC_IFACE) & 0xE3;
755         u16 daciface = snd_soc_read(codec, ES8323_DAC_IFACE) & 0xC7;
756         
757         int coeff;
758
759         coeff = get_coeff(es8323->sysclk, params_rate(params));
760         if (coeff < 0) {
761                 coeff = get_coeff(es8323->sysclk / 2, params_rate(params));
762                 srate |= 0x40;
763         }
764         if (coeff < 0) {
765                 dev_err(codec->dev,
766                         "Unable to configure sample rate %dHz with %dHz MCLK\n",
767                         params_rate(params), es8323->sysclk);
768                 return coeff;
769         }
770
771         /* bit size */
772  switch (params_format(params)) {
773   case SNDRV_PCM_FORMAT_S16_LE:
774       adciface |= 0x000C;
775       daciface |= 0x0018;
776       break;
777   case SNDRV_PCM_FORMAT_S20_3LE:
778       adciface |= 0x0004;
779       daciface |= 0x0008;
780       break;
781   case SNDRV_PCM_FORMAT_S24_LE:
782       break;
783   case SNDRV_PCM_FORMAT_S32_LE:
784       adciface |= 0x0010;
785       daciface |= 0x0020;
786       break;
787   }
788
789   /* set iface & srate*/
790   snd_soc_write(codec, ES8323_DAC_IFACE, daciface); //dac bits length
791   snd_soc_write(codec, ES8323_ADC_IFACE, adciface); //adc bits length
792
793         if (coeff >= 0)
794                 {
795                  snd_soc_write(codec, ES8323_IFACE, srate);  //bclk div,mclkdiv2
796                  snd_soc_write(codec, ES8323_ADCCONTROL5, coeff_div[coeff].sr | (coeff_div[coeff].usb) << 4);
797                  snd_soc_write(codec, ES8323_DACCONTROL2, coeff_div[coeff].sr | (coeff_div[coeff].usb) << 4);
798                 }
799         if (codecfirstuse == 0)
800                 {
801                         snd_soc_write(codec, ES8323_LOUT2_VOL, es8323_DEF_VOL);//0x1c);   // 
802                   snd_soc_write(codec, ES8323_ROUT2_VOL, es8323_DEF_VOL);//0x1c);   // 
803                   codecfirstuse=1;
804                         }
805
806         return 0;
807 }
808
809 static int es8323_mute(struct snd_soc_dai *dai, int mute)
810 {
811         struct snd_soc_codec *codec = dai->codec;
812         // u16 mute_reg = snd_soc_read(codec, ES8323_DACCONTROL3) & 0xfb;
813
814         DBG("Enter::%s----%d--mute=%d\n",__FUNCTION__,__LINE__,mute);
815
816         if (mute)
817                 //snd_soc_write(codec, ES8323_DACCONTROL3, mute_reg | 0x4);
818          {
819   snd_soc_write(codec, ES8323_DACCONTROL3, 0x06);//0xe6);
820
821          }
822         else
823         {
824
825         snd_soc_write(codec, ES8323_DACCONTROL3, 0x02);//0xe2);
826                 snd_soc_write(codec, 0x30,0x1e);
827                 snd_soc_write(codec, 0x31,0x1e);
828          
829
830         }
831     on_off_ext_amp(!mute);
832     
833         return 0;
834 }
835
836 /////////////////////////////////////////////////////////////////
837 static int es8323_set_bias_level(struct snd_soc_codec *codec,
838                                  enum snd_soc_bias_level level)
839 {
840         struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
841         // u16 OUT_VOL = snd_soc_read(codec, ES8323_LOUT1_VOL);
842         // u16 i;
843         
844         DBG("Enter::%s----%d level =%d\n",__FUNCTION__,__LINE__,level);
845         switch (level) {
846         case SND_SOC_BIAS_ON:
847                 es8323->is_biason = 1;
848                 break;
849         case SND_SOC_BIAS_PREPARE:
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_STANDBY:              
857         snd_soc_write(codec, ES8323_ANAVOLMANAG, 0x7C);
858         snd_soc_write(codec, ES8323_CHIPLOPOW1, 0x00);
859         snd_soc_write(codec, ES8323_CHIPLOPOW2, 0xFF);                                                  
860                 snd_soc_write(codec, ES8323_CHIPPOWER, 0x00);   
861                 snd_soc_write(codec, ES8323_ADCPOWER, 0x59);
862                 break;
863         case SND_SOC_BIAS_OFF:  
864                 snd_soc_write(codec, ES8323_ANAVOLMANAG, 0x7B);
865         snd_soc_write(codec, ES8323_CHIPLOPOW1, 0xFF);
866         snd_soc_write(codec, ES8323_CHIPLOPOW2, 0xFF);          
867                 snd_soc_write(codec, ES8323_ADCPOWER, 0xFF);                                    
868         snd_soc_write(codec, ES8323_CHIPPOWER, 0xAA);
869
870         //snd_soc_write(codec, 0x2b, 0x90);
871                 break;
872         }
873         codec->dapm.bias_level = level;
874         return 0;
875 }
876
877
878
879 #define es8323_RATES SNDRV_PCM_RATE_8000_96000
880
881 #define es8323_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
882         SNDRV_PCM_FMTBIT_S24_LE)
883
884 static struct snd_soc_dai_ops es8323_ops = {
885         .startup = es8323_pcm_startup,
886         .hw_params = es8323_pcm_hw_params,
887         .set_fmt = es8323_set_dai_fmt,
888         .set_sysclk = es8323_set_dai_sysclk,
889         .digital_mute = es8323_mute,
890 };
891
892 static struct snd_soc_dai_driver es8323_dai = {
893         .name = "ES8323 HiFi",
894         .playback = {
895                 .stream_name = "Playback",
896                 .channels_min = 1,
897                 .channels_max = 2,
898                 .rates = es8323_RATES,
899                 .formats = es8323_FORMATS,
900         },
901         .capture = {
902                 .stream_name = "Capture",
903                 .channels_min = 1,
904                 .channels_max = 2,
905                 .rates = es8323_RATES,
906                 .formats = es8323_FORMATS,
907          },
908         .ops = &es8323_ops,
909         .symmetric_rates = 1,
910 };
911
912 static int es8323_suspend(struct snd_soc_codec *codec, pm_message_t state)
913 {
914         // u16 i;
915         DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
916
917 #if 0
918         snd_soc_write(codec, 0x19, 0x06);
919         snd_soc_write(codec, 0x07, 0x7B);
920         snd_soc_write(codec, 0x06, 0xFF);
921         snd_soc_write(codec, 0x05, 0xFF);
922 #endif
923
924         snd_soc_write(codec, 0x19, 0x06);
925         snd_soc_write(codec, 0x30, 0x00);
926         snd_soc_write(codec, 0x31, 0x00);
927                                 snd_soc_write(codec, ES8323_ADCPOWER, 0xFF);                                    
928                                 snd_soc_write(codec, ES8323_DACPOWER, 0xc0);    
929                                 snd_soc_write(codec, ES8323_CHIPPOWER, 0xF3);
930                                 snd_soc_write(codec, 0x00, 0x00);
931                                 snd_soc_write(codec, 0x01, 0x58);
932                                 snd_soc_write(codec, 0x2b, 0x9c);       
933                                 msleep(50);
934                                 gpio_set_value(SPK_CON, 0);
935                                 return 0;
936 }
937
938 static int es8323_resume(struct snd_soc_codec *codec)
939 {
940         // u16 i;
941         // u8 data[2];
942         // u16 *cache = codec->reg_cache;       
943         snd_soc_write(codec, 0x2b, 0x80);       
944   snd_soc_write(codec, 0x01, 0x50);
945   snd_soc_write(codec, 0x00, 0x32);
946         snd_soc_write(codec, ES8323_CHIPPOWER, 0x00);   
947         snd_soc_write(codec, ES8323_DACPOWER, 0x0c);    
948         snd_soc_write(codec, ES8323_ADCPOWER, 0x59);
949   snd_soc_write(codec, 0x31, es8323_DEF_VOL);
950   snd_soc_write(codec, 0x30, es8323_DEF_VOL);
951         snd_soc_write(codec, 0x19, 0x02);                       
952         gpio_set_value(SPK_CON, 1);
953         return 0;
954 }
955
956 static u32 cur_reg=0;
957 static struct snd_soc_codec *es8323_codec;
958 static int entry_read(char *page, char **start, off_t off,
959                 int count, int *eof, void *data)
960 {
961         int len;
962
963         snd_soc_write(es8323_codec, ES8323_ADCPOWER, 0xff);
964         snd_soc_write(es8323_codec, ES8323_DACPOWER, 0xf0);
965         snd_soc_write(es8323_codec, ES8323_DACPOWER, 0xc0);
966         snd_soc_write(es8323_codec, ES8323_CHIPPOWER, 0xf3);
967
968         len = sprintf(page, "es8323 suspend...\n");
969
970         return len ;
971 }
972
973 #if (RT5633_SPK_TIMER == 1)
974 static void spk_work_handler(struct work_struct *work)
975 {
976         //if(!gpio_get_value(ES8323_HP_PIN)){
977                 //gpio_direction_output(SPK_CON,0);
978  //               gpio_set_value(SPK_CON, 0);
979 //      }else{
980                 //gpio_direction_output(SPK_CON,1);     
981  //               gpio_set_value(SPK_CON, 1);
982 //      }
983 }
984 void spk_timer_callback(unsigned long data )
985 {       
986         int ret = 0;
987         schedule_work(&spk_work);
988   ret = mod_timer(&spk_timer, jiffies + msecs_to_jiffies(1000));
989   if (ret) printk("Error in mod_timer\n");
990 }
991 #endif
992
993 static int es8323_probe(struct snd_soc_codec *codec)
994 {
995         // struct es8323_priv *es8323 = snd_soc_codec_get_drvdata(codec);
996         struct snd_soc_dapm_context *dapm = &codec->dapm;
997         int ret = 0;
998         unsigned long flags=0;
999         // u16 reg,i;
1000
1001     printk("%s\n", __func__);
1002 #if 0
1003     ret = gpio_request(RK30_PIN0_PC7, NULL);
1004     if (ret != 0) {
1005         printk("%s request RK30_PIN0_PC7 error", __func__);
1006         return ret;
1007     }
1008     gpio_direction_input(RK30_PIN0_PC7);
1009 #endif
1010     ret = gpio_request(SPK_CON, NULL);
1011     if (ret != 0) {
1012             printk("%s request SPK_CON error", __func__);
1013             return ret;
1014     }
1015     //gpio_set_value(SPK_CON, 1);
1016     gpio_direction_output(SPK_CON,0);
1017
1018
1019                 ret = gpio_request(HP_DET, NULL);
1020                 if (ret != 0) {
1021                                 printk("%s request HP_DET error", __func__);
1022                                 return ret;
1023                 }
1024                 gpio_direction_input(HP_DET);
1025                 
1026                 flags = gpio_get_value(HP_DET) ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1027                 
1028                 HP_IRQ = gpio_to_irq(HP_DET);
1029     if (HP_IRQ){
1030         ret = request_irq(HP_IRQ, hp_det_irq_handler, flags, "ES8323", NULL);
1031         if(ret == 0){
1032             printk("%s:register ISR (irq=%d)\n", __FUNCTION__,HP_IRQ);
1033         }
1034         else 
1035                         printk("request_irq HP_IRQ failed\n");
1036     }
1037     
1038         if (codec == NULL) {
1039                 dev_err(codec->dev, "Codec device not registered\n");
1040                 return -ENODEV;
1041         }
1042     codec->read  = es8323_read_reg_cache;
1043     codec->write = es8323_write;
1044     codec->hw_write = (hw_write_t)i2c_master_send;
1045         codec->control_data = container_of(codec->dev, struct i2c_client, dev);
1046
1047         es8323_codec = codec;
1048         ret = es8323_reset(codec);
1049         if (ret < 0) {
1050                 dev_err(codec->dev, "Failed to issue reset\n");
1051                 return ret;
1052         }
1053         #if (RT5633_SPK_TIMER == 1)
1054         setup_timer( &spk_timer, spk_timer_callback, 0 );
1055         ret = mod_timer( &spk_timer, jiffies + msecs_to_jiffies(5000) );
1056         if (ret)
1057                 printk("Error in mod_timer\n");
1058         INIT_WORK(&spk_work, spk_work_handler);
1059         es8323_ANVOL=1;
1060 #endif
1061         
1062   //es8323_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1063   
1064 #if 1   
1065     //snd_soc_write(codec, 0x35  , 0xa0); 
1066     //snd_soc_write(codec, 0x36  , 0x08); //for 1.8V VDD
1067 snd_soc_write(codec, 0x02,0xf3);
1068 snd_soc_write(codec, 0x2B,0x80);
1069 snd_soc_write(codec, 0x08,0x00);   //ES8388 salve  
1070 snd_soc_write(codec, 0x00,0x32);   //
1071 snd_soc_write(codec, 0x01,0x72);   //PLAYBACK & RECORD Mode,EnRefr=1
1072 snd_soc_write(codec, 0x03,0x59);   //pdn_ana=0,ibiasgen_pdn=0
1073 snd_soc_write(codec, 0x05,0x00);   //pdn_ana=0,ibiasgen_pdn=0
1074 snd_soc_write(codec, 0x06,0xc3);   //pdn_ana=0,ibiasgen_pdn=0 
1075 snd_soc_write(codec, 0x09,0x88);  //ADC L/R PGA =  +24dB
1076 //----------------------------------------------------------------------------------------------------------------
1077 snd_soc_write(codec, 0x0a,0xf0);  //ADC INPUT=LIN2/RIN2
1078 snd_soc_write(codec, 0x0b,0x82);  //ADC INPUT=LIN2/RIN2 //82
1079 //-----------------------------------------------------------------------------------------------------------------
1080 snd_soc_write(codec, 0x0C,0x4c);  //I2S-24BIT
1081 snd_soc_write(codec, 0x0d,0x02);  //MCLK/LRCK=256 
1082 snd_soc_write(codec, 0x10,0x00);  //ADC Left Volume=0db
1083 snd_soc_write(codec, 0x11,0x00);  //ADC Right Volume=0db
1084 snd_soc_write(codec, 0x12,0xea); // ALC stereo MAXGAIN: 35.5dB,  MINGAIN: +6dB (Record Volume increased!)
1085 snd_soc_write(codec, 0x13,0xc0);
1086 snd_soc_write(codec, 0x14,0x05);
1087 snd_soc_write(codec, 0x15,0x06);
1088 snd_soc_write(codec, 0x16,0x53);  
1089 snd_soc_write(codec, 0x17,0x18);  //I2S-16BIT
1090 snd_soc_write(codec, 0x18,0x02);
1091 snd_soc_write(codec, 0x1A,0x00);  //DAC VOLUME=0DB
1092 snd_soc_write(codec, 0x1B,0x00);
1093                 /*
1094                 snd_soc_write(codec, 0x1E,0x01);    //for 47uF capacitors ,15db Bass@90Hz,Fs=44100
1095                 snd_soc_write(codec, 0x1F,0x84);
1096                 snd_soc_write(codec, 0x20,0xED);
1097                 snd_soc_write(codec, 0x21,0xAF);
1098                 snd_soc_write(codec, 0x22,0x20);
1099                 snd_soc_write(codec, 0x23,0x6C);
1100                 snd_soc_write(codec, 0x24,0xE9);
1101                 snd_soc_write(codec, 0x25,0xBE);
1102                 */
1103 snd_soc_write(codec, 0x26,0x12);  //Left DAC TO Left IXER
1104 snd_soc_write(codec, 0x27,0xb8);  //Left DAC TO Left MIXER
1105 snd_soc_write(codec, 0x28,0x38);
1106 snd_soc_write(codec, 0x29,0x38);
1107 snd_soc_write(codec, 0x2A,0xb8);
1108 snd_soc_write(codec, 0x02,0x00); //aa //START DLL and state-machine,START DSM 
1109 snd_soc_write(codec, 0x19,0x02);  //SOFT RAMP RATE=32LRCKS/STEP,Enable ZERO-CROSS CHECK,DAC MUTE
1110 snd_soc_write(codec, 0x04,0x0c);   //pdn_ana=0,ibiasgen_pdn=0  
1111 msleep(100);
1112 snd_soc_write(codec, 0x2e,0x00); 
1113 snd_soc_write(codec, 0x2f,0x00);
1114 snd_soc_write(codec, 0x30,0x08); 
1115 snd_soc_write(codec, 0x31,0x08);
1116 msleep(200);
1117 snd_soc_write(codec, 0x30,0x0f); 
1118 snd_soc_write(codec, 0x31,0x0f);
1119 msleep(200);
1120 snd_soc_write(codec, 0x30,0x18); 
1121 snd_soc_write(codec, 0x31,0x18);
1122 msleep(100);
1123 snd_soc_write(codec, 0x04,0x2c);   //pdn_ana=0,ibiasgen_pdn=0 
1124 #endif  
1125         
1126   //s8323_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1127   //codec->dapm.bias_level = SND_SOC_BIAS_STANDBY;
1128       
1129         snd_soc_add_controls(codec, es8323_snd_controls,
1130                                 ARRAY_SIZE(es8323_snd_controls));
1131         snd_soc_dapm_new_controls(dapm, es8323_dapm_widgets,
1132                                   ARRAY_SIZE(es8323_dapm_widgets));
1133         snd_soc_dapm_add_routes(dapm, audio_map, ARRAY_SIZE(audio_map));
1134           
1135         create_proc_read_entry("es8323_suspend", 0644, NULL, entry_read, NULL);
1136
1137         return 0;
1138 }
1139
1140 static int es8323_remove(struct snd_soc_codec *codec)
1141 {
1142         es8323_set_bias_level(codec, SND_SOC_BIAS_OFF);
1143         return 0;
1144 }
1145
1146 #ifdef CONFIG_MACH_RK_FAC
1147 void es8323_codec_set_spk(bool on)
1148 {
1149         if(es8323_hdmi_ctrl)
1150         {
1151  set_spk = on;
1152   gpio_set_value(SPK_CON, on);
1153         }
1154 }
1155 #else
1156 void codec_set_spk(bool on)
1157 {
1158         DBG("Enter::%s----%d--, on = %d\n",__FUNCTION__,__LINE__, on);
1159
1160         set_spk = on;
1161         gpio_set_value(SPK_CON, on);
1162         //return;
1163 }
1164 #endif 
1165
1166 static struct snd_soc_codec_driver soc_codec_dev_es8323 = {
1167         .probe =        es8323_probe,
1168         .remove =       es8323_remove,
1169         .suspend =      es8323_suspend,
1170         .resume =       es8323_resume,
1171         .set_bias_level = es8323_set_bias_level,
1172         .reg_cache_size = ARRAY_SIZE(es8323_reg),
1173         .reg_word_size = sizeof(u16),
1174         .reg_cache_default = es8323_reg,
1175         //------------------------------------------
1176         //.volatile_register = es8323_volatile_register,
1177         //.readable_register = es8323_readable_register,
1178         .reg_cache_step = 1,
1179 #if 0
1180         .controls = es8323_snd_controls,
1181         .num_controls = ARRAY_SIZE(es8323_snd_controls),        
1182   .dapm_routes = audio_map,  
1183   .num_dapm_routes = ARRAY_SIZE(audio_map), 
1184   .dapm_widgets = es8323_dapm_widgets,  
1185   .num_dapm_widgets = ARRAY_SIZE(es8323_dapm_widgets),   
1186         
1187         //--------------------------------------------------    
1188         .read   = es8323_read_reg_cache,
1189         .write = es8323_write,  
1190 #endif
1191 };
1192
1193 #if defined(CONFIG_SPI_MASTER)
1194 static int __devinit es8323_spi_probe(struct spi_device *spi)
1195 {
1196         struct es8323_priv *es8323;
1197         int ret;
1198
1199         es8323 = kzalloc(sizeof(struct es8323_priv), GFP_KERNEL);
1200         if (es8323 == NULL)
1201                 return -ENOMEM;
1202
1203         es8323->control_type = SND_SOC_SPI;
1204         spi_set_drvdata(spi, es8323);
1205
1206         ret = snd_soc_register_codec(&spi->dev,
1207                         &soc_codec_dev_es8323, &es8323_dai, 1);
1208         if (ret < 0)
1209                 kfree(es8323);
1210         return ret;
1211 }
1212
1213 static int __devexit es8323_spi_remove(struct spi_device *spi)
1214 {
1215         snd_soc_unregister_codec(&spi->dev);
1216         kfree(spi_get_drvdata(spi));
1217         return 0;
1218 }
1219
1220 static struct spi_driver es8323_spi_driver = {
1221         .driver = {
1222                 .name   = "ES8323",
1223                 .owner  = THIS_MODULE,
1224         },
1225         .probe          = es8323_spi_probe,
1226         .remove         = __devexit_p(es8323_spi_remove),
1227 };
1228 #endif /* CONFIG_SPI_MASTER */
1229
1230 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1231 static ssize_t es8323_show(struct device *dev, struct device_attribute *attr, char *_buf)
1232 {
1233         return sprintf(_buf, "%s(): get 0x%04x=0x%04x\n", __FUNCTION__, cur_reg, 
1234                 snd_soc_read(es8323_codec, cur_reg));
1235 }
1236
1237 static u32 strtol(const char *nptr, int base)
1238 {
1239         u32 ret;
1240         if(!nptr || (base!=16 && base!=10 && base!=8))
1241         {
1242
1243                 printk("%s(): NULL pointer input\n", __FUNCTION__);
1244                 return -1;
1245         }
1246         for(ret=0; *nptr; nptr++)
1247         {
1248
1249
1250                 if((base==16 && *nptr>='A' && *nptr<='F') || 
1251                         (base==16 && *nptr>='a' && *nptr<='f') || 
1252                         (base>=10 && *nptr>='0' && *nptr<='9') ||
1253                         (base>=8 && *nptr>='0' && *nptr<='7') )
1254                 {
1255                         ret *= base;
1256                         if(base==16 && *nptr>='A' && *nptr<='F')
1257                                 ret += *nptr-'A'+10;
1258                         else if(base==16 && *nptr>='a' && *nptr<='f')
1259                                 ret += *nptr-'a'+10;
1260                         else if(base>=10 && *nptr>='0' && *nptr<='9')
1261                                 ret += *nptr-'0';
1262                         else if(base>=8 && *nptr>='0' && *nptr<='7')
1263                                 ret += *nptr-'0';
1264                 }
1265                 else
1266                         return ret;
1267         }
1268         return ret;
1269 }
1270
1271 static ssize_t es8323_store(struct device *dev,
1272                                         struct device_attribute *attr,
1273                                         const char *_buf, size_t _count)
1274 {
1275         const char * p=_buf;
1276         u32 reg, val;
1277         
1278         if(!strncmp(_buf, "get", strlen("get")))
1279         {
1280                 p+=strlen("get");
1281                 cur_reg=(u32)strtol(p, 16);
1282                 val=snd_soc_read(es8323_codec, cur_reg);
1283                 printk("%s(): get 0x%04x=0x%04x\n", __FUNCTION__, cur_reg, val);
1284         }
1285         else if(!strncmp(_buf, "put", strlen("put")))
1286         {
1287                 p+=strlen("put");
1288                 reg=strtol(p, 16);
1289                 p=strchr(_buf, '=');
1290                 if(p)
1291                 {
1292                         ++ p;
1293                         val=strtol(p, 16);
1294                         snd_soc_write(es8323_codec, reg, val);
1295                         printk("%s(): set 0x%04x=0x%04x\n", __FUNCTION__, reg, val);
1296                 }
1297                 else
1298                         printk("%s(): Bad string format input!\n", __FUNCTION__);
1299         }
1300         else
1301                 printk("%s(): Bad string format input!\n", __FUNCTION__);
1302         
1303         return _count;
1304
1305
1306 static struct device *es8323_dev = NULL;
1307 static struct class *es8323_class = NULL;
1308 static DEVICE_ATTR(es8323, 0664, es8323_show, es8323_store);
1309 static __devinit int es8323_i2c_probe(struct i2c_client *i2c,
1310                                       const struct i2c_device_id *id)
1311 {
1312         
1313         struct es8323_priv *es8323;
1314         int ret = -1;
1315         struct i2c_adapter *adapter = to_i2c_adapter(i2c->dev.parent);
1316         char reg;
1317         char tmp;
1318
1319          if (!i2c_check_functionality(adapter, I2C_FUNC_I2C)) {
1320         dev_warn(&adapter->dev,
1321                  "I2C-Adapter doesn't support I2C_FUNC_I2C\n");
1322         return -EIO;
1323     }
1324
1325         es8323 = kzalloc(sizeof(struct es8323_priv), GFP_KERNEL);
1326         if (es8323 == NULL)
1327                 return -ENOMEM;
1328
1329         i2c_set_clientdata(i2c, es8323);
1330         es8323->control_type = SND_SOC_I2C;
1331         
1332         reg = ES8323_DACCONTROL18;
1333         ret = i2c_master_reg8_recv(i2c, reg, &tmp, 1 ,200 * 1000);
1334         //ret =i2c_master_reg8_recv(client, 0x00, buf, 2, 200*1000);//i2c_write_bytes(client, &test_data, 1);   //Test I2C connection.
1335         if (ret < 0){
1336                                 printk("es8323 probe error\n");
1337                                 kfree(es8323);
1338                                 return ret;
1339         }
1340         
1341         printk("es8323 probe i2c recv ok\n");
1342
1343         ret =  snd_soc_register_codec(&i2c->dev,
1344                         &soc_codec_dev_es8323, &es8323_dai, 1);
1345         if (ret < 0) {
1346                 kfree(es8323);
1347                 return ret;
1348         }
1349         es8323_class = class_create(THIS_MODULE, "es8323");
1350         if (IS_ERR(es8323_class))
1351         {
1352                 printk("Create class audio_es8323.\n");
1353                 return -ENOMEM;
1354         }
1355         es8323_dev = device_create(es8323_class, NULL, MKDEV(0, 1), NULL, "dev");
1356         ret = device_create_file(es8323_dev, &dev_attr_es8323);
1357         if (ret < 0)
1358                 printk("failed to add dev_attr_es8323 file\n");
1359   #ifdef CONFIG_MACH_RK_FAC              
1360         es8323_hdmi_ctrl=1;
1361   #endif 
1362
1363         return ret;
1364 }
1365
1366 static __devexit int es8323_i2c_remove(struct i2c_client *client)
1367 {
1368         snd_soc_unregister_codec(&client->dev);
1369         kfree(i2c_get_clientdata(client));
1370         return 0;
1371 }
1372
1373 static const struct i2c_device_id es8323_i2c_id[] = {
1374         { "es8323", 0 },
1375         { }
1376 };
1377 MODULE_DEVICE_TABLE(i2c, es8323_i2c_id);
1378
1379 void es8323_i2c_shutdown(struct i2c_client *client)
1380 {
1381         printk("Chenzy-------hkw-------%s\n", __func__);
1382         gpio_direction_output(SPK_CON,0);
1383
1384         snd_soc_write(es8323_codec, ES8323_CONTROL2, 0x58);     
1385         snd_soc_write(es8323_codec, ES8323_CONTROL1, 0x32);                                     
1386         snd_soc_write(es8323_codec, ES8323_CHIPPOWER, 0xf3);
1387         snd_soc_write(es8323_codec, ES8323_DACPOWER, 0xc0);
1388
1389         snd_soc_write(es8323_codec, ES8323_DACCONTROL26, 0x00);
1390         snd_soc_write(es8323_codec, ES8323_DACCONTROL27, 0x00);
1391
1392         snd_soc_write(es8323_codec, ES8323_CONTROL1, 0x30);                                     
1393         snd_soc_write(es8323_codec, ES8323_CONTROL1, 0x34);                                     
1394
1395         mdelay(100);
1396 }
1397 #define  I2C_CLK_NAME  GPIO0B0_I2S8CHCLK_NAME
1398 #define  I2C_CLK_GPIO_MODE  GPIO0B_GPIO0B0
1399 #define  I2C_GPIO_OUTPUT  GPIO_LOW
1400 #define  I2C_CLK_CLK_MODE   GPIO0B_I2S_8CH_CLK
1401 #define  I2C_CLK_GPIO   RK30_PIN0_PB0
1402
1403 #define  I2C_MCLK_NAME  GPIO0B1_I2S8CHSCLK_NAME
1404 #define  I2C_MCLK_GPIO_MODE  GPIO0B_GPIO0B1
1405 #define  I2C_MGPIO_OUTPUT  GPIO_LOW
1406 #define  I2C_MCLK_CLK_MODE   GPIO0B_I2S_8CH_SCLK
1407 #define  I2C_MCLK_GPIO   RK30_PIN0_PB1
1408 static int   es8323_i2c_suspend (struct i2c_client *client, pm_message_t mesg)
1409 {
1410 #if 0
1411         rk30_mux_api_set(I2C_CLK_NAME,I2C_CLK_GPIO_MODE);
1412         if (gpio_request(I2C_CLK_GPIO, NULL)) {
1413                 printk("func %s, line %d: request gpio fail\n", __FUNCTION__, __LINE__);
1414                 return -1;
1415         }
1416
1417         gpio_direction_output(I2C_CLK_GPIO,I2C_GPIO_OUTPUT);
1418
1419         rk30_mux_api_set(I2C_MCLK_NAME,I2C_MCLK_GPIO_MODE);
1420         if (gpio_request(I2C_MCLK_GPIO, NULL)) {
1421                 printk("func %s, line %d: request gpio fail\n", __FUNCTION__, __LINE__);
1422                 return -1;
1423         }
1424
1425         gpio_direction_output(I2C_MCLK_GPIO,I2C_MGPIO_OUTPUT);
1426 #endif
1427         iomux_set(GPIO1_C1);
1428         if (gpio_request(RK30_PIN1_PC1, NULL)) {
1429                 printk("func %s, line %d: request gpio fail\n", __FUNCTION__, __LINE__);
1430                 return -1;
1431         }
1432         gpio_direction_input(RK30_PIN1_PC1);
1433         gpio_pull_updown(RK30_PIN1_PC1, PullDisable);
1434
1435 #if 0
1436         iomux_set(GPIO1_C2);
1437         gpio_direction_input(RK30_PIN1_PC2);
1438         gpio_pull_updown(RK30_PIN1_PC2, PullDisable);
1439
1440         iomux_set(GPIO1_C3);
1441         gpio_direction_input(RK30_PIN1_PC3);
1442         gpio_pull_updown(RK30_PIN1_PC3, PullDisable);
1443
1444         iomux_set(GPIO1_C4);
1445         gpio_direction_input(RK30_PIN1_PC4);
1446         gpio_pull_updown(RK30_PIN1_PC4, PullDisable);
1447
1448         iomux_set(GPIO1_C5);
1449         gpio_direction_input(RK30_PIN1_PC5);
1450         gpio_pull_updown(RK30_PIN1_PC5, PullDisable);
1451 #endif
1452
1453         return 0;
1454 }
1455
1456 static int   es8323_i2c_resume(struct i2c_client *client)
1457 {
1458 #if 0
1459         gpio_free(I2C_MCLK_GPIO);
1460         gpio_free(I2C_CLK_GPIO);
1461
1462         rk30_mux_api_set(I2C_MCLK_NAME,I2C_MCLK_CLK_MODE);
1463         rk30_mux_api_set(I2C_CLK_NAME,I2C_CLK_CLK_MODE);
1464 #endif
1465
1466         gpio_free(RK30_PIN1_PC1);
1467         iomux_set(I2S0_SCLK);
1468
1469         return 0;
1470 }
1471
1472 static struct i2c_driver es8323_i2c_driver = {
1473         .driver = {
1474                 .name = "ES8323",
1475                 .owner = THIS_MODULE,
1476         },
1477         .probe =    es8323_i2c_probe,
1478         .remove =   __devexit_p(es8323_i2c_remove),
1479         .shutdown = es8323_i2c_shutdown,
1480         .suspend  = es8323_i2c_suspend,
1481         .resume = es8323_i2c_resume,
1482         .id_table = es8323_i2c_id,
1483 };
1484 #endif
1485
1486 static int __init es8323_modinit(void)
1487 {
1488         return i2c_add_driver(&es8323_i2c_driver);
1489 }
1490 module_init(es8323_modinit);
1491
1492 static void __exit es8323_exit(void)
1493 {
1494
1495 //      if(0 == tcsi_get_value(TCSI_CODEC_ES8323))
1496 //              return;
1497 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1498         i2c_del_driver(&es8323_i2c_driver);
1499 #endif
1500 #if defined(CONFIG_SPI_MASTER)
1501         spi_unregister_driver(&es8323_spi_driver);
1502 #endif
1503 }
1504 module_exit(es8323_exit);
1505
1506
1507 MODULE_DESCRIPTION("ASoC es8323 driver");
1508 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1509 MODULE_LICENSE("GPL");
1510