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