| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
// |
| 3 |
// aw88166.c -- ALSA SoC AW88166 codec support |
| 4 |
// |
| 5 |
// Copyright (c) 2025 AWINIC Technology CO., LTD |
| 6 |
// |
| 7 |
// Author: Weidong Wang <[email protected]> |
| 8 |
// |
| 9 |
|
| 10 |
#include <linux/crc32.h> |
| 11 |
#include <linux/firmware.h> |
| 12 |
#include <linux/gpio/consumer.h> |
| 13 |
#include <linux/i2c.h> |
| 14 |
#include <linux/minmax.h> |
| 15 |
#include <linux/regmap.h> |
| 16 |
#include <sound/soc.h> |
| 17 |
#include "aw88166.h" |
| 18 |
#include "aw88395/aw88395_device.h" |
| 19 |
|
| 20 |
struct aw88166 { |
| 21 |
struct aw_device *aw_pa; |
| 22 |
struct mutex lock; |
| 23 |
struct gpio_desc *reset_gpio; |
| 24 |
struct delayed_work start_work; |
| 25 |
struct regmap *regmap; |
| 26 |
struct aw_container *aw_cfg; |
| 27 |
|
| 28 |
unsigned int check_val; |
| 29 |
unsigned int crc_init_val; |
| 30 |
unsigned int vcalb_init_val; |
| 31 |
unsigned int re_init_val; |
| 32 |
unsigned int dither_st; |
| 33 |
bool phase_sync; |
| 34 |
}; |
| 35 |
|
| 36 |
static const struct regmap_config aw88166_remap_config = { |
| 37 |
.val_bits = 16, |
| 38 |
.reg_bits = 8, |
| 39 |
.max_register = AW88166_REG_MAX, |
| 40 |
.reg_format_endian = REGMAP_ENDIAN_LITTLE, |
| 41 |
.val_format_endian = REGMAP_ENDIAN_BIG, |
| 42 |
}; |
| 43 |
|
| 44 |
static void aw_dev_pwd(struct aw_device *aw_dev, bool pwd) |
| 45 |
{ |
| 46 |
int ret; |
| 47 |
|
| 48 |
if (pwd) |
| 49 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG, |
| 50 |
~AW88166_PWDN_MASK, AW88166_PWDN_POWER_DOWN_VALUE); |
| 51 |
else |
| 52 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG, |
| 53 |
~AW88166_PWDN_MASK, AW88166_PWDN_WORKING_VALUE); |
| 54 |
|
| 55 |
if (ret) |
| 56 |
dev_dbg(aw_dev->dev, "%s failed", __func__); |
| 57 |
} |
| 58 |
|
| 59 |
static void aw_dev_get_int_status(struct aw_device *aw_dev, unsigned short *int_status) |
| 60 |
{ |
| 61 |
unsigned int reg_val; |
| 62 |
int ret; |
| 63 |
|
| 64 |
ret = regmap_read(aw_dev->regmap, AW88166_SYSINT_REG, ®_val); |
| 65 |
if (ret) |
| 66 |
dev_err(aw_dev->dev, "read interrupt reg fail, ret=%d", ret); |
| 67 |
else |
| 68 |
*int_status = reg_val; |
| 69 |
|
| 70 |
dev_dbg(aw_dev->dev, "read interrupt reg=0x%04x", *int_status); |
| 71 |
} |
| 72 |
|
| 73 |
static void aw_dev_clear_int_status(struct aw_device *aw_dev) |
| 74 |
{ |
| 75 |
u16 int_status; |
| 76 |
|
| 77 |
/* read int status and clear */ |
| 78 |
aw_dev_get_int_status(aw_dev, &int_status); |
| 79 |
/* make sure int status is clear */ |
| 80 |
aw_dev_get_int_status(aw_dev, &int_status); |
| 81 |
if (int_status) |
| 82 |
dev_dbg(aw_dev->dev, "int status(%d) is not cleaned.\n", int_status); |
| 83 |
} |
| 84 |
|
| 85 |
static int aw_dev_get_iis_status(struct aw_device *aw_dev) |
| 86 |
{ |
| 87 |
unsigned int reg_val; |
| 88 |
int ret; |
| 89 |
|
| 90 |
ret = regmap_read(aw_dev->regmap, AW88166_SYSST_REG, ®_val); |
| 91 |
if (ret) |
| 92 |
return ret; |
| 93 |
if ((reg_val & AW88166_BIT_PLL_CHECK) != AW88166_BIT_PLL_CHECK) { |
| 94 |
dev_err(aw_dev->dev, "check pll lock fail, reg_val:0x%04x", reg_val); |
| 95 |
return -EINVAL; |
| 96 |
} |
| 97 |
|
| 98 |
return 0; |
| 99 |
} |
| 100 |
|
| 101 |
static int aw_dev_check_mode1_pll(struct aw_device *aw_dev) |
| 102 |
{ |
| 103 |
int ret, i; |
| 104 |
|
| 105 |
for (i = 0; i < AW88166_DEV_SYSST_CHECK_MAX; i++) { |
| 106 |
ret = aw_dev_get_iis_status(aw_dev); |
| 107 |
if (ret) { |
| 108 |
dev_err(aw_dev->dev, "mode1 iis signal check error"); |
| 109 |
usleep_range(AW88166_2000_US, AW88166_2000_US + 10); |
| 110 |
} else { |
| 111 |
return 0; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return -EPERM; |
| 116 |
} |
| 117 |
|
| 118 |
static int aw_dev_check_mode2_pll(struct aw_device *aw_dev) |
| 119 |
{ |
| 120 |
unsigned int reg_val; |
| 121 |
int ret, i; |
| 122 |
|
| 123 |
ret = regmap_read(aw_dev->regmap, AW88166_PLLCTRL2_REG, ®_val); |
| 124 |
if (ret) |
| 125 |
return ret; |
| 126 |
|
| 127 |
reg_val &= (~AW88166_CCO_MUX_MASK); |
| 128 |
if (reg_val == AW88166_CCO_MUX_DIVIDED_VALUE) { |
| 129 |
dev_dbg(aw_dev->dev, "CCO_MUX is already divider"); |
| 130 |
return -EPERM; |
| 131 |
} |
| 132 |
|
| 133 |
/* change mode2 */ |
| 134 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_PLLCTRL2_REG, |
| 135 |
~AW88166_CCO_MUX_MASK, AW88166_CCO_MUX_DIVIDED_VALUE); |
| 136 |
if (ret) |
| 137 |
return ret; |
| 138 |
|
| 139 |
for (i = 0; i < AW88166_DEV_SYSST_CHECK_MAX; i++) { |
| 140 |
ret = aw_dev_get_iis_status(aw_dev); |
| 141 |
if (ret) { |
| 142 |
dev_err(aw_dev->dev, "mode2 iis signal check error"); |
| 143 |
usleep_range(AW88166_2000_US, AW88166_2000_US + 10); |
| 144 |
} else { |
| 145 |
break; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
/* change mode1 */ |
| 150 |
regmap_update_bits(aw_dev->regmap, AW88166_PLLCTRL2_REG, |
| 151 |
~AW88166_CCO_MUX_MASK, AW88166_CCO_MUX_BYPASS_VALUE); |
| 152 |
if (ret == 0) { |
| 153 |
usleep_range(AW88166_2000_US, AW88166_2000_US + 10); |
| 154 |
for (i = 0; i < AW88166_DEV_SYSST_CHECK_MAX; i++) { |
| 155 |
ret = aw_dev_get_iis_status(aw_dev); |
| 156 |
if (ret) { |
| 157 |
dev_err(aw_dev->dev, "mode2 switch to mode1, iis signal check error"); |
| 158 |
usleep_range(AW88166_2000_US, AW88166_2000_US + 10); |
| 159 |
} else { |
| 160 |
break; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return ret; |
| 166 |
} |
| 167 |
|
| 168 |
static int aw_dev_check_syspll(struct aw_device *aw_dev) |
| 169 |
{ |
| 170 |
int ret; |
| 171 |
|
| 172 |
ret = aw_dev_check_mode1_pll(aw_dev); |
| 173 |
if (ret) { |
| 174 |
dev_dbg(aw_dev->dev, "mode1 check iis failed try switch to mode2 check"); |
| 175 |
ret = aw_dev_check_mode2_pll(aw_dev); |
| 176 |
if (ret) { |
| 177 |
dev_err(aw_dev->dev, "mode2 check iis failed"); |
| 178 |
return ret; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
return 0; |
| 183 |
} |
| 184 |
|
| 185 |
static int aw_dev_check_sysst(struct aw_device *aw_dev) |
| 186 |
{ |
| 187 |
unsigned int check_val; |
| 188 |
unsigned int reg_val; |
| 189 |
int ret, i; |
| 190 |
|
| 191 |
ret = regmap_read(aw_dev->regmap, AW88166_PWMCTRL3_REG, ®_val); |
| 192 |
if (ret) |
| 193 |
return ret; |
| 194 |
|
| 195 |
if (reg_val & (~AW88166_NOISE_GATE_EN_MASK)) |
| 196 |
check_val = AW88166_BIT_SYSST_NOSWS_CHECK; |
| 197 |
else |
| 198 |
check_val = AW88166_BIT_SYSST_SWS_CHECK; |
| 199 |
|
| 200 |
for (i = 0; i < AW88166_DEV_SYSST_CHECK_MAX; i++) { |
| 201 |
ret = regmap_read(aw_dev->regmap, AW88166_SYSST_REG, ®_val); |
| 202 |
if (ret) |
| 203 |
return ret; |
| 204 |
|
| 205 |
if ((reg_val & (~AW88166_BIT_SYSST_CHECK_MASK) & check_val) != check_val) { |
| 206 |
dev_err(aw_dev->dev, "check sysst fail, cnt=%d, reg_val=0x%04x, check:0x%x", |
| 207 |
i, reg_val, AW88166_BIT_SYSST_NOSWS_CHECK); |
| 208 |
usleep_range(AW88166_2000_US, AW88166_2000_US + 10); |
| 209 |
} else { |
| 210 |
return 0; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return -EPERM; |
| 215 |
} |
| 216 |
|
| 217 |
static void aw_dev_amppd(struct aw_device *aw_dev, bool amppd) |
| 218 |
{ |
| 219 |
int ret; |
| 220 |
|
| 221 |
if (amppd) |
| 222 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG, |
| 223 |
~AW88166_AMPPD_MASK, AW88166_AMPPD_POWER_DOWN_VALUE); |
| 224 |
else |
| 225 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG, |
| 226 |
~AW88166_AMPPD_MASK, AW88166_AMPPD_WORKING_VALUE); |
| 227 |
|
| 228 |
if (ret) |
| 229 |
dev_dbg(aw_dev->dev, "%s failed", __func__); |
| 230 |
} |
| 231 |
|
| 232 |
static void aw_dev_dsp_enable(struct aw_device *aw_dev, bool is_enable) |
| 233 |
{ |
| 234 |
int ret; |
| 235 |
|
| 236 |
if (is_enable) |
| 237 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG, |
| 238 |
~AW88166_DSPBY_MASK, AW88166_DSPBY_WORKING_VALUE); |
| 239 |
else |
| 240 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG, |
| 241 |
~AW88166_DSPBY_MASK, AW88166_DSPBY_BYPASS_VALUE); |
| 242 |
|
| 243 |
if (ret) |
| 244 |
dev_dbg(aw_dev->dev, "%s failed\n", __func__); |
| 245 |
} |
| 246 |
|
| 247 |
static int aw88166_dev_get_icalk(struct aw88166 *aw88166, int16_t *icalk) |
| 248 |
{ |
| 249 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 250 |
unsigned int efrm_reg_val, efrl_reg_val; |
| 251 |
uint16_t ef_isn_geslp, ef_isn_h5bits; |
| 252 |
uint16_t icalk_val; |
| 253 |
int ret; |
| 254 |
|
| 255 |
ret = regmap_read(aw_dev->regmap, AW88166_EFRM2_REG, &efrm_reg_val); |
| 256 |
if (ret) |
| 257 |
return ret; |
| 258 |
|
| 259 |
ef_isn_geslp = (efrm_reg_val & (~AW88166_EF_ISN_GESLP_MASK)) >> |
| 260 |
AW88166_EF_ISN_GESLP_SHIFT; |
| 261 |
|
| 262 |
ret = regmap_read(aw_dev->regmap, AW88166_EFRL_REG, &efrl_reg_val); |
| 263 |
if (ret) |
| 264 |
return ret; |
| 265 |
|
| 266 |
ef_isn_h5bits = (efrl_reg_val & (~AW88166_EF_ISN_H5BITS_MASK)) >> |
| 267 |
AW88166_EF_ISN_H5BITS_SHIFT; |
| 268 |
|
| 269 |
if (aw88166->check_val == AW_EF_AND_CHECK) |
| 270 |
icalk_val = ef_isn_geslp & (ef_isn_h5bits | AW88166_EF_ISN_H5BITS_SIGN_MASK); |
| 271 |
else |
| 272 |
icalk_val = ef_isn_geslp | (ef_isn_h5bits & (~AW88166_EF_ISN_H5BITS_SIGN_MASK)); |
| 273 |
|
| 274 |
if (icalk_val & (~AW88166_ICALK_SIGN_MASK)) |
| 275 |
icalk_val = icalk_val | AW88166_ICALK_NEG_MASK; |
| 276 |
*icalk = (int16_t)icalk_val; |
| 277 |
|
| 278 |
return 0; |
| 279 |
} |
| 280 |
|
| 281 |
static int aw88166_dev_get_vcalk(struct aw88166 *aw88166, int16_t *vcalk) |
| 282 |
{ |
| 283 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 284 |
unsigned int efrm_reg_val, efrl_reg_val; |
| 285 |
uint16_t ef_vsn_geslp, ef_vsn_h3bits; |
| 286 |
uint16_t vcalk_val; |
| 287 |
int ret; |
| 288 |
|
| 289 |
ret = regmap_read(aw_dev->regmap, AW88166_EFRM2_REG, &efrm_reg_val); |
| 290 |
if (ret) |
| 291 |
return ret; |
| 292 |
|
| 293 |
ef_vsn_geslp = (efrm_reg_val & (~AW88166_EF_VSN_GESLP_MASK)) >> |
| 294 |
AW88166_EF_VSN_GESLP_SHIFT; |
| 295 |
|
| 296 |
ret = regmap_read(aw_dev->regmap, AW88166_EFRL_REG, &efrl_reg_val); |
| 297 |
if (ret) |
| 298 |
return ret; |
| 299 |
|
| 300 |
ef_vsn_h3bits = (efrl_reg_val & (~AW88166_EF_VSN_H3BITS_MASK)) >> |
| 301 |
AW88166_EF_VSN_H3BITS_SHIFT; |
| 302 |
|
| 303 |
if (aw88166->check_val == AW_EF_AND_CHECK) |
| 304 |
vcalk_val = ef_vsn_geslp & (ef_vsn_h3bits | AW88166_EF_VSN_H3BITS_SIGN_MASK); |
| 305 |
else |
| 306 |
vcalk_val = ef_vsn_geslp | (ef_vsn_h3bits & (~AW88166_EF_VSN_H3BITS_SIGN_MASK)); |
| 307 |
|
| 308 |
if (vcalk_val & (~AW88166_VCALK_SIGN_MASK)) |
| 309 |
vcalk_val = vcalk_val | AW88166_VCALK_NEG_MASK; |
| 310 |
*vcalk = (int16_t)vcalk_val; |
| 311 |
|
| 312 |
return 0; |
| 313 |
} |
| 314 |
|
| 315 |
static int aw88166_dev_set_vcalb(struct aw88166 *aw88166) |
| 316 |
{ |
| 317 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 318 |
int32_t ical_k, vcal_k, vcalb; |
| 319 |
int16_t icalk, vcalk; |
| 320 |
unsigned int reg_val; |
| 321 |
int ret; |
| 322 |
|
| 323 |
ret = aw88166_dev_get_icalk(aw88166, &icalk); |
| 324 |
if (ret) { |
| 325 |
dev_err(aw_dev->dev, "get icalk failed\n"); |
| 326 |
return ret; |
| 327 |
} |
| 328 |
ical_k = icalk * AW88166_ICABLK_FACTOR + AW88166_CABL_BASE_VALUE; |
| 329 |
|
| 330 |
ret = aw88166_dev_get_vcalk(aw88166, &vcalk); |
| 331 |
if (ret) { |
| 332 |
dev_err(aw_dev->dev, "get vbcalk failed\n"); |
| 333 |
return ret; |
| 334 |
} |
| 335 |
vcal_k = vcalk * AW88166_VCABLK_FACTOR + AW88166_CABL_BASE_VALUE; |
| 336 |
|
| 337 |
vcalb = AW88166_VCALB_ACCURACY * AW88166_VSCAL_FACTOR / |
| 338 |
AW88166_ISCAL_FACTOR * ical_k / vcal_k * aw88166->vcalb_init_val; |
| 339 |
|
| 340 |
vcalb = vcalb >> AW88166_VCALB_ADJ_FACTOR; |
| 341 |
reg_val = (uint32_t)vcalb; |
| 342 |
|
| 343 |
regmap_write(aw_dev->regmap, AW88166_DSPVCALB_REG, reg_val); |
| 344 |
|
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
static int aw_dev_init_vcalb_update(struct aw88166 *aw88166, int flag) |
| 349 |
{ |
| 350 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 351 |
int ret; |
| 352 |
|
| 353 |
switch (flag) { |
| 354 |
case AW88166_RECOVERY_SEC_DATA: |
| 355 |
ret = regmap_write(aw_dev->regmap, AW88166_DSPVCALB_REG, aw88166->vcalb_init_val); |
| 356 |
break; |
| 357 |
case AW88166_RECORD_SEC_DATA: |
| 358 |
ret = regmap_read(aw_dev->regmap, AW88166_DSPVCALB_REG, &aw88166->vcalb_init_val); |
| 359 |
break; |
| 360 |
default: |
| 361 |
dev_err(aw_dev->dev, "unsupported type:%d\n", flag); |
| 362 |
ret = -EINVAL; |
| 363 |
break; |
| 364 |
} |
| 365 |
|
| 366 |
return ret; |
| 367 |
} |
| 368 |
|
| 369 |
static int aw_dev_init_re_update(struct aw88166 *aw88166, int flag) |
| 370 |
{ |
| 371 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 372 |
unsigned int re_temp_h, re_temp_l; |
| 373 |
int ret; |
| 374 |
|
| 375 |
switch (flag) { |
| 376 |
case AW88166_RECOVERY_SEC_DATA: |
| 377 |
ret = regmap_write(aw_dev->regmap, AW88166_ACR1_REG, aw88166->re_init_val >> 16); |
| 378 |
if (ret) |
| 379 |
return ret; |
| 380 |
ret = regmap_write(aw_dev->regmap, AW88166_ACR2_REG, |
| 381 |
(uint16_t)aw88166->re_init_val); |
| 382 |
if (ret) |
| 383 |
return ret; |
| 384 |
break; |
| 385 |
case AW88166_RECORD_SEC_DATA: |
| 386 |
ret = regmap_read(aw_dev->regmap, AW88166_ACR1_REG, &re_temp_h); |
| 387 |
if (ret) |
| 388 |
return ret; |
| 389 |
ret = regmap_read(aw_dev->regmap, AW88166_ACR2_REG, &re_temp_l); |
| 390 |
if (ret) |
| 391 |
return ret; |
| 392 |
aw88166->re_init_val = (re_temp_h << 16) + re_temp_l; |
| 393 |
break; |
| 394 |
default: |
| 395 |
dev_err(aw_dev->dev, "unsupported type:%d\n", flag); |
| 396 |
ret = -EINVAL; |
| 397 |
break; |
| 398 |
} |
| 399 |
|
| 400 |
return ret; |
| 401 |
} |
| 402 |
|
| 403 |
static void aw_dev_backup_sec_record(struct aw88166 *aw88166) |
| 404 |
{ |
| 405 |
aw_dev_init_vcalb_update(aw88166, AW88166_RECORD_SEC_DATA); |
| 406 |
aw_dev_init_re_update(aw88166, AW88166_RECOVERY_SEC_DATA); |
| 407 |
} |
| 408 |
|
| 409 |
static void aw_dev_backup_sec_recovery(struct aw88166 *aw88166) |
| 410 |
{ |
| 411 |
aw_dev_init_vcalb_update(aw88166, AW88166_RECOVERY_SEC_DATA); |
| 412 |
aw_dev_init_re_update(aw88166, AW88166_RECOVERY_SEC_DATA); |
| 413 |
} |
| 414 |
|
| 415 |
static int aw_dev_update_cali_re(struct aw_cali_desc *cali_desc) |
| 416 |
{ |
| 417 |
struct aw_device *aw_dev = |
| 418 |
container_of(cali_desc, struct aw_device, cali_desc); |
| 419 |
uint16_t re_lbits, re_hbits; |
| 420 |
u32 cali_re; |
| 421 |
int ret; |
| 422 |
|
| 423 |
if ((aw_dev->cali_desc.cali_re >= AW88166_CALI_RE_MAX) || |
| 424 |
(aw_dev->cali_desc.cali_re <= AW88166_CALI_RE_MIN)) |
| 425 |
return -EINVAL; |
| 426 |
|
| 427 |
cali_re = AW88166_SHOW_RE_TO_DSP_RE((aw_dev->cali_desc.cali_re + |
| 428 |
aw_dev->cali_desc.ra), AW88166_DSP_RE_SHIFT); |
| 429 |
|
| 430 |
re_hbits = (cali_re & (~AW88166_CALI_RE_HBITS_MASK)) >> AW88166_CALI_RE_HBITS_SHIFT; |
| 431 |
re_lbits = (cali_re & (~AW88166_CALI_RE_LBITS_MASK)) >> AW88166_CALI_RE_LBITS_SHIFT; |
| 432 |
|
| 433 |
ret = regmap_write(aw_dev->regmap, AW88166_ACR1_REG, re_hbits); |
| 434 |
if (ret) { |
| 435 |
dev_err(aw_dev->dev, "set cali re error"); |
| 436 |
return ret; |
| 437 |
} |
| 438 |
|
| 439 |
ret = regmap_write(aw_dev->regmap, AW88166_ACR2_REG, re_lbits); |
| 440 |
if (ret) |
| 441 |
dev_err(aw_dev->dev, "set cali re error"); |
| 442 |
|
| 443 |
return ret; |
| 444 |
} |
| 445 |
|
| 446 |
static int aw_dev_fw_crc_check(struct aw_device *aw_dev) |
| 447 |
{ |
| 448 |
uint16_t check_val, fw_len_val; |
| 449 |
unsigned int reg_val; |
| 450 |
int ret; |
| 451 |
|
| 452 |
/* calculate fw_end_addr */ |
| 453 |
fw_len_val = ((aw_dev->dsp_fw_len / AW_FW_ADDR_LEN) - 1) + AW88166_CRC_FW_BASE_ADDR; |
| 454 |
|
| 455 |
/* write fw_end_addr to crc_end_addr */ |
| 456 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_CRCCTRL_REG, |
| 457 |
~AW88166_CRC_END_ADDR_MASK, fw_len_val); |
| 458 |
if (ret) |
| 459 |
return ret; |
| 460 |
/* enable fw crc check */ |
| 461 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_CRCCTRL_REG, |
| 462 |
~AW88166_CRC_CODE_EN_MASK, AW88166_CRC_CODE_EN_ENABLE_VALUE); |
| 463 |
|
| 464 |
usleep_range(AW88166_2000_US, AW88166_2000_US + 10); |
| 465 |
|
| 466 |
/* read crc check result */ |
| 467 |
regmap_read(aw_dev->regmap, AW88166_HAGCST_REG, ®_val); |
| 468 |
if (ret) |
| 469 |
return ret; |
| 470 |
|
| 471 |
check_val = (reg_val & (~AW88166_CRC_CHECK_BITS_MASK)) >> AW88166_CRC_CHECK_START_BIT; |
| 472 |
|
| 473 |
/* disable fw crc check */ |
| 474 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_CRCCTRL_REG, |
| 475 |
~AW88166_CRC_CODE_EN_MASK, AW88166_CRC_CODE_EN_DISABLE_VALUE); |
| 476 |
if (ret) |
| 477 |
return ret; |
| 478 |
|
| 479 |
if (check_val != AW88166_CRC_CHECK_PASS_VAL) { |
| 480 |
dev_err(aw_dev->dev, "%s failed, check_val 0x%x != 0x%x\n", |
| 481 |
__func__, check_val, AW88166_CRC_CHECK_PASS_VAL); |
| 482 |
ret = -EINVAL; |
| 483 |
} |
| 484 |
|
| 485 |
return ret; |
| 486 |
} |
| 487 |
|
| 488 |
static int aw_dev_cfg_crc_check(struct aw_device *aw_dev) |
| 489 |
{ |
| 490 |
uint16_t check_val, cfg_len_val; |
| 491 |
unsigned int reg_val; |
| 492 |
int ret; |
| 493 |
|
| 494 |
/* calculate cfg end addr */ |
| 495 |
cfg_len_val = ((aw_dev->dsp_cfg_len / AW_FW_ADDR_LEN) - 1) + AW88166_CRC_CFG_BASE_ADDR; |
| 496 |
|
| 497 |
/* write cfg_end_addr to crc_end_addr */ |
| 498 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_CRCCTRL_REG, |
| 499 |
~AW88166_CRC_END_ADDR_MASK, cfg_len_val); |
| 500 |
if (ret) |
| 501 |
return ret; |
| 502 |
|
| 503 |
/* enable cfg crc check */ |
| 504 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_CRCCTRL_REG, |
| 505 |
~AW88166_CRC_CFG_EN_MASK, AW88166_CRC_CFG_EN_ENABLE_VALUE); |
| 506 |
if (ret) |
| 507 |
return ret; |
| 508 |
|
| 509 |
usleep_range(AW88166_1000_US, AW88166_1000_US + 10); |
| 510 |
|
| 511 |
/* read crc check result */ |
| 512 |
ret = regmap_read(aw_dev->regmap, AW88166_HAGCST_REG, ®_val); |
| 513 |
if (ret) |
| 514 |
return ret; |
| 515 |
|
| 516 |
check_val = (reg_val & (~AW88166_CRC_CHECK_BITS_MASK)) >> AW88166_CRC_CHECK_START_BIT; |
| 517 |
|
| 518 |
/* disable cfg crc check */ |
| 519 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_CRCCTRL_REG, |
| 520 |
~AW88166_CRC_CFG_EN_MASK, AW88166_CRC_CFG_EN_DISABLE_VALUE); |
| 521 |
if (ret) |
| 522 |
return ret; |
| 523 |
|
| 524 |
if (check_val != AW88166_CRC_CHECK_PASS_VAL) { |
| 525 |
dev_err(aw_dev->dev, "crc_check failed, check val 0x%x != 0x%x\n", |
| 526 |
check_val, AW88166_CRC_CHECK_PASS_VAL); |
| 527 |
ret = -EINVAL; |
| 528 |
} |
| 529 |
|
| 530 |
return ret; |
| 531 |
} |
| 532 |
|
| 533 |
static int aw_dev_hw_crc_check(struct aw88166 *aw88166) |
| 534 |
{ |
| 535 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 536 |
int ret; |
| 537 |
|
| 538 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_I2SCFG1_REG, |
| 539 |
~AW88166_RAM_CG_BYP_MASK, AW88166_RAM_CG_BYP_BYPASS_VALUE); |
| 540 |
if (ret) |
| 541 |
return ret; |
| 542 |
|
| 543 |
ret = aw_dev_fw_crc_check(aw_dev); |
| 544 |
if (ret) { |
| 545 |
dev_err(aw_dev->dev, "fw_crc_check failed\n"); |
| 546 |
goto crc_check_failed; |
| 547 |
} |
| 548 |
|
| 549 |
ret = aw_dev_cfg_crc_check(aw_dev); |
| 550 |
if (ret) { |
| 551 |
dev_err(aw_dev->dev, "cfg_crc_check failed\n"); |
| 552 |
goto crc_check_failed; |
| 553 |
} |
| 554 |
|
| 555 |
ret = regmap_write(aw_dev->regmap, AW88166_CRCCTRL_REG, aw88166->crc_init_val); |
| 556 |
if (ret) |
| 557 |
return ret; |
| 558 |
|
| 559 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_I2SCFG1_REG, |
| 560 |
~AW88166_RAM_CG_BYP_MASK, AW88166_RAM_CG_BYP_WORK_VALUE); |
| 561 |
|
| 562 |
return ret; |
| 563 |
|
| 564 |
crc_check_failed: |
| 565 |
regmap_update_bits(aw_dev->regmap, AW88166_I2SCFG1_REG, |
| 566 |
~AW88166_RAM_CG_BYP_MASK, AW88166_RAM_CG_BYP_WORK_VALUE); |
| 567 |
return ret; |
| 568 |
} |
| 569 |
|
| 570 |
static void aw_dev_i2s_tx_enable(struct aw_device *aw_dev, bool flag) |
| 571 |
{ |
| 572 |
int ret; |
| 573 |
|
| 574 |
if (flag) |
| 575 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_I2SCTRL3_REG, |
| 576 |
~AW88166_I2STXEN_MASK, AW88166_I2STXEN_ENABLE_VALUE); |
| 577 |
else |
| 578 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_I2SCTRL3_REG, |
| 579 |
~AW88166_I2STXEN_MASK, AW88166_I2STXEN_DISABLE_VALUE); |
| 580 |
|
| 581 |
if (ret) |
| 582 |
dev_dbg(aw_dev->dev, "%s failed", __func__); |
| 583 |
} |
| 584 |
|
| 585 |
static int aw_dev_get_dsp_status(struct aw_device *aw_dev) |
| 586 |
{ |
| 587 |
unsigned int reg_val; |
| 588 |
int ret; |
| 589 |
|
| 590 |
ret = regmap_read(aw_dev->regmap, AW88166_WDT_REG, ®_val); |
| 591 |
if (ret) |
| 592 |
return ret; |
| 593 |
if (!(reg_val & (~AW88166_WDT_CNT_MASK))) |
| 594 |
return -EPERM; |
| 595 |
|
| 596 |
return 0; |
| 597 |
} |
| 598 |
|
| 599 |
static int aw_dev_dsp_check(struct aw_device *aw_dev) |
| 600 |
{ |
| 601 |
int ret, i; |
| 602 |
|
| 603 |
switch (aw_dev->dsp_cfg) { |
| 604 |
case AW88166_DEV_DSP_BYPASS: |
| 605 |
dev_dbg(aw_dev->dev, "dsp bypass"); |
| 606 |
ret = 0; |
| 607 |
break; |
| 608 |
case AW88166_DEV_DSP_WORK: |
| 609 |
aw_dev_dsp_enable(aw_dev, false); |
| 610 |
aw_dev_dsp_enable(aw_dev, true); |
| 611 |
usleep_range(AW88166_1000_US, AW88166_1000_US + 10); |
| 612 |
for (i = 0; i < AW88166_DEV_DSP_CHECK_MAX; i++) { |
| 613 |
ret = aw_dev_get_dsp_status(aw_dev); |
| 614 |
if (ret) { |
| 615 |
dev_err(aw_dev->dev, "dsp wdt status error=%d", ret); |
| 616 |
usleep_range(AW88166_2000_US, AW88166_2000_US + 10); |
| 617 |
} |
| 618 |
} |
| 619 |
break; |
| 620 |
default: |
| 621 |
dev_err(aw_dev->dev, "unknown dsp cfg=%d", aw_dev->dsp_cfg); |
| 622 |
ret = -EINVAL; |
| 623 |
break; |
| 624 |
} |
| 625 |
|
| 626 |
return ret; |
| 627 |
} |
| 628 |
|
| 629 |
static int aw_dev_set_volume(struct aw_device *aw_dev, unsigned int value) |
| 630 |
{ |
| 631 |
struct aw_volume_desc *vol_desc = &aw_dev->volume_desc; |
| 632 |
unsigned int reg_value; |
| 633 |
u16 real_value; |
| 634 |
int ret; |
| 635 |
|
| 636 |
real_value = min((value + vol_desc->init_volume), (unsigned int)AW88166_MUTE_VOL); |
| 637 |
|
| 638 |
ret = regmap_read(aw_dev->regmap, AW88166_SYSCTRL2_REG, ®_value); |
| 639 |
if (ret) |
| 640 |
return ret; |
| 641 |
|
| 642 |
dev_dbg(aw_dev->dev, "value 0x%x , reg:0x%x", value, real_value); |
| 643 |
|
| 644 |
real_value = (real_value << AW88166_VOL_START_BIT) | (reg_value & AW88166_VOL_MASK); |
| 645 |
|
| 646 |
ret = regmap_write(aw_dev->regmap, AW88166_SYSCTRL2_REG, real_value); |
| 647 |
|
| 648 |
return ret; |
| 649 |
} |
| 650 |
|
| 651 |
static void aw_dev_fade_in(struct aw_device *aw_dev) |
| 652 |
{ |
| 653 |
struct aw_volume_desc *desc = &aw_dev->volume_desc; |
| 654 |
u16 fade_in_vol = desc->ctl_volume; |
| 655 |
int fade_step = aw_dev->fade_step; |
| 656 |
int i; |
| 657 |
|
| 658 |
if (fade_step == 0 || aw_dev->fade_in_time == 0) { |
| 659 |
aw_dev_set_volume(aw_dev, fade_in_vol); |
| 660 |
return; |
| 661 |
} |
| 662 |
|
| 663 |
for (i = AW88166_MUTE_VOL; i >= fade_in_vol; i -= fade_step) { |
| 664 |
aw_dev_set_volume(aw_dev, i); |
| 665 |
usleep_range(aw_dev->fade_in_time, aw_dev->fade_in_time + 10); |
| 666 |
} |
| 667 |
|
| 668 |
if (i != fade_in_vol) |
| 669 |
aw_dev_set_volume(aw_dev, fade_in_vol); |
| 670 |
} |
| 671 |
|
| 672 |
static void aw_dev_fade_out(struct aw_device *aw_dev) |
| 673 |
{ |
| 674 |
struct aw_volume_desc *desc = &aw_dev->volume_desc; |
| 675 |
int fade_step = aw_dev->fade_step; |
| 676 |
int i; |
| 677 |
|
| 678 |
if (fade_step == 0 || aw_dev->fade_out_time == 0) { |
| 679 |
aw_dev_set_volume(aw_dev, AW88166_MUTE_VOL); |
| 680 |
return; |
| 681 |
} |
| 682 |
|
| 683 |
for (i = desc->ctl_volume; i <= AW88166_MUTE_VOL; i += fade_step) { |
| 684 |
aw_dev_set_volume(aw_dev, i); |
| 685 |
usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10); |
| 686 |
} |
| 687 |
|
| 688 |
if (i != AW88166_MUTE_VOL) { |
| 689 |
aw_dev_set_volume(aw_dev, AW88166_MUTE_VOL); |
| 690 |
usleep_range(aw_dev->fade_out_time, aw_dev->fade_out_time + 10); |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
static void aw88166_dev_mute(struct aw_device *aw_dev, bool is_mute) |
| 695 |
{ |
| 696 |
if (is_mute) { |
| 697 |
aw_dev_fade_out(aw_dev); |
| 698 |
regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG, |
| 699 |
~AW88166_HMUTE_MASK, AW88166_HMUTE_ENABLE_VALUE); |
| 700 |
} else { |
| 701 |
regmap_update_bits(aw_dev->regmap, AW88166_SYSCTRL_REG, |
| 702 |
~AW88166_HMUTE_MASK, AW88166_HMUTE_DISABLE_VALUE); |
| 703 |
aw_dev_fade_in(aw_dev); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
static void aw88166_dev_set_dither(struct aw88166 *aw88166, bool dither) |
| 708 |
{ |
| 709 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 710 |
|
| 711 |
if (dither) |
| 712 |
regmap_update_bits(aw_dev->regmap, AW88166_DBGCTRL_REG, |
| 713 |
~AW88166_DITHER_EN_MASK, AW88166_DITHER_EN_ENABLE_VALUE); |
| 714 |
else |
| 715 |
regmap_update_bits(aw_dev->regmap, AW88166_DBGCTRL_REG, |
| 716 |
~AW88166_DITHER_EN_MASK, AW88166_DITHER_EN_DISABLE_VALUE); |
| 717 |
} |
| 718 |
|
| 719 |
static int aw88166_dev_start(struct aw88166 *aw88166) |
| 720 |
{ |
| 721 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 722 |
int ret; |
| 723 |
|
| 724 |
if (aw_dev->status == AW88166_DEV_PW_ON) { |
| 725 |
dev_dbg(aw_dev->dev, "already power on"); |
| 726 |
return 0; |
| 727 |
} |
| 728 |
|
| 729 |
aw88166_dev_set_dither(aw88166, false); |
| 730 |
|
| 731 |
/* power on */ |
| 732 |
aw_dev_pwd(aw_dev, false); |
| 733 |
usleep_range(AW88166_2000_US, AW88166_2000_US + 10); |
| 734 |
|
| 735 |
ret = aw_dev_check_syspll(aw_dev); |
| 736 |
if (ret) { |
| 737 |
dev_err(aw_dev->dev, "pll check failed cannot start\n"); |
| 738 |
goto pll_check_fail; |
| 739 |
} |
| 740 |
|
| 741 |
/* amppd on */ |
| 742 |
aw_dev_amppd(aw_dev, false); |
| 743 |
usleep_range(AW88166_1000_US, AW88166_1000_US + 50); |
| 744 |
|
| 745 |
/* check i2s status */ |
| 746 |
ret = aw_dev_check_sysst(aw_dev); |
| 747 |
if (ret) { |
| 748 |
dev_err(aw_dev->dev, "sysst check failed\n"); |
| 749 |
goto sysst_check_fail; |
| 750 |
} |
| 751 |
|
| 752 |
if (aw_dev->dsp_cfg == AW88166_DEV_DSP_WORK) { |
| 753 |
aw_dev_backup_sec_recovery(aw88166); |
| 754 |
ret = aw_dev_hw_crc_check(aw88166); |
| 755 |
if (ret) { |
| 756 |
dev_err(aw_dev->dev, "dsp crc check failed\n"); |
| 757 |
goto crc_check_fail; |
| 758 |
} |
| 759 |
aw_dev_dsp_enable(aw_dev, false); |
| 760 |
aw88166_dev_set_vcalb(aw88166); |
| 761 |
aw_dev_update_cali_re(&aw_dev->cali_desc); |
| 762 |
ret = aw_dev_dsp_check(aw_dev); |
| 763 |
if (ret) { |
| 764 |
dev_err(aw_dev->dev, "dsp status check failed\n"); |
| 765 |
goto dsp_check_fail; |
| 766 |
} |
| 767 |
} else { |
| 768 |
dev_dbg(aw_dev->dev, "start pa with dsp bypass"); |
| 769 |
} |
| 770 |
|
| 771 |
/* enable tx feedback */ |
| 772 |
aw_dev_i2s_tx_enable(aw_dev, true); |
| 773 |
|
| 774 |
if (aw88166->dither_st == AW88166_DITHER_EN_ENABLE_VALUE) |
| 775 |
aw88166_dev_set_dither(aw88166, true); |
| 776 |
|
| 777 |
/* close mute */ |
| 778 |
aw88166_dev_mute(aw_dev, false); |
| 779 |
/* clear inturrupt */ |
| 780 |
aw_dev_clear_int_status(aw_dev); |
| 781 |
aw_dev->status = AW88166_DEV_PW_ON; |
| 782 |
|
| 783 |
return 0; |
| 784 |
|
| 785 |
dsp_check_fail: |
| 786 |
crc_check_fail: |
| 787 |
aw_dev_dsp_enable(aw_dev, false); |
| 788 |
sysst_check_fail: |
| 789 |
aw_dev_clear_int_status(aw_dev); |
| 790 |
aw_dev_amppd(aw_dev, true); |
| 791 |
pll_check_fail: |
| 792 |
aw_dev_pwd(aw_dev, true); |
| 793 |
aw_dev->status = AW88166_DEV_PW_OFF; |
| 794 |
|
| 795 |
return ret; |
| 796 |
} |
| 797 |
|
| 798 |
static int aw_dev_dsp_update_container(struct aw_device *aw_dev, |
| 799 |
unsigned char *data, unsigned int len, unsigned short base) |
| 800 |
{ |
| 801 |
u32 tmp_len; |
| 802 |
int i, ret; |
| 803 |
|
| 804 |
ret = regmap_write(aw_dev->regmap, AW88166_DSPMADD_REG, base); |
| 805 |
if (ret) |
| 806 |
return ret; |
| 807 |
|
| 808 |
for (i = 0; i < len; i += AW88166_MAX_RAM_WRITE_BYTE_SIZE) { |
| 809 |
tmp_len = min(len - i, AW88166_MAX_RAM_WRITE_BYTE_SIZE); |
| 810 |
ret = regmap_raw_write(aw_dev->regmap, AW88166_DSPMDAT_REG, |
| 811 |
&data[i], tmp_len); |
| 812 |
if (ret) |
| 813 |
return ret; |
| 814 |
} |
| 815 |
|
| 816 |
return 0; |
| 817 |
} |
| 818 |
|
| 819 |
static int aw_dev_get_ra(struct aw_cali_desc *cali_desc) |
| 820 |
{ |
| 821 |
struct aw_device *aw_dev = |
| 822 |
container_of(cali_desc, struct aw_device, cali_desc); |
| 823 |
u32 dsp_ra; |
| 824 |
int ret; |
| 825 |
|
| 826 |
ret = aw_dev_dsp_read(aw_dev, AW88166_DSP_REG_CFG_ADPZ_RA, |
| 827 |
&dsp_ra, AW_DSP_32_DATA); |
| 828 |
if (ret) { |
| 829 |
dev_err(aw_dev->dev, "read ra error\n"); |
| 830 |
return ret; |
| 831 |
} |
| 832 |
|
| 833 |
cali_desc->ra = AW88166_DSP_RE_TO_SHOW_RE(dsp_ra, |
| 834 |
AW88166_DSP_RE_SHIFT); |
| 835 |
|
| 836 |
return 0; |
| 837 |
} |
| 838 |
|
| 839 |
static int aw_dev_dsp_update_cfg(struct aw_device *aw_dev, |
| 840 |
unsigned char *data, unsigned int len) |
| 841 |
{ |
| 842 |
int ret; |
| 843 |
|
| 844 |
dev_dbg(aw_dev->dev, "dsp config len:%d", len); |
| 845 |
|
| 846 |
if (!len || !data) { |
| 847 |
dev_err(aw_dev->dev, "dsp config data is null or len is 0\n"); |
| 848 |
return -EINVAL; |
| 849 |
} |
| 850 |
|
| 851 |
ret = aw_dev_dsp_update_container(aw_dev, data, len, AW88166_DSP_CFG_ADDR); |
| 852 |
if (ret) |
| 853 |
return ret; |
| 854 |
|
| 855 |
aw_dev->dsp_cfg_len = len; |
| 856 |
|
| 857 |
ret = aw_dev_get_ra(&aw_dev->cali_desc); |
| 858 |
|
| 859 |
return ret; |
| 860 |
} |
| 861 |
|
| 862 |
static int aw_dev_dsp_update_fw(struct aw_device *aw_dev, |
| 863 |
unsigned char *data, unsigned int len) |
| 864 |
{ |
| 865 |
int ret; |
| 866 |
|
| 867 |
dev_dbg(aw_dev->dev, "dsp firmware len:%d", len); |
| 868 |
|
| 869 |
if (!len || !data) { |
| 870 |
dev_err(aw_dev->dev, "dsp firmware data is null or len is 0\n"); |
| 871 |
return -EINVAL; |
| 872 |
} |
| 873 |
|
| 874 |
aw_dev->dsp_fw_len = len; |
| 875 |
ret = aw_dev_dsp_update_container(aw_dev, data, len, AW88166_DSP_FW_ADDR); |
| 876 |
|
| 877 |
return ret; |
| 878 |
} |
| 879 |
|
| 880 |
static int aw_dev_check_sram(struct aw_device *aw_dev) |
| 881 |
{ |
| 882 |
unsigned int reg_val; |
| 883 |
|
| 884 |
/* read dsp_rom_check_reg */ |
| 885 |
aw_dev_dsp_read(aw_dev, AW88166_DSP_ROM_CHECK_ADDR, ®_val, AW_DSP_16_DATA); |
| 886 |
if (reg_val != AW88166_DSP_ROM_CHECK_DATA) { |
| 887 |
dev_err(aw_dev->dev, "check dsp rom failed, read[0x%x] != check[0x%x]\n", |
| 888 |
reg_val, AW88166_DSP_ROM_CHECK_DATA); |
| 889 |
return -EPERM; |
| 890 |
} |
| 891 |
|
| 892 |
/* check dsp_cfg_base_addr */ |
| 893 |
aw_dev_dsp_write(aw_dev, AW88166_DSP_CFG_ADDR, |
| 894 |
AW88166_DSP_ODD_NUM_BIT_TEST, AW_DSP_16_DATA); |
| 895 |
aw_dev_dsp_read(aw_dev, AW88166_DSP_CFG_ADDR, ®_val, AW_DSP_16_DATA); |
| 896 |
if (reg_val != AW88166_DSP_ODD_NUM_BIT_TEST) { |
| 897 |
dev_err(aw_dev->dev, "check dsp cfg failed, read[0x%x] != write[0x%x]\n", |
| 898 |
reg_val, AW88166_DSP_ODD_NUM_BIT_TEST); |
| 899 |
return -EPERM; |
| 900 |
} |
| 901 |
|
| 902 |
return 0; |
| 903 |
} |
| 904 |
|
| 905 |
static void aw_dev_select_memclk(struct aw_device *aw_dev, unsigned char flag) |
| 906 |
{ |
| 907 |
int ret; |
| 908 |
|
| 909 |
switch (flag) { |
| 910 |
case AW88166_DEV_MEMCLK_PLL: |
| 911 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_DBGCTRL_REG, |
| 912 |
~AW88166_MEM_CLKSEL_MASK, |
| 913 |
AW88166_MEM_CLKSEL_DAPHCLK_VALUE); |
| 914 |
if (ret) |
| 915 |
dev_err(aw_dev->dev, "memclk select pll failed\n"); |
| 916 |
break; |
| 917 |
case AW88166_DEV_MEMCLK_OSC: |
| 918 |
ret = regmap_update_bits(aw_dev->regmap, AW88166_DBGCTRL_REG, |
| 919 |
~AW88166_MEM_CLKSEL_MASK, |
| 920 |
AW88166_MEM_CLKSEL_OSCCLK_VALUE); |
| 921 |
if (ret) |
| 922 |
dev_err(aw_dev->dev, "memclk select OSC failed\n"); |
| 923 |
break; |
| 924 |
default: |
| 925 |
dev_err(aw_dev->dev, "unknown memclk config, flag=0x%x\n", flag); |
| 926 |
break; |
| 927 |
} |
| 928 |
} |
| 929 |
|
| 930 |
static int aw_dev_update_reg_container(struct aw88166 *aw88166, |
| 931 |
unsigned char *data, unsigned int len) |
| 932 |
{ |
| 933 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 934 |
struct aw_volume_desc *vol_desc = &aw_dev->volume_desc; |
| 935 |
u16 read_vol, reg_val; |
| 936 |
int data_len, i, ret; |
| 937 |
int16_t *reg_data; |
| 938 |
u8 reg_addr; |
| 939 |
|
| 940 |
reg_data = (int16_t *)data; |
| 941 |
data_len = len >> 1; |
| 942 |
|
| 943 |
if (data_len & 0x1) { |
| 944 |
dev_err(aw_dev->dev, "data len:%d unsupported\n", data_len); |
| 945 |
return -EINVAL; |
| 946 |
} |
| 947 |
|
| 948 |
for (i = 0; i < data_len; i += 2) { |
| 949 |
reg_addr = reg_data[i]; |
| 950 |
reg_val = reg_data[i + 1]; |
| 951 |
|
| 952 |
if (reg_addr == AW88166_DSPVCALB_REG) { |
| 953 |
aw88166->vcalb_init_val = reg_val; |
| 954 |
continue; |
| 955 |
} |
| 956 |
|
| 957 |
if (reg_addr == AW88166_SYSCTRL_REG) { |
| 958 |
if (reg_val & (~AW88166_DSPBY_MASK)) |
| 959 |
aw_dev->dsp_cfg = AW88166_DEV_DSP_BYPASS; |
| 960 |
else |
| 961 |
aw_dev->dsp_cfg = AW88166_DEV_DSP_WORK; |
| 962 |
|
| 963 |
reg_val &= (AW88166_HMUTE_MASK | AW88166_PWDN_MASK | |
| 964 |
AW88166_DSPBY_MASK); |
| 965 |
reg_val |= (AW88166_HMUTE_ENABLE_VALUE | AW88166_PWDN_POWER_DOWN_VALUE | |
| 966 |
AW88166_DSPBY_BYPASS_VALUE); |
| 967 |
} |
| 968 |
|
| 969 |
if (reg_addr == AW88166_I2SCTRL3_REG) { |
| 970 |
reg_val &= AW88166_I2STXEN_MASK; |
| 971 |
reg_val |= AW88166_I2STXEN_DISABLE_VALUE; |
| 972 |
} |
| 973 |
|
| 974 |
if (reg_addr == AW88166_SYSCTRL2_REG) { |
| 975 |
read_vol = (reg_val & (~AW88166_VOL_MASK)) >> |
| 976 |
AW88166_VOL_START_BIT; |
| 977 |
aw_dev->volume_desc.init_volume = read_vol; |
| 978 |
} |
| 979 |
|
| 980 |
if (reg_addr == AW88166_DBGCTRL_REG) { |
| 981 |
if ((reg_val & (~AW88166_EF_DBMD_MASK)) == AW88166_EF_DBMD_OR_VALUE) |
| 982 |
aw88166->check_val = AW_EF_OR_CHECK; |
| 983 |
else |
| 984 |
aw88166->check_val = AW_EF_AND_CHECK; |
| 985 |
|
| 986 |
aw88166->dither_st = reg_val & (~AW88166_DITHER_EN_MASK); |
| 987 |
} |
| 988 |
|
| 989 |
if (reg_addr == AW88166_ACR1_REG) { |
| 990 |
aw88166->re_init_val |= (uint32_t)reg_val << 16; |
| 991 |
continue; |
| 992 |
} |
| 993 |
|
| 994 |
if (reg_addr == AW88166_ACR2_REG) { |
| 995 |
aw88166->re_init_val |= (uint32_t)reg_val; |
| 996 |
continue; |
| 997 |
} |
| 998 |
|
| 999 |
if (reg_addr == AW88166_CRCCTRL_REG) |
| 1000 |
aw88166->crc_init_val = reg_val; |
| 1001 |
|
| 1002 |
ret = regmap_write(aw_dev->regmap, reg_addr, reg_val); |
| 1003 |
if (ret) |
| 1004 |
return ret; |
| 1005 |
} |
| 1006 |
|
| 1007 |
aw_dev_pwd(aw_dev, false); |
| 1008 |
usleep_range(AW88166_1000_US, AW88166_1000_US + 10); |
| 1009 |
|
| 1010 |
if (aw_dev->prof_cur != aw_dev->prof_index) |
| 1011 |
vol_desc->ctl_volume = 0; |
| 1012 |
else |
| 1013 |
aw_dev_set_volume(aw_dev, vol_desc->ctl_volume); |
| 1014 |
|
| 1015 |
return 0; |
| 1016 |
} |
| 1017 |
|
| 1018 |
static int aw_dev_reg_update(struct aw88166 *aw88166, |
| 1019 |
unsigned char *data, unsigned int len) |
| 1020 |
{ |
| 1021 |
int ret; |
| 1022 |
|
| 1023 |
if (!len || !data) { |
| 1024 |
dev_err(aw88166->aw_pa->dev, "reg data is null or len is 0\n"); |
| 1025 |
return -EINVAL; |
| 1026 |
} |
| 1027 |
|
| 1028 |
ret = aw_dev_update_reg_container(aw88166, data, len); |
| 1029 |
if (ret) |
| 1030 |
dev_err(aw88166->aw_pa->dev, "reg update failed\n"); |
| 1031 |
|
| 1032 |
return ret; |
| 1033 |
} |
| 1034 |
|
| 1035 |
static int aw88166_dev_get_prof_name(struct aw_device *aw_dev, int index, char **prof_name) |
| 1036 |
{ |
| 1037 |
struct aw_prof_info *prof_info = &aw_dev->prof_info; |
| 1038 |
struct aw_prof_desc *prof_desc; |
| 1039 |
|
| 1040 |
if ((index >= aw_dev->prof_info.count) || (index < 0)) { |
| 1041 |
dev_err(aw_dev->dev, "index[%d] overflow count[%d]\n", |
| 1042 |
index, aw_dev->prof_info.count); |
| 1043 |
return -EINVAL; |
| 1044 |
} |
| 1045 |
|
| 1046 |
prof_desc = &aw_dev->prof_info.prof_desc[index]; |
| 1047 |
|
| 1048 |
*prof_name = prof_info->prof_name_list[prof_desc->id]; |
| 1049 |
|
| 1050 |
return 0; |
| 1051 |
} |
| 1052 |
|
| 1053 |
static int aw88166_dev_get_prof_data(struct aw_device *aw_dev, int index, |
| 1054 |
struct aw_prof_desc **prof_desc) |
| 1055 |
{ |
| 1056 |
if ((index >= aw_dev->prof_info.count) || (index < 0)) { |
| 1057 |
dev_err(aw_dev->dev, "%s: index[%d] overflow count[%d]\n", |
| 1058 |
__func__, index, aw_dev->prof_info.count); |
| 1059 |
return -EINVAL; |
| 1060 |
} |
| 1061 |
|
| 1062 |
*prof_desc = &aw_dev->prof_info.prof_desc[index]; |
| 1063 |
|
| 1064 |
return 0; |
| 1065 |
} |
| 1066 |
|
| 1067 |
static int aw88166_dev_fw_update(struct aw88166 *aw88166, bool up_dsp_fw_en, bool force_up_en) |
| 1068 |
{ |
| 1069 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 1070 |
struct aw_prof_desc *prof_index_desc; |
| 1071 |
struct aw_sec_data_desc *sec_desc; |
| 1072 |
char *prof_name; |
| 1073 |
int ret; |
| 1074 |
|
| 1075 |
if ((aw_dev->prof_cur == aw_dev->prof_index) && |
| 1076 |
(force_up_en == AW88166_FORCE_UPDATE_OFF)) { |
| 1077 |
dev_dbg(aw_dev->dev, "scene no change, not update"); |
| 1078 |
return 0; |
| 1079 |
} |
| 1080 |
|
| 1081 |
if (aw_dev->fw_status == AW88166_DEV_FW_FAILED) { |
| 1082 |
dev_err(aw_dev->dev, "fw status[%d] error\n", aw_dev->fw_status); |
| 1083 |
return -EPERM; |
| 1084 |
} |
| 1085 |
|
| 1086 |
ret = aw88166_dev_get_prof_name(aw_dev, aw_dev->prof_index, &prof_name); |
| 1087 |
if (ret) |
| 1088 |
return ret; |
| 1089 |
|
| 1090 |
dev_dbg(aw_dev->dev, "start update %s", prof_name); |
| 1091 |
|
| 1092 |
ret = aw88166_dev_get_prof_data(aw_dev, aw_dev->prof_index, &prof_index_desc); |
| 1093 |
if (ret) |
| 1094 |
return ret; |
| 1095 |
|
| 1096 |
/* update reg */ |
| 1097 |
sec_desc = prof_index_desc->sec_desc; |
| 1098 |
ret = aw_dev_reg_update(aw88166, sec_desc[AW88395_DATA_TYPE_REG].data, |
| 1099 |
sec_desc[AW88395_DATA_TYPE_REG].len); |
| 1100 |
if (ret) { |
| 1101 |
dev_err(aw_dev->dev, "update reg failed\n"); |
| 1102 |
return ret; |
| 1103 |
} |
| 1104 |
|
| 1105 |
aw88166_dev_mute(aw_dev, true); |
| 1106 |
|
| 1107 |
if (aw_dev->dsp_cfg == AW88166_DEV_DSP_WORK) |
| 1108 |
aw_dev_dsp_enable(aw_dev, false); |
| 1109 |
|
| 1110 |
aw_dev_select_memclk(aw_dev, AW88166_DEV_MEMCLK_OSC); |
| 1111 |
|
| 1112 |
ret = aw_dev_check_sram(aw_dev); |
| 1113 |
if (ret) { |
| 1114 |
dev_err(aw_dev->dev, "check sram failed\n"); |
| 1115 |
goto error; |
| 1116 |
} |
| 1117 |
|
| 1118 |
aw_dev_backup_sec_recovery(aw88166); |
| 1119 |
|
| 1120 |
if (up_dsp_fw_en) { |
| 1121 |
dev_dbg(aw_dev->dev, "fw_ver: [%x]", prof_index_desc->fw_ver); |
| 1122 |
ret = aw_dev_dsp_update_fw(aw_dev, sec_desc[AW88395_DATA_TYPE_DSP_FW].data, |
| 1123 |
sec_desc[AW88395_DATA_TYPE_DSP_FW].len); |
| 1124 |
if (ret) { |
| 1125 |
dev_err(aw_dev->dev, "update dsp fw failed\n"); |
| 1126 |
goto error; |
| 1127 |
} |
| 1128 |
} |
| 1129 |
|
| 1130 |
/* update dsp config */ |
| 1131 |
ret = aw_dev_dsp_update_cfg(aw_dev, sec_desc[AW88395_DATA_TYPE_DSP_CFG].data, |
| 1132 |
sec_desc[AW88395_DATA_TYPE_DSP_CFG].len); |
| 1133 |
if (ret) { |
| 1134 |
dev_err(aw_dev->dev, "update dsp cfg failed\n"); |
| 1135 |
goto error; |
| 1136 |
} |
| 1137 |
|
| 1138 |
aw_dev_backup_sec_record(aw88166); |
| 1139 |
|
| 1140 |
aw_dev_select_memclk(aw_dev, AW88166_DEV_MEMCLK_PLL); |
| 1141 |
|
| 1142 |
aw_dev->prof_cur = aw_dev->prof_index; |
| 1143 |
|
| 1144 |
return 0; |
| 1145 |
|
| 1146 |
error: |
| 1147 |
aw_dev_select_memclk(aw_dev, AW88166_DEV_MEMCLK_PLL); |
| 1148 |
return ret; |
| 1149 |
} |
| 1150 |
|
| 1151 |
static void aw88166_start_pa(struct aw88166 *aw88166) |
| 1152 |
{ |
| 1153 |
int ret, i; |
| 1154 |
|
| 1155 |
for (i = 0; i < AW88166_START_RETRIES; i++) { |
| 1156 |
ret = aw88166_dev_start(aw88166); |
| 1157 |
if (ret) { |
| 1158 |
dev_err(aw88166->aw_pa->dev, "aw88166 device start failed. retry = %d", i); |
| 1159 |
ret = aw88166_dev_fw_update(aw88166, AW88166_DSP_FW_UPDATE_ON, true); |
| 1160 |
if (ret) { |
| 1161 |
dev_err(aw88166->aw_pa->dev, "fw update failed"); |
| 1162 |
continue; |
| 1163 |
} |
| 1164 |
} else { |
| 1165 |
dev_dbg(aw88166->aw_pa->dev, "start success\n"); |
| 1166 |
break; |
| 1167 |
} |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
static void aw88166_startup_work(struct work_struct *work) |
| 1172 |
{ |
| 1173 |
struct aw88166 *aw88166 = |
| 1174 |
container_of(work, struct aw88166, start_work.work); |
| 1175 |
|
| 1176 |
mutex_lock(&aw88166->lock); |
| 1177 |
aw88166_start_pa(aw88166); |
| 1178 |
mutex_unlock(&aw88166->lock); |
| 1179 |
} |
| 1180 |
|
| 1181 |
static void aw88166_start(struct aw88166 *aw88166, bool sync_start) |
| 1182 |
{ |
| 1183 |
int ret; |
| 1184 |
|
| 1185 |
if (aw88166->aw_pa->fw_status != AW88166_DEV_FW_OK) |
| 1186 |
return; |
| 1187 |
|
| 1188 |
if (aw88166->aw_pa->status == AW88166_DEV_PW_ON) |
| 1189 |
return; |
| 1190 |
|
| 1191 |
ret = aw88166_dev_fw_update(aw88166, AW88166_DSP_FW_UPDATE_OFF, aw88166->phase_sync); |
| 1192 |
if (ret) { |
| 1193 |
dev_err(aw88166->aw_pa->dev, "fw update failed\n"); |
| 1194 |
return; |
| 1195 |
} |
| 1196 |
|
| 1197 |
if (sync_start == AW88166_SYNC_START) |
| 1198 |
aw88166_start_pa(aw88166); |
| 1199 |
else |
| 1200 |
queue_delayed_work(system_dfl_wq, |
| 1201 |
&aw88166->start_work, |
| 1202 |
AW88166_START_WORK_DELAY_MS); |
| 1203 |
} |
| 1204 |
|
| 1205 |
static int aw_dev_check_sysint(struct aw_device *aw_dev) |
| 1206 |
{ |
| 1207 |
u16 reg_val; |
| 1208 |
|
| 1209 |
aw_dev_get_int_status(aw_dev, ®_val); |
| 1210 |
if (reg_val & AW88166_BIT_SYSINT_CHECK) { |
| 1211 |
dev_err(aw_dev->dev, "pa stop check fail:0x%04x\n", reg_val); |
| 1212 |
return -EINVAL; |
| 1213 |
} |
| 1214 |
|
| 1215 |
return 0; |
| 1216 |
} |
| 1217 |
|
| 1218 |
static int aw88166_stop(struct aw_device *aw_dev) |
| 1219 |
{ |
| 1220 |
struct aw_sec_data_desc *dsp_cfg = |
| 1221 |
&aw_dev->prof_info.prof_desc[aw_dev->prof_cur].sec_desc[AW88395_DATA_TYPE_DSP_CFG]; |
| 1222 |
struct aw_sec_data_desc *dsp_fw = |
| 1223 |
&aw_dev->prof_info.prof_desc[aw_dev->prof_cur].sec_desc[AW88395_DATA_TYPE_DSP_FW]; |
| 1224 |
int int_st; |
| 1225 |
|
| 1226 |
if (aw_dev->status == AW88166_DEV_PW_OFF) { |
| 1227 |
dev_dbg(aw_dev->dev, "already power off"); |
| 1228 |
return 0; |
| 1229 |
} |
| 1230 |
|
| 1231 |
aw_dev->status = AW88166_DEV_PW_OFF; |
| 1232 |
|
| 1233 |
aw88166_dev_mute(aw_dev, true); |
| 1234 |
usleep_range(AW88166_4000_US, AW88166_4000_US + 100); |
| 1235 |
|
| 1236 |
aw_dev_i2s_tx_enable(aw_dev, false); |
| 1237 |
usleep_range(AW88166_1000_US, AW88166_1000_US + 100); |
| 1238 |
|
| 1239 |
int_st = aw_dev_check_sysint(aw_dev); |
| 1240 |
|
| 1241 |
aw_dev_dsp_enable(aw_dev, false); |
| 1242 |
|
| 1243 |
aw_dev_amppd(aw_dev, true); |
| 1244 |
|
| 1245 |
if (int_st) { |
| 1246 |
aw_dev_select_memclk(aw_dev, AW88166_DEV_MEMCLK_OSC); |
| 1247 |
aw_dev_dsp_update_fw(aw_dev, dsp_fw->data, dsp_fw->len); |
| 1248 |
aw_dev_dsp_update_cfg(aw_dev, dsp_cfg->data, dsp_cfg->len); |
| 1249 |
aw_dev_select_memclk(aw_dev, AW88166_DEV_MEMCLK_PLL); |
| 1250 |
} |
| 1251 |
|
| 1252 |
aw_dev_pwd(aw_dev, true); |
| 1253 |
|
| 1254 |
return 0; |
| 1255 |
} |
| 1256 |
|
| 1257 |
static struct snd_soc_dai_driver aw88166_dai[] = { |
| 1258 |
{ |
| 1259 |
.name = "aw88166-aif", |
| 1260 |
.id = 1, |
| 1261 |
.playback = { |
| 1262 |
.stream_name = "Speaker_Playback", |
| 1263 |
.channels_min = 1, |
| 1264 |
.channels_max = 2, |
| 1265 |
.rates = AW88166_RATES, |
| 1266 |
.formats = AW88166_FORMATS, |
| 1267 |
}, |
| 1268 |
.capture = { |
| 1269 |
.stream_name = "Speaker_Capture", |
| 1270 |
.channels_min = 1, |
| 1271 |
.channels_max = 2, |
| 1272 |
.rates = AW88166_RATES, |
| 1273 |
.formats = AW88166_FORMATS, |
| 1274 |
}, |
| 1275 |
}, |
| 1276 |
}; |
| 1277 |
|
| 1278 |
static int aw88166_get_fade_in_time(struct snd_kcontrol *kcontrol, |
| 1279 |
struct snd_ctl_elem_value *ucontrol) |
| 1280 |
{ |
| 1281 |
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
| 1282 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(component); |
| 1283 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 1284 |
|
| 1285 |
ucontrol->value.integer.value[0] = aw_dev->fade_in_time; |
| 1286 |
|
| 1287 |
return 0; |
| 1288 |
} |
| 1289 |
|
| 1290 |
static int aw88166_set_fade_in_time(struct snd_kcontrol *kcontrol, |
| 1291 |
struct snd_ctl_elem_value *ucontrol) |
| 1292 |
{ |
| 1293 |
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
| 1294 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(component); |
| 1295 |
struct soc_mixer_control *mc = |
| 1296 |
(struct soc_mixer_control *)kcontrol->private_value; |
| 1297 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 1298 |
int time; |
| 1299 |
|
| 1300 |
time = ucontrol->value.integer.value[0]; |
| 1301 |
|
| 1302 |
if (time < mc->min || time > mc->max) |
| 1303 |
return -EINVAL; |
| 1304 |
|
| 1305 |
if (time != aw_dev->fade_in_time) { |
| 1306 |
aw_dev->fade_in_time = time; |
| 1307 |
return 1; |
| 1308 |
} |
| 1309 |
|
| 1310 |
return 0; |
| 1311 |
} |
| 1312 |
|
| 1313 |
static int aw88166_get_fade_out_time(struct snd_kcontrol *kcontrol, |
| 1314 |
struct snd_ctl_elem_value *ucontrol) |
| 1315 |
{ |
| 1316 |
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
| 1317 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(component); |
| 1318 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 1319 |
|
| 1320 |
ucontrol->value.integer.value[0] = aw_dev->fade_out_time; |
| 1321 |
|
| 1322 |
return 0; |
| 1323 |
} |
| 1324 |
|
| 1325 |
static int aw88166_set_fade_out_time(struct snd_kcontrol *kcontrol, |
| 1326 |
struct snd_ctl_elem_value *ucontrol) |
| 1327 |
{ |
| 1328 |
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol); |
| 1329 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(component); |
| 1330 |
struct soc_mixer_control *mc = |
| 1331 |
(struct soc_mixer_control *)kcontrol->private_value; |
| 1332 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 1333 |
int time; |
| 1334 |
|
| 1335 |
time = ucontrol->value.integer.value[0]; |
| 1336 |
if (time < mc->min || time > mc->max) |
| 1337 |
return -EINVAL; |
| 1338 |
|
| 1339 |
if (time != aw_dev->fade_out_time) { |
| 1340 |
aw_dev->fade_out_time = time; |
| 1341 |
return 1; |
| 1342 |
} |
| 1343 |
|
| 1344 |
return 0; |
| 1345 |
} |
| 1346 |
|
| 1347 |
static int aw88166_dev_set_profile_index(struct aw_device *aw_dev, int index) |
| 1348 |
{ |
| 1349 |
/* check the index whether is valid */ |
| 1350 |
if ((index >= aw_dev->prof_info.count) || (index < 0)) |
| 1351 |
return -EINVAL; |
| 1352 |
/* check the index whether change */ |
| 1353 |
if (aw_dev->prof_index == index) |
| 1354 |
return -EINVAL; |
| 1355 |
|
| 1356 |
aw_dev->prof_index = index; |
| 1357 |
dev_dbg(aw_dev->dev, "set prof[%s]", |
| 1358 |
aw_dev->prof_info.prof_name_list[aw_dev->prof_info.prof_desc[index].id]); |
| 1359 |
|
| 1360 |
return 0; |
| 1361 |
} |
| 1362 |
|
| 1363 |
static int aw88166_profile_info(struct snd_kcontrol *kcontrol, |
| 1364 |
struct snd_ctl_elem_info *uinfo) |
| 1365 |
{ |
| 1366 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1367 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec); |
| 1368 |
char *prof_name; |
| 1369 |
int count, ret; |
| 1370 |
|
| 1371 |
uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; |
| 1372 |
uinfo->count = 1; |
| 1373 |
|
| 1374 |
count = aw88166->aw_pa->prof_info.count; |
| 1375 |
if (count <= 0) { |
| 1376 |
uinfo->value.enumerated.items = 0; |
| 1377 |
return 0; |
| 1378 |
} |
| 1379 |
|
| 1380 |
uinfo->value.enumerated.items = count; |
| 1381 |
|
| 1382 |
if (uinfo->value.enumerated.item >= count) |
| 1383 |
uinfo->value.enumerated.item = count - 1; |
| 1384 |
|
| 1385 |
count = uinfo->value.enumerated.item; |
| 1386 |
|
| 1387 |
ret = aw88166_dev_get_prof_name(aw88166->aw_pa, count, &prof_name); |
| 1388 |
if (ret) { |
| 1389 |
strscpy(uinfo->value.enumerated.name, "null"); |
| 1390 |
return 0; |
| 1391 |
} |
| 1392 |
|
| 1393 |
strscpy(uinfo->value.enumerated.name, prof_name); |
| 1394 |
|
| 1395 |
return 0; |
| 1396 |
} |
| 1397 |
|
| 1398 |
static int aw88166_profile_get(struct snd_kcontrol *kcontrol, |
| 1399 |
struct snd_ctl_elem_value *ucontrol) |
| 1400 |
{ |
| 1401 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1402 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec); |
| 1403 |
|
| 1404 |
ucontrol->value.integer.value[0] = aw88166->aw_pa->prof_index; |
| 1405 |
|
| 1406 |
return 0; |
| 1407 |
} |
| 1408 |
|
| 1409 |
static int aw88166_profile_set(struct snd_kcontrol *kcontrol, |
| 1410 |
struct snd_ctl_elem_value *ucontrol) |
| 1411 |
{ |
| 1412 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1413 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec); |
| 1414 |
int ret; |
| 1415 |
|
| 1416 |
mutex_lock(&aw88166->lock); |
| 1417 |
ret = aw88166_dev_set_profile_index(aw88166->aw_pa, ucontrol->value.integer.value[0]); |
| 1418 |
if (ret) { |
| 1419 |
dev_dbg(codec->dev, "profile index does not change"); |
| 1420 |
mutex_unlock(&aw88166->lock); |
| 1421 |
return 0; |
| 1422 |
} |
| 1423 |
|
| 1424 |
if (aw88166->aw_pa->status) { |
| 1425 |
aw88166_stop(aw88166->aw_pa); |
| 1426 |
aw88166_start(aw88166, AW88166_SYNC_START); |
| 1427 |
} |
| 1428 |
|
| 1429 |
mutex_unlock(&aw88166->lock); |
| 1430 |
|
| 1431 |
return 1; |
| 1432 |
} |
| 1433 |
|
| 1434 |
static int aw88166_volume_get(struct snd_kcontrol *kcontrol, |
| 1435 |
struct snd_ctl_elem_value *ucontrol) |
| 1436 |
{ |
| 1437 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1438 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec); |
| 1439 |
struct aw_volume_desc *vol_desc = &aw88166->aw_pa->volume_desc; |
| 1440 |
|
| 1441 |
ucontrol->value.integer.value[0] = vol_desc->ctl_volume; |
| 1442 |
|
| 1443 |
return 0; |
| 1444 |
} |
| 1445 |
|
| 1446 |
static int aw88166_volume_set(struct snd_kcontrol *kcontrol, |
| 1447 |
struct snd_ctl_elem_value *ucontrol) |
| 1448 |
{ |
| 1449 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1450 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec); |
| 1451 |
struct aw_volume_desc *vol_desc = &aw88166->aw_pa->volume_desc; |
| 1452 |
struct soc_mixer_control *mc = |
| 1453 |
(struct soc_mixer_control *)kcontrol->private_value; |
| 1454 |
int value; |
| 1455 |
|
| 1456 |
value = ucontrol->value.integer.value[0]; |
| 1457 |
if (value < mc->min || value > mc->max) |
| 1458 |
return -EINVAL; |
| 1459 |
|
| 1460 |
if (vol_desc->ctl_volume != value) { |
| 1461 |
vol_desc->ctl_volume = value; |
| 1462 |
aw_dev_set_volume(aw88166->aw_pa, vol_desc->ctl_volume); |
| 1463 |
|
| 1464 |
return 1; |
| 1465 |
} |
| 1466 |
|
| 1467 |
return 0; |
| 1468 |
} |
| 1469 |
|
| 1470 |
static int aw88166_get_fade_step(struct snd_kcontrol *kcontrol, |
| 1471 |
struct snd_ctl_elem_value *ucontrol) |
| 1472 |
{ |
| 1473 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1474 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec); |
| 1475 |
|
| 1476 |
ucontrol->value.integer.value[0] = aw88166->aw_pa->fade_step; |
| 1477 |
|
| 1478 |
return 0; |
| 1479 |
} |
| 1480 |
|
| 1481 |
static int aw88166_set_fade_step(struct snd_kcontrol *kcontrol, |
| 1482 |
struct snd_ctl_elem_value *ucontrol) |
| 1483 |
{ |
| 1484 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1485 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec); |
| 1486 |
struct soc_mixer_control *mc = |
| 1487 |
(struct soc_mixer_control *)kcontrol->private_value; |
| 1488 |
int value; |
| 1489 |
|
| 1490 |
value = ucontrol->value.integer.value[0]; |
| 1491 |
if (value < mc->min || value > mc->max) |
| 1492 |
return -EINVAL; |
| 1493 |
|
| 1494 |
if (aw88166->aw_pa->fade_step != value) { |
| 1495 |
aw88166->aw_pa->fade_step = value; |
| 1496 |
return 1; |
| 1497 |
} |
| 1498 |
|
| 1499 |
return 0; |
| 1500 |
} |
| 1501 |
|
| 1502 |
static int aw88166_re_get(struct snd_kcontrol *kcontrol, |
| 1503 |
struct snd_ctl_elem_value *ucontrol) |
| 1504 |
{ |
| 1505 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1506 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec); |
| 1507 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 1508 |
|
| 1509 |
ucontrol->value.integer.value[0] = aw_dev->cali_desc.cali_re; |
| 1510 |
|
| 1511 |
return 0; |
| 1512 |
} |
| 1513 |
|
| 1514 |
static int aw88166_re_set(struct snd_kcontrol *kcontrol, |
| 1515 |
struct snd_ctl_elem_value *ucontrol) |
| 1516 |
{ |
| 1517 |
struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol); |
| 1518 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(codec); |
| 1519 |
struct soc_mixer_control *mc = |
| 1520 |
(struct soc_mixer_control *)kcontrol->private_value; |
| 1521 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 1522 |
int value; |
| 1523 |
|
| 1524 |
value = ucontrol->value.integer.value[0]; |
| 1525 |
if (value < mc->min || value > mc->max) |
| 1526 |
return -EINVAL; |
| 1527 |
|
| 1528 |
if (aw_dev->cali_desc.cali_re != value) { |
| 1529 |
aw_dev->cali_desc.cali_re = value; |
| 1530 |
return 1; |
| 1531 |
} |
| 1532 |
|
| 1533 |
return 0; |
| 1534 |
} |
| 1535 |
|
| 1536 |
static int aw88166_dev_init(struct aw88166 *aw88166, struct aw_container *aw_cfg) |
| 1537 |
{ |
| 1538 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 1539 |
int ret; |
| 1540 |
|
| 1541 |
ret = aw88395_dev_cfg_load(aw_dev, aw_cfg); |
| 1542 |
if (ret) { |
| 1543 |
dev_err(aw_dev->dev, "aw_dev acf parse failed\n"); |
| 1544 |
return -EINVAL; |
| 1545 |
} |
| 1546 |
aw_dev->fade_in_time = AW88166_1000_US / 10; |
| 1547 |
aw_dev->fade_out_time = AW88166_1000_US >> 1; |
| 1548 |
aw_dev->prof_cur = aw_dev->prof_info.prof_desc[0].id; |
| 1549 |
aw_dev->prof_index = aw_dev->prof_info.prof_desc[0].id; |
| 1550 |
|
| 1551 |
ret = aw88166_dev_fw_update(aw88166, AW88166_FORCE_UPDATE_ON, AW88166_DSP_FW_UPDATE_ON); |
| 1552 |
if (ret) { |
| 1553 |
dev_err(aw_dev->dev, "fw update failed ret = %d\n", ret); |
| 1554 |
return ret; |
| 1555 |
} |
| 1556 |
|
| 1557 |
aw88166_dev_mute(aw_dev, true); |
| 1558 |
|
| 1559 |
/* close tx feedback */ |
| 1560 |
aw_dev_i2s_tx_enable(aw_dev, false); |
| 1561 |
usleep_range(AW88166_1000_US, AW88166_1000_US + 100); |
| 1562 |
|
| 1563 |
/* enable amppd */ |
| 1564 |
aw_dev_amppd(aw_dev, true); |
| 1565 |
|
| 1566 |
/* close dsp */ |
| 1567 |
aw_dev_dsp_enable(aw_dev, false); |
| 1568 |
/* set power down */ |
| 1569 |
aw_dev_pwd(aw_dev, true); |
| 1570 |
|
| 1571 |
return 0; |
| 1572 |
} |
| 1573 |
|
| 1574 |
static int aw88166_request_firmware_file(struct aw88166 *aw88166) |
| 1575 |
{ |
| 1576 |
const struct firmware *cont = NULL; |
| 1577 |
int ret; |
| 1578 |
|
| 1579 |
aw88166->aw_pa->fw_status = AW88166_DEV_FW_FAILED; |
| 1580 |
|
| 1581 |
ret = request_firmware(&cont, AW88166_ACF_FILE, aw88166->aw_pa->dev); |
| 1582 |
if (ret) { |
| 1583 |
dev_err(aw88166->aw_pa->dev, "request [%s] failed!\n", AW88166_ACF_FILE); |
| 1584 |
return ret; |
| 1585 |
} |
| 1586 |
|
| 1587 |
dev_dbg(aw88166->aw_pa->dev, "loaded %s - size: %zu\n", |
| 1588 |
AW88166_ACF_FILE, cont ? cont->size : 0); |
| 1589 |
|
| 1590 |
aw88166->aw_cfg = devm_kzalloc(aw88166->aw_pa->dev, |
| 1591 |
struct_size(aw88166->aw_cfg, data, cont->size), GFP_KERNEL); |
| 1592 |
if (!aw88166->aw_cfg) { |
| 1593 |
release_firmware(cont); |
| 1594 |
return -ENOMEM; |
| 1595 |
} |
| 1596 |
aw88166->aw_cfg->len = (int)cont->size; |
| 1597 |
memcpy(aw88166->aw_cfg->data, cont->data, cont->size); |
| 1598 |
release_firmware(cont); |
| 1599 |
|
| 1600 |
ret = aw88395_dev_load_acf_check(aw88166->aw_pa, aw88166->aw_cfg); |
| 1601 |
if (ret) { |
| 1602 |
dev_err(aw88166->aw_pa->dev, "load [%s] failed!\n", AW88166_ACF_FILE); |
| 1603 |
return ret; |
| 1604 |
} |
| 1605 |
|
| 1606 |
mutex_lock(&aw88166->lock); |
| 1607 |
/* aw device init */ |
| 1608 |
ret = aw88166_dev_init(aw88166, aw88166->aw_cfg); |
| 1609 |
if (ret) |
| 1610 |
dev_err(aw88166->aw_pa->dev, "dev init failed\n"); |
| 1611 |
mutex_unlock(&aw88166->lock); |
| 1612 |
|
| 1613 |
return ret; |
| 1614 |
} |
| 1615 |
|
| 1616 |
static const struct snd_kcontrol_new aw88166_controls[] = { |
| 1617 |
SOC_SINGLE_EXT("PCM Playback Volume", AW88166_SYSCTRL2_REG, |
| 1618 |
6, AW88166_MUTE_VOL, 0, aw88166_volume_get, |
| 1619 |
aw88166_volume_set), |
| 1620 |
SOC_SINGLE_EXT("Fade Step", 0, 0, AW88166_MUTE_VOL, 0, |
| 1621 |
aw88166_get_fade_step, aw88166_set_fade_step), |
| 1622 |
SOC_SINGLE_EXT("Volume Ramp Up Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN, |
| 1623 |
aw88166_get_fade_in_time, aw88166_set_fade_in_time), |
| 1624 |
SOC_SINGLE_EXT("Volume Ramp Down Step", 0, 0, FADE_TIME_MAX, FADE_TIME_MIN, |
| 1625 |
aw88166_get_fade_out_time, aw88166_set_fade_out_time), |
| 1626 |
SOC_SINGLE_EXT("Calib", 0, 0, AW88166_CALI_RE_MAX, 0, |
| 1627 |
aw88166_re_get, aw88166_re_set), |
| 1628 |
AW88166_PROFILE_EXT("AW88166 Profile Set", aw88166_profile_info, |
| 1629 |
aw88166_profile_get, aw88166_profile_set), |
| 1630 |
}; |
| 1631 |
|
| 1632 |
static int aw88166_playback_event(struct snd_soc_dapm_widget *w, |
| 1633 |
struct snd_kcontrol *k, int event) |
| 1634 |
{ |
| 1635 |
struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm); |
| 1636 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(component); |
| 1637 |
|
| 1638 |
mutex_lock(&aw88166->lock); |
| 1639 |
switch (event) { |
| 1640 |
case SND_SOC_DAPM_PRE_PMU: |
| 1641 |
aw88166_start(aw88166, AW88166_ASYNC_START); |
| 1642 |
break; |
| 1643 |
case SND_SOC_DAPM_POST_PMD: |
| 1644 |
aw88166_stop(aw88166->aw_pa); |
| 1645 |
break; |
| 1646 |
default: |
| 1647 |
break; |
| 1648 |
} |
| 1649 |
mutex_unlock(&aw88166->lock); |
| 1650 |
|
| 1651 |
return 0; |
| 1652 |
} |
| 1653 |
|
| 1654 |
static const struct snd_soc_dapm_widget aw88166_dapm_widgets[] = { |
| 1655 |
/* playback */ |
| 1656 |
SND_SOC_DAPM_AIF_IN_E("AIF_RX", "Speaker_Playback", 0, SND_SOC_NOPM, 0, 0, |
| 1657 |
aw88166_playback_event, |
| 1658 |
SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD), |
| 1659 |
SND_SOC_DAPM_OUTPUT("DAC Output"), |
| 1660 |
|
| 1661 |
/* capture */ |
| 1662 |
SND_SOC_DAPM_AIF_OUT("AIF_TX", "Speaker_Capture", 0, SND_SOC_NOPM, 0, 0), |
| 1663 |
SND_SOC_DAPM_INPUT("ADC Input"), |
| 1664 |
}; |
| 1665 |
|
| 1666 |
static const struct snd_soc_dapm_route aw88166_audio_map[] = { |
| 1667 |
{"DAC Output", NULL, "AIF_RX"}, |
| 1668 |
{"AIF_TX", NULL, "ADC Input"}, |
| 1669 |
}; |
| 1670 |
|
| 1671 |
static int aw88166_codec_probe(struct snd_soc_component *component) |
| 1672 |
{ |
| 1673 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(component); |
| 1674 |
int ret; |
| 1675 |
|
| 1676 |
INIT_DELAYED_WORK(&aw88166->start_work, aw88166_startup_work); |
| 1677 |
|
| 1678 |
ret = aw88166_request_firmware_file(aw88166); |
| 1679 |
if (ret) |
| 1680 |
dev_err(aw88166->aw_pa->dev, "%s failed\n", __func__); |
| 1681 |
|
| 1682 |
return ret; |
| 1683 |
} |
| 1684 |
|
| 1685 |
static void aw88166_codec_remove(struct snd_soc_component *aw_codec) |
| 1686 |
{ |
| 1687 |
struct aw88166 *aw88166 = snd_soc_component_get_drvdata(aw_codec); |
| 1688 |
|
| 1689 |
cancel_delayed_work_sync(&aw88166->start_work); |
| 1690 |
} |
| 1691 |
|
| 1692 |
static const struct snd_soc_component_driver soc_codec_dev_aw88166 = { |
| 1693 |
.probe = aw88166_codec_probe, |
| 1694 |
.remove = aw88166_codec_remove, |
| 1695 |
.dapm_widgets = aw88166_dapm_widgets, |
| 1696 |
.num_dapm_widgets = ARRAY_SIZE(aw88166_dapm_widgets), |
| 1697 |
.dapm_routes = aw88166_audio_map, |
| 1698 |
.num_dapm_routes = ARRAY_SIZE(aw88166_audio_map), |
| 1699 |
.controls = aw88166_controls, |
| 1700 |
.num_controls = ARRAY_SIZE(aw88166_controls), |
| 1701 |
}; |
| 1702 |
|
| 1703 |
static void aw88166_hw_reset(struct aw88166 *aw88166) |
| 1704 |
{ |
| 1705 |
if (aw88166->reset_gpio) { |
| 1706 |
gpiod_set_value_cansleep(aw88166->reset_gpio, 1); |
| 1707 |
usleep_range(AW88166_1000_US, AW88166_1000_US + 10); |
| 1708 |
gpiod_set_value_cansleep(aw88166->reset_gpio, 0); |
| 1709 |
usleep_range(AW88166_1000_US, AW88166_1000_US + 10); |
| 1710 |
} |
| 1711 |
} |
| 1712 |
|
| 1713 |
static void aw88166_parse_channel_dt(struct aw88166 *aw88166) |
| 1714 |
{ |
| 1715 |
struct aw_device *aw_dev = aw88166->aw_pa; |
| 1716 |
struct device_node *np = aw_dev->dev->of_node; |
| 1717 |
u32 channel_value; |
| 1718 |
|
| 1719 |
of_property_read_u32(np, "awinic,audio-channel", &channel_value); |
| 1720 |
aw_dev->channel = channel_value; |
| 1721 |
aw88166->phase_sync = of_property_read_bool(np, "awinic,sync-flag"); |
| 1722 |
} |
| 1723 |
|
| 1724 |
static int aw88166_init(struct aw88166 *aw88166, struct i2c_client *i2c, struct regmap *regmap) |
| 1725 |
{ |
| 1726 |
struct aw_device *aw_dev; |
| 1727 |
unsigned int chip_id; |
| 1728 |
int ret; |
| 1729 |
|
| 1730 |
ret = regmap_read(regmap, AW88166_ID_REG, &chip_id); |
| 1731 |
if (ret) { |
| 1732 |
dev_err(&i2c->dev, "%s read chipid error. ret = %d\n", __func__, ret); |
| 1733 |
return ret; |
| 1734 |
} |
| 1735 |
|
| 1736 |
aw_dev = devm_kzalloc(&i2c->dev, sizeof(*aw_dev), GFP_KERNEL); |
| 1737 |
if (!aw_dev) |
| 1738 |
return -ENOMEM; |
| 1739 |
aw88166->aw_pa = aw_dev; |
| 1740 |
|
| 1741 |
aw_dev->i2c = i2c; |
| 1742 |
aw_dev->dev = &i2c->dev; |
| 1743 |
aw_dev->regmap = regmap; |
| 1744 |
mutex_init(&aw_dev->dsp_lock); |
| 1745 |
|
| 1746 |
aw_dev->chip_id = chip_id; |
| 1747 |
aw_dev->acf = NULL; |
| 1748 |
aw_dev->prof_info.prof_desc = NULL; |
| 1749 |
aw_dev->prof_info.count = 0; |
| 1750 |
aw_dev->prof_info.prof_type = AW88395_DEV_NONE_TYPE_ID; |
| 1751 |
aw_dev->channel = AW88166_DEV_DEFAULT_CH; |
| 1752 |
aw_dev->fw_status = AW88166_DEV_FW_FAILED; |
| 1753 |
|
| 1754 |
aw_dev->fade_step = AW88166_VOLUME_STEP_DB; |
| 1755 |
aw_dev->volume_desc.ctl_volume = AW88166_VOL_DEFAULT_VALUE; |
| 1756 |
|
| 1757 |
aw88166_parse_channel_dt(aw88166); |
| 1758 |
|
| 1759 |
return 0; |
| 1760 |
} |
| 1761 |
|
| 1762 |
static int aw88166_i2c_probe(struct i2c_client *i2c) |
| 1763 |
{ |
| 1764 |
struct aw88166 *aw88166; |
| 1765 |
int ret; |
| 1766 |
|
| 1767 |
if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_I2C)) |
| 1768 |
return dev_err_probe(&i2c->dev, -ENXIO, "check_functionality failed\n"); |
| 1769 |
|
| 1770 |
aw88166 = devm_kzalloc(&i2c->dev, sizeof(*aw88166), GFP_KERNEL); |
| 1771 |
if (!aw88166) |
| 1772 |
return -ENOMEM; |
| 1773 |
|
| 1774 |
mutex_init(&aw88166->lock); |
| 1775 |
|
| 1776 |
i2c_set_clientdata(i2c, aw88166); |
| 1777 |
|
| 1778 |
aw88166->reset_gpio = devm_gpiod_get_optional(&i2c->dev, "reset", GPIOD_OUT_LOW); |
| 1779 |
if (IS_ERR(aw88166->reset_gpio)) |
| 1780 |
return dev_err_probe(&i2c->dev, PTR_ERR(aw88166->reset_gpio), |
| 1781 |
"reset gpio not defined\n"); |
| 1782 |
aw88166_hw_reset(aw88166); |
| 1783 |
|
| 1784 |
aw88166->regmap = devm_regmap_init_i2c(i2c, &aw88166_remap_config); |
| 1785 |
if (IS_ERR(aw88166->regmap)) |
| 1786 |
return dev_err_probe(&i2c->dev, PTR_ERR(aw88166->regmap), |
| 1787 |
"failed to init regmap\n"); |
| 1788 |
|
| 1789 |
/* aw pa init */ |
| 1790 |
ret = aw88166_init(aw88166, i2c, aw88166->regmap); |
| 1791 |
if (ret) |
| 1792 |
return ret; |
| 1793 |
|
| 1794 |
return devm_snd_soc_register_component(&i2c->dev, |
| 1795 |
&soc_codec_dev_aw88166, |
| 1796 |
aw88166_dai, ARRAY_SIZE(aw88166_dai)); |
| 1797 |
} |
| 1798 |
|
| 1799 |
static const struct i2c_device_id aw88166_i2c_id[] = { |
| 1800 |
{ AW88166_I2C_NAME }, |
| 1801 |
{ } |
| 1802 |
}; |
| 1803 |
MODULE_DEVICE_TABLE(i2c, aw88166_i2c_id); |
| 1804 |
|
| 1805 |
static struct i2c_driver aw88166_i2c_driver = { |
| 1806 |
.driver = { |
| 1807 |
.name = AW88166_I2C_NAME, |
| 1808 |
}, |
| 1809 |
.probe = aw88166_i2c_probe, |
| 1810 |
.id_table = aw88166_i2c_id, |
| 1811 |
}; |
| 1812 |
module_i2c_driver(aw88166_i2c_driver); |
| 1813 |
|
| 1814 |
MODULE_DESCRIPTION("ASoC AW88166 Smart PA Driver"); |
| 1815 |
MODULE_LICENSE("GPL v2"); |