xref: /OK3568_Linux_fs/kernel/sound/soc/codecs/aw883xx/aw_monitor.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * awinic_monitor.c monitor_module
4  *
5  * Version: v0.1.17
6  *
7  * Copyright (c) 2019 AWINIC Technology CO., LTD
8  *
9  *  Author: Nick Li <liweilei@awinic.com.cn>
10  *
11  * This program is free software; you can redistribute  it and/or modify it
12  * under  the terms of  the GNU General  Public License as published by the
13  * Free Software Foundation;  either version 2 of the  License, or (at your
14  * option) any later version.
15  */
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <sound/core.h>
19 #include <sound/pcm.h>
20 #include <sound/pcm_params.h>
21 #include <sound/soc.h>
22 #include <linux/of_gpio.h>
23 #include <linux/delay.h>
24 #include <linux/device.h>
25 #include <linux/firmware.h>
26 #include <linux/of.h>
27 #include <linux/version.h>
28 #include <linux/input.h>
29 #include <linux/timer.h>
30 #include <linux/workqueue.h>
31 #include <linux/hrtimer.h>
32 #include <linux/power_supply.h>
33 #include "aw_monitor.h"
34 #include "aw_log.h"
35 #include "aw_device.h"
36 #include "aw883xx.h"
37 #include "aw_calib.h"
38 
39 
40 #define AW883XX_MONITOR_NAME "aw883xx_monitor.bin"
41 
42 /*****************************************************
43  * device monitor
44  *****************************************************/
45  #ifndef AW_SYS_BATTERY_ST
aw_monitor_get_chip_voltage(struct aw_device * aw_dev,unsigned int * vol)46 static int aw_monitor_get_chip_voltage(struct aw_device *aw_dev,
47 						unsigned int *vol)
48 {
49 	int ret = -1;
50 	uint16_t local_vol = 0;
51 	struct aw_voltage_desc *desc = &aw_dev->voltage_desc;
52 
53 	ret = aw_dev->ops.aw_reg_read(aw_dev, desc->reg, (uint16_t *)vol);
54 	if (ret < 0) {
55 		aw_dev_err(aw_dev->dev, "read voltage failed!");
56 		return ret;
57 	}
58 
59 	if (desc->int_bit == 0) {
60 		aw_dev_err(aw_dev->dev, "desc->int_bit:%d unsupported", desc->int_bit);
61 		return -EINVAL;
62 	}
63 
64 	local_vol = ((*vol) * desc->vbat_range) / desc->int_bit;
65 
66 	*vol = local_vol;
67 
68 	aw_dev_info(aw_dev->dev, "chip voltage is %d", *vol);
69 
70 	return 0;
71 }
72 
aw_monitor_get_chip_temperature(struct aw_device * aw_dev,int * temp)73 static int aw_monitor_get_chip_temperature(struct aw_device *aw_dev, int *temp)
74 {
75 	int ret = -1;
76 	uint16_t reg_val = 0;
77 	uint16_t local_temp;
78 	struct aw_temperature_desc *desc = &aw_dev->temp_desc;
79 
80 	ret = aw_dev->ops.aw_reg_read(aw_dev, desc->reg, &reg_val);
81 	if (ret < 0) {
82 		aw_dev_err(aw_dev->dev, "get temperature failed!");
83 		return ret;
84 	}
85 
86 	local_temp = reg_val;
87 
88 	if (local_temp & (~desc->sign_mask))
89 		local_temp = local_temp | desc->neg_mask;
90 
91 	*temp = (int)((int16_t)local_temp);
92 
93 	aw_dev_info(aw_dev->dev, "chip temperature = %d", *temp);
94 	return 0;
95 }
96 #endif
97 
aw_monitor_get_battery_state(struct aw_device * aw_dev,int * data,int data_type)98 static int aw_monitor_get_battery_state(struct aw_device *aw_dev,
99 			int *data, int data_type)
100 {
101 	char name[] = "battery";
102 	int ret = -1;
103 	union power_supply_propval prop = { 0 };
104 	struct power_supply *psy = NULL;
105 
106 	psy = power_supply_get_by_name(name);
107 	if (psy) {
108 		ret = power_supply_get_property(psy, data_type, &prop);
109 		if (ret < 0) {
110 			aw_dev_err(aw_dev->dev, "get data failed");
111 			return -EINVAL;
112 		}
113 		*data = prop.intval;
114 		aw_dev_dbg(aw_dev->dev, "get system data: %d", *data);
115 	} else {
116 		aw_dev_err(aw_dev->dev, "no struct power supply name : %s", name);
117 		return -EINVAL;
118 	}
119 
120 	return 0;
121 }
122 
aw_monitor_get_system_temperature(struct aw_device * aw_dev,int * temp)123 static int aw_monitor_get_system_temperature(struct aw_device *aw_dev, int *temp)
124 {
125 	int ret = -1;
126 	int sys_temp;
127 
128 	ret = aw_monitor_get_battery_state(aw_dev, &sys_temp, POWER_SUPPLY_PROP_TEMP);
129 	if (ret < 0) {
130 		aw_dev_err(aw_dev->dev, "get system temperature failed!");
131 		return ret;
132 	}
133 
134 	*temp = sys_temp / 10;
135 	aw_dev_dbg(aw_dev->dev, "system temperature = %d", *temp);
136 
137 	return ret;
138 }
139 
aw_monitor_set_system_temperature(struct aw_device * aw_dev,int sys_temp)140 static int aw_monitor_set_system_temperature(struct aw_device *aw_dev, int sys_temp)
141 {
142 	int ret = -1;
143 	struct aw_hw_temp_desc *hw_temp_desc = &aw_dev->hw_temp_desc;
144 
145 	ret = aw_dev->ops.aw_dsp_write(aw_dev, hw_temp_desc->dsp_reg,
146 				sys_temp, hw_temp_desc->data_type);
147 	if (ret < 0) {
148 		aw_dev_err(aw_dev->dev, "set system temperature failed!");
149 		return ret;
150 	}
151 
152 	return ret;
153 }
154 
155 #ifdef AW_SYS_BATTERY_ST
aw_monitor_get_system_voltage(struct aw_device * aw_dev,unsigned int * vol)156 static int aw_monitor_get_system_voltage(struct aw_device *aw_dev, unsigned int *vol)
157 {
158 	int ret = -1;
159 	unsigned int sys_vol;
160 
161 	ret = aw_monitor_get_battery_state(aw_dev, &sys_vol, POWER_SUPPLY_PROP_VOLTAGE_NOW);
162 	if (ret < 0) {
163 		aw_dev_err(aw_dev->dev, "get system voltage failed!");
164 		return ret;
165 	}
166 
167 	*vol = sys_vol / 1000;
168 	aw_dev_dbg(aw_dev->dev, "system voltage = %d", *vol);
169 
170 	return ret;
171 }
172 #endif
173 
aw_monitor_get_temperature(struct aw_device * aw_dev,int * temp)174 static int aw_monitor_get_temperature(struct aw_device *aw_dev, int *temp)
175 {
176 #ifdef AW_SYS_BATTERY_ST
177 	return aw_monitor_get_system_temperature(aw_dev, temp);
178 #else
179 	return aw_monitor_get_chip_temperature(aw_dev, temp);
180 #endif
181 }
182 
aw_monitor_get_voltage(struct aw_device * aw_dev,unsigned int * vol)183 static int aw_monitor_get_voltage(struct aw_device *aw_dev, unsigned int *vol)
184 {
185 #ifdef AW_SYS_BATTERY_ST
186 	return aw_monitor_get_system_voltage(aw_dev, vol);
187 #else
188 	return aw_monitor_get_chip_voltage(aw_dev, vol);
189 #endif
190 }
191 
aw_monitor_get_temp_and_vol(struct aw_device * aw_dev)192 static int aw_monitor_get_temp_and_vol(struct aw_device *aw_dev)
193 {
194 	struct aw_monitor_desc *monitor = &aw_dev->monitor_desc;
195 	unsigned int voltage = 0;
196 	int current_temp = 0;
197 	int ret = -1;
198 
199 #ifdef AW_DEBUG
200 	if (monitor->test_vol == 0) {
201 		ret = aw_monitor_get_voltage(aw_dev, &voltage);
202 		if (ret < 0)
203 			return ret;
204 	} else {
205 		voltage = monitor->test_vol;
206 	}
207 
208 	if (monitor->test_temp == 0) {
209 		ret = aw_monitor_get_temperature(aw_dev, &current_temp);
210 		if (ret < 0)
211 			return ret;
212 	} else {
213 		current_temp = monitor->test_temp;
214 	}
215 #else
216 	ret = aw_monitor_get_voltage(aw_dev, &voltage);
217 	if (ret < 0)
218 		return ret;
219 
220 	ret = aw_monitor_get_temperature(aw_dev, &current_temp);
221 	if (ret < 0)
222 		return ret;
223 #endif
224 
225 	monitor->vol_trace.sum_val += voltage;
226 	monitor->temp_trace.sum_val += current_temp;
227 	monitor->samp_count++;
228 
229 	return 0;
230 }
231 
aw_monitor_first_get_data_form_table(struct aw_device * aw_dev,struct aw_table_info table_info,struct aw_monitor_trace * data_trace)232 static int aw_monitor_first_get_data_form_table(struct aw_device *aw_dev,
233 				struct aw_table_info table_info,
234 			struct aw_monitor_trace *data_trace)
235 {
236 	int i;
237 
238 	if (table_info.aw_table == NULL) {
239 		aw_dev_err(aw_dev->dev, "table_info.aw_table is null");
240 		return -EINVAL;
241 	}
242 
243 	for (i = 0; i < table_info.table_num; i++) {
244 		if (data_trace->sum_val >= table_info.aw_table[i].min_val) {
245 			memcpy(&data_trace->aw_table, &table_info.aw_table[i],
246 				sizeof(struct aw_table));
247 			break;
248 		}
249 	}
250 	return 0;
251 }
252 
aw_monitor_trace_data_from_table(struct aw_device * aw_dev,struct aw_table_info table_info,struct aw_monitor_trace * data_trace)253 static int aw_monitor_trace_data_from_table(struct aw_device *aw_dev,
254 			struct aw_table_info table_info,
255 			struct aw_monitor_trace *data_trace)
256 {
257 	int i;
258 
259 	if (table_info.aw_table == NULL) {
260 		aw_dev_err(aw_dev->dev, "table_info.aw_table is null");
261 		return -EINVAL;
262 	}
263 
264 	for (i = 0; i < table_info.table_num; i++) {
265 		if (data_trace->sum_val >= table_info.aw_table[i].min_val &&
266 			data_trace->sum_val <= table_info.aw_table[i].max_val) {
267 			memcpy(&data_trace->aw_table, &table_info.aw_table[i],
268 				sizeof(struct aw_table));
269 			break;
270 		}
271 	}
272 
273 	return 0;
274 }
275 
aw_monitor_get_data_from_table(struct aw_device * aw_dev,struct aw_table_info table_info,struct aw_monitor_trace * data_trace,uint32_t aplha)276 static int aw_monitor_get_data_from_table(struct aw_device *aw_dev,
277 					struct aw_table_info table_info,
278 					struct aw_monitor_trace *data_trace,
279 					uint32_t aplha)
280 {
281 	struct aw_monitor_desc *monitor = &aw_dev->monitor_desc;
282 
283 	if (monitor->first_entry == AW_FIRST_ENTRY) {
284 		return aw_monitor_first_get_data_form_table(aw_dev,
285 						table_info, data_trace);
286 	} else {
287 		if (monitor->samp_count == 0) {
288 			aw_dev_err(aw_dev->dev, "monitor->samp_count:%d unsupported", monitor->samp_count);
289 			return -EINVAL;
290 		}
291 
292 		data_trace->sum_val = data_trace->sum_val / monitor->samp_count;
293 		data_trace->sum_val = ((int32_t)aplha * data_trace->sum_val +
294 			(1000 - (int32_t)aplha) * data_trace->pre_val) / 1000;
295 		return aw_monitor_trace_data_from_table(aw_dev,
296 						table_info, data_trace);
297 	}
298 
299 	return 0;
300 }
301 
aw_monitor_get_data(struct aw_device * aw_dev)302 static int aw_monitor_get_data(struct aw_device *aw_dev)
303 {
304 	struct aw_monitor_desc *monitor = &aw_dev->monitor_desc;
305 	struct aw_monitor_cfg *monitor_cfg = &monitor->monitor_cfg;
306 	struct aw_monitor_trace *vol_trace = &monitor->vol_trace;
307 	struct aw_monitor_trace *temp_trace = &monitor->temp_trace;
308 	int ret;
309 
310 	if (monitor_cfg->vol_switch) {
311 		ret = aw_monitor_get_data_from_table(aw_dev,
312 			monitor_cfg->vol_info, vol_trace,
313 			monitor_cfg->vol_aplha);
314 		if (ret < 0)
315 			return ret;
316 	} else {
317 		vol_trace->aw_table.ipeak = IPEAK_NONE;
318 		vol_trace->aw_table.gain = GAIN_NONE;
319 		vol_trace->aw_table.vmax = VMAX_NONE;
320 	}
321 
322 	if (monitor_cfg->temp_switch) {
323 		ret = aw_monitor_get_data_from_table(aw_dev,
324 			monitor_cfg->temp_info, temp_trace,
325 			monitor_cfg->temp_aplha);
326 		if (ret < 0)
327 			return ret;
328 	} else {
329 		temp_trace->aw_table.ipeak = IPEAK_NONE;
330 		temp_trace->aw_table.gain = GAIN_NONE;
331 		temp_trace->aw_table.vmax = VMAX_NONE;
332 	}
333 
334 	aw_dev_dbg(aw_dev->dev,
335 			"filter_vol:%d, vol: ipeak = 0x%x, gain = 0x%x, vmax = 0x%x",
336 			monitor->vol_trace.sum_val, vol_trace->aw_table.ipeak,
337 			vol_trace->aw_table.gain, vol_trace->aw_table.vmax);
338 
339 	aw_dev_dbg(aw_dev->dev,
340 			"filter_temp:%d, temp: ipeak = 0x%x, gain = 0x%x, vmax = 0x%x",
341 			monitor->temp_trace.sum_val, temp_trace->aw_table.ipeak,
342 			temp_trace->aw_table.gain, temp_trace->aw_table.vmax);
343 	return 0;
344 }
345 
aw_monitor_get_cfg(struct aw_device * aw_dev,struct aw_table * set_table)346 static void aw_monitor_get_cfg(struct aw_device *aw_dev,
347 					struct aw_table *set_table)
348 {
349 	struct aw_monitor_desc *monitor = &aw_dev->monitor_desc;
350 	struct aw_table *temp_data = &monitor->temp_trace.aw_table;
351 	struct aw_table *vol_data = &monitor->vol_trace.aw_table;
352 
353 	if (temp_data->ipeak == IPEAK_NONE && vol_data->ipeak == IPEAK_NONE) {
354 		memcpy(set_table, temp_data, sizeof(struct aw_table));
355 	} else if (temp_data->ipeak == IPEAK_NONE) {
356 		memcpy(set_table, vol_data, sizeof(struct aw_table));
357 	} else if (vol_data->ipeak == IPEAK_NONE) {
358 		memcpy(set_table, temp_data, sizeof(struct aw_table));
359 	} else {
360 		if (monitor->monitor_cfg.logic_switch == AW_MON_LOGIC_OR) {
361 			set_table->ipeak = (temp_data->ipeak < vol_data->ipeak ?
362 					temp_data->ipeak : vol_data->ipeak);
363 			set_table->gain = (temp_data->gain < vol_data->gain ?
364 					vol_data->gain : temp_data->gain);
365 			set_table->vmax = (temp_data->vmax < vol_data->vmax ?
366 					vol_data->vmax : temp_data->vmax);
367 		} else {
368 			set_table->ipeak = (temp_data->ipeak < vol_data->ipeak ?
369 				vol_data->ipeak : temp_data->ipeak);
370 			set_table->gain = (temp_data->gain < vol_data->gain ?
371 					temp_data->gain : vol_data->gain);
372 			set_table->vmax = (temp_data->vmax < vol_data->vmax ?
373 					temp_data->vmax : vol_data->vmax);
374 		}
375 	}
376 }
377 
aw_monitor_set_ipeak(struct aw_device * aw_dev,uint16_t ipeak)378 static void aw_monitor_set_ipeak(struct aw_device *aw_dev,
379 				uint16_t ipeak)
380 {
381 	struct aw_monitor_cfg *monitor_cfg = &aw_dev->monitor_desc.monitor_cfg;
382 	uint16_t reg_val = 0;
383 	uint16_t read_reg_val;
384 	int ret;
385 	struct aw_ipeak_desc *desc = &aw_dev->ipeak_desc;
386 
387 	if (ipeak == IPEAK_NONE || (!monitor_cfg->ipeak_switch))
388 		return;
389 
390 	ret = aw_dev->ops.aw_reg_read(aw_dev, desc->reg, &reg_val);
391 	if (ret < 0) {
392 		aw_dev_err(aw_dev->dev, "read ipeak failed");
393 		return;
394 	}
395 
396 	read_reg_val = reg_val;
397 	read_reg_val &= (~desc->mask);
398 
399 	if (read_reg_val == ipeak) {
400 		aw_dev_dbg(aw_dev->dev, "ipeak = 0x%x, no change",
401 					read_reg_val);
402 		return;
403 	}
404 	reg_val &= desc->mask;
405 	read_reg_val = ipeak;
406 	reg_val |= read_reg_val;
407 
408 	ret = aw_dev->ops.aw_reg_write(aw_dev, desc->reg, reg_val);
409 	if (ret < 0) {
410 		aw_dev_err(aw_dev->dev, "write ipeak failed");
411 		return;
412 	}
413 	aw_dev_info(aw_dev->dev, "set reg val = 0x%x, ipeak = 0x%x",
414 				reg_val, ipeak);
415 
416 }
417 
aw_monitor_set_gain(struct aw_device * aw_dev,uint16_t gain)418 static void aw_monitor_set_gain(struct aw_device *aw_dev, uint16_t gain)
419 {
420 	struct aw_monitor_cfg *monitor_cfg = &aw_dev->monitor_desc.monitor_cfg;
421 	uint16_t read_volume;
422 	uint16_t set_volume;
423 	int ret;
424 
425 	if (gain == GAIN_NONE || (!monitor_cfg->gain_switch))
426 		return;
427 
428 	ret = aw_dev->ops.aw_get_volume(aw_dev, &read_volume);
429 	if (ret < 0) {
430 		aw_dev_err(aw_dev->dev, "read volume failed");
431 		return;
432 	}
433 
434 	gain = aw_dev->ops.aw_reg_val_to_db(gain);
435 
436 	/*add offset*/
437 	set_volume = gain + aw_dev->volume_desc.init_volume;
438 
439 	if (read_volume == set_volume) {
440 		aw_dev_dbg(aw_dev->dev, "gain = 0x%x, no change", read_volume);
441 		return;
442 	}
443 
444 	ret = aw_dev->ops.aw_set_volume(aw_dev, set_volume);
445 	if (ret < 0) {
446 		aw_dev_err(aw_dev->dev, "set gain failed");
447 		return;
448 	}
449 	aw_dev_info(aw_dev->dev, "set reg val = 0x%x, gain = 0x%x",
450 				set_volume, gain);
451 
452 }
453 
aw_monitor_vmax_check(struct aw_device * aw_dev)454 static int aw_monitor_vmax_check(struct aw_device *aw_dev)
455 {
456 	int ret = -1;
457 
458 	ret = aw_dev_syspll_check(aw_dev);
459 	if (ret < 0) {
460 		aw_dev_err(aw_dev->dev, "no iis signal");
461 		return ret;
462 	}
463 
464 	ret = aw_dev_get_dsp_status(aw_dev);
465 	if (ret < 0) {
466 		aw_dev_err(aw_dev->dev, "dsp not work");
467 		return ret;
468 	}
469 
470 	return 0;
471 }
472 
aw_monitor_set_vmax(struct aw_device * aw_dev,uint32_t vmax)473 static void aw_monitor_set_vmax(struct aw_device *aw_dev,
474 						uint32_t vmax)
475 {
476 	struct aw_monitor_cfg *monitor_cfg = &aw_dev->monitor_desc.monitor_cfg;
477 	struct aw_vmax_desc *desc = &aw_dev->vmax_desc;
478 	uint16_t vmax_set;
479 	int ret = -1;
480 
481 	if (vmax == VMAX_NONE || (!monitor_cfg->vmax_switch))
482 		return;
483 
484 	ret = aw_monitor_vmax_check(aw_dev);
485 	if (ret < 0) {
486 		aw_dev_err(aw_dev->dev, "vmax_check fail, ret=%d", ret);
487 		return;
488 	}
489 
490 	if ((vmax == aw_dev->monitor_desc.pre_vmax) &&
491 		(aw_dev->monitor_desc.first_entry != AW_FIRST_ENTRY)) {
492 		aw_dev_dbg(aw_dev->dev, "vmax = 0x%x, no change", vmax);
493 		return;
494 	}
495 
496 	vmax_set = (uint16_t)((int16_t)vmax +
497 		(int16_t)aw_dev->vmax_desc.init_vmax);
498 
499 
500 	ret = aw_dev->ops.aw_dsp_write(aw_dev,
501 		desc->dsp_reg, vmax_set, desc->data_type);
502 	if (ret < 0)
503 		return;
504 
505 	aw_dev_info(aw_dev->dev, "get monitor vmax = 0x%x, set vmax = 0x%x",
506 		vmax, vmax_set);
507 }
508 
aw_monitor_check_sysint(struct aw_device * aw_dev)509 static void aw_monitor_check_sysint(struct aw_device *aw_dev)
510 {
511 	int ret = -1;
512 	uint16_t sysint;
513 	struct aw_int_desc *desc = &aw_dev->int_desc;
514 
515 	ret = aw_dev_get_int_status(aw_dev, &sysint);
516 	if (ret < 0)
517 		aw_dev_err(aw_dev->dev, "get_sysint fail, ret=%d", ret);
518 
519 	if (sysint & (desc->intst_mask)) {
520 		desc->sysint_st = sysint;
521 		aw_dev_err(aw_dev->dev, "check sysint fail, reg=0x%04x", sysint);
522 	}
523 }
524 
aw_monitor_work(struct aw_device * aw_dev)525 static int aw_monitor_work(struct aw_device *aw_dev)
526 {
527 	struct aw_monitor_desc *monitor = &aw_dev->monitor_desc;
528 	struct aw_monitor_cfg *monitor_cfg = &monitor->monitor_cfg;
529 	struct aw_table set_table;
530 	int ret = -1;
531 
532 	if (aw_cali_svc_get_cali_status(&aw_dev->cali_desc)) {
533 		aw_dev_info(aw_dev->dev, "done nothing during calibration");
534 		return 0;
535 	}
536 
537 	ret = aw_monitor_get_temp_and_vol(aw_dev);
538 	if (ret < 0)
539 		return ret;
540 
541 	if (monitor->samp_count < monitor_cfg->monitor_count &&
542 		(monitor->first_entry == AW_NOT_FIRST_ENTRY))
543 		return 0;
544 
545 	ret = aw_monitor_get_data(aw_dev);
546 	if (ret < 0)
547 		return ret;
548 
549 	aw_monitor_get_cfg(aw_dev, &set_table);
550 
551 	aw_dev_dbg(aw_dev->dev,
552 		"set_ipeak = 0x%x, set_gain = 0x%x, set_vmax = 0x%x",
553 		set_table.ipeak, set_table.gain, set_table.vmax);
554 
555 	aw_monitor_set_ipeak(aw_dev, set_table.ipeak);
556 
557 	aw_monitor_set_gain(aw_dev, set_table.gain);
558 
559 	aw_monitor_set_vmax(aw_dev, set_table.vmax);
560 
561 	aw_monitor_check_sysint(aw_dev);
562 
563 	monitor->samp_count = 0;
564 	monitor->temp_trace.pre_val = monitor->temp_trace.sum_val;
565 	monitor->temp_trace.sum_val = 0;
566 
567 	monitor->vol_trace.pre_val = monitor->vol_trace.sum_val;
568 	monitor->vol_trace.sum_val = 0;
569 
570 	if (monitor->first_entry == AW_FIRST_ENTRY)
571 		monitor->first_entry = AW_NOT_FIRST_ENTRY;
572 
573 	return 0;
574 }
575 
aw_monitor_work_func(struct work_struct * work)576 static void aw_monitor_work_func(struct work_struct *work)
577 {
578 	struct aw_device *aw_dev  = container_of(work,
579 		struct aw_device, monitor_desc.delay_work.work);
580 	struct aw883xx *aw883xx = (struct aw883xx *)aw_dev->private_data;
581 	struct aw_monitor_cfg *monitor_cfg = &aw_dev->monitor_desc.monitor_cfg;
582 	struct aw_monitor_desc *monitor = &aw_dev->monitor_desc;
583 
584 	aw_dev_dbg(aw_dev->dev, "scene_mode %d,monitor_status:%d, monitor_switch:%d",
585 		aw_dev->cur_prof, monitor_cfg->monitor_status,
586 		monitor_cfg->monitor_switch);
587 
588 	if ((monitor_cfg->monitor_status == AW_MON_CFG_OK) &&
589 		monitor_cfg->monitor_switch) {
590 
591 		if (!aw_dev_get_hmute(aw_dev)) {
592 			aw_monitor_work(aw_dev);
593 			queue_delayed_work(aw883xx->work_queue,
594 				&monitor->delay_work,
595 				msecs_to_jiffies(monitor_cfg->monitor_time));
596 		}
597 
598 	}
599 }
600 
aw_hw_monitor_work(struct aw_device * aw_dev)601 static int aw_hw_monitor_work(struct aw_device *aw_dev)
602 {
603 	int32_t sys_temp = 0;
604 
605 	aw_monitor_get_system_temperature(aw_dev, &sys_temp);
606 
607 	aw_monitor_set_system_temperature(aw_dev, sys_temp);
608 
609 	return 0;
610 }
611 
aw_hw_monitor_work_func(struct work_struct * work)612 static void aw_hw_monitor_work_func(struct work_struct *work)
613 {
614 	struct aw_device *aw_dev  = container_of(work,
615 		struct aw_device, monitor_desc.hw_monitor_work.work);
616 	struct aw883xx *aw883xx = (struct aw883xx *)aw_dev->private_data;
617 	struct aw_monitor_desc *monitor = &aw_dev->monitor_desc;
618 
619 	if (!aw_dev_get_hmute(aw_dev)) {
620 		aw_hw_monitor_work(aw_dev);
621 		queue_delayed_work(aw883xx->work_queue,
622 					&monitor->hw_monitor_work,
623 					msecs_to_jiffies(monitor->hw_monitor_delay));
624 	}
625 }
626 
aw_monitor_start(struct aw_monitor_desc * monitor_desc)627 void aw_monitor_start(struct aw_monitor_desc *monitor_desc)
628 {
629 	uint8_t temp_flag = monitor_desc->hw_temp_flag;
630 	struct aw_device *aw_dev = container_of(monitor_desc,
631 			struct aw_device, monitor_desc);
632 	struct aw883xx *aw883xx = (struct aw883xx *)aw_dev->private_data;
633 
634 	aw_dev_info(aw_dev->dev, "enter");
635 
636 	if (aw_dev->profctrl_desc.cur_mode == AW_RCV_MODE) {
637 		aw_dev_dbg(aw_dev->dev, "receiver mode no need to start monitor");
638 		return;
639 	}
640 
641 	if (monitor_desc->hw_mon_en) {
642 		aw_dev_info(aw_dev->dev, "hardware monitor is enable");
643 		if (temp_flag == AW_EXTERNAL_TEMP) {
644 			queue_delayed_work(aw883xx->work_queue,
645 					&monitor_desc->hw_monitor_work, 0);
646 		} else {
647 			aw_dev_dbg(aw_dev->dev, "Internal chip temperature used");
648 		}
649 		return;
650 	}
651 
652 	monitor_desc->first_entry = AW_FIRST_ENTRY;
653 	monitor_desc->samp_count = 0;
654 	monitor_desc->vol_trace.sum_val = 0;
655 	monitor_desc->temp_trace.sum_val = 0;
656 
657 	queue_delayed_work(aw883xx->work_queue,
658 				&monitor_desc->delay_work, 0);
659 }
660 
aw_monitor_stop(struct aw_monitor_desc * monitor_desc)661 int aw_monitor_stop(struct aw_monitor_desc *monitor_desc)
662 {
663 	struct aw_device *aw_dev = container_of(monitor_desc,
664 			struct aw_device, monitor_desc);
665 
666 	aw_dev_info(aw_dev->dev, "enter");
667 
668 	cancel_delayed_work_sync(&monitor_desc->delay_work);
669 	cancel_delayed_work_sync(&monitor_desc->hw_monitor_work);
670 
671 	return 0;
672 
673 }
674 
675 /*****************************************************
676 * load monitor config
677 *****************************************************/
aw_monitor_param_check_sum(struct aw_device * aw_dev,uint8_t * data,uint32_t data_len)678 static int aw_monitor_param_check_sum(struct aw_device *aw_dev,
679 					uint8_t *data, uint32_t data_len)
680 {
681 	int i, check_sum = 0;
682 	struct aw_monitor_hdr_v_0_1_1 *monitor_hdr =
683 		(struct aw_monitor_hdr_v_0_1_1 *)data;
684 
685 	if (data_len < sizeof(struct aw_monitor_hdr_v_0_1_1)) {
686 		aw_dev_err(aw_dev->dev,
687 			"data size smaller than hdr , please check monitor bin");
688 		return -ENOMEM;
689 	}
690 
691 	for (i = 4 ; i < data_len; i++)
692 		check_sum += (uint8_t)data[i];
693 
694 	if (monitor_hdr->check_sum != check_sum) {
695 		aw_dev_err(aw_dev->dev,
696 			"check_sum[%d] is not equal to actual check_sum[%d]",
697 				monitor_hdr->check_sum, check_sum);
698 		return -ENOMEM;
699 	}
700 
701 	return 0;
702 }
703 
aw_monitor_write_data_to_table(struct aw_device * aw_dev,struct aw_table_info * table_info,const char * offset_ptr)704 static void aw_monitor_write_data_to_table(struct aw_device *aw_dev,
705 		struct aw_table_info *table_info, const char *offset_ptr)
706 {
707 	int i;
708 
709 	for (i = 0; i < table_info->table_num * AW_TABLE_SIZE; i += AW_TABLE_SIZE) {
710 		table_info->aw_table[i / AW_TABLE_SIZE].min_val =
711 			AW_GET_16_DATA(offset_ptr[1 + i], offset_ptr[i]);
712 		table_info->aw_table[i / AW_TABLE_SIZE].max_val =
713 			AW_GET_16_DATA(offset_ptr[3 + i], offset_ptr[2 + i]);
714 		table_info->aw_table[i / AW_TABLE_SIZE].ipeak =
715 			AW_GET_16_DATA(offset_ptr[5 + i], offset_ptr[4 + i]);
716 		table_info->aw_table[i / AW_TABLE_SIZE].gain =
717 			AW_GET_16_DATA(offset_ptr[7 + i], offset_ptr[6 + i]);
718 		table_info->aw_table[i / AW_TABLE_SIZE].vmax =
719 			AW_GET_32_DATA(offset_ptr[11 + i], offset_ptr[10 + i],
720 				offset_ptr[9 + i], offset_ptr[8 + i]);
721 	}
722 
723 	for (i = 0; i < table_info->table_num; i++)
724 		aw_dev_info(aw_dev->dev,
725 			"min_val:%d, max_val:%d, ipeak:0x%x, gain:0x%x, vmax:0x%x",
726 			table_info->aw_table[i].min_val,
727 			table_info->aw_table[i].max_val,
728 			table_info->aw_table[i].ipeak,
729 			table_info->aw_table[i].gain,
730 			table_info->aw_table[i].vmax);
731 
732 }
733 
aw_monitor_parse_vol_data_v_0_1_1(struct aw_device * aw_dev,uint8_t * data)734 static int aw_monitor_parse_vol_data_v_0_1_1(struct aw_device *aw_dev, uint8_t *data)
735 {
736 	struct aw_monitor_hdr_v_0_1_1 *monitor_hdr =
737 			(struct aw_monitor_hdr_v_0_1_1 *)data;
738 	struct aw_table_info *vol_info =
739 		&aw_dev->monitor_desc.monitor_cfg.vol_info;
740 
741 	aw_dev_info(aw_dev->dev, "===parse vol start ===");
742 	if (vol_info->aw_table != NULL) {
743 		devm_kfree(aw_dev->dev, vol_info->aw_table);
744 		vol_info->aw_table = NULL;
745 	}
746 
747 	vol_info->aw_table = devm_kzalloc(aw_dev->dev,
748 					(monitor_hdr->vol_num * AW_TABLE_SIZE),
749 					GFP_KERNEL);
750 	if (vol_info->aw_table == NULL)
751 		return -ENOMEM;
752 
753 	vol_info->table_num = monitor_hdr->vol_num;
754 	aw_monitor_write_data_to_table(aw_dev, vol_info,
755 		&data[monitor_hdr->vol_offset]);
756 	aw_dev_info(aw_dev->dev, "===parse vol end ===");
757 	return 0;
758 }
759 
aw_monitor_parse_temp_data_v_0_1_1(struct aw_device * aw_dev,uint8_t * data)760 static int aw_monitor_parse_temp_data_v_0_1_1(struct aw_device *aw_dev, uint8_t *data)
761 {
762 	struct aw_monitor_hdr_v_0_1_1 *monitor_hdr =
763 			(struct aw_monitor_hdr_v_0_1_1 *)data;
764 	struct aw_table_info *temp_info =
765 		&aw_dev->monitor_desc.monitor_cfg.temp_info;
766 
767 	aw_dev_info(aw_dev->dev, "===parse temp start ===");
768 
769 	if (temp_info->aw_table != NULL) {
770 		devm_kfree(aw_dev->dev, temp_info->aw_table);
771 		temp_info->aw_table = NULL;
772 	}
773 
774 	temp_info->aw_table = devm_kzalloc(aw_dev->dev,
775 					(monitor_hdr->temp_num * AW_TABLE_SIZE),
776 					GFP_KERNEL);
777 	if (temp_info->aw_table == NULL)
778 		return -ENOMEM;
779 
780 	temp_info->table_num = monitor_hdr->temp_num;
781 	aw_monitor_write_data_to_table(aw_dev, temp_info,
782 		&data[monitor_hdr->temp_offset]);
783 	aw_dev_info(aw_dev->dev, "===parse temp end ===");
784 	return 0;
785 }
786 
aw_monitor_parse_hdr_v_0_1_1(struct aw_device * aw_dev,uint8_t * data)787 static void aw_monitor_parse_hdr_v_0_1_1(struct aw_device *aw_dev, uint8_t *data)
788 {
789 	struct aw_monitor_hdr_v_0_1_1 *monitor_hdr =
790 			(struct aw_monitor_hdr_v_0_1_1 *)data;
791 	struct aw_monitor_cfg *monitor_cfg = &aw_dev->monitor_desc.monitor_cfg;
792 
793 	monitor_cfg->monitor_switch =
794 		(monitor_hdr->enable_flag >> MONITOR_EN_BIT) & MONITOR_EN_MASK;
795 	monitor_cfg->monitor_time = monitor_hdr->monitor_time;
796 	monitor_cfg->monitor_count = monitor_hdr->monitor_count;
797 	monitor_cfg->ipeak_switch =
798 		(monitor_hdr->enable_flag >> MONITOR_IPEAK_EN_BIT) & MONITOR_EN_MASK;
799 	monitor_cfg->logic_switch =
800 		(monitor_hdr->enable_flag >> MONITOR_LOGIC_BIT) & MONITOR_EN_MASK;
801 	monitor_cfg->gain_switch =
802 		(monitor_hdr->enable_flag >> MONITOR_GAIN_EN_BIT) & MONITOR_EN_MASK;
803 	monitor_cfg->vmax_switch =
804 		(monitor_hdr->enable_flag >> MONITOR_VMAX_EN_BIT) & MONITOR_EN_MASK;
805 	monitor_cfg->temp_switch =
806 		(monitor_hdr->enable_flag >> MONITOR_TEMP_EN_BIT) & MONITOR_EN_MASK;
807 	monitor_cfg->temp_aplha = monitor_hdr->temp_aplha;
808 	monitor_cfg->vol_switch =
809 		(monitor_hdr->enable_flag >> MONITOR_VOL_EN_BIT) & MONITOR_EN_MASK;
810 	monitor_cfg->vol_aplha = monitor_hdr->vol_aplha;
811 
812 	aw_dev_info(aw_dev->dev, "chip name:%s",
813 		monitor_hdr->chip_type);
814 	aw_dev_info(aw_dev->dev, "ui ver:0x%x",
815 		monitor_hdr->ui_ver);
816 
817 	aw_dev_info(aw_dev->dev,
818 		"monitor_switch:%d, monitor_time:%d (ms), monitor_count:%d",
819 		monitor_cfg->monitor_switch, monitor_cfg->monitor_time,
820 		monitor_cfg->monitor_count);
821 
822 	aw_dev_info(aw_dev->dev,
823 		"logic_switch:%d, ipeak_switch:%d, gain_switch:%d, vmax_switch:%d",
824 		monitor_cfg->logic_switch, monitor_cfg->ipeak_switch,
825 		monitor_cfg->gain_switch, monitor_cfg->vmax_switch);
826 
827 	aw_dev_info(aw_dev->dev,
828 		"temp_switch:%d, temp_aplha:%d, vol_switch:%d, vol_aplha:%d",
829 		monitor_cfg->temp_switch, monitor_cfg->temp_aplha,
830 		monitor_cfg->vol_switch, monitor_cfg->vol_aplha);
831 }
832 
aw_monitor_check_fw_v_0_1_1(struct aw_device * aw_dev,uint8_t * data,uint32_t data_len)833 static int aw_monitor_check_fw_v_0_1_1(struct aw_device *aw_dev,
834 					uint8_t *data, uint32_t data_len)
835 {
836 	struct aw_monitor_hdr_v_0_1_1 *monitor_hdr =
837 				(struct aw_monitor_hdr_v_0_1_1 *)data;
838 	int temp_size, vol_size;
839 
840 	if (data_len < sizeof(struct aw_monitor_hdr_v_0_1_1)) {
841 		aw_dev_err(aw_dev->dev,
842 			"params size[%d] < struct aw_monitor_hdr size[%d]!",
843 			data_len, (int)sizeof(struct aw_monitor_hdr_v_0_1_1));
844 		return -ENOMEM;
845 	}
846 
847 	if (monitor_hdr->temp_offset > data_len) {
848 		aw_dev_err(aw_dev->dev, "temp_offset[%d] overflow file size[%d]!",
849 			monitor_hdr->temp_offset, data_len);
850 		return -ENOMEM;
851 	}
852 
853 	if (monitor_hdr->vol_offset > data_len) {
854 		aw_dev_err(aw_dev->dev, "vol_offset[%d] overflow file size[%d]!",
855 			monitor_hdr->vol_offset, data_len);
856 		return -ENOMEM;
857 	}
858 
859 	temp_size = monitor_hdr->temp_num * monitor_hdr->single_temp_size;
860 	if (temp_size > data_len) {
861 		aw_dev_err(aw_dev->dev, "temp_size:[%d] overflow file size[%d]!",
862 			temp_size, data_len);
863 		return -ENOMEM;
864 	}
865 
866 	vol_size = monitor_hdr->vol_num * monitor_hdr->single_vol_size;
867 	if (vol_size > data_len) {
868 		aw_dev_err(aw_dev->dev, "vol_size:[%d] overflow file size[%d]!",
869 			vol_size, data_len);
870 		return -ENOMEM;
871 	}
872 
873 	return 0;
874 }
875 
aw_monitor_parse_data_v_0_1_1(struct aw_device * aw_dev,uint8_t * data,uint32_t data_len)876 static int aw_monitor_parse_data_v_0_1_1(struct aw_device *aw_dev,
877 				uint8_t *data, uint32_t data_len)
878 {
879 	int ret;
880 	struct aw_monitor_cfg *monitor_cfg = &aw_dev->monitor_desc.monitor_cfg;
881 
882 	ret = aw_monitor_check_fw_v_0_1_1(aw_dev, data, data_len);
883 	if (ret < 0) {
884 		aw_dev_err(aw_dev->dev, "check monitor failed");
885 		return ret;
886 	}
887 
888 	aw_monitor_parse_hdr_v_0_1_1(aw_dev, data);
889 
890 	ret = aw_monitor_parse_temp_data_v_0_1_1(aw_dev, data);
891 	if (ret < 0)
892 		return ret;
893 
894 	ret = aw_monitor_parse_vol_data_v_0_1_1(aw_dev, data);
895 	if (ret < 0) {
896 		if (monitor_cfg->temp_info.aw_table != NULL) {
897 			devm_kfree(aw_dev->dev, monitor_cfg->temp_info.aw_table);
898 			monitor_cfg->temp_info.aw_table = NULL;
899 			monitor_cfg->temp_info.table_num = 0;
900 		}
901 		return ret;
902 	}
903 
904 	monitor_cfg->monitor_status = AW_MON_CFG_OK;
905 	return 0;
906 }
907 
aw_monitor_parse_fw(struct aw_monitor_desc * monitor_desc,uint8_t * data,uint32_t data_len)908 int aw_monitor_parse_fw(struct aw_monitor_desc *monitor_desc,
909 				uint8_t *data, uint32_t data_len)
910 {
911 	struct aw_monitor_hdr_v_0_1_1 *monitor_hdr = NULL;
912 	struct aw_device *aw_dev = NULL;
913 	int ret;
914 
915 	if (monitor_desc == NULL || data == NULL) {
916 		pr_err("monitor_desc or data is NULL");
917 		return -EINVAL;
918 	}
919 
920 	monitor_hdr = (struct aw_monitor_hdr_v_0_1_1 *)data;
921 	aw_dev = container_of(monitor_desc,
922 			struct aw_device, monitor_desc);
923 
924 	ret = aw_monitor_param_check_sum(aw_dev, data, data_len);
925 	if (ret < 0)
926 		return ret;
927 
928 	switch (monitor_hdr->monitor_ver) {
929 	case AW_MONITOR_HDR_VER_0_1_1:
930 		return aw_monitor_parse_data_v_0_1_1(aw_dev, data, data_len);
931 	default:
932 		aw_dev_err(aw_dev->dev, "cfg version:0x%x unsupported",
933 				monitor_hdr->monitor_ver);
934 		return -EINVAL;
935 	}
936 }
937 
aw_monitor_free_firmware(struct aw_device * aw_dev)938 static void aw_monitor_free_firmware(struct aw_device *aw_dev)
939 {
940 	struct aw_monitor_cfg *monitor_cfg =
941 				&aw_dev->monitor_desc.monitor_cfg;
942 
943 	monitor_cfg->monitor_status = AW_MON_CFG_ST;
944 
945 	if (monitor_cfg->temp_info.aw_table != NULL) {
946 		devm_kfree(aw_dev->dev, monitor_cfg->temp_info.aw_table);
947 		monitor_cfg->temp_info.aw_table = NULL;
948 	}
949 
950 	if (monitor_cfg->vol_info.aw_table != NULL) {
951 		devm_kfree(aw_dev->dev, monitor_cfg->vol_info.aw_table);
952 		monitor_cfg->vol_info.aw_table = NULL;
953 	}
954 
955 	memset(monitor_cfg, 0, sizeof(struct aw_monitor_cfg));
956 }
957 
aw_monitor_real_time_update_monitor(struct aw_device * aw_dev)958 static int aw_monitor_real_time_update_monitor(struct aw_device *aw_dev)
959 {
960 	const struct firmware *cont = NULL;
961 	struct aw_container *aw_monitor_cnt = NULL;
962 	int ret;
963 
964 	ret = request_firmware(&cont, AW883XX_MONITOR_NAME, aw_dev->dev);
965 	if (ret < 0) {
966 		aw_dev_err(aw_dev->dev, "failed to read %s", AW883XX_MONITOR_NAME);
967 		release_firmware(cont);
968 		return ret;
969 	}
970 
971 	aw_monitor_cnt = devm_kzalloc(aw_dev->dev,
972 			cont->size + sizeof(uint32_t), GFP_KERNEL);
973 	if (aw_monitor_cnt == NULL) {
974 		aw_dev_err(aw_dev->dev, "alloc failed!");
975 		release_firmware(cont);
976 		return ret;
977 	}
978 
979 	aw_monitor_cnt->len = cont->size;
980 	memcpy(aw_monitor_cnt->data, cont->data, cont->size);
981 	release_firmware(cont);
982 
983 	ret = aw_monitor_parse_fw(&aw_dev->monitor_desc,
984 			aw_monitor_cnt->data, aw_monitor_cnt->len);
985 	if (ret < 0)
986 		aw_dev_err(aw_dev->dev, "parse monitor firmware failed!");
987 
988 	devm_kfree(aw_dev->dev, aw_monitor_cnt);
989 	aw_monitor_cnt = NULL;
990 
991 	return ret;
992 }
993 
994 /*****************************************************
995  * monitor init
996  *****************************************************/
997 #ifdef AW_DEBUG
aw_vol_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)998 static ssize_t aw_vol_store(struct device *dev,
999 	struct device_attribute *attr, const char *buf, size_t count)
1000 {
1001 	struct aw883xx *aw883xx = dev_get_drvdata(dev);
1002 	struct aw_device *aw_dev = aw883xx->aw_pa;
1003 	uint32_t vol = 0;
1004 	int ret = -1;
1005 
1006 	if (count == 0)
1007 		return 0;
1008 
1009 	ret = kstrtouint(buf, 0, &vol);
1010 	if (ret < 0)
1011 		return ret;
1012 
1013 	aw_dev_info(aw_dev->dev, "vol set =%d", vol);
1014 	aw_dev->monitor_desc.test_vol = vol;
1015 
1016 	return count;
1017 }
1018 
aw_vol_show(struct device * dev,struct device_attribute * attr,char * buf)1019 static ssize_t aw_vol_show(struct device *dev,
1020 	struct device_attribute *attr, char *buf)
1021 {
1022 	struct aw883xx *aw883xx = dev_get_drvdata(dev);
1023 	struct aw_device *aw_dev = aw883xx->aw_pa;
1024 	ssize_t len = 0;
1025 
1026 	len += snprintf(buf+len, PAGE_SIZE-len,
1027 		"vol: %d\n",
1028 		aw_dev->monitor_desc.test_vol);
1029 	return len;
1030 }
1031 
aw_temp_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1032 static ssize_t aw_temp_store(struct device *dev,
1033 	struct device_attribute *attr, const char *buf, size_t count)
1034 {
1035 	struct aw883xx *aw883xx = dev_get_drvdata(dev);
1036 	struct aw_device *aw_dev = aw883xx->aw_pa;
1037 	int32_t temp = 0;
1038 	int ret = -1;
1039 
1040 	if (count == 0)
1041 		return 0;
1042 
1043 	ret = kstrtoint(buf, 0, &temp);
1044 	if (ret < 0)
1045 		return ret;
1046 
1047 	aw_dev_info(aw_dev->dev, "temp set =%d", temp);
1048 
1049 	aw_dev->monitor_desc.test_temp = temp;
1050 
1051 	return count;
1052 }
1053 
aw_temp_show(struct device * dev,struct device_attribute * attr,char * buf)1054 static ssize_t aw_temp_show(struct device *dev,
1055 	struct device_attribute *attr, char *buf)
1056 {
1057 	struct aw883xx *aw883xx = dev_get_drvdata(dev);
1058 	struct aw_device *aw_dev = aw883xx->aw_pa;
1059 	ssize_t len = 0;
1060 
1061 	len += snprintf(buf+len, PAGE_SIZE-len,
1062 		"aw883xx temp: %d\n",
1063 		aw_dev->monitor_desc.test_temp);
1064 
1065 	return len;
1066 }
1067 
1068 static DEVICE_ATTR(vol, S_IWUSR | S_IRUGO,
1069 	aw_vol_show, aw_vol_store);
1070 static DEVICE_ATTR(temp, S_IWUSR | S_IRUGO,
1071 	aw_temp_show, aw_temp_store);
1072 #endif
1073 
aw_monitor_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1074 static ssize_t aw_monitor_store(struct device *dev,
1075 	struct device_attribute *attr, const char *buf, size_t count)
1076 {
1077 	struct aw883xx *aw883xx = dev_get_drvdata(dev);
1078 	struct aw_device *aw_dev = aw883xx->aw_pa;
1079 	uint32_t enable = 0;
1080 	int ret = -1;
1081 
1082 	if (count == 0)
1083 		return 0;
1084 
1085 	ret = kstrtouint(buf, 0, &enable);
1086 	if (ret < 0)
1087 		return ret;
1088 
1089 	aw_dev_info(aw_dev->dev, "monitor enable set =%d", enable);
1090 
1091 	if (aw_dev->monitor_desc.monitor_cfg.monitor_switch == enable) {
1092 		return count;
1093 	} else {
1094 		aw_dev->monitor_desc.monitor_cfg.monitor_switch = enable;
1095 		if (enable)
1096 			aw_monitor_start(&aw_dev->monitor_desc);
1097 	}
1098 
1099 	return count;
1100 }
1101 
aw_monitor_show(struct device * dev,struct device_attribute * attr,char * buf)1102 static ssize_t aw_monitor_show(struct device *dev,
1103 	struct device_attribute *attr, char *buf)
1104 {
1105 	struct aw883xx *aw883xx = dev_get_drvdata(dev);
1106 	struct aw_device *aw_dev = aw883xx->aw_pa;
1107 	ssize_t len = 0;
1108 
1109 
1110 	len += snprintf(buf+len, PAGE_SIZE-len,
1111 		"aw883xx monitor_flag=%u\n",
1112 		aw_dev->monitor_desc.monitor_cfg.monitor_switch);
1113 	return len;
1114 }
1115 
aw_monitor_update_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)1116 static ssize_t aw_monitor_update_store(struct device *dev,
1117 	struct device_attribute *attr, const char *buf, size_t count)
1118 {
1119 	struct aw883xx *aw883xx = dev_get_drvdata(dev);
1120 	struct aw_device *aw_dev = aw883xx->aw_pa;
1121 
1122 	uint32_t update = 0;
1123 	int ret = -1;
1124 
1125 	if (count == 0)
1126 		return 0;
1127 
1128 	ret = kstrtouint(buf, 0, &update);
1129 	if (ret < 0)
1130 		return ret;
1131 
1132 	aw_dev_info(aw_dev->dev, "monitor update = %d", update);
1133 
1134 	if (update) {
1135 		aw_monitor_stop(&aw_dev->monitor_desc);
1136 		aw_monitor_free_firmware(aw_dev);
1137 		ret = aw_monitor_real_time_update_monitor(aw_dev);
1138 		if (ret < 0)
1139 			return ret;
1140 		aw_monitor_start(&aw_dev->monitor_desc);
1141 	}
1142 
1143 	return count;
1144 }
1145 
1146 static DEVICE_ATTR(monitor, S_IWUSR | S_IRUGO,
1147 	aw_monitor_show, aw_monitor_store);
1148 static DEVICE_ATTR(monitor_update, S_IWUSR,
1149 	NULL, aw_monitor_update_store);
1150 
1151 
1152 static struct attribute *aw_monitor_attr[] = {
1153 	&dev_attr_monitor.attr,
1154 	&dev_attr_monitor_update.attr,
1155 #ifdef AW_DEBUG
1156 	&dev_attr_vol.attr,
1157 	&dev_attr_temp.attr,
1158 #endif
1159 	NULL
1160 };
1161 
1162 static struct attribute_group aw_monitor_attr_group = {
1163 	.attrs = aw_monitor_attr,
1164 };
1165 
aw_monitor_parse_dt(struct aw_device * aw_dev)1166 static void aw_monitor_parse_dt(struct aw_device *aw_dev)
1167 {
1168 	int ret = -1;
1169 	uint32_t monitor_delay;
1170 	struct device_node *np = aw_dev->dev->of_node;
1171 
1172 	ret = of_property_read_u32(np, "hw-monitor-delay", &monitor_delay);
1173 	if (ret < 0) {
1174 		aw_dev_info(aw_dev->dev,
1175 			"read hw-monitor-delay failed, set deafult value:[%d]ms",
1176 			aw_dev->monitor_desc.hw_monitor_delay);
1177 	} else {
1178 		aw_dev_info(aw_dev->dev,
1179 			"parse hw-monitor-delay:[%d]", monitor_delay);
1180 		aw_dev->monitor_desc.hw_monitor_delay = monitor_delay;
1181 	}
1182 }
1183 
aw_monitor_init(struct aw_monitor_desc * monitor_desc)1184 void aw_monitor_init(struct aw_monitor_desc *monitor_desc)
1185 {
1186 	int ret;
1187 	struct aw_device *aw_dev = container_of(monitor_desc,
1188 				struct aw_device, monitor_desc);
1189 
1190 	aw_dev_info(aw_dev->dev, "enter");
1191 
1192 	aw_monitor_parse_dt(aw_dev);
1193 
1194 #ifdef AW_DEBUG
1195 	monitor_desc->test_vol = 0;
1196 	monitor_desc->test_temp = 0;
1197 #endif
1198 
1199 	INIT_DELAYED_WORK(&monitor_desc->delay_work, aw_monitor_work_func);
1200 	INIT_DELAYED_WORK(&monitor_desc->hw_monitor_work, aw_hw_monitor_work_func);
1201 
1202 	ret = sysfs_create_group(&aw_dev->dev->kobj,
1203 				&aw_monitor_attr_group);
1204 	if (ret < 0)
1205 		aw_dev_err(aw_dev->dev, "error creating sysfs attr files");
1206 }
1207 
aw_monitor_deinit(struct aw_monitor_desc * monitor_desc)1208 void aw_monitor_deinit(struct aw_monitor_desc *monitor_desc)
1209 {
1210 	struct aw_device *aw_dev =
1211 		container_of(monitor_desc, struct aw_device, monitor_desc);
1212 
1213 	aw_monitor_stop(monitor_desc);
1214 
1215 	sysfs_remove_group(&aw_dev->dev->kobj, &aw_monitor_attr_group);
1216 }
1217 
1218