huangcm
2025-04-26 bff3d3009b0d3a2be3ed92e4817fcabee9e76955
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
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
/*
 * sound\soc\sunxi\sunxi_sndcodec.c
 * (C) Copyright 2014-2017
 * Reuuimlla Technology Co., Ltd. <www.reuuimllatech.com>
 * huangxin <huangxin@Reuuimllatech.com>
 * liushaohua <liushaohua@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 "sun50iw3-codec.h"
#include <linux/delay.h>
 
#include "sunxi_rw_func.h"
 
#ifdef CONFIG_SND_SOC_HUB
#include "sunxi_hub.h"
#endif
 
typedef enum {
   RESUME_IRQ  = 0x0,
   SYSINIT_IRQ = 0x1,
   OTHER_IRQ   = 0x2,
} _jack_irq_times;
 
struct mc_private {
   struct delayed_work hs_init_work;
   struct delayed_work hs_detect_work;
   struct delayed_work hs_button_work;
   struct mutex jack_mlock;
   struct snd_soc_jack jack;
   struct snd_soc_codec *codec;
   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 aif2master;
   u32 aif2fmt;
   u32 aif3fmt;
   u32 key_volup;
   u32 key_voldown;
   u32 key_hook;
   u32 hp_detect_case;
};
 
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;
static struct snd_soc_dai *card0_device0_interface;
 
/*
* 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 mc_private *ctx = container_of(jack, struct mc_private, jack);
 
   reg_val = snd_soc_read(ctx->codec, SUNXI_HMIC_STS);
   tempdata = (reg_val >> HMIC_DATA) & 0x1f;
 
   pr_err("tempdata:%x,ctx->HEADSET_DATA:%x,reg_val:%x\n", tempdata,
          ctx->HEADSET_DATA, reg_val);
 
   ctx->headset_basedata = tempdata;
   if (tempdata >= ctx->HEADSET_DATA) {
       pr_err("%s,l:%d, ctx->HEADSET_DATA:%d\n", __func__, __LINE__,
              ctx->HEADSET_DATA);
       /*
       * headset:4
       */
       jack_type = SND_JACK_HEADSET;
   } else {
       /*
       * headphone:3
       * disable hbias and adc
       */
       pr_err("%s,l:%d, ctx->HEADSET_DATA:%d\n", __func__, __LINE__,
              ctx->HEADSET_DATA);
       snd_soc_update_bits(ctx->codec, JACK_MIC_CTRL,
                   (0x1 << HMICBIASEN), (0x0 << HMICBIASEN));
       snd_soc_update_bits(ctx->codec, JACK_MIC_CTRL,
                   (0x1 << MICADCEN), (0x0 << MICADCEN));
       jack_type = SND_JACK_HEADPHONE;
   }
 
   return jack_type;
}
 
