huangcm
2024-10-12 d3acb07ae52cd1e07661d853cb07895d324a847f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
/*
 * sound\soc\sunxi\sun50iw10-sndcodec.c
 * (C) Copyright 2014-2018
 * Reuuimlla Technology Co., Ltd. <www.reuuimllatech.com>
 * huangxin <huangxin@Reuuimllatech.com>
 * liushaohua <liushaohua@allwinnertech.com>
 * yumingfengng <yumingfeng@allwinnertech.com>
 * luguofang <luguofang@allwinnertech.com>
 *
 * some simple description for this code
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 */
#include <linux/module.h>
#include <linux/clk.h>
#include <linux/mutex.h>
#include <linux/gpio.h>
#include <linux/io.h>
#include <linux/input.h>
#include <sound/pcm.h>
#include <sound/soc.h>
#include <sound/jack.h>
#include <linux/of.h>
#include <sound/pcm_params.h>
#include <sound/soc-dapm.h>
#include <linux/delay.h>
 
#include "sun50iw10-codec.h"
#include "sunxi_rw_func.h"
 
#ifdef CONFIG_SND_SUNXI_SOC_SUN50IW10_JACK
static int mdata_threshold = 0x10;
module_param(mdata_threshold, int, 0644);
MODULE_PARM_DESC(mdata_threshold,
       "SUNXI hmic data threshold");
 
typedef enum {
   RESUME_IRQ  = 0x0,
   SYSINIT_IRQ = 0x1,
   OTHER_IRQ   = 0x2,
} _jack_irq_times;
 
enum HPDETECTWAY {
   HP_DETECT_LOW = 0x0,
   HP_DETECT_HIGH = 0x1,
};
 
enum dectect_jack {
   PLUG_OUT = 0x0,
   PLUG_IN  = 0x1,
};
 
static bool is_irq;
static int switch_state;
#endif
 
struct sunxi_card_priv {
   struct snd_soc_card *card;
#ifdef CONFIG_SND_SUNXI_SOC_SUN50IW10_JACK
   struct snd_soc_codec *codec;
   struct delayed_work hs_init_work;
   struct delayed_work hs_detect_work;
   struct delayed_work hs_button_work;
   struct delayed_work hs_checkplug_work;
   struct mutex jack_mlock;
   struct snd_soc_jack jack;
   struct timeval tv_headset_plugin;    /*4*/
   _jack_irq_times jack_irq_times;
   u32 detect_state;
   u32 jackirq;                /*switch irq*/
   u32 HEADSET_DATA;            /*threshod for switch insert*/
   u32 headset_basedata;
   u32 switch_status;
   u32 key_volup;
   u32 key_voldown;
   u32 key_hook;
   u32 key_voiceassist;
   u32 hp_detect_case;
   u32 noheadphonemic;
#endif
};
 
#ifdef CONFIG_SND_SUNXI_SOC_SUN50IW10_JACK
/*
 * Identify the jack type as Headset/Headphone/None
 */
static int sunxi_check_jack_type(struct snd_soc_jack *jack)
{
   u32 reg_val = 0;
   u32 jack_type = 0, tempdata = 0;
   struct sunxi_card_priv *priv = container_of(jack, struct sunxi_card_priv, jack);
 
   reg_val = snd_soc_read(priv->codec, SUNXI_HMIC_STS);
   tempdata = (reg_val >> HMIC_DATA) & 0x1f;
 
   priv->headset_basedata = tempdata;
   if (tempdata >= priv->HEADSET_DATA) {
       pr_debug("[SND_JACK_HEADSET], priv->HEADSET_DATA:0x%x\n",
              priv->HEADSET_DATA);
       /*
        * headset:4
        */
       jack_type = SND_JACK_HEADSET;
   } else {
       /*
        * headphone:3
        * disable hbias and adc
        */
       pr_debug("[SND_JACK_HEADPHONE] priv->HEADSET_DATA:0x%x\n",
           priv->HEADSET_DATA);
       if (!priv->noheadphonemic) {
           snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG,
                   (0x1 << HMICBIASEN), (0x0 << HMICBIASEN));
           snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG,
                   (0x1 << MICADCEN), (0x0 << MICADCEN));
       }
       jack_type = SND_JACK_HEADPHONE;
   }
 
   return jack_type;
}
 
