1 // SPDX-License-Identifier: GPL-2.0
3 // CS35l41 ALSA HDA audio driver
5 // Copyright 2021 Cirrus Logic, Inc.
7 // Author: Lucas Tanure <tanureal@opensource.cirrus.com>
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"
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"
24 #define CS35L41_FIRMWARE_ROOT "cirrus/"
25 #define CS35L41_PART "cs35l41"
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
37 static bool firmware_autostart = 1;
38 module_param(firmware_autostart, bool, 0444);
39 MODULE_PARM_DESC(firmware_autostart, "Allow automatic firmware download on boot"
40 "(0=Disable, 1=Enable) (default=1); ");
42 static const struct reg_sequence cs35l41_hda_config[] = {
43 { CS35L41_PLL_CLK_CTRL, 0x00000430 }, // 3072000Hz, BCLK Input, PLL_REFCLK_EN = 1
44 { CS35L41_DSP_CLK_CTRL, 0x00000003 }, // DSP CLK EN
45 { CS35L41_GLOBAL_CLK_CTRL, 0x00000003 }, // GLOBAL_FS = 48 kHz
46 { CS35L41_SP_ENABLES, 0x00010000 }, // ASP_RX1_EN = 1
47 { CS35L41_SP_RATE_CTRL, 0x00000021 }, // ASP_BCLK_FREQ = 3.072 MHz
48 { CS35L41_SP_FORMAT, 0x20200200 }, // 32 bits RX/TX slots, I2S, clk consumer
49 { CS35L41_SP_HIZ_CTRL, 0x00000002 }, // Hi-Z unused
50 { CS35L41_SP_TX_WL, 0x00000018 }, // 24 cycles/slot
51 { CS35L41_SP_RX_WL, 0x00000018 }, // 24 cycles/slot
52 { CS35L41_DAC_PCM1_SRC, 0x00000008 }, // DACPCM1_SRC = ASPRX1
53 { CS35L41_ASP_TX1_SRC, 0x00000018 }, // ASPTX1 SRC = VMON
54 { CS35L41_ASP_TX2_SRC, 0x00000019 }, // ASPTX2 SRC = IMON
55 { CS35L41_ASP_TX3_SRC, 0x00000032 }, // ASPTX3 SRC = ERRVOL
56 { CS35L41_ASP_TX4_SRC, 0x00000033 }, // ASPTX4 SRC = CLASSH_TGT
57 { CS35L41_DSP1_RX1_SRC, 0x00000008 }, // DSP1RX1 SRC = ASPRX1
58 { CS35L41_DSP1_RX2_SRC, 0x00000009 }, // DSP1RX2 SRC = ASPRX2
59 { CS35L41_DSP1_RX3_SRC, 0x00000018 }, // DSP1RX3 SRC = VMON
60 { CS35L41_DSP1_RX4_SRC, 0x00000019 }, // DSP1RX4 SRC = IMON
61 { CS35L41_DSP1_RX5_SRC, 0x00000020 }, // DSP1RX5 SRC = ERRVOL
64 static const struct reg_sequence cs35l41_hda_config_dsp[] = {
65 { CS35L41_PLL_CLK_CTRL, 0x00000430 }, // 3072000Hz, BCLK Input, PLL_REFCLK_EN = 1
66 { CS35L41_DSP_CLK_CTRL, 0x00000003 }, // DSP CLK EN
67 { CS35L41_GLOBAL_CLK_CTRL, 0x00000003 }, // GLOBAL_FS = 48 kHz
68 { CS35L41_SP_ENABLES, 0x00010001 }, // ASP_RX1_EN = 1, ASP_TX1_EN = 1
69 { CS35L41_SP_RATE_CTRL, 0x00000021 }, // ASP_BCLK_FREQ = 3.072 MHz
70 { CS35L41_SP_FORMAT, 0x20200200 }, // 32 bits RX/TX slots, I2S, clk consumer
71 { CS35L41_SP_HIZ_CTRL, 0x00000003 }, // Hi-Z unused/disabled
72 { CS35L41_SP_TX_WL, 0x00000018 }, // 24 cycles/slot
73 { CS35L41_SP_RX_WL, 0x00000018 }, // 24 cycles/slot
74 { CS35L41_DAC_PCM1_SRC, 0x00000032 }, // DACPCM1_SRC = ERR_VOL
75 { CS35L41_ASP_TX1_SRC, 0x00000018 }, // ASPTX1 SRC = VMON
76 { CS35L41_ASP_TX2_SRC, 0x00000019 }, // ASPTX2 SRC = IMON
77 { CS35L41_ASP_TX3_SRC, 0x00000028 }, // ASPTX3 SRC = VPMON
78 { CS35L41_ASP_TX4_SRC, 0x00000029 }, // ASPTX4 SRC = VBSTMON
79 { CS35L41_DSP1_RX1_SRC, 0x00000008 }, // DSP1RX1 SRC = ASPRX1
80 { CS35L41_DSP1_RX2_SRC, 0x00000008 }, // DSP1RX2 SRC = ASPRX1
81 { CS35L41_DSP1_RX3_SRC, 0x00000018 }, // DSP1RX3 SRC = VMON
82 { CS35L41_DSP1_RX4_SRC, 0x00000019 }, // DSP1RX4 SRC = IMON
83 { CS35L41_DSP1_RX5_SRC, 0x00000029 }, // DSP1RX5 SRC = VBSTMON
86 static const struct reg_sequence cs35l41_hda_unmute[] = {
87 { CS35L41_AMP_DIG_VOL_CTRL, 0x00008000 }, // AMP_HPF_PCM_EN = 1, AMP_VOL_PCM 0.0 dB
88 { CS35L41_AMP_GAIN_CTRL, 0x00000084 }, // AMP_GAIN_PCM 4.5 dB
91 static const struct reg_sequence cs35l41_hda_unmute_dsp[] = {
92 { CS35L41_AMP_DIG_VOL_CTRL, 0x00008000 }, // AMP_HPF_PCM_EN = 1, AMP_VOL_PCM 0.0 dB
93 { CS35L41_AMP_GAIN_CTRL, 0x00000233 }, // AMP_GAIN_PCM = 17.5dB AMP_GAIN_PDM = 19.5dB
96 static const struct reg_sequence cs35l41_hda_mute[] = {
97 { CS35L41_AMP_GAIN_CTRL, 0x00000000 }, // AMP_GAIN_PCM 0.5 dB
98 { CS35L41_AMP_DIG_VOL_CTRL, 0x0000A678 }, // AMP_HPF_PCM_EN = 1, AMP_VOL_PCM Mute
101 static void cs35l41_add_controls(struct cs35l41_hda *cs35l41)
103 struct hda_cs_dsp_ctl_info info;
105 info.device_name = cs35l41->amp_name;
106 info.fw_type = cs35l41->firmware_type;
107 info.card = cs35l41->codec->card;
109 hda_cs_dsp_add_controls(&cs35l41->cs_dsp, &info);
112 static const struct cs_dsp_client_ops client_ops = {
113 .control_remove = hda_cs_dsp_control_remove,
116 static int cs35l41_request_firmware_file(struct cs35l41_hda *cs35l41,
117 const struct firmware **firmware, char **filename,
118 const char *dir, const char *ssid, const char *amp_name,
119 int spkid, const char *filetype)
121 const char * const dsp_name = cs35l41->cs_dsp.name;
125 if (spkid > -1 && ssid && amp_name)
126 *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s-%s-spkid%d-%s.%s", dir, CS35L41_PART,
127 dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type],
128 ssid, spkid, amp_name, filetype);
129 else if (spkid > -1 && ssid)
130 *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s-%s-spkid%d.%s", dir, CS35L41_PART,
131 dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type],
132 ssid, spkid, filetype);
133 else if (ssid && amp_name)
134 *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s-%s-%s.%s", dir, CS35L41_PART,
135 dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type],
136 ssid, amp_name, filetype);
138 *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s-%s.%s", dir, CS35L41_PART,
139 dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type],
142 *filename = kasprintf(GFP_KERNEL, "%s%s-%s-%s.%s", dir, CS35L41_PART,
143 dsp_name, hda_cs_dsp_fw_ids[cs35l41->firmware_type],
146 if (*filename == NULL)
150 * Make sure that filename is lower-case and any non alpha-numeric
151 * characters except full stop and '/' are replaced with hyphens.
158 else if (c != '.' && c != '/')
163 ret = firmware_request_nowarn(firmware, *filename, cs35l41->dev);
165 dev_dbg(cs35l41->dev, "Failed to request '%s'\n", *filename);
173 static int cs35l41_request_firmware_files_spkid(struct cs35l41_hda *cs35l41,
174 const struct firmware **wmfw_firmware,
175 char **wmfw_filename,
176 const struct firmware **coeff_firmware,
177 char **coeff_filename)
181 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.wmfw */
182 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
183 CS35L41_FIRMWARE_ROOT,
184 cs35l41->acpi_subsystem_id, cs35l41->amp_name,
185 cs35l41->speaker_id, "wmfw");
187 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */
188 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
189 CS35L41_FIRMWARE_ROOT,
190 cs35l41->acpi_subsystem_id, cs35l41->amp_name,
191 cs35l41->speaker_id, "bin");
198 /* try cirrus/part-dspN-fwtype-sub<-ampname>.wmfw */
199 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
200 CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
201 cs35l41->amp_name, -1, "wmfw");
203 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */
204 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
205 CS35L41_FIRMWARE_ROOT,
206 cs35l41->acpi_subsystem_id, cs35l41->amp_name,
207 cs35l41->speaker_id, "bin");
214 /* try cirrus/part-dspN-fwtype-sub<-spkidN>.wmfw */
215 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
216 CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
217 NULL, cs35l41->speaker_id, "wmfw");
219 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */
220 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
221 CS35L41_FIRMWARE_ROOT,
222 cs35l41->acpi_subsystem_id,
223 cs35l41->amp_name, cs35l41->speaker_id, "bin");
225 /* try cirrus/part-dspN-fwtype-sub<-spkidN>.bin */
226 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware,
227 coeff_filename, CS35L41_FIRMWARE_ROOT,
228 cs35l41->acpi_subsystem_id, NULL,
229 cs35l41->speaker_id, "bin");
236 /* try cirrus/part-dspN-fwtype-sub.wmfw */
237 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
238 CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
241 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */
242 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
243 CS35L41_FIRMWARE_ROOT,
244 cs35l41->acpi_subsystem_id, cs35l41->amp_name,
245 cs35l41->speaker_id, "bin");
247 /* try cirrus/part-dspN-fwtype-sub<-spkidN>.bin */
248 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware,
249 coeff_filename, CS35L41_FIRMWARE_ROOT,
250 cs35l41->acpi_subsystem_id, NULL,
251 cs35l41->speaker_id, "bin");
258 release_firmware(*wmfw_firmware);
259 kfree(*wmfw_filename);
263 static int cs35l41_fallback_firmware_file(struct cs35l41_hda *cs35l41,
264 const struct firmware **wmfw_firmware,
265 char **wmfw_filename,
266 const struct firmware **coeff_firmware,
267 char **coeff_filename)
271 /* Handle fallback */
272 dev_warn(cs35l41->dev, "Falling back to default firmware.\n");
274 /* fallback try cirrus/part-dspN-fwtype.wmfw */
275 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
276 CS35L41_FIRMWARE_ROOT, NULL, NULL, -1, "wmfw");
280 /* fallback try cirrus/part-dspN-fwtype.bin */
281 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
282 CS35L41_FIRMWARE_ROOT, NULL, NULL, -1, "bin");
284 release_firmware(*wmfw_firmware);
285 kfree(*wmfw_filename);
291 dev_warn(cs35l41->dev, "Unable to find firmware and tuning\n");
295 static int cs35l41_request_firmware_files(struct cs35l41_hda *cs35l41,
296 const struct firmware **wmfw_firmware,
297 char **wmfw_filename,
298 const struct firmware **coeff_firmware,
299 char **coeff_filename)
303 if (cs35l41->speaker_id > -1) {
304 ret = cs35l41_request_firmware_files_spkid(cs35l41, wmfw_firmware, wmfw_filename,
305 coeff_firmware, coeff_filename);
309 /* try cirrus/part-dspN-fwtype-sub<-ampname>.wmfw */
310 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
311 CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
312 cs35l41->amp_name, -1, "wmfw");
314 /* try cirrus/part-dspN-fwtype-sub<-ampname>.bin */
315 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
316 CS35L41_FIRMWARE_ROOT,
317 cs35l41->acpi_subsystem_id, cs35l41->amp_name,
325 /* try cirrus/part-dspN-fwtype-sub.wmfw */
326 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
327 CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
330 /* try cirrus/part-dspN-fwtype-sub<-ampname>.bin */
331 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
332 CS35L41_FIRMWARE_ROOT,
333 cs35l41->acpi_subsystem_id,
334 cs35l41->amp_name, -1, "bin");
336 /* try cirrus/part-dspN-fwtype-sub.bin */
337 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
338 CS35L41_FIRMWARE_ROOT,
339 cs35l41->acpi_subsystem_id, NULL, -1,
347 /* if all attempts at finding firmware fail, try fallback */
353 release_firmware(*wmfw_firmware);
354 kfree(*wmfw_filename);
356 return cs35l41_fallback_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
357 coeff_firmware, coeff_filename);
360 #if IS_ENABLED(CONFIG_EFI)
361 static int cs35l41_apply_calibration(struct cs35l41_hda *cs35l41, __be32 ambient, __be32 r0,
362 __be32 status, __be32 checksum)
366 ret = hda_cs_dsp_write_ctl(&cs35l41->cs_dsp, CAL_AMBIENT_DSP_CTL_NAME, CAL_DSP_CTL_TYPE,
367 CAL_DSP_CTL_ALG, &ambient, 4);
369 dev_err(cs35l41->dev, "Cannot Write Control: %s - %d\n", CAL_AMBIENT_DSP_CTL_NAME,
373 ret = hda_cs_dsp_write_ctl(&cs35l41->cs_dsp, CAL_R_DSP_CTL_NAME, CAL_DSP_CTL_TYPE,
374 CAL_DSP_CTL_ALG, &r0, 4);
376 dev_err(cs35l41->dev, "Cannot Write Control: %s - %d\n", CAL_R_DSP_CTL_NAME, ret);
379 ret = hda_cs_dsp_write_ctl(&cs35l41->cs_dsp, CAL_STATUS_DSP_CTL_NAME, CAL_DSP_CTL_TYPE,
380 CAL_DSP_CTL_ALG, &status, 4);
382 dev_err(cs35l41->dev, "Cannot Write Control: %s - %d\n", CAL_STATUS_DSP_CTL_NAME,
386 ret = hda_cs_dsp_write_ctl(&cs35l41->cs_dsp, CAL_CHECKSUM_DSP_CTL_NAME, CAL_DSP_CTL_TYPE,
387 CAL_DSP_CTL_ALG, &checksum, 4);
389 dev_err(cs35l41->dev, "Cannot Write Control: %s - %d\n", CAL_CHECKSUM_DSP_CTL_NAME,
397 static int cs35l41_save_calibration(struct cs35l41_hda *cs35l41)
399 static efi_guid_t efi_guid = EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d, 0x93, 0xfe,
400 0x5a, 0xa3, 0x5d, 0xb3);
401 static efi_char16_t efi_name[] = L"CirrusSmartAmpCalibrationData";
402 const struct cs35l41_amp_efi_data *efi_data;
403 const struct cs35l41_amp_cal_data *cl;
404 unsigned long data_size = 0;
410 /* Get real size of UEFI variable */
411 status = efi.get_variable(efi_name, &efi_guid, &attr, &data_size, data);
412 if (status == EFI_BUFFER_TOO_SMALL) {
414 /* Allocate data buffer of data_size bytes */
415 data = vmalloc(data_size);
418 /* Get variable contents into buffer */
419 status = efi.get_variable(efi_name, &efi_guid, &attr, &data_size, data);
420 if (status == EFI_SUCCESS) {
421 efi_data = (struct cs35l41_amp_efi_data *)data;
422 dev_dbg(cs35l41->dev, "Calibration: Size=%d, Amp Count=%d\n",
423 efi_data->size, efi_data->count);
424 if (efi_data->count > cs35l41->index) {
425 cl = &efi_data->data[cs35l41->index];
426 dev_dbg(cs35l41->dev,
427 "Calibration: Ambient=%02x, Status=%02x, R0=%d\n",
428 cl->calAmbient, cl->calStatus, cl->calR);
430 /* Calibration can only be applied whilst the DSP is not running */
431 ret = cs35l41_apply_calibration(cs35l41,
432 cpu_to_be32(cl->calAmbient),
433 cpu_to_be32(cl->calR),
434 cpu_to_be32(cl->calStatus),
435 cpu_to_be32(cl->calR + 1));
443 static int cs35l41_save_calibration(struct cs35l41_hda *cs35l41)
445 dev_warn(cs35l41->dev, "Calibration not supported without EFI support.\n");
450 static int cs35l41_init_dsp(struct cs35l41_hda *cs35l41)
452 const struct firmware *coeff_firmware = NULL;
453 const struct firmware *wmfw_firmware = NULL;
454 struct cs_dsp *dsp = &cs35l41->cs_dsp;
455 char *coeff_filename = NULL;
456 char *wmfw_filename = NULL;
459 if (!cs35l41->halo_initialized) {
460 cs35l41_configure_cs_dsp(cs35l41->dev, cs35l41->regmap, dsp);
461 dsp->client_ops = &client_ops;
463 ret = cs_dsp_halo_init(&cs35l41->cs_dsp);
466 cs35l41->halo_initialized = true;
469 ret = cs35l41_request_firmware_files(cs35l41, &wmfw_firmware, &wmfw_filename,
470 &coeff_firmware, &coeff_filename);
474 dev_dbg(cs35l41->dev, "Loading WMFW Firmware: %s\n", wmfw_filename);
476 dev_dbg(cs35l41->dev, "Loading Coefficient File: %s\n", coeff_filename);
478 dev_warn(cs35l41->dev, "No Coefficient File available.\n");
480 ret = cs_dsp_power_up(dsp, wmfw_firmware, wmfw_filename, coeff_firmware, coeff_filename,
481 hda_cs_dsp_fw_ids[cs35l41->firmware_type]);
485 cs35l41_add_controls(cs35l41);
487 ret = cs35l41_save_calibration(cs35l41);
490 release_firmware(wmfw_firmware);
491 release_firmware(coeff_firmware);
492 kfree(wmfw_filename);
493 kfree(coeff_filename);
498 static void cs35l41_shutdown_dsp(struct cs35l41_hda *cs35l41)
500 struct cs_dsp *dsp = &cs35l41->cs_dsp;
503 cs_dsp_power_down(dsp);
504 cs35l41->firmware_running = false;
505 dev_dbg(cs35l41->dev, "Unloaded Firmware\n");
508 static void cs35l41_remove_dsp(struct cs35l41_hda *cs35l41)
510 struct cs_dsp *dsp = &cs35l41->cs_dsp;
512 cancel_work_sync(&cs35l41->fw_load_work);
514 mutex_lock(&cs35l41->fw_mutex);
515 cs35l41_shutdown_dsp(cs35l41);
517 cs35l41->halo_initialized = false;
518 mutex_unlock(&cs35l41->fw_mutex);
521 /* Protection release cycle to get the speaker out of Safe-Mode */
522 static void cs35l41_error_release(struct device *dev, struct regmap *regmap, unsigned int mask)
524 regmap_write(regmap, CS35L41_PROTECT_REL_ERR_IGN, 0);
525 regmap_set_bits(regmap, CS35L41_PROTECT_REL_ERR_IGN, mask);
526 regmap_clear_bits(regmap, CS35L41_PROTECT_REL_ERR_IGN, mask);
529 /* Clear all errors to release safe mode. Global Enable must be cleared first. */
530 static void cs35l41_irq_release(struct cs35l41_hda *cs35l41)
532 cs35l41_error_release(cs35l41->dev, cs35l41->regmap, cs35l41->irq_errors);
533 cs35l41->irq_errors = 0;
536 static void cs35l41_hda_play_start(struct device *dev)
538 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
539 struct regmap *reg = cs35l41->regmap;
541 dev_dbg(dev, "Play (Start)\n");
543 if (cs35l41->playback_started) {
544 dev_dbg(dev, "Playback already started.");
548 cs35l41->playback_started = true;
550 if (cs35l41->firmware_running) {
551 regmap_multi_reg_write(reg, cs35l41_hda_config_dsp,
552 ARRAY_SIZE(cs35l41_hda_config_dsp));
553 regmap_update_bits(reg, CS35L41_PWR_CTRL2,
554 CS35L41_VMON_EN_MASK | CS35L41_IMON_EN_MASK,
555 1 << CS35L41_VMON_EN_SHIFT | 1 << CS35L41_IMON_EN_SHIFT);
556 cs35l41_set_cspl_mbox_cmd(cs35l41->dev, reg, CSPL_MBOX_CMD_RESUME);
558 regmap_multi_reg_write(reg, cs35l41_hda_config, ARRAY_SIZE(cs35l41_hda_config));
560 regmap_update_bits(reg, CS35L41_PWR_CTRL2, CS35L41_AMP_EN_MASK, 1 << CS35L41_AMP_EN_SHIFT);
561 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST)
562 regmap_write(reg, CS35L41_GPIO1_CTRL1, 0x00008001);
566 static void cs35l41_hda_play_done(struct device *dev)
568 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
569 struct regmap *reg = cs35l41->regmap;
571 dev_dbg(dev, "Play (Complete)\n");
573 cs35l41_global_enable(dev, reg, cs35l41->hw_cfg.bst_type, 1,
574 cs35l41->firmware_running);
575 if (cs35l41->firmware_running) {
576 regmap_multi_reg_write(reg, cs35l41_hda_unmute_dsp,
577 ARRAY_SIZE(cs35l41_hda_unmute_dsp));
579 regmap_multi_reg_write(reg, cs35l41_hda_unmute,
580 ARRAY_SIZE(cs35l41_hda_unmute));
584 static void cs35l41_hda_pause_start(struct device *dev)
586 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
587 struct regmap *reg = cs35l41->regmap;
589 dev_dbg(dev, "Pause (Start)\n");
591 regmap_multi_reg_write(reg, cs35l41_hda_mute, ARRAY_SIZE(cs35l41_hda_mute));
592 cs35l41_global_enable(dev, reg, cs35l41->hw_cfg.bst_type, 0,
593 cs35l41->firmware_running);
596 static void cs35l41_hda_pause_done(struct device *dev)
598 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
599 struct regmap *reg = cs35l41->regmap;
601 dev_dbg(dev, "Pause (Complete)\n");
603 regmap_update_bits(reg, CS35L41_PWR_CTRL2, CS35L41_AMP_EN_MASK, 0 << CS35L41_AMP_EN_SHIFT);
604 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST)
605 regmap_write(reg, CS35L41_GPIO1_CTRL1, 0x00000001);
606 if (cs35l41->firmware_running) {
607 cs35l41_set_cspl_mbox_cmd(dev, reg, CSPL_MBOX_CMD_PAUSE);
608 regmap_update_bits(reg, CS35L41_PWR_CTRL2,
609 CS35L41_VMON_EN_MASK | CS35L41_IMON_EN_MASK,
610 0 << CS35L41_VMON_EN_SHIFT | 0 << CS35L41_IMON_EN_SHIFT);
612 cs35l41_irq_release(cs35l41);
613 cs35l41->playback_started = false;
616 static void cs35l41_hda_pre_playback_hook(struct device *dev, int action)
618 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
621 case HDA_GEN_PCM_ACT_CLEANUP:
622 mutex_lock(&cs35l41->fw_mutex);
623 cs35l41_hda_pause_start(dev);
624 mutex_unlock(&cs35l41->fw_mutex);
630 static void cs35l41_hda_playback_hook(struct device *dev, int action)
632 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
635 case HDA_GEN_PCM_ACT_OPEN:
637 * All amps must be resumed before we can start playing back.
638 * This ensures, for external boost, that all amps are in AMP_SAFE mode.
639 * Do this in HDA_GEN_PCM_ACT_OPEN, since this is run prior to any of the
642 pm_runtime_get_sync(dev);
644 case HDA_GEN_PCM_ACT_PREPARE:
645 mutex_lock(&cs35l41->fw_mutex);
646 cs35l41_hda_play_start(dev);
647 mutex_unlock(&cs35l41->fw_mutex);
649 case HDA_GEN_PCM_ACT_CLEANUP:
650 mutex_lock(&cs35l41->fw_mutex);
651 cs35l41_hda_pause_done(dev);
652 mutex_unlock(&cs35l41->fw_mutex);
654 case HDA_GEN_PCM_ACT_CLOSE:
655 mutex_lock(&cs35l41->fw_mutex);
656 if (!cs35l41->firmware_running && cs35l41->request_fw_load &&
657 !cs35l41->fw_request_ongoing) {
658 dev_info(dev, "Requesting Firmware Load after HDA_GEN_PCM_ACT_CLOSE\n");
659 cs35l41->fw_request_ongoing = true;
660 schedule_work(&cs35l41->fw_load_work);
662 mutex_unlock(&cs35l41->fw_mutex);
665 * Playback must be finished for all amps before we start runtime suspend.
666 * This ensures no amps are playing back when we start putting them to sleep.
668 pm_runtime_mark_last_busy(dev);
669 pm_runtime_put_autosuspend(dev);
676 static void cs35l41_hda_post_playback_hook(struct device *dev, int action)
678 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
681 case HDA_GEN_PCM_ACT_PREPARE:
682 mutex_lock(&cs35l41->fw_mutex);
683 cs35l41_hda_play_done(dev);
684 mutex_unlock(&cs35l41->fw_mutex);
691 static int cs35l41_hda_channel_map(struct device *dev, unsigned int tx_num, unsigned int *tx_slot,
692 unsigned int rx_num, unsigned int *rx_slot)
694 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
695 static const char * const channel_name[] = { "L", "R" };
697 if (!cs35l41->amp_name) {
698 if (*rx_slot >= ARRAY_SIZE(channel_name))
701 cs35l41->amp_name = devm_kasprintf(cs35l41->dev, GFP_KERNEL, "%s%d",
702 channel_name[*rx_slot], cs35l41->channel_index);
703 if (!cs35l41->amp_name)
707 return cs35l41_set_channels(cs35l41->dev, cs35l41->regmap, tx_num, tx_slot, rx_num,
711 static int cs35l41_ready_for_reset(struct cs35l41_hda *cs35l41)
715 mutex_lock(&cs35l41->fw_mutex);
716 if (cs35l41->firmware_running) {
718 regcache_cache_only(cs35l41->regmap, false);
720 ret = cs35l41_exit_hibernate(cs35l41->dev, cs35l41->regmap);
722 dev_warn(cs35l41->dev, "Unable to exit Hibernate.");
726 /* Test key needs to be unlocked to allow the OTP settings to re-apply */
727 cs35l41_test_key_unlock(cs35l41->dev, cs35l41->regmap);
728 ret = regcache_sync(cs35l41->regmap);
729 cs35l41_test_key_lock(cs35l41->dev, cs35l41->regmap);
731 dev_err(cs35l41->dev, "Failed to restore register cache: %d\n", ret);
735 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST)
736 cs35l41_init_boost(cs35l41->dev, cs35l41->regmap, &cs35l41->hw_cfg);
738 cs35l41_shutdown_dsp(cs35l41);
739 cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type);
742 regcache_cache_only(cs35l41->regmap, true);
743 regcache_mark_dirty(cs35l41->regmap);
745 mutex_unlock(&cs35l41->fw_mutex);
750 static int cs35l41_system_suspend_prep(struct device *dev)
752 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
754 dev_dbg(cs35l41->dev, "System Suspend Prepare\n");
756 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
757 dev_err_once(cs35l41->dev, "System Suspend not supported\n");
758 return 0; /* don't block the whole system suspend */
761 mutex_lock(&cs35l41->fw_mutex);
762 if (cs35l41->playback_started)
763 cs35l41_hda_pause_start(dev);
764 mutex_unlock(&cs35l41->fw_mutex);
769 static int cs35l41_system_suspend(struct device *dev)
771 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
774 dev_dbg(cs35l41->dev, "System Suspend\n");
776 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
777 dev_err_once(cs35l41->dev, "System Suspend not supported\n");
778 return 0; /* don't block the whole system suspend */
781 mutex_lock(&cs35l41->fw_mutex);
782 if (cs35l41->playback_started)
783 cs35l41_hda_pause_done(dev);
784 mutex_unlock(&cs35l41->fw_mutex);
786 ret = pm_runtime_force_suspend(dev);
788 dev_err(dev, "System Suspend Failed, unable to runtime suspend: %d\n", ret);
792 /* Shutdown DSP before system suspend */
793 ret = cs35l41_ready_for_reset(cs35l41);
796 dev_err(dev, "System Suspend Failed, not ready for Reset: %d\n", ret);
799 * Reset GPIO may be shared, so cannot reset here.
800 * However beyond this point, amps may be powered down.
805 static int cs35l41_system_resume(struct device *dev)
807 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
810 dev_dbg(cs35l41->dev, "System Resume\n");
812 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
813 dev_err_once(cs35l41->dev, "System Resume not supported\n");
814 return 0; /* don't block the whole system resume */
817 if (cs35l41->reset_gpio) {
818 usleep_range(2000, 2100);
819 gpiod_set_value_cansleep(cs35l41->reset_gpio, 1);
822 usleep_range(2000, 2100);
824 ret = pm_runtime_force_resume(dev);
826 dev_err(dev, "System Resume Failed: Unable to runtime resume: %d\n", ret);
830 mutex_lock(&cs35l41->fw_mutex);
832 if (cs35l41->request_fw_load && !cs35l41->fw_request_ongoing) {
833 cs35l41->fw_request_ongoing = true;
834 schedule_work(&cs35l41->fw_load_work);
836 mutex_unlock(&cs35l41->fw_mutex);
841 static int cs35l41_runtime_idle(struct device *dev)
843 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
845 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH)
846 return -EBUSY; /* suspend not supported yet on this model */
850 static int cs35l41_runtime_suspend(struct device *dev)
852 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
855 dev_dbg(cs35l41->dev, "Runtime Suspend\n");
857 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
858 dev_dbg(cs35l41->dev, "Runtime Suspend not supported\n");
862 mutex_lock(&cs35l41->fw_mutex);
864 if (cs35l41->firmware_running) {
865 ret = cs35l41_enter_hibernate(cs35l41->dev, cs35l41->regmap,
866 cs35l41->hw_cfg.bst_type);
870 cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type);
873 regcache_cache_only(cs35l41->regmap, true);
874 regcache_mark_dirty(cs35l41->regmap);
877 mutex_unlock(&cs35l41->fw_mutex);
882 static int cs35l41_runtime_resume(struct device *dev)
884 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
887 dev_dbg(cs35l41->dev, "Runtime Resume\n");
889 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
890 dev_dbg(cs35l41->dev, "Runtime Resume not supported\n");
894 mutex_lock(&cs35l41->fw_mutex);
896 regcache_cache_only(cs35l41->regmap, false);
898 if (cs35l41->firmware_running) {
899 ret = cs35l41_exit_hibernate(cs35l41->dev, cs35l41->regmap);
901 dev_warn(cs35l41->dev, "Unable to exit Hibernate.");
906 /* Test key needs to be unlocked to allow the OTP settings to re-apply */
907 cs35l41_test_key_unlock(cs35l41->dev, cs35l41->regmap);
908 ret = regcache_sync(cs35l41->regmap);
909 cs35l41_test_key_lock(cs35l41->dev, cs35l41->regmap);
911 dev_err(cs35l41->dev, "Failed to restore register cache: %d\n", ret);
915 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST)
916 cs35l41_init_boost(cs35l41->dev, cs35l41->regmap, &cs35l41->hw_cfg);
919 mutex_unlock(&cs35l41->fw_mutex);
924 static int cs35l41_smart_amp(struct cs35l41_hda *cs35l41)
929 ret = cs35l41_init_dsp(cs35l41);
931 dev_warn(cs35l41->dev, "Cannot Initialize Firmware. Error: %d\n", ret);
935 ret = cs35l41_write_fs_errata(cs35l41->dev, cs35l41->regmap);
937 dev_err(cs35l41->dev, "Cannot Write FS Errata: %d\n", ret);
941 ret = cs_dsp_run(&cs35l41->cs_dsp);
943 dev_err(cs35l41->dev, "Fail to start dsp: %d\n", ret);
947 ret = read_poll_timeout(hda_cs_dsp_read_ctl, ret,
948 be32_to_cpu(halo_sts) == HALO_STATE_CODE_RUN,
949 1000, 15000, false, &cs35l41->cs_dsp, HALO_STATE_DSP_CTL_NAME,
950 HALO_STATE_DSP_CTL_TYPE, HALO_STATE_DSP_CTL_ALG,
951 &halo_sts, sizeof(halo_sts));
954 dev_err(cs35l41->dev, "Timeout waiting for HALO Core to start. State: %u\n",
959 ret = cs35l41_set_cspl_mbox_cmd(cs35l41->dev, cs35l41->regmap, CSPL_MBOX_CMD_PAUSE);
961 dev_err(cs35l41->dev, "Error waiting for DSP to pause: %u\n", ret);
965 cs35l41->firmware_running = true;
970 cs35l41_shutdown_dsp(cs35l41);
974 static void cs35l41_load_firmware(struct cs35l41_hda *cs35l41, bool load)
976 if (cs35l41->firmware_running && !load) {
977 dev_dbg(cs35l41->dev, "Unloading Firmware\n");
978 cs35l41_shutdown_dsp(cs35l41);
979 } else if (!cs35l41->firmware_running && load) {
980 dev_dbg(cs35l41->dev, "Loading Firmware\n");
981 cs35l41_smart_amp(cs35l41);
983 dev_dbg(cs35l41->dev, "Unable to Load firmware.\n");
987 static int cs35l41_fw_load_ctl_get(struct snd_kcontrol *kcontrol,
988 struct snd_ctl_elem_value *ucontrol)
990 struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol);
992 ucontrol->value.integer.value[0] = cs35l41->request_fw_load;
996 static void cs35l41_fw_load_work(struct work_struct *work)
998 struct cs35l41_hda *cs35l41 = container_of(work, struct cs35l41_hda, fw_load_work);
1000 pm_runtime_get_sync(cs35l41->dev);
1002 mutex_lock(&cs35l41->fw_mutex);
1004 /* Recheck if playback is ongoing, mutex will block playback during firmware loading */
1005 if (cs35l41->playback_started)
1006 dev_err(cs35l41->dev, "Cannot Load/Unload firmware during Playback. Retrying...\n");
1008 cs35l41_load_firmware(cs35l41, cs35l41->request_fw_load);
1010 cs35l41->fw_request_ongoing = false;
1011 mutex_unlock(&cs35l41->fw_mutex);
1013 pm_runtime_mark_last_busy(cs35l41->dev);
1014 pm_runtime_put_autosuspend(cs35l41->dev);
1017 static int cs35l41_fw_load_ctl_put(struct snd_kcontrol *kcontrol,
1018 struct snd_ctl_elem_value *ucontrol)
1020 struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol);
1022 if (cs35l41->request_fw_load == ucontrol->value.integer.value[0])
1025 if (cs35l41->fw_request_ongoing) {
1026 dev_dbg(cs35l41->dev, "Existing request not complete\n");
1030 /* Check if playback is ongoing when initial request is made */
1031 if (cs35l41->playback_started) {
1032 dev_err(cs35l41->dev, "Cannot Load/Unload firmware during Playback\n");
1036 cs35l41->fw_request_ongoing = true;
1037 cs35l41->request_fw_load = ucontrol->value.integer.value[0];
1038 schedule_work(&cs35l41->fw_load_work);
1043 static int cs35l41_fw_type_ctl_get(struct snd_kcontrol *kcontrol,
1044 struct snd_ctl_elem_value *ucontrol)
1046 struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol);
1048 ucontrol->value.enumerated.item[0] = cs35l41->firmware_type;
1053 static int cs35l41_fw_type_ctl_put(struct snd_kcontrol *kcontrol,
1054 struct snd_ctl_elem_value *ucontrol)
1056 struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol);
1058 if (ucontrol->value.enumerated.item[0] < HDA_CS_DSP_NUM_FW) {
1059 if (cs35l41->firmware_type != ucontrol->value.enumerated.item[0]) {
1060 cs35l41->firmware_type = ucontrol->value.enumerated.item[0];
1070 static int cs35l41_fw_type_ctl_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1072 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(hda_cs_dsp_fw_ids), hda_cs_dsp_fw_ids);
1075 static int cs35l41_create_controls(struct cs35l41_hda *cs35l41)
1077 char fw_type_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1078 char fw_load_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1079 struct snd_kcontrol_new fw_type_ctl = {
1080 .name = fw_type_ctl_name,
1081 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1082 .info = cs35l41_fw_type_ctl_info,
1083 .get = cs35l41_fw_type_ctl_get,
1084 .put = cs35l41_fw_type_ctl_put,
1086 struct snd_kcontrol_new fw_load_ctl = {
1087 .name = fw_load_ctl_name,
1088 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1089 .info = snd_ctl_boolean_mono_info,
1090 .get = cs35l41_fw_load_ctl_get,
1091 .put = cs35l41_fw_load_ctl_put,
1095 scnprintf(fw_type_ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s DSP1 Firmware Type",
1097 scnprintf(fw_load_ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s DSP1 Firmware Load",
1100 ret = snd_ctl_add(cs35l41->codec->card, snd_ctl_new1(&fw_type_ctl, cs35l41));
1102 dev_err(cs35l41->dev, "Failed to add KControl %s = %d\n", fw_type_ctl.name, ret);
1106 dev_dbg(cs35l41->dev, "Added Control %s\n", fw_type_ctl.name);
1108 ret = snd_ctl_add(cs35l41->codec->card, snd_ctl_new1(&fw_load_ctl, cs35l41));
1110 dev_err(cs35l41->dev, "Failed to add KControl %s = %d\n", fw_load_ctl.name, ret);
1114 dev_dbg(cs35l41->dev, "Added Control %s\n", fw_load_ctl.name);
1119 static int cs35l41_hda_bind(struct device *dev, struct device *master, void *master_data)
1121 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
1122 struct hda_component *comps = master_data;
1123 unsigned int sleep_flags;
1126 if (!comps || cs35l41->index < 0 || cs35l41->index >= HDA_MAX_COMPONENTS)
1129 comps = &comps[cs35l41->index];
1133 pm_runtime_get_sync(dev);
1135 mutex_lock(&cs35l41->fw_mutex);
1138 if (!cs35l41->acpi_subsystem_id)
1139 cs35l41->acpi_subsystem_id = kasprintf(GFP_KERNEL, "%.8x",
1140 comps->codec->core.subsystem_id);
1141 cs35l41->codec = comps->codec;
1142 strscpy(comps->name, dev_name(dev), sizeof(comps->name));
1144 cs35l41->firmware_type = HDA_CS_DSP_FW_SPK_PROT;
1146 if (firmware_autostart) {
1147 dev_dbg(cs35l41->dev, "Firmware Autostart.\n");
1148 cs35l41->request_fw_load = true;
1149 if (cs35l41_smart_amp(cs35l41) < 0)
1150 dev_warn(cs35l41->dev, "Cannot Run Firmware, reverting to dsp bypass...\n");
1152 dev_dbg(cs35l41->dev, "Firmware Autostart is disabled.\n");
1155 ret = cs35l41_create_controls(cs35l41);
1157 comps->playback_hook = cs35l41_hda_playback_hook;
1158 comps->pre_playback_hook = cs35l41_hda_pre_playback_hook;
1159 comps->post_playback_hook = cs35l41_hda_post_playback_hook;
1161 mutex_unlock(&cs35l41->fw_mutex);
1163 sleep_flags = lock_system_sleep();
1164 if (!device_link_add(&comps->codec->core.dev, cs35l41->dev, DL_FLAG_STATELESS))
1165 dev_warn(dev, "Unable to create device link\n");
1166 unlock_system_sleep(sleep_flags);
1168 pm_runtime_mark_last_busy(dev);
1169 pm_runtime_put_autosuspend(dev);
1171 dev_info(cs35l41->dev,
1172 "CS35L41 Bound - SSID: %s, BST: %d, VSPK: %d, CH: %c, FW EN: %d, SPKID: %d\n",
1173 cs35l41->acpi_subsystem_id, cs35l41->hw_cfg.bst_type,
1174 cs35l41->hw_cfg.gpio1.func == CS35l41_VSPK_SWITCH,
1175 cs35l41->hw_cfg.spk_pos ? 'R' : 'L',
1176 cs35l41->firmware_running, cs35l41->speaker_id);
1181 static void cs35l41_hda_unbind(struct device *dev, struct device *master, void *master_data)
1183 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
1184 struct hda_component *comps = master_data;
1185 unsigned int sleep_flags;
1187 if (comps[cs35l41->index].dev == dev) {
1188 memset(&comps[cs35l41->index], 0, sizeof(*comps));
1189 sleep_flags = lock_system_sleep();
1190 device_link_remove(&comps->codec->core.dev, cs35l41->dev);
1191 unlock_system_sleep(sleep_flags);
1195 static const struct component_ops cs35l41_hda_comp_ops = {
1196 .bind = cs35l41_hda_bind,
1197 .unbind = cs35l41_hda_unbind,
1200 static irqreturn_t cs35l41_bst_short_err(int irq, void *data)
1202 struct cs35l41_hda *cs35l41 = data;
1204 dev_crit_ratelimited(cs35l41->dev, "LBST Error\n");
1205 set_bit(CS35L41_BST_SHORT_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1210 static irqreturn_t cs35l41_bst_dcm_uvp_err(int irq, void *data)
1212 struct cs35l41_hda *cs35l41 = data;
1214 dev_crit_ratelimited(cs35l41->dev, "DCM VBST Under Voltage Error\n");
1215 set_bit(CS35L41_BST_UVP_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1220 static irqreturn_t cs35l41_bst_ovp_err(int irq, void *data)
1222 struct cs35l41_hda *cs35l41 = data;
1224 dev_crit_ratelimited(cs35l41->dev, "VBST Over Voltage error\n");
1225 set_bit(CS35L41_BST_OVP_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1230 static irqreturn_t cs35l41_temp_err(int irq, void *data)
1232 struct cs35l41_hda *cs35l41 = data;
1234 dev_crit_ratelimited(cs35l41->dev, "Over temperature error\n");
1235 set_bit(CS35L41_TEMP_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1240 static irqreturn_t cs35l41_temp_warn(int irq, void *data)
1242 struct cs35l41_hda *cs35l41 = data;
1244 dev_crit_ratelimited(cs35l41->dev, "Over temperature warning\n");
1245 set_bit(CS35L41_TEMP_WARN_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1250 static irqreturn_t cs35l41_amp_short(int irq, void *data)
1252 struct cs35l41_hda *cs35l41 = data;
1254 dev_crit_ratelimited(cs35l41->dev, "Amp short error\n");
1255 set_bit(CS35L41_AMP_SHORT_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1260 static const struct cs35l41_irq cs35l41_irqs[] = {
1261 CS35L41_IRQ(BST_OVP_ERR, "Boost Overvoltage Error", cs35l41_bst_ovp_err),
1262 CS35L41_IRQ(BST_DCM_UVP_ERR, "Boost Undervoltage Error", cs35l41_bst_dcm_uvp_err),
1263 CS35L41_IRQ(BST_SHORT_ERR, "Boost Inductor Short Error", cs35l41_bst_short_err),
1264 CS35L41_IRQ(TEMP_WARN, "Temperature Warning", cs35l41_temp_warn),
1265 CS35L41_IRQ(TEMP_ERR, "Temperature Error", cs35l41_temp_err),
1266 CS35L41_IRQ(AMP_SHORT_ERR, "Amp Short", cs35l41_amp_short),
1269 static const struct regmap_irq cs35l41_reg_irqs[] = {
1270 CS35L41_REG_IRQ(IRQ1_STATUS1, BST_OVP_ERR),
1271 CS35L41_REG_IRQ(IRQ1_STATUS1, BST_DCM_UVP_ERR),
1272 CS35L41_REG_IRQ(IRQ1_STATUS1, BST_SHORT_ERR),
1273 CS35L41_REG_IRQ(IRQ1_STATUS1, TEMP_WARN),
1274 CS35L41_REG_IRQ(IRQ1_STATUS1, TEMP_ERR),
1275 CS35L41_REG_IRQ(IRQ1_STATUS1, AMP_SHORT_ERR),
1278 static struct regmap_irq_chip cs35l41_regmap_irq_chip = {
1279 .name = "cs35l41 IRQ1 Controller",
1280 .status_base = CS35L41_IRQ1_STATUS1,
1281 .mask_base = CS35L41_IRQ1_MASK1,
1282 .ack_base = CS35L41_IRQ1_STATUS1,
1284 .irqs = cs35l41_reg_irqs,
1285 .num_irqs = ARRAY_SIZE(cs35l41_reg_irqs),
1289 static int cs35l41_hda_apply_properties(struct cs35l41_hda *cs35l41)
1291 struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
1292 bool using_irq = false;
1297 if (!cs35l41->hw_cfg.valid)
1300 ret = cs35l41_init_boost(cs35l41->dev, cs35l41->regmap, hw_cfg);
1304 if (hw_cfg->gpio1.valid) {
1305 switch (hw_cfg->gpio1.func) {
1306 case CS35L41_NOT_USED:
1308 case CS35l41_VSPK_SWITCH:
1309 hw_cfg->gpio1.func = CS35L41_GPIO1_GPIO;
1310 hw_cfg->gpio1.out_en = true;
1313 hw_cfg->gpio1.func = CS35L41_GPIO1_MDSYNC;
1316 dev_err(cs35l41->dev, "Invalid function %d for GPIO1\n",
1317 hw_cfg->gpio1.func);
1322 if (hw_cfg->gpio2.valid) {
1323 switch (hw_cfg->gpio2.func) {
1324 case CS35L41_NOT_USED:
1326 case CS35L41_INTERRUPT:
1328 hw_cfg->gpio2.func = CS35L41_GPIO2_INT_OPEN_DRAIN;
1331 dev_err(cs35l41->dev, "Invalid GPIO2 function %d\n", hw_cfg->gpio2.func);
1336 irq_pol = cs35l41_gpio_config(cs35l41->regmap, hw_cfg);
1338 if (cs35l41->irq && using_irq) {
1339 ret = devm_regmap_add_irq_chip(cs35l41->dev, cs35l41->regmap, cs35l41->irq,
1340 IRQF_ONESHOT | IRQF_SHARED | irq_pol,
1341 0, &cs35l41_regmap_irq_chip, &cs35l41->irq_data);
1345 for (i = 0; i < ARRAY_SIZE(cs35l41_irqs); i++) {
1346 irq = regmap_irq_get_virq(cs35l41->irq_data, cs35l41_irqs[i].irq);
1350 ret = devm_request_threaded_irq(cs35l41->dev, irq, NULL,
1351 cs35l41_irqs[i].handler,
1352 IRQF_ONESHOT | IRQF_SHARED | irq_pol,
1353 cs35l41_irqs[i].name, cs35l41);
1359 return cs35l41_hda_channel_map(cs35l41->dev, 0, NULL, 1, &hw_cfg->spk_pos);
1362 int cs35l41_get_speaker_id(struct device *dev, int amp_index, int num_amps, int fixed_gpio_id)
1364 struct gpio_desc *speaker_id_desc;
1365 int speaker_id = -ENODEV;
1367 if (fixed_gpio_id >= 0) {
1368 dev_dbg(dev, "Found Fixed Speaker ID GPIO (index = %d)\n", fixed_gpio_id);
1369 speaker_id_desc = gpiod_get_index(dev, NULL, fixed_gpio_id, GPIOD_IN);
1370 if (IS_ERR(speaker_id_desc)) {
1371 speaker_id = PTR_ERR(speaker_id_desc);
1374 speaker_id = gpiod_get_value_cansleep(speaker_id_desc);
1375 gpiod_put(speaker_id_desc);
1376 dev_dbg(dev, "Speaker ID = %d\n", speaker_id);
1384 count = gpiod_count(dev, "spk-id");
1387 gpios_per_amp = count / num_amps;
1388 base_index = gpios_per_amp * amp_index;
1390 if (count % num_amps)
1393 dev_dbg(dev, "Found %d Speaker ID GPIOs per Amp\n", gpios_per_amp);
1395 for (i = 0; i < gpios_per_amp; i++) {
1396 speaker_id_desc = gpiod_get_index(dev, "spk-id", i + base_index,
1398 if (IS_ERR(speaker_id_desc)) {
1399 speaker_id = PTR_ERR(speaker_id_desc);
1402 tmp = gpiod_get_value_cansleep(speaker_id_desc);
1403 gpiod_put(speaker_id_desc);
1408 speaker_id |= tmp << i;
1410 dev_dbg(dev, "Speaker ID = %d\n", speaker_id);
1416 static int cs35l41_hda_read_acpi(struct cs35l41_hda *cs35l41, const char *hid, int id)
1418 struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
1419 u32 values[HDA_MAX_COMPONENTS];
1420 struct acpi_device *adev;
1421 struct device *physdev;
1427 adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1429 dev_err(cs35l41->dev, "Failed to find an ACPI device for %s\n", hid);
1433 physdev = get_device(acpi_get_first_physical_node(adev));
1436 sub = acpi_get_subsystem_id(ACPI_HANDLE(physdev));
1439 cs35l41->acpi_subsystem_id = sub;
1441 ret = cs35l41_add_dsd_properties(cs35l41, physdev, id, hid);
1443 dev_info(cs35l41->dev, "Using extra _DSD properties, bypassing _DSD in ACPI\n");
1447 property = "cirrus,dev-index";
1448 ret = device_property_count_u32(physdev, property);
1452 if (ret > ARRAY_SIZE(values)) {
1458 ret = device_property_read_u32_array(physdev, property, values, nval);
1462 cs35l41->index = -1;
1463 for (i = 0; i < nval; i++) {
1464 if (values[i] == id) {
1469 if (cs35l41->index == -1) {
1470 dev_err(cs35l41->dev, "No index found in %s\n", property);
1475 /* To use the same release code for all laptop variants we can't use devm_ version of
1476 * gpiod_get here, as CLSA010* don't have a fully functional bios with an _DSD node
1478 cs35l41->reset_gpio = fwnode_gpiod_get_index(acpi_fwnode_handle(adev), "reset", cs35l41->index,
1479 GPIOD_OUT_LOW, "cs35l41-reset");
1481 property = "cirrus,speaker-position";
1482 ret = device_property_read_u32_array(physdev, property, values, nval);
1485 hw_cfg->spk_pos = values[cs35l41->index];
1487 cs35l41->channel_index = 0;
1488 for (i = 0; i < cs35l41->index; i++)
1489 if (values[i] == hw_cfg->spk_pos)
1490 cs35l41->channel_index++;
1492 property = "cirrus,gpio1-func";
1493 ret = device_property_read_u32_array(physdev, property, values, nval);
1496 hw_cfg->gpio1.func = values[cs35l41->index];
1497 hw_cfg->gpio1.valid = true;
1499 property = "cirrus,gpio2-func";
1500 ret = device_property_read_u32_array(physdev, property, values, nval);
1503 hw_cfg->gpio2.func = values[cs35l41->index];
1504 hw_cfg->gpio2.valid = true;
1506 property = "cirrus,boost-peak-milliamp";
1507 ret = device_property_read_u32_array(physdev, property, values, nval);
1509 hw_cfg->bst_ipk = values[cs35l41->index];
1511 hw_cfg->bst_ipk = -1;
1513 property = "cirrus,boost-ind-nanohenry";
1514 ret = device_property_read_u32_array(physdev, property, values, nval);
1516 hw_cfg->bst_ind = values[cs35l41->index];
1518 hw_cfg->bst_ind = -1;
1520 property = "cirrus,boost-cap-microfarad";
1521 ret = device_property_read_u32_array(physdev, property, values, nval);
1523 hw_cfg->bst_cap = values[cs35l41->index];
1525 hw_cfg->bst_cap = -1;
1527 cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, cs35l41->index, nval, -1);
1529 if (hw_cfg->bst_ind > 0 || hw_cfg->bst_cap > 0 || hw_cfg->bst_ipk > 0)
1530 hw_cfg->bst_type = CS35L41_INT_BOOST;
1532 hw_cfg->bst_type = CS35L41_EXT_BOOST;
1534 hw_cfg->valid = true;
1535 put_device(physdev);
1540 dev_err(cs35l41->dev, "Failed property %s: %d\n", property, ret);
1541 hw_cfg->valid = false;
1542 hw_cfg->gpio1.valid = false;
1543 hw_cfg->gpio2.valid = false;
1545 put_device(physdev);
1550 int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int irq,
1551 struct regmap *regmap)
1553 unsigned int int_sts, regid, reg_revid, mtl_revid, chipid, int_status;
1554 struct cs35l41_hda *cs35l41;
1557 BUILD_BUG_ON(ARRAY_SIZE(cs35l41_irqs) != ARRAY_SIZE(cs35l41_reg_irqs));
1558 BUILD_BUG_ON(ARRAY_SIZE(cs35l41_irqs) != CS35L41_NUM_IRQ);
1561 return PTR_ERR(regmap);
1563 cs35l41 = devm_kzalloc(dev, sizeof(*cs35l41), GFP_KERNEL);
1569 cs35l41->regmap = regmap;
1570 dev_set_drvdata(dev, cs35l41);
1572 ret = cs35l41_hda_read_acpi(cs35l41, device_name, id);
1574 return dev_err_probe(cs35l41->dev, ret, "Platform not supported\n");
1576 if (IS_ERR(cs35l41->reset_gpio)) {
1577 ret = PTR_ERR(cs35l41->reset_gpio);
1578 cs35l41->reset_gpio = NULL;
1579 if (ret == -EBUSY) {
1580 dev_info(cs35l41->dev, "Reset line busy, assuming shared reset\n");
1582 dev_err_probe(cs35l41->dev, ret, "Failed to get reset GPIO\n");
1586 if (cs35l41->reset_gpio) {
1587 usleep_range(2000, 2100);
1588 gpiod_set_value_cansleep(cs35l41->reset_gpio, 1);
1591 usleep_range(2000, 2100);
1593 ret = regmap_read_poll_timeout(cs35l41->regmap, CS35L41_IRQ1_STATUS4, int_status,
1594 int_status & CS35L41_OTP_BOOT_DONE, 1000, 100000);
1596 dev_err(cs35l41->dev, "Failed waiting for OTP_BOOT_DONE: %d\n", ret);
1600 ret = regmap_read(cs35l41->regmap, CS35L41_IRQ1_STATUS3, &int_sts);
1601 if (ret || (int_sts & CS35L41_OTP_BOOT_ERR)) {
1602 dev_err(cs35l41->dev, "OTP Boot status %x error: %d\n",
1603 int_sts & CS35L41_OTP_BOOT_ERR, ret);
1608 ret = regmap_read(cs35l41->regmap, CS35L41_DEVID, ®id);
1610 dev_err(cs35l41->dev, "Get Device ID failed: %d\n", ret);
1614 ret = regmap_read(cs35l41->regmap, CS35L41_REVID, ®_revid);
1616 dev_err(cs35l41->dev, "Get Revision ID failed: %d\n", ret);
1620 mtl_revid = reg_revid & CS35L41_MTLREVID_MASK;
1622 chipid = (mtl_revid % 2) ? CS35L41R_CHIP_ID : CS35L41_CHIP_ID;
1623 if (regid != chipid) {
1624 dev_err(cs35l41->dev, "CS35L41 Device ID (%X). Expected ID %X\n", regid, chipid);
1629 ret = cs35l41_test_key_unlock(cs35l41->dev, cs35l41->regmap);
1633 ret = cs35l41_register_errata_patch(cs35l41->dev, cs35l41->regmap, reg_revid);
1637 ret = cs35l41_otp_unpack(cs35l41->dev, cs35l41->regmap);
1639 dev_err(cs35l41->dev, "OTP Unpack failed: %d\n", ret);
1643 ret = cs35l41_test_key_lock(cs35l41->dev, cs35l41->regmap);
1647 ret = regmap_multi_reg_write(cs35l41->regmap, cs35l41_hda_mute,
1648 ARRAY_SIZE(cs35l41_hda_mute));
1652 INIT_WORK(&cs35l41->fw_load_work, cs35l41_fw_load_work);
1653 mutex_init(&cs35l41->fw_mutex);
1655 pm_runtime_set_autosuspend_delay(cs35l41->dev, 3000);
1656 pm_runtime_use_autosuspend(cs35l41->dev);
1657 pm_runtime_mark_last_busy(cs35l41->dev);
1658 pm_runtime_set_active(cs35l41->dev);
1659 pm_runtime_get_noresume(cs35l41->dev);
1660 pm_runtime_enable(cs35l41->dev);
1662 ret = cs35l41_hda_apply_properties(cs35l41);
1666 pm_runtime_put_autosuspend(cs35l41->dev);
1668 ret = component_add(cs35l41->dev, &cs35l41_hda_comp_ops);
1670 dev_err(cs35l41->dev, "Register component failed: %d\n", ret);
1671 pm_runtime_disable(cs35l41->dev);
1675 dev_info(cs35l41->dev, "Cirrus Logic CS35L41 (%x), Revision: %02X\n", regid, reg_revid);
1680 pm_runtime_disable(cs35l41->dev);
1681 pm_runtime_put_noidle(cs35l41->dev);
1684 if (cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type))
1685 gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
1686 gpiod_put(cs35l41->reset_gpio);
1687 kfree(cs35l41->acpi_subsystem_id);
1691 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_probe, SND_HDA_SCODEC_CS35L41);
1693 void cs35l41_hda_remove(struct device *dev)
1695 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
1697 pm_runtime_get_sync(cs35l41->dev);
1698 pm_runtime_disable(cs35l41->dev);
1700 if (cs35l41->halo_initialized)
1701 cs35l41_remove_dsp(cs35l41);
1703 component_del(cs35l41->dev, &cs35l41_hda_comp_ops);
1705 pm_runtime_put_noidle(cs35l41->dev);
1707 if (cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type))
1708 gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
1709 gpiod_put(cs35l41->reset_gpio);
1710 kfree(cs35l41->acpi_subsystem_id);
1712 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_remove, SND_HDA_SCODEC_CS35L41);
1714 const struct dev_pm_ops cs35l41_hda_pm_ops = {
1715 RUNTIME_PM_OPS(cs35l41_runtime_suspend, cs35l41_runtime_resume,
1716 cs35l41_runtime_idle)
1717 .prepare = cs35l41_system_suspend_prep,
1718 SYSTEM_SLEEP_PM_OPS(cs35l41_system_suspend, cs35l41_system_resume)
1720 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_pm_ops, SND_HDA_SCODEC_CS35L41);
1722 MODULE_DESCRIPTION("CS35L41 HDA Driver");
1723 MODULE_IMPORT_NS(SND_HDA_CS_DSP_CONTROLS);
1724 MODULE_AUTHOR("Lucas Tanure, Cirrus Logic Inc, <tanureal@opensource.cirrus.com>");
1725 MODULE_LICENSE("GPL");
1726 MODULE_IMPORT_NS(FW_CS_DSP);