/* Checks jack insertion and identifies the jack type.*/
static void sunxi_check_hs_detect_status(struct work_struct *work)
{
   struct mc_private *ctx =
       container_of(work, struct mc_private, hs_detect_work.work);
   int jack_type = 0, reg_val = 0;
 
   mutex_lock(&ctx->jack_mlock);
   if (ctx->detect_state == PLUG_IN) {
       /*enable hbias and adc*/
       snd_soc_update_bits(ctx->codec, JACK_MIC_CTRL,
                   (0x1 << HMICBIASEN), (0x1 << HMICBIASEN));
       snd_soc_update_bits(ctx->codec, JACK_MIC_CTRL,
                   (0x1 << MICADCEN), (0x1 << MICADCEN));
       msleep(500);
       jack_type = sunxi_check_jack_type(&ctx->jack);
       if (jack_type != ctx->switch_status) {
           ctx->switch_status = jack_type;
           snd_jack_report(ctx->jack.jack, jack_type);
           pr_debug("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(ctx->codec, SUNXI_HMIC_STS);
           ctx->headset_basedata = (reg_val >> HMIC_DATA) & 0x1f;
           if (ctx->headset_basedata > 3)
               ctx->headset_basedata -= 3;
 
           do_gettimeofday(&ctx->tv_headset_plugin);
           usleep_range(1000, 2000);
           snd_soc_update_bits(ctx->codec, SUNXI_HMIC_CTRL2,
               (0x1f << MDATA_THRESHOLD),
               (ctx->headset_basedata << MDATA_THRESHOLD));
           snd_soc_update_bits(ctx->codec, SUNXI_HMIC_CTRL1,
                       (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(ctx->codec, SUNXI_HMIC_CTRL1,
                       (0x1 << MIC_DET_IRQ_EN),
                       (0x0 << MIC_DET_IRQ_EN));
           ctx->tv_headset_plugin.tv_sec = 0;
       }
   } else {
       snd_soc_update_bits(ctx->codec, SUNXI_HMIC_CTRL1,
                   (0x1 << MIC_DET_IRQ_EN),
                   (0x0 << MIC_DET_IRQ_EN));
       /*diable hbias and adc*/
       snd_soc_update_bits(ctx->codec, JACK_MIC_CTRL,
                   (0x1 << HMICBIASEN), (0x0 << HMICBIASEN));
       snd_soc_update_bits(ctx->codec, JACK_MIC_CTRL,
                   (0x1 << MICADCEN), (0x0 << MICADCEN));
       ctx->switch_status = 0;
       /*clear headset pulgout pending.*/
       snd_jack_report(ctx->jack.jack, ctx->switch_status);
       switch_state = ctx->switch_status;
       pr_err("plugout_end:switch:%d\n", ctx->switch_status);
       ctx->tv_headset_plugin.tv_sec = 0;
   }
   mutex_unlock(&ctx->jack_mlock);
}
 
static void sunxi_hs_init_work(struct work_struct *work)
{
   struct mc_private *ctx =
       container_of(work, struct mc_private, hs_init_work.work);
 
   mutex_lock(&ctx->jack_mlock);
   if (is_irq == true) {
       is_irq = false;
   } else {
       if (ctx->hp_detect_case == HP_DETECT_LOW) {
           ctx->switch_status = 0;
           snd_jack_report(ctx->jack.jack, ctx->switch_status);
           switch_state = 0;
       }
   }
   ctx->jack_irq_times = OTHER_IRQ;
   mutex_unlock(&ctx->jack_mlock);
}
 
/* Check for hook release */
static void sunxi_check_hs_button_status(struct work_struct *work)
{
   struct mc_private *ctx =
       container_of(work, struct mc_private, hs_button_work.work);
   u32 i = 0;
 
   mutex_lock(&ctx->jack_mlock);
   for (i = 0; i < 1; i++) {
       if (ctx->key_hook == 0) {
           pr_info("Hook (2)!!\n");
           ctx->switch_status &= ~SND_JACK_BTN_0;
           snd_jack_report(ctx->jack.jack, ctx->switch_status);
           break;
       }
       msleep(8);
   }
   mutex_unlock(&ctx->jack_mlock);
}
 
static irqreturn_t jack_interrupt(int irq, void *dev_id)
{
   struct mc_private *ctx = dev_id;
   struct timeval tv;
   u32 tempdata = 0, regval = 0;
   int jack_state = 0;
 
   if (ctx->jack_irq_times == RESUME_IRQ ||
       ctx->jack_irq_times == SYSINIT_IRQ) {
       is_irq = true;
       ctx->jack_irq_times = OTHER_IRQ;
   }
 
   pr_debug("SUNXI_HMIC_STS:%x,%d\n",
       snd_soc_read(ctx->codec, SUNXI_HMIC_STS), __LINE__);
 
   jack_state = snd_soc_read(ctx->codec, SUNXI_HMIC_STS);
   /*headphone insert*/
   if (jack_state & (1 << JACK_DET_IIN_ST)) {
       regval = snd_soc_read(ctx->codec, SUNXI_HMIC_STS);
       regval |= 0x1 << JACK_DET_IIN_ST;
       regval &= ~(0x1 << JACK_DET_OUT_ST);
       snd_soc_write(ctx->codec, SUNXI_HMIC_STS, regval);
       ctx->detect_state = PLUG_IN;
       schedule_delayed_work(&ctx->hs_detect_work,
                     msecs_to_jiffies(10));
   }
 
   /*headphone plugout*/
   if (jack_state & (1 << JACK_DET_OUT_ST)) {
       regval = snd_soc_read(ctx->codec, SUNXI_HMIC_STS);
       regval &= ~(0x1 << JACK_DET_IIN_ST);
       regval |= 0x1 << JACK_DET_OUT_ST;
       snd_soc_write(ctx->codec, SUNXI_HMIC_STS, regval);
       ctx->detect_state = PLUG_OUT;
       schedule_delayed_work(&ctx->hs_detect_work,
                     msecs_to_jiffies(10));
   }
 
   /*key*/
   if (jack_state & (1 << MIC_DET_ST)) {
       regval = snd_soc_read(ctx->codec, SUNXI_HMIC_STS);
       regval &= ~(0x1 << JACK_DET_IIN_ST);
       regval &= ~(0x1 << JACK_DET_OUT_ST);
       regval |= 0x1 << MIC_DET_ST;
       snd_soc_write(ctx->codec, SUNXI_HMIC_STS, regval);
 
       do_gettimeofday(&tv);
       if (abs(tv.tv_sec - ctx->tv_headset_plugin.tv_sec) > 2) {
           tempdata = snd_soc_read(ctx->codec, SUNXI_HMIC_STS);
           tempdata = (tempdata & 0x1f00) >> 8;
           pr_debug("headset key debug tempdata : 0x%x.\n",
                tempdata);
           if (tempdata == 2) {
               ctx->key_hook = 0;
               ctx->key_voldown = 0;
               ctx->key_volup++;
               if (ctx->key_volup == 1) {
                   pr_debug("Volume + !!\n");
                   ctx->key_volup = 0;
                   ctx->switch_status |= SND_JACK_BTN_1;
                   snd_jack_report(ctx->jack.jack,
                           ctx->switch_status);
                   ctx->switch_status &= ~SND_JACK_BTN_1;
                   snd_jack_report(ctx->jack.jack,
                           ctx->switch_status);
               }
           } else if (tempdata == 5) {
               ctx->key_volup = 0;
               ctx->key_hook = 0;
               ctx->key_voldown++;
               if (ctx->key_voldown == 1) {
                   pr_debug("Volume - !!\n");
                   ctx->key_voldown = 0;
                   ctx->switch_status |= SND_JACK_BTN_2;
                   snd_jack_report(ctx->jack.jack,
                           ctx->switch_status);
                   ctx->switch_status &= ~SND_JACK_BTN_2;
                   snd_jack_report(ctx->jack.jack,
                           ctx->switch_status);
               }
           } else if (tempdata == 0x0) {
               ctx->key_volup = 0;
               ctx->key_voldown = 0;
               ctx->key_hook++;
               if (ctx->key_hook >= 1) {
                   ctx->key_hook = 0;
                   if ((ctx->switch_status &
                        SND_JACK_BTN_0) == 0) {
                       ctx->switch_status |=
                           SND_JACK_BTN_0;
                       snd_jack_report(
                           ctx->jack.jack,
                           ctx->switch_status);
                       pr_debug("Hook (1)!!\n");
                   }
                   schedule_delayed_work(
                       &ctx->hs_button_work,
                       msecs_to_jiffies(180));
               }
           } else {
               /*This could be key release,fix me ! */
               pr_debug("tempdata:%x,Key data err:\n",
                    tempdata);
               ctx->key_volup = 0;
               ctx->key_voldown = 0;
               ctx->key_hook = 0;
           }
       } else {
       }
   }
 
   return IRQ_HANDLED;
}
 
static const struct snd_kcontrol_new ac_pin_controls[] = {
   SOC_DAPM_PIN_SWITCH("External Speaker"),
   SOC_DAPM_PIN_SWITCH("Headphone"),
   SOC_DAPM_PIN_SWITCH("Lineout"),
};
 
static const struct snd_soc_dapm_widget sunxi_ac_dapm_widgets[] = {
   SND_SOC_DAPM_MIC("External MainMic", NULL),
   SND_SOC_DAPM_MIC("HeadphoneMic", NULL),
};
 
static const struct snd_soc_dapm_route audio_map[] = {
   {"MainMic Bias", NULL, "External MainMic"},
   {"MIC1P", NULL, "MainMic Bias"},
   {"MIC1N", NULL, "MainMic Bias"},
   {"MIC3", NULL, "MainMic Bias"},
   {"MIC2", NULL, "HeadphoneMic"},
};
 
/*
 * Card initialization
 */
static int sunxi_audio_init(struct snd_soc_pcm_runtime *runtime)
{
   int ret;
   struct snd_soc_codec *codec = runtime->codec;
   struct snd_soc_dapm_context *dapm = &codec->component.dapm;
   struct mc_private *ctx = snd_soc_card_get_drvdata(runtime->card);
 
   card0_device0_interface = runtime->cpu_dai;
   ctx->codec = runtime->codec;
   ret = snd_soc_card_jack_new(runtime->card, "sunxi Audio Jack",
                  SND_JACK_HEADSET | SND_JACK_HEADPHONE |
                  SND_JACK_BTN_0 | SND_JACK_BTN_1 |
                  SND_JACK_BTN_2,
                  &ctx->jack, NULL, 0);
   if (ret) {
       pr_err("jack creation failed\n");
       return ret;
   }
 
   snd_jack_set_key(ctx->jack.jack, SND_JACK_BTN_0, KEY_MEDIA);
   snd_jack_set_key(ctx->jack.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
   snd_jack_set_key(ctx->jack.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
 
   snd_soc_dapm_disable_pin(dapm, "HPOUTR");
   snd_soc_dapm_disable_pin(dapm, "HPOUTL");
   snd_soc_dapm_disable_pin(dapm, "LINEOUTL");
   snd_soc_dapm_disable_pin(dapm, "LINEOUTR");
   snd_soc_dapm_disable_pin(dapm, "SPKL");
   snd_soc_dapm_disable_pin(dapm, "SPKR");
 
   snd_soc_dapm_disable_pin(&runtime->card->dapm, "External Speaker");
   snd_soc_dapm_disable_pin(&runtime->card->dapm, "Headphone");
   snd_soc_dapm_disable_pin(&runtime->card->dapm, "Lineout");
   snd_soc_dapm_sync(dapm);
   return 0;
}
static int sunxi_sndpcm_hw_params(struct snd_pcm_substream *substream,
                 struct snd_pcm_hw_params *params)
{
   int ret = 0;
   u32 freq_in = 22579200;
   struct snd_soc_pcm_runtime *rtd = substream->private_data;
   struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
   struct snd_soc_dai *codec_dai = rtd->codec_dai;
   unsigned long sample_rate = params_rate(params);
 
   switch (sample_rate) {
   case 8000:
   case 16000:
   case 32000:
   case 64000:
   case 128000:
   case 12000:
   case 24000:
   case 48000:
   case 96000:
   case 192000:
       freq_in = 24576000;
       break;
   }
   /* set the codec FLL */
   ret = snd_soc_dai_set_pll(codec_dai, PLLCLK, 0, freq_in, freq_in);
   if (ret < 0) {
       pr_err("err:%s,set codec dai pll failed.\n", __func__);
       return ret;
   }
   /*set system clock source freq */
   ret = snd_soc_dai_set_sysclk(cpu_dai, 0, freq_in, 0);
   if (ret < 0) {
       pr_err("err:%s,set cpu dai sysclk failed.\n", __func__);
       return ret;
   }
   /*set system clock source freq_in and set the mode as tdm or pcm*/
   ret = snd_soc_dai_set_sysclk(codec_dai, AIF1_CLK, freq_in, 0);
   if (ret < 0) {
       pr_err("err:%s,set codec dai sysclk faided.\n", __func__);
       return ret;
   }
   /*set system fmt:api2s:master aif1:slave*/
   ret = snd_soc_dai_set_fmt(cpu_dai, 0);
   if (ret < 0) {
       pr_err("%s,set cpu dai fmt failed.\n", __func__);
       return ret;
   }
 
   /*
   * codec: slave. AP: master
   */
   ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S |
                        SND_SOC_DAIFMT_NB_NF |
                        SND_SOC_DAIFMT_CBS_CFS);
   if (ret < 0) {
       pr_err("%s,set codec dai fmt failed.\n", __func__);
       return ret;
   }
 
   ret = snd_soc_dai_set_clkdiv(cpu_dai, 0, sample_rate);
   if (ret < 0) {
       pr_err("%s, set cpu dai clkdiv faided.\n", __func__);
       return ret;
 
   }
 
   return 0;
}
 
static struct snd_soc_ops sunxi_sndpcm_ops = {
   .hw_params = sunxi_sndpcm_hw_params,
};
 
static int bb_voice_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;
   struct mc_private *ctx = NULL;
 
   int ret = 0;
   int freq_in = 24576000;
 
   if (params_rate(params) != 8000)
       pr_warn("%s,line:%d,params_rate(params):%d\n", __func__,
              __LINE__, params_rate(params));
 
   ctx = snd_soc_card_get_drvdata(card);
   if (ctx == NULL) {
       pr_err("err:%s,get ctx failed.\n", __func__);
       return -EINVAL;
   }
 
   /*set system clock source freq */
   ret = snd_soc_dai_set_sysclk(card0_device0_interface, 0, freq_in, 0);
   if (ret < 0) {
       pr_err("err:%s,set cpu dai sysclk failed.\n", __func__);
       return ret;
   }
 
   /* set the codec aif1clk/aif2clk from pllclk */
   ret = snd_soc_dai_set_pll(codec_dai, PLLCLK, 0, freq_in, freq_in);
   if (ret < 0) {
       pr_err("err:%s,set codec dai pll failed.\n", __func__);
       return ret;
   }
   /*set system clock source aif2*/
   ret = snd_soc_dai_set_sysclk(codec_dai, AIF2_CLK, 0, 0);
   if (ret < 0) {
       pr_err("err:%s,set codec dai sysclk faied\n", __func__);
       return ret;
   }
   /* set codec DAI configuration */
   ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_A |
                        (ctx->aif2fmt << 8) |
                        (ctx->aif2master << 12));
   if (ret < 0)
       pr_err("err:%s,set codec dai fmt failed.\n", __func__);
 
   return ret;
}
 
static struct snd_soc_ops bb_voice_ops = {
   .hw_params = bb_voice_hw_params,
};
static int bb_clk_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;
   struct mc_private *ctx = NULL;
 
   int ret = 0;
   int freq_in = 24576000;
 
   if (params_rate(params) != 8000)
       pr_warn("%s,line:%d,params_rate(params):%d\n", __func__,
              __LINE__, params_rate(params));
 
   ctx = snd_soc_card_get_drvdata(card);
   if (ctx == NULL) {
       pr_err("err:%s,get ctx failed.\n", __func__);
       return -EINVAL;
   }
   /*set system clock source freq */
   ret = snd_soc_dai_set_sysclk(card0_device0_interface, 0, freq_in, 0);
   if (ret < 0) {
       pr_err("err:%s,set cpu dai sysclk failed.\n", __func__);
       return ret;
   }
   /* set the codec aif1clk/aif2clk from pllclk */
   ret = snd_soc_dai_set_pll(codec_dai, PLLCLK, 0, freq_in, freq_in);
   if (ret < 0) {
       pr_err("err:%s,set codec dai pll failed.\n", __func__);
       return ret;
   }
   /*set system clock source aif2*/
   ret = snd_soc_dai_set_sysclk(codec_dai, AIF2_CLK, 0, 0);
   if (ret < 0) {
       pr_err("err:%s,set codec dai sysclk faied\n", __func__);
       return ret;
   }
   /* set codec DAI configuration */
   ret = snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_DSP_A |
                        SND_SOC_DAIFMT_IB_NF |
                        SND_SOC_DAIFMT_CBM_CFM);
   if (ret < 0)
       pr_err("err:%s,set codec dai fmt failed.\n", __func__);
 
   return ret;
}
static struct snd_soc_ops bb_clk_ops = {
   .hw_params = bb_clk_hw_params,
};
static int bt_hw_params(struct snd_pcm_substream *substream,
       struct snd_pcm_hw_params *params)
{
   struct mc_private *ctx = NULL;
   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;
   int ret = 0;
 