/* Checks hs insertion by mdet */
static void sunxi_check_hs_plug(struct work_struct *work)
{
   struct sunxi_card_priv *priv =
       container_of(work, struct sunxi_card_priv, hs_checkplug_work.work);
   struct sunxi_codec_info *sunxi_codec = snd_soc_codec_get_drvdata(priv->codec);
   struct codec_spk_config *spk_cfg = &(sunxi_codec->spk_config);
 
   mutex_lock(&priv->jack_mlock);
   if (priv->detect_state != PLUG_IN) {
       /* Enable MDET */
       snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
               (0x1 << MIC_DET_IRQ_EN),
               (0x1 << MIC_DET_IRQ_EN));
       if (!priv->noheadphonemic) {
           snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG,
                   (0x1 << MICADCEN),
                   (0x1 << MICADCEN));
       }
       /* Enable PA */
       snd_soc_update_bits(priv->codec, SUNXI_HEADPHONE_REG,
               (0x1 << HPPA_EN),
               (0x1 << HPPA_EN));
 
       if (spk_cfg->used)
           gpio_set_value(spk_cfg->spk_gpio, spk_cfg->pa_level);
   } else {
       /*
        * Enable HPPA_EN
        * FIXME:When the Audio HAL is not at the do_output_standby,
        * apk not play the music at the same time, we can insert
        * headset now and click to play music immediately in the apk,
        * the Audio HAL will write data to the card and not update
        * the stream routing. Because we also set mute when
        * the mdet come into force, so that the dapm will not update
        * and it makes the mute.
        */
       snd_soc_update_bits(priv->codec, SUNXI_HEADPHONE_REG,
               (0x1 << HPPA_EN),
               (0x1 << HPPA_EN));
   }
   mutex_unlock(&priv->jack_mlock);
}
 
/* Checks jack insertion and identifies the jack type.*/
static void sunxi_check_hs_detect_status(struct work_struct *work)
{
   struct sunxi_card_priv *priv =
       container_of(work, struct sunxi_card_priv, hs_detect_work.work);
   int jack_type = 0, reg_val = 0;
 
   mutex_lock(&priv->jack_mlock);
   if (priv->detect_state == PLUG_IN) {
       /* Enable hbias and adc*/
       if (!priv->noheadphonemic) {
           snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG,
                   (0x1 << MICADCEN), (0x1 << MICADCEN));
       }
       msleep(100);
       jack_type = sunxi_check_jack_type(&priv->jack);
       if (jack_type != priv->switch_status) {
           priv->switch_status = jack_type;
           snd_jack_report(priv->jack.jack, jack_type);
           pr_err("plugin --> switch:%d\n", jack_type);
           switch_state = jack_type;
       }
 
       /*
        * if SND_JACK_HEADSET,enable mic detect irq
        */
       if (jack_type == SND_JACK_HEADSET) {
           /*
            * headset:clear headset insert pending.
            */
           reg_val = snd_soc_read(priv->codec, SUNXI_HMIC_STS);
           priv->headset_basedata = (reg_val >> HMIC_DATA) & 0x1f;
           if (priv->headset_basedata > 3)
               priv->headset_basedata -= 3;
 
           do_gettimeofday(&priv->tv_headset_plugin);
           usleep_range(1000, 2000);
           snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
               (0x1f << MDATA_THRESHOLD),
               (priv->headset_basedata << MDATA_THRESHOLD));
           snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
               (0x1 << MIC_DET_IRQ_EN),
               (0x1 << MIC_DET_IRQ_EN));
       } else if (jack_type == SND_JACK_HEADPHONE) {
           /*
            * if is HEADPHONE 3,close mic detect irq
            */
           snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
               (0x1 << MIC_DET_IRQ_EN),
               (0x0 << MIC_DET_IRQ_EN));
           priv->tv_headset_plugin.tv_sec = 0;
       }
   } else {
       priv->switch_status = 0;
       /*clear headset pulgout pending.*/
       snd_jack_report(priv->jack.jack, priv->switch_status);
       switch_state = priv->switch_status;
       pr_err("plugout --> switch:%d\n", priv->switch_status);
       priv->tv_headset_plugin.tv_sec = 0;
       /*enable hbias and adc*/
       if (!priv->noheadphonemic) {
           snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG,
                       (0x1 << HMICBIASEN),
                       (0x1 << HMICBIASEN));
           snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG,
                       (0x1 << MICADCEN),
                       (0x1 << MICADCEN));
       }
       snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
               (0x1f << MDATA_THRESHOLD),
               (mdata_threshold << MDATA_THRESHOLD));
       snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
                   (0x1 << MIC_DET_IRQ_EN),
                   (0x1 << MIC_DET_IRQ_EN));
   }
   mutex_unlock(&priv->jack_mlock);
}
 
