1 /*
2  * Copyright  2019 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef TFA2_DEV_H_
9 #define TFA2_DEV_H_
10 
11 #ifdef __KERNEL__
12 
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/err.h>
16 #include <linux/i2c.h>
17 #include <linux/delay.h>
18 #include <linux/ctype.h>
19 #include <asm/unaligned.h>
20 
21 #else
22 
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28 #include "stddef.h"
29 #include "config.h"
30 
31 #if !defined(kmalloc) || !defined(kfree)
32 #define kmalloc(ptr, flags)       malloc(ptr)
33 #define kfree(ptr)                free(ptr)
34 #define GFP_KERNEL                0
35 #define msleep_interruptible(val) usleep(1000 * val)
36 #endif
37 
38 #define le16_to_cpu(a)   (a)
39 
40 typedef enum log_level
41 {
42     LOG_NONE,
43     LOG_ERR,
44     LOG_WARN,
45     LOG_INFO,
46     LOG_DEBUG,
47 } log_level_t;
48 
49 /*
50     map kernel dev_* logging functions to user space
51     - dev pointer is used point to idx
52 */
53 #define dev_err(I, ...)  tfaprintf(LOG_ERR, I, __VA_ARGS__)
54 #define dev_warn(I, ...) tfaprintf(LOG_WARN, I, __VA_ARGS__)
55 #define dev_info(I, ...) tfaprintf(LOG_INFO, I, __VA_ARGS__)
56 #define dev_dbg(I, ...)  tfaprintf(LOG_DEBUG, I, __VA_ARGS__)
57 
58 /*
59     map kernel pr_* logging functions to user space
60     - no dev pointer available, make it NULL
61 */
62 #define pr_err(...)      dev_err(NULL, __VA_ARGS__)
63 #define pr_warn(...)     dev_warn(NULL, __VA_ARGS__)
64 #define pr_info(...)     dev_info(NULL, __VA_ARGS__)
65 #define pr_debug(...)    dev_dbg(NULL, __VA_ARGS__)
66 
67 struct i2c_client
68 {
69     int addr;  /*!< I2C slave addres */
70     void *hal; /*!< Device HAL plugin handle */
71     int dev;   /*!< used for user mode print, store idx */
72 };
73 
74 #endif
75 
76 #include "tfa9xxx_parameters.h"
77 #include "tfa_haptic_fw_defs.h"
78 
79 /*
80  * these are the locally defined bitfields
81  */
82 typedef enum tfa9xxx_core_bf
83 {
84     TFA9XXX_BF_AMPE       = 0x0030, /*!< Enable Amplifier                                   */
85     TFA9XXX_BF_AMPC       = 0x0060, /*!< CoolFlux control over amplifier                    */
86     TFA9XXX_BF_ACKCMD     = 0x9380, /*!< Firmware event acknowledge rpc command             */
87     TFA9XXX_BF_ACS        = 0x1090, /*!< Cold Start                                         */
88     TFA9XXX_BF_CFE        = 0x0020, /*!< Enable CoolFlux DSP                                */
89     TFA9XXX_BF_CFINT      = 0x9040, /*!< Coolflux Interrupt - auto clear                    */
90     TFA9XXX_BF_CFSM       = 0x5130, /*!< Coolflux firmware soft mute control                */
91     TFA9XXX_BF_VDDS       = 0x1000, /*!< POR                                                */
92     TFA9XXX_BF_OTDS       = 0x1020, /*!< OTP alarm                                          */
93     TFA9XXX_BF_OVDS       = 0x1030, /*!< OVP alarm                                          */
94     TFA9XXX_BF_UVDS       = 0x1040, /*!< UVP alarm                                          */
95     TFA9XXX_BF_CLKS       = 0x1060, /*!< Clocks stable                                      */
96     TFA9XXX_BF_MTPB       = 0x1070, /*!< MTP busy                                           */
97     TFA9XXX_BF_NOCLK      = 0x1080, /*!< Lost clock                                         */
98     TFA9XXX_BF_WDS        = 0x10a0, /*!< Watchdog                                           */
99     TFA9XXX_BF_SPKS       = 0x1170, /*!< Speaker status                                     */
100     TFA9XXX_BF_TDMERR     = 0x11a0, /*!< TDM error                                          */
101     TFA9XXX_BF_TDMLUTER   = 0x11b0, /*!< TDM lookup table error                             */
102     TFA9XXX_BF_DMEM       = 0x9011, /*!< Target memory for CFMA using I2C interface         */
103     TFA9XXX_BF_I2CR       = 0x0010, /*!< I2C Reset - Auto clear                             */
104     TFA9XXX_BF_ICLVDDS    = 0x4400, /*!< Clear POR                                          */
105     TFA9XXX_BF_IPOVDDS    = 0x4c00, /*!< Polarity POR                                       */
106     TFA9XXX_BF_ISTVDDS    = 0x4000, /*!< Status POR                                         */
107     TFA9XXX_BF_VOL        = 0x5187, /*!< CF firmware volume control                         */
108     TFA9XXX_BF_MANSCONF   = 0x0120, /*!< Device I2C settings configured                     */
109     TFA9XXX_BF_MANCOLD    = 0x130,  /*!< Execute cold start                                , */
110     TFA9XXX_BF_MANSTATE   = 0x1433, /*!< Device Manager status                              */
111     TFA9XXX_BF_OPENMTP    = 0x01e0, /*!< Control for FAIM protection                        */
112     TFA9XXX_BF_PWDN       = 0x0000, /*!< Powerdown control                                  */
113     TFA9XXX_BF_REQCMD     = 0x9080, /*!< Firmware event request rpc command                 */
114     TFA9XXX_BF_ACKCAL     = 0x93e0, /*!< Firmware event acknowledge calibration completed   */
115     TFA9XXX_BF_REV        = 0x030f, /*!< Revision info                                      */
116     TFA9XXX_BF_MANAOOSC   = 0x460,  /*!< Internal OSC1M off at PWDN                        , */
117     TFA9XXX_BF_RST        = 0x9000, /*!< Reset for Coolflux DSP                             */
118     TFA9XXX_BF_SBSL       = 0x0050, /*!< Coolflux configured                                */
119     TFA9XXX_BF_LPM1MODE   = 0x5ac1, /*!< Low power mode control                              */
120     TFA9XXX_BF_MTPK       = 0xa107, /*!< KEY2 to access KEY2 protected registers, customer key */
121     TFA9XXX_BF_TROS       = 0xb190, /*!< Select temp Speaker calibration                    */
122     TFA9XXX_BF_EXTTS      = 0xb108, /*!< External temperature (C)                           */
123     TFA9XXX_BF_SWPROFIL   = 0xe00f, /*!< Software profile data                              */
124     TFA9XXX_BF_SWVSTEP    = 0xe10f, /*!< Software vstep information                         */
125     TFA9XXX_BF_MTPADDR    = 0xa302, /*!< MTP address for read/writing mtp in manual single word mode, */
126     TFA9XXX_BF_KEY1LOCKED = 0xa200, /*!< Indicates KEY1 is locked                           */
127     TFA9XXX_BF_KEY2LOCKED = 0xa210, /*!< Indicates KEY2 is locked */
128     TFA9XXX_BF_MANCMTPI   = 0xa330, /*!< Start copying single word from MTP to I2C mtp register - auto clear  */
129     TFA9XXX_BF_MANCIMTP   = 0xa340, /*!< Start copying single word from I2C mtp register to mtp - auto clear  */
130     TFA9XXX_BF_CMTPI      = 0xa350, /*!< Start copying all the data from mtp to I2C mtp registers - auto clear */
131     // forbidden TFA9XXX_BF_CIMTP = 0xa360,    /*!< Start copying data from I2C mtp registers to mtp - auto clear */
132     TFA9XXX_BF_MTPRDMSB = 0xa50f, /*!< MSB word of MTP manual read data                   */
133     TFA9XXX_BF_MTPRDLSB = 0xa60f, /*!< LSB word of MTP manual read data                   */
134     TFA9XXX_BF_MTPWRMSB = 0xa70f, /*!< MSB word of MTP manual write data                   */
135     TFA9XXX_BF_MTPWRLSB = 0xa80f, /*!< LSB word of MTP manual write data                   */
136     TFA9XXX_BF_CUSTINFO =
137         0xf078, /*!< TODO fix dev specific  Reserved space for allowing customer to store speaker information */
138     TFA9XXX_BF_MTPOTC = 0xf000, /*!< Calibration schedule                               */
139     TFA9XXX_BF_MTPEX  = 0xf010, /*!< Calibration Ron executed                           */
140     TFA9XXX_BF_R25C   = 0xf50f, /*!< Ron resistance of speaker coil                     */
141 } tfa9xxx_core_bf_t;
142 
143 /* TODO haptic profile names */
144 #define HB_LRA_MAIN       "lra_main.hap"
145 #define HB_LRA_DATA       "lra_data.hap"
146 #define HB_LRA_RECALC     "lra_recalculation.hap"
147 #define HB_RECALC_PROFILE "lra_recalculation.hap"
148 
149 /* return true if in TFA range */
150 #define IS_I2C_TFA(slave) ((0x30 < slave) || (slave > 0x37))
151 
152 enum tfa9xxx_Status_ID
153 {
154     tfa9xxx_no_dsp_response      = -1, /* No response from DSP */
155     tfa9xxx_I2C_Req_Done         = 0,  /* Request executed correctly and result, if any, is available for download */
156     tfa9xxx_I2C_Req_Busy         = 1,  /* Request is being processed, just wait for result */
157     tfa9xxx_I2C_Req_Invalid_M_ID = 2,  /* Provided M-ID does not fit in valid rang [0..2] */
158     tfa9xxx_I2C_Req_Invalid_P_ID = 3,  /* Provided P-ID is not valid in the given M-ID context */
159     tfa9xxx_I2C_Req_Invalid_CC   = 4,  /* Invalid channel configuration bits (SC|DS|DP|DC) combination */
160     tfa9xxx_I2C_Req_Invalid_Seq =
161         5, /* Invalid sequence of commands, in case the DSP expects some commands in a specific order */
162     tfa9xxx_I2C_Req_Invalid_Param = 6, /* Generic error */
163     tfa9xxx_I2C_Req_Buffer_Overflow =
164         7, /* I2C buffer has overflowed: host has sent too many parameters, memory integrity is not guaranteed */
165     tfa9xxx_I2C_Req_Calib_Busy   = 8, /* Calibration not finished */
166     tfa9xxx_I2C_Req_Calib_Failed = 9  /* Calibration failed */
167 };
168 
169 /**
170  * Device states and modifier flags to allow a device/type independent fine
171  * grained control of the internal state.
172  * Values below 0x10 are referred to as base states which can be or-ed with
173  * state modifiers, from 0x10 and higher.
174  *
175  */
176 enum tfa_state
177 {
178     TFA_STATE_NONE,             /**< not requested, unknown or invalid */
179     TFA_STATE_POWERDOWN,        /**< PLL in powerdown, Algo is up/warm */
180     TFA_STATE_POWERUP,          /**< PLL to powerup, Algo can be up/warm */
181     TFA_STATE_OPERATING,        /**< Amp and Algo running */
182     TFA_STATE_RESET,            /**< I2C reset and ACS set */
183     TFA_STATE_INIT_CF,          /**< coolflux HW access possible (~initcf) */
184     TFA_STATE_OSC,              /**< internal oscillator */
185     TFA_STATE_CLOCK,            /**< always return with clock, use OSC if no external clock  */
186     TFA_STATE_POWERDOWN_HAPTIC, /**< PLL in powerdown, skip mute sequence */
187     /* --sticky state modifiers-- */
188     TFA_STATE_MUTE   = 0x10, /**< Algo & Amp mute */
189     TFA_STATE_UNMUTE = 0x20  /**< Algo & Amp unmute */
190 };
191 
192 /**
193  * HB1.5 roles depend on what is expected to be connected or
194  * only audio. This is derived from the container profiles
195  */
196 #define TFA_HB_ROLE_MASK (7) /* use this mask to get lra or ls role */
197 enum tfa_hb_role
198 {
199     tfa_hb_undetermined = -1, /**< not initialized */
200     tfa_hb_none         = 0,  /**< no haptic boost */
201     tfa_hb_lra,               /**< LRA expected */
202     tfa_hb_ls,                /**< loudspeaker expected */
203     tfa_hb_ready = (1 << 3)   /**< ready flag when config is loaded */
204 };
205 
206 /*
207  * haptic sequencer definition
208  */
209 struct tfa2_sequence
210 {
211     int32_t length;
212     int32_t *object;
213     int32_t *duration;
214     int32_t *level;
215     int32_t *freq;
216     int total_duration;
217     int object_table1_cache_save[FW_XMEM_NR_OBJECTS1][FW_XMEM_OBJECTSIZE];
218     int object_table2_cache_save[FW_XMEM_NR_OBJECTS2][FW_XMEM_OBJECTSIZE];
219 };
220 
221 struct haptic_data
222 {
223     int index;     /* selected object */
224     int frequency; /* frequency, valid if non-0 */
225     int amplitude; /* level, strength of signal 0-100% */
226     int duration;  /* time in milliseconds */
227 
228     int recalc_play_object; /* run boot obj in recalculation */
229     int use_bck;
230     int object_table1_cache[FW_XMEM_NR_OBJECTS1][FW_XMEM_OBJECTSIZE];
231     int object_table2_cache[FW_XMEM_NR_OBJECTS2][FW_XMEM_OBJECTSIZE];
232     int delay_attack; /* delay in milliseconds  between object trigger and object execution */
233 
234     int seq_max; /* total nr of sequencer virtual objects */
235     struct tfa2_sequence seq;
236 };
237 
238 enum tfa9xxx_DAI
239 {
240     Tfa9xxx_DAI_NONE      = 0x0,  /**< Unknown  */
241     Tfa9xxx_DAI_I2S       = 0x01, /**< I2S only */
242     Tfa9xxx_DAI_TDM       = 0x02, /**< TDM, I2S */
243     Tfa9xxx_DAI_PDM       = 0x04, /**< PDM  */
244     Tfa9xxx_DAI_SOUNDWIRE = 0x08, /**< SOUNDWIRE  */
245 };
246 
247 /**
248  * This is the main tfa device context structure, it will carry all information
249  * that is needed to handle a single I2C device instance.
250  * All functions dealing with the device will need access to the fields herein.
251  */
252 struct tfa2_device
253 {
254     struct i2c_client *i2c;
255     int dev_idx; /**< device container index */
256     int in_use;
257     int buffer_size;                 /**< lowest level max buffer size */
258                                      //	int has_msg; 			/**< support direct dsp messaging */
259     short slave_address;             /**< I2C slave address (not shifted) */
260     uint16_t rev;                    /**< full revid of this device */
261     int need_hw_init;                /**< hardware parameters not initialized */
262     int need_cf_init;                /**< CoolFlux not configured/patched */
263     int need_sb_config;              /**< SB firmware parameters not configured */
264     enum tfa_hb_role need_hb_config; /**< HB firmware parameters needed */
265                                      //	unsigned char tfa_family; /**< tfa1/tfa2 */
266                                      //	enum featureSupport supportDrc;
267                                      //	enum featureSupport supportFramework;
268                                      //	enum featureSupport support_saam;
269     int sw_feature_bits[2];          /**< cached copy of sw feature bits */
270     int hw_feature_bits;             /**< cached copy of hw feature bits */
271     int profile;                     /**< active profile */
272     int vstep;                       /**< active vstep */
273                                      //	unsigned char spkr_count;
274                                      //	unsigned char spkr_select;
275                                      //	unsigned char support_tcoef;/**< legacy tfa9887, will be removed */
276     enum tfa9xxx_DAI daimap;         /**< supported audio interface types */
277                                      //	int mohm; /**< speaker calibration value in milli ohms -1 is error */
278                                      //	struct tfa2_device_ops dev_ops;
279     uint16_t interrupt_enable[3];
280     uint16_t interrupt_status[3];
281     //	int ext_dsp; /**< respond to external DSP: -1:none, 0:no_dsp, 1:cold, 2:warm */
282     int bus;                     /* logical bus, i2c bus is  in i2c_client */
283     int tfadsp_event;            /**< enum tfadsp_event_en is for external registry */
284     int verbose;                 /**< verbosity level for debug print output */
285     enum tfa_state state;        /**< last known state or-ed with optional state_modifier */
286     struct nxpTfaContainer *cnt; /**< the loaded container file */
287     //	struct nxpTfaVolumeStepRegisterInfo *p_regInfo; /**< remember vstep for partial updates */
288     int partial_enable;       /**< enable partial updates */
289     void *data;               /**< typically pointing to Linux driver structure owning this device */
290     int convert_dsp32;        /**< convert 24 bit DSP messages to 32 bit */
291                               //	int sync_iv_delay; /**< synchronize I/V delay at cold start */
292     int is_probus_device;     /**< probus device: device without internal DSP */
293     int is_extern_dsp_device; /**< externel (non Coolflux) DSP device */
294                               //	int needs_reset; /**< add the reset trigger for SetAlgoParams and SetMBDrc commands */
295                               //	struct kmem_cache *cachep;	/**< Memory allocator handle */
296                               //	struct tfa_hal_dev *hal;  /* Device HAL plugin handle >>>> moved to i2c_client */
297     int reg_time;             /* worst case register read time in usec, for adding extra clock read delay */
298     /* function pointer for per-device overloading */
299     int (*tfa_init)(struct tfa2_device *tfa); /**< init for POR fixes like loading optimal settings */
300     uint16_t bf_clks;                         /**<     TFA9XXX_BF_CLKS   Clocks stable for overload   */
301     uint16_t bf_manstate;                     /**< TFA9XXX_BF_MANSTATE Device Manager status for overload */
302     uint16_t bf_manaoosc;                     /**< TFA9XXX_BF_MANAOOSC overload */
303     uint16_t bf_noclk;                        /**< TFA9XXX_BF_NOCLK overload */
304     uint16_t bf_mtpb;                         /**< TFA9XXX_BF_MTPB overload */
305     uint16_t bf_swprofil;                     /**< TFA9XXX_BF_SWPROFIL overload */
306     uint16_t bf_swvstep;                      /**< TFA9XXX_BF_SWVSTEP overload */
307     uint16_t bf_openmtp;                      /**< TFA9XXX_BF_OPENMTP overload */
308     uint16_t bf_lpm1mode;                     /**<  TFA9XXX_BF_LPM1MODE overload */
309     uint16_t bf_r25c;                         /**<  TFA9XXX_BF_R25C overload */
310     uint16_t status_mask[4];                  /**< status masks for tfa2_dev_status() */
311     uint16_t status_err[4];                   /**< error status for tfa2_dev_status() */
312     struct haptic_data hap_data;              /**< haptic specific data */
313     int (*dsp_init)(struct tfa2_device *tfa, const char *cmd_buf, size_t cmd_len);
314     int (*dsp_execute)(struct tfa2_device *tfa, const char *cmd_buf, size_t cmd_len, char *res_buf, size_t res_len);
315 };
316 
317 #pragma pack(push, 1)
318 struct tfa_patch_header
319 {
320     /* Generic Header */
321     uint16_t id;        /* PA */
322     char version[2];    /* 1_ */
323     char subversion[2]; /* 00 */
324     uint16_t size;
325     uint32_t crc;
326     char customer[8];
327     char application[8];
328     char type[8];
329 
330     /* Patch Header */
331     uint8_t rev;       /* 0x94 */
332     uint16_t address;  /* 0xffff */
333     uint16_t dev_type; /* 0x9914 */
334     uint8_t msb_rev;   /* 0x1a */
335 };
336 #pragma pack(pop)
337 
338 #define PATCH_HEADER_SIZE sizeof(struct tfa_patch_header)
339 
340 /**
341  *  @brief convert 24 bit BE DSP messages to a 32 bit signed LE integer
342  *
343  *  The input is sign extended to 32-bit from 24-bit.
344  *
345  *  @param data32 output 32 bit signed LE integer buffer
346  *  @param data24 input 24 bit BE buffer
347  *  @param length_bytes24 bytes in 24 bit BE buffer
348  *  @return total nr of bytes in the result
349  */
350 int tfa2_24_to_32(int32_t *data32, uint8_t *data24, int length_bytes24);
351 
352 /**
353  *   @brief  truncate 32 bit  integer buffer  to 24 bit BE
354  *
355  *   The input is truncated by chopping the highest byte.
356  *  @param data24 output 24 bit BE buffer
357  *  @param data32 input 32 bit signed LE integer buffer
358  *  @param length_words32 number of words in 32bit  buffer
359  *  @return total nr of bytes in the result
360  */
361 int tfa2_32_to_24(uint8_t *data24, int32_t *data32, int length_words32);
362 
363 int tfa2_dev_set_state(struct tfa2_device *tfa, enum tfa_state state);
364 
365 /* generic tfa i2c level driver functions from tfa_haptic.c */
366 int tfa2_dev_get_revid(struct tfa2_device *tfa);
367 
368 int tfa2_check_patch(const uint8_t *data, const int length, const uint16_t revid);
369 /*
370  * cold start the calibration profile
371  */
372 int tfa2_calibrate_profile_start(struct tfa2_device *tfa);
373 int tfa2_sb_calibrate(struct tfa2_device *tfa);
374 int tfa2_get_calibration_impedance(struct tfa2_device *tfa);
375 
376 int tfa2_process_patch_file(struct i2c_client *client, int length, const uint8_t *bytes);
377 
378 enum tfa2_cf_mem
379 {
380     TFA2_CF_MEM_PMEM  = 0,
381     TFA2_CF_MEM_XMEM  = 1,
382     TFA2_CF_MEM_YMEM  = 2,
383     TFA2_CF_MEM_IOMEM = 3,
384 };
385 
386 int tfa2_i2c_dsp_init(struct tfa2_device *tfa, const char *cmd_buf, size_t cmd_len);
387 int tfa2_i2c_dsp_execute(struct tfa2_device *tfa, const char *cmd_buf, size_t cmd_len, char *res_buf, size_t res_len);
388 int tfa2_i2c_write_cf_mem32(
389     struct i2c_client *client, uint16_t address, int32_t *input, int size, enum tfa2_cf_mem type);
390 int tfa2_i2c_read_cf_mem32(struct i2c_client *client, uint16_t address, int *data, int size, enum tfa2_cf_mem type);
391 int tfa2_i2c_write_cf_mem32_dsp_reset(
392     struct i2c_client *client, uint16_t address, int32_t *input, int size, enum tfa2_cf_mem type);
393 int tfa2_i2c_read_cf_mem32_dsp_reset(
394     struct i2c_client *client, uint16_t address, int *data, int size, enum tfa2_cf_mem type);
395 int tfa2_i2c_write_cf_mem24(
396     struct i2c_client *client, uint16_t address, uint8_t *input, int size, enum tfa2_cf_mem type);
397 int tfa2_i2c_read_cf_mem24(struct i2c_client *client, uint16_t address, uint8_t *data, int size, enum tfa2_cf_mem type);
398 
399 int tfa2_i2c_write_bf(struct i2c_client *client, uint16_t bitfield, uint16_t value);
400 int tfa2_i2c_write_bf_volatile(struct i2c_client *client, uint16_t bitfield, uint16_t value);
401 int tfa2_i2c_read_bf(struct i2c_client *client, uint16_t bitfield);
402 
403 /* i2c abstraction provided by driver */
404 int tfa2_i2c_write_raw(struct i2c_client *client, int len, const uint8_t *data);
405 int tfa2_i2c_write_read_raw(struct i2c_client *client, int wrlen, uint8_t *wrdata, int rdlen, uint8_t *rddata);
406 
407 int tfa2_i2c_set_bf_value(const uint16_t bf, const uint16_t bf_value, uint16_t *p_reg_value);
408 uint16_t tfa2_i2c_get_bf_value(const uint16_t bf, const uint16_t reg_value);
409 
410 /*** MTP support ***/
411 enum tfa_mtp
412 {
413     TFA_MTP_OTC,
414     TFA_MTP_EX,
415     TFA_MTP_R25C,
416     TFA_MTP_F0,
417     TFA_MTP_OPEN
418 };
419 int tfa2_i2c_hap_key2(struct i2c_client *i2c, int lock);
420 
421 int tfa2_i2c_read_reg(struct i2c_client *client, uint8_t reg);
422 int tfa2_i2c_write_reg(struct i2c_client *client, uint8_t reg, uint16_t val);
423 // i2c
424 int tfa2_dev_mtp_get(struct tfa2_device *tfa, enum tfa_mtp item);
425 int tfa2_dev_mtp_set(struct tfa2_device *tfa, enum tfa_mtp item, uint16_t value);
426 int tfa2_i2c_get_revid(struct i2c_client *i2c);
427 // dsp msg for tfa i2c
428 int tfa2_i2c_rpc_write(struct i2c_client *i2c, int length, const char *buffer);
429 // TODO new if
430 // tfa dev
431 int tfa2_dev_start(struct tfa2_device *tfa, int next_profile, int vstep);
432 int tfa2_dev_start_hw(struct tfa2_device *tfa, int profile);
433 int tfa2_dev_stop(struct tfa2_device *tfa); // TODO
434 int tfa2_dev_probe(struct tfa2_device *tfa);
435 int tfa2_set_query_info(struct tfa2_device *tfa);
436 // device specific ops init
437 int tfa2_dev_specific(struct tfa2_device *tfa);
438 int tfa2_dev_clock_stable_wait(struct tfa2_device *tfa);
439 int tfa2_dev_is_fw_cold(struct tfa2_device *tfa);
440 
441 int tfa2_get_noclk(struct tfa2_device *tfa);
442 
443 int tfa2_dev_set_volume(struct tfa2_device *tfa, uint8_t volume);
444 int tfa2_dev_get_volume(struct tfa2_device *tfa);
445 
446 // from tfa_service.h
447 /**
448  * Load the default HW settings in the device
449  * @param tfa the device struct pointer
450  */
451 int tfa2_dev_init(struct tfa2_device *tfa);
452 int tfa2_dev_mute(struct tfa2_device *tfa, int state);
453 
454 // low level, from tfa_init
455 int tfa2_dev_dsp_system_stable(struct tfa2_device *tfa, int *ready);
456 int tfa2_dev_faim_protect(struct tfa2_device *tfa, int state);
457 int tfa2_dev_set_swprofile(struct tfa2_device *tfa, uint16_t new_value);
458 int tfa2_dev_get_swprofile(struct tfa2_device *tfa);
459 int tfa2_dev_set_swvstep(struct tfa2_device *tfa, uint16_t new_value);
460 int tfa2_dev_get_swvstep(struct tfa2_device *tfa);
461 /* mtp */
462 int tfa2_dev_mtp_readpair(struct tfa2_device *tfa, uint16_t mtp_address, uint16_t mtp_data[2]);
463 int tfa2_dev_mtp_read(struct tfa2_device *tfa, uint16_t mtp_address);
464 int tfa2_dev_mtp_write(struct tfa2_device *tfa, uint16_t mtp_address, uint16_t mtp_value);
465 void tfa2_i2c_unlock(struct i2c_client *client);
466 
467 /*
468  * poll for the bf until value or loopcount exhaust return timeout
469  */
470 int tfa2_i2c_bf_poll(struct i2c_client *client, uint16_t bf, uint16_t value, int loop);
471 
472 /*
473  * the dsp init function
474  */
475 int tfa2_dsp_init(struct tfa2_device *tfa, const char *cmd_buf, size_t cmd_len);
476 
477 /*
478  * the dsp execute funtion will execute and the RPC message in the cmd_buf and return the result
479  */
480 int tfa2_dsp_execute(struct tfa2_device *tfa, const char *cmd_buf, size_t cmd_len, char *res_buf, size_t res_len);
481 
482 /**
483  * check the state of the DSP coolflux
484  * @param tfa the device struct pointer
485  * @return the value of CFE
486  */
487 int tfa2_dev_cf_enabled(struct tfa2_device *tfa);
488 
489 /**
490  * start CoolFlux DSP subsystem
491  *  this will load the patch witch will implicitly start the DSP
492  *   if no patch is available the DSP is started immediately
493  * @param tfa the device struct pointer
494  */
495 int tfa2_dev_start_cf(struct tfa2_device *tfa);
496 
497 /**
498  * start DSP firmware
499  * start the clocks and wait until the AMP is switching
500  * on return the DSP sub system will be ready for loading
501  * @param tfa the device struct pointer
502  * @param profile the profile that should be loaded on startup
503  */
504 int tfa2_dev_load_config(struct tfa2_device *tfa, int profile);
505 
506 /**
507  * start the maximus speakerboost algorithm
508  * this implies a full system startup when the system was not already started
509  * @param tfa the device struct pointer
510  * @param profile the profile that should be loaded
511  */
512 int tfa2_dev_start_speakerboost(struct tfa2_device *tfa, int profile);
513 
514 /**
515  * Get hw feature bits from container file
516  * @param tfa the device struct pointer
517  * @param hw_feature_register pointer to where hw features are stored
518  */
519 void tfa2_dev_get_hw_features_from_cnt(struct tfa2_device *tfa, int *hw_feature_register);
520 
521 /**
522  * Get sw feature bits from container file
523  * @param tfa the device struct pointer
524  * @param sw_feature_register pointer to where sw features are stored
525  */
526 void tfa2_dev_get_sw_features_from_cnt(struct tfa2_device *tfa, int sw_feature_register[2]);
527 
528 /**
529  * patch the ROM code of the DSP
530  * @param tfa the device struct pointer
531  * @param patchLength the number of bytes of patchBytes
532  * @param patchBytes pointer to the bytes to patch
533  */
534 int tfa2_dev_dsp_patch(struct tfa2_device *tfa, int patchLength, const uint8_t *patchBytes);
535 
536 /*
537  * update the struct for hw and fw init fields
538  *  if no DSP need_sb_config can be skipped
539  */
540 int tfa2_dev_update_config_init(struct tfa2_device *tfa);
541 
542 /*
543  * firmware RPC calls
544  */
545 
546 int tfa2dsp_fw_get_api_version(struct tfa2_device *tfa, uint8_t *buffer);
547 int tfa2dsp_fw_get_status_change(struct tfa2_device *tfa, uint8_t *buffer);
548 int tfa2dsp_fw_get_re25(struct tfa2_device *tfa, uint8_t *buffer);
549 int tfa2dsp_fw_get_tag(struct tfa2_device *tfa, uint8_t *buffer);
550 
551 /*
552  * set ISTVDDS
553  * clear SBSL and ACS (need clock for ACS)
554  */
555 int tfa2_dev_force_cold(struct tfa2_device *tfa);
556 int tfa2_process_patch_file(struct i2c_client *client, int length, const uint8_t *bytes);
557 int tfa2_check_patch(const uint8_t *data, const int length, const uint16_t revid);
558 
559 void tfa2_set_exttemp(struct tfa2_device *tfa, short ext_temp);
560 short tfa2_get_exttemp(struct tfa2_device *tfa);
561 
562 int tfa2_dev_mtp_busy(struct tfa2_device *tfa);
563 int tfa2_dev_mtp_to_i2c(struct tfa2_device *tfa);
564 int tfa2_dev_mtp_writepair(struct tfa2_device *tfa, uint16_t mtp_address, uint16_t mtp_data[2]);
565 int tfa2_dev_mtp_write(struct tfa2_device *tfa, uint16_t mtp_address, uint16_t mtp_value);
566 
567 int tfa2_dev_status(struct tfa2_device *tfa);
568 
569 /* called during cold start before powering up the device */
570 int tfa2_init_fix_powerup(struct tfa2_device *tfa);
571 /* called before powering down the device */
572 int tfa2_init_fix_powerdown(struct tfa2_device *tfa, int manstate);
573 /* device specifics for mtp access */
574 int tfa2_init_mtp_write_wrapper(struct tfa2_device *tfa,
575                                 uint16_t mtp_address,
576                                 uint16_t mtp_value,
577                                 int (*f)(struct tfa2_device *tfa, uint16_t mtp_address, uint16_t mtp_value));
578 /* called during warm profile switch */
579 int tfa2_init_fix_initcf(struct tfa2_device *tfa);
580 
581 #endif /* TFA2_DEV_H_ */
582