xref: /linux/sound/soc/codecs/aw88261.c (revision 519d0a6b2ca5a891340b6c24a4c40545f518e1a8)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // aw88261.c  --  AW88261 ALSA SoC Audio driver
4 //
5 // Copyright (c) 2023 awinic Technology CO., LTD
6 //
7 // Author: Jimmy Zhang <zhangjianming@awinic.com>
8 // Author: Weidong Wang <wangweidong.a@awinic.com>
9 //
10 
11 #include <linux/i2c.h>
12 #include <linux/firmware.h>
13 #include <linux/regmap.h>
14 #include <linux/regulator/consumer.h>
15 #include <sound/soc.h>
16 #include "aw88261.h"
17 #include "aw88395/aw88395_data_type.h"
18 #include "aw88395/aw88395_device.h"
19 
20 static const struct regmap_config aw88261_remap_config = {
21 	.val_bits = 16,
22 	.reg_bits = 8,
23 	.max_register = AW88261_REG_MAX,
24 	.reg_format_endian = REGMAP_ENDIAN_LITTLE,
25 	.val_format_endian = REGMAP_ENDIAN_BIG,
26 };
27 
28 static void aw88261_dev_set_volume(struct aw_device *aw_dev, unsigned int value)
29 {
30 	struct aw_volume_desc *vol_desc = &aw_dev->volume_desc;
31 	unsigned int real_value, volume;
32 	unsigned int reg_value;
33 
34 	volume = min((value + vol_desc->init_volume), (unsigned int)AW88261_MUTE_VOL);
35 	real_value = DB_TO_REG_VAL(volume);
36 
37 	regmap_read(aw_dev->regmap, AW88261_SYSCTRL2_REG, &reg_value);
38 
39 	real_value = (real_value | (reg_value & AW88261_VOL_START_MASK));
40 
41 	dev_dbg(aw_dev->dev, "value 0x%x , real_value:0x%x", value, real_value);
42 
43 	regmap_write(aw_dev->regmap, AW88261_SYSCTRL2_REG, real_value);
44 }
45 
46 static void aw88261_dev_fade_in(struct aw_device *aw_dev)
47 {
48 	struct aw_volume_desc *desc = &aw_dev->volume_desc;
49 	int fade_in_vol = desc->ctl_volume;
50 	int fade_step = aw_dev->fade_step;
51 	int i;
52 
53 	if (fade_step == 0 || aw_dev->fade_in_time == 0) {
54 		aw88261_dev_set_volume(aw_dev, fade_in_vol);
55 		return;
56 	}
57 
58 	for (i = AW88261_MUTE_VOL; i >= fade_in_vol; i -= fade_step) {
59 		aw88261_dev_set_volume(aw_dev, i);
60 		usleep_range(aw_dev->fade_in_time,
61 					aw_dev->fade_in_time + 10);
62 	}
63 
64 	if (i != fade_in_vol)
65 		aw88261_dev_set_volume(aw_dev, fade_in_vol);
66 }
67 
68 static void aw88261_dev_fade_out(struct aw_device *aw_dev)
69 {
70 	struct aw_volume_desc *desc = &aw_dev->volume_desc;
71 	int fade_step = aw_dev->fade_step;
72 	int i;
73 
74 	if (fade_step == 0 || aw_dev->fade_out_time == 0) {
75 		aw88261_dev_set_volume(aw_dev, AW88261_MUTE_VOL);
76 		return;
77 	}
78 
79 	for (i = desc->ctl_volume; i <= AW88261_MUTE_VOL; i += fade_step) {
80 		aw88261_dev_set_volume(aw_dev, i);
81 		usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10);
82 	}
83 
84 	if (i != AW88261_MUTE_VOL) {
85 		aw88261_dev_set_volume(aw_dev, AW88261_MUTE_VOL);
86 		usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10);
87 	}
88 }
89 
90 static void aw88261_dev_i2s_tx_enable(struct aw_device *aw_dev, bool flag)
91 {
92 	if (flag)
93 		regmap_update_bits(aw_dev->regmap, AW88261_I2SCFG1_REG,
94 			~AW88261_I2STXEN_MASK, AW88261_I2STXEN_ENABLE_VALUE);
95 	else
96 		regmap_update_bits(aw_dev->regmap, AW88261_I2SCFG1_REG,
97 			~AW88261_I2STXEN_MASK, AW88261_I2STXEN_DISABLE_VALUE);
98 }
99 
100 static void aw88261_dev_pwd(struct aw_device *aw_dev, bool pwd)
101 {
102 	if (pwd)
103 		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
104 				~AW88261_PWDN_MASK, AW88261_PWDN_POWER_DOWN_VALUE);
105 	else
106 		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
107 				~AW88261_PWDN_MASK, AW88261_PWDN_WORKING_VALUE);
108 }
109 
110 static void aw88261_dev_amppd(struct aw_device *aw_dev, bool amppd)
111 {
112 	if (amppd)
113 		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
114 				~AW88261_AMPPD_MASK, AW88261_AMPPD_POWER_DOWN_VALUE);
115 	else
116 		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
117 				~AW88261_AMPPD_MASK, AW88261_AMPPD_WORKING_VALUE);
118 }
119 
120 static void aw88261_dev_mute(struct aw_device *aw_dev, bool is_mute)
121 {
122 	if (is_mute) {
123 		aw88261_dev_fade_out(aw_dev);
124 		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
125 				~AW88261_HMUTE_MASK, AW88261_HMUTE_ENABLE_VALUE);
126 	} else {
127 		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
128 				~AW88261_HMUTE_MASK, AW88261_HMUTE_DISABLE_VALUE);
129 		aw88261_dev_fade_in(aw_dev);
130 	}
131 }
132 
133 static void aw88261_dev_clear_int_status(struct aw_device *aw_dev)
134 {
135 	unsigned int int_status;
136 
137 	/* read int status and clear */
138 	regmap_read(aw_dev->regmap, AW88261_SYSINT_REG, &int_status);
139 	/* make sure int status is clear */
140 	regmap_read(aw_dev->regmap, AW88261_SYSINT_REG, &int_status);
141 
142 	dev_dbg(aw_dev->dev, "read interrupt reg = 0x%04x", int_status);
143 }
144 
145 static int aw88261_dev_get_iis_status(struct aw_device *aw_dev)
146 {
147 	unsigned int reg_val;
148 	int ret;
149 
150 	ret = regmap_read(aw_dev->regmap, AW88261_SYSST_REG, &reg_val);
151 	if (ret)
152 		return ret;
153 	if ((reg_val & AW88261_BIT_PLL_CHECK) != AW88261_BIT_PLL_CHECK) {
154 		dev_err(aw_dev->dev, "check pll lock fail,reg_val:0x%04x", reg_val);
155 		return -EINVAL;
156 	}
157 
158 	return ret;
159 }
160 
161 static int aw88261_dev_check_mode1_pll(struct aw_device *aw_dev)
162 {
163 	int ret, i;
164 
165 	for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
166 		ret = aw88261_dev_get_iis_status(aw_dev);
167 		if (ret) {
168 			dev_err(aw_dev->dev, "mode1 iis signal check error");
169 			usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
170 		} else {
171 			return ret;
172 		}
173 	}
174 
175 	return -EPERM;
176 }
177 
178 static int aw88261_dev_check_mode2_pll(struct aw_device *aw_dev)
179 {
180 	unsigned int reg_val;
181 	int ret, i;
182 
183 	ret = regmap_read(aw_dev->regmap, AW88261_PLLCTRL1_REG, &reg_val);
184 	if (ret)
185 		return ret;
186 
187 	reg_val &= (~AW88261_CCO_MUX_MASK);
188 	if (reg_val == AW88261_CCO_MUX_DIVIDED_VALUE) {
189 		dev_dbg(aw_dev->dev, "CCO_MUX is already divider");
190 		return -EPERM;
191 	}
192 
193 	/* change mode2 */
194 	ret = regmap_update_bits(aw_dev->regmap, AW88261_PLLCTRL1_REG,
195 			~AW88261_CCO_MUX_MASK, AW88261_CCO_MUX_DIVIDED_VALUE);
196 	if (ret)
197 		return ret;
198 
199 	for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
200 		ret = aw88261_dev_get_iis_status(aw_dev);
201 		if (ret) {
202 			dev_err(aw_dev->dev, "mode2 iis signal check error");
203 			usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
204 		} else {
205 			break;
206 		}
207 	}
208 
209 	/* change mode1 */
210 	ret = regmap_update_bits(aw_dev->regmap, AW88261_PLLCTRL1_REG,
211 			~AW88261_CCO_MUX_MASK, AW88261_CCO_MUX_BYPASS_VALUE);
212 	if (ret == 0) {
213 		usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
214 		for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
215 			ret = aw88261_dev_check_mode1_pll(aw_dev);
216 			if (ret) {
217 				dev_err(aw_dev->dev, "mode2 switch to mode1, iis signal check error");
218 				usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
219 			} else {
220 				break;
221 			}
222 		}
223 	}
224 
225 	return ret;
226 }
227 
228 static int aw88261_dev_check_syspll(struct aw_device *aw_dev)
229 {
230 	int ret;
231 
232 	ret = aw88261_dev_check_mode1_pll(aw_dev);
233 	if (ret) {
234 		dev_dbg(aw_dev->dev, "mode1 check iis failed try switch to mode2 check");
235 		ret = aw88261_dev_check_mode2_pll(aw_dev);
236 		if (ret) {
237 			dev_err(aw_dev->dev, "mode2 check iis failed");
238 			return ret;
239 		}
240 	}
241 
242 	return ret;
243 }
244 
245 static int aw88261_dev_check_sysst(struct aw_device *aw_dev)
246 {
247 	unsigned int check_val;
248 	unsigned int reg_val;
249 	int ret, i;
250 
251 	for (i = 0; i < AW88261_DEV_SYSST_CHECK_MAX; i++) {
252 		ret = regmap_read(aw_dev->regmap, AW88261_SYSST_REG, &reg_val);
253 		if (ret)
254 			return ret;
255 
256 		check_val = reg_val & (~AW88261_BIT_SYSST_CHECK_MASK)
257 							& AW88261_BIT_SYSST_CHECK;
258 		if (check_val != AW88261_BIT_SYSST_CHECK) {
259 			dev_err(aw_dev->dev, "check sysst fail, reg_val=0x%04x, check:0x%x",
260 				reg_val, AW88261_BIT_SYSST_CHECK);
261 			usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
262 		} else {
263 			return 0;
264 		}
265 	}
266 
267 	return -EPERM;
268 }
269 
270 static void aw88261_dev_uls_hmute(struct aw_device *aw_dev, bool uls_hmute)
271 {
272 	if (uls_hmute)
273 		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
274 				~AW88261_ULS_HMUTE_MASK,
275 				AW88261_ULS_HMUTE_ENABLE_VALUE);
276 	else
277 		regmap_update_bits(aw_dev->regmap, AW88261_SYSCTRL_REG,
278 				~AW88261_ULS_HMUTE_MASK,
279 				AW88261_ULS_HMUTE_DISABLE_VALUE);
280 }
281 
282 static void aw88261_reg_force_set(struct aw88261 *aw88261)
283 {
284 	if (aw88261->frcset_en == AW88261_FRCSET_ENABLE) {
285 		/* set FORCE_PWM */
286 		regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL3_REG,
287 				AW88261_FORCE_PWM_MASK, AW88261_FORCE_PWM_FORCEMINUS_PWM_VALUE);
288 		/* set BOOST_OS_WIDTH */
289 		regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL5_REG,
290 				AW88261_BST_OS_WIDTH_MASK, AW88261_BST_OS_WIDTH_50NS_VALUE);
291 		/* set BURST_LOOPR */
292 		regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL6_REG,
293 				AW88261_BST_LOOPR_MASK, AW88261_BST_LOOPR_340K_VALUE);
294 		/* set RSQN_DLY */
295 		regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL7_REG,
296 				AW88261_RSQN_DLY_MASK, AW88261_RSQN_DLY_35NS_VALUE);
297 		/* set BURST_SSMODE */
298 		regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL8_REG,
299 				AW88261_BURST_SSMODE_MASK, AW88261_BURST_SSMODE_FAST_VALUE);
300 		/* set BST_BURST */
301 		regmap_update_bits(aw88261->regmap, AW88261_BSTCTRL9_REG,
302 				AW88261_BST_BURST_MASK, AW88261_BST_BURST_30MA_VALUE);
303 	} else {
304 		dev_dbg(aw88261->aw_pa->dev, "needn't set reg value");
305 	}
306 }
307 
308 static int aw88261_dev_get_icalk(struct aw_device *aw_dev, int16_t *icalk)
309 {
310 	u16 reg_icalk, reg_icalkl;
311 	unsigned int reg_val;
312 	int ret;
313 
314 	ret = regmap_read(aw_dev->regmap, AW88261_EFRH4_REG, &reg_val);
315 	if (ret)
316 		return ret;
317 
318 	reg_icalk = reg_val & (~AW88261_EF_ISN_GESLP_H_MASK);
319 
320 	ret = regmap_read(aw_dev->regmap, AW88261_EFRL4_REG, &reg_val);
321 	if (ret)
322 		return ret;
323 
324 	reg_icalkl = reg_val & (~AW88261_EF_ISN_GESLP_L_MASK);
325 
326 	reg_icalk = (reg_icalk >> AW88261_ICALK_SHIFT) & (reg_icalkl >> AW88261_ICALKL_SHIFT);
327 
328 	if (reg_icalk & (~AW88261_EF_ISN_GESLP_SIGN_MASK))
329 		reg_icalk = reg_icalk | ~AW88261_EF_ISN_GESLP_NEG;
330 
331 	*icalk = (int16_t)reg_icalk;
332 
333 	return ret;
334 }
335 
336 static int aw88261_dev_get_vcalk(struct aw_device *aw_dev, int16_t *vcalk)
337 {
338 	u16 reg_vcalk, reg_vcalkl;
339 	unsigned int reg_val;
340 	int ret;
341 
342 	ret = regmap_read(aw_dev->regmap, AW88261_EFRH3_REG, &reg_val);
343 	if (ret)
344 		return ret;
345 
346 	reg_vcalk = (u16)reg_val & (~AW88261_EF_VSN_GESLP_H_MASK);
347 
348 	ret = regmap_read(aw_dev->regmap, AW88261_EFRL3_REG, &reg_val);
349 	if (ret)
350 		return ret;
351 
352 	reg_vcalkl = (u16)reg_val & (~AW88261_EF_VSN_GESLP_L_MASK);
353 
354 	reg_vcalk = (reg_vcalk >> AW88261_VCALK_SHIFT) & (reg_vcalkl >> AW88261_VCALKL_SHIFT);
355 
356 	if (reg_vcalk & AW88261_EF_VSN_GESLP_SIGN_MASK)
357 		reg_vcalk = reg_vcalk | (~AW88261_EF_VSN_GESLP_NEG);
358 	*vcalk = (int16_t)reg_vcalk;
359 
360 	return ret;
361 }
362 
363 static int aw88261_dev_set_vcalb(struct aw_device *aw_dev)
364 {
365 	int16_t icalk_val, vcalk_val;
366 	int icalk, vcalk, vcalb;
367 	u32 reg_val;
368 	int ret;
369 
370 	ret = aw88261_dev_get_icalk(aw_dev, &icalk_val);
371 	if (ret)
372 		return ret;
373 
374 	ret = aw88261_dev_get_vcalk(aw_dev, &vcalk_val);
375 	if (ret)
376 		return ret;
377 
378 	icalk = AW88261_CABL_BASE_VALUE + AW88261_ICABLK_FACTOR * icalk_val;
379 	vcalk = AW88261_CABL_BASE_VALUE + AW88261_VCABLK_FACTOR * vcalk_val;
380 	if (!vcalk)
381 		return -EINVAL;
382 
383 	vcalb = AW88261_VCAL_FACTOR * icalk / vcalk;
384 	reg_val = (unsigned int)vcalb;
385 
386 	dev_dbg(aw_dev->dev, "icalk=%d, vcalk=%d, vcalb=%d, reg_val=0x%04x",
387 			icalk, vcalk, vcalb, reg_val);
388 	ret = regmap_write(aw_dev->regmap, AW88261_VSNTM1_REG, reg_val);
389 
390 	return ret;
391 }
392 
393 static int aw88261_dev_reg_update(struct aw88261 *aw88261,
394 					unsigned char *data, unsigned int len)
395 {
396 	struct aw_device *aw_dev = aw88261->aw_pa;
397 	struct aw_volume_desc *vol_desc = &aw_dev->volume_desc;
398 	unsigned int read_val, efcheck_val, read_vol;
399 	int data_len, i, ret;
400 	int16_t *reg_data;
401 	u16 reg_val;
402 	u8 reg_addr;
403 
404 	if (!len || !data) {
405 		dev_err(aw_dev->dev, "reg data is null or len is 0");
406 		return -EINVAL;
407 	}
408 
409 	reg_data = (int16_t *)data;
410 	data_len = len >> 1;
411 
412 	if (data_len & 0x1) {
413 		dev_err(aw_dev->dev, "data len:%d unsupported",	data_len);
414 		return -EINVAL;
415 	}
416 
417 	for (i = 0; i < data_len; i += 2) {
418 		reg_addr = reg_data[i];
419 		reg_val = reg_data[i + 1];
420 
421 		if (reg_addr == AW88261_SYSCTRL_REG) {
422 			aw88261->amppd_st = reg_val & (~AW88261_AMPPD_MASK);
423 			ret = regmap_read(aw_dev->regmap, reg_addr, &read_val);
424 			if (ret)
425 				break;
426 
427 			read_val &= (~AW88261_AMPPD_MASK) | (~AW88261_PWDN_MASK) |
428 								(~AW88261_HMUTE_MASK);
429 			reg_val &= (AW88261_AMPPD_MASK | AW88261_PWDN_MASK | AW88261_HMUTE_MASK);
430 			reg_val |= read_val;
431 
432 			/* enable uls hmute */
433 			reg_val &= AW88261_ULS_HMUTE_MASK;
434 			reg_val |= AW88261_ULS_HMUTE_ENABLE_VALUE;
435 		}
436 
437 		if (reg_addr == AW88261_DBGCTRL_REG) {
438 			efcheck_val = reg_val & (~AW88261_EF_DBMD_MASK);
439 			if (efcheck_val == AW88261_OR_VALUE)
440 				aw88261->efuse_check = AW88261_EF_OR_CHECK;
441 			else
442 				aw88261->efuse_check = AW88261_EF_AND_CHECK;
443 		}
444 
445 		/* i2stxen */
446 		if (reg_addr == AW88261_I2SCTRL3_REG) {
447 			/* close tx */
448 			reg_val &= AW88261_I2STXEN_MASK;
449 			reg_val |= AW88261_I2STXEN_DISABLE_VALUE;
450 		}
451 
452 		if (reg_addr == AW88261_SYSCTRL2_REG) {
453 			read_vol = (reg_val & (~AW88261_VOL_MASK)) >>
454 				AW88261_VOL_START_BIT;
455 			aw_dev->volume_desc.init_volume =
456 				REG_VAL_TO_DB(read_vol);
457 		}
458 
459 		if (reg_addr == AW88261_VSNTM1_REG)
460 			continue;
461 
462 		ret = regmap_write(aw_dev->regmap, reg_addr, reg_val);
463 		if (ret)
464 			break;
465 	}
466 
467 	ret = aw88261_dev_set_vcalb(aw_dev);
468 	if (ret)
469 		return ret;
470 
471 	if (aw_dev->prof_cur != aw_dev->prof_index)
472 		vol_desc->ctl_volume = 0;
473 
474 	/* keep min volume */
475 	aw88261_dev_set_volume(aw_dev, vol_desc->mute_volume);
476 
477 	return ret;
478 }
479 
480 static int aw88261_dev_get_prof_name(struct aw_device *aw_dev, int index, char **prof_name)
481 {
482 	struct aw_prof_info *prof_info = &aw_dev->prof_info;
483 	struct aw_prof_desc *prof_desc;
484 
485 	if ((index >= aw_dev->prof_info.count) || (index < 0)) {
486 		dev_err(aw_dev->dev, "index[%d] overflow count[%d]",
487 			index, aw_dev->prof_info.count);
488 		return -EINVAL;
489 	}
490 
491 	prof_desc = &aw_dev->prof_info.prof_desc[index];
492 
493 	*prof_name = prof_info->prof_name_list[prof_desc->id];
494 
495 	return 0;
496 }
497 
498 static int aw88261_dev_get_prof_data(struct aw_device *aw_dev, int index,
499 			struct aw_prof_desc **prof_desc)
500 {
501 	if ((index >= aw_dev->prof_info.count) || (index < 0)) {
502 		dev_err(aw_dev->dev, "%s: index[%d] overflow count[%d]\n",
503 				__func__, index, aw_dev->prof_info.count);
504 		return -EINVAL;
505 	}
506 
507 	*prof_desc = &aw_dev->prof_info.prof_desc[index];
508 
509 	return 0;
510 }
511 
512 static int aw88261_dev_fw_update(struct aw88261 *aw88261)
513 {
514 	struct aw_device *aw_dev = aw88261->aw_pa;
515 	struct aw_prof_desc *prof_index_desc;
516 	struct aw_sec_data_desc *sec_desc;
517 	char *prof_name;
518 	int ret;
519 
520 	ret = aw88261_dev_get_prof_name(aw_dev, aw_dev->prof_index, &prof_name);
521 	if (ret) {
522 		dev_err(aw_dev->dev, "get prof name failed");
523 		return -EINVAL;
524 	}
525 
526 	dev_dbg(aw_dev->dev, "start update %s", prof_name);
527 
528 	ret = aw88261_dev_get_prof_data(aw_dev, aw_dev->prof_index, &prof_index_desc);
529 	if (ret)
530 		return ret;
531 
532 	/* update reg */
533 	sec_desc = prof_index_desc->sec_desc;
534 	ret = aw88261_dev_reg_update(aw88261, sec_desc[AW88395_DATA_TYPE_REG].data,
535 					sec_desc[AW88395_DATA_TYPE_REG].len);
536 	if (ret) {
537 		dev_err(aw_dev->dev, "update reg failed");
538 		return ret;
539 	}
540 
541 	aw_dev->prof_cur = aw_dev->prof_index;
542 
543 	return ret;
544 }
545 
546 static int aw88261_dev_start(struct aw88261 *aw88261)
547 {
548 	struct aw_device *aw_dev = aw88261->aw_pa;
549 	int ret;
550 
551 	if (aw_dev->status == AW88261_DEV_PW_ON) {
552 		dev_info(aw_dev->dev, "already power on");
553 		return 0;
554 	}
555 
556 	/* power on */
557 	aw88261_dev_pwd(aw_dev, false);
558 	usleep_range(AW88261_2000_US, AW88261_2000_US + 10);
559 
560 	ret = aw88261_dev_check_syspll(aw_dev);
561 	if (ret) {
562 		dev_err(aw_dev->dev, "pll check failed cannot start");
563 		goto pll_check_fail;
564 	}
565 
566 	/* amppd on */
567 	aw88261_dev_amppd(aw_dev, false);
568 	usleep_range(AW88261_1000_US, AW88261_1000_US + 50);
569 
570 	/* check i2s status */
571 	ret = aw88261_dev_check_sysst(aw_dev);
572 	if (ret) {
573 		dev_err(aw_dev->dev, "sysst check failed");
574 		goto sysst_check_fail;
575 	}
576 
577 	/* enable tx feedback */
578 	aw88261_dev_i2s_tx_enable(aw_dev, true);
579 
580 	if (aw88261->amppd_st)
581 		aw88261_dev_amppd(aw_dev, true);
582 
583 	aw88261_reg_force_set(aw88261);
584 
585 	/* close uls mute */
586 	aw88261_dev_uls_hmute(aw_dev, false);
587 
588 	/* close mute */
589 	if (!aw88261->mute_st)
590 		aw88261_dev_mute(aw_dev, false);
591 
592 	/* clear inturrupt */
593 	aw88261_dev_clear_int_status(aw_dev);
594 	aw_dev->status = AW88261_DEV_PW_ON;
595 
596 	return 0;
597 
598 sysst_check_fail:
599 	aw88261_dev_i2s_tx_enable(aw_dev, false);
600 	aw88261_dev_clear_int_status(aw_dev);
601 	aw88261_dev_amppd(aw_dev, true);
602 pll_check_fail:
603 	aw88261_dev_pwd(aw_dev, true);
604 	aw_dev->status = AW88261_DEV_PW_OFF;
605 
606 	return ret;
607 }
608 
609 static int aw88261_dev_stop(struct aw_device *aw_dev)
610 {
611 	if (aw_dev->status == AW88261_DEV_PW_OFF) {
612 		dev_info(aw_dev->dev, "already power off");
613 		return 0;
614 	}
615 
616 	aw_dev->status = AW88261_DEV_PW_OFF;
617 
618 	/* clear inturrupt */
619 	aw88261_dev_clear_int_status(aw_dev);
620 
621 	aw88261_dev_uls_hmute(aw_dev, true);
622 	/* set mute */
623 	aw88261_dev_mute(aw_dev, true);
624 
625 	/* close tx feedback */
626 	aw88261_dev_i2s_tx_enable(aw_dev, false);
627 	usleep_range(AW88261_1000_US, AW88261_1000_US + 100);
628 
629 	/* enable amppd */
630 	aw88261_dev_amppd(aw_dev, true);
631 
632 	/* set power down */
633 	aw88261_dev_pwd(aw_dev, true);
634 
635 	return 0;
636 }
637 
638 static int aw88261_reg_update(struct aw88261 *aw88261, bool force)
639 {
640 	struct aw_device *aw_dev = aw88261->aw_pa;
641 	int ret;
642 
643 	if (force) {
644 		ret = regmap_write(aw_dev->regmap,
645 					AW88261_ID_REG, AW88261_SOFT_RESET_VALUE);
646 		if (ret)
647 			return ret;
648 
649 		ret = aw88261_dev_fw_update(aw88261);
650 		if (ret)
651 			return ret;
652 	} else {
653 		if (aw_dev->prof_cur != aw_dev->prof_index) {
654 			ret = aw88261_dev_fw_update(aw88261);
655 			if (ret)
656 				return ret;
657 		} else {
658 			ret = 0;
659 		}
660 	}
661 
662 	aw_dev->prof_cur = aw_dev->prof_index;
663 
664 	return ret;
665 }
666 
667 static void aw88261_start_pa(struct aw88261 *aw88261)
668 {
669 	int ret, i;
670 
671 	for (i = 0; i < AW88261_START_RETRIES; i++) {
672 		ret = aw88261_reg_update(aw88261, aw88261->phase_sync);
673 		if (ret) {
674 			dev_err(aw88261->aw_pa->dev, "fw update failed, cnt:%d\n", i);
675 			continue;
676 		}
677 		ret = aw88261_dev_start(aw88261);
678 		if (ret) {
679 			dev_err(aw88261->aw_pa->dev, "aw88261 device start failed. retry = %d", i);
680 			continue;
681 		} else {
682 			dev_info(aw88261->aw_pa->dev, "start success\n");
683 			break;
684 		}
685 	}
686 }
687 
688 static void aw88261_startup_work(struct work_struct *work)
689 {
690 	struct aw88261 *aw88261 =
691 		container_of(work, struct aw88261, start_work.work);
692 
693 	mutex_lock(&aw88261->lock);
694 	aw88261_start_pa(aw88261);
695 	mutex_unlock(&aw88261->lock);
696 }
697 
698 static void aw88261_start(struct aw88261 *aw88261, bool sync_start)
699 {
700 	if (aw88261->aw_pa->fw_status != AW88261_DEV_FW_OK)
701 		return;
702 
703 	if (aw88261->aw_pa->status == AW88261_DEV_PW_ON)
704 		return;
705 
706 	if (sync_start == AW88261_SYNC_START)
707 		aw88261_start_pa(aw88261);
708 	else
709 		queue_delayed_work(system_dfl_wq,
710 			&aw88261->start_work,
711 			AW88261_START_WORK_DELAY_MS);
712 }
713 
714 static struct snd_soc_dai_driver aw88261_dai[] = {
715 	{
716 		.name = "aw88261-aif",
717 		.id = 1,
718 		.playback = {
719 			.stream_name = "Speaker_Playback",
720 			.channels_min = 1,
721 			.channels_max = 2,
722 			.rates = AW88261_RATES,
723 			.formats = AW88261_FORMATS,
724 		},
725 		.capture = {
726 			.stream_name = "Speaker_Capture",
727 			.channels_min = 1,
728 			.channels_max = 2,
729 			.rates = AW88261_RATES,
730 			.formats = AW88261_FORMATS,
731 		},
732 	},
733 };
734 
735 static int aw88261_get_fade_in_time(struct snd_kcontrol *kcontrol,
736 	struct snd_ctl_elem_value *ucontrol)
737 {
738 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
739 	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
740 	struct aw_device *aw_dev = aw88261->aw_pa;
741 
742 	ucontrol->value.integer.value[0] = aw_dev->fade_in_time;
743 
744 	return 0;
745 }
746 
747 static int aw88261_set_fade_in_time(struct snd_kcontrol *kcontrol,
748 	struct snd_ctl_elem_value *ucontrol)
749 {
750 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
751 	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
752 	struct soc_mixer_control *mc =
753 		(struct soc_mixer_control *)kcontrol->private_value;
754 	struct aw_device *aw_dev = aw88261->aw_pa;
755 	int time;
756 
757 	time = ucontrol->value.integer.value[0];
758 
759 	if (time < mc->min || time > mc->max)
760 		return -EINVAL;
761 
762 	if (time != aw_dev->fade_in_time) {
763 		aw_dev->fade_in_time = time;
764 		return 1;
765 	}
766 
767 	return 0;
768 }
769 
770 static int aw88261_get_fade_out_time(struct snd_kcontrol *kcontrol,
771 	struct snd_ctl_elem_value *ucontrol)
772 {
773 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
774 	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
775 	struct aw_device *aw_dev = aw88261->aw_pa;
776 
777 	ucontrol->value.integer.value[0] = aw_dev->fade_out_time;
778 
779 	return 0;
780 }
781 
782 static int aw88261_set_fade_out_time(struct snd_kcontrol *kcontrol,
783 	struct snd_ctl_elem_value *ucontrol)
784 {
785 	struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
786 	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
787 	struct soc_mixer_control *mc =
788 		(struct soc_mixer_control *)kcontrol->private_value;
789 	struct aw_device *aw_dev = aw88261->aw_pa;
790 	int time;
791 
792 	time = ucontrol->value.integer.value[0];
793 	if (time < mc->min || time > mc->max)
794 		return -EINVAL;
795 
796 	if (time != aw_dev->fade_out_time) {
797 		aw_dev->fade_out_time = time;
798 		return 1;
799 	}
800 
801 	return 0;
802 }
803 
804 static int aw88261_dev_set_profile_index(struct aw_device *aw_dev, int index)
805 {
806 	/* check the index whether is valid */
807 	if ((index >= aw_dev->prof_info.count) || (index < 0))
808 		return -EINVAL;
809 	/* check the index whether change */
810 	if (aw_dev->prof_index == index)
811 		return -EPERM;
812 
813 	aw_dev->prof_index = index;
814 
815 	return 0;
816 }
817 
818 static int aw88261_profile_info(struct snd_kcontrol *kcontrol,
819 			 struct snd_ctl_elem_info *uinfo)
820 {
821 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
822 	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
823 	char *prof_name;
824 	int count, ret;
825 
826 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
827 	uinfo->count = 1;
828 
829 	count = aw88261->aw_pa->prof_info.count;
830 	if (count <= 0) {
831 		uinfo->value.enumerated.items = 0;
832 		return 0;
833 	}
834 
835 	uinfo->value.enumerated.items = count;
836 
837 	if (uinfo->value.enumerated.item >= count)
838 		uinfo->value.enumerated.item = count - 1;
839 
840 	count = uinfo->value.enumerated.item;
841 
842 	ret = aw88261_dev_get_prof_name(aw88261->aw_pa, count, &prof_name);
843 	if (ret) {
844 		strscpy(uinfo->value.enumerated.name, "null");
845 		return 0;
846 	}
847 
848 	strscpy(uinfo->value.enumerated.name, prof_name);
849 
850 	return 0;
851 }
852 
853 static int aw88261_profile_get(struct snd_kcontrol *kcontrol,
854 			struct snd_ctl_elem_value *ucontrol)
855 {
856 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
857 	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
858 
859 	ucontrol->value.integer.value[0] = aw88261->aw_pa->prof_index;
860 
861 	return 0;
862 }
863 
864 static int aw88261_profile_set(struct snd_kcontrol *kcontrol,
865 		struct snd_ctl_elem_value *ucontrol)
866 {
867 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
868 	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
869 	int ret;
870 
871 	/* pa stop or stopping just set profile */
872 	mutex_lock(&aw88261->lock);
873 	ret = aw88261_dev_set_profile_index(aw88261->aw_pa, ucontrol->value.integer.value[0]);
874 	if (ret) {
875 		dev_dbg(codec->dev, "profile index does not change");
876 		mutex_unlock(&aw88261->lock);
877 		return 0;
878 	}
879 
880 	if (aw88261->aw_pa->status) {
881 		aw88261_dev_stop(aw88261->aw_pa);
882 		aw88261_start(aw88261, AW88261_SYNC_START);
883 	}
884 
885 	mutex_unlock(&aw88261->lock);
886 
887 	return 1;
888 }
889 
890 static int aw88261_volume_get(struct snd_kcontrol *kcontrol,
891 				struct snd_ctl_elem_value *ucontrol)
892 {
893 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
894 	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
895 	struct aw_volume_desc *vol_desc = &aw88261->aw_pa->volume_desc;
896 
897 	ucontrol->value.integer.value[0] = vol_desc->ctl_volume;
898 
899 	return 0;
900 }
901 
902 static int aw88261_volume_set(struct snd_kcontrol *kcontrol,
903 				struct snd_ctl_elem_value *ucontrol)
904 {
905 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
906 	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
907 	struct aw_volume_desc *vol_desc = &aw88261->aw_pa->volume_desc;
908 	struct soc_mixer_control *mc =
909 		(struct soc_mixer_control *)kcontrol->private_value;
910 	int value;
911 
912 	value = ucontrol->value.integer.value[0];
913 
914 	if (value < mc->min || value > mc->max)
915 		return -EINVAL;
916 
917 	if (vol_desc->ctl_volume != value) {
918 		vol_desc->ctl_volume = value;
919 		aw88261_dev_set_volume(aw88261->aw_pa, vol_desc->ctl_volume);
920 
921 		return 1;
922 	}
923 
924 	return 0;
925 }
926 
927 static int aw88261_get_fade_step(struct snd_kcontrol *kcontrol,
928 				struct snd_ctl_elem_value *ucontrol)
929 {
930 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
931 	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
932 
933 	ucontrol->value.integer.value[0] = aw88261->aw_pa->fade_step;
934 
935 	return 0;
936 }
937 
938 static int aw88261_set_fade_step(struct snd_kcontrol *kcontrol,
939 				struct snd_ctl_elem_value *ucontrol)
940 {
941 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
942 	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(codec);
943 	struct soc_mixer_control *mc =
944 		(struct soc_mixer_control *)kcontrol->private_value;
945 	int value;
946 
947 	value = ucontrol->value.integer.value[0];
948 	if (value < mc->min || value > mc->max)
949 		return -EINVAL;
950 
951 	if (aw88261->aw_pa->fade_step != value) {
952 		aw88261->aw_pa->fade_step = value;
953 		return 1;
954 	}
955 
956 	return 0;
957 }
958 
959 static const struct snd_kcontrol_new aw88261_controls[] = {
960 	SOC_SINGLE_EXT("PCM Playback Volume", AW88261_SYSCTRL2_REG,
961 		6, AW88261_MUTE_VOL, 0, aw88261_volume_get,
962 		aw88261_volume_set),
963 	SOC_SINGLE_EXT("Fade Step", 0, 0, AW88261_MUTE_VOL, 0,
964 		aw88261_get_fade_step, aw88261_set_fade_step),
965 	SOC_SINGLE_EXT("Volume Ramp Up Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
966 		aw88261_get_fade_in_time, aw88261_set_fade_in_time),
967 	SOC_SINGLE_EXT("Volume Ramp Down Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
968 		aw88261_get_fade_out_time, aw88261_set_fade_out_time),
969 	AW88261_PROFILE_EXT("Profile Set", aw88261_profile_info,
970 		aw88261_profile_get, aw88261_profile_set),
971 };
972 
973 static int aw88261_playback_event(struct snd_soc_dapm_widget *w,
974 				struct snd_kcontrol *k, int event)
975 {
976 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
977 	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
978 
979 	mutex_lock(&aw88261->lock);
980 	switch (event) {
981 	case SND_SOC_DAPM_PRE_PMU:
982 		aw88261_start(aw88261, AW88261_ASYNC_START);
983 		break;
984 	case SND_SOC_DAPM_POST_PMD:
985 		aw88261_dev_stop(aw88261->aw_pa);
986 		break;
987 	default:
988 		break;
989 	}
990 	mutex_unlock(&aw88261->lock);
991 
992 	return 0;
993 }
994 
995 static const struct snd_soc_dapm_widget aw88261_dapm_widgets[] = {
996 	 /* playback */
997 	SND_SOC_DAPM_AIF_IN_E("AIF_RX", "Speaker_Playback", 0, 0, 0, 0,
998 					aw88261_playback_event,
999 					SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1000 	SND_SOC_DAPM_OUTPUT("DAC Output"),
1001 
1002 	/* capture */
1003 	SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM, 0, 0),
1004 	SND_SOC_DAPM_INPUT("ADC Input"),
1005 };
1006 
1007 static const struct snd_soc_dapm_route aw88261_audio_map[] = {
1008 	{"DAC Output", NULL, "AIF_RX"},
1009 	{"AIF_TX", NULL, "ADC Input"},
1010 };
1011 
1012 static int aw88261_frcset_check(struct aw88261 *aw88261)
1013 {
1014 	unsigned int reg_val;
1015 	u16 temh, teml, tem;
1016 	int ret;
1017 
1018 	ret = regmap_read(aw88261->regmap, AW88261_EFRH3_REG, &reg_val);
1019 	if (ret)
1020 		return ret;
1021 	temh = ((u16)reg_val & (~AW88261_TEMH_MASK));
1022 
1023 	ret = regmap_read(aw88261->regmap, AW88261_EFRL3_REG, &reg_val);
1024 	if (ret)
1025 		return ret;
1026 	teml = ((u16)reg_val & (~AW88261_TEML_MASK));
1027 
1028 	if (aw88261->efuse_check == AW88261_EF_OR_CHECK)
1029 		tem = (temh | teml);
1030 	else
1031 		tem = (temh & teml);
1032 
1033 	if (tem == AW88261_DEFAULT_CFG)
1034 		aw88261->frcset_en = AW88261_FRCSET_ENABLE;
1035 	else
1036 		aw88261->frcset_en = AW88261_FRCSET_DISABLE;
1037 
1038 	dev_dbg(aw88261->aw_pa->dev, "tem is 0x%04x, frcset_en is %d",
1039 						tem, aw88261->frcset_en);
1040 
1041 	return ret;
1042 }
1043 
1044 static int aw88261_dev_init(struct aw88261 *aw88261, struct aw_container *aw_cfg)
1045 {
1046 	struct aw_device *aw_dev = aw88261->aw_pa;
1047 	int ret;
1048 
1049 	ret = aw88395_dev_cfg_load(aw_dev, aw_cfg);
1050 	if (ret) {
1051 		dev_err(aw_dev->dev, "aw_dev acf parse failed");
1052 		return -EINVAL;
1053 	}
1054 
1055 	ret = regmap_write(aw_dev->regmap, AW88261_ID_REG, AW88261_SOFT_RESET_VALUE);
1056 	if (ret)
1057 		return ret;
1058 
1059 	aw_dev->fade_in_time = AW88261_500_US;
1060 	aw_dev->fade_out_time = AW88261_500_US;
1061 	aw_dev->prof_cur = AW88261_INIT_PROFILE;
1062 	aw_dev->prof_index = AW88261_INIT_PROFILE;
1063 
1064 	ret = aw88261_dev_fw_update(aw88261);
1065 	if (ret) {
1066 		dev_err(aw_dev->dev, "fw update failed ret = %d\n", ret);
1067 		return ret;
1068 	}
1069 
1070 	ret = aw88261_frcset_check(aw88261);
1071 	if (ret) {
1072 		dev_err(aw_dev->dev, "aw88261_frcset_check ret = %d\n", ret);
1073 		return ret;
1074 	}
1075 
1076 	aw88261_dev_clear_int_status(aw_dev);
1077 
1078 	aw88261_dev_uls_hmute(aw_dev, true);
1079 
1080 	aw88261_dev_mute(aw_dev, true);
1081 
1082 	aw88261_dev_i2s_tx_enable(aw_dev, false);
1083 
1084 	usleep_range(AW88261_1000_US, AW88261_1000_US + 100);
1085 
1086 	aw88261_dev_amppd(aw_dev, true);
1087 
1088 	aw88261_dev_pwd(aw_dev, true);
1089 
1090 	return 0;
1091 }
1092 
1093 static int aw88261_request_firmware_file(struct aw88261 *aw88261)
1094 {
1095 	const struct firmware *cont = NULL;
1096 	int ret;
1097 
1098 	aw88261->aw_pa->fw_status = AW88261_DEV_FW_FAILED;
1099 
1100 	ret = request_firmware(&cont, AW88261_ACF_FILE, aw88261->aw_pa->dev);
1101 	if (ret)
1102 		return dev_err_probe(aw88261->aw_pa->dev, ret,
1103 					"load [%s] failed!", AW88261_ACF_FILE);
1104 
1105 	dev_info(aw88261->aw_pa->dev, "loaded %s - size: %zu\n",
1106 			AW88261_ACF_FILE, cont ? cont->size : 0);
1107 
1108 	aw88261->aw_cfg = devm_kzalloc(aw88261->aw_pa->dev, cont->size + sizeof(int), GFP_KERNEL);
1109 	if (!aw88261->aw_cfg) {
1110 		release_firmware(cont);
1111 		return -ENOMEM;
1112 	}
1113 	aw88261->aw_cfg->len = (int)cont->size;
1114 	memcpy(aw88261->aw_cfg->data, cont->data, cont->size);
1115 	release_firmware(cont);
1116 
1117 	ret = aw88395_dev_load_acf_check(aw88261->aw_pa, aw88261->aw_cfg);
1118 	if (ret) {
1119 		dev_err(aw88261->aw_pa->dev, "load [%s] failed !", AW88261_ACF_FILE);
1120 		return ret;
1121 	}
1122 
1123 	mutex_lock(&aw88261->lock);
1124 	/* aw device init */
1125 	ret = aw88261_dev_init(aw88261, aw88261->aw_cfg);
1126 	if (ret)
1127 		dev_err(aw88261->aw_pa->dev, "dev init failed");
1128 	mutex_unlock(&aw88261->lock);
1129 
1130 	return ret;
1131 }
1132 
1133 static int aw88261_codec_probe(struct snd_soc_component *component)
1134 {
1135 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
1136 	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(component);
1137 	int ret;
1138 
1139 	INIT_DELAYED_WORK(&aw88261->start_work, aw88261_startup_work);
1140 
1141 	ret = aw88261_request_firmware_file(aw88261);
1142 	if (ret)
1143 		return dev_err_probe(aw88261->aw_pa->dev, ret,
1144 				"aw88261_request_firmware_file failed\n");
1145 
1146 	/* add widgets */
1147 	ret = snd_soc_dapm_new_controls(dapm, aw88261_dapm_widgets,
1148 							ARRAY_SIZE(aw88261_dapm_widgets));
1149 	if (ret)
1150 		return ret;
1151 
1152 	/* add route */
1153 	ret = snd_soc_dapm_add_routes(dapm, aw88261_audio_map,
1154 							ARRAY_SIZE(aw88261_audio_map));
1155 	if (ret)
1156 		return ret;
1157 
1158 	ret = snd_soc_add_component_controls(component, aw88261_controls,
1159 							ARRAY_SIZE(aw88261_controls));
1160 
1161 	return ret;
1162 }
1163 
1164 static void aw88261_codec_remove(struct snd_soc_component *aw_codec)
1165 {
1166 	struct aw88261 *aw88261 = snd_soc_component_get_drvdata(aw_codec);
1167 
1168 	cancel_delayed_work_sync(&aw88261->start_work);
1169 }
1170 
1171 static const struct snd_soc_component_driver soc_codec_dev_aw88261 = {
1172 	.probe = aw88261_codec_probe,
1173 	.remove = aw88261_codec_remove,
1174 };
1175 
1176 static void aw88261_parse_channel_dt(struct aw88261 *aw88261)
1177 {
1178 	struct aw_device *aw_dev = aw88261->aw_pa;
1179 	struct device_node *np = aw_dev->dev->of_node;
1180 	u32 channel_value = AW88261_DEV_DEFAULT_CH;
1181 
1182 	of_property_read_u32(np, "awinic,audio-channel", &channel_value);
1183 	aw88261->phase_sync = of_property_read_bool(np, "awinic,sync-flag");
1184 
1185 	aw_dev->channel = channel_value;
1186 }
1187 
1188 static int aw88261_init(struct aw88261 *aw88261, struct i2c_client *i2c, struct regmap *regmap)
1189 {
1190 	struct aw_device *aw_dev;
1191 	unsigned int chip_id;
1192 	int ret;
1193 
1194 	ret = devm_regulator_get_enable(&i2c->dev, "dvdd");
1195 	if (ret)
1196 		return dev_err_probe(&i2c->dev, ret, "Failed to enable dvdd supply\n");
1197 
1198 	/* read chip id */
1199 	ret = regmap_read(regmap, AW88261_ID_REG, &chip_id);
1200 	if (ret) {
1201 		dev_err(&i2c->dev, "%s read chipid error. ret = %d", __func__, ret);
1202 		return ret;
1203 	}
1204 	if (chip_id != AW88261_CHIP_ID) {
1205 		dev_err(&i2c->dev, "unsupported device");
1206 		return -ENXIO;
1207 	}
1208 
1209 	dev_info(&i2c->dev, "chip id = %x\n", chip_id);
1210 
1211 	aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL);
1212 	if (!aw_dev)
1213 		return -ENOMEM;
1214 
1215 	aw88261->aw_pa = aw_dev;
1216 	aw_dev->i2c = i2c;
1217 	aw_dev->regmap = regmap;
1218 	aw_dev->dev = &i2c->dev;
1219 	aw_dev->chip_id = AW88261_CHIP_ID;
1220 	aw_dev->acf = NULL;
1221 	aw_dev->prof_info.prof_desc = NULL;
1222 	aw_dev->prof_info.count = 0;
1223 	aw_dev->prof_info.prof_type = AW88395_DEV_NONE_TYPE_ID;
1224 	aw_dev->channel = 0;
1225 	aw_dev->fw_status = AW88261_DEV_FW_FAILED;
1226 	aw_dev->fade_step = AW88261_VOLUME_STEP_DB;
1227 	aw_dev->volume_desc.ctl_volume = AW88261_VOL_DEFAULT_VALUE;
1228 	aw_dev->volume_desc.mute_volume = AW88261_MUTE_VOL;
1229 	aw88261_parse_channel_dt(aw88261);
1230 
1231 	return ret;
1232 }
1233 
1234 static int aw88261_i2c_probe(struct i2c_client *i2c)
1235 {
1236 	struct aw88261 *aw88261;
1237 	int ret;
1238 
1239 	ret = i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C);
1240 	if (!ret)
1241 		return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed");
1242 
1243 	aw88261 = devm_kzalloc(&i2c->dev, sizeof(*aw88261), GFP_KERNEL);
1244 	if (!aw88261)
1245 		return -ENOMEM;
1246 
1247 	mutex_init(&aw88261->lock);
1248 
1249 	i2c_set_clientdata(i2c, aw88261);
1250 
1251 	aw88261->regmap = devm_regmap_init_i2c(i2c, &aw88261_remap_config);
1252 	if (IS_ERR(aw88261->regmap)) {
1253 		ret = PTR_ERR(aw88261->regmap);
1254 		return dev_err_probe(&i2c->dev, ret, "failed to init regmap: %d\n", ret);
1255 	}
1256 
1257 	/* aw pa init */
1258 	ret = aw88261_init(aw88261, i2c, aw88261->regmap);
1259 	if (ret)
1260 		return ret;
1261 
1262 	ret = devm_snd_soc_register_component(&i2c->dev,
1263 			&soc_codec_dev_aw88261,
1264 			aw88261_dai, ARRAY_SIZE(aw88261_dai));
1265 	if (ret)
1266 		dev_err(&i2c->dev, "failed to register aw88261: %d", ret);
1267 
1268 	return ret;
1269 }
1270 
1271 static const struct i2c_device_id aw88261_i2c_id[] = {
1272 	{ "aw88261" },
1273 	{ }
1274 };
1275 MODULE_DEVICE_TABLE(i2c, aw88261_i2c_id);
1276 
1277 static const struct of_device_id aw88261_of_table[] = {
1278 	{ .compatible = "awinic,aw88261" },
1279 	{ }
1280 };
1281 MODULE_DEVICE_TABLE(of, aw88261_of_table);
1282 
1283 static struct i2c_driver aw88261_i2c_driver = {
1284 	.driver = {
1285 		.name = "aw88261",
1286 		.of_match_table = aw88261_of_table,
1287 	},
1288 	.probe = aw88261_i2c_probe,
1289 	.id_table = aw88261_i2c_id,
1290 };
1291 module_i2c_driver(aw88261_i2c_driver);
1292 
1293 MODULE_DESCRIPTION("ASoC AW88261 Smart PA Driver");
1294 MODULE_LICENSE("GPL v2");
1295