static void sunxi_hs_init_work(struct work_struct *work)
{
   struct sunxi_card_priv *priv =
       container_of(work, struct sunxi_card_priv, hs_init_work.work);
 
   mutex_lock(&priv->jack_mlock);
   if (is_irq == true) {
       is_irq = false;
   } else {
       if (priv->hp_detect_case == HP_DETECT_LOW ||
           (priv->jack_irq_times == RESUME_IRQ)) {
           /*
            * It should be report after resume.
            * If the headset plugout after suspend, the system
            * can not know the state, so we should reset here
            * when resume.
            */
           pr_debug("[codec-machine] resume-->report switch.\n");
           priv->switch_status = 0;
           snd_jack_report(priv->jack.jack, priv->switch_status);
           switch_state = 0;
       }
   }
   priv->jack_irq_times = OTHER_IRQ;
   mutex_unlock(&priv->jack_mlock);
}
 
/* Check for hook release */
static void sunxi_check_hs_button_status(struct work_struct *work)
{
   struct sunxi_card_priv *priv =
       container_of(work, struct sunxi_card_priv, hs_button_work.work);
   u32 i = 0;
 
   mutex_lock(&priv->jack_mlock);
   for (i = 0; i < 1; i++) {
       if (priv->key_hook == 0) {
           pr_info("Hook (2)!!\n");
           priv->switch_status &= ~SND_JACK_BTN_0;
           snd_jack_report(priv->jack.jack, priv->switch_status);
           break;
       }
       /* may msleep 8 */
       msleep(20);
   }
 
   mutex_unlock(&priv->jack_mlock);
}
 
