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