Merge branch linux-linaro-lsk-v3.10-android
[firefly-linux-kernel-4.4.55.git] / sound / soc / codecs / tlv320aic3111.c
1 /*
2  * linux/sound/soc/codecs/tlv320aic3111.c
3  *
4  *
5  * Copyright (C) 2010 Texas Instruments, Inc.
6  *
7  * This package is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
12  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
13  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14  *
15  * History:
16  *
17  * Rev 0.1   ASoC driver support    Mistral         14-04-2010
18  * 
19  * Rev 0.2   Updated based Review Comments Mistral      29-06-2010 
20  *
21  * Rev 0.3   Updated for Codec Family Compatibility     12-07-2010
22  */
23
24 /*
25  ***************************************************************************** 
26  * Include Files
27  ***************************************************************************** 
28  */
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/pm.h>
34 #include <linux/i2c.h>
35 #include <linux/platform_device.h>
36 #include <linux/cdev.h>
37 #include <linux/of_gpio.h>
38 #include <linux/seq_file.h>
39 #include <linux/spi/spi.h>
40 #include <linux/clk.h>
41
42 #include <sound/core.h>
43 #include <sound/pcm.h>
44 #include <sound/pcm_params.h>
45 #include <sound/soc.h>
46 #include <sound/soc-dapm.h>
47 #include <sound/initval.h>
48 #include <sound/tlv.h>
49
50 //#include <mach/gpio.h>
51 #include "tlv320aic3111.h"
52
53 #if 0
54 #define AIC_DBG(x...)   printk(KERN_INFO x)
55 #else
56 #define AIC_DBG(x...)   do { } while (0)
57 #endif
58
59 #define GPIO_HIGH 1
60 #define GPIO_LOW 0
61 #define INVALID_GPIO -1
62
63 /* codec status */
64 #define AIC3110_IS_SHUTDOWN     0
65 #define AIC3110_IS_CAPTURE_ON   1
66 #define AIC3110_IS_PLAYBACK_ON  2
67 #define AIC3110_IS_INITPOWER_ON 4
68
69 /* work type */
70 #define AIC3110_POWERDOWN_NULL  0
71 #define AIC3110_POWERDOWN_PLAYBACK      1
72 #define AIC3110_POWERDOWN_CAPTURE       2
73 #define AIC3110_POWERDOWN_PLAYBACK_CAPTURE      3
74 #define JACK_DET_ADLOOP         msecs_to_jiffies(200)
75
76 #define SPK 1
77 #define HP 0
78
79 int aic3111_spk_ctl_gpio = INVALID_GPIO;
80 int aic3111_hp_det_gpio = INVALID_GPIO;
81
82 static int aic3111_power_speaker (bool on);
83 struct speaker_data {
84      struct timer_list timer;
85      struct semaphore sem;
86 };
87 enum 
88 {
89         POWER_STATE_OFF = 0,
90         POWER_STATE_ON,
91
92         POWER_STATE_SW_HP = 0,
93         POWER_STATE_SW_SPK,
94 }; 
95
96 static void aic3111_work(struct work_struct *work);
97
98 static struct workqueue_struct *aic3111_workq;
99 static DECLARE_DELAYED_WORK(delayed_work, aic3111_work);
100 static int aic3111_current_status = AIC3110_IS_SHUTDOWN, aic3111_work_type = AIC3110_POWERDOWN_NULL;
101 static bool isHSin = true, isSetHW = false;
102 int old_status = SPK;
103 /*
104  ***************************************************************************** 
105  * Global Variables
106  ***************************************************************************** 
107  */
108 /* Used to maintain the Register Access control*/
109 static u8 aic3111_reg_ctl;
110
111 static struct snd_soc_codec *aic3111_codec;
112 struct aic3111_priv *aic3111_privdata;
113 struct i2c_client *aic3111_i2c;
114
115   /* add a timer for checkout HP or SPK*/
116 static struct timer_list aic3111_timer;
117
118 /*Used to delay work hpdet switch irq handle*/
119 struct delayed_work aic3111_hpdet_work;
120
121 #ifdef CONFIG_MINI_DSP
122 extern int aic3111_minidsp_program (struct snd_soc_codec *codec);
123 extern void aic3111_add_minidsp_controls (struct snd_soc_codec *codec);
124 #endif
125
126 /*
127  *      AIC3111 register cache
128  *      We are caching the registers here.
129  *      NOTE: In AIC3111, there are 61 pages of 128 registers supported.
130  *      The following table contains the page0, page1 and page2 registers values.
131  */
132
133 #ifdef AIC3111_CODEC_SUPPORT
134 static const u8 aic31xx_reg[AIC31xx_CACHEREGNUM] = {
135 /* Page 0 Registers */
136 /* 0  */ 0x00, 0x00, 0x12, 0x00, 0x00, 0x11, 0x04, 0x00, 0x00, 0x00, 0x00,
137   0x01, 0x01, 0x00, 0x80, 0x80,
138 /* 10 */ 0x08, 0x00, 0x01, 0x01, 0x80, 0x80, 0x04, 0x00, 0x01, 0x00, 0x00,
139   0x00, 0x01, 0x00, 0x00, 0x00,
140 /* 20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
141   0x00, 0x00, 0x00, 0x00, 0x00,
142 /* 30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x55, 0x55, 0x00, 0x00,
143   0x00, 0x01, 0x01, 0x00, 0x14,
144 /* 40 */ 0x0c, 0x00, 0x00, 0x00, 0x6f, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00,
145   0xee, 0x10, 0xd8, 0x7e, 0xe3,
146 /* 50 */ 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00,
147   0x00, 0x00, 0x00, 0x00, 0x00,
148 /* 60 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
149   0x00, 0x00, 0x00, 0x00, 0x00,
150 /* 70 */ 0x00, 0x00, 0x10, 0x32, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
151   0x00, 0x00, 0x00, 0x12, 0x02,
152 /* Page 1 Registers */
153 /* 0 */ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
154   0x00, 0x00, 0x00, 0x00, 0x00,
155 /* 10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
156   0x00, 0x00, 0x00, 0x00, 0x00,
157 /* 20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
158   0x00, 0x00, 0x00, 0x00, 0x00,
159 /* 30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
160   0x80, 0x80, 0x00, 0x00, 0x00,
161 /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
162   0x00, 0x00, 0x00, 0x00, 0x00,
163 /* 50 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
164   0x00, 0x00, 0x00, 0x00, 0x00,
165 /* 60 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
166   0x00, 0x00, 0x00, 0x00, 0x00,
167 /* 70 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
168   0x00, 0x00, 0x00, 0x00, 0x00,
169 };
170
171 #elif defined(AIC3110_CODEC_SUPPORT)
172 /**************** AIC3110 REG CACHE ******************/
173 static const u8 aic31xx_reg[AIC31xx_CACHEREGNUM] = {
174 /* Page 0 Registers */
175 0x00, 0x00, 0x01, 0x56, 0x00, 0x11, 0x04, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x00, 0x80, 0x80, 
176 0x08, 0x00, 0x01, 0x01, 0x80, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 
177 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
178 0x00, 0x00, 0x00, 0x02, 0x32, 0x12, 0x03, 0x02, 0x02, 0x11, 0x10, 0x00, 0x01, 0x04, 0x00, 0x14, 
179 0x0c, 0x00, 0x00, 0x00, 0x0f, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0xee, 0x10, 0xd8, 0x7e, 0xe3, 
180 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
181 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
182 0x00, 0x00, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
183 /* Page 1 Registers */
184 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
185 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 
186 0x06, 0x3e, 0x00, 0x00, 0x7f, 0x7f, 0x7f, 0x7f, 0x02, 0x02, 0x00, 0x00, 0x20, 0x86, 0x00, 0x80, 
187 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
188 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
189 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
190 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
191 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
192 }; /**************************** End of AIC3110 REG CAHE ******************/
193
194 #elif defined(AIC3100_CODEC_SUPPORT)
195 /******************************* AIC3100 REG CACHE ***********************/
196 static const u8 aic31xx_reg[AIC31xx_CACHEREGNUM] = {
197 /* Page 0 Registers */
198 /* 0  */ 0x00, 0x00, 0x12, 0x00, 0x00, 0x11, 0x04, 0x00, 0x00, 0x00, 0x00,
199   0x01, 0x01, 0x00, 0x80, 0x00,
200 /* 10 */ 0x00, 0x00, 0x01, 0x01, 0x80, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
201   0x00, 0x01, 0x00, 0x00, 0x00,
202 /* 20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
203   0x00, 0x00, 0x00, 0x00, 0x00,
204 /* 30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x55, 0x55, 0x00, 0x00,
205   0x00, 0x01, 0x01, 0x00, 0x14,
206 /* 40 */ 0x0c, 0x00, 0x00, 0x00, 0x6f, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00,
207   0xee, 0x10, 0xd8, 0x7e, 0xe3,
208 /* 50 */ 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00,
209   0x00, 0x00, 0x00, 0x00, 0x00,
210 /* 60 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
211   0x00, 0x00, 0x00, 0x00, 0x00,
212 /* 70 */ 0x00, 0x00, 0x10, 0x32, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
213   0x00, 0x00, 0x00, 0x12, 0x02,
214 /* Page 1 Registers */
215 /* 0 */ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
216   0x00, 0x00, 0x00, 0x00, 0x00,
217 /* 10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
218   0x00, 0x00, 0x00, 0x00, 0x00,
219 /* 20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
220   0x00, 0x00, 0x00, 0x00, 0x00,
221 /* 30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
222   0x80, 0x80, 0x00, 0x00, 0x00,
223 /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
224   0x00, 0x00, 0x00, 0x00, 0x00,
225 /* 50 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
226   0x00, 0x00, 0x00, 0x00, 0x00,
227 /* 60 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
228   0x00, 0x00, 0x00, 0x00, 0x00,
229 /* 70 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
230   0x00, 0x00, 0x00, 0x00, 0x00,
231 }; /**************************** End of AIC3100 REG CACHE ******************/
232
233 #else /*#ifdef AIC3120_CODEC_SUPPORT */
234 static const u8 aic31xx_reg[AIC31xx_CACHEREGNUM] = {
235 /* Page 0 Registers */
236 /* 0  */ 0x00, 0x00, 0x12, 0x00, 0x00, 0x11, 0x04, 0x00, 0x00, 0x00, 0x00,
237   0x01, 0x01, 0x00, 0x80, 0x80,
238 /* 10 */ 0x08, 0x00, 0x01, 0x01, 0x80, 0x80, 0x04, 0x00, 0x01, 0x00, 0x00,
239   0x00, 0x01, 0x00, 0x00, 0x00,
240 /* 20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
241   0x00, 0x00, 0x00, 0x00, 0x00,
242 /* 30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x15, 0x55, 0x55, 0x00, 0x00,
243   0x00, 0x01, 0x01, 0x00, 0x14,
244 /* 40 */ 0x0c, 0x00, 0x00, 0x00, 0x6f, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00,
245   0xee, 0x10, 0xd8, 0x7e, 0xe3,
246 /* 50 */ 0x00, 0x00, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7f, 0x00, 0x00,
247   0x00, 0x00, 0x00, 0x00, 0x00,
248 /* 60 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
249   0x00, 0x00, 0x00, 0x00, 0x00,
250 /* 70 */ 0x00, 0x00, 0x10, 0x32, 0x54, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
251   0x00, 0x00, 0x00, 0x12, 0x02,
252 /* Page 1 Registers */
253 /* 0 */ 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
254   0x00, 0x00, 0x00, 0x00, 0x00,
255 /* 10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
256   0x00, 0x00, 0x00, 0x00, 0x00,
257 /* 20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
258   0x00, 0x00, 0x00, 0x00, 0x00,
259 /* 30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
260   0x80, 0x80, 0x00, 0x00, 0x00,
261 /* 40 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
262   0x00, 0x00, 0x00, 0x00, 0x00,
263 /* 50 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
264   0x00, 0x00, 0x00, 0x00, 0x00,
265 /* 60 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
266   0x00, 0x00, 0x00, 0x00, 0x00,
267 /* 70 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
268   0x00, 0x00, 0x00, 0x00, 0x00,
269 };
270
271 #endif
272
273 /*
274  *----------------------------------------------------------------------------
275  * Function : aic3111_change_page
276  * Purpose  : This function is to switch between page 0 and page 1.
277  *            
278  *----------------------------------------------------------------------------
279  */
280 static int aic3111_change_page (struct snd_soc_codec *codec, u8 new_page)
281 {
282         struct aic3111_priv *aic3111 = aic3111_privdata;
283         u8 data[2];
284
285         if (new_page == 2 || new_page > 8) {
286                 printk("ERROR::codec do not have page %d !!!!!!\n", new_page);
287                 return -1;
288         }
289
290         data[0] = 0;
291         data[1] = new_page;
292         aic3111->page_no = new_page;
293
294         if (codec->hw_write (codec->control_data, data, 2) != 2)
295         {
296                 printk ("Error in changing page to %d \n", new_page);
297                 return -1;
298         }
299
300         return 0;
301 }
302
303 /*
304  *----------------------------------------------------------------------------
305  * Function : aic3111_write_reg_cache
306  * Purpose  : This function is to write aic3111 register cache
307  *            
308  *----------------------------------------------------------------------------
309  */
310 static inline void aic3111_write_reg_cache (struct snd_soc_codec *codec, u16 reg, u8 value)
311 {
312         u8 *cache = codec->reg_cache;
313
314         if (reg >= AIC31xx_CACHEREGNUM)
315         {
316                 return;
317         }
318
319         cache[reg] = value;
320 }
321
322 /*
323  *----------------------------------------------------------------------------
324  * Function : aic3111_read
325  * Purpose  : This function is to read the aic3111 register space.
326  *            
327  *----------------------------------------------------------------------------
328  */
329 static unsigned int aic3111_read (struct snd_soc_codec *codec, unsigned int reg)
330 {
331         struct aic3111_priv *aic3111 = aic3111_privdata;
332         u8 value;
333         u8 page = reg / 128;
334
335         if (page == 2 || page > 8) {
336                 printk("aic3111_read::Error page, there's not page %d in codec tlv320aic3111 !!!\n", page);
337                 return -1;
338         }
339
340         reg = reg % 128;
341
342         if (aic3111->page_no != page)
343         {
344                 aic3111_change_page (codec, page);
345         }
346
347         i2c_master_send (codec->control_data, (char *) &reg, 1);
348         i2c_master_recv (codec->control_data, &value, 1);
349
350         return value;
351 }
352
353 /*
354  *----------------------------------------------------------------------------
355  * Function : aic3111_write
356  * Purpose  : This function is to write to the aic3111 register space.
357  *            
358  *----------------------------------------------------------------------------
359  */
360 static int aic3111_write (struct snd_soc_codec *codec, unsigned int reg, unsigned int value)
361 {
362         struct aic3111_priv *aic3111 = aic3111_privdata;
363         u8 data[2];
364         u8 page;
365         //printk("enter %s!!!!!!\n",__FUNCTION__);
366         //printk("aic3111_hp_det_gpio =%d!!!!!!\n",gpio_get_value(aic3111_hp_det_gpio));
367         page = reg / 128;
368         data[AIC3111_REG_OFFSET_INDEX] = reg % 128;
369
370         if (page == 2 || page > 9) {
371                 printk("aic3111_write::Error page, there's not page %d in codec tlv320aic3111 !!!\n", page);
372                 return -1;
373         }
374
375         if (aic3111->page_no != page)
376         {
377                 aic3111_change_page (codec, page);
378         }
379
380         /* data is
381         *   D15..D8 aic3111 register offset
382         *   D7...D0 register data
383         */
384         data[AIC3111_REG_DATA_INDEX] = value & AIC3111_8BITS_MASK;
385 #if defined(EN_REG_CACHE)
386         if ((page == 0) || (page == 1))
387         {
388                 aic3111_write_reg_cache (codec, reg, value);
389         }
390 #endif
391         if (codec->hw_write (codec->control_data, data, 2) != 2)
392         {
393                 printk ("Error in i2c write\n");
394                 return -EIO;
395         }
396
397         return 0;
398 }
399
400 static int aic3111_print_register_cache (struct platform_device *pdev)
401 {
402         struct snd_soc_codec *codec = aic3111_codec;
403         u8 *cache = codec->reg_cache;
404         int reg;
405
406         printk ("\n========3110 reg========\n");
407         for (reg = 0; reg < codec->reg_size; reg++) 
408         {
409                 if (reg == 0) printk ("Page 0\n");
410                 if (reg == 128) printk ("\nPage 1\n");
411                 if (reg%16 == 0 && reg != 0 && reg != 128) printk ("\n");
412                 printk("0x%02x, ",aic3111_read(codec,reg));
413         }
414         printk ("\n========3110 cache========\n");
415         for (reg = 0; reg < codec->reg_size; reg++) 
416         {
417                 if (reg == 0) printk ("Page 0\n");
418                 if (reg == 128) printk ("\nPage 1\n");
419                 if (reg%16 == 0 && reg != 0 && reg != 128) printk ("\n");
420                 printk ("0x%02x, ",cache[reg]);
421         }
422         printk ("\n==========================\n");
423         return 0;
424 }
425
426 static void aic3111_soft_reset (void)
427 {
428         struct snd_soc_codec *codec = aic3111_codec;
429
430         AIC_DBG("CODEC::%s\n",__FUNCTION__);
431
432         //aic3111_write (codec, 1, 0x01);
433         aic3111_write (codec, 63, 0x00);
434         gpio_set_value(aic3111_spk_ctl_gpio, GPIO_LOW);
435         msleep(10);
436         aic3111_write (aic3111_codec, (68), 0x01); //disable DRC
437         aic3111_write (aic3111_codec, (128 + 31), 0xc4);
438         aic3111_write (aic3111_codec, (128 + 36), 0x28); //Left Analog Vol to HPL
439         aic3111_write (aic3111_codec, (128 + 37), 0x28); //Right Analog Vol to HPL
440         aic3111_write (aic3111_codec, (128 + 40), 0x4f); //HPL driver PGA
441         aic3111_write (aic3111_codec, (128 + 41), 0x4f); //HPR driver PGA
442         aic3111_power_speaker(POWER_STATE_OFF); 
443         mdelay (20);
444         aic3111_write (codec, 1, 0x00);
445
446         memcpy(codec->reg_cache, aic31xx_reg,
447                sizeof(aic31xx_reg));
448
449         isSetHW = false;
450
451         return;
452 }
453
454 /*
455  *----------------------------------------------------------------------------
456  * Function : aic3111_set_bias_level
457  * Purpose  : This function is to get triggered when dapm events occurs.
458  *            
459  *----------------------------------------------------------------------------
460  */
461 static int aic3111_set_bias_level(struct snd_soc_codec *codec,
462                         enum snd_soc_bias_level level)
463 {
464         struct aic3111_priv *aic3111 = aic3111_privdata;
465         u8 value;
466
467         if (isSetHW)
468                 return 0;
469
470         AIC_DBG ("CODEC::%s>>>>>>level:%d>>>>master:%d\n", __FUNCTION__, level, aic3111->master);
471
472         switch (level) {
473          /* full On */
474         case SND_SOC_BIAS_ON:
475         /* all power is driven by DAPM system */
476                 if (aic3111->master)
477                 {
478                          /* Switch on PLL */
479                         value = aic3111_read(codec, CLK_REG_2);
480                         aic3111_write(codec, CLK_REG_2, (value | ENABLE_PLL));
481
482                         /* Switch on NDAC Divider */
483                         value = aic3111_read(codec, NDAC_CLK_REG);
484                         aic3111_write(codec, NDAC_CLK_REG, value | ENABLE_NDAC);
485
486                         /* Switch on MDAC Divider */
487                         value = aic3111_read(codec, MDAC_CLK_REG);
488                         aic3111_write(codec, MDAC_CLK_REG, value | ENABLE_MDAC);
489
490                         /* Switch on NADC Divider */
491                         value = aic3111_read(codec, NADC_CLK_REG);
492                         aic3111_write(codec, NADC_CLK_REG, value | ENABLE_MDAC);
493
494                         /* Switch on MADC Divider */
495                         value = aic3111_read(codec, MADC_CLK_REG);
496                         aic3111_write(codec, MADC_CLK_REG, value | ENABLE_MDAC);
497
498                         /* Switch on BCLK_N Divider */
499                         value = aic3111_read(codec, BCLK_N_VAL);
500                         aic3111_write(codec, BCLK_N_VAL, value | ENABLE_BCLK);
501                 } else {
502                         /* Switch on PLL */
503                         value = aic3111_read(codec, CLK_REG_2);
504                         aic3111_write(codec, CLK_REG_2, (value | ENABLE_PLL));
505
506                         /* Switch on NDAC Divider */
507                         value = aic3111_read(codec, NDAC_CLK_REG);
508                         aic3111_write(codec, NDAC_CLK_REG, value | ENABLE_NDAC);
509
510                         /* Switch on MDAC Divider */
511                         value = aic3111_read(codec, MDAC_CLK_REG);
512                         aic3111_write(codec, MDAC_CLK_REG, value | ENABLE_MDAC);
513
514                         /* Switch on NADC Divider */
515                         value = aic3111_read(codec, NADC_CLK_REG);
516                         aic3111_write(codec, NADC_CLK_REG, value | ENABLE_MDAC);
517
518                         /* Switch on MADC Divider */
519                         value = aic3111_read(codec, MADC_CLK_REG);
520                         aic3111_write(codec, MADC_CLK_REG, value | ENABLE_MDAC);
521
522                         /* Switch on BCLK_N Divider */
523                         value = aic3111_read(codec, BCLK_N_VAL);
524                         aic3111_write(codec, BCLK_N_VAL, value | ENABLE_BCLK);
525                 }
526                 break;
527
528         /* partial On */
529         case SND_SOC_BIAS_PREPARE:
530                 break;
531
532         /* Off, with power */
533         case SND_SOC_BIAS_STANDBY:
534                 /*
535                  * all power is driven by DAPM system,
536                  * so output power is safe if bypass was set
537                  */
538                 if (aic3111->master)
539                 {
540                         /* Switch off PLL */
541                         value = aic3111_read(codec, CLK_REG_2);
542                         aic3111_write(codec, CLK_REG_2, (value & ~ENABLE_PLL));
543
544                         /* Switch off NDAC Divider */
545                         value = aic3111_read(codec, NDAC_CLK_REG);
546                         aic3111_write(codec, NDAC_CLK_REG, value & ~ENABLE_NDAC);
547
548                         /* Switch off MDAC Divider */
549                         value = aic3111_read(codec, MDAC_CLK_REG);
550                         aic3111_write(codec, MDAC_CLK_REG, value & ~ENABLE_MDAC);
551
552                         /* Switch off NADC Divider */
553                         value = aic3111_read(codec, NADC_CLK_REG);
554                         aic3111_write(codec, NADC_CLK_REG, value & ~ENABLE_NDAC);
555
556                         /* Switch off MADC Divider */
557                         value = aic3111_read(codec, MADC_CLK_REG);
558                         aic3111_write(codec, MADC_CLK_REG, value & ~ENABLE_MDAC);
559                         value = aic3111_read(codec, BCLK_N_VAL);
560
561                         /* Switch off BCLK_N Divider */
562                         aic3111_write(codec, BCLK_N_VAL, value & ~ENABLE_BCLK);
563                 }
564                 break;
565
566         /* Off, without power */
567         case SND_SOC_BIAS_OFF:
568                 /* force all power off */
569                 break;
570         }
571         codec->dapm.bias_level = level;
572         return 0;
573 }
574
575 /* the structure contains the different values for mclk */
576 static const struct aic3111_rate_divs aic3111_divs[] = {
577 /* 
578  * mclk, rate, p_val, pll_j, pll_d, dosr, ndac, mdac, aosr, nadc, madc, blck_N, 
579  * codec_speficic_initializations 
580  */
581   /* 8k rate */
582   {12000000, 8000, 1, 7, 6800, 768,  5, 3, 128, 5, 18, 24},
583   //{12288000, 8000, 1, 7, 8643, 768,  5, 3, 128, 5, 18, 24},
584   {24000000, 8000, 2, 7, 6800, 768, 15, 1,  64, 45, 4, 24},
585   /* 11.025k rate */
586   {12000000, 11025, 1, 7, 5264, 512,  8, 2, 128,  8, 8, 16},
587   {24000000, 11025, 2, 7, 5264, 512, 16, 1,  64, 32, 4, 16},
588   /* 16k rate */
589   {12000000, 16000, 1, 7, 6800, 384,  5, 3, 128,  5, 9, 12},
590   {24000000, 16000, 2, 7, 6800, 384, 15, 1,  64, 18, 5, 12},
591   /* 22.05k rate */
592   {12000000, 22050, 1, 7, 5264, 256,  4, 4, 128,  4, 8, 8},
593   {24000000, 22050, 2, 7, 5264, 256, 16, 1,  64, 16, 4, 8},
594   /* 32k rate */
595   {12000000, 32000, 1, 7, 1680, 192, 2, 7, 64, 2, 21, 6},
596   {24000000, 32000, 2, 7, 1680, 192, 7, 2, 64, 7,  6, 6},
597   /* 44.1k rate */
598   {12000000, 44100, 1, 7, 5264, 128, 2, 8, 128, 2, 8, 4},
599   {11289600, 44100, 1, 8,    0, 128, 4, 4, 128, 4, 4, 4},
600   {24000000, 44100, 2, 7, 5264, 128, 8, 2,  64, 8, 4, 4},
601   /* 48k rate */
602   {12000000, 48000, 1, 8, 1920, 128, 2, 8, 128, 2, 8, 4},
603   {24000000, 48000, 2, 8, 1920, 128, 8, 2,  64, 8, 4, 4},
604   /*96k rate */
605   {12000000, 96000, 1, 8, 1920, 64, 2, 8, 64, 2, 8, 2},
606   {24000000, 96000, 2, 8, 1920, 64, 4, 4, 64, 8, 2, 2},
607   /*192k */
608   {12000000, 192000, 1, 8, 1920, 32, 2, 8, 32, 2, 8, 1},
609   {24000000, 192000, 2, 8, 1920, 32, 4, 4, 32, 4, 4, 1},
610 };
611
612 /*
613  *----------------------------------------------------------------------------
614  * Function : aic3111_get_divs
615  * Purpose  : This function is to get required divisor from the "aic3111_divs"
616  *            table.
617  *            
618  *----------------------------------------------------------------------------
619  */
620 static inline int aic3111_get_divs (int mclk, int rate)
621 {
622         int i;
623
624         AIC_DBG("Enter::%s\n",__FUNCTION__);
625
626         for (i = 0; i < ARRAY_SIZE (aic3111_divs); i++)
627         {
628                 if ((aic3111_divs[i].rate == rate) && (aic3111_divs[i].mclk == mclk))
629                 {
630                         return i;
631                 }
632         }
633
634         printk ("Master clock and sample rate is not supported\n");
635         return -EINVAL;
636 }
637
638 /*
639  *----------------------------------------------------------------------------
640  * Function : aic3111_hw_params
641  * Purpose  : This function is to set the hardware parameters for AIC3111.
642  *            The functions set the sample rate and audio serial data word 
643  *            length.
644  *            
645  *----------------------------------------------------------------------------
646  */
647 static int aic3111_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params,
648                        struct snd_soc_dai *codec_dai)
649 {
650         struct snd_soc_codec *codec = aic3111_codec;
651         struct aic3111_priv *aic3111 = aic3111_privdata;
652         int i;
653         u8 data;
654
655         if (isSetHW)
656                 return 0;
657
658         AIC_DBG("CODEC::%s\n", __FUNCTION__);
659
660         aic3111_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
661
662         i = aic3111_get_divs(aic3111->sysclk, params_rate (params));
663
664         if (i < 0) {
665                 printk ("sampling rate not supported\n");
666                 return i;
667         }
668
669         /* We will fix R value to 1 and will make P & J=K.D as varialble */
670
671         /* Setting P & R values */
672         aic3111_write(codec, CLK_REG_2, ((aic3111_divs[i].p_val << 4) | 0x01));
673
674         /* J value */
675         aic3111_write(codec, CLK_REG_3, aic3111_divs[i].pll_j);
676
677         /* MSB & LSB for D value */
678         aic3111_write(codec, CLK_REG_4, (aic3111_divs[i].pll_d >> 8));
679         aic3111_write(codec, CLK_REG_5, (aic3111_divs[i].pll_d & AIC3111_8BITS_MASK));
680
681         /* NDAC divider value */
682         aic3111_write(codec, NDAC_CLK_REG, aic3111_divs[i].ndac);
683
684         /* MDAC divider value */
685         aic3111_write(codec, MDAC_CLK_REG, aic3111_divs[i].mdac);
686
687         /* DOSR MSB & LSB values */
688         aic3111_write(codec, DAC_OSR_MSB, aic3111_divs[i].dosr >> 8);
689         aic3111_write(codec, DAC_OSR_LSB, aic3111_divs[i].dosr & AIC3111_8BITS_MASK);
690
691         /* NADC divider value */
692         aic3111_write(codec, NADC_CLK_REG, aic3111_divs[i].nadc);
693
694         /* MADC divider value */
695         aic3111_write(codec, MADC_CLK_REG, aic3111_divs[i].madc);
696
697         /* AOSR value */
698         aic3111_write(codec, ADC_OSR_REG, aic3111_divs[i].aosr);
699
700         /* BCLK N divider */
701         aic3111_write(codec, BCLK_N_VAL, aic3111_divs[i].blck_N);
702
703         aic3111_set_bias_level(codec, SND_SOC_BIAS_ON);
704
705         data = aic3111_read(codec, INTERFACE_SET_REG_1);
706
707         data = data & ~(3 << 4);
708
709         switch (params_format (params)) {
710         case SNDRV_PCM_FORMAT_S16_LE:
711                 break;
712         case SNDRV_PCM_FORMAT_S20_3LE:
713                 data |= (AIC3111_WORD_LEN_20BITS << DATA_LEN_SHIFT);
714                 break;
715         case SNDRV_PCM_FORMAT_S24_LE:
716                 data |= (AIC3111_WORD_LEN_24BITS << DATA_LEN_SHIFT);
717                 break;
718         case SNDRV_PCM_FORMAT_S32_LE:
719                 data |= (AIC3111_WORD_LEN_32BITS << DATA_LEN_SHIFT);
720                 break;
721         }
722
723         aic3111_write(codec, INTERFACE_SET_REG_1, data);
724
725         return 0;
726 }
727
728 /*
729  *----------------------------------------------------------------------------
730  * Function : aic3111_mute
731  * Purpose  : This function is to mute or unmute the left and right DAC
732  *            
733  *----------------------------------------------------------------------------
734  */
735 static int aic3111_mute (struct snd_soc_dai *codec_dai, int mute)
736 {
737         struct snd_soc_codec *codec = codec_dai->codec;
738         u8 dac_reg;
739
740         AIC_DBG ("CODEC::%s>>>>mute:%d\n", __FUNCTION__, mute);
741
742         dac_reg = aic3111_read (codec, DAC_MUTE_CTRL_REG) & ~MUTE_ON;
743         if (mute)
744                 ;//aic3111_write (codec, DAC_MUTE_CTRL_REG, dac_reg | MUTE_ON);
745         else {
746                 //aic3111_write (codec, DAC_MUTE_CTRL_REG, dac_reg);
747                 isSetHW = true;
748         }
749
750         return 0;
751 }
752
753 /*
754  *----------------------------------------------------------------------------
755  * Function : aic3111_set_dai_sysclk
756  * Purpose  : This function is to set the DAI system clock
757  *            
758  *----------------------------------------------------------------------------
759  */
760 static int aic3111_set_dai_sysclk (struct snd_soc_dai *codec_dai,
761                         int clk_id, unsigned int freq, int dir)
762 {
763         struct aic3111_priv *aic3111 = aic3111_privdata;
764
765         if (isSetHW)
766                 return 0;
767
768         AIC_DBG("Enter %s and line %d\n",__FUNCTION__,__LINE__);
769
770         switch (freq) {
771         case AIC3111_FREQ_11289600:
772         case AIC3111_FREQ_12000000:
773         case AIC3111_FREQ_24000000:
774                 aic3111->sysclk = freq;
775                 return 0;
776         }
777
778         printk ("Invalid frequency to set DAI system clock\n");
779         return -EINVAL;
780 }
781
782 /*
783  *----------------------------------------------------------------------------
784  * Function : aic3111_set_dai_fmt
785  * Purpose  : This function is to set the DAI format
786  *            
787  *----------------------------------------------------------------------------
788  */
789 static int aic3111_set_dai_fmt (struct snd_soc_dai *codec_dai, unsigned int fmt)
790 {
791         struct snd_soc_codec *codec = codec_dai->codec;
792         struct aic3111_priv *aic3111 = aic3111_privdata;
793         u8 iface_reg;
794
795         if (isSetHW)
796                 return 0;
797
798         AIC_DBG("Enter %s and line %d\n",__FUNCTION__,__LINE__);
799
800         iface_reg = aic3111_read (codec, INTERFACE_SET_REG_1);
801         iface_reg = iface_reg & ~(3 << 6 | 3 << 2);   //set I2S mode BCLK and WCLK is input
802
803         /* set master/slave audio interface */
804         switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
805         case SND_SOC_DAIFMT_CBM_CFM:
806                 aic3111->master = 1;
807                 iface_reg |= BIT_CLK_MASTER | WORD_CLK_MASTER;
808                 break;
809         case SND_SOC_DAIFMT_CBS_CFS:
810                 aic3111->master = 0;
811                 iface_reg &= ~(BIT_CLK_MASTER | WORD_CLK_MASTER);
812                 break;
813         case SND_SOC_DAIFMT_CBS_CFM:
814                 aic3111->master = 0;
815                 iface_reg |= BIT_CLK_MASTER;
816                 iface_reg &= ~(WORD_CLK_MASTER);
817                 break;
818         default:
819                 printk ("Invalid DAI master/slave interface\n");
820                 return -EINVAL;
821         }
822
823         /* interface format */
824         switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
825         case SND_SOC_DAIFMT_I2S:
826                 break;
827         case SND_SOC_DAIFMT_DSP_A:
828                 iface_reg |= (AIC3111_DSP_MODE << AUDIO_MODE_SHIFT);
829                 break;
830         case SND_SOC_DAIFMT_RIGHT_J:
831                 iface_reg |= (AIC3111_RIGHT_JUSTIFIED_MODE << AUDIO_MODE_SHIFT);
832                 break;
833         case SND_SOC_DAIFMT_LEFT_J:
834                 iface_reg |= (AIC3111_LEFT_JUSTIFIED_MODE << AUDIO_MODE_SHIFT);
835                 break;
836         default:
837                 printk ("Invalid DAI interface format\n");
838                 return -EINVAL;
839         }
840
841         aic3111_write (codec, INTERFACE_SET_REG_1, iface_reg);
842
843         return 0;
844 }
845
846
847 /*
848  *----------------------------------------------------------------------------
849  * Function : aic3111_power_headphone
850  * Purpose  : 
851  * parameter: on = 1: power up; 
852  *            on = 0: power dn; 
853  * xjq@rock-chips.com     
854  *----------------------------------------------------------------------------
855  */
856 static int aic3111_power_headphone (bool on)
857 {
858         struct snd_soc_codec *codec = aic3111_codec;
859
860         AIC_DBG("Enter %s and line %d\n",__FUNCTION__,__LINE__);
861
862         if (on == POWER_STATE_ON) {
863                 aic3111_write (codec, (63), 0xd4);
864 //              aic3111_write(codec, (128 + 35), 0x88);
865                 aic3111_write (codec, (68), 0x01); //disable DRC
866                 aic3111_write (codec, (128 + 31), 0xc4);
867                 aic3111_write (codec, (128 + 44), 0x00);
868                 aic3111_write (codec, (128 + 36), 0x28); //Left Analog Vol to HPL
869                 aic3111_write (codec, (128 + 37), 0x28); //Right Analog Vol to HPL
870 //              aic3111_write (codec, (128 + 40), 0x06); //HPL driver PGA
871 //              aic3111_write (codec, (128 + 41), 0x06); //HPR driver PGA
872                 aic3111_write (codec, (128 + 40), 0x4f); //HPL driver PGA
873                 aic3111_write (codec, (128 + 41), 0x4f); //HPR driver PGA
874
875         } else if (on == POWER_STATE_OFF) {
876
877                 aic3111_write (codec, (128 + 31), 0x00);
878                 aic3111_write (codec, (128 + 44), 0x00);
879                 aic3111_write (codec, (128 + 36), 0xff);
880                 aic3111_write (codec, (128 + 37), 0xff);
881                 aic3111_write (codec, (128 + 40), 0x02);
882                 aic3111_write (codec, (128 + 41), 0x02);
883         }
884
885         return 0;
886 }
887
888 /*
889  *----------------------------------------------------------------------------
890  * Function : aic3111_power_speaker
891  * Purpose  : 
892  * parameter: on = 1: power up; 
893  *            on = 0: power dn; 
894  * xjq@rock-chips.com     
895  *----------------------------------------------------------------------------
896  */
897 static int aic3111_power_speaker (bool on)
898 {
899         struct snd_soc_codec *codec = aic3111_codec;
900
901         AIC_DBG("Enter %s and line %d\n",__FUNCTION__,__LINE__);
902
903         if (on == POWER_STATE_ON) {
904 #if 0
905 //              aic3111_write(codec, (128 + 32), 0x86);
906                 aic3111_write(codec, (128 + 32), 0xc6);
907                 aic3111_write(codec, (128 + 30), 0x00);  
908 //              aic3111_write(codec, (128 + 38), 0x08); //set left speaker analog gain to -4.88db
909                 aic3111_write(codec, (128 + 38), 0x16); //set left speaker analog gain to -4.88db
910                 aic3111_write(codec, (128 + 39), 0x16); //Right Analog Vol to SPR
911 //              aic3111_write(codec, (128 + 38), 0x7f); //set left speaker analog gain to -4.88db
912 //              aic3111_write(codec, (128 + 39), 0x7f); //Right Analog Vol to SPR
913                 aic3111_write(codec, (128 + 42), 0x1d); //set left speaker driver gain to 12db
914                 aic3111_write(codec, (128 + 43), 0x1d); //bit3-4 output stage gain
915 //              aic3111_write(codec, (128 + 43), 0x00); //bit3-4 output stage gain
916                 aic3111_write(codec, (37), 0x98);
917
918 #if 1           /* DRC */
919                 aic3111_write(codec, (60), 0x02); //select PRB_P2
920                 aic3111_write(codec, (68), 0x61); //enable left and right DRC, set DRC threshold to -3db, set DRC hystersis to 1db
921                 aic3111_write(codec, (69), 0x00); //set hold time disable
922                 aic3111_write(codec, (70), 0x5D); //set attack time to 0.125db per sample period and decay time to 0.000488db per sample
923 #endif
924 #endif
925 #if 1
926                 aic3111_write(codec, (63), 0xfc);
927                 aic3111_write(codec, (128 + 32), 0xc6);
928                 aic3111_write(codec, (128 + 30), 0x00);
929                 aic3111_write(codec, (128 + 39), 0x08); //set left speaker analog gain to -4.88db
930                 aic3111_write(codec, (128 + 38), 0x08); //Right Analog Vol to SPR
931                 aic3111_write(codec, (128 + 43), 0x0D); //set left speaker driver gain to 12db
932                 aic3111_write(codec, (128 + 42), 0x0D); //bit3-4 output stage gain
933                 aic3111_write(codec, (37), 0x99);
934
935 #if 1           /* DRC */
936                 aic3111_write(codec, (60), 0x02); //select PRB_P2
937                 aic3111_write(codec, (68), 0x61); //enable left and right DRC, set DRC threshold to -3db, set DRC hystersis to 1db
938                 aic3111_write(codec, (69), 0x00); //set hold time disable
939                 aic3111_write(codec, (70), 0x5D); //set attack time to 0.125db per sample period and decay time to 0.000488db per sample
940 #endif
941 #endif
942         } else if (on == POWER_STATE_OFF) {
943
944                 aic3111_write(codec, (68), 0x01); //disable DRC
945                 aic3111_write(codec, (128 + 32), 0x06);
946                 aic3111_write(codec, (128 + 30), 0x00);
947                 aic3111_write(codec, (128 + 38), 0xff);
948                 aic3111_write(codec, (128 + 39), 0xff);
949                 aic3111_write(codec, (128 + 42), 0x00);
950                 aic3111_write(codec, (128 + 43), 0x00);
951                 aic3111_write(codec, (37), 0x00);
952         }
953
954         return 0;
955 }
956
957 /*
958  *----------------------------------------------------------------------------
959  * Function : aic3111_HS_switch
960  * Purpose  : This function is to initialise the AIC3111 driver
961  *            In PLAYBACK, switch between HP and SPK app.       
962  * parameter: on = 1: SPK power up & HP  power dn;      
963  *            on = 0: HP  power up & SPK power dn;              
964  * xjq@rock-chips.com     
965  *----------------------------------------------------------------------------
966  */
967 static int aic3111_HS_switch (bool on)
968 {
969         AIC_DBG("enter %s and line %d\n",__FUNCTION__,__LINE__);
970
971         if (POWER_STATE_SW_SPK == on) {
972
973                 //aic3111_power_headphone (POWER_STATE_OFF);
974                 aic3111_power_speaker (POWER_STATE_ON);
975         } else if (POWER_STATE_SW_HP == on) {
976
977                 aic3111_power_speaker (POWER_STATE_OFF);
978                 //aic3111_power_headphone (POWER_STATE_ON);
979
980                 //aic3111_power_speaker (POWER_STATE_ON);
981                 //aic3111_power_headphone (POWER_STATE_OFF);
982         }
983
984         return 0;
985 }
986
987 /*
988  *----------------------------------------------------------------------------
989  * Function : aic3111_SPK_HS_powerdown
990  * Purpose  : This function is to power down HP and SPK.                        
991  * xjq@rock-chips.com
992  *----------------------------------------------------------------------------
993  */
994 static int aic3111_SPK_HS_powerdown (void)
995 {
996         AIC_DBG("enter %s and line %d\n",__FUNCTION__,__LINE__);
997
998         //aic3111_power_headphone (POWER_STATE_OFF);
999         aic3111_power_speaker (POWER_STATE_OFF);
1000 //      aic3111_power_speaker (POWER_STATE_ON);
1001
1002         return 0;
1003 }
1004
1005 /*
1006  *----------------------------------------------------------------------------
1007  * Function : aic3111_power_init
1008  * Purpose  : pll clock setting
1009  * parameter: on = 1: power up; 
1010  *            on = 0: power dn; 
1011  * xjq@rock-chips.com     
1012  *----------------------------------------------------------------------------
1013  */
1014 static void aic3111_power_init (void)
1015 {
1016         struct snd_soc_codec *codec = aic3111_codec;
1017
1018         AIC_DBG("enter %s and line %d\n",__FUNCTION__,__LINE__);
1019
1020         if (!(aic3111_current_status & AIC3110_IS_INITPOWER_ON)) {
1021
1022                 AIC_DBG ("CODEC::%s\n", __FUNCTION__);
1023
1024                 aic3111_write(codec, (128 + 46), 0x0b);
1025                 aic3111_write(codec, (128 + 35), 0x44);
1026                 aic3111_write(codec,  (4), 0x03);
1027                 aic3111_write(codec, (29), 0x01);
1028                 aic3111_write(codec, (48), 0xC0);
1029                 aic3111_write(codec, (51), 0x14);
1030                 aic3111_write(codec, (67), 0x82);
1031
1032                 aic3111_current_status |= AIC3110_IS_INITPOWER_ON;
1033         }
1034
1035         return;
1036 }
1037
1038 /*
1039  *----------------------------------------------------------------------------
1040  * Function : aic3111_power_playback
1041  * Purpose  : 
1042  * parameter: on = 1: power up; 
1043  *            on = 0: power dn; 
1044  * xjq@rock-chips.com     
1045  *----------------------------------------------------------------------------
1046  */
1047 static int aic3111_power_playback (bool on)
1048 {
1049         struct snd_soc_codec *codec = aic3111_codec;
1050
1051         AIC_DBG ("CODEC::%s>>>>>>%d\n", __FUNCTION__, on);
1052         gpio_set_value(aic3111_spk_ctl_gpio, GPIO_LOW);
1053         aic3111_power_init();
1054
1055         if ((on == POWER_STATE_ON) &&
1056             !(aic3111_current_status & AIC3110_IS_PLAYBACK_ON)) {
1057 //      if(1){
1058                 //gpio_set_value(aic3111_spk_ctl_gpio, GPIO_HIGH);
1059
1060                 /****open HPL and HPR*******/
1061                 //aic3111_write(codec, (63), 0xfc);
1062                 msleep(10);
1063                 aic3111_write(codec, (65), 0x00); //LDAC VOL
1064                 aic3111_write(codec, (66), 0x00); //RDAC VOL
1065                 aic3111_write (aic3111_codec, (63), 0xd4);
1066 //              aic3111_write(codec, (128 + 35), 0x88);
1067
1068                 //aic3111_write (aic3111_codec, (68), 0x01); //disable DRC
1069                 //aic3111_write (aic3111_codec, (128 + 31), 0xc4);
1070                 aic3111_write (aic3111_codec, (128 + 44), 0x00);
1071                 //aic3111_write (aic3111_codec, (128 + 36), 0x28); //Left Analog Vol to HPL
1072                 //aic3111_write (aic3111_codec, (128 + 37), 0x28); //Right Analog Vol to HPL
1073                 aic3111_write (codec, (128 + 40), 0x06); //HPL driver PGA
1074                 aic3111_write (codec, (128 + 41), 0x06); //HPR driver PGA
1075                 //aic3111_write (aic3111_codec, (128 + 40), 0x4f); //HPL driver PGA
1076                 //aic3111_write (aic3111_codec, (128 + 41), 0x4f); //HPR driver PGA
1077                 //printk("HP INIT~~~~~~~~~~~~~~~~~~~~~~~~~`\n");
1078                 /***************************/           
1079                 
1080                 aic3111_HS_switch(isHSin);
1081
1082                 aic3111_write(codec, (65), 0x10); //LDAC VOL to +8db
1083                 aic3111_write(codec, (66), 0x10); //RDAC VOL to +8db
1084                 msleep(10);
1085                 aic3111_write(codec, (64), 0x00);
1086                 
1087                 aic3111_current_status |= AIC3110_IS_PLAYBACK_ON;
1088
1089         } else if ((on == POWER_STATE_OFF) &&
1090                    (aic3111_current_status & AIC3110_IS_PLAYBACK_ON)) {
1091
1092                 aic3111_write(codec, (68), 0x01); //disable DRC
1093                 aic3111_write(codec, (64), 0x0c);
1094                 aic3111_write(codec, (63), 0x00);
1095                 aic3111_write(codec, (65), 0x00); //LDAC VOL
1096                 aic3111_write(codec, (66), 0x00); //RDAC VOL
1097
1098                 aic3111_SPK_HS_powerdown();
1099
1100                 aic3111_current_status &= ~AIC3110_IS_PLAYBACK_ON;
1101         }
1102         //mdelay(800);
1103         gpio_set_value(aic3111_spk_ctl_gpio, GPIO_HIGH);
1104
1105         return 0;
1106 }
1107
1108 /*
1109  *----------------------------------------------------------------------------
1110  * Function : aic3111_power_capture
1111  * Purpose  : 
1112  * parameter: on = 1: power up; 
1113  *            on = 0: power dn; 
1114  * xjq@rock-chips.com     
1115  *----------------------------------------------------------------------------
1116  */
1117 static int aic3111_power_capture (bool on)
1118 {
1119         struct snd_soc_codec *codec = aic3111_codec;
1120
1121         AIC_DBG ("CODEC::%s>>>>>>%d\n", __FUNCTION__, on);
1122
1123         aic3111_power_init();
1124
1125         if ((on == POWER_STATE_ON) &&
1126             !(aic3111_current_status & AIC3110_IS_CAPTURE_ON)) {
1127                 aic3111_write(codec, (64), 0x0c);
1128                 msleep(10);
1129
1130                 aic3111_write(codec, (61), 0x0b);
1131                 aic3111_write(codec, (128 + 47), 0x00); //MIC PGA 0x80:0dB  0x14:10dB  0x28:20dB  0x3c:30dB 0x77:59dB
1132                 aic3111_write(codec, (128 + 48), 0x80); //MIC1LP\MIC1LM RIN = 10.
1133                 aic3111_write(codec, (128 + 49), 0x20);
1134                 aic3111_write(codec, (82), 0x00); //D7=0:0: ADC channel not muted
1135                 aic3111_write(codec, (83), 0x1A); //ADC Digital Volume 0 dB
1136                 aic3111_write(codec, (81), 0x80); //D7=1:ADC channel is powered up.
1137
1138 #if 1
1139                 /*configure register to creat a filter 20~3.5kHz*/
1140
1141                 aic3111_write(codec, (128*4 + 14), 0x7f);
1142                 aic3111_write(codec, (128*4 + 15), 0x00);
1143                 aic3111_write(codec, (128*4 + 16), 0xc0);
1144                 aic3111_write(codec, (128*4 + 17), 0x18);
1145                 aic3111_write(codec, (128*4 + 18), 0x00);
1146
1147                 aic3111_write(codec, (128*4 + 19), 0x00);
1148                 aic3111_write(codec, (128*4 + 20), 0x3f);
1149                 aic3111_write(codec, (128*4 + 21), 0x00);
1150                 aic3111_write(codec, (128*4 + 22), 0x00);
1151                 aic3111_write(codec, (128*4 + 23), 0x00);
1152
1153                 aic3111_write(codec, (128*4 + 24), 0x05);
1154                 aic3111_write(codec, (128*4 + 25), 0xd2);
1155                 aic3111_write(codec, (128*4 + 26), 0x05);
1156                 aic3111_write(codec, (128*4 + 27), 0xd2);
1157                 aic3111_write(codec, (128*4 + 28), 0x05);
1158
1159                 aic3111_write(codec, (128*4 + 29), 0xd2);
1160                 aic3111_write(codec, (128*4 + 30), 0x53);
1161                 aic3111_write(codec, (128*4 + 31), 0xff);
1162                 aic3111_write(codec, (128*4 + 32), 0xc0);
1163                 aic3111_write(codec, (128*4 + 33), 0xb5);
1164 #endif
1165                 msleep(10);
1166                 aic3111_write(codec, (64), 0x00);
1167                 aic3111_current_status |= AIC3110_IS_CAPTURE_ON;
1168
1169         } else if ((on == POWER_STATE_OFF) &&
1170                    (aic3111_current_status & AIC3110_IS_CAPTURE_ON)) {
1171
1172                 aic3111_write(codec, (61), 0x00);
1173                 aic3111_write(codec, (128 + 47), 0x00); //MIC PGA AOL
1174                 aic3111_write(codec, (128 + 48), 0x00);
1175                 aic3111_write(codec, (128 + 50), 0x00);
1176                 aic3111_write(codec, (81), 0x00);
1177                 aic3111_write(codec, (82), 0x80);
1178                 aic3111_write(codec, (83), 0x00); //ADC VOL
1179                 aic3111_write(codec, (86), 0x00);
1180
1181                 aic3111_current_status &= ~AIC3110_IS_CAPTURE_ON;
1182         }
1183
1184         return 0;
1185 }
1186
1187 /*
1188  *----------------------------------------------------------------------------
1189  * Function : aic3111_powerdown
1190  * Purpose  : This function is to power down codec.
1191  *            
1192  *----------------------------------------------------------------------------
1193  */
1194 static void aic3111_powerdown (void)
1195 {
1196         AIC_DBG ("CODEC::%s\n", __FUNCTION__);
1197
1198         if (aic3111_current_status != AIC3110_IS_SHUTDOWN) {
1199                 aic3111_soft_reset();//sai
1200                 aic3111_current_status = AIC3110_IS_SHUTDOWN;
1201         }
1202 }
1203
1204 /*
1205  *----------------------------------------------------------------------------
1206  * Function : aic3111_work
1207  * Purpose  : This function is to respond to HPDET handle.
1208  *            
1209  *----------------------------------------------------------------------------
1210  */
1211 static void aic3111_work (struct work_struct *work)
1212 {
1213         AIC_DBG("Enter %s and line %d\n",__FUNCTION__,__LINE__);
1214
1215         switch (aic3111_work_type) {
1216         case AIC3110_POWERDOWN_NULL:
1217                 break;
1218         case AIC3110_POWERDOWN_PLAYBACK:
1219                 aic3111_power_playback(POWER_STATE_OFF);
1220                 break;
1221         case AIC3110_POWERDOWN_CAPTURE:
1222                 aic3111_power_capture(POWER_STATE_OFF);
1223                 break;
1224         case AIC3110_POWERDOWN_PLAYBACK_CAPTURE:
1225                 aic3111_powerdown();//sai
1226                 break;
1227         default:
1228                 break;
1229         }
1230
1231         aic3111_work_type = AIC3110_POWERDOWN_NULL;
1232 }
1233
1234 /*
1235  *----------------------------------------------------------------------------
1236  * Function : aic3111_startup
1237  * Purpose  : This function is to start up codec.
1238  *            
1239  *----------------------------------------------------------------------------
1240  */
1241 static int aic3111_startup (struct snd_pcm_substream *substream,
1242                           struct snd_soc_dai *dai)
1243 {
1244 /*
1245         struct snd_soc_pcm_runtime *rtd = substream->private_data;
1246         struct snd_soc_codec *codec = aic3111_codec;
1247 */
1248
1249         AIC_DBG ("CODEC::%s----substream->stream:%s \n", __FUNCTION__,
1250                    substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? "PLAYBACK":"CAPTURE");
1251
1252         cancel_delayed_work_sync(&delayed_work);
1253
1254         if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1255
1256                 aic3111_power_playback(POWER_STATE_ON);
1257
1258         } else if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
1259
1260                 aic3111_power_capture(POWER_STATE_ON);
1261         }
1262
1263         return 0;
1264 }
1265
1266 /*
1267  *----------------------------------------------------------------------------
1268  * Function : aic3111_startup
1269  * Purpose  : This function is to shut down codec.
1270  *            
1271  *----------------------------------------------------------------------------
1272  */
1273 static void aic3111_shutdown (struct snd_pcm_substream *substream,
1274                             struct snd_soc_dai *dai)
1275 {
1276         struct snd_soc_dai *codec_dai = dai;
1277
1278         AIC_DBG ("CODEC::%s----substream->stream:%s \n", __FUNCTION__,
1279                    substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? "PLAYBACK":"CAPTURE");
1280
1281         if (!codec_dai->capture_active && !codec_dai->playback_active) {
1282
1283                 cancel_delayed_work_sync(&delayed_work);
1284
1285                 /* If codec is already shutdown, return */
1286                 if (aic3111_current_status == AIC3110_IS_SHUTDOWN)
1287                         return;
1288
1289                 AIC_DBG ("CODEC::Is going to power down aic3111\n");
1290
1291                 aic3111_work_type = AIC3110_POWERDOWN_PLAYBACK_CAPTURE;
1292
1293                 /* If codec is useless, queue work to close it */
1294                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1295                         queue_delayed_work(aic3111_workq, &delayed_work,
1296                                 msecs_to_jiffies(1000));
1297                 }
1298                 else {
1299                         queue_delayed_work(aic3111_workq, &delayed_work,
1300                                 msecs_to_jiffies(3000));
1301                 }
1302         } 
1303         else if (codec_dai->capture_active && !codec_dai->playback_active) {
1304
1305                 cancel_delayed_work_sync(&delayed_work);
1306
1307                 aic3111_work_type = AIC3110_POWERDOWN_PLAYBACK;
1308
1309                 /* Turn off playback and keep record on */
1310                 queue_delayed_work(aic3111_workq, &delayed_work,
1311                         msecs_to_jiffies(1000));
1312         } 
1313         else if (!codec_dai->capture_active && codec_dai->playback_active) {
1314
1315                 cancel_delayed_work_sync(&delayed_work);
1316
1317                 aic3111_work_type = AIC3110_POWERDOWN_CAPTURE;
1318
1319                 /* Turn off record and keep playback on */
1320                 queue_delayed_work(aic3111_workq, &delayed_work,
1321                         msecs_to_jiffies(3000));
1322         }
1323 }
1324
1325 /*
1326  *----------------------------------------------------------------------------
1327  * Function : aic3111_trigger
1328  * Purpose  : This function is to respond to playback trigger.
1329  *            
1330  *----------------------------------------------------------------------------
1331  */
1332 static int aic3111_trigger(struct snd_pcm_substream *substream,
1333                           int status,
1334                           struct snd_soc_dai *dai)
1335 {
1336         struct snd_soc_dai *codec_dai = dai;
1337
1338         if(status == 0)
1339         {
1340                 gpio_set_value(aic3111_spk_ctl_gpio, GPIO_LOW);
1341                 mdelay(10);
1342         }
1343
1344         AIC_DBG ("CODEC::%s----status = %d substream->stream:%s \n", __FUNCTION__, status,
1345                    substream->stream == SNDRV_PCM_STREAM_PLAYBACK ? "PLAYBACK":"CAPTURE");      
1346
1347         if (status == 1 || status == 0) {
1348                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1349                         codec_dai->playback_active = status;
1350                 } else {
1351                         codec_dai->capture_active = status;
1352                 }
1353         }
1354
1355         return 0;
1356 }
1357
1358 static struct snd_soc_dai_ops aic3111_dai_ops = {
1359         .hw_params = aic3111_hw_params,
1360         .digital_mute = aic3111_mute,
1361         .set_sysclk = aic3111_set_dai_sysclk,
1362         .set_fmt = aic3111_set_dai_fmt,
1363         .startup        = aic3111_startup,
1364         .shutdown       = aic3111_shutdown,
1365         .trigger        = aic3111_trigger,
1366 };
1367
1368 static struct snd_soc_dai_driver aic3111_dai[] = {
1369         {
1370                 .name = "AIC3111 HiFi",
1371                 .playback = {
1372                         .stream_name = "HiFi Playback",
1373                         .channels_min = 1,
1374                         .channels_max = 2,
1375                         .rates = AIC3111_RATES,
1376                         .formats = AIC3111_FORMATS,
1377                 },
1378                 .capture = {
1379                         .stream_name = "HiFi Capture",
1380                         .channels_min = 1,
1381                         .channels_max = 2,
1382                         .rates = AIC3111_RATES,
1383                         .formats = AIC3111_FORMATS,
1384                 },
1385                 .ops = &aic3111_dai_ops,
1386         },
1387 };
1388
1389 struct delayed_work aic3111_speaker_delayed_work;
1390 int speakeronoff;
1391
1392 static void  aic3111_speaker_delayed_work_func(struct work_struct  *work)
1393 {
1394         struct snd_soc_codec *codec = aic3111_codec;
1395
1396         if (aic3111_current_status & AIC3110_IS_PLAYBACK_ON){
1397                 if(speakeronoff) {
1398
1399                          //aic3111_write(codec, (128 + 32), 0xc6);
1400                         //printk("reg 128+32 = %x\n"aic3111_read(codec, (128 + 32)));
1401                         isHSin = 0;
1402                         //gpio_set_value(aic3111_spk_ctl_gpio, GPIO_LOW);
1403                         aic3111_power_speaker(POWER_STATE_OFF);
1404                         gpio_set_value(aic3111_spk_ctl_gpio, GPIO_HIGH);
1405                         //aic3111_power_headphone(POWER_STATE_ON);
1406                         //aic3111_write(codec, (128 + 35), 0x88);
1407                         printk("now hp sound\n");
1408                 } else {
1409
1410                         //aic3111_power_speaker(POWER_STATE_ON);
1411
1412                         isHSin = 1;
1413                         //aic3111_power_headphone(POWER_STATE_OFF);
1414                         gpio_set_value(aic3111_spk_ctl_gpio, GPIO_LOW);
1415                         aic3111_power_speaker(POWER_STATE_ON);
1416                         aic3111_write(codec, (128 + 35), 0x44);
1417                         aic3111_write(codec, (63), 0xfc);
1418                         printk("now spk sound\n");
1419
1420                 }
1421         }    
1422         //printk("----------------------------mma7660_work_func------------------------\n");
1423     
1424 }
1425
1426 /**for check hp or spk****/
1427 static int speaker_timer(unsigned long _data)
1428 {
1429         struct speaker_data *spk = (struct speaker_data *)_data;
1430         int new_status;
1431
1432         if (gpio_get_value(aic3111_hp_det_gpio) == 0) {
1433                 new_status = HP;
1434                 isHSin = 0;
1435                 //printk("hp now\n");
1436                 if(old_status != new_status)
1437                 {
1438                         old_status = new_status;
1439                         // printk("new_status = %d,old_status = %d\n",new_status,old_status);
1440                         old_status = new_status;
1441
1442                         schedule_delayed_work(&aic3111_speaker_delayed_work,msecs_to_jiffies(30));
1443                         speakeronoff=1;
1444                         //printk("HS RUN!!!!!!!!!!\n");
1445                 }
1446         }
1447
1448         if (gpio_get_value(aic3111_hp_det_gpio) == 1) {
1449                 new_status = SPK;
1450                 isHSin = 1;
1451                 //printk("speak now\n");
1452                 if(old_status != new_status)
1453                 {
1454                         old_status = new_status;
1455                         printk("new_status = %d,old_status = %d\n",new_status,old_status);
1456                         old_status = new_status;
1457
1458                         schedule_delayed_work(&aic3111_speaker_delayed_work,msecs_to_jiffies(30));
1459                         speakeronoff=0;
1460                         //printk("HS RUN!!!!!!!!!!\n");
1461                 }
1462         }
1463
1464         mod_timer(&spk->timer, jiffies + msecs_to_jiffies(200));
1465
1466         return 0;
1467 }
1468 /*
1469  *----------------------------------------------------------------------------
1470  * Function : aic3111_probe
1471  * Purpose  : This is first driver function called by the SoC core driver.
1472  *            
1473  *----------------------------------------------------------------------------
1474  */
1475 static int aic3111_probe (struct snd_soc_codec *codec)
1476 {
1477         int ret = 0;//, flags, hp_det_irq;
1478
1479         codec->hw_write = (hw_write_t) i2c_master_send;
1480         codec->control_data = aic3111_i2c;
1481         aic3111_codec = codec;
1482
1483 #if 1
1484         gpio_set_value(aic3111_hp_det_gpio,1);
1485         struct speaker_data *spk;
1486
1487         spk = kzalloc(sizeof(struct speaker_data), GFP_KERNEL);
1488         if (spk == NULL) {
1489                 printk("Allocate Memory Failed!\n");
1490                 ret = -ENOMEM;
1491                 //goto exit_gpio_free;
1492         }
1493
1494         setup_timer(&spk->timer, speaker_timer, (unsigned long)spk);
1495         mod_timer(&spk->timer, jiffies + JACK_DET_ADLOOP);
1496         INIT_DELAYED_WORK(&aic3111_speaker_delayed_work, aic3111_speaker_delayed_work_func);
1497
1498 /*********************/
1499         //pio_set_value(aic3111_spk_ctl_gpio, GPIO_LOW);
1500         //aic3111_power_speaker(POWER_STATE_OFF);
1501         //aic3111_power_headphone(POWER_STATE_ON);
1502 #endif
1503
1504         aic3111_workq = create_freezable_workqueue("aic3111");
1505         if (aic3111_workq == NULL) {
1506                 return -ENOMEM;
1507         }
1508
1509 /*      INIT_DELAYED_WORK (&aic3111_hpdet_work, aic3111_hpdet_work_handle);
1510         if (gpio_request (HP_DET_PIN, "hp_det")) {      
1511                 gpio_free (HP_DET_PIN);   
1512                 printk ("CODEC::tlv3110 hp det  pin request error\n");       
1513         }       
1514         else {
1515                 gpio_direction_input (HP_DET_PIN);
1516                 gpio_pull_updown (HP_DET_PIN, PullDisable);
1517                 hp_det_irq = gpio_to_irq (HP_DET_PIN);
1518                 isHSin = gpio_get_value (HP_DET_PIN);
1519                 
1520                 flags = isHSin ? IRQF_TRIGGER_FALLING : IRQF_TRIGGER_RISING;
1521                 ret = request_irq (hp_det_irq, aic3111_hpdet_isr, flags, "hpdet", codec);
1522                 if (ret < 0) {
1523                         printk ("CODEC::request hp_det_irq error\n");
1524                 }
1525         }
1526 */
1527         /* Just Reset codec */
1528         aic3111_soft_reset();
1529         gpio_set_value(aic3111_spk_ctl_gpio, GPIO_LOW);
1530         msleep(10);
1531         aic3111_write (aic3111_codec, (68), 0x01); //disable DRC
1532         aic3111_write (aic3111_codec, (128 + 31), 0xc4);
1533         aic3111_write (aic3111_codec, (128 + 36), 0x28); //Left Analog Vol to HPL
1534         aic3111_write (aic3111_codec, (128 + 37), 0x28); //Right Analog Vol to HPL
1535         aic3111_write (aic3111_codec, (128 + 40), 0x4f); //HPL driver PGA
1536         aic3111_write (aic3111_codec, (128 + 41), 0x4f); //HPR driver PGA
1537
1538         aic3111_set_bias_level (codec, SND_SOC_BIAS_STANDBY);
1539
1540         return 0;
1541 }
1542
1543 /*
1544  *----------------------------------------------------------------------------
1545  * Function : aic3111_remove
1546  * Purpose  : to remove aic3111 soc device 
1547  *            
1548  *----------------------------------------------------------------------------
1549  */
1550 static int aic3111_remove (struct snd_soc_codec *codec)
1551 {
1552         AIC_DBG ("CODEC::%s\n", __FUNCTION__);
1553
1554         /* Disable HPDET irq */
1555         //disable_irq_nosync (HP_DET_PIN);
1556
1557         /* power down chip */
1558         aic3111_set_bias_level (codec, SND_SOC_BIAS_OFF);
1559
1560         return 0;
1561 }
1562
1563 /*
1564  *----------------------------------------------------------------------------
1565  * Function : aic3111_suspend
1566  * Purpose  : This function is to suspend the AIC3111 driver.
1567  *            
1568  *----------------------------------------------------------------------------
1569  */
1570 static int aic3111_suspend (struct snd_soc_codec *codec)
1571 {
1572
1573         AIC_DBG ("CODEC::%s\n", __FUNCTION__);
1574
1575         aic3111_set_bias_level (codec, SND_SOC_BIAS_STANDBY);
1576
1577         aic3111_soft_reset();//sai
1578
1579         return 0;
1580 }
1581
1582 /*
1583  *----------------------------------------------------------------------------
1584  * Function : aic3111_resume
1585  * Purpose  : This function is to resume the AIC3111 driver
1586  *            
1587  *----------------------------------------------------------------------------
1588  */
1589 static int aic3111_resume (struct snd_soc_codec *codec)
1590 {
1591         //isHSin = gpio_get_value(HP_DET_PIN);
1592         aic3111_set_bias_level (codec, SND_SOC_BIAS_STANDBY);
1593         //aic3111_set_bias_level(codec, codec->suspend_bias_level);
1594
1595         return 0;
1596 }
1597
1598 /*
1599  *----------------------------------------------------------------------------
1600  * @struct  snd_soc_codec_device |
1601  *          This structure is soc audio codec device sturecute which pointer
1602  *          to basic functions aic3111_probe(), aic3111_remove(),  
1603  *          aic3111_suspend() and aic3111_resume()
1604  *----------------------------------------------------------------------------
1605  */
1606 static struct snd_soc_codec_driver soc_codec_dev_aic3111 = {
1607         .probe = aic3111_probe,
1608         .remove = aic3111_remove,
1609         .suspend = aic3111_suspend,
1610         .resume = aic3111_resume,
1611         .set_bias_level = aic3111_set_bias_level,
1612         .reg_cache_size = ARRAY_SIZE(aic31xx_reg),
1613         .reg_word_size = sizeof(u16),
1614         .reg_cache_default = aic31xx_reg,
1615         .reg_cache_step = 1,
1616 };
1617
1618 static const struct i2c_device_id tlv320aic3111_i2c_id[] = {
1619         { "aic3111", 0 },
1620         { }
1621 };
1622 MODULE_DEVICE_TABLE(i2c, tlv320aic3111_i2c_id);
1623
1624 /*
1625 dts:
1626         codec@1a {
1627                 compatible = "aic3111";
1628                 reg = <0x1a>;
1629                 spk-ctl-gpio = <&gpio6 GPIO_B5 GPIO_ACTIVE_HIGH>;
1630                 hp-det-pio = <&gpio6 GPIO_B6 GPIO_ACTIVE_HIGH>;
1631         };
1632 */
1633 static int tlv320aic3111_i2c_probe(struct i2c_client *i2c,
1634                     const struct i2c_device_id *id)
1635 {
1636         struct aic3111_priv *aic3111;
1637         int ret;
1638
1639         aic3111 = kzalloc(sizeof(struct aic3111_priv), GFP_KERNEL);
1640         if (NULL == aic3111)
1641                 return -ENOMEM;
1642
1643         aic3111_i2c = i2c;
1644
1645 #ifdef CONFIG_OF
1646         aic3111_spk_ctl_gpio= of_get_named_gpio_flags(i2c->dev.of_node, "spk-ctl-gpio", 0, NULL);
1647         if (aic3111_spk_ctl_gpio < 0) {
1648                 printk("%s() Can not read property spk-ctl-gpio\n", __FUNCTION__);
1649                 aic3111_spk_ctl_gpio = INVALID_GPIO;
1650         }
1651
1652         aic3111_hp_det_gpio = of_get_named_gpio_flags(i2c->dev.of_node, "hp-det-pio", 0, NULL);
1653         if (aic3111_hp_det_gpio < 0) {
1654                 printk("%s() Can not read property hp-det-pio\n", __FUNCTION__);
1655                 aic3111_hp_det_gpio = INVALID_GPIO;
1656         }
1657 #endif //#ifdef CONFIG_OF
1658
1659         i2c_set_clientdata(i2c, aic3111);
1660
1661         aic3111_privdata = aic3111;
1662
1663         ret = snd_soc_register_codec(&i2c->dev, &soc_codec_dev_aic3111,
1664                         aic3111_dai, ARRAY_SIZE(aic3111_dai));
1665         if (ret < 0)
1666                 kfree(aic3111);
1667
1668         return ret;
1669 }
1670
1671 static int tlv320aic3111_i2c_remove(struct i2c_client *client)
1672 {
1673         snd_soc_unregister_codec(&client->dev);
1674         kfree(i2c_get_clientdata(client));
1675         return 0;
1676 }
1677
1678 struct i2c_driver tlv320aic3111_i2c_driver = {
1679         .driver = {
1680                 .name = "AIC3111",
1681                 .owner = THIS_MODULE,
1682         },
1683         .probe = tlv320aic3111_i2c_probe,
1684         .remove   = tlv320aic3111_i2c_remove,
1685         .id_table = tlv320aic3111_i2c_id,
1686 };
1687
1688 static int __init tlv320aic3111_init (void)
1689 {
1690         return i2c_add_driver(&tlv320aic3111_i2c_driver);
1691 }
1692
1693 static void __exit tlv320aic3111_exit (void)
1694 {
1695         i2c_del_driver(&tlv320aic3111_i2c_driver);
1696 }
1697
1698 module_init (tlv320aic3111_init);
1699 module_exit (tlv320aic3111_exit);
1700
1701 MODULE_DESCRIPTION (" ASoC TLV320AIC3111 codec driver ");
1702 MODULE_AUTHOR (" Jaz B John <jazbjohn@mistralsolutions.com> ");
1703 MODULE_LICENSE ("GPL");
1704