static irqreturn_t jack_interrupt(int irq, void *dev_id)
{
   struct sunxi_card_priv *priv = dev_id;
   struct timeval tv;
   u32 tempdata = 0, regval = 0;
   int jack_state = 0;
   struct sunxi_codec_info *sunxi_codec = snd_soc_codec_get_drvdata(priv->codec);
   struct codec_spk_config *spk_cfg = &(sunxi_codec->spk_config);
 
   if (priv->jack_irq_times == RESUME_IRQ ||
       priv->jack_irq_times == SYSINIT_IRQ) {
       is_irq = true;
       priv->jack_irq_times = OTHER_IRQ;
   }
 
   pr_debug("[%s] SUNXI_HMIC_STS:0x%x\n", __func__,
               snd_soc_read(priv->codec, SUNXI_HMIC_STS));
 
   jack_state = snd_soc_read(priv->codec, SUNXI_HMIC_STS);
 
   if (priv->detect_state != PLUG_IN) {
       if (jack_state & (1 << MIC_DET_ST)) {
           regval = snd_soc_read(priv->codec, SUNXI_HMIC_CTRL);
           regval &= ~(0x1 << MIC_DET_IRQ_EN);
           snd_soc_write(priv->codec, SUNXI_HMIC_CTRL, regval);
 
           if (!priv->noheadphonemic) {
               regval = snd_soc_read(priv->codec, SUNXI_MICBIAS_REG);
               regval &= ~(0x1 << MICADCEN);
               snd_soc_write(priv->codec, SUNXI_MICBIAS_REG, regval);
           }
 
           /* clear mic detect status */
           regval = snd_soc_read(priv->codec, SUNXI_HMIC_STS);
           regval &= ~(0x1 << JACK_DET_IIN_ST);
           regval &= ~(0x1 << JACK_DET_OUT_ST);
           regval |= 0x1 << MIC_DET_ST;
           snd_soc_write(priv->codec, SUNXI_HMIC_STS, regval);
 
           /* close PA */
           regval = snd_soc_read(priv->codec, SUNXI_HEADPHONE_REG);
           regval &= ~(0x1 << HPPA_EN);
           snd_soc_write(priv->codec, SUNXI_HEADPHONE_REG, regval);
 
           if (spk_cfg->used)
               gpio_set_value(spk_cfg->spk_gpio, !(spk_cfg->pa_level));
           /* prevent mic detect false trigger */
           schedule_delayed_work(&priv->hs_checkplug_work,
               msecs_to_jiffies(700));
       }
   }
 
   /*headphone insert*/
   if (jack_state & (1 << JACK_DET_IIN_ST)) {
       regval = snd_soc_read(priv->codec, SUNXI_HMIC_STS);
       regval |= 0x1 << JACK_DET_IIN_ST;
       regval &= ~(0x1 << JACK_DET_OUT_ST);
       snd_soc_write(priv->codec, SUNXI_HMIC_STS, regval);
       priv->detect_state = PLUG_IN;
       schedule_delayed_work(&priv->hs_detect_work,
               msecs_to_jiffies(10));
   }
 
   /*headphone plugout*/
   if (jack_state & (1 << JACK_DET_OUT_ST)) {
       regval = snd_soc_read(priv->codec, SUNXI_HMIC_STS);
       regval &= ~(0x1 << JACK_DET_IIN_ST);
       regval |= 0x1 << JACK_DET_OUT_ST;
       snd_soc_write(priv->codec, SUNXI_HMIC_STS, regval);
       // priv->detect_state = PLUG_OUT;
       priv->detect_state = PLUG_IN; // always plugin
       schedule_delayed_work(&priv->hs_detect_work,
               msecs_to_jiffies(10));
   }
 
   /*key*/
   if ((priv->detect_state == PLUG_IN) &&
       (jack_state & (1 << MIC_DET_ST))) {
       regval = snd_soc_read(priv->codec, SUNXI_HMIC_STS);
       regval &= ~(0x1 << JACK_DET_IIN_ST);
       regval &= ~(0x1 << JACK_DET_OUT_ST);
       regval |= 0x1 << MIC_DET_ST;
       snd_soc_write(priv->codec, SUNXI_HMIC_STS, regval);
 
       do_gettimeofday(&tv);
       if (abs(tv.tv_sec - priv->tv_headset_plugin.tv_sec) > 2) {
           tempdata = snd_soc_read(priv->codec, SUNXI_HMIC_STS);
           tempdata = (tempdata & 0x1f00) >> 8;
           pr_err("[%s]: KEY tempdata: %d\n", __func__, tempdata);
           pr_debug("headset key debug tempdata : 0x%x.\n",
                tempdata);
 
           if (tempdata == 2) {
               priv->key_hook = 0;
               priv->key_voldown = 0;
               priv->key_voiceassist = 0;
               priv->key_volup++;
               if (priv->key_volup == 1) {
                   pr_debug("Volume ++\n");
                   priv->key_volup = 0;
                   priv->switch_status |= SND_JACK_BTN_1;
                   snd_jack_report(priv->jack.jack,
                           priv->switch_status);
                   priv->switch_status &= ~SND_JACK_BTN_1;
                   snd_jack_report(priv->jack.jack,
                           priv->switch_status);
               }
           } else if ((tempdata == 5) || tempdata == 4) {
               priv->key_volup = 0;
               priv->key_hook = 0;
               priv->key_voiceassist = 0;
               priv->key_voldown++;
               if (priv->key_voldown == 1) {
                   pr_debug("Volume --\n");
                   priv->key_voldown = 0;
                   priv->switch_status |= SND_JACK_BTN_2;
                   snd_jack_report(priv->jack.jack,
                           priv->switch_status);
                   priv->switch_status &= ~SND_JACK_BTN_2;
                   snd_jack_report(priv->jack.jack,
                           priv->switch_status);
               }
           /* add KET_VOICECOMMAND for voice assistant */
           } else if (tempdata == 1) {
               priv->key_volup = 0;
               priv->key_hook = 0;
               priv->key_voldown = 0;
               priv->key_voiceassist++;
               if (priv->key_voiceassist == 1) {
                   pr_debug("Voice Assistant Open\n");
                   priv->key_voiceassist = 0;
                   priv->switch_status |= SND_JACK_BTN_3;
                   snd_jack_report(priv->jack.jack,
                           priv->switch_status);
                   priv->switch_status &= ~SND_JACK_BTN_3;
                   snd_jack_report(priv->jack.jack,
                           priv->switch_status);
               }
           } else if (tempdata == 0x0) {
               priv->key_volup = 0;
               priv->key_voldown = 0;
               priv->key_voiceassist = 0;
               priv->key_hook++;
               if (priv->key_hook >= 1) {
                   priv->key_hook = 0;
                   if ((priv->switch_status &
                        SND_JACK_BTN_0) == 0) {
                       priv->switch_status |=
                           SND_JACK_BTN_0;
                       snd_jack_report(
                           priv->jack.jack,
                           priv->switch_status);
                       pr_debug("Hook (1)!!\n");
                   }
                   schedule_delayed_work(
                       &priv->hs_button_work,
                       msecs_to_jiffies(180));
               }
           } else {
               /*This could be key release,fix me ! */
               pr_err("tempdata:0x%x,Key data err:\n",
                    tempdata);
               priv->key_volup = 0;
               priv->key_voldown = 0;
               priv->key_hook = 0;
               priv->key_voiceassist = 0;
           }
       } else {
       }
   }
 
   return IRQ_HANDLED;
}
#endif
 
