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