ASoC: ac108: Fix changed interface
[platform/kernel/linux-rpi.git] / sound / soc / codecs / ac101.c
1 /*
2  * ac101.c
3  *
4  * (C) Copyright 2017-2018
5  * Seeed Technology Co., Ltd. <www.seeedstudio.com>
6  *
7  * PeterYang <linsheng.yang@seeed.cc>
8  *
9  * (C) Copyright 2014-2017
10  * Reuuimlla Technology Co., Ltd. <www.reuuimllatech.com>
11  *
12  * huangxin <huangxin@Reuuimllatech.com>
13  * liushaohua <liushaohua@allwinnertech.com>
14  *
15  * X-Powers AC101 codec driver
16  *
17  * This program is free software; you can redistribute it and/or
18  * modify it under the terms of the GNU General Public License as
19  * published by the Free Software Foundation; either version 2 of
20  * the License, or (at your option) any later version.
21  *
22  */
23
24 /* #undef AC101_DEBG
25  * use 'make DEBUG=1' to enable debugging
26  */
27 #include <linux/module.h>
28 #include <sound/pcm.h>
29 #include <sound/pcm_params.h>
30 #include <sound/soc.h>
31 #include <sound/soc-dapm.h>
32 #include <sound/tlv.h>
33 #include <linux/i2c.h>
34 #include <linux/irq.h>
35 #include <linux/workqueue.h>
36 #include <linux/clk.h>
37 #include <linux/gpio/consumer.h>
38 #include <linux/regmap.h>
39 #include <linux/input.h>
40 #include <linux/delay.h>
41 #include "ac101_regs.h"
42 #include "ac10x.h"
43
44 /*
45  * *** To sync channels ***
46  *
47  * 1. disable clock in codec   hw_params()
48  * 2. clear   fifo  in bcm2835 hw_params()
49  * 3. clear   fifo  in bcm2385 prepare()
50  * 4. enable  RX    in bcm2835 trigger()
51  * 5. enable  clock in machine trigger()
52  */
53
54 /*Default initialize configuration*/
55 static bool speaker_double_used = 1;
56 static int double_speaker_val   = 0x1B;
57 static int single_speaker_val   = 0x19;
58 static int headset_val          = 0x3B;
59 static int mainmic_val          = 0x4;
60 static int headsetmic_val       = 0x4;
61 static bool dmic_used           = 0;
62 static int adc_digital_val      = 0xb0b0;
63 static bool drc_used            = false;
64
65 #define AC101_RATES  (SNDRV_PCM_RATE_8000_96000 &               \
66                 ~(SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_64000 | \
67                 SNDRV_PCM_RATE_88200))
68 #define AC101_FORMATS (/*SNDRV_PCM_FMTBIT_S16_LE | \
69                         SNDRV_PCM_FMTBIT_S24_LE |*/     \
70                         SNDRV_PCM_FMTBIT_S32_LE | \
71                         0)
72
73 static struct ac10x_priv* static_ac10x;
74
75
76 int ac101_read(struct snd_soc_codec *codec, unsigned reg) {
77         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
78         int r, v = 0;
79
80         if ((r = regmap_read(ac10x->regmap101, reg, &v)) < 0) {
81                 dev_err(codec->dev, "read reg %02X fail\n",
82                          reg);
83                 return r;
84         }
85         return v;
86 }
87
88 int ac101_write(struct snd_soc_codec *codec, unsigned reg, unsigned val) {
89         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
90         int v;
91
92         v = regmap_write(ac10x->regmap101, reg, val);
93         return v;
94 }
95
96 int ac101_update_bits(struct snd_soc_codec *codec, unsigned reg,
97                         unsigned mask, unsigned value
98 ) {
99         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
100         int v;
101
102         v = regmap_update_bits(ac10x->regmap101, reg, mask, value);
103         return v;
104 }
105
106
107
108 #ifdef CONFIG_AC101_SWITCH_DETECT
109 /******************************************************************************/
110 /********************************switch****************************************/
111 /******************************************************************************/
112 #define KEY_HEADSETHOOK         226             /* key define */
113 #define HEADSET_FILTER_CNT      (10)
114
115 /*
116  * switch_hw_config:config the 53 codec register
117  */
118 static void switch_hw_config(struct snd_soc_codec *codec)
119 {
120         int r;
121
122         AC101_DBG();
123
124         /*HMIC/MMIC BIAS voltage level select:2.5v*/
125         ac101_update_bits(codec, OMIXER_BST1_CTRL, (0xf<<BIASVOLTAGE), (0xf<<BIASVOLTAGE));
126         /*debounce when Key down or keyup*/
127         ac101_update_bits(codec, HMIC_CTRL1, (0xf<<HMIC_M), (0x0<<HMIC_M));
128         /*debounce when earphone plugin or pullout*/
129         ac101_update_bits(codec, HMIC_CTRL1, (0xf<<HMIC_N), (0x0<<HMIC_N));
130         /*Down Sample Setting Select: Downby 4,32Hz*/
131         ac101_update_bits(codec, HMIC_CTRL2, (0x3<<HMIC_SAMPLE_SELECT), (0x02<<HMIC_SAMPLE_SELECT));
132         /*Hmic_th2 for detecting Keydown or Keyup.*/
133         ac101_update_bits(codec, HMIC_CTRL2, (0x1f<<HMIC_TH2), (0x8<<HMIC_TH2));
134         /*Hmic_th1[4:0],detecting eraphone plugin or pullout*/
135         ac101_update_bits(codec, HMIC_CTRL2, (0x1f<<HMIC_TH1), (0x1<<HMIC_TH1));
136         /*Headset microphone BIAS working mode: when HBIASEN = 1 */
137         ac101_update_bits(codec, ADC_APC_CTRL, (0x1<<HBIASMOD), (0x1<<HBIASMOD));
138         /*Headset microphone BIAS Enable*/
139         ac101_update_bits(codec, ADC_APC_CTRL, (0x1<<HBIASEN), (0x1<<HBIASEN));
140         /*Headset microphone BIAS Current sensor & ADC Enable*/
141         ac101_update_bits(codec, ADC_APC_CTRL, (0x1<<HBIASADCEN), (0x1<<HBIASADCEN));
142         /*Earphone Plugin/out Irq Enable*/
143         ac101_update_bits(codec, HMIC_CTRL1, (0x1<<HMIC_PULLOUT_IRQ), (0x1<<HMIC_PULLOUT_IRQ));
144         ac101_update_bits(codec, HMIC_CTRL1, (0x1<<HMIC_PLUGIN_IRQ), (0x1<<HMIC_PLUGIN_IRQ));
145
146         /*Hmic KeyUp/key down Irq Enable*/
147         ac101_update_bits(codec, HMIC_CTRL1, (0x1<<HMIC_KEYDOWN_IRQ), (0x1<<HMIC_KEYDOWN_IRQ));
148         ac101_update_bits(codec, HMIC_CTRL1, (0x1<<HMIC_KEYUP_IRQ), (0x1<<HMIC_KEYUP_IRQ));
149
150         /*headphone calibration clock frequency select*/
151         ac101_update_bits(codec, SPKOUT_CTRL, (0x7<<HPCALICKS), (0x7<<HPCALICKS));
152
153         /*clear hmic interrupt */
154         r = HMIC_PEND_ALL;
155         ac101_write(codec, HMIC_STS, r);
156
157         return;
158 }
159
160 /*
161  * switch_status_update: update the switch state.
162  */
163 static void switch_status_update(struct ac10x_priv *ac10x)
164 {
165         AC101_DBG("ac10x->state:%d\n", ac10x->state);
166
167         input_report_switch(ac10x->inpdev, SW_HEADPHONE_INSERT, ac10x->state);
168         input_sync(ac10x->inpdev);
169         return;
170 }
171
172 /*
173  * work_cb_clear_irq: clear audiocodec pending and Record the interrupt.
174  */
175 static void work_cb_clear_irq(struct work_struct *work)
176 {
177         int reg_val = 0;
178         struct ac10x_priv *ac10x = container_of(work, struct ac10x_priv, work_clear_irq);
179         struct snd_soc_codec *codec = ac10x->codec;
180
181         ac10x->irq_cntr++;
182
183         reg_val = ac101_read(codec, HMIC_STS);
184         if (BIT(HMIC_PULLOUT_PEND) & reg_val) {
185                 ac10x->pullout_cntr++;
186                 AC101_DBG("ac10x->pullout_cntr: %d\n", ac10x->pullout_cntr);
187         }
188
189         reg_val |= HMIC_PEND_ALL;
190         ac101_write(codec, HMIC_STS, reg_val);
191
192         reg_val = ac101_read(codec, HMIC_STS);
193         if ((reg_val & HMIC_PEND_ALL) != 0){
194                 reg_val |= HMIC_PEND_ALL;
195                 ac101_write(codec, HMIC_STS, reg_val);
196         }
197
198         if (cancel_work_sync(&ac10x->work_switch) != 0) {
199                 ac10x->irq_cntr--;
200         }
201
202         if (0 == schedule_work(&ac10x->work_switch)) {
203                 ac10x->irq_cntr--;
204                 AC101_DBG("[work_cb_clear_irq] add work struct failed!\n");
205         }
206 }
207
208 enum {
209         HBIAS_LEVEL_1 = 0x02,
210         HBIAS_LEVEL_2 = 0x0B,
211         HBIAS_LEVEL_3 = 0x13,
212         HBIAS_LEVEL_4 = 0x17,
213         HBIAS_LEVEL_5 = 0x19,
214 };
215
216 static int __ac101_get_hmic_data(struct snd_soc_codec *codec) {
217         #ifdef AC101_DEBG
218         static long counter;
219         #endif
220         int r, d;
221
222         d = GET_HMIC_DATA(ac101_read(codec, HMIC_STS));
223
224         r = 0x1 << HMIC_DATA_PEND;
225         ac101_write(codec, HMIC_STS, r);
226
227         /* prevent i2c accessing too frequently */
228         usleep_range(1500, 3000);
229
230         AC101_DBG("HMIC_DATA(%3ld): %02X\n", counter++, d);
231         return d;
232 }
233
234 /*
235  * work_cb_earphone_switch: judge the status of the headphone
236  */
237 static void work_cb_earphone_switch(struct work_struct *work)
238 {
239         struct ac10x_priv *ac10x = container_of(work, struct ac10x_priv, work_switch);
240         struct snd_soc_codec *codec = ac10x->codec;
241
242         static int hook_flag1 = 0, hook_flag2 = 0;
243         static int KEY_VOLUME_FLAG = 0;
244
245         unsigned filter_buf = 0;
246         int filt_index = 0;
247         int t = 0;
248
249         ac10x->irq_cntr--;
250
251         /* read HMIC_DATA */
252         t = __ac101_get_hmic_data(codec);
253
254         if ((t >= HBIAS_LEVEL_2) && (ac10x->mode == FOUR_HEADPHONE_PLUGIN)) {
255                 t = __ac101_get_hmic_data(codec);
256
257                 if (t >= HBIAS_LEVEL_5){
258                         msleep(150);
259                         t = __ac101_get_hmic_data(codec);
260                         if (((t < HBIAS_LEVEL_2 && t >= HBIAS_LEVEL_1 - 1) || t >= HBIAS_LEVEL_5)
261                         && (ac10x->pullout_cntr == 0)) {
262                                 input_report_key(ac10x->inpdev, KEY_HEADSETHOOK, 1);
263                                 input_sync(ac10x->inpdev);
264
265                                 AC101_DBG("KEY_HEADSETHOOK1\n");
266
267                                 if (hook_flag1 != hook_flag2)
268                                         hook_flag1 = hook_flag2 = 0;
269                                 hook_flag1++;
270                         }
271                         if (ac10x->pullout_cntr)
272                                 ac10x->pullout_cntr--;
273                 } else if (t >= HBIAS_LEVEL_4) {
274                         msleep(80);
275                         t = __ac101_get_hmic_data(codec);
276                         if (t < HBIAS_LEVEL_5 && t >= HBIAS_LEVEL_4 && (ac10x->pullout_cntr == 0)) {
277                                 KEY_VOLUME_FLAG = 1;
278                                 input_report_key(ac10x->inpdev, KEY_VOLUMEUP, 1);
279                                 input_sync(ac10x->inpdev);
280                                 input_report_key(ac10x->inpdev, KEY_VOLUMEUP, 0);
281                                 input_sync(ac10x->inpdev);
282
283                                 AC101_DBG("HMIC_DATA: %d KEY_VOLUMEUP\n", t);
284                         }
285                         if (ac10x->pullout_cntr)
286                                 ac10x->pullout_cntr--;
287                 } else if (t >= HBIAS_LEVEL_3){
288                         msleep(80);
289                         t = __ac101_get_hmic_data(codec);
290                         if (t < HBIAS_LEVEL_4 && t >= HBIAS_LEVEL_3  && (ac10x->pullout_cntr == 0)){
291                                 KEY_VOLUME_FLAG = 1;
292                                 input_report_key(ac10x->inpdev, KEY_VOLUMEDOWN, 1);
293                                 input_sync(ac10x->inpdev);
294                                 input_report_key(ac10x->inpdev, KEY_VOLUMEDOWN, 0);
295                                 input_sync(ac10x->inpdev);
296                                 AC101_DBG("KEY_VOLUMEDOWN\n");
297                         }
298                         if (ac10x->pullout_cntr)
299                                 ac10x->pullout_cntr--;
300                 }
301         } else if ((t < HBIAS_LEVEL_2 && t >= HBIAS_LEVEL_1) && (ac10x->mode == FOUR_HEADPHONE_PLUGIN)) {
302                 t = __ac101_get_hmic_data(codec);
303                 if (t < HBIAS_LEVEL_2 && t >= HBIAS_LEVEL_1) {
304                         if (KEY_VOLUME_FLAG) {
305                                 KEY_VOLUME_FLAG = 0;
306                         }
307                         if (hook_flag1 == (++hook_flag2)) {
308                                 hook_flag1 = hook_flag2 = 0;
309                                 input_report_key(ac10x->inpdev, KEY_HEADSETHOOK, 0);
310                                 input_sync(ac10x->inpdev);
311
312                                 AC101_DBG("KEY_HEADSETHOOK0\n");
313                         }
314                 }
315         } else {
316                 while (ac10x->irq_cntr == 0 && ac10x->irq != 0) {
317                         msleep(20);
318
319                         t = __ac101_get_hmic_data(codec);
320
321                         if (filt_index <= HEADSET_FILTER_CNT) {
322                                 if (filt_index++ == 0) {
323                                         filter_buf = t;
324                                 } else if (filter_buf != t) {
325                                         filt_index = 0;
326                                 }
327                                 continue;
328                         }
329
330                         filt_index = 0;
331                         if (filter_buf >= HBIAS_LEVEL_2) {
332                                 ac10x->mode = THREE_HEADPHONE_PLUGIN;
333                                 ac10x->state = 2;
334                         } else if (filter_buf >= HBIAS_LEVEL_1 - 1) {
335                                 ac10x->mode = FOUR_HEADPHONE_PLUGIN;
336                                 ac10x->state = 1;
337                         } else {
338                                 ac10x->mode = HEADPHONE_IDLE;
339                                 ac10x->state = 0;
340                         }
341                         switch_status_update(ac10x);
342                         ac10x->pullout_cntr = 0;
343                         break;
344                 }
345         }
346 }
347
348 /*
349  * audio_hmic_irq:  the interrupt handlers
350  */
351 static irqreturn_t audio_hmic_irq(int irq, void *para)
352 {
353         struct ac10x_priv *ac10x = (struct ac10x_priv *)para;
354         if (ac10x == NULL) {
355                 return -EINVAL;
356         }
357
358         if (0 == schedule_work(&ac10x->work_clear_irq)){
359                 AC101_DBG("[audio_hmic_irq] work already in queue_codec_irq, adding failed!\n");
360         }
361         return IRQ_HANDLED;
362 }
363
364 static int ac101_switch_probe(struct ac10x_priv *ac10x) {
365         struct i2c_client *i2c = ac10x->i2c101;
366         long ret;
367
368         ac10x->gpiod_irq = devm_gpiod_get_optional(&i2c->dev, "switch-irq", GPIOD_IN);
369         if (IS_ERR(ac10x->gpiod_irq)) {
370                 ac10x->gpiod_irq = NULL;
371                 dev_err(&i2c->dev, "failed get switch-irq in device tree\n");
372                 goto _err_irq;
373         }
374
375         gpiod_direction_input(ac10x->gpiod_irq);
376
377         ac10x->irq = gpiod_to_irq(ac10x->gpiod_irq);
378         if (IS_ERR_VALUE(ac10x->irq)) {
379                 pr_info("[ac101] map gpio to irq failed, errno = %ld\n", ac10x->irq);
380                 ac10x->irq = 0;
381                 goto _err_irq;
382         }
383
384         /* request irq, set irq type to falling edge trigger */
385         ret = devm_request_irq(ac10x->codec->dev, ac10x->irq, audio_hmic_irq, IRQF_TRIGGER_FALLING, "SWTICH_EINT", ac10x);
386         if (IS_ERR_VALUE(ret)) {
387                 pr_info("[ac101] request virq %ld failed, errno = %ld\n", ac10x->irq, ret);
388                 goto _err_irq;
389         }
390
391         ac10x->mode = HEADPHONE_IDLE;
392         ac10x->state = -1;
393
394         /*use for judge the state of switch*/
395         INIT_WORK(&ac10x->work_switch, work_cb_earphone_switch);
396         INIT_WORK(&ac10x->work_clear_irq, work_cb_clear_irq);
397
398         /********************create input device************************/
399         ac10x->inpdev = devm_input_allocate_device(ac10x->codec->dev);
400         if (!ac10x->inpdev) {
401                 AC101_DBG("input_allocate_device: not enough memory for input device\n");
402                 ret = -ENOMEM;
403                 goto _err_input_allocate_device;
404         }
405
406         ac10x->inpdev->name          = "seed-voicecard-headset";
407         ac10x->inpdev->phys          = dev_name(ac10x->codec->dev);
408         ac10x->inpdev->id.bustype    = BUS_I2C;
409         ac10x->inpdev->dev.parent    = ac10x->codec->dev;
410         input_set_drvdata(ac10x->inpdev, ac10x->codec);
411
412         ac10x->inpdev->evbit[0] = BIT_MASK(EV_KEY) | BIT(EV_SW);
413
414         set_bit(KEY_HEADSETHOOK, ac10x->inpdev->keybit);
415         set_bit(KEY_VOLUMEUP,    ac10x->inpdev->keybit);
416         set_bit(KEY_VOLUMEDOWN,  ac10x->inpdev->keybit);
417         input_set_capability(ac10x->inpdev, EV_SW, SW_HEADPHONE_INSERT);
418
419         ret = input_register_device(ac10x->inpdev);
420         if (ret) {
421                 AC101_DBG("input_register_device: input_register_device failed\n");
422                 goto _err_input_register_device;
423         }
424
425         /* the first headset state checking */
426         switch_hw_config(ac10x->codec);
427         ac10x->irq_cntr = 1;
428         schedule_work(&ac10x->work_switch);
429
430         return 0;
431
432 _err_input_register_device:
433 _err_input_allocate_device:
434
435         if (ac10x->irq) {
436                 devm_free_irq(&i2c->dev, ac10x->irq, ac10x);
437                 ac10x->irq = 0;
438         }
439 _err_irq:
440         return ret;
441 }
442 /******************************************************************************/
443 /********************************switch****************************************/
444 /******************************************************************************/
445 #endif
446
447
448
449 void drc_config(struct snd_soc_codec *codec)
450 {
451         int reg_val;
452         reg_val = ac101_read(codec, 0xa3);
453         reg_val &= ~(0x7ff<<0);
454         reg_val |= 1<<0;
455         ac101_write(codec, 0xa3, reg_val);
456         ac101_write(codec, 0xa4, 0x2baf);
457
458         reg_val = ac101_read(codec, 0xa5);
459         reg_val &= ~(0x7ff<<0);
460         reg_val |= 1<<0;
461         ac101_write(codec, 0xa5, reg_val);
462         ac101_write(codec, 0xa6, 0x2baf);
463
464         reg_val = ac101_read(codec, 0xa7);
465         reg_val &= ~(0x7ff<<0);
466         ac101_write(codec, 0xa7, reg_val);
467         ac101_write(codec, 0xa8, 0x44a);
468
469         reg_val = ac101_read(codec, 0xa9);
470         reg_val &= ~(0x7ff<<0);
471         ac101_write(codec, 0xa9, reg_val);
472         ac101_write(codec, 0xaa, 0x1e06);
473
474         reg_val = ac101_read(codec, 0xab);
475         reg_val &= ~(0x7ff<<0);
476         reg_val |= (0x352<<0);
477         ac101_write(codec, 0xab, reg_val);
478         ac101_write(codec, 0xac, 0x6910);
479
480         reg_val = ac101_read(codec, 0xad);
481         reg_val &= ~(0x7ff<<0);
482         reg_val |= (0x77a<<0);
483         ac101_write(codec, 0xad, reg_val);
484         ac101_write(codec, 0xae, 0xaaaa);
485
486         reg_val = ac101_read(codec, 0xaf);
487         reg_val &= ~(0x7ff<<0);
488         reg_val |= (0x2de<<0);
489         ac101_write(codec, 0xaf, reg_val);
490         ac101_write(codec, 0xb0, 0xc982);
491
492         ac101_write(codec, 0x16, 0x9f9f);
493
494 }
495
496 void drc_enable(struct snd_soc_codec *codec,bool on)
497 {
498         int reg_val;
499         if (on) {
500                 ac101_write(codec, 0xb5, 0xA080);
501                 reg_val = ac101_read(codec, MOD_CLK_ENA);
502                 reg_val |= (0x1<<6);
503                 ac101_write(codec, MOD_CLK_ENA, reg_val);
504                 reg_val = ac101_read(codec, MOD_RST_CTRL);
505                 reg_val |= (0x1<<6);
506                 ac101_write(codec, MOD_RST_CTRL, reg_val);
507
508                 reg_val = ac101_read(codec, 0xa0);
509                 reg_val |= (0x7<<0);
510                 ac101_write(codec, 0xa0, reg_val);
511         } else {
512                 ac101_write(codec, 0xb5, 0x0);
513                 reg_val = ac101_read(codec, MOD_CLK_ENA);
514                 reg_val &= ~(0x1<<6);
515                 ac101_write(codec, MOD_CLK_ENA, reg_val);
516                 reg_val = ac101_read(codec, MOD_RST_CTRL);
517                 reg_val &= ~(0x1<<6);
518                 ac101_write(codec, MOD_RST_CTRL, reg_val);
519
520                 reg_val = ac101_read(codec, 0xa0);
521                 reg_val &= ~(0x7<<0);
522                 ac101_write(codec, 0xa0, reg_val);
523         }
524 }
525
526 void set_configuration(struct snd_soc_codec *codec)
527 {
528         if (speaker_double_used) {
529                 ac101_update_bits(codec, SPKOUT_CTRL, (0x1f<<SPK_VOL), (double_speaker_val<<SPK_VOL));
530         } else {
531                 ac101_update_bits(codec, SPKOUT_CTRL, (0x1f<<SPK_VOL), (single_speaker_val<<SPK_VOL));
532         }
533         ac101_update_bits(codec, HPOUT_CTRL, (0x3f<<HP_VOL), (headset_val<<HP_VOL));
534         ac101_update_bits(codec, ADC_SRCBST_CTRL, (0x7<<ADC_MIC1G), (mainmic_val<<ADC_MIC1G));
535         ac101_update_bits(codec, ADC_SRCBST_CTRL, (0x7<<ADC_MIC2G), (headsetmic_val<<ADC_MIC2G));
536         if (dmic_used) {
537                 ac101_write(codec, ADC_VOL_CTRL, adc_digital_val);
538         }
539         if (drc_used) {
540                 drc_config(codec);
541         }
542         /*headphone calibration clock frequency select*/
543         ac101_update_bits(codec, SPKOUT_CTRL, (0x7<<HPCALICKS), (0x7<<HPCALICKS));
544
545         /* I2S1 DAC Timeslot 0 data <- I2S1 DAC channel 0 */
546         // "AIF1IN0L Mux" <= "AIF1DACL"
547         // "AIF1IN0R Mux" <= "AIF1DACR"
548         ac101_update_bits(codec, AIF1_DACDAT_CTRL, 0x3 << AIF1_DA0L_SRC, 0x0 << AIF1_DA0L_SRC);
549         ac101_update_bits(codec, AIF1_DACDAT_CTRL, 0x3 << AIF1_DA0R_SRC, 0x0 << AIF1_DA0R_SRC);
550         /* Timeslot 0 Left & Right Channel enable */
551         ac101_update_bits(codec, AIF1_DACDAT_CTRL, 0x3 << AIF1_DA0R_ENA, 0x3 << AIF1_DA0R_ENA);
552
553         /* DAC Digital Mixer Source Select <- I2S1 DA0 */
554         // "DACL Mixer" += "AIF1IN0L Mux"
555         // "DACR Mixer" += "AIF1IN0R Mux"
556         ac101_update_bits(codec, DAC_MXR_SRC, 0xF << DACL_MXR_ADCL, 0x8 << DACL_MXR_ADCL);
557         ac101_update_bits(codec, DAC_MXR_SRC, 0xF << DACR_MXR_ADCR, 0x8 << DACR_MXR_ADCR);
558         /* Internal DAC Analog Left & Right Channel enable */
559         ac101_update_bits(codec, OMIXER_DACA_CTRL, 0x3 << DACALEN, 0x3 << DACALEN);
560
561         /* Output Mixer Source Select */
562         // "Left Output Mixer"  += "DACL Mixer"
563         // "Right Output Mixer" += "DACR Mixer"
564         ac101_update_bits(codec, OMIXER_SR, 0x1 << LMIXMUTEDACL, 0x1 << LMIXMUTEDACL);
565         ac101_update_bits(codec, OMIXER_SR, 0x1 << RMIXMUTEDACR, 0x1 << RMIXMUTEDACR);
566         /* Left & Right Analog Output Mixer enable */
567         ac101_update_bits(codec, OMIXER_DACA_CTRL, 0x3 << LMIXEN, 0x3 << LMIXEN);
568
569         /* Headphone Ouput Control */ 
570         // "HP_R Mux" <= "DACR Mixer"
571         // "HP_L Mux" <= "DACL Mixer"
572         ac101_update_bits(codec, HPOUT_CTRL, 0x1 << LHPS, 0x0 << LHPS);
573         ac101_update_bits(codec, HPOUT_CTRL, 0x1 << RHPS, 0x0 << RHPS);
574
575         /* Speaker Output Control */
576         // "SPK_L Mux" <= "SPK_LR Adder"
577         // "SPK_R Mux" <= "SPK_LR Adder"
578         ac101_update_bits(codec, SPKOUT_CTRL, (0x1 << LSPKS) | (0x1 << RSPKS), (0x1 << LSPKS) | (0x1 << RSPKS));
579         /* Enable Left & Right Speaker */
580         ac101_update_bits(codec, SPKOUT_CTRL, (0x1 << LSPK_EN) | (0x1 << RSPK_EN), (0x1 << LSPK_EN) | (0x1 << RSPK_EN));
581         return;
582 }
583
584 static int late_enable_dac(struct snd_soc_codec* codec, int event) {
585         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
586
587         mutex_lock(&ac10x->dac_mutex);
588         switch (event) {
589         case SND_SOC_DAPM_PRE_PMU:
590                 AC101_DBG();
591                 if (ac10x->dac_enable == 0){
592                         /*enable dac module clk*/
593                         ac101_update_bits(codec, MOD_CLK_ENA, (0x1<<MOD_CLK_DAC_DIG), (0x1<<MOD_CLK_DAC_DIG));
594                         ac101_update_bits(codec, MOD_RST_CTRL, (0x1<<MOD_RESET_DAC_DIG), (0x1<<MOD_RESET_DAC_DIG));
595                         ac101_update_bits(codec, DAC_DIG_CTRL, (0x1<<ENDA), (0x1<<ENDA));
596                         ac101_update_bits(codec, DAC_DIG_CTRL, (0x1<<ENHPF),(0x1<<ENHPF));
597                 }
598                 ac10x->dac_enable++;
599                 break;
600         case SND_SOC_DAPM_POST_PMD:
601                 if (ac10x->dac_enable != 0){
602                         ac10x->dac_enable = 0;
603
604                         ac101_update_bits(codec, DAC_DIG_CTRL, (0x1<<ENHPF),(0x0<<ENHPF));
605                         ac101_update_bits(codec, DAC_DIG_CTRL, (0x1<<ENDA), (0x0<<ENDA));
606                         /*disable dac module clk*/
607                         ac101_update_bits(codec, MOD_CLK_ENA, (0x1<<MOD_CLK_DAC_DIG), (0x0<<MOD_CLK_DAC_DIG));
608                         ac101_update_bits(codec, MOD_RST_CTRL, (0x1<<MOD_RESET_DAC_DIG), (0x0<<MOD_RESET_DAC_DIG));
609                 }
610                 break;
611         }
612         mutex_unlock(&ac10x->dac_mutex);
613         return 0;
614 }
615
616 static int ac101_headphone_event(struct snd_soc_codec* codec, int event) {
617         switch (event) {
618         case SND_SOC_DAPM_POST_PMU:
619                 /*open*/
620                 AC101_DBG("post:open\n");
621                 ac101_update_bits(codec, OMIXER_DACA_CTRL, (0xf<<HPOUTPUTENABLE), (0xf<<HPOUTPUTENABLE));
622                 msleep(10);
623                 ac101_update_bits(codec, HPOUT_CTRL, (0x1<<HPPA_EN), (0x1<<HPPA_EN));
624                 ac101_update_bits(codec, HPOUT_CTRL, (0x3<<LHPPA_MUTE), (0x3<<LHPPA_MUTE));
625                 break;
626         case SND_SOC_DAPM_PRE_PMD:
627                 /*close*/
628                 AC101_DBG("pre:close\n");
629                 ac101_update_bits(codec, HPOUT_CTRL, (0x3<<LHPPA_MUTE), (0x0<<LHPPA_MUTE));
630                 msleep(10);
631                 ac101_update_bits(codec, OMIXER_DACA_CTRL, (0xf<<HPOUTPUTENABLE), (0x0<<HPOUTPUTENABLE));
632                 ac101_update_bits(codec, HPOUT_CTRL, (0x1<<HPPA_EN), (0x0<<HPPA_EN));
633                 break;
634         }
635         return 0;
636 }
637
638 static int ac101_sysclk_started(void) {
639         int reg_val;
640
641         reg_val = ac101_read(static_ac10x->codec, SYSCLK_CTRL);
642         return (reg_val & (0x1<<SYSCLK_ENA));
643 }
644
645 static int ac101_aif1clk(struct snd_soc_codec* codec, int event, int quick) {
646         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
647         int ret = 0;
648
649         switch (event) {
650         case SND_SOC_DAPM_PRE_PMU:
651                 if (ac10x->aif1_clken == 0){
652                         ret = ac101_update_bits(codec, SYSCLK_CTRL, (0x1<<AIF1CLK_ENA), (0x1<<AIF1CLK_ENA));
653                         if(!quick || _MASTER_MULTI_CODEC != _MASTER_AC101) {
654                                 /* enable aif1clk & sysclk */
655                                 ret = ret || ac101_update_bits(codec, MOD_CLK_ENA, (0x1<<MOD_CLK_AIF1), (0x1<<MOD_CLK_AIF1));
656                                 ret = ret || ac101_update_bits(codec, MOD_RST_CTRL, (0x1<<MOD_RESET_AIF1), (0x1<<MOD_RESET_AIF1));
657                         }
658                         ret = ret || ac101_update_bits(codec, SYSCLK_CTRL, (0x1<<SYSCLK_ENA), (0x1<<SYSCLK_ENA));
659
660                         if (ret) {
661                                 AC101_DBG("start sysclk failed\n");
662                         } else {
663                                 AC101_DBG("hw sysclk enable\n");
664                                 ac10x->aif1_clken++;
665                         }
666                 }
667                 break;
668         case SND_SOC_DAPM_POST_PMD:
669                 if (ac10x->aif1_clken != 0) {
670                         /* disable aif1clk & sysclk */
671                         ret = ac101_update_bits(codec, SYSCLK_CTRL, (0x1<<AIF1CLK_ENA),(0x0<<AIF1CLK_ENA));
672                         ret = ret || ac101_update_bits(codec, MOD_CLK_ENA, (0x1<<MOD_CLK_AIF1), (0x0<<MOD_CLK_AIF1));
673                         ret = ret || ac101_update_bits(codec, MOD_RST_CTRL, (0x1<<MOD_RESET_AIF1), (0x0<<MOD_RESET_AIF1));
674                         ret = ret || ac101_update_bits(codec, SYSCLK_CTRL, (0x1<<SYSCLK_ENA), (0x0<<SYSCLK_ENA));
675
676                         if (ret) {
677                                 AC101_DBG("stop sysclk failed\n");
678                         } else {
679                                 AC101_DBG("hw sysclk disable\n");
680                                 ac10x->aif1_clken = 0;
681                         }
682                         break;
683                 }
684         }
685
686         AC101_DBG("event=%d pre_up/%d post_down/%d\n", event, SND_SOC_DAPM_PRE_PMU, SND_SOC_DAPM_POST_PMD);
687
688         return ret;
689 }
690
691 /**
692  * snd_ac101_get_volsw - single mixer get callback
693  * @kcontrol: mixer control
694  * @ucontrol: control element information
695  *
696  * Callback to get the value of a single mixer control, or a double mixer
697  * control that spans 2 registers.
698  *
699  * Returns 0 for success.
700  */
701 static int snd_ac101_get_volsw(struct snd_kcontrol *kcontrol,
702         struct snd_ctl_elem_value *ucontrol
703 ){
704         struct soc_mixer_control *mc =
705                 (struct soc_mixer_control *)kcontrol->private_value;
706         unsigned int val, mask = (1 << fls(mc->max)) - 1;
707         unsigned int invert = mc->invert;
708         int ret;
709
710         if ((ret = ac101_read(static_ac10x->codec, mc->reg)) < 0)
711                 return ret;
712
713         val = (ret >> mc->shift) & mask;
714         ucontrol->value.integer.value[0] = val - mc->min;
715         if (invert) {
716                 ucontrol->value.integer.value[0] =
717                         mc->max - ucontrol->value.integer.value[0];
718         }
719
720         if (snd_soc_volsw_is_stereo(mc)) {
721                 val = (ret >> mc->rshift) & mask;
722                 ucontrol->value.integer.value[1] = val - mc->min;
723                 if (invert) {
724                         ucontrol->value.integer.value[1] =
725                                 mc->max - ucontrol->value.integer.value[1];
726                 }
727         }
728         return 0;
729 }
730
731 /**
732  * snd_ac101_put_volsw - single mixer put callback
733  * @kcontrol: mixer control
734  * @ucontrol: control element information
735  *
736  * Callback to set the value of a single mixer control, or a double mixer
737  * control that spans 2 registers.
738  *
739  * Returns 0 for success.
740  */
741 static int snd_ac101_put_volsw(struct snd_kcontrol *kcontrol,
742         struct snd_ctl_elem_value *ucontrol
743 ){
744         struct soc_mixer_control *mc =
745                 (struct soc_mixer_control *)kcontrol->private_value;
746         unsigned int sign_bit = mc->sign_bit;
747         unsigned int val, mask = (1 << fls(mc->max)) - 1;
748         unsigned int invert = mc->invert;
749         int ret;
750
751         if (sign_bit)
752                 mask = BIT(sign_bit + 1) - 1;
753
754         val = ((ucontrol->value.integer.value[0] + mc->min) & mask);
755         if (invert) {
756                 val = mc->max - val;
757         }
758
759         ret = ac101_update_bits(static_ac10x->codec, mc->reg, mask << mc->shift, val << mc->shift);
760
761         if (! snd_soc_volsw_is_stereo(mc)) {
762                 return ret;
763         }
764         val = ((ucontrol->value.integer.value[1] + mc->min) & mask);
765         if (invert) {
766                 val = mc->max - val;
767         }
768
769         ret = ac101_update_bits(static_ac10x->codec, mc->reg, mask << mc->rshift, val << mc->rshift);
770         return ret;
771 }
772
773
774 static const DECLARE_TLV_DB_SCALE(dac_vol_tlv, -11925, 75, 0);
775 static const DECLARE_TLV_DB_SCALE(dac_mix_vol_tlv, -600, 600, 0);
776 static const DECLARE_TLV_DB_SCALE(dig_vol_tlv, -7308, 116, 0);
777 static const DECLARE_TLV_DB_SCALE(speaker_vol_tlv, -4800, 150, 0);
778 static const DECLARE_TLV_DB_SCALE(headphone_vol_tlv, -6300, 100, 0);
779
780 static struct snd_kcontrol_new ac101_controls[] = {
781         /*DAC*/
782         SOC_DOUBLE_TLV("DAC volume", DAC_VOL_CTRL, DAC_VOL_L, DAC_VOL_R, 0xff, 0, dac_vol_tlv),
783         SOC_DOUBLE_TLV("DAC mixer gain", DAC_MXR_GAIN, DACL_MXR_GAIN, DACR_MXR_GAIN, 0xf, 0, dac_mix_vol_tlv),
784         SOC_SINGLE_TLV("digital volume", DAC_DBG_CTRL, DVC, 0x3f, 1, dig_vol_tlv),
785         SOC_SINGLE_TLV("speaker volume", SPKOUT_CTRL, SPK_VOL, 0x1f, 0, speaker_vol_tlv),
786         SOC_SINGLE_TLV("headphone volume", HPOUT_CTRL, HP_VOL, 0x3f, 0, headphone_vol_tlv),
787 };
788
789 /* PLL divisors */
790 struct pll_div {
791         unsigned int pll_in;
792         unsigned int pll_out;
793         int m;
794         int n_i;
795         int n_f;
796 };
797
798 struct aif1_fs {
799         unsigned samp_rate;
800         int bclk_div;
801         int srbit;
802         #define _SERIES_24_576K         0
803         #define _SERIES_22_579K         1
804         int series;
805 };
806
807 struct kv_map {
808         int val;
809         int bit;
810 };
811
812 /*
813  * Note : pll code from original tdm/i2s driver.
814  * freq_out = freq_in * N/(M*(2k+1)) , k=1,N=N_i+N_f,N_f=factor*0.2;
815  *              N_i[0,1023], N_f_factor[0,7], m[1,64]=REG_VAL[1-63,0]
816  */
817 static const struct pll_div codec_pll_div[] = {
818         {128000,   _FREQ_22_579K,  1, 529, 1},
819         {192000,   _FREQ_22_579K,  1, 352, 4},
820         {256000,   _FREQ_22_579K,  1, 264, 3},
821         {384000,   _FREQ_22_579K,  1, 176, 2}, /*((176+2*0.2)*6000000)/(38*(2*1+1))*/
822         {1411200,  _FREQ_22_579K,  1,  48, 0},
823         {2822400,  _FREQ_22_579K,  1,  24, 0}, /* accurate, 11025 * 256 */
824         {5644800,  _FREQ_22_579K,  1,  12, 0}, /* accurate, 22050 * 256 */
825         {6000000,  _FREQ_22_579K, 38, 429, 0}, /*((429+0*0.2)*6000000)/(38*(2*1+1))*/
826         {11289600, _FREQ_22_579K,  1,   6, 0}, /* accurate, 44100 * 256 */
827         {13000000, _FREQ_22_579K, 19,  99, 0},
828         {19200000, _FREQ_22_579K, 25,  88, 1},
829         {24000000, _FREQ_22_579K, 63, 177, 4}, /* 22577778 Hz */
830
831         {128000,   _FREQ_24_576K,  1, 576, 0},
832         {192000,   _FREQ_24_576K,  1, 384, 0},
833         {256000,   _FREQ_24_576K,  1, 288, 0},
834         {384000,   _FREQ_24_576K,  1, 192, 0},
835         {2048000,  _FREQ_24_576K,  1,  36, 0}, /* accurate,  8000 * 256 */
836         {3072000,  _FREQ_24_576K,  1,  24, 0}, /* accurate, 12000 * 256 */
837         {4096000,  _FREQ_24_576K,  1,  18, 0}, /* accurate, 16000 * 256 */
838         {6000000,  _FREQ_24_576K, 25, 307, 1},
839         {6144000,  _FREQ_24_576K,  4,  48, 0}, /* accurate, 24000 * 256 */
840         {12288000, _FREQ_24_576K,  8,  48, 0}, /* accurate, 48000 * 256 */
841         {13000000, _FREQ_24_576K, 42, 238, 1},
842         {19200000, _FREQ_24_576K, 25,  96, 0},
843         {24000000, _FREQ_24_576K, 25,  76, 4}, /* accurate */
844
845         {_FREQ_22_579K, _FREQ_22_579K,  8,  24, 0}, /* accurate, 88200 * 256 */
846         {_FREQ_24_576K, _FREQ_24_576K,  8,  24, 0}, /* accurate, 96000 * 256 */
847 };
848
849 static const struct aif1_fs codec_aif1_fs[] = {
850         {8000, 12, 0},
851         {11025, 8, 1, _SERIES_22_579K},
852         {12000, 8, 2},
853         {16000, 6, 3},
854         {22050, 4, 4, _SERIES_22_579K},
855         {24000, 4, 5},
856         /* {32000, 3, 6}, dividing by 3 is not support */
857         {44100, 2, 7, _SERIES_22_579K},
858         {48000, 2, 8},
859         {96000, 1, 9},
860 };
861
862 static const struct kv_map codec_aif1_lrck[] = {
863         {16, 0},
864         {32, 1},
865         {64, 2},
866         {128, 3},
867         {256, 4},
868 };
869
870 static const struct kv_map codec_aif1_wsize[] = {
871         {8, 0},
872         {16, 1},
873         {20, 2},
874         {24, 3},
875         {32, 3},
876 };
877
878 static const unsigned ac101_bclkdivs[] = {
879           1,   2,   4,   6,
880           8,  12,  16,  24,
881          32,  48,  64,  96,
882         128, 192,   0,   0,
883 };
884
885 static int ac101_aif_play(struct ac10x_priv* ac10x) {
886         struct snd_soc_codec * codec = ac10x->codec;
887
888         late_enable_dac(codec, SND_SOC_DAPM_PRE_PMU);
889         ac101_headphone_event(codec, SND_SOC_DAPM_POST_PMU);
890         if (drc_used) {
891                 drc_enable(codec, 1);
892         }
893
894         /* Enable Left & Right Speaker */
895         ac101_update_bits(codec, SPKOUT_CTRL, (0x1 << LSPK_EN) | (0x1 << RSPK_EN), (0x1 << LSPK_EN) | (0x1 << RSPK_EN));
896         if (ac10x->gpiod_spk_amp_gate) {
897                 gpiod_set_value(ac10x->gpiod_spk_amp_gate, 1);
898         }
899         return 0;
900 }
901
902 static void ac10x_work_aif_play(struct work_struct *work) {
903         struct ac10x_priv *ac10x = container_of(work, struct ac10x_priv, dlywork.work);
904
905         ac101_aif_play(ac10x);
906         return;
907 }
908
909 int ac101_aif_mute(struct snd_soc_dai *codec_dai, int mute)
910 {
911         struct snd_soc_codec *codec = codec_dai->codec;
912         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
913
914         AC101_DBG("mute=%d\n",  mute);
915
916         ac101_write(codec, DAC_VOL_CTRL, mute? 0: 0xA0A0);
917
918         if (!mute) {
919                 #if _MASTER_MULTI_CODEC != _MASTER_AC101
920                 /* enable global clock */
921                 ac10x->aif1_clken = 0;
922                 ac101_aif1clk(codec, SND_SOC_DAPM_PRE_PMU, 0);
923                 ac101_aif_play(ac10x);
924                 #else
925                 schedule_delayed_work(&ac10x->dlywork, msecs_to_jiffies(50));
926                 #endif
927         } else {
928                 #if _MASTER_MULTI_CODEC == _MASTER_AC101
929                 cancel_delayed_work_sync(&ac10x->dlywork);
930                 #endif
931
932                 if (ac10x->gpiod_spk_amp_gate) {
933                         gpiod_set_value(ac10x->gpiod_spk_amp_gate, 0);
934                 }
935                 /* Disable Left & Right Speaker */
936                 ac101_update_bits(codec, SPKOUT_CTRL, (0x1 << LSPK_EN) | (0x1 << RSPK_EN), (0x0 << LSPK_EN) | (0x0 << RSPK_EN));
937                 if (drc_used) {
938                         drc_enable(codec, 0);
939                 }
940                 ac101_headphone_event(codec, SND_SOC_DAPM_PRE_PMD);
941                 late_enable_dac(codec, SND_SOC_DAPM_POST_PMD);
942
943                 #if _MASTER_MULTI_CODEC != _MASTER_AC101
944                 ac10x->aif1_clken = 1;
945                 ac101_aif1clk(codec, SND_SOC_DAPM_POST_PMD, 0);
946                 #endif
947         }
948         return 0;
949 }
950
951 void ac101_aif_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *codec_dai)
952 {
953         struct snd_soc_codec *codec = codec_dai->codec;
954         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
955
956         AC101_DBG("stream = %s, play: %d, capt: %d, active: %d\n", 
957                 snd_pcm_stream_str(substream),
958                 codec_dai->playback_active, codec_dai->stream_active[SNDRV_PCM_STREAM_CAPTURE],
959                 snd_soc_dai_active(codec_dai));
960
961         if (!snd_soc_dai_active(codec_dai)) {
962                 ac10x->aif1_clken = 1;
963                 ac101_aif1clk(codec, SND_SOC_DAPM_POST_PMD, 0);
964         } else {
965                 ac101_aif1clk(codec, SND_SOC_DAPM_PRE_PMU, 0);
966         }
967 }
968
969 static int ac101_set_pll(struct snd_soc_dai *codec_dai, int pll_id, int source,
970                         unsigned int freq_in, unsigned int freq_out)
971 {
972         struct snd_soc_codec *codec = codec_dai->codec;
973         int i, m, n_i, n_f;
974
975         AC101_DBG("pll_id:%d\n",  pll_id);
976
977         /* clear volatile reserved bits*/
978         ac101_update_bits(codec, SYSCLK_CTRL, 0xFF & ~(0x1 << SYSCLK_ENA), 0x0);
979
980         /* select aif1 clk srouce from mclk1 */
981         ac101_update_bits(codec, SYSCLK_CTRL, (0x3<<AIF1CLK_SRC), (0x0<<AIF1CLK_SRC));
982         /* disable pll */
983         ac101_update_bits(codec, PLL_CTRL2, (0x1<<PLL_EN), (0<<PLL_EN));
984
985         if (!freq_out)
986                 return 0;
987         if ((freq_in < 128000) || (freq_in > _FREQ_24_576K)) {
988                 return -EINVAL;
989         } else if ((freq_in == _FREQ_24_576K) || (freq_in == _FREQ_22_579K)) {
990                 if (pll_id == AC101_MCLK1) {
991                         /*select aif1 clk source from mclk1*/
992                         ac101_update_bits(codec, SYSCLK_CTRL, (0x3<<AIF1CLK_SRC), (0x0<<AIF1CLK_SRC));
993                         return 0;
994                 }
995         }
996
997         switch (pll_id) {
998         case AC101_MCLK1:
999                 /*pll source from MCLK1*/
1000                 ac101_update_bits(codec, SYSCLK_CTRL, (0x3<<PLLCLK_SRC), (0x0<<PLLCLK_SRC));
1001                 break;
1002         case AC101_BCLK1:
1003                 /*pll source from BCLK1*/
1004                 ac101_update_bits(codec, SYSCLK_CTRL, (0x3<<PLLCLK_SRC), (0x2<<PLLCLK_SRC));
1005                 break;
1006         default:
1007                 return -EINVAL;
1008         }
1009
1010         /* freq_out = freq_in * n/(m*(2k+1)) , k=1,N=N_i+N_f */
1011         for (i = m = n_i = n_f = 0; i < ARRAY_SIZE(codec_pll_div); i++) {
1012                 if ((codec_pll_div[i].pll_in == freq_in) && (codec_pll_div[i].pll_out == freq_out)) {
1013                         m   = codec_pll_div[i].m;
1014                         n_i = codec_pll_div[i].n_i;
1015                         n_f = codec_pll_div[i].n_f;
1016                         break;
1017                 }
1018         }
1019         /* config pll m */
1020         if (m  == 64) m = 0;
1021         ac101_update_bits(codec, PLL_CTRL1, (0x3f<<PLL_POSTDIV_M), (m<<PLL_POSTDIV_M));
1022         /* config pll n */
1023         ac101_update_bits(codec, PLL_CTRL2, (0x3ff<<PLL_PREDIV_NI), (n_i<<PLL_PREDIV_NI));
1024         ac101_update_bits(codec, PLL_CTRL2, (0x7<<PLL_POSTDIV_NF), (n_f<<PLL_POSTDIV_NF));
1025         /* enable pll */
1026         ac101_update_bits(codec, PLL_CTRL2, (0x1<<PLL_EN), (1<<PLL_EN));
1027         ac101_update_bits(codec, SYSCLK_CTRL, (0x1<<PLLCLK_ENA),  (0x1<<PLLCLK_ENA));
1028         ac101_update_bits(codec, SYSCLK_CTRL, (0x3<<AIF1CLK_SRC), (0x3<<AIF1CLK_SRC));
1029
1030         return 0;
1031 }
1032
1033 int ac101_hw_params(struct snd_pcm_substream *substream,
1034         struct snd_pcm_hw_params *params,
1035         struct snd_soc_dai *codec_dai)
1036 {
1037         int i = 0;
1038         int AIF_CLK_CTRL = AIF1_CLK_CTRL;
1039         int aif1_word_size = 24;
1040         int aif1_slot_size = 32;
1041         int aif1_lrck_div;
1042         struct snd_soc_codec *codec = codec_dai->codec;
1043         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1044         int reg_val, freq_out;
1045         unsigned channels;
1046
1047         AC101_DBG("+++\n");
1048
1049         if (_MASTER_MULTI_CODEC == _MASTER_AC101 && ac101_sysclk_started()) {
1050                 /* not configure hw_param twice if stream is playback, tell the caller it's started */
1051                 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
1052                         return 1;
1053                 }
1054         }
1055
1056         /* get channels count & slot size */
1057         channels = params_channels(params);
1058
1059         switch (params_format(params)) {
1060         case SNDRV_PCM_FORMAT_S24_LE:
1061         case SNDRV_PCM_FORMAT_S32_LE:
1062                 aif1_slot_size = 32;
1063                 break;
1064         case SNDRV_PCM_FORMAT_S16_LE:
1065         default:
1066                 aif1_slot_size = 16;
1067                 break;
1068         }
1069
1070         /* set LRCK/BCLK ratio */
1071         aif1_lrck_div = aif1_slot_size * channels;
1072         for (i = 0; i < ARRAY_SIZE(codec_aif1_lrck); i++) {
1073                 if (codec_aif1_lrck[i].val == aif1_lrck_div) {
1074                         break;
1075                 }
1076         }
1077         ac101_update_bits(codec, AIF_CLK_CTRL, (0x7<<AIF1_LRCK_DIV), codec_aif1_lrck[i].bit<<AIF1_LRCK_DIV);
1078
1079         /* set PLL output freq */
1080         freq_out = _FREQ_24_576K;
1081         for (i = 0; i < ARRAY_SIZE(codec_aif1_fs); i++) {
1082                 if (codec_aif1_fs[i].samp_rate == params_rate(params)) {
1083                         if (codec_dai->stream_active[SNDRV_PCM_STREAM_CAPTURE] &&
1084                             dmic_used && codec_aif1_fs[i].samp_rate == 44100) {
1085                                 ac101_update_bits(codec, AIF_SR_CTRL, (0xf<<AIF1_FS), (0x4<<AIF1_FS));
1086                         } else {
1087                                 ac101_update_bits(codec, AIF_SR_CTRL, (0xf<<AIF1_FS), ((codec_aif1_fs[i].srbit)<<AIF1_FS));
1088                         }
1089                         if (codec_aif1_fs[i].series == _SERIES_22_579K)
1090                                 freq_out = _FREQ_22_579K;
1091                         break;
1092                 }
1093         }
1094
1095         /* set I2S word size */
1096         for (i = 0; i < ARRAY_SIZE(codec_aif1_wsize); i++) {
1097                 if (codec_aif1_wsize[i].val == aif1_word_size) {
1098                         break;
1099                 }
1100         }
1101         ac101_update_bits(codec, AIF_CLK_CTRL, (0x3<<AIF1_WORK_SIZ), ((codec_aif1_wsize[i].bit)<<AIF1_WORK_SIZ));
1102
1103         /* set TDM slot size */
1104         if ((reg_val = codec_aif1_wsize[i].bit) > 2) reg_val = 2;
1105         ac101_update_bits(codec, AIF1_ADCDAT_CTRL, 0x3 << AIF1_SLOT_SIZ, reg_val << AIF1_SLOT_SIZ);
1106
1107         /* setting pll if it's master mode */
1108         reg_val = ac101_read(codec, AIF_CLK_CTRL);
1109         if ((reg_val & (0x1 << AIF1_MSTR_MOD)) == 0) {
1110                 unsigned bclkdiv;
1111
1112                 ac101_set_pll(codec_dai, AC101_MCLK1, 0, ac10x->sysclk, freq_out);
1113
1114                 bclkdiv = freq_out / (aif1_lrck_div * params_rate(params));
1115                 for (i = 0; i < ARRAY_SIZE(ac101_bclkdivs) - 1; i++) {
1116                         if (ac101_bclkdivs[i] >= bclkdiv) {
1117                                 break;
1118                         }
1119                 }
1120                 ac101_update_bits(codec, AIF_CLK_CTRL, (0xf<<AIF1_BCLK_DIV), i<<AIF1_BCLK_DIV);
1121         } else {
1122                 /* set pll clock source to BCLK if slave mode */
1123                 ac101_set_pll(codec_dai, AC101_BCLK1, 0, aif1_lrck_div * params_rate(params), freq_out);
1124         }
1125
1126         #if _MASTER_MULTI_CODEC == _MASTER_AC101
1127         /* Master mode, to clear cpu_dai fifos, disable output bclk & lrck */
1128         ac101_aif1clk(codec, SND_SOC_DAPM_POST_PMD, 0);
1129         #endif
1130
1131         AC101_DBG("rate: %d , channels: %d , samp_res: %d",
1132                 params_rate(params), channels, aif1_slot_size);
1133
1134         AC101_DBG("---\n");
1135         return 0;
1136 }
1137
1138 int ac101_set_dai_fmt(struct snd_soc_dai *codec_dai, unsigned int fmt)
1139 {
1140         int reg_val;
1141         int AIF_CLK_CTRL = AIF1_CLK_CTRL;
1142         struct snd_soc_codec *codec = codec_dai->codec;
1143
1144         AC101_DBG();
1145
1146         /*
1147          *      master or slave selection
1148          *      0 = Master mode
1149          *      1 = Slave mode
1150          */
1151         reg_val = ac101_read(codec, AIF_CLK_CTRL);
1152         reg_val &= ~(0x1<<AIF1_MSTR_MOD);
1153         switch(fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1154         case SND_SOC_DAIFMT_CBM_CFM:   /* codec clk & frm master, ap is slave*/
1155                 #if _MASTER_MULTI_CODEC == _MASTER_AC101
1156                 pr_warn("AC101 as Master\n");
1157                 reg_val |= (0x0<<AIF1_MSTR_MOD);
1158                 break;
1159                 #else
1160                 pr_warn("AC108 as Master\n");
1161                 #endif
1162         case SND_SOC_DAIFMT_CBS_CFS:   /* codec clk & frm slave, ap is master*/
1163                 pr_warn("AC101 as Slave\n");
1164                 reg_val |= (0x1<<AIF1_MSTR_MOD);
1165                 break;
1166         default:
1167                 pr_err("unknwon master/slave format\n");
1168                 return -EINVAL;
1169         }
1170
1171         /*
1172          * Enable TDM mode
1173          */
1174         reg_val |=  (0x1 << AIF1_TDMM_ENA);
1175         ac101_write(codec, AIF_CLK_CTRL, reg_val);
1176
1177         /* i2s mode selection */
1178         reg_val = ac101_read(codec, AIF_CLK_CTRL);
1179         reg_val&=~(3<<AIF1_DATA_FMT);
1180         switch(fmt & SND_SOC_DAIFMT_FORMAT_MASK){
1181         case SND_SOC_DAIFMT_I2S:        /* I2S1 mode */
1182                 reg_val |= (0x0<<AIF1_DATA_FMT);
1183                 break;
1184         case SND_SOC_DAIFMT_RIGHT_J:    /* Right Justified mode */
1185                 reg_val |= (0x2<<AIF1_DATA_FMT);
1186                 break;
1187         case SND_SOC_DAIFMT_LEFT_J:     /* Left Justified mode */
1188                 reg_val |= (0x1<<AIF1_DATA_FMT);
1189                 break;
1190         case SND_SOC_DAIFMT_DSP_A:      /* L reg_val msb after FRM LRC */
1191                 reg_val |= (0x3<<AIF1_DATA_FMT);
1192                 break;
1193         case SND_SOC_DAIFMT_DSP_B:
1194                 /* TODO: data offset set to 0 */
1195                 reg_val |= (0x3<<AIF1_DATA_FMT);
1196                 break;
1197         default:
1198                 pr_err("%s, line:%d\n", __func__, __LINE__);
1199                 return -EINVAL;
1200         }
1201         ac101_write(codec, AIF_CLK_CTRL, reg_val);
1202
1203         /* DAI signal inversions */
1204         reg_val = ac101_read(codec, AIF_CLK_CTRL);
1205         switch(fmt & SND_SOC_DAIFMT_INV_MASK){
1206         case SND_SOC_DAIFMT_NB_NF:     /* normal bit clock + nor frame */
1207                 reg_val &= ~(0x1<<AIF1_LRCK_INV);
1208                 reg_val &= ~(0x1<<AIF1_BCLK_INV);
1209                 break;
1210         case SND_SOC_DAIFMT_NB_IF:     /* normal bclk + inv frm */
1211                 reg_val |= (0x1<<AIF1_LRCK_INV);
1212                 reg_val &= ~(0x1<<AIF1_BCLK_INV);
1213                 break;
1214         case SND_SOC_DAIFMT_IB_NF:     /* invert bclk + nor frm */
1215                 reg_val &= ~(0x1<<AIF1_LRCK_INV);
1216                 reg_val |= (0x1<<AIF1_BCLK_INV);
1217                 break;
1218         case SND_SOC_DAIFMT_IB_IF:     /* invert bclk + inv frm */
1219                 reg_val |= (0x1<<AIF1_LRCK_INV);
1220                 reg_val |= (0x1<<AIF1_BCLK_INV);
1221                 break;
1222         }
1223         ac101_write(codec, AIF_CLK_CTRL, reg_val);
1224
1225         return 0;
1226 }
1227
1228 int ac101_audio_startup(struct snd_pcm_substream *substream,
1229         struct snd_soc_dai *codec_dai)
1230 {
1231         // struct snd_soc_codec *codec = codec_dai->codec;
1232
1233         AC101_DBG("\n\n\n");
1234
1235         if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) {
1236         }
1237         return 0;
1238 }
1239
1240 #if _MASTER_MULTI_CODEC == _MASTER_AC101
1241 static int ac101_set_clock(int y_start_n_stop) {
1242         int r;
1243
1244         if (y_start_n_stop) {
1245                 /* enable global clock */
1246                 r = ac101_aif1clk(static_ac10x->codec, SND_SOC_DAPM_PRE_PMU, 1);
1247         } else {
1248                 /* disable global clock */
1249                 static_ac10x->aif1_clken = 1;
1250                 r = ac101_aif1clk(static_ac10x->codec, SND_SOC_DAPM_POST_PMD, 0);
1251         }
1252         return r;
1253 }
1254 #endif
1255
1256 int ac101_trigger(struct snd_pcm_substream *substream, int cmd,
1257                   struct snd_soc_dai *dai)
1258 {
1259         struct snd_soc_codec *codec = dai->codec;
1260         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1261         int ret = 0;
1262
1263         AC101_DBG("stream=%s  cmd=%d\n",
1264                 snd_pcm_stream_str(substream),
1265                 cmd);
1266
1267         switch (cmd) {
1268         case SNDRV_PCM_TRIGGER_START:
1269         case SNDRV_PCM_TRIGGER_RESUME:
1270         case SNDRV_PCM_TRIGGER_PAUSE_RELEASE:
1271                 #if _MASTER_MULTI_CODEC == _MASTER_AC101
1272                 if (ac10x->aif1_clken == 0){
1273                         /*
1274                          * enable aif1clk, it' here due to reduce time between 'AC108 Sysclk Enable' and 'AC101 Sysclk Enable'
1275                          * Or else the two AC108 chips lost the sync.
1276                          */
1277                         ret = 0;
1278                         ret = ret || ac101_update_bits(codec, MOD_CLK_ENA, (0x1<<MOD_CLK_AIF1), (0x1<<MOD_CLK_AIF1));
1279                         ret = ret || ac101_update_bits(codec, MOD_RST_CTRL, (0x1<<MOD_RESET_AIF1), (0x1<<MOD_RESET_AIF1));
1280                 }
1281                 #endif
1282                 break;
1283         case SNDRV_PCM_TRIGGER_STOP:
1284         case SNDRV_PCM_TRIGGER_SUSPEND:
1285         case SNDRV_PCM_TRIGGER_PAUSE_PUSH:
1286                 break;
1287         default:
1288                 ret = -EINVAL;
1289         }
1290         return ret;
1291 }
1292
1293 #if 0
1294 static int ac101_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1295                                   int clk_id, unsigned int freq, int dir)
1296 {
1297         struct snd_soc_codec *codec = codec_dai->codec;
1298         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1299
1300         AC101_DBG("id=%d freq=%d, dir=%d\n", 
1301                 clk_id, freq, dir);
1302
1303         ac10x->sysclk = freq;
1304
1305         return 0;
1306 }
1307
1308 static const struct snd_soc_dai_ops ac101_aif1_dai_ops = {
1309         //.startup      = ac101_audio_startup,
1310         //.shutdown     = ac101_aif_shutdown,
1311         //.set_sysclk   = ac101_set_dai_sysclk,
1312         //.set_pll      = ac101_set_pll,
1313         //.set_fmt      = ac101_set_dai_fmt,
1314         //.hw_params    = ac101_hw_params,
1315         //.trigger      = ac101_trigger,
1316         //.digital_mute = ac101_aif_mute,
1317 };
1318
1319 static struct snd_soc_dai_driver ac101_dai[] = {
1320         {
1321                 .name = "ac10x-aif1",
1322                 .id = AIF1_CLK,
1323                 .playback = {
1324                         .stream_name = "Playback",
1325                         .channels_min = 1,
1326                         .channels_max = 8,
1327                         .rates = AC101_RATES,
1328                         .formats = AC101_FORMATS,
1329                 },
1330                 #if 0
1331                 .capture = {
1332                         .stream_name = "Capture",
1333                         .channels_min = 1,
1334                         .channels_max = 8,
1335                         .rates = AC101_RATES,
1336                         .formats = AC101_FORMATS,
1337                 },
1338                 #endif
1339                 .ops = &ac101_aif1_dai_ops,
1340         }
1341 };
1342 #endif
1343
1344 static void codec_resume_work(struct work_struct *work)
1345 {
1346         struct ac10x_priv *ac10x = container_of(work, struct ac10x_priv, codec_resume);
1347         struct snd_soc_codec *codec = ac10x->codec;
1348
1349         AC101_DBG("+++\n");
1350
1351         set_configuration(codec);
1352         if (drc_used) {
1353                 drc_config(codec);
1354         }
1355         /*enable this bit to prevent leakage from ldoin*/
1356         ac101_update_bits(codec, ADDA_TUNE3, (0x1<<OSCEN), (0x1<<OSCEN));
1357
1358         AC101_DBG("---\n");
1359         return;
1360 }
1361
1362 int ac101_set_bias_level(struct snd_soc_codec *codec, enum snd_soc_bias_level level)
1363 {
1364         switch (level) {
1365         case SND_SOC_BIAS_ON:
1366                 AC101_DBG("SND_SOC_BIAS_ON\n");
1367                 break;
1368         case SND_SOC_BIAS_PREPARE:
1369                 AC101_DBG("SND_SOC_BIAS_PREPARE\n");
1370                 break;
1371         case SND_SOC_BIAS_STANDBY:
1372                 AC101_DBG("SND_SOC_BIAS_STANDBY\n");
1373                 #ifdef CONFIG_AC101_SWITCH_DETECT
1374                 switch_hw_config(codec);
1375                 #endif
1376                 break;
1377         case SND_SOC_BIAS_OFF:
1378                 #ifdef CONFIG_AC101_SWITCH_DETECT
1379                 ac101_update_bits(codec, ADC_APC_CTRL, (0x1<<HBIASEN), (0<<HBIASEN));
1380                 ac101_update_bits(codec, ADC_APC_CTRL, (0x1<<HBIASADCEN), (0<<HBIASADCEN));
1381                 #endif
1382                 ac101_update_bits(codec, OMIXER_DACA_CTRL, (0xf<<HPOUTPUTENABLE), (0<<HPOUTPUTENABLE));
1383                 ac101_update_bits(codec, ADDA_TUNE3, (0x1<<OSCEN), (0<<OSCEN));
1384                 AC101_DBG("SND_SOC_BIAS_OFF\n");
1385                 break;
1386         }
1387         snd_soc_codec_get_dapm(codec)->bias_level = level;
1388         return 0;
1389 }
1390
1391 int ac101_codec_probe(struct snd_soc_codec *codec)
1392 {
1393         int ret = 0;
1394         struct ac10x_priv *ac10x;
1395
1396         ac10x = dev_get_drvdata(codec->dev);
1397         if (ac10x == NULL) {
1398                 AC101_DBG("not set client data!\n");
1399                 return -ENOMEM;
1400         }
1401         ac10x->codec = codec;
1402
1403         INIT_DELAYED_WORK(&ac10x->dlywork, ac10x_work_aif_play);
1404         INIT_WORK(&ac10x->codec_resume, codec_resume_work);
1405         ac10x->dac_enable = 0;
1406         ac10x->aif1_clken = 0;
1407         mutex_init(&ac10x->dac_mutex);
1408
1409         #if _MASTER_MULTI_CODEC == _MASTER_AC101
1410         seeed_voice_card_register_set_clock(SNDRV_PCM_STREAM_PLAYBACK, ac101_set_clock);
1411         #endif
1412
1413         set_configuration(ac10x->codec);
1414
1415         /*enable this bit to prevent leakage from ldoin*/
1416         ac101_update_bits(codec, ADDA_TUNE3, (0x1<<OSCEN), (0x1<<OSCEN));
1417         ac101_write(codec, DAC_VOL_CTRL, 0);
1418
1419         /* customized get/put inteface */
1420         for (ret = 0; ret < ARRAY_SIZE(ac101_controls); ret++) {
1421                 struct snd_kcontrol_new* skn = &ac101_controls[ret];
1422
1423                 skn->get = snd_ac101_get_volsw;
1424                 skn->put = snd_ac101_put_volsw;
1425         }
1426         ret = snd_soc_add_codec_controls(codec, ac101_controls, ARRAY_SIZE(ac101_controls));
1427         if (ret) {
1428                 pr_err("[ac10x] Failed to register audio mode control, "
1429                                 "will continue without it.\n");
1430         }
1431
1432         #ifdef CONFIG_AC101_SWITCH_DETECT
1433         ret = ac101_switch_probe(ac10x);
1434         if (ret) {
1435                 // not care the switch return value
1436         }
1437         #endif
1438
1439         return 0;
1440 }
1441
1442 /* power down chip */
1443 int ac101_codec_remove(struct snd_soc_codec *codec)
1444 {
1445         #ifdef CONFIG_AC101_SWITCH_DETECT
1446         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1447
1448         if (ac10x->irq) {
1449                 devm_free_irq(codec->dev, ac10x->irq, ac10x);
1450                 ac10x->irq = 0;
1451         }
1452
1453         if (cancel_work_sync(&ac10x->work_switch) != 0) {
1454         }
1455
1456         if (cancel_work_sync(&ac10x->work_clear_irq) != 0) {
1457         }
1458
1459         if (ac10x->inpdev) {
1460                 input_unregister_device(ac10x->inpdev);
1461                 ac10x->inpdev = NULL;
1462         }
1463         #endif
1464
1465         return 0;
1466 }
1467
1468 int ac101_codec_suspend(struct snd_soc_codec *codec)
1469 {
1470         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1471
1472         AC101_DBG("[codec]:suspend\n");
1473         regcache_cache_only(ac10x->regmap101, true);
1474         return 0;
1475 }
1476
1477 int ac101_codec_resume(struct snd_soc_codec *codec)
1478 {
1479         struct ac10x_priv *ac10x = snd_soc_codec_get_drvdata(codec);
1480         int ret;
1481
1482         AC101_DBG("[codec]:resume");
1483
1484         /* Sync reg_cache with the hardware */
1485         regcache_cache_only(ac10x->regmap101, false);
1486         ret = regcache_sync(ac10x->regmap101);
1487         if (ret != 0) {
1488                 dev_err(codec->dev, "Failed to sync register cache: %d\n", ret);
1489                 regcache_cache_only(ac10x->regmap101, true);
1490                 return ret;
1491         }
1492
1493         #ifdef CONFIG_AC101_SWITCH_DETECT
1494         ac10x->mode = HEADPHONE_IDLE;
1495         ac10x->state = -1;
1496         #endif
1497
1498         ac101_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1499         schedule_work(&ac10x->codec_resume);
1500         return 0;
1501 }
1502
1503 /***************************************************************************/
1504 static ssize_t ac101_debug_store(struct device *dev,
1505         struct device_attribute *attr, const char *buf, size_t count)
1506 {
1507         struct ac10x_priv *ac10x = dev_get_drvdata(dev);
1508         int val = 0, flag = 0;
1509         u16 value_w, value_r;
1510         u8 reg, num, i=0;
1511
1512         val = simple_strtol(buf, NULL, 16);
1513         flag = (val >> 24) & 0xF;
1514         if (flag) {
1515                 reg = (val >> 16) & 0xFF;
1516                 value_w =  val & 0xFFFF;
1517                 ac101_write(ac10x->codec, reg, value_w);
1518                 printk("write 0x%x to reg:0x%x\n", value_w, reg);
1519         } else {
1520                 reg = (val >> 8) & 0xFF;
1521                 num = val & 0xff;
1522                 printk("\n");
1523                 printk("read:start add:0x%x,count:0x%x\n", reg, num);
1524
1525                 regcache_cache_bypass(ac10x->regmap101, true);
1526                 do {
1527                         value_r = ac101_read(ac10x->codec, reg);
1528                         printk("0x%x: 0x%04x ", reg++, value_r);
1529                         if (++i % 4 == 0 || i == num)
1530                                 printk("\n");
1531                 } while (i < num);
1532                 regcache_cache_bypass(ac10x->regmap101, false);
1533         }
1534         return count;
1535 }
1536 static ssize_t ac101_debug_show(struct device *dev,
1537         struct device_attribute *attr, char *buf)
1538 {
1539         printk("echo flag|reg|val > ac10x\n");
1540         printk("eg read star addres=0x06,count 0x10:echo 0610 >ac10x\n");
1541         printk("eg write value:0x13fe to address:0x06 :echo 10613fe > ac10x\n");
1542         return 0;
1543 }
1544 static DEVICE_ATTR(ac10x, 0644, ac101_debug_show, ac101_debug_store);
1545
1546 static struct attribute *audio_debug_attrs[] = {
1547         &dev_attr_ac10x.attr,
1548         NULL,
1549 };
1550
1551 static struct attribute_group audio_debug_attr_group = {
1552         .name   = "ac101_debug",
1553         .attrs  = audio_debug_attrs,
1554 };
1555 /***************************************************************************/
1556
1557 /************************************************************/
1558 static bool ac101_volatile_reg(struct device *dev, unsigned int reg)
1559 {
1560         switch (reg) {
1561         case PLL_CTRL2:
1562         case HMIC_STS:
1563                 return true;
1564         }
1565         return false;
1566 }
1567
1568 static const struct regmap_config ac101_regmap = {
1569         .reg_bits = 8,
1570         .val_bits = 16,
1571         .reg_stride = 1,
1572         .max_register = 0xB5,
1573         .cache_type = REGCACHE_FLAT,
1574         .volatile_reg = ac101_volatile_reg,
1575 };
1576
1577 /* Sync reg_cache from the hardware */
1578 int ac10x_fill_regcache(struct device* dev, struct regmap* map) {
1579         int r, i, n;
1580         int v;
1581
1582         n = regmap_get_max_register(map);
1583         for (i = 0; i < n; i++) {
1584                 regcache_cache_bypass(map, true);
1585                 r = regmap_read(map, i, &v);
1586                 if (r) {
1587                         dev_err(dev, "failed to read register %d\n", i);
1588                         continue;
1589                 }
1590                 regcache_cache_bypass(map, false);
1591
1592                 regcache_cache_only(map, true);
1593                 r = regmap_write(map, i, v);
1594                 regcache_cache_only(map, false);
1595         }
1596         regcache_cache_bypass(map, false);
1597         regcache_cache_only(map, false);
1598
1599         return 0;
1600 }
1601
1602 int ac101_probe(struct i2c_client *i2c, const struct i2c_device_id *id)
1603 {
1604         struct ac10x_priv *ac10x = i2c_get_clientdata(i2c);
1605         int ret = 0;
1606         unsigned v = 0;
1607
1608         AC101_DBG();
1609
1610         static_ac10x = ac10x;
1611
1612         ac10x->regmap101 = devm_regmap_init_i2c(i2c, &ac101_regmap);
1613         if (IS_ERR(ac10x->regmap101)) {
1614                 ret = PTR_ERR(ac10x->regmap101);
1615                 dev_err(&i2c->dev, "Fail to initialize I/O: %d\n", ret);
1616                 return ret;
1617         }
1618
1619         /* Chip reset */
1620         regcache_cache_only(ac10x->regmap101, false);
1621         ret = regmap_write(ac10x->regmap101, CHIP_AUDIO_RST, 0);
1622         msleep(50);
1623
1624         /* sync regcache for FLAT type */
1625         ac10x_fill_regcache(&i2c->dev, ac10x->regmap101);
1626
1627         ret = regmap_read(ac10x->regmap101, CHIP_AUDIO_RST, &v);
1628         if (ret < 0) {
1629                 dev_err(&i2c->dev, "failed to read vendor ID: %d\n", ret);
1630                 return ret;
1631         }
1632
1633         if (v != AC101_CHIP_ID) {
1634                 dev_err(&i2c->dev, "chip is not AC101 (%X)\n", v);
1635                 dev_err(&i2c->dev, "Expected %X\n", AC101_CHIP_ID);
1636                 return -ENODEV;
1637         }
1638
1639         ret = sysfs_create_group(&i2c->dev.kobj, &audio_debug_attr_group);
1640         if (ret) {
1641                 pr_err("failed to create attr group\n");
1642         }
1643
1644         ac10x->gpiod_spk_amp_gate = devm_gpiod_get_optional(&i2c->dev, "spk-amp-switch", GPIOD_OUT_LOW);
1645         if (IS_ERR(ac10x->gpiod_spk_amp_gate)) {
1646                 ac10x->gpiod_spk_amp_gate = NULL;
1647                 dev_err(&i2c->dev, "failed get spk-amp-switch in device tree\n");
1648         }
1649
1650         return 0;
1651 }
1652
1653 void ac101_shutdown(struct i2c_client *i2c)
1654 {
1655         struct ac10x_priv *ac10x = i2c_get_clientdata(i2c);
1656         struct snd_soc_codec *codec = ac10x->codec;
1657         int reg_val;
1658
1659         if (codec == NULL) {
1660                 pr_err(": no sound card.\n");
1661                 return;
1662         }
1663
1664         /*set headphone volume to 0*/
1665         reg_val = ac101_read(codec, HPOUT_CTRL);
1666         reg_val &= ~(0x3f<<HP_VOL);
1667         ac101_write(codec, HPOUT_CTRL, reg_val);
1668
1669         /*disable pa*/
1670         reg_val = ac101_read(codec, HPOUT_CTRL);
1671         reg_val &= ~(0x1<<HPPA_EN);
1672         ac101_write(codec, HPOUT_CTRL, reg_val);
1673
1674         /*hardware xzh support*/
1675         reg_val = ac101_read(codec, OMIXER_DACA_CTRL);
1676         reg_val &= ~(0xf<<HPOUTPUTENABLE);
1677         ac101_write(codec, OMIXER_DACA_CTRL, reg_val);
1678
1679         /*unmute l/r headphone pa*/
1680         reg_val = ac101_read(codec, HPOUT_CTRL);
1681         reg_val &= ~((0x1<<RHPPA_MUTE)|(0x1<<LHPPA_MUTE));
1682         ac101_write(codec, HPOUT_CTRL, reg_val);
1683         return;
1684 }
1685
1686 int ac101_remove(struct i2c_client *i2c)
1687 {
1688         sysfs_remove_group(&i2c->dev.kobj, &audio_debug_attr_group);
1689         return 0;
1690 }
1691
1692 MODULE_DESCRIPTION("ASoC ac10x driver");
1693 MODULE_AUTHOR("huangxin,liushaohua");
1694 MODULE_AUTHOR("PeterYang<linsheng.yang@seeed.cc>");