xref: /linux/sound/pci/hda/cs35l56_hda.c (revision 85d12eda2382cd5b93eed720b5a08f39d42cfef4)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // HDA audio driver for Cirrus Logic CS35L56 smart amp
4 //
5 // Copyright (C) 2023 Cirrus Logic, Inc. and
6 //                    Cirrus Logic International Semiconductor Ltd.
7 //
8 
9 #include <linux/acpi.h>
10 #include <linux/debugfs.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/module.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/regmap.h>
15 #include <linux/slab.h>
16 #include <sound/core.h>
17 #include <sound/hda_codec.h>
18 #include <sound/tlv.h>
19 #include "cs35l56_hda.h"
20 #include "hda_component.h"
21 #include "hda_cs_dsp_ctl.h"
22 #include "hda_generic.h"
23 
24  /*
25   * The cs35l56_hda_dai_config[] reg sequence configures the device as
26   *  ASP1_BCLK_FREQ = 3.072 MHz
27   *  ASP1_RX_WIDTH = 32 cycles per slot, ASP1_TX_WIDTH = 32 cycles per slot, ASP1_FMT = I2S
28   *  ASP1_DOUT_HIZ_CONTROL = Hi-Z during unused timeslots
29   *  ASP1_RX_WL = 24 bits per sample
30   *  ASP1_TX_WL = 24 bits per sample
31   *  ASP1_RXn_EN 1..3 and ASP1_TXn_EN 1..4 disabled
32   */
33 static const struct reg_sequence cs35l56_hda_dai_config[] = {
34 	{ CS35L56_ASP1_CONTROL1,	0x00000021 },
35 	{ CS35L56_ASP1_CONTROL2,	0x20200200 },
36 	{ CS35L56_ASP1_CONTROL3,	0x00000003 },
37 	{ CS35L56_ASP1_DATA_CONTROL5,	0x00000018 },
38 	{ CS35L56_ASP1_DATA_CONTROL1,	0x00000018 },
39 	{ CS35L56_ASP1_ENABLES1,	0x00000000 },
40 };
41 
42 static void cs35l56_hda_play(struct cs35l56_hda *cs35l56)
43 {
44 	unsigned int val;
45 	int ret;
46 
47 	pm_runtime_get_sync(cs35l56->base.dev);
48 	ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_PLAY);
49 	if (ret == 0) {
50 		/* Wait for firmware to enter PS0 power state */
51 		ret = regmap_read_poll_timeout(cs35l56->base.regmap,
52 					       CS35L56_TRANSDUCER_ACTUAL_PS,
53 					       val, (val == CS35L56_PS0),
54 					       CS35L56_PS0_POLL_US,
55 					       CS35L56_PS0_TIMEOUT_US);
56 		if (ret)
57 			dev_warn(cs35l56->base.dev, "PS0 wait failed: %d\n", ret);
58 	}
59 	regmap_set_bits(cs35l56->base.regmap, CS35L56_ASP1_ENABLES1,
60 			BIT(CS35L56_ASP_RX1_EN_SHIFT) | BIT(CS35L56_ASP_RX2_EN_SHIFT) |
61 			cs35l56->asp_tx_mask);
62 	cs35l56->playing = true;
63 }
64 
65 static void cs35l56_hda_pause(struct cs35l56_hda *cs35l56)
66 {
67 	cs35l56->playing = false;
68 	cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_PAUSE);
69 	regmap_clear_bits(cs35l56->base.regmap, CS35L56_ASP1_ENABLES1,
70 			  BIT(CS35L56_ASP_RX1_EN_SHIFT) | BIT(CS35L56_ASP_RX2_EN_SHIFT) |
71 			  BIT(CS35L56_ASP_TX1_EN_SHIFT) | BIT(CS35L56_ASP_TX2_EN_SHIFT) |
72 			  BIT(CS35L56_ASP_TX3_EN_SHIFT) | BIT(CS35L56_ASP_TX4_EN_SHIFT));
73 
74 	pm_runtime_mark_last_busy(cs35l56->base.dev);
75 	pm_runtime_put_autosuspend(cs35l56->base.dev);
76 }
77 
78 static void cs35l56_hda_playback_hook(struct device *dev, int action)
79 {
80 	struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
81 
82 	dev_dbg(cs35l56->base.dev, "%s()%d: action: %d\n", __func__, __LINE__, action);
83 
84 	switch (action) {
85 	case HDA_GEN_PCM_ACT_PREPARE:
86 		if (cs35l56->playing)
87 			break;
88 
89 		/* If we're suspended: flag that resume should start playback */
90 		if (cs35l56->suspended) {
91 			cs35l56->playing = true;
92 			break;
93 		}
94 
95 		cs35l56_hda_play(cs35l56);
96 		break;
97 	case HDA_GEN_PCM_ACT_CLEANUP:
98 		if (!cs35l56->playing)
99 			break;
100 
101 		cs35l56_hda_pause(cs35l56);
102 		break;
103 	default:
104 		break;
105 	}
106 }
107 
108 static int __maybe_unused cs35l56_hda_runtime_suspend(struct device *dev)
109 {
110 	struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
111 
112 	if (cs35l56->cs_dsp.booted)
113 		cs_dsp_stop(&cs35l56->cs_dsp);
114 
115 	return cs35l56_runtime_suspend_common(&cs35l56->base);
116 }
117 
118 static int __maybe_unused cs35l56_hda_runtime_resume(struct device *dev)
119 {
120 	struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
121 	int ret;
122 
123 	ret = cs35l56_runtime_resume_common(&cs35l56->base, false);
124 	if (ret < 0)
125 		return ret;
126 
127 	if (cs35l56->cs_dsp.booted) {
128 		ret = cs_dsp_run(&cs35l56->cs_dsp);
129 		if (ret) {
130 			dev_dbg(cs35l56->base.dev, "%s: cs_dsp_run ret %d\n", __func__, ret);
131 			goto err;
132 		}
133 	}
134 
135 	return 0;
136 
137 err:
138 	cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_ALLOW_AUTO_HIBERNATE);
139 	regmap_write(cs35l56->base.regmap, CS35L56_DSP_VIRTUAL1_MBOX_1,
140 		     CS35L56_MBOX_CMD_HIBERNATE_NOW);
141 
142 	regcache_cache_only(cs35l56->base.regmap, true);
143 
144 	return ret;
145 }
146 
147 static int cs35l56_hda_mixer_info(struct snd_kcontrol *kcontrol,
148 				  struct snd_ctl_elem_info *uinfo)
149 {
150 	uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
151 	uinfo->count = 1;
152 	uinfo->value.enumerated.items = CS35L56_NUM_INPUT_SRC;
153 	if (uinfo->value.enumerated.item >= CS35L56_NUM_INPUT_SRC)
154 		uinfo->value.enumerated.item = CS35L56_NUM_INPUT_SRC - 1;
155 	strscpy(uinfo->value.enumerated.name, cs35l56_tx_input_texts[uinfo->value.enumerated.item],
156 		sizeof(uinfo->value.enumerated.name));
157 
158 	return 0;
159 }
160 
161 static int cs35l56_hda_mixer_get(struct snd_kcontrol *kcontrol,
162 				 struct snd_ctl_elem_value *ucontrol)
163 {
164 	struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data;
165 	unsigned int reg_val;
166 	int i;
167 
168 	regmap_read(cs35l56->base.regmap, kcontrol->private_value, &reg_val);
169 	reg_val &= CS35L56_ASP_TXn_SRC_MASK;
170 
171 	for (i = 0; i < CS35L56_NUM_INPUT_SRC; ++i) {
172 		if (cs35l56_tx_input_values[i] == reg_val) {
173 			ucontrol->value.enumerated.item[0] = i;
174 			break;
175 		}
176 	}
177 
178 	return 0;
179 }
180 
181 static int cs35l56_hda_mixer_put(struct snd_kcontrol *kcontrol,
182 				 struct snd_ctl_elem_value *ucontrol)
183 {
184 	struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data;
185 	unsigned int item = ucontrol->value.enumerated.item[0];
186 	bool changed;
187 
188 	if (item >= CS35L56_NUM_INPUT_SRC)
189 		return -EINVAL;
190 
191 	regmap_update_bits_check(cs35l56->base.regmap, kcontrol->private_value,
192 				 CS35L56_INPUT_MASK, cs35l56_tx_input_values[item],
193 				 &changed);
194 
195 	return changed;
196 }
197 
198 static int cs35l56_hda_posture_info(struct snd_kcontrol *kcontrol,
199 				    struct snd_ctl_elem_info *uinfo)
200 {
201 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
202 	uinfo->count = 1;
203 	uinfo->value.integer.min = CS35L56_MAIN_POSTURE_MIN;
204 	uinfo->value.integer.max = CS35L56_MAIN_POSTURE_MAX;
205 	return 0;
206 }
207 
208 static int cs35l56_hda_posture_get(struct snd_kcontrol *kcontrol,
209 				   struct snd_ctl_elem_value *ucontrol)
210 {
211 	struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data;
212 	unsigned int pos;
213 	int ret;
214 
215 	ret = regmap_read(cs35l56->base.regmap, CS35L56_MAIN_POSTURE_NUMBER, &pos);
216 	if (ret)
217 		return ret;
218 
219 	ucontrol->value.integer.value[0] = pos;
220 
221 	return ret;
222 }
223 
224 static int cs35l56_hda_posture_put(struct snd_kcontrol *kcontrol,
225 				   struct snd_ctl_elem_value *ucontrol)
226 {
227 	struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data;
228 	unsigned long pos = ucontrol->value.integer.value[0];
229 	bool changed;
230 	int ret;
231 
232 	if ((pos < CS35L56_MAIN_POSTURE_MIN) ||
233 	    (pos > CS35L56_MAIN_POSTURE_MAX))
234 		return -EINVAL;
235 
236 	ret = regmap_update_bits_check(cs35l56->base.regmap,
237 				       CS35L56_MAIN_POSTURE_NUMBER,
238 				       CS35L56_MAIN_POSTURE_MASK,
239 				       pos, &changed);
240 	if (ret)
241 		return ret;
242 
243 	return changed;
244 }
245 
246 static const struct {
247 	const char *name;
248 	unsigned int reg;
249 } cs35l56_hda_mixer_controls[] = {
250 	{ "ASP1 TX1 Source", CS35L56_ASP1TX1_INPUT },
251 	{ "ASP1 TX2 Source", CS35L56_ASP1TX2_INPUT },
252 	{ "ASP1 TX3 Source", CS35L56_ASP1TX3_INPUT },
253 	{ "ASP1 TX4 Source", CS35L56_ASP1TX4_INPUT },
254 };
255 
256 static const DECLARE_TLV_DB_SCALE(cs35l56_hda_vol_tlv, -10000, 25, 0);
257 
258 static int cs35l56_hda_vol_info(struct snd_kcontrol *kcontrol,
259 				struct snd_ctl_elem_info *uinfo)
260 {
261 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
262 	uinfo->count = 1;
263 	uinfo->value.integer.step = 1;
264 	uinfo->value.integer.min = 0;
265 	uinfo->value.integer.max = CS35L56_MAIN_RENDER_USER_VOLUME_MAX -
266 				   CS35L56_MAIN_RENDER_USER_VOLUME_MIN;
267 
268 	return 0;
269 }
270 
271 static int cs35l56_hda_vol_get(struct snd_kcontrol *kcontrol,
272 			       struct snd_ctl_elem_value *ucontrol)
273 {
274 	struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data;
275 	unsigned int raw_vol;
276 	int vol;
277 	int ret;
278 
279 	ret = regmap_read(cs35l56->base.regmap, CS35L56_MAIN_RENDER_USER_VOLUME, &raw_vol);
280 
281 	if (ret)
282 		return ret;
283 
284 	vol = (s16)(raw_vol & 0xFFFF);
285 	vol >>= CS35L56_MAIN_RENDER_USER_VOLUME_SHIFT;
286 
287 	if (vol & BIT(CS35L56_MAIN_RENDER_USER_VOLUME_SIGNBIT))
288 		vol |= ~((int)(BIT(CS35L56_MAIN_RENDER_USER_VOLUME_SIGNBIT) - 1));
289 
290 	ucontrol->value.integer.value[0] = vol - CS35L56_MAIN_RENDER_USER_VOLUME_MIN;
291 
292 	return 0;
293 }
294 
295 static int cs35l56_hda_vol_put(struct snd_kcontrol *kcontrol,
296 			       struct snd_ctl_elem_value *ucontrol)
297 {
298 	struct cs35l56_hda *cs35l56 = (struct cs35l56_hda *)kcontrol->private_data;
299 	long vol = ucontrol->value.integer.value[0];
300 	unsigned int raw_vol;
301 	bool changed;
302 	int ret;
303 
304 	if ((vol < 0) || (vol > (CS35L56_MAIN_RENDER_USER_VOLUME_MAX -
305 				 CS35L56_MAIN_RENDER_USER_VOLUME_MIN)))
306 		return -EINVAL;
307 
308 	raw_vol = (vol + CS35L56_MAIN_RENDER_USER_VOLUME_MIN) <<
309 		  CS35L56_MAIN_RENDER_USER_VOLUME_SHIFT;
310 
311 	ret = regmap_update_bits_check(cs35l56->base.regmap,
312 				       CS35L56_MAIN_RENDER_USER_VOLUME,
313 				       CS35L56_MAIN_RENDER_USER_VOLUME_MASK,
314 				       raw_vol, &changed);
315 	if (ret)
316 		return ret;
317 
318 	return changed;
319 }
320 
321 static void cs35l56_hda_create_controls(struct cs35l56_hda *cs35l56)
322 {
323 	struct snd_kcontrol_new ctl_template = {
324 		.iface = SNDRV_CTL_ELEM_IFACE_MIXER,
325 		.access = SNDRV_CTL_ELEM_ACCESS_READWRITE,
326 		.info = cs35l56_hda_posture_info,
327 		.get = cs35l56_hda_posture_get,
328 		.put = cs35l56_hda_posture_put,
329 	};
330 	char name[64];
331 	int i;
332 
333 	snprintf(name, sizeof(name), "%s Posture Number", cs35l56->amp_name);
334 	ctl_template.name = name;
335 	cs35l56->posture_ctl = snd_ctl_new1(&ctl_template, cs35l56);
336 	if (snd_ctl_add(cs35l56->codec->card, cs35l56->posture_ctl))
337 		dev_err(cs35l56->base.dev, "Failed to add KControl: %s\n", ctl_template.name);
338 
339 	/* Mixer controls */
340 	ctl_template.info = cs35l56_hda_mixer_info;
341 	ctl_template.get = cs35l56_hda_mixer_get;
342 	ctl_template.put = cs35l56_hda_mixer_put;
343 
344 	BUILD_BUG_ON(ARRAY_SIZE(cs35l56->mixer_ctl) != ARRAY_SIZE(cs35l56_hda_mixer_controls));
345 
346 	for (i = 0; i < ARRAY_SIZE(cs35l56_hda_mixer_controls); ++i) {
347 		snprintf(name, sizeof(name), "%s %s", cs35l56->amp_name,
348 			 cs35l56_hda_mixer_controls[i].name);
349 		ctl_template.private_value = cs35l56_hda_mixer_controls[i].reg;
350 		cs35l56->mixer_ctl[i] = snd_ctl_new1(&ctl_template, cs35l56);
351 		if (snd_ctl_add(cs35l56->codec->card, cs35l56->mixer_ctl[i])) {
352 			dev_err(cs35l56->base.dev, "Failed to add KControl: %s\n",
353 				ctl_template.name);
354 		}
355 	}
356 
357 	ctl_template.info = cs35l56_hda_vol_info;
358 	ctl_template.get = cs35l56_hda_vol_get;
359 	ctl_template.put = cs35l56_hda_vol_put;
360 	ctl_template.access = (SNDRV_CTL_ELEM_ACCESS_READWRITE | SNDRV_CTL_ELEM_ACCESS_TLV_READ);
361 	ctl_template.tlv.p = cs35l56_hda_vol_tlv;
362 	snprintf(name, sizeof(name), "%s Speaker Playback Volume", cs35l56->amp_name);
363 	ctl_template.name = name;
364 	cs35l56->volume_ctl = snd_ctl_new1(&ctl_template, cs35l56);
365 	if (snd_ctl_add(cs35l56->codec->card, cs35l56->volume_ctl))
366 		dev_err(cs35l56->base.dev, "Failed to add KControl: %s\n", ctl_template.name);
367 }
368 
369 static void cs35l56_hda_remove_controls(struct cs35l56_hda *cs35l56)
370 {
371 	int i;
372 
373 	for (i = ARRAY_SIZE(cs35l56->mixer_ctl) - 1; i >= 0; i--)
374 		snd_ctl_remove(cs35l56->codec->card, cs35l56->mixer_ctl[i]);
375 
376 	snd_ctl_remove(cs35l56->codec->card, cs35l56->posture_ctl);
377 	snd_ctl_remove(cs35l56->codec->card, cs35l56->volume_ctl);
378 }
379 
380 static const struct cs_dsp_client_ops cs35l56_hda_client_ops = {
381 	.control_remove = hda_cs_dsp_control_remove,
382 };
383 
384 static int cs35l56_hda_request_firmware_file(struct cs35l56_hda *cs35l56,
385 					     const struct firmware **firmware, char **filename,
386 					     const char *dir, const char *system_name,
387 					     const char *amp_name,
388 					     const char *filetype)
389 {
390 	char *s, c;
391 	int ret = 0;
392 
393 	if (system_name && amp_name)
394 		*filename = kasprintf(GFP_KERNEL, "%scs35l56%s-%02x-dsp1-misc-%s-%s.%s", dir,
395 				      cs35l56->base.secured ? "s" : "", cs35l56->base.rev,
396 				      system_name, amp_name, filetype);
397 	else if (system_name)
398 		*filename = kasprintf(GFP_KERNEL, "%scs35l56%s-%02x-dsp1-misc-%s.%s", dir,
399 				      cs35l56->base.secured ? "s" : "", cs35l56->base.rev,
400 				      system_name, filetype);
401 	else
402 		*filename = kasprintf(GFP_KERNEL, "%scs35l56%s-%02x-dsp1-misc.%s", dir,
403 				      cs35l56->base.secured ? "s" : "", cs35l56->base.rev,
404 				      filetype);
405 
406 	if (!*filename)
407 		return -ENOMEM;
408 
409 	/*
410 	 * Make sure that filename is lower-case and any non alpha-numeric
411 	 * characters except full stop and forward slash are replaced with
412 	 * hyphens.
413 	 */
414 	s = *filename;
415 	while (*s) {
416 		c = *s;
417 		if (isalnum(c))
418 			*s = tolower(c);
419 		else if (c != '.' && c != '/')
420 			*s = '-';
421 		s++;
422 	}
423 
424 	ret = firmware_request_nowarn(firmware, *filename, cs35l56->base.dev);
425 	if (ret) {
426 		dev_dbg(cs35l56->base.dev, "Failed to request '%s'\n", *filename);
427 		kfree(*filename);
428 		*filename = NULL;
429 		return ret;
430 	}
431 
432 	dev_dbg(cs35l56->base.dev, "Found '%s'\n", *filename);
433 
434 	return 0;
435 }
436 
437 static const char cirrus_dir[] = "cirrus/";
438 static void cs35l56_hda_request_firmware_files(struct cs35l56_hda *cs35l56,
439 					       const struct firmware **wmfw_firmware,
440 					       char **wmfw_filename,
441 					       const struct firmware **coeff_firmware,
442 					       char **coeff_filename)
443 {
444 	const char *system_name = cs35l56->system_name;
445 	const char *amp_name = cs35l56->amp_name;
446 	int ret;
447 
448 	if (system_name && amp_name) {
449 		if (!cs35l56_hda_request_firmware_file(cs35l56, wmfw_firmware, wmfw_filename,
450 						       cirrus_dir, system_name, amp_name, "wmfw")) {
451 			cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename,
452 							  cirrus_dir, system_name, amp_name, "bin");
453 			return;
454 		}
455 	}
456 
457 	if (system_name) {
458 		if (!cs35l56_hda_request_firmware_file(cs35l56, wmfw_firmware, wmfw_filename,
459 						       cirrus_dir, system_name, NULL, "wmfw")) {
460 			if (amp_name)
461 				cs35l56_hda_request_firmware_file(cs35l56,
462 								  coeff_firmware, coeff_filename,
463 								  cirrus_dir, system_name,
464 								  amp_name, "bin");
465 			if (!*coeff_firmware)
466 				cs35l56_hda_request_firmware_file(cs35l56,
467 								  coeff_firmware, coeff_filename,
468 								  cirrus_dir, system_name,
469 								  NULL, "bin");
470 			return;
471 		}
472 	}
473 
474 	ret = cs35l56_hda_request_firmware_file(cs35l56, wmfw_firmware, wmfw_filename,
475 						cirrus_dir, NULL, NULL, "wmfw");
476 	if (!ret) {
477 		cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename,
478 						  cirrus_dir, NULL, NULL, "bin");
479 		return;
480 	}
481 
482 	/* When a firmware file is not found must still search for the coeff files */
483 	if (system_name) {
484 		if (amp_name)
485 			cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename,
486 							  cirrus_dir, system_name, amp_name, "bin");
487 		if (!*coeff_firmware)
488 			cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename,
489 							  cirrus_dir, system_name, NULL, "bin");
490 	}
491 
492 	if (!*coeff_firmware)
493 		cs35l56_hda_request_firmware_file(cs35l56, coeff_firmware, coeff_filename,
494 						  cirrus_dir, NULL, NULL, "bin");
495 }
496 
497 static void cs35l56_hda_release_firmware_files(const struct firmware *wmfw_firmware,
498 					       char *wmfw_filename,
499 					       const struct firmware *coeff_firmware,
500 					       char *coeff_filename)
501 {
502 	if (wmfw_firmware)
503 		release_firmware(wmfw_firmware);
504 	kfree(wmfw_filename);
505 
506 	if (coeff_firmware)
507 		release_firmware(coeff_firmware);
508 	kfree(coeff_filename);
509 }
510 
511 static void cs35l56_hda_add_dsp_controls(struct cs35l56_hda *cs35l56)
512 {
513 	struct hda_cs_dsp_ctl_info info;
514 
515 	info.device_name = cs35l56->amp_name;
516 	info.fw_type = HDA_CS_DSP_FW_MISC;
517 	info.card = cs35l56->codec->card;
518 
519 	hda_cs_dsp_add_controls(&cs35l56->cs_dsp, &info);
520 }
521 
522 static int cs35l56_hda_fw_load(struct cs35l56_hda *cs35l56)
523 {
524 	const struct firmware *coeff_firmware = NULL;
525 	const struct firmware *wmfw_firmware = NULL;
526 	char *coeff_filename = NULL;
527 	char *wmfw_filename = NULL;
528 	int ret = 0;
529 
530 	cs35l56_hda_request_firmware_files(cs35l56, &wmfw_firmware, &wmfw_filename,
531 					   &coeff_firmware, &coeff_filename);
532 
533 	/* Nothing to do - no firmware files were found to download */
534 	if (!wmfw_filename && !coeff_filename)
535 		return 0;
536 
537 	mutex_lock(&cs35l56->base.irq_lock);
538 	pm_runtime_get_sync(cs35l56->base.dev);
539 
540 	/*
541 	 * When the device is running in secure mode the firmware files can
542 	 * only contain insecure tunings and therefore we do not need to
543 	 * shutdown the firmware to apply them and can use the lower cost
544 	 * reinit sequence instead.
545 	 */
546 	if (!cs35l56->base.secured) {
547 		ret = cs35l56_firmware_shutdown(&cs35l56->base);
548 		if (ret)
549 			goto err;
550 	}
551 
552 	ret = cs_dsp_power_up(&cs35l56->cs_dsp, wmfw_firmware, wmfw_filename,
553 			      coeff_firmware, coeff_filename, "misc");
554 	if (ret) {
555 		dev_dbg(cs35l56->base.dev, "%s: cs_dsp_power_up ret %d\n", __func__, ret);
556 		goto err;
557 	}
558 
559 	if (wmfw_filename)
560 		dev_dbg(cs35l56->base.dev, "Loaded WMFW Firmware: %s\n", wmfw_filename);
561 
562 	if (coeff_filename)
563 		dev_dbg(cs35l56->base.dev, "Loaded Coefficients: %s\n", coeff_filename);
564 
565 	ret = cs_dsp_run(&cs35l56->cs_dsp);
566 	if (ret) {
567 		dev_dbg(cs35l56->base.dev, "%s: cs_dsp_run ret %d\n", __func__, ret);
568 		goto err;
569 	}
570 
571 	if (cs35l56->base.secured) {
572 		ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_AUDIO_REINIT);
573 		if (ret)
574 			goto err;
575 	} else {
576 		/* Reset the device and wait for it to boot */
577 		cs35l56_system_reset(&cs35l56->base, false);
578 		ret = cs35l56_wait_for_firmware_boot(&cs35l56->base);
579 		if (ret)
580 			goto err;
581 	}
582 
583 	/* Disable auto-hibernate so that runtime_pm has control */
584 	ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE);
585 	if (ret)
586 		goto err;
587 
588 	regcache_mark_dirty(cs35l56->base.regmap);
589 	regcache_sync(cs35l56->base.regmap);
590 
591 	regmap_clear_bits(cs35l56->base.regmap, CS35L56_PROTECTION_STATUS,
592 			  CS35L56_FIRMWARE_MISSING);
593 	cs35l56->base.fw_patched = true;
594 err:
595 	pm_runtime_put(cs35l56->base.dev);
596 	mutex_unlock(&cs35l56->base.irq_lock);
597 
598 	cs35l56_hda_release_firmware_files(wmfw_firmware, wmfw_filename,
599 					   coeff_firmware, coeff_filename);
600 
601 	return ret;
602 }
603 
604 static int cs35l56_hda_bind(struct device *dev, struct device *master, void *master_data)
605 {
606 	struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
607 	struct hda_component *comps = master_data;
608 	int ret;
609 
610 	if (!comps || cs35l56->index < 0 || cs35l56->index >= HDA_MAX_COMPONENTS)
611 		return -EINVAL;
612 
613 	comps = &comps[cs35l56->index];
614 	if (comps->dev)
615 		return -EBUSY;
616 
617 	comps->dev = dev;
618 	cs35l56->codec = comps->codec;
619 	strscpy(comps->name, dev_name(dev), sizeof(comps->name));
620 	comps->playback_hook = cs35l56_hda_playback_hook;
621 
622 	ret = cs35l56_hda_fw_load(cs35l56);
623 	if (ret)
624 		return ret;
625 
626 	cs35l56_hda_create_controls(cs35l56);
627 	cs35l56_hda_add_dsp_controls(cs35l56);
628 
629 #if IS_ENABLED(CONFIG_SND_DEBUG)
630 	cs35l56->debugfs_root = debugfs_create_dir(dev_name(cs35l56->base.dev), sound_debugfs_root);
631 	cs_dsp_init_debugfs(&cs35l56->cs_dsp, cs35l56->debugfs_root);
632 #endif
633 
634 	dev_dbg(cs35l56->base.dev, "Bound\n");
635 
636 	return 0;
637 }
638 
639 static void cs35l56_hda_unbind(struct device *dev, struct device *master, void *master_data)
640 {
641 	struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
642 	struct hda_component *comps = master_data;
643 
644 	cs35l56_hda_remove_controls(cs35l56);
645 
646 #if IS_ENABLED(CONFIG_SND_DEBUG)
647 	cs_dsp_cleanup_debugfs(&cs35l56->cs_dsp);
648 	debugfs_remove_recursive(cs35l56->debugfs_root);
649 #endif
650 
651 	cs_dsp_remove(&cs35l56->cs_dsp);
652 
653 	if (comps[cs35l56->index].dev == dev)
654 		memset(&comps[cs35l56->index], 0, sizeof(*comps));
655 
656 	dev_dbg(cs35l56->base.dev, "Unbound\n");
657 }
658 
659 static const struct component_ops cs35l56_hda_comp_ops = {
660 	.bind = cs35l56_hda_bind,
661 	.unbind = cs35l56_hda_unbind,
662 };
663 
664 static int cs35l56_hda_system_suspend(struct device *dev)
665 {
666 	struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
667 
668 	if (cs35l56->playing)
669 		cs35l56_hda_pause(cs35l56);
670 
671 	cs35l56->suspended = true;
672 
673 	/*
674 	 * The interrupt line is normally shared, but after we start suspending
675 	 * we can't check if our device is the source of an interrupt, and can't
676 	 * clear it. Prevent this race by temporarily disabling the parent irq
677 	 * until we reach _no_irq.
678 	 */
679 	if (cs35l56->base.irq)
680 		disable_irq(cs35l56->base.irq);
681 
682 	return pm_runtime_force_suspend(dev);
683 }
684 
685 static int cs35l56_hda_system_suspend_late(struct device *dev)
686 {
687 	struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
688 
689 	/*
690 	 * RESET is usually shared by all amps so it must not be asserted until
691 	 * all driver instances have done their suspend() stage.
692 	 */
693 	if (cs35l56->base.reset_gpio) {
694 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
695 		cs35l56_wait_min_reset_pulse();
696 	}
697 
698 	return 0;
699 }
700 
701 static int cs35l56_hda_system_suspend_no_irq(struct device *dev)
702 {
703 	struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
704 
705 	/* Handlers are now disabled so the parent IRQ can safely be re-enabled. */
706 	if (cs35l56->base.irq)
707 		enable_irq(cs35l56->base.irq);
708 
709 	return 0;
710 }
711 
712 static int cs35l56_hda_system_resume_no_irq(struct device *dev)
713 {
714 	struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
715 
716 	/*
717 	 * WAKE interrupts unmask if the CS35L56 hibernates, which can cause
718 	 * spurious interrupts, and the interrupt line is normally shared.
719 	 * We can't check if our device is the source of an interrupt, and can't
720 	 * clear it, until it has fully resumed. Prevent this race by temporarily
721 	 * disabling the parent irq until we complete resume().
722 	 */
723 	if (cs35l56->base.irq)
724 		disable_irq(cs35l56->base.irq);
725 
726 	return 0;
727 }
728 
729 static int cs35l56_hda_system_resume_early(struct device *dev)
730 {
731 	struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
732 
733 	/* Ensure a spec-compliant RESET pulse. */
734 	if (cs35l56->base.reset_gpio) {
735 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
736 		cs35l56_wait_min_reset_pulse();
737 
738 		/* Release shared RESET before drivers start resume(). */
739 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
740 		cs35l56_wait_control_port_ready();
741 	}
742 
743 	return 0;
744 }
745 
746 static int cs35l56_hda_system_resume(struct device *dev)
747 {
748 	struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
749 	int ret;
750 
751 	/* Undo pm_runtime_force_suspend() before re-enabling the irq */
752 	ret = pm_runtime_force_resume(dev);
753 	if (cs35l56->base.irq)
754 		enable_irq(cs35l56->base.irq);
755 
756 	if (ret)
757 		return ret;
758 
759 	cs35l56->suspended = false;
760 
761 	ret = cs35l56_is_fw_reload_needed(&cs35l56->base);
762 	dev_dbg(cs35l56->base.dev, "fw_reload_needed: %d\n", ret);
763 	if (ret > 0) {
764 		ret = cs35l56_hda_fw_load(cs35l56);
765 		if (ret)
766 			return ret;
767 	}
768 
769 	if (cs35l56->playing)
770 		cs35l56_hda_play(cs35l56);
771 
772 	return 0;
773 }
774 
775 static int cs35l56_hda_read_acpi(struct cs35l56_hda *cs35l56, int id)
776 {
777 	u32 values[HDA_MAX_COMPONENTS];
778 	struct acpi_device *adev;
779 	const char *property, *sub;
780 	size_t nval;
781 	int i, ret;
782 
783 	/*
784 	 * ACPI_COMPANION isn't available when this driver was instantiated by
785 	 * the serial-multi-instantiate driver, so lookup the node by HID
786 	 */
787 	if (!ACPI_COMPANION(cs35l56->base.dev)) {
788 		adev = acpi_dev_get_first_match_dev("CSC3556", NULL, -1);
789 		if (!adev) {
790 			dev_err(cs35l56->base.dev, "Failed to find an ACPI device for %s\n",
791 				dev_name(cs35l56->base.dev));
792 			return -ENODEV;
793 		}
794 		ACPI_COMPANION_SET(cs35l56->base.dev, adev);
795 	}
796 
797 	property = "cirrus,dev-index";
798 	ret = device_property_count_u32(cs35l56->base.dev, property);
799 	if (ret <= 0)
800 		goto err;
801 
802 	if (ret > ARRAY_SIZE(values)) {
803 		ret = -EINVAL;
804 		goto err;
805 	}
806 	nval = ret;
807 
808 	ret = device_property_read_u32_array(cs35l56->base.dev, property, values, nval);
809 	if (ret)
810 		goto err;
811 
812 	cs35l56->index = -1;
813 	for (i = 0; i < nval; i++) {
814 		if (values[i] == id) {
815 			cs35l56->index = i;
816 			break;
817 		}
818 	}
819 	if (cs35l56->index == -1) {
820 		dev_err(cs35l56->base.dev, "No index found in %s\n", property);
821 		ret = -ENODEV;
822 		goto err;
823 	}
824 
825 	sub = acpi_get_subsystem_id(ACPI_HANDLE(cs35l56->base.dev));
826 
827 	if (IS_ERR(sub)) {
828 		/* If no ACPI SUB, return 0 and fallback to legacy firmware path, otherwise fail */
829 		if (PTR_ERR(sub) == -ENODATA)
830 			return 0;
831 		else
832 			return PTR_ERR(sub);
833 	}
834 
835 	cs35l56->system_name = sub;
836 
837 	cs35l56->base.reset_gpio = devm_gpiod_get_index_optional(cs35l56->base.dev,
838 								 "reset",
839 								 cs35l56->index,
840 								 GPIOD_OUT_LOW);
841 	if (IS_ERR(cs35l56->base.reset_gpio)) {
842 		ret = PTR_ERR(cs35l56->base.reset_gpio);
843 
844 		/*
845 		 * If RESET is shared the first amp to probe will grab the reset
846 		 * line and reset all the amps
847 		 */
848 		if (ret != -EBUSY)
849 			return dev_err_probe(cs35l56->base.dev, ret, "Failed to get reset GPIO\n");
850 
851 		dev_info(cs35l56->base.dev, "Reset GPIO busy, assume shared reset\n");
852 		cs35l56->base.reset_gpio = NULL;
853 	}
854 
855 	return 0;
856 
857 err:
858 	dev_err(cs35l56->base.dev, "Failed property %s: %d\n", property, ret);
859 
860 	return ret;
861 }
862 
863 int cs35l56_hda_common_probe(struct cs35l56_hda *cs35l56, int id)
864 {
865 	int ret;
866 
867 	mutex_init(&cs35l56->base.irq_lock);
868 	dev_set_drvdata(cs35l56->base.dev, cs35l56);
869 
870 	ret = cs35l56_hda_read_acpi(cs35l56, id);
871 	if (ret) {
872 		dev_err_probe(cs35l56->base.dev, ret, "Platform not supported\n");
873 		goto err;
874 	}
875 
876 	cs35l56->amp_name = devm_kasprintf(cs35l56->base.dev, GFP_KERNEL, "AMP%d",
877 					   cs35l56->index + 1);
878 	if (!cs35l56->amp_name) {
879 		ret = -ENOMEM;
880 		goto err;
881 	}
882 
883 	cs35l56_init_cs_dsp(&cs35l56->base, &cs35l56->cs_dsp);
884 	cs35l56->cs_dsp.client_ops = &cs35l56_hda_client_ops;
885 
886 	if (cs35l56->base.reset_gpio) {
887 		dev_dbg(cs35l56->base.dev, "Hard reset\n");
888 
889 		/*
890 		 * The GPIOD_OUT_LOW to *_gpiod_get_*() will be ignored if the
891 		 * ACPI defines a different default state. So explicitly set low.
892 		 */
893 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
894 		cs35l56_wait_min_reset_pulse();
895 		gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 1);
896 	}
897 
898 	ret = cs35l56_hw_init(&cs35l56->base);
899 	if (ret < 0)
900 		goto err;
901 
902 	/* Reset the device and wait for it to boot */
903 	cs35l56_system_reset(&cs35l56->base, false);
904 	ret = cs35l56_wait_for_firmware_boot(&cs35l56->base);
905 	if (ret)
906 		goto err;
907 
908 	ret = cs35l56_set_patch(&cs35l56->base);
909 	if (ret)
910 		return ret;
911 
912 	regcache_mark_dirty(cs35l56->base.regmap);
913 	regcache_sync(cs35l56->base.regmap);
914 
915 	/* Disable auto-hibernate so that runtime_pm has control */
916 	ret = cs35l56_mbox_send(&cs35l56->base, CS35L56_MBOX_CMD_PREVENT_AUTO_HIBERNATE);
917 	if (ret)
918 		goto err;
919 
920 	ret = cs_dsp_halo_init(&cs35l56->cs_dsp);
921 	if (ret) {
922 		dev_err_probe(cs35l56->base.dev, ret, "cs_dsp_halo_init failed\n");
923 		goto err;
924 	}
925 
926 	dev_dbg(cs35l56->base.dev, "DSP system name: '%s', amp name: '%s'\n",
927 		cs35l56->system_name, cs35l56->amp_name);
928 
929 	regmap_multi_reg_write(cs35l56->base.regmap, cs35l56_hda_dai_config,
930 			       ARRAY_SIZE(cs35l56_hda_dai_config));
931 
932 	/*
933 	 * By default only enable one ASP1TXn, where n=amplifier index,
934 	 * This prevents multiple amps trying to drive the same slot.
935 	 */
936 	cs35l56->asp_tx_mask = BIT(cs35l56->index);
937 
938 	pm_runtime_set_autosuspend_delay(cs35l56->base.dev, 3000);
939 	pm_runtime_use_autosuspend(cs35l56->base.dev);
940 	pm_runtime_set_active(cs35l56->base.dev);
941 	pm_runtime_mark_last_busy(cs35l56->base.dev);
942 	pm_runtime_enable(cs35l56->base.dev);
943 
944 	ret = component_add(cs35l56->base.dev, &cs35l56_hda_comp_ops);
945 	if (ret) {
946 		dev_err(cs35l56->base.dev, "Register component failed: %d\n", ret);
947 		goto pm_err;
948 	}
949 
950 	cs35l56->base.init_done = true;
951 
952 	return 0;
953 
954 pm_err:
955 	pm_runtime_disable(cs35l56->base.dev);
956 err:
957 	gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
958 
959 	return ret;
960 }
961 EXPORT_SYMBOL_NS_GPL(cs35l56_hda_common_probe, SND_HDA_SCODEC_CS35L56);
962 
963 void cs35l56_hda_remove(struct device *dev)
964 {
965 	struct cs35l56_hda *cs35l56 = dev_get_drvdata(dev);
966 
967 	pm_runtime_get_sync(cs35l56->base.dev);
968 	pm_runtime_disable(cs35l56->base.dev);
969 
970 	component_del(cs35l56->base.dev, &cs35l56_hda_comp_ops);
971 
972 	kfree(cs35l56->system_name);
973 	pm_runtime_put_noidle(cs35l56->base.dev);
974 
975 	gpiod_set_value_cansleep(cs35l56->base.reset_gpio, 0);
976 }
977 EXPORT_SYMBOL_NS_GPL(cs35l56_hda_remove, SND_HDA_SCODEC_CS35L56);
978 
979 const struct dev_pm_ops cs35l56_hda_pm_ops = {
980 	SET_RUNTIME_PM_OPS(cs35l56_hda_runtime_suspend, cs35l56_hda_runtime_resume, NULL)
981 	SYSTEM_SLEEP_PM_OPS(cs35l56_hda_system_suspend, cs35l56_hda_system_resume)
982 	LATE_SYSTEM_SLEEP_PM_OPS(cs35l56_hda_system_suspend_late,
983 				 cs35l56_hda_system_resume_early)
984 	NOIRQ_SYSTEM_SLEEP_PM_OPS(cs35l56_hda_system_suspend_no_irq,
985 				  cs35l56_hda_system_resume_no_irq)
986 };
987 EXPORT_SYMBOL_NS_GPL(cs35l56_hda_pm_ops, SND_HDA_SCODEC_CS35L56);
988 
989 MODULE_DESCRIPTION("CS35L56 HDA Driver");
990 MODULE_IMPORT_NS(SND_HDA_CS_DSP_CONTROLS);
991 MODULE_IMPORT_NS(SND_SOC_CS35L56_SHARED);
992 MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
993 MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>");
994 MODULE_LICENSE("GPL");
995 MODULE_IMPORT_NS(FW_CS_DSP);
996