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