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