static const struct snd_kcontrol_new sunxi_card_controls[] = {
   SOC_DAPM_PIN_SWITCH("Headphone"),
   SOC_DAPM_PIN_SWITCH("HpSpeaker"),
   SOC_DAPM_PIN_SWITCH("LINEOUT"),
};
 
static const struct snd_soc_dapm_widget sunxi_card_dapm_widgets[] = {
   SND_SOC_DAPM_MIC("HeadphoneMic", NULL),
   SND_SOC_DAPM_MIC("Main Mic", NULL),
};
 
static const struct snd_soc_dapm_route sunxi_card_routes[] = {
   {"MainMic Bias", NULL, "Main Mic"},
   {"MIC1", NULL, "MainMic Bias"},
   {"MIC2", NULL, "HeadphoneMic"},
};
 
/*
 * Card initialization
 */
static int sunxi_card_init(struct snd_soc_pcm_runtime *rtd)
{
   struct snd_soc_codec *codec = rtd->codec;
   struct snd_soc_dapm_context *dapm = &codec->component.dapm;
 
#ifdef CONFIG_SND_SUNXI_SOC_SUN50IW10_JACK
   struct sunxi_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
   int ret;
   priv->codec = rtd->codec;
 
   ret = snd_soc_card_jack_new(rtd->card, "sunxi Audio Jack",
                  SND_JACK_HEADSET | SND_JACK_HEADPHONE |
                  SND_JACK_BTN_0 | SND_JACK_BTN_1 |
                  SND_JACK_BTN_2 | SND_JACK_BTN_3,
                  &priv->jack, NULL, 0);
   if (ret) {
       pr_err("jack creation failed\n");
       return ret;
   }
 
   snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_0, KEY_MEDIA);
   snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
   snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
   snd_jack_set_key(priv->jack.jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
#endif
 
   snd_soc_dapm_disable_pin(dapm, "HPOUTR");
   snd_soc_dapm_disable_pin(dapm, "HPOUTL");
 
   snd_soc_dapm_disable_pin(dapm, "LINEOUT");
   snd_soc_dapm_disable_pin(dapm, "HpSpeaker");
   snd_soc_dapm_disable_pin(dapm, "Headphone");
 
   snd_soc_dapm_sync(dapm);
 
   pr_warn("[%s] card init finished.\n", __func__);
   return 0;
}
 
static int sunxi_card_hw_params(struct snd_pcm_substream *substream,
               struct snd_pcm_hw_params *params)
{
   struct snd_soc_pcm_runtime *rtd = substream->private_data;
   struct snd_soc_dai *codec_dai = rtd->codec_dai;
   struct snd_soc_card *card = rtd->card;
   unsigned int freq;
   int ret;
   int stream_flag;
 
   switch (params_rate(params)) {
   case    8000:
   case    12000:
   case    16000:
   case    24000:
   case    32000:
   case    48000:
   case    96000:
   case    192000:
       freq = 24576000;
       break;
   case    11025:
   case    22050:
   case    44100:
       freq = 22579200;
       break;
   default:
       dev_err(card->dev, "invalid rate setting\n");
       return -EINVAL;
   }
 
   /* the substream type: 0->playback, 1->capture */
   stream_flag = substream->stream;
   pr_debug("sndcodec: stream_flag: %d\n", stream_flag);
 
