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