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