1 // SPDX-License-Identifier: GPL-2.0-only
3 // aw88399.c -- ALSA SoC AW88399 codec support
5 // Copyright (c) 2023 AWINIC Technology CO., LTD
10 #include <linux/crc32.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/firmware.h>
14 #include <linux/regmap.h>
15 #include <sound/soc.h>
17 #include "aw88395/aw88395_device.h"
19 static const struct regmap_config aw88399_remap_config = {
22 .max_register = AW88399_REG_MAX,
23 .reg_format_endian = REGMAP_ENDIAN_LITTLE,
24 .val_format_endian = REGMAP_ENDIAN_BIG,
27 static int aw_dev_dsp_write_16bit(struct aw_device *aw_dev,
28 unsigned short dsp_addr, unsigned int dsp_data)
32 ret = regmap_write(aw_dev->regmap, AW88399_DSPMADD_REG, dsp_addr);
34 dev_err(aw_dev->dev, "%s write addr error, ret=%d", __func__, ret);
38 ret = regmap_write(aw_dev->regmap, AW88399_DSPMDAT_REG, (u16)dsp_data);
40 dev_err(aw_dev->dev, "%s write data error, ret=%d", __func__, ret);
47 static int aw_dev_dsp_read_16bit(struct aw_device *aw_dev,
48 unsigned short dsp_addr, unsigned int *dsp_data)
50 unsigned int temp_data;
53 ret = regmap_write(aw_dev->regmap, AW88399_DSPMADD_REG, dsp_addr);
55 dev_err(aw_dev->dev, "%s write error, ret=%d", __func__, ret);
59 ret = regmap_read(aw_dev->regmap, AW88399_DSPMDAT_REG, &temp_data);
61 dev_err(aw_dev->dev, "%s read error, ret=%d", __func__, ret);
64 *dsp_data = temp_data;
69 static int aw_dev_dsp_read_32bit(struct aw_device *aw_dev,
70 unsigned short dsp_addr, unsigned int *dsp_data)
72 unsigned int temp_data;
75 ret = regmap_write(aw_dev->regmap, AW88399_DSPMADD_REG, dsp_addr);
77 dev_err(aw_dev->dev, "%s write error, ret=%d", __func__, ret);
81 ret = regmap_read(aw_dev->regmap, AW88399_DSPMDAT_REG, &temp_data);
83 dev_err(aw_dev->dev, "%s read error, ret=%d", __func__, ret);
86 *dsp_data = temp_data;
88 ret = regmap_read(aw_dev->regmap, AW88399_DSPMDAT_REG, &temp_data);
90 dev_err(aw_dev->dev, "%s read error, ret=%d", __func__, ret);
93 *dsp_data |= (temp_data << 16);
98 static int aw_dev_dsp_read(struct aw_device *aw_dev,
99 unsigned short dsp_addr, unsigned int *dsp_data, unsigned char data_type)
104 mutex_lock(&aw_dev->dsp_lock);
106 case AW88399_DSP_16_DATA:
107 ret = aw_dev_dsp_read_16bit(aw_dev, dsp_addr, dsp_data);
109 dev_err(aw_dev->dev, "read dsp_addr[0x%x] 16-bit dsp_data[0x%x] failed",
110 (u32)dsp_addr, *dsp_data);
112 case AW88399_DSP_32_DATA:
113 ret = aw_dev_dsp_read_32bit(aw_dev, dsp_addr, dsp_data);
115 dev_err(aw_dev->dev, "read dsp_addr[0x%x] 32r-bit dsp_data[0x%x] failed",
116 (u32)dsp_addr, *dsp_data);
119 dev_err(aw_dev->dev, "data type[%d] unsupported", data_type);
124 /* clear dsp chip select state */
125 if (regmap_read(aw_dev->regmap, AW88399_ID_REG, ®_value))
126 dev_err(aw_dev->dev, "%s fail to clear chip state. ret=%d\n", __func__, ret);
127 mutex_unlock(&aw_dev->dsp_lock);
132 static void aw_dev_pwd(struct aw_device *aw_dev, bool pwd)
137 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
138 ~AW88399_PWDN_MASK, AW88399_PWDN_POWER_DOWN_VALUE);
140 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
141 ~AW88399_PWDN_MASK, AW88399_PWDN_WORKING_VALUE);
144 dev_dbg(aw_dev->dev, "%s failed", __func__);
147 static void aw_dev_get_int_status(struct aw_device *aw_dev, unsigned short *int_status)
149 unsigned int reg_val;
152 ret = regmap_read(aw_dev->regmap, AW88399_SYSINT_REG, ®_val);
154 dev_err(aw_dev->dev, "read interrupt reg fail, ret=%d", ret);
156 *int_status = reg_val;
158 dev_dbg(aw_dev->dev, "read interrupt reg=0x%04x", *int_status);
161 static void aw_dev_clear_int_status(struct aw_device *aw_dev)
165 /* read int status and clear */
166 aw_dev_get_int_status(aw_dev, &int_status);
167 /* make sure int status is clear */
168 aw_dev_get_int_status(aw_dev, &int_status);
170 dev_dbg(aw_dev->dev, "int status(%d) is not cleaned.\n", int_status);
173 static int aw_dev_get_iis_status(struct aw_device *aw_dev)
175 unsigned int reg_val;
178 ret = regmap_read(aw_dev->regmap, AW88399_SYSST_REG, ®_val);
181 if ((reg_val & AW88399_BIT_PLL_CHECK) != AW88399_BIT_PLL_CHECK) {
182 dev_err(aw_dev->dev, "check pll lock fail, reg_val:0x%04x", reg_val);
189 static int aw_dev_check_mode1_pll(struct aw_device *aw_dev)
193 for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) {
194 ret = aw_dev_get_iis_status(aw_dev);
196 dev_err(aw_dev->dev, "mode1 iis signal check error");
197 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
206 static int aw_dev_check_mode2_pll(struct aw_device *aw_dev)
208 unsigned int reg_val;
211 ret = regmap_read(aw_dev->regmap, AW88399_PLLCTRL2_REG, ®_val);
215 reg_val &= (~AW88399_CCO_MUX_MASK);
216 if (reg_val == AW88399_CCO_MUX_DIVIDED_VALUE) {
217 dev_dbg(aw_dev->dev, "CCO_MUX is already divider");
222 ret = regmap_update_bits(aw_dev->regmap, AW88399_PLLCTRL2_REG,
223 ~AW88399_CCO_MUX_MASK, AW88399_CCO_MUX_DIVIDED_VALUE);
227 for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) {
228 ret = aw_dev_get_iis_status(aw_dev);
230 dev_err(aw_dev->dev, "mode2 iis signal check error");
231 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
238 regmap_update_bits(aw_dev->regmap, AW88399_PLLCTRL2_REG,
239 ~AW88399_CCO_MUX_MASK, AW88399_CCO_MUX_BYPASS_VALUE);
241 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
242 for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) {
243 ret = aw_dev_get_iis_status(aw_dev);
245 dev_err(aw_dev->dev, "mode2 switch to mode1, iis signal check error");
246 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
256 static int aw_dev_check_syspll(struct aw_device *aw_dev)
260 ret = aw_dev_check_mode1_pll(aw_dev);
262 dev_dbg(aw_dev->dev, "mode1 check iis failed try switch to mode2 check");
263 ret = aw_dev_check_mode2_pll(aw_dev);
265 dev_err(aw_dev->dev, "mode2 check iis failed");
273 static int aw_dev_check_sysst(struct aw_device *aw_dev)
275 unsigned int check_val;
276 unsigned int reg_val;
279 ret = regmap_read(aw_dev->regmap, AW88399_PWMCTRL3_REG, ®_val);
283 if (reg_val & (~AW88399_NOISE_GATE_EN_MASK))
284 check_val = AW88399_BIT_SYSST_NOSWS_CHECK;
286 check_val = AW88399_BIT_SYSST_SWS_CHECK;
288 for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) {
289 ret = regmap_read(aw_dev->regmap, AW88399_SYSST_REG, ®_val);
293 if ((reg_val & (~AW88399_BIT_SYSST_CHECK_MASK) & check_val) != check_val) {
294 dev_err(aw_dev->dev, "check sysst fail, cnt=%d, reg_val=0x%04x, check:0x%x",
295 i, reg_val, AW88399_BIT_SYSST_NOSWS_CHECK);
296 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
305 static void aw_dev_amppd(struct aw_device *aw_dev, bool amppd)
310 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
311 ~AW88399_AMPPD_MASK, AW88399_AMPPD_POWER_DOWN_VALUE);
313 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
314 ~AW88399_AMPPD_MASK, AW88399_AMPPD_WORKING_VALUE);
317 dev_dbg(aw_dev->dev, "%s failed", __func__);
320 static void aw_dev_dsp_enable(struct aw_device *aw_dev, bool is_enable)
325 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
326 ~AW88399_DSPBY_MASK, AW88399_DSPBY_WORKING_VALUE);
328 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
329 ~AW88399_DSPBY_MASK, AW88399_DSPBY_BYPASS_VALUE);
332 dev_dbg(aw_dev->dev, "%s failed\n", __func__);
335 static int aw88399_dev_get_icalk(struct aw88399 *aw88399, int16_t *icalk)
337 uint16_t icalkh_val, icalkl_val, icalk_val;
338 struct aw_device *aw_dev = aw88399->aw_pa;
339 unsigned int reg_val;
342 ret = regmap_read(aw_dev->regmap, AW88399_EFRH4_REG, ®_val);
345 icalkh_val = reg_val & (~AW88399_EF_ISN_GESLP_H_MASK);
347 ret = regmap_read(aw_dev->regmap, AW88399_EFRL4_REG, ®_val);
350 icalkl_val = reg_val & (~AW88399_EF_ISN_GESLP_L_MASK);
352 if (aw88399->check_val == AW_EF_AND_CHECK)
353 icalk_val = icalkh_val & icalkl_val;
355 icalk_val = icalkh_val | icalkl_val;
357 if (icalk_val & (~AW88399_EF_ISN_GESLP_SIGN_MASK))
358 icalk_val = icalk_val | AW88399_EF_ISN_GESLP_SIGN_NEG;
359 *icalk = (int16_t)icalk_val;
364 static int aw88399_dev_get_vcalk(struct aw88399 *aw88399, int16_t *vcalk)
366 uint16_t vcalkh_val, vcalkl_val, vcalk_val;
367 struct aw_device *aw_dev = aw88399->aw_pa;
368 unsigned int reg_val;
371 ret = regmap_read(aw_dev->regmap, AW88399_EFRH3_REG, ®_val);
375 vcalkh_val = reg_val & (~AW88399_EF_VSN_GESLP_H_MASK);
377 ret = regmap_read(aw_dev->regmap, AW88399_EFRL3_REG, ®_val);
381 vcalkl_val = reg_val & (~AW88399_EF_VSN_GESLP_L_MASK);
383 if (aw88399->check_val == AW_EF_AND_CHECK)
384 vcalk_val = vcalkh_val & vcalkl_val;
386 vcalk_val = vcalkh_val | vcalkl_val;
388 if (vcalk_val & AW88399_EF_VSN_GESLP_SIGN_MASK)
389 vcalk_val = vcalk_val | AW88399_EF_VSN_GESLP_SIGN_NEG;
390 *vcalk = (int16_t)vcalk_val;
395 static int aw88399_dev_get_internal_vcalk(struct aw88399 *aw88399, int16_t *vcalk)
397 uint16_t vcalkh_val, vcalkl_val, vcalk_val;
398 struct aw_device *aw_dev = aw88399->aw_pa;
399 unsigned int reg_val;
402 ret = regmap_read(aw_dev->regmap, AW88399_EFRH2_REG, ®_val);
405 vcalkh_val = reg_val & (~AW88399_INTERNAL_VSN_TRIM_H_MASK);
407 ret = regmap_read(aw_dev->regmap, AW88399_EFRL2_REG, ®_val);
410 vcalkl_val = reg_val & (~AW88399_INTERNAL_VSN_TRIM_L_MASK);
412 if (aw88399->check_val == AW_EF_AND_CHECK)
413 vcalk_val = (vcalkh_val >> AW88399_INTERNAL_VSN_TRIM_H_START_BIT) &
414 (vcalkl_val >> AW88399_INTERNAL_VSN_TRIM_L_START_BIT);
416 vcalk_val = (vcalkh_val >> AW88399_INTERNAL_VSN_TRIM_H_START_BIT) |
417 (vcalkl_val >> AW88399_INTERNAL_VSN_TRIM_L_START_BIT);
419 if (vcalk_val & (~AW88399_TEM4_SIGN_MASK))
420 vcalk_val = vcalk_val | AW88399_TEM4_SIGN_NEG;
422 *vcalk = (int16_t)vcalk_val;
427 static int aw_dev_set_vcalb(struct aw88399 *aw88399)
429 struct aw_device *aw_dev = aw88399->aw_pa;
430 unsigned int vsense_select, vsense_value;
431 int32_t ical_k, vcal_k, vcalb;
432 int16_t icalk, vcalk;
436 ret = regmap_read(aw_dev->regmap, AW88399_VSNCTRL1_REG, &vsense_value);
440 vsense_select = vsense_value & (~AW88399_VDSEL_MASK);
442 ret = aw88399_dev_get_icalk(aw88399, &icalk);
444 dev_err(aw_dev->dev, "get icalk failed\n");
448 ical_k = icalk * AW88399_ICABLK_FACTOR + AW88399_CABL_BASE_VALUE;
450 switch (vsense_select) {
451 case AW88399_DEV_VDSEL_VSENSE:
452 ret = aw88399_dev_get_vcalk(aw88399, &vcalk);
453 vcal_k = vcalk * AW88399_VCABLK_FACTOR + AW88399_CABL_BASE_VALUE;
454 vcalb = AW88399_VCALB_ACCURACY * AW88399_VSCAL_FACTOR / AW88399_ISCAL_FACTOR /
455 ical_k / vcal_k * aw88399->vcalb_init_val;
457 case AW88399_DEV_VDSEL_DAC:
458 ret = aw88399_dev_get_internal_vcalk(aw88399, &vcalk);
459 vcal_k = vcalk * AW88399_VCABLK_DAC_FACTOR + AW88399_CABL_BASE_VALUE;
460 vcalb = AW88399_VCALB_ACCURACY * AW88399_VSCAL_DAC_FACTOR /
461 AW88399_ISCAL_DAC_FACTOR / ical_k /
462 vcal_k * aw88399->vcalb_init_val;
465 dev_err(aw_dev->dev, "%s: unsupport vsense\n", __func__);
472 vcalb = vcalb >> AW88399_VCALB_ADJ_FACTOR;
473 reg_val = (uint32_t)vcalb;
475 regmap_write(aw_dev->regmap, AW88399_DSPVCALB_REG, reg_val);
480 static int aw_dev_update_cali_re(struct aw_cali_desc *cali_desc)
482 struct aw_device *aw_dev =
483 container_of(cali_desc, struct aw_device, cali_desc);
484 uint16_t re_lbits, re_hbits;
488 if ((aw_dev->cali_desc.cali_re >= AW88399_CALI_RE_MAX) ||
489 (aw_dev->cali_desc.cali_re <= AW88399_CALI_RE_MIN))
492 cali_re = AW88399_SHOW_RE_TO_DSP_RE((aw_dev->cali_desc.cali_re +
493 aw_dev->cali_desc.ra), AW88399_DSP_RE_SHIFT);
495 re_hbits = (cali_re & (~AW88399_CALI_RE_HBITS_MASK)) >> AW88399_CALI_RE_HBITS_SHIFT;
496 re_lbits = (cali_re & (~AW88399_CALI_RE_LBITS_MASK)) >> AW88399_CALI_RE_LBITS_SHIFT;
498 ret = regmap_write(aw_dev->regmap, AW88399_ACR1_REG, re_hbits);
500 dev_err(aw_dev->dev, "set cali re error");
504 ret = regmap_write(aw_dev->regmap, AW88399_ACR2_REG, re_lbits);
506 dev_err(aw_dev->dev, "set cali re error");
511 static int aw_dev_fw_crc_check(struct aw_device *aw_dev)
513 uint16_t check_val, fw_len_val;
514 unsigned int reg_val;
517 /* calculate fw_end_addr */
518 fw_len_val = ((aw_dev->dsp_fw_len / AW_FW_ADDR_LEN) - 1) + AW88399_CRC_FW_BASE_ADDR;
520 /* write fw_end_addr to crc_end_addr */
521 ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG,
522 ~AW88399_CRC_END_ADDR_MASK, fw_len_val);
525 /* enable fw crc check */
526 ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG,
527 ~AW88399_CRC_CODE_EN_MASK, AW88399_CRC_CODE_EN_ENABLE_VALUE);
529 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
531 /* read crc check result */
532 regmap_read(aw_dev->regmap, AW88399_HAGCST_REG, ®_val);
536 check_val = (reg_val & (~AW88399_CRC_CHECK_BITS_MASK)) >> AW88399_CRC_CHECK_START_BIT;
538 /* disable fw crc check */
539 ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG,
540 ~AW88399_CRC_CODE_EN_MASK, AW88399_CRC_CODE_EN_DISABLE_VALUE);
544 if (check_val != AW88399_CRC_CHECK_PASS_VAL) {
545 dev_err(aw_dev->dev, "%s failed, check_val 0x%x != 0x%x",
546 __func__, check_val, AW88399_CRC_CHECK_PASS_VAL);
553 static int aw_dev_cfg_crc_check(struct aw_device *aw_dev)
555 uint16_t check_val, cfg_len_val;
556 unsigned int reg_val;
559 /* calculate cfg end addr */
560 cfg_len_val = ((aw_dev->dsp_cfg_len / AW_FW_ADDR_LEN) - 1) + AW88399_CRC_CFG_BASE_ADDR;
562 /* write cfg_end_addr to crc_end_addr */
563 ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG,
564 ~AW88399_CRC_END_ADDR_MASK, cfg_len_val);
568 /* enable cfg crc check */
569 ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG,
570 ~AW88399_CRC_CFG_EN_MASK, AW88399_CRC_CFG_EN_ENABLE_VALUE);
574 usleep_range(AW88399_1000_US, AW88399_1000_US + 10);
576 /* read crc check result */
577 ret = regmap_read(aw_dev->regmap, AW88399_HAGCST_REG, ®_val);
581 check_val = (reg_val & (~AW88399_CRC_CHECK_BITS_MASK)) >> AW88399_CRC_CHECK_START_BIT;
583 /* disable cfg crc check */
584 ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG,
585 ~AW88399_CRC_CFG_EN_MASK, AW88399_CRC_CFG_EN_DISABLE_VALUE);
589 if (check_val != AW88399_CRC_CHECK_PASS_VAL) {
590 dev_err(aw_dev->dev, "crc_check failed, check val 0x%x != 0x%x",
591 check_val, AW88399_CRC_CHECK_PASS_VAL);
598 static int aw_dev_hw_crc_check(struct aw88399 *aw88399)
600 struct aw_device *aw_dev = aw88399->aw_pa;
603 ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG,
604 ~AW88399_RAM_CG_BYP_MASK, AW88399_RAM_CG_BYP_BYPASS_VALUE);
608 ret = aw_dev_fw_crc_check(aw_dev);
610 dev_err(aw_dev->dev, "fw_crc_check failed\n");
611 goto crc_check_failed;
614 ret = aw_dev_cfg_crc_check(aw_dev);
616 dev_err(aw_dev->dev, "cfg_crc_check failed\n");
617 goto crc_check_failed;
620 ret = regmap_write(aw_dev->regmap, AW88399_CRCCTRL_REG, aw88399->crc_init_val);
624 ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG,
625 ~AW88399_RAM_CG_BYP_MASK, AW88399_RAM_CG_BYP_WORK_VALUE);
630 regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG,
631 ~AW88399_RAM_CG_BYP_MASK, AW88399_RAM_CG_BYP_WORK_VALUE);
635 static void aw_dev_i2s_tx_enable(struct aw_device *aw_dev, bool flag)
640 ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCTRL3_REG,
641 ~AW88399_I2STXEN_MASK, AW88399_I2STXEN_ENABLE_VALUE);
643 ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG,
644 ~AW88399_I2STXEN_MASK, AW88399_I2STXEN_DISABLE_VALUE);
647 dev_dbg(aw_dev->dev, "%s failed", __func__);
650 static int aw_dev_get_dsp_status(struct aw_device *aw_dev)
652 unsigned int reg_val;
655 ret = regmap_read(aw_dev->regmap, AW88399_WDT_REG, ®_val);
658 if (!(reg_val & (~AW88399_WDT_CNT_MASK)))
664 static int aw_dev_dsp_check(struct aw_device *aw_dev)
668 switch (aw_dev->dsp_cfg) {
669 case AW88399_DEV_DSP_BYPASS:
670 dev_dbg(aw_dev->dev, "dsp bypass");
673 case AW88399_DEV_DSP_WORK:
674 aw_dev_dsp_enable(aw_dev, false);
675 aw_dev_dsp_enable(aw_dev, true);
676 usleep_range(AW88399_1000_US, AW88399_1000_US + 10);
677 for (i = 0; i < AW88399_DEV_DSP_CHECK_MAX; i++) {
678 ret = aw_dev_get_dsp_status(aw_dev);
680 dev_err(aw_dev->dev, "dsp wdt status error=%d", ret);
681 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
686 dev_err(aw_dev->dev, "unknown dsp cfg=%d", aw_dev->dsp_cfg);
694 static int aw_dev_set_volume(struct aw_device *aw_dev, unsigned int value)
696 struct aw_volume_desc *vol_desc = &aw_dev->volume_desc;
697 unsigned int reg_value;
701 real_value = min((value + vol_desc->init_volume), (unsigned int)AW88399_MUTE_VOL);
703 ret = regmap_read(aw_dev->regmap, AW88399_SYSCTRL2_REG, ®_value);
707 dev_dbg(aw_dev->dev, "value 0x%x , reg:0x%x", value, real_value);
709 real_value = (real_value << AW88399_VOL_START_BIT) | (reg_value & AW88399_VOL_MASK);
711 ret = regmap_write(aw_dev->regmap, AW88399_SYSCTRL2_REG, real_value);
716 static void aw_dev_fade_in(struct aw_device *aw_dev)
718 struct aw_volume_desc *desc = &aw_dev->volume_desc;
719 u16 fade_in_vol = desc->ctl_volume;
720 int fade_step = aw_dev->fade_step;
723 if (fade_step == 0 || aw_dev->fade_in_time == 0) {
724 aw_dev_set_volume(aw_dev, fade_in_vol);
728 for (i = AW88399_MUTE_VOL; i >= fade_in_vol; i -= fade_step) {
729 aw_dev_set_volume(aw_dev, i);
730 usleep_range(aw_dev->fade_in_time, aw_dev->fade_in_time + 10);
733 if (i != fade_in_vol)
734 aw_dev_set_volume(aw_dev, fade_in_vol);
737 static void aw_dev_fade_out(struct aw_device *aw_dev)
739 struct aw_volume_desc *desc = &aw_dev->volume_desc;
740 int fade_step = aw_dev->fade_step;
743 if (fade_step == 0 || aw_dev->fade_out_time == 0) {
744 aw_dev_set_volume(aw_dev, AW88399_MUTE_VOL);
748 for (i = desc->ctl_volume; i <= AW88399_MUTE_VOL; i += fade_step) {
749 aw_dev_set_volume(aw_dev, i);
750 usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10);
753 if (i != AW88399_MUTE_VOL) {
754 aw_dev_set_volume(aw_dev, AW88399_MUTE_VOL);
755 usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10);
759 static void aw88399_dev_mute(struct aw_device *aw_dev, bool is_mute)
762 aw_dev_fade_out(aw_dev);
763 regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
764 ~AW88399_HMUTE_MASK, AW88399_HMUTE_ENABLE_VALUE);
766 regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG,
767 ~AW88399_HMUTE_MASK, AW88399_HMUTE_DISABLE_VALUE);
768 aw_dev_fade_in(aw_dev);
772 static void aw88399_dev_set_dither(struct aw88399 *aw88399, bool dither)
774 struct aw_device *aw_dev = aw88399->aw_pa;
777 regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG,
778 ~AW88399_DITHER_EN_MASK, AW88399_DITHER_EN_ENABLE_VALUE);
780 regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG,
781 ~AW88399_DITHER_EN_MASK, AW88399_DITHER_EN_DISABLE_VALUE);
784 static int aw88399_dev_start(struct aw88399 *aw88399)
786 struct aw_device *aw_dev = aw88399->aw_pa;
789 if (aw_dev->status == AW88399_DEV_PW_ON) {
790 dev_dbg(aw_dev->dev, "already power on");
794 aw88399_dev_set_dither(aw88399, false);
797 aw_dev_pwd(aw_dev, false);
798 usleep_range(AW88399_2000_US, AW88399_2000_US + 10);
800 ret = aw_dev_check_syspll(aw_dev);
802 dev_err(aw_dev->dev, "pll check failed cannot start");
807 aw_dev_amppd(aw_dev, false);
808 usleep_range(AW88399_1000_US, AW88399_1000_US + 50);
810 /* check i2s status */
811 ret = aw_dev_check_sysst(aw_dev);
813 dev_err(aw_dev->dev, "sysst check failed");
814 goto sysst_check_fail;
817 if (aw_dev->dsp_cfg == AW88399_DEV_DSP_WORK) {
818 ret = aw_dev_hw_crc_check(aw88399);
820 dev_err(aw_dev->dev, "dsp crc check failed");
823 aw_dev_dsp_enable(aw_dev, false);
824 aw_dev_set_vcalb(aw88399);
825 aw_dev_update_cali_re(&aw_dev->cali_desc);
827 ret = aw_dev_dsp_check(aw_dev);
829 dev_err(aw_dev->dev, "dsp status check failed");
833 dev_dbg(aw_dev->dev, "start pa with dsp bypass");
836 /* enable tx feedback */
837 aw_dev_i2s_tx_enable(aw_dev, true);
839 if (aw88399->dither_st == AW88399_DITHER_EN_ENABLE_VALUE)
840 aw88399_dev_set_dither(aw88399, true);
843 aw88399_dev_mute(aw_dev, false);
844 /* clear inturrupt */
845 aw_dev_clear_int_status(aw_dev);
846 aw_dev->status = AW88399_DEV_PW_ON;
852 aw_dev_dsp_enable(aw_dev, false);
854 aw_dev_clear_int_status(aw_dev);
855 aw_dev_amppd(aw_dev, true);
857 aw_dev_pwd(aw_dev, true);
858 aw_dev->status = AW88399_DEV_PW_OFF;
863 static int aw_dev_dsp_update_container(struct aw_device *aw_dev,
864 unsigned char *data, unsigned int len, unsigned short base)
869 mutex_lock(&aw_dev->dsp_lock);
870 ret = regmap_write(aw_dev->regmap, AW88399_DSPMADD_REG, base);
872 goto error_operation;
874 for (i = 0; i < len; i += AW88399_MAX_RAM_WRITE_BYTE_SIZE) {
875 if ((len - i) < AW88399_MAX_RAM_WRITE_BYTE_SIZE)
878 tmp_len = AW88399_MAX_RAM_WRITE_BYTE_SIZE;
880 ret = regmap_raw_write(aw_dev->regmap, AW88399_DSPMDAT_REG,
883 goto error_operation;
885 mutex_unlock(&aw_dev->dsp_lock);
890 mutex_unlock(&aw_dev->dsp_lock);
894 static int aw_dev_get_ra(struct aw_cali_desc *cali_desc)
896 struct aw_device *aw_dev =
897 container_of(cali_desc, struct aw_device, cali_desc);
901 ret = aw_dev_dsp_read(aw_dev, AW88399_DSP_REG_CFG_ADPZ_RA,
902 &dsp_ra, AW88399_DSP_32_DATA);
904 dev_err(aw_dev->dev, "read ra error");
908 cali_desc->ra = AW88399_DSP_RE_TO_SHOW_RE(dsp_ra,
909 AW88399_DSP_RE_SHIFT);
914 static int aw_dev_dsp_update_cfg(struct aw_device *aw_dev,
915 unsigned char *data, unsigned int len)
919 dev_dbg(aw_dev->dev, "dsp config len:%d", len);
922 dev_err(aw_dev->dev, "dsp config data is null or len is 0");
926 ret = aw_dev_dsp_update_container(aw_dev, data, len, AW88399_DSP_CFG_ADDR);
930 aw_dev->dsp_cfg_len = len;
932 ret = aw_dev_get_ra(&aw_dev->cali_desc);
937 static int aw_dev_dsp_update_fw(struct aw_device *aw_dev,
938 unsigned char *data, unsigned int len)
942 dev_dbg(aw_dev->dev, "dsp firmware len:%d", len);
945 dev_err(aw_dev->dev, "dsp firmware data is null or len is 0");
949 aw_dev->dsp_fw_len = len;
950 ret = aw_dev_dsp_update_container(aw_dev, data, len, AW88399_DSP_FW_ADDR);
955 static int aw_dev_check_sram(struct aw_device *aw_dev)
957 unsigned int reg_val;
959 mutex_lock(&aw_dev->dsp_lock);
960 /* read dsp_rom_check_reg */
961 aw_dev_dsp_read_16bit(aw_dev, AW88399_DSP_ROM_CHECK_ADDR, ®_val);
962 if (reg_val != AW88399_DSP_ROM_CHECK_DATA) {
963 dev_err(aw_dev->dev, "check dsp rom failed, read[0x%x] != check[0x%x]",
964 reg_val, AW88399_DSP_ROM_CHECK_DATA);
968 /* check dsp_cfg_base_addr */
969 aw_dev_dsp_write_16bit(aw_dev, AW88399_DSP_CFG_ADDR, AW88399_DSP_ODD_NUM_BIT_TEST);
970 aw_dev_dsp_read_16bit(aw_dev, AW88399_DSP_CFG_ADDR, ®_val);
971 if (reg_val != AW88399_DSP_ODD_NUM_BIT_TEST) {
972 dev_err(aw_dev->dev, "check dsp cfg failed, read[0x%x] != write[0x%x]",
973 reg_val, AW88399_DSP_ODD_NUM_BIT_TEST);
976 mutex_unlock(&aw_dev->dsp_lock);
980 mutex_unlock(&aw_dev->dsp_lock);
984 static void aw_dev_select_memclk(struct aw_device *aw_dev, unsigned char flag)
989 case AW88399_DEV_MEMCLK_PLL:
990 ret = regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG,
991 ~AW88399_MEM_CLKSEL_MASK,
992 AW88399_MEM_CLKSEL_DAPHCLK_VALUE);
994 dev_err(aw_dev->dev, "memclk select pll failed");
996 case AW88399_DEV_MEMCLK_OSC:
997 ret = regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG,
998 ~AW88399_MEM_CLKSEL_MASK,
999 AW88399_MEM_CLKSEL_OSCCLK_VALUE);
1001 dev_err(aw_dev->dev, "memclk select OSC failed");
1004 dev_err(aw_dev->dev, "unknown memclk config, flag=0x%x", flag);
1009 static void aw_dev_get_cur_mode_st(struct aw_device *aw_dev)
1011 struct aw_profctrl_desc *profctrl_desc = &aw_dev->profctrl_desc;
1012 unsigned int reg_val;
1015 ret = regmap_read(aw_dev->regmap, AW88399_SYSCTRL_REG, ®_val);
1017 dev_dbg(aw_dev->dev, "%s failed", __func__);
1020 if ((reg_val & (~AW88399_RCV_MODE_MASK)) == AW88399_RCV_MODE_RECEIVER_VALUE)
1021 profctrl_desc->cur_mode = AW88399_RCV_MODE;
1023 profctrl_desc->cur_mode = AW88399_NOT_RCV_MODE;
1026 static int aw_dev_update_reg_container(struct aw88399 *aw88399,
1027 unsigned char *data, unsigned int len)
1029 struct aw_device *aw_dev = aw88399->aw_pa;
1030 struct aw_volume_desc *vol_desc = &aw_dev->volume_desc;
1031 u16 read_vol, reg_val;
1032 int data_len, i, ret;
1036 reg_data = (int16_t *)data;
1037 data_len = len >> 1;
1039 if (data_len & 0x1) {
1040 dev_err(aw_dev->dev, "data len:%d unsupported", data_len);
1044 for (i = 0; i < data_len; i += 2) {
1045 reg_addr = reg_data[i];
1046 reg_val = reg_data[i + 1];
1048 if (reg_addr == AW88399_DSPVCALB_REG) {
1049 aw88399->vcalb_init_val = reg_val;
1053 if (reg_addr == AW88399_SYSCTRL_REG) {
1054 if (reg_val & (~AW88399_DSPBY_MASK))
1055 aw_dev->dsp_cfg = AW88399_DEV_DSP_BYPASS;
1057 aw_dev->dsp_cfg = AW88399_DEV_DSP_WORK;
1059 reg_val &= (AW88399_HMUTE_MASK | AW88399_PWDN_MASK |
1060 AW88399_DSPBY_MASK);
1061 reg_val |= (AW88399_HMUTE_ENABLE_VALUE | AW88399_PWDN_POWER_DOWN_VALUE |
1062 AW88399_DSPBY_BYPASS_VALUE);
1065 if (reg_addr == AW88399_I2SCTRL3_REG) {
1066 reg_val &= AW88399_I2STXEN_MASK;
1067 reg_val |= AW88399_I2STXEN_DISABLE_VALUE;
1070 if (reg_addr == AW88399_SYSCTRL2_REG) {
1071 read_vol = (reg_val & (~AW88399_VOL_MASK)) >>
1072 AW88399_VOL_START_BIT;
1073 aw_dev->volume_desc.init_volume = read_vol;
1076 if (reg_addr == AW88399_DBGCTRL_REG) {
1077 if ((reg_val & (~AW88399_EF_DBMD_MASK)) == AW88399_EF_DBMD_OR_VALUE)
1078 aw88399->check_val = AW_EF_OR_CHECK;
1080 aw88399->check_val = AW_EF_AND_CHECK;
1082 aw88399->dither_st = reg_val & (~AW88399_DITHER_EN_MASK);
1085 if (reg_addr == AW88399_CRCCTRL_REG)
1086 aw88399->crc_init_val = reg_val;
1088 ret = regmap_write(aw_dev->regmap, reg_addr, reg_val);
1093 aw_dev_pwd(aw_dev, false);
1094 usleep_range(AW88399_1000_US, AW88399_1000_US + 10);
1096 aw_dev_get_cur_mode_st(aw_dev);
1098 if (aw_dev->prof_cur != aw_dev->prof_index)
1099 vol_desc->ctl_volume = 0;
1101 aw_dev_set_volume(aw_dev, vol_desc->ctl_volume);
1106 static int aw_dev_reg_update(struct aw88399 *aw88399,
1107 unsigned char *data, unsigned int len)
1111 if (!len || !data) {
1112 dev_err(aw88399->aw_pa->dev, "reg data is null or len is 0");
1116 ret = aw_dev_update_reg_container(aw88399, data, len);
1118 dev_err(aw88399->aw_pa->dev, "reg update failed");
1123 static int aw88399_dev_get_prof_name(struct aw_device *aw_dev, int index, char **prof_name)
1125 struct aw_prof_info *prof_info = &aw_dev->prof_info;
1126 struct aw_prof_desc *prof_desc;
1128 if ((index >= aw_dev->prof_info.count) || (index < 0)) {
1129 dev_err(aw_dev->dev, "index[%d] overflow count[%d]",
1130 index, aw_dev->prof_info.count);
1134 prof_desc = &aw_dev->prof_info.prof_desc[index];
1136 *prof_name = prof_info->prof_name_list[prof_desc->id];
1141 static int aw88399_dev_get_prof_data(struct aw_device *aw_dev, int index,
1142 struct aw_prof_desc **prof_desc)
1144 if ((index >= aw_dev->prof_info.count) || (index < 0)) {
1145 dev_err(aw_dev->dev, "%s: index[%d] overflow count[%d]\n",
1146 __func__, index, aw_dev->prof_info.count);
1150 *prof_desc = &aw_dev->prof_info.prof_desc[index];
1155 static int aw88399_dev_fw_update(struct aw88399 *aw88399, bool up_dsp_fw_en, bool force_up_en)
1157 struct aw_device *aw_dev = aw88399->aw_pa;
1158 struct aw_prof_desc *prof_index_desc;
1159 struct aw_sec_data_desc *sec_desc;
1163 if ((aw_dev->prof_cur == aw_dev->prof_index) &&
1164 (force_up_en == AW88399_FORCE_UPDATE_OFF)) {
1165 dev_dbg(aw_dev->dev, "scene no change, not update");
1169 if (aw_dev->fw_status == AW88399_DEV_FW_FAILED) {
1170 dev_err(aw_dev->dev, "fw status[%d] error", aw_dev->fw_status);
1174 ret = aw88399_dev_get_prof_name(aw_dev, aw_dev->prof_index, &prof_name);
1178 dev_dbg(aw_dev->dev, "start update %s", prof_name);
1180 ret = aw88399_dev_get_prof_data(aw_dev, aw_dev->prof_index, &prof_index_desc);
1185 sec_desc = prof_index_desc->sec_desc;
1186 ret = aw_dev_reg_update(aw88399, sec_desc[AW88395_DATA_TYPE_REG].data,
1187 sec_desc[AW88395_DATA_TYPE_REG].len);
1189 dev_err(aw_dev->dev, "update reg failed");
1193 aw88399_dev_mute(aw_dev, true);
1195 if (aw_dev->dsp_cfg == AW88399_DEV_DSP_WORK)
1196 aw_dev_dsp_enable(aw_dev, false);
1198 aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_OSC);
1200 ret = aw_dev_check_sram(aw_dev);
1202 dev_err(aw_dev->dev, "check sram failed");
1207 dev_dbg(aw_dev->dev, "fw_ver: [%x]", prof_index_desc->fw_ver);
1208 ret = aw_dev_dsp_update_fw(aw_dev, sec_desc[AW88395_DATA_TYPE_DSP_FW].data,
1209 sec_desc[AW88395_DATA_TYPE_DSP_FW].len);
1211 dev_err(aw_dev->dev, "update dsp fw failed");
1216 /* update dsp config */
1217 ret = aw_dev_dsp_update_cfg(aw_dev, sec_desc[AW88395_DATA_TYPE_DSP_CFG].data,
1218 sec_desc[AW88395_DATA_TYPE_DSP_CFG].len);
1220 dev_err(aw_dev->dev, "update dsp cfg failed");
1224 aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_PLL);
1226 aw_dev->prof_cur = aw_dev->prof_index;
1231 aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_PLL);
1235 static void aw88399_start_pa(struct aw88399 *aw88399)
1239 for (i = 0; i < AW88399_START_RETRIES; i++) {
1240 ret = aw88399_dev_start(aw88399);
1242 dev_err(aw88399->aw_pa->dev, "aw88399 device start failed. retry = %d", i);
1243 ret = aw88399_dev_fw_update(aw88399, AW88399_DSP_FW_UPDATE_ON, true);
1245 dev_err(aw88399->aw_pa->dev, "fw update failed");
1249 dev_dbg(aw88399->aw_pa->dev, "start success\n");
1255 static void aw88399_startup_work(struct work_struct *work)
1257 struct aw88399 *aw88399 =
1258 container_of(work, struct aw88399, start_work.work);
1260 mutex_lock(&aw88399->lock);
1261 aw88399_start_pa(aw88399);
1262 mutex_unlock(&aw88399->lock);
1265 static void aw88399_start(struct aw88399 *aw88399, bool sync_start)
1269 if (aw88399->aw_pa->fw_status != AW88399_DEV_FW_OK)
1272 if (aw88399->aw_pa->status == AW88399_DEV_PW_ON)
1275 ret = aw88399_dev_fw_update(aw88399, AW88399_DSP_FW_UPDATE_OFF, true);
1277 dev_err(aw88399->aw_pa->dev, "fw update failed.");
1281 if (sync_start == AW88399_SYNC_START)
1282 aw88399_start_pa(aw88399);
1284 queue_delayed_work(system_wq,
1285 &aw88399->start_work,
1286 AW88399_START_WORK_DELAY_MS);
1289 static int aw_dev_check_sysint(struct aw_device *aw_dev)
1293 aw_dev_get_int_status(aw_dev, ®_val);
1294 if (reg_val & AW88399_BIT_SYSINT_CHECK) {
1295 dev_err(aw_dev->dev, "pa stop check fail:0x%04x", reg_val);
1302 static int aw88399_stop(struct aw_device *aw_dev)
1304 struct aw_sec_data_desc *dsp_cfg =
1305 &aw_dev->prof_info.prof_desc[aw_dev->prof_cur].sec_desc[AW88395_DATA_TYPE_DSP_CFG];
1306 struct aw_sec_data_desc *dsp_fw =
1307 &aw_dev->prof_info.prof_desc[aw_dev->prof_cur].sec_desc[AW88395_DATA_TYPE_DSP_FW];
1310 if (aw_dev->status == AW88399_DEV_PW_OFF) {
1311 dev_dbg(aw_dev->dev, "already power off");
1315 aw_dev->status = AW88399_DEV_PW_OFF;
1317 aw88399_dev_mute(aw_dev, true);
1318 usleep_range(AW88399_4000_US, AW88399_4000_US + 100);
1320 aw_dev_i2s_tx_enable(aw_dev, false);
1321 usleep_range(AW88399_1000_US, AW88399_1000_US + 100);
1323 int_st = aw_dev_check_sysint(aw_dev);
1325 aw_dev_dsp_enable(aw_dev, false);
1327 aw_dev_amppd(aw_dev, true);
1330 aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_OSC);
1331 aw_dev_dsp_update_fw(aw_dev, dsp_fw->data, dsp_fw->len);
1332 aw_dev_dsp_update_cfg(aw_dev, dsp_cfg->data, dsp_cfg->len);
1333 aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_PLL);
1336 aw_dev_pwd(aw_dev, true);
1341 static struct snd_soc_dai_driver aw88399_dai[] = {
1343 .name = "aw88399-aif",
1346 .stream_name = "Speaker_Playback",
1349 .rates = AW88399_RATES,
1350 .formats = AW88399_FORMATS,
1353 .stream_name = "Speaker_Capture",
1356 .rates = AW88399_RATES,
1357 .formats = AW88399_FORMATS,
1362 static int aw88399_get_fade_in_time(struct snd_kcontrol *kcontrol,
1363 struct snd_ctl_elem_value *ucontrol)
1365 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1366 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component);
1367 struct aw_device *aw_dev = aw88399->aw_pa;
1369 ucontrol->value.integer.value[0] = aw_dev->fade_in_time;
1374 static int aw88399_set_fade_in_time(struct snd_kcontrol *kcontrol,
1375 struct snd_ctl_elem_value *ucontrol)
1377 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1378 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component);
1379 struct soc_mixer_control *mc =
1380 (struct soc_mixer_control *)kcontrol->private_value;
1381 struct aw_device *aw_dev = aw88399->aw_pa;
1384 time = ucontrol->value.integer.value[0];
1386 if (time < mc->min || time > mc->max)
1389 if (time != aw_dev->fade_in_time) {
1390 aw_dev->fade_in_time = time;
1397 static int aw88399_get_fade_out_time(struct snd_kcontrol *kcontrol,
1398 struct snd_ctl_elem_value *ucontrol)
1400 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1401 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component);
1402 struct aw_device *aw_dev = aw88399->aw_pa;
1404 ucontrol->value.integer.value[0] = aw_dev->fade_out_time;
1409 static int aw88399_set_fade_out_time(struct snd_kcontrol *kcontrol,
1410 struct snd_ctl_elem_value *ucontrol)
1412 struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
1413 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component);
1414 struct soc_mixer_control *mc =
1415 (struct soc_mixer_control *)kcontrol->private_value;
1416 struct aw_device *aw_dev = aw88399->aw_pa;
1419 time = ucontrol->value.integer.value[0];
1420 if (time < mc->min || time > mc->max)
1423 if (time != aw_dev->fade_out_time) {
1424 aw_dev->fade_out_time = time;
1431 static int aw88399_dev_set_profile_index(struct aw_device *aw_dev, int index)
1433 /* check the index whether is valid */
1434 if ((index >= aw_dev->prof_info.count) || (index < 0))
1436 /* check the index whether change */
1437 if (aw_dev->prof_index == index)
1440 aw_dev->prof_index = index;
1441 dev_dbg(aw_dev->dev, "set prof[%s]",
1442 aw_dev->prof_info.prof_name_list[aw_dev->prof_info.prof_desc[index].id]);
1447 static int aw88399_profile_info(struct snd_kcontrol *kcontrol,
1448 struct snd_ctl_elem_info *uinfo)
1450 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1451 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1452 char *prof_name, *name;
1455 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
1458 count = aw88399->aw_pa->prof_info.count;
1460 uinfo->value.enumerated.items = 0;
1464 uinfo->value.enumerated.items = count;
1466 if (uinfo->value.enumerated.item >= count)
1467 uinfo->value.enumerated.item = count - 1;
1469 name = uinfo->value.enumerated.name;
1470 count = uinfo->value.enumerated.item;
1472 ret = aw88399_dev_get_prof_name(aw88399->aw_pa, count, &prof_name);
1474 strscpy(uinfo->value.enumerated.name, "null",
1475 strlen("null") + 1);
1479 strscpy(name, prof_name, sizeof(uinfo->value.enumerated.name));
1484 static int aw88399_profile_get(struct snd_kcontrol *kcontrol,
1485 struct snd_ctl_elem_value *ucontrol)
1487 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1488 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1490 ucontrol->value.integer.value[0] = aw88399->aw_pa->prof_index;
1495 static int aw88399_profile_set(struct snd_kcontrol *kcontrol,
1496 struct snd_ctl_elem_value *ucontrol)
1498 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1499 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1502 mutex_lock(&aw88399->lock);
1503 ret = aw88399_dev_set_profile_index(aw88399->aw_pa, ucontrol->value.integer.value[0]);
1505 dev_dbg(codec->dev, "profile index does not change");
1506 mutex_unlock(&aw88399->lock);
1510 if (aw88399->aw_pa->status) {
1511 aw88399_stop(aw88399->aw_pa);
1512 aw88399_start(aw88399, AW88399_SYNC_START);
1515 mutex_unlock(&aw88399->lock);
1520 static int aw88399_volume_get(struct snd_kcontrol *kcontrol,
1521 struct snd_ctl_elem_value *ucontrol)
1523 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1524 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1525 struct aw_volume_desc *vol_desc = &aw88399->aw_pa->volume_desc;
1527 ucontrol->value.integer.value[0] = vol_desc->ctl_volume;
1532 static int aw88399_volume_set(struct snd_kcontrol *kcontrol,
1533 struct snd_ctl_elem_value *ucontrol)
1535 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1536 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1537 struct aw_volume_desc *vol_desc = &aw88399->aw_pa->volume_desc;
1538 struct soc_mixer_control *mc =
1539 (struct soc_mixer_control *)kcontrol->private_value;
1542 value = ucontrol->value.integer.value[0];
1543 if (value < mc->min || value > mc->max)
1546 if (vol_desc->ctl_volume != value) {
1547 vol_desc->ctl_volume = value;
1548 aw_dev_set_volume(aw88399->aw_pa, vol_desc->ctl_volume);
1556 static int aw88399_get_fade_step(struct snd_kcontrol *kcontrol,
1557 struct snd_ctl_elem_value *ucontrol)
1559 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1560 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1562 ucontrol->value.integer.value[0] = aw88399->aw_pa->fade_step;
1567 static int aw88399_set_fade_step(struct snd_kcontrol *kcontrol,
1568 struct snd_ctl_elem_value *ucontrol)
1570 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1571 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1572 struct soc_mixer_control *mc =
1573 (struct soc_mixer_control *)kcontrol->private_value;
1576 value = ucontrol->value.integer.value[0];
1577 if (value < mc->min || value > mc->max)
1580 if (aw88399->aw_pa->fade_step != value) {
1581 aw88399->aw_pa->fade_step = value;
1588 static int aw88399_re_get(struct snd_kcontrol *kcontrol,
1589 struct snd_ctl_elem_value *ucontrol)
1591 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1592 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1593 struct aw_device *aw_dev = aw88399->aw_pa;
1595 ucontrol->value.integer.value[0] = aw_dev->cali_desc.cali_re;
1600 static int aw88399_re_set(struct snd_kcontrol *kcontrol,
1601 struct snd_ctl_elem_value *ucontrol)
1603 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1604 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec);
1605 struct soc_mixer_control *mc =
1606 (struct soc_mixer_control *)kcontrol->private_value;
1607 struct aw_device *aw_dev = aw88399->aw_pa;
1610 value = ucontrol->value.integer.value[0];
1611 if (value < mc->min || value > mc->max)
1614 if (aw_dev->cali_desc.cali_re != value) {
1615 aw_dev->cali_desc.cali_re = value;
1622 static int aw88399_dev_init(struct aw88399 *aw88399, struct aw_container *aw_cfg)
1624 struct aw_device *aw_dev = aw88399->aw_pa;
1627 ret = aw88395_dev_cfg_load(aw_dev, aw_cfg);
1629 dev_err(aw_dev->dev, "aw_dev acf parse failed");
1632 aw_dev->fade_in_time = AW88399_1000_US / 10;
1633 aw_dev->fade_out_time = AW88399_1000_US >> 1;
1634 aw_dev->prof_cur = aw_dev->prof_info.prof_desc[0].id;
1635 aw_dev->prof_index = aw_dev->prof_info.prof_desc[0].id;
1637 ret = aw88399_dev_fw_update(aw88399, AW88399_FORCE_UPDATE_ON, AW88399_DSP_FW_UPDATE_ON);
1639 dev_err(aw_dev->dev, "fw update failed ret = %d\n", ret);
1643 aw88399_dev_mute(aw_dev, true);
1645 /* close tx feedback */
1646 aw_dev_i2s_tx_enable(aw_dev, false);
1647 usleep_range(AW88399_1000_US, AW88399_1000_US + 100);
1650 aw_dev_amppd(aw_dev, true);
1653 aw_dev_dsp_enable(aw_dev, false);
1654 /* set power down */
1655 aw_dev_pwd(aw_dev, true);
1660 static int aw88399_request_firmware_file(struct aw88399 *aw88399)
1662 const struct firmware *cont = NULL;
1665 aw88399->aw_pa->fw_status = AW88399_DEV_FW_FAILED;
1667 ret = request_firmware(&cont, AW88399_ACF_FILE, aw88399->aw_pa->dev);
1669 dev_err(aw88399->aw_pa->dev, "request [%s] failed!", AW88399_ACF_FILE);
1673 dev_dbg(aw88399->aw_pa->dev, "loaded %s - size: %zu\n",
1674 AW88399_ACF_FILE, cont ? cont->size : 0);
1676 aw88399->aw_cfg = devm_kzalloc(aw88399->aw_pa->dev,
1677 struct_size(aw88399->aw_cfg, data, cont->size), GFP_KERNEL);
1678 if (!aw88399->aw_cfg) {
1679 release_firmware(cont);
1682 aw88399->aw_cfg->len = (int)cont->size;
1683 memcpy(aw88399->aw_cfg->data, cont->data, cont->size);
1684 release_firmware(cont);
1686 ret = aw88395_dev_load_acf_check(aw88399->aw_pa, aw88399->aw_cfg);
1688 dev_err(aw88399->aw_pa->dev, "load [%s] failed!", AW88399_ACF_FILE);
1692 mutex_lock(&aw88399->lock);
1693 /* aw device init */
1694 ret = aw88399_dev_init(aw88399, aw88399->aw_cfg);
1696 dev_err(aw88399->aw_pa->dev, "dev init failed");
1697 mutex_unlock(&aw88399->lock);
1702 static const struct snd_kcontrol_new aw88399_controls[] = {
1703 SOC_SINGLE_EXT("PCM Playback Volume", AW88399_SYSCTRL2_REG,
1704 6, AW88399_MUTE_VOL, 0, aw88399_volume_get,
1705 aw88399_volume_set),
1706 SOC_SINGLE_EXT("Fade Step", 0, 0, AW88399_MUTE_VOL, 0,
1707 aw88399_get_fade_step, aw88399_set_fade_step),
1708 SOC_SINGLE_EXT("Volume Ramp Up Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
1709 aw88399_get_fade_in_time, aw88399_set_fade_in_time),
1710 SOC_SINGLE_EXT("Volume Ramp Down Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN,
1711 aw88399_get_fade_out_time, aw88399_set_fade_out_time),
1712 SOC_SINGLE_EXT("Calib", 0, 0, AW88399_CALI_RE_MAX, 0,
1713 aw88399_re_get, aw88399_re_set),
1714 AW88399_PROFILE_EXT("AW88399 Profile Set", aw88399_profile_info,
1715 aw88399_profile_get, aw88399_profile_set),
1718 static int aw88399_playback_event(struct snd_soc_dapm_widget *w,
1719 struct snd_kcontrol *k, int event)
1721 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
1722 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component);
1724 mutex_lock(&aw88399->lock);
1726 case SND_SOC_DAPM_PRE_PMU:
1727 aw88399_start(aw88399, AW88399_ASYNC_START);
1729 case SND_SOC_DAPM_POST_PMD:
1730 aw88399_stop(aw88399->aw_pa);
1735 mutex_unlock(&aw88399->lock);
1740 static const struct snd_soc_dapm_widget aw88399_dapm_widgets[] = {
1742 SND_SOC_DAPM_AIF_IN_E("AIF_RX", "Speaker_Playback", 0, 0, 0, 0,
1743 aw88399_playback_event,
1744 SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1745 SND_SOC_DAPM_OUTPUT("DAC Output"),
1748 SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM, 0, 0),
1749 SND_SOC_DAPM_INPUT("ADC Input"),
1752 static const struct snd_soc_dapm_route aw88399_audio_map[] = {
1753 {"DAC Output", NULL, "AIF_RX"},
1754 {"AIF_TX", NULL, "ADC Input"},
1757 static int aw88399_codec_probe(struct snd_soc_component *component)
1759 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component);
1762 INIT_DELAYED_WORK(&aw88399->start_work, aw88399_startup_work);
1764 ret = aw88399_request_firmware_file(aw88399);
1766 dev_err(aw88399->aw_pa->dev, "%s failed\n", __func__);
1771 static void aw88399_codec_remove(struct snd_soc_component *aw_codec)
1773 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(aw_codec);
1775 cancel_delayed_work_sync(&aw88399->start_work);
1778 static const struct snd_soc_component_driver soc_codec_dev_aw88399 = {
1779 .probe = aw88399_codec_probe,
1780 .remove = aw88399_codec_remove,
1781 .dapm_widgets = aw88399_dapm_widgets,
1782 .num_dapm_widgets = ARRAY_SIZE(aw88399_dapm_widgets),
1783 .dapm_routes = aw88399_audio_map,
1784 .num_dapm_routes = ARRAY_SIZE(aw88399_audio_map),
1785 .controls = aw88399_controls,
1786 .num_controls = ARRAY_SIZE(aw88399_controls),
1789 static void aw88399_hw_reset(struct aw88399 *aw88399)
1791 if (aw88399->reset_gpio) {
1792 gpiod_set_value_cansleep(aw88399->reset_gpio, 1);
1793 usleep_range(AW88399_1000_US, AW88399_1000_US + 10);
1794 gpiod_set_value_cansleep(aw88399->reset_gpio, 0);
1795 usleep_range(AW88399_1000_US, AW88399_1000_US + 10);
1796 gpiod_set_value_cansleep(aw88399->reset_gpio, 1);
1797 usleep_range(AW88399_1000_US, AW88399_1000_US + 10);
1801 static void aw88399_parse_channel_dt(struct aw_device *aw_dev)
1803 struct device_node *np = aw_dev->dev->of_node;
1806 of_property_read_u32(np, "awinic,audio-channel", &channel_value);
1807 aw_dev->channel = channel_value;
1810 static int aw88399_init(struct aw88399 *aw88399, struct i2c_client *i2c, struct regmap *regmap)
1812 struct aw_device *aw_dev;
1813 unsigned int chip_id;
1816 ret = regmap_read(regmap, AW88399_ID_REG, &chip_id);
1818 dev_err(&i2c->dev, "%s read chipid error. ret = %d", __func__, ret);
1821 if (chip_id != AW88399_CHIP_ID) {
1822 dev_err(&i2c->dev, "unsupported device");
1825 dev_dbg(&i2c->dev, "chip id = %x\n", chip_id);
1827 aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL);
1830 aw88399->aw_pa = aw_dev;
1833 aw_dev->dev = &i2c->dev;
1834 aw_dev->regmap = regmap;
1835 mutex_init(&aw_dev->dsp_lock);
1837 aw_dev->chip_id = chip_id;
1839 aw_dev->prof_info.prof_desc = NULL;
1840 aw_dev->prof_info.count = 0;
1841 aw_dev->prof_info.prof_type = AW88395_DEV_NONE_TYPE_ID;
1842 aw_dev->channel = AW88399_DEV_DEFAULT_CH;
1843 aw_dev->fw_status = AW88399_DEV_FW_FAILED;
1845 aw_dev->fade_step = AW88399_VOLUME_STEP_DB;
1846 aw_dev->volume_desc.ctl_volume = AW88399_VOL_DEFAULT_VALUE;
1848 aw88399_parse_channel_dt(aw_dev);
1853 static int aw88399_i2c_probe(struct i2c_client *i2c)
1855 struct aw88399 *aw88399;
1858 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C))
1859 return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed");
1861 aw88399 = devm_kzalloc(&i2c->dev, sizeof(*aw88399), GFP_KERNEL);
1865 mutex_init(&aw88399->lock);
1867 i2c_set_clientdata(i2c, aw88399);
1869 aw88399->reset_gpio = devm_gpiod_get_optional(&i2c->dev, "reset", GPIOD_OUT_LOW);
1870 if (IS_ERR(aw88399->reset_gpio))
1871 return dev_err_probe(&i2c->dev, PTR_ERR(aw88399->reset_gpio),
1872 "reset gpio not defined\n");
1873 aw88399_hw_reset(aw88399);
1875 aw88399->regmap = devm_regmap_init_i2c(i2c, &aw88399_remap_config);
1876 if (IS_ERR(aw88399->regmap))
1877 return dev_err_probe(&i2c->dev, PTR_ERR(aw88399->regmap),
1878 "failed to init regmap\n");
1881 ret = aw88399_init(aw88399, i2c, aw88399->regmap);
1885 ret = devm_snd_soc_register_component(&i2c->dev,
1886 &soc_codec_dev_aw88399,
1887 aw88399_dai, ARRAY_SIZE(aw88399_dai));
1889 dev_err(&i2c->dev, "failed to register aw88399: %d", ret);
1894 static const struct i2c_device_id aw88399_i2c_id[] = {
1895 { AW88399_I2C_NAME },
1898 MODULE_DEVICE_TABLE(i2c, aw88399_i2c_id);
1900 static struct i2c_driver aw88399_i2c_driver = {
1902 .name = AW88399_I2C_NAME,
1904 .probe = aw88399_i2c_probe,
1905 .id_table = aw88399_i2c_id,
1907 module_i2c_driver(aw88399_i2c_driver);
1909 MODULE_DESCRIPTION("ASoC AW88399 Smart PA Driver");
1910 MODULE_LICENSE("GPL v2");