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 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 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 0; 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 /* 559 * If the BIOS didn't patch the firmware a bin file is mandatory to 560 * enable the ASP· 561 */ 562 if (!coeff_firmware && firmware_missing) { 563 dev_err(cs35l56->base.dev, ".bin file required but not found\n"); 564 ret = -ENOENT; 565 goto err_fw_release; 566 } 567 568 mutex_lock(&cs35l56->base.irq_lock); 569 570 /* 571 * When the device is running in secure mode the firmware files can 572 * only contain insecure tunings and therefore we do not need to 573 * shutdown the firmware to apply them and can use the lower cost 574 * reinit sequence instead. 575 */ 576 if (!cs35l56->base.secured && (wmfw_firmware || coeff_firmware)) { 577 ret = cs35l56_firmware_shutdown(&cs35l56->base); 578 if (ret) 579 goto err; 580 } 581 582 ret = cs_dsp_power_up(&cs35l56->cs_dsp, wmfw_firmware, wmfw_filename, 583 coeff_firmware, coeff_filename, "misc"); 584 if (ret) { 585 dev_dbg(cs35l56->base.dev, "%s: cs_dsp_power_up ret %d\n", __func__, ret); 586 goto err; 587 } 588 589 if (wmfw_filename) 590 dev_dbg(cs35l56->base.dev, "Loaded WMFW Firmware: %s\n", wmfw_filename); 591 592 if (coeff_filename) 593 dev_dbg(cs35l56->base.dev, "Loaded Coefficients: %s\n", coeff_filename); 594 595 if (cs35l56->base.secured) { 596 ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT); 597 if (ret) 598 goto err_powered_up; 599 } else if (wmfw_firmware || coeff_firmware) { 600 /* If we downloaded firmware, reset the device and wait for it to boot */ 601 cs35l56_system_reset(&cs35l56->base, false); 602 regcache_mark_dirty(cs35l56->base.regmap); 603 ret = cs35l56_wait_for_firmware_boot(&cs35l56->base); 604 if (ret) 605 goto err_powered_up; 606 } 607 608 /* Disable auto-hibernate so that runtime_pm has control */ 609 ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE); 610 if (ret) 611 goto err_powered_up; 612 613 regcache_sync(cs35l56->base.regmap); 614 615 regmap_clear_bits(cs35l56->base.regmap, CS35L56_PROTECTION_STATUS, 616 CS35L56_FIRMWARE_MISSING); 617 cs35l56->base.fw_patched = true; 618 619 ret = cs_dsp_run(&cs35l56->cs_dsp); 620 if (ret) 621 dev_dbg(cs35l56->base.dev, "%s: cs_dsp_run ret %d\n", __func__, ret); 622 623 err_powered_up: 624 if (!cs35l56->base.fw_patched) 625 cs_dsp_power_down(&cs35l56->cs_dsp); 626 err: 627 mutex_unlock(&cs35l56->base.irq_lock); 628 err_fw_release: 629 cs35l56_hda_release_firmware_files(wmfw_firmware, wmfw_filename, 630 coeff_firmware, coeff_filename); 631 err_pm_put: 632 pm_runtime_put(cs35l56->base.dev); 633 634 return ret; 635 } 636 637 static int cs35l56_hda_bind(struct device *dev, struct device *master, void *master_data) 638 { 639 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 640 struct hda_component *comps = master_data; 641 int ret; 642 643 if (!comps || cs35l56->index < 0 || cs35l56->index >= HDA_MAX_COMPONENTS) 644 return -EINVAL; 645 646 comps = &comps[cs35l56->index]; 647 if (comps->dev) 648 return -EBUSY; 649 650 comps->dev = dev; 651 cs35l56->codec = comps->codec; 652 strscpy(comps->name, dev_name(dev), sizeof(comps->name)); 653 comps->playback_hook = cs35l56_hda_playback_hook; 654 655 ret = cs35l56_hda_fw_load(cs35l56); 656 if (ret) 657 return ret; 658 659 cs35l56_hda_create_controls(cs35l56); 660 cs35l56_hda_add_dsp_controls(cs35l56); 661 662 #if IS_ENABLED(CONFIG_SND_DEBUG) 663 cs35l56->debugfs_root = debugfs_create_dir(dev_name(cs35l56->base.dev), sound_debugfs_root); 664 cs_dsp_init_debugfs(&cs35l56->cs_dsp, cs35l56->debugfs_root); 665 #endif 666 667 dev_dbg(cs35l56->base.dev, "Bound\n"); 668 669 return 0; 670 } 671 672 static void cs35l56_hda_unbind(struct device *dev, struct device *master, void *master_data) 673 { 674 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 675 struct hda_component *comps = master_data; 676 677 cs35l56_hda_remove_controls(cs35l56); 678 679 #if IS_ENABLED(CONFIG_SND_DEBUG) 680 cs_dsp_cleanup_debugfs(&cs35l56->cs_dsp); 681 debugfs_remove_recursive(cs35l56->debugfs_root); 682 #endif 683 684 if (cs35l56->base.fw_patched) 685 cs_dsp_power_down(&cs35l56->cs_dsp); 686 687 cs_dsp_remove(&cs35l56->cs_dsp); 688 689 if (comps[cs35l56->index].dev == dev) 690 memset(&comps[cs35l56->index], 0, sizeof(*comps)); 691 692 dev_dbg(cs35l56->base.dev, "Unbound\n"); 693 } 694 695 static const struct component_ops cs35l56_hda_comp_ops = { 696 .bind = cs35l56_hda_bind, 697 .unbind = cs35l56_hda_unbind, 698 }; 699 700 static int cs35l56_hda_system_suspend(struct device *dev) 701 { 702 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 703 704 if (cs35l56->playing) 705 cs35l56_hda_pause(cs35l56); 706 707 cs35l56->suspended = true; 708 709 /* 710 * The interrupt line is normally shared, but after we start suspending 711 * we can't check if our device is the source of an interrupt, and can't 712 * clear it. Prevent this race by temporarily disabling the parent irq 713 * until we reach _no_irq. 714 */ 715 if (cs35l56->base.irq) 716 disable_irq(cs35l56->base.irq); 717 718 return pm_runtime_force_suspend(dev); 719 } 720 721 static int cs35l56_hda_system_suspend_late(struct device *dev) 722 { 723 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 724 725 /* 726 * RESET is usually shared by all amps so it must not be asserted until 727 * all driver instances have done their suspend() stage. 728 */ 729 if (cs35l56->base.reset_gpio) { 730 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 731 cs35l56_wait_min_reset_pulse(); 732 } 733 734 return 0; 735 } 736 737 static int cs35l56_hda_system_suspend_no_irq(struct device *dev) 738 { 739 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 740 741 /* Handlers are now disabled so the parent IRQ can safely be re-enabled. */ 742 if (cs35l56->base.irq) 743 enable_irq(cs35l56->base.irq); 744 745 return 0; 746 } 747 748 static int cs35l56_hda_system_resume_no_irq(struct device *dev) 749 { 750 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 751 752 /* 753 * WAKE interrupts unmask if the CS35L56 hibernates, which can cause 754 * spurious interrupts, and the interrupt line is normally shared. 755 * We can't check if our device is the source of an interrupt, and can't 756 * clear it, until it has fully resumed. Prevent this race by temporarily 757 * disabling the parent irq until we complete resume(). 758 */ 759 if (cs35l56->base.irq) 760 disable_irq(cs35l56->base.irq); 761 762 return 0; 763 } 764 765 static int cs35l56_hda_system_resume_early(struct device *dev) 766 { 767 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 768 769 /* Ensure a spec-compliant RESET pulse. */ 770 if (cs35l56->base.reset_gpio) { 771 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 772 cs35l56_wait_min_reset_pulse(); 773 774 /* Release shared RESET before drivers start resume(). */ 775 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1); 776 cs35l56_wait_control_port_ready(); 777 } 778 779 return 0; 780 } 781 782 static int cs35l56_hda_system_resume(struct device *dev) 783 { 784 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 785 int ret; 786 787 /* Undo pm_runtime_force_suspend() before re-enabling the irq */ 788 ret = pm_runtime_force_resume(dev); 789 if (cs35l56->base.irq) 790 enable_irq(cs35l56->base.irq); 791 792 if (ret) 793 return ret; 794 795 cs35l56->suspended = false; 796 797 ret = cs35l56_is_fw_reload_needed(&cs35l56->base); 798 dev_dbg(cs35l56->base.dev, "fw_reload_needed: %d\n", ret); 799 if (ret > 0) { 800 ret = cs35l56_hda_fw_load(cs35l56); 801 if (ret) 802 return ret; 803 } 804 805 if (cs35l56->playing) 806 cs35l56_hda_play(cs35l56); 807 808 return 0; 809 } 810 811 static int cs35l56_hda_read_acpi(struct cs35l56_hda *cs35l56, int id) 812 { 813 u32 values[HDA_MAX_COMPONENTS]; 814 struct acpi_device *adev; 815 const char *property, *sub; 816 size_t nval; 817 int i, ret; 818 819 /* 820 * ACPI_COMPANION isn't available when this driver was instantiated by 821 * the serial-multi-instantiate driver, so lookup the node by HID 822 */ 823 if (!ACPI_COMPANION(cs35l56->base.dev)) { 824 adev = acpi_dev_get_first_match_dev("CSC3556", NULL, -1); 825 if (!adev) { 826 dev_err(cs35l56->base.dev, "Failed to find an ACPI device for %s\n", 827 dev_name(cs35l56->base.dev)); 828 return -ENODEV; 829 } 830 ACPI_COMPANION_SET(cs35l56->base.dev, adev); 831 } 832 833 property = "cirrus,dev-index"; 834 ret = device_property_count_u32(cs35l56->base.dev, property); 835 if (ret <= 0) 836 goto err; 837 838 if (ret > ARRAY_SIZE(values)) { 839 ret = -EINVAL; 840 goto err; 841 } 842 nval = ret; 843 844 ret = device_property_read_u32_array(cs35l56->base.dev, property, values, nval); 845 if (ret) 846 goto err; 847 848 cs35l56->index = -1; 849 for (i = 0; i < nval; i++) { 850 if (values[i] == id) { 851 cs35l56->index = i; 852 break; 853 } 854 } 855 /* 856 * It's not an error for the ID to be missing: for I2C there can be 857 * an alias address that is not a real device. So reject silently. 858 */ 859 if (cs35l56->index == -1) { 860 dev_dbg(cs35l56->base.dev, "No index found in %s\n", property); 861 ret = -ENODEV; 862 goto err; 863 } 864 865 sub = acpi_get_subsystem_id(ACPI_HANDLE(cs35l56->base.dev)); 866 867 if (IS_ERR(sub)) { 868 dev_info(cs35l56->base.dev, 869 "Read ACPI _SUB failed(%ld): fallback to generic firmware\n", 870 PTR_ERR(sub)); 871 } else { 872 cs35l56->system_name = sub; 873 } 874 875 cs35l56->base.reset_gpio = devm_gpiod_get_index_optional(cs35l56->base.dev, 876 "reset", 877 cs35l56->index, 878 GPIOD_OUT_LOW); 879 if (IS_ERR(cs35l56->base.reset_gpio)) { 880 ret = PTR_ERR(cs35l56->base.reset_gpio); 881 882 /* 883 * If RESET is shared the first amp to probe will grab the reset 884 * line and reset all the amps 885 */ 886 if (ret != -EBUSY) 887 return dev_err_probe(cs35l56->base.dev, ret, "Failed to get reset GPIO\n"); 888 889 dev_info(cs35l56->base.dev, "Reset GPIO busy, assume shared reset\n"); 890 cs35l56->base.reset_gpio = NULL; 891 } 892 893 return 0; 894 895 err: 896 if (ret != -ENODEV) 897 dev_err(cs35l56->base.dev, "Failed property %s: %d\n", property, ret); 898 899 return ret; 900 } 901 902 int cs35l56_hda_common_probe(struct cs35l56_hda *cs35l56, int id) 903 { 904 int ret; 905 906 mutex_init(&cs35l56->base.irq_lock); 907 dev_set_drvdata(cs35l56->base.dev, cs35l56); 908 909 ret = cs35l56_hda_read_acpi(cs35l56, id); 910 if (ret) 911 goto err; 912 913 cs35l56->amp_name = devm_kasprintf(cs35l56->base.dev, GFP_KERNEL, "AMP%d", 914 cs35l56->index + 1); 915 if (!cs35l56->amp_name) { 916 ret = -ENOMEM; 917 goto err; 918 } 919 920 cs35l56_init_cs_dsp(&cs35l56->base, &cs35l56->cs_dsp); 921 cs35l56->cs_dsp.client_ops = &cs35l56_hda_client_ops; 922 923 if (cs35l56->base.reset_gpio) { 924 dev_dbg(cs35l56->base.dev, "Hard reset\n"); 925 926 /* 927 * The GPIOD_OUT_LOW to *_gpiod_get_*() will be ignored if the 928 * ACPI defines a different default state. So explicitly set low. 929 */ 930 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 931 cs35l56_wait_min_reset_pulse(); 932 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1); 933 } 934 935 ret = cs35l56_hw_init(&cs35l56->base); 936 if (ret < 0) 937 goto err; 938 939 /* Reset the device and wait for it to boot */ 940 cs35l56_system_reset(&cs35l56->base, false); 941 ret = cs35l56_wait_for_firmware_boot(&cs35l56->base); 942 if (ret) 943 goto err; 944 945 ret = cs35l56_set_patch(&cs35l56->base); 946 if (ret) 947 goto err; 948 949 regcache_mark_dirty(cs35l56->base.regmap); 950 regcache_sync(cs35l56->base.regmap); 951 952 /* Disable auto-hibernate so that runtime_pm has control */ 953 ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE); 954 if (ret) 955 goto err; 956 957 ret = cs_dsp_halo_init(&cs35l56->cs_dsp); 958 if (ret) { 959 dev_err_probe(cs35l56->base.dev, ret, "cs_dsp_halo_init failed\n"); 960 goto err; 961 } 962 963 dev_dbg(cs35l56->base.dev, "DSP system name: '%s', amp name: '%s'\n", 964 cs35l56->system_name, cs35l56->amp_name); 965 966 regmap_multi_reg_write(cs35l56->base.regmap, cs35l56_hda_dai_config, 967 ARRAY_SIZE(cs35l56_hda_dai_config)); 968 969 /* 970 * By default only enable one ASP1TXn, where n=amplifier index, 971 * This prevents multiple amps trying to drive the same slot. 972 */ 973 cs35l56->asp_tx_mask = BIT(cs35l56->index); 974 975 pm_runtime_set_autosuspend_delay(cs35l56->base.dev, 3000); 976 pm_runtime_use_autosuspend(cs35l56->base.dev); 977 pm_runtime_set_active(cs35l56->base.dev); 978 pm_runtime_mark_last_busy(cs35l56->base.dev); 979 pm_runtime_enable(cs35l56->base.dev); 980 981 ret = component_add(cs35l56->base.dev, &cs35l56_hda_comp_ops); 982 if (ret) { 983 dev_err(cs35l56->base.dev, "Register component failed: %d\n", ret); 984 goto pm_err; 985 } 986 987 cs35l56->base.init_done = true; 988 989 return 0; 990 991 pm_err: 992 pm_runtime_disable(cs35l56->base.dev); 993 err: 994 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 995 996 return ret; 997 } 998 EXPORT_SYMBOL_NS_GPL(cs35l56_hda_common_probe, SND_HDA_SCODEC_CS35L56); 999 1000 void cs35l56_hda_remove(struct device *dev) 1001 { 1002 struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev); 1003 1004 pm_runtime_dont_use_autosuspend(cs35l56->base.dev); 1005 pm_runtime_get_sync(cs35l56->base.dev); 1006 pm_runtime_disable(cs35l56->base.dev); 1007 1008 component_del(cs35l56->base.dev, &cs35l56_hda_comp_ops); 1009 1010 kfree(cs35l56->system_name); 1011 pm_runtime_put_noidle(cs35l56->base.dev); 1012 1013 gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0); 1014 } 1015 EXPORT_SYMBOL_NS_GPL(cs35l56_hda_remove, SND_HDA_SCODEC_CS35L56); 1016 1017 const struct dev_pm_ops cs35l56_hda_pm_ops = { 1018 RUNTIME_PM_OPS(cs35l56_hda_runtime_suspend, cs35l56_hda_runtime_resume, NULL) 1019 SYSTEM_SLEEP_PM_OPS(cs35l56_hda_system_suspend, cs35l56_hda_system_resume) 1020 LATE_SYSTEM_SLEEP_PM_OPS(cs35l56_hda_system_suspend_late, 1021 cs35l56_hda_system_resume_early) 1022 NOIRQ_SYSTEM_SLEEP_PM_OPS(cs35l56_hda_system_suspend_no_irq, 1023 cs35l56_hda_system_resume_no_irq) 1024 }; 1025 EXPORT_SYMBOL_NS_GPL(cs35l56_hda_pm_ops, SND_HDA_SCODEC_CS35L56); 1026 1027 MODULE_DESCRIPTION("CS35L56 HDA Driver"); 1028 MODULE_IMPORT_NS(SND_HDA_CS_DSP_CONTROLS); 1029 MODULE_IMPORT_NS(SND_SOC_CS35L56_SHARED); 1030 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>"); 1031 MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>"); 1032 MODULE_LICENSE("GPL"); 1033 MODULE_IMPORT_NS(FW_CS_DSP); 1034