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