1 /*
2  * Copyright 2019 NXP
3  * All rights reserved.
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <stdint.h>
10 #include <assert.h>
11 #include "fsl_common.h"
12 #include "fsl_tfa9896.h"
13 #include "fsl_tfa9896_buffer.h"
14 #include "fsl_debug_console.h"
15 /*******************************************************************************
16  * Definitions
17  ******************************************************************************/
18 /*******************************************************************************
19  * Prototypes
20  ******************************************************************************/
21 
22 /*******************************************************************************
23  * Code
24  ******************************************************************************/
TFA9896_Init(tfa9896_handle_t * handle,tfa9896_config_t * tfa9896Config)25 status_t TFA9896_Init(tfa9896_handle_t *handle, tfa9896_config_t *tfa9896Config)
26 {
27     uint8_t trial        = 10U;
28     uint8_t factory_done = 0;
29     uint16_t regRead = 0, do_calibrate = 0;
30     int status;
31     status_t error;
32     int calibrateDone = 0;
33     float re25;
34     int i, tries = 0;
35     tfa9896_config_t *config = tfa9896Config;
36     handle->config           = config;
37 
38     /* i2c bus initialization */
39     error = CODEC_I2C_Init(handle->i2cHandle, handle->config->i2cConfig.codecI2CInstance, TFA_I2C_BITRATE,
40                            handle->config->i2cConfig.codecI2CSourceClock);
41     if (error != kStatus_HAL_I2cSuccess)
42     {
43         return kStatus_TFA9896_I2C_Fatal;
44     }
45 
46     /*************************/
47     error = TFA9896_SetBits(handle, TFA1_BF_I2CR, 1);
48     if (error != kStatus_TFA9896_Ok)
49     {
50         return error;
51     }
52 
53     error = TFA9896_SetBits(handle, TFA1_BF_RST, 1);
54     if (error != kStatus_TFA9896_Ok)
55     {
56         return error;
57     }
58     /****************************/
59     error = TFA9896_WriteRegister(handle, 0x06, (uint16_t)0x000b);
60     if (error != kStatus_TFA9896_Ok)
61     {
62         return error;
63     }
64 
65     error = TFA9896_WriteRegister(handle, 0x07, (uint16_t)0x3e7f);
66     if (error != kStatus_TFA9896_Ok)
67     {
68         return error;
69     }
70 
71     error = TFA9896_WriteRegister(handle, 0x0a, (uint16_t)0x0d8a);
72     if (error != kStatus_TFA9896_Ok)
73     {
74         return error;
75     }
76 
77     error = TFA9896_WriteRegister(handle, 0x48, (uint16_t)0x0300);
78     if (error != kStatus_TFA9896_Ok)
79     {
80         return error;
81     }
82 
83     error = TFA9896_WriteRegister(handle, 0x88, (uint16_t)0x0100);
84     if (error != kStatus_TFA9896_Ok)
85     {
86         return error;
87     }
88 
89     error = TFA9896_ReadRegister(handle, 0x49, &regRead);
90     if (error != kStatus_TFA9896_Ok)
91     {
92         return error;
93     }
94 
95     regRead &= ~0x1;
96     error = TFA9896_WriteRegister(handle, 0x49, regRead);
97     if (error != kStatus_TFA9896_Ok)
98     {
99         return error;
100     }
101     /****************************/
102     error = TFA9896_SetBits(handle, TFA1_BF_AUDFS, 8);
103     if (error != kStatus_TFA9896_Ok)
104     {
105         return error;
106     }
107 
108     error = TFA9896_SetBits(handle, TFA1_BF_TDMSAMSZ, 15);
109     if (error != kStatus_TFA9896_Ok)
110     {
111         return error;
112     }
113 
114     error = TFA9896_SetBits(handle, TFA1_BF_NBCK, 0);
115     if (error != kStatus_TFA9896_Ok)
116     {
117         return error;
118     }
119 
120     if (handle->config->slaveAddress == 0x34)
121     {
122         error = TFA9896_SetBits(handle, TFA1_BF_CHS12, 1);
123         if (error != kStatus_TFA9896_Ok)
124         {
125             return error;
126         }
127     }
128     else if (handle->config->slaveAddress == 0x36)
129     {
130         error = TFA9896_SetBits(handle, TFA1_BF_CHS12, 2);
131         if (error != kStatus_TFA9896_Ok)
132         {
133             return error;
134         }
135     }
136 
137     error = TFA9896_SetBits(handle, TFA1_BF_IPLL, 0);
138     error = TFA9896_SetBits(handle, TFA1_BF_CLIP, 1);
139     if (error != kStatus_TFA9896_Ok)
140     {
141         return error;
142     }
143 
144     error = TFA9896_SetBits(handle, TFA1_BF_ATTEN, 5);
145     if (error != kStatus_TFA9896_Ok)
146     {
147         return error;
148     }
149 
150     error = TFA9896_SetBits(handle, TFA1_BF_DCTRIP, 7);
151     if (error != kStatus_TFA9896_Ok)
152     {
153         return error;
154     }
155 
156     error = TFA9896_SetBits(handle, bst_slpcmplvl, 3);
157     if (error != kStatus_TFA9896_Ok)
158     {
159         return error;
160     }
161 
162     error = TFA9896_SetBits(handle, ignore_flag_voutcomp86, 0);
163     if (error != kStatus_TFA9896_Ok)
164     {
165         return error;
166     }
167 
168     error = TFA9896_SetBits(handle, TFA1_BF_DCPWM, 1);
169     if (error != kStatus_TFA9896_Ok)
170     {
171         return error;
172     }
173 
174     error = TFA9896_SetBits(handle, TFA1_BF_CFE, 1);
175     if (error != kStatus_TFA9896_Ok)
176     {
177         return error;
178     }
179 
180     error = TFA9896_SetBits(handle, TFA1_BF_CHSA, 2);
181     if (error != kStatus_TFA9896_Ok)
182     {
183         return error;
184     }
185 
186     error = TFA9896_SetBits(handle, TFA1_BF_AMPC, 1);
187     if (error != kStatus_TFA9896_Ok)
188     {
189         return error;
190     }
191 
192     /*******PWUP**********/
193     TFA9896_Powerdown(handle, 0);
194     /* Check the PLL is powered up from status register 0*/
195     error = TFA9896_ReadRegister(handle, 0x00, &regRead);
196     if (error != kStatus_TFA9896_Ok)
197     {
198         return error;
199     }
200 
201     while ((regRead & 0x8000) == 0 && (tries < PLL_READ_TRIAL))
202     {
203         tries++;
204         error = TFA9896_ReadRegister(handle, 0x00, &regRead);
205         if (error != kStatus_TFA9896_Ok)
206         {
207             return error;
208         }
209     }
210     if (tries >= PLL_READ_TRIAL)
211     {
212         /* something wrong with communication with DSP */
213         error = kStatus_TFA9896_StateTimedOut;
214     }
215     /*************Force Coldstart*************/
216     while (TFA9896_GetBits(handle, TFA1_BF_ACS) != 1)
217     {
218         /*******************************************/
219         (void)TFA9896_DSP_Write_Mem_Word(handle, 0x8100, 1, DMEM_IOMEM);
220         /*****************************************************/
221         if (trial-- == 0)
222         {
223             return kStatus_TFA9896_DSP_not_running;
224         }
225     }
226     for (trial = 1; trial < 10; trial++)
227     {
228         (void)TFA9896_DSP_System_Stable(handle, &status);
229         if (status)
230         {
231             break;
232         }
233         else
234             for (i = 0; i < 0xfff; i++)
235                 ;
236     }
237     if (trial >= 10)
238     {
239         TFA9896_Printf("Timed out , exit, exit\n");
240         return kStatus_TFA9896_DSP_not_running;
241     }
242     else
243         TFA9896_Printf(" OK (trial=%d)\n", trial);
244     factory_done = TFA9896_isFactory(handle);
245     if (handle->config->calibrate)
246     {
247         do_calibrate = true;
248     }
249     if (do_calibrate)
250     {
251         error = TFA9896_ClearOneTimeCalibration(handle);
252         if (error != kStatus_TFA9896_Ok)
253         {
254             return error;
255         }
256         error = TFA9896_SetOneTimeCalibration(handle);
257         if (error != kStatus_TFA9896_Ok)
258         {
259             return error;
260         }
261     }
262     else if (!factory_done)
263     {
264         error = TFA9896_SetFactoryValues(handle);
265         if (error != kStatus_TFA9896_Ok)
266         {
267             return error;
268         }
269     }
270     /**************************************************/
271     error = TFA9896_CheckICROMversion(handle, patch_patch);
272     if (error != kStatus_TFA9896_Ok)
273     {
274         return error;
275     }
276     TFA9896_Printf("SC:%x PATCH loading Start--length to be  loaded:%d\n", handle, patch_patch_len);
277     error = TFA9896_ProcessPatchFile(handle, patch_patch_len - TFA9896_PATCH_EXTRA, patch_patch + TFA9896_PATCH_EXTRA);
278     if (error != kStatus_TFA9896_Ok)
279     {
280         return error;
281     }
282     TFA9896_Printf(" PATCH OK \n");
283 
284     error = TFA9896_DSP_Write_Mem_Word(handle, 512, 0, DMEM_XMEM);
285     if (error != kStatus_TFA9896_Ok)
286     {
287         return error;
288     }
289     /* Reset DSP once for sure after initializing */
290     error = TFA9896_SetBits(handle, TFA1_BF_RST, 0);
291     if (error != kStatus_TFA9896_Ok)
292     {
293         return error;
294     }
295     error = TFA9896_DSPWriteTables(handle);
296     if (error != kStatus_TFA9896_Ok)
297     {
298         return error;
299     }
300     /************************/
301     error = TFA9896_SetMute(handle, Mute_Digital);
302     if (error != kStatus_TFA9896_Ok)
303     {
304         return error;
305     }
306     TFA9896_Printf("SL:%x CONFIG loading Start--length to be  loaded:%d\n", config_config_len);
307     error = TFA9896_DspWriteConfig(handle, config_config_len, config_config);
308 
309     if (error != kStatus_TFA9896_Ok)
310     {
311         return error;
312     }
313     TFA9896_Printf(" CONFIG OK \n");
314     TFA9896_Printf("SL:%x Speaker loading Start--length to be  loaded:%d\n", speaker_speaker_len);
315     error = TFA9896_DspWriteSpeakerParameters(handle, speaker_speaker_len, speaker_speaker);
316     if (error != kStatus_TFA9896_Ok)
317     {
318         return error;
319     }
320     TFA9896_Printf("SpeakerOk\n");
321 
322     TFA9896_Printf("SC:%x Preset loading Start--length to be  loaded:%d\n", preset_preset_len);
323     error = TFA9896_DspWritePreset(handle, preset_preset_len, preset_preset);
324     if (error != kStatus_TFA9896_Ok)
325     {
326         return error;
327     }
328     TFA9896_Printf("PresetOk\n");
329     /******************************/
330     error = TFA9896_Write_FilterBank(handle, (tfa9896FilterM_t *)(eq_eq));
331     if (error != kStatus_TFA9896_Ok)
332     {
333         return error;
334     }
335     /*************Wait for AMPS*************/
336 
337     error = TFA9896_SetBits(handle, TFA1_BF_SBSL, 1);
338     if (error != kStatus_TFA9896_Ok)
339     {
340         return error;
341     }
342     /* Wait for calibration to complete*/
343     if (do_calibrate)
344     {
345         error = TFA9896_WaitCalibrateDone(handle, &calibrateDone);
346         if (error != kStatus_TFA9896_Ok)
347         {
348             return error;
349         }
350         if (calibrateDone)
351         {
352             error = TFA9896_DspGetCalibrationImpedance(handle, &re25);
353             if (error != kStatus_TFA9896_Ok)
354             {
355                 return error;
356             }
357         }
358         else
359         {
360             re25 = 0;
361         }
362         TFA9896_Printf("TFA9896 0x%02X Calibration value is %2.2f ohm\n", re25);
363     }
364     error = TFA9896_PrintCalibration(handle);
365     if (error != kStatus_TFA9896_Ok)
366     {
367         return error;
368     }
369     error = TFA9896_SetMute(handle, Mute_Off);
370     if (error != kStatus_TFA9896_Ok)
371     {
372         return error;
373     }
374 
375     return kStatus_TFA9896_Ok;
376 }
377 
TFA9896_Deinit(tfa9896_handle_t * handle)378 status_t TFA9896_Deinit(tfa9896_handle_t *handle)
379 {
380     TFA9896_Printf("%s: Device 0x%02x deinit", __FUNCTION__);
381     TFA9896_Powerdown(handle, 1);
382     return kStatus_TFA9896_Ok;
383 }
384 
TFA9896_Powerdown(tfa9896_handle_t * handle,int powerdown)385 void TFA9896_Powerdown(tfa9896_handle_t *handle, int powerdown)
386 {
387     TFA9896_SetBits(handle, TFA1_BF_PWDN, (uint16_t)powerdown);
388 }
389 
TFA9896_SetVolume(tfa9896_handle_t * handle,uint8_t volume_level)390 status_t TFA9896_SetVolume(tfa9896_handle_t *handle, uint8_t volume_level)
391 {
392     return TFA9896_SetBits(handle, TFA1_BF_VOL, (uint16_t)volume_level);
393 }
394