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