1 // SPDX-License-Identifier: GPL-2.0-only
3 // aw88399.c -- ALSA SoC AW88399 codec support
5 // Copyright (c) 2023 AWINIC Technology CO., LTD
7 // Author: Weidong Wang <wangweidong.a@awinic.com>
10 #include <linux/crc32.h>
11 #include <linux/i2c.h>
12 #include <linux/firmware.h>
13 #include <linux/of_gpio.h>
14 #include <linux/regmap.h>
15 #include <sound/soc.h>
17 #include "aw88395/aw88395_device.h"
18 #include "aw88395/aw88395_reg.h"
20 static const struct regmap_config aw88399_remap_config = {
23 .max_register = AW88399_REG_MAX,
24 .reg_format_endian = REGMAP_ENDIAN_LITTLE,
25 .val_format_endian = REGMAP_ENDIAN_BIG,
28 static int aw_dev_dsp_write_16bit(struct aw_device *aw_dev,
29 unsigned short dsp_addr, unsigned int dsp_data)
33 ret = regmap_write(aw_dev->regmap, AW88399_DSPMADD_REG, dsp_addr);
35 dev_err(aw_dev->dev, "%s write addr error, ret=%d", __func__, ret);
39 ret = regmap_write(aw_dev->regmap, AW88399_DSPMDAT_REG, (u16)dsp_data);
41 dev_err(aw_dev->dev, "%s write data error, ret=%d", __func__, ret);
48 static int aw_dev_dsp_read_16bit(struct aw_device *aw_dev,
49 unsigned short dsp_addr, unsigned int *dsp_data)
51 unsigned int temp_data;
54 ret = regmap_write(aw_dev->regmap, AW88399_DSPMADD_REG, dsp_addr);
56 dev_err(aw_dev->dev, "%s write error, ret=%d", __func__, ret);
60 ret = regmap_read(aw_dev->regmap, AW88399_DSPMDAT_REG, &temp_data);
62 dev_err(aw_dev->dev, "%s read error, ret=%d", __func__, ret);
65 *dsp_data = temp_data;
70 static int aw_dev_dsp_read_32bit(struct aw_device *aw_dev,
71 unsigned short dsp_addr, unsigned int *dsp_data)
73 unsigned int temp_data;
76 ret = regmap_write(aw_dev->regmap, AW88399_DSPMADD_REG, dsp_addr);
78 dev_err(aw_dev->dev, "%s write error, ret=%d", __func__, ret);
82 ret = regmap_read(aw_dev->regmap, AW88399_DSPMDAT_REG, &temp_data);
84 dev_err(aw_dev->dev, "%s read error, ret=%d", __func__, ret);
87 *dsp_data = temp_data;
89 ret = regmap_read(aw_dev->regmap, AW88399_DSPMDAT_REG, &temp_data);
91 dev_err(aw_dev->dev, "%s read error, ret=%d", __func__, ret);
94 *dsp_data |= (temp_data << 16);
99 static int aw_dev_dsp_read(struct aw_device *aw_dev,
100 unsigned short dsp_addr, unsigned int *dsp_data, unsigned char data_type)
105 mutex_lock(&aw_dev->dsp_lock);
107 case AW88399_DSP_16_DATA:
108 ret = aw_dev_dsp_read_16bit(aw_dev, dsp_addr, dsp_data);
110 dev_err(aw_dev->dev, "read dsp_addr[0x%x] 16-bit dsp_data[0x%x] failed",
111 (u32)dsp_addr, *dsp_data);
113 case AW88399_DSP_32_DATA:
114 ret = aw_dev_dsp_read_32bit(aw_dev, dsp_addr, dsp_data);
116 dev_err(aw_dev->dev, "read dsp_addr[0x%x] 32r-bit dsp_data[0x%x] failed",
117 (u32)dsp_addr, *dsp_data);
120 dev_err(aw_dev->dev, "data type[%d] unsupported", data_type);
125 /* clear dsp chip select state */
126 if (regmap_read(aw_dev->regmap, AW88399_ID_REG, ®_value))
127 dev_err(aw_dev->dev, "%s fail to clear chip state. ret=%d\n", __func__, ret);
128 mutex_unlock(&aw_dev->dsp_lock);
133 static void aw_dev_pwd(struct aw_device *aw_dev, bool pwd)
138 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
139 ~AW88399_PWDN_MASK, AW88399_PWDN_POWER_DOWN_VALUE);
141 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
142 ~AW88399_PWDN_MASK, AW88399_PWDN_WORKING_VALUE);
145 dev_dbg(aw_dev->dev, "%s failed", __func__);
148 static void aw_dev_get_int_status(struct aw_device *aw_dev, unsigned short *int_status)
150 unsigned int reg_val;
153 ret = regmap_read(aw_dev->regmap, AW88399_SYSINT_REG, ®_val);
155 dev_err(aw_dev->dev, "read interrupt reg fail, ret=%d", ret);
157 *int_status = reg_val;
159 dev_dbg(aw_dev->dev, "read interrupt reg=0x%04x", *int_status);
162 static void aw_dev_clear_int_status(struct aw_device *aw_dev)
166 /* read int status and clear */
167 aw_dev_get_int_status(aw_dev, &int_status);
168 /* make sure int status is clear */
169 aw_dev_get_int_status(aw_dev, &int_status);
171 dev_dbg(aw_dev->dev, "int status(%d) is not cleaned.\n", int_status);
174 static int aw_dev_get_iis_status(struct aw_device *aw_dev)
176 unsigned int reg_val;
179 ret = regmap_read(aw_dev->regmap, AW88399_SYSST_REG, ®_val);
182 if ((reg_val & AW88399_BIT_PLL_CHECK) != AW88399_BIT_PLL_CHECK) {
183 dev_err(aw_dev->dev, "check pll lock fail, reg_val:0x%04x", reg_val);
190 static int aw_dev_check_mode1_pll(struct aw_device *aw_dev)
194 for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) {
195 ret = aw_dev_get_iis_status(aw_dev);
197 dev_err(aw_dev->dev, "mode1 iis signal check error");
198 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
207 static int aw_dev_check_mode2_pll(struct aw_device *aw_dev)
209 unsigned int reg_val;
212 ret = regmap_read(aw_dev->regmap, AW88399_PLLCTRL2_REG, ®_val);
216 reg_val &= (~AW88399_CCO_MUX_MASK);
217 if (reg_val == AW88399_CCO_MUX_DIVIDED_VALUE) {
218 dev_dbg(aw_dev->dev, "CCO_MUX is already divider");
223 ret = regmap_update_bits(aw_dev->regmap, AW88399_PLLCTRL2_REG,
224 ~AW88399_CCO_MUX_MASK, AW88399_CCO_MUX_DIVIDED_VALUE);
228 for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) {
229 ret = aw_dev_get_iis_status(aw_dev);
231 dev_err(aw_dev->dev, "mode2 iis signal check error");
232 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
239 regmap_update_bits(aw_dev->regmap, AW88399_PLLCTRL2_REG,
240 ~AW88399_CCO_MUX_MASK, AW88399_CCO_MUX_BYPASS_VALUE);
242 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
243 for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) {
244 ret = aw_dev_get_iis_status(aw_dev);
246 dev_err(aw_dev->dev, "mode2 switch to mode1, iis signal check error");
247 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
257 static int aw_dev_check_syspll(struct aw_device *aw_dev)
261 ret = aw_dev_check_mode1_pll(aw_dev);
263 dev_dbg(aw_dev->dev, "mode1 check iis failed try switch to mode2 check");
264 ret = aw_dev_check_mode2_pll(aw_dev);
266 dev_err(aw_dev->dev, "mode2 check iis failed");
274 static int aw_dev_check_sysst(struct aw_device *aw_dev)
276 unsigned int check_val;
277 unsigned int reg_val;
280 ret = regmap_read(aw_dev->regmap, AW88399_PWMCTRL3_REG, ®_val);
284 if (reg_val & (~AW88399_NOISE_GATE_EN_MASK))
285 check_val = AW88399_BIT_SYSST_NOSWS_CHECK;
287 check_val = AW88399_BIT_SYSST_SWS_CHECK;
289 for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) {
290 ret = regmap_read(aw_dev->regmap, AW88399_SYSST_REG, ®_val);
294 if ((reg_val & (~AW88399_BIT_SYSST_CHECK_MASK) & check_val) != check_val) {
295 dev_err(aw_dev->dev, "check sysst fail, cnt=%d, reg_val=0x%04x, check:0x%x",
296 i, reg_val, AW88399_BIT_SYSST_NOSWS_CHECK);
297 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
306 static void aw_dev_amppd(struct aw_device *aw_dev, bool amppd)
311 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
312 ~AW88399_AMPPD_MASK, AW88399_AMPPD_POWER_DOWN_VALUE);
314 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
315 ~AW88399_AMPPD_MASK, AW88399_AMPPD_WORKING_VALUE);
318 dev_dbg(aw_dev->dev, "%s failed", __func__);
321 static void aw_dev_dsp_enable(struct aw_device *aw_dev, bool is_enable)
326 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
327 ~AW88399_DSPBY_MASK, AW88399_DSPBY_WORKING_VALUE);
329 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
330 ~AW88399_DSPBY_MASK, AW88399_DSPBY_BYPASS_VALUE);
333 dev_dbg(aw_dev->dev, "%s failed\n", __func__);
336 static int aw88399_dev_get_icalk(struct aw88399 *aw88399, int16_t *icalk)
338 uint16_t icalkh_val, icalkl_val, icalk_val;
339 struct aw_device *aw_dev = aw88399->aw_pa;
340 unsigned int reg_val;
343 ret = regmap_read(aw_dev->regmap, AW88399_EFRH4_REG, ®_val);
346 icalkh_val = reg_val & (~AW88399_EF_ISN_GESLP_H_MASK);
348 ret = regmap_read(aw_dev->regmap, AW88399_EFRL4_REG, ®_val);
351 icalkl_val = reg_val & (~AW88399_EF_ISN_GESLP_L_MASK);
353 if (aw88399->check_val == AW_EF_AND_CHECK)
354 icalk_val = icalkh_val & icalkl_val;
356 icalk_val = icalkh_val | icalkl_val;
358 if (icalk_val & (~AW88399_EF_ISN_GESLP_SIGN_MASK))
359 icalk_val = icalk_val | AW88399_EF_ISN_GESLP_SIGN_NEG;
360 *icalk = (int16_t)icalk_val;
365 static int aw88399_dev_get_vcalk(struct aw88399 *aw88399, int16_t *vcalk)
367 uint16_t vcalkh_val, vcalkl_val, vcalk_val;
368 struct aw_device *aw_dev = aw88399->aw_pa;
369 unsigned int reg_val;
372 ret = regmap_read(aw_dev->regmap, AW88399_EFRH3_REG, ®_val);
376 vcalkh_val = reg_val & (~AW88399_EF_VSN_GESLP_H_MASK);
378 ret = regmap_read(aw_dev->regmap, AW88399_EFRL3_REG, ®_val);
382 vcalkl_val = reg_val & (~AW88399_EF_VSN_GESLP_L_MASK);
384 if (aw88399->check_val == AW_EF_AND_CHECK)
385 vcalk_val = vcalkh_val & vcalkl_val;
387 vcalk_val = vcalkh_val | vcalkl_val;
389 if (vcalk_val & AW88399_EF_VSN_GESLP_SIGN_MASK)
390 vcalk_val = vcalk_val | AW88399_EF_VSN_GESLP_SIGN_NEG;
391 *vcalk = (int16_t)vcalk_val;
396 static int aw88399_dev_get_internal_vcalk(struct aw88399 *aw88399, int16_t *vcalk)
398 uint16_t vcalkh_val, vcalkl_val, vcalk_val;
399 struct aw_device *aw_dev = aw88399->aw_pa;
400 unsigned int reg_val;
403 ret = regmap_read(aw_dev->regmap, AW88399_EFRH2_REG, ®_val);
406 vcalkh_val = reg_val & (~AW88399_INTERNAL_VSN_TRIM_H_MASK);
408 ret = regmap_read(aw_dev->regmap, AW88399_EFRL2_REG, ®_val);
411 vcalkl_val = reg_val & (~AW88399_INTERNAL_VSN_TRIM_L_MASK);
413 if (aw88399->check_val == AW_EF_AND_CHECK)
414 vcalk_val = (vcalkh_val >> AW88399_INTERNAL_VSN_TRIM_H_START_BIT) &
415 (vcalkl_val >> AW88399_INTERNAL_VSN_TRIM_L_START_BIT);
417 vcalk_val = (vcalkh_val >> AW88399_INTERNAL_VSN_TRIM_H_START_BIT) |
418 (vcalkl_val >> AW88399_INTERNAL_VSN_TRIM_L_START_BIT);
420 if (vcalk_val & (~AW88399_TEM4_SIGN_MASK))
421 vcalk_val = vcalk_val | AW88399_TEM4_SIGN_NEG;
423 *vcalk = (int16_t)vcalk_val;
428 static int aw_dev_set_vcalb(struct aw88399 *aw88399)
430 struct aw_device *aw_dev = aw88399->aw_pa;
431 unsigned int vsense_select, vsense_value;
432 int32_t ical_k, vcal_k, vcalb;
433 int16_t icalk, vcalk;
437 ret = regmap_read(aw_dev->regmap, AW88399_VSNCTRL1_REG, &vsense_value);
441 vsense_select = vsense_select & (~AW88399_VDSEL_MASK);
443 ret = aw88399_dev_get_icalk(aw88399, &icalk);
445 dev_err(aw_dev->dev, "get icalk failed\n");
449 ical_k = icalk * AW88399_ICABLK_FACTOR + AW88399_CABL_BASE_VALUE;
451 switch (vsense_select) {
452 case AW88399_DEV_VDSEL_VSENSE:
453 ret = aw88399_dev_get_vcalk(aw88399, &vcalk);
454 vcal_k = vcalk * AW88399_VCABLK_FACTOR + AW88399_CABL_BASE_VALUE;
455 vcalb = AW88399_VCALB_ACCURACY * AW88399_VSCAL_FACTOR / AW88399_ISCAL_FACTOR /
456 ical_k / vcal_k * aw88399->vcalb_init_val;
458 case AW88399_DEV_VDSEL_DAC:
459 ret = aw88399_dev_get_internal_vcalk(aw88399, &vcalk);
460 vcal_k = vcalk * AW88399_VCABLK_DAC_FACTOR + AW88399_CABL_BASE_VALUE;
461 vcalb = AW88399_VCALB_ACCURACY * AW88399_VSCAL_DAC_FACTOR /
462 AW88399_ISCAL_DAC_FACTOR / ical_k /
463 vcal_k * aw88399->vcalb_init_val;
466 dev_err(aw_dev->dev, "%s: unsupport vsense\n", __func__);
473 vcalb = vcalb >> AW88399_VCALB_ADJ_FACTOR;
474 reg_val = (uint32_t)vcalb;
476 regmap_write(aw_dev->regmap, AW88399_DSPVCALB_REG, reg_val);
481 static int aw_dev_update_cali_re(struct aw_cali_desc *cali_desc)
483 struct aw_device *aw_dev =
484 container_of(cali_desc, struct aw_device, cali_desc);
485 uint16_t re_lbits, re_hbits;
489 if ((aw_dev->cali_desc.cali_re <= AW88399_CALI_RE_MAX) ||
490 (aw_dev->cali_desc.cali_re >= AW88399_CALI_RE_MIN))
493 cali_re = AW88399_SHOW_RE_TO_DSP_RE((aw_dev->cali_desc.cali_re +
494 aw_dev->cali_desc.ra), AW88399_DSP_RE_SHIFT);
496 re_hbits = (cali_re & (~AW88399_CALI_RE_HBITS_MASK)) >> AW88399_CALI_RE_HBITS_SHIFT;
497 re_lbits = (cali_re & (~AW88399_CALI_RE_LBITS_MASK)) >> AW88399_CALI_RE_LBITS_SHIFT;
499 ret = regmap_write(aw_dev->regmap, AW88399_ACR1_REG, re_hbits);
501 dev_err(aw_dev->dev, "set cali re error");
505 ret = regmap_write(aw_dev->regmap, AW88399_ACR2_REG, re_lbits);
507 dev_err(aw_dev->dev, "set cali re error");
512 static int aw_dev_fw_crc_check(struct aw_device *aw_dev)
514 uint16_t check_val, fw_len_val;
515 unsigned int reg_val;
518 /* calculate fw_end_addr */
519 fw_len_val = ((aw_dev->dsp_fw_len / AW_FW_ADDR_LEN) - 1) + AW88399_CRC_FW_BASE_ADDR;
521 /* write fw_end_addr to crc_end_addr */
522 ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG,
523 ~AW88399_CRC_END_ADDR_MASK, fw_len_val);
526 /* enable fw crc check */
527 ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG,
528 ~AW88399_CRC_CODE_EN_MASK, AW88399_CRC_CODE_EN_ENABLE_VALUE);
530 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
532 /* read crc check result */
533 regmap_read(aw_dev->regmap, AW88399_HAGCST_REG, ®_val);
537 check_val = (reg_val & (~AW88399_CRC_CHECK_BITS_MASK)) >> AW88399_CRC_CHECK_START_BIT;
539 /* disable fw crc check */
540 ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG,
541 ~AW88399_CRC_CODE_EN_MASK, AW88399_CRC_CODE_EN_DISABLE_VALUE);
545 if (check_val != AW88399_CRC_CHECK_PASS_VAL) {
546 dev_err(aw_dev->dev, "%s failed, check_val 0x%x != 0x%x",
547 __func__, check_val, AW88399_CRC_CHECK_PASS_VAL);
554 static int aw_dev_cfg_crc_check(struct aw_device *aw_dev)
556 uint16_t check_val, cfg_len_val;
557 unsigned int reg_val;
560 /* calculate cfg end addr */
561 cfg_len_val = ((aw_dev->dsp_cfg_len / AW_FW_ADDR_LEN) - 1) + AW88399_CRC_CFG_BASE_ADDR;
563 /* write cfg_end_addr to crc_end_addr */
564 ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG,
565 ~AW88399_CRC_END_ADDR_MASK, cfg_len_val);
569 /* enable cfg crc check */
570 ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG,
571 ~AW88399_CRC_CFG_EN_MASK, AW88399_CRC_CFG_EN_ENABLE_VALUE);
575 usleep_range(AW88399_1000_US, AW88399_1000_US + 10);
577 /* read crc check result */
578 ret = regmap_read(aw_dev->regmap, AW88399_HAGCST_REG, ®_val);
582 check_val = (reg_val & (~AW88399_CRC_CHECK_BITS_MASK)) >> AW88399_CRC_CHECK_START_BIT;
584 /* disable cfg crc check */
585 ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG,
586 ~AW88399_CRC_CFG_EN_MASK, AW88399_CRC_CFG_EN_DISABLE_VALUE);
590 if (check_val != AW88399_CRC_CHECK_PASS_VAL) {
591 dev_err(aw_dev->dev, "crc_check failed, check val 0x%x != 0x%x",
592 check_val, AW88399_CRC_CHECK_PASS_VAL);
599 static int aw_dev_hw_crc_check(struct aw88399 *aw88399)
601 struct aw_device *aw_dev = aw88399->aw_pa;
604 ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG,
605 ~AW88399_RAM_CG_BYP_MASK, AW88399_RAM_CG_BYP_BYPASS_VALUE);
609 ret = aw_dev_fw_crc_check(aw_dev);
611 dev_err(aw_dev->dev, "fw_crc_check failed\n");
612 goto crc_check_failed;
615 ret = aw_dev_cfg_crc_check(aw_dev);
617 dev_err(aw_dev->dev, "cfg_crc_check failed\n");
618 goto crc_check_failed;
621 ret = regmap_write(aw_dev->regmap, AW88399_CRCCTRL_REG, aw88399->crc_init_val);
625 ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG,
626 ~AW88399_RAM_CG_BYP_MASK, AW88399_RAM_CG_BYP_WORK_VALUE);
631 regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG,
632 ~AW88399_RAM_CG_BYP_MASK, AW88399_RAM_CG_BYP_WORK_VALUE);
636 static void aw_dev_i2s_tx_enable(struct aw_device *aw_dev, bool flag)
641 ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCTRL3_REG,
642 ~AW88399_I2STXEN_MASK, AW88399_I2STXEN_ENABLE_VALUE);
644 ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG,
645 ~AW88399_I2STXEN_MASK, AW88399_I2STXEN_DISABLE_VALUE);
648 dev_dbg(aw_dev->dev, "%s failed", __func__);
651 static int aw_dev_get_dsp_status(struct aw_device *aw_dev)
653 unsigned int reg_val;
656 ret = regmap_read(aw_dev->regmap, AW88399_WDT_REG, ®_val);
659 if (!(reg_val & (~AW88399_WDT_CNT_MASK)))
665 static int aw_dev_dsp_check(struct aw_device *aw_dev)
669 switch (aw_dev->dsp_cfg) {
670 case AW88399_DEV_DSP_BYPASS:
671 dev_dbg(aw_dev->dev, "dsp bypass");
674 case AW88399_DEV_DSP_WORK:
675 aw_dev_dsp_enable(aw_dev, false);
676 aw_dev_dsp_enable(aw_dev, true);
677 usleep_range(AW88399_1000_US, AW88399_1000_US + 10);
678 for (i = 0; i < AW88399_DEV_DSP_CHECK_MAX; i++) {
679 ret = aw_dev_get_dsp_status(aw_dev);
681 dev_err(aw_dev->dev, "dsp wdt status error=%d", ret);
682 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
687 dev_err(aw_dev->dev, "unknown dsp cfg=%d", aw_dev->dsp_cfg);
695 static int aw_dev_set_volume(struct aw_device *aw_dev, unsigned int value)
697 struct aw_volume_desc *vol_desc = &aw_dev->volume_desc;
698 unsigned int reg_value;
702 real_value = min((value + vol_desc->init_volume), (unsigned int)AW88399_MUTE_VOL);
704 ret = regmap_read(aw_dev->regmap, AW88399_SYSCTRL2_REG, ®_value);
708 dev_dbg(aw_dev->dev, "value 0x%x , reg:0x%x", value, real_value);
710 real_value = (real_value << AW88399_VOL_START_BIT) | (reg_value & AW88399_VOL_MASK);
712 ret = regmap_write(aw_dev->regmap, AW88399_SYSCTRL2_REG, real_value);
717 static void aw_dev_fade_in(struct aw_device *aw_dev)
719 struct aw_volume_desc *desc = &aw_dev->volume_desc;
720 u16 fade_in_vol = desc->ctl_volume;
721 int fade_step = aw_dev->fade_step;
724 if (fade_step == 0 || aw_dev->fade_in_time == 0) {
725 aw_dev_set_volume(aw_dev, fade_in_vol);
729 for (i = AW88399_MUTE_VOL; i >= fade_in_vol; i -= fade_step) {
730 aw_dev_set_volume(aw_dev, i);
731 usleep_range(aw_dev->fade_in_time, aw_dev->fade_in_time + 10);
734 if (i != fade_in_vol)
735 aw_dev_set_volume(aw_dev, fade_in_vol);
738 static void aw_dev_fade_out(struct aw_device *aw_dev)
740 struct aw_volume_desc *desc = &aw_dev->volume_desc;
741 int fade_step = aw_dev->fade_step;
744 if (fade_step == 0 || aw_dev->fade_out_time == 0) {
745 aw_dev_set_volume(aw_dev, AW88399_MUTE_VOL);
749 for (i = desc->ctl_volume; i <= AW88399_MUTE_VOL; i += fade_step) {
750 aw_dev_set_volume(aw_dev, i);
751 usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10);
754 if (i != AW88399_MUTE_VOL) {
755 aw_dev_set_volume(aw_dev, AW88399_MUTE_VOL);
756 usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10);
760 static void aw88399_dev_mute(struct aw_device *aw_dev, bool is_mute)
763 aw_dev_fade_out(aw_dev);
764 regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
765 ~AW88399_HMUTE_MASK, AW88399_HMUTE_ENABLE_VALUE);
767 regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
768 ~AW88399_HMUTE_MASK, AW88399_HMUTE_DISABLE_VALUE);
769 aw_dev_fade_in(aw_dev);
773 static void aw88399_dev_set_dither(struct aw88399 *aw88399, bool dither)
775 struct aw_device *aw_dev = aw88399->aw_pa;
778 regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG,
779 ~AW88399_DITHER_EN_MASK, AW88399_DITHER_EN_ENABLE_VALUE);
781 regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG,
782 ~AW88399_DITHER_EN_MASK, AW88399_DITHER_EN_DISABLE_VALUE);
785 static int aw88399_dev_start(struct aw88399 *aw88399)
787 struct aw_device *aw_dev = aw88399->aw_pa;
790 if (aw_dev->status == AW88399_DEV_PW_ON) {
791 dev_dbg(aw_dev->dev, "already power on");
795 aw88399_dev_set_dither(aw88399, false);
798 aw_dev_pwd(aw_dev, false);
799 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
801 ret = aw_dev_check_syspll(aw_dev);
803 dev_err(aw_dev->dev, "pll check failed cannot start");
808 aw_dev_amppd(aw_dev, false);
809 usleep_range(AW88399_1000_US, AW88399_1000_US + 50);
811 /* check i2s status */
812 ret = aw_dev_check_sysst(aw_dev);
814 dev_err(aw_dev->dev, "sysst check failed");
815 goto sysst_check_fail;
818 if (aw_dev->dsp_cfg == AW88399_DEV_DSP_WORK) {
819 ret = aw_dev_hw_crc_check(aw88399);
821 dev_err(aw_dev->dev, "dsp crc check failed");
824 aw_dev_dsp_enable(aw_dev, false);
825 aw_dev_set_vcalb(aw88399);
826 aw_dev_update_cali_re(&aw_dev->cali_desc);
828 ret = aw_dev_dsp_check(aw_dev);
830 dev_err(aw_dev->dev, "dsp status check failed");
834 dev_dbg(aw_dev->dev, "start pa with dsp bypass");
837 /* enable tx feedback */
838 aw_dev_i2s_tx_enable(aw_dev, true);
840 if (aw88399->dither_st == AW88399_DITHER_EN_ENABLE_VALUE)
841 aw88399_dev_set_dither(aw88399, true);
844 aw88399_dev_mute(aw_dev, false);
845 /* clear inturrupt */
846 aw_dev_clear_int_status(aw_dev);
847 aw_dev->status = AW88399_DEV_PW_ON;
853 aw_dev_dsp_enable(aw_dev, false);
855 aw_dev_clear_int_status(aw_dev);
856 aw_dev_amppd(aw_dev, true);
858 aw_dev_pwd(aw_dev, true);
859 aw_dev->status = AW88399_DEV_PW_OFF;
864 static int aw_dev_dsp_update_container(struct aw_device *aw_dev,
865 unsigned char *data, unsigned int len, unsigned short base)
870 mutex_lock(&aw_dev->dsp_lock);
871 ret = regmap_write(aw_dev->regmap, AW88399_DSPMADD_REG, base);
873 goto error_operation;
875 for (i = 0; i < len; i += AW88399_MAX_RAM_WRITE_BYTE_SIZE) {
876 if ((len - i) < AW88399_MAX_RAM_WRITE_BYTE_SIZE)
879 tmp_len = AW88399_MAX_RAM_WRITE_BYTE_SIZE;
881 ret = regmap_raw_write(aw_dev->regmap, AW88399_DSPMDAT_REG,
884 goto error_operation;
886 mutex_unlock(&aw_dev->dsp_lock);
891 mutex_unlock(&aw_dev->dsp_lock);
895 static int aw_dev_get_ra(struct aw_cali_desc *cali_desc)
897 struct aw_device *aw_dev =
898 container_of(cali_desc, struct aw_device, cali_desc);
902 ret = aw_dev_dsp_read(aw_dev, AW88399_DSP_REG_CFG_ADPZ_RA,
903 &dsp_ra, AW88399_DSP_32_DATA);
905 dev_err(aw_dev->dev, "read ra error");
909 cali_desc->ra = AW88399_DSP_RE_TO_SHOW_RE(dsp_ra,
910 AW88399_DSP_RE_SHIFT);
915 static int aw_dev_dsp_update_cfg(struct aw_device *aw_dev,
916 unsigned char *data, unsigned int len)
920 dev_dbg(aw_dev->dev, "dsp config len:%d", len);
923 dev_err(aw_dev->dev, "dsp config data is null or len is 0");
927 ret = aw_dev_dsp_update_container(aw_dev, data, len, AW88399_DSP_CFG_ADDR);
931 aw_dev->dsp_cfg_len = len;
933 ret = aw_dev_get_ra(&aw_dev->cali_desc);
938 static int aw_dev_dsp_update_fw(struct aw_device *aw_dev,
939 unsigned char *data, unsigned int len)
943 dev_dbg(aw_dev->dev, "dsp firmware len:%d", len);
946 dev_err(aw_dev->dev, "dsp firmware data is null or len is 0");
950 aw_dev->dsp_fw_len = len;
951 ret = aw_dev_dsp_update_container(aw_dev, data, len, AW88399_DSP_FW_ADDR);
956 static int aw_dev_check_sram(struct aw_device *aw_dev)
958 unsigned int reg_val;
960 mutex_lock(&aw_dev->dsp_lock);
961 /* read dsp_rom_check_reg */
962 aw_dev_dsp_read_16bit(aw_dev, AW88399_DSP_ROM_CHECK_ADDR, ®_val);
963 if (reg_val != AW88399_DSP_ROM_CHECK_DATA) {
964 dev_err(aw_dev->dev, "check dsp rom failed, read[0x%x] != check[0x%x]",
965 reg_val, AW88399_DSP_ROM_CHECK_DATA);
969 /* check dsp_cfg_base_addr */
970 aw_dev_dsp_write_16bit(aw_dev, AW88399_DSP_CFG_ADDR, AW88399_DSP_ODD_NUM_BIT_TEST);
971 aw_dev_dsp_read_16bit(aw_dev, AW88399_DSP_CFG_ADDR, ®_val);
972 if (reg_val != AW88399_DSP_ODD_NUM_BIT_TEST) {
973 dev_err(aw_dev->dev, "check dsp cfg failed, read[0x%x] != write[0x%x]",
974 reg_val, AW88399_DSP_ODD_NUM_BIT_TEST);
977 mutex_unlock(&aw_dev->dsp_lock);
981 mutex_unlock(&aw_dev->dsp_lock);
985 static void aw_dev_select_memclk(struct aw_device *aw_dev, unsigned char flag)
990 case AW88399_DEV_MEMCLK_PLL:
991 ret = regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG,
992 ~AW88399_MEM_CLKSEL_MASK,
993 AW88399_MEM_CLKSEL_DAPHCLK_VALUE);
995 dev_err(aw_dev->dev, "memclk select pll failed");
997 case AW88399_DEV_MEMCLK_OSC:
998 ret = regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG,
999 ~AW88399_MEM_CLKSEL_MASK,
1000 AW88399_MEM_CLKSEL_OSCCLK_VALUE);
1002 dev_err(aw_dev->dev, "memclk select OSC failed");
1005 dev_err(aw_dev->dev, "unknown memclk config, flag=0x%x", flag);
1010 static void aw_dev_get_cur_mode_st(struct aw_device *aw_dev)
1012 struct aw_profctrl_desc *profctrl_desc = &aw_dev->profctrl_desc;
1013 unsigned int reg_val;
1016 ret = regmap_read(aw_dev->regmap, AW88399_SYSCTRL_REG, ®_val);
1018 dev_dbg(aw_dev->dev, "%s failed", __func__);
1021 if ((reg_val & (~AW88399_RCV_MODE_MASK)) == AW88399_RCV_MODE_RECEIVER_VALUE)
1022 profctrl_desc->cur_mode = AW88399_RCV_MODE;
1024 profctrl_desc->cur_mode = AW88399_NOT_RCV_MODE;
1027 static int aw_dev_update_reg_container(struct aw88399 *aw88399,
1028 unsigned char *data, unsigned int len)
1030 struct aw_device *aw_dev = aw88399->aw_pa;
1031 struct aw_volume_desc *vol_desc = &aw_dev->volume_desc;
1032 u16 read_vol, reg_val;
1033 int data_len, i, ret;
1037 reg_data = (int16_t *)data;
1038 data_len = len >> 1;
1040 if (data_len & 0x1) {
1041 dev_err(aw_dev->dev, "data len:%d unsupported", data_len);
1045 for (i = 0; i < data_len; i += 2) {
1046 reg_addr = reg_data[i];
1047 reg_val = reg_data[i + 1];
1049 if (reg_addr == AW88399_DSPVCALB_REG) {
1050 aw88399->vcalb_init_val = reg_val;
1054 if (reg_addr == AW88399_SYSCTRL_REG) {
1055 if (reg_val & (~AW88399_DSPBY_MASK))
1056 aw_dev->dsp_cfg = AW88399_DEV_DSP_BYPASS;
1058 aw_dev->dsp_cfg = AW88399_DEV_DSP_WORK;
1060 reg_val &= (AW88399_HMUTE_MASK | AW88399_PWDN_MASK |
1061 AW88399_DSPBY_MASK);
1062 reg_val |= (AW88399_HMUTE_ENABLE_VALUE | AW88399_PWDN_POWER_DOWN_VALUE |
1063 AW88399_DSPBY_BYPASS_VALUE);
1066 if (reg_addr == AW88399_I2SCTRL3_REG) {
1067 reg_val &= AW88399_I2STXEN_MASK;
1068 reg_val |= AW88399_I2STXEN_DISABLE_VALUE;
1071 if (reg_addr == AW88399_SYSCTRL2_REG) {
1072 read_vol = (reg_val & (~AW88399_VOL_MASK)) >>
1073 AW88399_VOL_START_BIT;
1074 aw_dev->volume_desc.init_volume = read_vol;
1077 if (reg_addr == AW88399_DBGCTRL_REG) {
1078 if ((reg_val & (~AW88399_EF_DBMD_MASK)) == AW88399_EF_DBMD_OR_VALUE)
1079 aw88399->check_val = AW_EF_OR_CHECK;
1081 aw88399->check_val = AW_EF_AND_CHECK;
1083 aw88399->dither_st = reg_val & (~AW88399_DITHER_EN_MASK);
1086 if (reg_addr == AW88399_CRCCTRL_REG)
1087 aw88399->crc_init_val = reg_val;
1089 ret = regmap_write(aw_dev->regmap, reg_addr, reg_val);
1094 aw_dev_pwd(aw_dev, false);
1095 usleep_range(AW88399_1000_US, AW88399_1000_US + 10);
1097 aw_dev_get_cur_mode_st(aw_dev);
1099 if (aw_dev->prof_cur != aw_dev->prof_index)
1100 vol_desc->ctl_volume = 0;
1102 aw_dev_set_volume(aw_dev, vol_desc->ctl_volume);
1107 static int aw_dev_reg_update(struct aw88399 *aw88399,
1108 unsigned char *data, unsigned int len)
1112 if (!len || !data) {
1113 dev_err(aw88399->aw_pa->dev, "reg data is null or len is 0");
1117 ret = aw_dev_update_reg_container(aw88399, data, len);
1119 dev_err(aw88399->aw_pa->dev, "reg update failed");
1124 static int aw88399_dev_get_prof_name(struct aw_device *aw_dev, int index, char **prof_name)
1126 struct aw_prof_info *prof_info = &aw_dev->prof_info;
1127 struct aw_prof_desc *prof_desc;
1129 if ((index >= aw_dev->prof_info.count) || (index < 0)) {
1130 dev_err(aw_dev->dev, "index[%d] overflow count[%d]",
1131 index, aw_dev->prof_info.count);
1135 prof_desc = &aw_dev->prof_info.prof_desc[index];
1137 *prof_name = prof_info->prof_name_list[prof_desc->id];
1142 static int aw88399_dev_get_prof_data(struct aw_device *aw_dev, int index,
1143 struct aw_prof_desc **prof_desc)
1145 if ((index >= aw_dev->prof_info.count) || (index < 0)) {
1146 dev_err(aw_dev->dev, "%s: index[%d] overflow count[%d]\n",
1147 __func__, index, aw_dev->prof_info.count);
1151 *prof_desc = &aw_dev->prof_info.prof_desc[index];
1156 static int aw88399_dev_fw_update(struct aw88399 *aw88399, bool up_dsp_fw_en, bool force_up_en)
1158 struct aw_device *aw_dev = aw88399->aw_pa;
1159 struct aw_prof_desc *prof_index_desc;
1160 struct aw_sec_data_desc *sec_desc;
1164 if ((aw_dev->prof_cur == aw_dev->prof_index) &&
1165 (force_up_en == AW88399_FORCE_UPDATE_OFF)) {
1166 dev_dbg(aw_dev->dev, "scene no change, not update");
1170 if (aw_dev->fw_status == AW88399_DEV_FW_FAILED) {
1171 dev_err(aw_dev->dev, "fw status[%d] error", aw_dev->fw_status);
1175 ret = aw88399_dev_get_prof_name(aw_dev, aw_dev->prof_index, &prof_name);
1179 dev_dbg(aw_dev->dev, "start update %s", prof_name);
1181 ret = aw88399_dev_get_prof_data(aw_dev, aw_dev->prof_index, &prof_index_desc);
1186 sec_desc = prof_index_desc->sec_desc;
1187 ret = aw_dev_reg_update(aw88399, sec_desc[AW88395_DATA_TYPE_REG].data,
1188 sec_desc[AW88395_DATA_TYPE_REG].len);
1190 dev_err(aw_dev->dev, "update reg failed");
1194 aw88399_dev_mute(aw_dev, true);
1196 if (aw_dev->dsp_cfg == AW88399_DEV_DSP_WORK)
1197 aw_dev_dsp_enable(aw_dev, false);
1199 aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_OSC);
1201 ret = aw_dev_check_sram(aw_dev);
1203 dev_err(aw_dev->dev, "check sram failed");
1208 dev_dbg(aw_dev->dev, "fw_ver: [%x]", prof_index_desc->fw_ver);
1209 ret = aw_dev_dsp_update_fw(aw_dev, sec_desc[AW88395_DATA_TYPE_DSP_FW].data,
1210 sec_desc[AW88395_DATA_TYPE_DSP_FW].len);
1212 dev_err(aw_dev->dev, "update dsp fw failed");
1217 /* update dsp config */
1218 ret = aw_dev_dsp_update_cfg(aw_dev, sec_desc[AW88395_DATA_TYPE_DSP_CFG].data,
1219 sec_desc[AW88395_DATA_TYPE_DSP_CFG].len);
1221 dev_err(aw_dev->dev, "update dsp cfg failed");
1225 aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_PLL);
1227 aw_dev->prof_cur = aw_dev->prof_index;
1232 aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_PLL);
1236 static void aw88399_start_pa(struct aw88399 *aw88399)
1240 for (i = 0; i < AW88399_START_RETRIES; i++) {
1241 ret = aw88399_dev_start(aw88399);
1243 dev_err(aw88399->aw_pa->dev, "aw88399 device start failed. retry = %d", i);
1244 ret = aw88399_dev_fw_update(aw88399, AW88399_DSP_FW_UPDATE_ON, true);
1246 dev_err(aw88399->aw_pa->dev, "fw update failed");
1250 dev_dbg(aw88399->aw_pa->dev, "start success\n");
1256 static void aw88399_startup_work(struct work_struct *work)
1258 struct aw88399 *aw88399 =
1259 container_of(work, struct aw88399, start_work.work);
1261 mutex_lock(&aw88399->lock);
1262 aw88399_start_pa(aw88399);
1263 mutex_unlock(&aw88399->lock);
1266 static void aw88399_start(struct aw88399 *aw88399, bool sync_start)
1270 if (aw88399->aw_pa->fw_status != AW88399_DEV_FW_OK)
1273 if (aw88399->aw_pa->status == AW88399_DEV_PW_ON)
1276 ret = aw88399_dev_fw_update(aw88399, AW88399_DSP_FW_UPDATE_OFF, true);
1278 dev_err(aw88399->aw_pa->dev, "fw update failed.");
1282 if (sync_start == AW88399_SYNC_START)
1283 aw88399_start_pa(aw88399);
1285 queue_delayed_work(system_wq,
1286 &aw88399->start_work,
1287 AW88399_START_WORK_DELAY_MS);
1290 static int aw_dev_check_sysint(struct aw_device *aw_dev)
1294 aw_dev_get_int_status(aw_dev, ®_val);
1295 if (reg_val & AW88399_BIT_SYSINT_CHECK) {
1296 dev_err(aw_dev->dev, "pa stop check fail:0x%04x", reg_val);
1303 static int aw88399_stop(struct aw_device *aw_dev)
1305 struct aw_sec_data_desc *dsp_cfg =
1306 &aw_dev->prof_info.prof_desc[aw_dev->prof_cur].sec_desc[AW88395_DATA_TYPE_DSP_CFG];
1307 struct aw_sec_data_desc *dsp_fw =
1308 &aw_dev->prof_info.prof_desc[aw_dev->prof_cur].sec_desc[AW88395_DATA_TYPE_DSP_FW];
1311 if (aw_dev->status == AW88399_DEV_PW_OFF) {
1312 dev_dbg(aw_dev->dev, "already power off");
1316 aw_dev->status = AW88399_DEV_PW_OFF;
1318 aw88399_dev_mute(aw_dev, true);
1319 usleep_range(AW88399_4000_US, AW88399_4000_US + 100);
1321 aw_dev_i2s_tx_enable(aw_dev, false);
1322 usleep_range(AW88399_1000_US, AW88399_1000_US + 100);
1324 int_st = aw_dev_check_sysint(aw_dev);
1326 aw_dev_dsp_enable(aw_dev, false);
1328 aw_dev_amppd(aw_dev, true);
1331 aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_OSC);
1332 aw_dev_dsp_update_fw(aw_dev, dsp_fw->data, dsp_fw->len);
1333 aw_dev_dsp_update_cfg(aw_dev, dsp_cfg->data, dsp_cfg->len);
1334 aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_PLL);
1337 aw_dev_pwd(aw_dev, true);
1342 static struct snd_soc_dai_driver aw88399_dai[] = {
1344 .name = "aw88399-aif",
1347 .stream_name = "Speaker_Playback",
1350 .rates = AW88399_RATES,
1351 .formats = AW88399_FORMATS,
1354 .stream_name = "Speaker_Capture",
1357 .rates = AW88399_RATES,
1358 .formats = AW88399_FORMATS,
1363 static int aw88399_get_fade_in_time(struct snd_kcontrol *kcontrol,
1364 struct snd_ctl_elem_value *ucontrol)
1366 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1367 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component);
1368 struct aw_device *aw_dev = aw88399->aw_pa;
1370 ucontrol->value.integer.value[0] = aw_dev->fade_in_time;
1375 static int aw88399_set_fade_in_time(struct snd_kcontrol *kcontrol,
1376 struct snd_ctl_elem_value *ucontrol)
1378 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1379 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component);
1380 struct soc_mixer_control *mc =
1381 (struct soc_mixer_control *)kcontrol->private_value;
1382 struct aw_device *aw_dev = aw88399->aw_pa;
1385 time = ucontrol->value.integer.value[0];
1387 if (time < mc->min || time > mc->max)
1390 if (time != aw_dev->fade_in_time) {
1391 aw_dev->fade_in_time = time;
1398 static int aw88399_get_fade_out_time(struct snd_kcontrol *kcontrol,
1399 struct snd_ctl_elem_value *ucontrol)
1401 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1402 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component);
1403 struct aw_device *aw_dev = aw88399->aw_pa;
1405 ucontrol->value.integer.value[0] = aw_dev->fade_out_time;
1410 static int aw88399_set_fade_out_time(struct snd_kcontrol *kcontrol,
1411 struct snd_ctl_elem_value *ucontrol)
1413 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1414 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component);
1415 struct soc_mixer_control *mc =
1416 (struct soc_mixer_control *)kcontrol->private_value;
1417 struct aw_device *aw_dev = aw88399->aw_pa;
1420 time = ucontrol->value.integer.value[0];
1421 if (time < mc->min || time > mc->max)
1424 if (time != aw_dev->fade_out_time) {
1425 aw_dev->fade_out_time = time;
1432 static int aw88399_dev_set_profile_index(struct aw_device *aw_dev, int index)
1434 /* check the index whether is valid */
1435 if ((index >= aw_dev->prof_info.count) || (index < 0))
1437 /* check the index whether change */
1438 if (aw_dev->prof_index == index)
1441 aw_dev->prof_index = index;
1442 dev_dbg(aw_dev->dev, "set prof[%s]",
1443 aw_dev->prof_info.prof_name_list[aw_dev->prof_info.prof_desc[index].id]);
1448 static int aw88399_profile_info(struct snd_kcontrol *kcontrol,
1449 struct snd_ctl_elem_info *uinfo)
1451 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1452 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1453 char *prof_name, *name;
1456 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1459 count = aw88399->aw_pa->prof_info.count;
1461 uinfo->value.enumerated.items = 0;
1465 uinfo->value.enumerated.items = count;
1467 if (uinfo->value.enumerated.item >= count)
1468 uinfo->value.enumerated.item = count - 1;
1470 name = uinfo->value.enumerated.name;
1471 count = uinfo->value.enumerated.item;
1473 ret = aw88399_dev_get_prof_name(aw88399->aw_pa, count, &prof_name);
1475 strscpy(uinfo->value.enumerated.name, "null",
1476 strlen("null") + 1);
1480 strscpy(name, prof_name, sizeof(uinfo->value.enumerated.name));
1485 static int aw88399_profile_get(struct snd_kcontrol *kcontrol,
1486 struct snd_ctl_elem_value *ucontrol)
1488 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1489 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1491 ucontrol->value.integer.value[0] = aw88399->aw_pa->prof_index;
1496 static int aw88399_profile_set(struct snd_kcontrol *kcontrol,
1497 struct snd_ctl_elem_value *ucontrol)
1499 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1500 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1503 mutex_lock(&aw88399->lock);
1504 ret = aw88399_dev_set_profile_index(aw88399->aw_pa, ucontrol->value.integer.value[0]);
1506 dev_dbg(codec->dev, "profile index does not change");
1507 mutex_unlock(&aw88399->lock);
1511 if (aw88399->aw_pa->status) {
1512 aw88399_stop(aw88399->aw_pa);
1513 aw88399_start(aw88399, AW88399_SYNC_START);
1516 mutex_unlock(&aw88399->lock);
1521 static int aw88399_volume_get(struct snd_kcontrol *kcontrol,
1522 struct snd_ctl_elem_value *ucontrol)
1524 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1525 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1526 struct aw_volume_desc *vol_desc = &aw88399->aw_pa->volume_desc;
1528 ucontrol->value.integer.value[0] = vol_desc->ctl_volume;
1533 static int aw88399_volume_set(struct snd_kcontrol *kcontrol,
1534 struct snd_ctl_elem_value *ucontrol)
1536 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1537 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1538 struct aw_volume_desc *vol_desc = &aw88399->aw_pa->volume_desc;
1539 struct soc_mixer_control *mc =
1540 (struct soc_mixer_control *)kcontrol->private_value;
1543 value = ucontrol->value.integer.value[0];
1544 if (value < mc->min || value > mc->max)
1547 if (vol_desc->ctl_volume != value) {
1548 vol_desc->ctl_volume = value;
1549 aw_dev_set_volume(aw88399->aw_pa, vol_desc->ctl_volume);
1557 static int aw88399_get_fade_step(struct snd_kcontrol *kcontrol,
1558 struct snd_ctl_elem_value *ucontrol)
1560 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1561 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1563 ucontrol->value.integer.value[0] = aw88399->aw_pa->fade_step;
1568 static int aw88399_set_fade_step(struct snd_kcontrol *kcontrol,
1569 struct snd_ctl_elem_value *ucontrol)
1571 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1572 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1573 struct soc_mixer_control *mc =
1574 (struct soc_mixer_control *)kcontrol->private_value;
1577 value = ucontrol->value.integer.value[0];
1578 if (value < mc->min || value > mc->max)
1581 if (aw88399->aw_pa->fade_step != value) {
1582 aw88399->aw_pa->fade_step = value;
1589 static int aw88399_re_get(struct snd_kcontrol *kcontrol,
1590 struct snd_ctl_elem_value *ucontrol)
1592 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1593 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1594 struct aw_device *aw_dev = aw88399->aw_pa;
1596 ucontrol->value.integer.value[0] = aw_dev->cali_desc.cali_re;
1601 static int aw88399_re_set(struct snd_kcontrol *kcontrol,
1602 struct snd_ctl_elem_value *ucontrol)
1604 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1605 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1606 struct soc_mixer_control *mc =
1607 (struct soc_mixer_control *)kcontrol->private_value;
1608 struct aw_device *aw_dev = aw88399->aw_pa;
1611 value = ucontrol->value.integer.value[0];
1612 if (value < mc->min || value > mc->max)
1615 if (aw_dev->cali_desc.cali_re != value) {
1616 aw_dev->cali_desc.cali_re = value;
1623 static int aw88399_dev_init(struct aw88399 *aw88399, struct aw_container *aw_cfg)
1625 struct aw_device *aw_dev = aw88399->aw_pa;
1628 ret = aw88395_dev_cfg_load(aw_dev, aw_cfg);
1630 dev_err(aw_dev->dev, "aw_dev acf parse failed");
1633 aw_dev->fade_in_time = AW88399_1000_US / 10;
1634 aw_dev->fade_out_time = AW88399_1000_US >> 1;
1635 aw_dev->prof_cur = aw_dev->prof_info.prof_desc[0].id;
1636 aw_dev->prof_index = aw_dev->prof_info.prof_desc[0].id;
1638 ret = aw88399_dev_fw_update(aw88399, AW88399_FORCE_UPDATE_ON, AW88399_DSP_FW_UPDATE_ON);
1640 dev_err(aw_dev->dev, "fw update failed ret = %d\n", ret);
1644 aw88399_dev_mute(aw_dev, true);
1646 /* close tx feedback */
1647 aw_dev_i2s_tx_enable(aw_dev, false);
1648 usleep_range(AW88399_1000_US, AW88399_1000_US + 100);
1651 aw_dev_amppd(aw_dev, true);
1654 aw_dev_dsp_enable(aw_dev, false);
1655 /* set power down */
1656 aw_dev_pwd(aw_dev, true);
1661 static int aw88399_request_firmware_file(struct aw88399 *aw88399)
1663 const struct firmware *cont = NULL;
1666 aw88399->aw_pa->fw_status = AW88399_DEV_FW_FAILED;
1668 ret = request_firmware(&cont, AW88399_ACF_FILE, aw88399->aw_pa->dev);
1670 dev_err(aw88399->aw_pa->dev, "request [%s] failed!", AW88399_ACF_FILE);
1674 dev_dbg(aw88399->aw_pa->dev, "loaded %s - size: %zu\n",
1675 AW88399_ACF_FILE, cont ? cont->size : 0);
1677 aw88399->aw_cfg = devm_kzalloc(aw88399->aw_pa->dev,
1678 struct_size(aw88399->aw_cfg, data, cont->size), GFP_KERNEL);
1679 if (!aw88399->aw_cfg) {
1680 release_firmware(cont);
1683 aw88399->aw_cfg->len = (int)cont->size;
1684 memcpy(aw88399->aw_cfg->data, cont->data, cont->size);
1685 release_firmware(cont);
1687 ret = aw88395_dev_load_acf_check(aw88399->aw_pa, aw88399->aw_cfg);
1689 dev_err(aw88399->aw_pa->dev, "load [%s] failed!", AW88399_ACF_FILE);
1693 mutex_lock(&aw88399->lock);
1694 /* aw device init */
1695 ret = aw88399_dev_init(aw88399, aw88399->aw_cfg);
1697 dev_err(aw88399->aw_pa->dev, "dev init failed");
1698 mutex_unlock(&aw88399->lock);
1703 static const struct snd_kcontrol_new aw88399_controls[] = {
1704 SOC_SINGLE_EXT("PCM Playback Volume", AW88399_SYSCTRL2_REG,
1705 6, AW88399_MUTE_VOL, 0, aw88399_volume_get,
1706 aw88399_volume_set),
1707 SOC_SINGLE_EXT("Fade Step", 0, 0, AW88399_MUTE_VOL, 0,
1708 aw88399_get_fade_step, aw88399_set_fade_step),
1709 SOC_SINGLE_EXT("Volume Ramp Up Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
1710 aw88399_get_fade_in_time, aw88399_set_fade_in_time),
1711 SOC_SINGLE_EXT("Volume Ramp Down Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
1712 aw88399_get_fade_out_time, aw88399_set_fade_out_time),
1713 SOC_SINGLE_EXT("Calib", 0, 0, 100, 0,
1714 aw88399_re_get, aw88399_re_set),
1715 AW88399_PROFILE_EXT("AW88399 Profile Set", aw88399_profile_info,
1716 aw88399_profile_get, aw88399_profile_set),
1719 static int aw88399_playback_event(struct snd_soc_dapm_widget *w,
1720 struct snd_kcontrol *k, int event)
1722 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1723 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component);
1725 mutex_lock(&aw88399->lock);
1727 case SND_SOC_DAPM_PRE_PMU:
1728 aw88399_start(aw88399, AW88399_ASYNC_START);
1730 case SND_SOC_DAPM_POST_PMD:
1731 aw88399_stop(aw88399->aw_pa);
1736 mutex_unlock(&aw88399->lock);
1741 static const struct snd_soc_dapm_widget aw88399_dapm_widgets[] = {
1743 SND_SOC_DAPM_AIF_IN_E("AIF_RX", "Speaker_Playback", 0, 0, 0, 0,
1744 aw88399_playback_event,
1745 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1746 SND_SOC_DAPM_OUTPUT("DAC Output"),
1749 SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM, 0, 0),
1750 SND_SOC_DAPM_INPUT("ADC Input"),
1753 static const struct snd_soc_dapm_route aw88399_audio_map[] = {
1754 {"DAC Output", NULL, "AIF_RX"},
1755 {"AIF_TX", NULL, "ADC Input"},
1758 static int aw88399_codec_probe(struct snd_soc_component *component)
1760 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component);
1763 INIT_DELAYED_WORK(&aw88399->start_work, aw88399_startup_work);
1765 ret = aw88399_request_firmware_file(aw88399);
1767 dev_err(aw88399->aw_pa->dev, "%s failed\n", __func__);
1772 static void aw88399_codec_remove(struct snd_soc_component *aw_codec)
1774 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(aw_codec);
1776 cancel_delayed_work_sync(&aw88399->start_work);
1779 static const struct snd_soc_component_driver soc_codec_dev_aw88399 = {
1780 .probe = aw88399_codec_probe,
1781 .remove = aw88399_codec_remove,
1782 .dapm_widgets = aw88399_dapm_widgets,
1783 .num_dapm_widgets = ARRAY_SIZE(aw88399_dapm_widgets),
1784 .dapm_routes = aw88399_audio_map,
1785 .num_dapm_routes = ARRAY_SIZE(aw88399_audio_map),
1786 .controls = aw88399_controls,
1787 .num_controls = ARRAY_SIZE(aw88399_controls),
1790 static void aw88399_hw_reset(struct aw88399 *aw88399)
1792 if (aw88399->reset_gpio) {
1793 gpiod_set_value_cansleep(aw88399->reset_gpio, 1);
1794 usleep_range(AW88399_1000_US, AW88399_1000_US + 10);
1795 gpiod_set_value_cansleep(aw88399->reset_gpio, 0);
1796 usleep_range(AW88399_1000_US, AW88399_1000_US + 10);
1797 gpiod_set_value_cansleep(aw88399->reset_gpio, 1);
1798 usleep_range(AW88399_1000_US, AW88399_1000_US + 10);
1802 static void aw88399_parse_channel_dt(struct aw_device *aw_dev)
1804 struct device_node *np = aw_dev->dev->of_node;
1807 of_property_read_u32(np, "awinic,audio-channel", &channel_value);
1808 aw_dev->channel = channel_value;
1811 static int aw88399_init(struct aw88399 *aw88399, struct i2c_client *i2c, struct regmap *regmap)
1813 struct aw_device *aw_dev;
1814 unsigned int chip_id;
1817 ret = regmap_read(regmap, AW88399_ID_REG, &chip_id);
1819 dev_err(&i2c->dev, "%s read chipid error. ret = %d", __func__, ret);
1822 if (chip_id != AW88399_CHIP_ID) {
1823 dev_err(&i2c->dev, "unsupported device");
1826 dev_dbg(&i2c->dev, "chip id = %x\n", chip_id);
1828 aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL);
1831 aw88399->aw_pa = aw_dev;
1834 aw_dev->dev = &i2c->dev;
1835 aw_dev->regmap = regmap;
1836 mutex_init(&aw_dev->dsp_lock);
1838 aw_dev->chip_id = chip_id;
1840 aw_dev->prof_info.prof_desc = NULL;
1841 aw_dev->prof_info.count = 0;
1842 aw_dev->prof_info.prof_type = AW88395_DEV_NONE_TYPE_ID;
1843 aw_dev->channel = AW88399_DEV_DEFAULT_CH;
1844 aw_dev->fw_status = AW88399_DEV_FW_FAILED;
1846 aw_dev->fade_step = AW88399_VOLUME_STEP_DB;
1847 aw_dev->volume_desc.ctl_volume = AW88399_VOL_DEFAULT_VALUE;
1849 aw88399_parse_channel_dt(aw_dev);
1854 static int aw88399_i2c_probe(struct i2c_client *i2c)
1856 struct aw88399 *aw88399;
1859 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C))
1860 return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed");
1862 aw88399 = devm_kzalloc(&i2c->dev, sizeof(*aw88399), GFP_KERNEL);
1866 mutex_init(&aw88399->lock);
1868 i2c_set_clientdata(i2c, aw88399);
1870 aw88399->reset_gpio = devm_gpiod_get_optional(&i2c->dev, "reset", GPIOD_OUT_LOW);
1871 if (IS_ERR(aw88399->reset_gpio))
1872 return dev_err_probe(&i2c->dev, PTR_ERR(aw88399->reset_gpio),
1873 "reset gpio not defined\n");
1874 aw88399_hw_reset(aw88399);
1876 aw88399->regmap = devm_regmap_init_i2c(i2c, &aw88399_remap_config);
1877 if (IS_ERR(aw88399->regmap))
1878 return dev_err_probe(&i2c->dev, PTR_ERR(aw88399->regmap),
1879 "failed to init regmap\n");
1882 ret = aw88399_init(aw88399, i2c, aw88399->regmap);
1886 ret = devm_snd_soc_register_component(&i2c->dev,
1887 &soc_codec_dev_aw88399,
1888 aw88399_dai, ARRAY_SIZE(aw88399_dai));
1890 dev_err(&i2c->dev, "failed to register aw88399: %d", ret);
1895 static const struct i2c_device_id aw88399_i2c_id[] = {
1896 { AW88399_I2C_NAME, 0 },
1899 MODULE_DEVICE_TABLE(i2c, aw88399_i2c_id);
1901 static struct i2c_driver aw88399_i2c_driver = {
1903 .name = AW88399_I2C_NAME,
1905 .probe = aw88399_i2c_probe,
1906 .id_table = aw88399_i2c_id,
1908 module_i2c_driver(aw88399_i2c_driver);
1910 MODULE_DESCRIPTION("ASoC AW88399 Smart PA Driver");
1911 MODULE_LICENSE("GPL v2");