   if (params_rate(params) != 8000)
       pr_warn("%s,line:%d,params_rate(params):%d\n", __func__,
              __LINE__, params_rate(params));
   ctx = snd_soc_card_get_drvdata(card);
   if (ctx == NULL) {
       pr_err("err:%s,get ctx failed.\n", __func__);
       return -EINVAL;
   }
   /* set codec aif3 configuration */
   ret = snd_soc_dai_set_fmt(codec_dai, (ctx->aif3fmt << 8));
   if (ret < 0)
       return ret;
   return 0;
}
 
static struct snd_soc_ops bt_voice_ops = {
   .hw_params = bt_hw_params,
};
 
static struct snd_soc_dai_link sunxi_sndpcm_dai_link[] = {
   {
   .name = "audiocodec",
   .stream_name    = "SUNXI-CODEC",
   .cpu_dai_name    = "sunxi-internal-i2s",
   .codec_dai_name = "codec-aif1",
   .platform_name    = "sunxi-internal-i2s",
   .codec_name    = "sunxi-internal-codec",
   .init        = sunxi_audio_init,
   .ops = &sunxi_sndpcm_ops,
 
   },
/**/
   {
   .name = "Voice",
   .stream_name = "bb Voice",
   .cpu_dai_name = "bb-dai",
   .codec_dai_name = "codec-aif2",
   .codec_name = "sunxi-pcm-codec",
   .ops = &bb_voice_ops,
   },
/**/
   {
   .name = "bbclk",
   .stream_name = "bb-bt-clk",
   .cpu_dai_name = "bb-dai",
   .codec_dai_name = "codec-aif2",
   .codec_name = "sunxi-pcm-codec",
   .ops = &bb_clk_ops,
   },
   {
   .name = "bt",
   .stream_name = "bt Voice",
   .cpu_dai_name = "bb-dai",
   .codec_dai_name = "codec-aif3",
   .codec_name = "sunxi-pcm-codec",
   .ops = &bt_voice_ops,
   },
#ifdef CONFIG_SND_SOC_HUB
   {
   /*hub-spdif*/
   },
   {
   /*hub-hdmi*/
   },
#endif
/**/
};
 
