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 return 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");
194 /* try cirrus/part-dspN-fwtype-sub<-ampname>.wmfw */
195 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
196 CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
197 cs35l41->amp_name, -1, "wmfw");
199 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */
200 return cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
201 CS35L41_FIRMWARE_ROOT,
202 cs35l41->acpi_subsystem_id, cs35l41->amp_name,
203 cs35l41->speaker_id, "bin");
206 /* try cirrus/part-dspN-fwtype-sub<-spkidN>.wmfw */
207 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
208 CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
209 NULL, cs35l41->speaker_id, "wmfw");
211 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */
212 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
213 CS35L41_FIRMWARE_ROOT,
214 cs35l41->acpi_subsystem_id,
215 cs35l41->amp_name, cs35l41->speaker_id, "bin");
217 /* try cirrus/part-dspN-fwtype-sub<-spkidN>.bin */
218 return cs35l41_request_firmware_file(cs35l41, coeff_firmware,
219 coeff_filename, CS35L41_FIRMWARE_ROOT,
220 cs35l41->acpi_subsystem_id, NULL,
221 cs35l41->speaker_id, "bin");
224 /* try cirrus/part-dspN-fwtype-sub.wmfw */
225 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
226 CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
229 /* try cirrus/part-dspN-fwtype-sub<-spkidN><-ampname>.bin */
230 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
231 CS35L41_FIRMWARE_ROOT,
232 cs35l41->acpi_subsystem_id, cs35l41->amp_name,
233 cs35l41->speaker_id, "bin");
235 /* try cirrus/part-dspN-fwtype-sub<-spkidN>.bin */
236 return cs35l41_request_firmware_file(cs35l41, coeff_firmware,
237 coeff_filename, CS35L41_FIRMWARE_ROOT,
238 cs35l41->acpi_subsystem_id, NULL,
239 cs35l41->speaker_id, "bin");
245 static int cs35l41_request_firmware_files(struct cs35l41_hda *cs35l41,
246 const struct firmware **wmfw_firmware,
247 char **wmfw_filename,
248 const struct firmware **coeff_firmware,
249 char **coeff_filename)
253 if (cs35l41->speaker_id > -1) {
254 ret = cs35l41_request_firmware_files_spkid(cs35l41, wmfw_firmware, wmfw_filename,
255 coeff_firmware, coeff_filename);
260 /* try cirrus/part-dspN-fwtype-sub<-ampname>.wmfw */
261 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
262 CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
263 cs35l41->amp_name, -1, "wmfw");
265 /* try cirrus/part-dspN-fwtype-sub<-ampname>.bin */
266 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
267 CS35L41_FIRMWARE_ROOT,
268 cs35l41->acpi_subsystem_id, cs35l41->amp_name,
273 /* try cirrus/part-dspN-fwtype-sub.wmfw */
274 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
275 CS35L41_FIRMWARE_ROOT, cs35l41->acpi_subsystem_id,
278 /* try cirrus/part-dspN-fwtype-sub<-ampname>.bin */
279 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
280 CS35L41_FIRMWARE_ROOT,
281 cs35l41->acpi_subsystem_id,
282 cs35l41->amp_name, -1, "bin");
284 /* try cirrus/part-dspN-fwtype-sub.bin */
285 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
286 CS35L41_FIRMWARE_ROOT,
287 cs35l41->acpi_subsystem_id, NULL, -1,
295 /* Handle fallback */
296 dev_warn(cs35l41->dev, "Falling back to default firmware.\n");
298 release_firmware(*wmfw_firmware);
299 kfree(*wmfw_filename);
301 /* fallback try cirrus/part-dspN-fwtype.wmfw */
302 ret = cs35l41_request_firmware_file(cs35l41, wmfw_firmware, wmfw_filename,
303 CS35L41_FIRMWARE_ROOT, NULL, NULL, -1, "wmfw");
305 /* fallback try cirrus/part-dspN-fwtype.bin */
306 ret = cs35l41_request_firmware_file(cs35l41, coeff_firmware, coeff_filename,
307 CS35L41_FIRMWARE_ROOT, NULL, NULL, -1, "bin");
310 release_firmware(*wmfw_firmware);
311 kfree(*wmfw_filename);
312 dev_warn(cs35l41->dev, "Unable to find firmware and tuning\n");
317 #if IS_ENABLED(CONFIG_EFI)
318 static int cs35l41_apply_calibration(struct cs35l41_hda *cs35l41, __be32 ambient, __be32 r0,
319 __be32 status, __be32 checksum)
323 ret = hda_cs_dsp_write_ctl(&cs35l41->cs_dsp, CAL_AMBIENT_DSP_CTL_NAME, CAL_DSP_CTL_TYPE,
324 CAL_DSP_CTL_ALG, &ambient, 4);
326 dev_err(cs35l41->dev, "Cannot Write Control: %s - %d\n", CAL_AMBIENT_DSP_CTL_NAME,
330 ret = hda_cs_dsp_write_ctl(&cs35l41->cs_dsp, CAL_R_DSP_CTL_NAME, CAL_DSP_CTL_TYPE,
331 CAL_DSP_CTL_ALG, &r0, 4);
333 dev_err(cs35l41->dev, "Cannot Write Control: %s - %d\n", CAL_R_DSP_CTL_NAME, ret);
336 ret = hda_cs_dsp_write_ctl(&cs35l41->cs_dsp, CAL_STATUS_DSP_CTL_NAME, CAL_DSP_CTL_TYPE,
337 CAL_DSP_CTL_ALG, &status, 4);
339 dev_err(cs35l41->dev, "Cannot Write Control: %s - %d\n", CAL_STATUS_DSP_CTL_NAME,
343 ret = hda_cs_dsp_write_ctl(&cs35l41->cs_dsp, CAL_CHECKSUM_DSP_CTL_NAME, CAL_DSP_CTL_TYPE,
344 CAL_DSP_CTL_ALG, &checksum, 4);
346 dev_err(cs35l41->dev, "Cannot Write Control: %s - %d\n", CAL_CHECKSUM_DSP_CTL_NAME,
354 static int cs35l41_save_calibration(struct cs35l41_hda *cs35l41)
356 static efi_guid_t efi_guid = EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d, 0x93, 0xfe,
357 0x5a, 0xa3, 0x5d, 0xb3);
358 static efi_char16_t efi_name[] = L"CirrusSmartAmpCalibrationData";
359 const struct cs35l41_amp_efi_data *efi_data;
360 const struct cs35l41_amp_cal_data *cl;
361 unsigned long data_size = 0;
367 /* Get real size of UEFI variable */
368 status = efi.get_variable(efi_name, &efi_guid, &attr, &data_size, data);
369 if (status == EFI_BUFFER_TOO_SMALL) {
371 /* Allocate data buffer of data_size bytes */
372 data = vmalloc(data_size);
375 /* Get variable contents into buffer */
376 status = efi.get_variable(efi_name, &efi_guid, &attr, &data_size, data);
377 if (status == EFI_SUCCESS) {
378 efi_data = (struct cs35l41_amp_efi_data *)data;
379 dev_dbg(cs35l41->dev, "Calibration: Size=%d, Amp Count=%d\n",
380 efi_data->size, efi_data->count);
381 if (efi_data->count > cs35l41->index) {
382 cl = &efi_data->data[cs35l41->index];
383 dev_dbg(cs35l41->dev,
384 "Calibration: Ambient=%02x, Status=%02x, R0=%d\n",
385 cl->calAmbient, cl->calStatus, cl->calR);
387 /* Calibration can only be applied whilst the DSP is not running */
388 ret = cs35l41_apply_calibration(cs35l41,
389 cpu_to_be32(cl->calAmbient),
390 cpu_to_be32(cl->calR),
391 cpu_to_be32(cl->calStatus),
392 cpu_to_be32(cl->calR + 1));
400 static int cs35l41_save_calibration(struct cs35l41_hda *cs35l41)
402 dev_warn(cs35l41->dev, "Calibration not supported without EFI support.\n");
407 static int cs35l41_init_dsp(struct cs35l41_hda *cs35l41)
409 const struct firmware *coeff_firmware = NULL;
410 const struct firmware *wmfw_firmware = NULL;
411 struct cs_dsp *dsp = &cs35l41->cs_dsp;
412 char *coeff_filename = NULL;
413 char *wmfw_filename = NULL;
416 if (!cs35l41->halo_initialized) {
417 cs35l41_configure_cs_dsp(cs35l41->dev, cs35l41->regmap, dsp);
418 dsp->client_ops = &client_ops;
420 ret = cs_dsp_halo_init(&cs35l41->cs_dsp);
423 cs35l41->halo_initialized = true;
426 ret = cs35l41_request_firmware_files(cs35l41, &wmfw_firmware, &wmfw_filename,
427 &coeff_firmware, &coeff_filename);
431 dev_dbg(cs35l41->dev, "Loading WMFW Firmware: %s\n", wmfw_filename);
433 dev_dbg(cs35l41->dev, "Loading Coefficient File: %s\n", coeff_filename);
435 dev_warn(cs35l41->dev, "No Coefficient File available.\n");
437 ret = cs_dsp_power_up(dsp, wmfw_firmware, wmfw_filename, coeff_firmware, coeff_filename,
438 hda_cs_dsp_fw_ids[cs35l41->firmware_type]);
442 cs35l41_add_controls(cs35l41);
444 ret = cs35l41_save_calibration(cs35l41);
447 release_firmware(wmfw_firmware);
448 release_firmware(coeff_firmware);
449 kfree(wmfw_filename);
450 kfree(coeff_filename);
455 static void cs35l41_shutdown_dsp(struct cs35l41_hda *cs35l41)
457 struct cs_dsp *dsp = &cs35l41->cs_dsp;
460 cs_dsp_power_down(dsp);
461 cs35l41->firmware_running = false;
462 dev_dbg(cs35l41->dev, "Unloaded Firmware\n");
465 static void cs35l41_remove_dsp(struct cs35l41_hda *cs35l41)
467 struct cs_dsp *dsp = &cs35l41->cs_dsp;
469 cancel_work_sync(&cs35l41->fw_load_work);
471 mutex_lock(&cs35l41->fw_mutex);
472 cs35l41_shutdown_dsp(cs35l41);
474 cs35l41->halo_initialized = false;
475 mutex_unlock(&cs35l41->fw_mutex);
478 /* Protection release cycle to get the speaker out of Safe-Mode */
479 static void cs35l41_error_release(struct device *dev, struct regmap *regmap, unsigned int mask)
481 regmap_write(regmap, CS35L41_PROTECT_REL_ERR_IGN, 0);
482 regmap_set_bits(regmap, CS35L41_PROTECT_REL_ERR_IGN, mask);
483 regmap_clear_bits(regmap, CS35L41_PROTECT_REL_ERR_IGN, mask);
486 /* Clear all errors to release safe mode. Global Enable must be cleared first. */
487 static void cs35l41_irq_release(struct cs35l41_hda *cs35l41)
489 cs35l41_error_release(cs35l41->dev, cs35l41->regmap, cs35l41->irq_errors);
490 cs35l41->irq_errors = 0;
493 static void cs35l41_hda_play_start(struct device *dev)
495 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
496 struct regmap *reg = cs35l41->regmap;
498 dev_dbg(dev, "Play (Start)\n");
500 if (cs35l41->playback_started) {
501 dev_dbg(dev, "Playback already started.");
505 cs35l41->playback_started = true;
507 if (cs35l41->firmware_running) {
508 regmap_multi_reg_write(reg, cs35l41_hda_config_dsp,
509 ARRAY_SIZE(cs35l41_hda_config_dsp));
510 regmap_update_bits(reg, CS35L41_PWR_CTRL2,
511 CS35L41_VMON_EN_MASK | CS35L41_IMON_EN_MASK,
512 1 << CS35L41_VMON_EN_SHIFT | 1 << CS35L41_IMON_EN_SHIFT);
513 cs35l41_set_cspl_mbox_cmd(cs35l41->dev, reg, CSPL_MBOX_CMD_RESUME);
515 regmap_multi_reg_write(reg, cs35l41_hda_config, ARRAY_SIZE(cs35l41_hda_config));
517 regmap_update_bits(reg, CS35L41_PWR_CTRL2, CS35L41_AMP_EN_MASK, 1 << CS35L41_AMP_EN_SHIFT);
518 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST)
519 regmap_write(reg, CS35L41_GPIO1_CTRL1, 0x00008001);
523 static void cs35l41_hda_play_done(struct device *dev)
525 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
526 struct regmap *reg = cs35l41->regmap;
528 dev_dbg(dev, "Play (Complete)\n");
530 cs35l41_global_enable(dev, reg, cs35l41->hw_cfg.bst_type, 1, NULL,
531 cs35l41->firmware_running);
532 if (cs35l41->firmware_running) {
533 regmap_multi_reg_write(reg, cs35l41_hda_unmute_dsp,
534 ARRAY_SIZE(cs35l41_hda_unmute_dsp));
536 regmap_multi_reg_write(reg, cs35l41_hda_unmute,
537 ARRAY_SIZE(cs35l41_hda_unmute));
541 static void cs35l41_hda_pause_start(struct device *dev)
543 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
544 struct regmap *reg = cs35l41->regmap;
546 dev_dbg(dev, "Pause (Start)\n");
548 regmap_multi_reg_write(reg, cs35l41_hda_mute, ARRAY_SIZE(cs35l41_hda_mute));
549 cs35l41_global_enable(dev, reg, cs35l41->hw_cfg.bst_type, 0, NULL,
550 cs35l41->firmware_running);
553 static void cs35l41_hda_pause_done(struct device *dev)
555 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
556 struct regmap *reg = cs35l41->regmap;
558 dev_dbg(dev, "Pause (Complete)\n");
560 regmap_update_bits(reg, CS35L41_PWR_CTRL2, CS35L41_AMP_EN_MASK, 0 << CS35L41_AMP_EN_SHIFT);
561 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST)
562 regmap_write(reg, CS35L41_GPIO1_CTRL1, 0x00000001);
563 if (cs35l41->firmware_running) {
564 cs35l41_set_cspl_mbox_cmd(dev, reg, CSPL_MBOX_CMD_PAUSE);
565 regmap_update_bits(reg, CS35L41_PWR_CTRL2,
566 CS35L41_VMON_EN_MASK | CS35L41_IMON_EN_MASK,
567 0 << CS35L41_VMON_EN_SHIFT | 0 << CS35L41_IMON_EN_SHIFT);
569 cs35l41_irq_release(cs35l41);
570 cs35l41->playback_started = false;
573 static void cs35l41_hda_pre_playback_hook(struct device *dev, int action)
575 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
578 case HDA_GEN_PCM_ACT_CLEANUP:
579 mutex_lock(&cs35l41->fw_mutex);
580 cs35l41_hda_pause_start(dev);
581 mutex_unlock(&cs35l41->fw_mutex);
587 static void cs35l41_hda_playback_hook(struct device *dev, int action)
589 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
592 case HDA_GEN_PCM_ACT_OPEN:
594 * All amps must be resumed before we can start playing back.
595 * This ensures, for external boost, that all amps are in AMP_SAFE mode.
596 * Do this in HDA_GEN_PCM_ACT_OPEN, since this is run prior to any of the
599 pm_runtime_get_sync(dev);
601 case HDA_GEN_PCM_ACT_PREPARE:
602 mutex_lock(&cs35l41->fw_mutex);
603 cs35l41_hda_play_start(dev);
604 mutex_unlock(&cs35l41->fw_mutex);
606 case HDA_GEN_PCM_ACT_CLEANUP:
607 mutex_lock(&cs35l41->fw_mutex);
608 cs35l41_hda_pause_done(dev);
609 mutex_unlock(&cs35l41->fw_mutex);
611 case HDA_GEN_PCM_ACT_CLOSE:
612 mutex_lock(&cs35l41->fw_mutex);
613 if (!cs35l41->firmware_running && cs35l41->request_fw_load &&
614 !cs35l41->fw_request_ongoing) {
615 dev_info(dev, "Requesting Firmware Load after HDA_GEN_PCM_ACT_CLOSE\n");
616 cs35l41->fw_request_ongoing = true;
617 schedule_work(&cs35l41->fw_load_work);
619 mutex_unlock(&cs35l41->fw_mutex);
622 * Playback must be finished for all amps before we start runtime suspend.
623 * This ensures no amps are playing back when we start putting them to sleep.
625 pm_runtime_mark_last_busy(dev);
626 pm_runtime_put_autosuspend(dev);
633 static void cs35l41_hda_post_playback_hook(struct device *dev, int action)
635 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
638 case HDA_GEN_PCM_ACT_PREPARE:
639 mutex_lock(&cs35l41->fw_mutex);
640 cs35l41_hda_play_done(dev);
641 mutex_unlock(&cs35l41->fw_mutex);
648 static int cs35l41_hda_channel_map(struct device *dev, unsigned int tx_num, unsigned int *tx_slot,
649 unsigned int rx_num, unsigned int *rx_slot)
651 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
652 static const char * const channel_name[] = { "L", "R" };
654 if (!cs35l41->amp_name) {
655 if (*rx_slot >= ARRAY_SIZE(channel_name))
658 cs35l41->amp_name = devm_kasprintf(cs35l41->dev, GFP_KERNEL, "%s%d",
659 channel_name[*rx_slot], cs35l41->channel_index);
660 if (!cs35l41->amp_name)
664 return cs35l41_set_channels(cs35l41->dev, cs35l41->regmap, tx_num, tx_slot, rx_num,
668 static int cs35l41_ready_for_reset(struct cs35l41_hda *cs35l41)
672 mutex_lock(&cs35l41->fw_mutex);
673 if (cs35l41->firmware_running) {
675 regcache_cache_only(cs35l41->regmap, false);
677 ret = cs35l41_exit_hibernate(cs35l41->dev, cs35l41->regmap);
679 dev_warn(cs35l41->dev, "Unable to exit Hibernate.");
683 /* Test key needs to be unlocked to allow the OTP settings to re-apply */
684 cs35l41_test_key_unlock(cs35l41->dev, cs35l41->regmap);
685 ret = regcache_sync(cs35l41->regmap);
686 cs35l41_test_key_lock(cs35l41->dev, cs35l41->regmap);
688 dev_err(cs35l41->dev, "Failed to restore register cache: %d\n", ret);
692 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST)
693 cs35l41_init_boost(cs35l41->dev, cs35l41->regmap, &cs35l41->hw_cfg);
695 cs35l41_shutdown_dsp(cs35l41);
696 cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type);
699 regcache_cache_only(cs35l41->regmap, true);
700 regcache_mark_dirty(cs35l41->regmap);
702 mutex_unlock(&cs35l41->fw_mutex);
707 static int cs35l41_system_suspend_prep(struct device *dev)
709 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
711 dev_dbg(cs35l41->dev, "System Suspend Prepare\n");
713 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
714 dev_err_once(cs35l41->dev, "System Suspend not supported\n");
715 return 0; /* don't block the whole system suspend */
718 mutex_lock(&cs35l41->fw_mutex);
719 if (cs35l41->playback_started)
720 cs35l41_hda_pause_start(dev);
721 mutex_unlock(&cs35l41->fw_mutex);
726 static int cs35l41_system_suspend(struct device *dev)
728 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
731 dev_dbg(cs35l41->dev, "System Suspend\n");
733 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
734 dev_err_once(cs35l41->dev, "System Suspend not supported\n");
735 return 0; /* don't block the whole system suspend */
738 mutex_lock(&cs35l41->fw_mutex);
739 if (cs35l41->playback_started)
740 cs35l41_hda_pause_done(dev);
741 mutex_unlock(&cs35l41->fw_mutex);
743 ret = pm_runtime_force_suspend(dev);
745 dev_err(dev, "System Suspend Failed, unable to runtime suspend: %d\n", ret);
749 /* Shutdown DSP before system suspend */
750 ret = cs35l41_ready_for_reset(cs35l41);
753 dev_err(dev, "System Suspend Failed, not ready for Reset: %d\n", ret);
756 * Reset GPIO may be shared, so cannot reset here.
757 * However beyond this point, amps may be powered down.
762 static int cs35l41_system_resume(struct device *dev)
764 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
767 dev_dbg(cs35l41->dev, "System Resume\n");
769 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
770 dev_err_once(cs35l41->dev, "System Resume not supported\n");
771 return 0; /* don't block the whole system resume */
774 if (cs35l41->reset_gpio) {
775 usleep_range(2000, 2100);
776 gpiod_set_value_cansleep(cs35l41->reset_gpio, 1);
779 usleep_range(2000, 2100);
781 ret = pm_runtime_force_resume(dev);
783 dev_err(dev, "System Resume Failed: Unable to runtime resume: %d\n", ret);
787 mutex_lock(&cs35l41->fw_mutex);
789 if (cs35l41->request_fw_load && !cs35l41->fw_request_ongoing) {
790 cs35l41->fw_request_ongoing = true;
791 schedule_work(&cs35l41->fw_load_work);
793 mutex_unlock(&cs35l41->fw_mutex);
798 static int cs35l41_runtime_idle(struct device *dev)
800 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
802 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH)
803 return -EBUSY; /* suspend not supported yet on this model */
807 static int cs35l41_runtime_suspend(struct device *dev)
809 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
812 dev_dbg(cs35l41->dev, "Runtime Suspend\n");
814 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
815 dev_dbg(cs35l41->dev, "Runtime Suspend not supported\n");
819 mutex_lock(&cs35l41->fw_mutex);
821 if (cs35l41->firmware_running) {
822 ret = cs35l41_enter_hibernate(cs35l41->dev, cs35l41->regmap,
823 cs35l41->hw_cfg.bst_type);
827 cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type);
830 regcache_cache_only(cs35l41->regmap, true);
831 regcache_mark_dirty(cs35l41->regmap);
834 mutex_unlock(&cs35l41->fw_mutex);
839 static int cs35l41_runtime_resume(struct device *dev)
841 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
844 dev_dbg(cs35l41->dev, "Runtime Resume\n");
846 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST_NO_VSPK_SWITCH) {
847 dev_dbg(cs35l41->dev, "Runtime Resume not supported\n");
851 mutex_lock(&cs35l41->fw_mutex);
853 regcache_cache_only(cs35l41->regmap, false);
855 if (cs35l41->firmware_running) {
856 ret = cs35l41_exit_hibernate(cs35l41->dev, cs35l41->regmap);
858 dev_warn(cs35l41->dev, "Unable to exit Hibernate.");
863 /* Test key needs to be unlocked to allow the OTP settings to re-apply */
864 cs35l41_test_key_unlock(cs35l41->dev, cs35l41->regmap);
865 ret = regcache_sync(cs35l41->regmap);
866 cs35l41_test_key_lock(cs35l41->dev, cs35l41->regmap);
868 dev_err(cs35l41->dev, "Failed to restore register cache: %d\n", ret);
872 if (cs35l41->hw_cfg.bst_type == CS35L41_EXT_BOOST)
873 cs35l41_init_boost(cs35l41->dev, cs35l41->regmap, &cs35l41->hw_cfg);
876 mutex_unlock(&cs35l41->fw_mutex);
881 static int cs35l41_smart_amp(struct cs35l41_hda *cs35l41)
886 ret = cs35l41_init_dsp(cs35l41);
888 dev_warn(cs35l41->dev, "Cannot Initialize Firmware. Error: %d\n", ret);
892 ret = cs35l41_write_fs_errata(cs35l41->dev, cs35l41->regmap);
894 dev_err(cs35l41->dev, "Cannot Write FS Errata: %d\n", ret);
898 ret = cs_dsp_run(&cs35l41->cs_dsp);
900 dev_err(cs35l41->dev, "Fail to start dsp: %d\n", ret);
904 ret = read_poll_timeout(hda_cs_dsp_read_ctl, ret,
905 be32_to_cpu(halo_sts) == HALO_STATE_CODE_RUN,
906 1000, 15000, false, &cs35l41->cs_dsp, HALO_STATE_DSP_CTL_NAME,
907 HALO_STATE_DSP_CTL_TYPE, HALO_STATE_DSP_CTL_ALG,
908 &halo_sts, sizeof(halo_sts));
911 dev_err(cs35l41->dev, "Timeout waiting for HALO Core to start. State: %u\n",
916 ret = cs35l41_set_cspl_mbox_cmd(cs35l41->dev, cs35l41->regmap, CSPL_MBOX_CMD_PAUSE);
918 dev_err(cs35l41->dev, "Error waiting for DSP to pause: %u\n", ret);
922 cs35l41->firmware_running = true;
927 cs35l41_shutdown_dsp(cs35l41);
931 static void cs35l41_load_firmware(struct cs35l41_hda *cs35l41, bool load)
933 if (cs35l41->firmware_running && !load) {
934 dev_dbg(cs35l41->dev, "Unloading Firmware\n");
935 cs35l41_shutdown_dsp(cs35l41);
936 } else if (!cs35l41->firmware_running && load) {
937 dev_dbg(cs35l41->dev, "Loading Firmware\n");
938 cs35l41_smart_amp(cs35l41);
940 dev_dbg(cs35l41->dev, "Unable to Load firmware.\n");
944 static int cs35l41_fw_load_ctl_get(struct snd_kcontrol *kcontrol,
945 struct snd_ctl_elem_value *ucontrol)
947 struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol);
949 ucontrol->value.integer.value[0] = cs35l41->request_fw_load;
953 static void cs35l41_fw_load_work(struct work_struct *work)
955 struct cs35l41_hda *cs35l41 = container_of(work, struct cs35l41_hda, fw_load_work);
957 pm_runtime_get_sync(cs35l41->dev);
959 mutex_lock(&cs35l41->fw_mutex);
961 /* Recheck if playback is ongoing, mutex will block playback during firmware loading */
962 if (cs35l41->playback_started)
963 dev_err(cs35l41->dev, "Cannot Load/Unload firmware during Playback. Retrying...\n");
965 cs35l41_load_firmware(cs35l41, cs35l41->request_fw_load);
967 cs35l41->fw_request_ongoing = false;
968 mutex_unlock(&cs35l41->fw_mutex);
970 pm_runtime_mark_last_busy(cs35l41->dev);
971 pm_runtime_put_autosuspend(cs35l41->dev);
974 static int cs35l41_fw_load_ctl_put(struct snd_kcontrol *kcontrol,
975 struct snd_ctl_elem_value *ucontrol)
977 struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol);
979 if (cs35l41->request_fw_load == ucontrol->value.integer.value[0])
982 if (cs35l41->fw_request_ongoing) {
983 dev_dbg(cs35l41->dev, "Existing request not complete\n");
987 /* Check if playback is ongoing when initial request is made */
988 if (cs35l41->playback_started) {
989 dev_err(cs35l41->dev, "Cannot Load/Unload firmware during Playback\n");
993 cs35l41->fw_request_ongoing = true;
994 cs35l41->request_fw_load = ucontrol->value.integer.value[0];
995 schedule_work(&cs35l41->fw_load_work);
1000 static int cs35l41_fw_type_ctl_get(struct snd_kcontrol *kcontrol,
1001 struct snd_ctl_elem_value *ucontrol)
1003 struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol);
1005 ucontrol->value.enumerated.item[0] = cs35l41->firmware_type;
1010 static int cs35l41_fw_type_ctl_put(struct snd_kcontrol *kcontrol,
1011 struct snd_ctl_elem_value *ucontrol)
1013 struct cs35l41_hda *cs35l41 = snd_kcontrol_chip(kcontrol);
1015 if (ucontrol->value.enumerated.item[0] < HDA_CS_DSP_NUM_FW) {
1016 if (cs35l41->firmware_type != ucontrol->value.enumerated.item[0]) {
1017 cs35l41->firmware_type = ucontrol->value.enumerated.item[0];
1027 static int cs35l41_fw_type_ctl_info(struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1029 return snd_ctl_enum_info(uinfo, 1, ARRAY_SIZE(hda_cs_dsp_fw_ids), hda_cs_dsp_fw_ids);
1032 static int cs35l41_create_controls(struct cs35l41_hda *cs35l41)
1034 char fw_type_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1035 char fw_load_ctl_name[SNDRV_CTL_ELEM_ID_NAME_MAXLEN];
1036 struct snd_kcontrol_new fw_type_ctl = {
1037 .name = fw_type_ctl_name,
1038 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1039 .info = cs35l41_fw_type_ctl_info,
1040 .get = cs35l41_fw_type_ctl_get,
1041 .put = cs35l41_fw_type_ctl_put,
1043 struct snd_kcontrol_new fw_load_ctl = {
1044 .name = fw_load_ctl_name,
1045 .iface = SNDRV_CTL_ELEM_IFACE_CARD,
1046 .info = snd_ctl_boolean_mono_info,
1047 .get = cs35l41_fw_load_ctl_get,
1048 .put = cs35l41_fw_load_ctl_put,
1052 scnprintf(fw_type_ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s DSP1 Firmware Type",
1054 scnprintf(fw_load_ctl_name, SNDRV_CTL_ELEM_ID_NAME_MAXLEN, "%s DSP1 Firmware Load",
1057 ret = snd_ctl_add(cs35l41->codec->card, snd_ctl_new1(&fw_type_ctl, cs35l41));
1059 dev_err(cs35l41->dev, "Failed to add KControl %s = %d\n", fw_type_ctl.name, ret);
1063 dev_dbg(cs35l41->dev, "Added Control %s\n", fw_type_ctl.name);
1065 ret = snd_ctl_add(cs35l41->codec->card, snd_ctl_new1(&fw_load_ctl, cs35l41));
1067 dev_err(cs35l41->dev, "Failed to add KControl %s = %d\n", fw_load_ctl.name, ret);
1071 dev_dbg(cs35l41->dev, "Added Control %s\n", fw_load_ctl.name);
1076 static int cs35l41_hda_bind(struct device *dev, struct device *master, void *master_data)
1078 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
1079 struct hda_component *comps = master_data;
1080 unsigned int sleep_flags;
1083 if (!comps || cs35l41->index < 0 || cs35l41->index >= HDA_MAX_COMPONENTS)
1086 comps = &comps[cs35l41->index];
1090 pm_runtime_get_sync(dev);
1092 mutex_lock(&cs35l41->fw_mutex);
1095 if (!cs35l41->acpi_subsystem_id)
1096 cs35l41->acpi_subsystem_id = kasprintf(GFP_KERNEL, "%.8x",
1097 comps->codec->core.subsystem_id);
1098 cs35l41->codec = comps->codec;
1099 strscpy(comps->name, dev_name(dev), sizeof(comps->name));
1101 cs35l41->firmware_type = HDA_CS_DSP_FW_SPK_PROT;
1103 if (firmware_autostart) {
1104 dev_dbg(cs35l41->dev, "Firmware Autostart.\n");
1105 cs35l41->request_fw_load = true;
1106 if (cs35l41_smart_amp(cs35l41) < 0)
1107 dev_warn(cs35l41->dev, "Cannot Run Firmware, reverting to dsp bypass...\n");
1109 dev_dbg(cs35l41->dev, "Firmware Autostart is disabled.\n");
1112 ret = cs35l41_create_controls(cs35l41);
1114 comps->playback_hook = cs35l41_hda_playback_hook;
1115 comps->pre_playback_hook = cs35l41_hda_pre_playback_hook;
1116 comps->post_playback_hook = cs35l41_hda_post_playback_hook;
1118 mutex_unlock(&cs35l41->fw_mutex);
1120 sleep_flags = lock_system_sleep();
1121 if (!device_link_add(&comps->codec->core.dev, cs35l41->dev, DL_FLAG_STATELESS))
1122 dev_warn(dev, "Unable to create device link\n");
1123 unlock_system_sleep(sleep_flags);
1125 pm_runtime_mark_last_busy(dev);
1126 pm_runtime_put_autosuspend(dev);
1128 dev_info(cs35l41->dev,
1129 "CS35L41 Bound - SSID: %s, BST: %d, VSPK: %d, CH: %c, FW EN: %d, SPKID: %d\n",
1130 cs35l41->acpi_subsystem_id, cs35l41->hw_cfg.bst_type,
1131 cs35l41->hw_cfg.gpio1.func == CS35l41_VSPK_SWITCH,
1132 cs35l41->hw_cfg.spk_pos ? 'R' : 'L',
1133 cs35l41->firmware_running, cs35l41->speaker_id);
1138 static void cs35l41_hda_unbind(struct device *dev, struct device *master, void *master_data)
1140 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
1141 struct hda_component *comps = master_data;
1142 unsigned int sleep_flags;
1144 if (comps[cs35l41->index].dev == dev) {
1145 memset(&comps[cs35l41->index], 0, sizeof(*comps));
1146 sleep_flags = lock_system_sleep();
1147 device_link_remove(&comps->codec->core.dev, cs35l41->dev);
1148 unlock_system_sleep(sleep_flags);
1152 static const struct component_ops cs35l41_hda_comp_ops = {
1153 .bind = cs35l41_hda_bind,
1154 .unbind = cs35l41_hda_unbind,
1157 static irqreturn_t cs35l41_bst_short_err(int irq, void *data)
1159 struct cs35l41_hda *cs35l41 = data;
1161 dev_crit_ratelimited(cs35l41->dev, "LBST Error\n");
1162 set_bit(CS35L41_BST_SHORT_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1167 static irqreturn_t cs35l41_bst_dcm_uvp_err(int irq, void *data)
1169 struct cs35l41_hda *cs35l41 = data;
1171 dev_crit_ratelimited(cs35l41->dev, "DCM VBST Under Voltage Error\n");
1172 set_bit(CS35L41_BST_UVP_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1177 static irqreturn_t cs35l41_bst_ovp_err(int irq, void *data)
1179 struct cs35l41_hda *cs35l41 = data;
1181 dev_crit_ratelimited(cs35l41->dev, "VBST Over Voltage error\n");
1182 set_bit(CS35L41_BST_OVP_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1187 static irqreturn_t cs35l41_temp_err(int irq, void *data)
1189 struct cs35l41_hda *cs35l41 = data;
1191 dev_crit_ratelimited(cs35l41->dev, "Over temperature error\n");
1192 set_bit(CS35L41_TEMP_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1197 static irqreturn_t cs35l41_temp_warn(int irq, void *data)
1199 struct cs35l41_hda *cs35l41 = data;
1201 dev_crit_ratelimited(cs35l41->dev, "Over temperature warning\n");
1202 set_bit(CS35L41_TEMP_WARN_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1207 static irqreturn_t cs35l41_amp_short(int irq, void *data)
1209 struct cs35l41_hda *cs35l41 = data;
1211 dev_crit_ratelimited(cs35l41->dev, "Amp short error\n");
1212 set_bit(CS35L41_AMP_SHORT_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1217 static const struct cs35l41_irq cs35l41_irqs[] = {
1218 CS35L41_IRQ(BST_OVP_ERR, "Boost Overvoltage Error", cs35l41_bst_ovp_err),
1219 CS35L41_IRQ(BST_DCM_UVP_ERR, "Boost Undervoltage Error", cs35l41_bst_dcm_uvp_err),
1220 CS35L41_IRQ(BST_SHORT_ERR, "Boost Inductor Short Error", cs35l41_bst_short_err),
1221 CS35L41_IRQ(TEMP_WARN, "Temperature Warning", cs35l41_temp_warn),
1222 CS35L41_IRQ(TEMP_ERR, "Temperature Error", cs35l41_temp_err),
1223 CS35L41_IRQ(AMP_SHORT_ERR, "Amp Short", cs35l41_amp_short),
1226 static const struct regmap_irq cs35l41_reg_irqs[] = {
1227 CS35L41_REG_IRQ(IRQ1_STATUS1, BST_OVP_ERR),
1228 CS35L41_REG_IRQ(IRQ1_STATUS1, BST_DCM_UVP_ERR),
1229 CS35L41_REG_IRQ(IRQ1_STATUS1, BST_SHORT_ERR),
1230 CS35L41_REG_IRQ(IRQ1_STATUS1, TEMP_WARN),
1231 CS35L41_REG_IRQ(IRQ1_STATUS1, TEMP_ERR),
1232 CS35L41_REG_IRQ(IRQ1_STATUS1, AMP_SHORT_ERR),
1235 static struct regmap_irq_chip cs35l41_regmap_irq_chip = {
1236 .name = "cs35l41 IRQ1 Controller",
1237 .status_base = CS35L41_IRQ1_STATUS1,
1238 .mask_base = CS35L41_IRQ1_MASK1,
1239 .ack_base = CS35L41_IRQ1_STATUS1,
1241 .irqs = cs35l41_reg_irqs,
1242 .num_irqs = ARRAY_SIZE(cs35l41_reg_irqs),
1246 static int cs35l41_hda_apply_properties(struct cs35l41_hda *cs35l41)
1248 struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
1249 bool using_irq = false;
1254 if (!cs35l41->hw_cfg.valid)
1257 ret = cs35l41_init_boost(cs35l41->dev, cs35l41->regmap, hw_cfg);
1261 if (hw_cfg->gpio1.valid) {
1262 switch (hw_cfg->gpio1.func) {
1263 case CS35L41_NOT_USED:
1265 case CS35l41_VSPK_SWITCH:
1266 hw_cfg->gpio1.func = CS35L41_GPIO1_GPIO;
1267 hw_cfg->gpio1.out_en = true;
1270 hw_cfg->gpio1.func = CS35L41_GPIO1_MDSYNC;
1273 dev_err(cs35l41->dev, "Invalid function %d for GPIO1\n",
1274 hw_cfg->gpio1.func);
1279 if (hw_cfg->gpio2.valid) {
1280 switch (hw_cfg->gpio2.func) {
1281 case CS35L41_NOT_USED:
1283 case CS35L41_INTERRUPT:
1285 hw_cfg->gpio2.func = CS35L41_GPIO2_INT_OPEN_DRAIN;
1288 dev_err(cs35l41->dev, "Invalid GPIO2 function %d\n", hw_cfg->gpio2.func);
1293 irq_pol = cs35l41_gpio_config(cs35l41->regmap, hw_cfg);
1295 if (cs35l41->irq && using_irq) {
1296 ret = devm_regmap_add_irq_chip(cs35l41->dev, cs35l41->regmap, cs35l41->irq,
1297 IRQF_ONESHOT | IRQF_SHARED | irq_pol,
1298 0, &cs35l41_regmap_irq_chip, &cs35l41->irq_data);
1302 for (i = 0; i < ARRAY_SIZE(cs35l41_irqs); i++) {
1303 irq = regmap_irq_get_virq(cs35l41->irq_data, cs35l41_irqs[i].irq);
1307 ret = devm_request_threaded_irq(cs35l41->dev, irq, NULL,
1308 cs35l41_irqs[i].handler,
1309 IRQF_ONESHOT | IRQF_SHARED | irq_pol,
1310 cs35l41_irqs[i].name, cs35l41);
1316 return cs35l41_hda_channel_map(cs35l41->dev, 0, NULL, 1, &hw_cfg->spk_pos);
1319 int cs35l41_get_speaker_id(struct device *dev, int amp_index, int num_amps, int fixed_gpio_id)
1321 struct gpio_desc *speaker_id_desc;
1322 int speaker_id = -ENODEV;
1324 if (fixed_gpio_id >= 0) {
1325 dev_dbg(dev, "Found Fixed Speaker ID GPIO (index = %d)\n", fixed_gpio_id);
1326 speaker_id_desc = gpiod_get_index(dev, NULL, fixed_gpio_id, GPIOD_IN);
1327 if (IS_ERR(speaker_id_desc)) {
1328 speaker_id = PTR_ERR(speaker_id_desc);
1331 speaker_id = gpiod_get_value_cansleep(speaker_id_desc);
1332 gpiod_put(speaker_id_desc);
1333 dev_dbg(dev, "Speaker ID = %d\n", speaker_id);
1341 count = gpiod_count(dev, "spk-id");
1344 gpios_per_amp = count / num_amps;
1345 base_index = gpios_per_amp * amp_index;
1347 if (count % num_amps)
1350 dev_dbg(dev, "Found %d Speaker ID GPIOs per Amp\n", gpios_per_amp);
1352 for (i = 0; i < gpios_per_amp; i++) {
1353 speaker_id_desc = gpiod_get_index(dev, "spk-id", i + base_index,
1355 if (IS_ERR(speaker_id_desc)) {
1356 speaker_id = PTR_ERR(speaker_id_desc);
1359 tmp = gpiod_get_value_cansleep(speaker_id_desc);
1360 gpiod_put(speaker_id_desc);
1365 speaker_id |= tmp << i;
1367 dev_dbg(dev, "Speaker ID = %d\n", speaker_id);
1373 static int cs35l41_hda_read_acpi(struct cs35l41_hda *cs35l41, const char *hid, int id)
1375 struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
1376 u32 values[HDA_MAX_COMPONENTS];
1377 struct acpi_device *adev;
1378 struct device *physdev;
1384 adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1386 dev_err(cs35l41->dev, "Failed to find an ACPI device for %s\n", hid);
1390 physdev = get_device(acpi_get_first_physical_node(adev));
1393 sub = acpi_get_subsystem_id(ACPI_HANDLE(physdev));
1396 cs35l41->acpi_subsystem_id = sub;
1398 ret = cs35l41_add_dsd_properties(cs35l41, physdev, id, hid);
1400 dev_info(cs35l41->dev, "Using extra _DSD properties, bypassing _DSD in ACPI\n");
1404 property = "cirrus,dev-index";
1405 ret = device_property_count_u32(physdev, property);
1409 if (ret > ARRAY_SIZE(values)) {
1415 ret = device_property_read_u32_array(physdev, property, values, nval);
1419 cs35l41->index = -1;
1420 for (i = 0; i < nval; i++) {
1421 if (values[i] == id) {
1426 if (cs35l41->index == -1) {
1427 dev_err(cs35l41->dev, "No index found in %s\n", property);
1432 /* To use the same release code for all laptop variants we can't use devm_ version of
1433 * gpiod_get here, as CLSA010* don't have a fully functional bios with an _DSD node
1435 cs35l41->reset_gpio = fwnode_gpiod_get_index(acpi_fwnode_handle(adev), "reset", cs35l41->index,
1436 GPIOD_OUT_LOW, "cs35l41-reset");
1438 property = "cirrus,speaker-position";
1439 ret = device_property_read_u32_array(physdev, property, values, nval);
1442 hw_cfg->spk_pos = values[cs35l41->index];
1444 cs35l41->channel_index = 0;
1445 for (i = 0; i < cs35l41->index; i++)
1446 if (values[i] == hw_cfg->spk_pos)
1447 cs35l41->channel_index++;
1449 property = "cirrus,gpio1-func";
1450 ret = device_property_read_u32_array(physdev, property, values, nval);
1453 hw_cfg->gpio1.func = values[cs35l41->index];
1454 hw_cfg->gpio1.valid = true;
1456 property = "cirrus,gpio2-func";
1457 ret = device_property_read_u32_array(physdev, property, values, nval);
1460 hw_cfg->gpio2.func = values[cs35l41->index];
1461 hw_cfg->gpio2.valid = true;
1463 property = "cirrus,boost-peak-milliamp";
1464 ret = device_property_read_u32_array(physdev, property, values, nval);
1466 hw_cfg->bst_ipk = values[cs35l41->index];
1468 hw_cfg->bst_ipk = -1;
1470 property = "cirrus,boost-ind-nanohenry";
1471 ret = device_property_read_u32_array(physdev, property, values, nval);
1473 hw_cfg->bst_ind = values[cs35l41->index];
1475 hw_cfg->bst_ind = -1;
1477 property = "cirrus,boost-cap-microfarad";
1478 ret = device_property_read_u32_array(physdev, property, values, nval);
1480 hw_cfg->bst_cap = values[cs35l41->index];
1482 hw_cfg->bst_cap = -1;
1484 cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, cs35l41->index, nval, -1);
1486 if (hw_cfg->bst_ind > 0 || hw_cfg->bst_cap > 0 || hw_cfg->bst_ipk > 0)
1487 hw_cfg->bst_type = CS35L41_INT_BOOST;
1489 hw_cfg->bst_type = CS35L41_EXT_BOOST;
1491 hw_cfg->valid = true;
1492 put_device(physdev);
1497 dev_err(cs35l41->dev, "Failed property %s: %d\n", property, ret);
1498 hw_cfg->valid = false;
1499 hw_cfg->gpio1.valid = false;
1500 hw_cfg->gpio2.valid = false;
1502 put_device(physdev);
1507 int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int irq,
1508 struct regmap *regmap)
1510 unsigned int int_sts, regid, reg_revid, mtl_revid, chipid, int_status;
1511 struct cs35l41_hda *cs35l41;
1514 BUILD_BUG_ON(ARRAY_SIZE(cs35l41_irqs) != ARRAY_SIZE(cs35l41_reg_irqs));
1515 BUILD_BUG_ON(ARRAY_SIZE(cs35l41_irqs) != CS35L41_NUM_IRQ);
1518 return PTR_ERR(regmap);
1520 cs35l41 = devm_kzalloc(dev, sizeof(*cs35l41), GFP_KERNEL);
1526 cs35l41->regmap = regmap;
1527 dev_set_drvdata(dev, cs35l41);
1529 ret = cs35l41_hda_read_acpi(cs35l41, device_name, id);
1531 return dev_err_probe(cs35l41->dev, ret, "Platform not supported\n");
1533 if (IS_ERR(cs35l41->reset_gpio)) {
1534 ret = PTR_ERR(cs35l41->reset_gpio);
1535 cs35l41->reset_gpio = NULL;
1536 if (ret == -EBUSY) {
1537 dev_info(cs35l41->dev, "Reset line busy, assuming shared reset\n");
1539 dev_err_probe(cs35l41->dev, ret, "Failed to get reset GPIO\n");
1543 if (cs35l41->reset_gpio) {
1544 usleep_range(2000, 2100);
1545 gpiod_set_value_cansleep(cs35l41->reset_gpio, 1);
1548 usleep_range(2000, 2100);
1550 ret = regmap_read_poll_timeout(cs35l41->regmap, CS35L41_IRQ1_STATUS4, int_status,
1551 int_status & CS35L41_OTP_BOOT_DONE, 1000, 100000);
1553 dev_err(cs35l41->dev, "Failed waiting for OTP_BOOT_DONE: %d\n", ret);
1557 ret = regmap_read(cs35l41->regmap, CS35L41_IRQ1_STATUS3, &int_sts);
1558 if (ret || (int_sts & CS35L41_OTP_BOOT_ERR)) {
1559 dev_err(cs35l41->dev, "OTP Boot status %x error: %d\n",
1560 int_sts & CS35L41_OTP_BOOT_ERR, ret);
1565 ret = regmap_read(cs35l41->regmap, CS35L41_DEVID, ®id);
1567 dev_err(cs35l41->dev, "Get Device ID failed: %d\n", ret);
1571 ret = regmap_read(cs35l41->regmap, CS35L41_REVID, ®_revid);
1573 dev_err(cs35l41->dev, "Get Revision ID failed: %d\n", ret);
1577 mtl_revid = reg_revid & CS35L41_MTLREVID_MASK;
1579 chipid = (mtl_revid % 2) ? CS35L41R_CHIP_ID : CS35L41_CHIP_ID;
1580 if (regid != chipid) {
1581 dev_err(cs35l41->dev, "CS35L41 Device ID (%X). Expected ID %X\n", regid, chipid);
1586 ret = cs35l41_test_key_unlock(cs35l41->dev, cs35l41->regmap);
1590 ret = cs35l41_register_errata_patch(cs35l41->dev, cs35l41->regmap, reg_revid);
1594 ret = cs35l41_otp_unpack(cs35l41->dev, cs35l41->regmap);
1596 dev_err(cs35l41->dev, "OTP Unpack failed: %d\n", ret);
1600 ret = cs35l41_test_key_lock(cs35l41->dev, cs35l41->regmap);
1604 ret = regmap_multi_reg_write(cs35l41->regmap, cs35l41_hda_mute,
1605 ARRAY_SIZE(cs35l41_hda_mute));
1609 INIT_WORK(&cs35l41->fw_load_work, cs35l41_fw_load_work);
1610 mutex_init(&cs35l41->fw_mutex);
1612 pm_runtime_set_autosuspend_delay(cs35l41->dev, 3000);
1613 pm_runtime_use_autosuspend(cs35l41->dev);
1614 pm_runtime_mark_last_busy(cs35l41->dev);
1615 pm_runtime_set_active(cs35l41->dev);
1616 pm_runtime_get_noresume(cs35l41->dev);
1617 pm_runtime_enable(cs35l41->dev);
1619 ret = cs35l41_hda_apply_properties(cs35l41);
1623 pm_runtime_put_autosuspend(cs35l41->dev);
1625 ret = component_add(cs35l41->dev, &cs35l41_hda_comp_ops);
1627 dev_err(cs35l41->dev, "Register component failed: %d\n", ret);
1628 pm_runtime_disable(cs35l41->dev);
1632 dev_info(cs35l41->dev, "Cirrus Logic CS35L41 (%x), Revision: %02X\n", regid, reg_revid);
1637 pm_runtime_disable(cs35l41->dev);
1638 pm_runtime_put_noidle(cs35l41->dev);
1641 if (cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type))
1642 gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
1643 gpiod_put(cs35l41->reset_gpio);
1644 kfree(cs35l41->acpi_subsystem_id);
1648 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_probe, SND_HDA_SCODEC_CS35L41);
1650 void cs35l41_hda_remove(struct device *dev)
1652 struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
1654 pm_runtime_get_sync(cs35l41->dev);
1655 pm_runtime_disable(cs35l41->dev);
1657 if (cs35l41->halo_initialized)
1658 cs35l41_remove_dsp(cs35l41);
1660 component_del(cs35l41->dev, &cs35l41_hda_comp_ops);
1662 pm_runtime_put_noidle(cs35l41->dev);
1664 if (cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type))
1665 gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
1666 gpiod_put(cs35l41->reset_gpio);
1667 kfree(cs35l41->acpi_subsystem_id);
1669 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_remove, SND_HDA_SCODEC_CS35L41);
1671 const struct dev_pm_ops cs35l41_hda_pm_ops = {
1672 RUNTIME_PM_OPS(cs35l41_runtime_suspend, cs35l41_runtime_resume,
1673 cs35l41_runtime_idle)
1674 .prepare = cs35l41_system_suspend_prep,
1675 SYSTEM_SLEEP_PM_OPS(cs35l41_system_suspend, cs35l41_system_resume)
1677 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_pm_ops, SND_HDA_SCODEC_CS35L41);
1679 MODULE_DESCRIPTION("CS35L41 HDA Driver");
1680 MODULE_IMPORT_NS(SND_HDA_CS_DSP_CONTROLS);
1681 MODULE_AUTHOR("Lucas Tanure, Cirrus Logic Inc, <tanureal@opensource.cirrus.com>");
1682 MODULE_LICENSE("GPL");
1683 MODULE_IMPORT_NS(FW_CS_DSP);