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