1 // SPDX-License-Identifier: GPL-2.0+
2
3 #include <linux/module.h>
4 #include <asm/ioctls.h>
5 #include <asm/uaccess.h>
6 #include <linux/delay.h>
7 #include <linux/slab.h>
8 #include <linux/fs.h>
9 #include <linux/miscdevice.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/of.h>
13 #include <sound/core.h>
14 #include <sound/pcm.h>
15 #include <sound/pcm_params.h>
16 #include <sound/soc.h>
17
18 #include "aw_device.h"
19 #include "aw_log.h"
20 #include "aw_calib.h"
21 #include "aw883xx.h"
22
23
24 static bool is_single_cali; /*if mutli_dev cali false, single dev true*/
25 static unsigned int g_cali_re_time = AW_CALI_RE_DEFAULT_TIMER;
26 static unsigned int g_dev_select;
27 static struct miscdevice *g_misc_dev;
28 static unsigned int g_msic_wr_flag = CALI_STR_NONE;
29 static DEFINE_MUTEX(g_cali_lock);
30 static const char *cali_str[CALI_STR_MAX] = {"none", "start_cali", "cali_re",
31 "cali_f0", "store_re", "show_re", "show_r0", "show_cali_f0", "show_f0",
32 "show_te", "dev_sel", "get_ver", "get_re_range"
33 };
34
35
36 /******************************cali re store example start***********************************/
37 #ifdef AW_CALI_STORE_EXAMPLE
38 /*write cali to persist file example*/
39 #define AWINIC_CALI_FILE "/mnt/vendor/persist/factory/audio/aw_cali.bin"
40 #define AW_INT_DEC_DIGIT (10)
41
aw_fs_read(struct file * file,char * buf,size_t count,loff_t * pos)42 static void aw_fs_read(struct file *file, char *buf, size_t count, loff_t *pos)
43 {
44 #ifdef AW_KERNEL_VER_OVER_5_4_0
45 kernel_read(file, buf, count, pos);
46 #else
47 vfs_read(file, buf, count, pos);
48 #endif
49 }
50
aw_fs_write(struct file * file,char * buf,size_t count,loff_t * pos)51 static void aw_fs_write(struct file *file, char *buf, size_t count, loff_t *pos)
52 {
53 #ifdef AW_KERNEL_VER_OVER_5_4_0
54 kernel_write(file, buf, count, pos);
55 #else
56 vfs_write(file, buf, count, pos);
57 #endif
58 }
59
aw_cali_write_cali_re_to_file(int32_t cali_re,int channel)60 static int aw_cali_write_cali_re_to_file(int32_t cali_re, int channel)
61 {
62 struct file *fp = NULL;
63 char buf[50] = { 0 };
64 loff_t pos = 0;
65 mm_segment_t fs;
66
67 fp = filp_open(AWINIC_CALI_FILE, O_RDWR | O_CREAT, 0644);
68 if (IS_ERR(fp)) {
69 aw_pr_err("channel:%d open %s failed!",
70 channel, AWINIC_CALI_FILE);
71 return -EINVAL;
72 }
73
74 pos = AW_INT_DEC_DIGIT * channel;
75
76 snprintf(buf, sizeof(buf), "%10d", cali_re);
77
78 fs = get_fs();
79 set_fs(KERNEL_DS);
80
81 aw_fs_write(fp, buf, strlen(buf), &pos);
82
83 set_fs(fs);
84
85 aw_pr_info("channel:%d buf:%s cali_re:%d",
86 channel, buf, cali_re);
87
88 filp_close(fp, NULL);
89 return 0;
90 }
91
aw_cali_get_cali_re_from_file(int32_t * cali_re,int channel)92 static int aw_cali_get_cali_re_from_file(int32_t *cali_re, int channel)
93 {
94 struct file *fp = NULL;
95 int f_size;
96 char *buf = NULL;
97 int32_t int_cali_re = 0;
98 loff_t pos = 0;
99 mm_segment_t fs;
100
101 fp = filp_open(AWINIC_CALI_FILE, O_RDONLY, 0);
102 if (IS_ERR(fp)) {
103 aw_pr_err("channel:%d open %s failed!",
104 channel, AWINIC_CALI_FILE);
105 return -EINVAL;
106 }
107
108 pos = AW_INT_DEC_DIGIT * channel;
109
110 f_size = AW_INT_DEC_DIGIT;
111
112 buf = kzalloc(f_size + 1, GFP_ATOMIC);
113 if (!buf) {
114 aw_pr_err("channel:%d malloc mem %d failed!", channel, f_size);
115 filp_close(fp, NULL);
116 return -EINVAL;
117 }
118
119 fs = get_fs();
120 set_fs(KERNEL_DS);
121
122 aw_fs_read(fp, buf, f_size, &pos);
123
124 set_fs(fs);
125
126 if (sscanf(buf, "%d", &int_cali_re) == 1)
127 *cali_re = int_cali_re;
128 else
129 *cali_re = AW_ERRO_CALI_RE_VALUE;
130
131 aw_pr_info("channel:%d buf:%s int_cali_re: %d",
132 channel, buf, int_cali_re);
133
134 kfree(buf);
135 buf = NULL;
136 filp_close(fp, NULL);
137
138 return 0;
139 }
140 #endif
141 /********************cali re store example end*****************************/
142
143
144 /*custom need add to set/get cali_re form/to nv*/
aw_cali_write_re_to_nvram(int32_t cali_re,int32_t channel)145 static int aw_cali_write_re_to_nvram(int32_t cali_re, int32_t channel)
146 {
147 #ifdef AW_CALI_STORE_EXAMPLE
148 return aw_cali_write_cali_re_to_file(cali_re, channel);
149 #else
150 return 0;
151 #endif
152 }
aw_cali_read_re_from_nvram(int32_t * cali_re,int32_t channel)153 static int aw_cali_read_re_from_nvram(int32_t *cali_re, int32_t channel)
154 {
155 /*custom add, if success return value is 0 , else -1*/
156 #ifdef AW_CALI_STORE_EXAMPLE
157 return aw_cali_get_cali_re_from_file(cali_re, channel);
158 #else
159 return 0;
160 #endif
161 }
162
aw_run_mute_for_cali(struct aw_device * aw_dev,int8_t cali_result)163 static void aw_run_mute_for_cali(struct aw_device *aw_dev, int8_t cali_result)
164 {
165 struct aw_mute_desc *mute_desc = &aw_dev->mute_desc;
166
167 aw_dev_dbg(aw_dev->dev, "enter");
168 if (aw_dev->cali_desc.cali_check_st) {
169 if (cali_result == CALI_RESULT_ERROR) {
170 aw_dev->ops.aw_reg_write_bits(aw_dev, mute_desc->reg,
171 mute_desc->mask, mute_desc->enable);
172 } else if (cali_result == CALI_RESULT_NORMAL) {
173 aw_dev->ops.aw_reg_write_bits(aw_dev, mute_desc->reg,
174 mute_desc->mask,
175 mute_desc->disable);
176 }
177 } else {
178 aw_dev_info(aw_dev->dev, "cali check disable");
179 }
180 aw_dev_info(aw_dev->dev, "done");
181 }
182
aw_cali_svc_get_dev_re_range(struct aw_device * aw_dev,uint32_t * data_buf)183 static int aw_cali_svc_get_dev_re_range(struct aw_device *aw_dev,
184 uint32_t *data_buf)
185 {
186 data_buf[RE_MIN_FLAG] = aw_dev->re_range.re_min;
187 data_buf[RE_MAX_FLAG] = aw_dev->re_range.re_max;
188
189 return 0;
190 }
191
aw_cali_svc_get_devs_re_range(struct aw_device * aw_dev,uint32_t * data_buf,int num)192 static int aw_cali_svc_get_devs_re_range(struct aw_device *aw_dev,
193 uint32_t *data_buf, int num)
194 {
195 struct list_head *dev_list = NULL;
196 struct list_head *pos = NULL;
197 struct aw_device *local_dev = NULL;
198 int ret, cnt = 0;
199
200 /*get dev list*/
201 ret = aw_dev_get_list_head(&dev_list);
202 if (ret) {
203 aw_dev_err(aw_dev->dev, "get dev list failed");
204 return ret;
205 }
206
207 list_for_each(pos, dev_list) {
208 local_dev = container_of(pos, struct aw_device, list_node);
209 if (local_dev->channel < num) {
210 data_buf[RE_MIN_FLAG + local_dev->channel * 2] =
211 local_dev->re_range.re_min;
212 data_buf[RE_MAX_FLAG + local_dev->channel * 2] =
213 local_dev->re_range.re_max;
214 cnt++;
215 } else {
216 aw_dev_err(local_dev->dev, "channel num[%d] overflow buf num[%d]",
217 local_dev->channel, num);
218 return -EINVAL;
219 }
220 }
221
222 return cnt;
223 }
224
225
aw_cali_store_cali_re(struct aw_device * aw_dev,int32_t re)226 static int aw_cali_store_cali_re(struct aw_device *aw_dev, int32_t re)
227 {
228 int ret;
229
230 if ((re > aw_dev->re_range.re_min) && (re < aw_dev->re_range.re_max)) {
231 aw_dev->cali_desc.cali_re = re;
232 ret = aw_cali_write_re_to_nvram(re, aw_dev->channel);
233 if (ret < 0) {
234 aw_dev_err(aw_dev->dev, "write re to nvram failed!");
235 return ret;
236 }
237 } else {
238 aw_dev_err(aw_dev->dev, "invalid cali re %d!", re);
239 return -EINVAL;
240 }
241
242 return 0;
243 }
244
aw_cali_get_cali_re(struct aw_cali_desc * cali_desc)245 int aw_cali_get_cali_re(struct aw_cali_desc *cali_desc)
246 {
247 int ret;
248 int32_t cali_re = 0;
249 struct aw_device *aw_dev =
250 container_of(cali_desc, struct aw_device, cali_desc);
251
252 ret = aw_cali_read_re_from_nvram(&cali_re, aw_dev->channel);
253 if (ret < 0) {
254 cali_desc->cali_re = AW_ERRO_CALI_RE_VALUE;
255 cali_desc->cali_result = CALI_RESULT_NONE;
256 aw_dev_err(aw_dev->dev, "get re failed");
257 return ret;
258 }
259
260 if (cali_re < aw_dev->re_range.re_min || cali_re > aw_dev->re_range.re_max) {
261 aw_dev_err(aw_dev->dev,
262 "out range re value: %d", cali_re);
263 cali_desc->cali_re = AW_ERRO_CALI_RE_VALUE;
264 /*cali_result is error when aw-cali-check enable*/
265 if (aw_dev->cali_desc.cali_check_st) {
266 cali_desc->cali_result = CALI_RESULT_ERROR;
267 }
268 return -EINVAL;
269 }
270 cali_desc->cali_re = cali_re;
271
272 /*cali_result is normal when aw-cali-check enable*/
273 if (aw_dev->cali_desc.cali_check_st) {
274 cali_desc->cali_result = CALI_RESULT_NORMAL;
275 }
276
277 aw_dev_info(aw_dev->dev, "get cali re %d", cali_desc->cali_re);
278
279 return 0;
280 }
281
aw_cali_read_cali_re_from_dsp(struct aw_cali_desc * cali_desc,uint32_t * re)282 int aw_cali_read_cali_re_from_dsp(struct aw_cali_desc *cali_desc, uint32_t *re)
283 {
284 struct aw_device *aw_dev =
285 container_of(cali_desc, struct aw_device, cali_desc);
286 struct aw_adpz_re_desc *desc = &aw_dev->adpz_re_desc;
287 int ret;
288
289 ret = aw_dev->ops.aw_dsp_read(aw_dev, desc->dsp_reg, re, desc->data_type);
290 if (ret < 0) {
291 aw_dev_err(aw_dev->dev, "set cali re error");
292 return ret;
293 }
294
295 *re = AW_DSP_RE_TO_SHOW_RE(*re, desc->shift);
296 *re -= aw_dev->cali_desc.ra;
297
298 aw_dev_info(aw_dev->dev, "get dsp re:%d", *re);
299
300 return 0;
301 }
302
303
304 /*************Calibration base function************/
aw_cali_svc_set_cali_re_to_dsp(struct aw_cali_desc * cali_desc)305 int aw_cali_svc_set_cali_re_to_dsp(struct aw_cali_desc *cali_desc)
306 {
307 struct aw_device *aw_dev =
308 container_of(cali_desc, struct aw_device, cali_desc);
309 struct aw_adpz_re_desc *adpz_re_desc = &aw_dev->adpz_re_desc;
310 uint32_t cali_re = 0;
311 int ret;
312
313 cali_re = AW_SHOW_RE_TO_DSP_RE((aw_dev->cali_desc.cali_re +
314 aw_dev->cali_desc.ra), adpz_re_desc->shift);
315
316 /* set cali re to aw883xx */
317 ret = aw_dev->ops.aw_dsp_write(aw_dev,
318 adpz_re_desc->dsp_reg, cali_re, adpz_re_desc->data_type);
319 if (ret < 0) {
320 aw_dev_err(aw_dev->dev, "set cali re error");
321 return ret;
322 }
323
324 ret = aw_dev_modify_dsp_cfg(aw_dev, adpz_re_desc->dsp_reg,
325 cali_re, adpz_re_desc->data_type);
326 if (ret < 0) {
327 aw_dev_err(aw_dev->dev, "modify dsp cfg failed");
328 return ret;
329 }
330
331 return 0;
332 }
333
aw_cali_svc_get_ra(struct aw_cali_desc * cali_desc)334 int aw_cali_svc_get_ra(struct aw_cali_desc *cali_desc)
335 {
336 int ret;
337 uint32_t dsp_ra;
338 struct aw_device *aw_dev =
339 container_of(cali_desc, struct aw_device, cali_desc);
340 struct aw_ra_desc *desc = &aw_dev->ra_desc;
341
342 ret = aw_dev->ops.aw_dsp_read(aw_dev, desc->dsp_reg,
343 &dsp_ra, desc->data_type);
344 if (ret < 0) {
345 aw_dev_err(aw_dev->dev, "read ra error");
346 return ret;
347 }
348
349 cali_desc->ra = AW_DSP_RE_TO_SHOW_RE(dsp_ra,
350 aw_dev->adpz_re_desc.shift);
351 aw_dev_info(aw_dev->dev, "get ra:%d", cali_desc->ra);
352 return 0;
353 }
354
aw_cali_svc_get_dev_realtime_re(struct aw_device * aw_dev,uint32_t * re)355 static int aw_cali_svc_get_dev_realtime_re(struct aw_device *aw_dev,
356 uint32_t *re)
357 {
358 int ret;
359 struct aw_adpz_re_desc *re_desc = &aw_dev->adpz_re_desc;
360 struct aw_ra_desc *ra_desc = &aw_dev->ra_desc;
361 struct aw_adpz_t0_desc *t0_desc = &aw_dev->t0_desc;
362 uint32_t dsp_re = 0;
363 uint32_t show_re = 0;
364 uint32_t re_cacl = 0;
365 uint32_t ra = 0;
366 uint32_t t0 = 0;
367 int32_t te = 0;
368 int32_t te_cacl = 0;
369 uint32_t coil_alpha = 0;
370 uint16_t pst_rpt = 0;
371
372 ret = aw_dev->ops.aw_dsp_read(aw_dev, re_desc->dsp_reg, &dsp_re, re_desc->data_type);
373 if (ret < 0) {
374 aw_dev_err(aw_dev->dev, "dsp read re error");
375 return ret;
376 }
377
378 ret = aw_dev->ops.aw_dsp_read(aw_dev, ra_desc->dsp_reg, &ra, ra_desc->data_type);
379 if (ret < 0) {
380 aw_dev_err(aw_dev->dev, "dsp read ra error");
381 return ret;
382 }
383
384 re_cacl = dsp_re - ra;
385
386 ret = aw_dev->ops.aw_dsp_read(aw_dev, t0_desc->dsp_reg, &t0, t0_desc->data_type);
387 if (ret < 0) {
388 aw_dev_err(aw_dev->dev, "dsp read t0 error");
389 return ret;
390 }
391
392 ret = aw_dev->ops.aw_dsp_read(aw_dev, t0_desc->coilalpha_reg, &coil_alpha, t0_desc->coil_type);
393 if (ret < 0) {
394 aw_dev_err(aw_dev->dev, "dsp read coil_alpha error");
395 return ret;
396 }
397
398 ret = aw_dev->ops.aw_reg_read(aw_dev, aw_dev->spkr_temp_desc.reg, &pst_rpt);
399 if (ret < 0) {
400 aw_dev_err(aw_dev->dev, "reg read pst_rpt error");
401 return ret;
402 }
403
404 te = (int32_t)((uint32_t)pst_rpt - t0);
405
406 te_cacl = AW_TE_CACL_VALUE(te, (uint16_t)coil_alpha);
407
408 show_re = AW_RE_REALTIME_VALUE((int32_t)re_cacl, te_cacl);
409
410 *re = AW_DSP_RE_TO_SHOW_RE(show_re, re_desc->shift);
411 aw_dev_dbg(aw_dev->dev, "real_r0:[%d]", *re);
412
413 return 0;
414 }
415
aw_cali_svc_get_dev_re(struct aw_device * aw_dev,uint32_t * re)416 static int aw_cali_svc_get_dev_re(struct aw_device *aw_dev,
417 uint32_t *re)
418 {
419 int ret;
420 struct aw_ste_re_desc *desc = &aw_dev->ste_re_desc;
421 uint32_t dsp_re = 0;
422 uint32_t show_re = 0;
423
424 ret = aw_dev->ops.aw_dsp_read(aw_dev, desc->dsp_reg, &dsp_re, desc->data_type);
425 if (ret < 0) {
426 aw_dev_err(aw_dev->dev, "dsp read re error");
427 return ret;
428 }
429
430 show_re = AW_DSP_RE_TO_SHOW_RE(dsp_re,
431 aw_dev->ste_re_desc.shift);
432
433 *re = show_re - aw_dev->cali_desc.ra;
434 aw_dev_dbg(aw_dev->dev, "real_r0:[%d]", *re);
435
436 return 0;
437 }
438
aw_cali_svc_get_devs_r0(struct aw_device * aw_dev,int32_t * r0_buf,int num)439 static int aw_cali_svc_get_devs_r0(struct aw_device *aw_dev, int32_t *r0_buf, int num)
440 {
441 struct list_head *dev_list = NULL;
442 struct list_head *pos = NULL;
443 struct aw_device *local_dev = NULL;
444 int ret, cnt = 0;
445
446 //get dev list
447 ret = aw_dev_get_list_head(&dev_list);
448 if (ret) {
449 aw_dev_err(aw_dev->dev, "get dev list failed");
450 return ret;
451 }
452
453 list_for_each (pos, dev_list) {
454 local_dev = container_of(pos, struct aw_device, list_node);
455 if (local_dev->channel < num) {
456 ret = aw_cali_svc_get_dev_realtime_re(local_dev, &r0_buf[local_dev->channel]);
457 if (ret) {
458 aw_dev_err(local_dev->dev, "get r0 failed!");
459 return ret;
460 }
461 cnt++;
462 } else {
463 aw_dev_err(aw_dev->dev, "channel num[%d] overflow buf num[%d] ",
464 local_dev->channel, num);
465 }
466 }
467 return cnt;
468 }
469
aw_cali_svc_get_dev_f0(struct aw_device * aw_dev,uint32_t * f0)470 static int aw_cali_svc_get_dev_f0(struct aw_device *aw_dev,
471 uint32_t *f0)
472 {
473 struct aw_f0_desc *f0_desc = &aw_dev->f0_desc;
474 uint32_t dsp_val = 0;
475 int ret;
476
477 ret = aw_dev->ops.aw_dsp_read(aw_dev,
478 f0_desc->dsp_reg, &dsp_val, f0_desc->data_type);
479 if (ret < 0) {
480 aw_dev_err(aw_dev->dev, "read f0 failed");
481 return ret;
482 }
483
484 *f0 = dsp_val >> f0_desc->shift;
485 aw_dev_dbg(aw_dev->dev, "real_f0:[%d]", *f0);
486
487 return 0;
488 }
489
aw_cali_svc_get_devs_f0(struct aw_device * aw_dev,int32_t * f0_buf,int num)490 static int aw_cali_svc_get_devs_f0(struct aw_device *aw_dev, int32_t *f0_buf, int num)
491 {
492 struct list_head *dev_list = NULL;
493 struct list_head *pos = NULL;
494 struct aw_device *local_dev = NULL;
495 int ret, cnt = 0;
496
497 //get dev list
498 ret = aw_dev_get_list_head(&dev_list);
499 if (ret) {
500 aw_dev_err(aw_dev->dev, "get dev list failed");
501 return ret;
502 }
503
504 list_for_each (pos, dev_list) {
505 local_dev = container_of(pos, struct aw_device, list_node);
506 if (local_dev->channel < num) {
507 ret = aw_cali_svc_get_dev_f0(local_dev, &f0_buf[local_dev->channel]);
508 if (ret) {
509 aw_dev_err(local_dev->dev, "get f0 failed!");
510 return ret;
511 }
512 cnt++;
513 } else {
514 aw_dev_err(aw_dev->dev, "channel num[%d] overflow buf num[%d] ",
515 local_dev->channel, num);
516 }
517 }
518 return cnt;
519 }
520
aw_cali_svc_get_dev_q(struct aw_device * aw_dev,uint32_t * q)521 static int aw_cali_svc_get_dev_q(struct aw_device *aw_dev,
522 uint32_t *q)
523 {
524 struct aw_q_desc *q_desc = &aw_dev->q_desc;
525 uint32_t dsp_val = 0;
526 int ret;
527
528 ret = aw_dev->ops.aw_dsp_read(aw_dev,
529 q_desc->dsp_reg, &dsp_val, q_desc->data_type);
530 if (ret < 0) {
531 aw_dev_err(aw_dev->dev, "read q failed");
532 return ret;
533 }
534
535 *q = ((dsp_val * 1000) >> q_desc->shift);
536
537 return 0;
538 }
539
aw_cali_svc_get_dev_te(struct aw_cali_desc * cali_desc,int32_t * te)540 int aw_cali_svc_get_dev_te(struct aw_cali_desc *cali_desc, int32_t *te)
541 {
542 struct aw_device *aw_dev =
543 container_of(cali_desc, struct aw_device, cali_desc);
544 uint16_t reg_val = 0;
545 int ret;
546
547 ret = aw_dev->ops.aw_reg_read(aw_dev, aw_dev->spkr_temp_desc.reg, ®_val);
548 if (ret < 0) {
549 aw_dev_err(aw_dev->dev, "read temperature failed");
550 return ret;
551 }
552
553 *te = (int32_t)((int16_t)reg_val);
554 aw_dev_info(aw_dev->dev, "real_te:[%d]", *te);
555
556 return 0;
557 }
558
aw_cali_svc_get_devs_te(struct aw_device * aw_dev,int32_t * te_buf,int num)559 static int aw_cali_svc_get_devs_te(struct aw_device *aw_dev, int32_t *te_buf, int num)
560 {
561 struct list_head *dev_list = NULL;
562 struct list_head *pos = NULL;
563 struct aw_device *local_dev = NULL;
564 int ret, cnt = 0;
565
566 //get dev list
567 ret = aw_dev_get_list_head(&dev_list);
568 if (ret) {
569 aw_dev_err(aw_dev->dev, "get dev list failed");
570 return ret;
571 }
572
573 list_for_each (pos, dev_list) {
574 local_dev = container_of(pos, struct aw_device, list_node);
575 if (local_dev->channel < num) {
576 ret = aw_cali_svc_get_dev_te(&local_dev->cali_desc, &te_buf[local_dev->channel]);
577 if (ret) {
578 aw_dev_err(local_dev->dev, "get temperature failed!");
579 return ret;
580 }
581 cnt++;
582 } else {
583 aw_dev_err(aw_dev->dev, "channel num[%d] overflow buf num[%d]",
584 local_dev->channel, num);
585 }
586 }
587 return cnt;
588 }
589
aw_cali_svc_bubble_sort(uint32_t * data,int data_size)590 static void aw_cali_svc_bubble_sort(uint32_t *data, int data_size)
591 {
592 int loop_num = data_size - 1;
593 uint16_t temp_store = 0;
594 int i;
595 int j;
596
597 if (data == NULL) {
598 aw_pr_err("data is NULL");
599 return;
600 }
601
602 for (i = 0; i < loop_num; i++) {
603 for (j = 0; j < loop_num - i; j++) {
604 if (data[j] > data[j + 1]) {
605 temp_store = data[j];
606 data[j] = data[j + 1];
607 data[j + 1] = temp_store;
608 }
609 }
610 }
611 }
612
aw_cali_svc_del_max_min_ave_algo(uint32_t * data,int data_size)613 static int aw_cali_svc_del_max_min_ave_algo(uint32_t *data, int data_size)
614 {
615 int sum = 0;
616 int ave = 0;
617 int i = 0;
618
619 aw_cali_svc_bubble_sort(data, data_size);
620 for (i = 1; i < data_size - 1; i++)
621 sum += data[i];
622
623 if ((data_size - AW_CALI_DATA_SUM_RM) == 0) {
624 aw_pr_err("data_size id :%d less than 2", data_size);
625 return -EINVAL;
626 }
627
628 ave = sum / (data_size - AW_CALI_DATA_SUM_RM);
629
630 return ave;
631 }
632
aw_cali_svc_set_cali_status(struct aw_device * aw_dev,bool status)633 static void aw_cali_svc_set_cali_status(struct aw_device *aw_dev, bool status)
634 {
635 aw_dev->cali_desc.status = status;
636
637 if (status)
638 aw_monitor_stop(&aw_dev->monitor_desc);
639 else
640 aw_monitor_start(&aw_dev->monitor_desc);
641
642 aw_dev_info(aw_dev->dev, "cali %s",
643 (status == 0) ? ("disable") : ("enable"));
644 }
645
aw_cali_svc_get_cali_status(struct aw_cali_desc * cali_desc)646 bool aw_cali_svc_get_cali_status(struct aw_cali_desc *cali_desc)
647 {
648 return cali_desc->status;
649 }
650
aw_cali_svc_cali_init_check(struct aw_device * aw_dev)651 static int aw_cali_svc_cali_init_check(struct aw_device *aw_dev)
652 {
653 int ret;
654
655 aw_dev_dbg(aw_dev->dev, "enter");
656
657 ret = aw_dev_sysst_check(aw_dev);
658 if (ret < 0) {
659 aw_dev_err(aw_dev->dev, "syst_check failed");
660 return ret;
661 }
662
663 ret = aw_dev_get_dsp_status(aw_dev);
664 if (ret < 0) {
665 aw_dev_err(aw_dev->dev, "dsp status error");
666 return ret;
667 }
668
669 ret = aw_dev_get_hmute(aw_dev);
670 if (ret == 1) {
671 aw_dev_err(aw_dev->dev, "mute staus");
672 return -EINVAL;
673 }
674
675 return 0;
676 }
677
aw_cali_svc_get_cali_cfg(struct aw_device * aw_dev)678 static int aw_cali_svc_get_cali_cfg(struct aw_device *aw_dev)
679 {
680 int ret;
681 struct aw_cali_cfg_desc *desc = &aw_dev->cali_cfg_desc;
682 struct cali_cfg *cali_cfg = &aw_dev->cali_desc.cali_cfg;
683
684 aw_dev_dbg(aw_dev->dev, "enter");
685
686 ret = aw_dev->ops.aw_dsp_read(aw_dev,
687 desc->actampth_reg, &cali_cfg->data[0], desc->actampth_data_type);
688 if (ret < 0) {
689 aw_dev_err(aw_dev->dev, "dsp read reg0x%x error", desc->actampth_reg);
690 return ret;
691 }
692
693 ret = aw_dev->ops.aw_dsp_read(aw_dev,
694 desc->noiseampth_reg, &cali_cfg->data[1], desc->noiseampth_data_type);
695 if (ret < 0) {
696 aw_dev_err(aw_dev->dev, "dsp read reg0x%x error", desc->noiseampth_reg);
697 return ret;
698 }
699
700 ret = aw_dev->ops.aw_dsp_read(aw_dev,
701 desc->ustepn_reg, &cali_cfg->data[2], desc->ustepn_data_type);
702 if (ret < 0) {
703 aw_dev_err(aw_dev->dev, "dsp read reg0x%x error", desc->ustepn_reg);
704 return ret;
705 }
706
707 ret = aw_dev->ops.aw_dsp_read(aw_dev,
708 desc->alphan_reg, &cali_cfg->data[3], desc->alphan_data_type);
709 if (ret < 0) {
710 aw_dev_err(aw_dev->dev, "dsp read reg0x%x error", desc->alphan_reg);
711 return ret;
712 }
713
714 return 0;
715 }
716
aw_cali_svc_set_cali_cfg(struct aw_device * aw_dev,struct cali_cfg cali_cfg)717 static int aw_cali_svc_set_cali_cfg(struct aw_device *aw_dev,
718 struct cali_cfg cali_cfg)
719 {
720 int ret;
721 struct aw_cali_cfg_desc *desc = &aw_dev->cali_cfg_desc;
722
723 aw_dev_dbg(aw_dev->dev, "enter");
724
725 ret = aw_dev->ops.aw_dsp_write(aw_dev,
726 desc->actampth_reg, cali_cfg.data[0], desc->actampth_data_type);
727 if (ret < 0) {
728 aw_dev_err(aw_dev->dev, "dsp write reg0x%x error", desc->actampth_reg);
729 return ret;
730 }
731
732 ret = aw_dev->ops.aw_dsp_write(aw_dev,
733 desc->noiseampth_reg, cali_cfg.data[1], desc->noiseampth_data_type);
734 if (ret < 0) {
735 aw_dev_err(aw_dev->dev, "dsp write reg0x%x error", desc->noiseampth_reg);
736 return ret;
737 }
738
739 ret = aw_dev->ops.aw_dsp_write(aw_dev,
740 desc->ustepn_reg, cali_cfg.data[2], desc->ustepn_data_type);
741 if (ret < 0) {
742 aw_dev_err(aw_dev->dev, "dsp write reg0x%x error", desc->ustepn_reg);
743 return ret;
744 }
745
746 ret = aw_dev->ops.aw_dsp_write(aw_dev,
747 desc->alphan_reg, cali_cfg.data[3], desc->alphan_data_type);
748 if (ret < 0) {
749 aw_dev_err(aw_dev->dev, "dsp write reg0x%x error", desc->alphan_reg);
750 return ret;
751 }
752
753 return 0;
754 }
755
aw_cali_svc_get_smooth_cali_re(struct aw_device * aw_dev)756 static int aw_cali_svc_get_smooth_cali_re(struct aw_device *aw_dev)
757 {
758 int ret = 0;
759 int i = 0;
760 uint32_t re_temp[AW_CALI_READ_CNT_MAX] = { 0 };
761 uint32_t dsp_re;
762
763 aw_dev_dbg(aw_dev->dev, "enter");
764
765 for (i = 0; i < AW_CALI_READ_CNT_MAX; i++) {
766 ret = aw_cali_svc_get_dev_re(aw_dev, &re_temp[i]);
767 if (ret < 0)
768 goto cali_re_fail;
769
770 msleep(30);/*delay 30 ms*/
771 }
772 dsp_re = aw_cali_svc_del_max_min_ave_algo(re_temp,
773 AW_CALI_READ_CNT_MAX);
774
775 if (aw_dev->ops.aw_cali_svc_get_iv_st) {
776 ret = aw_dev->ops.aw_cali_svc_get_iv_st(aw_dev);
777 if (ret < 0) {
778 aw_dev_err(aw_dev->dev,
779 "get iv data failed");
780 goto cali_re_fail;
781 }
782 }
783
784 if (dsp_re < aw_dev->re_range.re_min || dsp_re > aw_dev->re_range.re_max) {
785 aw_dev_err(aw_dev->dev,
786 "out range re value: [%d]mohm", dsp_re);
787 aw_dev->cali_desc.cali_re = dsp_re;
788 if (aw_dev->cali_desc.cali_check_st) {
789 aw_dev->cali_desc.cali_result = CALI_RESULT_ERROR;
790 ret = aw_cali_write_re_to_nvram(dsp_re, aw_dev->channel);
791 if (ret < 0) {
792 aw_dev_err(aw_dev->dev, "write re failed");
793 }
794 }
795 aw_run_mute_for_cali(aw_dev, aw_dev->cali_desc.cali_result);
796 return 0;
797 }
798
799 ret = aw_cali_write_re_to_nvram(dsp_re, aw_dev->channel);
800 if (ret < 0) {
801 aw_dev_err(aw_dev->dev, "write re failed");
802 goto cali_re_fail;
803 }
804
805 if (aw_dev->cali_desc.cali_check_st)
806 aw_dev->cali_desc.cali_result = CALI_RESULT_NORMAL;
807
808 aw_dev->cali_desc.cali_re = dsp_re;
809 aw_dev_info(aw_dev->dev, "re[%d]mohm", aw_dev->cali_desc.cali_re);
810
811 aw_dev_dsp_enable(aw_dev, false);
812 aw_cali_svc_set_cali_re_to_dsp(&aw_dev->cali_desc);
813 aw_dev_dsp_enable(aw_dev, true);
814
815 return 0;
816
817 cali_re_fail:
818 if (aw_dev->cali_desc.cali_check_st)
819 aw_dev->cali_desc.cali_result = CALI_RESULT_ERROR;
820 aw_run_mute_for_cali(aw_dev, aw_dev->cali_desc.cali_result);
821 return -EINVAL;
822 }
823
aw_cali_svc_cali_en(struct aw_device * aw_dev,bool cali_en)824 static int aw_cali_svc_cali_en(struct aw_device *aw_dev, bool cali_en)
825 {
826 int ret = 0;
827 struct cali_cfg set_cfg;
828
829 aw_dev_info(aw_dev->dev, "cali_en:%d", cali_en);
830
831 aw_dev_dsp_enable(aw_dev, false);
832 if (cali_en) {
833 ret = aw_cali_svc_get_cali_cfg(aw_dev);
834 if (ret < 0) {
835 aw_dev_err(aw_dev->dev, "get cali cfg failed");
836 aw_dev_dsp_enable(aw_dev, true);
837 return ret;
838 }
839 set_cfg.data[0] = 0;
840 set_cfg.data[1] = 0;
841 set_cfg.data[2] = -1;
842 set_cfg.data[3] = 1;
843
844 ret = aw_cali_svc_set_cali_cfg(aw_dev, set_cfg);
845 if (ret < 0) {
846 aw_dev_err(aw_dev->dev, "set cali cfg failed");
847 aw_cali_svc_set_cali_cfg(aw_dev, aw_dev->cali_desc.cali_cfg);
848 aw_dev_dsp_enable(aw_dev, true);
849 return ret;
850 }
851 } else {
852 aw_cali_svc_set_cali_cfg(aw_dev, aw_dev->cali_desc.cali_cfg);
853 }
854 aw_dev_dsp_enable(aw_dev, true);
855
856 return 0;
857 }
858
aw_cali_svc_cali_run_dsp_vol(struct aw_device * aw_dev,int type,bool enable)859 static int aw_cali_svc_cali_run_dsp_vol(struct aw_device *aw_dev,
860 int type, bool enable)
861 {
862 int ret;
863 uint16_t reg_val = 0;
864 uint16_t set_vol = 0;
865 struct aw_dsp_vol_desc *desc = &aw_dev->dsp_vol_desc;
866
867 aw_dev_info(aw_dev->dev, "type:%d, enable:%d", type, enable);
868
869 if (enable) {
870 /*set dsp vol*/
871 if (type == CALI_TYPE_RE) {
872 set_vol = desc->mute_st;
873 } else if (type == CALI_TYPE_F0) {
874 set_vol = desc->noise_st;
875 } else {
876 aw_dev_err(aw_dev->dev, "type:%d unsupported", type);
877 return -EINVAL;
878 }
879
880 ret = aw_dev->ops.aw_reg_read(aw_dev,
881 desc->reg, ®_val);
882 if (ret < 0) {
883 aw_dev_err(aw_dev->dev, "read reg 0x%x failed", desc->reg);
884 return ret;
885 }
886
887 aw_dev->cali_desc.store_vol = reg_val & (~desc->mask);
888 reg_val &= desc->mask;
889 reg_val |= set_vol;
890
891 ret = aw_dev->ops.aw_reg_write(aw_dev,
892 desc->reg, reg_val);
893 if (ret < 0) {
894 aw_dev_err(aw_dev->dev, "write reg 0x%x failed", desc->reg);
895 return ret;
896 }
897 } else {
898 /*reset dsp vol*/
899 ret = aw_dev->ops.aw_reg_read(aw_dev,
900 desc->reg, ®_val);
901 if (ret < 0) {
902 aw_dev_err(aw_dev->dev, "read reg 0x%x failed", desc->reg);
903 return ret;
904 }
905
906 reg_val &= desc->mask;
907 reg_val |= aw_dev->cali_desc.store_vol;
908
909 ret = aw_dev->ops.aw_reg_write(aw_dev,
910 desc->reg, reg_val);
911 if (ret < 0) {
912 aw_dev_err(aw_dev->dev, "write reg 0x%x failed", desc->reg);
913 return ret;
914 }
915 }
916
917 return 0;
918 }
919
aw_cali_svc_set_white_noise(struct aw_device * aw_dev,bool noise_enable)920 static int aw_cali_svc_set_white_noise(struct aw_device *aw_dev,
921 bool noise_enable)
922 {
923 int ret;
924 uint32_t reg_val;
925 struct aw_noise_desc *desc = &aw_dev->noise_desc;
926
927 aw_dev_info(aw_dev->dev, "set noise %s",
928 (noise_enable == 0) ? ("disable") : ("enable"));
929
930 ret = aw_dev->ops.aw_dsp_read(aw_dev,
931 desc->dsp_reg, ®_val, desc->data_type);
932 if (ret < 0) {
933 aw_dev_err(aw_dev->dev, "read dsp reg 0x%x failed", desc->dsp_reg);
934 return ret;
935 }
936
937 if (noise_enable)
938 reg_val |= (~desc->mask);
939 else
940 reg_val &= desc->mask;
941
942
943 ret = aw_dev->ops.aw_dsp_write(aw_dev,
944 desc->dsp_reg, reg_val, desc->data_type);
945 if (ret < 0) {
946 aw_dev_err(aw_dev->dev, "write dsp reg 0x%x failed", desc->dsp_reg);
947 return ret;
948 }
949
950 return 0;
951 }
952
aw_cali_svc_cali_f0_en(struct aw_device * aw_dev,bool f0_enable)953 static int aw_cali_svc_cali_f0_en(struct aw_device *aw_dev, bool f0_enable)
954 {
955 int ret;
956 struct aw_cali_delay_desc *desc = &aw_dev->cali_delay_desc;
957
958 aw_dev_info(aw_dev->dev, "cali f0 %s",
959 (f0_enable == 0) ? ("disable") : ("enable"));
960
961 if (f0_enable) {
962 ret = aw_cali_svc_cali_run_dsp_vol(aw_dev, CALI_TYPE_F0, true);
963 if (ret < 0) {
964 aw_dev_err(aw_dev->dev, "run dsp volume error, ret=%d", ret);
965 return ret;
966 }
967
968 msleep(desc->delay);
969
970 ret = aw_cali_svc_set_white_noise(aw_dev, true);
971 if (ret < 0) {
972 aw_dev_err(aw_dev->dev, "write white noise error, ret=%d", ret);
973 aw_cali_svc_cali_run_dsp_vol(aw_dev, CALI_TYPE_F0, false);
974 return ret;
975 }
976 } else {
977 aw_cali_svc_set_white_noise(aw_dev, false);
978 aw_cali_svc_cali_run_dsp_vol(aw_dev, CALI_TYPE_F0, false);
979 }
980
981 return 0;
982 }
983
aw_cali_svc_get_cali_f0_q(struct aw_device * aw_dev)984 static int aw_cali_svc_get_cali_f0_q(struct aw_device *aw_dev)
985 {
986 int ret = -1;
987 int cnt = 0;
988 uint32_t f0 = 0;
989 uint32_t q = 0;
990 uint32_t f0_sum = 0;
991 uint32_t q_sum = 0;
992 struct aw_cali_desc *cali_desc = &aw_dev->cali_desc;
993
994 aw_dev_dbg(aw_dev->dev, "enter");
995
996 for (cnt = 0; cnt < F0_READ_CNT_MAX; cnt++) {
997 /*f0*/
998 ret = aw_cali_svc_get_dev_f0(aw_dev, &f0);
999 if (ret < 0)
1000 return ret;
1001 f0_sum += f0;
1002
1003 /*q*/
1004 ret = aw_cali_svc_get_dev_q(aw_dev, &q);
1005 if (ret < 0)
1006 return ret;
1007 q_sum += q;
1008 msleep(30);
1009 }
1010
1011 cali_desc->f0 = f0_sum / cnt;
1012 cali_desc->q = q_sum / cnt;
1013
1014 if (aw_dev->ops.aw_cali_svc_get_iv_st) {
1015 ret = aw_dev->ops.aw_cali_svc_get_iv_st(aw_dev);
1016 if (ret < 0) {
1017 aw_dev_err(aw_dev->dev,
1018 "get iv data failed, set default f0: 2600 q: 2600");
1019 cali_desc->f0 = 2600;
1020 cali_desc->q = 2600;
1021 }
1022 }
1023 aw_dev_info(aw_dev->dev, "f0[%d] q[%d]", cali_desc->f0, cali_desc->q);
1024 return 0;
1025 }
1026
aw_cali_svc_cali_mode_enable(struct aw_device * aw_dev,int type,unsigned int flag,bool is_enable)1027 static int aw_cali_svc_cali_mode_enable(struct aw_device *aw_dev,
1028 int type, unsigned int flag, bool is_enable)
1029 {
1030 int ret = 0;
1031
1032 aw_dev_info(aw_dev->dev, "type:%d, flag:0x%x, is_enable:%d",
1033 type, flag, is_enable);
1034
1035 if (is_enable) {
1036 ret = aw_cali_svc_cali_init_check(aw_dev);
1037 if (ret < 0) {
1038 aw_dev_err(aw_dev->dev, "init check failed");
1039 return ret;
1040 }
1041
1042 aw_cali_svc_set_cali_status(aw_dev, true);
1043
1044 ret = aw_cali_svc_cali_en(aw_dev, true);
1045 if (ret < 0) {
1046 aw_cali_svc_set_cali_status(aw_dev, false);
1047 return ret;
1048 }
1049
1050 if ((type == CALI_TYPE_RE) && (flag & CALI_OPS_HMUTE)) {
1051 ret = aw_cali_svc_cali_run_dsp_vol(aw_dev, CALI_TYPE_RE, true);
1052 if (ret < 0) {
1053 aw_cali_svc_cali_en(aw_dev, false);
1054 aw_cali_svc_set_cali_status(aw_dev, false);
1055 return ret;
1056 }
1057 } else if ((type == CALI_TYPE_F0) && (flag & CALI_OPS_NOISE)) {
1058 ret = aw_cali_svc_cali_f0_en(aw_dev, true);
1059 if (ret < 0) {
1060 aw_cali_svc_cali_en(aw_dev, false);
1061 aw_cali_svc_set_cali_status(aw_dev, false);
1062 return ret;
1063 }
1064 }
1065 } else {
1066
1067 if ((type == CALI_TYPE_RE) && (flag & CALI_OPS_HMUTE))
1068 aw_cali_svc_cali_run_dsp_vol(aw_dev, CALI_TYPE_RE, false);
1069 else if ((type == CALI_TYPE_F0) && (flag & CALI_OPS_NOISE))
1070 aw_cali_svc_cali_f0_en(aw_dev, false);
1071
1072 aw_cali_svc_cali_en(aw_dev, false);
1073 aw_dev_clear_int_status(aw_dev);
1074 aw_cali_svc_set_cali_status(aw_dev, false);
1075 }
1076
1077 return 0;
1078 }
1079
aw_cali_svc_devs_cali_mode_enable(struct list_head * dev_list,int type,unsigned int flag,bool is_enable)1080 static int aw_cali_svc_devs_cali_mode_enable(struct list_head *dev_list,
1081 int type, unsigned int flag,
1082 bool is_enable)
1083 {
1084 int ret = 0;
1085 struct list_head *pos = NULL;
1086 struct aw_device *local_dev = NULL;
1087
1088 list_for_each(pos, dev_list) {
1089 local_dev = container_of(pos, struct aw_device, list_node);
1090 if (is_enable)
1091 aw_run_mute_for_cali(local_dev, CALI_RESULT_NORMAL);
1092 ret = aw_cali_svc_cali_mode_enable(local_dev, type, flag, is_enable);
1093 if (ret < 0)
1094 return ret;
1095 if (!is_enable && (type == CALI_TYPE_F0))
1096 aw_run_mute_for_cali(local_dev, local_dev->cali_desc.cali_result);
1097 }
1098
1099 return ret;
1100 }
1101
aw_cali_svc_dev_cali_re(struct aw_device * aw_dev,unsigned int flag)1102 static int aw_cali_svc_dev_cali_re(struct aw_device *aw_dev, unsigned int flag)
1103 {
1104 int ret = 0;
1105
1106 aw_dev_info(aw_dev->dev, "enter");
1107
1108 aw_run_mute_for_cali(aw_dev, CALI_RESULT_NORMAL);
1109
1110 ret = aw_cali_svc_cali_mode_enable(aw_dev,
1111 CALI_TYPE_RE, flag, true);
1112 if (ret < 0)
1113 return ret;
1114
1115 msleep(g_cali_re_time);
1116
1117 ret = aw_cali_svc_get_smooth_cali_re(aw_dev);
1118 if (ret < 0)
1119 aw_dev_err(aw_dev->dev, "cali re failed");
1120
1121 aw_cali_svc_cali_mode_enable(aw_dev,
1122 CALI_TYPE_RE, flag, false);
1123
1124 return ret;
1125 }
1126
aw_cali_svc_devs_get_cali_re(struct list_head * dev_list)1127 static int aw_cali_svc_devs_get_cali_re(struct list_head *dev_list)
1128 {
1129 int ret = 0;
1130 struct list_head *pos = NULL;
1131 struct aw_device *local_dev = NULL;
1132
1133 list_for_each(pos, dev_list) {
1134 local_dev = container_of(pos, struct aw_device, list_node);
1135 ret = aw_cali_svc_get_smooth_cali_re(local_dev);
1136 if (ret < 0) {
1137 aw_dev_err(local_dev->dev, "get re failed");
1138 return ret;
1139 }
1140 }
1141
1142 return ret;
1143 }
1144
aw_cali_svc_devs_cali_re(struct aw_device * aw_dev,unsigned int flag)1145 static int aw_cali_svc_devs_cali_re(struct aw_device *aw_dev, unsigned int flag)
1146 {
1147 int ret = 0;
1148 struct list_head *dev_list = NULL;
1149
1150 aw_dev_info(aw_dev->dev, "enter");
1151
1152 ret = aw_dev_get_list_head(&dev_list);
1153 if (ret) {
1154 aw_dev_err(aw_dev->dev, " get dev list failed");
1155 return ret;
1156 }
1157
1158 ret = aw_cali_svc_devs_cali_mode_enable(dev_list, CALI_TYPE_RE, flag, true);
1159 if (ret < 0)
1160 goto error;
1161
1162 msleep(g_cali_re_time);
1163
1164 ret = aw_cali_svc_devs_get_cali_re(dev_list);
1165 if (ret < 0)
1166 goto error;
1167
1168 aw_cali_svc_devs_cali_mode_enable(dev_list, CALI_TYPE_RE, flag, false);
1169
1170 return 0;
1171
1172 error:
1173 aw_cali_svc_devs_cali_mode_enable(dev_list, CALI_TYPE_RE, flag, false);
1174 return ret;
1175 }
1176
aw_cali_svc_cali_re(struct aw_device * aw_dev,bool is_single,unsigned int flag)1177 static int aw_cali_svc_cali_re(struct aw_device *aw_dev, bool is_single, unsigned int flag)
1178 {
1179 if (is_single)
1180 return aw_cali_svc_dev_cali_re(aw_dev, flag);
1181 else
1182 return aw_cali_svc_devs_cali_re(aw_dev, flag);
1183 }
1184
aw_cali_svc_set_devs_re_str(struct aw_device * aw_dev,const char * re_str)1185 static int aw_cali_svc_set_devs_re_str(struct aw_device *aw_dev, const char *re_str)
1186 {
1187 struct list_head *dev_list = NULL, *pos = NULL;
1188 struct aw_device *local_dev = NULL;
1189 int ret, cnt = 0;
1190 int re_data[AW_DEV_CH_MAX] = { 0 };
1191 char str_data[32] = { 0 };
1192 int i, len = 0;
1193 int dev_num = 0;
1194
1195 /*get dev list*/
1196 ret = aw_dev_get_list_head(&dev_list);
1197 if (ret < 0) {
1198 aw_dev_err(aw_dev->dev, "get dev list failed");
1199 return ret;
1200 }
1201
1202 dev_num = aw_dev->ops.aw_get_dev_num();
1203
1204 for (i = 0 ; i < dev_num; i++) {
1205 memset(str_data, 0, sizeof(str_data));
1206 snprintf(str_data, sizeof(str_data), "dev[%d]:%s ", i, "%d");
1207 ret = sscanf(re_str + len, str_data, &re_data[i]);
1208 if (ret <= 0) {
1209 aw_dev_err(aw_dev->dev, "unsupported str: %s", re_str);
1210 return -EINVAL;
1211 }
1212 len += snprintf(str_data, sizeof(str_data), "dev[%d]:%d ", i, re_data[i]);
1213 if (len > strlen(re_str)) {
1214 aw_dev_err(aw_dev->dev, "%s: unsupported", re_str);
1215 return -EINVAL;
1216 }
1217 }
1218
1219 list_for_each (pos, dev_list) {
1220 local_dev = container_of(pos, struct aw_device, list_node);
1221 if (local_dev->channel < AW_DEV_CH_MAX) {
1222 ret = aw_cali_store_cali_re(local_dev, re_data[local_dev->channel]);
1223 if (ret < 0) {
1224 aw_dev_err(local_dev->dev, "store cali re failed");
1225 return ret;
1226 }
1227 cnt++;
1228 }
1229 }
1230
1231 return cnt;
1232 }
1233
aw_cali_svc_dev_cali_f0_q(struct aw_device * aw_dev,unsigned int flag)1234 static int aw_cali_svc_dev_cali_f0_q(struct aw_device *aw_dev, unsigned int flag)
1235 {
1236 int ret;
1237
1238 aw_run_mute_for_cali(aw_dev, CALI_RESULT_NORMAL);
1239
1240 ret = aw_cali_svc_cali_mode_enable(aw_dev, CALI_TYPE_F0, flag, true);
1241 if (ret < 0)
1242 return ret;
1243
1244 msleep(AW_CALI_F0_TIME);
1245
1246 ret = aw_cali_svc_get_cali_f0_q(aw_dev);
1247 if (ret < 0)
1248 aw_dev_err(aw_dev->dev, "get f0 q failed");
1249
1250 aw_cali_svc_cali_mode_enable(aw_dev, CALI_TYPE_F0, flag, false);
1251
1252 aw_run_mute_for_cali(aw_dev, aw_dev->cali_desc.cali_result);
1253
1254 return ret;
1255 }
1256
aw_cali_svc_devs_get_cali_f0_q(struct list_head * dev_list)1257 static int aw_cali_svc_devs_get_cali_f0_q(struct list_head *dev_list)
1258 {
1259 int ret = 0;
1260 struct list_head *pos = NULL;
1261 struct aw_device *local_dev = NULL;
1262
1263 list_for_each(pos, dev_list) {
1264 local_dev = container_of(pos, struct aw_device, list_node);
1265 ret = aw_cali_svc_get_cali_f0_q(local_dev);
1266 if (ret < 0) {
1267 aw_dev_err(local_dev->dev, "get f0 q failed");
1268 return ret;
1269 }
1270 }
1271
1272 return ret;
1273 }
1274
aw_cali_svc_devs_cali_f0_q(struct aw_device * aw_dev,unsigned int flag)1275 static int aw_cali_svc_devs_cali_f0_q(struct aw_device *aw_dev, unsigned int flag)
1276 {
1277 int ret;
1278 struct list_head *dev_list = NULL;
1279
1280 ret = aw_dev_get_list_head(&dev_list);
1281 if (ret) {
1282 aw_dev_err(aw_dev->dev, " get dev list failed");
1283 return ret;
1284 }
1285
1286 ret = aw_cali_svc_devs_cali_mode_enable(dev_list, CALI_TYPE_F0, flag, true);
1287 if (ret < 0)
1288 goto error;
1289
1290 msleep(AW_CALI_F0_TIME);
1291
1292 ret = aw_cali_svc_devs_get_cali_f0_q(dev_list);
1293 if (ret < 0)
1294 goto error;
1295
1296 aw_cali_svc_devs_cali_mode_enable(dev_list, CALI_TYPE_F0, flag, false);
1297
1298 return 0;
1299
1300 error:
1301 aw_cali_svc_devs_cali_mode_enable(dev_list, CALI_TYPE_F0, flag, false);
1302 return ret;
1303 }
1304
aw_cali_svc_cali_f0_q(struct aw_device * aw_dev,bool is_single,unsigned int flag)1305 static int aw_cali_svc_cali_f0_q(struct aw_device *aw_dev, bool is_single, unsigned int flag)
1306 {
1307 if (is_single)
1308 return aw_cali_svc_dev_cali_f0_q(aw_dev, flag);
1309 else
1310 return aw_cali_svc_devs_cali_f0_q(aw_dev, flag);
1311 }
1312
aw_cali_svc_get_dev_cali_val(struct aw_device * aw_dev,int type,uint32_t * data_buf)1313 static int aw_cali_svc_get_dev_cali_val(struct aw_device *aw_dev, int type, uint32_t *data_buf)
1314 {
1315 switch (type) {
1316 case GET_RE_TYPE:
1317 *data_buf = aw_dev->cali_desc.cali_re;
1318 break;
1319 case GET_F0_TYPE:
1320 *data_buf = aw_dev->cali_desc.f0;
1321 break;
1322 case GET_Q_TYPE:
1323 *data_buf = aw_dev->cali_desc.q;
1324 break;
1325 default:
1326 aw_dev_err(aw_dev->dev, "type:%d not support", type);
1327 return -EINVAL;
1328 }
1329
1330 return 0;
1331 }
1332
aw_cali_svc_get_devs_cali_val(struct aw_device * aw_dev,int type,uint32_t * data_buf,int num)1333 static int aw_cali_svc_get_devs_cali_val(struct aw_device *aw_dev, int type, uint32_t *data_buf, int num)
1334 {
1335 struct list_head *dev_list = NULL;
1336 struct list_head *pos = NULL;
1337 struct aw_device *local_dev = NULL;
1338 int ret, cnt = 0;
1339
1340 /*get dev list*/
1341 ret = aw_dev_get_list_head(&dev_list);
1342 if (ret) {
1343 aw_dev_err(aw_dev->dev, "get dev list failed");
1344 return ret;
1345 }
1346
1347 list_for_each(pos, dev_list) {
1348 local_dev = container_of(pos, struct aw_device, list_node);
1349 if (local_dev->channel < num) {
1350 switch (type) {
1351 case GET_RE_TYPE:
1352 data_buf[local_dev->channel] = local_dev->cali_desc.cali_re;
1353 break;
1354 case GET_F0_TYPE:
1355 data_buf[local_dev->channel] = local_dev->cali_desc.f0;
1356 break;
1357 case GET_Q_TYPE:
1358 data_buf[local_dev->channel] = local_dev->cali_desc.q;
1359 break;
1360 default:
1361 aw_dev_err(local_dev->dev, "type:%d not support", type);
1362 return -EINVAL;
1363 }
1364 cnt++;
1365 } else {
1366 aw_dev_err(local_dev->dev, "channel num[%d] overflow buf num[%d]",
1367 local_dev->channel, num);
1368 return -EINVAL;
1369 }
1370 }
1371
1372 return cnt;
1373 }
1374
aw_cali_svc_cali_re_f0_q(struct aw_device * aw_dev,bool is_single,unsigned int flag)1375 static int aw_cali_svc_cali_re_f0_q(struct aw_device *aw_dev, bool is_single, unsigned int flag)
1376 {
1377 int ret;
1378
1379 ret = aw_cali_svc_cali_re(aw_dev, is_single, flag);
1380 if (ret < 0)
1381 return ret;
1382
1383 ret = aw_cali_svc_cali_f0_q(aw_dev, is_single, flag);
1384 if (ret < 0)
1385 return ret;
1386
1387 return 0;
1388 }
1389
1390
1391
aw_cali_svc_cali_cmd(struct aw_device * aw_dev,int cali_cmd,bool is_single,unsigned int flag)1392 static int aw_cali_svc_cali_cmd(struct aw_device *aw_dev, int cali_cmd, bool is_single, unsigned int flag)
1393 {
1394 switch (cali_cmd) {
1395 case AW_CALI_CMD_RE: {
1396 return aw_cali_svc_cali_re(aw_dev, is_single, flag);
1397 } break;
1398 case AW_CALI_CMD_F0:
1399 case AW_CALI_CMD_F0_Q: {
1400 return aw_cali_svc_cali_f0_q(aw_dev, is_single, flag);
1401 } break;
1402 case AW_CALI_CMD_RE_F0:
1403 case AW_CALI_CMD_RE_F0_Q: {
1404 return aw_cali_svc_cali_re_f0_q(aw_dev, is_single, flag);
1405 }
1406 default: {
1407 aw_dev_err(aw_dev->dev, "unsupported cmd %d", cali_cmd);
1408 return -EINVAL;
1409 }
1410 }
1411 return 0;
1412 }
1413
aw_cali_svc_get_cmd_form_str(struct aw_device * aw_dev,const char * buf)1414 static int aw_cali_svc_get_cmd_form_str(struct aw_device *aw_dev, const char *buf)
1415 {
1416 int i;
1417
1418 for (i = 0; i < CALI_STR_MAX; i++) {
1419 if (!strncmp(cali_str[i], buf, strlen(cali_str[i]))) {
1420 break;
1421 }
1422 }
1423
1424 if (i == CALI_STR_MAX) {
1425 aw_dev_err(aw_dev->dev, "supported cmd [%s]!", buf);
1426 return -EINVAL;
1427 }
1428
1429 aw_dev_dbg(aw_dev->dev, "find str [%s]", cali_str[i]);
1430 return i;
1431 }
1432
1433 /*****************************attr cali***************************************************/
aw_cali_attr_time_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1434 static ssize_t aw_cali_attr_time_store(struct device *dev,
1435 struct device_attribute *attr, const char *buf, size_t count)
1436 {
1437 int ret;
1438 uint32_t time;
1439 struct aw883xx *aw883xx = dev_get_drvdata(dev);
1440 struct aw_device *aw_dev = aw883xx->aw_pa;
1441
1442 ret = kstrtoint(buf, 0, &time);
1443 if (ret < 0) {
1444 aw_dev_err(aw_dev->dev, "read buf %s failed", buf);
1445 return ret;
1446 }
1447
1448 if (time < 1000) {
1449 aw_dev_err(aw_dev->dev, "time:%d is too short, no set", time);
1450 return -EINVAL;
1451 }
1452
1453 g_cali_re_time = time;
1454 aw_dev_dbg(aw_dev->dev, "time:%u", time);
1455
1456 return count;
1457 }
1458
aw_cali_attr_time_show(struct device * dev,struct device_attribute * attr,char * buf)1459 static ssize_t aw_cali_attr_time_show(struct device *dev,
1460 struct device_attribute *attr, char *buf)
1461 {
1462 ssize_t len = 0;
1463
1464 len += snprintf(buf+len, PAGE_SIZE-len,
1465 "time: %u\n", g_cali_re_time);
1466
1467 return len;
1468 }
1469
aw_cali_attr_re_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1470 static ssize_t aw_cali_attr_re_store(struct device *dev,
1471 struct device_attribute *attr, const char *buf, size_t count)
1472 {
1473 struct aw883xx *aw883xx = dev_get_drvdata(dev);
1474 struct aw_device *aw_dev = aw883xx->aw_pa;
1475 int ret;
1476 int re;
1477
1478 if (is_single_cali) {
1479 ret = kstrtoint(buf, 0, &re);
1480 if (ret < 0) {
1481 aw_dev_err(aw_dev->dev, "read buf %s failed", buf);
1482 return ret;
1483 }
1484
1485 ret = aw_cali_store_cali_re(aw_dev, re);
1486 if (ret < 0) {
1487 aw_dev_err(aw_dev->dev, "store cali re failed!");
1488 return ret;
1489 }
1490 } else {
1491 ret = aw_cali_svc_set_devs_re_str(aw_dev, buf);
1492 if (ret <= 0) {
1493 aw_pr_err("set re str %s failed", buf);
1494 return -EPERM;
1495 }
1496 }
1497
1498 return count;
1499 }
1500
aw_cali_attr_re_show(struct device * dev,struct device_attribute * attr,char * buf)1501 static ssize_t aw_cali_attr_re_show(struct device *dev,
1502 struct device_attribute *attr, char *buf)
1503 {
1504 int ret, i;
1505 struct aw883xx *aw883xx = dev_get_drvdata(dev);
1506 struct aw_device *aw_dev = aw883xx->aw_pa;
1507 ssize_t len = 0;
1508 int32_t re[AW_DEV_CH_MAX] = { 0 };
1509
1510 ret = aw_cali_svc_cali_re(aw_dev, is_single_cali, CALI_OPS_HMUTE);
1511 if (ret < 0) {
1512 aw_dev_err(aw_dev->dev, "cali re failed");
1513 return ret;
1514 }
1515
1516 if (is_single_cali) {
1517 len += snprintf(buf+len, PAGE_SIZE-len, "dev[%d]: %umOhms \n",
1518 aw_dev->channel, aw_dev->cali_desc.cali_re);
1519 } else {
1520 ret = aw_cali_svc_get_devs_cali_val(aw_dev, GET_RE_TYPE, re, AW_DEV_CH_MAX);
1521 if (ret <= 0) {
1522 aw_dev_err(aw_dev->dev, "get re failed");
1523 } else {
1524 for (i = 0; i < ret; i++)
1525 len += snprintf(buf+len, PAGE_SIZE-len, "dev[%d]: %umOhms ", i, re[i]);
1526
1527 len += snprintf(buf+len, PAGE_SIZE-len, " \n");
1528 }
1529 }
1530
1531 return len;
1532 }
1533
aw_cali_attr_f0_show(struct device * dev,struct device_attribute * attr,char * buf)1534 static ssize_t aw_cali_attr_f0_show(struct device *dev,
1535 struct device_attribute *attr, char *buf)
1536 {
1537 int ret, i;
1538 struct aw883xx *aw883xx = dev_get_drvdata(dev);
1539 struct aw_device *aw_dev = aw883xx->aw_pa;
1540 ssize_t len = 0;
1541 uint32_t f0[AW_DEV_CH_MAX] = { 0 };
1542
1543 ret = aw_cali_svc_cali_f0_q(aw_dev, is_single_cali, CALI_OPS_NOISE);
1544 if (ret < 0) {
1545 aw_dev_err(aw_dev->dev, "cali f0 failed");
1546 return ret;
1547 }
1548
1549 if (is_single_cali) {
1550 len += snprintf(buf+len, PAGE_SIZE-len, "dev[%d]:%u Hz\n",
1551 aw_dev->channel, aw_dev->cali_desc.f0);
1552 } else {
1553 ret = aw_cali_svc_get_devs_cali_val(aw_dev, GET_F0_TYPE, f0, AW_DEV_CH_MAX);
1554 if (ret <= 0) {
1555 aw_dev_err(aw_dev->dev, "get re failed");
1556 } else {
1557 for (i = 0; i < ret; i++)
1558 len += snprintf(buf+len, PAGE_SIZE-len, "dev[%d]:%u Hz ", i, f0[i]);
1559
1560 len += snprintf(buf+len, PAGE_SIZE-len, " \n");
1561 }
1562 }
1563
1564 return len;
1565 }
1566
aw_cali_attr_f0_q_show(struct device * dev,struct device_attribute * attr,char * buf)1567 static ssize_t aw_cali_attr_f0_q_show(struct device *dev,
1568 struct device_attribute *attr, char *buf)
1569 {
1570 int ret, i;
1571 struct aw883xx *aw883xx = dev_get_drvdata(dev);
1572 struct aw_device *aw_dev = aw883xx->aw_pa;
1573 ssize_t len = 0;
1574 uint32_t f0[AW_DEV_CH_MAX] = { 0 };
1575 uint32_t q[AW_DEV_CH_MAX] = { 0 };
1576
1577 ret = aw_cali_svc_cali_f0_q(aw_dev, is_single_cali, CALI_OPS_NOISE);
1578 if (ret < 0) {
1579 aw_dev_err(aw_dev->dev, "cali f0 q failed");
1580 return ret;
1581 }
1582
1583 if (is_single_cali) {
1584 len += snprintf(buf+len, PAGE_SIZE-len, "dev[%d]f0:%u Hz q:%u\n",
1585 aw_dev->channel, aw_dev->cali_desc.f0, aw_dev->cali_desc.q);
1586 } else {
1587
1588 ret = aw_cali_svc_get_devs_cali_val(aw_dev, GET_F0_TYPE, f0, AW_DEV_CH_MAX);
1589 if (ret <= 0) {
1590 aw_dev_err(aw_dev->dev, "get f0 failed");
1591 return -EINVAL;
1592 }
1593
1594 ret = aw_cali_svc_get_devs_cali_val(aw_dev, GET_Q_TYPE, q, AW_DEV_CH_MAX);
1595 if (ret <= 0) {
1596 aw_dev_err(aw_dev->dev, "get q failed");
1597 return -EINVAL;
1598 }
1599
1600 for (i = 0; i < ret; i++)
1601 len += snprintf(buf+len, PAGE_SIZE-len, "dev[%d]:f0:%u Hz q:%u ",
1602 i, f0[i], q[i]);
1603
1604 len += snprintf(buf+len, PAGE_SIZE-len, " \n");
1605 }
1606
1607 return len;
1608 }
1609
aw_re_range_show(struct device * dev,struct device_attribute * attr,char * buf)1610 static ssize_t aw_re_range_show(struct device *dev,
1611 struct device_attribute *attr, char *buf)
1612 {
1613 ssize_t len = 0;
1614 struct aw883xx *aw883xx = dev_get_drvdata(dev);
1615 struct aw_device *aw_dev = aw883xx->aw_pa;
1616 uint32_t range_buf[RE_RANGE_NUM] = { 0 };
1617
1618 aw_cali_svc_get_dev_re_range(aw_dev, range_buf);
1619
1620 len += snprintf(buf + len, PAGE_SIZE - len,
1621 "re_min value: [%d]\n", range_buf[RE_MIN_FLAG]);
1622 len += snprintf(buf + len, PAGE_SIZE - len,
1623 "re_max value: [%d]\n", range_buf[RE_MAX_FLAG]);
1624
1625 return len;
1626 }
1627
1628 static DEVICE_ATTR(cali_time, S_IWUSR | S_IRUGO,
1629 aw_cali_attr_time_show, aw_cali_attr_time_store);
1630 static DEVICE_ATTR(cali_re, S_IRUGO | S_IWUSR,
1631 aw_cali_attr_re_show, aw_cali_attr_re_store);
1632 static DEVICE_ATTR(cali_f0, S_IRUGO,
1633 aw_cali_attr_f0_show, NULL);
1634 static DEVICE_ATTR(cali_f0_q, S_IRUGO,
1635 aw_cali_attr_f0_q_show, NULL);
1636 static DEVICE_ATTR(re_range, S_IRUGO,
1637 aw_re_range_show, NULL);
1638
1639 static struct attribute *aw_cali_attr[] = {
1640 &dev_attr_cali_time.attr,
1641 &dev_attr_cali_re.attr,
1642 &dev_attr_cali_f0.attr,
1643 &dev_attr_cali_f0_q.attr,
1644 &dev_attr_re_range.attr,
1645 NULL
1646 };
1647
1648 static struct attribute_group aw_cali_attr_group = {
1649 .attrs = aw_cali_attr,
1650 NULL
1651 };
1652
aw_cali_attr_init(struct aw_device * aw_dev)1653 static void aw_cali_attr_init(struct aw_device *aw_dev)
1654 {
1655 int ret;
1656
1657 ret = sysfs_create_group(&aw_dev->dev->kobj, &aw_cali_attr_group);
1658 if (ret < 0) {
1659 aw_dev_info(aw_dev->dev, "error creating sysfs cali attr files");
1660 }
1661 }
1662
aw_cali_attr_deinit(struct aw_device * aw_dev)1663 static void aw_cali_attr_deinit(struct aw_device *aw_dev)
1664 {
1665 sysfs_remove_group(&aw_dev->dev->kobj, &aw_cali_attr_group);
1666 aw_dev_info(aw_dev->dev, "attr files deinit");
1667 }
1668
1669
1670
1671
1672 /*****************************class node******************************************************/
aw_cali_class_time_show(struct class * class,struct class_attribute * attr,char * buf)1673 static ssize_t aw_cali_class_time_show(struct class *class, struct class_attribute *attr, char *buf)
1674 {
1675 ssize_t len = 0;
1676
1677 len += snprintf(buf+len, PAGE_SIZE-len,
1678 "time: %d\n", g_cali_re_time);
1679
1680 return len;
1681 }
1682
aw_cali_class_time_store(struct class * class,struct class_attribute * attr,const char * buf,size_t len)1683 static ssize_t aw_cali_class_time_store(struct class *class,
1684 struct class_attribute *attr, const char *buf, size_t len)
1685 {
1686 int ret;
1687 uint32_t time;
1688
1689 ret = kstrtoint(buf, 0, &time);
1690 if (ret < 0) {
1691 aw_pr_err("read buf %s failed", buf);
1692 return ret;
1693 }
1694
1695 if (time < 1000) {
1696 aw_pr_err("time:%d is too short, no set", time);
1697 return -EINVAL;
1698 }
1699
1700 g_cali_re_time = time;
1701 aw_pr_dbg("time:%d", time);
1702
1703 return len;
1704 }
1705
aw_cali_class_cali_re_show(struct class * class,struct class_attribute * attr,char * buf)1706 static ssize_t aw_cali_class_cali_re_show(struct class *class, struct class_attribute *attr, char *buf)
1707 {
1708 struct list_head *dev_list;
1709 struct aw_device *local_dev;
1710 int ret, i;
1711 ssize_t len = 0;
1712 uint32_t cali_re[AW_DEV_CH_MAX] = { 0 };
1713
1714 aw_pr_info("enter");
1715
1716 ret = aw_dev_get_list_head(&dev_list);
1717 if (ret) {
1718 aw_pr_err("get dev list failed");
1719 return ret;
1720 }
1721
1722 local_dev = list_first_entry(dev_list, struct aw_device, list_node);
1723
1724 ret = aw_cali_svc_cali_re(local_dev, false, CALI_OPS_HMUTE);
1725 if (ret < 0)
1726 return ret;
1727
1728 ret = aw_cali_svc_get_devs_cali_val(local_dev, GET_RE_TYPE, cali_re, AW_DEV_CH_MAX);
1729 if (ret <= 0) {
1730 aw_dev_err(local_dev->dev, "get re failed");
1731 } else {
1732 for (i = 0; i < ret; i++)
1733 len += snprintf(buf+len, PAGE_SIZE-len, "dev[%d]:%u mOhms ", i, cali_re[i]);
1734
1735 len += snprintf(buf+len, PAGE_SIZE-len, "\n");
1736 }
1737
1738 return len;
1739 }
1740
aw_cali_class_cali_re_store(struct class * class,struct class_attribute * attr,const char * buf,size_t len)1741 static ssize_t aw_cali_class_cali_re_store(struct class *class,
1742 struct class_attribute *attr, const char *buf, size_t len)
1743 {
1744 struct list_head *dev_list = NULL;
1745 struct aw_device *local_dev = NULL;
1746 int ret;
1747
1748 ret = aw_dev_get_list_head(&dev_list);
1749 if (ret) {
1750 aw_pr_err("get dev list failed");
1751 return ret;
1752 }
1753
1754 local_dev = list_first_entry(dev_list, struct aw_device, list_node);
1755
1756 ret = aw_cali_svc_set_devs_re_str(local_dev, buf);
1757 if (ret <= 0) {
1758 aw_pr_err("set re str %s failed", buf);
1759 return -EPERM;
1760 }
1761
1762 return len;
1763 }
1764
aw_cali_class_cali_f0_show(struct class * class,struct class_attribute * attr,char * buf)1765 static ssize_t aw_cali_class_cali_f0_show(struct class *class,
1766 struct class_attribute *attr, char *buf)
1767 {
1768 struct list_head *dev_list = NULL;
1769 struct aw_device *local_dev = NULL;
1770 int ret = -1;
1771 int i = 0;
1772 ssize_t len = 0;
1773 uint32_t f0[AW_DEV_CH_MAX] = { 0 };
1774
1775 aw_pr_info("enter");
1776
1777 ret = aw_dev_get_list_head(&dev_list);
1778 if (ret < 0) {
1779 aw_pr_err("get dev list failed");
1780 return ret;
1781 }
1782
1783 local_dev = list_first_entry(dev_list, struct aw_device, list_node);
1784
1785 ret = aw_cali_svc_cali_f0_q(local_dev, is_single_cali, CALI_OPS_NOISE);
1786 if (ret < 0) {
1787 aw_pr_err("cali f0 failed");
1788 return ret;
1789 }
1790
1791 ret = aw_cali_svc_get_devs_cali_val(local_dev, GET_F0_TYPE, f0, AW_DEV_CH_MAX);
1792 if (ret <= 0) {
1793 aw_pr_err("get f0 failed");
1794 } else {
1795 for (i = 0; i < ret; i++)
1796 len += snprintf(buf+len, PAGE_SIZE-len, "dev[%d]:%u Hz ",
1797 i, f0[i]);
1798
1799 len += snprintf(buf+len, PAGE_SIZE-len, " \n");
1800 }
1801
1802 return len;
1803 }
1804
aw_cali_class_cali_f0_q_show(struct class * class,struct class_attribute * attr,char * buf)1805 static ssize_t aw_cali_class_cali_f0_q_show(struct class *class, struct class_attribute *attr, char *buf)
1806 {
1807 struct list_head *dev_list = NULL;
1808 struct aw_device *local_dev = NULL;
1809 int ret, i;
1810 ssize_t len = 0;
1811 uint32_t f0[AW_DEV_CH_MAX] = { 0 };
1812 uint32_t q[AW_DEV_CH_MAX] = { 0 };
1813
1814 aw_pr_info("enter");
1815
1816 ret = aw_dev_get_list_head(&dev_list);
1817 if (ret < 0) {
1818 aw_pr_err("get dev list failed");
1819 return ret;
1820 }
1821
1822 local_dev = list_first_entry(dev_list, struct aw_device, list_node);
1823
1824 ret = aw_cali_svc_cali_f0_q(local_dev, is_single_cali, CALI_OPS_NOISE);
1825 if (ret < 0) {
1826 aw_dev_err(local_dev->dev, "cali f0 q failed");
1827 return ret;
1828 }
1829
1830 ret = aw_cali_svc_get_devs_cali_val(local_dev, GET_F0_TYPE, f0, AW_DEV_CH_MAX);
1831 if (ret <= 0) {
1832 aw_dev_err(local_dev->dev, "get f0 failed");
1833 return -EINVAL;
1834 }
1835
1836 ret = aw_cali_svc_get_devs_cali_val(local_dev, GET_Q_TYPE, q, AW_DEV_CH_MAX);
1837 if (ret <= 0) {
1838 aw_dev_err(local_dev->dev, "get q failed");
1839 return -EINVAL;
1840 }
1841
1842 for (i = 0; i < ret; i++)
1843 len += snprintf(buf+len, PAGE_SIZE-len, "dev[%d]:f0:%u Hz q:%u ",
1844 i, f0[i], q[i]);
1845
1846 len += snprintf(buf+len, PAGE_SIZE-len, " \n");
1847
1848 return len;
1849 }
1850
aw_class_re_range_show(struct class * class,struct class_attribute * attr,char * buf)1851 static ssize_t aw_class_re_range_show(struct class *class, struct class_attribute *attr, char *buf)
1852 {
1853 int ret, i;
1854 ssize_t len = 0;
1855 struct list_head *dev_list = NULL;
1856 struct aw_device *local_dev = NULL;
1857 uint32_t re_value[AW_DEV_RE_RANGE] = { 0 };
1858
1859 aw_pr_info("enter");
1860
1861 ret = aw_dev_get_list_head(&dev_list);
1862 if (ret < 0) {
1863 aw_pr_err("get dev list failed");
1864 return ret;
1865 }
1866
1867 local_dev = list_first_entry(dev_list, struct aw_device, list_node);
1868 ret = aw_cali_svc_get_devs_re_range(local_dev, re_value, AW_DEV_CH_MAX);
1869 if (ret <= 0) {
1870 aw_dev_err(local_dev->dev, "get re range failed");
1871 return -EINVAL;
1872 }
1873
1874 for (i = 0; i < ret; i++) {
1875 len += snprintf(buf+len, PAGE_SIZE-len, "dev[%d]:re_min:%d re_max:%d ",
1876 i, re_value[RE_MIN_FLAG + i * RE_RANGE_NUM],
1877 re_value[RE_MAX_FLAG + i * RE_RANGE_NUM]);
1878 }
1879 len += snprintf(buf+len, PAGE_SIZE-len, " \n");
1880
1881 return len;
1882 }
1883
1884 static struct class_attribute class_attr_cali_time = \
1885 __ATTR(cali_time, S_IWUSR | S_IRUGO, \
1886 aw_cali_class_time_show, aw_cali_class_time_store);
1887
1888 static struct class_attribute class_attr_re25_calib = \
1889 __ATTR(re25_calib, S_IWUSR | S_IRUGO, \
1890 aw_cali_class_cali_re_show, aw_cali_class_cali_re_store);
1891
1892 static struct class_attribute class_attr_f0_calib = \
1893 __ATTR(f0_calib, S_IRUGO, \
1894 aw_cali_class_cali_f0_show, NULL);
1895
1896 static struct class_attribute class_attr_f0_q_calib = \
1897 __ATTR(f0_q_calib, S_IRUGO, \
1898 aw_cali_class_cali_f0_q_show, NULL);
1899
1900 static struct class_attribute class_att_re_range = \
1901 __ATTR(re_range, S_IRUGO, \
1902 aw_class_re_range_show, NULL);
1903
1904 static struct class aw_cali_class = {
1905 .name = "smartpa",
1906 .owner = THIS_MODULE,
1907 };
1908
aw_cali_class_attr_init(struct aw_device * aw_dev)1909 static void aw_cali_class_attr_init(struct aw_device *aw_dev)
1910 {
1911 int ret;
1912
1913 if (aw_dev->channel != 0) {
1914 aw_dev_err(aw_dev->dev, "class node already register");
1915 return;
1916 }
1917
1918 ret = class_register(&aw_cali_class);
1919 if (ret < 0) {
1920 aw_dev_err(aw_dev->dev, "error creating class node");
1921 return;
1922 }
1923
1924 ret = class_create_file(&aw_cali_class, &class_attr_cali_time);
1925 if (ret)
1926 aw_dev_err(aw_dev->dev, "creat class_attr_cali_time fail");
1927
1928 ret = class_create_file(&aw_cali_class, &class_attr_re25_calib);
1929 if (ret)
1930 aw_dev_err(aw_dev->dev, "creat class_attr_re25_calib fail");
1931
1932 ret = class_create_file(&aw_cali_class, &class_attr_f0_calib);
1933 if (ret)
1934 aw_dev_err(aw_dev->dev, "creat class_attr_f0_calib fail");
1935
1936
1937 ret = class_create_file(&aw_cali_class, &class_attr_f0_q_calib);
1938 if (ret)
1939 aw_dev_err(aw_dev->dev, "creat class_attr_f0_q_calib fail");
1940
1941 ret = class_create_file(&aw_cali_class, &class_att_re_range);
1942 if (ret)
1943 aw_dev_err(aw_dev->dev, "creat class_att_re_range fail");
1944 }
1945
aw_cali_class_attr_deinit(struct aw_device * aw_dev)1946 static void aw_cali_class_attr_deinit(struct aw_device *aw_dev)
1947 {
1948 class_remove_file(&aw_cali_class, &class_att_re_range);
1949 class_remove_file(&aw_cali_class, &class_attr_f0_q_calib);
1950 class_remove_file(&aw_cali_class, &class_attr_f0_calib);
1951 class_remove_file(&aw_cali_class, &class_attr_re25_calib);
1952 class_remove_file(&aw_cali_class, &class_attr_cali_time);
1953
1954 class_unregister(&aw_cali_class);
1955 aw_dev_info(aw_dev->dev, "unregister class node");
1956 }
1957 /*****************************class node******************************************************/
1958
1959
1960 /*****************************misc cali******************************************************/
aw_cali_misc_open(struct inode * inode,struct file * file)1961 static int aw_cali_misc_open(struct inode *inode, struct file *file)
1962 {
1963 struct list_head *dev_list = NULL;
1964 struct list_head *pos = NULL;
1965 struct aw_device *local_dev = NULL;
1966 int ret;
1967
1968 aw_pr_dbg("misc open success");
1969
1970 ret = aw_dev_get_list_head(&dev_list);
1971 if (ret) {
1972 aw_pr_err("get dev list failed");
1973 file->private_data = NULL;
1974 return -EINVAL;
1975 }
1976
1977 //find select dev
1978 list_for_each (pos, dev_list) {
1979 local_dev = container_of(pos, struct aw_device, list_node);
1980 if (local_dev->channel == g_dev_select)
1981 break;
1982 }
1983
1984 if (local_dev == NULL) {
1985 aw_pr_err("get dev failed");
1986 return -EINVAL;
1987 }
1988
1989 //cannot find sel dev, use list first dev
1990 if (local_dev->channel != g_dev_select) {
1991 local_dev = list_first_entry(dev_list, struct aw_device, list_node);
1992 aw_dev_dbg(local_dev->dev, "can not find dev[%d], use default", g_dev_select);
1993 }
1994
1995 file->private_data = (void *)local_dev;
1996
1997 aw_dev_dbg(local_dev->dev, "misc open success");
1998
1999 return 0;
2000 }
2001
aw_cali_misc_release(struct inode * inode,struct file * file)2002 static int aw_cali_misc_release(struct inode *inode, struct file *file)
2003 {
2004 file->private_data = (void *)NULL;
2005
2006 aw_pr_dbg("misc release success");
2007
2008 return 0;
2009 }
2010
aw_cali_misc_ops_write(struct aw_device * aw_dev,unsigned int cmd,unsigned long arg)2011 static int aw_cali_misc_ops_write(struct aw_device *aw_dev,
2012 unsigned int cmd, unsigned long arg)
2013 {
2014
2015 unsigned int data_len = _IOC_SIZE(cmd);
2016 char *data_ptr = NULL;
2017 int ret = 0;
2018
2019 data_ptr = devm_kzalloc(aw_dev->dev, data_len, GFP_KERNEL);
2020 if (!data_ptr) {
2021 aw_dev_err(aw_dev->dev, "malloc failed !");
2022 return -ENOMEM;
2023 }
2024
2025 if (copy_from_user(data_ptr, (void __user *)arg, data_len)) {
2026 ret = -EFAULT;
2027 goto exit;
2028 }
2029
2030 switch (cmd) {
2031 case AW_IOCTL_SET_CALI_RE: {
2032 aw_cali_store_cali_re(aw_dev, *((int32_t *)data_ptr));
2033 } break;
2034 default:{
2035 aw_dev_err(aw_dev->dev, "unsupported cmd %d", cmd);
2036 ret = -EINVAL;
2037 } break;
2038 }
2039
2040 exit:
2041 devm_kfree(aw_dev->dev, data_ptr);
2042 data_ptr = NULL;
2043 return ret;
2044 }
2045
aw_cali_misc_ops_read(struct aw_device * aw_dev,unsigned int cmd,unsigned long arg)2046 static int aw_cali_misc_ops_read(struct aw_device *aw_dev,
2047 unsigned int cmd, unsigned long arg)
2048 {
2049
2050 int16_t data_len = _IOC_SIZE(cmd);
2051 char *data_ptr = NULL;
2052 int32_t *data_32_ptr = NULL;
2053 int ret = 0;
2054
2055 data_ptr = devm_kzalloc(aw_dev->dev, data_len, GFP_KERNEL);
2056 if (!data_ptr) {
2057 aw_dev_err(aw_dev->dev, "malloc failed !");
2058 return -ENOMEM;
2059 }
2060
2061 data_32_ptr = (int32_t *)data_ptr;
2062 switch (cmd) {
2063 case AW_IOCTL_GET_RE: {
2064 ret = aw_cali_svc_dev_cali_re(aw_dev, CALI_OPS_HMUTE);
2065 if (ret < 0)
2066 goto exit;
2067
2068 ret = aw_cali_svc_get_dev_cali_val(aw_dev, GET_RE_TYPE, data_32_ptr);
2069 } break;
2070 case AW_IOCTL_GET_CALI_F0: {
2071 ret = aw_cali_svc_dev_cali_f0_q(aw_dev, CALI_OPS_NOISE);
2072 if (ret < 0)
2073 goto exit;
2074
2075 ret = aw_cali_svc_get_dev_cali_val(aw_dev, GET_F0_TYPE, data_32_ptr);
2076 } break;
2077 case AW_IOCTL_GET_F0: {
2078 ret = aw_cali_svc_get_dev_f0(aw_dev, data_32_ptr);
2079 } break;
2080 case AW_IOCTL_GET_TE: {
2081 ret = aw_cali_svc_get_dev_te(&aw_dev->cali_desc, data_32_ptr);
2082 } break;
2083 case AW_IOCTL_GET_REAL_R0: {
2084 ret = aw_cali_svc_get_dev_realtime_re(aw_dev, data_32_ptr);
2085 } break;
2086 case AW_IOCTL_GET_RE_RANGE: {
2087 ret = aw_cali_svc_get_dev_re_range(aw_dev, data_32_ptr);
2088 } break;
2089 default:{
2090 aw_dev_err(aw_dev->dev, "unsupported cmd %d", cmd);
2091 ret = -EINVAL;
2092 } break;
2093 }
2094
2095 exit:
2096 if (copy_to_user((void __user *)arg,
2097 data_ptr, data_len)) {
2098 ret = -EFAULT;
2099 }
2100
2101 devm_kfree(aw_dev->dev, data_ptr);
2102 data_ptr = NULL;
2103 return ret;
2104 }
2105
aw_cali_misc_ops(struct aw_device * aw_dev,unsigned int cmd,unsigned long arg)2106 static int aw_cali_misc_ops(struct aw_device *aw_dev,
2107 unsigned int cmd, unsigned long arg)
2108 {
2109 int ret = 0;
2110
2111 switch (cmd) {
2112 case AW_IOCTL_SET_CALI_RE:
2113 return aw_cali_misc_ops_write(aw_dev, cmd, arg);
2114 case AW_IOCTL_GET_F0:
2115 case AW_IOCTL_GET_CALI_F0:
2116 case AW_IOCTL_GET_RE:
2117 case AW_IOCTL_GET_REAL_R0:
2118 case AW_IOCTL_GET_TE:
2119 case AW_IOCTL_GET_RE_RANGE:
2120 return aw_cali_misc_ops_read(aw_dev, cmd, arg);
2121 default:
2122 aw_dev_err(aw_dev->dev, "unsupported cmd %d", cmd);
2123 ret = -EINVAL;
2124 break;
2125 }
2126
2127 return ret;
2128 }
2129
aw_cali_misc_unlocked_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2130 static long aw_cali_misc_unlocked_ioctl(struct file *file,
2131 unsigned int cmd, unsigned long arg)
2132 {
2133 int ret = 0;
2134 struct aw_device *aw_dev = NULL;
2135
2136 if (((_IOC_TYPE(cmd)) != (AW_IOCTL_MAGIC))) {
2137 aw_pr_err(" cmd magic err");
2138 return -EINVAL;
2139 }
2140 aw_dev = (struct aw_device *)file->private_data;
2141 ret = aw_cali_misc_ops(aw_dev, cmd, arg);
2142 if (ret < 0)
2143 return -EINVAL;
2144 return 0;
2145 }
2146
2147 #ifdef CONFIG_COMPAT
aw_cali_misc_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)2148 static long aw_cali_misc_compat_ioctl(struct file *file,
2149 unsigned int cmd, unsigned long arg)
2150 {
2151 int ret = 0;
2152 struct aw_device *aw_dev = NULL;
2153
2154 if (((_IOC_TYPE(cmd)) != (AW_IOCTL_MAGIC))) {
2155 aw_pr_err("cmd magic err");
2156 return -EINVAL;
2157 }
2158 aw_dev = (struct aw_device *)file->private_data;
2159 ret = aw_cali_misc_ops(aw_dev, cmd, arg);
2160 if (ret < 0)
2161 return -EINVAL;
2162
2163
2164 return 0;
2165 }
2166 #endif
2167
aw_cali_misc_read(struct file * filp,char __user * buf,size_t size,loff_t * pos)2168 static ssize_t aw_cali_misc_read(struct file *filp, char __user *buf, size_t size, loff_t *pos)
2169 {
2170 int len = 0;
2171 int i, ret;
2172 struct aw_device *aw_dev = (struct aw_device *)filp->private_data;
2173 char local_buf[512] = { 0 };
2174 uint32_t temp_data[AW_DEV_CH_MAX] = { 0 };
2175 uint32_t re_value[AW_DEV_RE_RANGE] = { 0 };
2176
2177 aw_dev_info(aw_dev->dev, "enter");
2178
2179 if (*pos) {
2180 *pos = 0;
2181 return 0;
2182 }
2183
2184 switch (g_msic_wr_flag) {
2185 case CALI_STR_SHOW_RE: {
2186 ret = aw_cali_svc_get_devs_cali_val(aw_dev, GET_RE_TYPE, temp_data, AW_DEV_CH_MAX);
2187 if (ret <= 0) {
2188 aw_dev_err(aw_dev->dev, "get re failed");
2189 return -EINVAL;
2190 } else {
2191 for (i = 0; i < ret; i++)
2192 len += snprintf(local_buf+len, sizeof(local_buf) - len, "dev[%d]:%u ", i, temp_data[i]);
2193
2194 len += snprintf(local_buf+len, sizeof(local_buf) - len, "\n");
2195 }
2196 } break;
2197 case CALI_STR_SHOW_R0: {
2198 ret = aw_cali_svc_get_devs_r0(aw_dev, temp_data, AW_DEV_CH_MAX);
2199 if (ret <= 0) {
2200 aw_dev_err(aw_dev->dev, "get r0 failed");
2201 return -EINVAL;
2202 } else {
2203 for (i = 0; i < ret; i++)
2204 len += snprintf(local_buf+len, sizeof(local_buf)-len,
2205 "dev[%d]:%d ", i, temp_data[i]);
2206 len += snprintf(local_buf+len, sizeof(local_buf)-len, "\n");
2207 }
2208 } break;
2209 case CALI_STR_SHOW_CALI_F0: {
2210 ret = aw_cali_svc_get_devs_cali_val(aw_dev, GET_F0_TYPE, temp_data, AW_DEV_CH_MAX);
2211 if (ret <= 0) {
2212 aw_dev_err(aw_dev->dev, "get cali f0 failed");
2213 return -EINVAL;
2214 } else {
2215 for (i = 0; i < ret; i++)
2216 len += snprintf(local_buf+len, sizeof(local_buf)-len,
2217 "dev[%d]:%d ", i, temp_data[i]);
2218
2219 len += snprintf(local_buf+len, sizeof(local_buf)-len, "\n");
2220 }
2221 } break;
2222 case CALI_STR_SHOW_F0: {
2223 ret = aw_cali_svc_get_devs_f0(aw_dev, temp_data, AW_DEV_CH_MAX);
2224 if (ret <= 0) {
2225 aw_dev_err(aw_dev->dev, "get f0 failed");
2226 return -EINVAL;
2227 } else {
2228 for (i = 0; i < ret; i++)
2229 len += snprintf(local_buf+len, sizeof(local_buf)-len,
2230 "dev[%d]:%d ", i, temp_data[i]);
2231
2232 len += snprintf(local_buf+len, sizeof(local_buf) - len, "\n");
2233 }
2234 } break;
2235 case CALI_STR_SHOW_TE: {
2236 ret = aw_cali_svc_get_devs_te(aw_dev, temp_data, AW_DEV_CH_MAX);
2237 if (ret <= 0) {
2238 aw_dev_err(aw_dev->dev, "get te failed");
2239 return -EINVAL;
2240 } else {
2241 for (i = 0; i < ret; i++)
2242 len += snprintf(local_buf+len, sizeof(local_buf)-len,
2243 "dev[%d]:%d ", i, temp_data[i]);
2244 len += snprintf(local_buf+len, sizeof(local_buf)-len, "\n");
2245 }
2246 } break;
2247 case CALI_STR_VER: {
2248 if (aw_dev->ops.aw_get_version) {
2249 len = aw_dev->ops.aw_get_version(local_buf, sizeof(local_buf));
2250 if (len < 0) {
2251 aw_dev_err(aw_dev->dev, "get version failed");
2252 return -EINVAL;
2253 }
2254 len += snprintf(local_buf+len, sizeof(local_buf) - len, "\n");
2255 } else {
2256 aw_dev_err(aw_dev->dev, "get version is NULL");
2257 return -EINVAL;
2258 }
2259 } break;
2260 case CALI_STR_SHOW_RE_RANGE: {
2261 ret = aw_cali_svc_get_devs_re_range(aw_dev, re_value, AW_DEV_CH_MAX);
2262 if (ret <= 0) {
2263 aw_dev_err(aw_dev->dev, "get re range failed");
2264 return -EINVAL;
2265 } else {
2266 for (i = 0; i < ret; i++) {
2267 len += snprintf(local_buf+len, sizeof(local_buf)-len,
2268 "dev[%d]:re_min:%d re_max:%d\n",
2269 i, re_value[RE_MIN_FLAG + i * RE_RANGE_NUM],
2270 re_value[RE_MAX_FLAG + i * RE_RANGE_NUM]);
2271 }
2272 }
2273 } break;
2274 default: {
2275 if (g_msic_wr_flag == CALI_STR_NONE) {
2276 aw_dev_info(aw_dev->dev, "please write cmd first");
2277 return -EINVAL;
2278 } else {
2279 aw_dev_err(aw_dev->dev, "unsupported flag [%d]", g_msic_wr_flag);
2280 g_msic_wr_flag = CALI_STR_NONE;
2281 return -EINVAL;
2282 }
2283 } break;
2284 }
2285
2286 if (copy_to_user((void __user *)buf, local_buf, len)) {
2287 aw_dev_err(aw_dev->dev, "copy_to_user error");
2288 g_msic_wr_flag = CALI_STR_NONE;
2289 return -EFAULT;
2290 }
2291
2292 g_msic_wr_flag = CALI_STR_NONE;
2293 *pos += len;
2294 return len;
2295
2296 }
2297
aw_cali_misc_switch_dev(struct file * filp,struct aw_device * aw_dev,char * cmd_buf)2298 static int aw_cali_misc_switch_dev(struct file *filp, struct aw_device *aw_dev, char *cmd_buf)
2299 {
2300 int dev_select_num;
2301 struct list_head *dev_list = NULL;
2302 struct list_head *pos = NULL;
2303 struct aw_device *local_dev = NULL;
2304 int ret;
2305
2306 /*get sel dev str*/
2307 sscanf(cmd_buf, "dev_sel:dev[%d]", &dev_select_num);
2308
2309 if (dev_select_num >= AW_DEV_CH_MAX) {
2310 aw_dev_err(aw_dev->dev, "unsupport str [%s]", cmd_buf);
2311 return -EINVAL;
2312 }
2313
2314 /*get dev list*/
2315 ret = aw_dev_get_list_head(&dev_list);
2316 if (ret) {
2317 aw_dev_err(aw_dev->dev, "get dev list failed");
2318 return ret;
2319 }
2320
2321 /*find sel dev*/
2322 list_for_each (pos, dev_list) {
2323 local_dev = container_of(pos, struct aw_device, list_node);
2324 if (local_dev->channel == dev_select_num) {
2325 filp->private_data = (void *)local_dev;
2326 g_dev_select = dev_select_num;
2327 aw_dev_info(local_dev->dev, "switch to dev[%d]", dev_select_num);
2328 return 0;
2329 }
2330 }
2331 aw_dev_err(aw_dev->dev, " unsupport [%s]", cmd_buf);
2332 return -EINVAL;
2333 }
2334
aw_cali_misc_write(struct file * filp,const char __user * buf,size_t size,loff_t * pos)2335 static ssize_t aw_cali_misc_write(struct file *filp, const char __user *buf, size_t size, loff_t *pos)
2336 {
2337 char *kernel_buf = NULL;
2338 struct aw_device *aw_dev = (struct aw_device *)filp->private_data;
2339 int ret = 0;
2340
2341 aw_dev_info(aw_dev->dev, "enter, write size:%d", (int)size);
2342 kernel_buf = kzalloc(size + 1, GFP_KERNEL);
2343 if (kernel_buf == NULL) {
2344 aw_dev_err(aw_dev->dev, "kzalloc failed !");
2345 return -ENOMEM;
2346 }
2347
2348 if (copy_from_user(kernel_buf,
2349 (void __user *)buf,
2350 size)) {
2351 ret = -EFAULT;
2352 goto exit;
2353 }
2354
2355 ret = aw_cali_svc_get_cmd_form_str(aw_dev, kernel_buf);
2356 if (ret < 0) {
2357 aw_dev_err(aw_dev->dev, "upported cmd [%s]!", kernel_buf);
2358 ret = -EINVAL;
2359 goto exit;
2360 }
2361
2362 switch (ret) {
2363 case CALI_STR_CALI_RE_F0: {
2364 ret = aw_cali_svc_cali_cmd(aw_dev, AW_CALI_CMD_RE_F0,
2365 is_single_cali, CALI_OPS_HMUTE|CALI_OPS_NOISE);
2366 } break;
2367 case CALI_STR_CALI_RE: {
2368 ret = aw_cali_svc_cali_cmd(aw_dev, AW_CALI_CMD_RE,
2369 is_single_cali, CALI_OPS_HMUTE);
2370 } break;
2371 case CALI_STR_CALI_F0: {
2372 ret = aw_cali_svc_cali_cmd(aw_dev, AW_CALI_CMD_F0,
2373 is_single_cali, CALI_OPS_HMUTE|CALI_OPS_NOISE);
2374 } break;
2375 case CALI_STR_SET_RE: {
2376 /*skip store_re*/
2377 ret = aw_cali_svc_set_devs_re_str(aw_dev,
2378 kernel_buf + strlen(cali_str[CALI_STR_SET_RE]) + 1);
2379 } break;
2380 case CALI_STR_DEV_SEL: {
2381 ret = aw_cali_misc_switch_dev(filp, aw_dev, kernel_buf);
2382 } break;
2383 case CALI_STR_SHOW_RE: /*show cali_re*/
2384 case CALI_STR_SHOW_R0: /*show real r0*/
2385 case CALI_STR_SHOW_CALI_F0: /*GET DEV CALI_F0*/
2386 case CALI_STR_SHOW_F0: /*SHOW REAL F0*/
2387 case CALI_STR_SHOW_TE:
2388 case CALI_STR_VER:
2389 case CALI_STR_SHOW_RE_RANGE: {
2390 g_msic_wr_flag = ret;
2391 ret = 0;
2392 } break;
2393 default: {
2394 aw_dev_err(aw_dev->dev, "unsupported [%s]!", kernel_buf);
2395 ret = -EINVAL;
2396 } break;
2397 };
2398
2399 exit:
2400 aw_dev_dbg(aw_dev->dev, "cmd [%s]!", kernel_buf);
2401 if (kernel_buf) {
2402 kfree(kernel_buf);
2403 kernel_buf = NULL;
2404 }
2405 if (ret < 0)
2406 return -EINVAL;
2407 else
2408 return size;
2409 }
2410
2411 static const struct file_operations aw_cali_misc_fops = {
2412 .owner = THIS_MODULE,
2413 .open = aw_cali_misc_open,
2414 .read = aw_cali_misc_read,
2415 .write = aw_cali_misc_write,
2416 .release = aw_cali_misc_release,
2417 .unlocked_ioctl = aw_cali_misc_unlocked_ioctl,
2418 #ifdef CONFIG_COMPAT
2419 .compat_ioctl = aw_cali_misc_compat_ioctl,
2420 #endif
2421 };
2422
2423 struct miscdevice misc_cali = {
2424 .name = "aw_smartpa",
2425 .minor = MISC_DYNAMIC_MINOR,
2426 .fops = &aw_cali_misc_fops,
2427 };
2428
aw_cali_misc_init(struct aw_device * aw_dev)2429 static int aw_cali_misc_init(struct aw_device *aw_dev)
2430 {
2431 int ret;
2432
2433 mutex_lock(&g_cali_lock);
2434 if (g_misc_dev == NULL) {
2435 ret = misc_register(&misc_cali);
2436 if (ret) {
2437 aw_dev_err(aw_dev->dev, "misc register fail: %d\n", ret);
2438 mutex_unlock(&g_cali_lock);
2439 return -EINVAL;
2440 }
2441 g_misc_dev = &misc_cali;
2442 aw_dev_dbg(aw_dev->dev, "misc register success");
2443 } else {
2444 aw_dev_dbg(aw_dev->dev, "misc already register");
2445 }
2446 mutex_unlock(&g_cali_lock);
2447
2448 return 0;
2449 }
2450
aw_cali_misc_deinit(struct aw_device * aw_dev)2451 static void aw_cali_misc_deinit(struct aw_device *aw_dev)
2452 {
2453 mutex_lock(&g_cali_lock);
2454 if (g_misc_dev) {
2455 misc_deregister(g_misc_dev);
2456 g_misc_dev = NULL;
2457 }
2458 mutex_unlock(&g_cali_lock);
2459 aw_dev_dbg(aw_dev->dev, " misc unregister done");
2460 }
2461 /*****************************misc cali******************************************************/
2462
aw_cali_parse_dt(struct aw_device * aw_dev)2463 static void aw_cali_parse_dt(struct aw_device *aw_dev)
2464 {
2465 struct device_node *np = aw_dev->dev->of_node;
2466 int ret = -1;
2467 uint32_t cali_check = CALI_CHECK_DISABLE;
2468 struct aw_cali_desc *desc = &aw_dev->cali_desc;
2469
2470 ret = of_property_read_u32(np, "aw-cali-check", &cali_check);
2471 if (ret < 0) {
2472 aw_dev_info(aw_dev->dev, " cali-check get failed ,default turn off");
2473 cali_check = CALI_CHECK_DISABLE;
2474 }
2475
2476 desc->cali_check_st = cali_check;
2477 aw_dev_info(aw_dev->dev, "cali check :%s",
2478 (desc->cali_check_st) ? "enable" : "disable");
2479 }
2480
aw_cali_init(struct aw_cali_desc * cali_desc)2481 void aw_cali_init(struct aw_cali_desc *cali_desc)
2482 {
2483 struct aw_device *aw_dev =
2484 container_of(cali_desc, struct aw_device, cali_desc);
2485
2486 memset(cali_desc, 0, sizeof(struct aw_cali_desc));
2487
2488 aw_cali_parse_dt(aw_dev);
2489
2490 aw_cali_attr_init(aw_dev);
2491
2492 aw_cali_class_attr_init(aw_dev);
2493
2494 aw_cali_misc_init(aw_dev);
2495
2496 cali_desc->cali_result = CALI_RESULT_NONE;
2497 }
2498
aw_cali_deinit(struct aw_cali_desc * cali_desc)2499 void aw_cali_deinit(struct aw_cali_desc *cali_desc)
2500 {
2501 struct aw_device *aw_dev =
2502 container_of(cali_desc, struct aw_device, cali_desc);
2503
2504 aw_cali_attr_deinit(aw_dev);
2505
2506 aw_cali_class_attr_deinit(aw_dev);
2507
2508 aw_cali_misc_deinit(aw_dev);
2509 }
2510