xref: /linux/sound/hda/codecs/side-codecs/tas2781_hda_spi.c (revision a032fe30cf09b6723ab61a05aee057311b00f9e1)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // TAS2781 HDA SPI driver
4 //
5 // Copyright 2024 - 2025 Texas Instruments, Inc.
6 //
7 // Author: Baojun Xu <baojun.xu@ti.com>
8 
9 #include <linux/acpi.h>
10 #include <linux/array_size.h>
11 #include <linux/bits.h>
12 #include <linux/cleanup.h>
13 #include <linux/crc8.h>
14 #include <linux/crc32.h>
15 #include <linux/efi.h>
16 #include <linux/firmware.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/property.h>
22 #include <linux/regmap.h>
23 #include <linux/spi/spi.h>
24 #include <linux/time.h>
25 #include <linux/types.h>
26 #include <linux/units.h>
27 
28 #include <sound/hda_codec.h>
29 #include <sound/soc.h>
30 #include <sound/tas2781.h>
31 #include <sound/tlv.h>
32 #include <sound/tas2781-tlv.h>
33 
34 #include "hda_local.h"
35 #include "hda_auto_parser.h"
36 #include "hda_component.h"
37 #include "hda_jack.h"
38 #include "../generic.h"
39 #include "tas2781_hda.h"
40 
41 #define TASDEVICE_RANGE_MAX_SIZE	(256 * 128)
42 #define TASDEVICE_WIN_LEN		128
43 #define TAS2781_SPI_MAX_FREQ		(4 * HZ_PER_MHZ)
44 /* Flag of calibration registers address. */
45 #define TASDEVICE_CALIBRATION_REG_ADDRESS	BIT(7)
46 #define TASDEV_UEFI_CALI_REG_ADDR_FLG	BIT(7)
47 
48 /* System Reset Check Register */
49 #define TAS2781_REG_CLK_CONFIG		TASDEVICE_REG(0x0, 0x0, 0x5c)
50 #define TAS2781_REG_CLK_CONFIG_RESET	0x19
51 
52 struct tas2781_hda_spi_priv {
53 	struct snd_kcontrol *snd_ctls[3];
54 };
55 
56 static const struct regmap_range_cfg tasdevice_ranges[] = {
57 	{
58 		.range_min = 0,
59 		.range_max = TASDEVICE_RANGE_MAX_SIZE,
60 		.selector_reg = TASDEVICE_PAGE_SELECT,
61 		.selector_mask = GENMASK(7, 0),
62 		.selector_shift = 0,
63 		.window_start = 0,
64 		.window_len = TASDEVICE_WIN_LEN,
65 	},
66 };
67 
68 static const struct regmap_config tasdevice_regmap = {
69 	.reg_bits = 8,
70 	.val_bits = 8,
71 	.zero_flag_mask = true,
72 	.read_flag_mask = 0x01,
73 	.reg_shift = -1,
74 	.cache_type = REGCACHE_NONE,
75 	.ranges = tasdevice_ranges,
76 	.num_ranges = ARRAY_SIZE(tasdevice_ranges),
77 	.max_register = TASDEVICE_RANGE_MAX_SIZE,
78 };
79 
80 static int tasdevice_spi_dev_read(struct tasdevice_priv *tas_priv,
81 	unsigned short chn, unsigned int reg, unsigned int *val)
82 {
83 	int ret;
84 
85 	/*
86 	 * In our TAS2781 SPI mode, if read from other book (not book 0),
87 	 * or read from page number larger than 1 in book 0, one more byte
88 	 * read is needed, and first byte is a dummy byte, need to be ignored.
89 	 */
90 	if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) {
91 		unsigned char data[2];
92 
93 		ret = tasdevice_dev_bulk_read(tas_priv, chn, reg,
94 			data, sizeof(data));
95 		*val = data[1];
96 	} else {
97 		ret = tasdevice_dev_read(tas_priv, chn, reg, val);
98 	}
99 	if (ret < 0)
100 		dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
101 
102 	return ret;
103 }
104 
105 static int tasdevice_spi_dev_bulk_read(struct tasdevice_priv *tas_priv,
106 	unsigned short chn, unsigned int reg, unsigned char *data,
107 	unsigned int len)
108 {
109 	int ret;
110 
111 	/*
112 	 * In our TAS2781 SPI mode, if read from other book (not book 0),
113 	 * or read from page number larger than 1 in book 0, one more byte
114 	 * read is needed, and first byte is a dummy byte, need to be ignored.
115 	 */
116 	if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) {
117 		unsigned char buf[TASDEVICE_WIN_LEN + 1];
118 
119 		ret = tasdevice_dev_bulk_read(tas_priv, chn, reg,
120 			buf, len + 1);
121 		memcpy(data, buf + 1, len);
122 	} else {
123 		ret = tasdevice_dev_bulk_read(tas_priv, chn, reg, data, len);
124 	}
125 	if (ret < 0)
126 		dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
127 
128 	return ret;
129 }
130 
131 static int tasdevice_spi_dev_update_bits(struct tasdevice_priv *tas_priv,
132 	unsigned short chn, unsigned int reg, unsigned int mask,
133 	unsigned int value)
134 {
135 	int ret, val;
136 
137 	/*
138 	 * In our TAS2781 SPI mode, read/write was masked in last bit of
139 	 * address, it cause regmap_update_bits() not work as expected.
140 	 */
141 	ret = tasdevice_dev_read(tas_priv, chn, reg, &val);
142 	if (ret < 0) {
143 		dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
144 		return ret;
145 	}
146 
147 	ret = tasdevice_dev_write(tas_priv, chn, TASDEVICE_PAGE_REG(reg),
148 		(val & ~mask) | (mask & value));
149 	if (ret < 0)
150 		dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
151 
152 	return ret;
153 }
154 
155 static int tasdevice_spi_change_chn_book(struct tasdevice_priv *p,
156 	unsigned short chn, int book)
157 {
158 	int ret = 0;
159 
160 	if (chn == p->index) {
161 		struct tasdevice *tasdev = &p->tasdevice[chn];
162 		struct regmap *map = p->regmap;
163 
164 		if (tasdev->cur_book != book) {
165 			ret = regmap_write(map, TASDEVICE_BOOKCTL_REG, book);
166 			if (ret < 0)
167 				dev_err(p->dev, "%s, E=%d\n", __func__, ret);
168 			else
169 				tasdev->cur_book = book;
170 		}
171 	} else {
172 		ret = -EXDEV;
173 		dev_dbg(p->dev, "Not error, %s ignore channel(%d)\n",
174 			__func__, chn);
175 	}
176 
177 	return ret;
178 }
179 
180 static void tas2781_spi_reset(struct tasdevice_priv *tas_dev)
181 {
182 	int ret;
183 
184 	if (tas_dev->reset) {
185 		gpiod_set_value_cansleep(tas_dev->reset, 0);
186 		fsleep(800);
187 		gpiod_set_value_cansleep(tas_dev->reset, 1);
188 	} else {
189 		ret = tasdevice_dev_write(tas_dev, tas_dev->index,
190 			TASDEVICE_REG_SWRESET, TASDEVICE_REG_SWRESET_RESET);
191 		if (ret < 0) {
192 			dev_err(tas_dev->dev, "dev sw-reset fail, %d\n", ret);
193 			return;
194 		}
195 		fsleep(1000);
196 	}
197 }
198 
199 static int tascodec_spi_init(struct tasdevice_priv *tas_priv,
200 	void *codec, struct module *module,
201 	void (*cont)(const struct firmware *fw, void *context))
202 {
203 	int ret;
204 
205 	/*
206 	 * Codec Lock Hold to ensure that codec_probe and firmware parsing and
207 	 * loading do not simultaneously execute.
208 	 */
209 	guard(mutex)(&tas_priv->codec_lock);
210 
211 	scnprintf(tas_priv->rca_binaryname,
212 		sizeof(tas_priv->rca_binaryname), "%sRCA%d.bin",
213 		tas_priv->dev_name, tas_priv->ndev);
214 	crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL);
215 	tas_priv->codec = codec;
216 	ret = request_firmware_nowait(module, FW_ACTION_UEVENT,
217 		tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv,
218 		cont);
219 	if (ret)
220 		dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n",
221 			ret);
222 
223 	return ret;
224 }
225 
226 static void tasdevice_spi_init(struct tasdevice_priv *tas_priv)
227 {
228 	tas_priv->tasdevice[tas_priv->index].cur_book = -1;
229 	tas_priv->tasdevice[tas_priv->index].cur_conf = -1;
230 	tas_priv->tasdevice[tas_priv->index].cur_prog = -1;
231 
232 	tas_priv->isspi = true;
233 
234 	tas_priv->update_bits = tasdevice_spi_dev_update_bits;
235 	tas_priv->change_chn_book = tasdevice_spi_change_chn_book;
236 	tas_priv->dev_read = tasdevice_spi_dev_read;
237 	tas_priv->dev_bulk_read = tasdevice_spi_dev_bulk_read;
238 
239 	mutex_init(&tas_priv->codec_lock);
240 }
241 
242 static int tasdevice_spi_amp_putvol(struct tasdevice_priv *tas_priv,
243 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
244 {
245 	unsigned int invert = mc->invert;
246 	unsigned char mask;
247 	int max = mc->max;
248 	int val, ret;
249 
250 	mask = rounddown_pow_of_two(max);
251 	mask <<= mc->shift;
252 	val =  clamp(invert ? max - ucontrol->value.integer.value[0] :
253 		ucontrol->value.integer.value[0], 0, max);
254 
255 	ret = tasdevice_spi_dev_update_bits(tas_priv, tas_priv->index,
256 		mc->reg, mask, (unsigned int)(val << mc->shift));
257 	if (ret)
258 		dev_err(tas_priv->dev, "set AMP vol error in dev %d\n",
259 			tas_priv->index);
260 
261 	return ret;
262 }
263 
264 static int tasdevice_spi_amp_getvol(struct tasdevice_priv *tas_priv,
265 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
266 {
267 	unsigned int invert = mc->invert;
268 	unsigned char mask = 0;
269 	int max = mc->max;
270 	int ret, val;
271 
272 	ret = tasdevice_spi_dev_read(tas_priv, tas_priv->index, mc->reg, &val);
273 	if (ret) {
274 		dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__);
275 		return ret;
276 	}
277 
278 	mask = rounddown_pow_of_two(max);
279 	mask <<= mc->shift;
280 	val = (val & mask) >> mc->shift;
281 	val = clamp(invert ? max - val : val, 0, max);
282 	ucontrol->value.integer.value[0] = val;
283 
284 	return ret;
285 }
286 
287 static int tasdevice_spi_digital_putvol(struct tasdevice_priv *p,
288 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
289 {
290 	unsigned int invert = mc->invert;
291 	int max = mc->max;
292 	int val, ret;
293 
294 	val = clamp(invert ? max - ucontrol->value.integer.value[0] :
295 		ucontrol->value.integer.value[0], 0, max);
296 	ret = tasdevice_dev_write(p, p->index, mc->reg, (unsigned int)val);
297 	if (ret)
298 		dev_err(p->dev, "set digital vol err in dev %d\n", p->index);
299 
300 	return ret;
301 }
302 
303 static int tasdevice_spi_digital_getvol(struct tasdevice_priv *p,
304 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
305 {
306 	unsigned int invert = mc->invert;
307 	int max = mc->max;
308 	int ret, val;
309 
310 	ret = tasdevice_spi_dev_read(p, p->index, mc->reg, &val);
311 	if (ret) {
312 		dev_err(p->dev, "%s, get digital vol err\n", __func__);
313 		return ret;
314 	}
315 
316 	val = clamp(invert ? max - val : val, 0, max);
317 	ucontrol->value.integer.value[0] = val;
318 
319 	return ret;
320 }
321 
322 static int tas2781_read_acpi(struct tas2781_hda *tas_hda,
323 	const char *hid, int id)
324 {
325 	struct tasdevice_priv *p = tas_hda->priv;
326 	struct acpi_device *adev;
327 	struct device *physdev;
328 	u32 values[HDA_MAX_COMPONENTS];
329 	const char *property;
330 	size_t nval;
331 	int ret, i;
332 
333 	adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
334 	if (!adev) {
335 		dev_err(p->dev, "Failed to find ACPI device: %s\n", hid);
336 		return -ENODEV;
337 	}
338 
339 	strscpy(p->dev_name, hid, sizeof(p->dev_name));
340 	physdev = get_device(acpi_get_first_physical_node(adev));
341 	acpi_dev_put(adev);
342 
343 	property = "ti,dev-index";
344 	ret = device_property_count_u32(physdev, property);
345 	if (ret <= 0 || ret > ARRAY_SIZE(values)) {
346 		ret = -EINVAL;
347 		goto err;
348 	}
349 	p->ndev = nval = ret;
350 
351 	ret = device_property_read_u32_array(physdev, property, values, nval);
352 	if (ret)
353 		goto err;
354 
355 	p->index = U8_MAX;
356 	for (i = 0; i < nval; i++) {
357 		if (values[i] == id) {
358 			p->index = i;
359 			break;
360 		}
361 	}
362 	if (p->index == U8_MAX) {
363 		dev_dbg(p->dev, "No index found in %s\n", property);
364 		ret = -ENODEV;
365 		goto err;
366 	}
367 
368 	if (p->index == 0) {
369 		/* All of amps share same RESET pin. */
370 		p->reset = devm_gpiod_get_index_optional(physdev, "reset",
371 			p->index, GPIOD_OUT_LOW);
372 		if (IS_ERR(p->reset)) {
373 			ret = PTR_ERR(p->reset);
374 			dev_err_probe(p->dev, ret, "Failed on reset GPIO\n");
375 			goto err;
376 		}
377 	}
378 	put_device(physdev);
379 
380 	return 0;
381 err:
382 	dev_err(p->dev, "read acpi error, ret: %d\n", ret);
383 	put_device(physdev);
384 	acpi_dev_put(adev);
385 
386 	return ret;
387 }
388 
389 static void tas2781_hda_playback_hook(struct device *dev, int action)
390 {
391 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
392 	struct tasdevice_priv *tas_priv = tas_hda->priv;
393 
394 	if (action == HDA_GEN_PCM_ACT_OPEN) {
395 		pm_runtime_get_sync(dev);
396 		guard(mutex)(&tas_priv->codec_lock);
397 		if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK)
398 			tasdevice_tuning_switch(tas_hda->priv, 0);
399 	} else if (action == HDA_GEN_PCM_ACT_CLOSE) {
400 		guard(mutex)(&tas_priv->codec_lock);
401 		if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK)
402 			tasdevice_tuning_switch(tas_priv, 1);
403 		pm_runtime_put_autosuspend(dev);
404 	}
405 }
406 
407 /*
408  * tas2781_digital_getvol - get the volum control
409  * @kcontrol: control pointer
410  * @ucontrol: User data
411  *
412  * Customer Kcontrol for tas2781 is primarily for regmap booking, paging
413  * depends on internal regmap mechanism.
414  * tas2781 contains book and page two-level register map, especially
415  * book switching will set the register BXXP00R7F, after switching to the
416  * correct book, then leverage the mechanism for paging to access the
417  * register.
418  *
419  * Return 0 if succeeded.
420  */
421 static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
422 				  struct snd_ctl_elem_value *ucontrol)
423 {
424 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
425 	struct soc_mixer_control *mc =
426 		(struct soc_mixer_control *)kcontrol->private_value;
427 
428 	guard(mutex)(&tas_priv->codec_lock);
429 	return tasdevice_spi_digital_getvol(tas_priv, ucontrol, mc);
430 }
431 
432 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
433 			      struct snd_ctl_elem_value *ucontrol)
434 {
435 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
436 	struct soc_mixer_control *mc =
437 		(struct soc_mixer_control *)kcontrol->private_value;
438 
439 	guard(mutex)(&tas_priv->codec_lock);
440 	return tasdevice_spi_amp_getvol(tas_priv, ucontrol, mc);
441 }
442 
443 static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
444 				  struct snd_ctl_elem_value *ucontrol)
445 {
446 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
447 	struct soc_mixer_control *mc =
448 		(struct soc_mixer_control *)kcontrol->private_value;
449 
450 	guard(mutex)(&tas_priv->codec_lock);
451 	return tasdevice_spi_digital_putvol(tas_priv, ucontrol, mc);
452 }
453 
454 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
455 			      struct snd_ctl_elem_value *ucontrol)
456 {
457 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
458 	struct soc_mixer_control *mc =
459 		(struct soc_mixer_control *)kcontrol->private_value;
460 
461 	guard(mutex)(&tas_priv->codec_lock);
462 	return tasdevice_spi_amp_putvol(tas_priv, ucontrol, mc);
463 }
464 
465 static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,
466 				    struct snd_ctl_elem_value *ucontrol)
467 {
468 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
469 
470 	ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
471 	dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
472 		str_on_off(tas_priv->force_fwload_status));
473 
474 	return 0;
475 }
476 
477 static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
478 				    struct snd_ctl_elem_value *ucontrol)
479 {
480 	struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
481 	bool change, val = (bool)ucontrol->value.integer.value[0];
482 
483 	if (tas_priv->force_fwload_status == val) {
484 		change = false;
485 	} else {
486 		change = true;
487 		tas_priv->force_fwload_status = val;
488 	}
489 	dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
490 		str_on_off(tas_priv->force_fwload_status));
491 
492 	return change;
493 }
494 
495 static struct snd_kcontrol_new tas2781_snd_ctls[] = {
496 	ACARD_SINGLE_RANGE_EXT_TLV(NULL, TAS2781_AMP_LEVEL, 1, 0, 20, 0,
497 		tas2781_amp_getvol, tas2781_amp_putvol, amp_vol_tlv),
498 	ACARD_SINGLE_RANGE_EXT_TLV(NULL, TAS2781_DVC_LVL, 0, 0, 200, 1,
499 		tas2781_digital_getvol, tas2781_digital_putvol, dvc_tlv),
500 	ACARD_SINGLE_BOOL_EXT(NULL, 0, tas2781_force_fwload_get,
501 		tas2781_force_fwload_put),
502 };
503 
504 static struct snd_kcontrol_new tas2781_prof_ctl = {
505 	.iface = SNDRV_CTL_ELEM_IFACE_CARD,
506 	.info = tasdevice_info_profile,
507 	.get = tasdevice_get_profile_id,
508 	.put = tasdevice_set_profile_id,
509 };
510 
511 static struct snd_kcontrol_new tas2781_dsp_ctls[] = {
512 	/* Speaker Program */
513 	{
514 		.iface = SNDRV_CTL_ELEM_IFACE_CARD,
515 		.info = tasdevice_info_programs,
516 		.get = tasdevice_program_get,
517 		.put = tasdevice_program_put,
518 	},
519 	/* Speaker Config */
520 	{
521 		.iface = SNDRV_CTL_ELEM_IFACE_CARD,
522 		.info = tasdevice_info_config,
523 		.get = tasdevice_config_get,
524 		.put = tasdevice_config_put,
525 	},
526 };
527 
528 static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
529 {
530 	struct hda_codec *codec = tas_hda->priv->codec;
531 	struct tas2781_hda_spi_priv *h_priv = tas_hda->hda_priv;
532 
533 	snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
534 
535 	snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
536 
537 	for (int i = ARRAY_SIZE(h_priv->snd_ctls) - 1; i >= 0; i--)
538 		snd_ctl_remove(codec->card, h_priv->snd_ctls[i]);
539 
540 	snd_ctl_remove(codec->card, tas_hda->prof_ctl);
541 }
542 
543 static int tas2781_hda_spi_prf_ctl(struct tas2781_hda *h)
544 {
545 	struct tasdevice_priv *p = h->priv;
546 	struct hda_codec *c = p->codec;
547 	char name[64];
548 	int rc;
549 
550 	snprintf(name, sizeof(name), "Speaker-%d Profile Id", p->index);
551 	tas2781_prof_ctl.name = name;
552 	h->prof_ctl = snd_ctl_new1(&tas2781_prof_ctl, p);
553 	rc = snd_ctl_add(c->card, h->prof_ctl);
554 	if (rc)
555 		dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
556 			tas2781_prof_ctl.name, rc);
557 	return rc;
558 }
559 
560 static int tas2781_hda_spi_snd_ctls(struct tas2781_hda *h)
561 {
562 	struct tas2781_hda_spi_priv *h_priv = h->hda_priv;
563 	struct tasdevice_priv *p = h->priv;
564 	struct hda_codec *c = p->codec;
565 	char name[64];
566 	int i = 0;
567 	int rc;
568 
569 	snprintf(name, sizeof(name), "Speaker-%d Analog Volume", p->index);
570 	tas2781_snd_ctls[i].name = name;
571 	h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);
572 	rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);
573 	if (rc) {
574 		dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
575 			tas2781_snd_ctls[i].name, rc);
576 		return rc;
577 	}
578 	i++;
579 	snprintf(name, sizeof(name), "Speaker-%d Digital Volume", p->index);
580 	tas2781_snd_ctls[i].name = name;
581 	h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);
582 	rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);
583 	if (rc) {
584 		dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
585 			tas2781_snd_ctls[i].name, rc);
586 		return rc;
587 	}
588 	i++;
589 	snprintf(name, sizeof(name), "Froce Speaker-%d FW Load", p->index);
590 	tas2781_snd_ctls[i].name = name;
591 	h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);
592 	rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);
593 	if (rc) {
594 		dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
595 			tas2781_snd_ctls[i].name, rc);
596 	}
597 	return rc;
598 }
599 
600 static int tas2781_hda_spi_dsp_ctls(struct tas2781_hda *h)
601 {
602 	struct tasdevice_priv *p = h->priv;
603 	struct hda_codec *c = p->codec;
604 	char name[64];
605 	int i = 0;
606 	int rc;
607 
608 	snprintf(name, sizeof(name), "Speaker-%d Program Id", p->index);
609 	tas2781_dsp_ctls[i].name = name;
610 	h->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_ctls[i], p);
611 	rc = snd_ctl_add(c->card, h->dsp_prog_ctl);
612 	if (rc) {
613 		dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
614 			tas2781_dsp_ctls[i].name, rc);
615 		return rc;
616 	}
617 	i++;
618 	snprintf(name, sizeof(name), "Speaker-%d Config Id", p->index);
619 	tas2781_dsp_ctls[i].name = name;
620 	h->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_ctls[i], p);
621 	rc = snd_ctl_add(c->card, h->dsp_conf_ctl);
622 	if (rc) {
623 		dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
624 			tas2781_dsp_ctls[i].name, rc);
625 	}
626 
627 	return rc;
628 }
629 
630 static void tasdev_fw_ready(const struct firmware *fmw, void *context)
631 {
632 	struct tasdevice_priv *tas_priv = context;
633 	struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
634 	struct hda_codec *codec = tas_priv->codec;
635 	int ret, val;
636 
637 	pm_runtime_get_sync(tas_priv->dev);
638 	guard(mutex)(&tas_priv->codec_lock);
639 
640 	ret = tasdevice_rca_parser(tas_priv, fmw);
641 	if (ret)
642 		goto out;
643 
644 	/* Add control one time only. */
645 	ret = tas2781_hda_spi_prf_ctl(tas_hda);
646 	if (ret)
647 		goto out;
648 
649 	ret = tas2781_hda_spi_snd_ctls(tas_hda);
650 	if (ret)
651 		goto out;
652 
653 	tasdevice_dsp_remove(tas_priv);
654 
655 	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
656 	scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%04X-%01d.bin",
657 		lower_16_bits(codec->core.subsystem_id), tas_priv->index);
658 	ret = tasdevice_dsp_parser(tas_priv);
659 	if (ret) {
660 		dev_err(tas_priv->dev, "dspfw load %s error\n",
661 			tas_priv->coef_binaryname);
662 		tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
663 		goto out;
664 	}
665 
666 	ret = tas2781_hda_spi_dsp_ctls(tas_hda);
667 	if (ret)
668 		goto out;
669 	/* Perform AMP reset before firmware download. */
670 	tas2781_spi_reset(tas_priv);
671 	tas_priv->rcabin.profile_cfg_id = 0;
672 
673 	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
674 	ret = tasdevice_spi_dev_read(tas_priv, tas_priv->index,
675 		TAS2781_REG_CLK_CONFIG, &val);
676 	if (ret < 0)
677 		goto out;
678 
679 	if (val == TAS2781_REG_CLK_CONFIG_RESET) {
680 		ret = tasdevice_prmg_load(tas_priv, 0);
681 		if (ret < 0) {
682 			dev_err(tas_priv->dev, "FW download failed = %d\n",
683 				ret);
684 			goto out;
685 		}
686 		tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
687 	}
688 	if (tas_priv->fmw->nr_programs > 0)
689 		tas_priv->tasdevice[tas_priv->index].cur_prog = 0;
690 	if (tas_priv->fmw->nr_configurations > 0)
691 		tas_priv->tasdevice[tas_priv->index].cur_conf = 0;
692 
693 	/*
694 	 * If calibrated data occurs error, dsp will still works with default
695 	 * calibrated data inside algo.
696 	 */
697 	tas2781_save_calibration(tas_hda);
698 out:
699 	release_firmware(fmw);
700 	pm_runtime_put_autosuspend(tas_hda->priv->dev);
701 }
702 
703 static int tas2781_hda_bind(struct device *dev, struct device *master,
704 	void *master_data)
705 {
706 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
707 	struct hda_component_parent *parent = master_data;
708 	struct hda_component *comp;
709 	struct hda_codec *codec;
710 	int ret;
711 
712 	comp = hda_component_from_index(parent, tas_hda->priv->index);
713 	if (!comp)
714 		return -EINVAL;
715 
716 	if (comp->dev)
717 		return -EBUSY;
718 
719 	codec = parent->codec;
720 
721 	pm_runtime_get_sync(dev);
722 
723 	comp->dev = dev;
724 
725 	strscpy(comp->name, dev_name(dev), sizeof(comp->name));
726 
727 	ret = tascodec_spi_init(tas_hda->priv, codec, THIS_MODULE,
728 		tasdev_fw_ready);
729 	if (!ret)
730 		comp->playback_hook = tas2781_hda_playback_hook;
731 
732 	pm_runtime_put_autosuspend(dev);
733 
734 	return ret;
735 }
736 
737 static void tas2781_hda_unbind(struct device *dev, struct device *master,
738 			       void *master_data)
739 {
740 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
741 	struct hda_component_parent *parent = master_data;
742 	struct tasdevice_priv *tas_priv = tas_hda->priv;
743 	struct hda_component *comp;
744 
745 	comp = hda_component_from_index(parent, tas_priv->index);
746 	if (comp && (comp->dev == dev)) {
747 		comp->dev = NULL;
748 		memset(comp->name, 0, sizeof(comp->name));
749 		comp->playback_hook = NULL;
750 	}
751 
752 	tas2781_hda_remove_controls(tas_hda);
753 
754 	tasdevice_config_info_remove(tas_priv);
755 	tasdevice_dsp_remove(tas_priv);
756 
757 	tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
758 }
759 
760 static const struct component_ops tas2781_hda_comp_ops = {
761 	.bind = tas2781_hda_bind,
762 	.unbind = tas2781_hda_unbind,
763 };
764 
765 static int tas2781_hda_spi_probe(struct spi_device *spi)
766 {
767 	struct tas2781_hda_spi_priv *hda_priv;
768 	struct tasdevice_priv *tas_priv;
769 	struct tas2781_hda *tas_hda;
770 	const char *device_name;
771 	int ret = 0;
772 
773 	tas_hda = devm_kzalloc(&spi->dev, sizeof(*tas_hda), GFP_KERNEL);
774 	if (!tas_hda)
775 		return -ENOMEM;
776 
777 	hda_priv = devm_kzalloc(&spi->dev, sizeof(*hda_priv), GFP_KERNEL);
778 	if (!hda_priv)
779 		return -ENOMEM;
780 
781 	tas_hda->hda_priv = hda_priv;
782 	spi->max_speed_hz = TAS2781_SPI_MAX_FREQ;
783 
784 	tas_priv = devm_kzalloc(&spi->dev, sizeof(*tas_priv), GFP_KERNEL);
785 	if (!tas_priv)
786 		return -ENOMEM;
787 	tas_priv->dev = &spi->dev;
788 	tas_hda->priv = tas_priv;
789 	tas_priv->regmap = devm_regmap_init_spi(spi, &tasdevice_regmap);
790 	if (IS_ERR(tas_priv->regmap)) {
791 		ret = PTR_ERR(tas_priv->regmap);
792 		dev_err(tas_priv->dev, "Failed to allocate regmap: %d\n",
793 			ret);
794 		return ret;
795 	}
796 	if (strstr(dev_name(&spi->dev), "TXNW2781")) {
797 		device_name = "TXNW2781";
798 	} else {
799 		dev_err(tas_priv->dev, "Unmatched spi dev %s\n",
800 			dev_name(&spi->dev));
801 		return -ENODEV;
802 	}
803 
804 	tas_priv->irq = spi->irq;
805 	dev_set_drvdata(&spi->dev, tas_hda);
806 	ret = tas2781_read_acpi(tas_hda, device_name,
807 				spi_get_chipselect(spi, 0));
808 	if (ret)
809 		return dev_err_probe(tas_priv->dev, ret,
810 				"Platform not supported\n");
811 
812 	tasdevice_spi_init(tas_priv);
813 
814 	pm_runtime_set_autosuspend_delay(tas_priv->dev, 3000);
815 	pm_runtime_use_autosuspend(tas_priv->dev);
816 	pm_runtime_set_active(tas_priv->dev);
817 	pm_runtime_get_noresume(tas_priv->dev);
818 	pm_runtime_enable(tas_priv->dev);
819 
820 	pm_runtime_put_autosuspend(tas_priv->dev);
821 
822 	ret = component_add(tas_priv->dev, &tas2781_hda_comp_ops);
823 	if (ret) {
824 		dev_err(tas_priv->dev, "Register component fail: %d\n", ret);
825 		pm_runtime_disable(tas_priv->dev);
826 		tas2781_hda_remove(&spi->dev, &tas2781_hda_comp_ops);
827 	}
828 
829 	return ret;
830 }
831 
832 static void tas2781_hda_spi_remove(struct spi_device *spi)
833 {
834 	tas2781_hda_remove(&spi->dev, &tas2781_hda_comp_ops);
835 }
836 
837 static int tas2781_runtime_suspend(struct device *dev)
838 {
839 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
840 	struct tasdevice_priv *tas_priv = tas_hda->priv;
841 
842 	guard(mutex)(&tas_priv->codec_lock);
843 
844 	if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK
845 		&& tas_priv->playback_started)
846 		tasdevice_tuning_switch(tas_priv, 1);
847 
848 	tas_priv->tasdevice[tas_priv->index].cur_book = -1;
849 	tas_priv->tasdevice[tas_priv->index].cur_conf = -1;
850 
851 	return 0;
852 }
853 
854 static int tas2781_runtime_resume(struct device *dev)
855 {
856 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
857 	struct tasdevice_priv *tas_priv = tas_hda->priv;
858 
859 	guard(mutex)(&tas_priv->codec_lock);
860 
861 	if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK
862 		&& tas_priv->playback_started)
863 		tasdevice_tuning_switch(tas_priv, 0);
864 
865 	return 0;
866 }
867 
868 static int tas2781_system_suspend(struct device *dev)
869 {
870 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
871 	struct tasdevice_priv *tas_priv = tas_hda->priv;
872 	int ret;
873 
874 	ret = pm_runtime_force_suspend(dev);
875 	if (ret)
876 		return ret;
877 
878 	/* Shutdown chip before system suspend */
879 	if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK
880 		&& tas_priv->playback_started)
881 		tasdevice_tuning_switch(tas_priv, 1);
882 
883 	return 0;
884 }
885 
886 static int tas2781_system_resume(struct device *dev)
887 {
888 	struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
889 	struct tasdevice_priv *tas_priv = tas_hda->priv;
890 	int ret, val;
891 
892 	ret = pm_runtime_force_resume(dev);
893 	if (ret)
894 		return ret;
895 
896 	guard(mutex)(&tas_priv->codec_lock);
897 	ret = tas_priv->dev_read(tas_priv, tas_priv->index,
898 		TAS2781_REG_CLK_CONFIG, &val);
899 	if (ret < 0)
900 		return ret;
901 
902 	if (val == TAS2781_REG_CLK_CONFIG_RESET) {
903 		tas_priv->tasdevice[tas_priv->index].cur_book = -1;
904 		tas_priv->tasdevice[tas_priv->index].cur_conf = -1;
905 		tas_priv->tasdevice[tas_priv->index].cur_prog = -1;
906 
907 		ret = tasdevice_prmg_load(tas_priv, 0);
908 		if (ret < 0) {
909 			dev_err(tas_priv->dev,
910 				"FW download failed = %d\n", ret);
911 			return ret;
912 		}
913 		tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
914 
915 		if (tas_priv->playback_started)
916 			tasdevice_tuning_switch(tas_priv, 0);
917 	}
918 
919 	return ret;
920 }
921 
922 static const struct dev_pm_ops tas2781_hda_pm_ops = {
923 	RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
924 	SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
925 };
926 
927 static const struct spi_device_id tas2781_hda_spi_id[] = {
928 	{ "tas2781-hda", },
929 	{}
930 };
931 
932 static const struct acpi_device_id tas2781_acpi_hda_match[] = {
933 	{"TXNW2781", },
934 	{}
935 };
936 MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
937 
938 static struct spi_driver tas2781_hda_spi_driver = {
939 	.driver = {
940 		.name		= "tas2781-hda",
941 		.acpi_match_table = tas2781_acpi_hda_match,
942 		.pm		= &tas2781_hda_pm_ops,
943 	},
944 	.id_table	= tas2781_hda_spi_id,
945 	.probe		= tas2781_hda_spi_probe,
946 	.remove		= tas2781_hda_spi_remove,
947 };
948 module_spi_driver(tas2781_hda_spi_driver);
949 
950 MODULE_DESCRIPTION("TAS2781 HDA SPI Driver");
951 MODULE_AUTHOR("Baojun, Xu, <baojun.xug@ti.com>");
952 MODULE_LICENSE("GPL");
953 MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");
954 MODULE_IMPORT_NS("SND_HDA_SCODEC_TAS2781");
955