   /* To surpport playback and capture func in different freq point */
   if (freq == 22579200) {
       if (stream_flag == 0) {
           ret = snd_soc_dai_set_sysclk(codec_dai, 0, freq, 0);
           if (ret < 0) {
               dev_err(card->dev, "sndcodec:set codec dai sysclk faided, freq:%d\n", freq);
               return ret;
           }
       }
   }
 
   if (freq == 22579200) {
       if (stream_flag == 1) {
           ret = snd_soc_dai_set_sysclk(codec_dai, 1, freq, 0);
           if (ret < 0) {
               dev_err(card->dev, "sndcodec:set codec dai sysclk faided, freq:%d\n", freq);
               return ret;
           }
       }
   }
 
   if (freq == 24576000) {
       if (stream_flag == 0) {
           ret = snd_soc_dai_set_sysclk(codec_dai, 2, freq, 0);
           if (ret < 0) {
               dev_err(card->dev, "sndcodec:set codec dai sysclk faided, freq:%d\n", freq);
               return ret;
           }
       }
   }
 
   if (freq == 24576000) {
           if (stream_flag == 1) {
           ret = snd_soc_dai_set_sysclk(codec_dai, 3, freq, 0);
           if (ret < 0) {
               dev_err(card->dev, "sndcodec:set codec dai sysclk faided, freq:%d\n", freq);
               return ret;
           }
       }
   }
 
   return 0;
}
 
static struct snd_soc_ops sunxi_card_ops = {
   .hw_params = sunxi_card_hw_params,
};
 
static struct snd_soc_dai_link sunxi_card_dai_link[] = {
   {
       .name        = "sun50iw10-codec",
       .stream_name    = "SUNXI-CODEC",
       .codec_name    = "codec",
       .codec_dai_name = "sun50iw10codec",
       .cpu_dai_name    = "codec",
       .platform_name    = "codec",
       .init        = sunxi_card_init,
       .ops        = &sunxi_card_ops,
   },
};
 
#ifdef CONFIG_SND_SUNXI_SOC_SUN50IW10_JACK
static void sunxi_hs_reg_init(struct sunxi_card_priv *priv)
{
   snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL, (0xffff << 0),
               (0x0 << 0));
   snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
           (0x1f << MDATA_THRESHOLD),
           (0x17 << MDATA_THRESHOLD));
 
   snd_soc_update_bits(priv->codec, SUNXI_HMIC_STS, (0xffff << 0),
               (0x6000 << 0));
   snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG,
           (0xff << SELDETADCBF), (0x40 << SELDETADCBF));
 
   if (priv->hp_detect_case == HP_DETECT_LOW) {
       snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG,
                   (0x1 << AUTOPLEN), (0x1 << AUTOPLEN));
       snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG,
                   (0x1 << DETMODE), (0x0 << DETMODE));
   } else {
       snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG,
                   (0x1 << AUTOPLEN), (0x0 << AUTOPLEN));
       snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG,
                   (0x1 << DETMODE), (0x1 << DETMODE));
   }
   snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG, (0x1 << JACKDETEN),
               (0x1 << JACKDETEN));
   /*enable jack in /out irq*/
   snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
               (0x1 << JACK_IN_IRQ_EN), (0x1 << JACK_IN_IRQ_EN));
   snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
               (0x1 << JACK_OUT_IRQ_EN), (0x1 << JACK_OUT_IRQ_EN));
 
   snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
           (0xf << HMIC_N), (HP_DEBOUCE_TIME << HMIC_N));
 
   if (!priv->noheadphonemic) {
       snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG,
                   (0x1 << HMICBIASEN), (0x1 << HMICBIASEN));
       snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG,
                   (0x1 << MICADCEN), (0x1 << MICADCEN));
   }
 
   snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
               (0x1 << MIC_DET_IRQ_EN),
               (0x1 << MIC_DET_IRQ_EN));
 
   schedule_delayed_work(&priv->hs_init_work, msecs_to_jiffies(10));
}
 
static void snd_sunxi_unregister_jack(struct sunxi_card_priv *priv)
{
   /*
    * Set process button events to false so that the button
    * delayed work will not be scheduled.
    */
   cancel_delayed_work_sync(&priv->hs_detect_work);
   cancel_delayed_work_sync(&priv->hs_button_work);
   cancel_delayed_work_sync(&priv->hs_init_work);
}
#endif
 
