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