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