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
|
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <drivers/audio.h>
#ifdef RT_USING_FINSH
#include <finsh.h>
#endif
#include "board.h"
#include "dma.h"
#ifdef RT_USING_ICODEC
#include "drv_gpio.h"
#include "drv_clock.h"
#include "drv_aic.h"
#include "drv_aic_i2s.h"
#include "drv_codec_icodec.h"
#define CODEC_DEBUG 0
#if CODEC_DEBUG
#define CODEC_DBG(...) rt_kprintf("[CODEC]"),rt_kprintf(__VA_ARGS__)
#else
#define CODEC_DBG(...)
#endif
/*
* Sampling rate
*/
const int sample_attr[] =
{
8000, 11025, 12000, 16000,
22050, 24000, 32000, 44100,
48000, 88200, 96000, 176400,
192000,
};
static uint8_t _g_icodec_reg_defcache[SCODA_MAX_REG_NUM] =
{
#if 1
/* reg 0x0 ... 0x9 */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x03,
/* reg 0xa ... 0x13 */
0x00,0x40,0x30,0x80,0x01,0x00,0x00,0x00,0x0f,0x40,
/* reg 0x14 ... 0x1d */
0x07,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x00,0xff,
/* reg 0x1e ... 0x27 */
0x00,0x06,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* reg 0x28 ... 0x31 */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* reg 0x32 ... 0x39 */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* extern reg */
0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x34,0x07,0x44,0x1f,0x00,
#else
/* reg 0x0 ... 0x9 */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x03,
/* reg 0xa ... 0x13 */
0x00,0x00,0x30,0xb0,0x01,0x00,0x00,0x00,0x0F,0x40,
/* reg 0x14 ... 0x1d */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff,0x00,0xff,
/* reg 0x1e ... 0x27 */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* reg 0x28 ... 0x31 */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* reg 0x32 ... 0x39 */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
/* extern reg */
0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,
0x34,0x07,0x44,0x1f,0x00,
#endif
};
static int jz_icodec_reg_volatile(uint32_t reg)
{
if (reg > SCODA_MAX_REG_NUM)
return 1;
switch (reg)
{
case SCODA_REG_SR:
case SCODA_REG_SR2:
case SCODA_REG_SIGR:
case SCODA_REG_SIGR3:
case SCODA_REG_SIGR5:
case SCODA_REG_SIGR7:
case SCODA_REG_MR:
case SCODA_REG_IFR:
case SCODA_REG_IFR2:
case SCODA_REG_SR_ADC_AGCDGL:
case SCODA_REG_SR_ADC_AGCDGR:
case SCODA_REG_SR_ADC_AGCAGL:
case SCODA_REG_SR_ADC_AGCAGR:
case SCODA_REG_SR_TR1:
case SCODA_REG_SR_TR2:
case SCODA_REG_SR_TR_SRCDAC:
return 1;
default:
return 0;
}
}
static int jz_icodec_reg_writable(uint32_t reg)
{
if (reg > SCODA_MAX_REG_NUM)
return 0;
switch (reg) {
case SCODA_REG_SR:
case SCODA_REG_SR2:
case SCODA_REG_SIGR:
case SCODA_REG_SIGR3:
case SCODA_REG_SIGR5:
case SCODA_REG_SIGR7:
case SCODA_REG_MR:
case SCODA_REG_SR_ADC_AGCDGL:
case SCODA_REG_SR_ADC_AGCDGR:
case SCODA_REG_SR_ADC_AGCAGL:
case SCODA_REG_SR_ADC_AGCAGR:
case SCODA_REG_SR_TR1:
case SCODA_REG_SR_TR2:
case SCODA_REG_SR_TR_SRCDAC:
return 0;
default:
return 1;
}
}
static int jz_icodec_reg_readable(uint32_t reg)
{
if (reg > SCODA_MAX_REG_NUM)
return 0;
else
return 1;
}
static uint8_t jz_icodec_reg_read(struct jz_icodec *icodec, uint32_t reg)
{
int ret = 0;
uint8_t val = 0;
if (!jz_icodec_reg_volatile(reg))
{
val = icodec_hw_read(icodec, reg);
if ((reg == SCODA_REG_GCR_DACL) || (reg == SCODA_REG_GCR_DACR)) {
if (val < 32)
val = 31 - val;
else
val = 95 - val;
}
return val;
}
if (jz_icodec_reg_readable(reg))
return icodec_hw_read(icodec, reg);
return 0;
}
static int jz_icodec_reg_write(struct jz_icodec *codec, uint16_t reg, int value)
{
int ret = 0;
int val = value;
if (jz_icodec_reg_writable(reg))
{
if (!jz_icodec_reg_volatile(reg))
{
if((reg == SCODA_REG_GCR_DACL)||(reg == SCODA_REG_GCR_DACR))
{
if(val < 32)
val = 31 - val;
else
val = 95 - val;
}
_g_icodec_reg_defcache[reg] = val;
}
return icodec_hw_write(codec, reg, val);
}
return 0;
}
static int jz_icodec_reg_update_bits(struct jz_icodec *icodec, uint16_t reg, uint32_t mask, uint16_t value)
{
uint8_t change;
uint8_t old, new;
int ret;
ret = jz_icodec_reg_read(icodec, reg);
if (ret < 0)
return ret;
old = ret;
new = (old & ~mask) | (value & mask);
change = old != new;
if (change)
ret = jz_icodec_reg_write(icodec, reg, new);
if (ret < 0)
return ret;
return change;
}
static int jz_icodec_set_sampling_rate(struct jz_icodec *icodec, int rate)
{
/* sampling rate */
int speed_sel = 0;
if(rate == icodec->replay_config.samplerate)
return rate;
/* set sampling rate */
for (speed_sel = 0; rate > sample_attr[speed_sel]; speed_sel++) ;
jz_icodec_reg_update_bits(icodec, SCODA_REG_FCR_DAC, SCODA_FCR_FREQ_MASK, (speed_sel << SCODA_FCR_FREQ_SHIFT));
jz_icodec_reg_update_bits(icodec, SCODA_REG_FCR_ADC, SCODA_FCR_FREQ_MASK, (speed_sel << SCODA_FCR_FREQ_SHIFT));
rate = sample_attr[speed_sel];
icodec->replay_config.samplerate = rate;
return rate;
}
static void jz_icodec_hw_params(struct jz_icodec* icodec,int stream)
{
int playback = (stream == AUDIO_STREAM_REPLAY);
int speed_sel = 0;
int bit_width_sel = 3;
int aicr_reg = playback ? SCODA_REG_AICR_DAC : SCODA_REG_AICR_ADC;
int fcr_reg = playback ? SCODA_REG_FCR_DAC : SCODA_REG_FCR_ADC;
/* bit width */
switch (icodec->replay_config.samplefmt)
{
case AUDIO_FMT_PCM_S16_LE:
bit_width_sel = 0;
break;
case AUDIO_FMT_PCM_S24_LE:
bit_width_sel = 3;
break;
}
/*sample rate*/
for (speed_sel = 0; icodec->replay_config.samplerate > sample_attr[speed_sel]; speed_sel++);
jz_icodec_reg_update_bits(icodec, aicr_reg, SCODA_AICR_DAC_ADWL_MASK,(bit_width_sel << SCODA_AICR_DAC_ADWL_SHIFT));
jz_icodec_reg_update_bits(icodec, fcr_reg, SCODA_FCR_FREQ_MASK,(speed_sel << SCODA_FCR_FREQ_SHIFT));
}
static int jz_icodec_digital_mute(struct jz_icodec *icodec, int mute)
{
jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_DAC, SCODA_CR_DAC_SMUTE_MASK, (!!mute) << SCODA_CR_DAC_SMUTE_SHIFT);
jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_ADC, SCODA_CR_ADC_SMUTE_MASK, (!!mute) << SCODA_CR_ADC_SMUTE_SHIFT);
return 0;
}
static void jz_icodec_startup(struct jz_icodec *icodec)
{
/*power on codec*/
if (jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_VIC, SCODA_CR_VIC_SB_MASK, 0))
rt_thread_delay(rt_tick_from_millisecond(250));
if (jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_VIC, SCODA_CR_VIC_SB_SLEEP_MASK, 0))
rt_thread_delay(rt_tick_from_millisecond(400));
}
static void jz_icodec_shutdown(struct jz_icodec *icodec)
{
/*power off codec*/
jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_VIC, SCODA_CR_VIC_SB_SLEEP_MASK, 1);
jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_VIC, SCODA_CR_VIC_SB_MASK, 1);
}
static void jz_icodec_mute_stream(struct jz_icodec *icodec, int mute, int stream)
{
if(stream == AUDIO_STREAM_REPLAY)
{
jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_DAC, SCODA_CR_DAC_SMUTE_MASK, (!!mute) << SCODA_CR_DAC_SMUTE_SHIFT);
}
else if(stream == AUDIO_STREAM_RECORD)
{
jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_ADC, SCODA_CR_ADC_SMUTE_MASK, (!!mute) << SCODA_CR_ADC_SMUTE_SHIFT);
}
}
#define VOLUME_MIN 0
#define VOLUME_MAX 100
#define REPLAY_REG_MAX (63)
static int jz_icodec_set_replay_volume(struct jz_icodec *icodec,int val)
{
int phyValue = 0;
/* get current volume */
if (val < VOLUME_MIN)
val = VOLUME_MIN;
else if(val >= VOLUME_MAX)
val = VOLUME_MAX;
phyValue = (val* REPLAY_REG_MAX) / VOLUME_MAX;
CODEC_DBG("volume = %d\n",val);
jz_icodec_reg_write(icodec,SCODA_REG_GCR_DACL,phyValue);
jz_icodec_reg_write(icodec,SCODA_REG_GCR_DACR,phyValue);
icodec->user_replay_volume = jz_icodec_reg_read(icodec,SCODA_REG_GCR_DACL);
if (val == 0)
{
jz_icodec_digital_mute(icodec,1);
}
else
{
jz_icodec_digital_mute(icodec,0);
}
return val;
}
#define REPLAY_MIXER_REG_MAX 31
int jz_icodec_set_replay_mixer_volume(struct jz_icodec *icodec,int val)
{
int phyValue = 0;
/* get current volume */
if (val < VOLUME_MIN)
val = VOLUME_MIN;
else if(val >= VOLUME_MAX)
val = VOLUME_MAX;
phyValue = (val * REPLAY_MIXER_REG_MAX) / VOLUME_MAX;
jz_icodec_reg_write(icodec,SCODA_REG_GCR_MIXDACL,phyValue);
jz_icodec_reg_write(icodec,SCODA_REG_GCR_MIXDACR,phyValue);
return val;
}
#define DIGITAL_CAP_REG_MAX 43
int jz_icodec_set_digital_capture_volume(struct jz_icodec *icodec,int val)
{
int phyValue = 0;
/* get current volume */
if (val < VOLUME_MIN)
val = VOLUME_MIN;
else if(val >= VOLUME_MAX)
val = VOLUME_MAX;
phyValue = (val * DIGITAL_CAP_REG_MAX) / VOLUME_MAX;
jz_icodec_reg_write(icodec,SCODA_REG_GCR_ADCL,phyValue);
jz_icodec_reg_write(icodec,SCODA_REG_GCR_ADCR,phyValue);
return val;
}
#define DIGITAL_CAP_MIX_REG_MAX 31
int jz_icodec_set_digital_capture_mixer_volume(struct jz_icodec *icodec,int val)
{
int phyValue = 0;
/* get current volume */
if (val < VOLUME_MIN)
val = VOLUME_MIN;
else if(val >= VOLUME_MAX)
val = VOLUME_MAX;
phyValue = (val * DIGITAL_CAP_MIX_REG_MAX) / VOLUME_MAX;
jz_icodec_reg_write(icodec,SCODA_REG_GCR_MIXADCL,phyValue);
jz_icodec_reg_write(icodec,SCODA_REG_GCR_MIXADCR,phyValue);
return val;
}
#define MIC_REG_MAX 4
int aic_icodec_set_mic_volume(struct jz_icodec *icodec,int val)
{
int phyValue = 0;
/* get current volume */
if (val < VOLUME_MIN)
val = VOLUME_MIN;
else if(val >= VOLUME_MAX)
val = VOLUME_MAX;
phyValue = MIC_REG_MAX - (val) * MIC_REG_MAX / VOLUME_MAX;
jz_icodec_reg_write(icodec,SCODA_REG_GCR_MIC1,phyValue);
return val;
}
enum
{
AMIC_ON = 0,
DMIC_ON = 1,
};
void jz_icodec_adc_mic_select(struct jz_icodec *icodec, int dmic)
{
if(dmic == DMIC_ON)
{
jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_ADC, SCODA_CR_ADC_MIC_SEL_MASK, (1 << SCODA_CR_ADC_MIC_SEL_SHIFT));
}
else
{
jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_ADC, SCODA_CR_ADC_MIC_SEL_MASK, (0 << SCODA_CR_ADC_MIC_SEL_SHIFT));
}
}
void jz_icodec_adc_capture_enable(struct jz_icodec *icodec,int enable)
{
if(enable)
{
jz_icodec_reg_update_bits(icodec, SCODA_REG_AICR_ADC, SCODA_AICR_ADC_SB_MASK, (0 << SCODA_AICR_ADC_SB_SHIFT));
}
else
{
jz_icodec_reg_update_bits(icodec, SCODA_REG_AICR_ADC, SCODA_AICR_ADC_SB_MASK, (1 << SCODA_AICR_ADC_SB_SHIFT));
}
}
/*********************************************************************************************************
** Audio device
*********************************************************************************************************/
static rt_err_t icodec_getcaps (struct rt_audio_device *audio,struct rt_audio_caps *caps)
{
rt_err_t result = RT_EOK;
struct jz_icodec *icodec = (struct jz_icodec *)audio->parent.user_data;
CODEC_DBG("type = %d\n",caps->main_type);
switch (caps->main_type)
{
case AUDIO_TYPE_QUERY: /* qurey the types of hw_codec device */
{
switch (caps->sub_type)
{
case AUDIO_TYPE_QUERY:
caps->udata.mask = AUDIO_TYPE_OUTPUT | AUDIO_TYPE_MIXER;
break;
default:
result = -RT_ERROR;
break;
}
break;
}
case AUDIO_TYPE_OUTPUT: /* Provide capabilities of OUTPUT unit */
switch (caps->sub_type)
{
case AUDIO_DSP_PARAM:
if (audio->replay == NULL)
{
result = -RT_ERROR;
break;
}
caps->udata.config.channels = icodec->replay_config.channels;
caps->udata.config.samplefmt = icodec->replay_config.samplefmt;
caps->udata.config.samplerate = icodec->replay_config.samplerate;
caps->udata.config.samplefmts = icodec->replay_config.samplefmts;
break;
default:
result = -RT_ERROR;
break;
}
break;
case AUDIO_TYPE_MIXER: /* report the Mixer Units */
switch (caps->sub_type)
{
case AUDIO_MIXER_QUERY:
caps->udata.mask = AUDIO_MIXER_VOLUME | AUDIO_MIXER_DIGITAL | AUDIO_MIXER_LINE;
break;
case AUDIO_MIXER_VOLUME:
caps->udata.value = icodec->user_replay_volume;
break;
case AUDIO_MIXER_DIGITAL:
break;
case AUDIO_MIXER_LINE:
break;
default:
result = -RT_ERROR;
break;
}
break;
default:
result = -RT_ERROR;
break;
}
return result;
}
static rt_err_t icodec_configure (struct rt_audio_device *audio,struct rt_audio_caps *caps)
{
rt_err_t result = RT_EOK;
struct jz_icodec *icodec = (struct jz_icodec *) audio->parent.user_data;
CODEC_DBG("type = %d\n",caps->main_type);
switch (caps->main_type)
{
case AUDIO_TYPE_MIXER:
switch (caps->sub_type)
{
case AUDIO_MIXER_VOLUME:
{
int volume = caps->udata.value;
jz_icodec_set_replay_volume(icodec, volume);
}
break;
case AUDIO_MIXER_DIGITAL:
{
int gain = caps->udata.value;
jz_icodec_set_replay_mixer_volume(icodec, gain);
}
break;
case AUDIO_MIXER_LINE:
{
int gain = caps->udata.value;
//set linein valume...
}
break;
case AUDIO_MIXER_EXTEND:
break;
default:
result = -RT_ERROR;
break;
}
break;
case AUDIO_TYPE_OUTPUT:
{
switch (caps->sub_type)
{
case AUDIO_DSP_PARAM:
{
CODEC_DBG(" AUDIO_TYPE_OUTPUT:\n");CODEC_DBG(" Number of channels: %u\n", caps->udata.config.channels);CODEC_DBG(" Sample rate: %u\n", caps->udata.config.samplerate);CODEC_DBG(" Sample format: %u\n", caps->udata.config.samplefmt);
//upgrate codec chip
icodec->i2s->channels = caps->udata.config.channels;
icodec->i2s->rates = caps->udata.config.samplerate;
icodec->i2s->fmt_width = rt_audio_format_to_bits(caps->udata.config.samplefmt);
aic_i2s_hw_params(icodec->i2s, AUDIO_STREAM_REPLAY);
aic_i2s_set_sysclk(icodec->i2s, icodec->i2s->rates);
//save config
icodec->replay_config.channels = caps->udata.config.channels;
icodec->replay_config.samplefmt = caps->udata.config.samplefmt;
icodec->replay_config.samplerate = caps->udata.config.samplerate;
icodec->replay_config.samplefmts = caps->udata.config.samplefmts;
break;
}
case AUDIO_DSP_SAMPLERATE:
{
int rate = caps->udata.value;
jz_icodec_set_sampling_rate(icodec, rate);
break;
}
default:
result = -RT_ERROR;
break;
}
}
break;
default:
result = -RT_ERROR;
break;
}
return result;
}
static rt_err_t icodec_init (struct rt_audio_device *audio)
{
struct jz_icodec *icodec = (struct jz_icodec *)audio->parent.user_data;
uint16_t i;
/* disable shutdown */
gpio_set_value(AUDIO_SHUTDOWN_PORT,AUDIO_SHUTDOWN_PIN,AUDIO_SHUTDOWN_MUTE);
rt_thread_delay(RT_TICK_PER_SECOND / 4);
gpio_set_value(AUDIO_SHUTDOWN_PORT,AUDIO_SHUTDOWN_PIN,!AUDIO_SHUTDOWN_MUTE);
rt_thread_delay(RT_TICK_PER_SECOND / 4);
/* write default value ... */
for (i = 0; i < sizeof(_g_icodec_reg_defcache); ++i)
{
jz_icodec_reg_write(icodec, i, _g_icodec_reg_defcache[i]);
}
/* power off codec */
jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_VIC, SCODA_CR_VIC_SB_SLEEP_MASK, 1);
jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_VIC, SCODA_CR_VIC_SB_MASK, 1);
/* codec select enable 24M clock*/
jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_CK , SCODA_CR_CK_MCLK_DIV_MASK, 1 << SCODA_CR_CK_MCLK_DIV_SHIFT);
jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_CK , SCODA_CR_CK_SDCLK_MASK, 0 << SCODA_CR_CK_SDCLK_SHIFT);
jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_CK , SCODA_CR_CRYSTAL_MASK, 0 << SCODA_CR_CRYSTAL_SHIFT);
/*codec select Dac/Adc i2s interface*/
jz_icodec_reg_update_bits(icodec, SCODA_REG_AICR_DAC, SCODA_AICR_DAC_SLAVE_MASK, 0);
jz_icodec_reg_update_bits(icodec, SCODA_REG_AICR_DAC, SCODA_AICR_DAC_AUDIO_MASK, SCODA_AICR_DAC_AUDIOIF_I2S);
jz_icodec_reg_update_bits(icodec, SCODA_REG_AICR_ADC, SCODA_AICR_ADC_AUDIO_MASK, 0);
jz_icodec_reg_update_bits(icodec, SCODA_REG_AICR_ADC, SCODA_AICR_ADC_AUDIO_MASK, SCODA_AICR_ADC_AUDIOIF_I2S);
/*codec generated IRQ is a high level */
jz_icodec_reg_update_bits(icodec, SCODA_REG_ICR, SCODA_ICR_INT_FORM_MASK, SCODA_ICR_INT_FORM_LOW);
/*codec irq mask*/
jz_icodec_reg_write(icodec, SCODA_REG_IMR, SCODA_IMR_COMMON_MASK);
jz_icodec_reg_write(icodec, SCODA_REG_IMR2, SCODA_IMR2_COMMON_MASK);
/*codec clear all irq*/
jz_icodec_reg_write(icodec, SCODA_REG_IFR, SCODA_IMR_COMMON_MASK);
jz_icodec_reg_write(icodec, SCODA_REG_IFR2, SCODA_IMR2_COMMON_MASK);
/* PCM Format */
#if (ICODEC_PCM_FORMAT == AUDIO_FMT_PCM_S16_LE)
jz_icodec_reg_update_bits(icodec, SCODA_REG_AICR_DAC, SCODA_AICR_DAC_ADWL_MASK, (0 << SCODA_AICR_DAC_ADWL_SHIFT));
jz_icodec_reg_update_bits(icodec, SCODA_REG_AICR_ADC, SCODA_AICR_ADC_ADWL_MASK, (0 << SCODA_AICR_ADC_ADWL_SHIFT));
#endif
/* sampling rate */
jz_icodec_set_sampling_rate(icodec,ICODEC_SAMPLING_RATE);
/*power on codec*/
jz_icodec_digital_mute(icodec,0);
if (jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_VIC, SCODA_CR_VIC_SB_MASK, 0))
rt_thread_delay(rt_tick_from_millisecond(250));
if (jz_icodec_reg_update_bits(icodec, SCODA_REG_CR_VIC, SCODA_CR_VIC_SB_SLEEP_MASK, 0))
rt_thread_delay(rt_tick_from_millisecond(400));
return RT_EOK;
}
static rt_err_t icodec_shutdown (struct rt_audio_device *audio)
{
struct jz_icodec *icodec = (struct jz_icodec *)audio->parent.user_data;
#ifdef AUDIO_SHUTDOWN_PORT
gpio_set_value(AUDIO_SHUTDOWN_PORT,AUDIO_SHUTDOWN_PIN,AUDIO_SHUTDOWN_MUTE);
#endif
return RT_EOK;
}
rt_err_t icodec_start (struct rt_audio_device *audio,int stream)
{
struct jz_icodec *icodec = (struct jz_icodec *)audio->parent.user_data;
aic_i2s_trigger(icodec->i2s,I2S_TRIGGER_START,stream);
return RT_EOK;
}
rt_err_t icodec_stop (struct rt_audio_device *audio,int stream)
{
struct jz_icodec *icodec = (struct jz_icodec *)audio->parent.user_data;
aic_i2s_trigger(icodec->i2s,I2S_TRIGGER_STOP,stream);
return RT_EOK;
}
static rt_err_t icodec_suspend (struct rt_audio_device *audio,int stream)
{
struct jz_icodec *icodec = (struct jz_icodec *)audio->parent.user_data;
aic_i2s_trigger(icodec->i2s,I2S_TRIGGER_SUSPEND,stream);
return RT_EOK;
}
static rt_err_t icodec_resume (struct rt_audio_device *audio,int stream)
{
struct jz_icodec *icodec = (struct jz_icodec *)audio->parent.user_data;
aic_i2s_trigger(icodec->i2s,I2S_TRIGGER_RESUME,stream);
return RT_EOK;
}
static rt_err_t icodec_control (struct rt_audio_device *audio, int cmd, void *args)
{
rt_err_t result = RT_EOK;
struct jz_icodec *icodec = (struct jz_icodec *)audio->parent.user_data;
switch (cmd)
{
case AUDIO_CTL_HWRESET:
break;
case AUDIO_CTL_GETBUFFERINFO:
{
struct rt_audio_buf_info *info = (struct rt_audio_buf_info *)args;
if(info != RT_NULL)
{
info->buffer_count = CFG_I2S_DMA_PAGE_NUM;
info->buffer_size = CFG_I2S_DMA_PAGE_SIZE;
}
}
break;
default:
result = -RT_ERROR;
break;
}
return result;
}
static void codec_write_complete(void *data, void *pbuf)
{
struct rt_audio_device *audio = (struct rt_audio_device *)data;
/* notify transmitted complete. */
rt_audio_tx_complete(audio,pbuf);
}
static rt_size_t icodec_transmit (struct rt_audio_device *audio,const void *writeBuf,void *readBuf, rt_size_t size)
{
struct jz_icodec *icodec = (struct jz_icodec *)audio->parent.user_data;
CODEC_DBG("writeBuf = %x,readBuf=%x,size=%d\n",(rt_uint32_t)writeBuf,(rt_uint32_t)readBuf,size);
if(writeBuf != RT_NULL)
{
return aic_i2s_send(icodec->i2s, writeBuf, size, codec_write_complete, (void *)audio);
}
return 0;
}
static struct jz_icodec _g_jz_icodec =
{
.mapped_base = AIC_BASE + 0xA0,
.user_replay_volume = 31,
};
static struct rt_audio_device _g_audio_device;
const struct rt_audio_ops _g_audio_ops =
{
.getcaps = icodec_getcaps,
.configure = icodec_configure,
.init = icodec_init,
.shutdown = icodec_shutdown,
.start = icodec_start,
.stop = icodec_stop,
.suspend = icodec_suspend ,
.resume = icodec_resume ,
.control = icodec_control,
.transmit = icodec_transmit,
};
int rt_hw_codec_init(void)
{
int result;
struct rt_audio_device *audio = &_g_audio_device;
struct jz_icodec *icodec = &_g_jz_icodec;
struct jz_i2s *i2s;
rt_kprintf("init i2s....\n");
i2s = rt_hw_aic_i2s_init();
if(i2s == RT_NULL)
{
CODEC_DBG("i2s device not found!\r\n");
return -RT_EIO;
}
icodec->i2s = i2s;
#ifdef AUDIO_DEVICE_USE_PRIVATE_BUFFER
{
rt_uint8_t *mempool = (rt_uint8_t *)rt_malloc(CODEC_MP_SZ);
if(mempool == RT_NULL)
{
CODEC_DBG("no memory...\n");
return -RT_ENOMEM;
}
rt_mp_init(&icodec->mp,"codecbuf",mempool,CODEC_MP_SZ,CODEC_MP_BLOCK_SZ);
}
#endif /* AUDIO_DEVICE_USE_PRIVATE_BUFFER */
//init default configuration
{
icodec->replay_config.channels = 2;
icodec->replay_config.samplefmt = AUDIO_FMT_PCM_S16_LE;
icodec->replay_config.samplerate = 44100;
icodec->replay_config.samplefmts = AUDIO_FMT_PCM_S16_LE;
}
audio->ops = (struct rt_audio_ops *)&_g_audio_ops;
result = rt_audio_register(audio,"sound0",RT_DEVICE_FLAG_WRONLY,icodec);
if(result != RT_EOK)
{
CODEC_DBG("icodec device register error..\n");
return result;
}
rt_kprintf("codec initialization done!\n");
return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_codec_init);
#endif /* RT_USING_ICODEC */
|