static void sunxi_hs_reg_init(struct mc_private *ctx)
{
   snd_soc_update_bits(ctx->codec, SUNXI_HMIC_CTRL2, (0xffff << 0),
               (0x0 << 0)); /*0x314*/
   snd_soc_update_bits(ctx->codec, SUNXI_HMIC_CTRL2, (0x1f << 8),
               (0x17 << 8)); /*0x318*/
   snd_soc_update_bits(ctx->codec, SUNXI_HMIC_STS, (0xffff << 0),
               (0x0 << 0)); /*0x1c*/
   snd_soc_update_bits(ctx->codec, MDET_CTRL, (0xffff << 0), (0x70 << 0));
 
   if (ctx->hp_detect_case == HP_DETECT_LOW) {
       snd_soc_update_bits(ctx->codec, JACK_MIC_CTRL,
                   (0x1 << AUTOPLEN), (0x1 << AUTOPLEN));
       snd_soc_update_bits(ctx->codec, ADC_CTRL,
                   (0x1 << DET_MOD), (0x0 << DET_MOD));
   } else {
       snd_soc_update_bits(ctx->codec, JACK_MIC_CTRL,
                   (0x1 << AUTOPLEN), (0x0 << AUTOPLEN));
       /*new bit in a63, tell hw high level when insert hp*/
       snd_soc_update_bits(ctx->codec, ADC_CTRL,
                   (0x1 << DET_MOD), (0x1 << DET_MOD));
   }
   snd_soc_update_bits(ctx->codec, JACK_MIC_CTRL, (0x1 << JACKDETEN),
               (0x1 << JACKDETEN));
   /*enable jack in /out irq*/ /*0x310*/
   snd_soc_update_bits(ctx->codec, SUNXI_HMIC_CTRL1,
               (0x1 << JACK_IN_IRQ_EN), (0x1 << JACK_IN_IRQ_EN));
   snd_soc_update_bits(ctx->codec, SUNXI_HMIC_CTRL1,
               (0x1 << JACK_OUT_IRQ_EN), (0x1 << JACK_OUT_IRQ_EN));
   snd_soc_update_bits(ctx->codec, SUNXI_HMIC_CTRL1, (0xf << HMIC_N),
               (HP_DEBOUCE_TIME << HMIC_N));
 
   schedule_delayed_work(&ctx->hs_init_work, msecs_to_jiffies(10));
}
static int sunxi_suspend(struct snd_soc_card *card)
{
   struct mc_private *ctx = snd_soc_card_get_drvdata(card);
 
   disable_irq(ctx->jackirq);
   pr_debug("[codec-machine]  suspend.\n");
 
   snd_soc_update_bits(ctx->codec, SUNXI_HMIC_CTRL1,
               (0x1 << MIC_DET_IRQ_EN), (0x0 << MIC_DET_IRQ_EN));
   snd_soc_update_bits(ctx->codec, SUNXI_HMIC_CTRL1,
               (0x1 << JACK_IN_IRQ_EN), (0x0 << JACK_IN_IRQ_EN));
   snd_soc_update_bits(ctx->codec, SUNXI_HMIC_CTRL1,
               (0x1 << JACK_OUT_IRQ_EN), (0x0 << JACK_OUT_IRQ_EN));
   snd_soc_update_bits(ctx->codec, JACK_MIC_CTRL, (0x1 << JACKDETEN),
               (0x0 << JACKDETEN));
 
   return 0;
}
 
