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