xref: /linux/sound/soc/codecs/aw88395/aw88395_lib.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
14345865bSWeidong Wang // SPDX-License-Identifier: GPL-2.0-only
24345865bSWeidong Wang //
34345865bSWeidong Wang // aw88395_lib.c  -- ACF bin parsing and check library file for aw88395
44345865bSWeidong Wang //
54345865bSWeidong Wang // Copyright (c) 2022-2023 AWINIC Technology CO., LTD
64345865bSWeidong Wang //
74345865bSWeidong Wang // Author: Bruce zhao <zhaolei@awinic.com>
84345865bSWeidong Wang //
94345865bSWeidong Wang 
10*d0f4ce8aSKrzysztof Kozlowski #include <linux/cleanup.h>
114345865bSWeidong Wang #include <linux/crc8.h>
124345865bSWeidong Wang #include <linux/i2c.h>
134345865bSWeidong Wang #include "aw88395_lib.h"
144345865bSWeidong Wang #include "aw88395_device.h"
154345865bSWeidong Wang 
164345865bSWeidong Wang #define AW88395_CRC8_POLYNOMIAL 0x8C
174345865bSWeidong Wang DECLARE_CRC8_TABLE(aw_crc8_table);
184345865bSWeidong Wang 
194345865bSWeidong Wang static char *profile_name[AW88395_PROFILE_MAX] = {
204345865bSWeidong Wang 	"Music", "Voice", "Voip", "Ringtone",
214345865bSWeidong Wang 	"Ringtone_hs", "Lowpower", "Bypass",
224345865bSWeidong Wang 	"Mmi", "Fm", "Notification", "Receiver"
234345865bSWeidong Wang };
244345865bSWeidong Wang 
254345865bSWeidong Wang static int aw_parse_bin_header(struct aw_device *aw_dev, struct aw_bin *bin);
264345865bSWeidong Wang 
aw_check_sum(struct aw_device * aw_dev,struct aw_bin * bin,int bin_num)274345865bSWeidong Wang static int aw_check_sum(struct aw_device *aw_dev, struct aw_bin *bin, int bin_num)
284345865bSWeidong Wang {
294345865bSWeidong Wang 	unsigned char *p_check_sum;
304345865bSWeidong Wang 	unsigned int sum_data = 0;
314345865bSWeidong Wang 	unsigned int check_sum;
324345865bSWeidong Wang 	unsigned int i, len;
334345865bSWeidong Wang 
344345865bSWeidong Wang 	p_check_sum = &(bin->info.data[(bin->header_info[bin_num].valid_data_addr -
354345865bSWeidong Wang 						bin->header_info[bin_num].header_len)]);
364345865bSWeidong Wang 	len = bin->header_info[bin_num].bin_data_len + bin->header_info[bin_num].header_len;
374345865bSWeidong Wang 	check_sum = le32_to_cpup((void *)p_check_sum);
384345865bSWeidong Wang 
394345865bSWeidong Wang 	for (i = 4; i < len; i++)
404345865bSWeidong Wang 		sum_data += *(p_check_sum + i);
414345865bSWeidong Wang 
424345865bSWeidong Wang 	dev_dbg(aw_dev->dev, "%s -- check_sum = %p, check_sum = 0x%x, sum_data = 0x%x",
434345865bSWeidong Wang 					__func__, p_check_sum, check_sum, sum_data);
444345865bSWeidong Wang 	if (sum_data != check_sum) {
454345865bSWeidong Wang 		dev_err(aw_dev->dev, "%s. CheckSum Fail.bin_num=%d, CheckSum:0x%x, SumData:0x%x",
464345865bSWeidong Wang 				__func__, bin_num, check_sum, sum_data);
474345865bSWeidong Wang 		return -EINVAL;
484345865bSWeidong Wang 	}
494345865bSWeidong Wang 
504345865bSWeidong Wang 	return 0;
514345865bSWeidong Wang }
524345865bSWeidong Wang 
aw_check_data_version(struct aw_device * aw_dev,struct aw_bin * bin,int bin_num)534345865bSWeidong Wang static int aw_check_data_version(struct aw_device *aw_dev, struct aw_bin *bin, int bin_num)
544345865bSWeidong Wang {
554345865bSWeidong Wang 	if (bin->header_info[bin_num].bin_data_ver < DATA_VERSION_V1 ||
564345865bSWeidong Wang 		bin->header_info[bin_num].bin_data_ver > DATA_VERSION_MAX) {
574345865bSWeidong Wang 		dev_err(aw_dev->dev, "aw_bin_parse Unrecognized this bin data version\n");
584345865bSWeidong Wang 		return -EINVAL;
594345865bSWeidong Wang 	}
604345865bSWeidong Wang 
614345865bSWeidong Wang 	return 0;
624345865bSWeidong Wang }
634345865bSWeidong Wang 
aw_check_register_num(struct aw_device * aw_dev,struct aw_bin * bin,int bin_num)644345865bSWeidong Wang static int aw_check_register_num(struct aw_device *aw_dev, struct aw_bin *bin, int bin_num)
654345865bSWeidong Wang {
664345865bSWeidong Wang 	struct bin_header_info temp_info = bin->header_info[bin_num];
674345865bSWeidong Wang 	unsigned int check_register_num, parse_register_num;
684345865bSWeidong Wang 	unsigned char *p_check_sum;
694345865bSWeidong Wang 
704345865bSWeidong Wang 	p_check_sum = &(bin->info.data[(temp_info.valid_data_addr)]);
714345865bSWeidong Wang 
724345865bSWeidong Wang 	parse_register_num = le32_to_cpup((void *)p_check_sum);
734345865bSWeidong Wang 	check_register_num = (bin->header_info[bin_num].bin_data_len - CHECK_REGISTER_NUM_OFFSET) /
744345865bSWeidong Wang 				(bin->header_info[bin_num].reg_byte_len +
754345865bSWeidong Wang 				bin->header_info[bin_num].data_byte_len);
764345865bSWeidong Wang 	dev_dbg(aw_dev->dev, "%s,parse_register_num = 0x%x,check_register_num = 0x%x\n",
774345865bSWeidong Wang 				__func__, parse_register_num, check_register_num);
784345865bSWeidong Wang 	if (parse_register_num != check_register_num) {
794345865bSWeidong Wang 		dev_err(aw_dev->dev, "%s parse_register_num = 0x%x,check_register_num = 0x%x\n",
804345865bSWeidong Wang 				__func__, parse_register_num, check_register_num);
814345865bSWeidong Wang 		return -EINVAL;
824345865bSWeidong Wang 	}
834345865bSWeidong Wang 
844345865bSWeidong Wang 	bin->header_info[bin_num].reg_num = parse_register_num;
854345865bSWeidong Wang 	bin->header_info[bin_num].valid_data_len = temp_info.bin_data_len - VALID_DATA_LEN;
864345865bSWeidong Wang 	bin->header_info[bin_num].valid_data_addr = temp_info.valid_data_addr + VALID_DATA_ADDR;
874345865bSWeidong Wang 
884345865bSWeidong Wang 	return 0;
894345865bSWeidong Wang }
904345865bSWeidong Wang 
aw_check_dsp_reg_num(struct aw_device * aw_dev,struct aw_bin * bin,int bin_num)914345865bSWeidong Wang static int aw_check_dsp_reg_num(struct aw_device *aw_dev, struct aw_bin *bin, int bin_num)
924345865bSWeidong Wang {
934345865bSWeidong Wang 	struct bin_header_info temp_info = bin->header_info[bin_num];
944345865bSWeidong Wang 	unsigned int check_dsp_reg_num, parse_dsp_reg_num;
954345865bSWeidong Wang 	unsigned char *p_check_sum;
964345865bSWeidong Wang 
974345865bSWeidong Wang 	p_check_sum = &(bin->info.data[(temp_info.valid_data_addr)]);
984345865bSWeidong Wang 
994345865bSWeidong Wang 	parse_dsp_reg_num = le32_to_cpup((void *)(p_check_sum + PARSE_DSP_REG_NUM));
1004345865bSWeidong Wang 	bin->header_info[bin_num].reg_data_byte_len =
1014345865bSWeidong Wang 			le32_to_cpup((void *)(p_check_sum + REG_DATA_BYTP_LEN));
1024345865bSWeidong Wang 	check_dsp_reg_num = (bin->header_info[bin_num].bin_data_len - CHECK_DSP_REG_NUM) /
1034345865bSWeidong Wang 				bin->header_info[bin_num].reg_data_byte_len;
1044345865bSWeidong Wang 	dev_dbg(aw_dev->dev, "%s bin_num = %d, parse_dsp_reg_num = 0x%x, check_dsp_reg_num = 0x%x",
1054345865bSWeidong Wang 					__func__, bin_num, check_dsp_reg_num, check_dsp_reg_num);
1064345865bSWeidong Wang 	if (parse_dsp_reg_num != check_dsp_reg_num) {
1074345865bSWeidong Wang 		dev_err(aw_dev->dev, "aw_bin_parse check dsp reg num error\n");
1084345865bSWeidong Wang 		dev_err(aw_dev->dev, "%s parse_dsp_reg_num = 0x%x, check_dsp_reg_num = 0x%x",
1094345865bSWeidong Wang 					__func__, check_dsp_reg_num, check_dsp_reg_num);
1104345865bSWeidong Wang 		return -EINVAL;
1114345865bSWeidong Wang 	}
1124345865bSWeidong Wang 
1134345865bSWeidong Wang 	bin->header_info[bin_num].download_addr = le32_to_cpup((void *)p_check_sum);
1144345865bSWeidong Wang 	bin->header_info[bin_num].reg_num = parse_dsp_reg_num;
1154345865bSWeidong Wang 	bin->header_info[bin_num].valid_data_len = temp_info.bin_data_len - DSP_VALID_DATA_LEN;
1164345865bSWeidong Wang 	bin->header_info[bin_num].valid_data_addr = temp_info.valid_data_addr +
1174345865bSWeidong Wang 								DSP_VALID_DATA_ADDR;
1184345865bSWeidong Wang 
1194345865bSWeidong Wang 	return 0;
1204345865bSWeidong Wang }
1214345865bSWeidong Wang 
aw_check_soc_app_num(struct aw_device * aw_dev,struct aw_bin * bin,int bin_num)1224345865bSWeidong Wang static int aw_check_soc_app_num(struct aw_device *aw_dev, struct aw_bin *bin, int bin_num)
1234345865bSWeidong Wang {
1244345865bSWeidong Wang 	struct bin_header_info temp_info = bin->header_info[bin_num];
1254345865bSWeidong Wang 	unsigned int check_soc_app_num, parse_soc_app_num;
1264345865bSWeidong Wang 	unsigned char *p_check_sum;
1274345865bSWeidong Wang 
1284345865bSWeidong Wang 	p_check_sum = &(bin->info.data[(temp_info.valid_data_addr)]);
1294345865bSWeidong Wang 
1304345865bSWeidong Wang 	bin->header_info[bin_num].app_version = le32_to_cpup((void *)p_check_sum);
1314345865bSWeidong Wang 	parse_soc_app_num = le32_to_cpup((void *)(p_check_sum + PARSE_SOC_APP_NUM));
1324345865bSWeidong Wang 	check_soc_app_num = bin->header_info[bin_num].bin_data_len - CHECK_SOC_APP_NUM;
1334345865bSWeidong Wang 	dev_dbg(aw_dev->dev, "%s bin_num = %d, parse_soc_app_num=0x%x, check_soc_app_num = 0x%x\n",
1344345865bSWeidong Wang 					__func__, bin_num, parse_soc_app_num, check_soc_app_num);
1354345865bSWeidong Wang 	if (parse_soc_app_num != check_soc_app_num) {
1364345865bSWeidong Wang 		dev_err(aw_dev->dev, "%s parse_soc_app_num=0x%x, check_soc_app_num = 0x%x\n",
1374345865bSWeidong Wang 					__func__, parse_soc_app_num, check_soc_app_num);
1384345865bSWeidong Wang 		return -EINVAL;
1394345865bSWeidong Wang 	}
1404345865bSWeidong Wang 
1414345865bSWeidong Wang 	bin->header_info[bin_num].reg_num = parse_soc_app_num;
1424345865bSWeidong Wang 	bin->header_info[bin_num].download_addr = le32_to_cpup((void *)(p_check_sum +
1434345865bSWeidong Wang 								APP_DOWNLOAD_ADDR));
1444345865bSWeidong Wang 	bin->header_info[bin_num].valid_data_len = temp_info.bin_data_len - APP_VALID_DATA_LEN;
1454345865bSWeidong Wang 	bin->header_info[bin_num].valid_data_addr = temp_info.valid_data_addr +
1464345865bSWeidong Wang 								APP_VALID_DATA_ADDR;
1474345865bSWeidong Wang 
1484345865bSWeidong Wang 	return 0;
1494345865bSWeidong Wang }
1504345865bSWeidong Wang 
aw_get_single_bin_header(struct aw_bin * bin)1514345865bSWeidong Wang static void aw_get_single_bin_header(struct aw_bin *bin)
1524345865bSWeidong Wang {
1534345865bSWeidong Wang 	memcpy((void *)&bin->header_info[bin->all_bin_parse_num], bin->p_addr, DATA_LEN);
1544345865bSWeidong Wang 
1554345865bSWeidong Wang 	bin->header_info[bin->all_bin_parse_num].header_len = HEADER_LEN;
1564345865bSWeidong Wang 	bin->all_bin_parse_num += 1;
1574345865bSWeidong Wang }
1584345865bSWeidong Wang 
aw_parse_one_of_multi_bins(struct aw_device * aw_dev,unsigned int bin_num,int bin_serial_num,struct aw_bin * bin)1594345865bSWeidong Wang static int aw_parse_one_of_multi_bins(struct aw_device *aw_dev, unsigned int bin_num,
1604345865bSWeidong Wang 					int bin_serial_num, struct aw_bin *bin)
1614345865bSWeidong Wang {
1624345865bSWeidong Wang 	struct bin_header_info aw_bin_header_info;
1634345865bSWeidong Wang 	unsigned int bin_start_addr;
1644345865bSWeidong Wang 	unsigned int valid_data_len;
1654345865bSWeidong Wang 
1664345865bSWeidong Wang 	if (bin->info.len < sizeof(struct bin_header_info)) {
1674345865bSWeidong Wang 		dev_err(aw_dev->dev, "bin_header_info size[%d] overflow file size[%d]\n",
1684345865bSWeidong Wang 				(int)sizeof(struct bin_header_info), bin->info.len);
1694345865bSWeidong Wang 		return -EINVAL;
1704345865bSWeidong Wang 	}
1714345865bSWeidong Wang 
1724345865bSWeidong Wang 	aw_bin_header_info = bin->header_info[bin->all_bin_parse_num - 1];
1734345865bSWeidong Wang 	if (!bin_serial_num) {
1744345865bSWeidong Wang 		bin_start_addr = le32_to_cpup((void *)(bin->p_addr + START_ADDR_OFFSET));
1754345865bSWeidong Wang 		bin->p_addr += (HEADER_LEN + bin_start_addr);
1764345865bSWeidong Wang 		bin->header_info[bin->all_bin_parse_num].valid_data_addr =
1774345865bSWeidong Wang 			aw_bin_header_info.valid_data_addr + VALID_DATA_ADDR + 8 * bin_num +
1784345865bSWeidong Wang 			VALID_DATA_ADDR_OFFSET;
1794345865bSWeidong Wang 	} else {
1804345865bSWeidong Wang 		valid_data_len = aw_bin_header_info.bin_data_len;
1814345865bSWeidong Wang 		bin->p_addr += (HDADER_LEN + valid_data_len);
1824345865bSWeidong Wang 		bin->header_info[bin->all_bin_parse_num].valid_data_addr =
1834345865bSWeidong Wang 		    aw_bin_header_info.valid_data_addr + aw_bin_header_info.bin_data_len +
1844345865bSWeidong Wang 		    VALID_DATA_ADDR_OFFSET;
1854345865bSWeidong Wang 	}
1864345865bSWeidong Wang 
1874345865bSWeidong Wang 	return aw_parse_bin_header(aw_dev, bin);
1884345865bSWeidong Wang }
1894345865bSWeidong Wang 
aw_get_multi_bin_header(struct aw_device * aw_dev,struct aw_bin * bin)1904345865bSWeidong Wang static int aw_get_multi_bin_header(struct aw_device *aw_dev, struct aw_bin *bin)
1914345865bSWeidong Wang {
1924345865bSWeidong Wang 	unsigned int bin_num, i;
1934345865bSWeidong Wang 	int ret;
1944345865bSWeidong Wang 
1954345865bSWeidong Wang 	bin_num = le32_to_cpup((void *)(bin->p_addr + VALID_DATA_ADDR_OFFSET));
1964345865bSWeidong Wang 	if (bin->multi_bin_parse_num == 1)
1974345865bSWeidong Wang 		bin->header_info[bin->all_bin_parse_num].valid_data_addr =
1984345865bSWeidong Wang 							VALID_DATA_ADDR_OFFSET;
1994345865bSWeidong Wang 
2004345865bSWeidong Wang 	aw_get_single_bin_header(bin);
2014345865bSWeidong Wang 
2024345865bSWeidong Wang 	for (i = 0; i < bin_num; i++) {
2034345865bSWeidong Wang 		dev_dbg(aw_dev->dev, "aw_bin_parse enter multi bin for is %d\n", i);
2044345865bSWeidong Wang 		ret = aw_parse_one_of_multi_bins(aw_dev, bin_num, i, bin);
2054345865bSWeidong Wang 		if (ret < 0)
2064345865bSWeidong Wang 			return ret;
2074345865bSWeidong Wang 	}
2084345865bSWeidong Wang 
2094345865bSWeidong Wang 	return 0;
2104345865bSWeidong Wang }
2114345865bSWeidong Wang 
aw_parse_bin_header(struct aw_device * aw_dev,struct aw_bin * bin)2124345865bSWeidong Wang static int aw_parse_bin_header(struct aw_device *aw_dev, struct aw_bin *bin)
2134345865bSWeidong Wang {
2144345865bSWeidong Wang 	unsigned int bin_data_type;
2154345865bSWeidong Wang 
2164345865bSWeidong Wang 	if (bin->info.len < sizeof(struct bin_header_info)) {
2174345865bSWeidong Wang 		dev_err(aw_dev->dev, "bin_header_info size[%d] overflow file size[%d]\n",
2184345865bSWeidong Wang 				(int)sizeof(struct bin_header_info), bin->info.len);
2194345865bSWeidong Wang 		return -EINVAL;
2204345865bSWeidong Wang 	}
2214345865bSWeidong Wang 
2224345865bSWeidong Wang 	bin_data_type = le32_to_cpup((void *)(bin->p_addr + BIN_DATA_TYPE_OFFSET));
2234345865bSWeidong Wang 	dev_dbg(aw_dev->dev, "aw_bin_parse bin_data_type 0x%x\n", bin_data_type);
2244345865bSWeidong Wang 	switch (bin_data_type) {
2254345865bSWeidong Wang 	case DATA_TYPE_REGISTER:
2264345865bSWeidong Wang 	case DATA_TYPE_DSP_REG:
2274345865bSWeidong Wang 	case DATA_TYPE_SOC_APP:
2284345865bSWeidong Wang 		bin->single_bin_parse_num += 1;
2294345865bSWeidong Wang 		dev_dbg(aw_dev->dev, "%s bin->single_bin_parse_num is %d\n", __func__,
2304345865bSWeidong Wang 						bin->single_bin_parse_num);
2314345865bSWeidong Wang 		if (!bin->multi_bin_parse_num)
2324345865bSWeidong Wang 			bin->header_info[bin->all_bin_parse_num].valid_data_addr =
2334345865bSWeidong Wang 								VALID_DATA_ADDR_OFFSET;
2344345865bSWeidong Wang 		aw_get_single_bin_header(bin);
2354345865bSWeidong Wang 		return 0;
2364345865bSWeidong Wang 	case DATA_TYPE_MULTI_BINS:
2374345865bSWeidong Wang 		bin->multi_bin_parse_num += 1;
2384345865bSWeidong Wang 		dev_dbg(aw_dev->dev, "%s bin->multi_bin_parse_num is %d\n", __func__,
2394345865bSWeidong Wang 						bin->multi_bin_parse_num);
2404345865bSWeidong Wang 		return aw_get_multi_bin_header(aw_dev, bin);
2414345865bSWeidong Wang 	default:
2424345865bSWeidong Wang 		dev_dbg(aw_dev->dev, "%s There is no corresponding type\n", __func__);
2434345865bSWeidong Wang 		return 0;
2444345865bSWeidong Wang 	}
2454345865bSWeidong Wang }
2464345865bSWeidong Wang 
aw_check_bin_header_version(struct aw_device * aw_dev,struct aw_bin * bin)2474345865bSWeidong Wang static int aw_check_bin_header_version(struct aw_device *aw_dev, struct aw_bin *bin)
2484345865bSWeidong Wang {
2494345865bSWeidong Wang 	unsigned int header_version;
2504345865bSWeidong Wang 
2514345865bSWeidong Wang 	header_version = le32_to_cpup((void *)(bin->p_addr + HEADER_VERSION_OFFSET));
2524345865bSWeidong Wang 	dev_dbg(aw_dev->dev, "aw_bin_parse header_version 0x%x\n", header_version);
2534345865bSWeidong Wang 
2544345865bSWeidong Wang 	switch (header_version) {
2554345865bSWeidong Wang 	case HEADER_VERSION_V1:
2564345865bSWeidong Wang 		return aw_parse_bin_header(aw_dev, bin);
2574345865bSWeidong Wang 	default:
2584345865bSWeidong Wang 		dev_err(aw_dev->dev, "aw_bin_parse Unrecognized this bin header version\n");
2594345865bSWeidong Wang 		return -EINVAL;
2604345865bSWeidong Wang 	}
2614345865bSWeidong Wang }
2624345865bSWeidong Wang 
aw_parsing_bin_file(struct aw_device * aw_dev,struct aw_bin * bin)2634345865bSWeidong Wang static int aw_parsing_bin_file(struct aw_device *aw_dev, struct aw_bin *bin)
2644345865bSWeidong Wang {
2654345865bSWeidong Wang 	int ret = -EINVAL;
2664345865bSWeidong Wang 	int i;
2674345865bSWeidong Wang 
2684345865bSWeidong Wang 	if (!bin) {
2694345865bSWeidong Wang 		dev_err(aw_dev->dev, "aw_bin_parse bin is NULL\n");
2704345865bSWeidong Wang 		return ret;
2714345865bSWeidong Wang 	}
2724345865bSWeidong Wang 	bin->p_addr = bin->info.data;
2734345865bSWeidong Wang 	bin->all_bin_parse_num = 0;
2744345865bSWeidong Wang 	bin->multi_bin_parse_num = 0;
2754345865bSWeidong Wang 	bin->single_bin_parse_num = 0;
2764345865bSWeidong Wang 
2774345865bSWeidong Wang 	ret = aw_check_bin_header_version(aw_dev, bin);
2784345865bSWeidong Wang 	if (ret < 0) {
2794345865bSWeidong Wang 		dev_err(aw_dev->dev, "aw_bin_parse check bin header version error\n");
2804345865bSWeidong Wang 		return ret;
2814345865bSWeidong Wang 	}
2824345865bSWeidong Wang 
2834345865bSWeidong Wang 	for (i = 0; i < bin->all_bin_parse_num; i++) {
2844345865bSWeidong Wang 		ret = aw_check_sum(aw_dev, bin, i);
2854345865bSWeidong Wang 		if (ret < 0) {
2864345865bSWeidong Wang 			dev_err(aw_dev->dev, "aw_bin_parse check sum data error\n");
2874345865bSWeidong Wang 			return ret;
2884345865bSWeidong Wang 		}
2894345865bSWeidong Wang 		ret = aw_check_data_version(aw_dev, bin, i);
2904345865bSWeidong Wang 		if (ret < 0) {
2914345865bSWeidong Wang 			dev_err(aw_dev->dev, "aw_bin_parse check data version error\n");
2924345865bSWeidong Wang 			return ret;
2934345865bSWeidong Wang 		}
2944345865bSWeidong Wang 		if (bin->header_info[i].bin_data_ver == DATA_VERSION_V1) {
2954345865bSWeidong Wang 			switch (bin->header_info[i].bin_data_type) {
2964345865bSWeidong Wang 			case DATA_TYPE_REGISTER:
2974345865bSWeidong Wang 				ret = aw_check_register_num(aw_dev, bin, i);
2984345865bSWeidong Wang 				break;
2994345865bSWeidong Wang 			case DATA_TYPE_DSP_REG:
3004345865bSWeidong Wang 				ret = aw_check_dsp_reg_num(aw_dev, bin, i);
3014345865bSWeidong Wang 				break;
3024345865bSWeidong Wang 			case DATA_TYPE_SOC_APP:
3034345865bSWeidong Wang 				ret = aw_check_soc_app_num(aw_dev, bin, i);
3044345865bSWeidong Wang 				break;
3054345865bSWeidong Wang 			default:
3064345865bSWeidong Wang 				bin->header_info[i].valid_data_len =
3074345865bSWeidong Wang 						bin->header_info[i].bin_data_len;
3084345865bSWeidong Wang 				ret = 0;
3094345865bSWeidong Wang 				break;
3104345865bSWeidong Wang 			}
3114345865bSWeidong Wang 			if (ret < 0)
3124345865bSWeidong Wang 				return ret;
3134345865bSWeidong Wang 		}
3144345865bSWeidong Wang 	}
3154345865bSWeidong Wang 
3164345865bSWeidong Wang 	return 0;
3174345865bSWeidong Wang }
3184345865bSWeidong Wang 
aw_dev_parse_raw_reg(unsigned char * data,unsigned int data_len,struct aw_prof_desc * prof_desc)3194345865bSWeidong Wang static int aw_dev_parse_raw_reg(unsigned char *data, unsigned int data_len,
3204345865bSWeidong Wang 		struct aw_prof_desc *prof_desc)
3214345865bSWeidong Wang {
3224345865bSWeidong Wang 	prof_desc->sec_desc[AW88395_DATA_TYPE_REG].data = data;
3234345865bSWeidong Wang 	prof_desc->sec_desc[AW88395_DATA_TYPE_REG].len = data_len;
3244345865bSWeidong Wang 
3254345865bSWeidong Wang 	prof_desc->prof_st = AW88395_PROFILE_OK;
3264345865bSWeidong Wang 
3274345865bSWeidong Wang 	return 0;
3284345865bSWeidong Wang }
3294345865bSWeidong Wang 
aw_dev_parse_raw_dsp_cfg(unsigned char * data,unsigned int data_len,struct aw_prof_desc * prof_desc)3304345865bSWeidong Wang static int aw_dev_parse_raw_dsp_cfg(unsigned char *data, unsigned int data_len,
3314345865bSWeidong Wang 		struct aw_prof_desc *prof_desc)
3324345865bSWeidong Wang {
3334345865bSWeidong Wang 	if (data_len & 0x01)
3344345865bSWeidong Wang 		return -EINVAL;
3354345865bSWeidong Wang 
3364345865bSWeidong Wang 	swab16_array((u16 *)data, data_len >> 1);
3374345865bSWeidong Wang 
3384345865bSWeidong Wang 	prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_CFG].data = data;
3394345865bSWeidong Wang 	prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_CFG].len = data_len;
3404345865bSWeidong Wang 
3414345865bSWeidong Wang 	prof_desc->prof_st = AW88395_PROFILE_OK;
3424345865bSWeidong Wang 
3434345865bSWeidong Wang 	return 0;
3444345865bSWeidong Wang }
3454345865bSWeidong Wang 
aw_dev_parse_raw_dsp_fw(unsigned char * data,unsigned int data_len,struct aw_prof_desc * prof_desc)3464345865bSWeidong Wang static int aw_dev_parse_raw_dsp_fw(unsigned char *data,	unsigned int data_len,
3474345865bSWeidong Wang 		struct aw_prof_desc *prof_desc)
3484345865bSWeidong Wang {
3494345865bSWeidong Wang 	if (data_len & 0x01)
3504345865bSWeidong Wang 		return -EINVAL;
3514345865bSWeidong Wang 
3524345865bSWeidong Wang 	swab16_array((u16 *)data, data_len >> 1);
3534345865bSWeidong Wang 
3544345865bSWeidong Wang 	prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_FW].data = data;
3554345865bSWeidong Wang 	prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_FW].len = data_len;
3564345865bSWeidong Wang 
3574345865bSWeidong Wang 	prof_desc->prof_st = AW88395_PROFILE_OK;
3584345865bSWeidong Wang 
3594345865bSWeidong Wang 	return 0;
3604345865bSWeidong Wang }
3614345865bSWeidong Wang 
aw_dev_prof_parse_multi_bin(struct aw_device * aw_dev,unsigned char * data,unsigned int data_len,struct aw_prof_desc * prof_desc)3624345865bSWeidong Wang static int aw_dev_prof_parse_multi_bin(struct aw_device *aw_dev, unsigned char *data,
3634345865bSWeidong Wang 				unsigned int data_len, struct aw_prof_desc *prof_desc)
3644345865bSWeidong Wang {
3654345865bSWeidong Wang 	int ret;
3664345865bSWeidong Wang 	int i;
3674345865bSWeidong Wang 
368*d0f4ce8aSKrzysztof Kozlowski 	struct aw_bin *aw_bin __free(kfree) = kzalloc(data_len + sizeof(struct aw_bin),
369*d0f4ce8aSKrzysztof Kozlowski 						     GFP_KERNEL);
3704345865bSWeidong Wang 	if (!aw_bin)
3714345865bSWeidong Wang 		return -ENOMEM;
3724345865bSWeidong Wang 
3734345865bSWeidong Wang 	aw_bin->info.len = data_len;
3744345865bSWeidong Wang 	memcpy(aw_bin->info.data, data, data_len);
3754345865bSWeidong Wang 
3764345865bSWeidong Wang 	ret = aw_parsing_bin_file(aw_dev, aw_bin);
3774345865bSWeidong Wang 	if (ret < 0) {
3784345865bSWeidong Wang 		dev_err(aw_dev->dev, "parse bin failed");
379*d0f4ce8aSKrzysztof Kozlowski 		return ret;
3804345865bSWeidong Wang 	}
3814345865bSWeidong Wang 
3824345865bSWeidong Wang 	for (i = 0; i < aw_bin->all_bin_parse_num; i++) {
3834345865bSWeidong Wang 		switch (aw_bin->header_info[i].bin_data_type) {
3844345865bSWeidong Wang 		case DATA_TYPE_REGISTER:
3854345865bSWeidong Wang 			prof_desc->sec_desc[AW88395_DATA_TYPE_REG].len =
3864345865bSWeidong Wang 					aw_bin->header_info[i].valid_data_len;
3874345865bSWeidong Wang 			prof_desc->sec_desc[AW88395_DATA_TYPE_REG].data =
3884345865bSWeidong Wang 					data + aw_bin->header_info[i].valid_data_addr;
3894345865bSWeidong Wang 			break;
3904345865bSWeidong Wang 		case DATA_TYPE_DSP_REG:
391*d0f4ce8aSKrzysztof Kozlowski 			if (aw_bin->header_info[i].valid_data_len & 0x01)
392*d0f4ce8aSKrzysztof Kozlowski 				return -EINVAL;
3934345865bSWeidong Wang 
3944345865bSWeidong Wang 			swab16_array((u16 *)(data + aw_bin->header_info[i].valid_data_addr),
3954345865bSWeidong Wang 					aw_bin->header_info[i].valid_data_len >> 1);
3964345865bSWeidong Wang 
3974345865bSWeidong Wang 			prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_CFG].len =
3984345865bSWeidong Wang 					aw_bin->header_info[i].valid_data_len;
3994345865bSWeidong Wang 			prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_CFG].data =
4004345865bSWeidong Wang 					data + aw_bin->header_info[i].valid_data_addr;
4014345865bSWeidong Wang 			break;
4024345865bSWeidong Wang 		case DATA_TYPE_DSP_FW:
4034345865bSWeidong Wang 		case DATA_TYPE_SOC_APP:
404*d0f4ce8aSKrzysztof Kozlowski 			if (aw_bin->header_info[i].valid_data_len & 0x01)
405*d0f4ce8aSKrzysztof Kozlowski 				return -EINVAL;
4064345865bSWeidong Wang 
4074345865bSWeidong Wang 			swab16_array((u16 *)(data + aw_bin->header_info[i].valid_data_addr),
4084345865bSWeidong Wang 					aw_bin->header_info[i].valid_data_len >> 1);
4094345865bSWeidong Wang 
4104345865bSWeidong Wang 			prof_desc->fw_ver = aw_bin->header_info[i].app_version;
4114345865bSWeidong Wang 			prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_FW].len =
4124345865bSWeidong Wang 					aw_bin->header_info[i].valid_data_len;
4134345865bSWeidong Wang 			prof_desc->sec_desc[AW88395_DATA_TYPE_DSP_FW].data =
4144345865bSWeidong Wang 					data + aw_bin->header_info[i].valid_data_addr;
4154345865bSWeidong Wang 			break;
4164345865bSWeidong Wang 		default:
4174345865bSWeidong Wang 			dev_dbg(aw_dev->dev, "bin_data_type not found");
4184345865bSWeidong Wang 			break;
4194345865bSWeidong Wang 		}
4204345865bSWeidong Wang 	}
4214345865bSWeidong Wang 	prof_desc->prof_st = AW88395_PROFILE_OK;
4224345865bSWeidong Wang 
423*d0f4ce8aSKrzysztof Kozlowski 	return 0;
4244345865bSWeidong Wang }
4254345865bSWeidong Wang 
aw_dev_parse_reg_bin_with_hdr(struct aw_device * aw_dev,uint8_t * data,uint32_t data_len,struct aw_prof_desc * prof_desc)4267f4ec778SWeidong Wang static int aw_dev_parse_reg_bin_with_hdr(struct aw_device *aw_dev,
4277f4ec778SWeidong Wang 			uint8_t *data, uint32_t data_len, struct aw_prof_desc *prof_desc)
4287f4ec778SWeidong Wang {
4297f4ec778SWeidong Wang 	int ret;
4307f4ec778SWeidong Wang 
431*d0f4ce8aSKrzysztof Kozlowski 	struct aw_bin *aw_bin __free(kfree) = kzalloc(data_len + sizeof(*aw_bin),
432*d0f4ce8aSKrzysztof Kozlowski 						      GFP_KERNEL);
4337f4ec778SWeidong Wang 	if (!aw_bin)
4347f4ec778SWeidong Wang 		return -ENOMEM;
4357f4ec778SWeidong Wang 
4367f4ec778SWeidong Wang 	aw_bin->info.len = data_len;
4377f4ec778SWeidong Wang 	memcpy(aw_bin->info.data, data, data_len);
4387f4ec778SWeidong Wang 
4397f4ec778SWeidong Wang 	ret = aw_parsing_bin_file(aw_dev, aw_bin);
4407f4ec778SWeidong Wang 	if (ret < 0) {
4417f4ec778SWeidong Wang 		dev_err(aw_dev->dev, "parse bin failed");
442*d0f4ce8aSKrzysztof Kozlowski 		return ret;
4437f4ec778SWeidong Wang 	}
4447f4ec778SWeidong Wang 
4457f4ec778SWeidong Wang 	if ((aw_bin->all_bin_parse_num != 1) ||
4467f4ec778SWeidong Wang 		(aw_bin->header_info[0].bin_data_type != DATA_TYPE_REGISTER)) {
4477f4ec778SWeidong Wang 		dev_err(aw_dev->dev, "bin num or type error");
448*d0f4ce8aSKrzysztof Kozlowski 		return -EINVAL;
4497f4ec778SWeidong Wang 	}
4507f4ec778SWeidong Wang 
4517f4ec778SWeidong Wang 	prof_desc->sec_desc[AW88395_DATA_TYPE_REG].data =
4527f4ec778SWeidong Wang 				data + aw_bin->header_info[0].valid_data_addr;
4537f4ec778SWeidong Wang 	prof_desc->sec_desc[AW88395_DATA_TYPE_REG].len =
4547f4ec778SWeidong Wang 				aw_bin->header_info[0].valid_data_len;
4557f4ec778SWeidong Wang 	prof_desc->prof_st = AW88395_PROFILE_OK;
4567f4ec778SWeidong Wang 
4577f4ec778SWeidong Wang 	return 0;
4587f4ec778SWeidong Wang }
4597f4ec778SWeidong Wang 
aw_dev_parse_data_by_sec_type(struct aw_device * aw_dev,struct aw_cfg_hdr * cfg_hdr,struct aw_cfg_dde * cfg_dde,struct aw_prof_desc * scene_prof_desc)4604345865bSWeidong Wang static int aw_dev_parse_data_by_sec_type(struct aw_device *aw_dev, struct aw_cfg_hdr *cfg_hdr,
4614345865bSWeidong Wang 			struct aw_cfg_dde *cfg_dde, struct aw_prof_desc *scene_prof_desc)
4624345865bSWeidong Wang {
4634345865bSWeidong Wang 	switch (cfg_dde->data_type) {
4644345865bSWeidong Wang 	case ACF_SEC_TYPE_REG:
4654345865bSWeidong Wang 		return aw_dev_parse_raw_reg((u8 *)cfg_hdr + cfg_dde->data_offset,
4664345865bSWeidong Wang 				cfg_dde->data_size, scene_prof_desc);
4674345865bSWeidong Wang 	case ACF_SEC_TYPE_DSP_CFG:
4684345865bSWeidong Wang 		return aw_dev_parse_raw_dsp_cfg((u8 *)cfg_hdr + cfg_dde->data_offset,
4694345865bSWeidong Wang 				cfg_dde->data_size, scene_prof_desc);
4704345865bSWeidong Wang 	case ACF_SEC_TYPE_DSP_FW:
4714345865bSWeidong Wang 		return aw_dev_parse_raw_dsp_fw(
4724345865bSWeidong Wang 				(u8 *)cfg_hdr + cfg_dde->data_offset,
4734345865bSWeidong Wang 				cfg_dde->data_size, scene_prof_desc);
4744345865bSWeidong Wang 	case ACF_SEC_TYPE_MULTIPLE_BIN:
4754345865bSWeidong Wang 		return aw_dev_prof_parse_multi_bin(
4764345865bSWeidong Wang 				aw_dev, (u8 *)cfg_hdr + cfg_dde->data_offset,
4774345865bSWeidong Wang 				cfg_dde->data_size, scene_prof_desc);
4787f4ec778SWeidong Wang 	case ACF_SEC_TYPE_HDR_REG:
4797f4ec778SWeidong Wang 		return aw_dev_parse_reg_bin_with_hdr(aw_dev, (u8 *)cfg_hdr + cfg_dde->data_offset,
4807f4ec778SWeidong Wang 				cfg_dde->data_size, scene_prof_desc);
4814345865bSWeidong Wang 	default:
4824345865bSWeidong Wang 		dev_err(aw_dev->dev, "%s cfg_dde->data_type = %d\n", __func__, cfg_dde->data_type);
4834345865bSWeidong Wang 		break;
4844345865bSWeidong Wang 	}
4854345865bSWeidong Wang 
4864345865bSWeidong Wang 	return 0;
4874345865bSWeidong Wang }
4884345865bSWeidong Wang 
aw_dev_parse_dev_type(struct aw_device * aw_dev,struct aw_cfg_hdr * prof_hdr,struct aw_all_prof_info * all_prof_info)4894345865bSWeidong Wang static int aw_dev_parse_dev_type(struct aw_device *aw_dev,
4904345865bSWeidong Wang 		struct aw_cfg_hdr *prof_hdr, struct aw_all_prof_info *all_prof_info)
4914345865bSWeidong Wang {
4924345865bSWeidong Wang 	struct aw_cfg_dde *cfg_dde =
4934345865bSWeidong Wang 		(struct aw_cfg_dde *)((char *)prof_hdr + prof_hdr->hdr_offset);
4944345865bSWeidong Wang 	int sec_num = 0;
4954345865bSWeidong Wang 	int ret, i;
4964345865bSWeidong Wang 
4974345865bSWeidong Wang 	for (i = 0; i < prof_hdr->ddt_num; i++) {
4984345865bSWeidong Wang 		if ((aw_dev->i2c->adapter->nr == cfg_dde[i].dev_bus) &&
4994345865bSWeidong Wang 		    (aw_dev->i2c->addr == cfg_dde[i].dev_addr) &&
5004345865bSWeidong Wang 		    (cfg_dde[i].type == AW88395_DEV_TYPE_ID) &&
5014345865bSWeidong Wang 		    (cfg_dde[i].data_type != ACF_SEC_TYPE_MONITOR)) {
5024345865bSWeidong Wang 			if (cfg_dde[i].dev_profile >= AW88395_PROFILE_MAX) {
5034345865bSWeidong Wang 				dev_err(aw_dev->dev, "dev_profile [%d] overflow",
5044345865bSWeidong Wang 							cfg_dde[i].dev_profile);
5054345865bSWeidong Wang 				return -EINVAL;
5064345865bSWeidong Wang 			}
507fc213b8dSWeidong Wang 			aw_dev->prof_data_type = cfg_dde[i].data_type;
5084345865bSWeidong Wang 			ret = aw_dev_parse_data_by_sec_type(aw_dev, prof_hdr, &cfg_dde[i],
5094345865bSWeidong Wang 					&all_prof_info->prof_desc[cfg_dde[i].dev_profile]);
5104345865bSWeidong Wang 			if (ret < 0) {
5114345865bSWeidong Wang 				dev_err(aw_dev->dev, "parse failed");
5124345865bSWeidong Wang 				return ret;
5134345865bSWeidong Wang 			}
5144345865bSWeidong Wang 			sec_num++;
5154345865bSWeidong Wang 		}
5164345865bSWeidong Wang 	}
5174345865bSWeidong Wang 
5184345865bSWeidong Wang 	if (sec_num == 0) {
5194345865bSWeidong Wang 		dev_dbg(aw_dev->dev, "get dev type num is %d, please use default", sec_num);
5204345865bSWeidong Wang 		return AW88395_DEV_TYPE_NONE;
5214345865bSWeidong Wang 	}
5224345865bSWeidong Wang 
5234345865bSWeidong Wang 	return AW88395_DEV_TYPE_OK;
5244345865bSWeidong Wang }
5254345865bSWeidong Wang 
aw_dev_parse_dev_default_type(struct aw_device * aw_dev,struct aw_cfg_hdr * prof_hdr,struct aw_all_prof_info * all_prof_info)5264345865bSWeidong Wang static int aw_dev_parse_dev_default_type(struct aw_device *aw_dev,
5274345865bSWeidong Wang 		struct aw_cfg_hdr *prof_hdr, struct aw_all_prof_info *all_prof_info)
5284345865bSWeidong Wang {
5294345865bSWeidong Wang 	struct aw_cfg_dde *cfg_dde =
5304345865bSWeidong Wang 		(struct aw_cfg_dde *)((char *)prof_hdr + prof_hdr->hdr_offset);
5314345865bSWeidong Wang 	int sec_num = 0;
5324345865bSWeidong Wang 	int ret, i;
5334345865bSWeidong Wang 
5344345865bSWeidong Wang 	for (i = 0; i < prof_hdr->ddt_num; i++) {
5354345865bSWeidong Wang 		if ((aw_dev->channel == cfg_dde[i].dev_index) &&
5364345865bSWeidong Wang 		    (cfg_dde[i].type == AW88395_DEV_DEFAULT_TYPE_ID) &&
5374345865bSWeidong Wang 		    (cfg_dde[i].data_type != ACF_SEC_TYPE_MONITOR)) {
5384345865bSWeidong Wang 			if (cfg_dde[i].dev_profile >= AW88395_PROFILE_MAX) {
5394345865bSWeidong Wang 				dev_err(aw_dev->dev, "dev_profile [%d] overflow",
5404345865bSWeidong Wang 					cfg_dde[i].dev_profile);
5414345865bSWeidong Wang 				return -EINVAL;
5424345865bSWeidong Wang 			}
543fc213b8dSWeidong Wang 			aw_dev->prof_data_type = cfg_dde[i].data_type;
5444345865bSWeidong Wang 			ret = aw_dev_parse_data_by_sec_type(aw_dev, prof_hdr, &cfg_dde[i],
5454345865bSWeidong Wang 					&all_prof_info->prof_desc[cfg_dde[i].dev_profile]);
5464345865bSWeidong Wang 			if (ret < 0) {
5474345865bSWeidong Wang 				dev_err(aw_dev->dev, "parse failed");
5484345865bSWeidong Wang 				return ret;
5494345865bSWeidong Wang 			}
5504345865bSWeidong Wang 			sec_num++;
5514345865bSWeidong Wang 		}
5524345865bSWeidong Wang 	}
5534345865bSWeidong Wang 
5544345865bSWeidong Wang 	if (sec_num == 0) {
5554345865bSWeidong Wang 		dev_err(aw_dev->dev, "get dev default type failed, get num[%d]", sec_num);
5564345865bSWeidong Wang 		return -EINVAL;
5574345865bSWeidong Wang 	}
5584345865bSWeidong Wang 
5594345865bSWeidong Wang 	return 0;
5604345865bSWeidong Wang }
5614345865bSWeidong Wang 
aw_dev_cfg_get_reg_valid_prof(struct aw_device * aw_dev,struct aw_all_prof_info * all_prof_info)562fc213b8dSWeidong Wang static int aw_dev_cfg_get_reg_valid_prof(struct aw_device *aw_dev,
563b116c832SWeidong Wang 				struct aw_all_prof_info *all_prof_info)
5647f4ec778SWeidong Wang {
565b116c832SWeidong Wang 	struct aw_prof_desc *prof_desc = all_prof_info->prof_desc;
5667f4ec778SWeidong Wang 	struct aw_prof_info *prof_info = &aw_dev->prof_info;
5677f4ec778SWeidong Wang 	int num = 0;
5687f4ec778SWeidong Wang 	int i;
5697f4ec778SWeidong Wang 
5707f4ec778SWeidong Wang 	for (i = 0; i < AW88395_PROFILE_MAX; i++) {
5717f4ec778SWeidong Wang 		if (prof_desc[i].prof_st == AW88395_PROFILE_OK)
5727f4ec778SWeidong Wang 			prof_info->count++;
5737f4ec778SWeidong Wang 	}
5747f4ec778SWeidong Wang 
5757f4ec778SWeidong Wang 	dev_dbg(aw_dev->dev, "get valid profile:%d", aw_dev->prof_info.count);
5767f4ec778SWeidong Wang 
5777f4ec778SWeidong Wang 	if (!prof_info->count) {
5787f4ec778SWeidong Wang 		dev_err(aw_dev->dev, "no profile data");
5797f4ec778SWeidong Wang 		return -EPERM;
5807f4ec778SWeidong Wang 	}
5817f4ec778SWeidong Wang 
5827f4ec778SWeidong Wang 	prof_info->prof_desc = devm_kcalloc(aw_dev->dev,
5837f4ec778SWeidong Wang 					prof_info->count, sizeof(struct aw_prof_desc),
5847f4ec778SWeidong Wang 					GFP_KERNEL);
5857f4ec778SWeidong Wang 	if (!prof_info->prof_desc)
5867f4ec778SWeidong Wang 		return -ENOMEM;
5877f4ec778SWeidong Wang 
5887f4ec778SWeidong Wang 	for (i = 0; i < AW88395_PROFILE_MAX; i++) {
5897f4ec778SWeidong Wang 		if (prof_desc[i].prof_st == AW88395_PROFILE_OK) {
5907f4ec778SWeidong Wang 			if (num >= prof_info->count) {
5917f4ec778SWeidong Wang 				dev_err(aw_dev->dev, "overflow count[%d]",
5927f4ec778SWeidong Wang 						prof_info->count);
5937f4ec778SWeidong Wang 				return -EINVAL;
5947f4ec778SWeidong Wang 			}
5957f4ec778SWeidong Wang 			prof_info->prof_desc[num] = prof_desc[i];
5967f4ec778SWeidong Wang 			prof_info->prof_desc[num].id = i;
5977f4ec778SWeidong Wang 			num++;
5987f4ec778SWeidong Wang 		}
5997f4ec778SWeidong Wang 	}
6007f4ec778SWeidong Wang 
6017f4ec778SWeidong Wang 	return 0;
6027f4ec778SWeidong Wang }
6037f4ec778SWeidong Wang 
aw_dev_cfg_get_multiple_valid_prof(struct aw_device * aw_dev,struct aw_all_prof_info * all_prof_info)604fc213b8dSWeidong Wang static int aw_dev_cfg_get_multiple_valid_prof(struct aw_device *aw_dev,
605b116c832SWeidong Wang 				struct aw_all_prof_info *all_prof_info)
6064345865bSWeidong Wang {
607b116c832SWeidong Wang 	struct aw_prof_desc *prof_desc = all_prof_info->prof_desc;
6084345865bSWeidong Wang 	struct aw_prof_info *prof_info = &aw_dev->prof_info;
6094345865bSWeidong Wang 	struct aw_sec_data_desc *sec_desc;
6104345865bSWeidong Wang 	int num = 0;
6114345865bSWeidong Wang 	int i;
6124345865bSWeidong Wang 
6134345865bSWeidong Wang 	for (i = 0; i < AW88395_PROFILE_MAX; i++) {
6144345865bSWeidong Wang 		if (prof_desc[i].prof_st == AW88395_PROFILE_OK) {
6154345865bSWeidong Wang 			sec_desc = prof_desc[i].sec_desc;
6164345865bSWeidong Wang 			if ((sec_desc[AW88395_DATA_TYPE_REG].data != NULL) &&
6174345865bSWeidong Wang 			    (sec_desc[AW88395_DATA_TYPE_REG].len != 0) &&
6184345865bSWeidong Wang 			    (sec_desc[AW88395_DATA_TYPE_DSP_CFG].data != NULL) &&
6194345865bSWeidong Wang 			    (sec_desc[AW88395_DATA_TYPE_DSP_CFG].len != 0) &&
6204345865bSWeidong Wang 			    (sec_desc[AW88395_DATA_TYPE_DSP_FW].data != NULL) &&
6214345865bSWeidong Wang 			    (sec_desc[AW88395_DATA_TYPE_DSP_FW].len != 0))
6224345865bSWeidong Wang 				prof_info->count++;
6234345865bSWeidong Wang 		}
6244345865bSWeidong Wang 	}
6254345865bSWeidong Wang 
6264345865bSWeidong Wang 	dev_dbg(aw_dev->dev, "get valid profile:%d", aw_dev->prof_info.count);
6274345865bSWeidong Wang 
6284345865bSWeidong Wang 	if (!prof_info->count) {
6294345865bSWeidong Wang 		dev_err(aw_dev->dev, "no profile data");
6304345865bSWeidong Wang 		return -EPERM;
6314345865bSWeidong Wang 	}
6324345865bSWeidong Wang 
6334345865bSWeidong Wang 	prof_info->prof_desc = devm_kcalloc(aw_dev->dev,
6344345865bSWeidong Wang 					prof_info->count, sizeof(struct aw_prof_desc),
6354345865bSWeidong Wang 					GFP_KERNEL);
6364345865bSWeidong Wang 	if (!prof_info->prof_desc)
6374345865bSWeidong Wang 		return -ENOMEM;
6384345865bSWeidong Wang 
6394345865bSWeidong Wang 	for (i = 0; i < AW88395_PROFILE_MAX; i++) {
6404345865bSWeidong Wang 		if (prof_desc[i].prof_st == AW88395_PROFILE_OK) {
6414345865bSWeidong Wang 			sec_desc = prof_desc[i].sec_desc;
6424345865bSWeidong Wang 			if ((sec_desc[AW88395_DATA_TYPE_REG].data != NULL) &&
6434345865bSWeidong Wang 			    (sec_desc[AW88395_DATA_TYPE_REG].len != 0) &&
6444345865bSWeidong Wang 			    (sec_desc[AW88395_DATA_TYPE_DSP_CFG].data != NULL) &&
6454345865bSWeidong Wang 			    (sec_desc[AW88395_DATA_TYPE_DSP_CFG].len != 0) &&
6464345865bSWeidong Wang 			    (sec_desc[AW88395_DATA_TYPE_DSP_FW].data != NULL) &&
6474345865bSWeidong Wang 			    (sec_desc[AW88395_DATA_TYPE_DSP_FW].len != 0)) {
6484345865bSWeidong Wang 				if (num >= prof_info->count) {
6494345865bSWeidong Wang 					dev_err(aw_dev->dev, "overflow count[%d]",
6504345865bSWeidong Wang 							prof_info->count);
6514345865bSWeidong Wang 					return -EINVAL;
6524345865bSWeidong Wang 				}
6534345865bSWeidong Wang 				prof_info->prof_desc[num] = prof_desc[i];
6544345865bSWeidong Wang 				prof_info->prof_desc[num].id = i;
6554345865bSWeidong Wang 				num++;
6564345865bSWeidong Wang 			}
6574345865bSWeidong Wang 		}
6584345865bSWeidong Wang 	}
6594345865bSWeidong Wang 
6604345865bSWeidong Wang 	return 0;
6614345865bSWeidong Wang }
6624345865bSWeidong Wang 
aw_dev_load_cfg_by_hdr(struct aw_device * aw_dev,struct aw_cfg_hdr * prof_hdr)6634345865bSWeidong Wang static int aw_dev_load_cfg_by_hdr(struct aw_device *aw_dev,
6644345865bSWeidong Wang 		struct aw_cfg_hdr *prof_hdr)
6654345865bSWeidong Wang {
6664345865bSWeidong Wang 	int ret;
6674345865bSWeidong Wang 
668*d0f4ce8aSKrzysztof Kozlowski 	struct aw_all_prof_info *all_prof_info __free(kfree) = kzalloc(sizeof(*all_prof_info),
669*d0f4ce8aSKrzysztof Kozlowski 								       GFP_KERNEL);
6704345865bSWeidong Wang 	if (!all_prof_info)
6714345865bSWeidong Wang 		return -ENOMEM;
6724345865bSWeidong Wang 
6734345865bSWeidong Wang 	ret = aw_dev_parse_dev_type(aw_dev, prof_hdr, all_prof_info);
6744345865bSWeidong Wang 	if (ret < 0) {
675*d0f4ce8aSKrzysztof Kozlowski 		return ret;
6764345865bSWeidong Wang 	} else if (ret == AW88395_DEV_TYPE_NONE) {
6774345865bSWeidong Wang 		dev_dbg(aw_dev->dev, "get dev type num is 0, parse default dev");
6784345865bSWeidong Wang 		ret = aw_dev_parse_dev_default_type(aw_dev, prof_hdr, all_prof_info);
6794345865bSWeidong Wang 		if (ret < 0)
680*d0f4ce8aSKrzysztof Kozlowski 			return ret;
6814345865bSWeidong Wang 	}
6824345865bSWeidong Wang 
683fc213b8dSWeidong Wang 	switch (aw_dev->prof_data_type) {
684fc213b8dSWeidong Wang 	case ACF_SEC_TYPE_MULTIPLE_BIN:
685fc213b8dSWeidong Wang 		ret = aw_dev_cfg_get_multiple_valid_prof(aw_dev, all_prof_info);
6867f4ec778SWeidong Wang 		break;
687fc213b8dSWeidong Wang 	case ACF_SEC_TYPE_HDR_REG:
688fc213b8dSWeidong Wang 		ret = aw_dev_cfg_get_reg_valid_prof(aw_dev, all_prof_info);
6897f4ec778SWeidong Wang 		break;
6907f4ec778SWeidong Wang 	default:
691fc213b8dSWeidong Wang 		dev_err(aw_dev->dev, "unsupport data type\n");
6927f4ec778SWeidong Wang 		ret = -EINVAL;
6937f4ec778SWeidong Wang 		break;
6947f4ec778SWeidong Wang 	}
695fc213b8dSWeidong Wang 	if (!ret)
6964345865bSWeidong Wang 		aw_dev->prof_info.prof_name_list = profile_name;
6974345865bSWeidong Wang 
6984345865bSWeidong Wang 	return ret;
6994345865bSWeidong Wang }
7004345865bSWeidong Wang 
aw_dev_create_prof_name_list_v1(struct aw_device * aw_dev)7014345865bSWeidong Wang static int aw_dev_create_prof_name_list_v1(struct aw_device *aw_dev)
7024345865bSWeidong Wang {
7034345865bSWeidong Wang 	struct aw_prof_info *prof_info = &aw_dev->prof_info;
7044345865bSWeidong Wang 	struct aw_prof_desc *prof_desc = prof_info->prof_desc;
7054345865bSWeidong Wang 	int i;
7064345865bSWeidong Wang 
7074345865bSWeidong Wang 	if (!prof_desc) {
7084345865bSWeidong Wang 		dev_err(aw_dev->dev, "prof_desc is NULL");
7094345865bSWeidong Wang 		return -EINVAL;
7104345865bSWeidong Wang 	}
7114345865bSWeidong Wang 
7124345865bSWeidong Wang 	prof_info->prof_name_list = devm_kzalloc(aw_dev->dev,
7134345865bSWeidong Wang 					prof_info->count * PROFILE_STR_MAX,
7144345865bSWeidong Wang 					GFP_KERNEL);
7154345865bSWeidong Wang 	if (!prof_info->prof_name_list)
7164345865bSWeidong Wang 		return -ENOMEM;
7174345865bSWeidong Wang 
7184345865bSWeidong Wang 	for (i = 0; i < prof_info->count; i++) {
7194345865bSWeidong Wang 		prof_desc[i].id = i;
7204345865bSWeidong Wang 		prof_info->prof_name_list[i] = prof_desc[i].prf_str;
7214345865bSWeidong Wang 		dev_dbg(aw_dev->dev, "prof name is %s", prof_info->prof_name_list[i]);
7224345865bSWeidong Wang 	}
7234345865bSWeidong Wang 
7244345865bSWeidong Wang 	return 0;
7254345865bSWeidong Wang }
7264345865bSWeidong Wang 
aw_get_dde_type_info(struct aw_device * aw_dev,struct aw_container * aw_cfg)7274345865bSWeidong Wang static int aw_get_dde_type_info(struct aw_device *aw_dev, struct aw_container *aw_cfg)
7284345865bSWeidong Wang {
7294345865bSWeidong Wang 	struct aw_cfg_hdr *cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
7304345865bSWeidong Wang 	struct aw_cfg_dde_v1 *cfg_dde =
7314345865bSWeidong Wang 		(struct aw_cfg_dde_v1 *)(aw_cfg->data + cfg_hdr->hdr_offset);
7324345865bSWeidong Wang 	int default_num = 0;
7334345865bSWeidong Wang 	int dev_num = 0;
7344345865bSWeidong Wang 	unsigned int i;
7354345865bSWeidong Wang 
7364345865bSWeidong Wang 	for (i = 0; i < cfg_hdr->ddt_num; i++) {
7374345865bSWeidong Wang 		if (cfg_dde[i].type == AW88395_DEV_TYPE_ID)
7384345865bSWeidong Wang 			dev_num++;
7394345865bSWeidong Wang 
7404345865bSWeidong Wang 		if (cfg_dde[i].type == AW88395_DEV_DEFAULT_TYPE_ID)
7414345865bSWeidong Wang 			default_num++;
7424345865bSWeidong Wang 	}
7434345865bSWeidong Wang 
7444345865bSWeidong Wang 	if (dev_num != 0) {
7454345865bSWeidong Wang 		aw_dev->prof_info.prof_type = AW88395_DEV_TYPE_ID;
7464345865bSWeidong Wang 	} else if (default_num != 0) {
7474345865bSWeidong Wang 		aw_dev->prof_info.prof_type = AW88395_DEV_DEFAULT_TYPE_ID;
7484345865bSWeidong Wang 	} else {
7494345865bSWeidong Wang 		dev_err(aw_dev->dev, "can't find scene");
7504345865bSWeidong Wang 		return -EINVAL;
7514345865bSWeidong Wang 	}
7524345865bSWeidong Wang 
7534345865bSWeidong Wang 	return 0;
7544345865bSWeidong Wang }
7554345865bSWeidong Wang 
aw_get_dev_scene_count_v1(struct aw_device * aw_dev,struct aw_container * aw_cfg,unsigned int * scene_num)7564345865bSWeidong Wang static int aw_get_dev_scene_count_v1(struct aw_device *aw_dev, struct aw_container *aw_cfg,
7574345865bSWeidong Wang 						unsigned int *scene_num)
7584345865bSWeidong Wang {
7594345865bSWeidong Wang 	struct aw_cfg_hdr *cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
7604345865bSWeidong Wang 	struct aw_cfg_dde_v1 *cfg_dde =
7614345865bSWeidong Wang 		(struct aw_cfg_dde_v1 *)(aw_cfg->data + cfg_hdr->hdr_offset);
7624345865bSWeidong Wang 	unsigned int i;
7634345865bSWeidong Wang 
7647f4ec778SWeidong Wang 	for (i = 0; i < cfg_hdr->ddt_num; ++i) {
7657f4ec778SWeidong Wang 		if (((cfg_dde[i].data_type == ACF_SEC_TYPE_REG) ||
766fc213b8dSWeidong Wang 		     (cfg_dde[i].data_type == ACF_SEC_TYPE_HDR_REG) ||
767fc213b8dSWeidong Wang 		     (cfg_dde[i].data_type == ACF_SEC_TYPE_MULTIPLE_BIN)) &&
7687f4ec778SWeidong Wang 		    (aw_dev->chip_id == cfg_dde[i].chip_id) &&
7697f4ec778SWeidong Wang 		    (aw_dev->i2c->adapter->nr == cfg_dde[i].dev_bus) &&
7707f4ec778SWeidong Wang 		    (aw_dev->i2c->addr == cfg_dde[i].dev_addr))
7717f4ec778SWeidong Wang 			(*scene_num)++;
7727f4ec778SWeidong Wang 	}
773fc213b8dSWeidong Wang 
774fc213b8dSWeidong Wang 	if ((*scene_num) == 0) {
775fc213b8dSWeidong Wang 		dev_err(aw_dev->dev, "failed to obtain scene, scenu_num = %d\n", (*scene_num));
776fc213b8dSWeidong Wang 		return -EINVAL;
7777f4ec778SWeidong Wang 	}
7784345865bSWeidong Wang 
779fc213b8dSWeidong Wang 	return 0;
7804345865bSWeidong Wang }
7814345865bSWeidong Wang 
aw_get_default_scene_count_v1(struct aw_device * aw_dev,struct aw_container * aw_cfg,unsigned int * scene_num)7824345865bSWeidong Wang static int aw_get_default_scene_count_v1(struct aw_device *aw_dev,
7834345865bSWeidong Wang 						struct aw_container *aw_cfg,
7844345865bSWeidong Wang 						unsigned int *scene_num)
7854345865bSWeidong Wang {
7864345865bSWeidong Wang 	struct aw_cfg_hdr *cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
7874345865bSWeidong Wang 	struct aw_cfg_dde_v1 *cfg_dde =
7884345865bSWeidong Wang 		(struct aw_cfg_dde_v1 *)(aw_cfg->data + cfg_hdr->hdr_offset);
7894345865bSWeidong Wang 	unsigned int i;
7904345865bSWeidong Wang 
791fc213b8dSWeidong Wang 
7924345865bSWeidong Wang 	for (i = 0; i < cfg_hdr->ddt_num; ++i) {
793fc213b8dSWeidong Wang 		if (((cfg_dde[i].data_type == ACF_SEC_TYPE_MULTIPLE_BIN) ||
794fc213b8dSWeidong Wang 		     (cfg_dde[i].data_type == ACF_SEC_TYPE_REG) ||
7957f4ec778SWeidong Wang 		     (cfg_dde[i].data_type == ACF_SEC_TYPE_HDR_REG)) &&
7967f4ec778SWeidong Wang 		    (aw_dev->chip_id == cfg_dde[i].chip_id) &&
7977f4ec778SWeidong Wang 		    (aw_dev->channel == cfg_dde[i].dev_index))
7987f4ec778SWeidong Wang 			(*scene_num)++;
7997f4ec778SWeidong Wang 	}
800fc213b8dSWeidong Wang 
801fc213b8dSWeidong Wang 	if ((*scene_num) == 0) {
802fc213b8dSWeidong Wang 		dev_err(aw_dev->dev, "failed to obtain scene, scenu_num = %d\n", (*scene_num));
803fc213b8dSWeidong Wang 		return -EINVAL;
8047f4ec778SWeidong Wang 	}
8054345865bSWeidong Wang 
806fc213b8dSWeidong Wang 	return 0;
8074345865bSWeidong Wang }
8084345865bSWeidong Wang 
aw_dev_parse_scene_count_v1(struct aw_device * aw_dev,struct aw_container * aw_cfg,unsigned int * count)8094345865bSWeidong Wang static int aw_dev_parse_scene_count_v1(struct aw_device *aw_dev,
8104345865bSWeidong Wang 							struct aw_container *aw_cfg,
8114345865bSWeidong Wang 							unsigned int *count)
8124345865bSWeidong Wang {
8134345865bSWeidong Wang 	int ret;
8144345865bSWeidong Wang 
8154345865bSWeidong Wang 	ret = aw_get_dde_type_info(aw_dev, aw_cfg);
8164345865bSWeidong Wang 	if (ret < 0)
8174345865bSWeidong Wang 		return ret;
8184345865bSWeidong Wang 
8194345865bSWeidong Wang 	switch (aw_dev->prof_info.prof_type) {
8204345865bSWeidong Wang 	case AW88395_DEV_TYPE_ID:
8214345865bSWeidong Wang 		ret = aw_get_dev_scene_count_v1(aw_dev, aw_cfg, count);
8224345865bSWeidong Wang 		break;
8234345865bSWeidong Wang 	case AW88395_DEV_DEFAULT_TYPE_ID:
8244345865bSWeidong Wang 		ret = aw_get_default_scene_count_v1(aw_dev, aw_cfg, count);
8254345865bSWeidong Wang 		break;
8264345865bSWeidong Wang 	default:
8274345865bSWeidong Wang 		dev_err(aw_dev->dev, "unsupported prof_type[%x]", aw_dev->prof_info.prof_type);
8284345865bSWeidong Wang 		ret = -EINVAL;
8294345865bSWeidong Wang 		break;
8304345865bSWeidong Wang 	}
8314345865bSWeidong Wang 
8324345865bSWeidong Wang 	return ret;
8334345865bSWeidong Wang }
8344345865bSWeidong Wang 
aw_dev_parse_data_by_sec_type_v1(struct aw_device * aw_dev,struct aw_cfg_hdr * prof_hdr,struct aw_cfg_dde_v1 * cfg_dde,int * cur_scene_id)8354345865bSWeidong Wang static int aw_dev_parse_data_by_sec_type_v1(struct aw_device *aw_dev,
8364345865bSWeidong Wang 							struct aw_cfg_hdr *prof_hdr,
8374345865bSWeidong Wang 							struct aw_cfg_dde_v1 *cfg_dde,
8384345865bSWeidong Wang 							int *cur_scene_id)
8394345865bSWeidong Wang {
8404345865bSWeidong Wang 	struct aw_prof_info *prof_info = &aw_dev->prof_info;
8414345865bSWeidong Wang 	int ret;
8424345865bSWeidong Wang 
8434345865bSWeidong Wang 	switch (cfg_dde->data_type) {
8444345865bSWeidong Wang 	case ACF_SEC_TYPE_MULTIPLE_BIN:
8454345865bSWeidong Wang 		ret = aw_dev_prof_parse_multi_bin(aw_dev, (u8 *)prof_hdr + cfg_dde->data_offset,
8464345865bSWeidong Wang 					cfg_dde->data_size, &prof_info->prof_desc[*cur_scene_id]);
8474345865bSWeidong Wang 		if (ret < 0) {
8484345865bSWeidong Wang 			dev_err(aw_dev->dev, "parse multi bin failed");
8494345865bSWeidong Wang 			return ret;
8504345865bSWeidong Wang 		}
8514345865bSWeidong Wang 		prof_info->prof_desc[*cur_scene_id].prf_str = cfg_dde->dev_profile_str;
8524345865bSWeidong Wang 		prof_info->prof_desc[*cur_scene_id].id = cfg_dde->dev_profile;
8534345865bSWeidong Wang 		(*cur_scene_id)++;
8544345865bSWeidong Wang 		break;
8557f4ec778SWeidong Wang 	case ACF_SEC_TYPE_HDR_REG:
8567f4ec778SWeidong Wang 		ret =  aw_dev_parse_reg_bin_with_hdr(aw_dev,
8577f4ec778SWeidong Wang 				(uint8_t *)prof_hdr + cfg_dde->data_offset,
8587f4ec778SWeidong Wang 				cfg_dde->data_size, &prof_info->prof_desc[*cur_scene_id]);
8597f4ec778SWeidong Wang 		if (ret < 0) {
8607f4ec778SWeidong Wang 			dev_err(aw_dev->dev, "parse reg bin with hdr failed");
8617f4ec778SWeidong Wang 			return ret;
8627f4ec778SWeidong Wang 		}
8637f4ec778SWeidong Wang 		prof_info->prof_desc[*cur_scene_id].prf_str = cfg_dde->dev_profile_str;
8647f4ec778SWeidong Wang 		prof_info->prof_desc[*cur_scene_id].id = cfg_dde->dev_profile;
8657f4ec778SWeidong Wang 		(*cur_scene_id)++;
8667f4ec778SWeidong Wang 		break;
8674345865bSWeidong Wang 	default:
8684345865bSWeidong Wang 		dev_err(aw_dev->dev, "unsupported SEC_TYPE [%d]", cfg_dde->data_type);
8694345865bSWeidong Wang 		return -EINVAL;
8704345865bSWeidong Wang 	}
8714345865bSWeidong Wang 
8724345865bSWeidong Wang 	return 0;
8734345865bSWeidong Wang }
8744345865bSWeidong Wang 
aw_dev_parse_dev_type_v1(struct aw_device * aw_dev,struct aw_cfg_hdr * prof_hdr)8754345865bSWeidong Wang static int aw_dev_parse_dev_type_v1(struct aw_device *aw_dev,
8764345865bSWeidong Wang 		struct aw_cfg_hdr *prof_hdr)
8774345865bSWeidong Wang {
8784345865bSWeidong Wang 	struct aw_cfg_dde_v1 *cfg_dde =
8794345865bSWeidong Wang 		(struct aw_cfg_dde_v1 *)((char *)prof_hdr + prof_hdr->hdr_offset);
880983272a2STom Rix 	int cur_scene_id = 0;
8814345865bSWeidong Wang 	unsigned int i;
8824345865bSWeidong Wang 	int ret;
8834345865bSWeidong Wang 
8844345865bSWeidong Wang 	for (i = 0; i < prof_hdr->ddt_num; i++) {
8854345865bSWeidong Wang 		if ((aw_dev->i2c->adapter->nr == cfg_dde[i].dev_bus) &&
8864345865bSWeidong Wang 		    (aw_dev->i2c->addr == cfg_dde[i].dev_addr) &&
8874345865bSWeidong Wang 		    (aw_dev->chip_id == cfg_dde[i].chip_id)) {
8884345865bSWeidong Wang 			ret = aw_dev_parse_data_by_sec_type_v1(aw_dev, prof_hdr,
8894345865bSWeidong Wang 							&cfg_dde[i], &cur_scene_id);
8904345865bSWeidong Wang 			if (ret < 0) {
8914345865bSWeidong Wang 				dev_err(aw_dev->dev, "parse failed");
8924345865bSWeidong Wang 				return ret;
8934345865bSWeidong Wang 			}
8944345865bSWeidong Wang 		}
8954345865bSWeidong Wang 	}
8964345865bSWeidong Wang 
8974345865bSWeidong Wang 	if (cur_scene_id == 0) {
8984345865bSWeidong Wang 		dev_err(aw_dev->dev, "get dev type failed, get num [%d]", cur_scene_id);
8994345865bSWeidong Wang 		return -EINVAL;
9004345865bSWeidong Wang 	}
9014345865bSWeidong Wang 
9024345865bSWeidong Wang 	return 0;
9034345865bSWeidong Wang }
9044345865bSWeidong Wang 
aw_dev_parse_default_type_v1(struct aw_device * aw_dev,struct aw_cfg_hdr * prof_hdr)9054345865bSWeidong Wang static int aw_dev_parse_default_type_v1(struct aw_device *aw_dev,
9064345865bSWeidong Wang 		struct aw_cfg_hdr *prof_hdr)
9074345865bSWeidong Wang {
9084345865bSWeidong Wang 	struct aw_cfg_dde_v1 *cfg_dde =
9094345865bSWeidong Wang 		(struct aw_cfg_dde_v1 *)((char *)prof_hdr + prof_hdr->hdr_offset);
9104345865bSWeidong Wang 	int cur_scene_id = 0;
9114345865bSWeidong Wang 	unsigned int i;
9124345865bSWeidong Wang 	int ret;
9134345865bSWeidong Wang 
9144345865bSWeidong Wang 	for (i = 0; i < prof_hdr->ddt_num; i++) {
9154345865bSWeidong Wang 		if ((aw_dev->channel == cfg_dde[i].dev_index) &&
9164345865bSWeidong Wang 			(aw_dev->chip_id == cfg_dde[i].chip_id)) {
9174345865bSWeidong Wang 			ret = aw_dev_parse_data_by_sec_type_v1(aw_dev, prof_hdr,
9184345865bSWeidong Wang 							&cfg_dde[i], &cur_scene_id);
9194345865bSWeidong Wang 			if (ret < 0) {
9204345865bSWeidong Wang 				dev_err(aw_dev->dev, "parse failed");
9214345865bSWeidong Wang 				return ret;
9224345865bSWeidong Wang 			}
9234345865bSWeidong Wang 		}
9244345865bSWeidong Wang 	}
9254345865bSWeidong Wang 
9264345865bSWeidong Wang 	if (cur_scene_id == 0) {
9274345865bSWeidong Wang 		dev_err(aw_dev->dev, "get dev default type failed, get num[%d]", cur_scene_id);
9284345865bSWeidong Wang 		return -EINVAL;
9294345865bSWeidong Wang 	}
9304345865bSWeidong Wang 
9314345865bSWeidong Wang 	return 0;
9324345865bSWeidong Wang }
9334345865bSWeidong Wang 
aw_dev_parse_by_hdr_v1(struct aw_device * aw_dev,struct aw_cfg_hdr * cfg_hdr)9344345865bSWeidong Wang static int aw_dev_parse_by_hdr_v1(struct aw_device *aw_dev,
9354345865bSWeidong Wang 		struct aw_cfg_hdr *cfg_hdr)
9364345865bSWeidong Wang {
9374345865bSWeidong Wang 	int ret;
9384345865bSWeidong Wang 
9394345865bSWeidong Wang 	switch (aw_dev->prof_info.prof_type) {
9404345865bSWeidong Wang 	case AW88395_DEV_TYPE_ID:
9414345865bSWeidong Wang 		ret = aw_dev_parse_dev_type_v1(aw_dev, cfg_hdr);
9424345865bSWeidong Wang 		break;
9434345865bSWeidong Wang 	case AW88395_DEV_DEFAULT_TYPE_ID:
9444345865bSWeidong Wang 		ret = aw_dev_parse_default_type_v1(aw_dev, cfg_hdr);
9454345865bSWeidong Wang 		break;
9464345865bSWeidong Wang 	default:
9474345865bSWeidong Wang 		dev_err(aw_dev->dev, "prof type matched failed, get num[%d]",
9484345865bSWeidong Wang 			aw_dev->prof_info.prof_type);
9494345865bSWeidong Wang 		ret =  -EINVAL;
9504345865bSWeidong Wang 		break;
9514345865bSWeidong Wang 	}
9524345865bSWeidong Wang 
9534345865bSWeidong Wang 	return ret;
9544345865bSWeidong Wang }
9554345865bSWeidong Wang 
aw_dev_load_cfg_by_hdr_v1(struct aw_device * aw_dev,struct aw_container * aw_cfg)9564345865bSWeidong Wang static int aw_dev_load_cfg_by_hdr_v1(struct aw_device *aw_dev,
9574345865bSWeidong Wang 						struct aw_container *aw_cfg)
9584345865bSWeidong Wang {
9594345865bSWeidong Wang 	struct aw_cfg_hdr *cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
9604345865bSWeidong Wang 	struct aw_prof_info *prof_info = &aw_dev->prof_info;
9614345865bSWeidong Wang 	int ret;
9624345865bSWeidong Wang 
9634345865bSWeidong Wang 	ret = aw_dev_parse_scene_count_v1(aw_dev, aw_cfg, &prof_info->count);
9644345865bSWeidong Wang 	if (ret < 0) {
9654345865bSWeidong Wang 		dev_err(aw_dev->dev, "get scene count failed");
9664345865bSWeidong Wang 		return ret;
9674345865bSWeidong Wang 	}
9684345865bSWeidong Wang 
9694345865bSWeidong Wang 	prof_info->prof_desc = devm_kcalloc(aw_dev->dev,
9704345865bSWeidong Wang 					prof_info->count, sizeof(struct aw_prof_desc),
9714345865bSWeidong Wang 					GFP_KERNEL);
9724345865bSWeidong Wang 	if (!prof_info->prof_desc)
9734345865bSWeidong Wang 		return -ENOMEM;
9744345865bSWeidong Wang 
9754345865bSWeidong Wang 	ret = aw_dev_parse_by_hdr_v1(aw_dev, cfg_hdr);
9764345865bSWeidong Wang 	if (ret < 0) {
9774345865bSWeidong Wang 		dev_err(aw_dev->dev, "parse hdr failed");
9784345865bSWeidong Wang 		return ret;
9794345865bSWeidong Wang 	}
9804345865bSWeidong Wang 
9814345865bSWeidong Wang 	ret = aw_dev_create_prof_name_list_v1(aw_dev);
9824345865bSWeidong Wang 	if (ret < 0) {
9834345865bSWeidong Wang 		dev_err(aw_dev->dev, "create prof name list failed");
9844345865bSWeidong Wang 		return ret;
9854345865bSWeidong Wang 	}
9864345865bSWeidong Wang 
9874345865bSWeidong Wang 	return 0;
9884345865bSWeidong Wang }
9894345865bSWeidong Wang 
aw88395_dev_cfg_load(struct aw_device * aw_dev,struct aw_container * aw_cfg)9904345865bSWeidong Wang int aw88395_dev_cfg_load(struct aw_device *aw_dev, struct aw_container *aw_cfg)
9914345865bSWeidong Wang {
9924345865bSWeidong Wang 	struct aw_cfg_hdr *cfg_hdr;
9934345865bSWeidong Wang 	int ret;
9944345865bSWeidong Wang 
9954345865bSWeidong Wang 	cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
9964345865bSWeidong Wang 
9974345865bSWeidong Wang 	switch (cfg_hdr->hdr_version) {
9984345865bSWeidong Wang 	case AW88395_CFG_HDR_VER:
9994345865bSWeidong Wang 		ret = aw_dev_load_cfg_by_hdr(aw_dev, cfg_hdr);
10004345865bSWeidong Wang 		if (ret < 0) {
10012a096315SColin Ian King 			dev_err(aw_dev->dev, "hdr_version[0x%x] parse failed",
10024345865bSWeidong Wang 						cfg_hdr->hdr_version);
10034345865bSWeidong Wang 			return ret;
10044345865bSWeidong Wang 		}
10054345865bSWeidong Wang 		break;
10064345865bSWeidong Wang 	case AW88395_CFG_HDR_VER_V1:
10074345865bSWeidong Wang 		ret = aw_dev_load_cfg_by_hdr_v1(aw_dev, aw_cfg);
10084345865bSWeidong Wang 		if (ret < 0) {
10092a096315SColin Ian King 			dev_err(aw_dev->dev, "hdr_version[0x%x] parse failed",
10104345865bSWeidong Wang 						cfg_hdr->hdr_version);
10114345865bSWeidong Wang 			return ret;
10124345865bSWeidong Wang 		}
10134345865bSWeidong Wang 		break;
10144345865bSWeidong Wang 	default:
10154345865bSWeidong Wang 		dev_err(aw_dev->dev, "unsupported hdr_version [0x%x]", cfg_hdr->hdr_version);
10164345865bSWeidong Wang 		return -EINVAL;
10174345865bSWeidong Wang 	}
10184345865bSWeidong Wang 	aw_dev->fw_status = AW88395_DEV_FW_OK;
10194345865bSWeidong Wang 
10204345865bSWeidong Wang 	return 0;
10214345865bSWeidong Wang }
10224345865bSWeidong Wang EXPORT_SYMBOL_GPL(aw88395_dev_cfg_load);
10234345865bSWeidong Wang 
aw_dev_check_cfg_by_hdr(struct aw_device * aw_dev,struct aw_container * aw_cfg)10244345865bSWeidong Wang static int aw_dev_check_cfg_by_hdr(struct aw_device *aw_dev, struct aw_container *aw_cfg)
10254345865bSWeidong Wang {
10264345865bSWeidong Wang 	unsigned int end_data_offset;
10274345865bSWeidong Wang 	struct aw_cfg_hdr *cfg_hdr;
10284345865bSWeidong Wang 	struct aw_cfg_dde *cfg_dde;
10294345865bSWeidong Wang 	unsigned int act_data = 0;
10304345865bSWeidong Wang 	unsigned int hdr_ddt_len;
10314345865bSWeidong Wang 	unsigned int i;
10324345865bSWeidong Wang 	u8 act_crc8;
10334345865bSWeidong Wang 
10344345865bSWeidong Wang 	cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
10354345865bSWeidong Wang 	/* check file type id is awinic acf file */
10364345865bSWeidong Wang 	if (cfg_hdr->id != ACF_FILE_ID) {
10374345865bSWeidong Wang 		dev_err(aw_dev->dev, "not acf type file");
10384345865bSWeidong Wang 		return -EINVAL;
10394345865bSWeidong Wang 	}
10404345865bSWeidong Wang 
10414345865bSWeidong Wang 	hdr_ddt_len = cfg_hdr->hdr_offset + cfg_hdr->ddt_size;
10424345865bSWeidong Wang 	if (hdr_ddt_len > aw_cfg->len) {
104314d5d454SWeidong Wang 		dev_err(aw_dev->dev, "hdr_len with ddt_len [%d] overflow file size[%d]",
10444345865bSWeidong Wang 		cfg_hdr->hdr_offset, aw_cfg->len);
10454345865bSWeidong Wang 		return -EINVAL;
10464345865bSWeidong Wang 	}
10474345865bSWeidong Wang 
10484345865bSWeidong Wang 	/* check data size */
10494345865bSWeidong Wang 	cfg_dde = (struct aw_cfg_dde *)((char *)aw_cfg->data + cfg_hdr->hdr_offset);
10504345865bSWeidong Wang 	act_data += hdr_ddt_len;
10514345865bSWeidong Wang 	for (i = 0; i < cfg_hdr->ddt_num; i++)
10524345865bSWeidong Wang 		act_data += cfg_dde[i].data_size;
10534345865bSWeidong Wang 
10544345865bSWeidong Wang 	if (act_data != aw_cfg->len) {
10554345865bSWeidong Wang 		dev_err(aw_dev->dev, "act_data[%d] not equal to file size[%d]!",
10564345865bSWeidong Wang 			act_data, aw_cfg->len);
10574345865bSWeidong Wang 		return -EINVAL;
10584345865bSWeidong Wang 	}
10594345865bSWeidong Wang 
10604345865bSWeidong Wang 	for (i = 0; i < cfg_hdr->ddt_num; i++) {
10614345865bSWeidong Wang 		/* data check */
10624345865bSWeidong Wang 		end_data_offset = cfg_dde[i].data_offset + cfg_dde[i].data_size;
10634345865bSWeidong Wang 		if (end_data_offset > aw_cfg->len) {
10644345865bSWeidong Wang 			dev_err(aw_dev->dev, "ddt_num[%d] end_data_offset[%d] overflow size[%d]",
10654345865bSWeidong Wang 				i, end_data_offset, aw_cfg->len);
10664345865bSWeidong Wang 			return -EINVAL;
10674345865bSWeidong Wang 		}
10684345865bSWeidong Wang 
10694345865bSWeidong Wang 		/* crc check */
10704345865bSWeidong Wang 		act_crc8 = crc8(aw_crc8_table, aw_cfg->data + cfg_dde[i].data_offset,
10714345865bSWeidong Wang 							cfg_dde[i].data_size, 0);
10724345865bSWeidong Wang 		if (act_crc8 != cfg_dde[i].data_crc) {
10734345865bSWeidong Wang 			dev_err(aw_dev->dev, "ddt_num[%d] act_crc8:0x%x != data_crc:0x%x",
10744345865bSWeidong Wang 				i, (u32)act_crc8, cfg_dde[i].data_crc);
10754345865bSWeidong Wang 			return -EINVAL;
10764345865bSWeidong Wang 		}
10774345865bSWeidong Wang 	}
10784345865bSWeidong Wang 
10794345865bSWeidong Wang 	return 0;
10804345865bSWeidong Wang }
10814345865bSWeidong Wang 
aw_dev_check_acf_by_hdr_v1(struct aw_device * aw_dev,struct aw_container * aw_cfg)10824345865bSWeidong Wang static int aw_dev_check_acf_by_hdr_v1(struct aw_device *aw_dev, struct aw_container *aw_cfg)
10834345865bSWeidong Wang {
10844345865bSWeidong Wang 	struct aw_cfg_dde_v1 *cfg_dde;
10854345865bSWeidong Wang 	unsigned int end_data_offset;
10864345865bSWeidong Wang 	struct aw_cfg_hdr *cfg_hdr;
10874345865bSWeidong Wang 	unsigned int act_data = 0;
10884345865bSWeidong Wang 	unsigned int hdr_ddt_len;
10894345865bSWeidong Wang 	u8 act_crc8;
10904345865bSWeidong Wang 	int i;
10914345865bSWeidong Wang 
10924345865bSWeidong Wang 	cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
10934345865bSWeidong Wang 
10944345865bSWeidong Wang 	/* check file type id is awinic acf file */
10954345865bSWeidong Wang 	if (cfg_hdr->id != ACF_FILE_ID) {
10964345865bSWeidong Wang 		dev_err(aw_dev->dev, "not acf type file");
10974345865bSWeidong Wang 		return -EINVAL;
10984345865bSWeidong Wang 	}
10994345865bSWeidong Wang 
11004345865bSWeidong Wang 	hdr_ddt_len = cfg_hdr->hdr_offset + cfg_hdr->ddt_size;
11014345865bSWeidong Wang 	if (hdr_ddt_len > aw_cfg->len) {
11024345865bSWeidong Wang 		dev_err(aw_dev->dev, "hdrlen with ddt_len [%d] overflow file size[%d]",
11034345865bSWeidong Wang 		cfg_hdr->hdr_offset, aw_cfg->len);
11044345865bSWeidong Wang 		return -EINVAL;
11054345865bSWeidong Wang 	}
11064345865bSWeidong Wang 
11074345865bSWeidong Wang 	/* check data size */
11084345865bSWeidong Wang 	cfg_dde = (struct aw_cfg_dde_v1 *)((char *)aw_cfg->data + cfg_hdr->hdr_offset);
11094345865bSWeidong Wang 	act_data += hdr_ddt_len;
11104345865bSWeidong Wang 	for (i = 0; i < cfg_hdr->ddt_num; i++)
11114345865bSWeidong Wang 		act_data += cfg_dde[i].data_size;
11124345865bSWeidong Wang 
11134345865bSWeidong Wang 	if (act_data != aw_cfg->len) {
11144345865bSWeidong Wang 		dev_err(aw_dev->dev, "act_data[%d] not equal to file size[%d]!",
11154345865bSWeidong Wang 			act_data, aw_cfg->len);
11164345865bSWeidong Wang 		return -EINVAL;
11174345865bSWeidong Wang 	}
11184345865bSWeidong Wang 
11194345865bSWeidong Wang 	for (i = 0; i < cfg_hdr->ddt_num; i++) {
11204345865bSWeidong Wang 		/* data check */
11214345865bSWeidong Wang 		end_data_offset = cfg_dde[i].data_offset + cfg_dde[i].data_size;
11224345865bSWeidong Wang 		if (end_data_offset > aw_cfg->len) {
11234345865bSWeidong Wang 			dev_err(aw_dev->dev, "ddt_num[%d] end_data_offset[%d] overflow size[%d]",
11244345865bSWeidong Wang 				i, end_data_offset, aw_cfg->len);
11254345865bSWeidong Wang 			return -EINVAL;
11264345865bSWeidong Wang 		}
11274345865bSWeidong Wang 
11284345865bSWeidong Wang 		/* crc check */
11294345865bSWeidong Wang 		act_crc8 = crc8(aw_crc8_table, aw_cfg->data + cfg_dde[i].data_offset,
11304345865bSWeidong Wang 									cfg_dde[i].data_size, 0);
11314345865bSWeidong Wang 		if (act_crc8 != cfg_dde[i].data_crc) {
11324345865bSWeidong Wang 			dev_err(aw_dev->dev, "ddt_num[%d] act_crc8:0x%x != data_crc 0x%x",
11334345865bSWeidong Wang 				i, (u32)act_crc8, cfg_dde[i].data_crc);
11344345865bSWeidong Wang 			return -EINVAL;
11354345865bSWeidong Wang 		}
11364345865bSWeidong Wang 	}
11374345865bSWeidong Wang 
11384345865bSWeidong Wang 	return 0;
11394345865bSWeidong Wang }
11404345865bSWeidong Wang 
aw88395_dev_load_acf_check(struct aw_device * aw_dev,struct aw_container * aw_cfg)11414345865bSWeidong Wang int aw88395_dev_load_acf_check(struct aw_device *aw_dev, struct aw_container *aw_cfg)
11424345865bSWeidong Wang {
11434345865bSWeidong Wang 	struct aw_cfg_hdr *cfg_hdr;
11444345865bSWeidong Wang 
11454345865bSWeidong Wang 	if (!aw_cfg) {
11464345865bSWeidong Wang 		dev_err(aw_dev->dev, "aw_prof is NULL");
11474345865bSWeidong Wang 		return -EINVAL;
11484345865bSWeidong Wang 	}
11494345865bSWeidong Wang 
11504345865bSWeidong Wang 	if (aw_cfg->len < sizeof(struct aw_cfg_hdr)) {
11514345865bSWeidong Wang 		dev_err(aw_dev->dev, "cfg hdr size[%d] overflow file size[%d]",
11524345865bSWeidong Wang 			aw_cfg->len, (int)sizeof(struct aw_cfg_hdr));
11534345865bSWeidong Wang 		return -EINVAL;
11544345865bSWeidong Wang 	}
11554345865bSWeidong Wang 
11564345865bSWeidong Wang 	crc8_populate_lsb(aw_crc8_table, AW88395_CRC8_POLYNOMIAL);
11574345865bSWeidong Wang 
11584345865bSWeidong Wang 	cfg_hdr = (struct aw_cfg_hdr *)aw_cfg->data;
11594345865bSWeidong Wang 	switch (cfg_hdr->hdr_version) {
11604345865bSWeidong Wang 	case AW88395_CFG_HDR_VER:
11614345865bSWeidong Wang 		return aw_dev_check_cfg_by_hdr(aw_dev, aw_cfg);
11624345865bSWeidong Wang 	case AW88395_CFG_HDR_VER_V1:
11634345865bSWeidong Wang 		return aw_dev_check_acf_by_hdr_v1(aw_dev, aw_cfg);
11644345865bSWeidong Wang 	default:
11654345865bSWeidong Wang 		dev_err(aw_dev->dev, "unsupported hdr_version [0x%x]", cfg_hdr->hdr_version);
11664345865bSWeidong Wang 		return -EINVAL;
11674345865bSWeidong Wang 	}
11684345865bSWeidong Wang 
11694345865bSWeidong Wang 	return 0;
11704345865bSWeidong Wang }
11714345865bSWeidong Wang EXPORT_SYMBOL_GPL(aw88395_dev_load_acf_check);
11724345865bSWeidong Wang 
11734345865bSWeidong Wang MODULE_DESCRIPTION("AW88395 ACF File Parsing Lib");
11744345865bSWeidong Wang MODULE_LICENSE("GPL v2");
1175