Change root folder name, create pasdk subfolder
[processor-sdk/performance-audio-sr.git] / processor_audio_sdk_1_00_00_00 / pasdk / test_dsp / sap / audio_dc_cfg.c
1 /*
2  * Copyright (c) 2015, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
34 /**
35  * \file      audio_dc_config.c
36  *
37  * \brief     Configures Audio daughter card HW modules
38  *
39  */
41 #include "audio_dc_cfg.h"
42 #include "vproccmds_a.h"
43 #include <stdarg.h>
44 #include "string.h"
46 Platform_STATUS audioHDMIConfig(void);
48 #define HSR4_I2C_ADDR 0x5D
49 #define HSR4_I2C_PORT_NUM I2C_PORT_1
51 /* ------------------------------------------------------------------------ *
52  *  Prototypes                                                              *
53  * ------------------------------------------------------------------------ */
55 /**
56  *  \brief    Initializes ADC module
57  *
58  *  This function initializes and configures the ADC modules
59  *  on audio daughter card
60  *
61  *  \param     devId  [IN]  ADC Device Id
62  *  \param     config [IN]  ADC configuration parameters
63  *
64  *  \return    Platform_EOK on Success or error code
65  */
66 Platform_STATUS audioAdcConfig(AdcDevId  devId, AdcConfig *config)
67 {
68         Platform_STATUS status;
70         if(config == NULL)
71         {
72                 IFPRINT(platform_write("Invalid Inputs\n"));
73                 return (Platform_EINVALID);
74         }
76         /* Initialize all the HW instances of ADC */
77         status = platformAudioAdcInit(devId);
78         if(status != Platform_EOK)
79         {
80                 IFPRINT(platform_write("audioAdcConfig : platformaudioAdcConfig Failed\n"));
81                 return (status);
82         }
84         /* Set ADC channel gain */
85         status = platformAudioAdcSetGain(devId, ADC_CH_ALL, config->gain);
86         if(status != Platform_EOK)
87         {
88                 IFPRINT(platform_write("audioAdcConfig : platformAudioAdcSetGain Failed\n"));
89                 return (status);
90         }
92         /* Configure Left input mux for ADC1L */
93         status = platformAudioAdcSetLeftInputMux(devId, ADC_CH1_LEFT,
94                                                  config->adc1LMux);
95         if(status != Platform_EOK)
96         {
97                 IFPRINT(platform_write("audioAdcConfig : platformAudioAdcSetLeftInputMux Failed\n"));
98                 return (status);
99         }
101         /* Configure Left input mux for ADC2L*/
102         status = platformAudioAdcSetLeftInputMux(devId, ADC_CH2_LEFT,
103                                                  config->adc2LMux);
104         if(status != Platform_EOK)
105         {
106                 IFPRINT(platform_write("audioAdcConfig : platformAudioAdcSetLeftInputMux Failed\n"));
107                 return (status);
108         }
110         /* Configure Right input mux for ADC1R */
111         status = platformAudioAdcSetRightInputMux(devId, ADC_CH1_RIGHT,
112                                                   config->adc1RMux);
113         if(status != Platform_EOK)
114         {
115                 IFPRINT(platform_write("audioAdcConfig : platformAudioAdcSetRightInputMux Failed\n"));
116                 return (status);
117         }
119         /* Configure Right input mux for ADC2R */
120         status = platformAudioAdcSetRightInputMux(devId, ADC_CH2_RIGHT,
121                                                   config->adc2RMux);
122         if(status != Platform_EOK)
123         {
124                 IFPRINT(platform_write("audioAdcConfig : platformAudioAdcSetRightInputMux Failed\n"));
125                 return (status);
126         }
128         /* Configure audio data format */
129         status = platformAudioAdcDataConfig(devId, config->wlen, config->format);
130         if(status != Platform_EOK)
131         {
132                 IFPRINT(platform_write("audioAdcConfig : platformAudioAdcDataConfig Failed\n"));
133                 return (status);
134         }
136         /* Configure all the interrupts */
137         status = platformAudioAdcConfigIntr(devId, ADC_INTR_ALL,
138                                             config->intEnable);
139         if(status != Platform_EOK)
140         {
141                 IFPRINT(platform_write("audioAdcConfig : platformAudioAdcConfigIntr Failed\n"));
142                 return (status);
143         }
145         return (status);
148 /**
149  *  \brief    Initializes DAC module
150  *
151  *  This function initializes and configures the DAC modules
152  *  on audio daughter card
153  *
154  *  \param     devId  [IN]  DAC Device Id
155  *  \param     config [IN]  DAC configuration parameters
156  *
157  *  \return    Platform_EOK on Success or error code
158  */
159 Platform_STATUS audioDacConfig(DacDevId devId, DacConfig *config)
161         Platform_STATUS status;
163         if(config == NULL)
164         {
165                 IFPRINT(platform_write("Invalid Inputs\n"));
166                 return (Platform_EINVALID);
167         }
169         /* Initialize Audio DAC */
170         status = platformAudioDacInit(devId);
171         if(status != Platform_EOK)
172         {
173                 IFPRINT(platform_write("audioDacConfig : platformaudioDacConfig Failed\n"));
174                 return (status);
175         }
177         /* Configure AMUTE control event */
178         status = platformAudioDacAmuteCtrl(devId, config->amuteCtrl,
179                                            config->amuteEnable);
180         if(status != Platform_EOK)
181         {
182                 IFPRINT(platform_write("audioDacConfig : platformAudioDacAmuteCtrl Failed\n"));
183                 return (status);
184         }
186         /* Set sampling mode */
187         status = platformAudioDacSetSamplingMode(devId, config->samplingMode);
188         if(status != Platform_EOK)
189         {
190                 IFPRINT(platform_write("audioDacConfig : platformAudioDacSetSamplingMode Failed\n"));
191                 return (status);
192         }
194         /* Set data format */
195         status = platformAudioDacSetDataFormat(devId, config->dataFormat);
196         if(status != Platform_EOK)
197         {
198                 IFPRINT(platform_write("audioDacConfig : platformAudioDacSetDataFormat Failed\n"));
199                 return (status);
200         }
202         /* Enable soft mute control */
203         status = platformAudioDacSoftMuteCtrl(devId, DAC_CHAN_ALL,
204                                               config->softMuteEnable);
205         if(status != Platform_EOK)
206         {
207                 IFPRINT(platform_write("audioDacConfig : platformAudioDacSoftMuteCtrl Failed\n"));
208                 return (status);
209         }
211         /* Set attenuation mode */
212         status = platformAudioDacSetAttnMode(devId, config->attnMode);
213         if(status != Platform_EOK)
214         {
215                 IFPRINT(platform_write("audioDacConfig : platformAudioDacSetAttnMode Failed\n"));
216                 return (status);
217         }
219         /* Set De-emphasis control */
220         status = platformAudioDacDeempCtrl(devId, config->deempCtrl);
221         if(status != Platform_EOK)
222         {
223                 IFPRINT(platform_write("audioDacConfig : platformAudioDacDeempCtrl Failed\n"));
224                 return (status);
225         }
227         /* Set DAC volume */
228         status = platformAudioDacSetVolume(devId, DAC_CHAN_ALL, config->volume);
229         if(status != Platform_EOK)
230         {
231                 IFPRINT(platform_write("audioDacConfig : platformAudioDacSetVolume Failed\n"));
232                 return (status);
233         }
235         return (status);
238 /**
239  *  \brief    Initializes DIR module
240  *
241  *  This function initializes and configures the DIR modules
242  *  on audio daughter card
243  *
244  *  \return    Platform_EOK on Success or error code
245  */
246 Platform_STATUS audioDirConfig(void)
248         Platform_STATUS status;
249         int8_t          fsout;
250 #ifdef CHECK_ERROR_STATUS
251         uint32_t        timeout;
252 #endif
254         status = platformAudioDirInit();
255         if(status != Platform_EOK)
256         {
257                 IFPRINT(platform_write("audioDirConfig : Audio DIR Configuration Failed!\n"));
258                 return (status);
259         }
261 #ifdef CHECK_ERROR_STATUS
262         /* DIR should be in PLL mode.
263            Wait for ERROR signal to be low as DIR is configured for
264            AUTO mode */
265         timeout = ERROR_STATUS_WAIT_TIMEOUT;
266         while (timeout)
267         {
268                 if(!platformAudioDirGetErrStatus())
269                 {
270                         IFPRINT(platform_write("audioDirConfig : DIR in PLL Mode\n"));
271                         break;
272                 }
274                 IFPRINT(platform_write("audioDirConfig : Waiting for DIR to Enter PLL Mode...\n"));
275                 platform_delay(1000);
276                 timeout--;
277         }
279         if(!timeout)
280         {
281                 IFPRINT(platform_write("audioDirConfig : DIR is not in PLL Mode!!\n"));
282                 return (Platform_EFAIL);
283         }
284 #endif
286         fsout = platformAudioDirGetFsOut();
287         if(fsout == 2)
288         {
289                 IFPRINT(platform_write("audioDirConfig : Out of Range Sampling Frequency\n"));
290         }
291         else if(fsout == 0)
292         {
293                 IFPRINT(platform_write("audioDirConfig : Calculated Sampling Frequency Output is 43 kHz\9645.2 kHz\n"));
294         }
295         else if(fsout == 1)
296         {
297                 IFPRINT(platform_write("audioDirConfig : Calculated Sampling Frequency Output is 46.8 kHz\9649.2 kHz\n"));
298         }
299         else if(fsout == 3)
300         {
301                 IFPRINT(platform_write("audioDirConfig : Calculated Sampling Frequency Output is 31.2 kHz\9632.8 kHz\n"));
302         }
303         else
304         {
305                 IFPRINT(platform_write("audioDirConfig : Error in Reading FSOUT status \n"));
306                 status = Platform_EFAIL;
307         }
309         return (status);
312 int alpha_i2c_write(unsigned short, ...);
313 void set_audio_desc(unsigned char ,unsigned char ,unsigned char ,unsigned char ,unsigned char );
314 void hrptredid();
315 void  hdmi128();
318 int gret_val=0;
319 int alpha_i2c_write(unsigned short var1, ...)
321         unsigned short alpha_type,length,temp_var;
322         int i,offset,ret_val;
323         unsigned char cmd[50];
324         char *s;
325         va_list argp;
326         va_start(argp, var1);
328         alpha_type = var1>> 8;
329         switch(alpha_type)
330         {
331                 case 0xca:
332                 case 0xc2:
333                 case 0xc3:
334                 case 0xc4:
335                         length = 4;
336                         break;
337                 case 0xcb:
338                         length = 6;
339                         break;
340                 case 0xcc:
341                         length = 8;
342                         break;
343                 case 0xcd:
344                 case 0xc5:
345                         length= 8; // temporary - data starts after 8 bytes
346                         break;
347         }
349         cmd[0]=length;
350         temp_var=var1;
351         for(i=0;i<length-2;i+=2) // convert to bytes as per protocol
352         {
353                 cmd[i+1]= temp_var & 0xff;
354                 cmd[i+2]= temp_var >> 8;
355                 temp_var=va_arg(argp, short);
356         }
357         cmd[i+1]= temp_var & 0xff;
358         cmd[i+2]= temp_var >> 8;
361         if(alpha_type == 0xcd) // special processing for variable length
362         {
363                 offset=9;
364                 s = va_arg(argp, char *); // remaining data is in form of string
365                 length = temp_var; // last short indicates data length
366                 cmd[0]+=length;
367                 for(i=offset;i<offset+length;i++)
368                         cmd[i]=s[i-offset];
369         }
370 va_end(argp);
373 ret_val = i2cWrite(HSR4_I2C_PORT_NUM, HSR4_I2C_ADDR, cmd, cmd[0]+1, I2C_RELEASE_BUS);
374 if(ret_val !=I2C_RET_OK)
375                 gret_val++;
378 return ret_val;
381 void set_audio_desc(unsigned char var1,unsigned char var2,unsigned char var3,unsigned char var4,unsigned char var5)
383         int ret_val=I2C_RET_OK;
384         do{ret_val=alpha_i2c_write(HSDIO_EDID_AUDIO_DESC_FORMAT(var1, var2));}while (ret_val !=I2C_RET_OK);
385         platform_delay(10000);
386         do{ret_val=alpha_i2c_write(HSDIO_EDID_AUDIO_DESC_NUM_CHANNELS(var1, var3));}while (ret_val !=I2C_RET_OK);
387         platform_delay(10000);
388         do{ret_val=alpha_i2c_write(HSDIO_EDID_AUDIO_DESC_SAMPLE_RATES(var1, var4));}while (ret_val !=I2C_RET_OK);
389         platform_delay(10000);
390         do{ret_val=alpha_i2c_write(HSDIO_EDID_AUDIO_DESC_MISC(var1, var5));}while (ret_val !=I2C_RET_OK);
391         platform_delay(1000);
392         do{ret_val=alpha_i2c_write(HSDIO_ALERT(HSDIO_ALERT_INPUT_AUDIO_CHANGE_msk));}while (ret_val !=I2C_RET_OK);
393         platform_delay(1000);
394         do{ret_val=alpha_i2c_write(HSDIO_ALERT(HSDIO_ALERT_INPUT_AUDIO_MUTE_msk));}while (ret_val !=I2C_RET_OK);
395         platform_delay(1000);
398 void hrptredid()
400         int ret_val=I2C_RET_OK;
402         do{
403                 platform_delay(10000);
404                 ret_val=alpha_i2c_write(HSDIO_EDID_SPEAKER_ALLOCATION_BLOCK(0xFF));
405         }while (ret_val !=I2C_RET_OK);
407         platform_delay(10000);
408         do{ret_val=alpha_i2c_write(HSDIO_EDID_SPEAKER_ALLOCATION_BLOCK_2(0x7));}while (ret_val !=I2C_RET_OK);
410         set_audio_desc(0,1,2,0x7f,7);   // Stero PCM
411         set_audio_desc(1,1,8,0x7f,7);   // Multi Ch PCM
412         set_audio_desc(2,2,6,0x7,80);   //AC3
413         set_audio_desc(3,10,8,0x07,0);  // EAC3 //DTS (3,7,6,0x1e,192)
414         set_audio_desc(4,12,8,0x7F,0);  // MLP (THD) //DTS (4,7,8,0x6,192)
415         // Empty descriptors to be programmed to 0s
416         set_audio_desc(5,0,0,0,0);      //AAC LC (5,6,6,0x1f,192);
417         set_audio_desc(6,0,0,0,0);
418         set_audio_desc(7,0,0,0,0);
419         set_audio_desc(8,0,0,0,0);      // DTS-HD (8,11,8,0x7F,1)
420         set_audio_desc(9,0,0,0,0);
421         set_audio_desc(10,0,0,0,0);
422         set_audio_desc(11,0,0,0,0);
423         set_audio_desc(12,0,0,0,0);
424         set_audio_desc(13,0,0,0,0);
425         set_audio_desc(14,0,0,0,0);
426         set_audio_desc(15,0,0,0,0);
427         set_audio_desc(16,0,0,0,0);
428         set_audio_desc(17,0,0,0,0);
429         set_audio_desc(18,0,0,0,0);
430         set_audio_desc(19,0,0,0,0);
431         platform_delay(10000);
432         do{ret_val=alpha_i2c_write(HSDIO_EDID_GO);}while (ret_val !=0);
433         //fclose(fp1);
436 void  hdmi128()
438                 int ret_val=0;
439         platform_delay(10000);
440         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_MCLK_TO_HOST(HSDIO_AudioMClk_128X));}while (ret_val !=0);
441         platform_delay(10000);
442         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_FORMAT_TO_HOST(HSDIO_AudioFmt_I2S));}while (ret_val !=0);
443         platform_delay(10000);
444         do{ret_val=alpha_i2c_write(HSDIO_IMPLEMENT_AUDIO_TO_HOST_CMDS);}while (ret_val !=0);
445         platform_delay(10000);
446         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_OUTPUT_PRESENT(HSDIO_AudioPresent_HAS_NO_AUDIO));}while (ret_val !=0);
447         platform_delay(10000);
448         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_OUTPUT_FORMAT(HSDIO_AudioFmt_I2S));}while (ret_val !=0);
449         platform_delay(10000);
450         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_OUTPUT_SAMPLE_SIZE(HSDIO_AudioSampleSize_32));}while (ret_val !=0);
451         platform_delay(10000);
452         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_OUTPUT_FREQ(HSDIO_AudioFreq_48K));}while (ret_val !=0);
453         platform_delay(10000);
454         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_OUTPUT_LAYOUT(HSDIO_AudioLayout_2));}while (ret_val !=0);
455         platform_delay(10000);
456         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_OUTPUT_MCLK(HSDIO_AudioMClk_AUTO));}while (ret_val !=0);
457         platform_delay(10000);
458         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_OUTPUT_AUX_CHANNEL_CNT(1));}while (ret_val !=0);
459         platform_delay(10000);
460         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_OUTPUT_AUX_SPEAKER_MAPPING(0));}while (ret_val !=0);
461         platform_delay(10000);
462         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_OUTPUT_CHSTS_PCM_PREEMPHASIS(HSDIO_CSFormatInfo_PCM_NO_PRE));}while (ret_val !=0);
463         platform_delay(10000);
464         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_OUTPUT_CHSTS_COPYRIGHT(HSDIO_CSCopyright_PROTECTED));}while (ret_val !=0);
465         platform_delay(10000);
466         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_OUTPUT_CHSTS_WORD_LENGTH(HSDIO_CSWordLength_24));}while (ret_val !=0);
467         platform_delay(10000);
468         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_OUTPUT_GO);}while (ret_val !=0);
469         platform_delay(10000);
470         do{ret_val=alpha_i2c_write(HSDIO_AUDIO_ROUTING(HSDIO_AudioRouting_HSDIOIN_HOSTOUT));}while (ret_val !=0);
471         platform_delay(10000);
472         do{ret_val=alpha_i2c_write(HSDIO_SYS_CFG_GO);}while (ret_val !=0);
475 Platform_STATUS audioHDMIConfig(void)
477         Platform_STATUS status = 0;
479         hrptredid();
481         hdmi128();
483         return (status);
486 unsigned int read_hdmi_samprate()
488         unsigned char data[50];
489         Uint8 length;
490         int ret_val=7;
491         //int ret_val2=0;
492         int clear_to_read=5;
494         clear_to_read=alpha_i2c_write(HSDIO_ALERT_STS); //clear the interrupt on ~HMINT by reading the Alert Status register
496         ret_val=alpha_i2c_write(HSDIO_AUDIO_INPUT_FREQ_STS);
498         if(!ret_val) i2cRead(HSR4_I2C_PORT_NUM, HSR4_I2C_ADDR,&length,0,1,1);
499         if(!ret_val) i2cRead(HSR4_I2C_PORT_NUM, HSR4_I2C_ADDR,&data[0],0,1,length);
500         if(!ret_val) ret_val= data[3]; // indicates sample rate
501         else
502         ret_val = 0;
504         return ret_val;
507 /* Nothing past this point */