1 // SPDX-License-Identifier: GPL-2.0-only 2 // 3 // HDA audio driver for Cirrus Logic CS35L56 smart amp 4 // 5 // Copyright (C) 2023 Cirrus Logic, Inc. and 6 // Cirrus Logic International Semiconductor Ltd. 7 // 8 9 #include <linux/acpi.h> 10 #include <linux/debugfs.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/module.h> 13 #include <linux/pm_runtime.h> 14 #include <linux/regmap.h> 15 #include <linux/slab.h> 16 #include <sound/core.h> 17 #include <sound/cs-amp-lib.h> 18 #include <sound/hda_codec.h> 19 #include <sound/tlv.h> 20 #include "cirrus_scodec.h" 21 #include "cs35l56_hda.h" 22 #include "hda_component.h" 23 #include "hda_cs_dsp_ctl.h" 24 #include "hda_generic.h" 25 26 /* 27 * The cs35l56_hda_dai_config[] reg sequence configures the device as 28 * ASP1_BCLK_FREQ = 3.072 MHz 29 * ASP1_RX_WIDTH = 32 cycles per slot, ASP1_TX_WIDTH = 32 cycles per slot, ASP1_FMT = I2S 30 * ASP1_DOUT_HIZ_CONTROL = Hi-Z during unused timeslots 31 * ASP1_RX_WL = 24 bits per sample 32 * ASP1_TX_WL = 24 bits per sample 33 * ASP1_RXn_EN 1..3 and ASP1_TXn_EN 1..4 disabled 34 * 35 * Override any Windows-specific mixer settings applied by the firmware. 36 */ 37 static const struct reg_sequence cs35l56_hda_dai_config[] = { 38 { CS35L56_ASP1_CONTROL1, 0x00000021 }, 39 { CS35L56_ASP1_CONTROL2, 0x20200200 }, 40 { CS35L56_ASP1_CONTROL3, 0x00000003 }, 41 { CS35L56_ASP1_FRAME_CONTROL1, 0x03020100 }, 42 { CS35L56_ASP1_FRAME_CONTROL5, 0x00020100 }, 43 { CS35L56_ASP1_DATA_CONTROL5, 0x00000018 }, 44 { CS35L56_ASP1_DATA_CONTROL1, 0x00000018 }, 45 { CS35L56_ASP1_ENABLES1, 0x00000000 }, 46 { CS35L56_ASP1TX1_INPUT, 0x00000018 }, 47 { CS35L56_ASP1TX2_INPUT, 0x00000019 }, 48 { CS35L56_ASP1TX3_INPUT, 0x00000020 }, 49 { CS35L56_ASP1TX4_INPUT, 0x00000028 }, 50 51 }; 52 53 static void cs35l56_hda_wait_dsp_ready(struct cs35l56_hda *cs35l56) 54 { 55 /* Wait for patching to complete */ 56 flush_work(&cs35l56->dsp_work); 57 } 58 59 static void cs35l56_hda_play(struct cs35l56_hda *cs35l56) 60 { 61 unsigned int val; 62 int ret; 63 64 cs35l56_hda_wait_dsp_ready(cs35l56); 65 66 pm_runtime_get_sync(cs35l56->base.dev); 67 ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_PLAY); 68 if (ret == 0) { 69 /* Wait for firmware to enter PS0 power state */ 70 ret = regmap_read_poll_timeout(cs35l56->base.regmap, 71 CS35L56_TRANSDUCER_ACTUAL_PS, 72 val, (val == CS35L56_PS0), 73 CS35L56_PS0_POLL_US, 74 CS35L56_PS0_TIMEOUT_US); 75 if (ret) 76 dev_warn(cs35l56->base.dev, "PS0 wait failed: %d\n", ret); 77 } 78 regmap_set_bits(cs35l56->base.regmap, CS35L56_ASP1_ENABLES1, 79 BIT(CS35L56_ASP_RX1_EN_SHIFT) | BIT(CS35L56_ASP_RX2_EN_SHIFT) | 80 cs35l56->asp_tx_mask); 81 cs35l56->playing = true; 82 } 83 84 static void cs35l56_hda_pause(struct cs35l56_hda *cs35l56) 85 { 86 cs35l56->playing = false; 87 cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_PAUSE); 88 regmap_clear_bits(cs35l56->base.regmap, CS35L56_ASP1_ENABLES1, 89 BIT(CS35L56_ASP_RX1_EN_SHIFT) | BIT(CS35L56_ASP_RX2_EN_SHIFT) | 90 BIT(CS35L56_ASP_TX1_EN_SHIFT) | BIT(CS35L56_ASP_TX2_EN_SHIFT) | 91 BIT(CS35L56_ASP_TX3_EN_SHIFT) | BIT(CS35L56_ASP_TX4_EN_SHIFT)); 92 93 pm_runtime_mark_last_busy(cs35l56->base.dev); 94 pm_runtime_put_autosuspend(cs35l56->base.dev); 95 } 96 97 static void cs35l56_hda_playback_hook(struct device *dev, int action) 98 { 99 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 100 101 dev_dbg(cs35l56->base.dev, "%s()%d: action: %d\n", __func__, __LINE__, action); 102 103 switch (action) { 104 case HDA_GEN_PCM_ACT_PREPARE: 105 if (cs35l56->playing) 106 break; 107 108 /* If we're suspended: flag that resume should start playback */ 109 if (cs35l56->suspended) { 110 cs35l56->playing = true; 111 break; 112 } 113 114 cs35l56_hda_play(cs35l56); 115 break; 116 case HDA_GEN_PCM_ACT_CLEANUP: 117 if (!cs35l56->playing) 118 break; 119 120 cs35l56_hda_pause(cs35l56); 121 break; 122 default: 123 break; 124 } 125 } 126 127 static int cs35l56_hda_runtime_suspend(struct device *dev) 128 { 129 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 130 131 if (cs35l56->cs_dsp.booted) 132 cs_dsp_stop(&cs35l56->cs_dsp); 133 134 return cs35l56_runtime_suspend_common(&cs35l56->base); 135 } 136 137 static int cs35l56_hda_runtime_resume(struct device *dev) 138 { 139 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 140 int ret; 141 142 ret = cs35l56_runtime_resume_common(&cs35l56->base, false); 143 if (ret < 0) 144 return ret; 145 146 if (cs35l56->cs_dsp.booted) { 147 ret = cs_dsp_run(&cs35l56->cs_dsp); 148 if (ret) { 149 dev_dbg(cs35l56->base.dev, "%s: cs_dsp_run ret %d\n", __func__, ret); 150 goto err; 151 } 152 } 153 154 ret = cs35l56_force_sync_asp1_registers_from_cache(&cs35l56->base); 155 if (ret) 156 goto err; 157 158 return 0; 159 160 err: 161 cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_ALLOW_AUTO_HIBERNATE); 162 regmap_write(cs35l56->base.regmap, CS35L56_DSP_VIRTUAL1_MBOX_1, 163 CS35L56_MBOX_CMD_HIBERNATE_NOW); 164 165 regcache_cache_only(cs35l56->base.regmap, true); 166 167 return ret; 168 } 169 170 static int cs35l56_hda_mixer_info(struct snd_kcontrol *kcontrol, 171 struct snd_ctl_elem_info *uinfo) 172 { 173 uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED; 174 uinfo->count = 1; 175 uinfo->value.enumerated.items = CS35L56_NUM_INPUT_SRC; 176 if (uinfo->value.enumerated.item >= CS35L56_NUM_INPUT_SRC) 177 uinfo->value.enumerated.item = CS35L56_NUM_INPUT_SRC - 1; 178 strscpy(uinfo->value.enumerated.name, cs35l56_tx_input_texts[uinfo->value.enumerated.item], 179 sizeof(uinfo->value.enumerated.name)); 180 181 return 0; 182 } 183 184 static int cs35l56_hda_mixer_get(struct snd_kcontrol *kcontrol, 185 struct snd_ctl_elem_value *ucontrol) 186 { 187 struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data; 188 unsigned int reg_val; 189 int i; 190 191 cs35l56_hda_wait_dsp_ready(cs35l56); 192 193 regmap_read(cs35l56->base.regmap, kcontrol->private_value, ®_val); 194 reg_val &= CS35L56_ASP_TXn_SRC_MASK; 195 196 for (i = 0; i < CS35L56_NUM_INPUT_SRC; ++i) { 197 if (cs35l56_tx_input_values[i] == reg_val) { 198 ucontrol->value.enumerated.item[0] = i; 199 break; 200 } 201 } 202 203 return 0; 204 } 205 206 static int cs35l56_hda_mixer_put(struct snd_kcontrol *kcontrol, 207 struct snd_ctl_elem_value *ucontrol) 208 { 209 struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data; 210 unsigned int item = ucontrol->value.enumerated.item[0]; 211 bool changed; 212 213 if (item >= CS35L56_NUM_INPUT_SRC) 214 return -EINVAL; 215 216 cs35l56_hda_wait_dsp_ready(cs35l56); 217 218 regmap_update_bits_check(cs35l56->base.regmap, kcontrol->private_value, 219 CS35L56_INPUT_MASK, cs35l56_tx_input_values[item], 220 &changed); 221 222 return changed; 223 } 224 225 static int cs35l56_hda_posture_info(struct snd_kcontrol *kcontrol, 226 struct snd_ctl_elem_info *uinfo) 227 { 228 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 229 uinfo->count = 1; 230 uinfo->value.integer.min = CS35L56_MAIN_POSTURE_MIN; 231 uinfo->value.integer.max = CS35L56_MAIN_POSTURE_MAX; 232 return 0; 233 } 234 235 static int cs35l56_hda_posture_get(struct snd_kcontrol *kcontrol, 236 struct snd_ctl_elem_value *ucontrol) 237 { 238 struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data; 239 unsigned int pos; 240 int ret; 241 242 cs35l56_hda_wait_dsp_ready(cs35l56); 243 244 ret = regmap_read(cs35l56->base.regmap, CS35L56_MAIN_POSTURE_NUMBER, &pos); 245 if (ret) 246 return ret; 247 248 ucontrol->value.integer.value[0] = pos; 249 250 return 0; 251 } 252 253 static int cs35l56_hda_posture_put(struct snd_kcontrol *kcontrol, 254 struct snd_ctl_elem_value *ucontrol) 255 { 256 struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data; 257 unsigned long pos = ucontrol->value.integer.value[0]; 258 bool changed; 259 int ret; 260 261 if ((pos < CS35L56_MAIN_POSTURE_MIN) || 262 (pos > CS35L56_MAIN_POSTURE_MAX)) 263 return -EINVAL; 264 265 cs35l56_hda_wait_dsp_ready(cs35l56); 266 267 ret = regmap_update_bits_check(cs35l56->base.regmap, 268 CS35L56_MAIN_POSTURE_NUMBER, 269 CS35L56_MAIN_POSTURE_MASK, 270 pos, &changed); 271 if (ret) 272 return ret; 273 274 return changed; 275 } 276 277 static const struct { 278 const char *name; 279 unsigned int reg; 280 } cs35l56_hda_mixer_controls[] = { 281 { "ASP1 TX1 Source", CS35L56_ASP1TX1_INPUT }, 282 { "ASP1 TX2 Source", CS35L56_ASP1TX2_INPUT }, 283 { "ASP1 TX3 Source", CS35L56_ASP1TX3_INPUT }, 284 { "ASP1 TX4 Source", CS35L56_ASP1TX4_INPUT }, 285 }; 286 287 static const DECLARE_TLV_DB_SCALE(cs35l56_hda_vol_tlv, -10000, 25, 0); 288 289 static int cs35l56_hda_vol_info(struct snd_kcontrol *kcontrol, 290 struct snd_ctl_elem_info *uinfo) 291 { 292 uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER; 293 uinfo->count = 1; 294 uinfo->value.integer.step = 1; 295 uinfo->value.integer.min = 0; 296 uinfo->value.integer.max = CS35L56_MAIN_RENDER_USER_VOLUME_MAX - 297 CS35L56_MAIN_RENDER_USER_VOLUME_MIN; 298 299 return 0; 300 } 301 302 static int cs35l56_hda_vol_get(struct snd_kcontrol *kcontrol, 303 struct snd_ctl_elem_value *ucontrol) 304 { 305 struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data; 306 unsigned int raw_vol; 307 int vol; 308 int ret; 309 310 cs35l56_hda_wait_dsp_ready(cs35l56); 311 312 ret = regmap_read(cs35l56->base.regmap, CS35L56_MAIN_RENDER_USER_VOLUME, &raw_vol); 313 314 if (ret) 315 return ret; 316 317 vol = (s16)(raw_vol & 0xFFFF); 318 vol >>= CS35L56_MAIN_RENDER_USER_VOLUME_SHIFT; 319 320 if (vol & BIT(CS35L56_MAIN_RENDER_USER_VOLUME_SIGNBIT)) 321 vol |= ~((int)(BIT(CS35L56_MAIN_RENDER_USER_VOLUME_SIGNBIT) - 1)); 322 323 ucontrol->value.integer.value[0] = vol - CS35L56_MAIN_RENDER_USER_VOLUME_MIN; 324 325 return 0; 326 } 327 328 static int cs35l56_hda_vol_put(struct snd_kcontrol *kcontrol, 329 struct snd_ctl_elem_value *ucontrol) 330 { 331 struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data; 332 long vol = ucontrol->value.integer.value[0]; 333 unsigned int raw_vol; 334 bool changed; 335 int ret; 336 337 if ((vol < 0) || (vol > (CS35L56_MAIN_RENDER_USER_VOLUME_MAX - 338 CS35L56_MAIN_RENDER_USER_VOLUME_MIN))) 339 return -EINVAL; 340 341 raw_vol = (vol + CS35L56_MAIN_RENDER_USER_VOLUME_MIN) << 342 CS35L56_MAIN_RENDER_USER_VOLUME_SHIFT; 343 344 cs35l56_hda_wait_dsp_ready(cs35l56); 345 346 ret = regmap_update_bits_check(cs35l56->base.regmap, 347 CS35L56_MAIN_RENDER_USER_VOLUME, 348 CS35L56_MAIN_RENDER_USER_VOLUME_MASK, 349 raw_vol, &changed); 350 if (ret) 351 return ret; 352 353 return changed; 354 } 355 356 static void cs35l56_hda_create_controls(struct cs35l56_hda *cs35l56) 357 { 358 struct snd_kcontrol_new ctl_template = { 359 .iface = SNDRV_CTL_ELEM_IFACE_MIXER, 360 .access = SNDRV_CTL_ELEM_ACCESS_READWRITE, 361 .info = cs35l56_hda_posture_info, 362 .get = cs35l56_hda_posture_get, 363 .put = cs35l56_hda_posture_put, 364 }; 365 char name[64]; 366 int i; 367 368 snprintf(name, sizeof(name), "%s Posture Number", cs35l56->amp_name); 369 ctl_template.name = name; 370 cs35l56->posture_ctl = snd_ctl_new1(&ctl_template, cs35l56); 371 if (snd_ctl_add(cs35l56->codec->card, cs35l56->posture_ctl)) 372 dev_err(cs35l56->base.dev, "Failed to add KControl: %s\n", ctl_template.name); 373 374 /* Mixer controls */ 375 ctl_template.info = cs35l56_hda_mixer_info; 376 ctl_template.get = cs35l56_hda_mixer_get; 377 ctl_template.put = cs35l56_hda_mixer_put; 378 379 BUILD_BUG_ON(ARRAY_SIZE(cs35l56->mixer_ctl) != ARRAY_SIZE(cs35l56_hda_mixer_controls)); 380 381 for (i = 0; i < ARRAY_SIZE(cs35l56_hda_mixer_controls); ++i) { 382 snprintf(name, sizeof(name), "%s %s", cs35l56->amp_name, 383 cs35l56_hda_mixer_controls[i].name); 384 ctl_template.private_value = cs35l56_hda_mixer_controls[i].reg; 385 cs35l56->mixer_ctl[i] = snd_ctl_new1(&ctl_template, cs35l56); 386 if (snd_ctl_add(cs35l56->codec->card, cs35l56->mixer_ctl[i])) { 387 dev_err(cs35l56->base.dev, "Failed to add KControl: %s\n", 388 ctl_template.name); 389 } 390 } 391 392 ctl_template.info = cs35l56_hda_vol_info; 393 ctl_template.get = cs35l56_hda_vol_get; 394 ctl_template.put = cs35l56_hda_vol_put; 395 ctl_template.access = (SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ); 396 ctl_template.tlv.p = cs35l56_hda_vol_tlv; 397 snprintf(name, sizeof(name), "%s Speaker Playback Volume", cs35l56->amp_name); 398 ctl_template.name = name; 399 cs35l56->volume_ctl = snd_ctl_new1(&ctl_template, cs35l56); 400 if (snd_ctl_add(cs35l56->codec->card, cs35l56->volume_ctl)) 401 dev_err(cs35l56->base.dev, "Failed to add KControl: %s\n", ctl_template.name); 402 } 403 404 static void cs35l56_hda_remove_controls(struct cs35l56_hda *cs35l56) 405 { 406 int i; 407 408 for (i = ARRAY_SIZE(cs35l56->mixer_ctl) - 1; i >= 0; i--) 409 snd_ctl_remove(cs35l56->codec->card, cs35l56->mixer_ctl[i]); 410 411 snd_ctl_remove(cs35l56->codec->card, cs35l56->posture_ctl); 412 snd_ctl_remove(cs35l56->codec->card, cs35l56->volume_ctl); 413 } 414 415 static const struct cs_dsp_client_ops cs35l56_hda_client_ops = { 416 .control_remove = hda_cs_dsp_control_remove, 417 }; 418 419 static int cs35l56_hda_request_firmware_file(struct cs35l56_hda *cs35l56, 420 const struct firmware **firmware, char **filename, 421 const char *base_name, const char *system_name, 422 const char *amp_name, 423 const char *filetype) 424 { 425 char *s, c; 426 int ret = 0; 427 428 if (system_name && amp_name) 429 *filename = kasprintf(GFP_KERNEL, "%s-%s-%s.%s", base_name, 430 system_name, amp_name, filetype); 431 else if (system_name) 432 *filename = kasprintf(GFP_KERNEL, "%s-%s.%s", base_name, 433 system_name, filetype); 434 else 435 *filename = kasprintf(GFP_KERNEL, "%s.%s", base_name, filetype); 436 437 if (!*filename) 438 return -ENOMEM; 439 440 /* 441 * Make sure that filename is lower-case and any non alpha-numeric 442 * characters except full stop and forward slash are replaced with 443 * hyphens. 444 */ 445 s = *filename; 446 while (*s) { 447 c = *s; 448 if (isalnum(c)) 449 *s = tolower(c); 450 else if (c != '.' && c != '/') 451 *s = '-'; 452 s++; 453 } 454 455 ret = firmware_request_nowarn(firmware, *filename, cs35l56->base.dev); 456 if (ret) { 457 dev_dbg(cs35l56->base.dev, "Failed to request '%s'\n", *filename); 458 kfree(*filename); 459 *filename = NULL; 460 return ret; 461 } 462 463 dev_dbg(cs35l56->base.dev, "Found '%s'\n", *filename); 464 465 return 0; 466 } 467 468 static void cs35l56_hda_request_firmware_files(struct cs35l56_hda *cs35l56, 469 unsigned int preloaded_fw_ver, 470 const struct firmware **wmfw_firmware, 471 char **wmfw_filename, 472 const struct firmware **coeff_firmware, 473 char **coeff_filename) 474 { 475 const char *system_name = cs35l56->system_name; 476 const char *amp_name = cs35l56->amp_name; 477 char base_name[37]; 478 int ret; 479 480 if (preloaded_fw_ver) { 481 snprintf(base_name, sizeof(base_name), 482 "cirrus/cs35l%02x-%02x%s-%06x-dsp1-misc", 483 cs35l56->base.type, 484 cs35l56->base.rev, 485 cs35l56->base.secured ? "-s" : "", 486 preloaded_fw_ver & 0xffffff); 487 } else { 488 snprintf(base_name, sizeof(base_name), 489 "cirrus/cs35l%02x-%02x%s-dsp1-misc", 490 cs35l56->base.type, 491 cs35l56->base.rev, 492 cs35l56->base.secured ? "-s" : ""); 493 } 494 495 if (system_name && amp_name) { 496 if (!cs35l56_hda_request_firmware_file(cs35l56, wmfw_firmware, wmfw_filename, 497 base_name, system_name, amp_name, "wmfw")) { 498 cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename, 499 base_name, system_name, amp_name, "bin"); 500 return; 501 } 502 } 503 504 if (system_name) { 505 if (!cs35l56_hda_request_firmware_file(cs35l56, wmfw_firmware, wmfw_filename, 506 base_name, system_name, NULL, "wmfw")) { 507 if (amp_name) 508 cs35l56_hda_request_firmware_file(cs35l56, 509 coeff_firmware, coeff_filename, 510 base_name, system_name, 511 amp_name, "bin"); 512 if (!*coeff_firmware) 513 cs35l56_hda_request_firmware_file(cs35l56, 514 coeff_firmware, coeff_filename, 515 base_name, system_name, 516 NULL, "bin"); 517 return; 518 } 519 520 /* 521 * Check for system-specific bin files without wmfw before 522 * falling back to generic firmware 523 */ 524 if (amp_name) 525 cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename, 526 base_name, system_name, amp_name, "bin"); 527 if (!*coeff_firmware) 528 cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename, 529 base_name, system_name, NULL, "bin"); 530 531 if (*coeff_firmware) 532 return; 533 } 534 535 ret = cs35l56_hda_request_firmware_file(cs35l56, wmfw_firmware, wmfw_filename, 536 base_name, NULL, NULL, "wmfw"); 537 if (!ret) { 538 cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename, 539 base_name, NULL, NULL, "bin"); 540 return; 541 } 542 543 if (!*coeff_firmware) 544 cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename, 545 base_name, NULL, NULL, "bin"); 546 } 547 548 static void cs35l56_hda_release_firmware_files(const struct firmware *wmfw_firmware, 549 char *wmfw_filename, 550 const struct firmware *coeff_firmware, 551 char *coeff_filename) 552 { 553 if (wmfw_firmware) 554 release_firmware(wmfw_firmware); 555 kfree(wmfw_filename); 556 557 if (coeff_firmware) 558 release_firmware(coeff_firmware); 559 kfree(coeff_filename); 560 } 561 562 static void cs35l56_hda_create_dsp_controls_work(struct work_struct *work) 563 { 564 struct cs35l56_hda *cs35l56 = container_of(work, struct cs35l56_hda, control_work); 565 struct hda_cs_dsp_ctl_info info; 566 567 info.device_name = cs35l56->amp_name; 568 info.fw_type = HDA_CS_DSP_FW_MISC; 569 info.card = cs35l56->codec->card; 570 571 hda_cs_dsp_add_controls(&cs35l56->cs_dsp, &info); 572 } 573 574 static void cs35l56_hda_apply_calibration(struct cs35l56_hda *cs35l56) 575 { 576 int ret; 577 578 if (!cs35l56->base.cal_data_valid || cs35l56->base.secured) 579 return; 580 581 ret = cs_amp_write_cal_coeffs(&cs35l56->cs_dsp, 582 &cs35l56_calibration_controls, 583 &cs35l56->base.cal_data); 584 if (ret < 0) 585 dev_warn(cs35l56->base.dev, "Failed to write calibration: %d\n", ret); 586 else 587 dev_info(cs35l56->base.dev, "Calibration applied\n"); 588 } 589 590 static void cs35l56_hda_fw_load(struct cs35l56_hda *cs35l56) 591 { 592 const struct firmware *coeff_firmware = NULL; 593 const struct firmware *wmfw_firmware = NULL; 594 char *coeff_filename = NULL; 595 char *wmfw_filename = NULL; 596 unsigned int preloaded_fw_ver; 597 bool firmware_missing; 598 bool add_dsp_controls_required = false; 599 int ret; 600 601 /* 602 * control_work must be flushed before proceeding, but we can't do that 603 * here as it would create a deadlock on controls_rwsem so it must be 604 * performed before queuing dsp_work. 605 */ 606 WARN_ON_ONCE(work_busy(&cs35l56->control_work)); 607 608 /* 609 * Prepare for a new DSP power-up. If the DSP has had firmware 610 * downloaded previously then it needs to be powered down so that it 611 * can be updated and if hadn't been patched before then the controls 612 * will need to be added once firmware download succeeds. 613 */ 614 if (cs35l56->base.fw_patched) 615 cs_dsp_power_down(&cs35l56->cs_dsp); 616 else 617 add_dsp_controls_required = true; 618 619 cs35l56->base.fw_patched = false; 620 621 ret = pm_runtime_resume_and_get(cs35l56->base.dev); 622 if (ret < 0) { 623 dev_err(cs35l56->base.dev, "Failed to resume and get %d\n", ret); 624 return; 625 } 626 627 /* 628 * The firmware can only be upgraded if it is currently running 629 * from the built-in ROM. If not, the wmfw/bin must be for the 630 * version of firmware that is running on the chip. 631 */ 632 ret = cs35l56_read_prot_status(&cs35l56->base, &firmware_missing, &preloaded_fw_ver); 633 if (ret) 634 goto err_pm_put; 635 636 if (firmware_missing) 637 preloaded_fw_ver = 0; 638 639 cs35l56_hda_request_firmware_files(cs35l56, preloaded_fw_ver, 640 &wmfw_firmware, &wmfw_filename, 641 &coeff_firmware, &coeff_filename); 642 643 /* 644 * If the BIOS didn't patch the firmware a bin file is mandatory to 645 * enable the ASP· 646 */ 647 if (!coeff_firmware && firmware_missing) { 648 dev_err(cs35l56->base.dev, ".bin file required but not found\n"); 649 goto err_fw_release; 650 } 651 652 mutex_lock(&cs35l56->base.irq_lock); 653 654 /* 655 * If the firmware hasn't been patched it must be shutdown before 656 * doing a full patch and reset afterwards. If it is already 657 * running a patched version the firmware files only contain 658 * tunings and we can use the lower cost reinit sequence instead. 659 */ 660 if (firmware_missing && (wmfw_firmware || coeff_firmware)) { 661 ret = cs35l56_firmware_shutdown(&cs35l56->base); 662 if (ret) 663 goto err; 664 } 665 666 ret = cs_dsp_power_up(&cs35l56->cs_dsp, wmfw_firmware, wmfw_filename, 667 coeff_firmware, coeff_filename, "misc"); 668 if (ret) { 669 dev_dbg(cs35l56->base.dev, "%s: cs_dsp_power_up ret %d\n", __func__, ret); 670 goto err; 671 } 672 673 if (wmfw_filename) 674 dev_dbg(cs35l56->base.dev, "Loaded WMFW Firmware: %s\n", wmfw_filename); 675 676 if (coeff_filename) 677 dev_dbg(cs35l56->base.dev, "Loaded Coefficients: %s\n", coeff_filename); 678 679 /* If we downloaded firmware, reset the device and wait for it to boot */ 680 if (firmware_missing && (wmfw_firmware || coeff_firmware)) { 681 cs35l56_system_reset(&cs35l56->base, false); 682 regcache_mark_dirty(cs35l56->base.regmap); 683 ret = cs35l56_wait_for_firmware_boot(&cs35l56->base); 684 if (ret) 685 goto err_powered_up; 686 687 regcache_cache_only(cs35l56->base.regmap, false); 688 } 689 690 /* Disable auto-hibernate so that runtime_pm has control */ 691 ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE); 692 if (ret) 693 goto err_powered_up; 694 695 regcache_sync(cs35l56->base.regmap); 696 697 regmap_clear_bits(cs35l56->base.regmap, CS35L56_PROTECTION_STATUS, 698 CS35L56_FIRMWARE_MISSING); 699 cs35l56->base.fw_patched = true; 700 701 /* 702 * Adding controls is deferred to prevent a lock inversion - ALSA takes 703 * the controls_rwsem when adding a control, the get() / put() 704 * functions of a control are called holding controls_rwsem and those 705 * that depend on running firmware wait for dsp_work() to complete. 706 */ 707 if (add_dsp_controls_required) 708 queue_work(system_long_wq, &cs35l56->control_work); 709 710 ret = cs_dsp_run(&cs35l56->cs_dsp); 711 if (ret) 712 dev_dbg(cs35l56->base.dev, "%s: cs_dsp_run ret %d\n", __func__, ret); 713 714 cs35l56_hda_apply_calibration(cs35l56); 715 ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT); 716 if (ret) 717 cs_dsp_stop(&cs35l56->cs_dsp); 718 719 err_powered_up: 720 if (!cs35l56->base.fw_patched) 721 cs_dsp_power_down(&cs35l56->cs_dsp); 722 err: 723 mutex_unlock(&cs35l56->base.irq_lock); 724 err_fw_release: 725 cs35l56_hda_release_firmware_files(wmfw_firmware, wmfw_filename, 726 coeff_firmware, coeff_filename); 727 err_pm_put: 728 pm_runtime_put(cs35l56->base.dev); 729 } 730 731 static void cs35l56_hda_dsp_work(struct work_struct *work) 732 { 733 struct cs35l56_hda *cs35l56 = container_of(work, struct cs35l56_hda, dsp_work); 734 735 cs35l56_hda_fw_load(cs35l56); 736 } 737 738 static int cs35l56_hda_bind(struct device *dev, struct device *master, void *master_data) 739 { 740 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 741 struct hda_component_parent *parent = master_data; 742 struct hda_component *comp; 743 744 comp = hda_component_from_index(parent, cs35l56->index); 745 if (!comp) 746 return -EINVAL; 747 748 if (comp->dev) 749 return -EBUSY; 750 751 comp->dev = dev; 752 cs35l56->codec = parent->codec; 753 strscpy(comp->name, dev_name(dev), sizeof(comp->name)); 754 comp->playback_hook = cs35l56_hda_playback_hook; 755 756 flush_work(&cs35l56->control_work); 757 queue_work(system_long_wq, &cs35l56->dsp_work); 758 759 cs35l56_hda_create_controls(cs35l56); 760 761 #if IS_ENABLED(CONFIG_SND_DEBUG) 762 cs35l56->debugfs_root = debugfs_create_dir(dev_name(cs35l56->base.dev), sound_debugfs_root); 763 cs_dsp_init_debugfs(&cs35l56->cs_dsp, cs35l56->debugfs_root); 764 #endif 765 766 dev_dbg(cs35l56->base.dev, "Bound\n"); 767 768 return 0; 769 } 770 771 static void cs35l56_hda_unbind(struct device *dev, struct device *master, void *master_data) 772 { 773 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 774 struct hda_component_parent *parent = master_data; 775 struct hda_component *comp; 776 777 cancel_work_sync(&cs35l56->dsp_work); 778 cancel_work_sync(&cs35l56->control_work); 779 780 cs35l56_hda_remove_controls(cs35l56); 781 782 #if IS_ENABLED(CONFIG_SND_DEBUG) 783 cs_dsp_cleanup_debugfs(&cs35l56->cs_dsp); 784 debugfs_remove_recursive(cs35l56->debugfs_root); 785 #endif 786 787 if (cs35l56->base.fw_patched) 788 cs_dsp_power_down(&cs35l56->cs_dsp); 789 790 comp = hda_component_from_index(parent, cs35l56->index); 791 if (comp && (comp->dev == dev)) 792 memset(comp, 0, sizeof(*comp)); 793 794 cs35l56->codec = NULL; 795 796 dev_dbg(cs35l56->base.dev, "Unbound\n"); 797 } 798 799 static const struct component_ops cs35l56_hda_comp_ops = { 800 .bind = cs35l56_hda_bind, 801 .unbind = cs35l56_hda_unbind, 802 }; 803 804 static int cs35l56_hda_system_suspend(struct device *dev) 805 { 806 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 807 808 cs35l56_hda_wait_dsp_ready(cs35l56); 809 flush_work(&cs35l56->control_work); 810 811 if (cs35l56->playing) 812 cs35l56_hda_pause(cs35l56); 813 814 cs35l56->suspended = true; 815 816 /* 817 * The interrupt line is normally shared, but after we start suspending 818 * we can't check if our device is the source of an interrupt, and can't 819 * clear it. Prevent this race by temporarily disabling the parent irq 820 * until we reach _no_irq. 821 */ 822 if (cs35l56->base.irq) 823 disable_irq(cs35l56->base.irq); 824 825 return pm_runtime_force_suspend(dev); 826 } 827 828 static int cs35l56_hda_system_suspend_late(struct device *dev) 829 { 830 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 831 832 /* 833 * RESET is usually shared by all amps so it must not be asserted until 834 * all driver instances have done their suspend() stage. 835 */ 836 if (cs35l56->base.reset_gpio) { 837 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 838 cs35l56_wait_min_reset_pulse(); 839 } 840 841 return 0; 842 } 843 844 static int cs35l56_hda_system_suspend_no_irq(struct device *dev) 845 { 846 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 847 848 /* Handlers are now disabled so the parent IRQ can safely be re-enabled. */ 849 if (cs35l56->base.irq) 850 enable_irq(cs35l56->base.irq); 851 852 return 0; 853 } 854 855 static int cs35l56_hda_system_resume_no_irq(struct device *dev) 856 { 857 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 858 859 /* 860 * WAKE interrupts unmask if the CS35L56 hibernates, which can cause 861 * spurious interrupts, and the interrupt line is normally shared. 862 * We can't check if our device is the source of an interrupt, and can't 863 * clear it, until it has fully resumed. Prevent this race by temporarily 864 * disabling the parent irq until we complete resume(). 865 */ 866 if (cs35l56->base.irq) 867 disable_irq(cs35l56->base.irq); 868 869 return 0; 870 } 871 872 static int cs35l56_hda_system_resume_early(struct device *dev) 873 { 874 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 875 876 /* Ensure a spec-compliant RESET pulse. */ 877 if (cs35l56->base.reset_gpio) { 878 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 879 cs35l56_wait_min_reset_pulse(); 880 881 /* Release shared RESET before drivers start resume(). */ 882 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1); 883 cs35l56_wait_control_port_ready(); 884 } 885 886 return 0; 887 } 888 889 static int cs35l56_hda_system_resume(struct device *dev) 890 { 891 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 892 int ret; 893 894 /* Undo pm_runtime_force_suspend() before re-enabling the irq */ 895 ret = pm_runtime_force_resume(dev); 896 if (cs35l56->base.irq) 897 enable_irq(cs35l56->base.irq); 898 899 if (ret) 900 return ret; 901 902 cs35l56->suspended = false; 903 904 if (!cs35l56->codec) 905 return 0; 906 907 ret = cs35l56_is_fw_reload_needed(&cs35l56->base); 908 dev_dbg(cs35l56->base.dev, "fw_reload_needed: %d\n", ret); 909 if (ret > 0) 910 queue_work(system_long_wq, &cs35l56->dsp_work); 911 912 if (cs35l56->playing) 913 cs35l56_hda_play(cs35l56); 914 915 return 0; 916 } 917 918 static int cs35l56_hda_read_acpi(struct cs35l56_hda *cs35l56, int hid, int id) 919 { 920 u32 values[HDA_MAX_COMPONENTS]; 921 char hid_string[8]; 922 struct acpi_device *adev; 923 const char *property, *sub; 924 size_t nval; 925 int i, ret; 926 927 /* 928 * ACPI_COMPANION isn't available when this driver was instantiated by 929 * the serial-multi-instantiate driver, so lookup the node by HID 930 */ 931 if (!ACPI_COMPANION(cs35l56->base.dev)) { 932 snprintf(hid_string, sizeof(hid_string), "CSC%04X", hid); 933 adev = acpi_dev_get_first_match_dev(hid_string, NULL, -1); 934 if (!adev) { 935 dev_err(cs35l56->base.dev, "Failed to find an ACPI device for %s\n", 936 dev_name(cs35l56->base.dev)); 937 return -ENODEV; 938 } 939 ACPI_COMPANION_SET(cs35l56->base.dev, adev); 940 } 941 942 property = "cirrus,dev-index"; 943 ret = device_property_count_u32(cs35l56->base.dev, property); 944 if (ret <= 0) 945 goto err; 946 947 if (ret > ARRAY_SIZE(values)) { 948 ret = -EINVAL; 949 goto err; 950 } 951 nval = ret; 952 953 ret = device_property_read_u32_array(cs35l56->base.dev, property, values, nval); 954 if (ret) 955 goto err; 956 957 cs35l56->index = -1; 958 for (i = 0; i < nval; i++) { 959 if (values[i] == id) { 960 cs35l56->index = i; 961 break; 962 } 963 } 964 /* 965 * It's not an error for the ID to be missing: for I2C there can be 966 * an alias address that is not a real device. So reject silently. 967 */ 968 if (cs35l56->index == -1) { 969 dev_dbg(cs35l56->base.dev, "No index found in %s\n", property); 970 ret = -ENODEV; 971 goto err; 972 } 973 974 sub = acpi_get_subsystem_id(ACPI_HANDLE(cs35l56->base.dev)); 975 976 if (IS_ERR(sub)) { 977 dev_info(cs35l56->base.dev, 978 "Read ACPI _SUB failed(%ld): fallback to generic firmware\n", 979 PTR_ERR(sub)); 980 } else { 981 ret = cirrus_scodec_get_speaker_id(cs35l56->base.dev, cs35l56->index, nval, -1); 982 if (ret == -ENOENT) { 983 cs35l56->system_name = sub; 984 } else if (ret >= 0) { 985 cs35l56->system_name = kasprintf(GFP_KERNEL, "%s-spkid%d", sub, ret); 986 kfree(sub); 987 if (!cs35l56->system_name) 988 return -ENOMEM; 989 } else { 990 return ret; 991 } 992 } 993 994 cs35l56->base.reset_gpio = devm_gpiod_get_index_optional(cs35l56->base.dev, 995 "reset", 996 cs35l56->index, 997 GPIOD_OUT_LOW); 998 if (IS_ERR(cs35l56->base.reset_gpio)) { 999 ret = PTR_ERR(cs35l56->base.reset_gpio); 1000 1001 /* 1002 * If RESET is shared the first amp to probe will grab the reset 1003 * line and reset all the amps 1004 */ 1005 if (ret != -EBUSY) 1006 return dev_err_probe(cs35l56->base.dev, ret, "Failed to get reset GPIO\n"); 1007 1008 dev_info(cs35l56->base.dev, "Reset GPIO busy, assume shared reset\n"); 1009 cs35l56->base.reset_gpio = NULL; 1010 } 1011 1012 return 0; 1013 1014 err: 1015 if (ret != -ENODEV) 1016 dev_err(cs35l56->base.dev, "Failed property %s: %d\n", property, ret); 1017 1018 return ret; 1019 } 1020 1021 int cs35l56_hda_common_probe(struct cs35l56_hda *cs35l56, int hid, int id) 1022 { 1023 int ret; 1024 1025 mutex_init(&cs35l56->base.irq_lock); 1026 dev_set_drvdata(cs35l56->base.dev, cs35l56); 1027 1028 INIT_WORK(&cs35l56->dsp_work, cs35l56_hda_dsp_work); 1029 INIT_WORK(&cs35l56->control_work, cs35l56_hda_create_dsp_controls_work); 1030 1031 ret = cs35l56_hda_read_acpi(cs35l56, hid, id); 1032 if (ret) 1033 goto err; 1034 1035 cs35l56->amp_name = devm_kasprintf(cs35l56->base.dev, GFP_KERNEL, "AMP%d", 1036 cs35l56->index + 1); 1037 if (!cs35l56->amp_name) { 1038 ret = -ENOMEM; 1039 goto err; 1040 } 1041 1042 cs35l56->base.cal_index = cs35l56->index; 1043 1044 cs35l56_init_cs_dsp(&cs35l56->base, &cs35l56->cs_dsp); 1045 cs35l56->cs_dsp.client_ops = &cs35l56_hda_client_ops; 1046 1047 if (cs35l56->base.reset_gpio) { 1048 dev_dbg(cs35l56->base.dev, "Hard reset\n"); 1049 1050 /* 1051 * The GPIOD_OUT_LOW to *_gpiod_get_*() will be ignored if the 1052 * ACPI defines a different default state. So explicitly set low. 1053 */ 1054 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 1055 cs35l56_wait_min_reset_pulse(); 1056 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1); 1057 } 1058 1059 ret = cs35l56_hw_init(&cs35l56->base); 1060 if (ret < 0) 1061 goto err; 1062 1063 /* Reset the device and wait for it to boot */ 1064 cs35l56_system_reset(&cs35l56->base, false); 1065 ret = cs35l56_wait_for_firmware_boot(&cs35l56->base); 1066 if (ret) 1067 goto err; 1068 1069 regcache_cache_only(cs35l56->base.regmap, false); 1070 1071 ret = cs35l56_set_patch(&cs35l56->base); 1072 if (ret) 1073 goto err; 1074 1075 regcache_mark_dirty(cs35l56->base.regmap); 1076 regcache_sync(cs35l56->base.regmap); 1077 1078 /* Disable auto-hibernate so that runtime_pm has control */ 1079 ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE); 1080 if (ret) 1081 goto err; 1082 1083 ret = cs35l56_get_calibration(&cs35l56->base); 1084 if (ret) 1085 goto err; 1086 1087 ret = cs_dsp_halo_init(&cs35l56->cs_dsp); 1088 if (ret) { 1089 dev_err_probe(cs35l56->base.dev, ret, "cs_dsp_halo_init failed\n"); 1090 goto err; 1091 } 1092 1093 dev_info(cs35l56->base.dev, "DSP system name: '%s', amp name: '%s'\n", 1094 cs35l56->system_name, cs35l56->amp_name); 1095 1096 regmap_multi_reg_write(cs35l56->base.regmap, cs35l56_hda_dai_config, 1097 ARRAY_SIZE(cs35l56_hda_dai_config)); 1098 ret = cs35l56_force_sync_asp1_registers_from_cache(&cs35l56->base); 1099 if (ret) 1100 goto dsp_err; 1101 1102 /* 1103 * By default only enable one ASP1TXn, where n=amplifier index, 1104 * This prevents multiple amps trying to drive the same slot. 1105 */ 1106 cs35l56->asp_tx_mask = BIT(cs35l56->index); 1107 1108 pm_runtime_set_autosuspend_delay(cs35l56->base.dev, 3000); 1109 pm_runtime_use_autosuspend(cs35l56->base.dev); 1110 pm_runtime_set_active(cs35l56->base.dev); 1111 pm_runtime_mark_last_busy(cs35l56->base.dev); 1112 pm_runtime_enable(cs35l56->base.dev); 1113 1114 cs35l56->base.init_done = true; 1115 1116 ret = component_add(cs35l56->base.dev, &cs35l56_hda_comp_ops); 1117 if (ret) { 1118 dev_err(cs35l56->base.dev, "Register component failed: %d\n", ret); 1119 goto pm_err; 1120 } 1121 1122 return 0; 1123 1124 pm_err: 1125 pm_runtime_disable(cs35l56->base.dev); 1126 dsp_err: 1127 cs_dsp_remove(&cs35l56->cs_dsp); 1128 err: 1129 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 1130 1131 return ret; 1132 } 1133 EXPORT_SYMBOL_NS_GPL(cs35l56_hda_common_probe, SND_HDA_SCODEC_CS35L56); 1134 1135 void cs35l56_hda_remove(struct device *dev) 1136 { 1137 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 1138 1139 component_del(cs35l56->base.dev, &cs35l56_hda_comp_ops); 1140 1141 pm_runtime_dont_use_autosuspend(cs35l56->base.dev); 1142 pm_runtime_get_sync(cs35l56->base.dev); 1143 pm_runtime_disable(cs35l56->base.dev); 1144 1145 cs_dsp_remove(&cs35l56->cs_dsp); 1146 1147 kfree(cs35l56->system_name); 1148 pm_runtime_put_noidle(cs35l56->base.dev); 1149 1150 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 1151 } 1152 EXPORT_SYMBOL_NS_GPL(cs35l56_hda_remove, SND_HDA_SCODEC_CS35L56); 1153 1154 const struct dev_pm_ops cs35l56_hda_pm_ops = { 1155 RUNTIME_PM_OPS(cs35l56_hda_runtime_suspend, cs35l56_hda_runtime_resume, NULL) 1156 SYSTEM_SLEEP_PM_OPS(cs35l56_hda_system_suspend, cs35l56_hda_system_resume) 1157 LATE_SYSTEM_SLEEP_PM_OPS(cs35l56_hda_system_suspend_late, 1158 cs35l56_hda_system_resume_early) 1159 NOIRQ_SYSTEM_SLEEP_PM_OPS(cs35l56_hda_system_suspend_no_irq, 1160 cs35l56_hda_system_resume_no_irq) 1161 }; 1162 EXPORT_SYMBOL_NS_GPL(cs35l56_hda_pm_ops, SND_HDA_SCODEC_CS35L56); 1163 1164 MODULE_DESCRIPTION("CS35L56 HDA Driver"); 1165 MODULE_IMPORT_NS(FW_CS_DSP); 1166 MODULE_IMPORT_NS(SND_HDA_CIRRUS_SCODEC); 1167 MODULE_IMPORT_NS(SND_HDA_CS_DSP_CONTROLS); 1168 MODULE_IMPORT_NS(SND_SOC_CS35L56_SHARED); 1169 MODULE_IMPORT_NS(SND_SOC_CS_AMP_LIB); 1170 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>"); 1171 MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>"); 1172 MODULE_LICENSE("GPL"); 1173