1 /*
2  * Copyright  2019 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef TFA2_CONTAINER_H
9 #define TFA2_CONTAINER_H
10 
11 #define TFA_MAX_CNT_LENGTH (256 * 1024)
12 #define MEMTRACK_MAX_WORDS 150
13 #define LSMODEL_MAX_WORDS  150
14 #define TFA98XX_MAXTAG     (150)
15 #define FW_VAR_API_VERSION (521)
16 
17 #include "tfa2_dev.h"
18 
19 #include "tfa9xxx_parameters.h"
20 
21 // TODO move relevant comments
22 #if 1
23 /* tfa2_container.c */
24 int tfa2_cnt_get_init_multi_msg(nxpTfaContainer_t *cnt, int dev_idx, int profile, char *mmsg, int *length);
25 int tfa2_load_cnt(void *cnt, int length);
26 int tfa2_cnt_crc_check_container(nxpTfaContainer_t *cont);
27 char *tfa2_cnt_get_string(nxpTfaContainer_t *cnt, nxpTfaDescPtr_t *dsc);
28 char *tfa2_cnt_device_name(nxpTfaContainer_t *cnt, int dev_idx);
29 char *tfa2_cnt_profile_name(nxpTfaContainer_t *cnt, int dev_idx, int prof_idx);
30 int tfa2_cnt_get_app_name(struct tfa2_device *tfa, char *name);
31 int tfa2_cnt_get_cmd(nxpTfaContainer_t *cnt, int devidx, int profidx, int offset, uint8_t **array, int *length);
32 nxpTfaDescPtr_t *tfa2_cnt_get_dsc(nxpTfaContainer_t *cnt, nxpTfaDescriptorType_t type, int dev_idx);
33 void tfa2_cnt_show_header(nxpTfaHeader_t *hdr);
34 int tfa2_cnt_get_slave(nxpTfaContainer_t *cnt, int dev_idx);
35 nxpTfaDeviceList_t *tfa2_cnt_device(nxpTfaContainer_t *cnt, int dev_idx);
36 nxpTfaDeviceList_t *tfa2_cnt_get_dev_list(nxpTfaContainer_t *cont, int dev_idx);
37 nxpTfaProfileList_t *tfa2_cnt_get_dev_prof_list(nxpTfaContainer_t *cont, int devIdx, int profIdx);
38 int tfa2_dev_get_dev_nprof(struct tfa2_device *tfa);
39 int tfa2_cnt_get_dev_nprof(nxpTfaContainer_t *cnt, int dev_idx);
40 int tfa2_cnt_grep_profile_name(nxpTfaContainer_t *cnt, int dev_idx, const char *string);
41 int tfa2_cnt_grep_nth_profile_name(nxpTfaContainer_t *cnt, int dev_idx, int n, const char *string);
42 int tfa2_cnt_get_clockdep_idx(
43     struct tfa2_device *tfa, nxpTfaDescPtr_t *dsc_list, int length, int *clockdep_idx, int *default_section_idx);
44 int tfa2_cnt_write_regs_dev(struct tfa2_device *tfa);
45 int tfa2_cnt_check_revid(struct tfa2_device *tfa);
46 int tfa2_cnt_write_regs_profile(struct tfa2_device *tfa, int prof_idx);
47 int tfa2_cnt_write_msg(struct tfa2_device *tfa, int wlength, char *wbuf);
48 int tfa2_cnt_write_patches(struct tfa2_device *tfa);
49 int tfa2_cnt_write_msg_dsc(struct tfa2_device *tfa, nxpTfaDescPtr_t *dsc);
50 int tfa2_cnt_write_files(struct tfa2_device *tfa);
51 int tfa2_cnt_write_files_profile(struct tfa2_device *tfa, int prof_idx, int vstep_idx);
52 int tfa2_cnt_write_transient_profile(struct tfa2_device *tfa, int prof_idx);
53 int tfa2_cnt_write_file(struct tfa2_device *tfa, nxpTfaFileDsc_t *file);
54 int tfa2_cnt_write_profile(struct tfa2_device *tfa, int prof_idx, int vstep_idx);
55 int tfa2_cnt_get_idx(struct tfa2_device *tfa);
56 int tfa2_cnt_write_patch(struct tfa2_device *tfa, nxpTfaPatch_t *patchfile);
57 
58 // TODO move to app top level?
59 void tfa2_show_current_state(struct tfa2_device *tfa);
60 
61 /* from tfa2_container_crc32.c */
62 uint32_t crc32_le(uint32_t crc, unsigned char const *buf, size_t len);
63 
64 #else
65 
66 /**
67  * Pass the container buffer, initialize and allocate internal memory.
68  *
69  * @param cnt pointer to the start of the buffer holding the container file
70  * @param length of the data in bytes
71  * @return
72  *  - tfa_error_ok if normal
73  *  - tfa_error_container invalid container data
74  *  - tfa_error_bad_param invalid parameter
75  *
76  */
77 int tfa2_load_cnt(void *cnt, int length);
78 
79 /**
80  * Return the descriptor string
81  * @param cnt pointer to the container struct
82  * @param dsc pointer to nxpTfa descriptor
83  * @return descriptor string
84  */
85 char *tfa2_cnt_get_string(nxpTfaContainer_t *cnt, nxpTfaDescPtr_t *dsc);
86 
87 /**
88  * Return the descriptor tfahal
89  * @param cnt pointer to the container struct
90  * @param dsc pointer to nxpTfa descriptor
91  * @return descriptor tfahal
92  */
93 char *tfa2_cont_get_tfahal(nxpTfaContainer_t *cnt, nxpTfaDescPtr_t *dsc);
94 
95 /**
96  * Gets the string for the given command type number
97  * @param type number representing a command
98  * @return string of a command
99  */
100 char *tfa2_cnt_get_command_string(uint32_t type);
101 
102 /**
103  * get the device type from the patch in this devicelist
104  *  - find the patch file for this devidx
105  *  - return the devid from the patch or 0 if not found
106  * @param cnt pointer to container file
107  * @param dev_idx device index
108  * @return descriptor string
109  */
110 int tfa2_cnt_get_devid(nxpTfaContainer_t *cnt, int dev_idx);
111 
112 /**
113  * Get the slave for the device if it exists.
114  * @param cnt
115  * @param the index of the device
116  * @return slave
117  */
118 int tfa2_cnt_get_slave(nxpTfaContainer_t *cnt, int dev_idx);
119 
120 void tfa2_cnt_set_slave(uint8_t slave_addr);
121 
122 /**
123  * Get the index for a slave address.
124  * @param tfa the device struct pointer
125  * @return the device index
126  */
127 int tfa2_cnt_get_idx(struct tfa2_device *tfa);
128 
129 /**
130  * Write reg and bitfield items in the devicelist to the target.
131  * @param tfa the device struct pointer
132  * @return errno
133  */
134 int tfa2_cnt_write_regs_dev(struct tfa2_device *tfa);
135 
136 /**
137  * Write  reg  and bitfield items in the profilelist to the target.
138  * @param tfa the device struct pointer
139  * @param prof_idx the profile index
140  * @return errno
141  */
142 int tfa2_cnt_write_regs_profile(struct tfa2_device *tfa, int prof_idx);
143 
144 /**
145  * Write a patchfile in the devicelist to the target.
146  * @param tfa the device struct pointer
147  * @return errno
148  */
149 int tfa2_cnt_write_patches(struct tfa2_device *tfa);
150 
151 /**
152  * Write all  param files in the devicelist to the target.
153  * @param tfa the device struct pointer
154  * @return errno
155  */
156 int tfa2_cnt_write_files(struct tfa2_device *tfa);
157 
158 /**
159  * Get sample rate from passed profile index
160  * @param tfa the device struct pointer
161  * @param prof_idx the index of the profile
162  * @return sample rate value
163  */
164 unsigned int tfa98xx_get_profile_sr(struct tfa2_device *tfa, unsigned int prof_idx);
165 
166 /**
167  * Get the device name string
168  * @param cnt the pointer to the container struct
169  * @param dev_idx the index of the device
170  * @return device name string or error string if not found
171  */
172 char *tfa2_cnt_device_name(nxpTfaContainer_t *cnt, int dev_idx);
173 
174 /**
175  * Get the application name from the container file application field
176  * @param tfa the device struct pointer
177  * @param name the input stringbuffer with size: sizeof(application field)+1
178  * @return actual string length
179  */
180 int tfa2_cnt_get_app_name(struct tfa2_device *tfa, char *name);
181 
182 /**
183  * Get profile index of the calibration profile
184  * @param tfa the device struct pointer
185  * @return profile index, -2 if no calibration profile is found or -1 on error
186  */
187 int tfa2_cnt_get_cal_profile(struct tfa2_device *tfa);
188 
189 /**
190  * Is the profile a tap profile ?
191  * @param tfa the device struct pointer
192  * @param prof_idx the index of the profile
193  * @return 1 if the profile is a tap profile or 0 if not
194  */
195 int tfa2_cnt_is_tap_profile(struct tfa2_device *tfa, int prof_idx);
196 
197 /**
198  * Get the name of the profile at certain index for a device in the container file
199  * @param cnt the pointer to the container struct
200  * @param dev_idx the index of the device
201  * @param prof_idx the index of the profile
202  * @return profile name string or NULL string if not found
203  */
204 char *tfa2_cnt_profile_name(nxpTfaContainer_t *cnt, int dev_idx, int prof_idx);
205 
206 /**
207  * Process all items in the profilelist
208  * NOTE an error return during processing will leave the device muted
209  * @param tfa the device struct pointer
210  * @param prof_idx index of the profile
211  * @param vstep_idx index of the vstep
212  * @return errno
213  */
214 int tfa2_cnt_write_profile(struct tfa2_device *tfa, int prof_idx, int vstep_idx);
215 
216 /**
217  * Specify the speaker configurations (cmd id) (Left, right, both, none)
218  * @param dev_idx index of the device
219  * @param configuration name string of the configuration
220  */
221 void tfa98xx_set_spkr_select(int dev_idx, char *configuration);
222 
223 int tfa2_cont_write_filterbank(struct tfa2_device *tfa, nxpTfaFilter_t *filter);
224 
225 /**
226  * Write all  param files in the profilelist to the target
227  * this is used during startup when maybe ACS is set
228  * @param tfa the device struct pointer
229  * @param prof_idx the index of the profile
230  * @param vstep_idx the index of the vstep
231  * @return errno
232  */
233 int tfa2_cnt_write_files_profile(struct tfa2_device *tfa, int prof_idx, int vstep_idx);
234 // int tfa2_cnt_write_filesVstep(struct tfa2_device *tfa, int prof_idx, int vstep_idx);
235 int tfa2_cnt_write_drc_file(struct tfa2_device *tfa, int size, uint8_t data[]);
236 
237 /**
238  * Get the device list dsc from the tfaContainer
239  * @param cont pointer to the tfaContainer
240  * @param dev_idx the index of the device
241  * @return device list pointer
242  */
243 nxpTfaDeviceList_t *tfa2_cnt_get_dev_list(nxpTfaContainer_t *cont, int dev_idx);
244 
245 /**
246  * Get the Nth profile for the Nth device
247  * @param cont pointer to the tfaContainer
248  * @param dev_idx the index of the device
249  * @param prof_idx the index of the profile
250  * @return profile list pointer
251  */
252 nxpTfaProfileList_t *tfa2_cnt_get_dev_prof_list(nxpTfaContainer_t *cont, int dev_idx, int prof_idx);
253 
254 /**
255  * Get the 1st  profilename match for this  device
256  * @param cont pointer to the tfaContainer
257  * @param dev_idx the index of the device
258  * @param string to search
259  * @return profile  index
260  */
261 int tfa2_cnt_grep_profile_name(nxpTfaContainer_t *cnt, int devidx, const char *string);
262 
263 /**
264  * Get the number of profiles for device from container in tfa
265  * @param tfa the device struct pointer
266  * @return device list pointer
267  */
268 int tfa2_dev_get_dev_nprof(struct tfa2_device *tfa);
269 
270 /**
271  * Get the number of profiles for device from container
272  * @param cont pointer to the tfaContainer
273  * @param dev_idx the index of the device
274  * @return device list pointer
275  */
276 int tfa2_cnt_get_dev_nprof(nxpTfaContainer_t *cnt, int dev_idx);
277 
278 /**
279  * Get the Nth livedata for the Nth device
280  * @param cont pointer to the tfaContainer
281  * @param dev_idx the index of the device
282  * @param livedata_idx the index of the livedata
283  * @return livedata list pointer
284  */
285 nxpTfaLiveDataList_t *tfa2_cnt_get_dev_live_data_list(nxpTfaContainer_t *cont, int dev_idx, int livedata_idx);
286 
287 /**
288  * Check CRC for container
289  * @param cont pointer to the tfaContainer
290  * @return error value 0 on error
291  */
292 int tfa2_cnt_crc_check_container(nxpTfaContainer_t *cont);
293 
294 /**
295  * Get the device list pointer
296  * @param cnt pointer to the container struct
297  * @param dev_idx the index of the device
298  * @return pointer to device list
299  */
300 nxpTfaDeviceList_t *tfa2_cnt_device(nxpTfaContainer_t *cnt, int dev_idx);
301 
302 /**
303  * Return the pointer to the first profile in a list from the tfaContainer
304  * @param cont pointer to the tfaContainer
305  * @return pointer to first profile in profile list
306  */
307 nxpTfaProfileList_t *tfa2_cnt_get1st_prof_list(nxpTfaContainer_t *cont);
308 
309 /**
310  * Return the pointer to the next profile in a list
311  * @param prof is the pointer to the profile list
312  * @return profile list pointer
313  */
314 nxpTfaProfileList_t *tfa2_cnt_next_profile(nxpTfaProfileList_t *prof);
315 
316 /**
317  * Return the pointer to the first livedata in a list from the tfaContainer
318  * @param cont pointer to the tfaContainer
319  * @return pointer to first livedata in profile list
320  */
321 nxpTfaLiveDataList_t *tfa2_cnt_get1st_live_data_list(nxpTfaContainer_t *cont);
322 
323 /**
324  * Return the pointer to the next livedata in a list
325  * @param livedata_idx is the pointer to the livedata list
326  * @return livedata list pointer
327  */
328 nxpTfaLiveDataList_t *tfa2_cnt_next_live_data(nxpTfaLiveDataList_t *livedata_idx);
329 
330 /**
331  * Write a bit field
332  * @param tfa the device struct pointer
333  * @param bf bitfield to write
334  * @return errno
335  */
336 int tfaRunWriteBitfield(struct tfa2_device *tfa, nxpTfaBitfield_t bf);
337 
338 /**
339  * Write a parameter file to the device
340  * @param tfa the device struct pointer
341  * @param file filedescriptor pointer
342  * @return errno
343  */
344 int tfa2_cnt_write_file(struct tfa2_device *tfa, nxpTfaFileDsc_t *file);
345 /**
346  * Get the max volume step associated with Nth profile for the Nth device
347  * @param tfa the device struct pointer
348  * @param prof_idx profile index
349  * @return the number of vsteps
350  */
351 int tfacont_get_max_vstep(struct tfa2_device *tfa, int prof_idx);
352 
353 /**
354  * Get the file contents associated with the device or profile
355  * Search within the device tree, if not found, search within the profile
356  * tree. There can only be one type of file within profile or device.
357  * @param tfa the device struct pointer
358  * @param prof_idx I2C profile index in the device
359  * @param type file type
360  * @return 0 NULL if file type is not found
361  * @return 1 file contents
362  */
363 nxpTfaFileDsc_t *tfacont_getfiledata(struct tfa2_device *tfa, int prof_idx, enum nxpTfaHeaderType type);
364 
365 /**
366  * Dump the contents of the file header
367  * @param hdr pointer to file header data
368  */
369 void tfa2_cnt_show_header(nxpTfaHeader_t *hdr);
370 
371 /**
372  * Read a bit field
373  * @param tfa the device struct pointer
374  * @param bf bitfield to read out
375  * @return errno
376  */
377 int tfaRunReadBitfield(struct tfa2_device *tfa, nxpTfaBitfield_t *bf);
378 
379 ///**
380 // * Get hw feature bits from container file
381 // * @param tfa the device struct pointer
382 // * @param hw_feature_register pointer to where hw features are stored
383 // */
384 // void get_hw_features_from_cnt(struct tfa2_device *tfa, int *hw_feature_register);
385 //
386 ///**
387 // * Get sw feature bits from container file
388 // * @param tfa the device struct pointer
389 // * @param sw_feature_register pointer to where sw features are stored
390 // */
391 // void get_sw_features_from_cnt(struct tfa2_device *tfa, int sw_feature_register[2]);
392 
393 /**
394  * Factory trimming for the Boost converter
395  * check if there is a correction needed
396  * @param tfa the device struct pointer
397  */
398 int tfa98xx_factory_trimmer(struct tfa2_device *tfa);
399 
400 /**
401  * Search for filters settings and if found then write them to the device
402  * @param tfa the device struct pointer
403  * @param prof_idx profile to look in
404  * @return errno
405  */
406 int tfa2_set_filters(struct tfa2_device *tfa, int prof_idx);
407 
408 /**
409  * Get the firmware version from the patch in the container file
410  * @param tfa the device struct pointer
411  * @return firmware version
412  */
413 int tfa2_cnt_get_patch_version(struct tfa2_device *tfa);
414 
415 int tfa2_tib_dsp_msgmulti(struct tfa2_device *tfa, int length, const char *buffer);
416 
417 /*
418  * Get profile index of the calibration profile.
419  * @param tfa the device struct pointer
420  * @return (profile index) if found, (-2) if no calibration profile is found or (-1) on error
421  */
422 int tfa2_cnt_get_main_profile(struct tfa2_device *tfa);
423 
424 /*
425  * write the rpc msg fomr the descriptor
426  */
427 int tfa2_cnt_write_msg_dsc(struct tfa2_device *tfa, nxpTfaDescPtr_t *dsc);
428 #endif
429 
430 #endif /* TFA2_CONTAINER_H */
431