xref: /linux/sound/pci/hda/cs35l41_hda.c (revision 3f41368fbfe1b3d5922d317fe1a0a0cab6846802)
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 *comps = master_data;
1423 	unsigned int sleep_flags;
1424 	int ret = 0;
1425 
1426 	if (!comps || cs35l41->index < 0 || cs35l41->index >= HDA_MAX_COMPONENTS)
1427 		return -EINVAL;
1428 
1429 	comps = &comps[cs35l41->index];
1430 	if (comps->dev)
1431 		return -EBUSY;
1432 
1433 	pm_runtime_get_sync(dev);
1434 
1435 	mutex_lock(&cs35l41->fw_mutex);
1436 
1437 	comps->dev = dev;
1438 	if (!cs35l41->acpi_subsystem_id)
1439 		cs35l41->acpi_subsystem_id = kasprintf(GFP_KERNEL, "%.8x",
1440 						       comps->codec->core.subsystem_id);
1441 	cs35l41->codec = comps->codec;
1442 	strscpy(comps->name, dev_name(dev), sizeof(comps->name));
1443 
1444 	cs35l41->firmware_type = HDA_CS_DSP_FW_SPK_PROT;
1445 
1446 	if (firmware_autostart) {
1447 		dev_dbg(cs35l41->dev, "Firmware Autostart.\n");
1448 		cs35l41->request_fw_load = true;
1449 		if (cs35l41_smart_amp(cs35l41) < 0)
1450 			dev_warn(cs35l41->dev, "Cannot Run Firmware, reverting to dsp bypass...\n");
1451 	} else {
1452 		dev_dbg(cs35l41->dev, "Firmware Autostart is disabled.\n");
1453 	}
1454 
1455 	ret = cs35l41_create_controls(cs35l41);
1456 
1457 	comps->playback_hook = cs35l41_hda_playback_hook;
1458 	comps->pre_playback_hook = cs35l41_hda_pre_playback_hook;
1459 	comps->post_playback_hook = cs35l41_hda_post_playback_hook;
1460 	comps->acpi_notify = cs35l41_acpi_device_notify;
1461 	comps->adev = cs35l41->dacpi;
1462 
1463 	comps->acpi_notifications_supported = cs35l41_dsm_supported(acpi_device_handle(comps->adev),
1464 		CS35L41_DSM_GET_MUTE);
1465 
1466 	cs35l41->mute_override = cs35l41_get_acpi_mute_state(cs35l41,
1467 						acpi_device_handle(cs35l41->dacpi)) > 0;
1468 
1469 	mutex_unlock(&cs35l41->fw_mutex);
1470 
1471 	sleep_flags = lock_system_sleep();
1472 	if (!device_link_add(&comps->codec->core.dev, cs35l41->dev, DL_FLAG_STATELESS))
1473 		dev_warn(dev, "Unable to create device link\n");
1474 	unlock_system_sleep(sleep_flags);
1475 
1476 	pm_runtime_mark_last_busy(dev);
1477 	pm_runtime_put_autosuspend(dev);
1478 
1479 	dev_info(cs35l41->dev,
1480 		 "CS35L41 Bound - SSID: %s, BST: %d, VSPK: %d, CH: %c, FW EN: %d, SPKID: %d\n",
1481 		 cs35l41->acpi_subsystem_id, cs35l41->hw_cfg.bst_type,
1482 		 cs35l41->hw_cfg.gpio1.func == CS35l41_VSPK_SWITCH,
1483 		 cs35l41->hw_cfg.spk_pos ? 'R' : 'L',
1484 		 cs35l41->cs_dsp.running, cs35l41->speaker_id);
1485 
1486 	return ret;
1487 }
1488 
1489 static void cs35l41_hda_unbind(struct device *dev, struct device *master, void *master_data)
1490 {
1491 	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
1492 	struct hda_component *comps = master_data;
1493 	unsigned int sleep_flags;
1494 
1495 	if (comps[cs35l41->index].dev == dev) {
1496 		memset(&comps[cs35l41->index], 0, sizeof(*comps));
1497 		sleep_flags = lock_system_sleep();
1498 		device_link_remove(&comps->codec->core.dev, cs35l41->dev);
1499 		unlock_system_sleep(sleep_flags);
1500 	}
1501 }
1502 
1503 static const struct component_ops cs35l41_hda_comp_ops = {
1504 	.bind = cs35l41_hda_bind,
1505 	.unbind = cs35l41_hda_unbind,
1506 };
1507 
1508 static irqreturn_t cs35l41_bst_short_err(int irq, void *data)
1509 {
1510 	struct cs35l41_hda *cs35l41 = data;
1511 
1512 	dev_crit_ratelimited(cs35l41->dev, "LBST Error\n");
1513 	set_bit(CS35L41_BST_SHORT_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1514 
1515 	return IRQ_HANDLED;
1516 }
1517 
1518 static irqreturn_t cs35l41_bst_dcm_uvp_err(int irq, void *data)
1519 {
1520 	struct cs35l41_hda *cs35l41 = data;
1521 
1522 	dev_crit_ratelimited(cs35l41->dev, "DCM VBST Under Voltage Error\n");
1523 	set_bit(CS35L41_BST_UVP_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1524 
1525 	return IRQ_HANDLED;
1526 }
1527 
1528 static irqreturn_t cs35l41_bst_ovp_err(int irq, void *data)
1529 {
1530 	struct cs35l41_hda *cs35l41 = data;
1531 
1532 	dev_crit_ratelimited(cs35l41->dev, "VBST Over Voltage error\n");
1533 	set_bit(CS35L41_BST_OVP_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1534 
1535 	return IRQ_HANDLED;
1536 }
1537 
1538 static irqreturn_t cs35l41_temp_err(int irq, void *data)
1539 {
1540 	struct cs35l41_hda *cs35l41 = data;
1541 
1542 	dev_crit_ratelimited(cs35l41->dev, "Over temperature error\n");
1543 	set_bit(CS35L41_TEMP_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1544 
1545 	return IRQ_HANDLED;
1546 }
1547 
1548 static irqreturn_t cs35l41_temp_warn(int irq, void *data)
1549 {
1550 	struct cs35l41_hda *cs35l41 = data;
1551 
1552 	dev_crit_ratelimited(cs35l41->dev, "Over temperature warning\n");
1553 	set_bit(CS35L41_TEMP_WARN_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1554 
1555 	return IRQ_HANDLED;
1556 }
1557 
1558 static irqreturn_t cs35l41_amp_short(int irq, void *data)
1559 {
1560 	struct cs35l41_hda *cs35l41 = data;
1561 
1562 	dev_crit_ratelimited(cs35l41->dev, "Amp short error\n");
1563 	set_bit(CS35L41_AMP_SHORT_ERR_RLS_SHIFT, &cs35l41->irq_errors);
1564 
1565 	return IRQ_HANDLED;
1566 }
1567 
1568 static const struct cs35l41_irq cs35l41_irqs[] = {
1569 	CS35L41_IRQ(BST_OVP_ERR, "Boost Overvoltage Error", cs35l41_bst_ovp_err),
1570 	CS35L41_IRQ(BST_DCM_UVP_ERR, "Boost Undervoltage Error", cs35l41_bst_dcm_uvp_err),
1571 	CS35L41_IRQ(BST_SHORT_ERR, "Boost Inductor Short Error", cs35l41_bst_short_err),
1572 	CS35L41_IRQ(TEMP_WARN, "Temperature Warning", cs35l41_temp_warn),
1573 	CS35L41_IRQ(TEMP_ERR, "Temperature Error", cs35l41_temp_err),
1574 	CS35L41_IRQ(AMP_SHORT_ERR, "Amp Short", cs35l41_amp_short),
1575 };
1576 
1577 static const struct regmap_irq cs35l41_reg_irqs[] = {
1578 	CS35L41_REG_IRQ(IRQ1_STATUS1, BST_OVP_ERR),
1579 	CS35L41_REG_IRQ(IRQ1_STATUS1, BST_DCM_UVP_ERR),
1580 	CS35L41_REG_IRQ(IRQ1_STATUS1, BST_SHORT_ERR),
1581 	CS35L41_REG_IRQ(IRQ1_STATUS1, TEMP_WARN),
1582 	CS35L41_REG_IRQ(IRQ1_STATUS1, TEMP_ERR),
1583 	CS35L41_REG_IRQ(IRQ1_STATUS1, AMP_SHORT_ERR),
1584 };
1585 
1586 static struct regmap_irq_chip cs35l41_regmap_irq_chip = {
1587 	.name = "cs35l41 IRQ1 Controller",
1588 	.status_base = CS35L41_IRQ1_STATUS1,
1589 	.mask_base = CS35L41_IRQ1_MASK1,
1590 	.ack_base = CS35L41_IRQ1_STATUS1,
1591 	.num_regs = 4,
1592 	.irqs = cs35l41_reg_irqs,
1593 	.num_irqs = ARRAY_SIZE(cs35l41_reg_irqs),
1594 	.runtime_pm = true,
1595 };
1596 
1597 static void cs35l41_configure_interrupt(struct cs35l41_hda *cs35l41, int irq_pol)
1598 {
1599 	int irq;
1600 	int ret;
1601 	int i;
1602 
1603 	if (!cs35l41->irq) {
1604 		dev_warn(cs35l41->dev, "No Interrupt Found");
1605 		goto err;
1606 	}
1607 
1608 	ret = devm_regmap_add_irq_chip(cs35l41->dev, cs35l41->regmap, cs35l41->irq,
1609 					IRQF_ONESHOT | IRQF_SHARED | irq_pol,
1610 					0, &cs35l41_regmap_irq_chip, &cs35l41->irq_data);
1611 	if (ret) {
1612 		dev_dbg(cs35l41->dev, "Unable to add IRQ Chip: %d.", ret);
1613 		goto err;
1614 	}
1615 
1616 	for (i = 0; i < ARRAY_SIZE(cs35l41_irqs); i++) {
1617 		irq = regmap_irq_get_virq(cs35l41->irq_data, cs35l41_irqs[i].irq);
1618 		if (irq < 0) {
1619 			ret = irq;
1620 			dev_dbg(cs35l41->dev, "Unable to map IRQ %s: %d.", cs35l41_irqs[i].name,
1621 				ret);
1622 			goto err;
1623 		}
1624 
1625 		ret = devm_request_threaded_irq(cs35l41->dev, irq, NULL,
1626 						cs35l41_irqs[i].handler,
1627 						IRQF_ONESHOT | IRQF_SHARED | irq_pol,
1628 						cs35l41_irqs[i].name, cs35l41);
1629 		if (ret) {
1630 			dev_dbg(cs35l41->dev, "Unable to allocate IRQ %s:: %d.",
1631 				cs35l41_irqs[i].name, ret);
1632 			goto err;
1633 		}
1634 	}
1635 	return;
1636 err:
1637 	dev_warn(cs35l41->dev,
1638 		 "IRQ Config Failed. Amp errors may not be recoverable without reboot.");
1639 }
1640 
1641 static int cs35l41_hda_apply_properties(struct cs35l41_hda *cs35l41)
1642 {
1643 	struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
1644 	bool using_irq = false;
1645 	int irq_pol;
1646 	int ret;
1647 
1648 	if (!cs35l41->hw_cfg.valid)
1649 		return -EINVAL;
1650 
1651 	ret = cs35l41_init_boost(cs35l41->dev, cs35l41->regmap, hw_cfg);
1652 	if (ret)
1653 		return ret;
1654 
1655 	if (hw_cfg->gpio1.valid) {
1656 		switch (hw_cfg->gpio1.func) {
1657 		case CS35L41_NOT_USED:
1658 			break;
1659 		case CS35l41_VSPK_SWITCH:
1660 			hw_cfg->gpio1.func = CS35L41_GPIO1_GPIO;
1661 			hw_cfg->gpio1.out_en = true;
1662 			break;
1663 		case CS35l41_SYNC:
1664 			hw_cfg->gpio1.func = CS35L41_GPIO1_MDSYNC;
1665 			break;
1666 		default:
1667 			dev_err(cs35l41->dev, "Invalid function %d for GPIO1\n",
1668 				hw_cfg->gpio1.func);
1669 			return -EINVAL;
1670 		}
1671 	}
1672 
1673 	if (hw_cfg->gpio2.valid) {
1674 		switch (hw_cfg->gpio2.func) {
1675 		case CS35L41_NOT_USED:
1676 			break;
1677 		case CS35L41_INTERRUPT:
1678 			using_irq = true;
1679 			hw_cfg->gpio2.func = CS35L41_GPIO2_INT_OPEN_DRAIN;
1680 			break;
1681 		default:
1682 			dev_err(cs35l41->dev, "Invalid GPIO2 function %d\n", hw_cfg->gpio2.func);
1683 			return -EINVAL;
1684 		}
1685 	}
1686 
1687 	irq_pol = cs35l41_gpio_config(cs35l41->regmap, hw_cfg);
1688 
1689 	if (using_irq)
1690 		cs35l41_configure_interrupt(cs35l41, irq_pol);
1691 
1692 	return cs35l41_hda_channel_map(cs35l41->dev, 0, NULL, 1, &hw_cfg->spk_pos);
1693 }
1694 
1695 int cs35l41_get_speaker_id(struct device *dev, int amp_index, int num_amps, int fixed_gpio_id)
1696 {
1697 	struct gpio_desc *speaker_id_desc;
1698 	int speaker_id = -ENODEV;
1699 
1700 	if (fixed_gpio_id >= 0) {
1701 		dev_dbg(dev, "Found Fixed Speaker ID GPIO (index = %d)\n", fixed_gpio_id);
1702 		speaker_id_desc = gpiod_get_index(dev, NULL, fixed_gpio_id, GPIOD_IN);
1703 		if (IS_ERR(speaker_id_desc)) {
1704 			speaker_id = PTR_ERR(speaker_id_desc);
1705 			return speaker_id;
1706 		}
1707 		speaker_id = gpiod_get_value_cansleep(speaker_id_desc);
1708 		gpiod_put(speaker_id_desc);
1709 		dev_dbg(dev, "Speaker ID = %d\n", speaker_id);
1710 	} else {
1711 		int base_index;
1712 		int gpios_per_amp;
1713 		int count;
1714 		int tmp;
1715 		int i;
1716 
1717 		count = gpiod_count(dev, "spk-id");
1718 		if (count > 0) {
1719 			speaker_id = 0;
1720 			gpios_per_amp = count / num_amps;
1721 			base_index = gpios_per_amp * amp_index;
1722 
1723 			if (count % num_amps)
1724 				return -EINVAL;
1725 
1726 			dev_dbg(dev, "Found %d Speaker ID GPIOs per Amp\n", gpios_per_amp);
1727 
1728 			for (i = 0; i < gpios_per_amp; i++) {
1729 				speaker_id_desc = gpiod_get_index(dev, "spk-id", i + base_index,
1730 								  GPIOD_IN);
1731 				if (IS_ERR(speaker_id_desc)) {
1732 					speaker_id = PTR_ERR(speaker_id_desc);
1733 					break;
1734 				}
1735 				tmp = gpiod_get_value_cansleep(speaker_id_desc);
1736 				gpiod_put(speaker_id_desc);
1737 				if (tmp < 0) {
1738 					speaker_id = tmp;
1739 					break;
1740 				}
1741 				speaker_id |= tmp << i;
1742 			}
1743 			dev_dbg(dev, "Speaker ID = %d\n", speaker_id);
1744 		}
1745 	}
1746 	return speaker_id;
1747 }
1748 
1749 static int cs35l41_hda_read_acpi(struct cs35l41_hda *cs35l41, const char *hid, int id)
1750 {
1751 	struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
1752 	u32 values[HDA_MAX_COMPONENTS];
1753 	struct acpi_device *adev;
1754 	struct device *physdev;
1755 	struct spi_device *spi;
1756 	const char *sub;
1757 	char *property;
1758 	size_t nval;
1759 	int i, ret;
1760 
1761 	adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
1762 	if (!adev) {
1763 		dev_err(cs35l41->dev, "Failed to find an ACPI device for %s\n", hid);
1764 		return -ENODEV;
1765 	}
1766 
1767 	cs35l41->dacpi = adev;
1768 	physdev = get_device(acpi_get_first_physical_node(adev));
1769 
1770 	sub = acpi_get_subsystem_id(ACPI_HANDLE(physdev));
1771 	if (IS_ERR(sub))
1772 		sub = NULL;
1773 	cs35l41->acpi_subsystem_id = sub;
1774 
1775 	ret = cs35l41_add_dsd_properties(cs35l41, physdev, id, hid);
1776 	if (!ret) {
1777 		dev_info(cs35l41->dev, "Using extra _DSD properties, bypassing _DSD in ACPI\n");
1778 		goto out;
1779 	}
1780 
1781 	property = "cirrus,dev-index";
1782 	ret = device_property_count_u32(physdev, property);
1783 	if (ret <= 0)
1784 		goto err;
1785 
1786 	if (ret > ARRAY_SIZE(values)) {
1787 		ret = -EINVAL;
1788 		goto err;
1789 	}
1790 	nval = ret;
1791 
1792 	ret = device_property_read_u32_array(physdev, property, values, nval);
1793 	if (ret)
1794 		goto err;
1795 
1796 	cs35l41->index = -1;
1797 	for (i = 0; i < nval; i++) {
1798 		if (values[i] == id) {
1799 			cs35l41->index = i;
1800 			break;
1801 		}
1802 	}
1803 	if (cs35l41->index == -1) {
1804 		dev_err(cs35l41->dev, "No index found in %s\n", property);
1805 		ret = -ENODEV;
1806 		goto err;
1807 	}
1808 
1809 	/* To use the same release code for all laptop variants we can't use devm_ version of
1810 	 * gpiod_get here, as CLSA010* don't have a fully functional bios with an _DSD node
1811 	 */
1812 	cs35l41->reset_gpio = fwnode_gpiod_get_index(acpi_fwnode_handle(adev), "reset", cs35l41->index,
1813 						     GPIOD_OUT_LOW, "cs35l41-reset");
1814 
1815 	property = "cirrus,speaker-position";
1816 	ret = device_property_read_u32_array(physdev, property, values, nval);
1817 	if (ret)
1818 		goto err;
1819 	hw_cfg->spk_pos = values[cs35l41->index];
1820 
1821 	cs35l41->channel_index = 0;
1822 	for (i = 0; i < cs35l41->index; i++)
1823 		if (values[i] == hw_cfg->spk_pos)
1824 			cs35l41->channel_index++;
1825 
1826 	property = "cirrus,gpio1-func";
1827 	ret = device_property_read_u32_array(physdev, property, values, nval);
1828 	if (ret)
1829 		goto err;
1830 	hw_cfg->gpio1.func = values[cs35l41->index];
1831 	hw_cfg->gpio1.valid = true;
1832 
1833 	property = "cirrus,gpio2-func";
1834 	ret = device_property_read_u32_array(physdev, property, values, nval);
1835 	if (ret)
1836 		goto err;
1837 	hw_cfg->gpio2.func = values[cs35l41->index];
1838 	hw_cfg->gpio2.valid = true;
1839 
1840 	property = "cirrus,boost-peak-milliamp";
1841 	ret = device_property_read_u32_array(physdev, property, values, nval);
1842 	if (ret == 0)
1843 		hw_cfg->bst_ipk = values[cs35l41->index];
1844 	else
1845 		hw_cfg->bst_ipk = -1;
1846 
1847 	property = "cirrus,boost-ind-nanohenry";
1848 	ret = device_property_read_u32_array(physdev, property, values, nval);
1849 	if (ret == 0)
1850 		hw_cfg->bst_ind = values[cs35l41->index];
1851 	else
1852 		hw_cfg->bst_ind = -1;
1853 
1854 	property = "cirrus,boost-cap-microfarad";
1855 	ret = device_property_read_u32_array(physdev, property, values, nval);
1856 	if (ret == 0)
1857 		hw_cfg->bst_cap = values[cs35l41->index];
1858 	else
1859 		hw_cfg->bst_cap = -1;
1860 
1861 	cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, cs35l41->index, nval, -1);
1862 
1863 	if (hw_cfg->bst_ind > 0 || hw_cfg->bst_cap > 0 || hw_cfg->bst_ipk > 0)
1864 		hw_cfg->bst_type = CS35L41_INT_BOOST;
1865 	else
1866 		hw_cfg->bst_type = CS35L41_EXT_BOOST;
1867 
1868 	hw_cfg->valid = true;
1869 out:
1870 	put_device(physdev);
1871 
1872 	cs35l41->bypass_fw = false;
1873 	if (cs35l41->control_bus == SPI) {
1874 		spi = to_spi_device(cs35l41->dev);
1875 		if (spi->max_speed_hz < CS35L41_MAX_ACCEPTABLE_SPI_SPEED_HZ) {
1876 			dev_warn(cs35l41->dev,
1877 				 "SPI speed is too slow to support firmware download: %d Hz.\n",
1878 				 spi->max_speed_hz);
1879 			cs35l41->bypass_fw = true;
1880 		}
1881 	}
1882 
1883 	return 0;
1884 
1885 err:
1886 	dev_err(cs35l41->dev, "Failed property %s: %d\n", property, ret);
1887 	hw_cfg->valid = false;
1888 	hw_cfg->gpio1.valid = false;
1889 	hw_cfg->gpio2.valid = false;
1890 	acpi_dev_put(cs35l41->dacpi);
1891 	put_device(physdev);
1892 
1893 	return ret;
1894 }
1895 
1896 int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int irq,
1897 		      struct regmap *regmap, enum control_bus control_bus)
1898 {
1899 	unsigned int regid, reg_revid;
1900 	struct cs35l41_hda *cs35l41;
1901 	int ret;
1902 
1903 	BUILD_BUG_ON(ARRAY_SIZE(cs35l41_irqs) != ARRAY_SIZE(cs35l41_reg_irqs));
1904 	BUILD_BUG_ON(ARRAY_SIZE(cs35l41_irqs) != CS35L41_NUM_IRQ);
1905 
1906 	if (IS_ERR(regmap))
1907 		return PTR_ERR(regmap);
1908 
1909 	cs35l41 = devm_kzalloc(dev, sizeof(*cs35l41), GFP_KERNEL);
1910 	if (!cs35l41)
1911 		return -ENOMEM;
1912 
1913 	cs35l41->dev = dev;
1914 	cs35l41->irq = irq;
1915 	cs35l41->regmap = regmap;
1916 	cs35l41->control_bus = control_bus;
1917 	dev_set_drvdata(dev, cs35l41);
1918 
1919 	ret = cs35l41_hda_read_acpi(cs35l41, device_name, id);
1920 	if (ret)
1921 		return dev_err_probe(cs35l41->dev, ret, "Platform not supported\n");
1922 
1923 	if (IS_ERR(cs35l41->reset_gpio)) {
1924 		ret = PTR_ERR(cs35l41->reset_gpio);
1925 		cs35l41->reset_gpio = NULL;
1926 		if (ret == -EBUSY) {
1927 			dev_info(cs35l41->dev, "Reset line busy, assuming shared reset\n");
1928 		} else {
1929 			dev_err_probe(cs35l41->dev, ret, "Failed to get reset GPIO\n");
1930 			goto err;
1931 		}
1932 	}
1933 	if (cs35l41->reset_gpio) {
1934 		gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
1935 		usleep_range(2000, 2100);
1936 		gpiod_set_value_cansleep(cs35l41->reset_gpio, 1);
1937 	}
1938 
1939 	usleep_range(2000, 2100);
1940 	regmap_write(cs35l41->regmap, CS35L41_SFT_RESET, CS35L41_SOFTWARE_RESET);
1941 	usleep_range(2000, 2100);
1942 
1943 	ret = cs35l41_wait_boot_done(cs35l41);
1944 	if (ret)
1945 		goto err;
1946 
1947 	ret = cs35l41_verify_id(cs35l41, &regid, &reg_revid);
1948 	if (ret)
1949 		goto err;
1950 
1951 	ret = cs35l41_test_key_unlock(cs35l41->dev, cs35l41->regmap);
1952 	if (ret)
1953 		goto err;
1954 
1955 	ret = cs35l41_register_errata_patch(cs35l41->dev, cs35l41->regmap, reg_revid);
1956 	if (ret)
1957 		goto err;
1958 
1959 	ret = cs35l41_otp_unpack(cs35l41->dev, cs35l41->regmap);
1960 	if (ret) {
1961 		dev_err_probe(cs35l41->dev, ret, "OTP Unpack failed\n");
1962 		goto err;
1963 	}
1964 
1965 	ret = cs35l41_test_key_lock(cs35l41->dev, cs35l41->regmap);
1966 	if (ret)
1967 		goto err;
1968 
1969 	ret = cs35l41_get_calibration(cs35l41);
1970 	if (ret && ret != -ENOENT)
1971 		goto err;
1972 
1973 	cs35l41_mute(cs35l41->dev, true);
1974 
1975 	INIT_WORK(&cs35l41->fw_load_work, cs35l41_fw_load_work);
1976 	mutex_init(&cs35l41->fw_mutex);
1977 
1978 	pm_runtime_set_autosuspend_delay(cs35l41->dev, 3000);
1979 	pm_runtime_use_autosuspend(cs35l41->dev);
1980 	pm_runtime_mark_last_busy(cs35l41->dev);
1981 	pm_runtime_set_active(cs35l41->dev);
1982 	pm_runtime_get_noresume(cs35l41->dev);
1983 	pm_runtime_enable(cs35l41->dev);
1984 
1985 	ret = cs35l41_hda_apply_properties(cs35l41);
1986 	if (ret)
1987 		goto err_pm;
1988 
1989 	pm_runtime_put_autosuspend(cs35l41->dev);
1990 
1991 	ret = component_add(cs35l41->dev, &cs35l41_hda_comp_ops);
1992 	if (ret) {
1993 		dev_err_probe(cs35l41->dev, ret, "Register component failed\n");
1994 		goto err_pm;
1995 	}
1996 
1997 	dev_info(cs35l41->dev, "Cirrus Logic CS35L41 (%x), Revision: %02X\n", regid, reg_revid);
1998 
1999 	return 0;
2000 
2001 err_pm:
2002 	pm_runtime_dont_use_autosuspend(cs35l41->dev);
2003 	pm_runtime_disable(cs35l41->dev);
2004 	pm_runtime_put_noidle(cs35l41->dev);
2005 
2006 err:
2007 	if (cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type))
2008 		gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
2009 	gpiod_put(cs35l41->reset_gpio);
2010 	gpiod_put(cs35l41->cs_gpio);
2011 	acpi_dev_put(cs35l41->dacpi);
2012 	kfree(cs35l41->acpi_subsystem_id);
2013 
2014 	return ret;
2015 }
2016 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_probe, SND_HDA_SCODEC_CS35L41);
2017 
2018 void cs35l41_hda_remove(struct device *dev)
2019 {
2020 	struct cs35l41_hda *cs35l41 = dev_get_drvdata(dev);
2021 
2022 	pm_runtime_get_sync(cs35l41->dev);
2023 	pm_runtime_dont_use_autosuspend(cs35l41->dev);
2024 	pm_runtime_disable(cs35l41->dev);
2025 
2026 	if (cs35l41->halo_initialized)
2027 		cs35l41_remove_dsp(cs35l41);
2028 
2029 	component_del(cs35l41->dev, &cs35l41_hda_comp_ops);
2030 
2031 	acpi_dev_put(cs35l41->dacpi);
2032 
2033 	pm_runtime_put_noidle(cs35l41->dev);
2034 
2035 	if (cs35l41_safe_reset(cs35l41->regmap, cs35l41->hw_cfg.bst_type))
2036 		gpiod_set_value_cansleep(cs35l41->reset_gpio, 0);
2037 	gpiod_put(cs35l41->reset_gpio);
2038 	gpiod_put(cs35l41->cs_gpio);
2039 	kfree(cs35l41->acpi_subsystem_id);
2040 }
2041 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_remove, SND_HDA_SCODEC_CS35L41);
2042 
2043 const struct dev_pm_ops cs35l41_hda_pm_ops = {
2044 	RUNTIME_PM_OPS(cs35l41_runtime_suspend, cs35l41_runtime_resume,
2045 		       cs35l41_runtime_idle)
2046 	.prepare = cs35l41_system_suspend_prep,
2047 	SYSTEM_SLEEP_PM_OPS(cs35l41_system_suspend, cs35l41_system_resume)
2048 };
2049 EXPORT_SYMBOL_NS_GPL(cs35l41_hda_pm_ops, SND_HDA_SCODEC_CS35L41);
2050 
2051 MODULE_DESCRIPTION("CS35L41 HDA Driver");
2052 MODULE_IMPORT_NS(SND_HDA_CS_DSP_CONTROLS);
2053 MODULE_IMPORT_NS(SND_SOC_CS_AMP_LIB);
2054 MODULE_AUTHOR("Lucas Tanure, Cirrus Logic Inc, <tanureal@opensource.cirrus.com>");
2055 MODULE_LICENSE("GPL");
2056 MODULE_IMPORT_NS(FW_CS_DSP);
2057