1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // CS35l41 ALSA HDA audio driver 4 // 5 // Copyright 2021 Cirrus Logic, Inc. 6 // 7 // Author: Lucas Tanure <tanureal@opensource.cirrus.com> 8 9 #include <linux/acpi.h> 10 #include <linux/module.h> 11 #include <linux/moduleparam.h> 12 #include <sound/hda_codec.h> 13 #include <sound/soc.h> 14 #include <linux/pm_runtime.h> 15 #include <linux/spi/spi.h> 16 #include <linux/vmalloc.h> 17 #include "hda_local.h" 18 #include "hda_auto_parser.h" 19 #include "hda_jack.h" 20 #include "hda_generic.h" 21 #include "hda_component.h" 22 #include "cs35l41_hda.h" 23 #include "hda_cs_dsp_ctl.h" 24 #include "cs35l41_hda_property.h" 25 26 #define CS35L41_PART "cs35l41" 27 28 #define HALO_STATE_DSP_CTL_NAME "HALO_STATE" 29 #define HALO_STATE_DSP_CTL_TYPE 5 30 #define HALO_STATE_DSP_CTL_ALG 262308 31 #define CAL_R_DSP_CTL_NAME "CAL_R" 32 #define CAL_STATUS_DSP_CTL_NAME "CAL_STATUS" 33 #define CAL_CHECKSUM_DSP_CTL_NAME "CAL_CHECKSUM" 34 #define CAL_AMBIENT_DSP_CTL_NAME "CAL_AMBIENT" 35 #define CAL_DSP_CTL_TYPE 5 36 #define CAL_DSP_CTL_ALG 205 37 #define CS35L41_UUID "50d90cdc-3de4-4f18-b528-c7fe3b71f40d" 38 #define CS35L41_DSM_GET_MUTE 5 39 #define CS35L41_NOTIFY_EVENT 0x91 40 #define CS35L41_TUNING_SIG 0x109A4A35 41 42 enum cs35l41_tuning_param_types { 43 TUNING_PARAM_GAIN, 44 }; 45 46 struct cs35l41_tuning_param_hdr { 47 __le32 tuning_index; 48 __le32 type; 49 __le32 size; 50 } __packed; 51 52 struct cs35l41_tuning_param { 53 struct cs35l41_tuning_param_hdr hdr; 54 union { 55 __le32 gain; 56 }; 57 } __packed; 58 59 struct cs35l41_tuning_params { 60 __le32 signature; 61 __le32 version; 62 __le32 size; 63 __le32 num_entries; 64 u8 data[]; 65 } __packed; 66 67 /* Firmware calibration controls */ 68 static const struct cirrus_amp_cal_controls cs35l41_calibration_controls = { 69 .alg_id = CAL_DSP_CTL_ALG, 70 .mem_region = CAL_DSP_CTL_TYPE, 71 .ambient = CAL_AMBIENT_DSP_CTL_NAME, 72 .calr = CAL_R_DSP_CTL_NAME, 73 .status = CAL_STATUS_DSP_CTL_NAME, 74 .checksum = CAL_CHECKSUM_DSP_CTL_NAME, 75 }; 76 77 static bool firmware_autostart = 1; 78 module_param(firmware_autostart, bool, 0444); 79 MODULE_PARM_DESC(firmware_autostart, "Allow automatic firmware download on boot" 80 "(0=Disable, 1=Enable) (default=1); "); 81 82 static const struct reg_sequence cs35l41_hda_config[] = { 83 { CS35L41_PLL_CLK_CTRL, 0x00000430 }, // 3072000Hz, BCLK Input, PLL_REFCLK_EN = 1 84 { CS35L41_DSP_CLK_CTRL, 0x00000003 }, // DSP CLK EN 85 { CS35L41_GLOBAL_CLK_CTRL, 0x00000003 }, // GLOBAL_FS = 48 kHz 86 { CS35L41_SP_ENABLES, 0x00010000 }, // ASP_RX1_EN = 1 87 { CS35L41_SP_RATE_CTRL, 0x00000021 }, // ASP_BCLK_FREQ = 3.072 MHz 88 { CS35L41_SP_FORMAT, 0x20200200 }, // 32 bits RX/TX slots, I2S, clk consumer 89 { CS35L41_SP_HIZ_CTRL, 0x00000002 }, // Hi-Z unused 90 { CS35L41_SP_TX_WL, 0x00000018 }, // 24 cycles/slot 91 { CS35L41_SP_RX_WL, 0x00000018 }, // 24 cycles/slot 92 { CS35L41_DAC_PCM1_SRC, 0x00000008 }, // DACPCM1_SRC = ASPRX1 93 { CS35L41_ASP_TX1_SRC, 0x00000018 }, // ASPTX1 SRC = VMON 94 { CS35L41_ASP_TX2_SRC, 0x00000019 }, // ASPTX2 SRC = IMON 95 { CS35L41_ASP_TX3_SRC, 0x00000032 }, // ASPTX3 SRC = ERRVOL 96 { CS35L41_ASP_TX4_SRC, 0x00000033 }, // ASPTX4 SRC = CLASSH_TGT 97 { CS35L41_DSP1_RX1_SRC, 0x00000008 }, // DSP1RX1 SRC = ASPRX1 98 { CS35L41_DSP1_RX2_SRC, 0x00000009 }, // DSP1RX2 SRC = ASPRX2 99 { CS35L41_DSP1_RX3_SRC, 0x00000018 }, // DSP1RX3 SRC = VMON 100 { CS35L41_DSP1_RX4_SRC, 0x00000019 }, // DSP1RX4 SRC = IMON 101 { CS35L41_DSP1_RX5_SRC, 0x00000020 }, // DSP1RX5 SRC = ERRVOL 102 }; 103 104 static const struct reg_sequence cs35l41_hda_config_dsp[] = { 105 { CS35L41_PLL_CLK_CTRL, 0x00000430 }, // 3072000Hz, BCLK Input, PLL_REFCLK_EN = 1 106 { CS35L41_DSP_CLK_CTRL, 0x00000003 }, // DSP CLK EN 107 { CS35L41_GLOBAL_CLK_CTRL, 0x00000003 }, // GLOBAL_FS = 48 kHz 108 { CS35L41_SP_ENABLES, 0x00010001 }, // ASP_RX1_EN = 1, ASP_TX1_EN = 1 109 { CS35L41_SP_RATE_CTRL, 0x00000021 }, // ASP_BCLK_FREQ = 3.072 MHz 110 { CS35L41_SP_FORMAT, 0x20200200 }, // 32 bits RX/TX slots, I2S, clk consumer 111 { CS35L41_SP_HIZ_CTRL, 0x00000003 }, // Hi-Z unused/disabled 112 { CS35L41_SP_TX_WL, 0x00000018 }, // 24 cycles/slot 113 { CS35L41_SP_RX_WL, 0x00000018 }, // 24 cycles/slot 114 { CS35L41_DAC_PCM1_SRC, 0x00000032 }, // DACPCM1_SRC = DSP1TX1 115 { CS35L41_ASP_TX1_SRC, 0x00000018 }, // ASPTX1 SRC = VMON 116 { CS35L41_ASP_TX2_SRC, 0x00000019 }, // ASPTX2 SRC = IMON 117 { CS35L41_ASP_TX3_SRC, 0x00000028 }, // ASPTX3 SRC = VPMON 118 { CS35L41_ASP_TX4_SRC, 0x00000029 }, // ASPTX4 SRC = VBSTMON 119 { CS35L41_DSP1_RX1_SRC, 0x00000008 }, // DSP1RX1 SRC = ASPRX1 120 { CS35L41_DSP1_RX2_SRC, 0x00000008 }, // DSP1RX2 SRC = ASPRX1 121 { CS35L41_DSP1_RX3_SRC, 0x00000018 }, // DSP1RX3 SRC = VMON 122 { CS35L41_DSP1_RX4_SRC, 0x00000019 }, // DSP1RX4 SRC = IMON 123 { CS35L41_DSP1_RX6_SRC, 0x00000029 }, // DSP1RX6 SRC = VBSTMON 124 }; 125 126 static const struct reg_sequence cs35l41_hda_unmute[] = { 127 { CS35L41_AMP_DIG_VOL_CTRL, 0x00008000 }, // AMP_HPF_PCM_EN = 1, AMP_VOL_PCM 0.0 dB 128 { CS35L41_AMP_GAIN_CTRL, 0x00000084 }, // AMP_GAIN_PCM 4.5 dB 129 }; 130 131 static const struct reg_sequence cs35l41_hda_mute[] = { 132 { CS35L41_AMP_GAIN_CTRL, 0x00000000 }, // AMP_GAIN_PCM 0.5 dB 133 { CS35L41_AMP_DIG_VOL_CTRL, 0x0000A678 }, // AMP_HPF_PCM_EN = 1, AMP_VOL_PCM Mute 134 }; 135 136 static const struct cs_dsp_client_ops client_ops = { 137 /* cs_dsp requires the client to provide this even if it is empty */ 138 }; 139 140 static int cs35l41_request_tuning_param_file(struct cs35l41_hda *cs35l41, char *tuning_filename, 141 const struct firmware **firmware, char **filename, 142 const char *ssid) 143 { 144 int ret = 0; 145 146 /* Filename is the same as the tuning file with "cfg" suffix */ 147 *filename = kasprintf(GFP_KERNEL, "%scfg", tuning_filename); 148 if (*filename == NULL) 149 return -ENOMEM; 150 151 ret = firmware_request_nowarn(firmware, *filename, cs35l41->dev); 152 if (ret != 0) { 153 dev_dbg(cs35l41->dev, "Failed to request '%s'\n", *filename); 154 kfree(*filename); 155 *filename = NULL; 156 } 157 158 return ret; 159 } 160 161 static int cs35l41_request_firmware_file(struct cs35l41_hda *cs35l41, 162 const struct firmware **firmware, char **filename, 163 const char *ssid, const char *amp_name, 164 int spkid, const char *filetype) 165 { 166 const char * const dsp_name = cs35l41->cs_dsp.name; 167 char *s, c; 168 int ret = 0; 169 170 if (spkid > -1 && ssid && amp_name) 171 *filename = kasprintf(GFP_KERNEL, "cirrus/%s-%s-%s-%s-spkid%d-%s.%s", CS35L41_PART, 172 dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type], 173 ssid, spkid, amp_name, filetype); 174 else if (spkid > -1 && ssid) 175 *filename = kasprintf(GFP_KERNEL, "cirrus/%s-%s-%s-%s-spkid%d.%s", CS35L41_PART, 176 dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type], 177 ssid, spkid, filetype); 178 else if (ssid && amp_name) 179 *filename = kasprintf(GFP_KERNEL, "cirrus/%s-%s-%s-%s-%s.%s", CS35L41_PART, 180 dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type], 181 ssid, amp_name, filetype); 182 else if (ssid) 183 *filename = kasprintf(GFP_KERNEL, "cirrus/%s-%s-%s-%s.%s", CS35L41_PART, 184 dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type], 185 ssid, filetype); 186 else 187 *filename = kasprintf(GFP_KERNEL, "cirrus/%s-%s-%s.%s", CS35L41_PART, 188 dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type], 189 filetype); 190 191 if (*filename == NULL) 192 return -ENOMEM; 193 194 /* 195 * Make sure that filename is lower-case and any non alpha-numeric 196 * characters except full stop and '/' are replaced with hyphens. 197 */ 198 s = *filename; 199 while (*s) { 200 c = *s; 201 if (isalnum(c)) 202 *s = tolower(c); 203 else if (c != '.' && c != '/') 204 *s = '-'; 205 s++; 206 } 207 208 ret = firmware_request_nowarn(firmware, *filename, cs35l41->dev); 209 if (ret != 0) { 210 dev_dbg(cs35l41->dev, "Failed to request '%s'\n", *filename); 211 kfree(*filename); 212 *filename = NULL; 213 } 214 215 return ret; 216 } 217 218 static int cs35l41_request_firmware_files_spkid(struct cs35l41_hda *cs35l41, 219 const struct firmware **wmfw_firmware, 220 char **wmfw_filename, 221 const struct firmware **coeff_firmware, 222 char **coeff_filename) 223 { 224 int ret; 225 226 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.wmfw */ 227 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename, 228 cs35l41->acpi_subsystem_id, cs35l41->amp_name, 229 cs35l41->speaker_id, "wmfw"); 230 if (!ret) { 231 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */ 232 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename, 233 cs35l41->acpi_subsystem_id, cs35l41->amp_name, 234 cs35l41->speaker_id, "bin"); 235 if (ret) 236 goto coeff_err; 237 238 return 0; 239 } 240 241 /* try cirrus/part-dspN-fwtype-sub<-ampname>.wmfw */ 242 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename, 243 cs35l41->acpi_subsystem_id, 244 cs35l41->amp_name, -1, "wmfw"); 245 if (!ret) { 246 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */ 247 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename, 248 cs35l41->acpi_subsystem_id, cs35l41->amp_name, 249 cs35l41->speaker_id, "bin"); 250 if (ret) 251 goto coeff_err; 252 253 return 0; 254 } 255 256 /* try cirrus/part-dspN-fwtype-sub<-spkidN>.wmfw */ 257 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename, 258 cs35l41->acpi_subsystem_id, 259 NULL, cs35l41->speaker_id, "wmfw"); 260 if (!ret) { 261 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */ 262 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename, 263 cs35l41->acpi_subsystem_id, 264 cs35l41->amp_name, cs35l41->speaker_id, "bin"); 265 if (ret) 266 /* try cirrus/part-dspN-fwtype-sub<-spkidN>.bin */ 267 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, 268 coeff_filename, 269 cs35l41->acpi_subsystem_id, NULL, 270 cs35l41->speaker_id, "bin"); 271 if (ret) 272 goto coeff_err; 273 274 return 0; 275 } 276 277 /* try cirrus/part-dspN-fwtype-sub.wmfw */ 278 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename, 279 cs35l41->acpi_subsystem_id, 280 NULL, -1, "wmfw"); 281 if (!ret) { 282 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */ 283 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename, 284 cs35l41->acpi_subsystem_id, cs35l41->amp_name, 285 cs35l41->speaker_id, "bin"); 286 if (ret) 287 /* try cirrus/part-dspN-fwtype-sub<-spkidN>.bin */ 288 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, 289 coeff_filename, 290 cs35l41->acpi_subsystem_id, NULL, 291 cs35l41->speaker_id, "bin"); 292 if (ret) 293 goto coeff_err; 294 } 295 296 return ret; 297 coeff_err: 298 release_firmware(*wmfw_firmware); 299 kfree(*wmfw_filename); 300 return ret; 301 } 302 303 static int cs35l41_fallback_firmware_file(struct cs35l41_hda *cs35l41, 304 const struct firmware **wmfw_firmware, 305 char **wmfw_filename, 306 const struct firmware **coeff_firmware, 307 char **coeff_filename) 308 { 309 int ret; 310 311 /* Handle fallback */ 312 dev_warn(cs35l41->dev, "Falling back to default firmware.\n"); 313 314 /* fallback try cirrus/part-dspN-fwtype.wmfw */ 315 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename, 316 NULL, NULL, -1, "wmfw"); 317 if (ret) 318 goto err; 319 320 /* fallback try cirrus/part-dspN-fwtype.bin */ 321 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename, 322 NULL, NULL, -1, "bin"); 323 if (ret) { 324 release_firmware(*wmfw_firmware); 325 kfree(*wmfw_filename); 326 goto err; 327 } 328 return 0; 329 330 err: 331 dev_warn(cs35l41->dev, "Unable to find firmware and tuning\n"); 332 return ret; 333 } 334 335 static int cs35l41_request_firmware_files(struct cs35l41_hda *cs35l41, 336 const struct firmware **wmfw_firmware, 337 char **wmfw_filename, 338 const struct firmware **coeff_firmware, 339 char **coeff_filename) 340 { 341 int ret; 342 343 if (cs35l41->speaker_id > -1) { 344 ret = cs35l41_request_firmware_files_spkid(cs35l41, wmfw_firmware, wmfw_filename, 345 coeff_firmware, coeff_filename); 346 goto out; 347 } 348 349 /* try cirrus/part-dspN-fwtype-sub<-ampname>.wmfw */ 350 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename, 351 cs35l41->acpi_subsystem_id, 352 cs35l41->amp_name, -1, "wmfw"); 353 if (!ret) { 354 /* try cirrus/part-dspN-fwtype-sub<-ampname>.bin */ 355 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename, 356 cs35l41->acpi_subsystem_id, cs35l41->amp_name, 357 -1, "bin"); 358 if (ret) 359 goto coeff_err; 360 361 goto out; 362 } 363 364 /* try cirrus/part-dspN-fwtype-sub.wmfw */ 365 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename, 366 cs35l41->acpi_subsystem_id, 367 NULL, -1, "wmfw"); 368 if (!ret) { 369 /* try cirrus/part-dspN-fwtype-sub<-ampname>.bin */ 370 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename, 371 cs35l41->acpi_subsystem_id, 372 cs35l41->amp_name, -1, "bin"); 373 if (ret) 374 /* try cirrus/part-dspN-fwtype-sub.bin */ 375 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename, 376 cs35l41->acpi_subsystem_id, NULL, -1, 377 "bin"); 378 if (ret) 379 goto coeff_err; 380 } 381 382 out: 383 if (ret) 384 /* if all attempts at finding firmware fail, try fallback */ 385 goto fallback; 386 387 return 0; 388 389 coeff_err: 390 release_firmware(*wmfw_firmware); 391 kfree(*wmfw_filename); 392 fallback: 393 return cs35l41_fallback_firmware_file(cs35l41, wmfw_firmware, wmfw_filename, 394 coeff_firmware, coeff_filename); 395 } 396 397 398 static void cs35l41_hda_apply_calibration(struct cs35l41_hda *cs35l41) 399 { 400 int ret; 401 402 if (!cs35l41->cal_data_valid) 403 return; 404 405 ret = cs_amp_write_cal_coeffs(&cs35l41->cs_dsp, &cs35l41_calibration_controls, 406 &cs35l41->cal_data); 407 if (ret < 0) 408 dev_warn(cs35l41->dev, "Failed to apply calibration: %d\n", ret); 409 else 410 dev_info(cs35l41->dev, "Calibration applied: R0=%d\n", cs35l41->cal_data.calR); 411 } 412 413 static int cs35l41_read_silicon_uid(struct cs35l41_hda *cs35l41, u64 *uid) 414 { 415 u32 tmp; 416 int ret; 417 418 ret = regmap_read(cs35l41->regmap, CS35L41_DIE_STS2, &tmp); 419 if (ret) { 420 dev_err(cs35l41->dev, "Cannot obtain CS35L41_DIE_STS2: %d\n", ret); 421 return ret; 422 } 423 424 *uid = tmp; 425 *uid <<= 32; 426 427 ret = regmap_read(cs35l41->regmap, CS35L41_DIE_STS1, &tmp); 428 if (ret) { 429 dev_err(cs35l41->dev, "Cannot obtain CS35L41_DIE_STS1: %d\n", ret); 430 return ret; 431 } 432 433 *uid |= tmp; 434 435 dev_dbg(cs35l41->dev, "UniqueID = %#llx\n", *uid); 436 437 return 0; 438 } 439 440 static int cs35l41_get_calibration(struct cs35l41_hda *cs35l41) 441 { 442 u64 silicon_uid; 443 int ret; 444 445 ret = cs35l41_read_silicon_uid(cs35l41, &silicon_uid); 446 if (ret < 0) 447 return ret; 448 449 ret = cs_amp_get_efi_calibration_data(cs35l41->dev, silicon_uid, 450 cs35l41->index, 451 &cs35l41->cal_data); 452 453 /* Only return an error status if probe should be aborted */ 454 if ((ret == -ENOENT) || (ret == -EOVERFLOW)) 455 return 0; 456 457 if (ret < 0) 458 return ret; 459 460 cs35l41->cal_data_valid = true; 461 462 return 0; 463 } 464 465 466 static void cs35l41_set_default_tuning_params(struct cs35l41_hda *cs35l41) 467 { 468 cs35l41->tuning_gain = DEFAULT_AMP_GAIN_PCM; 469 } 470 471 static int cs35l41_read_tuning_params(struct cs35l41_hda *cs35l41, const struct firmware *firmware) 472 { 473 struct cs35l41_tuning_params *params; 474 unsigned int offset = 0; 475 unsigned int end; 476 int i; 477 478 params = (void *)&firmware->data[0]; 479 480 if (le32_to_cpu(params->size) != firmware->size) { 481 dev_err(cs35l41->dev, "Wrong Size for Tuning Param file. Expected %d got %zu\n", 482 le32_to_cpu(params->size), firmware->size); 483 return -EINVAL; 484 } 485 486 if (le32_to_cpu(params->version) != 1) { 487 dev_err(cs35l41->dev, "Unsupported Tuning Param Version: %d\n", 488 le32_to_cpu(params->version)); 489 return -EINVAL; 490 } 491 492 if (le32_to_cpu(params->signature) != CS35L41_TUNING_SIG) { 493 dev_err(cs35l41->dev, 494 "Mismatched Signature for Tuning Param file. Expected %#x got %#x\n", 495 CS35L41_TUNING_SIG, le32_to_cpu(params->signature)); 496 return -EINVAL; 497 } 498 499 end = firmware->size - sizeof(struct cs35l41_tuning_params); 500 501 for (i = 0; i < le32_to_cpu(params->num_entries); i++) { 502 struct cs35l41_tuning_param *param; 503 504 if ((offset >= end) || ((offset + sizeof(struct cs35l41_tuning_param_hdr)) >= end)) 505 return -EFAULT; 506 507 param = (void *)¶ms->data[offset]; 508 offset += le32_to_cpu(param->hdr.size); 509 510 if (offset > end) 511 return -EFAULT; 512 513 switch (le32_to_cpu(param->hdr.type)) { 514 case TUNING_PARAM_GAIN: 515 cs35l41->tuning_gain = le32_to_cpu(param->gain); 516 dev_dbg(cs35l41->dev, "Applying Gain: %d\n", cs35l41->tuning_gain); 517 break; 518 default: 519 break; 520 } 521 } 522 523 return 0; 524 } 525 526 static int cs35l41_load_tuning_params(struct cs35l41_hda *cs35l41, char *tuning_filename) 527 { 528 const struct firmware *tuning_param_file = NULL; 529 char *tuning_param_filename = NULL; 530 int ret; 531 532 ret = cs35l41_request_tuning_param_file(cs35l41, tuning_filename, &tuning_param_file, 533 &tuning_param_filename, cs35l41->acpi_subsystem_id); 534 if (ret) { 535 dev_dbg(cs35l41->dev, "Missing Tuning Param for file: %s: %d\n", tuning_filename, 536 ret); 537 return 0; 538 } 539 540 ret = cs35l41_read_tuning_params(cs35l41, tuning_param_file); 541 if (ret) { 542 dev_err(cs35l41->dev, "Error reading Tuning Params from file: %s: %d\n", 543 tuning_param_filename, ret); 544 /* Reset to default Tuning Parameters */ 545 cs35l41_set_default_tuning_params(cs35l41); 546 } 547 548 release_firmware(tuning_param_file); 549 kfree(tuning_param_filename); 550 551 return ret; 552 } 553 554 static int cs35l41_init_dsp(struct cs35l41_hda *cs35l41) 555 { 556 const struct firmware *coeff_firmware = NULL; 557 const struct firmware *wmfw_firmware = NULL; 558 struct cs_dsp *dsp = &cs35l41->cs_dsp; 559 char *coeff_filename = NULL; 560 char *wmfw_filename = NULL; 561 int ret; 562 563 if (!cs35l41->halo_initialized) { 564 cs35l41_configure_cs_dsp(cs35l41->dev, cs35l41->regmap, dsp); 565 dsp->client_ops = &client_ops; 566 567 ret = cs_dsp_halo_init(&cs35l41->cs_dsp); 568 if (ret) 569 return ret; 570 cs35l41->halo_initialized = true; 571 } 572 573 cs35l41_set_default_tuning_params(cs35l41); 574 575 ret = cs35l41_request_firmware_files(cs35l41, &wmfw_firmware, &wmfw_filename, 576 &coeff_firmware, &coeff_filename); 577 if (ret < 0) 578 return ret; 579 580 dev_dbg(cs35l41->dev, "Loading WMFW Firmware: %s\n", wmfw_filename); 581 if (coeff_filename) { 582 dev_dbg(cs35l41->dev, "Loading Coefficient File: %s\n", coeff_filename); 583 ret = cs35l41_load_tuning_params(cs35l41, coeff_filename); 584 if (ret) 585 dev_warn(cs35l41->dev, "Unable to load Tuning Parameters: %d\n", ret); 586 } else { 587 dev_warn(cs35l41->dev, "No Coefficient File available.\n"); 588 } 589 590 ret = cs_dsp_power_up(dsp, wmfw_firmware, wmfw_filename, coeff_firmware, coeff_filename, 591 hda_cs_dsp_fw_ids[cs35l41->firmware_type]); 592 if (ret) 593 goto err; 594 595 cs35l41_hda_apply_calibration(cs35l41); 596 597 err: 598 if (ret) 599 cs35l41_set_default_tuning_params(cs35l41); 600 release_firmware(wmfw_firmware); 601 release_firmware(coeff_firmware); 602 kfree(wmfw_filename); 603 kfree(coeff_filename); 604 605 return ret; 606 } 607 608 static void cs35l41_shutdown_dsp(struct cs35l41_hda *cs35l41) 609 { 610 struct cs_dsp *dsp = &cs35l41->cs_dsp; 611 612 cs35l41_set_default_tuning_params(cs35l41); 613 cs_dsp_stop(dsp); 614 cs_dsp_power_down(dsp); 615 dev_dbg(cs35l41->dev, "Unloaded Firmware\n"); 616 } 617 618 static void cs35l41_remove_dsp(struct cs35l41_hda *cs35l41) 619 { 620 struct cs_dsp *dsp = &cs35l41->cs_dsp; 621 622 cancel_work_sync(&cs35l41->fw_load_work); 623 624 mutex_lock(&cs35l41->fw_mutex); 625 cs35l41_shutdown_dsp(cs35l41); 626 cs_dsp_remove(dsp); 627 cs35l41->halo_initialized = false; 628 mutex_unlock(&cs35l41->fw_mutex); 629 } 630 631 /* Protection release cycle to get the speaker out of Safe-Mode */ 632 static void cs35l41_error_release(struct device *dev, struct regmap *regmap, unsigned int mask) 633 { 634 regmap_write(regmap, CS35L41_PROTECT_REL_ERR_IGN, 0); 635 regmap_set_bits(regmap, CS35L41_PROTECT_REL_ERR_IGN, mask); 636 regmap_clear_bits(regmap, CS35L41_PROTECT_REL_ERR_IGN, mask); 637 } 638 639 /* Clear all errors to release safe mode. Global Enable must be cleared first. */ 640 static void cs35l41_irq_release(struct cs35l41_hda *cs35l41) 641 { 642 cs35l41_error_release(cs35l41->dev, cs35l41->regmap, cs35l41->irq_errors); 643 cs35l41->irq_errors = 0; 644 } 645 646 static void cs35l41_hda_play_start(struct device *dev) 647 { 648 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 649 struct regmap *reg = cs35l41->regmap; 650 651 dev_dbg(dev, "Play (Start)\n"); 652 653 if (cs35l41->playback_started) { 654 dev_dbg(dev, "Playback already started."); 655 return; 656 } 657 658 cs35l41->playback_started = true; 659 660 if (cs35l41->cs_dsp.running) { 661 regmap_multi_reg_write(reg, cs35l41_hda_config_dsp, 662 ARRAY_SIZE(cs35l41_hda_config_dsp)); 663 if (cs35l41->hw_cfg.bst_type == CS35L41_INT_BOOST) 664 regmap_write(reg, CS35L41_DSP1_RX5_SRC, CS35L41_INPUT_SRC_VPMON); 665 else 666 regmap_write(reg, CS35L41_DSP1_RX5_SRC, CS35L41_INPUT_SRC_VBSTMON); 667 regmap_update_bits(reg, CS35L41_PWR_CTRL2, 668 CS35L41_VMON_EN_MASK | CS35L41_IMON_EN_MASK, 669 1 << CS35L41_VMON_EN_SHIFT | 1 << CS35L41_IMON_EN_SHIFT); 670 cs35l41_set_cspl_mbox_cmd(cs35l41->dev, reg, CSPL_MBOX_CMD_RESUME); 671 } else { 672 regmap_multi_reg_write(reg, cs35l41_hda_config, ARRAY_SIZE(cs35l41_hda_config)); 673 } 674 regmap_update_bits(reg, CS35L41_PWR_CTRL2, CS35L41_AMP_EN_MASK, 1 << CS35L41_AMP_EN_SHIFT); 675 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST) 676 regmap_write(reg, CS35L41_GPIO1_CTRL1, 0x00008001); 677 678 } 679 680 static void cs35l41_mute(struct device *dev, bool mute) 681 { 682 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 683 struct regmap *reg = cs35l41->regmap; 684 unsigned int amp_gain; 685 686 dev_dbg(dev, "Mute(%d:%d) Playback Started: %d\n", mute, cs35l41->mute_override, 687 cs35l41->playback_started); 688 689 if (cs35l41->playback_started) { 690 if (mute || cs35l41->mute_override) { 691 dev_dbg(dev, "Muting\n"); 692 regmap_multi_reg_write(reg, cs35l41_hda_mute, ARRAY_SIZE(cs35l41_hda_mute)); 693 } else { 694 dev_dbg(dev, "Unmuting\n"); 695 if (cs35l41->cs_dsp.running) { 696 dev_dbg(dev, "Using Tuned Gain: %d\n", cs35l41->tuning_gain); 697 amp_gain = (cs35l41->tuning_gain << CS35L41_AMP_GAIN_PCM_SHIFT) | 698 (DEFAULT_AMP_GAIN_PDM << CS35L41_AMP_GAIN_PDM_SHIFT); 699 700 /* AMP_HPF_PCM_EN = 1, AMP_VOL_PCM 0.0 dB */ 701 regmap_write(reg, CS35L41_AMP_DIG_VOL_CTRL, 0x00008000); 702 regmap_write(reg, CS35L41_AMP_GAIN_CTRL, amp_gain); 703 } else { 704 regmap_multi_reg_write(reg, cs35l41_hda_unmute, 705 ARRAY_SIZE(cs35l41_hda_unmute)); 706 } 707 } 708 } 709 } 710 711 static void cs35l41_hda_play_done(struct device *dev) 712 { 713 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 714 struct regmap *reg = cs35l41->regmap; 715 716 dev_dbg(dev, "Play (Complete)\n"); 717 718 cs35l41_global_enable(dev, reg, cs35l41->hw_cfg.bst_type, 1, 719 &cs35l41->cs_dsp); 720 cs35l41_mute(dev, false); 721 } 722 723 static void cs35l41_hda_pause_start(struct device *dev) 724 { 725 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 726 struct regmap *reg = cs35l41->regmap; 727 728 dev_dbg(dev, "Pause (Start)\n"); 729 730 cs35l41_mute(dev, true); 731 cs35l41_global_enable(dev, reg, cs35l41->hw_cfg.bst_type, 0, 732 &cs35l41->cs_dsp); 733 } 734 735 static void cs35l41_hda_pause_done(struct device *dev) 736 { 737 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 738 struct regmap *reg = cs35l41->regmap; 739 740 dev_dbg(dev, "Pause (Complete)\n"); 741 742 regmap_update_bits(reg, CS35L41_PWR_CTRL2, CS35L41_AMP_EN_MASK, 0 << CS35L41_AMP_EN_SHIFT); 743 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST) 744 regmap_write(reg, CS35L41_GPIO1_CTRL1, 0x00000001); 745 if (cs35l41->cs_dsp.running) { 746 cs35l41_set_cspl_mbox_cmd(dev, reg, CSPL_MBOX_CMD_PAUSE); 747 regmap_update_bits(reg, CS35L41_PWR_CTRL2, 748 CS35L41_VMON_EN_MASK | CS35L41_IMON_EN_MASK, 749 0 << CS35L41_VMON_EN_SHIFT | 0 << CS35L41_IMON_EN_SHIFT); 750 } 751 cs35l41_irq_release(cs35l41); 752 cs35l41->playback_started = false; 753 } 754 755 static void cs35l41_hda_pre_playback_hook(struct device *dev, int action) 756 { 757 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 758 759 switch (action) { 760 case HDA_GEN_PCM_ACT_CLEANUP: 761 mutex_lock(&cs35l41->fw_mutex); 762 cs35l41_hda_pause_start(dev); 763 mutex_unlock(&cs35l41->fw_mutex); 764 break; 765 default: 766 break; 767 } 768 } 769 static void cs35l41_hda_playback_hook(struct device *dev, int action) 770 { 771 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 772 773 switch (action) { 774 case HDA_GEN_PCM_ACT_OPEN: 775 /* 776 * All amps must be resumed before we can start playing back. 777 * This ensures, for external boost, that all amps are in AMP_SAFE mode. 778 * Do this in HDA_GEN_PCM_ACT_OPEN, since this is run prior to any of the 779 * other actions. 780 */ 781 pm_runtime_get_sync(dev); 782 break; 783 case HDA_GEN_PCM_ACT_PREPARE: 784 mutex_lock(&cs35l41->fw_mutex); 785 cs35l41_hda_play_start(dev); 786 mutex_unlock(&cs35l41->fw_mutex); 787 break; 788 case HDA_GEN_PCM_ACT_CLEANUP: 789 mutex_lock(&cs35l41->fw_mutex); 790 cs35l41_hda_pause_done(dev); 791 mutex_unlock(&cs35l41->fw_mutex); 792 break; 793 case HDA_GEN_PCM_ACT_CLOSE: 794 mutex_lock(&cs35l41->fw_mutex); 795 if (!cs35l41->cs_dsp.running && cs35l41->request_fw_load && 796 !cs35l41->fw_request_ongoing) { 797 dev_info(dev, "Requesting Firmware Load after HDA_GEN_PCM_ACT_CLOSE\n"); 798 cs35l41->fw_request_ongoing = true; 799 schedule_work(&cs35l41->fw_load_work); 800 } 801 mutex_unlock(&cs35l41->fw_mutex); 802 803 /* 804 * Playback must be finished for all amps before we start runtime suspend. 805 * This ensures no amps are playing back when we start putting them to sleep. 806 */ 807 pm_runtime_mark_last_busy(dev); 808 pm_runtime_put_autosuspend(dev); 809 break; 810 default: 811 break; 812 } 813 } 814 815 static void cs35l41_hda_post_playback_hook(struct device *dev, int action) 816 { 817 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 818 819 switch (action) { 820 case HDA_GEN_PCM_ACT_PREPARE: 821 mutex_lock(&cs35l41->fw_mutex); 822 cs35l41_hda_play_done(dev); 823 mutex_unlock(&cs35l41->fw_mutex); 824 break; 825 default: 826 break; 827 } 828 } 829 830 static int cs35l41_hda_channel_map(struct device *dev, unsigned int tx_num, unsigned int *tx_slot, 831 unsigned int rx_num, unsigned int *rx_slot) 832 { 833 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 834 static const char * const channel_name[] = { "L", "R" }; 835 836 if (!cs35l41->amp_name) { 837 if (*rx_slot >= ARRAY_SIZE(channel_name)) 838 return -EINVAL; 839 840 cs35l41->amp_name = devm_kasprintf(cs35l41->dev, GFP_KERNEL, "%s%d", 841 channel_name[*rx_slot], cs35l41->channel_index); 842 if (!cs35l41->amp_name) 843 return -ENOMEM; 844 } 845 846 return cs35l41_set_channels(cs35l41->dev, cs35l41->regmap, tx_num, tx_slot, rx_num, 847 rx_slot); 848 } 849 850 static int cs35l41_verify_id(struct cs35l41_hda *cs35l41, unsigned int *regid, unsigned int *reg_revid) 851 { 852 unsigned int mtl_revid, chipid; 853 int ret; 854 855 ret = regmap_read(cs35l41->regmap, CS35L41_DEVID, regid); 856 if (ret) { 857 dev_err_probe(cs35l41->dev, ret, "Get Device ID failed\n"); 858 return ret; 859 } 860 861 ret = regmap_read(cs35l41->regmap, CS35L41_REVID, reg_revid); 862 if (ret) { 863 dev_err_probe(cs35l41->dev, ret, "Get Revision ID failed\n"); 864 return ret; 865 } 866 867 mtl_revid = *reg_revid & CS35L41_MTLREVID_MASK; 868 869 chipid = (mtl_revid % 2) ? CS35L41R_CHIP_ID : CS35L41_CHIP_ID; 870 if (*regid != chipid) { 871 dev_err(cs35l41->dev, "CS35L41 Device ID (%X). Expected ID %X\n", *regid, chipid); 872 return -ENODEV; 873 } 874 875 return 0; 876 } 877 878 static int cs35l41_ready_for_reset(struct cs35l41_hda *cs35l41) 879 { 880 mutex_lock(&cs35l41->fw_mutex); 881 if (cs35l41->cs_dsp.running) { 882 cs35l41->cs_dsp.running = false; 883 cs35l41->cs_dsp.booted = false; 884 } 885 regcache_mark_dirty(cs35l41->regmap); 886 mutex_unlock(&cs35l41->fw_mutex); 887 888 return 0; 889 } 890 891 static int cs35l41_system_suspend_prep(struct device *dev) 892 { 893 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 894 895 dev_dbg(cs35l41->dev, "System Suspend Prepare\n"); 896 897 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) { 898 dev_err_once(cs35l41->dev, "System Suspend not supported\n"); 899 return 0; /* don't block the whole system suspend */ 900 } 901 902 mutex_lock(&cs35l41->fw_mutex); 903 if (cs35l41->playback_started) 904 cs35l41_hda_pause_start(dev); 905 mutex_unlock(&cs35l41->fw_mutex); 906 907 return 0; 908 } 909 910 static int cs35l41_system_suspend(struct device *dev) 911 { 912 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 913 int ret; 914 915 dev_dbg(cs35l41->dev, "System Suspend\n"); 916 917 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) { 918 dev_err_once(cs35l41->dev, "System Suspend not supported\n"); 919 return 0; /* don't block the whole system suspend */ 920 } 921 922 mutex_lock(&cs35l41->fw_mutex); 923 if (cs35l41->playback_started) 924 cs35l41_hda_pause_done(dev); 925 mutex_unlock(&cs35l41->fw_mutex); 926 927 ret = pm_runtime_force_suspend(dev); 928 if (ret) { 929 dev_err(dev, "System Suspend Failed, unable to runtime suspend: %d\n", ret); 930 return ret; 931 } 932 933 /* Shutdown DSP before system suspend */ 934 ret = cs35l41_ready_for_reset(cs35l41); 935 if (ret) 936 dev_err(dev, "System Suspend Failed, not ready for Reset: %d\n", ret); 937 938 if (cs35l41->reset_gpio) { 939 dev_info(cs35l41->dev, "Asserting Reset\n"); 940 gpiod_set_value_cansleep(cs35l41->reset_gpio, 0); 941 usleep_range(2000, 2100); 942 } 943 944 dev_dbg(cs35l41->dev, "System Suspended\n"); 945 946 return ret; 947 } 948 949 static int cs35l41_wait_boot_done(struct cs35l41_hda *cs35l41) 950 { 951 unsigned int int_status; 952 int ret; 953 954 ret = regmap_read_poll_timeout(cs35l41->regmap, CS35L41_IRQ1_STATUS4, int_status, 955 int_status & CS35L41_OTP_BOOT_DONE, 1000, 100000); 956 if (ret) { 957 dev_err(cs35l41->dev, "Failed waiting for OTP_BOOT_DONE\n"); 958 return ret; 959 } 960 961 ret = regmap_read(cs35l41->regmap, CS35L41_IRQ1_STATUS3, &int_status); 962 if (ret || (int_status & CS35L41_OTP_BOOT_ERR)) { 963 dev_err(cs35l41->dev, "OTP Boot status %x error\n", 964 int_status & CS35L41_OTP_BOOT_ERR); 965 if (!ret) 966 ret = -EIO; 967 return ret; 968 } 969 970 return 0; 971 } 972 973 static int cs35l41_system_resume(struct device *dev) 974 { 975 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 976 int ret; 977 978 dev_dbg(cs35l41->dev, "System Resume\n"); 979 980 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) { 981 dev_err_once(cs35l41->dev, "System Resume not supported\n"); 982 return 0; /* don't block the whole system resume */ 983 } 984 985 if (cs35l41->reset_gpio) { 986 gpiod_set_value_cansleep(cs35l41->reset_gpio, 0); 987 usleep_range(2000, 2100); 988 gpiod_set_value_cansleep(cs35l41->reset_gpio, 1); 989 } 990 991 usleep_range(2000, 2100); 992 993 regcache_cache_only(cs35l41->regmap, false); 994 995 regmap_write(cs35l41->regmap, CS35L41_SFT_RESET, CS35L41_SOFTWARE_RESET); 996 usleep_range(2000, 2100); 997 998 ret = cs35l41_wait_boot_done(cs35l41); 999 if (ret) 1000 return ret; 1001 1002 regcache_cache_only(cs35l41->regmap, true); 1003 1004 ret = pm_runtime_force_resume(dev); 1005 if (ret) { 1006 dev_err(dev, "System Resume Failed: Unable to runtime resume: %d\n", ret); 1007 return ret; 1008 } 1009 1010 mutex_lock(&cs35l41->fw_mutex); 1011 1012 if (cs35l41->request_fw_load && !cs35l41->fw_request_ongoing) { 1013 cs35l41->fw_request_ongoing = true; 1014 schedule_work(&cs35l41->fw_load_work); 1015 } 1016 mutex_unlock(&cs35l41->fw_mutex); 1017 1018 return ret; 1019 } 1020 1021 static int cs35l41_runtime_idle(struct device *dev) 1022 { 1023 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 1024 1025 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) 1026 return -EBUSY; /* suspend not supported yet on this model */ 1027 return 0; 1028 } 1029 1030 static int cs35l41_runtime_suspend(struct device *dev) 1031 { 1032 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 1033 int ret = 0; 1034 1035 dev_dbg(cs35l41->dev, "Runtime Suspend\n"); 1036 1037 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) { 1038 dev_dbg(cs35l41->dev, "Runtime Suspend not supported\n"); 1039 return 0; 1040 } 1041 1042 mutex_lock(&cs35l41->fw_mutex); 1043 1044 if (cs35l41->cs_dsp.running) { 1045 ret = cs35l41_enter_hibernate(cs35l41->dev, cs35l41->regmap, 1046 cs35l41->hw_cfg.bst_type); 1047 if (ret) 1048 goto err; 1049 } else { 1050 cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type); 1051 } 1052 1053 regcache_cache_only(cs35l41->regmap, true); 1054 regcache_mark_dirty(cs35l41->regmap); 1055 1056 err: 1057 mutex_unlock(&cs35l41->fw_mutex); 1058 1059 return ret; 1060 } 1061 1062 static int cs35l41_runtime_resume(struct device *dev) 1063 { 1064 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 1065 unsigned int regid, reg_revid; 1066 int ret = 0; 1067 1068 dev_dbg(cs35l41->dev, "Runtime Resume\n"); 1069 1070 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) { 1071 dev_dbg(cs35l41->dev, "Runtime Resume not supported\n"); 1072 return 0; 1073 } 1074 1075 mutex_lock(&cs35l41->fw_mutex); 1076 1077 regcache_cache_only(cs35l41->regmap, false); 1078 1079 if (cs35l41->cs_dsp.running) { 1080 ret = cs35l41_exit_hibernate(cs35l41->dev, cs35l41->regmap); 1081 if (ret) { 1082 dev_warn(cs35l41->dev, "Unable to exit Hibernate."); 1083 goto err; 1084 } 1085 } 1086 1087 ret = cs35l41_verify_id(cs35l41, ®id, ®_revid); 1088 if (ret) 1089 goto err; 1090 1091 /* Test key needs to be unlocked to allow the OTP settings to re-apply */ 1092 cs35l41_test_key_unlock(cs35l41->dev, cs35l41->regmap); 1093 ret = regcache_sync(cs35l41->regmap); 1094 cs35l41_test_key_lock(cs35l41->dev, cs35l41->regmap); 1095 if (ret) { 1096 dev_err(cs35l41->dev, "Failed to restore register cache: %d\n", ret); 1097 goto err; 1098 } 1099 1100 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST) 1101 cs35l41_init_boost(cs35l41->dev, cs35l41->regmap, &cs35l41->hw_cfg); 1102 1103 dev_dbg(cs35l41->dev, "CS35L41 Resumed (%x), Revision: %02X\n", regid, reg_revid); 1104 1105 err: 1106 mutex_unlock(&cs35l41->fw_mutex); 1107 1108 return ret; 1109 } 1110 1111 static int cs35l41_smart_amp(struct cs35l41_hda *cs35l41) 1112 { 1113 unsigned int fw_status; 1114 __be32 halo_sts; 1115 int ret; 1116 1117 if (cs35l41->bypass_fw) { 1118 dev_warn(cs35l41->dev, "Bypassing Firmware.\n"); 1119 return 0; 1120 } 1121 1122 ret = cs35l41_init_dsp(cs35l41); 1123 if (ret) { 1124 dev_warn(cs35l41->dev, "Cannot Initialize Firmware. Error: %d\n", ret); 1125 goto clean_dsp; 1126 } 1127 1128 ret = cs35l41_write_fs_errata(cs35l41->dev, cs35l41->regmap); 1129 if (ret) { 1130 dev_err(cs35l41->dev, "Cannot Write FS Errata: %d\n", ret); 1131 goto clean_dsp; 1132 } 1133 1134 ret = cs_dsp_run(&cs35l41->cs_dsp); 1135 if (ret) { 1136 dev_err(cs35l41->dev, "Fail to start dsp: %d\n", ret); 1137 goto clean_dsp; 1138 } 1139 1140 ret = read_poll_timeout(hda_cs_dsp_read_ctl, ret, 1141 be32_to_cpu(halo_sts) == HALO_STATE_CODE_RUN, 1142 1000, 15000, false, &cs35l41->cs_dsp, HALO_STATE_DSP_CTL_NAME, 1143 HALO_STATE_DSP_CTL_TYPE, HALO_STATE_DSP_CTL_ALG, 1144 &halo_sts, sizeof(halo_sts)); 1145 1146 if (ret) { 1147 dev_err(cs35l41->dev, "Timeout waiting for HALO Core to start. State: %u\n", 1148 halo_sts); 1149 goto clean_dsp; 1150 } 1151 1152 ret = regmap_read(cs35l41->regmap, CS35L41_DSP_MBOX_2, &fw_status); 1153 if (ret < 0) { 1154 dev_err(cs35l41->dev, 1155 "Failed to read firmware status: %d\n", ret); 1156 goto clean_dsp; 1157 } 1158 1159 switch (fw_status) { 1160 case CSPL_MBOX_STS_RUNNING: 1161 case CSPL_MBOX_STS_PAUSED: 1162 break; 1163 default: 1164 dev_err(cs35l41->dev, "Firmware status is invalid: %u\n", 1165 fw_status); 1166 ret = -EINVAL; 1167 goto clean_dsp; 1168 } 1169 1170 ret = cs35l41_set_cspl_mbox_cmd(cs35l41->dev, cs35l41->regmap, CSPL_MBOX_CMD_PAUSE); 1171 if (ret) { 1172 dev_err(cs35l41->dev, "Error waiting for DSP to pause: %u\n", ret); 1173 goto clean_dsp; 1174 } 1175 1176 dev_info(cs35l41->dev, "Firmware Loaded - Type: %s, Gain: %d\n", 1177 hda_cs_dsp_fw_ids[cs35l41->firmware_type], cs35l41->tuning_gain); 1178 1179 return 0; 1180 1181 clean_dsp: 1182 cs35l41_shutdown_dsp(cs35l41); 1183 return ret; 1184 } 1185 1186 static void cs35l41_load_firmware(struct cs35l41_hda *cs35l41, bool load) 1187 { 1188 if (cs35l41->cs_dsp.running && !load) { 1189 dev_dbg(cs35l41->dev, "Unloading Firmware\n"); 1190 cs35l41_shutdown_dsp(cs35l41); 1191 } else if (!cs35l41->cs_dsp.running && load) { 1192 dev_dbg(cs35l41->dev, "Loading Firmware\n"); 1193 cs35l41_smart_amp(cs35l41); 1194 } else { 1195 dev_dbg(cs35l41->dev, "Unable to Load firmware.\n"); 1196 } 1197 } 1198 1199 static int cs35l41_fw_load_ctl_get(struct snd_kcontrol *kcontrol, 1200 struct snd_ctl_elem_value *ucontrol) 1201 { 1202 struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol); 1203 1204 ucontrol->value.integer.value[0] = cs35l41->request_fw_load; 1205 return 0; 1206 } 1207 1208 static int cs35l41_mute_override_ctl_get(struct snd_kcontrol *kcontrol, 1209 struct snd_ctl_elem_value *ucontrol) 1210 { 1211 struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol); 1212 1213 ucontrol->value.integer.value[0] = cs35l41->mute_override; 1214 return 0; 1215 } 1216 1217 static void cs35l41_fw_load_work(struct work_struct *work) 1218 { 1219 struct cs35l41_hda *cs35l41 = container_of(work, struct cs35l41_hda, fw_load_work); 1220 1221 pm_runtime_get_sync(cs35l41->dev); 1222 1223 mutex_lock(&cs35l41->fw_mutex); 1224 1225 /* Recheck if playback is ongoing, mutex will block playback during firmware loading */ 1226 if (cs35l41->playback_started) 1227 dev_err(cs35l41->dev, "Cannot Load/Unload firmware during Playback. Retrying...\n"); 1228 else 1229 cs35l41_load_firmware(cs35l41, cs35l41->request_fw_load); 1230 1231 cs35l41->fw_request_ongoing = false; 1232 mutex_unlock(&cs35l41->fw_mutex); 1233 1234 pm_runtime_mark_last_busy(cs35l41->dev); 1235 pm_runtime_put_autosuspend(cs35l41->dev); 1236 } 1237 1238 static int cs35l41_fw_load_ctl_put(struct snd_kcontrol *kcontrol, 1239 struct snd_ctl_elem_value *ucontrol) 1240 { 1241 struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol); 1242 1243 if (cs35l41->request_fw_load == ucontrol->value.integer.value[0]) 1244 return 0; 1245 1246 if (cs35l41->fw_request_ongoing) { 1247 dev_dbg(cs35l41->dev, "Existing request not complete\n"); 1248 return -EBUSY; 1249 } 1250 1251 /* Check if playback is ongoing when initial request is made */ 1252 if (cs35l41->playback_started) { 1253 dev_err(cs35l41->dev, "Cannot Load/Unload firmware during Playback\n"); 1254 return -EBUSY; 1255 } 1256 1257 cs35l41->fw_request_ongoing = true; 1258 cs35l41->request_fw_load = ucontrol->value.integer.value[0]; 1259 schedule_work(&cs35l41->fw_load_work); 1260 1261 return 1; 1262 } 1263 1264 static int cs35l41_fw_type_ctl_get(struct snd_kcontrol *kcontrol, 1265 struct snd_ctl_elem_value *ucontrol) 1266 { 1267 struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol); 1268 1269 ucontrol->value.enumerated.item[0] = cs35l41->firmware_type; 1270 1271 return 0; 1272 } 1273 1274 static int cs35l41_fw_type_ctl_put(struct snd_kcontrol *kcontrol, 1275 struct snd_ctl_elem_value *ucontrol) 1276 { 1277 struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol); 1278 1279 if (ucontrol->value.enumerated.item[0] < HDA_CS_DSP_NUM_FW) { 1280 if (cs35l41->firmware_type != ucontrol->value.enumerated.item[0]) { 1281 cs35l41->firmware_type = ucontrol->value.enumerated.item[0]; 1282 return 1; 1283 } else { 1284 return 0; 1285 } 1286 } 1287 1288 return -EINVAL; 1289 } 1290 1291 static int cs35l41_fw_type_ctl_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo) 1292 { 1293 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(hda_cs_dsp_fw_ids), hda_cs_dsp_fw_ids); 1294 } 1295 1296 static int cs35l41_create_controls(struct cs35l41_hda *cs35l41) 1297 { 1298 char fw_type_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 1299 char fw_load_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 1300 char mute_override_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN]; 1301 struct snd_kcontrol_new fw_type_ctl = { 1302 .name = fw_type_ctl_name, 1303 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 1304 .info = cs35l41_fw_type_ctl_info, 1305 .get = cs35l41_fw_type_ctl_get, 1306 .put = cs35l41_fw_type_ctl_put, 1307 }; 1308 struct snd_kcontrol_new fw_load_ctl = { 1309 .name = fw_load_ctl_name, 1310 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 1311 .info = snd_ctl_boolean_mono_info, 1312 .get = cs35l41_fw_load_ctl_get, 1313 .put = cs35l41_fw_load_ctl_put, 1314 }; 1315 struct snd_kcontrol_new mute_override_ctl = { 1316 .name = mute_override_ctl_name, 1317 .iface = SNDRV_CTL_ELEM_IFACE_CARD, 1318 .info = snd_ctl_boolean_mono_info, 1319 .access = SNDRV_CTL_ELEM_ACCESS_READ | SNDRV_CTL_ELEM_ACCESS_VOLATILE, 1320 .get = cs35l41_mute_override_ctl_get, 1321 }; 1322 int ret; 1323 1324 scnprintf(fw_type_ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s DSP1 Firmware Type", 1325 cs35l41->amp_name); 1326 scnprintf(fw_load_ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s DSP1 Firmware Load", 1327 cs35l41->amp_name); 1328 scnprintf(mute_override_ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s Forced Mute Status", 1329 cs35l41->amp_name); 1330 1331 ret = snd_ctl_add(cs35l41->codec->card, snd_ctl_new1(&fw_type_ctl, cs35l41)); 1332 if (ret) { 1333 dev_err(cs35l41->dev, "Failed to add KControl %s = %d\n", fw_type_ctl.name, ret); 1334 return ret; 1335 } 1336 1337 dev_dbg(cs35l41->dev, "Added Control %s\n", fw_type_ctl.name); 1338 1339 ret = snd_ctl_add(cs35l41->codec->card, snd_ctl_new1(&fw_load_ctl, cs35l41)); 1340 if (ret) { 1341 dev_err(cs35l41->dev, "Failed to add KControl %s = %d\n", fw_load_ctl.name, ret); 1342 return ret; 1343 } 1344 1345 dev_dbg(cs35l41->dev, "Added Control %s\n", fw_load_ctl.name); 1346 1347 ret = snd_ctl_add(cs35l41->codec->card, snd_ctl_new1(&mute_override_ctl, cs35l41)); 1348 if (ret) { 1349 dev_err(cs35l41->dev, "Failed to add KControl %s = %d\n", mute_override_ctl.name, 1350 ret); 1351 return ret; 1352 } 1353 1354 dev_dbg(cs35l41->dev, "Added Control %s\n", mute_override_ctl.name); 1355 1356 return 0; 1357 } 1358 1359 static bool cs35l41_dsm_supported(acpi_handle handle, unsigned int commands) 1360 { 1361 guid_t guid; 1362 1363 guid_parse(CS35L41_UUID, &guid); 1364 1365 return acpi_check_dsm(handle, &guid, 0, BIT(commands)); 1366 } 1367 1368 static int cs35l41_get_acpi_mute_state(struct cs35l41_hda *cs35l41, acpi_handle handle) 1369 { 1370 guid_t guid; 1371 union acpi_object *ret; 1372 int mute = -ENODEV; 1373 1374 guid_parse(CS35L41_UUID, &guid); 1375 1376 if (cs35l41_dsm_supported(handle, CS35L41_DSM_GET_MUTE)) { 1377 ret = acpi_evaluate_dsm(handle, &guid, 0, CS35L41_DSM_GET_MUTE, NULL); 1378 mute = *ret->buffer.pointer; 1379 dev_dbg(cs35l41->dev, "CS35L41_DSM_GET_MUTE: %d\n", mute); 1380 } 1381 1382 dev_dbg(cs35l41->dev, "%s: %d\n", __func__, mute); 1383 1384 return mute; 1385 } 1386 1387 static void cs35l41_acpi_device_notify(acpi_handle handle, u32 event, struct device *dev) 1388 { 1389 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 1390 int mute; 1391 1392 if (event != CS35L41_NOTIFY_EVENT) 1393 return; 1394 1395 mute = cs35l41_get_acpi_mute_state(cs35l41, handle); 1396 if (mute < 0) { 1397 dev_warn(cs35l41->dev, "Unable to retrieve mute state: %d\n", mute); 1398 return; 1399 } 1400 1401 dev_dbg(cs35l41->dev, "Requesting mute value: %d\n", mute); 1402 cs35l41->mute_override = (mute > 0); 1403 cs35l41_mute(cs35l41->dev, cs35l41->mute_override); 1404 } 1405 1406 static int cs35l41_hda_bind(struct device *dev, struct device *master, void *master_data) 1407 { 1408 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 1409 struct hda_component_parent *parent = master_data; 1410 struct hda_component *comp; 1411 unsigned int sleep_flags; 1412 int ret = 0; 1413 1414 comp = hda_component_from_index(parent, cs35l41->index); 1415 if (!comp) 1416 return -EINVAL; 1417 1418 if (comp->dev) 1419 return -EBUSY; 1420 1421 pm_runtime_get_sync(dev); 1422 1423 mutex_lock(&cs35l41->fw_mutex); 1424 1425 comp->dev = dev; 1426 cs35l41->codec = parent->codec; 1427 if (!cs35l41->acpi_subsystem_id) 1428 cs35l41->acpi_subsystem_id = kasprintf(GFP_KERNEL, "%.8x", 1429 cs35l41->codec->core.subsystem_id); 1430 1431 strscpy(comp->name, dev_name(dev), sizeof(comp->name)); 1432 1433 cs35l41->firmware_type = HDA_CS_DSP_FW_SPK_PROT; 1434 1435 if (firmware_autostart) { 1436 dev_dbg(cs35l41->dev, "Firmware Autostart.\n"); 1437 cs35l41->request_fw_load = true; 1438 if (cs35l41_smart_amp(cs35l41) < 0) 1439 dev_warn(cs35l41->dev, "Cannot Run Firmware, reverting to dsp bypass...\n"); 1440 } else { 1441 dev_dbg(cs35l41->dev, "Firmware Autostart is disabled.\n"); 1442 } 1443 1444 ret = cs35l41_create_controls(cs35l41); 1445 1446 comp->playback_hook = cs35l41_hda_playback_hook; 1447 comp->pre_playback_hook = cs35l41_hda_pre_playback_hook; 1448 comp->post_playback_hook = cs35l41_hda_post_playback_hook; 1449 comp->acpi_notify = cs35l41_acpi_device_notify; 1450 comp->adev = cs35l41->dacpi; 1451 1452 comp->acpi_notifications_supported = cs35l41_dsm_supported(acpi_device_handle(comp->adev), 1453 CS35L41_DSM_GET_MUTE); 1454 1455 cs35l41->mute_override = cs35l41_get_acpi_mute_state(cs35l41, 1456 acpi_device_handle(cs35l41->dacpi)) > 0; 1457 1458 mutex_unlock(&cs35l41->fw_mutex); 1459 1460 sleep_flags = lock_system_sleep(); 1461 if (!device_link_add(&cs35l41->codec->core.dev, cs35l41->dev, DL_FLAG_STATELESS)) 1462 dev_warn(dev, "Unable to create device link\n"); 1463 unlock_system_sleep(sleep_flags); 1464 1465 pm_runtime_mark_last_busy(dev); 1466 pm_runtime_put_autosuspend(dev); 1467 1468 dev_info(cs35l41->dev, 1469 "CS35L41 Bound - SSID: %s, BST: %d, VSPK: %d, CH: %c, FW EN: %d, SPKID: %d\n", 1470 cs35l41->acpi_subsystem_id, cs35l41->hw_cfg.bst_type, 1471 cs35l41->hw_cfg.gpio1.func == CS35l41_VSPK_SWITCH, 1472 cs35l41->hw_cfg.spk_pos ? 'R' : 'L', 1473 cs35l41->cs_dsp.running, cs35l41->speaker_id); 1474 1475 return ret; 1476 } 1477 1478 static void cs35l41_hda_unbind(struct device *dev, struct device *master, void *master_data) 1479 { 1480 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 1481 struct hda_component_parent *parent = master_data; 1482 struct hda_component *comp; 1483 unsigned int sleep_flags; 1484 1485 comp = hda_component_from_index(parent, cs35l41->index); 1486 if (!comp) 1487 return; 1488 1489 if (comp->dev == dev) { 1490 sleep_flags = lock_system_sleep(); 1491 device_link_remove(&cs35l41->codec->core.dev, cs35l41->dev); 1492 unlock_system_sleep(sleep_flags); 1493 memset(comp, 0, sizeof(*comp)); 1494 } 1495 } 1496 1497 static const struct component_ops cs35l41_hda_comp_ops = { 1498 .bind = cs35l41_hda_bind, 1499 .unbind = cs35l41_hda_unbind, 1500 }; 1501 1502 static irqreturn_t cs35l41_bst_short_err(int irq, void *data) 1503 { 1504 struct cs35l41_hda *cs35l41 = data; 1505 1506 dev_crit_ratelimited(cs35l41->dev, "LBST Error\n"); 1507 set_bit(CS35L41_BST_SHORT_ERR_RLS_SHIFT, &cs35l41->irq_errors); 1508 1509 return IRQ_HANDLED; 1510 } 1511 1512 static irqreturn_t cs35l41_bst_dcm_uvp_err(int irq, void *data) 1513 { 1514 struct cs35l41_hda *cs35l41 = data; 1515 1516 dev_crit_ratelimited(cs35l41->dev, "DCM VBST Under Voltage Error\n"); 1517 set_bit(CS35L41_BST_UVP_ERR_RLS_SHIFT, &cs35l41->irq_errors); 1518 1519 return IRQ_HANDLED; 1520 } 1521 1522 static irqreturn_t cs35l41_bst_ovp_err(int irq, void *data) 1523 { 1524 struct cs35l41_hda *cs35l41 = data; 1525 1526 dev_crit_ratelimited(cs35l41->dev, "VBST Over Voltage error\n"); 1527 set_bit(CS35L41_BST_OVP_ERR_RLS_SHIFT, &cs35l41->irq_errors); 1528 1529 return IRQ_HANDLED; 1530 } 1531 1532 static irqreturn_t cs35l41_temp_err(int irq, void *data) 1533 { 1534 struct cs35l41_hda *cs35l41 = data; 1535 1536 dev_crit_ratelimited(cs35l41->dev, "Over temperature error\n"); 1537 set_bit(CS35L41_TEMP_ERR_RLS_SHIFT, &cs35l41->irq_errors); 1538 1539 return IRQ_HANDLED; 1540 } 1541 1542 static irqreturn_t cs35l41_temp_warn(int irq, void *data) 1543 { 1544 struct cs35l41_hda *cs35l41 = data; 1545 1546 dev_crit_ratelimited(cs35l41->dev, "Over temperature warning\n"); 1547 set_bit(CS35L41_TEMP_WARN_ERR_RLS_SHIFT, &cs35l41->irq_errors); 1548 1549 return IRQ_HANDLED; 1550 } 1551 1552 static irqreturn_t cs35l41_amp_short(int irq, void *data) 1553 { 1554 struct cs35l41_hda *cs35l41 = data; 1555 1556 dev_crit_ratelimited(cs35l41->dev, "Amp short error\n"); 1557 set_bit(CS35L41_AMP_SHORT_ERR_RLS_SHIFT, &cs35l41->irq_errors); 1558 1559 return IRQ_HANDLED; 1560 } 1561 1562 static const struct cs35l41_irq cs35l41_irqs[] = { 1563 CS35L41_IRQ(BST_OVP_ERR, "Boost Overvoltage Error", cs35l41_bst_ovp_err), 1564 CS35L41_IRQ(BST_DCM_UVP_ERR, "Boost Undervoltage Error", cs35l41_bst_dcm_uvp_err), 1565 CS35L41_IRQ(BST_SHORT_ERR, "Boost Inductor Short Error", cs35l41_bst_short_err), 1566 CS35L41_IRQ(TEMP_WARN, "Temperature Warning", cs35l41_temp_warn), 1567 CS35L41_IRQ(TEMP_ERR, "Temperature Error", cs35l41_temp_err), 1568 CS35L41_IRQ(AMP_SHORT_ERR, "Amp Short", cs35l41_amp_short), 1569 }; 1570 1571 static const struct regmap_irq cs35l41_reg_irqs[] = { 1572 CS35L41_REG_IRQ(IRQ1_STATUS1, BST_OVP_ERR), 1573 CS35L41_REG_IRQ(IRQ1_STATUS1, BST_DCM_UVP_ERR), 1574 CS35L41_REG_IRQ(IRQ1_STATUS1, BST_SHORT_ERR), 1575 CS35L41_REG_IRQ(IRQ1_STATUS1, TEMP_WARN), 1576 CS35L41_REG_IRQ(IRQ1_STATUS1, TEMP_ERR), 1577 CS35L41_REG_IRQ(IRQ1_STATUS1, AMP_SHORT_ERR), 1578 }; 1579 1580 static struct regmap_irq_chip cs35l41_regmap_irq_chip = { 1581 .name = "cs35l41 IRQ1 Controller", 1582 .status_base = CS35L41_IRQ1_STATUS1, 1583 .mask_base = CS35L41_IRQ1_MASK1, 1584 .ack_base = CS35L41_IRQ1_STATUS1, 1585 .num_regs = 4, 1586 .irqs = cs35l41_reg_irqs, 1587 .num_irqs = ARRAY_SIZE(cs35l41_reg_irqs), 1588 .runtime_pm = true, 1589 }; 1590 1591 static void cs35l41_configure_interrupt(struct cs35l41_hda *cs35l41, int irq_pol) 1592 { 1593 int irq; 1594 int ret; 1595 int i; 1596 1597 if (!cs35l41->irq) { 1598 dev_warn(cs35l41->dev, "No Interrupt Found"); 1599 goto err; 1600 } 1601 1602 ret = devm_regmap_add_irq_chip(cs35l41->dev, cs35l41->regmap, cs35l41->irq, 1603 IRQF_ONESHOT | IRQF_SHARED | irq_pol, 1604 0, &cs35l41_regmap_irq_chip, &cs35l41->irq_data); 1605 if (ret) { 1606 dev_dbg(cs35l41->dev, "Unable to add IRQ Chip: %d.", ret); 1607 goto err; 1608 } 1609 1610 for (i = 0; i < ARRAY_SIZE(cs35l41_irqs); i++) { 1611 irq = regmap_irq_get_virq(cs35l41->irq_data, cs35l41_irqs[i].irq); 1612 if (irq < 0) { 1613 ret = irq; 1614 dev_dbg(cs35l41->dev, "Unable to map IRQ %s: %d.", cs35l41_irqs[i].name, 1615 ret); 1616 goto err; 1617 } 1618 1619 ret = devm_request_threaded_irq(cs35l41->dev, irq, NULL, 1620 cs35l41_irqs[i].handler, 1621 IRQF_ONESHOT | IRQF_SHARED | irq_pol, 1622 cs35l41_irqs[i].name, cs35l41); 1623 if (ret) { 1624 dev_dbg(cs35l41->dev, "Unable to allocate IRQ %s:: %d.", 1625 cs35l41_irqs[i].name, ret); 1626 goto err; 1627 } 1628 } 1629 return; 1630 err: 1631 dev_warn(cs35l41->dev, 1632 "IRQ Config Failed. Amp errors may not be recoverable without reboot."); 1633 } 1634 1635 static int cs35l41_hda_apply_properties(struct cs35l41_hda *cs35l41) 1636 { 1637 struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg; 1638 bool using_irq = false; 1639 int irq_pol; 1640 int ret; 1641 1642 if (!cs35l41->hw_cfg.valid) 1643 return -EINVAL; 1644 1645 ret = cs35l41_init_boost(cs35l41->dev, cs35l41->regmap, hw_cfg); 1646 if (ret) 1647 return ret; 1648 1649 if (hw_cfg->gpio1.valid) { 1650 switch (hw_cfg->gpio1.func) { 1651 case CS35L41_NOT_USED: 1652 break; 1653 case CS35l41_VSPK_SWITCH: 1654 hw_cfg->gpio1.func = CS35L41_GPIO1_GPIO; 1655 hw_cfg->gpio1.out_en = true; 1656 break; 1657 case CS35l41_SYNC: 1658 hw_cfg->gpio1.func = CS35L41_GPIO1_MDSYNC; 1659 break; 1660 default: 1661 dev_err(cs35l41->dev, "Invalid function %d for GPIO1\n", 1662 hw_cfg->gpio1.func); 1663 return -EINVAL; 1664 } 1665 } 1666 1667 if (hw_cfg->gpio2.valid) { 1668 switch (hw_cfg->gpio2.func) { 1669 case CS35L41_NOT_USED: 1670 break; 1671 case CS35L41_INTERRUPT: 1672 using_irq = true; 1673 hw_cfg->gpio2.func = CS35L41_GPIO2_INT_OPEN_DRAIN; 1674 break; 1675 default: 1676 dev_err(cs35l41->dev, "Invalid GPIO2 function %d\n", hw_cfg->gpio2.func); 1677 return -EINVAL; 1678 } 1679 } 1680 1681 irq_pol = cs35l41_gpio_config(cs35l41->regmap, hw_cfg); 1682 1683 if (using_irq) 1684 cs35l41_configure_interrupt(cs35l41, irq_pol); 1685 1686 return cs35l41_hda_channel_map(cs35l41->dev, 0, NULL, 1, &hw_cfg->spk_pos); 1687 } 1688 1689 int cs35l41_get_speaker_id(struct device *dev, int amp_index, int num_amps, int fixed_gpio_id) 1690 { 1691 struct gpio_desc *speaker_id_desc; 1692 int speaker_id = -ENODEV; 1693 1694 if (fixed_gpio_id >= 0) { 1695 dev_dbg(dev, "Found Fixed Speaker ID GPIO (index = %d)\n", fixed_gpio_id); 1696 speaker_id_desc = gpiod_get_index(dev, NULL, fixed_gpio_id, GPIOD_IN); 1697 if (IS_ERR(speaker_id_desc)) { 1698 speaker_id = PTR_ERR(speaker_id_desc); 1699 return speaker_id; 1700 } 1701 speaker_id = gpiod_get_value_cansleep(speaker_id_desc); 1702 gpiod_put(speaker_id_desc); 1703 dev_dbg(dev, "Speaker ID = %d\n", speaker_id); 1704 } else { 1705 int base_index; 1706 int gpios_per_amp; 1707 int count; 1708 int tmp; 1709 int i; 1710 1711 count = gpiod_count(dev, "spk-id"); 1712 if (count > 0) { 1713 speaker_id = 0; 1714 gpios_per_amp = count / num_amps; 1715 base_index = gpios_per_amp * amp_index; 1716 1717 if (count % num_amps) 1718 return -EINVAL; 1719 1720 dev_dbg(dev, "Found %d Speaker ID GPIOs per Amp\n", gpios_per_amp); 1721 1722 for (i = 0; i < gpios_per_amp; i++) { 1723 speaker_id_desc = gpiod_get_index(dev, "spk-id", i + base_index, 1724 GPIOD_IN); 1725 if (IS_ERR(speaker_id_desc)) { 1726 speaker_id = PTR_ERR(speaker_id_desc); 1727 break; 1728 } 1729 tmp = gpiod_get_value_cansleep(speaker_id_desc); 1730 gpiod_put(speaker_id_desc); 1731 if (tmp < 0) { 1732 speaker_id = tmp; 1733 break; 1734 } 1735 speaker_id |= tmp << i; 1736 } 1737 dev_dbg(dev, "Speaker ID = %d\n", speaker_id); 1738 } 1739 } 1740 return speaker_id; 1741 } 1742 1743 int cs35l41_hda_parse_acpi(struct cs35l41_hda *cs35l41, struct device *physdev, int id) 1744 { 1745 struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg; 1746 u32 values[HDA_MAX_COMPONENTS]; 1747 char *property; 1748 size_t nval; 1749 int i, ret; 1750 1751 property = "cirrus,dev-index"; 1752 ret = device_property_count_u32(physdev, property); 1753 if (ret <= 0) 1754 goto err; 1755 1756 if (ret > ARRAY_SIZE(values)) { 1757 ret = -EINVAL; 1758 goto err; 1759 } 1760 nval = ret; 1761 1762 ret = device_property_read_u32_array(physdev, property, values, nval); 1763 if (ret) 1764 goto err; 1765 1766 cs35l41->index = -1; 1767 for (i = 0; i < nval; i++) { 1768 if (values[i] == id) { 1769 cs35l41->index = i; 1770 break; 1771 } 1772 } 1773 if (cs35l41->index == -1) { 1774 dev_err(cs35l41->dev, "No index found in %s\n", property); 1775 ret = -ENODEV; 1776 goto err; 1777 } 1778 1779 /* To use the same release code for all laptop variants we can't use devm_ version of 1780 * gpiod_get here, as CLSA010* don't have a fully functional bios with an _DSD node 1781 */ 1782 cs35l41->reset_gpio = fwnode_gpiod_get_index(acpi_fwnode_handle(cs35l41->dacpi), "reset", 1783 cs35l41->index, GPIOD_OUT_LOW, 1784 "cs35l41-reset"); 1785 1786 property = "cirrus,speaker-position"; 1787 ret = device_property_read_u32_array(physdev, property, values, nval); 1788 if (ret) 1789 goto err; 1790 hw_cfg->spk_pos = values[cs35l41->index]; 1791 1792 cs35l41->channel_index = 0; 1793 for (i = 0; i < cs35l41->index; i++) 1794 if (values[i] == hw_cfg->spk_pos) 1795 cs35l41->channel_index++; 1796 1797 property = "cirrus,gpio1-func"; 1798 ret = device_property_read_u32_array(physdev, property, values, nval); 1799 if (ret) 1800 goto err; 1801 hw_cfg->gpio1.func = values[cs35l41->index]; 1802 hw_cfg->gpio1.valid = true; 1803 1804 property = "cirrus,gpio2-func"; 1805 ret = device_property_read_u32_array(physdev, property, values, nval); 1806 if (ret) 1807 goto err; 1808 hw_cfg->gpio2.func = values[cs35l41->index]; 1809 hw_cfg->gpio2.valid = true; 1810 1811 property = "cirrus,boost-peak-milliamp"; 1812 ret = device_property_read_u32_array(physdev, property, values, nval); 1813 if (ret == 0) 1814 hw_cfg->bst_ipk = values[cs35l41->index]; 1815 else 1816 hw_cfg->bst_ipk = -1; 1817 1818 property = "cirrus,boost-ind-nanohenry"; 1819 ret = device_property_read_u32_array(physdev, property, values, nval); 1820 if (ret == 0) 1821 hw_cfg->bst_ind = values[cs35l41->index]; 1822 else 1823 hw_cfg->bst_ind = -1; 1824 1825 property = "cirrus,boost-cap-microfarad"; 1826 ret = device_property_read_u32_array(physdev, property, values, nval); 1827 if (ret == 0) 1828 hw_cfg->bst_cap = values[cs35l41->index]; 1829 else 1830 hw_cfg->bst_cap = -1; 1831 1832 cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, cs35l41->index, nval, -1); 1833 1834 if (hw_cfg->bst_ind > 0 || hw_cfg->bst_cap > 0 || hw_cfg->bst_ipk > 0) 1835 hw_cfg->bst_type = CS35L41_INT_BOOST; 1836 else 1837 hw_cfg->bst_type = CS35L41_EXT_BOOST; 1838 1839 hw_cfg->valid = true; 1840 1841 return 0; 1842 err: 1843 dev_err(cs35l41->dev, "Failed property %s: %d\n", property, ret); 1844 hw_cfg->valid = false; 1845 hw_cfg->gpio1.valid = false; 1846 hw_cfg->gpio2.valid = false; 1847 acpi_dev_put(cs35l41->dacpi); 1848 1849 return ret; 1850 } 1851 1852 static int cs35l41_hda_read_acpi(struct cs35l41_hda *cs35l41, const char *hid, int id) 1853 { 1854 struct acpi_device *adev; 1855 struct device *physdev; 1856 struct spi_device *spi; 1857 const char *sub; 1858 int ret; 1859 1860 adev = acpi_dev_get_first_match_dev(hid, NULL, -1); 1861 if (!adev) { 1862 dev_err(cs35l41->dev, "Failed to find an ACPI device for %s\n", hid); 1863 return -ENODEV; 1864 } 1865 1866 cs35l41->dacpi = adev; 1867 physdev = get_device(acpi_get_first_physical_node(adev)); 1868 1869 sub = acpi_get_subsystem_id(ACPI_HANDLE(physdev)); 1870 if (IS_ERR(sub)) 1871 sub = NULL; 1872 cs35l41->acpi_subsystem_id = sub; 1873 1874 ret = cs35l41_add_dsd_properties(cs35l41, physdev, id, hid); 1875 if (!ret) { 1876 dev_info(cs35l41->dev, "Using extra _DSD properties, bypassing _DSD in ACPI\n"); 1877 goto out; 1878 } 1879 1880 ret = cs35l41_hda_parse_acpi(cs35l41, physdev, id); 1881 if (ret) { 1882 put_device(physdev); 1883 return ret; 1884 } 1885 out: 1886 put_device(physdev); 1887 1888 cs35l41->bypass_fw = false; 1889 if (cs35l41->control_bus == SPI) { 1890 spi = to_spi_device(cs35l41->dev); 1891 if (spi->max_speed_hz < CS35L41_MAX_ACCEPTABLE_SPI_SPEED_HZ) { 1892 dev_warn(cs35l41->dev, 1893 "SPI speed is too slow to support firmware download: %d Hz.\n", 1894 spi->max_speed_hz); 1895 cs35l41->bypass_fw = true; 1896 } 1897 } 1898 1899 return 0; 1900 } 1901 1902 int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int irq, 1903 struct regmap *regmap, enum control_bus control_bus) 1904 { 1905 unsigned int regid, reg_revid; 1906 struct cs35l41_hda *cs35l41; 1907 int ret; 1908 1909 BUILD_BUG_ON(ARRAY_SIZE(cs35l41_irqs) != ARRAY_SIZE(cs35l41_reg_irqs)); 1910 BUILD_BUG_ON(ARRAY_SIZE(cs35l41_irqs) != CS35L41_NUM_IRQ); 1911 1912 if (IS_ERR(regmap)) 1913 return PTR_ERR(regmap); 1914 1915 cs35l41 = devm_kzalloc(dev, sizeof(*cs35l41), GFP_KERNEL); 1916 if (!cs35l41) 1917 return -ENOMEM; 1918 1919 cs35l41->dev = dev; 1920 cs35l41->irq = irq; 1921 cs35l41->regmap = regmap; 1922 cs35l41->control_bus = control_bus; 1923 dev_set_drvdata(dev, cs35l41); 1924 1925 ret = cs35l41_hda_read_acpi(cs35l41, device_name, id); 1926 if (ret) 1927 return dev_err_probe(cs35l41->dev, ret, "Platform not supported\n"); 1928 1929 if (IS_ERR(cs35l41->reset_gpio)) { 1930 ret = PTR_ERR(cs35l41->reset_gpio); 1931 cs35l41->reset_gpio = NULL; 1932 if (ret == -EBUSY) { 1933 dev_info(cs35l41->dev, "Reset line busy, assuming shared reset\n"); 1934 } else { 1935 dev_err_probe(cs35l41->dev, ret, "Failed to get reset GPIO\n"); 1936 goto err; 1937 } 1938 } 1939 if (cs35l41->reset_gpio) { 1940 gpiod_set_value_cansleep(cs35l41->reset_gpio, 0); 1941 usleep_range(2000, 2100); 1942 gpiod_set_value_cansleep(cs35l41->reset_gpio, 1); 1943 } 1944 1945 usleep_range(2000, 2100); 1946 regmap_write(cs35l41->regmap, CS35L41_SFT_RESET, CS35L41_SOFTWARE_RESET); 1947 usleep_range(2000, 2100); 1948 1949 ret = cs35l41_wait_boot_done(cs35l41); 1950 if (ret) 1951 goto err; 1952 1953 ret = cs35l41_verify_id(cs35l41, ®id, ®_revid); 1954 if (ret) 1955 goto err; 1956 1957 ret = cs35l41_test_key_unlock(cs35l41->dev, cs35l41->regmap); 1958 if (ret) 1959 goto err; 1960 1961 ret = cs35l41_register_errata_patch(cs35l41->dev, cs35l41->regmap, reg_revid); 1962 if (ret) 1963 goto err; 1964 1965 ret = cs35l41_otp_unpack(cs35l41->dev, cs35l41->regmap); 1966 if (ret) { 1967 dev_err_probe(cs35l41->dev, ret, "OTP Unpack failed\n"); 1968 goto err; 1969 } 1970 1971 ret = cs35l41_test_key_lock(cs35l41->dev, cs35l41->regmap); 1972 if (ret) 1973 goto err; 1974 1975 ret = cs35l41_get_calibration(cs35l41); 1976 if (ret && ret != -ENOENT) 1977 goto err; 1978 1979 cs35l41_mute(cs35l41->dev, true); 1980 1981 INIT_WORK(&cs35l41->fw_load_work, cs35l41_fw_load_work); 1982 mutex_init(&cs35l41->fw_mutex); 1983 1984 pm_runtime_set_autosuspend_delay(cs35l41->dev, 3000); 1985 pm_runtime_use_autosuspend(cs35l41->dev); 1986 pm_runtime_mark_last_busy(cs35l41->dev); 1987 pm_runtime_set_active(cs35l41->dev); 1988 pm_runtime_get_noresume(cs35l41->dev); 1989 pm_runtime_enable(cs35l41->dev); 1990 1991 ret = cs35l41_hda_apply_properties(cs35l41); 1992 if (ret) 1993 goto err_pm; 1994 1995 pm_runtime_put_autosuspend(cs35l41->dev); 1996 1997 ret = component_add(cs35l41->dev, &cs35l41_hda_comp_ops); 1998 if (ret) { 1999 dev_err_probe(cs35l41->dev, ret, "Register component failed\n"); 2000 goto err_pm; 2001 } 2002 2003 dev_info(cs35l41->dev, "Cirrus Logic CS35L41 (%x), Revision: %02X\n", regid, reg_revid); 2004 2005 return 0; 2006 2007 err_pm: 2008 pm_runtime_dont_use_autosuspend(cs35l41->dev); 2009 pm_runtime_disable(cs35l41->dev); 2010 pm_runtime_put_noidle(cs35l41->dev); 2011 2012 err: 2013 if (cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type)) 2014 gpiod_set_value_cansleep(cs35l41->reset_gpio, 0); 2015 gpiod_put(cs35l41->reset_gpio); 2016 gpiod_put(cs35l41->cs_gpio); 2017 acpi_dev_put(cs35l41->dacpi); 2018 kfree(cs35l41->acpi_subsystem_id); 2019 2020 return ret; 2021 } 2022 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_probe, SND_HDA_SCODEC_CS35L41); 2023 2024 void cs35l41_hda_remove(struct device *dev) 2025 { 2026 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev); 2027 2028 component_del(cs35l41->dev, &cs35l41_hda_comp_ops); 2029 2030 pm_runtime_get_sync(cs35l41->dev); 2031 pm_runtime_dont_use_autosuspend(cs35l41->dev); 2032 pm_runtime_disable(cs35l41->dev); 2033 2034 if (cs35l41->halo_initialized) 2035 cs35l41_remove_dsp(cs35l41); 2036 2037 acpi_dev_put(cs35l41->dacpi); 2038 2039 pm_runtime_put_noidle(cs35l41->dev); 2040 2041 if (cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type)) 2042 gpiod_set_value_cansleep(cs35l41->reset_gpio, 0); 2043 gpiod_put(cs35l41->reset_gpio); 2044 gpiod_put(cs35l41->cs_gpio); 2045 kfree(cs35l41->acpi_subsystem_id); 2046 } 2047 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_remove, SND_HDA_SCODEC_CS35L41); 2048 2049 const struct dev_pm_ops cs35l41_hda_pm_ops = { 2050 RUNTIME_PM_OPS(cs35l41_runtime_suspend, cs35l41_runtime_resume, 2051 cs35l41_runtime_idle) 2052 .prepare = cs35l41_system_suspend_prep, 2053 SYSTEM_SLEEP_PM_OPS(cs35l41_system_suspend, cs35l41_system_resume) 2054 }; 2055 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_pm_ops, SND_HDA_SCODEC_CS35L41); 2056 2057 MODULE_DESCRIPTION("CS35L41 HDA Driver"); 2058 MODULE_IMPORT_NS(SND_HDA_CS_DSP_CONTROLS); 2059 MODULE_IMPORT_NS(SND_SOC_CS_AMP_LIB); 2060 MODULE_AUTHOR("Lucas Tanure, Cirrus Logic Inc, <tanureal@opensource.cirrus.com>"); 2061 MODULE_LICENSE("GPL"); 2062 MODULE_IMPORT_NS(FW_CS_DSP); 2063
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.