1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // TAS2781 HDA I2C driver 4 // 5 // Copyright 2023 Texas Instruments, Inc. 6 // 7 // Author: Shenghao Ding <shenghao-ding@ti.com> 8 9 #include <linux/acpi.h> 10 #include <linux/crc8.h> 11 #include <linux/crc32.h> 12 #include <linux/efi.h> 13 #include <linux/firmware.h> 14 #include <linux/i2c.h> 15 #include <linux/mod_devicetable.h> 16 #include <linux/module.h> 17 #include <linux/pm_runtime.h> 18 #include <linux/regmap.h> 19 #include <sound/hda_codec.h> 20 #include <sound/soc.h> 21 #include <sound/tas2781.h> 22 #include <sound/tlv.h> 23 #include <sound/tas2781-tlv.h> 24 25 #include "hda_local.h" 26 #include "hda_auto_parser.h" 27 #include "hda_component.h" 28 #include "hda_jack.h" 29 #include "hda_generic.h" 30 31 #define TASDEVICE_SPEAKER_CALIBRATION_SIZE 20 32 33 /* No standard control callbacks for SNDRV_CTL_ELEM_IFACE_CARD 34 * Define two controls, one is Volume control callbacks, the other is 35 * flag setting control callbacks. 36 */ 37 38 /* Volume control callbacks for tas2781 */ 39 #define ACARD_SINGLE_RANGE_EXT_TLV(xname, xreg, xshift, xmin, xmax, xinvert, \ 40 xhandler_get, xhandler_put, tlv_array) \ 41 { .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = (xname),\ 42 .access = SNDRV_CTL_ELEM_ACCESS_TLV_READ |\ 43 SNDRV_CTL_ELEM_ACCESS_READWRITE,\ 44 .tlv.p = (tlv_array), \ 45 .info = snd_soc_info_volsw_range, \ 46 .get = xhandler_get, .put = xhandler_put, \ 47 .private_value = (unsigned long)&(struct soc_mixer_control) \ 48 {.reg = xreg, .rreg = xreg, .shift = xshift, \ 49 .rshift = xshift, .min = xmin, .max = xmax, \ 50 .invert = xinvert} } 51 52 /* Flag control callbacks for tas2781 */ 53 #define ACARD_SINGLE_BOOL_EXT(xname, xdata, xhandler_get, xhandler_put) \ 54 { .iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = xname, \ 55 .info = snd_ctl_boolean_mono_info, \ 56 .get = xhandler_get, .put = xhandler_put, \ 57 .private_value = xdata } 58 59 enum calib_data { 60 R0_VAL = 0, 61 INV_R0, 62 R0LOW, 63 POWER, 64 TLIM, 65 CALIB_MAX 66 }; 67 68 #define TAS2563_MAX_CHANNELS 4 69 70 #define TAS2563_CAL_POWER TASDEVICE_REG(0, 0x0d, 0x3c) 71 #define TAS2563_CAL_R0 TASDEVICE_REG(0, 0x0f, 0x34) 72 #define TAS2563_CAL_INVR0 TASDEVICE_REG(0, 0x0f, 0x40) 73 #define TAS2563_CAL_R0_LOW TASDEVICE_REG(0, 0x0f, 0x48) 74 #define TAS2563_CAL_TLIM TASDEVICE_REG(0, 0x10, 0x14) 75 #define TAS2563_CAL_N 5 76 #define TAS2563_CAL_DATA_SIZE 4 77 #define TAS2563_CAL_CH_SIZE 20 78 #define TAS2563_CAL_ARRAY_SIZE 80 79 80 static unsigned int cal_regs[TAS2563_CAL_N] = { 81 TAS2563_CAL_POWER, TAS2563_CAL_R0, TAS2563_CAL_INVR0, 82 TAS2563_CAL_R0_LOW, TAS2563_CAL_TLIM, 83 }; 84 85 86 struct tas2781_hda { 87 struct device *dev; 88 struct tasdevice_priv *priv; 89 struct snd_kcontrol *dsp_prog_ctl; 90 struct snd_kcontrol *dsp_conf_ctl; 91 struct snd_kcontrol *prof_ctl; 92 struct snd_kcontrol *snd_ctls[2]; 93 }; 94 95 static int tas2781_get_i2c_res(struct acpi_resource *ares, void *data) 96 { 97 struct tasdevice_priv *tas_priv = data; 98 struct acpi_resource_i2c_serialbus *sb; 99 100 if (i2c_acpi_get_i2c_resource(ares, &sb)) { 101 if (tas_priv->ndev < TASDEVICE_MAX_CHANNELS && 102 sb->slave_address != tas_priv->global_addr) { 103 tas_priv->tasdevice[tas_priv->ndev].dev_addr = 104 (unsigned int)sb->slave_address; 105 tas_priv->ndev++; 106 } 107 } 108 return 1; 109 } 110 111 static int tas2781_read_acpi(struct tasdevice_priv *p, const char *hid) 112 { 113 struct acpi_device *adev; 114 LIST_HEAD(resources); 115 int ret; 116 117 adev = acpi_dev_get_first_match_dev(hid, NULL, -1); 118 if (!adev) { 119 dev_err(p->dev, 120 "Failed to find an ACPI device for %s\n", hid); 121 return -ENODEV; 122 } 123 124 ret = acpi_dev_get_resources(adev, &resources, tas2781_get_i2c_res, p); 125 if (ret < 0) 126 goto err; 127 128 acpi_dev_free_resource_list(&resources); 129 strscpy(p->dev_name, hid, sizeof(p->dev_name)); 130 acpi_dev_put(adev); 131 132 return 0; 133 134 err: 135 dev_err(p->dev, "read acpi error, ret: %d\n", ret); 136 acpi_dev_put(adev); 137 138 return ret; 139 } 140 141 static void tas2781_hda_playback_hook(struct device *dev, int action) 142 { 143 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 144 145 dev_dbg(tas_hda->dev, "%s: action = %d\n", __func__, action); 146 switch (action) { 147 case HDA_GEN_PCM_ACT_OPEN: 148 pm_runtime_get_sync(dev); 149 mutex_lock(&tas_hda->priv->codec_lock); 150 tasdevice_tuning_switch(tas_hda->priv, 0); 151 tas_hda->priv->playback_started = true; 152 mutex_unlock(&tas_hda->priv->codec_lock); 153 break; 154 case HDA_GEN_PCM_ACT_CLOSE: 155 mutex_lock(&tas_hda->priv->codec_lock); 156 tasdevice_tuning_switch(tas_hda->priv, 1); 157 tas_hda->priv->playback_started = false; 158 mutex_unlock(&tas_hda->priv->codec_lock); 159 160 pm_runtime_mark_last_busy(dev); 161 pm_runtime_put_autosuspend(dev); 162 break; 163 default: 164 break; 165 } 166 } 167 168 static int tasdevice_info_profile(struct snd_kcontrol *kcontrol, 169 struct snd_ctl_elem_info *uinfo) 170 { 171 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 172 173 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 174 uinfo->count = 1; 175 uinfo->value.integer.min = 0; 176 uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1; 177 178 return 0; 179 } 180 181 static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol, 182 struct snd_ctl_elem_value *ucontrol) 183 { 184 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 185 186 mutex_lock(&tas_priv->codec_lock); 187 188 ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id; 189 190 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n", 191 __func__, kcontrol->id.name, tas_priv->rcabin.profile_cfg_id); 192 193 mutex_unlock(&tas_priv->codec_lock); 194 195 return 0; 196 } 197 198 static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol, 199 struct snd_ctl_elem_value *ucontrol) 200 { 201 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 202 int nr_profile = ucontrol->value.integer.value[0]; 203 int max = tas_priv->rcabin.ncfgs - 1; 204 int val, ret = 0; 205 206 val = clamp(nr_profile, 0, max); 207 208 mutex_lock(&tas_priv->codec_lock); 209 210 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n", 211 __func__, kcontrol->id.name, 212 tas_priv->rcabin.profile_cfg_id, val); 213 214 if (tas_priv->rcabin.profile_cfg_id != val) { 215 tas_priv->rcabin.profile_cfg_id = val; 216 ret = 1; 217 } 218 219 mutex_unlock(&tas_priv->codec_lock); 220 221 return ret; 222 } 223 224 static int tasdevice_info_programs(struct snd_kcontrol *kcontrol, 225 struct snd_ctl_elem_info *uinfo) 226 { 227 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 228 struct tasdevice_fw *tas_fw = tas_priv->fmw; 229 230 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 231 uinfo->count = 1; 232 uinfo->value.integer.min = 0; 233 uinfo->value.integer.max = tas_fw->nr_programs - 1; 234 235 return 0; 236 } 237 238 static int tasdevice_info_config(struct snd_kcontrol *kcontrol, 239 struct snd_ctl_elem_info *uinfo) 240 { 241 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 242 struct tasdevice_fw *tas_fw = tas_priv->fmw; 243 244 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 245 uinfo->count = 1; 246 uinfo->value.integer.min = 0; 247 uinfo->value.integer.max = tas_fw->nr_configurations - 1; 248 249 return 0; 250 } 251 252 static int tasdevice_program_get(struct snd_kcontrol *kcontrol, 253 struct snd_ctl_elem_value *ucontrol) 254 { 255 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 256 257 mutex_lock(&tas_priv->codec_lock); 258 259 ucontrol->value.integer.value[0] = tas_priv->cur_prog; 260 261 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n", 262 __func__, kcontrol->id.name, tas_priv->cur_prog); 263 264 mutex_unlock(&tas_priv->codec_lock); 265 266 return 0; 267 } 268 269 static int tasdevice_program_put(struct snd_kcontrol *kcontrol, 270 struct snd_ctl_elem_value *ucontrol) 271 { 272 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 273 struct tasdevice_fw *tas_fw = tas_priv->fmw; 274 int nr_program = ucontrol->value.integer.value[0]; 275 int max = tas_fw->nr_programs - 1; 276 int val, ret = 0; 277 278 val = clamp(nr_program, 0, max); 279 280 mutex_lock(&tas_priv->codec_lock); 281 282 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n", 283 __func__, kcontrol->id.name, tas_priv->cur_prog, val); 284 285 if (tas_priv->cur_prog != val) { 286 tas_priv->cur_prog = val; 287 ret = 1; 288 } 289 290 mutex_unlock(&tas_priv->codec_lock); 291 292 return ret; 293 } 294 295 static int tasdevice_config_get(struct snd_kcontrol *kcontrol, 296 struct snd_ctl_elem_value *ucontrol) 297 { 298 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 299 300 mutex_lock(&tas_priv->codec_lock); 301 302 ucontrol->value.integer.value[0] = tas_priv->cur_conf; 303 304 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n", 305 __func__, kcontrol->id.name, tas_priv->cur_conf); 306 307 mutex_unlock(&tas_priv->codec_lock); 308 309 return 0; 310 } 311 312 static int tasdevice_config_put(struct snd_kcontrol *kcontrol, 313 struct snd_ctl_elem_value *ucontrol) 314 { 315 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 316 struct tasdevice_fw *tas_fw = tas_priv->fmw; 317 int nr_config = ucontrol->value.integer.value[0]; 318 int max = tas_fw->nr_configurations - 1; 319 int val, ret = 0; 320 321 val = clamp(nr_config, 0, max); 322 323 mutex_lock(&tas_priv->codec_lock); 324 325 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n", 326 __func__, kcontrol->id.name, tas_priv->cur_conf, val); 327 328 if (tas_priv->cur_conf != val) { 329 tas_priv->cur_conf = val; 330 ret = 1; 331 } 332 333 mutex_unlock(&tas_priv->codec_lock); 334 335 return ret; 336 } 337 338 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol, 339 struct snd_ctl_elem_value *ucontrol) 340 { 341 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 342 struct soc_mixer_control *mc = 343 (struct soc_mixer_control *)kcontrol->private_value; 344 int ret; 345 346 mutex_lock(&tas_priv->codec_lock); 347 348 ret = tasdevice_amp_getvol(tas_priv, ucontrol, mc); 349 350 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %ld\n", 351 __func__, kcontrol->id.name, ucontrol->value.integer.value[0]); 352 353 mutex_unlock(&tas_priv->codec_lock); 354 355 return ret; 356 } 357 358 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol, 359 struct snd_ctl_elem_value *ucontrol) 360 { 361 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 362 struct soc_mixer_control *mc = 363 (struct soc_mixer_control *)kcontrol->private_value; 364 int ret; 365 366 mutex_lock(&tas_priv->codec_lock); 367 368 dev_dbg(tas_priv->dev, "%s: kcontrol %s: -> %ld\n", 369 __func__, kcontrol->id.name, ucontrol->value.integer.value[0]); 370 371 /* The check of the given value is in tasdevice_amp_putvol. */ 372 ret = tasdevice_amp_putvol(tas_priv, ucontrol, mc); 373 374 mutex_unlock(&tas_priv->codec_lock); 375 376 return ret; 377 } 378 379 static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol, 380 struct snd_ctl_elem_value *ucontrol) 381 { 382 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 383 384 mutex_lock(&tas_priv->codec_lock); 385 386 ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status; 387 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n", 388 __func__, kcontrol->id.name, tas_priv->force_fwload_status); 389 390 mutex_unlock(&tas_priv->codec_lock); 391 392 return 0; 393 } 394 395 static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol, 396 struct snd_ctl_elem_value *ucontrol) 397 { 398 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 399 bool change, val = (bool)ucontrol->value.integer.value[0]; 400 401 mutex_lock(&tas_priv->codec_lock); 402 403 dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n", 404 __func__, kcontrol->id.name, 405 tas_priv->force_fwload_status, val); 406 407 if (tas_priv->force_fwload_status == val) 408 change = false; 409 else { 410 change = true; 411 tas_priv->force_fwload_status = val; 412 } 413 414 mutex_unlock(&tas_priv->codec_lock); 415 416 return change; 417 } 418 419 static const struct snd_kcontrol_new tas2781_snd_controls[] = { 420 ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL, 421 1, 0, 20, 0, tas2781_amp_getvol, 422 tas2781_amp_putvol, amp_vol_tlv), 423 ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0, 424 tas2781_force_fwload_get, tas2781_force_fwload_put), 425 }; 426 427 static const struct snd_kcontrol_new tas2781_prof_ctrl = { 428 .name = "Speaker Profile Id", 429 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 430 .info = tasdevice_info_profile, 431 .get = tasdevice_get_profile_id, 432 .put = tasdevice_set_profile_id, 433 }; 434 435 static const struct snd_kcontrol_new tas2781_dsp_prog_ctrl = { 436 .name = "Speaker Program Id", 437 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 438 .info = tasdevice_info_programs, 439 .get = tasdevice_program_get, 440 .put = tasdevice_program_put, 441 }; 442 443 static const struct snd_kcontrol_new tas2781_dsp_conf_ctrl = { 444 .name = "Speaker Config Id", 445 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 446 .info = tasdevice_info_config, 447 .get = tasdevice_config_get, 448 .put = tasdevice_config_put, 449 }; 450 451 static void tas2563_apply_calib(struct tasdevice_priv *tas_priv) 452 { 453 int offset = 0; 454 __be32 data; 455 int ret; 456 457 for (int i = 0; i < tas_priv->ndev; i++) { 458 for (int j = 0; j < TAS2563_CAL_N; ++j) { 459 data = cpu_to_be32( 460 *(uint32_t *)&tas_priv->cali_data.data[offset]); 461 ret = tasdevice_dev_bulk_write(tas_priv, i, cal_regs[j], 462 (unsigned char *)&data, TAS2563_CAL_DATA_SIZE); 463 if (ret) 464 dev_err(tas_priv->dev, 465 "Error writing calib regs\n"); 466 offset += TAS2563_CAL_DATA_SIZE; 467 } 468 } 469 } 470 471 static int tas2563_save_calibration(struct tasdevice_priv *tas_priv) 472 { 473 static efi_guid_t efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc, 474 0x09, 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92); 475 476 static efi_char16_t *efi_vars[TAS2563_MAX_CHANNELS][TAS2563_CAL_N] = { 477 { L"Power_1", L"R0_1", L"InvR0_1", L"R0_Low_1", L"TLim_1" }, 478 { L"Power_2", L"R0_2", L"InvR0_2", L"R0_Low_2", L"TLim_2" }, 479 { L"Power_3", L"R0_3", L"InvR0_3", L"R0_Low_3", L"TLim_3" }, 480 { L"Power_4", L"R0_4", L"InvR0_4", L"R0_Low_4", L"TLim_4" }, 481 }; 482 483 unsigned long max_size = TAS2563_CAL_DATA_SIZE; 484 unsigned int offset = 0; 485 efi_status_t status; 486 unsigned int attr; 487 488 tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev, 489 TAS2563_CAL_ARRAY_SIZE, GFP_KERNEL); 490 if (!tas_priv->cali_data.data) 491 return -ENOMEM; 492 493 for (int i = 0; i < tas_priv->ndev; ++i) { 494 for (int j = 0; j < TAS2563_CAL_N; ++j) { 495 status = efi.get_variable(efi_vars[i][j], 496 &efi_guid, &attr, &max_size, 497 &tas_priv->cali_data.data[offset]); 498 if (status != EFI_SUCCESS || 499 max_size != TAS2563_CAL_DATA_SIZE) { 500 dev_warn(tas_priv->dev, 501 "Calibration data read failed %ld\n", status); 502 return -EINVAL; 503 } 504 offset += TAS2563_CAL_DATA_SIZE; 505 } 506 } 507 508 tas_priv->cali_data.total_sz = offset; 509 tasdevice_apply_calibration(tas_priv); 510 511 return 0; 512 } 513 514 static void tas2781_apply_calib(struct tasdevice_priv *tas_priv) 515 { 516 static const unsigned char page_array[CALIB_MAX] = { 517 0x17, 0x18, 0x18, 0x13, 0x18, 518 }; 519 static const unsigned char rgno_array[CALIB_MAX] = { 520 0x74, 0x0c, 0x14, 0x70, 0x7c, 521 }; 522 unsigned char *data; 523 int i, j, rc; 524 525 for (i = 0; i < tas_priv->ndev; i++) { 526 data = tas_priv->cali_data.data + 527 i * TASDEVICE_SPEAKER_CALIBRATION_SIZE; 528 for (j = 0; j < CALIB_MAX; j++) { 529 rc = tasdevice_dev_bulk_write(tas_priv, i, 530 TASDEVICE_REG(0, page_array[j], rgno_array[j]), 531 &(data[4 * j]), 4); 532 if (rc < 0) 533 dev_err(tas_priv->dev, 534 "chn %d calib %d bulk_wr err = %d\n", 535 i, j, rc); 536 } 537 } 538 } 539 540 /* Update the calibration data, including speaker impedance, f0, etc, into algo. 541 * Calibrate data is done by manufacturer in the factory. These data are used 542 * by Algo for calculating the speaker temperature, speaker membrane excursion 543 * and f0 in real time during playback. 544 */ 545 static int tas2781_save_calibration(struct tasdevice_priv *tas_priv) 546 { 547 efi_guid_t efi_guid = EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d, 548 0x93, 0xfe, 0x5a, 0xa3, 0x5d, 0xb3); 549 static efi_char16_t efi_name[] = L"CALI_DATA"; 550 struct tm *tm = &tas_priv->tm; 551 unsigned int attr, crc; 552 unsigned int *tmp_val; 553 efi_status_t status; 554 555 /* Lenovo devices */ 556 if (tas_priv->catlog_id == LENOVO) 557 efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc, 0x09, 558 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92); 559 560 tas_priv->cali_data.total_sz = 0; 561 /* Get real size of UEFI variable */ 562 status = efi.get_variable(efi_name, &efi_guid, &attr, 563 &tas_priv->cali_data.total_sz, tas_priv->cali_data.data); 564 if (status == EFI_BUFFER_TOO_SMALL) { 565 /* Allocate data buffer of data_size bytes */ 566 tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev, 567 tas_priv->cali_data.total_sz, GFP_KERNEL); 568 if (!tas_priv->cali_data.data) 569 return -ENOMEM; 570 /* Get variable contents into buffer */ 571 status = efi.get_variable(efi_name, &efi_guid, &attr, 572 &tas_priv->cali_data.total_sz, 573 tas_priv->cali_data.data); 574 } 575 if (status != EFI_SUCCESS) 576 return -EINVAL; 577 578 tmp_val = (unsigned int *)tas_priv->cali_data.data; 579 580 crc = crc32(~0, tas_priv->cali_data.data, 84) ^ ~0; 581 dev_dbg(tas_priv->dev, "cali crc 0x%08x PK tmp_val 0x%08x\n", 582 crc, tmp_val[21]); 583 584 if (crc == tmp_val[21]) { 585 time64_to_tm(tmp_val[20], 0, tm); 586 dev_dbg(tas_priv->dev, "%4ld-%2d-%2d, %2d:%2d:%2d\n", 587 tm->tm_year, tm->tm_mon, tm->tm_mday, 588 tm->tm_hour, tm->tm_min, tm->tm_sec); 589 tasdevice_apply_calibration(tas_priv); 590 } else 591 tas_priv->cali_data.total_sz = 0; 592 593 return 0; 594 } 595 596 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda) 597 { 598 struct hda_codec *codec = tas_hda->priv->codec; 599 600 snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl); 601 snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl); 602 603 for (int i = ARRAY_SIZE(tas_hda->snd_ctls) - 1; i >= 0; i--) 604 snd_ctl_remove(codec->card, tas_hda->snd_ctls[i]); 605 606 snd_ctl_remove(codec->card, tas_hda->prof_ctl); 607 } 608 609 static void tasdev_fw_ready(const struct firmware *fmw, void *context) 610 { 611 struct tasdevice_priv *tas_priv = context; 612 struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev); 613 struct hda_codec *codec = tas_priv->codec; 614 int i, ret; 615 616 pm_runtime_get_sync(tas_priv->dev); 617 mutex_lock(&tas_priv->codec_lock); 618 619 ret = tasdevice_rca_parser(tas_priv, fmw); 620 if (ret) 621 goto out; 622 623 tas_hda->prof_ctl = snd_ctl_new1(&tas2781_prof_ctrl, tas_priv); 624 ret = snd_ctl_add(codec->card, tas_hda->prof_ctl); 625 if (ret) { 626 dev_err(tas_priv->dev, 627 "Failed to add KControl %s = %d\n", 628 tas2781_prof_ctrl.name, ret); 629 goto out; 630 } 631 632 for (i = 0; i < ARRAY_SIZE(tas2781_snd_controls); i++) { 633 tas_hda->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_controls[i], 634 tas_priv); 635 ret = snd_ctl_add(codec->card, tas_hda->snd_ctls[i]); 636 if (ret) { 637 dev_err(tas_priv->dev, 638 "Failed to add KControl %s = %d\n", 639 tas2781_snd_controls[i].name, ret); 640 goto out; 641 } 642 } 643 644 tasdevice_dsp_remove(tas_priv); 645 646 tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING; 647 scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%04X.bin", 648 codec->core.subsystem_id & 0xffff); 649 ret = tasdevice_dsp_parser(tas_priv); 650 if (ret) { 651 dev_err(tas_priv->dev, "dspfw load %s error\n", 652 tas_priv->coef_binaryname); 653 tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL; 654 goto out; 655 } 656 657 tas_hda->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_prog_ctrl, 658 tas_priv); 659 ret = snd_ctl_add(codec->card, tas_hda->dsp_prog_ctl); 660 if (ret) { 661 dev_err(tas_priv->dev, 662 "Failed to add KControl %s = %d\n", 663 tas2781_dsp_prog_ctrl.name, ret); 664 goto out; 665 } 666 667 tas_hda->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_conf_ctrl, 668 tas_priv); 669 ret = snd_ctl_add(codec->card, tas_hda->dsp_conf_ctl); 670 if (ret) { 671 dev_err(tas_priv->dev, 672 "Failed to add KControl %s = %d\n", 673 tas2781_dsp_conf_ctrl.name, ret); 674 goto out; 675 } 676 677 tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK; 678 tasdevice_prmg_load(tas_priv, 0); 679 if (tas_priv->fmw->nr_programs > 0) 680 tas_priv->cur_prog = 0; 681 if (tas_priv->fmw->nr_configurations > 0) 682 tas_priv->cur_conf = 0; 683 684 /* If calibrated data occurs error, dsp will still works with default 685 * calibrated data inside algo. 686 */ 687 tasdevice_save_calibration(tas_priv); 688 689 tasdevice_tuning_switch(tas_hda->priv, 0); 690 tas_hda->priv->playback_started = true; 691 692 out: 693 mutex_unlock(&tas_hda->priv->codec_lock); 694 if (fmw) 695 release_firmware(fmw); 696 pm_runtime_mark_last_busy(tas_hda->dev); 697 pm_runtime_put_autosuspend(tas_hda->dev); 698 } 699 700 static int tas2781_hda_bind(struct device *dev, struct device *master, 701 void *master_data) 702 { 703 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 704 struct hda_component_parent *parent = master_data; 705 struct hda_component *comp; 706 struct hda_codec *codec; 707 unsigned int subid; 708 int ret; 709 710 comp = hda_component_from_index(parent, tas_hda->priv->index); 711 if (!comp) 712 return -EINVAL; 713 714 if (comp->dev) 715 return -EBUSY; 716 717 codec = parent->codec; 718 subid = codec->core.subsystem_id >> 16; 719 720 switch (subid) { 721 case 0x17aa: 722 tas_hda->priv->catlog_id = LENOVO; 723 break; 724 default: 725 tas_hda->priv->catlog_id = OTHERS; 726 break; 727 } 728 729 pm_runtime_get_sync(dev); 730 731 comp->dev = dev; 732 733 strscpy(comp->name, dev_name(dev), sizeof(comp->name)); 734 735 ret = tascodec_init(tas_hda->priv, codec, THIS_MODULE, tasdev_fw_ready); 736 if (!ret) 737 comp->playback_hook = tas2781_hda_playback_hook; 738 739 pm_runtime_mark_last_busy(dev); 740 pm_runtime_put_autosuspend(dev); 741 742 return ret; 743 } 744 745 static void tas2781_hda_unbind(struct device *dev, 746 struct device *master, void *master_data) 747 { 748 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 749 struct hda_component_parent *parent = master_data; 750 struct hda_component *comp; 751 752 comp = hda_component_from_index(parent, tas_hda->priv->index); 753 if (comp && (comp->dev == dev)) { 754 comp->dev = NULL; 755 memset(comp->name, 0, sizeof(comp->name)); 756 comp->playback_hook = NULL; 757 } 758 759 tas2781_hda_remove_controls(tas_hda); 760 761 tasdevice_config_info_remove(tas_hda->priv); 762 tasdevice_dsp_remove(tas_hda->priv); 763 764 tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING; 765 } 766 767 static const struct component_ops tas2781_hda_comp_ops = { 768 .bind = tas2781_hda_bind, 769 .unbind = tas2781_hda_unbind, 770 }; 771 772 static void tas2781_hda_remove(struct device *dev) 773 { 774 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 775 776 component_del(tas_hda->dev, &tas2781_hda_comp_ops); 777 778 pm_runtime_get_sync(tas_hda->dev); 779 pm_runtime_disable(tas_hda->dev); 780 781 pm_runtime_put_noidle(tas_hda->dev); 782 783 tasdevice_remove(tas_hda->priv); 784 } 785 786 static int tas2781_hda_i2c_probe(struct i2c_client *clt) 787 { 788 struct tas2781_hda *tas_hda; 789 const char *device_name; 790 int ret; 791 792 793 tas_hda = devm_kzalloc(&clt->dev, sizeof(*tas_hda), GFP_KERNEL); 794 if (!tas_hda) 795 return -ENOMEM; 796 797 dev_set_drvdata(&clt->dev, tas_hda); 798 tas_hda->dev = &clt->dev; 799 800 tas_hda->priv = tasdevice_kzalloc(clt); 801 if (!tas_hda->priv) 802 return -ENOMEM; 803 804 if (strstr(dev_name(&clt->dev), "TIAS2781")) { 805 device_name = "TIAS2781"; 806 tas_hda->priv->save_calibration = tas2781_save_calibration; 807 tas_hda->priv->apply_calibration = tas2781_apply_calib; 808 tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR; 809 } else if (strstr(dev_name(&clt->dev), "INT8866")) { 810 device_name = "INT8866"; 811 tas_hda->priv->save_calibration = tas2563_save_calibration; 812 tas_hda->priv->apply_calibration = tas2563_apply_calib; 813 tas_hda->priv->global_addr = TAS2563_GLOBAL_ADDR; 814 } else 815 return -ENODEV; 816 817 tas_hda->priv->irq_info.irq = clt->irq; 818 ret = tas2781_read_acpi(tas_hda->priv, device_name); 819 if (ret) 820 return dev_err_probe(tas_hda->dev, ret, 821 "Platform not supported\n"); 822 823 ret = tasdevice_init(tas_hda->priv); 824 if (ret) 825 goto err; 826 827 pm_runtime_set_autosuspend_delay(tas_hda->dev, 3000); 828 pm_runtime_use_autosuspend(tas_hda->dev); 829 pm_runtime_mark_last_busy(tas_hda->dev); 830 pm_runtime_set_active(tas_hda->dev); 831 pm_runtime_enable(tas_hda->dev); 832 833 tasdevice_reset(tas_hda->priv); 834 835 ret = component_add(tas_hda->dev, &tas2781_hda_comp_ops); 836 if (ret) { 837 dev_err(tas_hda->dev, "Register component failed: %d\n", ret); 838 pm_runtime_disable(tas_hda->dev); 839 } 840 841 err: 842 if (ret) 843 tas2781_hda_remove(&clt->dev); 844 return ret; 845 } 846 847 static void tas2781_hda_i2c_remove(struct i2c_client *clt) 848 { 849 tas2781_hda_remove(&clt->dev); 850 } 851 852 static int tas2781_runtime_suspend(struct device *dev) 853 { 854 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 855 856 dev_dbg(tas_hda->dev, "Runtime Suspend\n"); 857 858 mutex_lock(&tas_hda->priv->codec_lock); 859 860 /* The driver powers up the amplifiers at module load time. 861 * Stop the playback if it's unused. 862 */ 863 if (tas_hda->priv->playback_started) { 864 tasdevice_tuning_switch(tas_hda->priv, 1); 865 tas_hda->priv->playback_started = false; 866 } 867 868 mutex_unlock(&tas_hda->priv->codec_lock); 869 870 return 0; 871 } 872 873 static int tas2781_runtime_resume(struct device *dev) 874 { 875 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 876 877 dev_dbg(tas_hda->dev, "Runtime Resume\n"); 878 879 mutex_lock(&tas_hda->priv->codec_lock); 880 881 tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog); 882 883 /* If calibrated data occurs error, dsp will still works with default 884 * calibrated data inside algo. 885 */ 886 tasdevice_apply_calibration(tas_hda->priv); 887 888 mutex_unlock(&tas_hda->priv->codec_lock); 889 890 return 0; 891 } 892 893 static int tas2781_system_suspend(struct device *dev) 894 { 895 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 896 897 dev_dbg(tas_hda->priv->dev, "System Suspend\n"); 898 899 mutex_lock(&tas_hda->priv->codec_lock); 900 901 /* Shutdown chip before system suspend */ 902 if (tas_hda->priv->playback_started) 903 tasdevice_tuning_switch(tas_hda->priv, 1); 904 905 mutex_unlock(&tas_hda->priv->codec_lock); 906 907 /* 908 * Reset GPIO may be shared, so cannot reset here. 909 * However beyond this point, amps may be powered down. 910 */ 911 return 0; 912 } 913 914 static int tas2781_system_resume(struct device *dev) 915 { 916 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 917 int i; 918 919 dev_dbg(tas_hda->priv->dev, "System Resume\n"); 920 921 mutex_lock(&tas_hda->priv->codec_lock); 922 923 for (i = 0; i < tas_hda->priv->ndev; i++) { 924 tas_hda->priv->tasdevice[i].cur_book = -1; 925 tas_hda->priv->tasdevice[i].cur_prog = -1; 926 tas_hda->priv->tasdevice[i].cur_conf = -1; 927 } 928 tasdevice_reset(tas_hda->priv); 929 tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog); 930 931 /* If calibrated data occurs error, dsp will still work with default 932 * calibrated data inside algo. 933 */ 934 tasdevice_apply_calibration(tas_hda->priv); 935 936 if (tas_hda->priv->playback_started) 937 tasdevice_tuning_switch(tas_hda->priv, 0); 938 939 mutex_unlock(&tas_hda->priv->codec_lock); 940 941 return 0; 942 } 943 944 static const struct dev_pm_ops tas2781_hda_pm_ops = { 945 RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL) 946 SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume) 947 }; 948 949 static const struct i2c_device_id tas2781_hda_i2c_id[] = { 950 { "tas2781-hda", 0 }, 951 {} 952 }; 953 954 static const struct acpi_device_id tas2781_acpi_hda_match[] = { 955 {"TIAS2781", 0 }, 956 {"INT8866", 0 }, 957 {} 958 }; 959 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match); 960 961 static struct i2c_driver tas2781_hda_i2c_driver = { 962 .driver = { 963 .name = "tas2781-hda", 964 .acpi_match_table = tas2781_acpi_hda_match, 965 .pm = &tas2781_hda_pm_ops, 966 }, 967 .id_table = tas2781_hda_i2c_id, 968 .probe = tas2781_hda_i2c_probe, 969 .remove = tas2781_hda_i2c_remove, 970 }; 971 module_i2c_driver(tas2781_hda_i2c_driver); 972 973 MODULE_DESCRIPTION("TAS2781 HDA Driver"); 974 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>"); 975 MODULE_LICENSE("GPL"); 976 MODULE_IMPORT_NS(SND_SOC_TAS2781_FMWLIB); 977