static int sunxi_card_suspend(struct snd_soc_card *card)
{
#ifdef CONFIG_SND_SUNXI_SOC_SUN50IW10_JACK
   struct sunxi_card_priv *priv = snd_soc_card_get_drvdata(card);
 
   disable_irq(priv->jackirq);
 
   snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
               (0x1 << MIC_DET_IRQ_EN), (0x0 << MIC_DET_IRQ_EN));
   snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
               (0x1 << JACK_IN_IRQ_EN), (0x0 << JACK_IN_IRQ_EN));
   snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
               (0x1 << JACK_OUT_IRQ_EN), (0x0 << JACK_OUT_IRQ_EN));
   snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG, (0x1 << JACKDETEN),
               (0x0 << JACKDETEN));
#endif
   pr_debug("[codec-machine]  suspend.\n");
 
   return 0;
}
 
static int sunxi_card_resume(struct snd_soc_card *card)
{
#ifdef CONFIG_SND_SUNXI_SOC_SUN50IW10_JACK
   struct sunxi_card_priv *priv = snd_soc_card_get_drvdata(card);
 
   enable_irq(priv->jackirq);
   priv->jack_irq_times = RESUME_IRQ;
   priv->detect_state = PLUG_OUT;/*todo..?*/
   sunxi_hs_reg_init(priv);
#endif
   pr_debug("[codec-machine]  resume.\n");
 
   return 0;
}
 
static struct snd_soc_card snd_soc_sunxi_card = {
   .name            = "sun50iw10-codec",
   .owner            = THIS_MODULE,
   .dai_link        = sunxi_card_dai_link,
   .num_links        = ARRAY_SIZE(sunxi_card_dai_link),
#if 0
   .controls        = sunxi_card_controls,
   .num_controls        = ARRAY_SIZE(sunxi_card_controls),
   .dapm_widgets        = sunxi_card_dapm_widgets,
   .num_dapm_widgets     = ARRAY_SIZE(sunxi_card_dapm_widgets),
   .dapm_routes         = sunxi_card_routes,
   .num_dapm_routes     = ARRAY_SIZE(sunxi_card_routes),
#endif
   .suspend_post        = sunxi_card_suspend,
   .resume_post        = sunxi_card_resume,
 
};
 
