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 <[email protected]>
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 						sizeof(uinfo->value.enumerated.name));
919 		return 0;
920 	}
921 
922 	strscpy(uinfo->value.enumerated.name, prof_name, sizeof(uinfo->value.enumerated.name));
923 
924 	return 0;
925 }
926 
aw88081_profile_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)927 static int aw88081_profile_get(struct snd_kcontrol *kcontrol,
928 			struct snd_ctl_elem_value *ucontrol)
929 {
930 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
931 	struct aw88081 *aw88081 = snd_soc_component_get_drvdata(codec);
932 
933 	ucontrol->value.integer.value[0] = aw88081->aw_pa->prof_index;
934 
935 	return 0;
936 }
937 
aw88081_profile_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)938 static int aw88081_profile_set(struct snd_kcontrol *kcontrol,
939 		struct snd_ctl_elem_value *ucontrol)
940 {
941 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
942 	struct aw88081 *aw88081 = snd_soc_component_get_drvdata(codec);
943 	int ret;
944 
945 	/* pa stop or stopping just set profile */
946 	mutex_lock(&aw88081->lock);
947 	ret = aw88081_dev_set_profile_index(aw88081->aw_pa, ucontrol->value.integer.value[0]);
948 	if (ret) {
949 		dev_dbg(codec->dev, "profile index does not change");
950 		mutex_unlock(&aw88081->lock);
951 		return 0;
952 	}
953 
954 	if (aw88081->aw_pa->status) {
955 		aw88081_stop(aw88081);
956 		aw88081_start(aw88081, AW88081_SYNC_START);
957 	}
958 
959 	mutex_unlock(&aw88081->lock);
960 
961 	return 1;
962 }
963 
aw88081_volume_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)964 static int aw88081_volume_get(struct snd_kcontrol *kcontrol,
965 				struct snd_ctl_elem_value *ucontrol)
966 {
967 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
968 	struct aw88081 *aw88081 = snd_soc_component_get_drvdata(codec);
969 	struct aw_volume_desc *vol_desc = &aw88081->aw_pa->volume_desc;
970 
971 	ucontrol->value.integer.value[0] = vol_desc->ctl_volume;
972 
973 	return 0;
974 }
975 
aw88081_volume_set(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)976 static int aw88081_volume_set(struct snd_kcontrol *kcontrol,
977 				struct snd_ctl_elem_value *ucontrol)
978 {
979 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
980 	struct aw88081 *aw88081 = snd_soc_component_get_drvdata(codec);
981 	struct aw_volume_desc *vol_desc = &aw88081->aw_pa->volume_desc;
982 	struct soc_mixer_control *mc =
983 		(struct soc_mixer_control *)kcontrol->private_value;
984 	int value;
985 
986 	value = ucontrol->value.integer.value[0];
987 
988 	if (value < mc->min || value > mc->max)
989 		return -EINVAL;
990 
991 	aw88083_i2c_wen(aw88081, true);
992 
993 	if (vol_desc->ctl_volume != value) {
994 		vol_desc->ctl_volume = value;
995 		aw88081_dev_set_volume(aw88081->aw_pa, vol_desc->ctl_volume);
996 		return 1;
997 	}
998 
999 	aw88083_i2c_wen(aw88081, false);
1000 
1001 	return 0;
1002 }
1003 
aw88081_get_fade_step(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1004 static int aw88081_get_fade_step(struct snd_kcontrol *kcontrol,
1005 				struct snd_ctl_elem_value *ucontrol)
1006 {
1007 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1008 	struct aw88081 *aw88081 = snd_soc_component_get_drvdata(codec);
1009 
1010 	ucontrol->value.integer.value[0] = aw88081->aw_pa->fade_step;
1011 
1012 	return 0;
1013 }
1014 
aw88081_set_fade_step(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1015 static int aw88081_set_fade_step(struct snd_kcontrol *kcontrol,
1016 				struct snd_ctl_elem_value *ucontrol)
1017 {
1018 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1019 	struct aw88081 *aw88081 = snd_soc_component_get_drvdata(codec);
1020 	struct soc_mixer_control *mc =
1021 		(struct soc_mixer_control *)kcontrol->private_value;
1022 	int value;
1023 
1024 	value = ucontrol->value.integer.value[0];
1025 	if (value < mc->min || value > mc->max)
1026 		return -EINVAL;
1027 
1028 	if (aw88081->aw_pa->fade_step != value) {
1029 		aw88081->aw_pa->fade_step = value;
1030 		return 1;
1031 	}
1032 
1033 	return 0;
1034 }
1035 
1036 static const struct snd_kcontrol_new aw88081_controls[] = {
1037 	SOC_SINGLE_EXT("PCM Playback Volume", AW88081_SYSCTRL2_REG,
1038 		0, AW88081_MUTE_VOL, 0, aw88081_volume_get,
1039 		aw88081_volume_set),
1040 	SOC_SINGLE_EXT("Fade Step", 0, 0, AW88081_MUTE_VOL, 0,
1041 		aw88081_get_fade_step, aw88081_set_fade_step),
1042 	SOC_SINGLE_EXT("Volume Ramp Up Step", 0, 0, FADE_TIME_MAX, 0,
1043 		aw88081_get_fade_in_time, aw88081_set_fade_in_time),
1044 	SOC_SINGLE_EXT("Volume Ramp Down Step", 0, 0, FADE_TIME_MAX, 0,
1045 		aw88081_get_fade_out_time, aw88081_set_fade_out_time),
1046 	AW88081_PROFILE_EXT("Profile Set", aw88081_profile_info,
1047 		aw88081_profile_get, aw88081_profile_set),
1048 };
1049 
aw88081_parse_channel_dt(struct aw88081 * aw88081)1050 static void aw88081_parse_channel_dt(struct aw88081 *aw88081)
1051 {
1052 	struct aw_device *aw_dev = aw88081->aw_pa;
1053 	struct device_node *np = aw_dev->dev->of_node;
1054 	u32 channel_value = AW88081_DEV_DEFAULT_CH;
1055 
1056 	of_property_read_u32(np, "awinic,audio-channel", &channel_value);
1057 	aw88081->phase_sync = of_property_read_bool(np, "awinic,sync-flag");
1058 
1059 	aw_dev->channel = channel_value;
1060 }
1061 
aw88081_init(struct aw88081 * aw88081,struct i2c_client * i2c,struct regmap * regmap)1062 static int aw88081_init(struct aw88081 *aw88081, struct i2c_client *i2c, struct regmap *regmap)
1063 {
1064 	struct aw_device *aw_dev;
1065 	unsigned int chip_id;
1066 	int ret;
1067 
1068 	/* read chip id */
1069 	ret = regmap_read(regmap, AW88081_ID_REG, &chip_id);
1070 	if (ret) {
1071 		dev_err(&i2c->dev, "%s read chipid error. ret = %d", __func__, ret);
1072 		return ret;
1073 	}
1074 
1075 	switch (chip_id) {
1076 	case AW88081_CHIP_ID:
1077 		dev_dbg(&i2c->dev, "chip id = 0x%x\n", chip_id);
1078 		break;
1079 	case AW88083_CHIP_ID:
1080 		dev_dbg(&i2c->dev, "chip id = 0x%x\n", chip_id);
1081 		break;
1082 	default:
1083 		dev_err(&i2c->dev, "unsupported device");
1084 		return -ENXIO;
1085 	}
1086 
1087 	aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL);
1088 	if (!aw_dev)
1089 		return -ENOMEM;
1090 
1091 	aw88081->aw_pa = aw_dev;
1092 	aw_dev->i2c = i2c;
1093 	aw_dev->regmap = regmap;
1094 	aw_dev->dev = &i2c->dev;
1095 	aw_dev->chip_id = chip_id;
1096 	aw_dev->acf = NULL;
1097 	aw_dev->prof_info.prof_desc = NULL;
1098 	aw_dev->prof_info.prof_type = AW88395_DEV_NONE_TYPE_ID;
1099 	aw_dev->fade_step = AW88081_VOLUME_STEP_DB;
1100 	aw_dev->volume_desc.mute_volume = AW88081_MUTE_VOL;
1101 	aw88081_parse_channel_dt(aw88081);
1102 
1103 	return 0;
1104 }
1105 
aw88081_dev_init(struct aw88081 * aw88081,struct aw_container * aw_cfg)1106 static int aw88081_dev_init(struct aw88081 *aw88081, struct aw_container *aw_cfg)
1107 {
1108 	struct aw_device *aw_dev = aw88081->aw_pa;
1109 	int ret;
1110 
1111 	ret = aw88395_dev_cfg_load(aw_dev, aw_cfg);
1112 	if (ret) {
1113 		dev_err(aw_dev->dev, "aw_dev acf parse failed");
1114 		return -EINVAL;
1115 	}
1116 
1117 	ret = regmap_write(aw_dev->regmap, AW88081_ID_REG, AW88081_SOFT_RESET_VALUE);
1118 	if (ret)
1119 		return ret;
1120 
1121 	aw_dev->fade_in_time = AW88081_500_US;
1122 	aw_dev->fade_out_time = AW88081_500_US;
1123 	aw_dev->prof_cur = AW88081_INIT_PROFILE;
1124 	aw_dev->prof_index = AW88081_INIT_PROFILE;
1125 
1126 	ret = aw88081_dev_fw_update(aw88081);
1127 	if (ret) {
1128 		dev_err(aw_dev->dev, "fw update failed ret = %d\n", ret);
1129 		return ret;
1130 	}
1131 
1132 	aw_dev->status = AW88081_DEV_PW_ON;
1133 	aw88081_stop(aw88081);
1134 
1135 	return 0;
1136 }
1137 
aw88081_request_firmware_file(struct aw88081 * aw88081)1138 static int aw88081_request_firmware_file(struct aw88081 *aw88081)
1139 {
1140 	const struct firmware *cont = NULL;
1141 	int ret;
1142 
1143 	aw88081->aw_pa->fw_status = AW88081_DEV_FW_FAILED;
1144 
1145 	ret = request_firmware(&cont, AW88081_ACF_FILE, aw88081->aw_pa->dev);
1146 	if (ret)
1147 		return ret;
1148 
1149 	dev_dbg(aw88081->aw_pa->dev, "loaded %s - size: %zu\n",
1150 			AW88081_ACF_FILE, cont ? cont->size : 0);
1151 
1152 	aw88081->aw_cfg = devm_kzalloc(aw88081->aw_pa->dev, cont->size + sizeof(int), GFP_KERNEL);
1153 	if (!aw88081->aw_cfg) {
1154 		release_firmware(cont);
1155 		return -ENOMEM;
1156 	}
1157 	aw88081->aw_cfg->len = (int)cont->size;
1158 	memcpy(aw88081->aw_cfg->data, cont->data, cont->size);
1159 	release_firmware(cont);
1160 
1161 	ret = aw88395_dev_load_acf_check(aw88081->aw_pa, aw88081->aw_cfg);
1162 	if (ret)
1163 		return ret;
1164 
1165 	mutex_lock(&aw88081->lock);
1166 	ret = aw88081_dev_init(aw88081, aw88081->aw_cfg);
1167 	mutex_unlock(&aw88081->lock);
1168 
1169 	return ret;
1170 }
1171 
aw88081_playback_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * k,int event)1172 static int aw88081_playback_event(struct snd_soc_dapm_widget *w,
1173 				struct snd_kcontrol *k, int event)
1174 {
1175 	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1176 	struct aw88081 *aw88081 = snd_soc_component_get_drvdata(component);
1177 
1178 	mutex_lock(&aw88081->lock);
1179 	switch (event) {
1180 	case SND_SOC_DAPM_PRE_PMU:
1181 		aw88081_start(aw88081, AW88081_ASYNC_START);
1182 		break;
1183 	case SND_SOC_DAPM_POST_PMD:
1184 		aw88081_stop(aw88081);
1185 		break;
1186 	default:
1187 		break;
1188 	}
1189 	mutex_unlock(&aw88081->lock);
1190 
1191 	return 0;
1192 }
1193 
1194 static const struct snd_soc_dapm_widget aw88081_dapm_widgets[] = {
1195 	 /* playback */
1196 	SND_SOC_DAPM_AIF_IN_E("AIF_RX", "Speaker_Playback", 0, SND_SOC_NOPM, 0, 0,
1197 					aw88081_playback_event,
1198 					SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1199 	SND_SOC_DAPM_OUTPUT("DAC Output"),
1200 
1201 	/* capture */
1202 	SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM, 0, 0),
1203 	SND_SOC_DAPM_INPUT("ADC Input"),
1204 };
1205 
1206 static const struct snd_soc_dapm_route aw88081_audio_map[] = {
1207 	{"DAC Output", NULL, "AIF_RX"},
1208 	{"AIF_TX", NULL, "ADC Input"},
1209 };
1210 
aw88081_codec_probe(struct snd_soc_component * component)1211 static int aw88081_codec_probe(struct snd_soc_component *component)
1212 {
1213 	struct aw88081 *aw88081 = snd_soc_component_get_drvdata(component);
1214 	int ret;
1215 
1216 	INIT_DELAYED_WORK(&aw88081->start_work, aw88081_startup_work);
1217 
1218 	ret = aw88081_request_firmware_file(aw88081);
1219 	if (ret)
1220 		dev_err(aw88081->aw_pa->dev, "%s: request firmware failed\n", __func__);
1221 
1222 	return ret;
1223 }
1224 
aw88081_codec_remove(struct snd_soc_component * aw_codec)1225 static void aw88081_codec_remove(struct snd_soc_component *aw_codec)
1226 {
1227 	struct aw88081 *aw88081 = snd_soc_component_get_drvdata(aw_codec);
1228 
1229 	cancel_delayed_work_sync(&aw88081->start_work);
1230 }
1231 
1232 static const struct snd_soc_component_driver soc_codec_dev_aw88081 = {
1233 	.probe = aw88081_codec_probe,
1234 	.remove = aw88081_codec_remove,
1235 	.dapm_widgets = aw88081_dapm_widgets,
1236 	.num_dapm_widgets = ARRAY_SIZE(aw88081_dapm_widgets),
1237 	.dapm_routes = aw88081_audio_map,
1238 	.num_dapm_routes = ARRAY_SIZE(aw88081_audio_map),
1239 	.controls = aw88081_controls,
1240 	.num_controls = ARRAY_SIZE(aw88081_controls),
1241 };
1242 
1243 static const struct i2c_device_id aw88081_i2c_id[] = {
1244 	{ AW88081_I2C_NAME, AW88081},
1245 	{ AW88083_I2C_NAME, AW88083},
1246 	{ }
1247 };
1248 MODULE_DEVICE_TABLE(i2c, aw88081_i2c_id);
1249 
aw88081_i2c_probe(struct i2c_client * i2c)1250 static int aw88081_i2c_probe(struct i2c_client *i2c)
1251 {
1252 	const struct regmap_config *regmap_config;
1253 	const struct i2c_device_id *id;
1254 	struct aw88081 *aw88081;
1255 	int ret;
1256 
1257 	ret = i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C);
1258 	if (!ret)
1259 		return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed");
1260 
1261 	aw88081 = devm_kzalloc(&i2c->dev, sizeof(*aw88081), GFP_KERNEL);
1262 	if (!aw88081)
1263 		return -ENOMEM;
1264 
1265 	id = i2c_match_id(aw88081_i2c_id, i2c);
1266 	aw88081->devtype = id->driver_data;
1267 
1268 	mutex_init(&aw88081->lock);
1269 
1270 	i2c_set_clientdata(i2c, aw88081);
1271 
1272 	switch (aw88081->devtype) {
1273 	case AW88081:
1274 		regmap_config = &aw88081_regmap_config;
1275 		break;
1276 	case AW88083:
1277 		regmap_config = &aw88083_regmap_config;
1278 		break;
1279 	default:
1280 		return -EINVAL;
1281 	}
1282 
1283 	aw88081->regmap = devm_regmap_init_i2c(i2c, regmap_config);
1284 	if (IS_ERR(aw88081->regmap))
1285 		return dev_err_probe(&i2c->dev, PTR_ERR(aw88081->regmap),
1286 						"failed to init regmap\n");
1287 
1288 	/* aw pa init */
1289 	ret = aw88081_init(aw88081, i2c, aw88081->regmap);
1290 	if (ret)
1291 		return ret;
1292 
1293 	return devm_snd_soc_register_component(&i2c->dev,
1294 			&soc_codec_dev_aw88081,
1295 			aw88081_dai, ARRAY_SIZE(aw88081_dai));
1296 }
1297 
1298 static struct i2c_driver aw88081_i2c_driver = {
1299 	.driver = {
1300 		.name = AW88081_I2C_NAME,
1301 	},
1302 	.probe = aw88081_i2c_probe,
1303 	.id_table = aw88081_i2c_id,
1304 };
1305 module_i2c_driver(aw88081_i2c_driver);
1306 
1307 MODULE_DESCRIPTION("ASoC AW88081 Smart PA Driver");
1308 MODULE_LICENSE("GPL v2");
1309