xref: /linux/sound/soc/codecs/aw88166.c (revision 67f8bc848ee31831336bd478e57d2f993551902e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // aw88166.c --  ALSA SoC AW88166 codec support
4 //
5 // Copyright (c) 2025 AWINIC Technology CO., LTD
6 //
7 // Author: Weidong Wang <wangweidong.a@awinic.com>
8 //
9 
10 #include <linux/cleanup.h>
11 #include <linux/crc32.h>
12 #include <linux/firmware.h>
13 #include <linux/gpio/consumer.h>
14 #include <linux/i2c.h>
15 #include <linux/minmax.h>
16 #include <linux/regmap.h>
17 #include <sound/soc.h>
18 #include "aw88166.h"
19 #include "aw88395/aw88395_device.h"
20 
21 struct aw88166 {
22 	struct aw_device *aw_pa;
23 	struct mutex lock;
24 	struct gpio_desc *reset_gpio;
25 	struct delayed_work start_work;
26 	struct regmap *regmap;
27 	struct aw_container *aw_cfg;
28 
29 	unsigned int check_val;
30 	unsigned int crc_init_val;
31 	unsigned int vcalb_init_val;
32 	unsigned int re_init_val;
33 	unsigned int dither_st;
34 	bool phase_sync;
35 };
36 
37 static const struct regmap_config aw88166_remap_config = {
38 	.val_bits = 16,
39 	.reg_bits = 8,
40 	.max_register = AW88166_REG_MAX,
41 	.reg_format_endian = REGMAP_ENDIAN_LITTLE,
42 	.val_format_endian = REGMAP_ENDIAN_BIG,
43 };
44 
45 static void aw_dev_pwd(struct aw_device *aw_dev, bool pwd)
46 {
47 	int ret;
48 
49 	if (pwd)
50 		ret = regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG,
51 				~AW88166_PWDN_MASK, AW88166_PWDN_POWER_DOWN_VALUE);
52 	else
53 		ret = regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG,
54 				~AW88166_PWDN_MASK, AW88166_PWDN_WORKING_VALUE);
55 
56 	if (ret)
57 		dev_dbg(aw_dev->dev, "%s failed", __func__);
58 }
59 
60 static void aw_dev_get_int_status(struct aw_device *aw_dev, unsigned short *int_status)
61 {
62 	unsigned int reg_val;
63 	int ret;
64 
65 	ret = regmap_read(aw_dev->regmap, AW88166_SYSINT_REG, &reg_val);
66 	if (ret)
67 		dev_err(aw_dev->dev, "read interrupt reg fail, ret=%d", ret);
68 	else
69 		*int_status = reg_val;
70 
71 	dev_dbg(aw_dev->dev, "read interrupt reg=0x%04x", *int_status);
72 }
73 
74 static void aw_dev_clear_int_status(struct aw_device *aw_dev)
75 {
76 	u16 int_status;
77 
78 	/* read int status and clear */
79 	aw_dev_get_int_status(aw_dev, &int_status);
80 	/* make sure int status is clear */
81 	aw_dev_get_int_status(aw_dev, &int_status);
82 	if (int_status)
83 		dev_dbg(aw_dev->dev, "int status(%d) is not cleaned.\n", int_status);
84 }
85 
86 static int aw_dev_get_iis_status(struct aw_device *aw_dev)
87 {
88 	unsigned int reg_val;
89 	int ret;
90 
91 	ret = regmap_read(aw_dev->regmap, AW88166_SYSST_REG, &reg_val);
92 	if (ret)
93 		return ret;
94 	if ((reg_val & AW88166_BIT_PLL_CHECK) != AW88166_BIT_PLL_CHECK) {
95 		dev_err(aw_dev->dev, "check pll lock fail, reg_val:0x%04x", reg_val);
96 		return -EINVAL;
97 	}
98 
99 	return 0;
100 }
101 
102 static int aw_dev_check_mode1_pll(struct aw_device *aw_dev)
103 {
104 	int ret, i;
105 
106 	for (i = 0; i < AW88166_DEV_SYSST_CHECK_MAX; i++) {
107 		ret = aw_dev_get_iis_status(aw_dev);
108 		if (ret) {
109 			dev_err(aw_dev->dev, "mode1 iis signal check error");
110 			usleep_range(AW88166_2000_US, AW88166_2000_US + 10);
111 		} else {
112 			return 0;
113 		}
114 	}
115 
116 	return -EPERM;
117 }
118 
119 static int aw_dev_check_mode2_pll(struct aw_device *aw_dev)
120 {
121 	unsigned int reg_val;
122 	int ret, i;
123 
124 	ret = regmap_read(aw_dev->regmap, AW88166_PLLCTRL2_REG, &reg_val);
125 	if (ret)
126 		return ret;
127 
128 	reg_val &= (~AW88166_CCO_MUX_MASK);
129 	if (reg_val == AW88166_CCO_MUX_DIVIDED_VALUE) {
130 		dev_dbg(aw_dev->dev, "CCO_MUX is already divider");
131 		return -EPERM;
132 	}
133 
134 	/* change mode2 */
135 	ret = regmap_update_bits(aw_dev->regmap, AW88166_PLLCTRL2_REG,
136 			~AW88166_CCO_MUX_MASK, AW88166_CCO_MUX_DIVIDED_VALUE);
137 	if (ret)
138 		return ret;
139 
140 	for (i = 0; i < AW88166_DEV_SYSST_CHECK_MAX; i++) {
141 		ret = aw_dev_get_iis_status(aw_dev);
142 		if (ret) {
143 			dev_err(aw_dev->dev, "mode2 iis signal check error");
144 			usleep_range(AW88166_2000_US, AW88166_2000_US + 10);
145 		} else {
146 			break;
147 		}
148 	}
149 
150 	/* change mode1 */
151 	regmap_update_bits(aw_dev->regmap, AW88166_PLLCTRL2_REG,
152 			~AW88166_CCO_MUX_MASK, AW88166_CCO_MUX_BYPASS_VALUE);
153 	if (ret == 0) {
154 		usleep_range(AW88166_2000_US, AW88166_2000_US + 10);
155 		for (i = 0; i < AW88166_DEV_SYSST_CHECK_MAX; i++) {
156 			ret = aw_dev_get_iis_status(aw_dev);
157 			if (ret) {
158 				dev_err(aw_dev->dev, "mode2 switch to mode1, iis signal check error");
159 				usleep_range(AW88166_2000_US, AW88166_2000_US + 10);
160 			} else {
161 				break;
162 			}
163 		}
164 	}
165 
166 	return ret;
167 }
168 
169 static int aw_dev_check_syspll(struct aw_device *aw_dev)
170 {
171 	int ret;
172 
173 	ret = aw_dev_check_mode1_pll(aw_dev);
174 	if (ret) {
175 		dev_dbg(aw_dev->dev, "mode1 check iis failed try switch to mode2 check");
176 		ret = aw_dev_check_mode2_pll(aw_dev);
177 		if (ret) {
178 			dev_err(aw_dev->dev, "mode2 check iis failed");
179 			return ret;
180 		}
181 	}
182 
183 	return 0;
184 }
185 
186 static int aw_dev_check_sysst(struct aw_device *aw_dev)
187 {
188 	unsigned int check_val;
189 	unsigned int reg_val;
190 	int ret, i;
191 
192 	ret = regmap_read(aw_dev->regmap, AW88166_PWMCTRL3_REG, &reg_val);
193 	if (ret)
194 		return ret;
195 
196 	if (reg_val & (~AW88166_NOISE_GATE_EN_MASK))
197 		check_val = AW88166_BIT_SYSST_NOSWS_CHECK;
198 	else
199 		check_val = AW88166_BIT_SYSST_SWS_CHECK;
200 
201 	for (i = 0; i < AW88166_DEV_SYSST_CHECK_MAX; i++) {
202 		ret = regmap_read(aw_dev->regmap, AW88166_SYSST_REG, &reg_val);
203 		if (ret)
204 			return ret;
205 
206 		if ((reg_val & (~AW88166_BIT_SYSST_CHECK_MASK) & check_val) != check_val) {
207 			dev_err(aw_dev->dev, "check sysst fail, cnt=%d, reg_val=0x%04x, check:0x%x",
208 				i, reg_val, AW88166_BIT_SYSST_NOSWS_CHECK);
209 			usleep_range(AW88166_2000_US, AW88166_2000_US + 10);
210 		} else {
211 			return 0;
212 		}
213 	}
214 
215 	return -EPERM;
216 }
217 
218 static void aw_dev_amppd(struct aw_device *aw_dev, bool amppd)
219 {
220 	int ret;
221 
222 	if (amppd)
223 		ret = regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG,
224 				~AW88166_AMPPD_MASK, AW88166_AMPPD_POWER_DOWN_VALUE);
225 	else
226 		ret = regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG,
227 				~AW88166_AMPPD_MASK, AW88166_AMPPD_WORKING_VALUE);
228 
229 	if (ret)
230 		dev_dbg(aw_dev->dev, "%s failed", __func__);
231 }
232 
233 static void aw_dev_dsp_enable(struct aw_device *aw_dev, bool is_enable)
234 {
235 	int ret;
236 
237 	if (is_enable)
238 		ret = regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG,
239 					~AW88166_DSPBY_MASK, AW88166_DSPBY_WORKING_VALUE);
240 	else
241 		ret = regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG,
242 					~AW88166_DSPBY_MASK, AW88166_DSPBY_BYPASS_VALUE);
243 
244 	if (ret)
245 		dev_dbg(aw_dev->dev, "%s failed\n", __func__);
246 }
247 
248 static int aw88166_dev_get_icalk(struct aw88166 *aw88166, int16_t *icalk)
249 {
250 	struct aw_device *aw_dev = aw88166->aw_pa;
251 	unsigned int efrm_reg_val, efrl_reg_val;
252 	uint16_t ef_isn_geslp, ef_isn_h5bits;
253 	uint16_t icalk_val;
254 	int ret;
255 
256 	ret = regmap_read(aw_dev->regmap, AW88166_EFRM2_REG, &efrm_reg_val);
257 	if (ret)
258 		return ret;
259 
260 	ef_isn_geslp = (efrm_reg_val & (~AW88166_EF_ISN_GESLP_MASK)) >>
261 						AW88166_EF_ISN_GESLP_SHIFT;
262 
263 	ret = regmap_read(aw_dev->regmap, AW88166_EFRL_REG, &efrl_reg_val);
264 	if (ret)
265 		return ret;
266 
267 	ef_isn_h5bits = (efrl_reg_val & (~AW88166_EF_ISN_H5BITS_MASK)) >>
268 						AW88166_EF_ISN_H5BITS_SHIFT;
269 
270 	if (aw88166->check_val == AW_EF_AND_CHECK)
271 		icalk_val = ef_isn_geslp & (ef_isn_h5bits | AW88166_EF_ISN_H5BITS_SIGN_MASK);
272 	else
273 		icalk_val = ef_isn_geslp | (ef_isn_h5bits & (~AW88166_EF_ISN_H5BITS_SIGN_MASK));
274 
275 	if (icalk_val & (~AW88166_ICALK_SIGN_MASK))
276 		icalk_val = icalk_val | AW88166_ICALK_NEG_MASK;
277 	*icalk = (int16_t)icalk_val;
278 
279 	return 0;
280 }
281 
282 static int aw88166_dev_get_vcalk(struct aw88166 *aw88166, int16_t *vcalk)
283 {
284 	struct aw_device *aw_dev = aw88166->aw_pa;
285 	unsigned int efrm_reg_val, efrl_reg_val;
286 	uint16_t ef_vsn_geslp, ef_vsn_h3bits;
287 	uint16_t vcalk_val;
288 	int ret;
289 
290 	ret = regmap_read(aw_dev->regmap, AW88166_EFRM2_REG, &efrm_reg_val);
291 	if (ret)
292 		return ret;
293 
294 	ef_vsn_geslp = (efrm_reg_val & (~AW88166_EF_VSN_GESLP_MASK)) >>
295 					AW88166_EF_VSN_GESLP_SHIFT;
296 
297 	ret = regmap_read(aw_dev->regmap, AW88166_EFRL_REG, &efrl_reg_val);
298 	if (ret)
299 		return ret;
300 
301 	ef_vsn_h3bits = (efrl_reg_val & (~AW88166_EF_VSN_H3BITS_MASK)) >>
302 					AW88166_EF_VSN_H3BITS_SHIFT;
303 
304 	if (aw88166->check_val == AW_EF_AND_CHECK)
305 		vcalk_val = ef_vsn_geslp & (ef_vsn_h3bits | AW88166_EF_VSN_H3BITS_SIGN_MASK);
306 	else
307 		vcalk_val = ef_vsn_geslp | (ef_vsn_h3bits & (~AW88166_EF_VSN_H3BITS_SIGN_MASK));
308 
309 	if (vcalk_val & (~AW88166_VCALK_SIGN_MASK))
310 		vcalk_val = vcalk_val | AW88166_VCALK_NEG_MASK;
311 	*vcalk = (int16_t)vcalk_val;
312 
313 	return 0;
314 }
315 
316 static int aw88166_dev_set_vcalb(struct aw88166 *aw88166)
317 {
318 	struct aw_device *aw_dev = aw88166->aw_pa;
319 	int32_t ical_k, vcal_k, vcalb;
320 	int16_t icalk, vcalk;
321 	unsigned int reg_val;
322 	int ret;
323 
324 	ret = aw88166_dev_get_icalk(aw88166, &icalk);
325 	if (ret) {
326 		dev_err(aw_dev->dev, "get icalk failed\n");
327 		return ret;
328 	}
329 	ical_k = icalk * AW88166_ICABLK_FACTOR + AW88166_CABL_BASE_VALUE;
330 
331 	ret = aw88166_dev_get_vcalk(aw88166, &vcalk);
332 	if (ret) {
333 		dev_err(aw_dev->dev, "get vbcalk failed\n");
334 		return ret;
335 	}
336 	vcal_k = vcalk * AW88166_VCABLK_FACTOR + AW88166_CABL_BASE_VALUE;
337 
338 	vcalb = AW88166_VCALB_ACCURACY * AW88166_VSCAL_FACTOR /
339 			AW88166_ISCAL_FACTOR * ical_k / vcal_k * aw88166->vcalb_init_val;
340 
341 	vcalb = vcalb >> AW88166_VCALB_ADJ_FACTOR;
342 	reg_val = (uint32_t)vcalb;
343 
344 	regmap_write(aw_dev->regmap, AW88166_DSPVCALB_REG, reg_val);
345 
346 	return 0;
347 }
348 
349 static int aw_dev_init_vcalb_update(struct aw88166 *aw88166, int flag)
350 {
351 	struct aw_device *aw_dev = aw88166->aw_pa;
352 	int ret;
353 
354 	switch (flag) {
355 	case AW88166_RECOVERY_SEC_DATA:
356 		ret = regmap_write(aw_dev->regmap, AW88166_DSPVCALB_REG, aw88166->vcalb_init_val);
357 		break;
358 	case AW88166_RECORD_SEC_DATA:
359 		ret = regmap_read(aw_dev->regmap, AW88166_DSPVCALB_REG, &aw88166->vcalb_init_val);
360 		break;
361 	default:
362 		dev_err(aw_dev->dev, "unsupported type:%d\n", flag);
363 		ret = -EINVAL;
364 		break;
365 	}
366 
367 	return ret;
368 }
369 
370 static int aw_dev_init_re_update(struct aw88166 *aw88166, int flag)
371 {
372 	struct aw_device *aw_dev = aw88166->aw_pa;
373 	unsigned int re_temp_h, re_temp_l;
374 	int ret;
375 
376 	switch (flag) {
377 	case AW88166_RECOVERY_SEC_DATA:
378 		ret = regmap_write(aw_dev->regmap, AW88166_ACR1_REG, aw88166->re_init_val >> 16);
379 		if (ret)
380 			return ret;
381 		ret = regmap_write(aw_dev->regmap, AW88166_ACR2_REG,
382 						(uint16_t)aw88166->re_init_val);
383 		if (ret)
384 			return ret;
385 		break;
386 	case AW88166_RECORD_SEC_DATA:
387 		ret = regmap_read(aw_dev->regmap, AW88166_ACR1_REG, &re_temp_h);
388 		if (ret)
389 			return ret;
390 		ret = regmap_read(aw_dev->regmap, AW88166_ACR2_REG, &re_temp_l);
391 		if (ret)
392 			return ret;
393 		aw88166->re_init_val = (re_temp_h << 16) + re_temp_l;
394 		break;
395 	default:
396 		dev_err(aw_dev->dev, "unsupported type:%d\n", flag);
397 		ret = -EINVAL;
398 		break;
399 	}
400 
401 	return ret;
402 }
403 
404 static void aw_dev_backup_sec_record(struct aw88166 *aw88166)
405 {
406 	aw_dev_init_vcalb_update(aw88166, AW88166_RECORD_SEC_DATA);
407 	aw_dev_init_re_update(aw88166, AW88166_RECOVERY_SEC_DATA);
408 }
409 
410 static void aw_dev_backup_sec_recovery(struct aw88166 *aw88166)
411 {
412 	aw_dev_init_vcalb_update(aw88166, AW88166_RECOVERY_SEC_DATA);
413 	aw_dev_init_re_update(aw88166, AW88166_RECOVERY_SEC_DATA);
414 }
415 
416 static int aw_dev_update_cali_re(struct aw_cali_desc *cali_desc)
417 {
418 	struct aw_device *aw_dev =
419 		container_of(cali_desc, struct aw_device, cali_desc);
420 	uint16_t re_lbits, re_hbits;
421 	u32 cali_re;
422 	int ret;
423 
424 	if ((aw_dev->cali_desc.cali_re >= AW88166_CALI_RE_MAX) ||
425 			(aw_dev->cali_desc.cali_re <= AW88166_CALI_RE_MIN))
426 		return -EINVAL;
427 
428 	cali_re = AW88166_SHOW_RE_TO_DSP_RE((aw_dev->cali_desc.cali_re +
429 				aw_dev->cali_desc.ra), AW88166_DSP_RE_SHIFT);
430 
431 	re_hbits = (cali_re & (~AW88166_CALI_RE_HBITS_MASK)) >> AW88166_CALI_RE_HBITS_SHIFT;
432 	re_lbits = (cali_re & (~AW88166_CALI_RE_LBITS_MASK)) >> AW88166_CALI_RE_LBITS_SHIFT;
433 
434 	ret = regmap_write(aw_dev->regmap, AW88166_ACR1_REG, re_hbits);
435 	if (ret) {
436 		dev_err(aw_dev->dev, "set cali re error");
437 		return ret;
438 	}
439 
440 	ret = regmap_write(aw_dev->regmap, AW88166_ACR2_REG, re_lbits);
441 	if (ret)
442 		dev_err(aw_dev->dev, "set cali re error");
443 
444 	return ret;
445 }
446 
447 static int aw_dev_fw_crc_check(struct aw_device *aw_dev)
448 {
449 	uint16_t check_val, fw_len_val;
450 	unsigned int reg_val;
451 	int ret;
452 
453 	/* calculate fw_end_addr */
454 	fw_len_val = ((aw_dev->dsp_fw_len / AW_FW_ADDR_LEN) - 1) + AW88166_CRC_FW_BASE_ADDR;
455 
456 	/* write fw_end_addr to crc_end_addr */
457 	ret = regmap_update_bits(aw_dev->regmap, AW88166_CRCCTRL_REG,
458 					~AW88166_CRC_END_ADDR_MASK, fw_len_val);
459 	if (ret)
460 		return ret;
461 	/* enable fw crc check */
462 	ret = regmap_update_bits(aw_dev->regmap, AW88166_CRCCTRL_REG,
463 		~AW88166_CRC_CODE_EN_MASK, AW88166_CRC_CODE_EN_ENABLE_VALUE);
464 
465 	usleep_range(AW88166_2000_US, AW88166_2000_US + 10);
466 
467 	/* read crc check result */
468 	regmap_read(aw_dev->regmap, AW88166_HAGCST_REG, &reg_val);
469 	if (ret)
470 		return ret;
471 
472 	check_val = (reg_val & (~AW88166_CRC_CHECK_BITS_MASK)) >> AW88166_CRC_CHECK_START_BIT;
473 
474 	/* disable fw crc check */
475 	ret = regmap_update_bits(aw_dev->regmap, AW88166_CRCCTRL_REG,
476 		~AW88166_CRC_CODE_EN_MASK, AW88166_CRC_CODE_EN_DISABLE_VALUE);
477 	if (ret)
478 		return ret;
479 
480 	if (check_val != AW88166_CRC_CHECK_PASS_VAL) {
481 		dev_err(aw_dev->dev, "%s failed, check_val 0x%x != 0x%x\n",
482 				__func__, check_val, AW88166_CRC_CHECK_PASS_VAL);
483 		ret = -EINVAL;
484 	}
485 
486 	return ret;
487 }
488 
489 static int aw_dev_cfg_crc_check(struct aw_device *aw_dev)
490 {
491 	uint16_t check_val, cfg_len_val;
492 	unsigned int reg_val;
493 	int ret;
494 
495 	/* calculate cfg end addr */
496 	cfg_len_val = ((aw_dev->dsp_cfg_len / AW_FW_ADDR_LEN) - 1) + AW88166_CRC_CFG_BASE_ADDR;
497 
498 	/* write cfg_end_addr to crc_end_addr */
499 	ret = regmap_update_bits(aw_dev->regmap, AW88166_CRCCTRL_REG,
500 				~AW88166_CRC_END_ADDR_MASK, cfg_len_val);
501 	if (ret)
502 		return ret;
503 
504 	/* enable cfg crc check */
505 	ret = regmap_update_bits(aw_dev->regmap, AW88166_CRCCTRL_REG,
506 			~AW88166_CRC_CFG_EN_MASK, AW88166_CRC_CFG_EN_ENABLE_VALUE);
507 	if (ret)
508 		return ret;
509 
510 	usleep_range(AW88166_1000_US, AW88166_1000_US + 10);
511 
512 	/* read crc check result */
513 	ret = regmap_read(aw_dev->regmap, AW88166_HAGCST_REG, &reg_val);
514 	if (ret)
515 		return ret;
516 
517 	check_val = (reg_val & (~AW88166_CRC_CHECK_BITS_MASK)) >> AW88166_CRC_CHECK_START_BIT;
518 
519 	/* disable cfg crc check */
520 	ret = regmap_update_bits(aw_dev->regmap, AW88166_CRCCTRL_REG,
521 			~AW88166_CRC_CFG_EN_MASK, AW88166_CRC_CFG_EN_DISABLE_VALUE);
522 	if (ret)
523 		return ret;
524 
525 	if (check_val != AW88166_CRC_CHECK_PASS_VAL) {
526 		dev_err(aw_dev->dev, "crc_check failed, check val 0x%x != 0x%x\n",
527 						check_val, AW88166_CRC_CHECK_PASS_VAL);
528 		ret = -EINVAL;
529 	}
530 
531 	return ret;
532 }
533 
534 static int aw_dev_hw_crc_check(struct aw88166 *aw88166)
535 {
536 	struct aw_device *aw_dev = aw88166->aw_pa;
537 	int ret;
538 
539 	ret = regmap_update_bits(aw_dev->regmap, AW88166_I2SCFG1_REG,
540 		~AW88166_RAM_CG_BYP_MASK, AW88166_RAM_CG_BYP_BYPASS_VALUE);
541 	if (ret)
542 		return ret;
543 
544 	ret = aw_dev_fw_crc_check(aw_dev);
545 	if (ret) {
546 		dev_err(aw_dev->dev, "fw_crc_check failed\n");
547 		goto crc_check_failed;
548 	}
549 
550 	ret = aw_dev_cfg_crc_check(aw_dev);
551 	if (ret) {
552 		dev_err(aw_dev->dev, "cfg_crc_check failed\n");
553 		goto crc_check_failed;
554 	}
555 
556 	ret = regmap_write(aw_dev->regmap, AW88166_CRCCTRL_REG, aw88166->crc_init_val);
557 	if (ret)
558 		return ret;
559 
560 	ret = regmap_update_bits(aw_dev->regmap, AW88166_I2SCFG1_REG,
561 		~AW88166_RAM_CG_BYP_MASK, AW88166_RAM_CG_BYP_WORK_VALUE);
562 
563 	return ret;
564 
565 crc_check_failed:
566 	regmap_update_bits(aw_dev->regmap, AW88166_I2SCFG1_REG,
567 		~AW88166_RAM_CG_BYP_MASK, AW88166_RAM_CG_BYP_WORK_VALUE);
568 	return ret;
569 }
570 
571 static void aw_dev_i2s_tx_enable(struct aw_device *aw_dev, bool flag)
572 {
573 	int ret;
574 
575 	if (flag)
576 		ret = regmap_update_bits(aw_dev->regmap, AW88166_I2SCTRL3_REG,
577 			~AW88166_I2STXEN_MASK, AW88166_I2STXEN_ENABLE_VALUE);
578 	else
579 		ret = regmap_update_bits(aw_dev->regmap, AW88166_I2SCTRL3_REG,
580 			~AW88166_I2STXEN_MASK, AW88166_I2STXEN_DISABLE_VALUE);
581 
582 	if (ret)
583 		dev_dbg(aw_dev->dev, "%s failed", __func__);
584 }
585 
586 static int aw_dev_get_dsp_status(struct aw_device *aw_dev)
587 {
588 	unsigned int reg_val;
589 	int ret;
590 
591 	ret = regmap_read(aw_dev->regmap, AW88166_WDT_REG, &reg_val);
592 	if (ret)
593 		return ret;
594 	if (!(reg_val & (~AW88166_WDT_CNT_MASK)))
595 		return -EPERM;
596 
597 	return 0;
598 }
599 
600 static int aw_dev_dsp_check(struct aw_device *aw_dev)
601 {
602 	int ret, i;
603 
604 	switch (aw_dev->dsp_cfg) {
605 	case AW88166_DEV_DSP_BYPASS:
606 		dev_dbg(aw_dev->dev, "dsp bypass");
607 		ret = 0;
608 		break;
609 	case AW88166_DEV_DSP_WORK:
610 		aw_dev_dsp_enable(aw_dev, false);
611 		aw_dev_dsp_enable(aw_dev, true);
612 		usleep_range(AW88166_1000_US, AW88166_1000_US + 10);
613 		for (i = 0; i < AW88166_DEV_DSP_CHECK_MAX; i++) {
614 			ret = aw_dev_get_dsp_status(aw_dev);
615 			if (ret) {
616 				dev_err(aw_dev->dev, "dsp wdt status error=%d", ret);
617 				usleep_range(AW88166_2000_US, AW88166_2000_US + 10);
618 			}
619 		}
620 		break;
621 	default:
622 		dev_err(aw_dev->dev, "unknown dsp cfg=%d", aw_dev->dsp_cfg);
623 		ret = -EINVAL;
624 		break;
625 	}
626 
627 	return ret;
628 }
629 
630 static int aw_dev_set_volume(struct aw_device *aw_dev, unsigned int value)
631 {
632 	struct aw_volume_desc *vol_desc = &aw_dev->volume_desc;
633 	unsigned int reg_value;
634 	u16 real_value;
635 	int ret;
636 
637 	real_value = min((value + vol_desc->init_volume), (unsigned int)AW88166_MUTE_VOL);
638 
639 	ret = regmap_read(aw_dev->regmap, AW88166_SYSCTRL2_REG, &reg_value);
640 	if (ret)
641 		return ret;
642 
643 	dev_dbg(aw_dev->dev, "value 0x%x , reg:0x%x", value, real_value);
644 
645 	real_value = (real_value << AW88166_VOL_START_BIT) | (reg_value & AW88166_VOL_MASK);
646 
647 	ret = regmap_write(aw_dev->regmap, AW88166_SYSCTRL2_REG, real_value);
648 
649 	return ret;
650 }
651 
652 static void aw_dev_fade_in(struct aw_device *aw_dev)
653 {
654 	struct aw_volume_desc *desc = &aw_dev->volume_desc;
655 	u16 fade_in_vol = desc->ctl_volume;
656 	int fade_step = aw_dev->fade_step;
657 	int i;
658 
659 	if (fade_step == 0 || aw_dev->fade_in_time == 0) {
660 		aw_dev_set_volume(aw_dev, fade_in_vol);
661 		return;
662 	}
663 
664 	for (i = AW88166_MUTE_VOL; i >= fade_in_vol; i -= fade_step) {
665 		aw_dev_set_volume(aw_dev, i);
666 		usleep_range(aw_dev->fade_in_time, aw_dev->fade_in_time + 10);
667 	}
668 
669 	if (i != fade_in_vol)
670 		aw_dev_set_volume(aw_dev, fade_in_vol);
671 }
672 
673 static void aw_dev_fade_out(struct aw_device *aw_dev)
674 {
675 	struct aw_volume_desc *desc = &aw_dev->volume_desc;
676 	int fade_step = aw_dev->fade_step;
677 	int i;
678 
679 	if (fade_step == 0 || aw_dev->fade_out_time == 0) {
680 		aw_dev_set_volume(aw_dev, AW88166_MUTE_VOL);
681 		return;
682 	}
683 
684 	for (i = desc->ctl_volume; i <= AW88166_MUTE_VOL; i += fade_step) {
685 		aw_dev_set_volume(aw_dev, i);
686 		usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10);
687 	}
688 
689 	if (i != AW88166_MUTE_VOL) {
690 		aw_dev_set_volume(aw_dev, AW88166_MUTE_VOL);
691 		usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10);
692 	}
693 }
694 
695 static void aw88166_dev_mute(struct aw_device *aw_dev, bool is_mute)
696 {
697 	if (is_mute) {
698 		aw_dev_fade_out(aw_dev);
699 		regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG,
700 				~AW88166_HMUTE_MASK, AW88166_HMUTE_ENABLE_VALUE);
701 	} else {
702 		regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG,
703 				~AW88166_HMUTE_MASK, AW88166_HMUTE_DISABLE_VALUE);
704 		aw_dev_fade_in(aw_dev);
705 	}
706 }
707 
708 static void aw88166_dev_set_dither(struct aw88166 *aw88166, bool dither)
709 {
710 	struct aw_device *aw_dev = aw88166->aw_pa;
711 
712 	if (dither)
713 		regmap_update_bits(aw_dev->regmap, AW88166_DBGCTRL_REG,
714 				~AW88166_DITHER_EN_MASK, AW88166_DITHER_EN_ENABLE_VALUE);
715 	else
716 		regmap_update_bits(aw_dev->regmap, AW88166_DBGCTRL_REG,
717 				~AW88166_DITHER_EN_MASK, AW88166_DITHER_EN_DISABLE_VALUE);
718 }
719 
720 static int aw88166_dev_start(struct aw88166 *aw88166)
721 {
722 	struct aw_device *aw_dev = aw88166->aw_pa;
723 	int ret;
724 
725 	if (aw_dev->status == AW88166_DEV_PW_ON) {
726 		dev_dbg(aw_dev->dev, "already power on");
727 		return 0;
728 	}
729 
730 	aw88166_dev_set_dither(aw88166, false);
731 
732 	/* power on */
733 	aw_dev_pwd(aw_dev, false);
734 	usleep_range(AW88166_2000_US, AW88166_2000_US + 10);
735 
736 	ret = aw_dev_check_syspll(aw_dev);
737 	if (ret) {
738 		dev_err(aw_dev->dev, "pll check failed cannot start\n");
739 		goto pll_check_fail;
740 	}
741 
742 	/* amppd on */
743 	aw_dev_amppd(aw_dev, false);
744 	usleep_range(AW88166_1000_US, AW88166_1000_US + 50);
745 
746 	/* check i2s status */
747 	ret = aw_dev_check_sysst(aw_dev);
748 	if (ret) {
749 		dev_err(aw_dev->dev, "sysst check failed\n");
750 		goto sysst_check_fail;
751 	}
752 
753 	if (aw_dev->dsp_cfg == AW88166_DEV_DSP_WORK) {
754 		aw_dev_backup_sec_recovery(aw88166);
755 		ret = aw_dev_hw_crc_check(aw88166);
756 		if (ret) {
757 			dev_err(aw_dev->dev, "dsp crc check failed\n");
758 			goto crc_check_fail;
759 		}
760 		aw_dev_dsp_enable(aw_dev, false);
761 		aw88166_dev_set_vcalb(aw88166);
762 		aw_dev_update_cali_re(&aw_dev->cali_desc);
763 		ret = aw_dev_dsp_check(aw_dev);
764 		if (ret) {
765 			dev_err(aw_dev->dev, "dsp status check failed\n");
766 			goto dsp_check_fail;
767 		}
768 	} else {
769 		dev_dbg(aw_dev->dev, "start pa with dsp bypass");
770 	}
771 
772 	/* enable tx feedback */
773 	aw_dev_i2s_tx_enable(aw_dev, true);
774 
775 	if (aw88166->dither_st == AW88166_DITHER_EN_ENABLE_VALUE)
776 		aw88166_dev_set_dither(aw88166, true);
777 
778 	/* close mute */
779 	aw88166_dev_mute(aw_dev, false);
780 	/* clear inturrupt */
781 	aw_dev_clear_int_status(aw_dev);
782 	aw_dev->status = AW88166_DEV_PW_ON;
783 
784 	return 0;
785 
786 dsp_check_fail:
787 crc_check_fail:
788 	aw_dev_dsp_enable(aw_dev, false);
789 sysst_check_fail:
790 	aw_dev_clear_int_status(aw_dev);
791 	aw_dev_amppd(aw_dev, true);
792 pll_check_fail:
793 	aw_dev_pwd(aw_dev, true);
794 	aw_dev->status = AW88166_DEV_PW_OFF;
795 
796 	return ret;
797 }
798 
799 static int aw_dev_dsp_update_container(struct aw_device *aw_dev,
800 			unsigned char *data, unsigned int len, unsigned short base)
801 {
802 	u32 tmp_len;
803 	int i, ret;
804 
805 	ret = regmap_write(aw_dev->regmap, AW88166_DSPMADD_REG, base);
806 	if (ret)
807 		return ret;
808 
809 	for (i = 0; i < len; i += AW88166_MAX_RAM_WRITE_BYTE_SIZE) {
810 		tmp_len = min(len - i, AW88166_MAX_RAM_WRITE_BYTE_SIZE);
811 		ret = regmap_raw_write(aw_dev->regmap, AW88166_DSPMDAT_REG,
812 					&data[i], tmp_len);
813 		if (ret)
814 			return ret;
815 	}
816 
817 	return 0;
818 }
819 
820 static int aw_dev_get_ra(struct aw_cali_desc *cali_desc)
821 {
822 	struct aw_device *aw_dev =
823 		container_of(cali_desc, struct aw_device, cali_desc);
824 	u32 dsp_ra;
825 	int ret;
826 
827 	ret = aw_dev_dsp_read(aw_dev, AW88166_DSP_REG_CFG_ADPZ_RA,
828 				&dsp_ra, AW_DSP_32_DATA);
829 	if (ret) {
830 		dev_err(aw_dev->dev, "read ra error\n");
831 		return ret;
832 	}
833 
834 	cali_desc->ra = AW88166_DSP_RE_TO_SHOW_RE(dsp_ra,
835 					AW88166_DSP_RE_SHIFT);
836 
837 	return 0;
838 }
839 
840 static int aw_dev_dsp_update_cfg(struct aw_device *aw_dev,
841 			unsigned char *data, unsigned int len)
842 {
843 	int ret;
844 
845 	dev_dbg(aw_dev->dev, "dsp config len:%d", len);
846 
847 	if (!len || !data) {
848 		dev_err(aw_dev->dev, "dsp config data is null or len is 0\n");
849 		return -EINVAL;
850 	}
851 
852 	ret = aw_dev_dsp_update_container(aw_dev, data, len, AW88166_DSP_CFG_ADDR);
853 	if (ret)
854 		return ret;
855 
856 	aw_dev->dsp_cfg_len = len;
857 
858 	ret = aw_dev_get_ra(&aw_dev->cali_desc);
859 
860 	return ret;
861 }
862 
863 static int aw_dev_dsp_update_fw(struct aw_device *aw_dev,
864 			unsigned char *data, unsigned int len)
865 {
866 	int ret;
867 
868 	dev_dbg(aw_dev->dev, "dsp firmware len:%d", len);
869 
870 	if (!len || !data) {
871 		dev_err(aw_dev->dev, "dsp firmware data is null or len is 0\n");
872 		return -EINVAL;
873 	}
874 
875 	aw_dev->dsp_fw_len = len;
876 	ret = aw_dev_dsp_update_container(aw_dev, data, len, AW88166_DSP_FW_ADDR);
877 
878 	return ret;
879 }
880 
881 static int aw_dev_check_sram(struct aw_device *aw_dev)
882 {
883 	unsigned int reg_val;
884 
885 	/* read dsp_rom_check_reg */
886 	aw_dev_dsp_read(aw_dev, AW88166_DSP_ROM_CHECK_ADDR, &reg_val, AW_DSP_16_DATA);
887 	if (reg_val != AW88166_DSP_ROM_CHECK_DATA) {
888 		dev_err(aw_dev->dev, "check dsp rom failed, read[0x%x] != check[0x%x]\n",
889 						reg_val, AW88166_DSP_ROM_CHECK_DATA);
890 		return -EPERM;
891 	}
892 
893 	/* check dsp_cfg_base_addr */
894 	aw_dev_dsp_write(aw_dev, AW88166_DSP_CFG_ADDR,
895 				AW88166_DSP_ODD_NUM_BIT_TEST, AW_DSP_16_DATA);
896 	aw_dev_dsp_read(aw_dev, AW88166_DSP_CFG_ADDR, &reg_val, AW_DSP_16_DATA);
897 	if (reg_val != AW88166_DSP_ODD_NUM_BIT_TEST) {
898 		dev_err(aw_dev->dev, "check dsp cfg failed, read[0x%x] != write[0x%x]\n",
899 						reg_val, AW88166_DSP_ODD_NUM_BIT_TEST);
900 		return -EPERM;
901 	}
902 
903 	return 0;
904 }
905 
906 static void aw_dev_select_memclk(struct aw_device *aw_dev, unsigned char flag)
907 {
908 	int ret;
909 
910 	switch (flag) {
911 	case AW88166_DEV_MEMCLK_PLL:
912 		ret = regmap_update_bits(aw_dev->regmap, AW88166_DBGCTRL_REG,
913 					~AW88166_MEM_CLKSEL_MASK,
914 					AW88166_MEM_CLKSEL_DAPHCLK_VALUE);
915 		if (ret)
916 			dev_err(aw_dev->dev, "memclk select pll failed\n");
917 		break;
918 	case AW88166_DEV_MEMCLK_OSC:
919 		ret = regmap_update_bits(aw_dev->regmap, AW88166_DBGCTRL_REG,
920 					~AW88166_MEM_CLKSEL_MASK,
921 					AW88166_MEM_CLKSEL_OSCCLK_VALUE);
922 		if (ret)
923 			dev_err(aw_dev->dev, "memclk select OSC failed\n");
924 		break;
925 	default:
926 		dev_err(aw_dev->dev, "unknown memclk config, flag=0x%x\n", flag);
927 		break;
928 	}
929 }
930 
931 static int aw_dev_update_reg_container(struct aw88166 *aw88166,
932 				unsigned char *data, unsigned int len)
933 {
934 	struct aw_device *aw_dev = aw88166->aw_pa;
935 	struct aw_volume_desc *vol_desc = &aw_dev->volume_desc;
936 	u16 read_vol, reg_val;
937 	int data_len, i, ret;
938 	int16_t *reg_data;
939 	u8 reg_addr;
940 
941 	reg_data = (int16_t *)data;
942 	data_len = len >> 1;
943 
944 	if (data_len & 0x1) {
945 		dev_err(aw_dev->dev, "data len:%d unsupported\n",	data_len);
946 		return -EINVAL;
947 	}
948 
949 	for (i = 0; i < data_len; i += 2) {
950 		reg_addr = reg_data[i];
951 		reg_val = reg_data[i + 1];
952 
953 		if (reg_addr == AW88166_DSPVCALB_REG) {
954 			aw88166->vcalb_init_val = reg_val;
955 			continue;
956 		}
957 
958 		if (reg_addr == AW88166_SYSCTRL_REG) {
959 			if (reg_val & (~AW88166_DSPBY_MASK))
960 				aw_dev->dsp_cfg = AW88166_DEV_DSP_BYPASS;
961 			else
962 				aw_dev->dsp_cfg = AW88166_DEV_DSP_WORK;
963 
964 			reg_val &= (AW88166_HMUTE_MASK | AW88166_PWDN_MASK |
965 						AW88166_DSPBY_MASK);
966 			reg_val |= (AW88166_HMUTE_ENABLE_VALUE | AW88166_PWDN_POWER_DOWN_VALUE |
967 						AW88166_DSPBY_BYPASS_VALUE);
968 		}
969 
970 		if (reg_addr == AW88166_I2SCTRL3_REG) {
971 			reg_val &= AW88166_I2STXEN_MASK;
972 			reg_val |= AW88166_I2STXEN_DISABLE_VALUE;
973 		}
974 
975 		if (reg_addr == AW88166_SYSCTRL2_REG) {
976 			read_vol = (reg_val & (~AW88166_VOL_MASK)) >>
977 				AW88166_VOL_START_BIT;
978 			aw_dev->volume_desc.init_volume = read_vol;
979 		}
980 
981 		if (reg_addr == AW88166_DBGCTRL_REG) {
982 			if ((reg_val & (~AW88166_EF_DBMD_MASK)) == AW88166_EF_DBMD_OR_VALUE)
983 				aw88166->check_val = AW_EF_OR_CHECK;
984 			else
985 				aw88166->check_val = AW_EF_AND_CHECK;
986 
987 			aw88166->dither_st = reg_val & (~AW88166_DITHER_EN_MASK);
988 		}
989 
990 		if (reg_addr == AW88166_ACR1_REG) {
991 			aw88166->re_init_val |= (uint32_t)reg_val << 16;
992 			continue;
993 		}
994 
995 		if (reg_addr == AW88166_ACR2_REG) {
996 			aw88166->re_init_val |= (uint32_t)reg_val;
997 			continue;
998 		}
999 
1000 		if (reg_addr == AW88166_CRCCTRL_REG)
1001 			aw88166->crc_init_val = reg_val;
1002 
1003 		ret = regmap_write(aw_dev->regmap, reg_addr, reg_val);
1004 		if (ret)
1005 			return ret;
1006 	}
1007 
1008 	aw_dev_pwd(aw_dev, false);
1009 	usleep_range(AW88166_1000_US, AW88166_1000_US + 10);
1010 
1011 	if (aw_dev->prof_cur != aw_dev->prof_index)
1012 		vol_desc->ctl_volume = 0;
1013 	else
1014 		aw_dev_set_volume(aw_dev, vol_desc->ctl_volume);
1015 
1016 	return 0;
1017 }
1018 
1019 static int aw_dev_reg_update(struct aw88166 *aw88166,
1020 					unsigned char *data, unsigned int len)
1021 {
1022 	int ret;
1023 
1024 	if (!len || !data) {
1025 		dev_err(aw88166->aw_pa->dev, "reg data is null or len is 0\n");
1026 		return -EINVAL;
1027 	}
1028 
1029 	ret = aw_dev_update_reg_container(aw88166, data, len);
1030 	if (ret)
1031 		dev_err(aw88166->aw_pa->dev, "reg update failed\n");
1032 
1033 	return ret;
1034 }
1035 
1036 static int aw88166_dev_get_prof_name(struct aw_device *aw_dev, int index, char **prof_name)
1037 {
1038 	struct aw_prof_info *prof_info = &aw_dev->prof_info;
1039 	struct aw_prof_desc *prof_desc;
1040 
1041 	if ((index >= aw_dev->prof_info.count) || (index < 0)) {
1042 		dev_err(aw_dev->dev, "index[%d] overflow count[%d]\n",
1043 						index, aw_dev->prof_info.count);
1044 		return -EINVAL;
1045 	}
1046 
1047 	prof_desc = &aw_dev->prof_info.prof_desc[index];
1048 
1049 	*prof_name = prof_info->prof_name_list[prof_desc->id];
1050 
1051 	return 0;
1052 }
1053 
1054 static int aw88166_dev_get_prof_data(struct aw_device *aw_dev, int index,
1055 			struct aw_prof_desc **prof_desc)
1056 {
1057 	if ((index >= aw_dev->prof_info.count) || (index < 0)) {
1058 		dev_err(aw_dev->dev, "%s: index[%d] overflow count[%d]\n",
1059 				__func__, index, aw_dev->prof_info.count);
1060 		return -EINVAL;
1061 	}
1062 
1063 	*prof_desc = &aw_dev->prof_info.prof_desc[index];
1064 
1065 	return 0;
1066 }
1067 
1068 static int aw88166_dev_fw_update(struct aw88166 *aw88166, bool up_dsp_fw_en, bool force_up_en)
1069 {
1070 	struct aw_device *aw_dev = aw88166->aw_pa;
1071 	struct aw_prof_desc *prof_index_desc;
1072 	struct aw_sec_data_desc *sec_desc;
1073 	char *prof_name;
1074 	int ret;
1075 
1076 	if ((aw_dev->prof_cur == aw_dev->prof_index) &&
1077 			(force_up_en == AW88166_FORCE_UPDATE_OFF)) {
1078 		dev_dbg(aw_dev->dev, "scene no change, not update");
1079 		return 0;
1080 	}
1081 
1082 	if (aw_dev->fw_status == AW88166_DEV_FW_FAILED) {
1083 		dev_err(aw_dev->dev, "fw status[%d] error\n", aw_dev->fw_status);
1084 		return -EPERM;
1085 	}
1086 
1087 	ret = aw88166_dev_get_prof_name(aw_dev, aw_dev->prof_index, &prof_name);
1088 	if (ret)
1089 		return ret;
1090 
1091 	dev_dbg(aw_dev->dev, "start update %s", prof_name);
1092 
1093 	ret = aw88166_dev_get_prof_data(aw_dev, aw_dev->prof_index, &prof_index_desc);
1094 	if (ret)
1095 		return ret;
1096 
1097 	/* update reg */
1098 	sec_desc = prof_index_desc->sec_desc;
1099 	ret = aw_dev_reg_update(aw88166, sec_desc[AW88395_DATA_TYPE_REG].data,
1100 					sec_desc[AW88395_DATA_TYPE_REG].len);
1101 	if (ret) {
1102 		dev_err(aw_dev->dev, "update reg failed\n");
1103 		return ret;
1104 	}
1105 
1106 	aw88166_dev_mute(aw_dev, true);
1107 
1108 	if (aw_dev->dsp_cfg == AW88166_DEV_DSP_WORK)
1109 		aw_dev_dsp_enable(aw_dev, false);
1110 
1111 	aw_dev_select_memclk(aw_dev, AW88166_DEV_MEMCLK_OSC);
1112 
1113 	ret = aw_dev_check_sram(aw_dev);
1114 	if (ret) {
1115 		dev_err(aw_dev->dev, "check sram failed\n");
1116 		goto error;
1117 	}
1118 
1119 	aw_dev_backup_sec_recovery(aw88166);
1120 
1121 	if (up_dsp_fw_en) {
1122 		dev_dbg(aw_dev->dev, "fw_ver: [%x]", prof_index_desc->fw_ver);
1123 		ret = aw_dev_dsp_update_fw(aw_dev, sec_desc[AW88395_DATA_TYPE_DSP_FW].data,
1124 					sec_desc[AW88395_DATA_TYPE_DSP_FW].len);
1125 		if (ret) {
1126 			dev_err(aw_dev->dev, "update dsp fw failed\n");
1127 			goto error;
1128 		}
1129 	}
1130 
1131 	/* update dsp config */
1132 	ret = aw_dev_dsp_update_cfg(aw_dev, sec_desc[AW88395_DATA_TYPE_DSP_CFG].data,
1133 					sec_desc[AW88395_DATA_TYPE_DSP_CFG].len);
1134 	if (ret) {
1135 		dev_err(aw_dev->dev, "update dsp cfg failed\n");
1136 		goto error;
1137 	}
1138 
1139 	aw_dev_backup_sec_record(aw88166);
1140 
1141 	aw_dev_select_memclk(aw_dev, AW88166_DEV_MEMCLK_PLL);
1142 
1143 	aw_dev->prof_cur = aw_dev->prof_index;
1144 
1145 	return 0;
1146 
1147 error:
1148 	aw_dev_select_memclk(aw_dev, AW88166_DEV_MEMCLK_PLL);
1149 	return ret;
1150 }
1151 
1152 static void aw88166_start_pa(struct aw88166 *aw88166)
1153 {
1154 	int ret, i;
1155 
1156 	for (i = 0; i < AW88166_START_RETRIES; i++) {
1157 		ret = aw88166_dev_start(aw88166);
1158 		if (ret) {
1159 			dev_err(aw88166->aw_pa->dev, "aw88166 device start failed. retry = %d", i);
1160 			ret = aw88166_dev_fw_update(aw88166, AW88166_DSP_FW_UPDATE_ON, true);
1161 			if (ret) {
1162 				dev_err(aw88166->aw_pa->dev, "fw update failed");
1163 				continue;
1164 			}
1165 		} else {
1166 			dev_dbg(aw88166->aw_pa->dev, "start success\n");
1167 			break;
1168 		}
1169 	}
1170 }
1171 
1172 static void aw88166_startup_work(struct work_struct *work)
1173 {
1174 	struct aw88166 *aw88166 =
1175 		container_of(work, struct aw88166, start_work.work);
1176 
1177 	guard(mutex)(&aw88166->lock);
1178 	aw88166_start_pa(aw88166);
1179 }
1180 
1181 static void aw88166_start(struct aw88166 *aw88166, bool sync_start)
1182 {
1183 	int ret;
1184 
1185 	if (aw88166->aw_pa->fw_status != AW88166_DEV_FW_OK)
1186 		return;
1187 
1188 	if (aw88166->aw_pa->status == AW88166_DEV_PW_ON)
1189 		return;
1190 
1191 	ret = aw88166_dev_fw_update(aw88166, AW88166_DSP_FW_UPDATE_OFF, aw88166->phase_sync);
1192 	if (ret) {
1193 		dev_err(aw88166->aw_pa->dev, "fw update failed\n");
1194 		return;
1195 	}
1196 
1197 	if (sync_start == AW88166_SYNC_START)
1198 		aw88166_start_pa(aw88166);
1199 	else
1200 		queue_delayed_work(system_dfl_wq,
1201 			&aw88166->start_work,
1202 			AW88166_START_WORK_DELAY_MS);
1203 }
1204 
1205 static int aw_dev_check_sysint(struct aw_device *aw_dev)
1206 {
1207 	u16 reg_val;
1208 
1209 	aw_dev_get_int_status(aw_dev, &reg_val);
1210 	if (reg_val & AW88166_BIT_SYSINT_CHECK) {
1211 		dev_err(aw_dev->dev, "pa stop check fail:0x%04x\n", reg_val);
1212 		return -EINVAL;
1213 	}
1214 
1215 	return 0;
1216 }
1217 
1218 static int aw88166_stop(struct aw_device *aw_dev)
1219 {
1220 	struct aw_sec_data_desc *dsp_cfg =
1221 		&aw_dev->prof_info.prof_desc[aw_dev->prof_cur].sec_desc[AW88395_DATA_TYPE_DSP_CFG];
1222 	struct aw_sec_data_desc *dsp_fw =
1223 		&aw_dev->prof_info.prof_desc[aw_dev->prof_cur].sec_desc[AW88395_DATA_TYPE_DSP_FW];
1224 	int int_st;
1225 
1226 	if (aw_dev->status == AW88166_DEV_PW_OFF) {
1227 		dev_dbg(aw_dev->dev, "already power off");
1228 		return 0;
1229 	}
1230 
1231 	aw_dev->status = AW88166_DEV_PW_OFF;
1232 
1233 	aw88166_dev_mute(aw_dev, true);
1234 	usleep_range(AW88166_4000_US, AW88166_4000_US + 100);
1235 
1236 	aw_dev_i2s_tx_enable(aw_dev, false);
1237 	usleep_range(AW88166_1000_US, AW88166_1000_US + 100);
1238 
1239 	int_st = aw_dev_check_sysint(aw_dev);
1240 
1241 	aw_dev_dsp_enable(aw_dev, false);
1242 
1243 	aw_dev_amppd(aw_dev, true);
1244 
1245 	if (int_st) {
1246 		aw_dev_select_memclk(aw_dev, AW88166_DEV_MEMCLK_OSC);
1247 		aw_dev_dsp_update_fw(aw_dev, dsp_fw->data, dsp_fw->len);
1248 		aw_dev_dsp_update_cfg(aw_dev, dsp_cfg->data, dsp_cfg->len);
1249 		aw_dev_select_memclk(aw_dev, AW88166_DEV_MEMCLK_PLL);
1250 	}
1251 
1252 	aw_dev_pwd(aw_dev, true);
1253 
1254 	return 0;
1255 }
1256 
1257 static struct snd_soc_dai_driver aw88166_dai[] = {
1258 	{
1259 		.name = "aw88166-aif",
1260 		.id = 1,
1261 		.playback = {
1262 			.stream_name = "Speaker_Playback",
1263 			.channels_min = 1,
1264 			.channels_max = 2,
1265 			.rates = AW88166_RATES,
1266 			.formats = AW88166_FORMATS,
1267 		},
1268 		.capture = {
1269 			.stream_name = "Speaker_Capture",
1270 			.channels_min = 1,
1271 			.channels_max = 2,
1272 			.rates = AW88166_RATES,
1273 			.formats = AW88166_FORMATS,
1274 		},
1275 	},
1276 };
1277 
1278 static int aw88166_get_fade_in_time(struct snd_kcontrol *kcontrol,
1279 	struct snd_ctl_elem_value *ucontrol)
1280 {
1281 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1282 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(component);
1283 	struct aw_device *aw_dev = aw88166->aw_pa;
1284 
1285 	ucontrol->value.integer.value[0] = aw_dev->fade_in_time;
1286 
1287 	return 0;
1288 }
1289 
1290 static int aw88166_set_fade_in_time(struct snd_kcontrol *kcontrol,
1291 	struct snd_ctl_elem_value *ucontrol)
1292 {
1293 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1294 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(component);
1295 	struct soc_mixer_control *mc =
1296 		(struct soc_mixer_control *)kcontrol->private_value;
1297 	struct aw_device *aw_dev = aw88166->aw_pa;
1298 	int time;
1299 
1300 	time = ucontrol->value.integer.value[0];
1301 
1302 	if (time < mc->min || time > mc->max)
1303 		return -EINVAL;
1304 
1305 	if (time != aw_dev->fade_in_time) {
1306 		aw_dev->fade_in_time = time;
1307 		return 1;
1308 	}
1309 
1310 	return 0;
1311 }
1312 
1313 static int aw88166_get_fade_out_time(struct snd_kcontrol *kcontrol,
1314 	struct snd_ctl_elem_value *ucontrol)
1315 {
1316 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1317 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(component);
1318 	struct aw_device *aw_dev = aw88166->aw_pa;
1319 
1320 	ucontrol->value.integer.value[0] = aw_dev->fade_out_time;
1321 
1322 	return 0;
1323 }
1324 
1325 static int aw88166_set_fade_out_time(struct snd_kcontrol *kcontrol,
1326 	struct snd_ctl_elem_value *ucontrol)
1327 {
1328 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
1329 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(component);
1330 	struct soc_mixer_control *mc =
1331 		(struct soc_mixer_control *)kcontrol->private_value;
1332 	struct aw_device *aw_dev = aw88166->aw_pa;
1333 	int time;
1334 
1335 	time = ucontrol->value.integer.value[0];
1336 	if (time < mc->min || time > mc->max)
1337 		return -EINVAL;
1338 
1339 	if (time != aw_dev->fade_out_time) {
1340 		aw_dev->fade_out_time = time;
1341 		return 1;
1342 	}
1343 
1344 	return 0;
1345 }
1346 
1347 static int aw88166_dev_set_profile_index(struct aw_device *aw_dev, int index)
1348 {
1349 	/* check the index whether is valid */
1350 	if ((index >= aw_dev->prof_info.count) || (index < 0))
1351 		return -EINVAL;
1352 	/* check the index whether change */
1353 	if (aw_dev->prof_index == index)
1354 		return -EINVAL;
1355 
1356 	aw_dev->prof_index = index;
1357 	dev_dbg(aw_dev->dev, "set prof[%s]",
1358 		aw_dev->prof_info.prof_name_list[aw_dev->prof_info.prof_desc[index].id]);
1359 
1360 	return 0;
1361 }
1362 
1363 static int aw88166_profile_info(struct snd_kcontrol *kcontrol,
1364 			 struct snd_ctl_elem_info *uinfo)
1365 {
1366 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1367 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec);
1368 	char *prof_name;
1369 	int count, ret;
1370 
1371 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1372 	uinfo->count = 1;
1373 
1374 	count = aw88166->aw_pa->prof_info.count;
1375 	if (count <= 0) {
1376 		uinfo->value.enumerated.items = 0;
1377 		return 0;
1378 	}
1379 
1380 	uinfo->value.enumerated.items = count;
1381 
1382 	if (uinfo->value.enumerated.item >= count)
1383 		uinfo->value.enumerated.item = count - 1;
1384 
1385 	count = uinfo->value.enumerated.item;
1386 
1387 	ret = aw88166_dev_get_prof_name(aw88166->aw_pa, count, &prof_name);
1388 	if (ret) {
1389 		strscpy(uinfo->value.enumerated.name, "null");
1390 		return 0;
1391 	}
1392 
1393 	strscpy(uinfo->value.enumerated.name, prof_name);
1394 
1395 	return 0;
1396 }
1397 
1398 static int aw88166_profile_get(struct snd_kcontrol *kcontrol,
1399 			struct snd_ctl_elem_value *ucontrol)
1400 {
1401 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1402 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec);
1403 
1404 	ucontrol->value.integer.value[0] = aw88166->aw_pa->prof_index;
1405 
1406 	return 0;
1407 }
1408 
1409 static int aw88166_profile_set(struct snd_kcontrol *kcontrol,
1410 		struct snd_ctl_elem_value *ucontrol)
1411 {
1412 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1413 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec);
1414 	int ret;
1415 
1416 	guard(mutex)(&aw88166->lock);
1417 	ret = aw88166_dev_set_profile_index(aw88166->aw_pa, ucontrol->value.integer.value[0]);
1418 	if (ret) {
1419 		dev_dbg(codec->dev, "profile index does not change");
1420 		return 0;
1421 	}
1422 
1423 	if (aw88166->aw_pa->status) {
1424 		aw88166_stop(aw88166->aw_pa);
1425 		aw88166_start(aw88166, AW88166_SYNC_START);
1426 	}
1427 
1428 	return 1;
1429 }
1430 
1431 static int aw88166_volume_get(struct snd_kcontrol *kcontrol,
1432 				struct snd_ctl_elem_value *ucontrol)
1433 {
1434 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1435 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec);
1436 	struct aw_volume_desc *vol_desc = &aw88166->aw_pa->volume_desc;
1437 
1438 	ucontrol->value.integer.value[0] = vol_desc->ctl_volume;
1439 
1440 	return 0;
1441 }
1442 
1443 static int aw88166_volume_set(struct snd_kcontrol *kcontrol,
1444 				struct snd_ctl_elem_value *ucontrol)
1445 {
1446 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1447 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec);
1448 	struct aw_volume_desc *vol_desc = &aw88166->aw_pa->volume_desc;
1449 	struct soc_mixer_control *mc =
1450 		(struct soc_mixer_control *)kcontrol->private_value;
1451 	int value;
1452 
1453 	value = ucontrol->value.integer.value[0];
1454 	if (value < mc->min || value > mc->max)
1455 		return -EINVAL;
1456 
1457 	if (vol_desc->ctl_volume != value) {
1458 		vol_desc->ctl_volume = value;
1459 		aw_dev_set_volume(aw88166->aw_pa, vol_desc->ctl_volume);
1460 
1461 		return 1;
1462 	}
1463 
1464 	return 0;
1465 }
1466 
1467 static int aw88166_get_fade_step(struct snd_kcontrol *kcontrol,
1468 				struct snd_ctl_elem_value *ucontrol)
1469 {
1470 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1471 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec);
1472 
1473 	ucontrol->value.integer.value[0] = aw88166->aw_pa->fade_step;
1474 
1475 	return 0;
1476 }
1477 
1478 static int aw88166_set_fade_step(struct snd_kcontrol *kcontrol,
1479 				struct snd_ctl_elem_value *ucontrol)
1480 {
1481 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1482 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec);
1483 	struct soc_mixer_control *mc =
1484 		(struct soc_mixer_control *)kcontrol->private_value;
1485 	int value;
1486 
1487 	value = ucontrol->value.integer.value[0];
1488 	if (value < mc->min || value > mc->max)
1489 		return -EINVAL;
1490 
1491 	if (aw88166->aw_pa->fade_step != value) {
1492 		aw88166->aw_pa->fade_step = value;
1493 		return 1;
1494 	}
1495 
1496 	return 0;
1497 }
1498 
1499 static int aw88166_re_get(struct snd_kcontrol *kcontrol,
1500 				struct snd_ctl_elem_value *ucontrol)
1501 {
1502 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1503 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec);
1504 	struct aw_device *aw_dev = aw88166->aw_pa;
1505 
1506 	ucontrol->value.integer.value[0] = aw_dev->cali_desc.cali_re;
1507 
1508 	return 0;
1509 }
1510 
1511 static int aw88166_re_set(struct snd_kcontrol *kcontrol,
1512 				struct snd_ctl_elem_value *ucontrol)
1513 {
1514 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1515 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec);
1516 	struct soc_mixer_control *mc =
1517 		(struct soc_mixer_control *)kcontrol->private_value;
1518 	struct aw_device *aw_dev = aw88166->aw_pa;
1519 	int value;
1520 
1521 	value = ucontrol->value.integer.value[0];
1522 	if (value < mc->min || value > mc->max)
1523 		return -EINVAL;
1524 
1525 	if (aw_dev->cali_desc.cali_re != value) {
1526 		aw_dev->cali_desc.cali_re = value;
1527 		return 1;
1528 	}
1529 
1530 	return 0;
1531 }
1532 
1533 static int aw88166_dev_init(struct aw88166 *aw88166, struct aw_container *aw_cfg)
1534 {
1535 	struct aw_device *aw_dev = aw88166->aw_pa;
1536 	int ret;
1537 
1538 	ret = aw88395_dev_cfg_load(aw_dev, aw_cfg);
1539 	if (ret) {
1540 		dev_err(aw_dev->dev, "aw_dev acf parse failed\n");
1541 		return -EINVAL;
1542 	}
1543 	aw_dev->fade_in_time = AW88166_1000_US / 10;
1544 	aw_dev->fade_out_time = AW88166_1000_US >> 1;
1545 	aw_dev->prof_cur = aw_dev->prof_info.prof_desc[0].id;
1546 	aw_dev->prof_index = aw_dev->prof_info.prof_desc[0].id;
1547 
1548 	ret = aw88166_dev_fw_update(aw88166, AW88166_FORCE_UPDATE_ON, AW88166_DSP_FW_UPDATE_ON);
1549 	if (ret) {
1550 		dev_err(aw_dev->dev, "fw update failed ret = %d\n", ret);
1551 		return ret;
1552 	}
1553 
1554 	aw88166_dev_mute(aw_dev, true);
1555 
1556 	/* close tx feedback */
1557 	aw_dev_i2s_tx_enable(aw_dev, false);
1558 	usleep_range(AW88166_1000_US, AW88166_1000_US + 100);
1559 
1560 	/* enable amppd */
1561 	aw_dev_amppd(aw_dev, true);
1562 
1563 	/* close dsp */
1564 	aw_dev_dsp_enable(aw_dev, false);
1565 	/* set power down */
1566 	aw_dev_pwd(aw_dev, true);
1567 
1568 	return 0;
1569 }
1570 
1571 static int aw88166_request_firmware_file(struct aw88166 *aw88166)
1572 {
1573 	const struct firmware *cont __free(firmware) = NULL;
1574 	const char *fw_name;
1575 	int ret;
1576 
1577 	aw88166->aw_pa->fw_status = AW88166_DEV_FW_FAILED;
1578 
1579 	if (device_property_read_string(aw88166->aw_pa->dev, "firmware-name", &fw_name) < 0)
1580 		fw_name = AW88166_ACF_FILE;
1581 
1582 	ret = request_firmware(&cont, fw_name, aw88166->aw_pa->dev);
1583 	if (ret) {
1584 		dev_err(aw88166->aw_pa->dev, "request [%s] failed!\n", fw_name);
1585 		return ret;
1586 	}
1587 
1588 	dev_dbg(aw88166->aw_pa->dev, "loaded %s - size: %zu\n",
1589 			fw_name, cont ? cont->size : 0);
1590 
1591 	aw88166->aw_cfg = devm_kzalloc(aw88166->aw_pa->dev,
1592 			struct_size(aw88166->aw_cfg, data, cont->size), GFP_KERNEL);
1593 	if (!aw88166->aw_cfg)
1594 		return -ENOMEM;
1595 
1596 	aw88166->aw_cfg->len = (int)cont->size;
1597 	memcpy(aw88166->aw_cfg->data, cont->data, cont->size);
1598 
1599 	ret = aw88395_dev_load_acf_check(aw88166->aw_pa, aw88166->aw_cfg);
1600 	if (ret) {
1601 		dev_err(aw88166->aw_pa->dev, "load [%s] failed!\n", fw_name);
1602 		return ret;
1603 	}
1604 
1605 	scoped_guard(mutex, &aw88166->lock) {
1606 		/* aw device init */
1607 		ret = aw88166_dev_init(aw88166, aw88166->aw_cfg);
1608 		if (ret)
1609 			dev_err(aw88166->aw_pa->dev, "dev init failed\n");
1610 	}
1611 
1612 	return ret;
1613 }
1614 
1615 static const struct snd_kcontrol_new aw88166_controls[] = {
1616 	SOC_SINGLE_EXT("PCM Playback Volume", AW88166_SYSCTRL2_REG,
1617 		6, AW88166_MUTE_VOL, 0, aw88166_volume_get,
1618 		aw88166_volume_set),
1619 	SOC_SINGLE_EXT("Fade Step", 0, 0, AW88166_MUTE_VOL, 0,
1620 		aw88166_get_fade_step, aw88166_set_fade_step),
1621 	SOC_SINGLE_EXT("Volume Ramp Up Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
1622 		aw88166_get_fade_in_time, aw88166_set_fade_in_time),
1623 	SOC_SINGLE_EXT("Volume Ramp Down Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
1624 		aw88166_get_fade_out_time, aw88166_set_fade_out_time),
1625 	SOC_SINGLE_EXT("Calib", 0, 0, AW88166_CALI_RE_MAX, 0,
1626 		aw88166_re_get, aw88166_re_set),
1627 	AW88166_PROFILE_EXT("AW88166 Profile Set", aw88166_profile_info,
1628 		aw88166_profile_get, aw88166_profile_set),
1629 };
1630 
1631 static int aw88166_playback_event(struct snd_soc_dapm_widget *w,
1632 				struct snd_kcontrol *k, int event)
1633 {
1634 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1635 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(component);
1636 
1637 	guard(mutex)(&aw88166->lock);
1638 	switch (event) {
1639 	case SND_SOC_DAPM_PRE_PMU:
1640 		aw88166_start(aw88166, AW88166_ASYNC_START);
1641 		break;
1642 	case SND_SOC_DAPM_POST_PMD:
1643 		aw88166_stop(aw88166->aw_pa);
1644 		break;
1645 	default:
1646 		break;
1647 	}
1648 
1649 	return 0;
1650 }
1651 
1652 static const struct snd_soc_dapm_widget aw88166_dapm_widgets[] = {
1653 	 /* playback */
1654 	SND_SOC_DAPM_AIF_IN_E("AIF_RX", "Speaker_Playback", 0, SND_SOC_NOPM, 0, 0,
1655 					aw88166_playback_event,
1656 					SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1657 	SND_SOC_DAPM_OUTPUT("DAC Output"),
1658 
1659 	/* capture */
1660 	SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM, 0, 0),
1661 	SND_SOC_DAPM_INPUT("ADC Input"),
1662 };
1663 
1664 static const struct snd_soc_dapm_route aw88166_audio_map[] = {
1665 	{"DAC Output", NULL, "AIF_RX"},
1666 	{"AIF_TX", NULL, "ADC Input"},
1667 };
1668 
1669 static int aw88166_codec_probe(struct snd_soc_component *component)
1670 {
1671 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(component);
1672 	int ret;
1673 
1674 	INIT_DELAYED_WORK(&aw88166->start_work, aw88166_startup_work);
1675 
1676 	ret = aw88166_request_firmware_file(aw88166);
1677 	if (ret)
1678 		dev_err(aw88166->aw_pa->dev, "%s failed\n", __func__);
1679 
1680 	return ret;
1681 }
1682 
1683 static void aw88166_codec_remove(struct snd_soc_component *aw_codec)
1684 {
1685 	struct aw88166 *aw88166 = snd_soc_component_get_drvdata(aw_codec);
1686 
1687 	cancel_delayed_work_sync(&aw88166->start_work);
1688 }
1689 
1690 static const struct snd_soc_component_driver soc_codec_dev_aw88166 = {
1691 	.probe = aw88166_codec_probe,
1692 	.remove = aw88166_codec_remove,
1693 	.dapm_widgets = aw88166_dapm_widgets,
1694 	.num_dapm_widgets = ARRAY_SIZE(aw88166_dapm_widgets),
1695 	.dapm_routes = aw88166_audio_map,
1696 	.num_dapm_routes = ARRAY_SIZE(aw88166_audio_map),
1697 	.controls = aw88166_controls,
1698 	.num_controls = ARRAY_SIZE(aw88166_controls),
1699 };
1700 
1701 static void aw88166_hw_reset(struct aw88166 *aw88166)
1702 {
1703 	if (aw88166->reset_gpio) {
1704 		gpiod_set_value_cansleep(aw88166->reset_gpio, 1);
1705 		usleep_range(AW88166_1000_US, AW88166_1000_US + 10);
1706 		gpiod_set_value_cansleep(aw88166->reset_gpio, 0);
1707 		usleep_range(AW88166_1000_US, AW88166_1000_US + 10);
1708 	}
1709 }
1710 
1711 static void aw88166_parse_channel_dt(struct aw88166 *aw88166)
1712 {
1713 	struct aw_device *aw_dev = aw88166->aw_pa;
1714 	struct device_node *np = aw_dev->dev->of_node;
1715 	u32 channel_value;
1716 
1717 	of_property_read_u32(np, "awinic,audio-channel", &channel_value);
1718 	aw_dev->channel = channel_value;
1719 	aw88166->phase_sync = of_property_read_bool(np, "awinic,sync-flag");
1720 }
1721 
1722 static int aw88166_init(struct aw88166 *aw88166, struct i2c_client *i2c, struct regmap *regmap)
1723 {
1724 	struct aw_device *aw_dev;
1725 	unsigned int chip_id;
1726 	int ret;
1727 
1728 	ret = regmap_read(regmap, AW88166_ID_REG, &chip_id);
1729 	if (ret) {
1730 		dev_err(&i2c->dev, "%s read chipid error. ret = %d\n", __func__, ret);
1731 		return ret;
1732 	}
1733 
1734 	aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL);
1735 	if (!aw_dev)
1736 		return -ENOMEM;
1737 	aw88166->aw_pa = aw_dev;
1738 
1739 	aw_dev->i2c = i2c;
1740 	aw_dev->dev = &i2c->dev;
1741 	aw_dev->regmap = regmap;
1742 	mutex_init(&aw_dev->dsp_lock);
1743 
1744 	aw_dev->chip_id = chip_id;
1745 	aw_dev->acf = NULL;
1746 	aw_dev->prof_info.prof_desc = NULL;
1747 	aw_dev->prof_info.count = 0;
1748 	aw_dev->prof_info.prof_type = AW88395_DEV_NONE_TYPE_ID;
1749 	aw_dev->channel = AW88166_DEV_DEFAULT_CH;
1750 	aw_dev->fw_status = AW88166_DEV_FW_FAILED;
1751 
1752 	aw_dev->fade_step = AW88166_VOLUME_STEP_DB;
1753 	aw_dev->volume_desc.ctl_volume = AW88166_VOL_DEFAULT_VALUE;
1754 
1755 	aw88166_parse_channel_dt(aw88166);
1756 
1757 	return 0;
1758 }
1759 
1760 static int aw88166_i2c_probe(struct i2c_client *i2c)
1761 {
1762 	struct aw88166 *aw88166;
1763 	int ret;
1764 
1765 	if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C))
1766 		return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed\n");
1767 
1768 	aw88166 = devm_kzalloc(&i2c->dev, sizeof(*aw88166), GFP_KERNEL);
1769 	if (!aw88166)
1770 		return -ENOMEM;
1771 
1772 	mutex_init(&aw88166->lock);
1773 
1774 	i2c_set_clientdata(i2c, aw88166);
1775 
1776 	aw88166->reset_gpio = devm_gpiod_get_optional(&i2c->dev, "reset", GPIOD_OUT_LOW);
1777 	if (IS_ERR(aw88166->reset_gpio))
1778 		return dev_err_probe(&i2c->dev, PTR_ERR(aw88166->reset_gpio),
1779 							"reset gpio not defined\n");
1780 	aw88166_hw_reset(aw88166);
1781 
1782 	aw88166->regmap = devm_regmap_init_i2c(i2c, &aw88166_remap_config);
1783 	if (IS_ERR(aw88166->regmap))
1784 		return dev_err_probe(&i2c->dev, PTR_ERR(aw88166->regmap),
1785 							"failed to init regmap\n");
1786 
1787 	/* aw pa init */
1788 	ret = aw88166_init(aw88166, i2c, aw88166->regmap);
1789 	if (ret)
1790 		return ret;
1791 
1792 	return devm_snd_soc_register_component(&i2c->dev,
1793 			&soc_codec_dev_aw88166,
1794 			aw88166_dai, ARRAY_SIZE(aw88166_dai));
1795 }
1796 
1797 static const struct i2c_device_id aw88166_i2c_id[] = {
1798 	{ .name = AW88166_I2C_NAME },
1799 	{ }
1800 };
1801 MODULE_DEVICE_TABLE(i2c, aw88166_i2c_id);
1802 
1803 static struct i2c_driver aw88166_i2c_driver = {
1804 	.driver = {
1805 		.name = AW88166_I2C_NAME,
1806 	},
1807 	.probe = aw88166_i2c_probe,
1808 	.id_table = aw88166_i2c_id,
1809 };
1810 module_i2c_driver(aw88166_i2c_driver);
1811 
1812 MODULE_DESCRIPTION("ASoC AW88166 Smart PA Driver");
1813 MODULE_LICENSE("GPL v2");
1814