1 /*
2 * Copyright 2019 NXP
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8 #include "tfa2_dev.h"
9 #include "tfa2_haptic.h"
10 #include "tfa2_container.h"
11 #include "tfa_haptic_fw_defs.h"
12 #include <stddef.h>
13
14 static int check_haptic_version = 1;
15
16 /* enable or disable version check */
tfa2_haptic_set_version_check(int check)17 void tfa2_haptic_set_version_check(int check)
18 {
19 check_haptic_version = check;
20 }
21
22 /* print effect object settings to string */
tfa2_haptic_current_effect(struct haptic_data * data,char * str,int len)23 static int tfa2_haptic_current_effect(struct haptic_data *data, char *str, int len)
24 {
25 if (!str)
26 return -EINVAL;
27
28 return snprintf(str, len, "index:%d,\tamplitude:%d,\tduration:%d,\tfrequency:%d", data->index, data->amplitude,
29 data->duration, data->frequency ? data->frequency : -1);
30 }
31
32 /* get pointer to object in cache */
get_obj_cache_ptr(struct haptic_data * data,int idx)33 static struct haptic_tone_object *get_obj_cache_ptr(struct haptic_data *data, int idx)
34 {
35 struct haptic_tone_object *obj;
36
37 if (idx >= (FW_XMEM_NR_OBJECTS1 + FW_XMEM_NR_OBJECTS2))
38 obj = NULL;
39 else if (idx >= FW_XMEM_NR_OBJECTS1)
40 obj = (struct haptic_tone_object *)&data->object_table2_cache[idx - FW_XMEM_NR_OBJECTS1][0];
41 else
42 obj = (struct haptic_tone_object *)&data->object_table1_cache[idx][0];
43
44 return obj;
45 }
46
47 /* get xmem address of object */
get_obj_xmem_addr(int idx)48 static int get_obj_xmem_addr(int idx)
49 {
50 int addr;
51
52 if (idx >= (FW_XMEM_NR_OBJECTS1 + FW_XMEM_NR_OBJECTS2))
53 addr = -1;
54 else if (idx >= FW_XMEM_NR_OBJECTS1)
55 addr = FW_XMEM_GENOBJECTS2 + (idx - FW_XMEM_NR_OBJECTS1) * FW_XMEM_OBJECTSIZE;
56 else
57 addr = FW_XMEM_GENOBJECTS1 + idx * FW_XMEM_OBJECTSIZE;
58
59 return addr;
60 }
61
62 /* get index for CMDOBJSEL0/1 of object */
get_cmdobjsel_index(int idx)63 static int get_cmdobjsel_index(int idx)
64 {
65 int cmdobjsel_idx;
66
67 if (idx >= (FW_XMEM_NR_OBJECTS1 + FW_XMEM_NR_OBJECTS2))
68 cmdobjsel_idx = -1;
69 else if (idx >= FW_XMEM_NR_OBJECTS1)
70 cmdobjsel_idx = idx - FW_XMEM_NR_OBJECTS1 + FW_CMDOBJSEL_TABLE2_OFFFSET;
71 else
72 cmdobjsel_idx = idx;
73
74 return cmdobjsel_idx;
75 }
76
77 /******************************************************************************
78 * haptic front-end functions
79 */
80 /* start resonance */
tfa2_haptic_start(struct tfa2_device * tfa,struct haptic_data * data,int index)81 int tfa2_haptic_start(struct tfa2_device *tfa, struct haptic_data *data, int index)
82 {
83 int rc = -EINVAL; //, virtual_object = index - FW_HB_SEQ_OBJ;
84
85 dev_dbg(&tfa->i2c->dev, "starting obj[%d] of total %d ms\n", index + 1, tfa2_haptic_get_duration(tfa, index));
86
87 if (index < FW_HB_SEQ_OBJ)
88 {
89 rc = tfa2_haptic_start_object(tfa->i2c, data, index);
90 }
91 else if (index < FW_HB_SEQ_OBJ + data->seq_max)
92 {
93 rc = tfa2_hap_sequencer_object(tfa, index + 1);
94 }
95
96 return rc;
97 }
tfa2_haptic_start_object(struct i2c_client * client,struct haptic_data * data,int index)98 int tfa2_haptic_start_object(struct i2c_client *client, struct haptic_data *data, int index)
99 {
100 int rc;
101 char str[256];
102 struct haptic_tone_object *obj;
103 int address;
104 int cmdobjsel_index;
105 int object_changed = 0;
106 int cmdobjsel = FW_XMEM_CMDOBJSEL0;
107 int level;
108 #ifdef MEASURE_START_TIMING
109 ktime_t start_time, stop_time;
110 u64 delta_time;
111
112 start_time = ktime_get_boottime();
113 #endif
114
115 obj = get_obj_cache_ptr(data, index);
116 address = get_obj_xmem_addr(index);
117 cmdobjsel_index = get_cmdobjsel_index(index);
118 if (!obj || (address < 0) || (cmdobjsel_index < 0))
119 {
120 dev_err(&client->dev, "invalid object idx=%d, addr=%d, cmdobjsel_idx=%d\n", index, address, cmdobjsel_index);
121 return -EINVAL;
122 }
123
124 tfa2_haptic_current_effect(data, str, sizeof(str));
125 dev_dbg(&client->dev, "started (%d) %s\n", index + 1, str);
126
127 /* Make sure the DSP is running! */
128
129 /* level for all types */
130 level = (data->amplitude * 0x7fffff + 50) / 100; /* Q1.23, percentage of max */
131 object_changed += (obj->level != level);
132 obj->level = level;
133
134 if ((obj->type == object_tone) || (obj->type == object_silence))
135 {
136 int samples;
137
138 /* duration is in sample count : 48k is 48 samples/ms */
139 samples = data->duration * 48; /* use DSP timer */
140 object_changed += (obj->durationCntMax != samples);
141 obj->durationCntMax = samples;
142
143 if (data->frequency)
144 {
145 int freq = data->frequency << 11; /* Q13.11 */
146 object_changed += (obj->freq != freq);
147 obj->freq = freq;
148 }
149 }
150
151 if (object_changed > 0)
152 {
153 /* write parameters */
154 rc = tfa2_i2c_write_cf_mem32(client, address, (int32_t *)obj, FW_XMEM_OBJECTSIZE, TFA2_CF_MEM_XMEM);
155 if (rc < 0)
156 return rc;
157 }
158
159 /* to start write cmdObjSel = index */
160 rc = tfa2_i2c_write_cf_mem32(client, cmdobjsel, (int32_t *)&cmdobjsel_index, 1, TFA2_CF_MEM_XMEM);
161
162 #ifdef MEASURE_START_TIMING
163 stop_time = ktime_get_boottime();
164 delta_time = ktime_to_ns(ktime_sub(stop_time, start_time));
165 do_div(delta_time, 1000);
166 dev_dbg(&client->dev, "tfa_haptic_start duration = %lld us (%lld )\n", delta_time, delta_time + 900);
167 #endif
168 return rc;
169 }
170
171 /* stop resonance */
tfa2_haptic_stop(struct tfa2_device * tfa,struct haptic_data * data,int index)172 int tfa2_haptic_stop(struct tfa2_device *tfa, struct haptic_data *data, int index)
173 {
174 int stop_obj = FW_HB_STOP_OBJ; /* first table */
175 int cmdobjsel = FW_XMEM_CMDOBJSEL0;
176
177 return tfa2_i2c_write_cf_mem32(tfa->i2c, cmdobjsel, (int32_t *)&stop_obj, 1, TFA2_CF_MEM_XMEM);
178 }
179
180 /* duration in msecs of current object */
tfa2_haptic_get_duration(struct tfa2_device * tfa,int index)181 int tfa2_haptic_get_duration(struct tfa2_device *tfa, int index)
182 {
183 int total_duration;
184 struct haptic_data *data = &tfa->hap_data;
185 /* error if too high */
186 if (index > FW_HB_SEQ_OBJ + data->seq_max)
187 return -EINVAL;
188
189 if (index < FW_HB_SEQ_OBJ)
190 {
191 /* normal object */
192 struct haptic_tone_object *obj = get_obj_cache_ptr(data, index);
193 /* duration is in sample count : 48k is 48 samples/ms */
194 int duration = obj->durationCntMax / 48;
195
196 if (obj->type != object_wave)
197 {
198 /* take into account that duration might get adjusted in tfa2_haptic_start_object() */
199 duration = data->duration ? data->duration : duration;
200 if (obj->boostBrakeOn)
201 duration += obj->boostLength / 48;
202 }
203 total_duration = duration + data->delay_attack; /* add object hold time */
204 }
205 else
206 { /* sequencer */
207 int rc;
208 struct tfa2_sequence seq;
209 rc = tfa2_hap_cnt_get_sequencer(tfa->cnt, tfa->dev_idx, index - FW_HB_SEQ_OBJ, &seq);
210 if (rc < 0)
211 {
212 pr_err("No sequencer\n");
213 return -EINVAL;
214 }
215 total_duration = tfa2_hap_get_sequencer_duration(tfa, &seq);
216 }
217 return total_duration;
218 }
219
220 /* update the duration in msecs for the time spend in boost break */
tfa2_haptic_update_duration(struct haptic_data * data,int duration)221 int tfa2_haptic_update_duration(struct haptic_data *data, int duration)
222 {
223 struct haptic_tone_object *obj = get_obj_cache_ptr(data, data->index);
224 int boost_length = 0;
225
226 data->duration = duration;
227
228 if ((obj->type == object_tone) && (obj->boostBrakeOn == 1))
229 boost_length = (obj->boostLength / 48);
230
231 if (boost_length >= duration)
232 {
233 pr_err("%s: boost break length (%d) is longer then duration (%d)", __func__, boost_length, duration);
234 return -EINVAL;
235 }
236
237 /* substract boost brake length from user provided duration */
238 data->duration -= boost_length;
239
240 return 0;
241 }
242
tfa2_haptic_object_type(struct haptic_data * data,int index)243 enum tfa_haptic_object_type tfa2_haptic_object_type(struct haptic_data *data, int index)
244 {
245 struct haptic_tone_object *obj = get_obj_cache_ptr(data, index);
246 return obj->type ? object_tone : object_wave;
247 }
248
249 /*
250 * extract the effect settings from the negative input value
251 * byte[2/3] is the frequency of object (if non-0)
252 * byte[1] is index of object
253 * byte[0] is the amplitude % from level
254 * return 0 if to be ignored for playback
255 */
tfa2_haptic_parse_value(struct haptic_data * data,int value)256 int tfa2_haptic_parse_value(struct haptic_data *data, int value)
257 {
258 uint32_t xvalue;
259 int level;
260
261 if (value < 0)
262 {
263 xvalue = value * -1;
264 data->index = ((xvalue >> 8) & 0xff) - 1; /* get byte[1] */
265 level = xvalue & 0xff; /* get byte[0] */
266 if (level < 4)
267 data->amplitude = level * 33;
268 else
269 data->amplitude = level;
270 data->frequency = xvalue >> 16; /* freq */
271 }
272
273 return (value > 0) ? 1 : 0;
274 }
275
tfa2_haptic_read_r0(struct i2c_client * client,int * p_value)276 int tfa2_haptic_read_r0(struct i2c_client *client, int *p_value)
277 {
278 return tfa2_i2c_read_cf_mem32(client, FW_XMEM_R0, p_value, 1, TFA2_CF_MEM_XMEM);
279 }
280
tfa2_haptic_read_f0(struct i2c_client * client,int * p_value)281 int tfa2_haptic_read_f0(struct i2c_client *client, int *p_value)
282 {
283 return tfa2_i2c_read_cf_mem32(client, FW_XMEM_F0, p_value, 1, TFA2_CF_MEM_XMEM);
284 }
285
tfa2_haptic_read_sampcnt0(struct i2c_client * client,int * p_value)286 int tfa2_haptic_read_sampcnt0(struct i2c_client *client, int *p_value)
287 {
288 return tfa2_i2c_read_cf_mem32(client, FW_XMEM_SAMPCNT0, p_value, 1, TFA2_CF_MEM_XMEM);
289 }
tfa2_haptic_read_recalc_selector(struct i2c_client * client,int * p_value)290 int tfa2_haptic_read_recalc_selector(struct i2c_client *client, int *p_value)
291 {
292 return tfa2_i2c_read_cf_mem32(client, FW_XMEM_RECALCSEL, p_value, 1, TFA2_CF_MEM_XMEM);
293 }
tfa2_haptic_write_recalc_selector(struct i2c_client * client,int value)294 int tfa2_haptic_write_recalc_selector(struct i2c_client *client, int value)
295 {
296 return tfa2_i2c_write_cf_mem32(client, FW_XMEM_RECALCSEL, (int32_t *)&value, 1, TFA2_CF_MEM_XMEM);
297 }
298
tfa2_haptic_disable_f0_trc(struct i2c_client * client,int disable)299 int tfa2_haptic_disable_f0_trc(struct i2c_client *client, int disable)
300 {
301 int disable_f0_trc = (disable != 0);
302 return tfa2_i2c_write_cf_mem32(client, FW_XMEM_DISF0TRC, (int32_t *)&disable_f0_trc, 1, TFA2_CF_MEM_XMEM);
303 }
304
tfa2_haptic_obj0_set(struct i2c_client * client,int objnr)305 int tfa2_haptic_obj0_set(struct i2c_client *client, int objnr)
306 {
307 return tfa2_i2c_write_cf_mem32(client, FW_XMEM_CMDOBJSEL0, (int32_t *)&objnr, 1, TFA2_CF_MEM_XMEM);
308 }
309
tfa2_haptic_obj0_wait_finish(struct i2c_client * client)310 int tfa2_haptic_obj0_wait_finish(struct i2c_client *client)
311 {
312 int rc, loop = 50, ready = 0, sampcnt0;
313
314 do
315 {
316 rc = tfa2_haptic_read_sampcnt0(client, &sampcnt0);
317 if (rc < 0)
318 return rc;
319 ready = sampcnt0 <= 0;
320 if (ready == 1)
321 break;
322 msleep_interruptible(50); /* wait to avoid busload */ // TODO decrease time?...
323 } while (loop--);
324
325 if (sampcnt0 > 0)
326 return -ETIMEDOUT;
327
328 return 0;
329 }
tfa2_haptic_calibrate_wait(struct i2c_client * client)330 int tfa2_haptic_calibrate_wait(struct i2c_client *client)
331 {
332 int loop = 50, ready = 0, sampcnt0;
333 int f0;
334
335 do
336 {
337 tfa2_haptic_read_sampcnt0(client, &sampcnt0);
338 tfa2_haptic_read_f0(client, &f0);
339 ready = (sampcnt0 <= 0) && (f0 != 0);
340 if (ready == 1)
341 break;
342 msleep_interruptible(50); /* wait to avoid busload */ // TODO decrease time?...
343 } while (loop--);
344
345 if (sampcnt0 > 0)
346 return -ETIMEDOUT;
347
348 return 0;
349 }
350
tfa2_haptic_recalculate_wait(struct tfa2_device * tfa,int object)351 int tfa2_haptic_recalculate_wait(struct tfa2_device *tfa, int object)
352 {
353 int loop, ready = 0, sampcnt0 = 0;
354 int recalc_selector;
355 int manstate = tfa2_i2c_read_bf(tfa->i2c, tfa->bf_manstate);
356
357 loop = manstate == 6 ? 250 : 50; /* extend loop if in initcf */
358
359 if (object >= 0)
360 {
361 int ms, duration;
362
363 /* get this object duration from xmem */
364 tfa2_i2c_read_cf_mem32(
365 tfa->i2c,
366 FW_XMEM_GENOBJECTS1 + object * FW_XMEM_OBJECTSIZE + offsetof(struct haptic_tone_object, durationCntMax) / 4,
367 &duration, 1, TFA2_CF_MEM_XMEM);
368 /* duration is in sample count : 48k is 48 samples/ms */
369 ms = duration / 48;
370 if (ms > 0 && ms < 1000000)
371 {
372 ms += manstate == 6 ? ms : 0; /* extend time if in initcf */
373 msleep_interruptible(ms); /* wait for obj to finish */
374 }
375 else
376 return -EINVAL;
377 }
378
379 do
380 {
381 if (object >= 0)
382 {
383 tfa2_haptic_read_sampcnt0(tfa->i2c, &sampcnt0);
384 }
385 tfa2_haptic_read_recalc_selector(tfa->i2c, &recalc_selector);
386 ready = (sampcnt0 <= 0) && (recalc_selector < 0);
387 if (ready == 1)
388 break;
389 } while (loop--);
390
391 if (sampcnt0 > 0)
392 return -ETIMEDOUT;
393
394 return 0;
395 }
396
397 /* handy wrappers */
get_hap_profile(struct tfa2_device * tfa,char * string)398 static int get_hap_profile(struct tfa2_device *tfa, char *string)
399 {
400 // TODO limit to use .hap names only
401 return tfa2_cnt_grep_profile_name(tfa->cnt, tfa->dev_idx, string);
402 }
403
tfa2_hap_patch_version(struct tfa2_device * tfa,char * string)404 int tfa2_hap_patch_version(struct tfa2_device *tfa, char *string)
405 {
406 int rc, fw_version;
407
408 /* this is called after patch load, keep the dsp in reset */
409 rc = tfa2_i2c_read_cf_mem32_dsp_reset(tfa->i2c, FW_XMEM_VERSION, &fw_version, 1, TFA2_CF_MEM_XMEM);
410 dev_dbg(&tfa->i2c->dev, "%s patch version %d.%d.%d\n", string, (fw_version >> 16) & 0xff, (fw_version >> 8) & 0xff,
411 fw_version & 0xff);
412
413 if (check_haptic_version && ((fw_version & FW_VERSION_MASK) != FW_VERSION))
414 {
415 dev_err(&tfa->i2c->dev, "%s: unsupported firmware version 0x%x, expected 0x%x\n", __func__, fw_version,
416 FW_VERSION);
417 return -EINVAL;
418 }
419
420 return rc;
421 }
422
423 /*
424 * execute recalculation, assume data is loaded
425 */
tfa2_hap_recalc(struct tfa2_device * tfa,int object)426 static int tfa2_hap_recalc(struct tfa2_device *tfa, int object)
427 {
428 int rc, f0, f0mtp, fresout;
429
430 /* assume that the recalculation is been loaded */
431
432 /* if F0 == 0 assume not calibrated */
433 f0mtp = tfa2_dev_mtp_get(tfa, TFA_MTP_F0); /* raw */
434 f0 = f0mtp / 2 + 80; /* start at 80 */
435 dev_dbg(&tfa->i2c->dev, "%s:F0 MTP:%d, F0:%d\n", __func__, f0mtp, f0);
436
437 /* allowed range is between 80 and 336 Hz */
438 if ((80 < f0) && (f0 < 336))
439 {
440 int recalc_obj = -1;
441 if (tfa->hap_data.recalc_play_object)
442 {
443 /* read recalc object id from data patch */
444 tfa2_i2c_read_cf_mem32(tfa->i2c, FW_XMEM_CMDOBJSEL0, &recalc_obj, 1, TFA2_CF_MEM_XMEM);
445 }
446 else
447 recalc_obj = object;
448
449 tfa2_i2c_write_bf(tfa->i2c, TFA9XXX_BF_RST, 0);
450 /* Go to power on state */
451 rc = tfa2_dev_set_state(tfa, TFA_STATE_POWERUP);
452 if (rc < 0)
453 return rc;
454
455 msleep_interruptible(1);
456 dev_dbg(&tfa->i2c->dev, "%s: manstate:%d\n", __func__, tfa2_i2c_read_bf(tfa->i2c, tfa->bf_manstate));
457
458 if (recalc_obj >= 0)
459 {
460 /* Loading startup Object ID */
461 tfa2_haptic_obj0_set(tfa->i2c, recalc_obj);
462 dev_dbg(&tfa->i2c->dev, "recalculation startup object id: %d\n", recalc_obj);
463 }
464
465 /* trigger recalculation */
466 tfa2_haptic_write_recalc_selector(tfa->i2c, 1);
467
468 /* wait for recalculation to finish */
469 rc = tfa2_haptic_recalculate_wait(tfa, recalc_obj);
470 if (rc < 0)
471 {
472 dev_err(&tfa->i2c->dev, "Error, recalculation did not finish\n");
473 return rc;
474 }
475
476 rc = tfa2_haptic_read_f0(tfa->i2c, &fresout);
477 if (rc < 0)
478 {
479 dev_err(&tfa->i2c->dev, "Error reading f0\n");
480 return rc;
481 }
482 dev_info(&tfa->i2c->dev, "recalculation %s\n", (fresout == 0) ? "NOT done!" : "done");
483
484 /* stop DSP */
485 tfa2_i2c_write_bf(tfa->i2c, TFA9XXX_BF_RST, 1);
486 }
487 else
488 {
489 dev_dbg(&tfa->i2c->dev, "Warning: f0:%d not in 80/336 Hz range, cannot recalculate\n", f0);
490 /* no error return, in case of 1st time calibration it would fail */
491 }
492
493 return 0;
494 }
495
tfa2_hap_load_data(struct tfa2_device * tfa)496 int tfa2_hap_load_data(struct tfa2_device *tfa)
497 {
498 int rc, hbprofile;
499 char profile_name[32];
500
501 strcpy(profile_name, (tfa->need_hb_config & TFA_HB_ROLE_MASK) == tfa_hb_lra ? "lra" : "ls");
502 strcat(profile_name, "_data.hap");
503
504 hbprofile = get_hap_profile(tfa, profile_name); /* get "*_data.hap" */
505 if (hbprofile < 0)
506 {
507 dev_err(&tfa->i2c->dev, "No [%s] profile found\n", profile_name);
508 return -EINVAL;
509 }
510 rc = tfa2_cnt_write_files_profile(tfa, hbprofile, 0); /* write patch from profile*/
511 if (rc < 0)
512 return rc;
513
514 /* print and check version */
515 return tfa2_hap_patch_version(tfa, "data");
516 }
517
tfa2_hap_load_recalc(struct tfa2_device * tfa)518 int tfa2_hap_load_recalc(struct tfa2_device *tfa)
519 {
520 int rc, hbprofile;
521
522 hbprofile = get_hap_profile(tfa, "lra_recalculation.hap");
523 rc = tfa2_cnt_write_files_profile(tfa, hbprofile, 0); /* write recalc patch*/
524 if (rc < 0)
525 return rc;
526
527 /* print and check version */
528 rc = tfa2_hap_patch_version(tfa, "recalculation");
529
530 return rc;
531 }
532
tfa2_haptic_obj_get(struct haptic_data * data,int idx)533 void tfa2_haptic_obj_get(struct haptic_data *data, int idx)
534 {
535 struct haptic_tone_object *obj = get_obj_cache_ptr(data, idx);
536
537 if (obj == NULL)
538 return;
539
540 /* default tone effect */
541 data->index = idx;
542 data->amplitude = (100 * obj->level + 0x3fffff) / 0x7fffff;
543 data->duration = obj->durationCntMax / 48;
544 data->frequency = obj->freq >> 11; /* Q13.11 */
545 }
546
tfa2_haptic_obj_get_duration(struct haptic_data * data,int idx)547 void tfa2_haptic_obj_get_duration(struct haptic_data *data, int idx)
548 {
549 struct haptic_tone_object *obj = get_obj_cache_ptr(data, idx);
550
551 if (obj == NULL)
552 return;
553 data->duration = obj->durationCntMax / 48;
554 if (obj->boostBrakeOn)
555 data->duration += obj->boostLength / 48;
556 }
557
558 /* settings for obj[0] */
tfa2_haptic_obj_get_defaults(struct haptic_data * data)559 static void tfa2_haptic_obj_get_defaults(struct haptic_data *data)
560 {
561 tfa2_haptic_obj_get(data, 0);
562 }
563
564 /*
565 * front-end recalculation function
566 * return if recalculation has been done already
567 * load data patch and recalculate
568 */
tfa2_hap_recalculate(struct tfa2_device * tfa,int object)569 int tfa2_hap_recalculate(struct tfa2_device *tfa, int object)
570 {
571 int rc;
572
573 tfa2_dev_update_config_init(tfa);
574
575 /* silent return if not relevant */
576 if (tfa->need_hb_config != tfa_hb_lra)
577 return 0;
578
579 if (tfa->need_hw_init)
580 {
581 rc = tfa2_dev_start_hw(tfa, 0);
582 if (rc < 0)
583 return rc;
584 }
585
586 rc = tfa2_hap_load_data(tfa);
587 if (rc < 0)
588 return rc;
589
590 rc = tfa2_hap_load_recalc(tfa);
591 if (rc < 0)
592 return rc;
593
594 rc = tfa2_dev_set_state(tfa, TFA_STATE_OPERATING);
595 if (rc < 0)
596 return rc;
597 /* make sure we are in 9 */
598 rc = tfa2_i2c_bf_poll(tfa->i2c, tfa->bf_manstate, 9, 50);
599 if (rc < 0)
600 {
601 dev_err(&tfa->i2c->dev, "Error, waiting for operating state in recalculation\n");
602 return rc;
603 }
604
605 rc = tfa2_hap_recalc(tfa, object);
606 if (rc < 0)
607 return rc;
608
609 return rc;
610 }
611
612 /*
613 * load and activate hapticboost
614 * if entered cold a full fw boot + lra recalculation will be done
615 */
tfa2_dev_start_hapticboost(struct tfa2_device * tfa)616 int tfa2_dev_start_hapticboost(struct tfa2_device *tfa)
617 {
618 int rc;
619 char profile_name[32];
620
621 strcpy(profile_name, (tfa->need_hb_config & TFA_HB_ROLE_MASK) == tfa_hb_lra ? "lra" : "ls");
622 dev_dbg(&tfa->i2c->dev, "%s: cold starting as %s\n", __FUNCTION__, profile_name);
623
624 /* cold start, assume RST=1 */
625 // tfa2_i2c_write_bf(tfa->i2c, TFA9XXX_BF_RST, 1); //TODO optimize RST ,fix patches
626
627 /* load data patch first */
628 rc = tfa2_hap_load_data(tfa); /* will cache object table */
629 if (rc < 0)
630 {
631 dev_err(&tfa->i2c->dev, "Error loading data patch\n");
632 return rc;
633 }
634
635 if ((tfa->need_hb_config & TFA_HB_ROLE_MASK) == tfa_hb_lra)
636 {
637 rc = tfa2_hap_load_recalc(tfa);
638 if (rc < 0)
639 return rc;
640 /* run recalculation */
641 rc = tfa2_hap_recalc(tfa, -1); /* driver mode, no object */
642 }
643
644 /* no error check on xmem reads, the current rc will be returned */
645 /* cache object tables */
646 tfa2_i2c_read_cf_mem32(tfa->i2c, FW_XMEM_GENOBJECTS1, (int *)tfa->hap_data.object_table1_cache,
647 FW_XMEM_OBJECTSIZE * FW_XMEM_NR_OBJECTS1, TFA2_CF_MEM_XMEM);
648 tfa2_i2c_read_cf_mem32(tfa->i2c, FW_XMEM_GENOBJECTS2, (int *)tfa->hap_data.object_table2_cache,
649 FW_XMEM_OBJECTSIZE * FW_XMEM_NR_OBJECTS2, TFA2_CF_MEM_XMEM);
650 /* cache delay_attack */
651 tfa2_i2c_read_cf_mem32(tfa->i2c, FW_XMEM_DELAY_ATTACK_SMP, &tfa->hap_data.delay_attack, 1, TFA2_CF_MEM_XMEM);
652 tfa->hap_data.delay_attack /= 48; /* make it milliseconds */
653
654 /* get max nr of sequencer virtual objects */
655 tfa->hap_data.seq_max = tfa2_hap_cnt_sequencer_count(tfa->cnt, tfa->dev_idx);
656
657 /* get the defaults */
658 tfa2_haptic_obj_get_defaults(&tfa->hap_data);
659
660 return rc; /* recalc rc in case of lra */
661 }
662
tfa2_hap_calibrate(struct tfa2_device * tfa)663 int tfa2_hap_calibrate(struct tfa2_device *tfa)
664 {
665 int f0, r0;
666 uint16_t mtp_f0;
667 int rc, ret, current_profile;
668 int range[4];
669
670 /* Update init state */
671 tfa2_dev_update_config_init(tfa);
672
673 if (!tfa->need_hb_config)
674 {
675 dev_err(&tfa->i2c->dev, "HB calibration not done, no config found in container\n");
676 return -EINVAL;
677 }
678
679 mtp_f0 = tfa2_dev_mtp_get(tfa, TFA_MTP_F0);
680
681 dev_dbg(&tfa->i2c->dev, "Current MTP F0:%d\n", mtp_f0);
682
683 /* if not 0, wipe it */
684 if (mtp_f0)
685 {
686 /* Go to the powerup state to allow MTP writing*/
687 rc = tfa2_dev_set_state(tfa, TFA_STATE_POWERUP);
688 if (rc < 0)
689 return rc;
690
691 /* Clear f0 in MTP */
692 rc = tfa2_dev_mtp_set(tfa, TFA_MTP_F0, 0);
693 if (rc < 0)
694 {
695 dev_err(&tfa->i2c->dev, "Error clearing F0 in MTP\n");
696 return rc;
697 }
698 /* turn off again, so we are always enter with pll off */
699 rc = tfa2_dev_set_state(tfa, TFA_STATE_POWERDOWN);
700 if (rc < 0)
701 return rc;
702 }
703
704 current_profile = tfa2_dev_get_swprofile(tfa);
705 if (current_profile < 0)
706 {
707 /* no current profile, take the 1st profile */
708 current_profile = 0;
709 }
710
711 /* cold run of calibration profile */
712 rc = tfa2_calibrate_profile_start(tfa);
713 if (rc < 0)
714 {
715 dev_err(&tfa->i2c->dev, "Error, calibration profile start\n");
716 return rc;
717 }
718
719 /* Trigger calibration */
720 tfa2_haptic_obj0_set(tfa->i2c, FW_HB_CAL_OBJ);
721
722 msleep_interruptible(1000 + tfa->hap_data.delay_attack); // TODO determine optimal delay
723
724 /* wait for calibration to finish */
725 rc = tfa2_haptic_obj0_wait_finish(tfa->i2c);
726 if (rc < 0)
727 {
728 dev_err(&tfa->i2c->dev, "Error, calibration did not finish\n");
729 goto reload_profile;
730 }
731
732 rc = tfa2_haptic_read_f0(tfa->i2c, &f0);
733 if (rc < 0)
734 {
735 dev_err(&tfa->i2c->dev, "Error reading f0\n");
736 goto reload_profile;
737 }
738 dev_dbg(&tfa->i2c->dev, "F0 = %d.%03d Hz (0x%x)\n", TFA2_HAPTIC_FP_INT(f0, FW_XMEM_F0_SHIFT),
739 TFA2_HAPTIC_FP_FRAC(f0, FW_XMEM_F0_SHIFT), f0);
740
741 rc = tfa2_haptic_read_r0(tfa->i2c, &r0);
742 if (rc < 0)
743 {
744 dev_err(&tfa->i2c->dev, "Error reading r0\n");
745 goto reload_profile;
746 }
747 dev_dbg(&tfa->i2c->dev, "R0 = %d.%03d ohm (0x%x)\n", TFA2_HAPTIC_FP_INT(r0, FW_XMEM_R0_SHIFT),
748 TFA2_HAPTIC_FP_FRAC(r0, FW_XMEM_R0_SHIFT), r0);
749
750 /* Check F0/R0 ranges */
751 rc = tfa2_i2c_read_cf_mem32(tfa->i2c, FW_XMEM_F0_R0_RANGES, range, 4, TFA2_CF_MEM_XMEM);
752 if (rc < 0)
753 {
754 dev_err(&tfa->i2c->dev, "Error reading F0/R0 ranges\n");
755 goto reload_profile;
756 }
757
758 rc = 0;
759 if ((f0 < range[3]) || (f0 > range[2]))
760 {
761 dev_err(&tfa->i2c->dev, "F0 out of range: 0x%06x < 0x%06x < 0x%06x\n", range[3], f0, range[2]);
762 rc = -ERANGE;
763 }
764 if ((r0 < range[1]) || (r0 > range[0]))
765 {
766 dev_err(&tfa->i2c->dev, "R0 out of range: 0x%06x < 0x%06x < 0x%06x\n", range[1], r0, range[0]);
767 rc = -ERANGE;
768 }
769 if (rc != 0)
770 {
771 dev_err(&tfa->i2c->dev, "range check failed, result not written\n");
772 goto reload_profile;
773 }
774
775 /* 16 bit f0 value to store in F0 MTP location
776 * Encoding:
777 * MTPValue = round (( f0 - 80 Hz ) * 2 )
778 * f0 [in Hz] = MTPValue / 2 + 80 Hz
779 * */
780
781 /* convert F0 to Hz */
782 f0 = TFA2_HAPTIC_FP_INT(f0, FW_XMEM_F0_SHIFT) + (TFA2_HAPTIC_FP_FRAC(f0, FW_XMEM_F0_SHIFT) > 499 ? 1 : 0);
783 mtp_f0 = (f0 - 80) * 2;
784 dev_dbg(&tfa->i2c->dev, "MTPF0 = %d (0x%x)\n", mtp_f0 / 2 + 80, mtp_f0);
785
786 // TODO check F0 for non-zero; always make no sense if the FW uses MTP
787
788 /* Store f0 in MTP */
789 rc = tfa2_dev_mtp_set(tfa, TFA_MTP_F0, mtp_f0);
790 if (rc < 0)
791 {
792 dev_err(&tfa->i2c->dev, "Error writing F0 to MTP\n");
793 goto reload_profile;
794 }
795
796 /* TODO if calibrate always: Store f0 in shadow register */
797 // tfa2_i2c_write_bf(tfa->i2c, TFA9XXX_BF_CUSTINFO, mtp_f0);
798
799 reload_profile:
800 /* force a cold start to do recalculation */
801 ret = tfa2_dev_force_cold(tfa);
802 if (ret < 0)
803 {
804 dev_err(&tfa->i2c->dev, "%s: force cold failed\n", __func__);
805 return ret;
806 }
807
808 /* re-load current profile */
809 ret = tfa2_dev_start(tfa, current_profile, 0);
810 if (ret < 0)
811 {
812 dev_err(&tfa->i2c->dev, "%s: Cannot reload profile %d\n", __func__, current_profile);
813 return ret;
814 }
815
816 return rc;
817 }
818
819 /*
820 * free the sequencer
821 *
822 */
tfa2_hap_sequencer_destroy(struct tfa2_sequence * seq)823 void tfa2_hap_sequencer_destroy(struct tfa2_sequence *seq)
824 {
825 kfree(seq->object);
826 kfree(seq->duration);
827 kfree(seq->level);
828 kfree(seq->freq);
829 }
830 /*
831 * run the sequencer
832 *
833 */
tfa2_hap_sequencer(struct tfa2_device * tfa,struct tfa2_sequence * seq)834 int tfa2_hap_sequencer(struct tfa2_device *tfa, struct tfa2_sequence *seq)
835 {
836 int rc, i, idx, this_duration, total_duration = 0;
837 int save_duration, save_amplitude, save_frequency;
838
839 if (seq == 0 || seq->object == 0)
840 return -EINVAL;
841
842 /* save default object */
843 save_duration = tfa->hap_data.duration;
844 save_amplitude = tfa->hap_data.amplitude;
845 save_frequency = tfa->hap_data.frequency;
846
847 for (i = 0; i < seq->length; i++)
848 {
849 idx = seq->object[i] - 1; /* lists starts counting at 1 */
850 /* get current settings */
851 tfa2_haptic_obj_get(&tfa->hap_data, idx);
852 /* adjust according to table */
853 if (seq->duration && seq->duration[i])
854 tfa->hap_data.duration = seq->duration[i];
855 /* adjust according to table */
856 if (seq->level && seq->level[i])
857 tfa->hap_data.amplitude = seq->level[i];
858 /* adjust according to table */
859 if (seq->freq && seq->freq[i])
860 tfa->hap_data.frequency = seq->freq[i];
861
862 rc = tfa2_haptic_start_object(tfa->i2c, &tfa->hap_data, idx);
863 if (rc < 0)
864 {
865 total_duration = rc; /* return value */
866 break;
867 }
868 this_duration = tfa2_haptic_get_duration(tfa, idx);
869 total_duration += this_duration;
870 /* sleep while buzzing */
871 msleep_interruptible(this_duration);
872 /* obj should be finished by now */
873 rc = tfa2_haptic_obj0_wait_finish(tfa->i2c);
874 if (rc < 0)
875 {
876 total_duration = rc; /* return value */
877 break;
878 }
879 }
880
881 /* restore default object */
882 tfa->hap_data.duration = save_duration;
883 tfa->hap_data.amplitude = save_amplitude;
884 tfa->hap_data.frequency = save_frequency;
885
886 return total_duration;
887 }
tfa2_hap_get_sequencer_duration(struct tfa2_device * tfa,struct tfa2_sequence * seq)888 int tfa2_hap_get_sequencer_duration(struct tfa2_device *tfa, struct tfa2_sequence *seq)
889 {
890 int i, idx, this_duration, total_duration = 0;
891 struct haptic_data *data = &tfa->hap_data;
892
893 if (seq == 0 || seq->object == 0)
894 return -EINVAL;
895
896 for (i = 0; i < seq->length; i++)
897 {
898 idx = seq->object[i] - 1; /* lists starts counting at 1 */
899 /* get current settings */
900 tfa2_haptic_obj_get_duration(data, idx);
901
902 /* adjust according to table */
903 if (seq->duration && seq->duration[i])
904 data->duration = seq->duration[i];
905
906 this_duration = tfa2_haptic_get_duration(tfa, idx);
907 total_duration += this_duration;
908 }
909 seq->total_duration = total_duration;
910
911 return total_duration;
912 }
tfa2_hap_save_haptic_cache(struct tfa2_device * tfa,struct tfa2_sequence * seq)913 void tfa2_hap_save_haptic_cache(struct tfa2_device *tfa, struct tfa2_sequence *seq)
914 {
915 memcpy(&seq->object_table1_cache_save, &tfa->hap_data.object_table1_cache,
916 sizeof(tfa->hap_data.object_table1_cache));
917 memcpy(&seq->object_table2_cache_save, &tfa->hap_data.object_table2_cache,
918 sizeof(tfa->hap_data.object_table2_cache));
919 }
920
tfa2_hap_test_restore_haptic_cache(struct tfa2_device * tfa,struct tfa2_sequence * seq)921 void tfa2_hap_test_restore_haptic_cache(struct tfa2_device *tfa, struct tfa2_sequence *seq)
922 {
923 if (memcmp(&tfa->hap_data.object_table1_cache, &seq->object_table1_cache_save,
924 sizeof(tfa->hap_data.object_table1_cache)))
925 {
926 memcpy(&tfa->hap_data.object_table1_cache, &seq->object_table1_cache_save,
927 sizeof(tfa->hap_data.object_table1_cache));
928 tfa2_i2c_write_cf_mem32(tfa->i2c, FW_XMEM_GENOBJECTS1, (int32_t *)tfa->hap_data.object_table1_cache,
929 FW_XMEM_NR_OBJECTS1 * FW_XMEM_OBJECTSIZE, TFA2_CF_MEM_XMEM);
930 }
931
932 if (memcmp(&tfa->hap_data.object_table2_cache, &seq->object_table2_cache_save,
933 sizeof(tfa->hap_data.object_table2_cache)))
934 {
935 memcpy(&tfa->hap_data.object_table2_cache, &seq->object_table2_cache_save,
936 sizeof(tfa->hap_data.object_table2_cache));
937 tfa2_i2c_write_cf_mem32(tfa->i2c, FW_XMEM_GENOBJECTS2, (int32_t *)tfa->hap_data.object_table2_cache,
938 FW_XMEM_NR_OBJECTS2 * FW_XMEM_OBJECTSIZE, TFA2_CF_MEM_XMEM);
939 }
940 }
941
942 /*
943 * run a sequencer virtual object
944 */
tfa2_hap_sequencer_object(struct tfa2_device * tfa,int virtual_obj)945 int tfa2_hap_sequencer_object(struct tfa2_device *tfa, int virtual_obj)
946 {
947 // sequencer
948 struct tfa2_sequence seq;
949 int dev_idx = tfa->dev_idx;
950 int rc;
951
952 tfa2_hap_save_haptic_cache(tfa, &seq);
953 rc = tfa2_hap_cnt_get_sequencer(tfa->cnt, dev_idx, virtual_obj - FW_HB_SEQ_OBJ - 1, &seq);
954 if (rc < 0)
955 {
956 pr_err("No sequencer\n");
957 return -EINVAL;
958 }
959 rc = tfa2_hap_sequencer(tfa, &seq);
960 tfa2_hap_test_restore_haptic_cache(tfa, &seq);
961 tfa2_hap_sequencer_destroy(&seq);
962 if (rc < 0)
963 {
964 pr_err("Sequencer play error:%d\n", rc);
965 return rc;
966 }
967
968 return 0;
969 }
970
971 /*
972 * return the nr of sequencers
973 */
tfa2_hap_cnt_sequencer_count(nxpTfaContainer_t * cnt,int devidx)974 int tfa2_hap_cnt_sequencer_count(nxpTfaContainer_t *cnt, int devidx)
975 {
976 int rc, total = 0;
977
978 do
979 {
980 rc = tfa2_cnt_grep_nth_profile_name(cnt, devidx, total, ".seq.hap");
981
982 if (rc == -EINVAL)
983 {
984 rc = 0;
985 break;
986 }
987 total++;
988 } while (rc >= 0);
989
990 return rc ? rc : total; /* return error or count */
991 }
992
993 /*
994 * find the nth sequencer in the container
995 * for each array type
996 * allocate 32bit buffer
997 * convert 24 to 32 bits into seq struct
998 */
tfa2_hap_cnt_get_sequencer(nxpTfaContainer_t * cnt,int devidx,int seqidx,struct tfa2_sequence * seq)999 int tfa2_hap_cnt_get_sequencer(nxpTfaContainer_t *cnt, int devidx, int seqidx, struct tfa2_sequence *seq)
1000 {
1001 int i, rc, seq_profidx;
1002 uint8_t *array24;
1003 int length24, length32, type;
1004
1005 seq_profidx = tfa2_cnt_grep_nth_profile_name(cnt, devidx, seqidx, ".seq.hap");
1006 if (seq_profidx < 0)
1007 {
1008 pr_err("can't find sequencer profile .seq.hap\n");
1009 return -EINVAL;
1010 }
1011
1012 seq->length = 0;
1013 seq->object = 0;
1014 seq->duration = 0;
1015 seq->level = 0;
1016 seq->freq = 0;
1017 seq->total_duration = 0;
1018
1019 /* there may be 4 different arrays in a sequencer profile */
1020 for (i = 0; i < 4; i++)
1021 {
1022 rc = tfa2_cnt_get_cmd(cnt, devidx, seq_profidx, i, &array24, &length24);
1023 if (rc < 0)
1024 {
1025 break; /* we are done, no error report here */
1026 }
1027 /* get the array type, 1 24bit word */
1028 type = (((array24[0] << 16) + (array24[1] << 8) + array24[2]) << 8) >> 8;
1029 length32 = length24 + length24 / 3 - 4;
1030 if (seq->length == 0)
1031 seq->length = length32 / sizeof(int32_t);
1032
1033 switch (type)
1034 {
1035 case 0:
1036 seq->object = (int32_t *)kmalloc(length32, GFP_KERNEL);
1037 tfa2_24_to_32(seq->object, array24 + 3, length24 - 3);
1038 break;
1039 case 1:
1040 seq->duration = (int32_t *)kmalloc(length32, GFP_KERNEL);
1041 tfa2_24_to_32(seq->duration, array24 + 3, length24 - 3);
1042 break;
1043 case 2:
1044 seq->level = (int32_t *)kmalloc(length32, GFP_KERNEL);
1045 tfa2_24_to_32(seq->level, array24 + 3, length24 - 3);
1046 break;
1047 case 3:
1048 seq->freq = (int32_t *)kmalloc(length32, GFP_KERNEL);
1049 tfa2_24_to_32(seq->freq, array24 + 3, length24 - 3);
1050 break;
1051 default:
1052 return -EINVAL;
1053 }
1054 }
1055
1056 return 0;
1057 }
1058