static int sunxi_resume(struct snd_soc_card *card)
{
   struct mc_private *ctx = snd_soc_card_get_drvdata(card);
 
   enable_irq(ctx->jackirq);
   ctx->jack_irq_times = RESUME_IRQ;
   ctx->detect_state = PLUG_OUT;/*todo..?*/
   sunxi_hs_reg_init(ctx);
   pr_debug("[codec-machine]  resume.\n");
 
   return 0;
}
 
static struct snd_soc_card snd_soc_sunxi_sndpcm = {
   .name        = "audiocodec",
   .owner        = THIS_MODULE,
   .dai_link    = sunxi_sndpcm_dai_link,
   .num_links    = ARRAY_SIZE(sunxi_sndpcm_dai_link),
   .dapm_widgets    = sunxi_ac_dapm_widgets,
   .num_dapm_widgets    = ARRAY_SIZE(sunxi_ac_dapm_widgets),
   .dapm_routes    = audio_map,
   .num_dapm_routes    = ARRAY_SIZE(audio_map),
   .controls    = ac_pin_controls,
   .num_controls    = ARRAY_SIZE(ac_pin_controls),
   .suspend_post    = sunxi_suspend,
   .resume_post    = sunxi_resume,
};
 
static struct snd_soc_dai_driver voice_dai[] = {
   {
       .name = "bb-dai",
       .id = 1,
       .playback = {
           .channels_min = 1,
           .channels_max = 2,
           .rates = SNDRV_PCM_RATE_8000_48000,
           .formats = SNDRV_PCM_FMTBIT_S16_LE,},
       .capture = {
           .channels_min = 1,
           .channels_max = 2,
           .rates = SNDRV_PCM_RATE_8000_48000,
           .formats = SNDRV_PCM_FMTBIT_S16_LE,},
   },
   {
       .name = "no-dai",
       .id = 2,
   }
};
 
