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