1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // aw88399.c -- ALSA SoC AW88399 codec support 4 // 5 // Copyright (c) 2023 AWINIC Technology CO., LTD 6 // 7 // Author: Weidong Wang <wangweidong.a@awinic.com> 8 // 9 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> 16 #include "aw88399.h" 17 #include "aw88395/aw88395_device.h" 18 #include "aw88395/aw88395_reg.h" 19 20 static const struct regmap_config aw88399_remap_config = { 21 .val_bits = 16, 22 .reg_bits = 8, 23 .max_register = AW88399_REG_MAX, 24 .reg_format_endian = REGMAP_ENDIAN_LITTLE, 25 .val_format_endian = REGMAP_ENDIAN_BIG, 26 }; 27 28 static int aw_dev_dsp_write_16bit(struct aw_device *aw_dev, 29 unsigned short dsp_addr, unsigned int dsp_data) 30 { 31 int ret; 32 33 ret = regmap_write(aw_dev->regmap, AW88399_DSPMADD_REG, dsp_addr); 34 if (ret) { 35 dev_err(aw_dev->dev, "%s write addr error, ret=%d", __func__, ret); 36 return ret; 37 } 38 39 ret = regmap_write(aw_dev->regmap, AW88399_DSPMDAT_REG, (u16)dsp_data); 40 if (ret) { 41 dev_err(aw_dev->dev, "%s write data error, ret=%d", __func__, ret); 42 return ret; 43 } 44 45 return 0; 46 } 47 48 static int aw_dev_dsp_read_16bit(struct aw_device *aw_dev, 49 unsigned short dsp_addr, unsigned int *dsp_data) 50 { 51 unsigned int temp_data; 52 int ret; 53 54 ret = regmap_write(aw_dev->regmap, AW88399_DSPMADD_REG, dsp_addr); 55 if (ret) { 56 dev_err(aw_dev->dev, "%s write error, ret=%d", __func__, ret); 57 return ret; 58 } 59 60 ret = regmap_read(aw_dev->regmap, AW88399_DSPMDAT_REG, &temp_data); 61 if (ret) { 62 dev_err(aw_dev->dev, "%s read error, ret=%d", __func__, ret); 63 return ret; 64 } 65 *dsp_data = temp_data; 66 67 return 0; 68 } 69 70 static int aw_dev_dsp_read_32bit(struct aw_device *aw_dev, 71 unsigned short dsp_addr, unsigned int *dsp_data) 72 { 73 unsigned int temp_data; 74 int ret; 75 76 ret = regmap_write(aw_dev->regmap, AW88399_DSPMADD_REG, dsp_addr); 77 if (ret) { 78 dev_err(aw_dev->dev, "%s write error, ret=%d", __func__, ret); 79 return ret; 80 } 81 82 ret = regmap_read(aw_dev->regmap, AW88399_DSPMDAT_REG, &temp_data); 83 if (ret) { 84 dev_err(aw_dev->dev, "%s read error, ret=%d", __func__, ret); 85 return ret; 86 } 87 *dsp_data = temp_data; 88 89 ret = regmap_read(aw_dev->regmap, AW88399_DSPMDAT_REG, &temp_data); 90 if (ret) { 91 dev_err(aw_dev->dev, "%s read error, ret=%d", __func__, ret); 92 return ret; 93 } 94 *dsp_data |= (temp_data << 16); 95 96 return 0; 97 } 98 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) 101 { 102 u32 reg_value; 103 int ret; 104 105 mutex_lock(&aw_dev->dsp_lock); 106 switch (data_type) { 107 case AW88399_DSP_16_DATA: 108 ret = aw_dev_dsp_read_16bit(aw_dev, dsp_addr, dsp_data); 109 if (ret) 110 dev_err(aw_dev->dev, "read dsp_addr[0x%x] 16-bit dsp_data[0x%x] failed", 111 (u32)dsp_addr, *dsp_data); 112 break; 113 case AW88399_DSP_32_DATA: 114 ret = aw_dev_dsp_read_32bit(aw_dev, dsp_addr, dsp_data); 115 if (ret) 116 dev_err(aw_dev->dev, "read dsp_addr[0x%x] 32r-bit dsp_data[0x%x] failed", 117 (u32)dsp_addr, *dsp_data); 118 break; 119 default: 120 dev_err(aw_dev->dev, "data type[%d] unsupported", data_type); 121 ret = -EINVAL; 122 break; 123 } 124 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); 129 130 return ret; 131 } 132 133 static void aw_dev_pwd(struct aw_device *aw_dev, bool pwd) 134 { 135 int ret; 136 137 if (pwd) 138 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG, 139 ~AW88399_PWDN_MASK, AW88399_PWDN_POWER_DOWN_VALUE); 140 else 141 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG, 142 ~AW88399_PWDN_MASK, AW88399_PWDN_WORKING_VALUE); 143 144 if (ret) 145 dev_dbg(aw_dev->dev, "%s failed", __func__); 146 } 147 148 static void aw_dev_get_int_status(struct aw_device *aw_dev, unsigned short *int_status) 149 { 150 unsigned int reg_val; 151 int ret; 152 153 ret = regmap_read(aw_dev->regmap, AW88399_SYSINT_REG, ®_val); 154 if (ret) 155 dev_err(aw_dev->dev, "read interrupt reg fail, ret=%d", ret); 156 else 157 *int_status = reg_val; 158 159 dev_dbg(aw_dev->dev, "read interrupt reg=0x%04x", *int_status); 160 } 161 162 static void aw_dev_clear_int_status(struct aw_device *aw_dev) 163 { 164 u16 int_status; 165 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); 170 if (int_status) 171 dev_dbg(aw_dev->dev, "int status(%d) is not cleaned.\n", int_status); 172 } 173 174 static int aw_dev_get_iis_status(struct aw_device *aw_dev) 175 { 176 unsigned int reg_val; 177 int ret; 178 179 ret = regmap_read(aw_dev->regmap, AW88399_SYSST_REG, ®_val); 180 if (ret) 181 return ret; 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); 184 return -EINVAL; 185 } 186 187 return 0; 188 } 189 190 static int aw_dev_check_mode1_pll(struct aw_device *aw_dev) 191 { 192 int ret, i; 193 194 for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) { 195 ret = aw_dev_get_iis_status(aw_dev); 196 if (ret) { 197 dev_err(aw_dev->dev, "mode1 iis signal check error"); 198 usleep_range(AW88399_2000_US, AW88399_2000_US + 10); 199 } else { 200 return 0; 201 } 202 } 203 204 return -EPERM; 205 } 206 207 static int aw_dev_check_mode2_pll(struct aw_device *aw_dev) 208 { 209 unsigned int reg_val; 210 int ret, i; 211 212 ret = regmap_read(aw_dev->regmap, AW88399_PLLCTRL2_REG, ®_val); 213 if (ret) 214 return ret; 215 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"); 219 return -EPERM; 220 } 221 222 /* change mode2 */ 223 ret = regmap_update_bits(aw_dev->regmap, AW88399_PLLCTRL2_REG, 224 ~AW88399_CCO_MUX_MASK, AW88399_CCO_MUX_DIVIDED_VALUE); 225 if (ret) 226 return ret; 227 228 for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) { 229 ret = aw_dev_get_iis_status(aw_dev); 230 if (ret) { 231 dev_err(aw_dev->dev, "mode2 iis signal check error"); 232 usleep_range(AW88399_2000_US, AW88399_2000_US + 10); 233 } else { 234 break; 235 } 236 } 237 238 /* change mode1 */ 239 regmap_update_bits(aw_dev->regmap, AW88399_PLLCTRL2_REG, 240 ~AW88399_CCO_MUX_MASK, AW88399_CCO_MUX_BYPASS_VALUE); 241 if (ret == 0) { 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); 245 if (ret) { 246 dev_err(aw_dev->dev, "mode2 switch to mode1, iis signal check error"); 247 usleep_range(AW88399_2000_US, AW88399_2000_US + 10); 248 } else { 249 break; 250 } 251 } 252 } 253 254 return ret; 255 } 256 257 static int aw_dev_check_syspll(struct aw_device *aw_dev) 258 { 259 int ret; 260 261 ret = aw_dev_check_mode1_pll(aw_dev); 262 if (ret) { 263 dev_dbg(aw_dev->dev, "mode1 check iis failed try switch to mode2 check"); 264 ret = aw_dev_check_mode2_pll(aw_dev); 265 if (ret) { 266 dev_err(aw_dev->dev, "mode2 check iis failed"); 267 return ret; 268 } 269 } 270 271 return 0; 272 } 273 274 static int aw_dev_check_sysst(struct aw_device *aw_dev) 275 { 276 unsigned int check_val; 277 unsigned int reg_val; 278 int ret, i; 279 280 ret = regmap_read(aw_dev->regmap, AW88399_PWMCTRL3_REG, ®_val); 281 if (ret) 282 return ret; 283 284 if (reg_val & (~AW88399_NOISE_GATE_EN_MASK)) 285 check_val = AW88399_BIT_SYSST_NOSWS_CHECK; 286 else 287 check_val = AW88399_BIT_SYSST_SWS_CHECK; 288 289 for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) { 290 ret = regmap_read(aw_dev->regmap, AW88399_SYSST_REG, ®_val); 291 if (ret) 292 return ret; 293 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); 298 } else { 299 return 0; 300 } 301 } 302 303 return -EPERM; 304 } 305 306 static void aw_dev_amppd(struct aw_device *aw_dev, bool amppd) 307 { 308 int ret; 309 310 if (amppd) 311 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG, 312 ~AW88399_AMPPD_MASK, AW88399_AMPPD_POWER_DOWN_VALUE); 313 else 314 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG, 315 ~AW88399_AMPPD_MASK, AW88399_AMPPD_WORKING_VALUE); 316 317 if (ret) 318 dev_dbg(aw_dev->dev, "%s failed", __func__); 319 } 320 321 static void aw_dev_dsp_enable(struct aw_device *aw_dev, bool is_enable) 322 { 323 int ret; 324 325 if (is_enable) 326 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG, 327 ~AW88399_DSPBY_MASK, AW88399_DSPBY_WORKING_VALUE); 328 else 329 ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG, 330 ~AW88399_DSPBY_MASK, AW88399_DSPBY_BYPASS_VALUE); 331 332 if (ret) 333 dev_dbg(aw_dev->dev, "%s failed\n", __func__); 334 } 335 336 static int aw88399_dev_get_icalk(struct aw88399 *aw88399, int16_t *icalk) 337 { 338 uint16_t icalkh_val, icalkl_val, icalk_val; 339 struct aw_device *aw_dev = aw88399->aw_pa; 340 unsigned int reg_val; 341 int ret; 342 343 ret = regmap_read(aw_dev->regmap, AW88399_EFRH4_REG, ®_val); 344 if (ret) 345 return ret; 346 icalkh_val = reg_val & (~AW88399_EF_ISN_GESLP_H_MASK); 347 348 ret = regmap_read(aw_dev->regmap, AW88399_EFRL4_REG, ®_val); 349 if (ret) 350 return ret; 351 icalkl_val = reg_val & (~AW88399_EF_ISN_GESLP_L_MASK); 352 353 if (aw88399->check_val == AW_EF_AND_CHECK) 354 icalk_val = icalkh_val & icalkl_val; 355 else 356 icalk_val = icalkh_val | icalkl_val; 357 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; 361 362 return 0; 363 } 364 365 static int aw88399_dev_get_vcalk(struct aw88399 *aw88399, int16_t *vcalk) 366 { 367 uint16_t vcalkh_val, vcalkl_val, vcalk_val; 368 struct aw_device *aw_dev = aw88399->aw_pa; 369 unsigned int reg_val; 370 int ret; 371 372 ret = regmap_read(aw_dev->regmap, AW88399_EFRH3_REG, ®_val); 373 if (ret) 374 return ret; 375 376 vcalkh_val = reg_val & (~AW88399_EF_VSN_GESLP_H_MASK); 377 378 ret = regmap_read(aw_dev->regmap, AW88399_EFRL3_REG, ®_val); 379 if (ret) 380 return ret; 381 382 vcalkl_val = reg_val & (~AW88399_EF_VSN_GESLP_L_MASK); 383 384 if (aw88399->check_val == AW_EF_AND_CHECK) 385 vcalk_val = vcalkh_val & vcalkl_val; 386 else 387 vcalk_val = vcalkh_val | vcalkl_val; 388 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; 392 393 return 0; 394 } 395 396 static int aw88399_dev_get_internal_vcalk(struct aw88399 *aw88399, int16_t *vcalk) 397 { 398 uint16_t vcalkh_val, vcalkl_val, vcalk_val; 399 struct aw_device *aw_dev = aw88399->aw_pa; 400 unsigned int reg_val; 401 int ret; 402 403 ret = regmap_read(aw_dev->regmap, AW88399_EFRH2_REG, ®_val); 404 if (ret) 405 return ret; 406 vcalkh_val = reg_val & (~AW88399_INTERNAL_VSN_TRIM_H_MASK); 407 408 ret = regmap_read(aw_dev->regmap, AW88399_EFRL2_REG, ®_val); 409 if (ret) 410 return ret; 411 vcalkl_val = reg_val & (~AW88399_INTERNAL_VSN_TRIM_L_MASK); 412 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); 416 else 417 vcalk_val = (vcalkh_val >> AW88399_INTERNAL_VSN_TRIM_H_START_BIT) | 418 (vcalkl_val >> AW88399_INTERNAL_VSN_TRIM_L_START_BIT); 419 420 if (vcalk_val & (~AW88399_TEM4_SIGN_MASK)) 421 vcalk_val = vcalk_val | AW88399_TEM4_SIGN_NEG; 422 423 *vcalk = (int16_t)vcalk_val; 424 425 return 0; 426 } 427 428 static int aw_dev_set_vcalb(struct aw88399 *aw88399) 429 { 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; 434 uint16_t reg_val; 435 int ret; 436 437 ret = regmap_read(aw_dev->regmap, AW88399_VSNCTRL1_REG, &vsense_value); 438 if (ret) 439 return ret; 440 441 vsense_select = vsense_value & (~AW88399_VDSEL_MASK); 442 443 ret = aw88399_dev_get_icalk(aw88399, &icalk); 444 if (ret) { 445 dev_err(aw_dev->dev, "get icalk failed\n"); 446 return ret; 447 } 448 449 ical_k = icalk * AW88399_ICABLK_FACTOR + AW88399_CABL_BASE_VALUE; 450 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; 457 break; 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; 464 break; 465 default: 466 dev_err(aw_dev->dev, "%s: unsupport vsense\n", __func__); 467 ret = -EINVAL; 468 break; 469 } 470 if (ret) 471 return ret; 472 473 vcalb = vcalb >> AW88399_VCALB_ADJ_FACTOR; 474 reg_val = (uint32_t)vcalb; 475 476 regmap_write(aw_dev->regmap, AW88399_DSPVCALB_REG, reg_val); 477 478 return 0; 479 } 480 481 static int aw_dev_update_cali_re(struct aw_cali_desc *cali_desc) 482 { 483 struct aw_device *aw_dev = 484 container_of(cali_desc, struct aw_device, cali_desc); 485 uint16_t re_lbits, re_hbits; 486 u32 cali_re; 487 int ret; 488 489 if ((aw_dev->cali_desc.cali_re >= AW88399_CALI_RE_MAX) || 490 (aw_dev->cali_desc.cali_re <= AW88399_CALI_RE_MIN)) 491 return -EINVAL; 492 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); 495 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; 498 499 ret = regmap_write(aw_dev->regmap, AW88399_ACR1_REG, re_hbits); 500 if (ret) { 501 dev_err(aw_dev->dev, "set cali re error"); 502 return ret; 503 } 504 505 ret = regmap_write(aw_dev->regmap, AW88399_ACR2_REG, re_lbits); 506 if (ret) 507 dev_err(aw_dev->dev, "set cali re error"); 508 509 return ret; 510 } 511 512 static int aw_dev_fw_crc_check(struct aw_device *aw_dev) 513 { 514 uint16_t check_val, fw_len_val; 515 unsigned int reg_val; 516 int ret; 517 518 /* calculate fw_end_addr */ 519 fw_len_val = ((aw_dev->dsp_fw_len / AW_FW_ADDR_LEN) - 1) + AW88399_CRC_FW_BASE_ADDR; 520 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); 524 if (ret) 525 return ret; 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); 529 530 usleep_range(AW88399_2000_US, AW88399_2000_US + 10); 531 532 /* read crc check result */ 533 regmap_read(aw_dev->regmap, AW88399_HAGCST_REG, ®_val); 534 if (ret) 535 return ret; 536 537 check_val = (reg_val & (~AW88399_CRC_CHECK_BITS_MASK)) >> AW88399_CRC_CHECK_START_BIT; 538 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); 542 if (ret) 543 return ret; 544 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); 548 ret = -EINVAL; 549 } 550 551 return ret; 552 } 553 554 static int aw_dev_cfg_crc_check(struct aw_device *aw_dev) 555 { 556 uint16_t check_val, cfg_len_val; 557 unsigned int reg_val; 558 int ret; 559 560 /* calculate cfg end addr */ 561 cfg_len_val = ((aw_dev->dsp_cfg_len / AW_FW_ADDR_LEN) - 1) + AW88399_CRC_CFG_BASE_ADDR; 562 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); 566 if (ret) 567 return ret; 568 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); 572 if (ret) 573 return ret; 574 575 usleep_range(AW88399_1000_US, AW88399_1000_US + 10); 576 577 /* read crc check result */ 578 ret = regmap_read(aw_dev->regmap, AW88399_HAGCST_REG, ®_val); 579 if (ret) 580 return ret; 581 582 check_val = (reg_val & (~AW88399_CRC_CHECK_BITS_MASK)) >> AW88399_CRC_CHECK_START_BIT; 583 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); 587 if (ret) 588 return ret; 589 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); 593 ret = -EINVAL; 594 } 595 596 return ret; 597 } 598 599 static int aw_dev_hw_crc_check(struct aw88399 *aw88399) 600 { 601 struct aw_device *aw_dev = aw88399->aw_pa; 602 int ret; 603 604 ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG, 605 ~AW88399_RAM_CG_BYP_MASK, AW88399_RAM_CG_BYP_BYPASS_VALUE); 606 if (ret) 607 return ret; 608 609 ret = aw_dev_fw_crc_check(aw_dev); 610 if (ret) { 611 dev_err(aw_dev->dev, "fw_crc_check failed\n"); 612 goto crc_check_failed; 613 } 614 615 ret = aw_dev_cfg_crc_check(aw_dev); 616 if (ret) { 617 dev_err(aw_dev->dev, "cfg_crc_check failed\n"); 618 goto crc_check_failed; 619 } 620 621 ret = regmap_write(aw_dev->regmap, AW88399_CRCCTRL_REG, aw88399->crc_init_val); 622 if (ret) 623 return ret; 624 625 ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG, 626 ~AW88399_RAM_CG_BYP_MASK, AW88399_RAM_CG_BYP_WORK_VALUE); 627 628 return ret; 629 630 crc_check_failed: 631 regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG, 632 ~AW88399_RAM_CG_BYP_MASK, AW88399_RAM_CG_BYP_WORK_VALUE); 633 return ret; 634 } 635 636 static void aw_dev_i2s_tx_enable(struct aw_device *aw_dev, bool flag) 637 { 638 int ret; 639 640 if (flag) 641 ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCTRL3_REG, 642 ~AW88399_I2STXEN_MASK, AW88399_I2STXEN_ENABLE_VALUE); 643 else 644 ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG, 645 ~AW88399_I2STXEN_MASK, AW88399_I2STXEN_DISABLE_VALUE); 646 647 if (ret) 648 dev_dbg(aw_dev->dev, "%s failed", __func__); 649 } 650 651 static int aw_dev_get_dsp_status(struct aw_device *aw_dev) 652 { 653 unsigned int reg_val; 654 int ret; 655 656 ret = regmap_read(aw_dev->regmap, AW88399_WDT_REG, ®_val); 657 if (ret) 658 return ret; 659 if (!(reg_val & (~AW88399_WDT_CNT_MASK))) 660 ret = -EPERM; 661 662 return 0; 663 } 664 665 static int aw_dev_dsp_check(struct aw_device *aw_dev) 666 { 667 int ret, i; 668 669 switch (aw_dev->dsp_cfg) { 670 case AW88399_DEV_DSP_BYPASS: 671 dev_dbg(aw_dev->dev, "dsp bypass"); 672 ret = 0; 673 break; 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); 680 if (ret) { 681 dev_err(aw_dev->dev, "dsp wdt status error=%d", ret); 682 usleep_range(AW88399_2000_US, AW88399_2000_US + 10); 683 } 684 } 685 break; 686 default: 687 dev_err(aw_dev->dev, "unknown dsp cfg=%d", aw_dev->dsp_cfg); 688 ret = -EINVAL; 689 break; 690 } 691 692 return ret; 693 } 694 695 static int aw_dev_set_volume(struct aw_device *aw_dev, unsigned int value) 696 { 697 struct aw_volume_desc *vol_desc = &aw_dev->volume_desc; 698 unsigned int reg_value; 699 u16 real_value; 700 int ret; 701 702 real_value = min((value + vol_desc->init_volume), (unsigned int)AW88399_MUTE_VOL); 703 704 ret = regmap_read(aw_dev->regmap, AW88399_SYSCTRL2_REG, ®_value); 705 if (ret) 706 return ret; 707 708 dev_dbg(aw_dev->dev, "value 0x%x , reg:0x%x", value, real_value); 709 710 real_value = (real_value << AW88399_VOL_START_BIT) | (reg_value & AW88399_VOL_MASK); 711 712 ret = regmap_write(aw_dev->regmap, AW88399_SYSCTRL2_REG, real_value); 713 714 return ret; 715 } 716 717 static void aw_dev_fade_in(struct aw_device *aw_dev) 718 { 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; 722 int i; 723 724 if (fade_step == 0 || aw_dev->fade_in_time == 0) { 725 aw_dev_set_volume(aw_dev, fade_in_vol); 726 return; 727 } 728 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); 732 } 733 734 if (i != fade_in_vol) 735 aw_dev_set_volume(aw_dev, fade_in_vol); 736 } 737 738 static void aw_dev_fade_out(struct aw_device *aw_dev) 739 { 740 struct aw_volume_desc *desc = &aw_dev->volume_desc; 741 int fade_step = aw_dev->fade_step; 742 int i; 743 744 if (fade_step == 0 || aw_dev->fade_out_time == 0) { 745 aw_dev_set_volume(aw_dev, AW88399_MUTE_VOL); 746 return; 747 } 748 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); 752 } 753 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); 757 } 758 } 759 760 static void aw88399_dev_mute(struct aw_device *aw_dev, bool is_mute) 761 { 762 if (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); 766 } else { 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); 770 } 771 } 772 773 static void aw88399_dev_set_dither(struct aw88399 *aw88399, bool dither) 774 { 775 struct aw_device *aw_dev = aw88399->aw_pa; 776 777 if (dither) 778 regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG, 779 ~AW88399_DITHER_EN_MASK, AW88399_DITHER_EN_ENABLE_VALUE); 780 else 781 regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG, 782 ~AW88399_DITHER_EN_MASK, AW88399_DITHER_EN_DISABLE_VALUE); 783 } 784 785 static int aw88399_dev_start(struct aw88399 *aw88399) 786 { 787 struct aw_device *aw_dev = aw88399->aw_pa; 788 int ret; 789 790 if (aw_dev->status == AW88399_DEV_PW_ON) { 791 dev_dbg(aw_dev->dev, "already power on"); 792 return 0; 793 } 794 795 aw88399_dev_set_dither(aw88399, false); 796 797 /* power on */ 798 aw_dev_pwd(aw_dev, false); 799 usleep_range(AW88399_2000_US, AW88399_2000_US + 10); 800 801 ret = aw_dev_check_syspll(aw_dev); 802 if (ret) { 803 dev_err(aw_dev->dev, "pll check failed cannot start"); 804 goto pll_check_fail; 805 } 806 807 /* amppd on */ 808 aw_dev_amppd(aw_dev, false); 809 usleep_range(AW88399_1000_US, AW88399_1000_US + 50); 810 811 /* check i2s status */ 812 ret = aw_dev_check_sysst(aw_dev); 813 if (ret) { 814 dev_err(aw_dev->dev, "sysst check failed"); 815 goto sysst_check_fail; 816 } 817 818 if (aw_dev->dsp_cfg == AW88399_DEV_DSP_WORK) { 819 ret = aw_dev_hw_crc_check(aw88399); 820 if (ret) { 821 dev_err(aw_dev->dev, "dsp crc check failed"); 822 goto crc_check_fail; 823 } 824 aw_dev_dsp_enable(aw_dev, false); 825 aw_dev_set_vcalb(aw88399); 826 aw_dev_update_cali_re(&aw_dev->cali_desc); 827 828 ret = aw_dev_dsp_check(aw_dev); 829 if (ret) { 830 dev_err(aw_dev->dev, "dsp status check failed"); 831 goto dsp_check_fail; 832 } 833 } else { 834 dev_dbg(aw_dev->dev, "start pa with dsp bypass"); 835 } 836 837 /* enable tx feedback */ 838 aw_dev_i2s_tx_enable(aw_dev, true); 839 840 if (aw88399->dither_st == AW88399_DITHER_EN_ENABLE_VALUE) 841 aw88399_dev_set_dither(aw88399, true); 842 843 /* close mute */ 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; 848 849 return 0; 850 851 dsp_check_fail: 852 crc_check_fail: 853 aw_dev_dsp_enable(aw_dev, false); 854 sysst_check_fail: 855 aw_dev_clear_int_status(aw_dev); 856 aw_dev_amppd(aw_dev, true); 857 pll_check_fail: 858 aw_dev_pwd(aw_dev, true); 859 aw_dev->status = AW88399_DEV_PW_OFF; 860 861 return ret; 862 } 863 864 static int aw_dev_dsp_update_container(struct aw_device *aw_dev, 865 unsigned char *data, unsigned int len, unsigned short base) 866 { 867 u32 tmp_len; 868 int i, ret; 869 870 mutex_lock(&aw_dev->dsp_lock); 871 ret = regmap_write(aw_dev->regmap, AW88399_DSPMADD_REG, base); 872 if (ret) 873 goto error_operation; 874 875 for (i = 0; i < len; i += AW88399_MAX_RAM_WRITE_BYTE_SIZE) { 876 if ((len - i) < AW88399_MAX_RAM_WRITE_BYTE_SIZE) 877 tmp_len = len - i; 878 else 879 tmp_len = AW88399_MAX_RAM_WRITE_BYTE_SIZE; 880 881 ret = regmap_raw_write(aw_dev->regmap, AW88399_DSPMDAT_REG, 882 &data[i], tmp_len); 883 if (ret) 884 goto error_operation; 885 } 886 mutex_unlock(&aw_dev->dsp_lock); 887 888 return 0; 889 890 error_operation: 891 mutex_unlock(&aw_dev->dsp_lock); 892 return ret; 893 } 894 895 static int aw_dev_get_ra(struct aw_cali_desc *cali_desc) 896 { 897 struct aw_device *aw_dev = 898 container_of(cali_desc, struct aw_device, cali_desc); 899 u32 dsp_ra; 900 int ret; 901 902 ret = aw_dev_dsp_read(aw_dev, AW88399_DSP_REG_CFG_ADPZ_RA, 903 &dsp_ra, AW88399_DSP_32_DATA); 904 if (ret) { 905 dev_err(aw_dev->dev, "read ra error"); 906 return ret; 907 } 908 909 cali_desc->ra = AW88399_DSP_RE_TO_SHOW_RE(dsp_ra, 910 AW88399_DSP_RE_SHIFT); 911 912 return 0; 913 } 914 915 static int aw_dev_dsp_update_cfg(struct aw_device *aw_dev, 916 unsigned char *data, unsigned int len) 917 { 918 int ret; 919 920 dev_dbg(aw_dev->dev, "dsp config len:%d", len); 921 922 if (!len || !data) { 923 dev_err(aw_dev->dev, "dsp config data is null or len is 0"); 924 return -EINVAL; 925 } 926 927 ret = aw_dev_dsp_update_container(aw_dev, data, len, AW88399_DSP_CFG_ADDR); 928 if (ret) 929 return ret; 930 931 aw_dev->dsp_cfg_len = len; 932 933 ret = aw_dev_get_ra(&aw_dev->cali_desc); 934 935 return ret; 936 } 937 938 static int aw_dev_dsp_update_fw(struct aw_device *aw_dev, 939 unsigned char *data, unsigned int len) 940 { 941 int ret; 942 943 dev_dbg(aw_dev->dev, "dsp firmware len:%d", len); 944 945 if (!len || !data) { 946 dev_err(aw_dev->dev, "dsp firmware data is null or len is 0"); 947 return -EINVAL; 948 } 949 950 aw_dev->dsp_fw_len = len; 951 ret = aw_dev_dsp_update_container(aw_dev, data, len, AW88399_DSP_FW_ADDR); 952 953 return ret; 954 } 955 956 static int aw_dev_check_sram(struct aw_device *aw_dev) 957 { 958 unsigned int reg_val; 959 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); 966 goto error; 967 } 968 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); 975 goto error; 976 } 977 mutex_unlock(&aw_dev->dsp_lock); 978 979 return 0; 980 error: 981 mutex_unlock(&aw_dev->dsp_lock); 982 return -EPERM; 983 } 984 985 static void aw_dev_select_memclk(struct aw_device *aw_dev, unsigned char flag) 986 { 987 int ret; 988 989 switch (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); 994 if (ret) 995 dev_err(aw_dev->dev, "memclk select pll failed"); 996 break; 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); 1001 if (ret) 1002 dev_err(aw_dev->dev, "memclk select OSC failed"); 1003 break; 1004 default: 1005 dev_err(aw_dev->dev, "unknown memclk config, flag=0x%x", flag); 1006 break; 1007 } 1008 } 1009 1010 static void aw_dev_get_cur_mode_st(struct aw_device *aw_dev) 1011 { 1012 struct aw_profctrl_desc *profctrl_desc = &aw_dev->profctrl_desc; 1013 unsigned int reg_val; 1014 int ret; 1015 1016 ret = regmap_read(aw_dev->regmap, AW88399_SYSCTRL_REG, ®_val); 1017 if (ret) { 1018 dev_dbg(aw_dev->dev, "%s failed", __func__); 1019 return; 1020 } 1021 if ((reg_val & (~AW88399_RCV_MODE_MASK)) == AW88399_RCV_MODE_RECEIVER_VALUE) 1022 profctrl_desc->cur_mode = AW88399_RCV_MODE; 1023 else 1024 profctrl_desc->cur_mode = AW88399_NOT_RCV_MODE; 1025 } 1026 1027 static int aw_dev_update_reg_container(struct aw88399 *aw88399, 1028 unsigned char *data, unsigned int len) 1029 { 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; 1034 int16_t *reg_data; 1035 u8 reg_addr; 1036 1037 reg_data = (int16_t *)data; 1038 data_len = len >> 1; 1039 1040 if (data_len & 0x1) { 1041 dev_err(aw_dev->dev, "data len:%d unsupported", data_len); 1042 return -EINVAL; 1043 } 1044 1045 for (i = 0; i < data_len; i += 2) { 1046 reg_addr = reg_data[i]; 1047 reg_val = reg_data[i + 1]; 1048 1049 if (reg_addr == AW88399_DSPVCALB_REG) { 1050 aw88399->vcalb_init_val = reg_val; 1051 continue; 1052 } 1053 1054 if (reg_addr == AW88399_SYSCTRL_REG) { 1055 if (reg_val & (~AW88399_DSPBY_MASK)) 1056 aw_dev->dsp_cfg = AW88399_DEV_DSP_BYPASS; 1057 else 1058 aw_dev->dsp_cfg = AW88399_DEV_DSP_WORK; 1059 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); 1064 } 1065 1066 if (reg_addr == AW88399_I2SCTRL3_REG) { 1067 reg_val &= AW88399_I2STXEN_MASK; 1068 reg_val |= AW88399_I2STXEN_DISABLE_VALUE; 1069 } 1070 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; 1075 } 1076 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; 1080 else 1081 aw88399->check_val = AW_EF_AND_CHECK; 1082 1083 aw88399->dither_st = reg_val & (~AW88399_DITHER_EN_MASK); 1084 } 1085 1086 if (reg_addr == AW88399_CRCCTRL_REG) 1087 aw88399->crc_init_val = reg_val; 1088 1089 ret = regmap_write(aw_dev->regmap, reg_addr, reg_val); 1090 if (ret) 1091 return ret; 1092 } 1093 1094 aw_dev_pwd(aw_dev, false); 1095 usleep_range(AW88399_1000_US, AW88399_1000_US + 10); 1096 1097 aw_dev_get_cur_mode_st(aw_dev); 1098 1099 if (aw_dev->prof_cur != aw_dev->prof_index) 1100 vol_desc->ctl_volume = 0; 1101 else 1102 aw_dev_set_volume(aw_dev, vol_desc->ctl_volume); 1103 1104 return 0; 1105 } 1106 1107 static int aw_dev_reg_update(struct aw88399 *aw88399, 1108 unsigned char *data, unsigned int len) 1109 { 1110 int ret; 1111 1112 if (!len || !data) { 1113 dev_err(aw88399->aw_pa->dev, "reg data is null or len is 0"); 1114 return -EINVAL; 1115 } 1116 1117 ret = aw_dev_update_reg_container(aw88399, data, len); 1118 if (ret) 1119 dev_err(aw88399->aw_pa->dev, "reg update failed"); 1120 1121 return ret; 1122 } 1123 1124 static int aw88399_dev_get_prof_name(struct aw_device *aw_dev, int index, char **prof_name) 1125 { 1126 struct aw_prof_info *prof_info = &aw_dev->prof_info; 1127 struct aw_prof_desc *prof_desc; 1128 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); 1132 return -EINVAL; 1133 } 1134 1135 prof_desc = &aw_dev->prof_info.prof_desc[index]; 1136 1137 *prof_name = prof_info->prof_name_list[prof_desc->id]; 1138 1139 return 0; 1140 } 1141 1142 static int aw88399_dev_get_prof_data(struct aw_device *aw_dev, int index, 1143 struct aw_prof_desc **prof_desc) 1144 { 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); 1148 return -EINVAL; 1149 } 1150 1151 *prof_desc = &aw_dev->prof_info.prof_desc[index]; 1152 1153 return 0; 1154 } 1155 1156 static int aw88399_dev_fw_update(struct aw88399 *aw88399, bool up_dsp_fw_en, bool force_up_en) 1157 { 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; 1161 char *prof_name; 1162 int ret; 1163 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"); 1167 return 0; 1168 } 1169 1170 if (aw_dev->fw_status == AW88399_DEV_FW_FAILED) { 1171 dev_err(aw_dev->dev, "fw status[%d] error", aw_dev->fw_status); 1172 return -EPERM; 1173 } 1174 1175 ret = aw88399_dev_get_prof_name(aw_dev, aw_dev->prof_index, &prof_name); 1176 if (ret) 1177 return ret; 1178 1179 dev_dbg(aw_dev->dev, "start update %s", prof_name); 1180 1181 ret = aw88399_dev_get_prof_data(aw_dev, aw_dev->prof_index, &prof_index_desc); 1182 if (ret) 1183 return ret; 1184 1185 /* update reg */ 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); 1189 if (ret) { 1190 dev_err(aw_dev->dev, "update reg failed"); 1191 return ret; 1192 } 1193 1194 aw88399_dev_mute(aw_dev, true); 1195 1196 if (aw_dev->dsp_cfg == AW88399_DEV_DSP_WORK) 1197 aw_dev_dsp_enable(aw_dev, false); 1198 1199 aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_OSC); 1200 1201 ret = aw_dev_check_sram(aw_dev); 1202 if (ret) { 1203 dev_err(aw_dev->dev, "check sram failed"); 1204 goto error; 1205 } 1206 1207 if (up_dsp_fw_en) { 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); 1211 if (ret) { 1212 dev_err(aw_dev->dev, "update dsp fw failed"); 1213 goto error; 1214 } 1215 } 1216 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); 1220 if (ret) { 1221 dev_err(aw_dev->dev, "update dsp cfg failed"); 1222 goto error; 1223 } 1224 1225 aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_PLL); 1226 1227 aw_dev->prof_cur = aw_dev->prof_index; 1228 1229 return 0; 1230 1231 error: 1232 aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_PLL); 1233 return ret; 1234 } 1235 1236 static void aw88399_start_pa(struct aw88399 *aw88399) 1237 { 1238 int ret, i; 1239 1240 for (i = 0; i < AW88399_START_RETRIES; i++) { 1241 ret = aw88399_dev_start(aw88399); 1242 if (ret) { 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); 1245 if (ret) { 1246 dev_err(aw88399->aw_pa->dev, "fw update failed"); 1247 continue; 1248 } 1249 } else { 1250 dev_dbg(aw88399->aw_pa->dev, "start success\n"); 1251 break; 1252 } 1253 } 1254 } 1255 1256 static void aw88399_startup_work(struct work_struct *work) 1257 { 1258 struct aw88399 *aw88399 = 1259 container_of(work, struct aw88399, start_work.work); 1260 1261 mutex_lock(&aw88399->lock); 1262 aw88399_start_pa(aw88399); 1263 mutex_unlock(&aw88399->lock); 1264 } 1265 1266 static void aw88399_start(struct aw88399 *aw88399, bool sync_start) 1267 { 1268 int ret; 1269 1270 if (aw88399->aw_pa->fw_status != AW88399_DEV_FW_OK) 1271 return; 1272 1273 if (aw88399->aw_pa->status == AW88399_DEV_PW_ON) 1274 return; 1275 1276 ret = aw88399_dev_fw_update(aw88399, AW88399_DSP_FW_UPDATE_OFF, true); 1277 if (ret) { 1278 dev_err(aw88399->aw_pa->dev, "fw update failed."); 1279 return; 1280 } 1281 1282 if (sync_start == AW88399_SYNC_START) 1283 aw88399_start_pa(aw88399); 1284 else 1285 queue_delayed_work(system_wq, 1286 &aw88399->start_work, 1287 AW88399_START_WORK_DELAY_MS); 1288 } 1289 1290 static int aw_dev_check_sysint(struct aw_device *aw_dev) 1291 { 1292 u16 reg_val; 1293 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); 1297 return -EINVAL; 1298 } 1299 1300 return 0; 1301 } 1302 1303 static int aw88399_stop(struct aw_device *aw_dev) 1304 { 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]; 1309 int int_st; 1310 1311 if (aw_dev->status == AW88399_DEV_PW_OFF) { 1312 dev_dbg(aw_dev->dev, "already power off"); 1313 return 0; 1314 } 1315 1316 aw_dev->status = AW88399_DEV_PW_OFF; 1317 1318 aw88399_dev_mute(aw_dev, true); 1319 usleep_range(AW88399_4000_US, AW88399_4000_US + 100); 1320 1321 aw_dev_i2s_tx_enable(aw_dev, false); 1322 usleep_range(AW88399_1000_US, AW88399_1000_US + 100); 1323 1324 int_st = aw_dev_check_sysint(aw_dev); 1325 1326 aw_dev_dsp_enable(aw_dev, false); 1327 1328 aw_dev_amppd(aw_dev, true); 1329 1330 if (int_st) { 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); 1335 } 1336 1337 aw_dev_pwd(aw_dev, true); 1338 1339 return 0; 1340 } 1341 1342 static struct snd_soc_dai_driver aw88399_dai[] = { 1343 { 1344 .name = "aw88399-aif", 1345 .id = 1, 1346 .playback = { 1347 .stream_name = "Speaker_Playback", 1348 .channels_min = 1, 1349 .channels_max = 2, 1350 .rates = AW88399_RATES, 1351 .formats = AW88399_FORMATS, 1352 }, 1353 .capture = { 1354 .stream_name = "Speaker_Capture", 1355 .channels_min = 1, 1356 .channels_max = 2, 1357 .rates = AW88399_RATES, 1358 .formats = AW88399_FORMATS, 1359 }, 1360 }, 1361 }; 1362 1363 static int aw88399_get_fade_in_time(struct snd_kcontrol *kcontrol, 1364 struct snd_ctl_elem_value *ucontrol) 1365 { 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; 1369 1370 ucontrol->value.integer.value[0] = aw_dev->fade_in_time; 1371 1372 return 0; 1373 } 1374 1375 static int aw88399_set_fade_in_time(struct snd_kcontrol *kcontrol, 1376 struct snd_ctl_elem_value *ucontrol) 1377 { 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; 1383 int time; 1384 1385 time = ucontrol->value.integer.value[0]; 1386 1387 if (time < mc->min || time > mc->max) 1388 return -EINVAL; 1389 1390 if (time != aw_dev->fade_in_time) { 1391 aw_dev->fade_in_time = time; 1392 return 1; 1393 } 1394 1395 return 0; 1396 } 1397 1398 static int aw88399_get_fade_out_time(struct snd_kcontrol *kcontrol, 1399 struct snd_ctl_elem_value *ucontrol) 1400 { 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; 1404 1405 ucontrol->value.integer.value[0] = aw_dev->fade_out_time; 1406 1407 return 0; 1408 } 1409 1410 static int aw88399_set_fade_out_time(struct snd_kcontrol *kcontrol, 1411 struct snd_ctl_elem_value *ucontrol) 1412 { 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; 1418 int time; 1419 1420 time = ucontrol->value.integer.value[0]; 1421 if (time < mc->min || time > mc->max) 1422 return -EINVAL; 1423 1424 if (time != aw_dev->fade_out_time) { 1425 aw_dev->fade_out_time = time; 1426 return 1; 1427 } 1428 1429 return 0; 1430 } 1431 1432 static int aw88399_dev_set_profile_index(struct aw_device *aw_dev, int index) 1433 { 1434 /* check the index whether is valid */ 1435 if ((index >= aw_dev->prof_info.count) || (index < 0)) 1436 return -EINVAL; 1437 /* check the index whether change */ 1438 if (aw_dev->prof_index == index) 1439 return -EINVAL; 1440 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]); 1444 1445 return 0; 1446 } 1447 1448 static int aw88399_profile_info(struct snd_kcontrol *kcontrol, 1449 struct snd_ctl_elem_info *uinfo) 1450 { 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; 1454 int count, ret; 1455 1456 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 1457 uinfo->count = 1; 1458 1459 count = aw88399->aw_pa->prof_info.count; 1460 if (count <= 0) { 1461 uinfo->value.enumerated.items = 0; 1462 return 0; 1463 } 1464 1465 uinfo->value.enumerated.items = count; 1466 1467 if (uinfo->value.enumerated.item >= count) 1468 uinfo->value.enumerated.item = count - 1; 1469 1470 name = uinfo->value.enumerated.name; 1471 count = uinfo->value.enumerated.item; 1472 1473 ret = aw88399_dev_get_prof_name(aw88399->aw_pa, count, &prof_name); 1474 if (ret) { 1475 strscpy(uinfo->value.enumerated.name, "null", 1476 strlen("null") + 1); 1477 return 0; 1478 } 1479 1480 strscpy(name, prof_name, sizeof(uinfo->value.enumerated.name)); 1481 1482 return 0; 1483 } 1484 1485 static int aw88399_profile_get(struct snd_kcontrol *kcontrol, 1486 struct snd_ctl_elem_value *ucontrol) 1487 { 1488 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 1489 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); 1490 1491 ucontrol->value.integer.value[0] = aw88399->aw_pa->prof_index; 1492 1493 return 0; 1494 } 1495 1496 static int aw88399_profile_set(struct snd_kcontrol *kcontrol, 1497 struct snd_ctl_elem_value *ucontrol) 1498 { 1499 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 1500 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); 1501 int ret; 1502 1503 mutex_lock(&aw88399->lock); 1504 ret = aw88399_dev_set_profile_index(aw88399->aw_pa, ucontrol->value.integer.value[0]); 1505 if (ret) { 1506 dev_dbg(codec->dev, "profile index does not change"); 1507 mutex_unlock(&aw88399->lock); 1508 return 0; 1509 } 1510 1511 if (aw88399->aw_pa->status) { 1512 aw88399_stop(aw88399->aw_pa); 1513 aw88399_start(aw88399, AW88399_SYNC_START); 1514 } 1515 1516 mutex_unlock(&aw88399->lock); 1517 1518 return 1; 1519 } 1520 1521 static int aw88399_volume_get(struct snd_kcontrol *kcontrol, 1522 struct snd_ctl_elem_value *ucontrol) 1523 { 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; 1527 1528 ucontrol->value.integer.value[0] = vol_desc->ctl_volume; 1529 1530 return 0; 1531 } 1532 1533 static int aw88399_volume_set(struct snd_kcontrol *kcontrol, 1534 struct snd_ctl_elem_value *ucontrol) 1535 { 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; 1541 int value; 1542 1543 value = ucontrol->value.integer.value[0]; 1544 if (value < mc->min || value > mc->max) 1545 return -EINVAL; 1546 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); 1550 1551 return 1; 1552 } 1553 1554 return 0; 1555 } 1556 1557 static int aw88399_get_fade_step(struct snd_kcontrol *kcontrol, 1558 struct snd_ctl_elem_value *ucontrol) 1559 { 1560 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 1561 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); 1562 1563 ucontrol->value.integer.value[0] = aw88399->aw_pa->fade_step; 1564 1565 return 0; 1566 } 1567 1568 static int aw88399_set_fade_step(struct snd_kcontrol *kcontrol, 1569 struct snd_ctl_elem_value *ucontrol) 1570 { 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; 1575 int value; 1576 1577 value = ucontrol->value.integer.value[0]; 1578 if (value < mc->min || value > mc->max) 1579 return -EINVAL; 1580 1581 if (aw88399->aw_pa->fade_step != value) { 1582 aw88399->aw_pa->fade_step = value; 1583 return 1; 1584 } 1585 1586 return 0; 1587 } 1588 1589 static int aw88399_re_get(struct snd_kcontrol *kcontrol, 1590 struct snd_ctl_elem_value *ucontrol) 1591 { 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; 1595 1596 ucontrol->value.integer.value[0] = aw_dev->cali_desc.cali_re; 1597 1598 return 0; 1599 } 1600 1601 static int aw88399_re_set(struct snd_kcontrol *kcontrol, 1602 struct snd_ctl_elem_value *ucontrol) 1603 { 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; 1609 int value; 1610 1611 value = ucontrol->value.integer.value[0]; 1612 if (value < mc->min || value > mc->max) 1613 return -EINVAL; 1614 1615 if (aw_dev->cali_desc.cali_re != value) { 1616 aw_dev->cali_desc.cali_re = value; 1617 return 1; 1618 } 1619 1620 return 0; 1621 } 1622 1623 static int aw88399_dev_init(struct aw88399 *aw88399, struct aw_container *aw_cfg) 1624 { 1625 struct aw_device *aw_dev = aw88399->aw_pa; 1626 int ret; 1627 1628 ret = aw88395_dev_cfg_load(aw_dev, aw_cfg); 1629 if (ret) { 1630 dev_err(aw_dev->dev, "aw_dev acf parse failed"); 1631 return -EINVAL; 1632 } 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; 1637 1638 ret = aw88399_dev_fw_update(aw88399, AW88399_FORCE_UPDATE_ON, AW88399_DSP_FW_UPDATE_ON); 1639 if (ret) { 1640 dev_err(aw_dev->dev, "fw update failed ret = %d\n", ret); 1641 return ret; 1642 } 1643 1644 aw88399_dev_mute(aw_dev, true); 1645 1646 /* close tx feedback */ 1647 aw_dev_i2s_tx_enable(aw_dev, false); 1648 usleep_range(AW88399_1000_US, AW88399_1000_US + 100); 1649 1650 /* enable amppd */ 1651 aw_dev_amppd(aw_dev, true); 1652 1653 /* close dsp */ 1654 aw_dev_dsp_enable(aw_dev, false); 1655 /* set power down */ 1656 aw_dev_pwd(aw_dev, true); 1657 1658 return 0; 1659 } 1660 1661 static int aw88399_request_firmware_file(struct aw88399 *aw88399) 1662 { 1663 const struct firmware *cont = NULL; 1664 int ret; 1665 1666 aw88399->aw_pa->fw_status = AW88399_DEV_FW_FAILED; 1667 1668 ret = request_firmware(&cont, AW88399_ACF_FILE, aw88399->aw_pa->dev); 1669 if (ret) { 1670 dev_err(aw88399->aw_pa->dev, "request [%s] failed!", AW88399_ACF_FILE); 1671 return ret; 1672 } 1673 1674 dev_dbg(aw88399->aw_pa->dev, "loaded %s - size: %zu\n", 1675 AW88399_ACF_FILE, cont ? cont->size : 0); 1676 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); 1681 return -ENOMEM; 1682 } 1683 aw88399->aw_cfg->len = (int)cont->size; 1684 memcpy(aw88399->aw_cfg->data, cont->data, cont->size); 1685 release_firmware(cont); 1686 1687 ret = aw88395_dev_load_acf_check(aw88399->aw_pa, aw88399->aw_cfg); 1688 if (ret) { 1689 dev_err(aw88399->aw_pa->dev, "load [%s] failed!", AW88399_ACF_FILE); 1690 return ret; 1691 } 1692 1693 mutex_lock(&aw88399->lock); 1694 /* aw device init */ 1695 ret = aw88399_dev_init(aw88399, aw88399->aw_cfg); 1696 if (ret) 1697 dev_err(aw88399->aw_pa->dev, "dev init failed"); 1698 mutex_unlock(&aw88399->lock); 1699 1700 return ret; 1701 } 1702 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, AW88399_CALI_RE_MAX, 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), 1717 }; 1718 1719 static int aw88399_playback_event(struct snd_soc_dapm_widget *w, 1720 struct snd_kcontrol *k, int event) 1721 { 1722 struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); 1723 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component); 1724 1725 mutex_lock(&aw88399->lock); 1726 switch (event) { 1727 case SND_SOC_DAPM_PRE_PMU: 1728 aw88399_start(aw88399, AW88399_ASYNC_START); 1729 break; 1730 case SND_SOC_DAPM_POST_PMD: 1731 aw88399_stop(aw88399->aw_pa); 1732 break; 1733 default: 1734 break; 1735 } 1736 mutex_unlock(&aw88399->lock); 1737 1738 return 0; 1739 } 1740 1741 static const struct snd_soc_dapm_widget aw88399_dapm_widgets[] = { 1742 /* playback */ 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"), 1747 1748 /* capture */ 1749 SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM, 0, 0), 1750 SND_SOC_DAPM_INPUT("ADC Input"), 1751 }; 1752 1753 static const struct snd_soc_dapm_route aw88399_audio_map[] = { 1754 {"DAC Output", NULL, "AIF_RX"}, 1755 {"AIF_TX", NULL, "ADC Input"}, 1756 }; 1757 1758 static int aw88399_codec_probe(struct snd_soc_component *component) 1759 { 1760 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component); 1761 int ret; 1762 1763 INIT_DELAYED_WORK(&aw88399->start_work, aw88399_startup_work); 1764 1765 ret = aw88399_request_firmware_file(aw88399); 1766 if (ret) 1767 dev_err(aw88399->aw_pa->dev, "%s failed\n", __func__); 1768 1769 return ret; 1770 } 1771 1772 static void aw88399_codec_remove(struct snd_soc_component *aw_codec) 1773 { 1774 struct aw88399 *aw88399 = snd_soc_component_get_drvdata(aw_codec); 1775 1776 cancel_delayed_work_sync(&aw88399->start_work); 1777 } 1778 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), 1788 }; 1789 1790 static void aw88399_hw_reset(struct aw88399 *aw88399) 1791 { 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); 1799 } 1800 } 1801 1802 static void aw88399_parse_channel_dt(struct aw_device *aw_dev) 1803 { 1804 struct device_node *np = aw_dev->dev->of_node; 1805 u32 channel_value; 1806 1807 of_property_read_u32(np, "awinic,audio-channel", &channel_value); 1808 aw_dev->channel = channel_value; 1809 } 1810 1811 static int aw88399_init(struct aw88399 *aw88399, struct i2c_client *i2c, struct regmap *regmap) 1812 { 1813 struct aw_device *aw_dev; 1814 unsigned int chip_id; 1815 int ret; 1816 1817 ret = regmap_read(regmap, AW88399_ID_REG, &chip_id); 1818 if (ret) { 1819 dev_err(&i2c->dev, "%s read chipid error. ret = %d", __func__, ret); 1820 return ret; 1821 } 1822 if (chip_id != AW88399_CHIP_ID) { 1823 dev_err(&i2c->dev, "unsupported device"); 1824 return -ENXIO; 1825 } 1826 dev_dbg(&i2c->dev, "chip id = %x\n", chip_id); 1827 1828 aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL); 1829 if (!aw_dev) 1830 return -ENOMEM; 1831 aw88399->aw_pa = aw_dev; 1832 1833 aw_dev->i2c = i2c; 1834 aw_dev->dev = &i2c->dev; 1835 aw_dev->regmap = regmap; 1836 mutex_init(&aw_dev->dsp_lock); 1837 1838 aw_dev->chip_id = chip_id; 1839 aw_dev->acf = NULL; 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; 1845 1846 aw_dev->fade_step = AW88399_VOLUME_STEP_DB; 1847 aw_dev->volume_desc.ctl_volume = AW88399_VOL_DEFAULT_VALUE; 1848 1849 aw88399_parse_channel_dt(aw_dev); 1850 1851 return 0; 1852 } 1853 1854 static int aw88399_i2c_probe(struct i2c_client *i2c) 1855 { 1856 struct aw88399 *aw88399; 1857 int ret; 1858 1859 if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C)) 1860 return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed"); 1861 1862 aw88399 = devm_kzalloc(&i2c->dev, sizeof(*aw88399), GFP_KERNEL); 1863 if (!aw88399) 1864 return -ENOMEM; 1865 1866 mutex_init(&aw88399->lock); 1867 1868 i2c_set_clientdata(i2c, aw88399); 1869 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); 1875 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"); 1880 1881 /* aw pa init */ 1882 ret = aw88399_init(aw88399, i2c, aw88399->regmap); 1883 if (ret) 1884 return ret; 1885 1886 ret = devm_snd_soc_register_component(&i2c->dev, 1887 &soc_codec_dev_aw88399, 1888 aw88399_dai, ARRAY_SIZE(aw88399_dai)); 1889 if (ret) 1890 dev_err(&i2c->dev, "failed to register aw88399: %d", ret); 1891 1892 return ret; 1893 } 1894 1895 static const struct i2c_device_id aw88399_i2c_id[] = { 1896 { AW88399_I2C_NAME, 0 }, 1897 { } 1898 }; 1899 MODULE_DEVICE_TABLE(i2c, aw88399_i2c_id); 1900 1901 static struct i2c_driver aw88399_i2c_driver = { 1902 .driver = { 1903 .name = AW88399_I2C_NAME, 1904 }, 1905 .probe = aw88399_i2c_probe, 1906 .id_table = aw88399_i2c_id, 1907 }; 1908 module_i2c_driver(aw88399_i2c_driver); 1909 1910 MODULE_DESCRIPTION("ASoC AW88399 Smart PA Driver"); 1911 MODULE_LICENSE("GPL v2"); 1912