static const struct snd_soc_component_driver voice_component = {
   .name        = "bb-voice",
};
 
static int sunxi_machine_probe(struct platform_device *pdev)
{
   int ret = 0;
   u32 temp_val;
   struct mc_private *ctx = NULL;
   struct device_node *np = pdev->dev.of_node;
 
   if (!np) {
       dev_err(&pdev->dev, "can not get dt node for this device.\n");
       return -EINVAL;
   }
 
   ctx = devm_kzalloc(&pdev->dev, sizeof(struct mc_private), GFP_KERNEL);
   if (!ctx) {
       pr_err("[audio] allocation mem failed\n");
       return -ENOMEM;
   }
   /* register voice DAI here */
   ret = snd_soc_register_component(&pdev->dev, &voice_component,
                    voice_dai, ARRAY_SIZE(voice_dai));
   if (ret) {
       dev_err(&pdev->dev, "register DAI failed\n");
       goto err0;
   }
 
#ifdef CONFIG_SND_SOC_HUB
   /*
   *sunxi_sndpcm_dai_link[0]:audiocodec
   *sunxi_sndpcm_dai_link[1]:Voice
   *sunxi_sndpcm_dai_link[2]:bbclk
   *sunxi_sndpcm_dai_link[3]:bt
   *sunxi_sndpcm_dai_link[4]:hub-hdmi
   *sunxi_sndpcm_dai_link[5]:hub-spdif
   */
   memcpy(sunxi_sndpcm_dai_link + 4, sunxi_hub_dai_link,
          sizeof(sunxi_hub_dai_link));
#endif
   /* register the soc card */
   snd_soc_sunxi_sndpcm.dev = &pdev->dev;
   snd_soc_card_set_drvdata(&snd_soc_sunxi_sndpcm, ctx);
   platform_set_drvdata(pdev, &snd_soc_sunxi_sndpcm);
   ctx->jackirq = platform_get_irq(pdev, 0);
   if (ctx->jackirq < 0) {
       pr_err("[audio]irq failed\n");
       ret = -ENODEV;
       goto err1;
   }
 
   ret = of_property_read_u32(np, "aif2fmt", &temp_val);
   if (ret < 0)
       pr_err("[audio]aif2fmt configurations missing or invalid.\n");
   else
       ctx->aif2fmt = temp_val;
 
 
   ret = of_property_read_u32(np, "aif3fmt", &temp_val);
   if (ret < 0)
       pr_err("[audio]aif3fmt configurations missing or invalid.\n");
   else
       ctx->aif3fmt = temp_val;
 
   ret = of_property_read_u32(np, "aif2master", &temp_val);
   if (ret < 0)
       pr_err(
           "[audio]aif2master configurations missing or invalid.\n");
   else
       ctx->aif2master = temp_val;
 
   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
       ctx->hp_detect_case = temp_val;
   /*here it is different form A64,we use bit det_mod to tell hw the
   *high/low level,thus the interrupt bit will always the same
   */
   pr_debug("%s,line:%d,IN_EN:%d,OUT_EN:%d,_OUT_ST:%d, IN_ST:%d\n",
        __func__, __LINE__, JACK_IN_IRQ_EN, JACK_OUT_IRQ_EN,
        JACK_DET_OUT_ST, JACK_DET_IIN_ST);
   sunxi_sndpcm_dai_link[0].cpu_dai_name = NULL;
   sunxi_sndpcm_dai_link[0].cpu_of_node =
       of_parse_phandle(np, "sunxi,i2s-controller", 0);
   if (!sunxi_sndpcm_dai_link[0].cpu_of_node) {
       dev_err(&pdev->dev,
           "Property 'sunxi,i2s-controller' missing or invalid\n");
       ret = -EINVAL;
       goto err1;
   }
   sunxi_sndpcm_dai_link[0].platform_name = NULL;
   sunxi_sndpcm_dai_link[0].platform_of_node =
       sunxi_sndpcm_dai_link[0].cpu_of_node;
 
   sunxi_sndpcm_dai_link[0].codec_name = NULL;
   sunxi_sndpcm_dai_link[0].codec_of_node =
       of_parse_phandle(np, "sunxi,audio-codec", 0);
   if (!sunxi_sndpcm_dai_link[0].codec_of_node) {
       dev_err(&pdev->dev,
           "Property 'sunxi,audio-codec' missing or invalid\n");
       ret = -EINVAL;
       goto err1;
   }
   sunxi_sndpcm_dai_link[1].codec_name = NULL;
   sunxi_sndpcm_dai_link[1].codec_of_node =
       sunxi_sndpcm_dai_link[0].codec_of_node;
 
   sunxi_sndpcm_dai_link[2].codec_name = NULL;
   sunxi_sndpcm_dai_link[2].codec_of_node =
       sunxi_sndpcm_dai_link[0].codec_of_node;
 
   sunxi_sndpcm_dai_link[3].codec_name = NULL;
   sunxi_sndpcm_dai_link[3].codec_of_node =
       sunxi_sndpcm_dai_link[0].codec_of_node;
 
   ret = snd_soc_register_card(&snd_soc_sunxi_sndpcm);
   if (ret) {
       pr_err("snd_soc_register_card failed %d\n", ret);
       goto err1;
   }
   /*
   *it is confuse we open bias here when High LEVEL,
   *disable it first and pay attention here!
   */
#if 0
   if (ctx->hp_detect_case == HP_DETECT_HIGH) { /*yj*/
       snd_soc_update_bits(ctx->codec, JACK_MIC_CTRL,
                   (0x1 << INNERRESEN), (0x1 << INNERRESEN));
       snd_soc_update_bits(ctx->codec, JACK_MIC_CTRL,
                   (0x1 << HMICBIASEN), (0x1 << HMICBIASEN));
       snd_soc_update_bits(ctx->codec, JACK_MIC_CTRL,
                   (0x1 << MICADCEN), (0x1 << MICADCEN));
   }
#endif
   ctx->jack_irq_times = SYSINIT_IRQ;
   /*
   *initial the parameters for judge switch state
   */
   ctx->HEADSET_DATA = 0x10;
   ctx->detect_state = PLUG_OUT;
   INIT_DELAYED_WORK(&ctx->hs_detect_work, sunxi_check_hs_detect_status);
   INIT_DELAYED_WORK(&ctx->hs_button_work, sunxi_check_hs_button_status);
   INIT_DELAYED_WORK(&ctx->hs_init_work, sunxi_hs_init_work);
   mutex_init(&ctx->jack_mlock);
 
   pr_debug("register jack interrupt.,jackirq:%d\n", ctx->jackirq);
   ret =
       request_irq(ctx->jackirq, jack_interrupt, 0, "audio jack irq", ctx);
 
   sunxi_hs_reg_init(ctx);
   pr_debug(
       "%s,line:%d,0X310:%X,0X314:%X,0X318:%X,0X1C:%X,0X1D:%X\n", __func__,
       __LINE__, snd_soc_read(ctx->codec, 0x310),
       snd_soc_read(ctx->codec, 0x314), snd_soc_read(ctx->codec, 0x318),
       snd_soc_read(ctx->codec, 0x1C), snd_soc_read(ctx->codec, 0x1D));
 
   return 0;
err1:
   snd_soc_unregister_component(&pdev->dev);
err0:
   return ret;
}
 
