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