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