1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // TAS2781 HDA SPI driver 4 // 5 // Copyright 2024 - 2026 Texas Instruments, Inc. 6 // 7 // Author: Baojun Xu <baojun.xu@ti.com> 8 9 #include <linux/acpi.h> 10 #include <linux/array_size.h> 11 #include <linux/bits.h> 12 #include <linux/cleanup.h> 13 #include <linux/crc8.h> 14 #include <linux/crc32.h> 15 #include <linux/efi.h> 16 #include <linux/firmware.h> 17 #include <linux/mod_devicetable.h> 18 #include <linux/module.h> 19 #include <linux/mutex.h> 20 #include <linux/pm_runtime.h> 21 #include <linux/property.h> 22 #include <linux/regmap.h> 23 #include <linux/spi/spi.h> 24 #include <linux/time.h> 25 #include <linux/types.h> 26 #include <linux/units.h> 27 28 #include <sound/hda_codec.h> 29 #include <sound/soc.h> 30 #include <sound/tas2781.h> 31 #include <sound/tlv.h> 32 #include <sound/tas2781-tlv.h> 33 34 #include "hda_local.h" 35 #include "hda_auto_parser.h" 36 #include "hda_component.h" 37 #include "hda_jack.h" 38 #include "../generic.h" 39 #include "tas2781_hda.h" 40 41 #define TASDEVICE_RANGE_MAX_SIZE (256 * 128) 42 #define TASDEVICE_WIN_LEN 128 43 #define TAS2781_SPI_MAX_FREQ (4 * HZ_PER_MHZ) 44 45 /* System Reset Check Register */ 46 #define TAS2781_REG_CLK_CONFIG TASDEVICE_REG(0x0, 0x0, 0x5c) 47 #define TAS2781_REG_CLK_CONFIG_RESET 0x19 48 49 struct tas2781_hda_spi_priv { 50 struct snd_kcontrol *snd_ctls[3]; 51 }; 52 53 static const struct regmap_range_cfg tasdevice_ranges[] = { 54 { 55 .range_min = 0, 56 .range_max = TASDEVICE_RANGE_MAX_SIZE, 57 .selector_reg = TASDEVICE_PAGE_SELECT, 58 .selector_mask = GENMASK(7, 0), 59 .selector_shift = 0, 60 .window_start = 0, 61 .window_len = TASDEVICE_WIN_LEN, 62 }, 63 }; 64 65 static const struct regmap_config tasdevice_regmap = { 66 .reg_bits = 8, 67 .val_bits = 8, 68 .zero_flag_mask = true, 69 .read_flag_mask = 0x01, 70 .reg_shift = -1, 71 .cache_type = REGCACHE_NONE, 72 .ranges = tasdevice_ranges, 73 .num_ranges = ARRAY_SIZE(tasdevice_ranges), 74 .max_register = TASDEVICE_RANGE_MAX_SIZE, 75 }; 76 77 static int tasdevice_spi_dev_read(struct tasdevice_priv *tas_priv, 78 unsigned short chn, unsigned int reg, unsigned int *val) 79 { 80 int ret; 81 82 /* 83 * In our TAS2781 SPI mode, if read from other book (not book 0), 84 * or read from page number larger than 1 in book 0, one more byte 85 * read is needed, and first byte is a dummy byte, need to be ignored. 86 */ 87 if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) { 88 unsigned char data[2]; 89 90 ret = tasdevice_dev_bulk_read(tas_priv, chn, reg, 91 data, sizeof(data)); 92 *val = data[1]; 93 } else { 94 ret = tasdevice_dev_read(tas_priv, chn, reg, val); 95 } 96 if (ret < 0) 97 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 98 99 return ret; 100 } 101 102 static int tasdevice_spi_dev_bulk_read(struct tasdevice_priv *tas_priv, 103 unsigned short chn, unsigned int reg, unsigned char *data, 104 unsigned int len) 105 { 106 int ret; 107 108 /* 109 * In our TAS2781 SPI mode, if read from other book (not book 0), 110 * or read from page number larger than 1 in book 0, one more byte 111 * read is needed, and first byte is a dummy byte, need to be ignored. 112 */ 113 if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) { 114 unsigned char buf[TASDEVICE_WIN_LEN + 1]; 115 116 ret = tasdevice_dev_bulk_read(tas_priv, chn, reg, 117 buf, len + 1); 118 memcpy(data, buf + 1, len); 119 } else { 120 ret = tasdevice_dev_bulk_read(tas_priv, chn, reg, data, len); 121 } 122 if (ret < 0) 123 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 124 125 return ret; 126 } 127 128 static int tasdevice_spi_dev_update_bits(struct tasdevice_priv *tas_priv, 129 unsigned short chn, unsigned int reg, unsigned int mask, 130 unsigned int value) 131 { 132 int ret, val; 133 134 /* 135 * In our TAS2781 SPI mode, read/write was masked in last bit of 136 * address, it cause regmap_update_bits() not work as expected. 137 */ 138 ret = tasdevice_dev_read(tas_priv, chn, reg, &val); 139 if (ret < 0) { 140 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 141 return ret; 142 } 143 144 ret = tasdevice_dev_write(tas_priv, chn, TASDEVICE_PAGE_REG(reg), 145 (val & ~mask) | (mask & value)); 146 if (ret < 0) 147 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 148 149 return ret; 150 } 151 152 static int tasdevice_spi_change_chn_book(struct tasdevice_priv *p, 153 unsigned short chn, int book) 154 { 155 int ret = 0; 156 157 if (chn == p->index) { 158 struct tasdevice *tasdev = &p->tasdevice[chn]; 159 struct regmap *map = p->regmap; 160 161 if (tasdev->cur_book != book) { 162 ret = regmap_write(map, TASDEVICE_BOOKCTL_REG, book); 163 if (ret < 0) 164 dev_err(p->dev, "%s, E=%d\n", __func__, ret); 165 else 166 tasdev->cur_book = book; 167 } 168 } else { 169 ret = -EXDEV; 170 dev_dbg(p->dev, "Not error, %s ignore channel(%d)\n", 171 __func__, chn); 172 } 173 174 return ret; 175 } 176 177 static void tas2781_spi_reset(struct tasdevice_priv *tas_dev) 178 { 179 int ret; 180 181 if (tas_dev->reset) { 182 gpiod_set_value_cansleep(tas_dev->reset, 0); 183 fsleep(800); 184 gpiod_set_value_cansleep(tas_dev->reset, 1); 185 } else { 186 ret = tasdevice_dev_write(tas_dev, tas_dev->index, 187 TASDEVICE_REG_SWRESET, TASDEVICE_REG_SWRESET_RESET); 188 if (ret < 0) { 189 dev_err(tas_dev->dev, "dev sw-reset fail, %d\n", ret); 190 return; 191 } 192 fsleep(1000); 193 } 194 } 195 196 static int tascodec_spi_init(struct tasdevice_priv *tas_priv, 197 void *codec, struct module *module, 198 void (*cont)(const struct firmware *fw, void *context)) 199 { 200 int ret; 201 202 /* 203 * Codec Lock Hold to ensure that codec_probe and firmware parsing and 204 * loading do not simultaneously execute. 205 */ 206 guard(mutex)(&tas_priv->codec_lock); 207 208 scnprintf(tas_priv->rca_binaryname, 209 sizeof(tas_priv->rca_binaryname), "%sRCA%d.bin", 210 tas_priv->dev_name, tas_priv->ndev); 211 crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL); 212 tas_priv->codec = codec; 213 ret = request_firmware_nowait(module, FW_ACTION_UEVENT, 214 tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv, 215 cont); 216 if (ret) 217 dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n", 218 ret); 219 220 return ret; 221 } 222 223 static void tasdevice_spi_init(struct tasdevice_priv *tas_priv) 224 { 225 tas_priv->tasdevice[tas_priv->index].cur_book = -1; 226 tas_priv->tasdevice[tas_priv->index].cur_conf = -1; 227 tas_priv->tasdevice[tas_priv->index].cur_prog = -1; 228 229 tas_priv->isspi = true; 230 231 tas_priv->update_bits = tasdevice_spi_dev_update_bits; 232 tas_priv->change_chn_book = tasdevice_spi_change_chn_book; 233 tas_priv->dev_read = tasdevice_spi_dev_read; 234 tas_priv->dev_bulk_read = tasdevice_spi_dev_bulk_read; 235 236 mutex_init(&tas_priv->codec_lock); 237 } 238 239 static int tasdevice_spi_amp_putvol(struct tasdevice_priv *tas_priv, 240 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 241 { 242 unsigned int invert = mc->invert; 243 unsigned char mask; 244 int max = mc->max; 245 int val, ret; 246 247 mask = rounddown_pow_of_two(max); 248 mask <<= mc->shift; 249 val = clamp(invert ? max - ucontrol->value.integer.value[0] : 250 ucontrol->value.integer.value[0], 0, max); 251 252 ret = tasdevice_spi_dev_update_bits(tas_priv, tas_priv->index, 253 mc->reg, mask, (unsigned int)(val << mc->shift)); 254 if (ret) 255 dev_err(tas_priv->dev, "set AMP vol error in dev %d\n", 256 tas_priv->index); 257 258 return ret; 259 } 260 261 static int tasdevice_spi_amp_getvol(struct tasdevice_priv *tas_priv, 262 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 263 { 264 unsigned int invert = mc->invert; 265 unsigned char mask = 0; 266 int max = mc->max; 267 int ret, val; 268 269 ret = tasdevice_spi_dev_read(tas_priv, tas_priv->index, mc->reg, &val); 270 if (ret) { 271 dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__); 272 return ret; 273 } 274 275 mask = rounddown_pow_of_two(max); 276 mask <<= mc->shift; 277 val = (val & mask) >> mc->shift; 278 val = clamp(invert ? max - val : val, 0, max); 279 ucontrol->value.integer.value[0] = val; 280 281 return ret; 282 } 283 284 static int tasdevice_spi_digital_putvol(struct tasdevice_priv *p, 285 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 286 { 287 unsigned int invert = mc->invert; 288 int max = mc->max; 289 int val, ret; 290 291 val = clamp(invert ? max - ucontrol->value.integer.value[0] : 292 ucontrol->value.integer.value[0], 0, max); 293 ret = tasdevice_dev_write(p, p->index, mc->reg, (unsigned int)val); 294 if (ret) 295 dev_err(p->dev, "set digital vol err in dev %d\n", p->index); 296 297 return ret; 298 } 299 300 static int tasdevice_spi_digital_getvol(struct tasdevice_priv *p, 301 struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 302 { 303 unsigned int invert = mc->invert; 304 int max = mc->max; 305 int ret, val; 306 307 ret = tasdevice_spi_dev_read(p, p->index, mc->reg, &val); 308 if (ret) { 309 dev_err(p->dev, "%s, get digital vol err\n", __func__); 310 return ret; 311 } 312 313 val = clamp(invert ? max - val : val, 0, max); 314 ucontrol->value.integer.value[0] = val; 315 316 return ret; 317 } 318 319 static int tas2781_read_acpi(struct tas2781_hda *tas_hda, 320 const char *hid, int id) 321 { 322 struct tasdevice_priv *p = tas_hda->priv; 323 struct acpi_device *adev; 324 struct device *physdev; 325 u32 values[HDA_MAX_COMPONENTS]; 326 const char *property; 327 size_t nval; 328 int ret, i; 329 330 adev = acpi_dev_get_first_match_dev(hid, NULL, -1); 331 if (!adev) { 332 dev_err(p->dev, "Failed to find ACPI device: %s\n", hid); 333 return -ENODEV; 334 } 335 336 strscpy(p->dev_name, hid, sizeof(p->dev_name)); 337 physdev = get_device(acpi_get_first_physical_node(adev)); 338 acpi_dev_put(adev); 339 340 property = "ti,dev-index"; 341 ret = device_property_count_u32(physdev, property); 342 if (ret <= 0 || ret > ARRAY_SIZE(values)) { 343 ret = -EINVAL; 344 goto err; 345 } 346 p->ndev = nval = ret; 347 348 ret = device_property_read_u32_array(physdev, property, values, nval); 349 if (ret) 350 goto err; 351 352 p->index = U8_MAX; 353 for (i = 0; i < nval; i++) { 354 if (values[i] == id) { 355 p->index = i; 356 break; 357 } 358 } 359 if (p->index == U8_MAX) { 360 dev_dbg(p->dev, "No index found in %s\n", property); 361 ret = -ENODEV; 362 goto err; 363 } 364 365 if (p->index == 0) { 366 /* All of amps share same RESET pin. */ 367 p->reset = devm_gpiod_get_index_optional(physdev, "reset", 368 p->index, GPIOD_OUT_LOW); 369 if (IS_ERR(p->reset)) { 370 ret = PTR_ERR(p->reset); 371 dev_err_probe(p->dev, ret, "Failed on reset GPIO\n"); 372 goto err; 373 } 374 } 375 put_device(physdev); 376 377 return 0; 378 err: 379 dev_err(p->dev, "read acpi error, ret: %d\n", ret); 380 put_device(physdev); 381 acpi_dev_put(adev); 382 383 return ret; 384 } 385 386 static void tas2781_hda_playback_hook(struct device *dev, int action) 387 { 388 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 389 struct tasdevice_priv *tas_priv = tas_hda->priv; 390 391 if (action == HDA_GEN_PCM_ACT_OPEN) { 392 pm_runtime_get_sync(dev); 393 guard(mutex)(&tas_priv->codec_lock); 394 if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK) 395 tasdevice_tuning_switch(tas_hda->priv, 0); 396 } else if (action == HDA_GEN_PCM_ACT_CLOSE) { 397 guard(mutex)(&tas_priv->codec_lock); 398 if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK) 399 tasdevice_tuning_switch(tas_priv, 1); 400 pm_runtime_put_autosuspend(dev); 401 } 402 } 403 404 /* 405 * tas2781_digital_getvol - get the volum control 406 * @kcontrol: control pointer 407 * @ucontrol: User data 408 * 409 * Customer Kcontrol for tas2781 is primarily for regmap booking, paging 410 * depends on internal regmap mechanism. 411 * tas2781 contains book and page two-level register map, especially 412 * book switching will set the register BXXP00R7F, after switching to the 413 * correct book, then leverage the mechanism for paging to access the 414 * register. 415 * 416 * Return 0 if succeeded. 417 */ 418 static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol, 419 struct snd_ctl_elem_value *ucontrol) 420 { 421 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 422 struct soc_mixer_control *mc = 423 (struct soc_mixer_control *)kcontrol->private_value; 424 425 guard(mutex)(&tas_priv->codec_lock); 426 return tasdevice_spi_digital_getvol(tas_priv, ucontrol, mc); 427 } 428 429 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol, 430 struct snd_ctl_elem_value *ucontrol) 431 { 432 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 433 struct soc_mixer_control *mc = 434 (struct soc_mixer_control *)kcontrol->private_value; 435 436 guard(mutex)(&tas_priv->codec_lock); 437 return tasdevice_spi_amp_getvol(tas_priv, ucontrol, mc); 438 } 439 440 static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol, 441 struct snd_ctl_elem_value *ucontrol) 442 { 443 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 444 struct soc_mixer_control *mc = 445 (struct soc_mixer_control *)kcontrol->private_value; 446 447 guard(mutex)(&tas_priv->codec_lock); 448 return tasdevice_spi_digital_putvol(tas_priv, ucontrol, mc); 449 } 450 451 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol, 452 struct snd_ctl_elem_value *ucontrol) 453 { 454 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 455 struct soc_mixer_control *mc = 456 (struct soc_mixer_control *)kcontrol->private_value; 457 458 guard(mutex)(&tas_priv->codec_lock); 459 return tasdevice_spi_amp_putvol(tas_priv, ucontrol, mc); 460 } 461 462 static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol, 463 struct snd_ctl_elem_value *ucontrol) 464 { 465 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 466 467 ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status; 468 dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__, 469 str_on_off(tas_priv->force_fwload_status)); 470 471 return 0; 472 } 473 474 static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol, 475 struct snd_ctl_elem_value *ucontrol) 476 { 477 struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol); 478 bool change, val = (bool)ucontrol->value.integer.value[0]; 479 480 if (tas_priv->force_fwload_status == val) { 481 change = false; 482 } else { 483 change = true; 484 tas_priv->force_fwload_status = val; 485 } 486 dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__, 487 str_on_off(tas_priv->force_fwload_status)); 488 489 return change; 490 } 491 492 static struct snd_kcontrol_new tas2781_snd_ctls[] = { 493 ACARD_SINGLE_RANGE_EXT_TLV(NULL, TAS2781_AMP_LEVEL, 1, 0, 20, 0, 494 tas2781_amp_getvol, tas2781_amp_putvol, 495 tas2781_amp_tlv), 496 ACARD_SINGLE_RANGE_EXT_TLV(NULL, TAS2781_DVC_LVL, 0, 0, 200, 1, 497 tas2781_digital_getvol, tas2781_digital_putvol, 498 tas2781_dvc_tlv), 499 ACARD_SINGLE_BOOL_EXT(NULL, 0, tas2781_force_fwload_get, 500 tas2781_force_fwload_put), 501 }; 502 503 static struct snd_kcontrol_new tas2781_prof_ctl = { 504 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 505 .info = tasdevice_info_profile, 506 .get = tasdevice_get_profile_id, 507 .put = tasdevice_set_profile_id, 508 }; 509 510 static struct snd_kcontrol_new tas2781_dsp_ctls[] = { 511 /* Speaker Program */ 512 { 513 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 514 .info = tasdevice_info_programs, 515 .get = tasdevice_program_get, 516 .put = tasdevice_program_put, 517 }, 518 /* Speaker Config */ 519 { 520 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 521 .info = tasdevice_info_config, 522 .get = tasdevice_config_get, 523 .put = tasdevice_config_put, 524 }, 525 }; 526 527 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda) 528 { 529 struct hda_codec *codec = tas_hda->priv->codec; 530 struct tas2781_hda_spi_priv *h_priv = tas_hda->hda_priv; 531 532 snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl); 533 534 snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl); 535 536 for (int i = ARRAY_SIZE(h_priv->snd_ctls) - 1; i >= 0; i--) 537 snd_ctl_remove(codec->card, h_priv->snd_ctls[i]); 538 539 snd_ctl_remove(codec->card, tas_hda->prof_ctl); 540 } 541 542 static int tas2781_hda_spi_prf_ctl(struct tas2781_hda *h) 543 { 544 struct tasdevice_priv *p = h->priv; 545 struct hda_codec *c = p->codec; 546 char name[64]; 547 int rc; 548 549 snprintf(name, sizeof(name), "Speaker-%d Profile Id", p->index); 550 tas2781_prof_ctl.name = name; 551 h->prof_ctl = snd_ctl_new1(&tas2781_prof_ctl, p); 552 rc = snd_ctl_add(c->card, h->prof_ctl); 553 if (rc) 554 dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n", 555 tas2781_prof_ctl.name, rc); 556 return rc; 557 } 558 559 static int tas2781_hda_spi_snd_ctls(struct tas2781_hda *h) 560 { 561 struct tas2781_hda_spi_priv *h_priv = h->hda_priv; 562 struct tasdevice_priv *p = h->priv; 563 struct hda_codec *c = p->codec; 564 char name[64]; 565 int i = 0; 566 int rc; 567 568 snprintf(name, sizeof(name), "Speaker-%d Analog Volume", p->index); 569 tas2781_snd_ctls[i].name = name; 570 h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p); 571 rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]); 572 if (rc) { 573 dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n", 574 tas2781_snd_ctls[i].name, rc); 575 return rc; 576 } 577 i++; 578 snprintf(name, sizeof(name), "Speaker-%d Digital Volume", p->index); 579 tas2781_snd_ctls[i].name = name; 580 h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p); 581 rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]); 582 if (rc) { 583 dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n", 584 tas2781_snd_ctls[i].name, rc); 585 return rc; 586 } 587 i++; 588 snprintf(name, sizeof(name), "Froce Speaker-%d FW Load", p->index); 589 tas2781_snd_ctls[i].name = name; 590 h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p); 591 rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]); 592 if (rc) { 593 dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n", 594 tas2781_snd_ctls[i].name, rc); 595 } 596 return rc; 597 } 598 599 static int tas2781_hda_spi_dsp_ctls(struct tas2781_hda *h) 600 { 601 struct tasdevice_priv *p = h->priv; 602 struct hda_codec *c = p->codec; 603 char name[64]; 604 int i = 0; 605 int rc; 606 607 snprintf(name, sizeof(name), "Speaker-%d Program Id", p->index); 608 tas2781_dsp_ctls[i].name = name; 609 h->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_ctls[i], p); 610 rc = snd_ctl_add(c->card, h->dsp_prog_ctl); 611 if (rc) { 612 dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n", 613 tas2781_dsp_ctls[i].name, rc); 614 return rc; 615 } 616 i++; 617 snprintf(name, sizeof(name), "Speaker-%d Config Id", p->index); 618 tas2781_dsp_ctls[i].name = name; 619 h->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_ctls[i], p); 620 rc = snd_ctl_add(c->card, h->dsp_conf_ctl); 621 if (rc) { 622 dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n", 623 tas2781_dsp_ctls[i].name, rc); 624 } 625 626 return rc; 627 } 628 629 static void tasdev_fw_ready(const struct firmware *fmw, void *context) 630 { 631 struct tasdevice_priv *tas_priv = context; 632 struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev); 633 struct hda_codec *codec = tas_priv->codec; 634 int ret; 635 636 guard(pm_runtime_active_auto)(tas_priv->dev); 637 guard(mutex)(&tas_priv->codec_lock); 638 639 ret = tasdevice_rca_parser(tas_priv, fmw); 640 if (ret) 641 goto out; 642 643 /* Add control one time only. */ 644 ret = tas2781_hda_spi_prf_ctl(tas_hda); 645 if (ret) 646 goto out; 647 648 ret = tas2781_hda_spi_snd_ctls(tas_hda); 649 if (ret) 650 goto out; 651 652 tasdevice_dsp_remove(tas_priv); 653 654 tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING; 655 scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%04X-%01d.bin", 656 lower_16_bits(codec->core.subsystem_id), tas_priv->index); 657 ret = tasdevice_dsp_parser(tas_priv); 658 if (ret) { 659 dev_err(tas_priv->dev, "dspfw load %s error\n", 660 tas_priv->coef_binaryname); 661 tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL; 662 goto out; 663 } 664 665 ret = tas2781_hda_spi_dsp_ctls(tas_hda); 666 if (ret) 667 goto out; 668 /* Perform AMP reset before firmware download. */ 669 tas2781_spi_reset(tas_priv); 670 tas_priv->rcabin.profile_cfg_id = 0; 671 672 tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK; 673 674 ret = tasdevice_prmg_load(tas_priv, 0); 675 if (ret < 0) { 676 dev_err(tas_priv->dev, "FW download failed = %d\n", ret); 677 goto out; 678 } 679 tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK; 680 681 if (tas_priv->fmw->nr_programs > 0) 682 tas_priv->tasdevice[tas_priv->index].cur_prog = 0; 683 if (tas_priv->fmw->nr_configurations > 0) 684 tas_priv->tasdevice[tas_priv->index].cur_conf = 0; 685 686 /* 687 * If calibrated data occurs error, dsp will still works with default 688 * calibrated data inside algo. 689 */ 690 tas2781_save_calibration(tas_hda); 691 out: 692 release_firmware(fmw); 693 } 694 695 static int tas2781_hda_bind(struct device *dev, struct device *master, 696 void *master_data) 697 { 698 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 699 struct hda_component_parent *parent = master_data; 700 struct hda_component *comp; 701 struct hda_codec *codec; 702 int ret; 703 704 comp = hda_component_from_index(parent, tas_hda->priv->index); 705 if (!comp) 706 return -EINVAL; 707 708 if (comp->dev) 709 return -EBUSY; 710 711 codec = parent->codec; 712 713 guard(pm_runtime_active_auto)(dev); 714 715 comp->dev = dev; 716 717 strscpy(comp->name, dev_name(dev), sizeof(comp->name)); 718 719 ret = tascodec_spi_init(tas_hda->priv, codec, THIS_MODULE, 720 tasdev_fw_ready); 721 if (!ret) 722 comp->playback_hook = tas2781_hda_playback_hook; 723 724 /* Only HP Laptop support SPI-based TAS2781 */ 725 tas_hda->catlog_id = HP; 726 727 return ret; 728 } 729 730 static void tas2781_hda_unbind(struct device *dev, struct device *master, 731 void *master_data) 732 { 733 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 734 struct hda_component_parent *parent = master_data; 735 struct tasdevice_priv *tas_priv = tas_hda->priv; 736 struct hda_component *comp; 737 738 comp = hda_component_from_index(parent, tas_priv->index); 739 if (comp && (comp->dev == dev)) { 740 comp->dev = NULL; 741 memset(comp->name, 0, sizeof(comp->name)); 742 comp->playback_hook = NULL; 743 } 744 745 tas2781_hda_remove_controls(tas_hda); 746 747 tasdevice_config_info_remove(tas_priv); 748 tasdevice_dsp_remove(tas_priv); 749 750 tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING; 751 } 752 753 static const struct component_ops tas2781_hda_comp_ops = { 754 .bind = tas2781_hda_bind, 755 .unbind = tas2781_hda_unbind, 756 }; 757 758 static int tas2781_hda_spi_probe(struct spi_device *spi) 759 { 760 struct tas2781_hda_spi_priv *hda_priv; 761 struct tasdevice_priv *tas_priv; 762 struct tas2781_hda *tas_hda; 763 const char *device_name; 764 int ret = 0; 765 766 tas_hda = devm_kzalloc(&spi->dev, sizeof(*tas_hda), GFP_KERNEL); 767 if (!tas_hda) 768 return -ENOMEM; 769 770 hda_priv = devm_kzalloc(&spi->dev, sizeof(*hda_priv), GFP_KERNEL); 771 if (!hda_priv) 772 return -ENOMEM; 773 774 tas_hda->hda_priv = hda_priv; 775 spi->max_speed_hz = TAS2781_SPI_MAX_FREQ; 776 777 tas_priv = devm_kzalloc(&spi->dev, sizeof(*tas_priv), GFP_KERNEL); 778 if (!tas_priv) 779 return -ENOMEM; 780 tas_priv->dev = &spi->dev; 781 tas_hda->priv = tas_priv; 782 tas_priv->regmap = devm_regmap_init_spi(spi, &tasdevice_regmap); 783 if (IS_ERR(tas_priv->regmap)) { 784 ret = PTR_ERR(tas_priv->regmap); 785 dev_err(tas_priv->dev, "Failed to allocate regmap: %d\n", 786 ret); 787 return ret; 788 } 789 if (strstr(dev_name(&spi->dev), "TXNW2781")) { 790 device_name = "TXNW2781"; 791 } else { 792 dev_err(tas_priv->dev, "Unmatched spi dev %s\n", 793 dev_name(&spi->dev)); 794 return -ENODEV; 795 } 796 797 tas_priv->irq = spi->irq; 798 dev_set_drvdata(&spi->dev, tas_hda); 799 ret = tas2781_read_acpi(tas_hda, device_name, 800 spi_get_chipselect(spi, 0)); 801 if (ret) 802 return dev_err_probe(tas_priv->dev, ret, 803 "Platform not supported\n"); 804 805 tasdevice_spi_init(tas_priv); 806 807 pm_runtime_set_autosuspend_delay(tas_priv->dev, 3000); 808 pm_runtime_use_autosuspend(tas_priv->dev); 809 pm_runtime_set_active(tas_priv->dev); 810 pm_runtime_get_noresume(tas_priv->dev); 811 pm_runtime_enable(tas_priv->dev); 812 813 pm_runtime_put_autosuspend(tas_priv->dev); 814 815 ret = component_add(tas_priv->dev, &tas2781_hda_comp_ops); 816 if (ret) { 817 dev_err(tas_priv->dev, "Register component fail: %d\n", ret); 818 pm_runtime_disable(tas_priv->dev); 819 tas2781_hda_remove(&spi->dev, &tas2781_hda_comp_ops); 820 } 821 822 return ret; 823 } 824 825 static void tas2781_hda_spi_remove(struct spi_device *spi) 826 { 827 tas2781_hda_remove(&spi->dev, &tas2781_hda_comp_ops); 828 } 829 830 static int tas2781_runtime_suspend(struct device *dev) 831 { 832 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 833 struct tasdevice_priv *tas_priv = tas_hda->priv; 834 835 guard(mutex)(&tas_priv->codec_lock); 836 837 if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK 838 && tas_priv->playback_started) 839 tasdevice_tuning_switch(tas_priv, 1); 840 841 tas_priv->tasdevice[tas_priv->index].cur_book = -1; 842 tas_priv->tasdevice[tas_priv->index].cur_conf = -1; 843 844 return 0; 845 } 846 847 static int tas2781_runtime_resume(struct device *dev) 848 { 849 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 850 struct tasdevice_priv *tas_priv = tas_hda->priv; 851 852 guard(mutex)(&tas_priv->codec_lock); 853 854 if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK 855 && tas_priv->playback_started) 856 tasdevice_tuning_switch(tas_priv, 0); 857 858 return 0; 859 } 860 861 static int tas2781_system_suspend(struct device *dev) 862 { 863 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 864 struct tasdevice_priv *tas_priv = tas_hda->priv; 865 int ret; 866 867 ret = pm_runtime_force_suspend(dev); 868 if (ret) 869 return ret; 870 871 /* Shutdown chip before system suspend */ 872 if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK 873 && tas_priv->playback_started) 874 tasdevice_tuning_switch(tas_priv, 1); 875 876 return 0; 877 } 878 879 static int tas2781_system_resume(struct device *dev) 880 { 881 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 882 struct tasdevice_priv *tas_priv = tas_hda->priv; 883 int ret, val; 884 885 ret = pm_runtime_force_resume(dev); 886 if (ret) 887 return ret; 888 889 guard(mutex)(&tas_priv->codec_lock); 890 ret = tas_priv->dev_read(tas_priv, tas_priv->index, 891 TAS2781_REG_CLK_CONFIG, &val); 892 if (ret < 0) 893 return ret; 894 895 if (val == TAS2781_REG_CLK_CONFIG_RESET) { 896 tas_priv->tasdevice[tas_priv->index].cur_book = -1; 897 tas_priv->tasdevice[tas_priv->index].cur_conf = -1; 898 tas_priv->tasdevice[tas_priv->index].cur_prog = -1; 899 900 ret = tasdevice_prmg_load(tas_priv, 0); 901 if (ret < 0) { 902 dev_err(tas_priv->dev, 903 "FW download failed = %d\n", ret); 904 return ret; 905 } 906 tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK; 907 908 if (tas_priv->playback_started) 909 tasdevice_tuning_switch(tas_priv, 0); 910 } 911 912 return ret; 913 } 914 915 static const struct dev_pm_ops tas2781_hda_pm_ops = { 916 RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL) 917 SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume) 918 }; 919 920 static const struct spi_device_id tas2781_hda_spi_id[] = { 921 { "tas2781-hda", }, 922 {} 923 }; 924 925 static const struct acpi_device_id tas2781_acpi_hda_match[] = { 926 {"TXNW2781", }, 927 {} 928 }; 929 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match); 930 931 static struct spi_driver tas2781_hda_spi_driver = { 932 .driver = { 933 .name = "tas2781-hda", 934 .acpi_match_table = tas2781_acpi_hda_match, 935 .pm = &tas2781_hda_pm_ops, 936 }, 937 .id_table = tas2781_hda_spi_id, 938 .probe = tas2781_hda_spi_probe, 939 .remove = tas2781_hda_spi_remove, 940 }; 941 module_spi_driver(tas2781_hda_spi_driver); 942 943 MODULE_DESCRIPTION("TAS2781 HDA SPI Driver"); 944 MODULE_AUTHOR("Baojun, Xu, <baojun.xug@ti.com>"); 945 MODULE_LICENSE("GPL"); 946 MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB"); 947 MODULE_IMPORT_NS("SND_HDA_SCODEC_TAS2781"); 948