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