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