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