1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // ALSA SoC Texas Instruments TAS2563/TAS2781 Audio Smart Amplifier 4 // 5 // Copyright (C) 2022 - 2025 Texas Instruments Incorporated 6 // https://www.ti.com 7 // 8 // The TAS2563/TAS2781 driver implements a flexible and configurable 9 // algo coefficient setting for one, two, or even multiple 10 // TAS2563/TAS2781 chips. 11 // 12 // Author: Shenghao Ding <shenghao-ding@ti.com> 13 // Author: Kevin Lu <kevin-lu@ti.com> 14 // 15 16 #include <linux/crc8.h> 17 #include <linux/firmware.h> 18 #include <linux/gpio/consumer.h> 19 #include <linux/i2c.h> 20 #include <linux/init.h> 21 #include <linux/interrupt.h> 22 #include <linux/module.h> 23 #include <linux/of.h> 24 #include <linux/of_address.h> 25 #include <linux/of_irq.h> 26 #include <linux/regmap.h> 27 #include <linux/slab.h> 28 #include <sound/pcm_params.h> 29 #include <sound/soc.h> 30 #include <sound/tas2781.h> 31 #include <sound/tas2781-comlib-i2c.h> 32 #include <sound/tlv.h> 33 #include <sound/tas2x20-tlv.h> 34 #include <sound/tas2563-tlv.h> 35 #include <sound/tas2781-tlv.h> 36 #include <sound/tas5825-tlv.h> 37 #include <linux/unaligned.h> 38 39 #define X2563_CL_STT_VAL(xreg, xval) \ 40 { .reg = xreg, \ 41 .val = { xval }, \ 42 .val_len = 1, } 43 44 #define X2563_CL_STT_4BYTS(xreg, byte0, byte1, byte2, byte3) \ 45 { .reg = xreg, \ 46 .val = { byte0, byte1, byte2, byte3 }, \ 47 .val_len = 4, } 48 49 static const struct bulk_reg_val tas2563_cali_start_reg[] = { 50 X2563_CL_STT_VAL(TAS2563_IDLE, 0x00), 51 X2563_CL_STT_4BYTS(TAS2563_PRM_ENFF_REG, 0x40, 0x00, 0x00, 0x00), 52 X2563_CL_STT_4BYTS(TAS2563_PRM_DISTCK_REG, 0x40, 0x00, 0x00, 0x00), 53 X2563_CL_STT_4BYTS(TAS2563_PRM_TE_SCTHR_REG, 0x7f, 0xff, 0xff, 0xff), 54 X2563_CL_STT_4BYTS(TAS2563_PRM_PLT_FLAG_REG, 0x40, 0x00, 0x00, 0x00), 55 X2563_CL_STT_4BYTS(TAS2563_PRM_SINEGAIN_REG, 0x0a, 0x3d, 0x70, 0xa4), 56 X2563_CL_STT_4BYTS(TAS2563_TE_TA1_REG, 0x00, 0x36, 0x91, 0x5e), 57 X2563_CL_STT_4BYTS(TAS2563_TE_TA1_AT_REG, 0x00, 0x36, 0x91, 0x5e), 58 X2563_CL_STT_4BYTS(TAS2563_TE_TA2_REG, 0x00, 0x06, 0xd3, 0x72), 59 X2563_CL_STT_4BYTS(TAS2563_TE_AT_REG, 0x00, 0x36, 0x91, 0x5e), 60 X2563_CL_STT_4BYTS(TAS2563_TE_DT_REG, 0x00, 0x36, 0x91, 0x5e), 61 }; 62 63 #define X2781_CL_STT_VAL(xreg, xval, xlocked) \ 64 { .reg = xreg, \ 65 .val = { xval }, \ 66 .val_len = 1, \ 67 .is_locked = xlocked, } 68 69 #define X2781_CL_STT_4BYTS_UNLOCKED(xreg, byte0, byte1, byte2, byte3) \ 70 { .reg = xreg, \ 71 .val = { byte0, byte1, byte2, byte3 }, \ 72 .val_len = 4, \ 73 .is_locked = false, } 74 75 #define X2781_CL_STT_LEN_UNLOCKED(xreg) \ 76 { .reg = xreg, \ 77 .val_len = 4, \ 78 .is_locked = false, } 79 80 static const struct bulk_reg_val tas2781_cali_start_reg[] = { 81 X2781_CL_STT_VAL(TAS2781_PRM_INT_MASK_REG, 0xfe, false), 82 X2781_CL_STT_VAL(TAS2781_PRM_CLK_CFG_REG, 0xdd, false), 83 X2781_CL_STT_VAL(TAS2781_PRM_RSVD_REG, 0x20, false), 84 X2781_CL_STT_VAL(TAS2781_PRM_TEST_57_REG, 0x14, true), 85 X2781_CL_STT_VAL(TAS2781_PRM_TEST_62_REG, 0x45, true), 86 X2781_CL_STT_VAL(TAS2781_PRM_PVDD_UVLO_REG, 0x03, false), 87 X2781_CL_STT_VAL(TAS2781_PRM_CHNL_0_REG, 0xa8, false), 88 X2781_CL_STT_VAL(TAS2781_PRM_NG_CFG0_REG, 0xb9, false), 89 X2781_CL_STT_VAL(TAS2781_PRM_IDLE_CH_DET_REG, 0x92, false), 90 /* 91 * This register is pilot tone threshold, different with the 92 * calibration tool version, it will be updated in 93 * tas2781_calib_start_put(), set to 1mA. 94 */ 95 X2781_CL_STT_4BYTS_UNLOCKED(0, 0x00, 0x00, 0x00, 0x56), 96 X2781_CL_STT_4BYTS_UNLOCKED(TAS2781_PRM_PLT_FLAG_REG, 97 0x40, 0x00, 0x00, 0x00), 98 X2781_CL_STT_LEN_UNLOCKED(TAS2781_PRM_SINEGAIN_REG), 99 X2781_CL_STT_LEN_UNLOCKED(TAS2781_PRM_SINEGAIN2_REG), 100 }; 101 102 static const struct i2c_device_id tasdevice_id[] = { 103 { "tas2020", TAS2020 }, 104 { "tas2118", TAS2118 }, 105 { "tas2120", TAS2120 }, 106 { "tas2320", TAS2320 }, 107 { "tas2563", TAS2563 }, 108 { "tas2570", TAS2570 }, 109 { "tas2572", TAS2572 }, 110 { "tas2781", TAS2781 }, 111 { "tas5802", TAS5802 }, 112 { "tas5815", TAS5815 }, 113 { "tas5825", TAS5825 }, 114 { "tas5827", TAS5827 }, 115 { "tas5828", TAS5828 }, 116 {} 117 }; 118 MODULE_DEVICE_TABLE(i2c, tasdevice_id); 119 120 #ifdef CONFIG_OF 121 static const struct of_device_id tasdevice_of_match[] = { 122 { .compatible = "ti,tas2020" }, 123 { .compatible = "ti,tas2118" }, 124 { .compatible = "ti,tas2120" }, 125 { .compatible = "ti,tas2320" }, 126 { .compatible = "ti,tas2563" }, 127 { .compatible = "ti,tas2570" }, 128 { .compatible = "ti,tas2572" }, 129 { .compatible = "ti,tas2781" }, 130 { .compatible = "ti,tas5802" }, 131 { .compatible = "ti,tas5815" }, 132 { .compatible = "ti,tas5825" }, 133 { .compatible = "ti,tas5827" }, 134 { .compatible = "ti,tas5828" }, 135 {}, 136 }; 137 MODULE_DEVICE_TABLE(of, tasdevice_of_match); 138 #endif 139 140 /** 141 * tas2781_digital_getvol - get the volum control 142 * @kcontrol: control pointer 143 * @ucontrol: User data 144 * Customer Kcontrol for tas2781 is primarily for regmap booking, paging 145 * depends on internal regmap mechanism. 146 * tas2781 contains book and page two-level register map, especially 147 * book switching will set the register BXXP00R7F, after switching to the 148 * correct book, then leverage the mechanism for paging to access the 149 * register. 150 */ 151 static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol, 152 struct snd_ctl_elem_value *ucontrol) 153 { 154 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 155 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 156 struct soc_mixer_control *mc = 157 (struct soc_mixer_control *)kcontrol->private_value; 158 159 return tasdevice_digital_getvol(tas_priv, ucontrol, mc); 160 } 161 162 static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol, 163 struct snd_ctl_elem_value *ucontrol) 164 { 165 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 166 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 167 struct soc_mixer_control *mc = 168 (struct soc_mixer_control *)kcontrol->private_value; 169 170 return tasdevice_digital_putvol(tas_priv, ucontrol, mc); 171 } 172 173 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol, 174 struct snd_ctl_elem_value *ucontrol) 175 { 176 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 177 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 178 struct soc_mixer_control *mc = 179 (struct soc_mixer_control *)kcontrol->private_value; 180 181 return tasdevice_amp_getvol(tas_priv, ucontrol, mc); 182 } 183 184 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol, 185 struct snd_ctl_elem_value *ucontrol) 186 { 187 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 188 struct tasdevice_priv *tas_priv = 189 snd_soc_component_get_drvdata(codec); 190 struct soc_mixer_control *mc = 191 (struct soc_mixer_control *)kcontrol->private_value; 192 193 return tasdevice_amp_putvol(tas_priv, ucontrol, mc); 194 } 195 196 static int tasdev_force_fwload_get(struct snd_kcontrol *kcontrol, 197 struct snd_ctl_elem_value *ucontrol) 198 { 199 struct snd_soc_component *component = 200 snd_soc_kcontrol_component(kcontrol); 201 struct tasdevice_priv *tas_priv = 202 snd_soc_component_get_drvdata(component); 203 204 ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status; 205 dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__, 206 tas_priv->force_fwload_status ? "ON" : "OFF"); 207 208 return 0; 209 } 210 211 static int tasdev_force_fwload_put(struct snd_kcontrol *kcontrol, 212 struct snd_ctl_elem_value *ucontrol) 213 { 214 struct snd_soc_component *component = 215 snd_soc_kcontrol_component(kcontrol); 216 struct tasdevice_priv *tas_priv = 217 snd_soc_component_get_drvdata(component); 218 bool change, val = (bool)ucontrol->value.integer.value[0]; 219 220 if (tas_priv->force_fwload_status == val) 221 change = false; 222 else { 223 change = true; 224 tas_priv->force_fwload_status = val; 225 } 226 dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__, 227 tas_priv->force_fwload_status ? "ON" : "OFF"); 228 229 return change; 230 } 231 232 static int tasdev_cali_data_get(struct snd_kcontrol *kcontrol, 233 struct snd_ctl_elem_value *ucontrol) 234 { 235 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 236 struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp); 237 struct soc_bytes_ext *bytes_ext = 238 (struct soc_bytes_ext *) kcontrol->private_value; 239 struct calidata *cali_data = &priv->cali_data; 240 struct cali_reg *p = &cali_data->cali_reg_array; 241 unsigned char *dst = ucontrol->value.bytes.data; 242 unsigned char *data = cali_data->data; 243 unsigned int i = 0; 244 unsigned int j, k; 245 int rc; 246 247 guard(mutex)(&priv->codec_lock); 248 if (!priv->is_user_space_calidata) 249 return -1; 250 251 if (!p->r0_reg) 252 return -1; 253 254 dst[i++] = bytes_ext->max; 255 dst[i++] = 'r'; 256 257 dst[i++] = TASDEVICE_BOOK_ID(p->r0_reg); 258 dst[i++] = TASDEVICE_PAGE_ID(p->r0_reg); 259 dst[i++] = TASDEVICE_PAGE_REG(p->r0_reg); 260 261 dst[i++] = TASDEVICE_BOOK_ID(p->r0_low_reg); 262 dst[i++] = TASDEVICE_PAGE_ID(p->r0_low_reg); 263 dst[i++] = TASDEVICE_PAGE_REG(p->r0_low_reg); 264 265 dst[i++] = TASDEVICE_BOOK_ID(p->invr0_reg); 266 dst[i++] = TASDEVICE_PAGE_ID(p->invr0_reg); 267 dst[i++] = TASDEVICE_PAGE_REG(p->invr0_reg); 268 269 dst[i++] = TASDEVICE_BOOK_ID(p->pow_reg); 270 dst[i++] = TASDEVICE_PAGE_ID(p->pow_reg); 271 dst[i++] = TASDEVICE_PAGE_REG(p->pow_reg); 272 273 dst[i++] = TASDEVICE_BOOK_ID(p->tlimit_reg); 274 dst[i++] = TASDEVICE_PAGE_ID(p->tlimit_reg); 275 dst[i++] = TASDEVICE_PAGE_REG(p->tlimit_reg); 276 277 for (j = 0, k = 0; j < priv->ndev; j++) { 278 if (j == data[k]) { 279 dst[i++] = j; 280 k++; 281 } else { 282 dev_err(priv->dev, "chn %d device %u not match\n", 283 j, data[k]); 284 k += 21; 285 continue; 286 } 287 rc = tasdevice_dev_bulk_read(priv, j, p->r0_reg, &dst[i], 4); 288 if (rc < 0) { 289 dev_err(priv->dev, "chn %d r0_reg bulk_rd err = %d\n", 290 j, rc); 291 i += 20; 292 k += 20; 293 continue; 294 } 295 rc = memcmp(&dst[i], &data[k], 4); 296 if (rc != 0) 297 dev_dbg(priv->dev, "chn %d r0_data is not same\n", j); 298 k += 4; 299 i += 4; 300 rc = tasdevice_dev_bulk_read(priv, j, p->r0_low_reg, 301 &dst[i], 4); 302 if (rc < 0) { 303 dev_err(priv->dev, "chn %d r0_low bulk_rd err = %d\n", 304 j, rc); 305 i += 16; 306 k += 16; 307 continue; 308 } 309 rc = memcmp(&dst[i], &data[k], 4); 310 if (rc != 0) 311 dev_dbg(priv->dev, "chn %d r0_low is not same\n", j); 312 i += 4; 313 k += 4; 314 rc = tasdevice_dev_bulk_read(priv, j, p->invr0_reg, 315 &dst[i], 4); 316 if (rc < 0) { 317 dev_err(priv->dev, "chn %d invr0 bulk_rd err = %d\n", 318 j, rc); 319 i += 12; 320 k += 12; 321 continue; 322 } 323 rc = memcmp(&dst[i], &data[k], 4); 324 if (rc != 0) 325 dev_dbg(priv->dev, "chn %d invr0 is not same\n", j); 326 i += 4; 327 k += 4; 328 rc = tasdevice_dev_bulk_read(priv, j, p->pow_reg, &dst[i], 4); 329 if (rc < 0) { 330 dev_err(priv->dev, "chn %d pow_reg bulk_rd err = %d\n", 331 j, rc); 332 i += 8; 333 k += 8; 334 continue; 335 } 336 rc = memcmp(&dst[i], &data[k], 4); 337 if (rc != 0) 338 dev_dbg(priv->dev, "chn %d pow_reg is not same\n", j); 339 i += 4; 340 k += 4; 341 rc = tasdevice_dev_bulk_read(priv, j, p->tlimit_reg, 342 &dst[i], 4); 343 if (rc < 0) { 344 dev_err(priv->dev, "chn %d tlimit bulk_rd err = %d\n", 345 j, rc); 346 } 347 rc = memcmp(&dst[i], &data[k], 4); 348 if (rc != 0) 349 dev_dbg(priv->dev, "chn %d tlimit is not same\n", j); 350 i += 4; 351 k += 4; 352 } 353 return 0; 354 } 355 356 static int calib_data_get(struct tasdevice_priv *tas_priv, int reg, 357 unsigned char *dst) 358 { 359 struct i2c_client *clt = (struct i2c_client *)tas_priv->client; 360 struct tasdevice *tasdev = tas_priv->tasdevice; 361 int rc = -1; 362 int i; 363 364 for (i = 0; i < tas_priv->ndev; i++) { 365 if (clt->addr == tasdev[i].dev_addr) { 366 /* First byte is the device index. */ 367 dst[0] = i; 368 rc = tasdevice_dev_bulk_read(tas_priv, i, reg, &dst[1], 369 4); 370 break; 371 } 372 } 373 374 return rc; 375 } 376 377 static int partial_cali_data_update(int *reg, int j) 378 { 379 switch (tas2781_cali_start_reg[j].reg) { 380 case 0: 381 return reg[0]; 382 case TAS2781_PRM_PLT_FLAG_REG: 383 return reg[1]; 384 case TAS2781_PRM_SINEGAIN_REG: 385 return reg[2]; 386 case TAS2781_PRM_SINEGAIN2_REG: 387 return reg[3]; 388 default: 389 return 0; 390 } 391 } 392 393 static void sngl_calib_start(struct tasdevice_priv *tas_priv, int i, 394 int *reg, unsigned char *dat) 395 { 396 struct tasdevice *tasdev = tas_priv->tasdevice; 397 struct bulk_reg_val *p = tasdev[i].cali_data_backup; 398 struct bulk_reg_val *t = &tasdev[i].alp_cali_bckp; 399 const int sum = ARRAY_SIZE(tas2781_cali_start_reg); 400 unsigned char val[4]; 401 int j, r; 402 403 if (p == NULL) 404 return; 405 406 /* Store the current setting from the chip */ 407 for (j = 0; j < sum; j++) { 408 if (p[j].val_len == 1) { 409 if (p[j].is_locked) 410 tasdevice_dev_write(tas_priv, i, 411 TAS2781_TEST_UNLOCK_REG, 412 TAS2781_TEST_PAGE_UNLOCK); 413 tasdevice_dev_read(tas_priv, i, p[j].reg, 414 (int *)&p[j].val[0]); 415 } else { 416 if (!tas_priv->dspbin_typ) { 417 r = partial_cali_data_update(reg, j); 418 if (r) 419 p[j].reg = r; 420 } 421 422 if (p[j].reg) 423 tasdevice_dev_bulk_read(tas_priv, i, p[j].reg, 424 p[j].val, 4); 425 } 426 } 427 428 if (tas_priv->dspbin_typ == TASDEV_ALPHA) 429 tasdevice_dev_bulk_read(tas_priv, i, t->reg, t->val, 4); 430 431 /* Update the setting for calibration */ 432 for (j = 0; j < sum - 4; j++) { 433 if (p[j].val_len == 1) { 434 if (p[j].is_locked) 435 tasdevice_dev_write(tas_priv, i, 436 TAS2781_TEST_UNLOCK_REG, 437 TAS2781_TEST_PAGE_UNLOCK); 438 tasdevice_dev_write(tas_priv, i, p[j].reg, 439 tas2781_cali_start_reg[j].val[0]); 440 } 441 } 442 443 if (tas_priv->dspbin_typ == TASDEV_ALPHA) { 444 val[0] = 0x00; 445 val[1] = 0x00; 446 val[2] = 0x21; 447 val[3] = 0x8e; 448 } else { 449 val[0] = tas2781_cali_start_reg[j].val[0]; 450 val[1] = tas2781_cali_start_reg[j].val[1]; 451 val[2] = tas2781_cali_start_reg[j].val[2]; 452 val[3] = tas2781_cali_start_reg[j].val[3]; 453 } 454 tasdevice_dev_bulk_write(tas_priv, i, p[j].reg, val, 4); 455 tasdevice_dev_bulk_write(tas_priv, i, p[j + 1].reg, 456 (unsigned char *)tas2781_cali_start_reg[j + 1].val, 4); 457 tasdevice_dev_bulk_write(tas_priv, i, p[j + 2].reg, &dat[1], 4); 458 tasdevice_dev_bulk_write(tas_priv, i, p[j + 3].reg, &dat[5], 4); 459 if (tas_priv->dspbin_typ == TASDEV_ALPHA) { 460 val[0] = 0x00; 461 val[1] = 0x00; 462 val[2] = 0x2a; 463 val[3] = 0x0b; 464 465 tasdevice_dev_bulk_read(tas_priv, i, t->reg, val, 4); 466 } 467 } 468 469 static int tas2781_calib_start_put(struct snd_kcontrol *kcontrol, 470 struct snd_ctl_elem_value *ucontrol) 471 { 472 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 473 struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp); 474 struct soc_bytes_ext *bytes_ext = 475 (struct soc_bytes_ext *) kcontrol->private_value; 476 unsigned char *dat = ucontrol->value.bytes.data; 477 int i, reg[4]; 478 int j = 0; 479 480 guard(mutex)(&priv->codec_lock); 481 if (priv->chip_id != TAS2781 || bytes_ext->max != dat[0] || 482 dat[1] != 'r') { 483 dev_err(priv->dev, "%s: package fmt or chipid incorrect\n", 484 __func__); 485 return 0; 486 } 487 j += 2; 488 /* refresh pilot tone and SineGain register */ 489 for (i = 0; i < ARRAY_SIZE(reg); i++) { 490 reg[i] = TASDEVICE_REG(dat[j], dat[j + 1], dat[j + 2]); 491 j += 3; 492 } 493 494 for (i = 0; i < priv->ndev; i++) { 495 int k = i * 9 + j; 496 497 if (dat[k] != i) { 498 dev_err(priv->dev, "%s:no cal-setting for dev %d\n", 499 __func__, i); 500 continue; 501 } 502 sngl_calib_start(priv, i, reg, dat + k); 503 } 504 return 1; 505 } 506 507 static void tas2781_calib_stop_put(struct tasdevice_priv *priv) 508 { 509 const int sum = ARRAY_SIZE(tas2781_cali_start_reg); 510 int i, j; 511 512 for (i = 0; i < priv->ndev; i++) { 513 struct tasdevice *tasdev = priv->tasdevice; 514 struct bulk_reg_val *p = tasdev[i].cali_data_backup; 515 struct bulk_reg_val *t = &tasdev[i].alp_cali_bckp; 516 517 if (p == NULL) 518 continue; 519 520 for (j = 0; j < sum; j++) { 521 if (p[j].val_len == 1) { 522 if (p[j].is_locked) 523 tasdevice_dev_write(priv, i, 524 TAS2781_TEST_UNLOCK_REG, 525 TAS2781_TEST_PAGE_UNLOCK); 526 tasdevice_dev_write(priv, i, p[j].reg, 527 p[j].val[0]); 528 } else { 529 if (!p[j].reg) 530 continue; 531 tasdevice_dev_bulk_write(priv, i, p[j].reg, 532 p[j].val, 4); 533 } 534 } 535 536 if (priv->dspbin_typ == TASDEV_ALPHA) 537 tasdevice_dev_bulk_write(priv, i, t->reg, t->val, 4); 538 } 539 } 540 541 static int tas2563_calib_start_put(struct snd_kcontrol *kcontrol, 542 struct snd_ctl_elem_value *ucontrol) 543 { 544 struct bulk_reg_val *q = (struct bulk_reg_val *)tas2563_cali_start_reg; 545 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 546 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp); 547 const int sum = ARRAY_SIZE(tas2563_cali_start_reg); 548 int i, j; 549 550 guard(mutex)(&tas_priv->codec_lock); 551 if (tas_priv->chip_id != TAS2563) 552 return -1; 553 554 for (i = 0; i < tas_priv->ndev; i++) { 555 struct tasdevice *tasdev = tas_priv->tasdevice; 556 struct bulk_reg_val *p = tasdev[i].cali_data_backup; 557 558 if (p == NULL) 559 continue; 560 for (j = 0; j < sum; j++) { 561 if (p[j].val_len == 1) 562 tasdevice_dev_read(tas_priv, 563 i, p[j].reg, 564 (unsigned int *)&p[j].val[0]); 565 else 566 tasdevice_dev_bulk_read(tas_priv, 567 i, p[j].reg, p[j].val, 4); 568 } 569 570 for (j = 0; j < sum; j++) { 571 if (p[j].val_len == 1) 572 tasdevice_dev_write(tas_priv, i, p[j].reg, 573 q[j].val[0]); 574 else 575 tasdevice_dev_bulk_write(tas_priv, i, p[j].reg, 576 q[j].val, 4); 577 } 578 } 579 580 return 1; 581 } 582 583 static void tas2563_calib_stop_put(struct tasdevice_priv *tas_priv) 584 { 585 const int sum = ARRAY_SIZE(tas2563_cali_start_reg); 586 int i, j; 587 588 for (i = 0; i < tas_priv->ndev; i++) { 589 struct tasdevice *tasdev = tas_priv->tasdevice; 590 struct bulk_reg_val *p = tasdev[i].cali_data_backup; 591 592 if (p == NULL) 593 continue; 594 595 for (j = 0; j < sum; j++) { 596 if (p[j].val_len == 1) 597 tasdevice_dev_write(tas_priv, i, p[j].reg, 598 p[j].val[0]); 599 else 600 tasdevice_dev_bulk_write(tas_priv, i, p[j].reg, 601 p[j].val, 4); 602 } 603 } 604 } 605 606 static int tasdev_calib_stop_put(struct snd_kcontrol *kcontrol, 607 struct snd_ctl_elem_value *ucontrol) 608 { 609 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 610 struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp); 611 612 guard(mutex)(&priv->codec_lock); 613 if (priv->chip_id == TAS2563) 614 tas2563_calib_stop_put(priv); 615 else 616 tas2781_calib_stop_put(priv); 617 618 return 1; 619 } 620 621 static int tasdev_cali_data_put(struct snd_kcontrol *kcontrol, 622 struct snd_ctl_elem_value *ucontrol) 623 { 624 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 625 struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp); 626 struct soc_bytes_ext *bytes_ext = 627 (struct soc_bytes_ext *) kcontrol->private_value; 628 struct calidata *cali_data = &priv->cali_data; 629 struct cali_reg *p = &cali_data->cali_reg_array; 630 unsigned char *src = ucontrol->value.bytes.data; 631 unsigned char *dst = cali_data->data; 632 int i = 0; 633 int j; 634 635 guard(mutex)(&priv->codec_lock); 636 if (src[0] != bytes_ext->max || src[1] != 'r') { 637 dev_err(priv->dev, "%s: pkg fmt invalid\n", __func__); 638 return 0; 639 } 640 for (j = 0; j < priv->ndev; j++) { 641 if (src[17 + j * 21] != j) { 642 dev_err(priv->dev, "%s: pkg fmt invalid\n", __func__); 643 return 0; 644 } 645 } 646 i += 2; 647 priv->is_user_space_calidata = true; 648 649 if (priv->dspbin_typ == TASDEV_BASIC) { 650 p->r0_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]); 651 i += 3; 652 p->r0_low_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]); 653 i += 3; 654 p->invr0_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]); 655 i += 3; 656 p->pow_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]); 657 i += 3; 658 p->tlimit_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]); 659 i += 3; 660 } else { 661 i += 15; 662 } 663 664 memcpy(dst, &src[i], cali_data->total_sz); 665 return 1; 666 } 667 668 static int tas2781_latch_reg_get(struct snd_kcontrol *kcontrol, 669 struct snd_ctl_elem_value *ucontrol) 670 { 671 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 672 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp); 673 struct i2c_client *clt = (struct i2c_client *)tas_priv->client; 674 struct soc_bytes_ext *bytes_ext = 675 (struct soc_bytes_ext *) kcontrol->private_value; 676 struct tasdevice *tasdev = tas_priv->tasdevice; 677 unsigned char *dst = ucontrol->value.bytes.data; 678 int i, val, rc = -1; 679 680 dst[0] = bytes_ext->max; 681 guard(mutex)(&tas_priv->codec_lock); 682 for (i = 0; i < tas_priv->ndev; i++) { 683 if (clt->addr == tasdev[i].dev_addr) { 684 /* First byte is the device index. */ 685 dst[1] = i; 686 rc = tasdevice_dev_read(tas_priv, i, 687 TAS2781_RUNTIME_LATCH_RE_REG, &val); 688 if (rc < 0) 689 dev_err(tas_priv->dev, "%s, get value error\n", 690 __func__); 691 else 692 dst[2] = val; 693 694 break; 695 } 696 } 697 698 return rc; 699 } 700 701 static int tasdev_tf_data_get(struct snd_kcontrol *kcontrol, 702 struct snd_ctl_elem_value *ucontrol) 703 { 704 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 705 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp); 706 struct soc_bytes_ext *bytes_ext = 707 (struct soc_bytes_ext *) kcontrol->private_value; 708 unsigned char *dst = ucontrol->value.bytes.data; 709 unsigned int reg = TAS2781_RUNTIME_RE_REG_TF; 710 711 if (tas_priv->chip_id == TAS2781) { 712 struct tasdevice_fw *tas_fmw = tas_priv->fmw; 713 struct fct_param_address *p = &(tas_fmw->fct_par_addr); 714 715 reg = TAS2781_RUNTIME_RE_REG_TF; 716 if (tas_priv->dspbin_typ) 717 reg = TASDEVICE_REG(p->tf_reg[0], p->tf_reg[1], 718 p->tf_reg[2]); 719 } else { 720 reg = TAS2563_RUNTIME_RE_REG_TF; 721 } 722 723 guard(mutex)(&tas_priv->codec_lock); 724 dst[0] = bytes_ext->max; 725 return calib_data_get(tas_priv, reg, &dst[1]); 726 } 727 728 static int tasdev_re_data_get(struct snd_kcontrol *kcontrol, 729 struct snd_ctl_elem_value *ucontrol) 730 { 731 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 732 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp); 733 struct soc_bytes_ext *bytes_ext = 734 (struct soc_bytes_ext *) kcontrol->private_value; 735 unsigned char *dst = ucontrol->value.bytes.data; 736 unsigned int reg = TAS2781_RUNTIME_RE_REG; 737 738 if (tas_priv->chip_id == TAS2781) { 739 struct tasdevice_fw *tas_fmw = tas_priv->fmw; 740 struct fct_param_address *p = &(tas_fmw->fct_par_addr); 741 742 if (tas_priv->dspbin_typ) 743 reg = TASDEVICE_REG(p->r0_reg[0], p->r0_reg[1], 744 p->r0_reg[2]); 745 } else { 746 reg = TAS2563_RUNTIME_RE_REG; 747 } 748 749 guard(mutex)(&tas_priv->codec_lock); 750 dst[0] = bytes_ext->max; 751 return calib_data_get(tas_priv, reg, &dst[1]); 752 } 753 754 static int tasdev_r0_data_get(struct snd_kcontrol *kcontrol, 755 struct snd_ctl_elem_value *ucontrol) 756 { 757 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 758 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp); 759 struct calidata *cali_data = &tas_priv->cali_data; 760 struct soc_bytes_ext *bytes_ext = 761 (struct soc_bytes_ext *) kcontrol->private_value; 762 unsigned char *dst = ucontrol->value.bytes.data; 763 unsigned int reg; 764 765 guard(mutex)(&tas_priv->codec_lock); 766 767 if (tas_priv->chip_id == TAS2563) 768 reg = TAS2563_PRM_R0_REG; 769 else if (cali_data->cali_reg_array.r0_reg) 770 reg = cali_data->cali_reg_array.r0_reg; 771 else 772 return -1; 773 dst[0] = bytes_ext->max; 774 return calib_data_get(tas_priv, reg, &dst[1]); 775 } 776 777 static int tasdev_XMA1_data_get(struct snd_kcontrol *kcontrol, 778 struct snd_ctl_elem_value *ucontrol) 779 { 780 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 781 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp); 782 struct tasdevice_fw *tas_fmw = tas_priv->fmw; 783 struct fct_param_address *p = &(tas_fmw->fct_par_addr); 784 struct soc_bytes_ext *bytes_ext = 785 (struct soc_bytes_ext *) kcontrol->private_value; 786 unsigned char *dst = ucontrol->value.bytes.data; 787 unsigned int reg = TASDEVICE_XM_A1_REG; 788 789 if (tas_priv->dspbin_typ) 790 reg = TASDEVICE_REG(p->a1_reg[0], p->a1_reg[1], p->a1_reg[2]); 791 792 guard(mutex)(&tas_priv->codec_lock); 793 dst[0] = bytes_ext->max; 794 return calib_data_get(tas_priv, reg, &dst[1]); 795 } 796 797 static int tasdev_XMA2_data_get(struct snd_kcontrol *kcontrol, 798 struct snd_ctl_elem_value *ucontrol) 799 { 800 struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol); 801 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp); 802 struct tasdevice_fw *tas_fmw = tas_priv->fmw; 803 struct fct_param_address *p = &(tas_fmw->fct_par_addr); 804 struct soc_bytes_ext *bytes_ext = 805 (struct soc_bytes_ext *) kcontrol->private_value; 806 unsigned char *dst = ucontrol->value.bytes.data; 807 unsigned int reg = TASDEVICE_XM_A2_REG; 808 809 if (tas_priv->dspbin_typ) 810 reg = TASDEVICE_REG(p->a2_reg[0], p->a2_reg[1], p->a2_reg[2]); 811 812 guard(mutex)(&tas_priv->codec_lock); 813 dst[0] = bytes_ext->max; 814 return calib_data_get(tas_priv, reg, &dst[1]); 815 } 816 817 static int tasdev_nop_get( 818 struct snd_kcontrol *kcontrol, 819 struct snd_ctl_elem_value *ucontrol) 820 { 821 return 0; 822 } 823 824 static int tasdevice_digital_gain_get( 825 struct snd_kcontrol *kcontrol, 826 struct snd_ctl_elem_value *ucontrol) 827 { 828 struct soc_mixer_control *mc = 829 (struct soc_mixer_control *)kcontrol->private_value; 830 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 831 struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec); 832 unsigned int l = 0, r = mc->max; 833 unsigned int target, ar_mid, mid, ar_l, ar_r; 834 unsigned int reg = mc->reg; 835 unsigned char data[4]; 836 int ret; 837 838 mutex_lock(&tas_dev->codec_lock); 839 /* Read the primary device */ 840 ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4); 841 if (ret) { 842 dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__); 843 goto out; 844 } 845 846 target = get_unaligned_be32(&data[0]); 847 848 while (r > 1 + l) { 849 mid = (l + r) / 2; 850 ar_mid = get_unaligned_be32(tas_dev->dvc_tlv_table[mid]); 851 if (target < ar_mid) 852 r = mid; 853 else 854 l = mid; 855 } 856 857 ar_l = get_unaligned_be32(tas_dev->dvc_tlv_table[l]); 858 ar_r = get_unaligned_be32(tas_dev->dvc_tlv_table[r]); 859 860 /* find out the member same as or closer to the current volume */ 861 ucontrol->value.integer.value[0] = 862 abs(target - ar_l) <= abs(target - ar_r) ? l : r; 863 out: 864 mutex_unlock(&tas_dev->codec_lock); 865 return 0; 866 } 867 868 static int tasdevice_digital_gain_put( 869 struct snd_kcontrol *kcontrol, 870 struct snd_ctl_elem_value *ucontrol) 871 { 872 struct soc_mixer_control *mc = 873 (struct soc_mixer_control *)kcontrol->private_value; 874 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 875 struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec); 876 int vol = ucontrol->value.integer.value[0]; 877 int status = 0, max = mc->max, rc = 1; 878 int i, ret; 879 unsigned int reg = mc->reg; 880 unsigned int volrd, volwr; 881 unsigned char data[4]; 882 883 vol = clamp(vol, 0, max); 884 mutex_lock(&tas_dev->codec_lock); 885 /* Read the primary device */ 886 ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4); 887 if (ret) { 888 dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__); 889 rc = -1; 890 goto out; 891 } 892 893 volrd = get_unaligned_be32(&data[0]); 894 volwr = get_unaligned_be32(tas_dev->dvc_tlv_table[vol]); 895 896 if (volrd == volwr) { 897 rc = 0; 898 goto out; 899 } 900 901 for (i = 0; i < tas_dev->ndev; i++) { 902 ret = tasdevice_dev_bulk_write(tas_dev, i, reg, 903 (unsigned char *)tas_dev->dvc_tlv_table[vol], 4); 904 if (ret) { 905 dev_err(tas_dev->dev, 906 "%s, set digital vol error in dev %d\n", 907 __func__, i); 908 status |= BIT(i); 909 } 910 } 911 912 if (status) 913 rc = -1; 914 out: 915 mutex_unlock(&tas_dev->codec_lock); 916 return rc; 917 } 918 919 static const struct snd_kcontrol_new tasdevice_cali_controls[] = { 920 SOC_SINGLE_EXT("Calibration Stop", SND_SOC_NOPM, 0, 1, 0, 921 tasdev_nop_get, tasdev_calib_stop_put), 922 SND_SOC_BYTES_EXT("Amp TF Data", 6, tasdev_tf_data_get, NULL), 923 SND_SOC_BYTES_EXT("Amp RE Data", 6, tasdev_re_data_get, NULL), 924 SND_SOC_BYTES_EXT("Amp R0 Data", 6, tasdev_r0_data_get, NULL), 925 SND_SOC_BYTES_EXT("Amp XMA1 Data", 6, tasdev_XMA1_data_get, NULL), 926 SND_SOC_BYTES_EXT("Amp XMA2 Data", 6, tasdev_XMA2_data_get, NULL), 927 }; 928 929 static const struct snd_kcontrol_new tas2x20_snd_controls[] = { 930 SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS2X20_AMP_LEVEL, 931 0, 0, 42, 1, tas2781_amp_getvol, 932 tas2781_amp_putvol, tas2x20_amp_tlv), 933 SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2X20_DVC_LEVEL, 934 0, 0, ARRAY_SIZE(tas2x20_dvc_table) - 1, 0, 935 tasdevice_digital_gain_get, tasdevice_digital_gain_put, 936 tas2x20_dvc_tlv), 937 }; 938 939 static const struct snd_kcontrol_new tas2781_snd_controls[] = { 940 SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS2781_AMP_LEVEL, 941 1, 0, 20, 0, tas2781_amp_getvol, 942 tas2781_amp_putvol, tas2781_amp_tlv), 943 SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2781_DVC_LVL, 944 0, 0, 200, 1, tas2781_digital_getvol, 945 tas2781_digital_putvol, tas2781_dvc_tlv), 946 }; 947 948 static const struct snd_kcontrol_new tas5825_snd_controls[] = { 949 SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS5825_AMP_LEVEL, 950 0, 0, 31, 1, tas2781_amp_getvol, 951 tas2781_amp_putvol, tas5825_amp_tlv), 952 SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS5825_DVC_LEVEL, 953 0, 0, 254, 1, tas2781_amp_getvol, 954 tas2781_amp_putvol, tas5825_dvc_tlv), 955 }; 956 957 static const struct snd_kcontrol_new tas2781_cali_controls[] = { 958 SND_SOC_BYTES_EXT("Amp Latch Data", 3, tas2781_latch_reg_get, NULL), 959 }; 960 961 static const struct snd_kcontrol_new tas2563_snd_controls[] = { 962 SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2563_DVC_LVL, 0, 963 0, ARRAY_SIZE(tas2563_dvc_table) - 1, 0, 964 tasdevice_digital_gain_get, tasdevice_digital_gain_put, 965 tas2563_dvc_tlv), 966 }; 967 968 static const struct snd_kcontrol_new tas2563_cali_controls[] = { 969 SOC_SINGLE_EXT("Calibration Start", SND_SOC_NOPM, 0, 1, 0, 970 tasdev_nop_get, tas2563_calib_start_put), 971 }; 972 973 static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol, 974 struct snd_ctl_elem_value *ucontrol) 975 { 976 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 977 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 978 int ret = 0; 979 980 if (tas_priv->rcabin.profile_cfg_id != 981 ucontrol->value.integer.value[0]) { 982 tas_priv->rcabin.profile_cfg_id = 983 ucontrol->value.integer.value[0]; 984 ret = 1; 985 } 986 987 return ret; 988 } 989 990 static int tasdevice_info_active_num(struct snd_kcontrol *kcontrol, 991 struct snd_ctl_elem_info *uinfo) 992 { 993 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 994 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 995 996 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 997 uinfo->count = 1; 998 uinfo->value.integer.min = 0; 999 uinfo->value.integer.max = tas_priv->ndev - 1; 1000 1001 return 0; 1002 } 1003 1004 static int tasdevice_info_chip_id(struct snd_kcontrol *kcontrol, 1005 struct snd_ctl_elem_info *uinfo) 1006 { 1007 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1008 uinfo->count = 1; 1009 uinfo->value.integer.min = TAS2020; 1010 uinfo->value.integer.max = TAS_OTHERS; 1011 1012 return 0; 1013 } 1014 1015 static int tasdevice_info_programs(struct snd_kcontrol *kcontrol, 1016 struct snd_ctl_elem_info *uinfo) 1017 { 1018 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 1019 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1020 struct tasdevice_fw *tas_fw = tas_priv->fmw; 1021 1022 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1023 uinfo->count = 1; 1024 uinfo->value.integer.min = 0; 1025 uinfo->value.integer.max = (int)tas_fw->nr_programs; 1026 1027 return 0; 1028 } 1029 1030 static int tasdevice_info_configurations( 1031 struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 1032 { 1033 struct snd_soc_component *codec = 1034 snd_soc_kcontrol_component(kcontrol); 1035 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1036 struct tasdevice_fw *tas_fw = tas_priv->fmw; 1037 1038 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1039 uinfo->count = 1; 1040 uinfo->value.integer.min = 0; 1041 uinfo->value.integer.max = (int)tas_fw->nr_configurations - 1; 1042 1043 return 0; 1044 } 1045 1046 static int tasdevice_info_profile(struct snd_kcontrol *kcontrol, 1047 struct snd_ctl_elem_info *uinfo) 1048 { 1049 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 1050 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1051 1052 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 1053 uinfo->count = 1; 1054 uinfo->value.integer.min = 0; 1055 uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1; 1056 1057 return 0; 1058 } 1059 1060 static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol, 1061 struct snd_ctl_elem_value *ucontrol) 1062 { 1063 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 1064 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1065 1066 ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id; 1067 1068 return 0; 1069 } 1070 1071 static int tasdevice_get_chip_id(struct snd_kcontrol *kcontrol, 1072 struct snd_ctl_elem_value *ucontrol) 1073 { 1074 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 1075 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1076 1077 ucontrol->value.integer.value[0] = tas_priv->chip_id; 1078 1079 return 0; 1080 } 1081 1082 static int tasdevice_create_control(struct tasdevice_priv *tas_priv) 1083 { 1084 struct snd_kcontrol_new *prof_ctrls; 1085 int nr_controls = 1; 1086 int mix_index = 0; 1087 int ret; 1088 char *name; 1089 1090 prof_ctrls = devm_kcalloc(tas_priv->dev, nr_controls, 1091 sizeof(prof_ctrls[0]), GFP_KERNEL); 1092 if (!prof_ctrls) { 1093 ret = -ENOMEM; 1094 goto out; 1095 } 1096 1097 /* Create a mixer item for selecting the active profile */ 1098 name = devm_kstrdup(tas_priv->dev, "Speaker Profile Id", GFP_KERNEL); 1099 if (!name) { 1100 ret = -ENOMEM; 1101 goto out; 1102 } 1103 prof_ctrls[mix_index].name = name; 1104 prof_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1105 prof_ctrls[mix_index].info = tasdevice_info_profile; 1106 prof_ctrls[mix_index].get = tasdevice_get_profile_id; 1107 prof_ctrls[mix_index].put = tasdevice_set_profile_id; 1108 mix_index++; 1109 1110 ret = snd_soc_add_component_controls(tas_priv->codec, 1111 prof_ctrls, nr_controls < mix_index ? nr_controls : mix_index); 1112 1113 out: 1114 return ret; 1115 } 1116 1117 static int tasdevice_program_get(struct snd_kcontrol *kcontrol, 1118 struct snd_ctl_elem_value *ucontrol) 1119 { 1120 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 1121 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1122 1123 ucontrol->value.integer.value[0] = tas_priv->cur_prog; 1124 1125 return 0; 1126 } 1127 1128 static int tasdevice_program_put(struct snd_kcontrol *kcontrol, 1129 struct snd_ctl_elem_value *ucontrol) 1130 { 1131 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 1132 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1133 unsigned int nr_program = ucontrol->value.integer.value[0]; 1134 int ret = 0; 1135 1136 if (tas_priv->cur_prog != nr_program) { 1137 tas_priv->cur_prog = nr_program; 1138 ret = 1; 1139 } 1140 1141 return ret; 1142 } 1143 1144 static int tasdevice_configuration_get(struct snd_kcontrol *kcontrol, 1145 struct snd_ctl_elem_value *ucontrol) 1146 { 1147 1148 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 1149 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1150 1151 ucontrol->value.integer.value[0] = tas_priv->cur_conf; 1152 1153 return 0; 1154 } 1155 1156 static int tasdevice_configuration_put( 1157 struct snd_kcontrol *kcontrol, 1158 struct snd_ctl_elem_value *ucontrol) 1159 { 1160 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 1161 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1162 unsigned int nr_configuration = ucontrol->value.integer.value[0]; 1163 int ret = 0; 1164 1165 if (tas_priv->cur_conf != nr_configuration) { 1166 tas_priv->cur_conf = nr_configuration; 1167 ret = 1; 1168 } 1169 1170 return ret; 1171 } 1172 1173 static int tasdevice_active_num_get(struct snd_kcontrol *kcontrol, 1174 struct snd_ctl_elem_value *ucontrol) 1175 { 1176 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 1177 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1178 struct i2c_client *clt = (struct i2c_client *)tas_priv->client; 1179 struct tasdevice *tasdev = tas_priv->tasdevice; 1180 int i; 1181 1182 for (i = 0; i < tas_priv->ndev; i++) { 1183 if (clt->addr == tasdev[i].dev_addr) { 1184 ucontrol->value.integer.value[0] = i; 1185 return 0; 1186 } 1187 } 1188 1189 return -1; 1190 } 1191 1192 static int tasdevice_active_num_put(struct snd_kcontrol *kcontrol, 1193 struct snd_ctl_elem_value *ucontrol) 1194 { 1195 struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol); 1196 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1197 int dev_id = ucontrol->value.integer.value[0]; 1198 int max = tas_priv->ndev - 1; 1199 1200 dev_id = clamp(dev_id, 0, max); 1201 1202 guard(mutex)(&tas_priv->codec_lock); 1203 return tasdev_chn_switch(tas_priv, dev_id); 1204 } 1205 1206 static int tasdevice_dsp_create_ctrls(struct tasdevice_priv *tas_priv) 1207 { 1208 struct snd_kcontrol_new *dsp_ctrls; 1209 char *active_dev_num, *chip_id, *fw_load; 1210 char *conf_name, *prog_name; 1211 int nr_controls = 5; 1212 int mix_index = 0; 1213 1214 /* Alloc kcontrol via devm_kzalloc, which don't manually 1215 * free the kcontrol 1216 */ 1217 dsp_ctrls = devm_kcalloc(tas_priv->dev, nr_controls, 1218 sizeof(dsp_ctrls[0]), GFP_KERNEL); 1219 if (!dsp_ctrls) 1220 return -ENOMEM; 1221 1222 /* Create mixer items for selecting the active Program and Config */ 1223 prog_name = devm_kstrdup(tas_priv->dev, "Speaker Program Id", 1224 GFP_KERNEL); 1225 if (!prog_name) 1226 return -ENOMEM; 1227 1228 dsp_ctrls[mix_index].name = prog_name; 1229 dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1230 dsp_ctrls[mix_index].info = tasdevice_info_programs; 1231 dsp_ctrls[mix_index].get = tasdevice_program_get; 1232 dsp_ctrls[mix_index].put = tasdevice_program_put; 1233 mix_index++; 1234 1235 conf_name = devm_kstrdup(tas_priv->dev, "Speaker Config Id", 1236 GFP_KERNEL); 1237 if (!conf_name) 1238 return -ENOMEM; 1239 1240 dsp_ctrls[mix_index].name = conf_name; 1241 dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1242 dsp_ctrls[mix_index].info = tasdevice_info_configurations; 1243 dsp_ctrls[mix_index].get = tasdevice_configuration_get; 1244 dsp_ctrls[mix_index].put = tasdevice_configuration_put; 1245 mix_index++; 1246 1247 active_dev_num = devm_kstrdup(tas_priv->dev, "Activate Tasdevice Num", 1248 GFP_KERNEL); 1249 if (!active_dev_num) 1250 return -ENOMEM; 1251 1252 dsp_ctrls[mix_index].name = active_dev_num; 1253 dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1254 dsp_ctrls[mix_index].info = tasdevice_info_active_num; 1255 dsp_ctrls[mix_index].get = tasdevice_active_num_get; 1256 dsp_ctrls[mix_index].put = tasdevice_active_num_put; 1257 mix_index++; 1258 1259 chip_id = devm_kstrdup(tas_priv->dev, "Tasdevice Chip Id", GFP_KERNEL); 1260 if (!chip_id) 1261 return -ENOMEM; 1262 1263 dsp_ctrls[mix_index].name = chip_id; 1264 dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1265 dsp_ctrls[mix_index].info = tasdevice_info_chip_id; 1266 dsp_ctrls[mix_index].get = tasdevice_get_chip_id; 1267 mix_index++; 1268 1269 fw_load = devm_kstrdup(tas_priv->dev, "Speaker Force Firmware Load", 1270 GFP_KERNEL); 1271 if (!fw_load) 1272 return -ENOMEM; 1273 1274 dsp_ctrls[mix_index].name = fw_load; 1275 dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1276 dsp_ctrls[mix_index].info = snd_soc_info_bool_ext; 1277 dsp_ctrls[mix_index].put = tasdev_force_fwload_put; 1278 dsp_ctrls[mix_index].get = tasdev_force_fwload_get; 1279 dsp_ctrls[mix_index].private_value = 0UL; 1280 mix_index++; 1281 1282 return snd_soc_add_component_controls(tas_priv->codec, dsp_ctrls, 1283 nr_controls < mix_index ? nr_controls : mix_index); 1284 } 1285 1286 static void cali_reg_update(struct bulk_reg_val *p, 1287 struct fct_param_address *t) 1288 { 1289 const int sum = ARRAY_SIZE(tas2781_cali_start_reg); 1290 int reg, j; 1291 1292 for (j = 0; j < sum; j++) { 1293 switch (tas2781_cali_start_reg[j].reg) { 1294 case 0: 1295 reg = TASDEVICE_REG(t->thr[0], t->thr[1], t->thr[2]); 1296 break; 1297 case TAS2781_PRM_PLT_FLAG_REG: 1298 reg = TASDEVICE_REG(t->plt_flg[0], t->plt_flg[1], 1299 t->plt_flg[2]); 1300 break; 1301 case TAS2781_PRM_SINEGAIN_REG: 1302 reg = TASDEVICE_REG(t->sin_gn[0], t->sin_gn[1], 1303 t->sin_gn[2]); 1304 break; 1305 case TAS2781_PRM_SINEGAIN2_REG: 1306 reg = TASDEVICE_REG(t->sin_gn[0], t->sin_gn[1], 1307 t->sin_gn[2]); 1308 break; 1309 default: 1310 reg = 0; 1311 break; 1312 } 1313 if (reg) 1314 p[j].reg = reg; 1315 } 1316 } 1317 1318 static void alpa_cali_update(struct bulk_reg_val *p, 1319 struct fct_param_address *t) 1320 { 1321 p->is_locked = false; 1322 p->reg = TASDEVICE_REG(t->thr2[0], t->thr2[1], t->thr2[2]); 1323 p->val_len = 4; 1324 } 1325 1326 static int tasdevice_create_cali_ctrls(struct tasdevice_priv *priv) 1327 { 1328 struct calidata *cali_data = &priv->cali_data; 1329 struct tasdevice *tasdev = priv->tasdevice; 1330 struct tasdevice_fw *fmw = priv->fmw; 1331 struct soc_bytes_ext *ext_cali_data; 1332 struct snd_kcontrol_new *cali_ctrls; 1333 unsigned int nctrls; 1334 char *cali_name; 1335 int rc, i; 1336 1337 rc = snd_soc_add_component_controls(priv->codec, 1338 tasdevice_cali_controls, ARRAY_SIZE(tasdevice_cali_controls)); 1339 if (rc < 0) { 1340 dev_err(priv->dev, "%s: Add cali controls err rc = %d", 1341 __func__, rc); 1342 return rc; 1343 } 1344 1345 if (priv->chip_id == TAS2781) { 1346 struct fct_param_address *t = &(fmw->fct_par_addr); 1347 1348 cali_ctrls = (struct snd_kcontrol_new *)tas2781_cali_controls; 1349 nctrls = ARRAY_SIZE(tas2781_cali_controls); 1350 for (i = 0; i < priv->ndev; i++) { 1351 struct bulk_reg_val *p; 1352 1353 p = tasdev[i].cali_data_backup = 1354 kmemdup(tas2781_cali_start_reg, 1355 sizeof(tas2781_cali_start_reg), GFP_KERNEL); 1356 if (!tasdev[i].cali_data_backup) 1357 return -ENOMEM; 1358 if (priv->dspbin_typ) { 1359 cali_reg_update(p, t); 1360 if (priv->dspbin_typ == TASDEV_ALPHA) { 1361 p = &tasdev[i].alp_cali_bckp; 1362 alpa_cali_update(p, t); 1363 } 1364 } 1365 } 1366 } else { 1367 cali_ctrls = (struct snd_kcontrol_new *)tas2563_cali_controls; 1368 nctrls = ARRAY_SIZE(tas2563_cali_controls); 1369 for (i = 0; i < priv->ndev; i++) { 1370 tasdev[i].cali_data_backup = 1371 kmemdup(tas2563_cali_start_reg, 1372 sizeof(tas2563_cali_start_reg), GFP_KERNEL); 1373 if (!tasdev[i].cali_data_backup) 1374 return -ENOMEM; 1375 } 1376 } 1377 1378 rc = snd_soc_add_component_controls(priv->codec, cali_ctrls, nctrls); 1379 if (rc < 0) { 1380 dev_err(priv->dev, "%s: Add chip cali ctrls err rc = %d", 1381 __func__, rc); 1382 return rc; 1383 } 1384 1385 /* index for cali_ctrls */ 1386 i = 0; 1387 if (priv->chip_id == TAS2781) 1388 nctrls = 2; 1389 else 1390 nctrls = 1; 1391 1392 /* 1393 * Alloc kcontrol via devm_kzalloc(), which don't manually 1394 * free the kcontrol。 1395 */ 1396 cali_ctrls = devm_kcalloc(priv->dev, nctrls, 1397 sizeof(cali_ctrls[0]), GFP_KERNEL); 1398 if (!cali_ctrls) 1399 return -ENOMEM; 1400 1401 ext_cali_data = devm_kzalloc(priv->dev, sizeof(*ext_cali_data), 1402 GFP_KERNEL); 1403 if (!ext_cali_data) 1404 return -ENOMEM; 1405 1406 cali_name = devm_kstrdup(priv->dev, "Speaker Calibrated Data", 1407 GFP_KERNEL); 1408 if (!cali_name) 1409 return -ENOMEM; 1410 /* the number of calibrated data per tas2563/tas2781 */ 1411 cali_data->cali_dat_sz_per_dev = 20; 1412 /* 1413 * Data structure for tas2563/tas2781 calibrated data: 1414 * Pkg len (1 byte) 1415 * Reg id (1 byte, constant 'r') 1416 * book, page, register array for calibrated data (15 bytes) 1417 * for (i = 0; i < Device-Sum; i++) { 1418 * Device #i index_info (1 byte) 1419 * Calibrated data for Device #i (20 bytes) 1420 * } 1421 */ 1422 ext_cali_data->max = priv->ndev * 1423 (cali_data->cali_dat_sz_per_dev + 1) + 1 + 15 + 1; 1424 priv->cali_data.total_sz = priv->ndev * 1425 (cali_data->cali_dat_sz_per_dev + 1); 1426 cali_ctrls[i].name = cali_name; 1427 cali_ctrls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1428 cali_ctrls[i].info = snd_soc_bytes_info_ext; 1429 cali_ctrls[i].get = tasdev_cali_data_get; 1430 cali_ctrls[i].put = tasdev_cali_data_put; 1431 cali_ctrls[i].private_value = (unsigned long)ext_cali_data; 1432 i++; 1433 1434 cali_data->data = devm_kzalloc(priv->dev, cali_data->total_sz, 1435 GFP_KERNEL); 1436 if (!cali_data->data) 1437 return -ENOMEM; 1438 1439 if (priv->chip_id == TAS2781) { 1440 struct soc_bytes_ext *ext_cali_start; 1441 char *cali_start_name; 1442 1443 ext_cali_start = devm_kzalloc(priv->dev, 1444 sizeof(*ext_cali_start), GFP_KERNEL); 1445 if (!ext_cali_start) 1446 return -ENOMEM; 1447 1448 cali_start_name = devm_kstrdup(priv->dev, 1449 "Calibration Start", GFP_KERNEL); 1450 if (!cali_start_name) 1451 return -ENOMEM; 1452 /* 1453 * package structure for tas2781 ftc start: 1454 * Pkg len (1 byte) 1455 * Reg id (1 byte, constant 'r') 1456 * book, page, register for pilot threshold, pilot tone 1457 * and sine gain (12 bytes) 1458 * for (i = 0; i < Device-Sum; i++) { 1459 * Device #i index_info (1 byte) 1460 * Sine gain for Device #i (8 bytes) 1461 * } 1462 */ 1463 ext_cali_start->max = 14 + priv->ndev * 9; 1464 cali_ctrls[i].name = cali_start_name; 1465 cali_ctrls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER; 1466 cali_ctrls[i].info = snd_soc_bytes_info_ext; 1467 cali_ctrls[i].put = tas2781_calib_start_put; 1468 cali_ctrls[i].get = tasdev_nop_get; 1469 cali_ctrls[i].private_value = (unsigned long)ext_cali_start; 1470 i++; 1471 } 1472 1473 return snd_soc_add_component_controls(priv->codec, cali_ctrls, 1474 nctrls < i ? nctrls : i); 1475 } 1476 1477 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C 1478 /* 1479 * This debugfs node is a bridge to the acoustic tuning application 1480 * tool which can tune the chips' acoustic effect. 1481 * 1482 * package structure for PPC3 communications: 1483 * Pkg len (1 byte) 1484 * Pkg id (1 byte, 'r' or 'w') 1485 * Dev id (1 byte, i2c address) 1486 * Book id (1 byte) 1487 * Page id (1 byte) 1488 * Reg id (1 byte) 1489 * switch (pkg id) { 1490 * case 'w': 1491 * 1 byte, length of data to read 1492 * case 'r': 1493 * data payload (1~128 bytes) 1494 * } 1495 */ 1496 static ssize_t acoustic_ctl_read(struct file *file, char __user *to, 1497 size_t count, loff_t *ppos) 1498 { 1499 struct snd_soc_component *comp = file->private_data; 1500 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp); 1501 struct acoustic_data *p = &tas_priv->acou_data; 1502 int ret = -1; 1503 1504 if (p->id == 'r' && p->len == count && count <= sizeof(*p)) 1505 ret = simple_read_from_buffer(to, count, ppos, p, p->len); 1506 else 1507 dev_err(tas_priv->dev, "Not ready for get.\n"); 1508 return ret; 1509 } 1510 1511 static ssize_t acoustic_ctl_write(struct file *file, 1512 const char __user *from, size_t count, loff_t *ppos) 1513 { 1514 struct snd_soc_component *comp = file->private_data; 1515 struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp); 1516 struct acoustic_data *p = &priv->acou_data; 1517 unsigned int max_pkg_len = sizeof(*p); 1518 unsigned char *src; 1519 int j, len, reg, val; 1520 unsigned short chn; 1521 int ret = -1; 1522 1523 if (count > sizeof(*p)) { 1524 dev_err(priv->dev, "count(%u) is larger than max(%u).\n", 1525 (unsigned int)count, max_pkg_len); 1526 return ret; 1527 } 1528 1529 src = memdup_user(from, count); 1530 if (IS_ERR(src)) 1531 return PTR_ERR(src); 1532 1533 if (src[0] > max_pkg_len && src[0] != count) { 1534 dev_err(priv->dev, "pkg(%u), max(%u), count(%u) mismatch.\n", 1535 src[0], max_pkg_len, (unsigned int)count); 1536 ret = 0; 1537 goto exit; 1538 } 1539 1540 switch (src[1]) { 1541 case 'r': 1542 /* length of data to read */ 1543 len = src[6]; 1544 break; 1545 case 'w': 1546 /* Skip 6 bytes for package type and register address */ 1547 len = src[0] - 6; 1548 break; 1549 default: 1550 dev_err(priv->dev, "%s Wrong code %02x.\n", __func__, src[1]); 1551 ret = 0; 1552 goto exit; 1553 } 1554 1555 if (len < 1) { 1556 dev_err(priv->dev, "pkg fmt invalid %02x.\n", len); 1557 ret = 0; 1558 goto exit; 1559 } 1560 1561 for (j = 0; j < priv->ndev; j++) 1562 if (src[2] == priv->tasdevice[j].dev_addr) { 1563 chn = j; 1564 break; 1565 } 1566 if (j >= priv->ndev) { 1567 dev_err(priv->dev, "no such device 0x%02x.\n", src[2]); 1568 ret = 0; 1569 goto exit; 1570 } 1571 1572 reg = TASDEVICE_REG(src[3], src[4], src[5]); 1573 1574 guard(mutex)(&priv->codec_lock); 1575 1576 if (src[1] == 'w') { 1577 if (len > 1) 1578 ret = tasdevice_dev_bulk_write(priv, chn, reg, 1579 &src[6], len); 1580 else 1581 ret = tasdevice_dev_write(priv, chn, reg, src[6]); 1582 } else { 1583 struct acoustic_data *p = &priv->acou_data; 1584 1585 memcpy(p, src, 6); 1586 if (len > 1) { 1587 ret = tasdevice_dev_bulk_read(priv, chn, reg, 1588 p->data, len); 1589 } else { 1590 ret = tasdevice_dev_read(priv, chn, reg, &val); 1591 p->data[0] = val; 1592 } 1593 p->len = len + 6; 1594 } 1595 1596 if (ret) 1597 dev_err(priv->dev, "i2c communication error.\n"); 1598 else 1599 ret = count; 1600 exit: 1601 kfree(src); 1602 return ret; 1603 } 1604 1605 static const struct file_operations acoustic_ctl_fops = { 1606 .open = simple_open, 1607 .read = acoustic_ctl_read, 1608 .write = acoustic_ctl_write, 1609 }; 1610 #endif 1611 1612 static void tasdevice_fw_ready(const struct firmware *fmw, 1613 void *context) 1614 { 1615 struct tasdevice_priv *tas_priv = context; 1616 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C 1617 struct snd_soc_component *comp = tas_priv->codec; 1618 struct dentry *debugfs_root = comp->debugfs_root; 1619 char *acoustic_debugfs_node; 1620 #endif 1621 int ret = 0; 1622 int i; 1623 1624 mutex_lock(&tas_priv->codec_lock); 1625 1626 ret = tasdevice_rca_parser(tas_priv, fmw); 1627 if (ret) { 1628 tasdevice_config_info_remove(tas_priv); 1629 goto out; 1630 } 1631 tasdevice_create_control(tas_priv); 1632 1633 tasdevice_dsp_remove(tas_priv); 1634 tasdevice_calbin_remove(tas_priv); 1635 /* 1636 * The baseline is the RCA-only case, and then the code attempts to 1637 * load DSP firmware but in case of failures just keep going, i.e. 1638 * failing to load DSP firmware is NOT an error. 1639 */ 1640 tas_priv->fw_state = TASDEVICE_RCA_FW_OK; 1641 /* There is no DSP firmware required for TAS2118/2X20/257X. */ 1642 switch (tas_priv->chip_id) { 1643 case TAS2020: 1644 case TAS2118: 1645 case TAS2120: 1646 case TAS2320: 1647 case TAS2570: 1648 case TAS2572: 1649 goto out; 1650 } 1651 if (tas_priv->name_prefix) 1652 scnprintf(tas_priv->coef_binaryname, 64, "%s-%s_coef.bin", 1653 tas_priv->name_prefix, tas_priv->dev_name); 1654 else 1655 scnprintf(tas_priv->coef_binaryname, 64, "%s_coef.bin", 1656 tas_priv->dev_name); 1657 ret = tasdevice_dsp_parser(tas_priv); 1658 if (ret) { 1659 dev_err(tas_priv->dev, "dspfw load %s error\n", 1660 tas_priv->coef_binaryname); 1661 goto out; 1662 } 1663 1664 /* 1665 * If no dsp-related kcontrol created, the dsp resource will be freed. 1666 */ 1667 ret = tasdevice_dsp_create_ctrls(tas_priv); 1668 if (ret) { 1669 dev_err(tas_priv->dev, "dsp controls error\n"); 1670 goto out; 1671 } 1672 tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK; 1673 1674 /* There is no calibration required for 1675 * TAS5802/TAS5815/TAS5825/TAS5827/TAS5828. 1676 */ 1677 if (tas_priv->chip_id < TAS5802) { 1678 ret = tasdevice_create_cali_ctrls(tas_priv); 1679 if (ret) { 1680 dev_err(tas_priv->dev, "cali controls error\n"); 1681 goto out; 1682 } 1683 /* If calibrated data occurs error, dsp will still works 1684 * with default calibrated data inside algo. 1685 */ 1686 for (i = 0; i < tas_priv->ndev; i++) { 1687 if (tas_priv->name_prefix) 1688 scnprintf(tas_priv->cal_binaryname[i], 64, 1689 "%s-%s_cal_0x%02x.bin", 1690 tas_priv->name_prefix, 1691 tas_priv->dev_name, 1692 tas_priv->tasdevice[i].dev_addr); 1693 else 1694 scnprintf(tas_priv->cal_binaryname[i], 64, 1695 "%s_cal_0x%02x.bin", 1696 tas_priv->dev_name, 1697 tas_priv->tasdevice[i].dev_addr); 1698 ret = tas2781_load_calibration(tas_priv, 1699 tas_priv->cal_binaryname[i], i); 1700 if (ret != 0) 1701 dev_err(tas_priv->dev, 1702 "%s: load %s error, keep default.\n", 1703 __func__, tas_priv->cal_binaryname[i]); 1704 } 1705 } 1706 1707 tasdevice_prmg_load(tas_priv, 0); 1708 tas_priv->cur_prog = 0; 1709 1710 /* Init common setting for different audio profiles */ 1711 if (tas_priv->rcabin.init_profile_id >= 0) 1712 tasdevice_select_cfg_blk(tas_priv, 1713 tas_priv->rcabin.init_profile_id, 1714 TASDEVICE_BIN_BLK_PRE_POWER_UP); 1715 1716 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C 1717 if (tas_priv->name_prefix) 1718 acoustic_debugfs_node = devm_kasprintf(tas_priv->dev, 1719 GFP_KERNEL, "%s_acoustic_ctl", tas_priv->name_prefix); 1720 else 1721 acoustic_debugfs_node = devm_kstrdup(tas_priv->dev, 1722 "acoustic_ctl", GFP_KERNEL); 1723 debugfs_create_file(acoustic_debugfs_node, 0644, debugfs_root, 1724 comp, &acoustic_ctl_fops); 1725 #endif 1726 out: 1727 if (tas_priv->fw_state == TASDEVICE_RCA_FW_OK) { 1728 switch (tas_priv->chip_id) { 1729 case TAS2563: 1730 case TAS2781: 1731 case TAS5802: 1732 case TAS5815: 1733 case TAS5825: 1734 case TAS5827: 1735 case TAS5828: 1736 /* If DSP FW fail, DSP kcontrol won't be created. */ 1737 tasdevice_dsp_remove(tas_priv); 1738 } 1739 } 1740 mutex_unlock(&tas_priv->codec_lock); 1741 release_firmware(fmw); 1742 } 1743 1744 static int tasdevice_dapm_event(struct snd_soc_dapm_widget *w, 1745 struct snd_kcontrol *kcontrol, int event) 1746 { 1747 struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm); 1748 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1749 int state = 0; 1750 1751 /* Codec Lock Hold */ 1752 mutex_lock(&tas_priv->codec_lock); 1753 if (event == SND_SOC_DAPM_PRE_PMD) 1754 state = 1; 1755 tasdevice_tuning_switch(tas_priv, state); 1756 /* Codec Lock Release*/ 1757 mutex_unlock(&tas_priv->codec_lock); 1758 1759 return 0; 1760 } 1761 1762 static const struct snd_soc_dapm_widget tasdevice_dapm_widgets[] = { 1763 SND_SOC_DAPM_AIF_IN("ASI", "ASI Playback", 0, SND_SOC_NOPM, 0, 0), 1764 SND_SOC_DAPM_AIF_OUT_E("ASI OUT", "ASI Capture", 0, SND_SOC_NOPM, 1765 0, 0, tasdevice_dapm_event, 1766 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD), 1767 SND_SOC_DAPM_SPK("SPK", tasdevice_dapm_event), 1768 SND_SOC_DAPM_OUTPUT("OUT"), 1769 SND_SOC_DAPM_INPUT("DMIC"), 1770 }; 1771 1772 static const struct snd_soc_dapm_route tasdevice_audio_map[] = { 1773 {"SPK", NULL, "ASI"}, 1774 {"OUT", NULL, "SPK"}, 1775 {"ASI OUT", NULL, "DMIC"}, 1776 }; 1777 1778 static int tasdevice_startup(struct snd_pcm_substream *substream, 1779 struct snd_soc_dai *dai) 1780 { 1781 struct snd_soc_component *codec = dai->component; 1782 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1783 1784 switch (tas_priv->fw_state) { 1785 case TASDEVICE_RCA_FW_OK: 1786 case TASDEVICE_DSP_FW_ALL_OK: 1787 return 0; 1788 default: 1789 return -EINVAL; 1790 } 1791 } 1792 1793 static int tasdevice_hw_params(struct snd_pcm_substream *substream, 1794 struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) 1795 { 1796 struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(dai); 1797 unsigned int slot_width; 1798 unsigned int fsrate; 1799 int bclk_rate; 1800 1801 fsrate = params_rate(params); 1802 switch (fsrate) { 1803 case 48000: 1804 case 44100: 1805 break; 1806 default: 1807 dev_err(tas_priv->dev, "%s: incorrect sample rate = %u\n", 1808 __func__, fsrate); 1809 return -EINVAL; 1810 } 1811 1812 slot_width = params_width(params); 1813 switch (slot_width) { 1814 case 16: 1815 case 20: 1816 case 24: 1817 case 32: 1818 break; 1819 default: 1820 dev_err(tas_priv->dev, "%s: incorrect slot width = %u\n", 1821 __func__, slot_width); 1822 return -EINVAL; 1823 } 1824 1825 bclk_rate = snd_soc_params_to_bclk(params); 1826 if (bclk_rate < 0) { 1827 dev_err(tas_priv->dev, "%s: incorrect bclk rate = %d\n", 1828 __func__, bclk_rate); 1829 return bclk_rate; 1830 } 1831 1832 return 0; 1833 } 1834 1835 static int tasdevice_set_dai_sysclk(struct snd_soc_dai *codec_dai, 1836 int clk_id, unsigned int freq, int dir) 1837 { 1838 struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(codec_dai); 1839 1840 tas_priv->sysclk = freq; 1841 1842 return 0; 1843 } 1844 1845 static const struct snd_soc_dai_ops tasdevice_dai_ops = { 1846 .startup = tasdevice_startup, 1847 .hw_params = tasdevice_hw_params, 1848 .set_sysclk = tasdevice_set_dai_sysclk, 1849 }; 1850 1851 static struct snd_soc_dai_driver tasdevice_dai_driver[] = { 1852 { 1853 .name = "tasdev_codec", 1854 .id = 0, 1855 .playback = { 1856 .stream_name = "Playback", 1857 .channels_min = 1, 1858 .channels_max = 4, 1859 .rates = TASDEVICE_RATES, 1860 .formats = TASDEVICE_FORMATS, 1861 }, 1862 .capture = { 1863 .stream_name = "Capture", 1864 .channels_min = 1, 1865 .channels_max = 4, 1866 .rates = TASDEVICE_RATES, 1867 .formats = TASDEVICE_FORMATS, 1868 }, 1869 .ops = &tasdevice_dai_ops, 1870 .symmetric_rate = 1, 1871 }, 1872 }; 1873 1874 static int tasdevice_codec_probe(struct snd_soc_component *codec) 1875 { 1876 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1877 struct snd_kcontrol_new *p; 1878 unsigned int size; 1879 int rc; 1880 1881 switch (tas_priv->chip_id) { 1882 case TAS2020: 1883 case TAS2118: 1884 case TAS2120: 1885 case TAS2320: 1886 case TAS2570: 1887 case TAS2572: 1888 p = (struct snd_kcontrol_new *)tas2x20_snd_controls; 1889 size = ARRAY_SIZE(tas2x20_snd_controls); 1890 tas_priv->dvc_tlv_table = tas2x20_dvc_table; 1891 break; 1892 case TAS2781: 1893 p = (struct snd_kcontrol_new *)tas2781_snd_controls; 1894 size = ARRAY_SIZE(tas2781_snd_controls); 1895 break; 1896 case TAS5802: 1897 case TAS5815: 1898 case TAS5825: 1899 case TAS5827: 1900 case TAS5828: 1901 p = (struct snd_kcontrol_new *)tas5825_snd_controls; 1902 size = ARRAY_SIZE(tas5825_snd_controls); 1903 break; 1904 default: 1905 p = (struct snd_kcontrol_new *)tas2563_snd_controls; 1906 size = ARRAY_SIZE(tas2563_snd_controls); 1907 tas_priv->dvc_tlv_table = tas2563_dvc_table; 1908 break; 1909 } 1910 1911 rc = snd_soc_add_component_controls(codec, p, size); 1912 if (rc < 0) { 1913 dev_err(tas_priv->dev, "%s: Add control err rc = %d", 1914 __func__, rc); 1915 return rc; 1916 } 1917 1918 tas_priv->name_prefix = codec->name_prefix; 1919 return tascodec_init(tas_priv, codec, THIS_MODULE, tasdevice_fw_ready); 1920 } 1921 1922 static void tasdevice_deinit(void *context) 1923 { 1924 struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context; 1925 struct tasdevice *tasdev = tas_priv->tasdevice; 1926 int i; 1927 1928 for (i = 0; i < tas_priv->ndev; i++) 1929 kfree(tasdev[i].cali_data_backup); 1930 1931 tasdevice_config_info_remove(tas_priv); 1932 tasdevice_dsp_remove(tas_priv); 1933 tasdevice_calbin_remove(tas_priv); 1934 tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING; 1935 } 1936 1937 static void tasdevice_codec_remove(struct snd_soc_component *codec) 1938 { 1939 struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec); 1940 1941 tasdevice_deinit(tas_priv); 1942 } 1943 1944 static const struct snd_soc_component_driver 1945 soc_codec_driver_tasdevice = { 1946 .probe = tasdevice_codec_probe, 1947 .remove = tasdevice_codec_remove, 1948 .dapm_widgets = tasdevice_dapm_widgets, 1949 .num_dapm_widgets = ARRAY_SIZE(tasdevice_dapm_widgets), 1950 .dapm_routes = tasdevice_audio_map, 1951 .num_dapm_routes = ARRAY_SIZE(tasdevice_audio_map), 1952 .idle_bias_on = 1, 1953 .endianness = 1, 1954 }; 1955 1956 static void tasdevice_parse_dt(struct tasdevice_priv *tas_priv) 1957 { 1958 struct i2c_client *client = (struct i2c_client *)tas_priv->client; 1959 unsigned int dev_addrs[TASDEVICE_MAX_CHANNELS]; 1960 int i, ndev = 0; 1961 1962 if (tas_priv->isacpi) { 1963 ndev = device_property_read_u32_array(&client->dev, 1964 "ti,audio-slots", NULL, 0); 1965 if (ndev <= 0) { 1966 ndev = 1; 1967 dev_addrs[0] = client->addr; 1968 } else { 1969 ndev = (ndev < ARRAY_SIZE(dev_addrs)) 1970 ? ndev : ARRAY_SIZE(dev_addrs); 1971 ndev = device_property_read_u32_array(&client->dev, 1972 "ti,audio-slots", dev_addrs, ndev); 1973 } 1974 1975 tas_priv->irq = 1976 acpi_dev_gpio_irq_get(ACPI_COMPANION(&client->dev), 0); 1977 } else if (IS_ENABLED(CONFIG_OF)) { 1978 struct device_node *np = tas_priv->dev->of_node; 1979 u64 addr; 1980 1981 for (i = 0; i < TASDEVICE_MAX_CHANNELS; i++) { 1982 if (of_property_read_reg(np, i, &addr, NULL)) 1983 break; 1984 dev_addrs[ndev++] = addr; 1985 } 1986 1987 tas_priv->irq = of_irq_get(np, 0); 1988 } else { 1989 ndev = 1; 1990 dev_addrs[0] = client->addr; 1991 } 1992 tas_priv->ndev = ndev; 1993 for (i = 0; i < ndev; i++) 1994 tas_priv->tasdevice[i].dev_addr = dev_addrs[i]; 1995 1996 tas_priv->reset = devm_gpiod_get_optional(&client->dev, 1997 "reset", GPIOD_OUT_HIGH); 1998 if (IS_ERR(tas_priv->reset)) 1999 dev_err(tas_priv->dev, "%s Can't get reset GPIO\n", 2000 __func__); 2001 2002 strcpy(tas_priv->dev_name, tasdevice_id[tas_priv->chip_id].name); 2003 } 2004 2005 static int tasdevice_i2c_probe(struct i2c_client *i2c) 2006 { 2007 const struct acpi_device_id *acpi_id; 2008 struct tasdevice_priv *tas_priv; 2009 int ret; 2010 2011 tas_priv = tasdevice_kzalloc(i2c); 2012 if (!tas_priv) 2013 return -ENOMEM; 2014 2015 dev_set_drvdata(&i2c->dev, tas_priv); 2016 2017 if (ACPI_HANDLE(&i2c->dev)) { 2018 acpi_id = acpi_match_device(i2c->dev.driver->acpi_match_table, 2019 &i2c->dev); 2020 if (!acpi_id) { 2021 dev_err(&i2c->dev, "No driver data\n"); 2022 ret = -EINVAL; 2023 goto err; 2024 } 2025 tas_priv->chip_id = acpi_id->driver_data; 2026 tas_priv->isacpi = true; 2027 } else { 2028 tas_priv->chip_id = (uintptr_t)i2c_get_match_data(i2c); 2029 tas_priv->isacpi = false; 2030 } 2031 2032 tasdevice_parse_dt(tas_priv); 2033 2034 ret = tasdevice_init(tas_priv); 2035 if (ret) 2036 goto err; 2037 2038 tasdevice_reset(tas_priv); 2039 2040 ret = devm_snd_soc_register_component(tas_priv->dev, 2041 &soc_codec_driver_tasdevice, 2042 tasdevice_dai_driver, ARRAY_SIZE(tasdevice_dai_driver)); 2043 if (ret) { 2044 dev_err(tas_priv->dev, "%s: codec register error:0x%08x\n", 2045 __func__, ret); 2046 goto err; 2047 } 2048 err: 2049 if (ret < 0) 2050 tasdevice_remove(tas_priv); 2051 return ret; 2052 } 2053 2054 static void tasdevice_i2c_remove(struct i2c_client *client) 2055 { 2056 struct tasdevice_priv *tas_priv = i2c_get_clientdata(client); 2057 2058 tasdevice_remove(tas_priv); 2059 } 2060 2061 #ifdef CONFIG_ACPI 2062 static const struct acpi_device_id tasdevice_acpi_match[] = { 2063 { "TXNW2020", TAS2020 }, 2064 { "TXNW2118", TAS2118 }, 2065 { "TXNW2120", TAS2120 }, 2066 { "TXNW2320", TAS2320 }, 2067 { "TXNW2563", TAS2563 }, 2068 { "TXNW2570", TAS2570 }, 2069 { "TXNW2572", TAS2572 }, 2070 { "TXNW2781", TAS2781 }, 2071 { "TXNW5802", TAS5802 }, 2072 { "TXNW5815", TAS5815 }, 2073 { "TXNW5825", TAS5825 }, 2074 { "TXNW5827", TAS5827 }, 2075 { "TXNW5828", TAS5828 }, 2076 {}, 2077 }; 2078 2079 MODULE_DEVICE_TABLE(acpi, tasdevice_acpi_match); 2080 #endif 2081 2082 static struct i2c_driver tasdevice_i2c_driver = { 2083 .driver = { 2084 .name = "tasdev-codec", 2085 .of_match_table = of_match_ptr(tasdevice_of_match), 2086 #ifdef CONFIG_ACPI 2087 .acpi_match_table = ACPI_PTR(tasdevice_acpi_match), 2088 #endif 2089 }, 2090 .probe = tasdevice_i2c_probe, 2091 .remove = tasdevice_i2c_remove, 2092 .id_table = tasdevice_id, 2093 }; 2094 2095 module_i2c_driver(tasdevice_i2c_driver); 2096 2097 MODULE_AUTHOR("Shenghao Ding <shenghao-ding@ti.com>"); 2098 MODULE_AUTHOR("Kevin Lu <kevin-lu@ti.com>"); 2099 MODULE_DESCRIPTION("ASoC TAS2781 Driver"); 2100 MODULE_LICENSE("GPL"); 2101 MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB"); 2102