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