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