static int sunxi_card_dev_probe(struct platform_device *pdev)
{
   int ret = 0;
#ifdef CONFIG_SND_SUNXI_SOC_SUN50IW10_JACK
   u32 temp_val;
#endif
   struct sunxi_card_priv *priv = NULL;
   struct device_node *np = pdev->dev.of_node;
   struct snd_soc_card *card = &snd_soc_sunxi_card;
   struct snd_soc_dapm_context *dapm = &card->dapm;
 
   if (!np) {
       dev_err(&pdev->dev, "can not get dt node for this device.\n");
       return -EINVAL;
   }
 
   /* register the soc card */
   card->dev = &pdev->dev;
 
   priv = devm_kzalloc(&pdev->dev,
       sizeof(struct sunxi_card_priv), GFP_KERNEL);
   if (!priv) {
       dev_err(&pdev->dev, "devm_kzalloc failed %d\n", ret);
       return -ENOMEM;
   }
   priv->card = card;
 
   snd_soc_card_set_drvdata(card, priv);
   ret = snd_soc_register_card(card);
   if (ret) {
       dev_err(&pdev->dev, "snd_soc_register_card failed %d\n", ret);
       goto err_devm_kfree;
   }
 
   ret = snd_soc_add_card_controls(card, sunxi_card_controls,
                   ARRAY_SIZE(sunxi_card_controls));
   if (ret)
       pr_err("failed to register codec controls!\n");
 
   snd_soc_dapm_new_controls(dapm, sunxi_card_dapm_widgets,
               ARRAY_SIZE(sunxi_card_dapm_widgets));
   snd_soc_dapm_add_routes(dapm, sunxi_card_routes,
               ARRAY_SIZE(sunxi_card_routes));
 
 
#ifdef CONFIG_SND_SUNXI_SOC_SUN50IW10_JACK
   ret = of_property_read_u32(np, "hp_detect_case", &temp_val);
   if (ret < 0) {
       pr_err("[audio] hp_detect_case  missing or invalid.\n");
   } else {
       priv->hp_detect_case = temp_val;
       pr_err("[audio] hp_detect_case: %d\n", priv->hp_detect_case);
   }
 
   ret = of_property_read_u32(np, "noheadphonemic", &temp_val);
   if (ret < 0) {
       pr_err("[audio] noheadphonemic missing or it has headphonemic.\n");
       priv->noheadphonemic = 0;
   } else {
       priv->noheadphonemic = temp_val;
       pr_debug("[audio] noheadphonemic: %d\n", priv->noheadphonemic);
   }
 
   priv->jackirq = platform_get_irq(pdev, 0);
   if (priv->jackirq < 0) {
       pr_err("[audio] irq failed\n");
       ret = -ENODEV;
   }
 
   priv->jack_irq_times = SYSINIT_IRQ;
 
   /*
    * initial the parameters for judge switch state
    */
   priv->HEADSET_DATA = 0x10;
   priv->detect_state = PLUG_IN;
   INIT_DELAYED_WORK(&priv->hs_detect_work, sunxi_check_hs_detect_status);
   INIT_DELAYED_WORK(&priv->hs_button_work, sunxi_check_hs_button_status);
   INIT_DELAYED_WORK(&priv->hs_init_work, sunxi_hs_init_work);
   INIT_DELAYED_WORK(&priv->hs_checkplug_work, sunxi_check_hs_plug);
   mutex_init(&priv->jack_mlock);
 
   ret = request_irq(priv->jackirq, jack_interrupt,
          0, "audio jack irq", priv);
 
   sunxi_hs_reg_init(priv);
#if 0
   pr_debug("[%s] 0x310:0x%X,0x314:0x%X,0x318:0x%X,0x1C:0x%X,0x1D:0x%X\n",
           __func__, snd_soc_read(priv->codec, 0x310),
           snd_soc_read(priv->codec, 0x314),
           snd_soc_read(priv->codec, 0x318),
           snd_soc_read(priv->codec, 0x1C),
           snd_soc_read(priv->codec, 0x1D));
#endif
#endif
 
   dev_warn(&pdev->dev, "[%s] register card finished.\n", __func__);
 
   schedule_delayed_work(&priv->hs_detect_work, msecs_to_jiffies(10));
 
   return 0;
 
err_devm_kfree:
   devm_kfree(&pdev->dev, priv);
   return ret;
}
 
static int __exit sunxi_card_dev_remove(struct platform_device *pdev)
{
   struct snd_soc_card *card = platform_get_drvdata(pdev);
   struct sunxi_card_priv *priv = snd_soc_card_get_drvdata(card);
 
#ifdef CONFIG_SND_SUNXI_SOC_SUN50IW10_JACK
   snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
               (0x1 << JACK_IN_IRQ_EN), (0x0 << JACK_IN_IRQ_EN));
   snd_soc_update_bits(priv->codec, SUNXI_HMIC_CTRL,
               (0x1 << JACK_OUT_IRQ_EN), (0x0 << JACK_OUT_IRQ_EN));
   snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG, (0x1 << JACKDETEN),
               (0x0 << JACKDETEN));
   snd_soc_update_bits(priv->codec, SUNXI_MICBIAS_REG,
               (0x1 << HMICBIASEN), (0x0 << HMICBIASEN));
   snd_sunxi_unregister_jack(priv);
#endif
 
   snd_soc_unregister_card(card);
   devm_kfree(&pdev->dev, priv);
 
   dev_warn(&pdev->dev, "[%s] unregister card finished.\n", __func__);
 
   return 0;
}
 
static const struct of_device_id sunxi_card_of_match[] = {
   { .compatible = "allwinner,sunxi-codec-machine", },
   {},
};
 
static struct platform_driver sunxi_machine_driver = {
   .driver = {
       .name = "sunxi-codec-machine",
       .owner = THIS_MODULE,
       .pm = &snd_soc_pm_ops,
       .of_match_table = sunxi_card_of_match,
   },
   .probe = sunxi_card_dev_probe,
   .remove = __exit_p(sunxi_card_dev_remove),
};
 
module_platform_driver(sunxi_machine_driver);
#ifdef CONFIG_SND_SUNXI_SOC_SUN50IW10_JACK
module_param_named(switch_state, switch_state, int, S_IRUGO | S_IWUSR);
#endif
 
MODULE_AUTHOR("luguofang <luguofang@allwinnertech.com>");
MODULE_DESCRIPTION("SUNXI Codec Machine ASoC driver");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:sunxi-codec-machine");