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