| 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 <[email protected]> |
| 8 |
// |
| 9 |
|
| 10 |
#include <linux/crc32.h> |
| 11 |
#include <linux/gpio/consumer.h> |
| 12 |
#include <linux/i2c.h> |
| 13 |
#include <linux/firmware.h> |
| 14 |
#include <linux/minmax.h> |
| 15 |
#include <linux/regmap.h> |
| 16 |
#include <linux/sort.h> |
| 17 |
#include <sound/soc.h> |
| 18 |
#include "aw88399.h" |
| 19 |
#include "aw88395/aw88395_device.h" |
| 20 |
|
| 21 |
static const struct regmap_config aw88399_remap_config = { |
| 22 |
.val_bits = 16, |
| 23 |
.reg_bits = 8, |
| 24 |
.max_register = AW88399_REG_MAX, |
| 25 |
.reg_format_endian = REGMAP_ENDIAN_LITTLE, |
| 26 |
.val_format_endian = REGMAP_ENDIAN_BIG, |
| 27 |
}; |
| 28 |
|
| 29 |
static void aw_dev_pwd(struct aw_device *aw_dev, bool pwd) |
| 30 |
{ |
| 31 |
int ret; |
| 32 |
|
| 33 |
if (pwd) |
| 34 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG, |
| 35 |
~AW88399_PWDN_MASK, AW88399_PWDN_POWER_DOWN_VALUE); |
| 36 |
else |
| 37 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG, |
| 38 |
~AW88399_PWDN_MASK, AW88399_PWDN_WORKING_VALUE); |
| 39 |
|
| 40 |
if (ret) |
| 41 |
dev_dbg(aw_dev->dev, "%s failed", __func__); |
| 42 |
} |
| 43 |
|
| 44 |
static void aw_dev_get_int_status(struct aw_device *aw_dev, unsigned short *int_status) |
| 45 |
{ |
| 46 |
unsigned int reg_val; |
| 47 |
int ret; |
| 48 |
|
| 49 |
ret = regmap_read(aw_dev->regmap, AW88399_SYSINT_REG, ®_val); |
| 50 |
if (ret) |
| 51 |
dev_err(aw_dev->dev, "read interrupt reg fail, ret=%d", ret); |
| 52 |
else |
| 53 |
*int_status = reg_val; |
| 54 |
|
| 55 |
dev_dbg(aw_dev->dev, "read interrupt reg=0x%04x", *int_status); |
| 56 |
} |
| 57 |
|
| 58 |
static void aw_dev_clear_int_status(struct aw_device *aw_dev) |
| 59 |
{ |
| 60 |
u16 int_status; |
| 61 |
|
| 62 |
/* read int status and clear */ |
| 63 |
aw_dev_get_int_status(aw_dev, &int_status); |
| 64 |
/* make sure int status is clear */ |
| 65 |
aw_dev_get_int_status(aw_dev, &int_status); |
| 66 |
if (int_status) |
| 67 |
dev_dbg(aw_dev->dev, "int status(%d) is not cleaned.\n", int_status); |
| 68 |
} |
| 69 |
|
| 70 |
static int aw_dev_get_iis_status(struct aw_device *aw_dev) |
| 71 |
{ |
| 72 |
unsigned int reg_val; |
| 73 |
int ret; |
| 74 |
|
| 75 |
ret = regmap_read(aw_dev->regmap, AW88399_SYSST_REG, ®_val); |
| 76 |
if (ret) |
| 77 |
return ret; |
| 78 |
if ((reg_val & AW88399_BIT_PLL_CHECK) != AW88399_BIT_PLL_CHECK) { |
| 79 |
dev_err(aw_dev->dev, "check pll lock fail, reg_val:0x%04x", reg_val); |
| 80 |
return -EINVAL; |
| 81 |
} |
| 82 |
|
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
static int aw_dev_check_mode1_pll(struct aw_device *aw_dev) |
| 87 |
{ |
| 88 |
int ret, i; |
| 89 |
|
| 90 |
for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) { |
| 91 |
ret = aw_dev_get_iis_status(aw_dev); |
| 92 |
if (ret) { |
| 93 |
dev_err(aw_dev->dev, "mode1 iis signal check error"); |
| 94 |
usleep_range(AW88399_2000_US, AW88399_2000_US + 10); |
| 95 |
} else { |
| 96 |
return 0; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return -EPERM; |
| 101 |
} |
| 102 |
|
| 103 |
static int aw_dev_check_mode2_pll(struct aw_device *aw_dev) |
| 104 |
{ |
| 105 |
unsigned int reg_val; |
| 106 |
int ret, i; |
| 107 |
|
| 108 |
ret = regmap_read(aw_dev->regmap, AW88399_PLLCTRL2_REG, ®_val); |
| 109 |
if (ret) |
| 110 |
return ret; |
| 111 |
|
| 112 |
reg_val &= (~AW88399_CCO_MUX_MASK); |
| 113 |
if (reg_val == AW88399_CCO_MUX_DIVIDED_VALUE) { |
| 114 |
dev_dbg(aw_dev->dev, "CCO_MUX is already divider"); |
| 115 |
return -EPERM; |
| 116 |
} |
| 117 |
|
| 118 |
/* change mode2 */ |
| 119 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_PLLCTRL2_REG, |
| 120 |
~AW88399_CCO_MUX_MASK, AW88399_CCO_MUX_DIVIDED_VALUE); |
| 121 |
if (ret) |
| 122 |
return ret; |
| 123 |
|
| 124 |
for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) { |
| 125 |
ret = aw_dev_get_iis_status(aw_dev); |
| 126 |
if (ret) { |
| 127 |
dev_err(aw_dev->dev, "mode2 iis signal check error"); |
| 128 |
usleep_range(AW88399_2000_US, AW88399_2000_US + 10); |
| 129 |
} else { |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
/* change mode1 */ |
| 135 |
regmap_update_bits(aw_dev->regmap, AW88399_PLLCTRL2_REG, |
| 136 |
~AW88399_CCO_MUX_MASK, AW88399_CCO_MUX_BYPASS_VALUE); |
| 137 |
if (ret == 0) { |
| 138 |
usleep_range(AW88399_2000_US, AW88399_2000_US + 10); |
| 139 |
for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) { |
| 140 |
ret = aw_dev_get_iis_status(aw_dev); |
| 141 |
if (ret) { |
| 142 |
dev_err(aw_dev->dev, "mode2 switch to mode1, iis signal check error"); |
| 143 |
usleep_range(AW88399_2000_US, AW88399_2000_US + 10); |
| 144 |
} else { |
| 145 |
break; |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return ret; |
| 151 |
} |
| 152 |
|
| 153 |
static int aw_dev_check_syspll(struct aw_device *aw_dev) |
| 154 |
{ |
| 155 |
int ret; |
| 156 |
|
| 157 |
ret = aw_dev_check_mode1_pll(aw_dev); |
| 158 |
if (ret) { |
| 159 |
dev_dbg(aw_dev->dev, "mode1 check iis failed try switch to mode2 check"); |
| 160 |
ret = aw_dev_check_mode2_pll(aw_dev); |
| 161 |
if (ret) { |
| 162 |
dev_err(aw_dev->dev, "mode2 check iis failed"); |
| 163 |
return ret; |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
static int aw_dev_check_sysst(struct aw_device *aw_dev) |
| 171 |
{ |
| 172 |
unsigned int check_val; |
| 173 |
unsigned int reg_val; |
| 174 |
int ret, i; |
| 175 |
|
| 176 |
ret = regmap_read(aw_dev->regmap, AW88399_PWMCTRL3_REG, ®_val); |
| 177 |
if (ret) |
| 178 |
return ret; |
| 179 |
|
| 180 |
if (reg_val & (~AW88399_NOISE_GATE_EN_MASK)) |
| 181 |
check_val = AW88399_BIT_SYSST_NOSWS_CHECK; |
| 182 |
else |
| 183 |
check_val = AW88399_BIT_SYSST_SWS_CHECK; |
| 184 |
|
| 185 |
for (i = 0; i < AW88399_DEV_SYSST_CHECK_MAX; i++) { |
| 186 |
ret = regmap_read(aw_dev->regmap, AW88399_SYSST_REG, ®_val); |
| 187 |
if (ret) |
| 188 |
return ret; |
| 189 |
|
| 190 |
if ((reg_val & (~AW88399_BIT_SYSST_CHECK_MASK) & check_val) != check_val) { |
| 191 |
dev_err(aw_dev->dev, "check sysst fail, cnt=%d, reg_val=0x%04x, check:0x%x", |
| 192 |
i, reg_val, AW88399_BIT_SYSST_NOSWS_CHECK); |
| 193 |
usleep_range(AW88399_2000_US, AW88399_2000_US + 10); |
| 194 |
} else { |
| 195 |
return 0; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
return -EPERM; |
| 200 |
} |
| 201 |
|
| 202 |
static void aw_dev_amppd(struct aw_device *aw_dev, bool amppd) |
| 203 |
{ |
| 204 |
int ret; |
| 205 |
|
| 206 |
if (amppd) |
| 207 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG, |
| 208 |
~AW88399_AMPPD_MASK, AW88399_AMPPD_POWER_DOWN_VALUE); |
| 209 |
else |
| 210 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG, |
| 211 |
~AW88399_AMPPD_MASK, AW88399_AMPPD_WORKING_VALUE); |
| 212 |
|
| 213 |
if (ret) |
| 214 |
dev_dbg(aw_dev->dev, "%s failed", __func__); |
| 215 |
} |
| 216 |
|
| 217 |
static void aw_dev_dsp_enable(struct aw_device *aw_dev, bool is_enable) |
| 218 |
{ |
| 219 |
int ret; |
| 220 |
|
| 221 |
if (is_enable) |
| 222 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG, |
| 223 |
~AW88399_DSPBY_MASK, AW88399_DSPBY_WORKING_VALUE); |
| 224 |
else |
| 225 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG, |
| 226 |
~AW88399_DSPBY_MASK, AW88399_DSPBY_BYPASS_VALUE); |
| 227 |
|
| 228 |
if (ret) |
| 229 |
dev_dbg(aw_dev->dev, "%s failed\n", __func__); |
| 230 |
} |
| 231 |
|
| 232 |
static int aw88399_dev_get_icalk(struct aw88399 *aw88399, int16_t *icalk) |
| 233 |
{ |
| 234 |
uint16_t icalkh_val, icalkl_val, icalk_val; |
| 235 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 236 |
unsigned int reg_val; |
| 237 |
int ret; |
| 238 |
|
| 239 |
ret = regmap_read(aw_dev->regmap, AW88399_EFRH4_REG, ®_val); |
| 240 |
if (ret) |
| 241 |
return ret; |
| 242 |
icalkh_val = reg_val & (~AW88399_EF_ISN_GESLP_H_MASK); |
| 243 |
|
| 244 |
ret = regmap_read(aw_dev->regmap, AW88399_EFRL4_REG, ®_val); |
| 245 |
if (ret) |
| 246 |
return ret; |
| 247 |
icalkl_val = reg_val & (~AW88399_EF_ISN_GESLP_L_MASK); |
| 248 |
|
| 249 |
if (aw88399->check_val == AW_EF_AND_CHECK) |
| 250 |
icalk_val = icalkh_val & icalkl_val; |
| 251 |
else |
| 252 |
icalk_val = icalkh_val | icalkl_val; |
| 253 |
|
| 254 |
if (icalk_val & (~AW88399_EF_ISN_GESLP_SIGN_MASK)) |
| 255 |
icalk_val = icalk_val | AW88399_EF_ISN_GESLP_SIGN_NEG; |
| 256 |
*icalk = (int16_t)icalk_val; |
| 257 |
|
| 258 |
return 0; |
| 259 |
} |
| 260 |
|
| 261 |
static int aw88399_dev_get_vcalk(struct aw88399 *aw88399, int16_t *vcalk) |
| 262 |
{ |
| 263 |
uint16_t vcalkh_val, vcalkl_val, vcalk_val; |
| 264 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 265 |
unsigned int reg_val; |
| 266 |
int ret; |
| 267 |
|
| 268 |
ret = regmap_read(aw_dev->regmap, AW88399_EFRH3_REG, ®_val); |
| 269 |
if (ret) |
| 270 |
return ret; |
| 271 |
|
| 272 |
vcalkh_val = reg_val & (~AW88399_EF_VSN_GESLP_H_MASK); |
| 273 |
|
| 274 |
ret = regmap_read(aw_dev->regmap, AW88399_EFRL3_REG, ®_val); |
| 275 |
if (ret) |
| 276 |
return ret; |
| 277 |
|
| 278 |
vcalkl_val = reg_val & (~AW88399_EF_VSN_GESLP_L_MASK); |
| 279 |
|
| 280 |
if (aw88399->check_val == AW_EF_AND_CHECK) |
| 281 |
vcalk_val = vcalkh_val & vcalkl_val; |
| 282 |
else |
| 283 |
vcalk_val = vcalkh_val | vcalkl_val; |
| 284 |
|
| 285 |
if (vcalk_val & AW88399_EF_VSN_GESLP_SIGN_MASK) |
| 286 |
vcalk_val = vcalk_val | AW88399_EF_VSN_GESLP_SIGN_NEG; |
| 287 |
*vcalk = (int16_t)vcalk_val; |
| 288 |
|
| 289 |
return 0; |
| 290 |
} |
| 291 |
|
| 292 |
static int aw88399_dev_get_internal_vcalk(struct aw88399 *aw88399, int16_t *vcalk) |
| 293 |
{ |
| 294 |
uint16_t vcalkh_val, vcalkl_val, vcalk_val; |
| 295 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 296 |
unsigned int reg_val; |
| 297 |
int ret; |
| 298 |
|
| 299 |
ret = regmap_read(aw_dev->regmap, AW88399_EFRH2_REG, ®_val); |
| 300 |
if (ret) |
| 301 |
return ret; |
| 302 |
vcalkh_val = reg_val & (~AW88399_INTERNAL_VSN_TRIM_H_MASK); |
| 303 |
|
| 304 |
ret = regmap_read(aw_dev->regmap, AW88399_EFRL2_REG, ®_val); |
| 305 |
if (ret) |
| 306 |
return ret; |
| 307 |
vcalkl_val = reg_val & (~AW88399_INTERNAL_VSN_TRIM_L_MASK); |
| 308 |
|
| 309 |
if (aw88399->check_val == AW_EF_AND_CHECK) |
| 310 |
vcalk_val = (vcalkh_val >> AW88399_INTERNAL_VSN_TRIM_H_START_BIT) & |
| 311 |
(vcalkl_val >> AW88399_INTERNAL_VSN_TRIM_L_START_BIT); |
| 312 |
else |
| 313 |
vcalk_val = (vcalkh_val >> AW88399_INTERNAL_VSN_TRIM_H_START_BIT) | |
| 314 |
(vcalkl_val >> AW88399_INTERNAL_VSN_TRIM_L_START_BIT); |
| 315 |
|
| 316 |
if (vcalk_val & (~AW88399_TEM4_SIGN_MASK)) |
| 317 |
vcalk_val = vcalk_val | AW88399_TEM4_SIGN_NEG; |
| 318 |
|
| 319 |
*vcalk = (int16_t)vcalk_val; |
| 320 |
|
| 321 |
return 0; |
| 322 |
} |
| 323 |
|
| 324 |
static int aw_dev_set_vcalb(struct aw88399 *aw88399) |
| 325 |
{ |
| 326 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 327 |
unsigned int vsense_select, vsense_value; |
| 328 |
int32_t ical_k, vcal_k, vcalb; |
| 329 |
int16_t icalk, vcalk; |
| 330 |
uint16_t reg_val; |
| 331 |
int ret; |
| 332 |
|
| 333 |
ret = regmap_read(aw_dev->regmap, AW88399_VSNCTRL1_REG, &vsense_value); |
| 334 |
if (ret) |
| 335 |
return ret; |
| 336 |
|
| 337 |
vsense_select = vsense_value & (~AW88399_VDSEL_MASK); |
| 338 |
|
| 339 |
ret = aw88399_dev_get_icalk(aw88399, &icalk); |
| 340 |
if (ret) { |
| 341 |
dev_err(aw_dev->dev, "get icalk failed\n"); |
| 342 |
return ret; |
| 343 |
} |
| 344 |
|
| 345 |
ical_k = icalk * AW88399_ICABLK_FACTOR + AW88399_CABL_BASE_VALUE; |
| 346 |
|
| 347 |
switch (vsense_select) { |
| 348 |
case AW88399_DEV_VDSEL_VSENSE: |
| 349 |
ret = aw88399_dev_get_vcalk(aw88399, &vcalk); |
| 350 |
vcal_k = vcalk * AW88399_VCABLK_FACTOR + AW88399_CABL_BASE_VALUE; |
| 351 |
vcalb = AW88399_VCALB_ACCURACY * AW88399_VSCAL_FACTOR / AW88399_ISCAL_FACTOR * |
| 352 |
ical_k / vcal_k * aw88399->vcalb_init_val; |
| 353 |
break; |
| 354 |
case AW88399_DEV_VDSEL_DAC: |
| 355 |
ret = aw88399_dev_get_internal_vcalk(aw88399, &vcalk); |
| 356 |
vcal_k = vcalk * AW88399_VCABLK_DAC_FACTOR + AW88399_CABL_BASE_VALUE; |
| 357 |
vcalb = AW88399_VCALB_ACCURACY * AW88399_VSCAL_DAC_FACTOR / |
| 358 |
AW88399_ISCAL_DAC_FACTOR * ical_k / |
| 359 |
vcal_k * aw88399->vcalb_init_val; |
| 360 |
break; |
| 361 |
default: |
| 362 |
dev_err(aw_dev->dev, "%s: unsupported vsense\n", __func__); |
| 363 |
ret = -EINVAL; |
| 364 |
break; |
| 365 |
} |
| 366 |
if (ret) |
| 367 |
return ret; |
| 368 |
|
| 369 |
vcalb = vcalb >> AW88399_VCALB_ADJ_FACTOR; |
| 370 |
reg_val = (uint32_t)vcalb; |
| 371 |
|
| 372 |
regmap_write(aw_dev->regmap, AW88399_DSPVCALB_REG, reg_val); |
| 373 |
|
| 374 |
return 0; |
| 375 |
} |
| 376 |
|
| 377 |
static int aw_dev_update_cali_re(struct aw_cali_desc *cali_desc) |
| 378 |
{ |
| 379 |
struct aw_device *aw_dev = |
| 380 |
container_of(cali_desc, struct aw_device, cali_desc); |
| 381 |
uint16_t re_lbits, re_hbits; |
| 382 |
u32 cali_re; |
| 383 |
int ret; |
| 384 |
|
| 385 |
if ((aw_dev->cali_desc.cali_re >= AW88399_CALI_RE_MAX) || |
| 386 |
(aw_dev->cali_desc.cali_re <= AW88399_CALI_RE_MIN)) |
| 387 |
return -EINVAL; |
| 388 |
|
| 389 |
cali_re = AW88399_SHOW_RE_TO_DSP_RE((aw_dev->cali_desc.cali_re + |
| 390 |
aw_dev->cali_desc.ra), AW88399_DSP_RE_SHIFT); |
| 391 |
|
| 392 |
re_hbits = (cali_re & (~AW88399_CALI_RE_HBITS_MASK)) >> AW88399_CALI_RE_HBITS_SHIFT; |
| 393 |
re_lbits = (cali_re & (~AW88399_CALI_RE_LBITS_MASK)) >> AW88399_CALI_RE_LBITS_SHIFT; |
| 394 |
|
| 395 |
ret = regmap_write(aw_dev->regmap, AW88399_ACR1_REG, re_hbits); |
| 396 |
if (ret) { |
| 397 |
dev_err(aw_dev->dev, "set cali re error"); |
| 398 |
return ret; |
| 399 |
} |
| 400 |
|
| 401 |
ret = regmap_write(aw_dev->regmap, AW88399_ACR2_REG, re_lbits); |
| 402 |
if (ret) |
| 403 |
dev_err(aw_dev->dev, "set cali re error"); |
| 404 |
|
| 405 |
return ret; |
| 406 |
} |
| 407 |
|
| 408 |
static int aw_dev_fw_crc_check(struct aw_device *aw_dev) |
| 409 |
{ |
| 410 |
uint16_t check_val, fw_len_val; |
| 411 |
unsigned int reg_val; |
| 412 |
int ret; |
| 413 |
|
| 414 |
/* calculate fw_end_addr */ |
| 415 |
fw_len_val = ((aw_dev->dsp_fw_len / AW_FW_ADDR_LEN) - 1) + AW88399_CRC_FW_BASE_ADDR; |
| 416 |
|
| 417 |
/* write fw_end_addr to crc_end_addr */ |
| 418 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG, |
| 419 |
~AW88399_CRC_END_ADDR_MASK, fw_len_val); |
| 420 |
if (ret) |
| 421 |
return ret; |
| 422 |
/* enable fw crc check */ |
| 423 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG, |
| 424 |
~AW88399_CRC_CODE_EN_MASK, AW88399_CRC_CODE_EN_ENABLE_VALUE); |
| 425 |
|
| 426 |
usleep_range(AW88399_2000_US, AW88399_2000_US + 10); |
| 427 |
|
| 428 |
/* read crc check result */ |
| 429 |
regmap_read(aw_dev->regmap, AW88399_HAGCST_REG, ®_val); |
| 430 |
if (ret) |
| 431 |
return ret; |
| 432 |
|
| 433 |
check_val = (reg_val & (~AW88399_CRC_CHECK_BITS_MASK)) >> AW88399_CRC_CHECK_START_BIT; |
| 434 |
|
| 435 |
/* disable fw crc check */ |
| 436 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG, |
| 437 |
~AW88399_CRC_CODE_EN_MASK, AW88399_CRC_CODE_EN_DISABLE_VALUE); |
| 438 |
if (ret) |
| 439 |
return ret; |
| 440 |
|
| 441 |
if (check_val != AW88399_CRC_CHECK_PASS_VAL) { |
| 442 |
dev_err(aw_dev->dev, "%s failed, check_val 0x%x != 0x%x", |
| 443 |
__func__, check_val, AW88399_CRC_CHECK_PASS_VAL); |
| 444 |
ret = -EINVAL; |
| 445 |
} |
| 446 |
|
| 447 |
return ret; |
| 448 |
} |
| 449 |
|
| 450 |
static int aw_dev_cfg_crc_check(struct aw_device *aw_dev) |
| 451 |
{ |
| 452 |
uint16_t check_val, cfg_len_val; |
| 453 |
unsigned int reg_val; |
| 454 |
int ret; |
| 455 |
|
| 456 |
/* calculate cfg end addr */ |
| 457 |
cfg_len_val = ((aw_dev->dsp_cfg_len / AW_FW_ADDR_LEN) - 1) + AW88399_CRC_CFG_BASE_ADDR; |
| 458 |
|
| 459 |
/* write cfg_end_addr to crc_end_addr */ |
| 460 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG, |
| 461 |
~AW88399_CRC_END_ADDR_MASK, cfg_len_val); |
| 462 |
if (ret) |
| 463 |
return ret; |
| 464 |
|
| 465 |
/* enable cfg crc check */ |
| 466 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG, |
| 467 |
~AW88399_CRC_CFG_EN_MASK, AW88399_CRC_CFG_EN_ENABLE_VALUE); |
| 468 |
if (ret) |
| 469 |
return ret; |
| 470 |
|
| 471 |
usleep_range(AW88399_1000_US, AW88399_1000_US + 10); |
| 472 |
|
| 473 |
/* read crc check result */ |
| 474 |
ret = regmap_read(aw_dev->regmap, AW88399_HAGCST_REG, ®_val); |
| 475 |
if (ret) |
| 476 |
return ret; |
| 477 |
|
| 478 |
check_val = (reg_val & (~AW88399_CRC_CHECK_BITS_MASK)) >> AW88399_CRC_CHECK_START_BIT; |
| 479 |
|
| 480 |
/* disable cfg crc check */ |
| 481 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_CRCCTRL_REG, |
| 482 |
~AW88399_CRC_CFG_EN_MASK, AW88399_CRC_CFG_EN_DISABLE_VALUE); |
| 483 |
if (ret) |
| 484 |
return ret; |
| 485 |
|
| 486 |
if (check_val != AW88399_CRC_CHECK_PASS_VAL) { |
| 487 |
dev_err(aw_dev->dev, "crc_check failed, check val 0x%x != 0x%x", |
| 488 |
check_val, AW88399_CRC_CHECK_PASS_VAL); |
| 489 |
ret = -EINVAL; |
| 490 |
} |
| 491 |
|
| 492 |
return ret; |
| 493 |
} |
| 494 |
|
| 495 |
static int aw_dev_hw_crc_check(struct aw88399 *aw88399) |
| 496 |
{ |
| 497 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 498 |
int ret; |
| 499 |
|
| 500 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG, |
| 501 |
~AW88399_RAM_CG_BYP_MASK, AW88399_RAM_CG_BYP_BYPASS_VALUE); |
| 502 |
if (ret) |
| 503 |
return ret; |
| 504 |
|
| 505 |
ret = aw_dev_fw_crc_check(aw_dev); |
| 506 |
if (ret) { |
| 507 |
dev_err(aw_dev->dev, "fw_crc_check failed\n"); |
| 508 |
goto crc_check_failed; |
| 509 |
} |
| 510 |
|
| 511 |
ret = aw_dev_cfg_crc_check(aw_dev); |
| 512 |
if (ret) { |
| 513 |
dev_err(aw_dev->dev, "cfg_crc_check failed\n"); |
| 514 |
goto crc_check_failed; |
| 515 |
} |
| 516 |
|
| 517 |
ret = regmap_write(aw_dev->regmap, AW88399_CRCCTRL_REG, aw88399->crc_init_val); |
| 518 |
if (ret) |
| 519 |
return ret; |
| 520 |
|
| 521 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG, |
| 522 |
~AW88399_RAM_CG_BYP_MASK, AW88399_RAM_CG_BYP_WORK_VALUE); |
| 523 |
|
| 524 |
return ret; |
| 525 |
|
| 526 |
crc_check_failed: |
| 527 |
regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG, |
| 528 |
~AW88399_RAM_CG_BYP_MASK, AW88399_RAM_CG_BYP_WORK_VALUE); |
| 529 |
return ret; |
| 530 |
} |
| 531 |
|
| 532 |
static void aw_dev_i2s_tx_enable(struct aw_device *aw_dev, bool flag) |
| 533 |
{ |
| 534 |
int ret; |
| 535 |
|
| 536 |
if (flag) |
| 537 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCTRL3_REG, |
| 538 |
~AW88399_I2STXEN_MASK, AW88399_I2STXEN_ENABLE_VALUE); |
| 539 |
else |
| 540 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_I2SCFG1_REG, |
| 541 |
~AW88399_I2STXEN_MASK, AW88399_I2STXEN_DISABLE_VALUE); |
| 542 |
|
| 543 |
if (ret) |
| 544 |
dev_dbg(aw_dev->dev, "%s failed", __func__); |
| 545 |
} |
| 546 |
|
| 547 |
static int aw_dev_get_dsp_status(struct aw_device *aw_dev) |
| 548 |
{ |
| 549 |
unsigned int reg_val; |
| 550 |
int ret; |
| 551 |
|
| 552 |
ret = regmap_read(aw_dev->regmap, AW88399_WDT_REG, ®_val); |
| 553 |
if (ret) |
| 554 |
return ret; |
| 555 |
if (!(reg_val & (~AW88399_WDT_CNT_MASK))) |
| 556 |
return -EPERM; |
| 557 |
|
| 558 |
return 0; |
| 559 |
} |
| 560 |
|
| 561 |
static int aw_dev_dsp_check(struct aw_device *aw_dev) |
| 562 |
{ |
| 563 |
int ret, i; |
| 564 |
|
| 565 |
switch (aw_dev->dsp_cfg) { |
| 566 |
case AW88399_DEV_DSP_BYPASS: |
| 567 |
dev_dbg(aw_dev->dev, "dsp bypass"); |
| 568 |
ret = 0; |
| 569 |
break; |
| 570 |
case AW88399_DEV_DSP_WORK: |
| 571 |
aw_dev_dsp_enable(aw_dev, false); |
| 572 |
aw_dev_dsp_enable(aw_dev, true); |
| 573 |
usleep_range(AW88399_1000_US, AW88399_1000_US + 10); |
| 574 |
for (i = 0; i < AW88399_DEV_DSP_CHECK_MAX; i++) { |
| 575 |
ret = aw_dev_get_dsp_status(aw_dev); |
| 576 |
if (ret) { |
| 577 |
dev_err(aw_dev->dev, "dsp wdt status error=%d", ret); |
| 578 |
usleep_range(AW88399_2000_US, AW88399_2000_US + 10); |
| 579 |
} |
| 580 |
} |
| 581 |
break; |
| 582 |
default: |
| 583 |
dev_err(aw_dev->dev, "unknown dsp cfg=%d", aw_dev->dsp_cfg); |
| 584 |
ret = -EINVAL; |
| 585 |
break; |
| 586 |
} |
| 587 |
|
| 588 |
return ret; |
| 589 |
} |
| 590 |
|
| 591 |
static int aw_dev_set_volume(struct aw_device *aw_dev, unsigned int value) |
| 592 |
{ |
| 593 |
struct aw_volume_desc *vol_desc = &aw_dev->volume_desc; |
| 594 |
unsigned int reg_value; |
| 595 |
u16 real_value; |
| 596 |
int ret; |
| 597 |
|
| 598 |
real_value = min((value + vol_desc->init_volume), (unsigned int)AW88399_MUTE_VOL); |
| 599 |
|
| 600 |
ret = regmap_read(aw_dev->regmap, AW88399_SYSCTRL2_REG, ®_value); |
| 601 |
if (ret) |
| 602 |
return ret; |
| 603 |
|
| 604 |
dev_dbg(aw_dev->dev, "value 0x%x , reg:0x%x", value, real_value); |
| 605 |
|
| 606 |
real_value = (real_value << AW88399_VOL_START_BIT) | (reg_value & AW88399_VOL_MASK); |
| 607 |
|
| 608 |
ret = regmap_write(aw_dev->regmap, AW88399_SYSCTRL2_REG, real_value); |
| 609 |
|
| 610 |
return ret; |
| 611 |
} |
| 612 |
|
| 613 |
static void aw_dev_fade_in(struct aw_device *aw_dev) |
| 614 |
{ |
| 615 |
struct aw_volume_desc *desc = &aw_dev->volume_desc; |
| 616 |
u16 fade_in_vol = desc->ctl_volume; |
| 617 |
int fade_step = aw_dev->fade_step; |
| 618 |
int i; |
| 619 |
|
| 620 |
if (fade_step == 0 || aw_dev->fade_in_time == 0) { |
| 621 |
aw_dev_set_volume(aw_dev, fade_in_vol); |
| 622 |
return; |
| 623 |
} |
| 624 |
|
| 625 |
for (i = AW88399_MUTE_VOL; i >= fade_in_vol; i -= fade_step) { |
| 626 |
aw_dev_set_volume(aw_dev, i); |
| 627 |
usleep_range(aw_dev->fade_in_time, aw_dev->fade_in_time + 10); |
| 628 |
} |
| 629 |
|
| 630 |
if (i != fade_in_vol) |
| 631 |
aw_dev_set_volume(aw_dev, fade_in_vol); |
| 632 |
} |
| 633 |
|
| 634 |
static void aw_dev_fade_out(struct aw_device *aw_dev) |
| 635 |
{ |
| 636 |
struct aw_volume_desc *desc = &aw_dev->volume_desc; |
| 637 |
int fade_step = aw_dev->fade_step; |
| 638 |
int i; |
| 639 |
|
| 640 |
if (fade_step == 0 || aw_dev->fade_out_time == 0) { |
| 641 |
aw_dev_set_volume(aw_dev, AW88399_MUTE_VOL); |
| 642 |
return; |
| 643 |
} |
| 644 |
|
| 645 |
for (i = desc->ctl_volume; i <= AW88399_MUTE_VOL; i += fade_step) { |
| 646 |
aw_dev_set_volume(aw_dev, i); |
| 647 |
usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10); |
| 648 |
} |
| 649 |
|
| 650 |
if (i != AW88399_MUTE_VOL) { |
| 651 |
aw_dev_set_volume(aw_dev, AW88399_MUTE_VOL); |
| 652 |
usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10); |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
static void aw88399_dev_mute(struct aw_device *aw_dev, bool is_mute) |
| 657 |
{ |
| 658 |
if (is_mute) { |
| 659 |
aw_dev_fade_out(aw_dev); |
| 660 |
regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG, |
| 661 |
~AW88399_HMUTE_MASK, AW88399_HMUTE_ENABLE_VALUE); |
| 662 |
} else { |
| 663 |
regmap_update_bits(aw_dev->regmap, AW88399_SYSCTRL_REG, |
| 664 |
~AW88399_HMUTE_MASK, AW88399_HMUTE_DISABLE_VALUE); |
| 665 |
aw_dev_fade_in(aw_dev); |
| 666 |
} |
| 667 |
} |
| 668 |
|
| 669 |
static void aw88399_dev_set_dither(struct aw88399 *aw88399, bool dither) |
| 670 |
{ |
| 671 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 672 |
|
| 673 |
if (dither) |
| 674 |
regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG, |
| 675 |
~AW88399_DITHER_EN_MASK, AW88399_DITHER_EN_ENABLE_VALUE); |
| 676 |
else |
| 677 |
regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG, |
| 678 |
~AW88399_DITHER_EN_MASK, AW88399_DITHER_EN_DISABLE_VALUE); |
| 679 |
} |
| 680 |
|
| 681 |
static int aw88399_dev_start(struct aw88399 *aw88399) |
| 682 |
{ |
| 683 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 684 |
int ret; |
| 685 |
|
| 686 |
if (aw_dev->status == AW88399_DEV_PW_ON) { |
| 687 |
dev_dbg(aw_dev->dev, "already power on"); |
| 688 |
return 0; |
| 689 |
} |
| 690 |
|
| 691 |
aw88399_dev_set_dither(aw88399, false); |
| 692 |
|
| 693 |
/* power on */ |
| 694 |
aw_dev_pwd(aw_dev, false); |
| 695 |
usleep_range(AW88399_2000_US, AW88399_2000_US + 10); |
| 696 |
|
| 697 |
ret = aw_dev_check_syspll(aw_dev); |
| 698 |
if (ret) { |
| 699 |
dev_err(aw_dev->dev, "pll check failed cannot start"); |
| 700 |
goto pll_check_fail; |
| 701 |
} |
| 702 |
|
| 703 |
/* amppd on */ |
| 704 |
aw_dev_amppd(aw_dev, false); |
| 705 |
usleep_range(AW88399_1000_US, AW88399_1000_US + 50); |
| 706 |
|
| 707 |
/* check i2s status */ |
| 708 |
ret = aw_dev_check_sysst(aw_dev); |
| 709 |
if (ret) { |
| 710 |
dev_err(aw_dev->dev, "sysst check failed"); |
| 711 |
goto sysst_check_fail; |
| 712 |
} |
| 713 |
|
| 714 |
if (aw_dev->dsp_cfg == AW88399_DEV_DSP_WORK) { |
| 715 |
ret = aw_dev_hw_crc_check(aw88399); |
| 716 |
if (ret) { |
| 717 |
dev_err(aw_dev->dev, "dsp crc check failed"); |
| 718 |
goto crc_check_fail; |
| 719 |
} |
| 720 |
aw_dev_dsp_enable(aw_dev, false); |
| 721 |
aw_dev_set_vcalb(aw88399); |
| 722 |
aw_dev_update_cali_re(&aw_dev->cali_desc); |
| 723 |
|
| 724 |
ret = aw_dev_dsp_check(aw_dev); |
| 725 |
if (ret) { |
| 726 |
dev_err(aw_dev->dev, "dsp status check failed"); |
| 727 |
goto dsp_check_fail; |
| 728 |
} |
| 729 |
} else { |
| 730 |
dev_dbg(aw_dev->dev, "start pa with dsp bypass"); |
| 731 |
} |
| 732 |
|
| 733 |
/* enable tx feedback */ |
| 734 |
aw_dev_i2s_tx_enable(aw_dev, true); |
| 735 |
|
| 736 |
if (aw88399->dither_st == AW88399_DITHER_EN_ENABLE_VALUE) |
| 737 |
aw88399_dev_set_dither(aw88399, true); |
| 738 |
|
| 739 |
/* close mute */ |
| 740 |
aw88399_dev_mute(aw_dev, false); |
| 741 |
/* clear inturrupt */ |
| 742 |
aw_dev_clear_int_status(aw_dev); |
| 743 |
aw_dev->status = AW88399_DEV_PW_ON; |
| 744 |
|
| 745 |
return 0; |
| 746 |
|
| 747 |
dsp_check_fail: |
| 748 |
crc_check_fail: |
| 749 |
aw_dev_dsp_enable(aw_dev, false); |
| 750 |
sysst_check_fail: |
| 751 |
aw_dev_clear_int_status(aw_dev); |
| 752 |
aw_dev_amppd(aw_dev, true); |
| 753 |
pll_check_fail: |
| 754 |
aw_dev_pwd(aw_dev, true); |
| 755 |
aw_dev->status = AW88399_DEV_PW_OFF; |
| 756 |
|
| 757 |
return ret; |
| 758 |
} |
| 759 |
|
| 760 |
static int aw_dev_dsp_update_container(struct aw_device *aw_dev, |
| 761 |
unsigned char *data, unsigned int len, unsigned short base) |
| 762 |
{ |
| 763 |
u32 tmp_len; |
| 764 |
int i, ret; |
| 765 |
|
| 766 |
ret = regmap_write(aw_dev->regmap, AW88399_DSPMADD_REG, base); |
| 767 |
if (ret) |
| 768 |
return ret; |
| 769 |
|
| 770 |
for (i = 0; i < len; i += AW88399_MAX_RAM_WRITE_BYTE_SIZE) { |
| 771 |
tmp_len = min(len - i, AW88399_MAX_RAM_WRITE_BYTE_SIZE); |
| 772 |
ret = regmap_raw_write(aw_dev->regmap, AW88399_DSPMDAT_REG, |
| 773 |
&data[i], tmp_len); |
| 774 |
if (ret) |
| 775 |
return ret; |
| 776 |
} |
| 777 |
|
| 778 |
return 0; |
| 779 |
} |
| 780 |
|
| 781 |
static int aw_dev_get_ra(struct aw_cali_desc *cali_desc) |
| 782 |
{ |
| 783 |
struct aw_device *aw_dev = |
| 784 |
container_of(cali_desc, struct aw_device, cali_desc); |
| 785 |
u32 dsp_ra; |
| 786 |
int ret; |
| 787 |
|
| 788 |
ret = aw_dev_dsp_read(aw_dev, AW88399_DSP_REG_CFG_ADPZ_RA, |
| 789 |
&dsp_ra, AW_DSP_32_DATA); |
| 790 |
if (ret) { |
| 791 |
dev_err(aw_dev->dev, "read ra error"); |
| 792 |
return ret; |
| 793 |
} |
| 794 |
|
| 795 |
cali_desc->ra = AW88399_DSP_RE_TO_SHOW_RE(dsp_ra, |
| 796 |
AW88399_DSP_RE_SHIFT); |
| 797 |
|
| 798 |
return 0; |
| 799 |
} |
| 800 |
|
| 801 |
static int aw_dev_dsp_update_cfg(struct aw_device *aw_dev, |
| 802 |
unsigned char *data, unsigned int len) |
| 803 |
{ |
| 804 |
int ret; |
| 805 |
|
| 806 |
dev_dbg(aw_dev->dev, "dsp config len:%d", len); |
| 807 |
|
| 808 |
if (!len || !data) { |
| 809 |
dev_err(aw_dev->dev, "dsp config data is null or len is 0"); |
| 810 |
return -EINVAL; |
| 811 |
} |
| 812 |
|
| 813 |
ret = aw_dev_dsp_update_container(aw_dev, data, len, AW88399_DSP_CFG_ADDR); |
| 814 |
if (ret) |
| 815 |
return ret; |
| 816 |
|
| 817 |
aw_dev->dsp_cfg_len = len; |
| 818 |
|
| 819 |
ret = aw_dev_get_ra(&aw_dev->cali_desc); |
| 820 |
|
| 821 |
return ret; |
| 822 |
} |
| 823 |
|
| 824 |
static int aw_dev_dsp_update_fw(struct aw_device *aw_dev, |
| 825 |
unsigned char *data, unsigned int len) |
| 826 |
{ |
| 827 |
int ret; |
| 828 |
|
| 829 |
dev_dbg(aw_dev->dev, "dsp firmware len:%d", len); |
| 830 |
|
| 831 |
if (!len || !data) { |
| 832 |
dev_err(aw_dev->dev, "dsp firmware data is null or len is 0"); |
| 833 |
return -EINVAL; |
| 834 |
} |
| 835 |
|
| 836 |
aw_dev->dsp_fw_len = len; |
| 837 |
ret = aw_dev_dsp_update_container(aw_dev, data, len, AW88399_DSP_FW_ADDR); |
| 838 |
|
| 839 |
return ret; |
| 840 |
} |
| 841 |
|
| 842 |
static int aw_dev_check_sram(struct aw_device *aw_dev) |
| 843 |
{ |
| 844 |
unsigned int reg_val; |
| 845 |
|
| 846 |
/* read dsp_rom_check_reg */ |
| 847 |
aw_dev_dsp_read(aw_dev, AW88399_DSP_ROM_CHECK_ADDR, ®_val, AW_DSP_16_DATA); |
| 848 |
if (reg_val != AW88399_DSP_ROM_CHECK_DATA) { |
| 849 |
dev_err(aw_dev->dev, "check dsp rom failed, read[0x%x] != check[0x%x]", |
| 850 |
reg_val, AW88399_DSP_ROM_CHECK_DATA); |
| 851 |
return -EPERM; |
| 852 |
} |
| 853 |
|
| 854 |
/* check dsp_cfg_base_addr */ |
| 855 |
aw_dev_dsp_write(aw_dev, AW88399_DSP_CFG_ADDR, |
| 856 |
AW88399_DSP_ODD_NUM_BIT_TEST, AW_DSP_16_DATA); |
| 857 |
aw_dev_dsp_read(aw_dev, AW88399_DSP_CFG_ADDR, ®_val, AW_DSP_16_DATA); |
| 858 |
if (reg_val != AW88399_DSP_ODD_NUM_BIT_TEST) { |
| 859 |
dev_err(aw_dev->dev, "check dsp cfg failed, read[0x%x] != write[0x%x]", |
| 860 |
reg_val, AW88399_DSP_ODD_NUM_BIT_TEST); |
| 861 |
return -EPERM; |
| 862 |
} |
| 863 |
|
| 864 |
return 0; |
| 865 |
} |
| 866 |
|
| 867 |
static void aw_dev_select_memclk(struct aw_device *aw_dev, unsigned char flag) |
| 868 |
{ |
| 869 |
int ret; |
| 870 |
|
| 871 |
switch (flag) { |
| 872 |
case AW88399_DEV_MEMCLK_PLL: |
| 873 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG, |
| 874 |
~AW88399_MEM_CLKSEL_MASK, |
| 875 |
AW88399_MEM_CLKSEL_DAPHCLK_VALUE); |
| 876 |
if (ret) |
| 877 |
dev_err(aw_dev->dev, "memclk select pll failed"); |
| 878 |
break; |
| 879 |
case AW88399_DEV_MEMCLK_OSC: |
| 880 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG, |
| 881 |
~AW88399_MEM_CLKSEL_MASK, |
| 882 |
AW88399_MEM_CLKSEL_OSCCLK_VALUE); |
| 883 |
if (ret) |
| 884 |
dev_err(aw_dev->dev, "memclk select OSC failed"); |
| 885 |
break; |
| 886 |
default: |
| 887 |
dev_err(aw_dev->dev, "unknown memclk config, flag=0x%x", flag); |
| 888 |
break; |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
static void aw_dev_get_cur_mode_st(struct aw_device *aw_dev) |
| 893 |
{ |
| 894 |
struct aw_profctrl_desc *profctrl_desc = &aw_dev->profctrl_desc; |
| 895 |
unsigned int reg_val; |
| 896 |
int ret; |
| 897 |
|
| 898 |
ret = regmap_read(aw_dev->regmap, AW88399_SYSCTRL_REG, ®_val); |
| 899 |
if (ret) { |
| 900 |
dev_dbg(aw_dev->dev, "%s failed", __func__); |
| 901 |
return; |
| 902 |
} |
| 903 |
if ((reg_val & (~AW88399_RCV_MODE_MASK)) == AW88399_RCV_MODE_RECEIVER_VALUE) |
| 904 |
profctrl_desc->cur_mode = AW88399_RCV_MODE; |
| 905 |
else |
| 906 |
profctrl_desc->cur_mode = AW88399_NOT_RCV_MODE; |
| 907 |
} |
| 908 |
|
| 909 |
static int aw_dev_update_reg_container(struct aw88399 *aw88399, |
| 910 |
unsigned char *data, unsigned int len) |
| 911 |
{ |
| 912 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 913 |
struct aw_volume_desc *vol_desc = &aw_dev->volume_desc; |
| 914 |
u16 read_vol, reg_val; |
| 915 |
int data_len, i, ret; |
| 916 |
int16_t *reg_data; |
| 917 |
u8 reg_addr; |
| 918 |
|
| 919 |
reg_data = (int16_t *)data; |
| 920 |
data_len = len >> 1; |
| 921 |
|
| 922 |
if (data_len & 0x1) { |
| 923 |
dev_err(aw_dev->dev, "data len:%d unsupported", data_len); |
| 924 |
return -EINVAL; |
| 925 |
} |
| 926 |
|
| 927 |
for (i = 0; i < data_len; i += 2) { |
| 928 |
reg_addr = reg_data[i]; |
| 929 |
reg_val = reg_data[i + 1]; |
| 930 |
|
| 931 |
if (reg_addr == AW88399_DSPVCALB_REG) { |
| 932 |
aw88399->vcalb_init_val = reg_val; |
| 933 |
continue; |
| 934 |
} |
| 935 |
|
| 936 |
if (reg_addr == AW88399_SYSCTRL_REG) { |
| 937 |
if (reg_val & (~AW88399_DSPBY_MASK)) |
| 938 |
aw_dev->dsp_cfg = AW88399_DEV_DSP_BYPASS; |
| 939 |
else |
| 940 |
aw_dev->dsp_cfg = AW88399_DEV_DSP_WORK; |
| 941 |
|
| 942 |
reg_val &= (AW88399_HMUTE_MASK | AW88399_PWDN_MASK | |
| 943 |
AW88399_DSPBY_MASK); |
| 944 |
reg_val |= (AW88399_HMUTE_ENABLE_VALUE | AW88399_PWDN_POWER_DOWN_VALUE | |
| 945 |
AW88399_DSPBY_BYPASS_VALUE); |
| 946 |
} |
| 947 |
|
| 948 |
if (reg_addr == AW88399_I2SCTRL3_REG) { |
| 949 |
reg_val &= AW88399_I2STXEN_MASK; |
| 950 |
reg_val |= AW88399_I2STXEN_DISABLE_VALUE; |
| 951 |
} |
| 952 |
|
| 953 |
if (reg_addr == AW88399_SYSCTRL2_REG) { |
| 954 |
read_vol = (reg_val & (~AW88399_VOL_MASK)) >> |
| 955 |
AW88399_VOL_START_BIT; |
| 956 |
aw_dev->volume_desc.init_volume = read_vol; |
| 957 |
} |
| 958 |
|
| 959 |
if (reg_addr == AW88399_DBGCTRL_REG) { |
| 960 |
if ((reg_val & (~AW88399_EF_DBMD_MASK)) == AW88399_EF_DBMD_OR_VALUE) |
| 961 |
aw88399->check_val = AW_EF_OR_CHECK; |
| 962 |
else |
| 963 |
aw88399->check_val = AW_EF_AND_CHECK; |
| 964 |
|
| 965 |
aw88399->dither_st = reg_val & (~AW88399_DITHER_EN_MASK); |
| 966 |
} |
| 967 |
|
| 968 |
if (reg_addr == AW88399_CRCCTRL_REG) |
| 969 |
aw88399->crc_init_val = reg_val; |
| 970 |
|
| 971 |
ret = regmap_write(aw_dev->regmap, reg_addr, reg_val); |
| 972 |
if (ret) |
| 973 |
return ret; |
| 974 |
} |
| 975 |
|
| 976 |
aw_dev_pwd(aw_dev, false); |
| 977 |
usleep_range(AW88399_1000_US, AW88399_1000_US + 10); |
| 978 |
|
| 979 |
aw_dev_get_cur_mode_st(aw_dev); |
| 980 |
|
| 981 |
if (aw_dev->prof_cur != aw_dev->prof_index) |
| 982 |
vol_desc->ctl_volume = 0; |
| 983 |
else |
| 984 |
aw_dev_set_volume(aw_dev, vol_desc->ctl_volume); |
| 985 |
|
| 986 |
return 0; |
| 987 |
} |
| 988 |
|
| 989 |
static int aw_dev_reg_update(struct aw88399 *aw88399, |
| 990 |
unsigned char *data, unsigned int len) |
| 991 |
{ |
| 992 |
int ret; |
| 993 |
|
| 994 |
if (!len || !data) { |
| 995 |
dev_err(aw88399->aw_pa->dev, "reg data is null or len is 0"); |
| 996 |
return -EINVAL; |
| 997 |
} |
| 998 |
|
| 999 |
ret = aw_dev_update_reg_container(aw88399, data, len); |
| 1000 |
if (ret) |
| 1001 |
dev_err(aw88399->aw_pa->dev, "reg update failed"); |
| 1002 |
|
| 1003 |
return ret; |
| 1004 |
} |
| 1005 |
|
| 1006 |
static int aw88399_dev_get_prof_name(struct aw_device *aw_dev, int index, char **prof_name) |
| 1007 |
{ |
| 1008 |
struct aw_prof_info *prof_info = &aw_dev->prof_info; |
| 1009 |
struct aw_prof_desc *prof_desc; |
| 1010 |
|
| 1011 |
if ((index >= aw_dev->prof_info.count) || (index < 0)) { |
| 1012 |
dev_err(aw_dev->dev, "index[%d] overflow count[%d]", |
| 1013 |
index, aw_dev->prof_info.count); |
| 1014 |
return -EINVAL; |
| 1015 |
} |
| 1016 |
|
| 1017 |
prof_desc = &aw_dev->prof_info.prof_desc[index]; |
| 1018 |
|
| 1019 |
*prof_name = prof_info->prof_name_list[prof_desc->id]; |
| 1020 |
|
| 1021 |
return 0; |
| 1022 |
} |
| 1023 |
|
| 1024 |
static int aw88399_dev_get_prof_data(struct aw_device *aw_dev, int index, |
| 1025 |
struct aw_prof_desc **prof_desc) |
| 1026 |
{ |
| 1027 |
if ((index >= aw_dev->prof_info.count) || (index < 0)) { |
| 1028 |
dev_err(aw_dev->dev, "%s: index[%d] overflow count[%d]\n", |
| 1029 |
__func__, index, aw_dev->prof_info.count); |
| 1030 |
return -EINVAL; |
| 1031 |
} |
| 1032 |
|
| 1033 |
*prof_desc = &aw_dev->prof_info.prof_desc[index]; |
| 1034 |
|
| 1035 |
return 0; |
| 1036 |
} |
| 1037 |
|
| 1038 |
static int aw88399_dev_fw_update(struct aw88399 *aw88399, bool up_dsp_fw_en, bool force_up_en) |
| 1039 |
{ |
| 1040 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 1041 |
struct aw_prof_desc *prof_index_desc; |
| 1042 |
struct aw_sec_data_desc *sec_desc; |
| 1043 |
char *prof_name; |
| 1044 |
int ret; |
| 1045 |
|
| 1046 |
if ((aw_dev->prof_cur == aw_dev->prof_index) && |
| 1047 |
(force_up_en == AW88399_FORCE_UPDATE_OFF)) { |
| 1048 |
dev_dbg(aw_dev->dev, "scene no change, not update"); |
| 1049 |
return 0; |
| 1050 |
} |
| 1051 |
|
| 1052 |
if (aw_dev->fw_status == AW88399_DEV_FW_FAILED) { |
| 1053 |
dev_err(aw_dev->dev, "fw status[%d] error", aw_dev->fw_status); |
| 1054 |
return -EPERM; |
| 1055 |
} |
| 1056 |
|
| 1057 |
ret = aw88399_dev_get_prof_name(aw_dev, aw_dev->prof_index, &prof_name); |
| 1058 |
if (ret) |
| 1059 |
return ret; |
| 1060 |
|
| 1061 |
dev_dbg(aw_dev->dev, "start update %s", prof_name); |
| 1062 |
|
| 1063 |
ret = aw88399_dev_get_prof_data(aw_dev, aw_dev->prof_index, &prof_index_desc); |
| 1064 |
if (ret) |
| 1065 |
return ret; |
| 1066 |
|
| 1067 |
/* update reg */ |
| 1068 |
sec_desc = prof_index_desc->sec_desc; |
| 1069 |
ret = aw_dev_reg_update(aw88399, sec_desc[AW88395_DATA_TYPE_REG].data, |
| 1070 |
sec_desc[AW88395_DATA_TYPE_REG].len); |
| 1071 |
if (ret) { |
| 1072 |
dev_err(aw_dev->dev, "update reg failed"); |
| 1073 |
return ret; |
| 1074 |
} |
| 1075 |
|
| 1076 |
aw88399_dev_mute(aw_dev, true); |
| 1077 |
|
| 1078 |
if (aw_dev->dsp_cfg == AW88399_DEV_DSP_WORK) |
| 1079 |
aw_dev_dsp_enable(aw_dev, false); |
| 1080 |
|
| 1081 |
aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_OSC); |
| 1082 |
|
| 1083 |
ret = aw_dev_check_sram(aw_dev); |
| 1084 |
if (ret) { |
| 1085 |
dev_err(aw_dev->dev, "check sram failed"); |
| 1086 |
goto error; |
| 1087 |
} |
| 1088 |
|
| 1089 |
if (up_dsp_fw_en) { |
| 1090 |
dev_dbg(aw_dev->dev, "fw_ver: [%x]", prof_index_desc->fw_ver); |
| 1091 |
ret = aw_dev_dsp_update_fw(aw_dev, sec_desc[AW88395_DATA_TYPE_DSP_FW].data, |
| 1092 |
sec_desc[AW88395_DATA_TYPE_DSP_FW].len); |
| 1093 |
if (ret) { |
| 1094 |
dev_err(aw_dev->dev, "update dsp fw failed"); |
| 1095 |
goto error; |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
/* update dsp config */ |
| 1100 |
ret = aw_dev_dsp_update_cfg(aw_dev, sec_desc[AW88395_DATA_TYPE_DSP_CFG].data, |
| 1101 |
sec_desc[AW88395_DATA_TYPE_DSP_CFG].len); |
| 1102 |
if (ret) { |
| 1103 |
dev_err(aw_dev->dev, "update dsp cfg failed"); |
| 1104 |
goto error; |
| 1105 |
} |
| 1106 |
|
| 1107 |
aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_PLL); |
| 1108 |
|
| 1109 |
aw_dev->prof_cur = aw_dev->prof_index; |
| 1110 |
|
| 1111 |
return 0; |
| 1112 |
|
| 1113 |
error: |
| 1114 |
aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_PLL); |
| 1115 |
return ret; |
| 1116 |
} |
| 1117 |
|
| 1118 |
static void aw88399_start_pa(struct aw88399 *aw88399) |
| 1119 |
{ |
| 1120 |
int ret, i; |
| 1121 |
|
| 1122 |
for (i = 0; i < AW88399_START_RETRIES; i++) { |
| 1123 |
ret = aw88399_dev_start(aw88399); |
| 1124 |
if (ret) { |
| 1125 |
dev_err(aw88399->aw_pa->dev, "aw88399 device start failed. retry = %d", i); |
| 1126 |
ret = aw88399_dev_fw_update(aw88399, AW88399_DSP_FW_UPDATE_ON, true); |
| 1127 |
if (ret) { |
| 1128 |
dev_err(aw88399->aw_pa->dev, "fw update failed"); |
| 1129 |
continue; |
| 1130 |
} |
| 1131 |
} else { |
| 1132 |
dev_dbg(aw88399->aw_pa->dev, "start success\n"); |
| 1133 |
break; |
| 1134 |
} |
| 1135 |
} |
| 1136 |
} |
| 1137 |
|
| 1138 |
static void aw88399_startup_work(struct work_struct *work) |
| 1139 |
{ |
| 1140 |
struct aw88399 *aw88399 = |
| 1141 |
container_of(work, struct aw88399, start_work.work); |
| 1142 |
|
| 1143 |
mutex_lock(&aw88399->lock); |
| 1144 |
aw88399_start_pa(aw88399); |
| 1145 |
mutex_unlock(&aw88399->lock); |
| 1146 |
} |
| 1147 |
|
| 1148 |
static void aw88399_start(struct aw88399 *aw88399, bool sync_start) |
| 1149 |
{ |
| 1150 |
int ret; |
| 1151 |
|
| 1152 |
if (aw88399->aw_pa->fw_status != AW88399_DEV_FW_OK) |
| 1153 |
return; |
| 1154 |
|
| 1155 |
if (aw88399->aw_pa->status == AW88399_DEV_PW_ON) |
| 1156 |
return; |
| 1157 |
|
| 1158 |
ret = aw88399_dev_fw_update(aw88399, AW88399_DSP_FW_UPDATE_OFF, true); |
| 1159 |
if (ret) { |
| 1160 |
dev_err(aw88399->aw_pa->dev, "fw update failed."); |
| 1161 |
return; |
| 1162 |
} |
| 1163 |
|
| 1164 |
if (sync_start == AW88399_SYNC_START) |
| 1165 |
aw88399_start_pa(aw88399); |
| 1166 |
else |
| 1167 |
queue_delayed_work(system_dfl_wq, |
| 1168 |
&aw88399->start_work, |
| 1169 |
AW88399_START_WORK_DELAY_MS); |
| 1170 |
} |
| 1171 |
|
| 1172 |
static int aw_dev_check_sysint(struct aw_device *aw_dev) |
| 1173 |
{ |
| 1174 |
u16 reg_val; |
| 1175 |
|
| 1176 |
aw_dev_get_int_status(aw_dev, ®_val); |
| 1177 |
if (reg_val & AW88399_BIT_SYSINT_CHECK) { |
| 1178 |
dev_err(aw_dev->dev, "pa stop check fail:0x%04x", reg_val); |
| 1179 |
return -EINVAL; |
| 1180 |
} |
| 1181 |
|
| 1182 |
return 0; |
| 1183 |
} |
| 1184 |
|
| 1185 |
static int aw88399_stop(struct aw_device *aw_dev) |
| 1186 |
{ |
| 1187 |
struct aw_sec_data_desc *dsp_cfg = |
| 1188 |
&aw_dev->prof_info.prof_desc[aw_dev->prof_cur].sec_desc[AW88395_DATA_TYPE_DSP_CFG]; |
| 1189 |
struct aw_sec_data_desc *dsp_fw = |
| 1190 |
&aw_dev->prof_info.prof_desc[aw_dev->prof_cur].sec_desc[AW88395_DATA_TYPE_DSP_FW]; |
| 1191 |
int int_st; |
| 1192 |
|
| 1193 |
if (aw_dev->status == AW88399_DEV_PW_OFF) { |
| 1194 |
dev_dbg(aw_dev->dev, "already power off"); |
| 1195 |
return 0; |
| 1196 |
} |
| 1197 |
|
| 1198 |
aw_dev->status = AW88399_DEV_PW_OFF; |
| 1199 |
|
| 1200 |
aw88399_dev_mute(aw_dev, true); |
| 1201 |
usleep_range(AW88399_4000_US, AW88399_4000_US + 100); |
| 1202 |
|
| 1203 |
aw_dev_i2s_tx_enable(aw_dev, false); |
| 1204 |
usleep_range(AW88399_1000_US, AW88399_1000_US + 100); |
| 1205 |
|
| 1206 |
int_st = aw_dev_check_sysint(aw_dev); |
| 1207 |
|
| 1208 |
aw_dev_dsp_enable(aw_dev, false); |
| 1209 |
|
| 1210 |
aw_dev_amppd(aw_dev, true); |
| 1211 |
|
| 1212 |
if (int_st) { |
| 1213 |
aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_OSC); |
| 1214 |
aw_dev_dsp_update_fw(aw_dev, dsp_fw->data, dsp_fw->len); |
| 1215 |
aw_dev_dsp_update_cfg(aw_dev, dsp_cfg->data, dsp_cfg->len); |
| 1216 |
aw_dev_select_memclk(aw_dev, AW88399_DEV_MEMCLK_PLL); |
| 1217 |
} |
| 1218 |
|
| 1219 |
aw_dev_pwd(aw_dev, true); |
| 1220 |
|
| 1221 |
return 0; |
| 1222 |
} |
| 1223 |
|
| 1224 |
static struct snd_soc_dai_driver aw88399_dai[] = { |
| 1225 |
{ |
| 1226 |
.name = "aw88399-aif", |
| 1227 |
.id = 1, |
| 1228 |
.playback = { |
| 1229 |
.stream_name = "Speaker_Playback", |
| 1230 |
.channels_min = 1, |
| 1231 |
.channels_max = 2, |
| 1232 |
.rates = AW88399_RATES, |
| 1233 |
.formats = AW88399_FORMATS, |
| 1234 |
}, |
| 1235 |
.capture = { |
| 1236 |
.stream_name = "Speaker_Capture", |
| 1237 |
.channels_min = 1, |
| 1238 |
.channels_max = 2, |
| 1239 |
.rates = AW88399_RATES, |
| 1240 |
.formats = AW88399_FORMATS, |
| 1241 |
}, |
| 1242 |
}, |
| 1243 |
}; |
| 1244 |
|
| 1245 |
static void aw_cali_svc_run_mute(struct aw_device *aw_dev, uint16_t cali_result) |
| 1246 |
{ |
| 1247 |
if (cali_result == CALI_RESULT_ERROR) |
| 1248 |
aw88399_dev_mute(aw_dev, true); |
| 1249 |
else if (cali_result == CALI_RESULT_NORMAL) |
| 1250 |
aw88399_dev_mute(aw_dev, false); |
| 1251 |
} |
| 1252 |
|
| 1253 |
static int aw_cali_svc_get_cali_cfg(struct aw_device *aw_dev) |
| 1254 |
{ |
| 1255 |
struct cali_cfg *cali_cfg = &aw_dev->cali_desc.cali_cfg; |
| 1256 |
int ret; |
| 1257 |
|
| 1258 |
ret = aw_dev_dsp_read(aw_dev, AW88399_DSP_REG_CFG_MBMEC_ACTAMPTH, |
| 1259 |
&cali_cfg->data[0], AW_DSP_32_DATA); |
| 1260 |
if (ret) |
| 1261 |
return ret; |
| 1262 |
|
| 1263 |
ret = aw_dev_dsp_read(aw_dev, AW88399_DSP_REG_CFG_MBMEC_NOISEAMPTH, |
| 1264 |
&cali_cfg->data[1], AW_DSP_32_DATA); |
| 1265 |
if (ret) |
| 1266 |
return ret; |
| 1267 |
|
| 1268 |
ret = aw_dev_dsp_read(aw_dev, AW88399_DSP_REG_CFG_ADPZ_USTEPN, |
| 1269 |
&cali_cfg->data[2], AW_DSP_16_DATA); |
| 1270 |
if (ret) |
| 1271 |
return ret; |
| 1272 |
|
| 1273 |
ret = aw_dev_dsp_read(aw_dev, AW88399_DSP_REG_CFG_RE_ALPHA, |
| 1274 |
&cali_cfg->data[3], AW_DSP_16_DATA); |
| 1275 |
|
| 1276 |
return ret; |
| 1277 |
} |
| 1278 |
|
| 1279 |
static int aw_cali_svc_set_cali_cfg(struct aw_device *aw_dev, |
| 1280 |
struct cali_cfg cali_cfg) |
| 1281 |
{ |
| 1282 |
int ret; |
| 1283 |
|
| 1284 |
ret = aw_dev_dsp_write(aw_dev, AW88399_DSP_REG_CFG_MBMEC_ACTAMPTH, |
| 1285 |
cali_cfg.data[0], AW_DSP_32_DATA); |
| 1286 |
if (ret) |
| 1287 |
return ret; |
| 1288 |
|
| 1289 |
ret = aw_dev_dsp_write(aw_dev, AW88399_DSP_REG_CFG_MBMEC_NOISEAMPTH, |
| 1290 |
cali_cfg.data[1], AW_DSP_32_DATA); |
| 1291 |
if (ret) |
| 1292 |
return ret; |
| 1293 |
|
| 1294 |
ret = aw_dev_dsp_write(aw_dev, AW88399_DSP_REG_CFG_ADPZ_USTEPN, |
| 1295 |
cali_cfg.data[2], AW_DSP_16_DATA); |
| 1296 |
if (ret) |
| 1297 |
return ret; |
| 1298 |
|
| 1299 |
ret = aw_dev_dsp_write(aw_dev, AW88399_DSP_REG_CFG_RE_ALPHA, |
| 1300 |
cali_cfg.data[3], AW_DSP_16_DATA); |
| 1301 |
|
| 1302 |
return ret; |
| 1303 |
} |
| 1304 |
|
| 1305 |
static int aw_cali_svc_cali_en(struct aw_device *aw_dev, bool cali_en) |
| 1306 |
{ |
| 1307 |
struct cali_cfg set_cfg; |
| 1308 |
int ret; |
| 1309 |
|
| 1310 |
aw_dev_dsp_enable(aw_dev, false); |
| 1311 |
if (cali_en) { |
| 1312 |
regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG, |
| 1313 |
~AW883XX_DSP_NG_EN_MASK, AW883XX_DSP_NG_EN_DISABLE_VALUE); |
| 1314 |
aw_dev_dsp_write(aw_dev, AW88399_DSP_LOW_POWER_SWITCH_CFG_ADDR, |
| 1315 |
AW88399_DSP_LOW_POWER_SWITCH_DISABLE, AW_DSP_16_DATA); |
| 1316 |
|
| 1317 |
ret = aw_cali_svc_get_cali_cfg(aw_dev); |
| 1318 |
if (ret) { |
| 1319 |
dev_err(aw_dev->dev, "get cali cfg failed\n"); |
| 1320 |
aw_dev_dsp_enable(aw_dev, true); |
| 1321 |
return ret; |
| 1322 |
} |
| 1323 |
set_cfg.data[0] = 0; |
| 1324 |
set_cfg.data[1] = 0; |
| 1325 |
set_cfg.data[2] = -1; |
| 1326 |
set_cfg.data[3] = 1; |
| 1327 |
|
| 1328 |
ret = aw_cali_svc_set_cali_cfg(aw_dev, set_cfg); |
| 1329 |
if (ret) { |
| 1330 |
dev_err(aw_dev->dev, "set cali cfg failed\n"); |
| 1331 |
aw_cali_svc_set_cali_cfg(aw_dev, aw_dev->cali_desc.cali_cfg); |
| 1332 |
aw_dev_dsp_enable(aw_dev, true); |
| 1333 |
return ret; |
| 1334 |
} |
| 1335 |
} else { |
| 1336 |
aw_cali_svc_set_cali_cfg(aw_dev, aw_dev->cali_desc.cali_cfg); |
| 1337 |
} |
| 1338 |
|
| 1339 |
aw_dev_dsp_enable(aw_dev, true); |
| 1340 |
|
| 1341 |
return 0; |
| 1342 |
} |
| 1343 |
|
| 1344 |
static int aw_cali_svc_cali_run_dsp_vol(struct aw_device *aw_dev, bool enable) |
| 1345 |
{ |
| 1346 |
unsigned int reg_val; |
| 1347 |
int ret; |
| 1348 |
|
| 1349 |
if (enable) { |
| 1350 |
ret = regmap_read(aw_dev->regmap, AW88399_DSPCFG_REG, ®_val); |
| 1351 |
if (ret) { |
| 1352 |
dev_err(aw_dev->dev, "read reg 0x%x failed\n", AW88399_DSPCFG_REG); |
| 1353 |
return ret; |
| 1354 |
} |
| 1355 |
|
| 1356 |
aw_dev->cali_desc.store_vol = reg_val & (~AW88399_DSP_VOL_MASK); |
| 1357 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_DSPCFG_REG, |
| 1358 |
~AW88399_DSP_VOL_MASK, AW88399_DSP_VOL_MUTE); |
| 1359 |
} else { |
| 1360 |
ret = regmap_update_bits(aw_dev->regmap, AW88399_DSPCFG_REG, |
| 1361 |
~AW88399_DSP_VOL_MASK, aw_dev->cali_desc.store_vol); |
| 1362 |
} |
| 1363 |
|
| 1364 |
return ret; |
| 1365 |
} |
| 1366 |
|
| 1367 |
static void aw_cali_svc_backup_info(struct aw_device *aw_dev) |
| 1368 |
{ |
| 1369 |
struct aw_cali_backup_desc *backup_desc = &aw_dev->cali_desc.backup_info; |
| 1370 |
unsigned int reg_val, dsp_val; |
| 1371 |
|
| 1372 |
regmap_read(aw_dev->regmap, AW88399_DBGCTRL_REG, ®_val); |
| 1373 |
backup_desc->dsp_ng_cfg = reg_val & (~AW883XX_DSP_NG_EN_MASK); |
| 1374 |
|
| 1375 |
aw_dev_dsp_read(aw_dev, AW88399_DSP_LOW_POWER_SWITCH_CFG_ADDR, &dsp_val, AW_DSP_16_DATA); |
| 1376 |
|
| 1377 |
backup_desc->dsp_lp_cfg = dsp_val; |
| 1378 |
} |
| 1379 |
|
| 1380 |
static void aw_cali_svc_recover_info(struct aw_device *aw_dev) |
| 1381 |
{ |
| 1382 |
struct aw_cali_backup_desc *backup_desc = &aw_dev->cali_desc.backup_info; |
| 1383 |
|
| 1384 |
regmap_update_bits(aw_dev->regmap, AW88399_DBGCTRL_REG, |
| 1385 |
~AW883XX_DSP_NG_EN_MASK, backup_desc->dsp_ng_cfg); |
| 1386 |
|
| 1387 |
aw_dev_dsp_write(aw_dev, AW88399_DSP_LOW_POWER_SWITCH_CFG_ADDR, |
| 1388 |
backup_desc->dsp_lp_cfg, AW_DSP_16_DATA); |
| 1389 |
} |
| 1390 |
|
| 1391 |
static int aw_cali_svc_cali_re_mode_enable(struct aw_device *aw_dev, bool is_enable) |
| 1392 |
{ |
| 1393 |
int ret; |
| 1394 |
|
| 1395 |
if (is_enable) { |
| 1396 |
ret = aw_dev_check_syspll(aw_dev); |
| 1397 |
if (ret) { |
| 1398 |
dev_err(aw_dev->dev, "pll check failed cannot start\n"); |
| 1399 |
return ret; |
| 1400 |
} |
| 1401 |
|
| 1402 |
ret = aw_dev_get_dsp_status(aw_dev); |
| 1403 |
if (ret) { |
| 1404 |
dev_err(aw_dev->dev, "dsp status error\n"); |
| 1405 |
return ret; |
| 1406 |
} |
| 1407 |
|
| 1408 |
aw_cali_svc_backup_info(aw_dev); |
| 1409 |
ret = aw_cali_svc_cali_en(aw_dev, true); |
| 1410 |
if (ret) { |
| 1411 |
dev_err(aw_dev->dev, "aw_cali_svc_cali_en failed\n"); |
| 1412 |
return ret; |
| 1413 |
} |
| 1414 |
|
| 1415 |
ret = aw_cali_svc_cali_run_dsp_vol(aw_dev, true); |
| 1416 |
if (ret) { |
| 1417 |
aw_cali_svc_cali_en(aw_dev, false); |
| 1418 |
return ret; |
| 1419 |
} |
| 1420 |
|
| 1421 |
} else { |
| 1422 |
aw_cali_svc_cali_run_dsp_vol(aw_dev, false); |
| 1423 |
aw_cali_svc_recover_info(aw_dev); |
| 1424 |
aw_cali_svc_cali_en(aw_dev, false); |
| 1425 |
} |
| 1426 |
|
| 1427 |
return 0; |
| 1428 |
} |
| 1429 |
|
| 1430 |
static int aw_cali_svc_get_dev_re(struct aw_device *aw_dev, uint32_t *re) |
| 1431 |
{ |
| 1432 |
uint32_t dsp_re, show_re; |
| 1433 |
int ret; |
| 1434 |
|
| 1435 |
ret = aw_dev_dsp_read(aw_dev, AW88399_DSP_REG_CALRE, &dsp_re, AW_DSP_16_DATA); |
| 1436 |
if (ret) |
| 1437 |
return ret; |
| 1438 |
|
| 1439 |
show_re = AW88399_DSP_RE_TO_SHOW_RE(dsp_re, AW88399_DSP_REG_CALRE_SHIFT); |
| 1440 |
|
| 1441 |
*re = (uint32_t)(show_re - aw_dev->cali_desc.ra); |
| 1442 |
|
| 1443 |
return 0; |
| 1444 |
} |
| 1445 |
|
| 1446 |
static void aw_cali_svc_del_max_min_ave_algo(uint32_t *data, int data_size, uint32_t *dsp_re) |
| 1447 |
{ |
| 1448 |
int sum = 0, i; |
| 1449 |
|
| 1450 |
for (i = 1; i < data_size - 1; i++) |
| 1451 |
sum += data[i]; |
| 1452 |
|
| 1453 |
*dsp_re = sum / (data_size - AW_CALI_DATA_SUM_RM); |
| 1454 |
} |
| 1455 |
|
| 1456 |
static int aw_cali_svc_get_iv_st(struct aw_device *aw_dev) |
| 1457 |
{ |
| 1458 |
unsigned int reg_data; |
| 1459 |
int ret, i; |
| 1460 |
|
| 1461 |
for (i = 0; i < AW_GET_IV_CNT_MAX; i++) { |
| 1462 |
ret = regmap_read(aw_dev->regmap, AW88399_ASR1_REG, ®_data); |
| 1463 |
if (ret) { |
| 1464 |
dev_err(aw_dev->dev, "read 0x%x failed\n", AW88399_ASR1_REG); |
| 1465 |
return ret; |
| 1466 |
} |
| 1467 |
|
| 1468 |
reg_data &= (~AW88399_REABS_MASK); |
| 1469 |
if (!reg_data) |
| 1470 |
return 0; |
| 1471 |
msleep(30); |
| 1472 |
} |
| 1473 |
|
| 1474 |
dev_err(aw_dev->dev, "IV data abnormal, please check\n"); |
| 1475 |
|
| 1476 |
return -EINVAL; |
| 1477 |
} |
| 1478 |
|
| 1479 |
static int compare_ints(const void *a, const void *b) |
| 1480 |
{ |
| 1481 |
return *(int *)a - *(int *)b; |
| 1482 |
} |
| 1483 |
|
| 1484 |
static int aw_cali_svc_get_smooth_cali_re(struct aw_device *aw_dev) |
| 1485 |
{ |
| 1486 |
uint32_t re_temp[AW_CALI_READ_CNT_MAX]; |
| 1487 |
uint32_t dsp_re; |
| 1488 |
int ret, i; |
| 1489 |
|
| 1490 |
for (i = 0; i < AW_CALI_READ_CNT_MAX; i++) { |
| 1491 |
ret = aw_cali_svc_get_dev_re(aw_dev, &re_temp[i]); |
| 1492 |
if (ret) |
| 1493 |
goto cali_re_fail; |
| 1494 |
msleep(30); |
| 1495 |
} |
| 1496 |
|
| 1497 |
sort(re_temp, AW_CALI_READ_CNT_MAX, sizeof(uint32_t), compare_ints, NULL); |
| 1498 |
|
| 1499 |
aw_cali_svc_del_max_min_ave_algo(re_temp, AW_CALI_READ_CNT_MAX, &dsp_re); |
| 1500 |
|
| 1501 |
ret = aw_cali_svc_get_iv_st(aw_dev); |
| 1502 |
if (ret) { |
| 1503 |
dev_err(aw_dev->dev, "get iv data failed"); |
| 1504 |
goto cali_re_fail; |
| 1505 |
} |
| 1506 |
|
| 1507 |
if (dsp_re < AW88399_CALI_RE_MIN || dsp_re > AW88399_CALI_RE_MAX) { |
| 1508 |
dev_err(aw_dev->dev, "out range re value: [%d]mohm\n", dsp_re); |
| 1509 |
aw_dev->cali_desc.cali_re = dsp_re; |
| 1510 |
aw_dev->cali_desc.cali_result = CALI_RESULT_ERROR; |
| 1511 |
aw_cali_svc_run_mute(aw_dev, aw_dev->cali_desc.cali_result); |
| 1512 |
|
| 1513 |
return 0; |
| 1514 |
} |
| 1515 |
|
| 1516 |
aw_dev->cali_desc.cali_result = CALI_RESULT_NORMAL; |
| 1517 |
|
| 1518 |
aw_dev->cali_desc.cali_re = dsp_re; |
| 1519 |
dev_dbg(aw_dev->dev, "re[%d]mohm\n", aw_dev->cali_desc.cali_re); |
| 1520 |
|
| 1521 |
aw_dev_dsp_enable(aw_dev, false); |
| 1522 |
aw_dev_update_cali_re(&aw_dev->cali_desc); |
| 1523 |
aw_dev_dsp_enable(aw_dev, true); |
| 1524 |
|
| 1525 |
return 0; |
| 1526 |
|
| 1527 |
cali_re_fail: |
| 1528 |
aw_dev->cali_desc.cali_result = CALI_RESULT_ERROR; |
| 1529 |
aw_cali_svc_run_mute(aw_dev, aw_dev->cali_desc.cali_result); |
| 1530 |
return -EINVAL; |
| 1531 |
} |
| 1532 |
|
| 1533 |
static int aw_cali_svc_dev_cali_re(struct aw88399 *aw88399) |
| 1534 |
{ |
| 1535 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 1536 |
struct aw_cali_desc *cali_desc = &aw_dev->cali_desc; |
| 1537 |
int ret; |
| 1538 |
|
| 1539 |
if (cali_desc->cali_running) { |
| 1540 |
dev_err(aw_dev->dev, "calibration in progress\n"); |
| 1541 |
return -EINVAL; |
| 1542 |
} |
| 1543 |
|
| 1544 |
cali_desc->cali_running = true; |
| 1545 |
aw_cali_svc_run_mute(aw_dev, CALI_RESULT_NORMAL); |
| 1546 |
|
| 1547 |
ret = aw_cali_svc_cali_re_mode_enable(aw_dev, true); |
| 1548 |
if (ret) { |
| 1549 |
dev_err(aw_dev->dev, "start cali re failed\n"); |
| 1550 |
goto re_mode_err; |
| 1551 |
} |
| 1552 |
|
| 1553 |
msleep(1000); |
| 1554 |
|
| 1555 |
ret = aw_cali_svc_get_smooth_cali_re(aw_dev); |
| 1556 |
if (ret) |
| 1557 |
dev_err(aw_dev->dev, "get cali re failed\n"); |
| 1558 |
|
| 1559 |
aw_cali_svc_cali_re_mode_enable(aw_dev, false); |
| 1560 |
|
| 1561 |
re_mode_err: |
| 1562 |
cali_desc->cali_running = false; |
| 1563 |
|
| 1564 |
return ret; |
| 1565 |
} |
| 1566 |
|
| 1567 |
static int aw88399_get_fade_in_time(struct snd_kcontrol *kcontrol, |
| 1568 |
struct snd_ctl_elem_value *ucontrol) |
| 1569 |
{ |
| 1570 |
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
| 1571 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component); |
| 1572 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 1573 |
|
| 1574 |
ucontrol->value.integer.value[0] = aw_dev->fade_in_time; |
| 1575 |
|
| 1576 |
return 0; |
| 1577 |
} |
| 1578 |
|
| 1579 |
static int aw88399_set_fade_in_time(struct snd_kcontrol *kcontrol, |
| 1580 |
struct snd_ctl_elem_value *ucontrol) |
| 1581 |
{ |
| 1582 |
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
| 1583 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component); |
| 1584 |
struct soc_mixer_control *mc = |
| 1585 |
(struct soc_mixer_control *)kcontrol->private_value; |
| 1586 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 1587 |
int time; |
| 1588 |
|
| 1589 |
time = ucontrol->value.integer.value[0]; |
| 1590 |
|
| 1591 |
if (time < mc->min || time > mc->max) |
| 1592 |
return -EINVAL; |
| 1593 |
|
| 1594 |
if (time != aw_dev->fade_in_time) { |
| 1595 |
aw_dev->fade_in_time = time; |
| 1596 |
return 1; |
| 1597 |
} |
| 1598 |
|
| 1599 |
return 0; |
| 1600 |
} |
| 1601 |
|
| 1602 |
static int aw88399_get_fade_out_time(struct snd_kcontrol *kcontrol, |
| 1603 |
struct snd_ctl_elem_value *ucontrol) |
| 1604 |
{ |
| 1605 |
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
| 1606 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component); |
| 1607 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 1608 |
|
| 1609 |
ucontrol->value.integer.value[0] = aw_dev->fade_out_time; |
| 1610 |
|
| 1611 |
return 0; |
| 1612 |
} |
| 1613 |
|
| 1614 |
static int aw88399_set_fade_out_time(struct snd_kcontrol *kcontrol, |
| 1615 |
struct snd_ctl_elem_value *ucontrol) |
| 1616 |
{ |
| 1617 |
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
| 1618 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component); |
| 1619 |
struct soc_mixer_control *mc = |
| 1620 |
(struct soc_mixer_control *)kcontrol->private_value; |
| 1621 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 1622 |
int time; |
| 1623 |
|
| 1624 |
time = ucontrol->value.integer.value[0]; |
| 1625 |
if (time < mc->min || time > mc->max) |
| 1626 |
return -EINVAL; |
| 1627 |
|
| 1628 |
if (time != aw_dev->fade_out_time) { |
| 1629 |
aw_dev->fade_out_time = time; |
| 1630 |
return 1; |
| 1631 |
} |
| 1632 |
|
| 1633 |
return 0; |
| 1634 |
} |
| 1635 |
|
| 1636 |
static int aw88399_dev_set_profile_index(struct aw_device *aw_dev, int index) |
| 1637 |
{ |
| 1638 |
/* check the index whether is valid */ |
| 1639 |
if ((index >= aw_dev->prof_info.count) || (index < 0)) |
| 1640 |
return -EINVAL; |
| 1641 |
/* check the index whether change */ |
| 1642 |
if (aw_dev->prof_index == index) |
| 1643 |
return -EINVAL; |
| 1644 |
|
| 1645 |
aw_dev->prof_index = index; |
| 1646 |
dev_dbg(aw_dev->dev, "set prof[%s]", |
| 1647 |
aw_dev->prof_info.prof_name_list[aw_dev->prof_info.prof_desc[index].id]); |
| 1648 |
|
| 1649 |
return 0; |
| 1650 |
} |
| 1651 |
|
| 1652 |
static int aw88399_profile_info(struct snd_kcontrol *kcontrol, |
| 1653 |
struct snd_ctl_elem_info *uinfo) |
| 1654 |
{ |
| 1655 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1656 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); |
| 1657 |
char *prof_name; |
| 1658 |
int count, ret; |
| 1659 |
|
| 1660 |
uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 1661 |
uinfo->count = 1; |
| 1662 |
|
| 1663 |
count = aw88399->aw_pa->prof_info.count; |
| 1664 |
if (count <= 0) { |
| 1665 |
uinfo->value.enumerated.items = 0; |
| 1666 |
return 0; |
| 1667 |
} |
| 1668 |
|
| 1669 |
uinfo->value.enumerated.items = count; |
| 1670 |
|
| 1671 |
if (uinfo->value.enumerated.item >= count) |
| 1672 |
uinfo->value.enumerated.item = count - 1; |
| 1673 |
|
| 1674 |
count = uinfo->value.enumerated.item; |
| 1675 |
|
| 1676 |
ret = aw88399_dev_get_prof_name(aw88399->aw_pa, count, &prof_name); |
| 1677 |
if (ret) { |
| 1678 |
strscpy(uinfo->value.enumerated.name, "null"); |
| 1679 |
return 0; |
| 1680 |
} |
| 1681 |
|
| 1682 |
strscpy(uinfo->value.enumerated.name, prof_name); |
| 1683 |
|
| 1684 |
return 0; |
| 1685 |
} |
| 1686 |
|
| 1687 |
static int aw88399_profile_get(struct snd_kcontrol *kcontrol, |
| 1688 |
struct snd_ctl_elem_value *ucontrol) |
| 1689 |
{ |
| 1690 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1691 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); |
| 1692 |
|
| 1693 |
ucontrol->value.integer.value[0] = aw88399->aw_pa->prof_index; |
| 1694 |
|
| 1695 |
return 0; |
| 1696 |
} |
| 1697 |
|
| 1698 |
static int aw88399_profile_set(struct snd_kcontrol *kcontrol, |
| 1699 |
struct snd_ctl_elem_value *ucontrol) |
| 1700 |
{ |
| 1701 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1702 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); |
| 1703 |
int ret; |
| 1704 |
|
| 1705 |
mutex_lock(&aw88399->lock); |
| 1706 |
ret = aw88399_dev_set_profile_index(aw88399->aw_pa, ucontrol->value.integer.value[0]); |
| 1707 |
if (ret) { |
| 1708 |
dev_dbg(codec->dev, "profile index does not change"); |
| 1709 |
mutex_unlock(&aw88399->lock); |
| 1710 |
return 0; |
| 1711 |
} |
| 1712 |
|
| 1713 |
if (aw88399->aw_pa->status) { |
| 1714 |
aw88399_stop(aw88399->aw_pa); |
| 1715 |
aw88399_start(aw88399, AW88399_SYNC_START); |
| 1716 |
} |
| 1717 |
|
| 1718 |
mutex_unlock(&aw88399->lock); |
| 1719 |
|
| 1720 |
return 1; |
| 1721 |
} |
| 1722 |
|
| 1723 |
static int aw88399_volume_get(struct snd_kcontrol *kcontrol, |
| 1724 |
struct snd_ctl_elem_value *ucontrol) |
| 1725 |
{ |
| 1726 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1727 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); |
| 1728 |
struct aw_volume_desc *vol_desc = &aw88399->aw_pa->volume_desc; |
| 1729 |
|
| 1730 |
ucontrol->value.integer.value[0] = vol_desc->ctl_volume; |
| 1731 |
|
| 1732 |
return 0; |
| 1733 |
} |
| 1734 |
|
| 1735 |
static int aw88399_volume_set(struct snd_kcontrol *kcontrol, |
| 1736 |
struct snd_ctl_elem_value *ucontrol) |
| 1737 |
{ |
| 1738 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1739 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); |
| 1740 |
struct aw_volume_desc *vol_desc = &aw88399->aw_pa->volume_desc; |
| 1741 |
struct soc_mixer_control *mc = |
| 1742 |
(struct soc_mixer_control *)kcontrol->private_value; |
| 1743 |
int value; |
| 1744 |
|
| 1745 |
value = ucontrol->value.integer.value[0]; |
| 1746 |
if (value < mc->min || value > mc->max) |
| 1747 |
return -EINVAL; |
| 1748 |
|
| 1749 |
if (vol_desc->ctl_volume != value) { |
| 1750 |
vol_desc->ctl_volume = value; |
| 1751 |
aw_dev_set_volume(aw88399->aw_pa, vol_desc->ctl_volume); |
| 1752 |
|
| 1753 |
return 1; |
| 1754 |
} |
| 1755 |
|
| 1756 |
return 0; |
| 1757 |
} |
| 1758 |
|
| 1759 |
static int aw88399_get_fade_step(struct snd_kcontrol *kcontrol, |
| 1760 |
struct snd_ctl_elem_value *ucontrol) |
| 1761 |
{ |
| 1762 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1763 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); |
| 1764 |
|
| 1765 |
ucontrol->value.integer.value[0] = aw88399->aw_pa->fade_step; |
| 1766 |
|
| 1767 |
return 0; |
| 1768 |
} |
| 1769 |
|
| 1770 |
static int aw88399_set_fade_step(struct snd_kcontrol *kcontrol, |
| 1771 |
struct snd_ctl_elem_value *ucontrol) |
| 1772 |
{ |
| 1773 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1774 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); |
| 1775 |
struct soc_mixer_control *mc = |
| 1776 |
(struct soc_mixer_control *)kcontrol->private_value; |
| 1777 |
int value; |
| 1778 |
|
| 1779 |
value = ucontrol->value.integer.value[0]; |
| 1780 |
if (value < mc->min || value > mc->max) |
| 1781 |
return -EINVAL; |
| 1782 |
|
| 1783 |
if (aw88399->aw_pa->fade_step != value) { |
| 1784 |
aw88399->aw_pa->fade_step = value; |
| 1785 |
return 1; |
| 1786 |
} |
| 1787 |
|
| 1788 |
return 0; |
| 1789 |
} |
| 1790 |
|
| 1791 |
static int aw88399_re_get(struct snd_kcontrol *kcontrol, |
| 1792 |
struct snd_ctl_elem_value *ucontrol) |
| 1793 |
{ |
| 1794 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1795 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); |
| 1796 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 1797 |
|
| 1798 |
ucontrol->value.integer.value[0] = aw_dev->cali_desc.cali_re; |
| 1799 |
|
| 1800 |
return 0; |
| 1801 |
} |
| 1802 |
|
| 1803 |
static int aw88399_re_set(struct snd_kcontrol *kcontrol, |
| 1804 |
struct snd_ctl_elem_value *ucontrol) |
| 1805 |
{ |
| 1806 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1807 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); |
| 1808 |
struct soc_mixer_control *mc = |
| 1809 |
(struct soc_mixer_control *)kcontrol->private_value; |
| 1810 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 1811 |
int value; |
| 1812 |
|
| 1813 |
value = ucontrol->value.integer.value[0]; |
| 1814 |
if (value < mc->min || value > mc->max) |
| 1815 |
return -EINVAL; |
| 1816 |
|
| 1817 |
if (aw_dev->cali_desc.cali_re != value) { |
| 1818 |
aw_dev->cali_desc.cali_re = value; |
| 1819 |
return 1; |
| 1820 |
} |
| 1821 |
|
| 1822 |
return 0; |
| 1823 |
} |
| 1824 |
|
| 1825 |
static int aw88399_calib_switch_get(struct snd_kcontrol *kcontrol, |
| 1826 |
struct snd_ctl_elem_value *ucontrol) |
| 1827 |
{ |
| 1828 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1829 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); |
| 1830 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 1831 |
|
| 1832 |
ucontrol->value.integer.value[0] = aw_dev->cali_desc.cali_switch; |
| 1833 |
|
| 1834 |
return 0; |
| 1835 |
} |
| 1836 |
|
| 1837 |
static int aw88399_calib_switch_set(struct snd_kcontrol *kcontrol, |
| 1838 |
struct snd_ctl_elem_value *ucontrol) |
| 1839 |
{ |
| 1840 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1841 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); |
| 1842 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 1843 |
|
| 1844 |
if (aw_dev->cali_desc.cali_switch == ucontrol->value.integer.value[0]) |
| 1845 |
return 0; |
| 1846 |
|
| 1847 |
aw_dev->cali_desc.cali_switch = ucontrol->value.integer.value[0]; |
| 1848 |
|
| 1849 |
return 1; |
| 1850 |
} |
| 1851 |
|
| 1852 |
static int aw88399_calib_get(struct snd_kcontrol *kcontrol, |
| 1853 |
struct snd_ctl_elem_value *ucontrol) |
| 1854 |
{ |
| 1855 |
/* do nothing */ |
| 1856 |
return 0; |
| 1857 |
} |
| 1858 |
|
| 1859 |
static int aw88399_calib_set(struct snd_kcontrol *kcontrol, |
| 1860 |
struct snd_ctl_elem_value *ucontrol) |
| 1861 |
{ |
| 1862 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1863 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(codec); |
| 1864 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 1865 |
|
| 1866 |
if (aw_dev->status && aw_dev->cali_desc.cali_switch) |
| 1867 |
aw_cali_svc_dev_cali_re(aw88399); |
| 1868 |
|
| 1869 |
return 0; |
| 1870 |
} |
| 1871 |
|
| 1872 |
static int aw88399_dev_init(struct aw88399 *aw88399, struct aw_container *aw_cfg) |
| 1873 |
{ |
| 1874 |
struct aw_device *aw_dev = aw88399->aw_pa; |
| 1875 |
int ret; |
| 1876 |
|
| 1877 |
ret = aw88395_dev_cfg_load(aw_dev, aw_cfg); |
| 1878 |
if (ret) { |
| 1879 |
dev_err(aw_dev->dev, "aw_dev acf parse failed"); |
| 1880 |
return -EINVAL; |
| 1881 |
} |
| 1882 |
aw_dev->fade_in_time = AW88399_1000_US / 10; |
| 1883 |
aw_dev->fade_out_time = AW88399_1000_US >> 1; |
| 1884 |
aw_dev->prof_cur = aw_dev->prof_info.prof_desc[0].id; |
| 1885 |
aw_dev->prof_index = aw_dev->prof_info.prof_desc[0].id; |
| 1886 |
|
| 1887 |
ret = aw88399_dev_fw_update(aw88399, AW88399_FORCE_UPDATE_ON, AW88399_DSP_FW_UPDATE_ON); |
| 1888 |
if (ret) { |
| 1889 |
dev_err(aw_dev->dev, "fw update failed ret = %d\n", ret); |
| 1890 |
return ret; |
| 1891 |
} |
| 1892 |
|
| 1893 |
aw88399_dev_mute(aw_dev, true); |
| 1894 |
|
| 1895 |
/* close tx feedback */ |
| 1896 |
aw_dev_i2s_tx_enable(aw_dev, false); |
| 1897 |
usleep_range(AW88399_1000_US, AW88399_1000_US + 100); |
| 1898 |
|
| 1899 |
/* enable amppd */ |
| 1900 |
aw_dev_amppd(aw_dev, true); |
| 1901 |
|
| 1902 |
/* close dsp */ |
| 1903 |
aw_dev_dsp_enable(aw_dev, false); |
| 1904 |
/* set power down */ |
| 1905 |
aw_dev_pwd(aw_dev, true); |
| 1906 |
|
| 1907 |
return 0; |
| 1908 |
} |
| 1909 |
|
| 1910 |
static int aw88399_request_firmware_file(struct aw88399 *aw88399) |
| 1911 |
{ |
| 1912 |
const struct firmware *cont = NULL; |
| 1913 |
int ret; |
| 1914 |
|
| 1915 |
aw88399->aw_pa->fw_status = AW88399_DEV_FW_FAILED; |
| 1916 |
|
| 1917 |
ret = request_firmware(&cont, AW88399_ACF_FILE, aw88399->aw_pa->dev); |
| 1918 |
if (ret) { |
| 1919 |
dev_err(aw88399->aw_pa->dev, "request [%s] failed!", AW88399_ACF_FILE); |
| 1920 |
return ret; |
| 1921 |
} |
| 1922 |
|
| 1923 |
dev_dbg(aw88399->aw_pa->dev, "loaded %s - size: %zu\n", |
| 1924 |
AW88399_ACF_FILE, cont ? cont->size : 0); |
| 1925 |
|
| 1926 |
aw88399->aw_cfg = devm_kzalloc(aw88399->aw_pa->dev, |
| 1927 |
struct_size(aw88399->aw_cfg, data, cont->size), GFP_KERNEL); |
| 1928 |
if (!aw88399->aw_cfg) { |
| 1929 |
release_firmware(cont); |
| 1930 |
return -ENOMEM; |
| 1931 |
} |
| 1932 |
aw88399->aw_cfg->len = (int)cont->size; |
| 1933 |
memcpy(aw88399->aw_cfg->data, cont->data, cont->size); |
| 1934 |
release_firmware(cont); |
| 1935 |
|
| 1936 |
ret = aw88395_dev_load_acf_check(aw88399->aw_pa, aw88399->aw_cfg); |
| 1937 |
if (ret) { |
| 1938 |
dev_err(aw88399->aw_pa->dev, "load [%s] failed!", AW88399_ACF_FILE); |
| 1939 |
return ret; |
| 1940 |
} |
| 1941 |
|
| 1942 |
mutex_lock(&aw88399->lock); |
| 1943 |
/* aw device init */ |
| 1944 |
ret = aw88399_dev_init(aw88399, aw88399->aw_cfg); |
| 1945 |
if (ret) |
| 1946 |
dev_err(aw88399->aw_pa->dev, "dev init failed"); |
| 1947 |
mutex_unlock(&aw88399->lock); |
| 1948 |
|
| 1949 |
return ret; |
| 1950 |
} |
| 1951 |
|
| 1952 |
static const struct snd_kcontrol_new aw88399_controls[] = { |
| 1953 |
SOC_SINGLE_EXT("PCM Playback Volume", AW88399_SYSCTRL2_REG, |
| 1954 |
6, AW88399_MUTE_VOL, 0, aw88399_volume_get, |
| 1955 |
aw88399_volume_set), |
| 1956 |
SOC_SINGLE_EXT("Fade Step", 0, 0, AW88399_MUTE_VOL, 0, |
| 1957 |
aw88399_get_fade_step, aw88399_set_fade_step), |
| 1958 |
SOC_SINGLE_EXT("Volume Ramp Up Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN, |
| 1959 |
aw88399_get_fade_in_time, aw88399_set_fade_in_time), |
| 1960 |
SOC_SINGLE_EXT("Volume Ramp Down Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN, |
| 1961 |
aw88399_get_fade_out_time, aw88399_set_fade_out_time), |
| 1962 |
SOC_SINGLE_EXT("Calib", 0, 0, AW88399_CALI_RE_MAX, 0, |
| 1963 |
aw88399_re_get, aw88399_re_set), |
| 1964 |
SOC_SINGLE_BOOL_EXT("Calib Switch", 0, |
| 1965 |
aw88399_calib_switch_get, aw88399_calib_switch_set), |
| 1966 |
SOC_SINGLE_EXT("Trigger Calib", SND_SOC_NOPM, 0, 1, 0, |
| 1967 |
aw88399_calib_get, aw88399_calib_set), |
| 1968 |
AW88399_PROFILE_EXT("AW88399 Profile Set", aw88399_profile_info, |
| 1969 |
aw88399_profile_get, aw88399_profile_set), |
| 1970 |
}; |
| 1971 |
|
| 1972 |
static int aw88399_playback_event(struct snd_soc_dapm_widget *w, |
| 1973 |
struct snd_kcontrol *k, int event) |
| 1974 |
{ |
| 1975 |
struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); |
| 1976 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component); |
| 1977 |
|
| 1978 |
mutex_lock(&aw88399->lock); |
| 1979 |
switch (event) { |
| 1980 |
case SND_SOC_DAPM_PRE_PMU: |
| 1981 |
aw88399_start(aw88399, AW88399_ASYNC_START); |
| 1982 |
break; |
| 1983 |
case SND_SOC_DAPM_POST_PMD: |
| 1984 |
aw88399_stop(aw88399->aw_pa); |
| 1985 |
break; |
| 1986 |
default: |
| 1987 |
break; |
| 1988 |
} |
| 1989 |
mutex_unlock(&aw88399->lock); |
| 1990 |
|
| 1991 |
return 0; |
| 1992 |
} |
| 1993 |
|
| 1994 |
static const struct snd_soc_dapm_widget aw88399_dapm_widgets[] = { |
| 1995 |
/* playback */ |
| 1996 |
SND_SOC_DAPM_AIF_IN_E("AIF_RX", "Speaker_Playback", 0, 0, 0, 0, |
| 1997 |
aw88399_playback_event, |
| 1998 |
SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), |
| 1999 |
SND_SOC_DAPM_OUTPUT("DAC Output"), |
| 2000 |
|
| 2001 |
/* capture */ |
| 2002 |
SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM, 0, 0), |
| 2003 |
SND_SOC_DAPM_INPUT("ADC Input"), |
| 2004 |
}; |
| 2005 |
|
| 2006 |
static const struct snd_soc_dapm_route aw88399_audio_map[] = { |
| 2007 |
{"DAC Output", NULL, "AIF_RX"}, |
| 2008 |
{"AIF_TX", NULL, "ADC Input"}, |
| 2009 |
}; |
| 2010 |
|
| 2011 |
static int aw88399_codec_probe(struct snd_soc_component *component) |
| 2012 |
{ |
| 2013 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(component); |
| 2014 |
int ret; |
| 2015 |
|
| 2016 |
INIT_DELAYED_WORK(&aw88399->start_work, aw88399_startup_work); |
| 2017 |
|
| 2018 |
ret = aw88399_request_firmware_file(aw88399); |
| 2019 |
if (ret) |
| 2020 |
dev_err(aw88399->aw_pa->dev, "%s failed\n", __func__); |
| 2021 |
|
| 2022 |
return ret; |
| 2023 |
} |
| 2024 |
|
| 2025 |
static void aw88399_codec_remove(struct snd_soc_component *aw_codec) |
| 2026 |
{ |
| 2027 |
struct aw88399 *aw88399 = snd_soc_component_get_drvdata(aw_codec); |
| 2028 |
|
| 2029 |
cancel_delayed_work_sync(&aw88399->start_work); |
| 2030 |
} |
| 2031 |
|
| 2032 |
static const struct snd_soc_component_driver soc_codec_dev_aw88399 = { |
| 2033 |
.probe = aw88399_codec_probe, |
| 2034 |
.remove = aw88399_codec_remove, |
| 2035 |
.dapm_widgets = aw88399_dapm_widgets, |
| 2036 |
.num_dapm_widgets = ARRAY_SIZE(aw88399_dapm_widgets), |
| 2037 |
.dapm_routes = aw88399_audio_map, |
| 2038 |
.num_dapm_routes = ARRAY_SIZE(aw88399_audio_map), |
| 2039 |
.controls = aw88399_controls, |
| 2040 |
.num_controls = ARRAY_SIZE(aw88399_controls), |
| 2041 |
}; |
| 2042 |
|
| 2043 |
static void aw88399_hw_reset(struct aw88399 *aw88399) |
| 2044 |
{ |
| 2045 |
if (aw88399->reset_gpio) { |
| 2046 |
gpiod_set_value_cansleep(aw88399->reset_gpio, 1); |
| 2047 |
usleep_range(AW88399_1000_US, AW88399_1000_US + 10); |
| 2048 |
gpiod_set_value_cansleep(aw88399->reset_gpio, 0); |
| 2049 |
usleep_range(AW88399_1000_US, AW88399_1000_US + 10); |
| 2050 |
gpiod_set_value_cansleep(aw88399->reset_gpio, 1); |
| 2051 |
usleep_range(AW88399_1000_US, AW88399_1000_US + 10); |
| 2052 |
} |
| 2053 |
} |
| 2054 |
|
| 2055 |
static void aw88399_parse_channel_dt(struct aw_device *aw_dev) |
| 2056 |
{ |
| 2057 |
struct device_node *np = aw_dev->dev->of_node; |
| 2058 |
u32 channel_value; |
| 2059 |
|
| 2060 |
of_property_read_u32(np, "awinic,audio-channel", &channel_value); |
| 2061 |
aw_dev->channel = channel_value; |
| 2062 |
} |
| 2063 |
|
| 2064 |
static int aw88399_init(struct aw88399 *aw88399, struct i2c_client *i2c, struct regmap *regmap) |
| 2065 |
{ |
| 2066 |
struct aw_device *aw_dev; |
| 2067 |
unsigned int chip_id; |
| 2068 |
int ret; |
| 2069 |
|
| 2070 |
ret = regmap_read(regmap, AW88399_ID_REG, &chip_id); |
| 2071 |
if (ret) { |
| 2072 |
dev_err(&i2c->dev, "%s read chipid error. ret = %d", __func__, ret); |
| 2073 |
return ret; |
| 2074 |
} |
| 2075 |
if (chip_id != AW88399_CHIP_ID) { |
| 2076 |
dev_err(&i2c->dev, "unsupported device"); |
| 2077 |
return -ENXIO; |
| 2078 |
} |
| 2079 |
dev_dbg(&i2c->dev, "chip id = %x\n", chip_id); |
| 2080 |
|
| 2081 |
aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL); |
| 2082 |
if (!aw_dev) |
| 2083 |
return -ENOMEM; |
| 2084 |
aw88399->aw_pa = aw_dev; |
| 2085 |
|
| 2086 |
aw_dev->i2c = i2c; |
| 2087 |
aw_dev->dev = &i2c->dev; |
| 2088 |
aw_dev->regmap = regmap; |
| 2089 |
mutex_init(&aw_dev->dsp_lock); |
| 2090 |
|
| 2091 |
aw_dev->chip_id = chip_id; |
| 2092 |
aw_dev->acf = NULL; |
| 2093 |
aw_dev->prof_info.prof_desc = NULL; |
| 2094 |
aw_dev->prof_info.count = 0; |
| 2095 |
aw_dev->prof_info.prof_type = AW88395_DEV_NONE_TYPE_ID; |
| 2096 |
aw_dev->channel = AW88399_DEV_DEFAULT_CH; |
| 2097 |
aw_dev->fw_status = AW88399_DEV_FW_FAILED; |
| 2098 |
|
| 2099 |
aw_dev->fade_step = AW88399_VOLUME_STEP_DB; |
| 2100 |
aw_dev->volume_desc.ctl_volume = AW88399_VOL_DEFAULT_VALUE; |
| 2101 |
|
| 2102 |
aw88399_parse_channel_dt(aw_dev); |
| 2103 |
|
| 2104 |
return 0; |
| 2105 |
} |
| 2106 |
|
| 2107 |
static int aw88399_i2c_probe(struct i2c_client *i2c) |
| 2108 |
{ |
| 2109 |
struct aw88399 *aw88399; |
| 2110 |
int ret; |
| 2111 |
|
| 2112 |
if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C)) |
| 2113 |
return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed"); |
| 2114 |
|
| 2115 |
aw88399 = devm_kzalloc(&i2c->dev, sizeof(*aw88399), GFP_KERNEL); |
| 2116 |
if (!aw88399) |
| 2117 |
return -ENOMEM; |
| 2118 |
|
| 2119 |
mutex_init(&aw88399->lock); |
| 2120 |
|
| 2121 |
i2c_set_clientdata(i2c, aw88399); |
| 2122 |
|
| 2123 |
aw88399->reset_gpio = devm_gpiod_get_optional(&i2c->dev, "reset", GPIOD_OUT_LOW); |
| 2124 |
if (IS_ERR(aw88399->reset_gpio)) |
| 2125 |
return dev_err_probe(&i2c->dev, PTR_ERR(aw88399->reset_gpio), |
| 2126 |
"reset gpio not defined\n"); |
| 2127 |
aw88399_hw_reset(aw88399); |
| 2128 |
|
| 2129 |
aw88399->regmap = devm_regmap_init_i2c(i2c, &aw88399_remap_config); |
| 2130 |
if (IS_ERR(aw88399->regmap)) |
| 2131 |
return dev_err_probe(&i2c->dev, PTR_ERR(aw88399->regmap), |
| 2132 |
"failed to init regmap\n"); |
| 2133 |
|
| 2134 |
/* aw pa init */ |
| 2135 |
ret = aw88399_init(aw88399, i2c, aw88399->regmap); |
| 2136 |
if (ret) |
| 2137 |
return ret; |
| 2138 |
|
| 2139 |
ret = devm_snd_soc_register_component(&i2c->dev, |
| 2140 |
&soc_codec_dev_aw88399, |
| 2141 |
aw88399_dai, ARRAY_SIZE(aw88399_dai)); |
| 2142 |
if (ret) |
| 2143 |
dev_err(&i2c->dev, "failed to register aw88399: %d", ret); |
| 2144 |
|
| 2145 |
return ret; |
| 2146 |
} |
| 2147 |
|
| 2148 |
static const struct i2c_device_id aw88399_i2c_id[] = { |
| 2149 |
{ AW88399_I2C_NAME }, |
| 2150 |
{ } |
| 2151 |
}; |
| 2152 |
MODULE_DEVICE_TABLE(i2c, aw88399_i2c_id); |
| 2153 |
|
| 2154 |
#ifdef CONFIG_ACPI |
| 2155 |
static const struct acpi_device_id aw88399_acpi_match[] = { |
| 2156 |
{ "AWDZ8399", 0 }, |
| 2157 |
{ }, |
| 2158 |
}; |
| 2159 |
MODULE_DEVICE_TABLE(acpi, aw88399_acpi_match); |
| 2160 |
#endif |
| 2161 |
|
| 2162 |
static struct i2c_driver aw88399_i2c_driver = { |
| 2163 |
.driver = { |
| 2164 |
.name = AW88399_I2C_NAME, |
| 2165 |
.acpi_match_table = ACPI_PTR(aw88399_acpi_match), |
| 2166 |
}, |
| 2167 |
.probe = aw88399_i2c_probe, |
| 2168 |
.id_table = aw88399_i2c_id, |
| 2169 |
}; |
| 2170 |
module_i2c_driver(aw88399_i2c_driver); |
| 2171 |
|
| 2172 |
MODULE_DESCRIPTION("ASoC AW88399 Smart PA Driver"); |
| 2173 |
MODULE_LICENSE("GPL v2"); |