static void snd_sunxi_unregister_jack(struct mc_private *ctx)
{
   /*
   *Set process button events to false so that the button
   *delayed work will not be scheduled.
   */
   cancel_delayed_work_sync(&ctx->hs_detect_work);
   cancel_delayed_work_sync(&ctx->hs_button_work);
   cancel_delayed_work_sync(&ctx->hs_init_work);
}
 
static void sunxi_machine_shutdown(struct platform_device *pdev)
{
   struct snd_soc_card *soc_card = platform_get_drvdata(pdev);
   struct mc_private *ctx = snd_soc_card_get_drvdata(soc_card);
 
   /*
   *avoid some case when device register to device module
   *but not register to ALSA,this will happen
   *when shutdown at kernel start stage.
   */
   if (ctx->codec == NULL)
       return;
 
   snd_soc_update_bits(ctx->codec, SUNXI_HMIC_CTRL1,
               (0x1 << JACK_IN_IRQ_EN), (0x0 << JACK_IN_IRQ_EN));
   snd_soc_update_bits(ctx->codec, SUNXI_HMIC_CTRL1,
               (0x1 << JACK_OUT_IRQ_EN), (0x0 << JACK_OUT_IRQ_EN));
   snd_soc_update_bits(ctx->codec, JACK_MIC_CTRL, (0x1 << JACKDETEN),
               (0x0 << JACKDETEN));
   snd_sunxi_unregister_jack(ctx);
}
 
static const struct of_device_id sunxi_machine_of_match[] = {
   { .compatible = "allwinner,sunxi-codec-machine", },
   {},
};
 
/*method relating*/
static struct platform_driver sunxi_machine_driver = {
   .driver = {
       .name = "sunxi-codec-machine",
       .owner = THIS_MODULE,
       .pm = &snd_soc_pm_ops,
       .of_match_table = sunxi_machine_of_match,
   },
   .probe = sunxi_machine_probe,
   .shutdown = sunxi_machine_shutdown,
};
 
static int __init sunxi_machine_init(void)
{
   int err = 0;
 
   err = platform_driver_register(&sunxi_machine_driver);
   if (err < 0)
       return err;
 
   return 0;
}
module_init(sunxi_machine_init);
 
static void __exit sunxi_machine_exit(void)
{
   platform_driver_unregister(&sunxi_machine_driver);
}
module_exit(sunxi_machine_exit);
 
module_param_named(switch_state, switch_state, int, S_IRUGO | S_IWUSR);
 
MODULE_AUTHOR("huangxin,liushaohua");
MODULE_DESCRIPTION("SUNXI_sndpcm ALSA SoC audio driver");
MODULE_LICENSE("GPL");