xref: /linux/sound/soc/codecs/tas2781-i2c.c (revision 05a54fa773284d1a7923cdfdd8f0c8dabb98bd26)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // ALSA SoC Texas Instruments TAS2563/TAS2781 Audio Smart Amplifier
4 //
5 // Copyright (C) 2022 - 2025 Texas Instruments Incorporated
6 // https://www.ti.com
7 //
8 // The TAS2563/TAS2781 driver implements a flexible and configurable
9 // algo coefficient setting for one, two, or even multiple
10 // TAS2563/TAS2781 chips.
11 //
12 // Author: Shenghao Ding <shenghao-ding@ti.com>
13 // Author: Kevin Lu <kevin-lu@ti.com>
14 //
15 
16 #include <linux/crc8.h>
17 #include <linux/firmware.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/i2c.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/of.h>
24 #include <linux/of_address.h>
25 #include <linux/of_irq.h>
26 #include <linux/regmap.h>
27 #include <linux/slab.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/tas2781.h>
31 #include <sound/tas2781-comlib-i2c.h>
32 #include <sound/tlv.h>
33 #include <sound/tas2x20-tlv.h>
34 #include <sound/tas2563-tlv.h>
35 #include <sound/tas2781-tlv.h>
36 #include <sound/tas5825-tlv.h>
37 #include <linux/unaligned.h>
38 
39 #define X2563_CL_STT_VAL(xreg, xval) \
40 {	.reg = xreg, \
41 	.val = { xval }, \
42 	.val_len = 1, }
43 
44 #define X2563_CL_STT_4BYTS(xreg, byte0, byte1, byte2, byte3) \
45 {	.reg = xreg, \
46 	.val = { byte0, byte1, byte2, byte3 }, \
47 	.val_len = 4, }
48 
49 static const struct bulk_reg_val tas2563_cali_start_reg[] = {
50 	X2563_CL_STT_VAL(TAS2563_IDLE, 0x00),
51 	X2563_CL_STT_4BYTS(TAS2563_PRM_ENFF_REG, 0x40, 0x00, 0x00, 0x00),
52 	X2563_CL_STT_4BYTS(TAS2563_PRM_DISTCK_REG, 0x40, 0x00, 0x00, 0x00),
53 	X2563_CL_STT_4BYTS(TAS2563_PRM_TE_SCTHR_REG, 0x7f, 0xff, 0xff, 0xff),
54 	X2563_CL_STT_4BYTS(TAS2563_PRM_PLT_FLAG_REG, 0x40, 0x00, 0x00, 0x00),
55 	X2563_CL_STT_4BYTS(TAS2563_PRM_SINEGAIN_REG, 0x0a, 0x3d, 0x70, 0xa4),
56 	X2563_CL_STT_4BYTS(TAS2563_TE_TA1_REG, 0x00, 0x36, 0x91, 0x5e),
57 	X2563_CL_STT_4BYTS(TAS2563_TE_TA1_AT_REG, 0x00, 0x36, 0x91, 0x5e),
58 	X2563_CL_STT_4BYTS(TAS2563_TE_TA2_REG, 0x00, 0x06, 0xd3, 0x72),
59 	X2563_CL_STT_4BYTS(TAS2563_TE_AT_REG, 0x00, 0x36, 0x91, 0x5e),
60 	X2563_CL_STT_4BYTS(TAS2563_TE_DT_REG, 0x00, 0x36, 0x91, 0x5e),
61 };
62 
63 #define X2781_CL_STT_VAL(xreg, xval, xlocked) \
64 {	.reg = xreg, \
65 	.val = { xval }, \
66 	.val_len = 1, \
67 	.is_locked = xlocked, }
68 
69 #define X2781_CL_STT_4BYTS_UNLOCKED(xreg, byte0, byte1, byte2, byte3) \
70 {	.reg = xreg, \
71 	.val = { byte0, byte1, byte2, byte3 }, \
72 	.val_len = 4, \
73 	.is_locked = false, }
74 
75 #define X2781_CL_STT_LEN_UNLOCKED(xreg) \
76 {	.reg = xreg, \
77 	.val_len = 4, \
78 	.is_locked = false, }
79 
80 static const struct bulk_reg_val tas2781_cali_start_reg[] = {
81 	X2781_CL_STT_VAL(TAS2781_PRM_INT_MASK_REG, 0xfe, false),
82 	X2781_CL_STT_VAL(TAS2781_PRM_CLK_CFG_REG, 0xdd, false),
83 	X2781_CL_STT_VAL(TAS2781_PRM_RSVD_REG, 0x20, false),
84 	X2781_CL_STT_VAL(TAS2781_PRM_TEST_57_REG, 0x14, true),
85 	X2781_CL_STT_VAL(TAS2781_PRM_TEST_62_REG, 0x45, true),
86 	X2781_CL_STT_VAL(TAS2781_PRM_PVDD_UVLO_REG, 0x03, false),
87 	X2781_CL_STT_VAL(TAS2781_PRM_CHNL_0_REG, 0xa8, false),
88 	X2781_CL_STT_VAL(TAS2781_PRM_NG_CFG0_REG, 0xb9, false),
89 	X2781_CL_STT_VAL(TAS2781_PRM_IDLE_CH_DET_REG, 0x92, false),
90 	/*
91 	 * This register is pilot tone threshold, different with the
92 	 * calibration tool version, it will be updated in
93 	 * tas2781_calib_start_put(), set to 1mA.
94 	 */
95 	X2781_CL_STT_4BYTS_UNLOCKED(0, 0x00, 0x00, 0x00, 0x56),
96 	X2781_CL_STT_4BYTS_UNLOCKED(TAS2781_PRM_PLT_FLAG_REG,
97 		0x40, 0x00, 0x00, 0x00),
98 	X2781_CL_STT_LEN_UNLOCKED(TAS2781_PRM_SINEGAIN_REG),
99 	X2781_CL_STT_LEN_UNLOCKED(TAS2781_PRM_SINEGAIN2_REG),
100 };
101 
102 static const struct i2c_device_id tasdevice_id[] = {
103 	{ "tas2020", TAS2020 },
104 	{ "tas2118", TAS2118 },
105 	{ "tas2120", TAS2120 },
106 	{ "tas2320", TAS2320 },
107 	{ "tas2563", TAS2563 },
108 	{ "tas2570", TAS2570 },
109 	{ "tas2572", TAS2572 },
110 	{ "tas2781", TAS2781 },
111 	{ "tas5825", TAS5825 },
112 	{ "tas5827", TAS5827 },
113 	{}
114 };
115 MODULE_DEVICE_TABLE(i2c, tasdevice_id);
116 
117 #ifdef CONFIG_OF
118 static const struct of_device_id tasdevice_of_match[] = {
119 	{ .compatible = "ti,tas2020" },
120 	{ .compatible = "ti,tas2118" },
121 	{ .compatible = "ti,tas2120" },
122 	{ .compatible = "ti,tas2320" },
123 	{ .compatible = "ti,tas2563" },
124 	{ .compatible = "ti,tas2570" },
125 	{ .compatible = "ti,tas2572" },
126 	{ .compatible = "ti,tas2781" },
127 	{ .compatible = "ti,tas5825" },
128 	{ .compatible = "ti,tas5827" },
129 	{},
130 };
131 MODULE_DEVICE_TABLE(of, tasdevice_of_match);
132 #endif
133 
134 /**
135  * tas2781_digital_getvol - get the volum control
136  * @kcontrol: control pointer
137  * @ucontrol: User data
138  * Customer Kcontrol for tas2781 is primarily for regmap booking, paging
139  * depends on internal regmap mechanism.
140  * tas2781 contains book and page two-level register map, especially
141  * book switching will set the register BXXP00R7F, after switching to the
142  * correct book, then leverage the mechanism for paging to access the
143  * register.
144  */
145 static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
146 	struct snd_ctl_elem_value *ucontrol)
147 {
148 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
149 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
150 	struct soc_mixer_control *mc =
151 		(struct soc_mixer_control *)kcontrol->private_value;
152 
153 	return tasdevice_digital_getvol(tas_priv, ucontrol, mc);
154 }
155 
156 static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,
157 	struct snd_ctl_elem_value *ucontrol)
158 {
159 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
160 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
161 	struct soc_mixer_control *mc =
162 		(struct soc_mixer_control *)kcontrol->private_value;
163 
164 	return tasdevice_digital_putvol(tas_priv, ucontrol, mc);
165 }
166 
167 static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
168 	struct snd_ctl_elem_value *ucontrol)
169 {
170 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
171 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
172 	struct soc_mixer_control *mc =
173 		(struct soc_mixer_control *)kcontrol->private_value;
174 
175 	return tasdevice_amp_getvol(tas_priv, ucontrol, mc);
176 }
177 
178 static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
179 	struct snd_ctl_elem_value *ucontrol)
180 {
181 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
182 	struct tasdevice_priv *tas_priv =
183 		snd_soc_component_get_drvdata(codec);
184 	struct soc_mixer_control *mc =
185 		(struct soc_mixer_control *)kcontrol->private_value;
186 
187 	return tasdevice_amp_putvol(tas_priv, ucontrol, mc);
188 }
189 
190 static int tasdev_force_fwload_get(struct snd_kcontrol *kcontrol,
191 	struct snd_ctl_elem_value *ucontrol)
192 {
193 	struct snd_soc_component *component =
194 		snd_soc_kcontrol_component(kcontrol);
195 	struct tasdevice_priv *tas_priv =
196 		snd_soc_component_get_drvdata(component);
197 
198 	ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
199 	dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
200 			tas_priv->force_fwload_status ? "ON" : "OFF");
201 
202 	return 0;
203 }
204 
205 static int tasdev_force_fwload_put(struct snd_kcontrol *kcontrol,
206 	struct snd_ctl_elem_value *ucontrol)
207 {
208 	struct snd_soc_component *component =
209 		snd_soc_kcontrol_component(kcontrol);
210 	struct tasdevice_priv *tas_priv =
211 		snd_soc_component_get_drvdata(component);
212 	bool change, val = (bool)ucontrol->value.integer.value[0];
213 
214 	if (tas_priv->force_fwload_status == val)
215 		change = false;
216 	else {
217 		change = true;
218 		tas_priv->force_fwload_status = val;
219 	}
220 	dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
221 		tas_priv->force_fwload_status ? "ON" : "OFF");
222 
223 	return change;
224 }
225 
226 static int tasdev_cali_data_get(struct snd_kcontrol *kcontrol,
227 	struct snd_ctl_elem_value *ucontrol)
228 {
229 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
230 	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
231 	struct soc_bytes_ext *bytes_ext =
232 		(struct soc_bytes_ext *) kcontrol->private_value;
233 	struct calidata *cali_data = &priv->cali_data;
234 	struct cali_reg *p = &cali_data->cali_reg_array;
235 	unsigned char *dst = ucontrol->value.bytes.data;
236 	unsigned char *data = cali_data->data;
237 	unsigned int i = 0;
238 	unsigned int j, k;
239 	int rc;
240 
241 	guard(mutex)(&priv->codec_lock);
242 	if (!priv->is_user_space_calidata)
243 		return -1;
244 
245 	if (!p->r0_reg)
246 		return -1;
247 
248 	dst[i++] = bytes_ext->max;
249 	dst[i++] = 'r';
250 
251 	dst[i++] = TASDEVICE_BOOK_ID(p->r0_reg);
252 	dst[i++] = TASDEVICE_PAGE_ID(p->r0_reg);
253 	dst[i++] = TASDEVICE_PAGE_REG(p->r0_reg);
254 
255 	dst[i++] = TASDEVICE_BOOK_ID(p->r0_low_reg);
256 	dst[i++] = TASDEVICE_PAGE_ID(p->r0_low_reg);
257 	dst[i++] = TASDEVICE_PAGE_REG(p->r0_low_reg);
258 
259 	dst[i++] = TASDEVICE_BOOK_ID(p->invr0_reg);
260 	dst[i++] = TASDEVICE_PAGE_ID(p->invr0_reg);
261 	dst[i++] = TASDEVICE_PAGE_REG(p->invr0_reg);
262 
263 	dst[i++] = TASDEVICE_BOOK_ID(p->pow_reg);
264 	dst[i++] = TASDEVICE_PAGE_ID(p->pow_reg);
265 	dst[i++] = TASDEVICE_PAGE_REG(p->pow_reg);
266 
267 	dst[i++] = TASDEVICE_BOOK_ID(p->tlimit_reg);
268 	dst[i++] = TASDEVICE_PAGE_ID(p->tlimit_reg);
269 	dst[i++] = TASDEVICE_PAGE_REG(p->tlimit_reg);
270 
271 	for (j = 0, k = 0; j < priv->ndev; j++) {
272 		if (j == data[k]) {
273 			dst[i++] = j;
274 			k++;
275 		} else {
276 			dev_err(priv->dev, "chn %d device %u not match\n",
277 				j, data[k]);
278 			k += 21;
279 			continue;
280 		}
281 		rc = tasdevice_dev_bulk_read(priv, j, p->r0_reg, &dst[i], 4);
282 		if (rc < 0) {
283 			dev_err(priv->dev, "chn %d r0_reg bulk_rd err = %d\n",
284 				j, rc);
285 			i += 20;
286 			k += 20;
287 			continue;
288 		}
289 		rc = memcmp(&dst[i], &data[k], 4);
290 		if (rc != 0)
291 			dev_dbg(priv->dev, "chn %d r0_data is not same\n", j);
292 		k += 4;
293 		i += 4;
294 		rc = tasdevice_dev_bulk_read(priv, j, p->r0_low_reg,
295 			&dst[i], 4);
296 		if (rc < 0) {
297 			dev_err(priv->dev, "chn %d r0_low bulk_rd err = %d\n",
298 				j, rc);
299 			i += 16;
300 			k += 16;
301 			continue;
302 		}
303 		rc = memcmp(&dst[i], &data[k], 4);
304 		if (rc != 0)
305 			dev_dbg(priv->dev, "chn %d r0_low is not same\n", j);
306 		i += 4;
307 		k += 4;
308 		rc = tasdevice_dev_bulk_read(priv, j, p->invr0_reg,
309 			&dst[i], 4);
310 		if (rc < 0) {
311 			dev_err(priv->dev, "chn %d invr0 bulk_rd err = %d\n",
312 				j, rc);
313 			i += 12;
314 			k += 12;
315 			continue;
316 		}
317 		rc = memcmp(&dst[i], &data[k], 4);
318 		if (rc != 0)
319 			dev_dbg(priv->dev, "chn %d invr0 is not same\n", j);
320 		i += 4;
321 		k += 4;
322 		rc = tasdevice_dev_bulk_read(priv, j, p->pow_reg, &dst[i], 4);
323 		if (rc < 0) {
324 			dev_err(priv->dev, "chn %d pow_reg bulk_rd err = %d\n",
325 				j, rc);
326 			i += 8;
327 			k += 8;
328 			continue;
329 		}
330 		rc = memcmp(&dst[i], &data[k], 4);
331 		if (rc != 0)
332 			dev_dbg(priv->dev, "chn %d pow_reg is not same\n", j);
333 		i += 4;
334 		k += 4;
335 		rc = tasdevice_dev_bulk_read(priv, j, p->tlimit_reg,
336 			&dst[i], 4);
337 		if (rc < 0) {
338 			dev_err(priv->dev, "chn %d tlimit bulk_rd err = %d\n",
339 				j, rc);
340 		}
341 		rc = memcmp(&dst[i], &data[k], 4);
342 		if (rc != 0)
343 			dev_dbg(priv->dev, "chn %d tlimit is not same\n", j);
344 		i += 4;
345 		k += 4;
346 	}
347 	return 0;
348 }
349 
350 static int calib_data_get(struct tasdevice_priv *tas_priv, int reg,
351 	unsigned char *dst)
352 {
353 	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;
354 	struct tasdevice *tasdev = tas_priv->tasdevice;
355 	int rc = -1;
356 	int i;
357 
358 	for (i = 0; i < tas_priv->ndev; i++) {
359 		if (clt->addr == tasdev[i].dev_addr) {
360 			/* First byte is the device index. */
361 			dst[0] = i;
362 			rc = tasdevice_dev_bulk_read(tas_priv, i, reg, &dst[1],
363 				4);
364 			break;
365 		}
366 	}
367 
368 	return rc;
369 }
370 
371 static int partial_cali_data_update(int *reg, int j)
372 {
373 	switch (tas2781_cali_start_reg[j].reg) {
374 	case 0:
375 		return reg[0];
376 	case TAS2781_PRM_PLT_FLAG_REG:
377 		return reg[1];
378 	case TAS2781_PRM_SINEGAIN_REG:
379 		return reg[2];
380 	case TAS2781_PRM_SINEGAIN2_REG:
381 		return reg[3];
382 	default:
383 		return 0;
384 	}
385 }
386 
387 static void sngl_calib_start(struct tasdevice_priv *tas_priv, int i,
388 	int *reg, unsigned char *dat)
389 {
390 	struct tasdevice *tasdev = tas_priv->tasdevice;
391 	struct bulk_reg_val *p = tasdev[i].cali_data_backup;
392 	struct bulk_reg_val *t = &tasdev[i].alp_cali_bckp;
393 	const int sum = ARRAY_SIZE(tas2781_cali_start_reg);
394 	unsigned char val[4];
395 	int j, r;
396 
397 	if (p == NULL)
398 		return;
399 
400 	/* Store the current setting from the chip */
401 	for (j = 0; j < sum; j++) {
402 		if (p[j].val_len == 1) {
403 			if (p[j].is_locked)
404 				tasdevice_dev_write(tas_priv, i,
405 					TAS2781_TEST_UNLOCK_REG,
406 					TAS2781_TEST_PAGE_UNLOCK);
407 			tasdevice_dev_read(tas_priv, i, p[j].reg,
408 				(int *)&p[j].val[0]);
409 		} else {
410 			if (!tas_priv->dspbin_typ) {
411 				r = partial_cali_data_update(reg, j);
412 				if (r)
413 					p[j].reg = r;
414 			}
415 
416 			if (p[j].reg)
417 				tasdevice_dev_bulk_read(tas_priv, i, p[j].reg,
418 					p[j].val, 4);
419 		}
420 	}
421 
422 	if (tas_priv->dspbin_typ == TASDEV_ALPHA)
423 		tasdevice_dev_bulk_read(tas_priv, i, t->reg, t->val, 4);
424 
425 	/* Update the setting for calibration */
426 	for (j = 0; j < sum - 4; j++) {
427 		if (p[j].val_len == 1) {
428 			if (p[j].is_locked)
429 				tasdevice_dev_write(tas_priv, i,
430 					TAS2781_TEST_UNLOCK_REG,
431 					TAS2781_TEST_PAGE_UNLOCK);
432 			tasdevice_dev_write(tas_priv, i, p[j].reg,
433 				tas2781_cali_start_reg[j].val[0]);
434 		}
435 	}
436 
437 	if (tas_priv->dspbin_typ == TASDEV_ALPHA) {
438 		val[0] = 0x00;
439 		val[1] = 0x00;
440 		val[2] = 0x21;
441 		val[3] = 0x8e;
442 	} else {
443 		val[0] = tas2781_cali_start_reg[j].val[0];
444 		val[1] = tas2781_cali_start_reg[j].val[1];
445 		val[2] = tas2781_cali_start_reg[j].val[2];
446 		val[3] = tas2781_cali_start_reg[j].val[3];
447 	}
448 	tasdevice_dev_bulk_write(tas_priv, i, p[j].reg, val, 4);
449 	tasdevice_dev_bulk_write(tas_priv, i, p[j + 1].reg,
450 		(unsigned char *)tas2781_cali_start_reg[j + 1].val, 4);
451 	tasdevice_dev_bulk_write(tas_priv, i, p[j + 2].reg, &dat[1], 4);
452 	tasdevice_dev_bulk_write(tas_priv, i, p[j + 3].reg, &dat[5], 4);
453 	if (tas_priv->dspbin_typ == TASDEV_ALPHA) {
454 		val[0] = 0x00;
455 		val[1] = 0x00;
456 		val[2] = 0x2a;
457 		val[3] = 0x0b;
458 
459 		tasdevice_dev_bulk_read(tas_priv, i, t->reg, val, 4);
460 	}
461 }
462 
463 static int tas2781_calib_start_put(struct snd_kcontrol *kcontrol,
464 	struct snd_ctl_elem_value *ucontrol)
465 {
466 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
467 	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
468 	struct soc_bytes_ext *bytes_ext =
469 		(struct soc_bytes_ext *) kcontrol->private_value;
470 	unsigned char *dat = ucontrol->value.bytes.data;
471 	int i, reg[4];
472 	int j = 0;
473 
474 	guard(mutex)(&priv->codec_lock);
475 	if (priv->chip_id != TAS2781 || bytes_ext->max != dat[0] ||
476 		dat[1] != 'r') {
477 		dev_err(priv->dev, "%s: package fmt or chipid incorrect\n",
478 			__func__);
479 		return 0;
480 	}
481 	j += 2;
482 	/* refresh pilot tone and SineGain register */
483 	for (i = 0; i < ARRAY_SIZE(reg); i++) {
484 		reg[i] = TASDEVICE_REG(dat[j], dat[j + 1], dat[j + 2]);
485 		j += 3;
486 	}
487 
488 	for (i = 0; i < priv->ndev; i++) {
489 		int k = i * 9 + j;
490 
491 		if (dat[k] != i) {
492 			dev_err(priv->dev, "%s:no cal-setting for dev %d\n",
493 				__func__, i);
494 			continue;
495 		}
496 		sngl_calib_start(priv, i, reg, dat + k);
497 	}
498 	return 1;
499 }
500 
501 static void tas2781_calib_stop_put(struct tasdevice_priv *priv)
502 {
503 	const int sum = ARRAY_SIZE(tas2781_cali_start_reg);
504 	int i, j;
505 
506 	for (i = 0; i < priv->ndev; i++) {
507 		struct tasdevice *tasdev = priv->tasdevice;
508 		struct bulk_reg_val *p = tasdev[i].cali_data_backup;
509 		struct bulk_reg_val *t = &tasdev[i].alp_cali_bckp;
510 
511 		if (p == NULL)
512 			continue;
513 
514 		for (j = 0; j < sum; j++) {
515 			if (p[j].val_len == 1) {
516 				if (p[j].is_locked)
517 					tasdevice_dev_write(priv, i,
518 						TAS2781_TEST_UNLOCK_REG,
519 						TAS2781_TEST_PAGE_UNLOCK);
520 				tasdevice_dev_write(priv, i, p[j].reg,
521 					p[j].val[0]);
522 			} else {
523 				if (!p[j].reg)
524 					continue;
525 				tasdevice_dev_bulk_write(priv, i, p[j].reg,
526 					p[j].val, 4);
527 			}
528 		}
529 
530 		if (priv->dspbin_typ == TASDEV_ALPHA)
531 			tasdevice_dev_bulk_write(priv, i, t->reg, t->val, 4);
532 	}
533 }
534 
535 static int tas2563_calib_start_put(struct snd_kcontrol *kcontrol,
536 	struct snd_ctl_elem_value *ucontrol)
537 {
538 	struct bulk_reg_val *q = (struct bulk_reg_val *)tas2563_cali_start_reg;
539 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
540 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
541 	const int sum = ARRAY_SIZE(tas2563_cali_start_reg);
542 	int i, j;
543 
544 	guard(mutex)(&tas_priv->codec_lock);
545 	if (tas_priv->chip_id != TAS2563)
546 		return -1;
547 
548 	for (i = 0; i < tas_priv->ndev; i++) {
549 		struct tasdevice *tasdev = tas_priv->tasdevice;
550 		struct bulk_reg_val *p = tasdev[i].cali_data_backup;
551 
552 		if (p == NULL)
553 			continue;
554 		for (j = 0; j < sum; j++) {
555 			if (p[j].val_len == 1)
556 				tasdevice_dev_read(tas_priv,
557 					i, p[j].reg,
558 					(unsigned int *)&p[j].val[0]);
559 			else
560 				tasdevice_dev_bulk_read(tas_priv,
561 					i, p[j].reg, p[j].val, 4);
562 		}
563 
564 		for (j = 0; j < sum; j++) {
565 			if (p[j].val_len == 1)
566 				tasdevice_dev_write(tas_priv, i, p[j].reg,
567 					q[j].val[0]);
568 			else
569 				tasdevice_dev_bulk_write(tas_priv, i, p[j].reg,
570 					q[j].val, 4);
571 		}
572 	}
573 
574 	return 1;
575 }
576 
577 static void tas2563_calib_stop_put(struct tasdevice_priv *tas_priv)
578 {
579 	const int sum = ARRAY_SIZE(tas2563_cali_start_reg);
580 	int i, j;
581 
582 	for (i = 0; i < tas_priv->ndev; i++) {
583 		struct tasdevice *tasdev = tas_priv->tasdevice;
584 		struct bulk_reg_val *p = tasdev[i].cali_data_backup;
585 
586 		if (p == NULL)
587 			continue;
588 
589 		for (j = 0; j < sum; j++) {
590 			if (p[j].val_len == 1)
591 				tasdevice_dev_write(tas_priv, i, p[j].reg,
592 					p[j].val[0]);
593 			else
594 				tasdevice_dev_bulk_write(tas_priv, i, p[j].reg,
595 					p[j].val, 4);
596 		}
597 	}
598 }
599 
600 static int tasdev_calib_stop_put(struct snd_kcontrol *kcontrol,
601 	struct snd_ctl_elem_value *ucontrol)
602 {
603 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
604 	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
605 
606 	guard(mutex)(&priv->codec_lock);
607 	if (priv->chip_id == TAS2563)
608 		tas2563_calib_stop_put(priv);
609 	else
610 		tas2781_calib_stop_put(priv);
611 
612 	return 1;
613 }
614 
615 static int tasdev_cali_data_put(struct snd_kcontrol *kcontrol,
616 	struct snd_ctl_elem_value *ucontrol)
617 {
618 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
619 	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
620 	struct soc_bytes_ext *bytes_ext =
621 		(struct soc_bytes_ext *) kcontrol->private_value;
622 	struct calidata *cali_data = &priv->cali_data;
623 	struct cali_reg *p = &cali_data->cali_reg_array;
624 	unsigned char *src = ucontrol->value.bytes.data;
625 	unsigned char *dst = cali_data->data;
626 	int i = 0;
627 	int j;
628 
629 	guard(mutex)(&priv->codec_lock);
630 	if (src[0] != bytes_ext->max || src[1] != 'r') {
631 		dev_err(priv->dev, "%s: pkg fmt invalid\n", __func__);
632 		return 0;
633 	}
634 	for (j = 0; j < priv->ndev; j++) {
635 		if (src[17 + j * 21] != j) {
636 			dev_err(priv->dev, "%s: pkg fmt invalid\n", __func__);
637 			return 0;
638 		}
639 	}
640 	i += 2;
641 	priv->is_user_space_calidata = true;
642 
643 	if (priv->dspbin_typ == TASDEV_BASIC) {
644 		p->r0_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
645 		i += 3;
646 		p->r0_low_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
647 		i += 3;
648 		p->invr0_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
649 		i += 3;
650 		p->pow_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
651 		i += 3;
652 		p->tlimit_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
653 		i += 3;
654 	} else {
655 		i += 15;
656 	}
657 
658 	memcpy(dst, &src[i], cali_data->total_sz);
659 	return 1;
660 }
661 
662 static int tas2781_latch_reg_get(struct snd_kcontrol *kcontrol,
663 	struct snd_ctl_elem_value *ucontrol)
664 {
665 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
666 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
667 	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;
668 	struct soc_bytes_ext *bytes_ext =
669 		(struct soc_bytes_ext *) kcontrol->private_value;
670 	struct tasdevice *tasdev = tas_priv->tasdevice;
671 	unsigned char *dst = ucontrol->value.bytes.data;
672 	int i, val, rc = -1;
673 
674 	dst[0] = bytes_ext->max;
675 	guard(mutex)(&tas_priv->codec_lock);
676 	for (i = 0; i < tas_priv->ndev; i++) {
677 		if (clt->addr == tasdev[i].dev_addr) {
678 			/* First byte is the device index. */
679 			dst[1] = i;
680 			rc = tasdevice_dev_read(tas_priv, i,
681 				TAS2781_RUNTIME_LATCH_RE_REG, &val);
682 			if (rc < 0)
683 				dev_err(tas_priv->dev, "%s, get value error\n",
684 					__func__);
685 			else
686 				dst[2] = val;
687 
688 			break;
689 		}
690 	}
691 
692 	return rc;
693 }
694 
695 static int tasdev_tf_data_get(struct snd_kcontrol *kcontrol,
696 	struct snd_ctl_elem_value *ucontrol)
697 {
698 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
699 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
700 	struct soc_bytes_ext *bytes_ext =
701 		(struct soc_bytes_ext *) kcontrol->private_value;
702 	unsigned char *dst = ucontrol->value.bytes.data;
703 	unsigned int reg = TAS2781_RUNTIME_RE_REG_TF;
704 
705 	if (tas_priv->chip_id == TAS2781) {
706 		struct tasdevice_fw *tas_fmw = tas_priv->fmw;
707 		struct fct_param_address *p = &(tas_fmw->fct_par_addr);
708 
709 		reg = TAS2781_RUNTIME_RE_REG_TF;
710 		if (tas_priv->dspbin_typ)
711 			reg = TASDEVICE_REG(p->tf_reg[0], p->tf_reg[1],
712 				p->tf_reg[2]);
713 	} else {
714 		reg = TAS2563_RUNTIME_RE_REG_TF;
715 	}
716 
717 	guard(mutex)(&tas_priv->codec_lock);
718 	dst[0] = bytes_ext->max;
719 	return calib_data_get(tas_priv, reg, &dst[1]);
720 }
721 
722 static int tasdev_re_data_get(struct snd_kcontrol *kcontrol,
723 	struct snd_ctl_elem_value *ucontrol)
724 {
725 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
726 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
727 	struct soc_bytes_ext *bytes_ext =
728 		(struct soc_bytes_ext *) kcontrol->private_value;
729 	unsigned char *dst = ucontrol->value.bytes.data;
730 	unsigned int reg = TAS2781_RUNTIME_RE_REG;
731 
732 	if (tas_priv->chip_id == TAS2781) {
733 		struct tasdevice_fw *tas_fmw = tas_priv->fmw;
734 		struct fct_param_address *p = &(tas_fmw->fct_par_addr);
735 
736 		if (tas_priv->dspbin_typ)
737 			reg = TASDEVICE_REG(p->r0_reg[0], p->r0_reg[1],
738 				p->r0_reg[2]);
739 	} else {
740 		reg = TAS2563_RUNTIME_RE_REG;
741 	}
742 
743 	guard(mutex)(&tas_priv->codec_lock);
744 	dst[0] = bytes_ext->max;
745 	return calib_data_get(tas_priv, reg, &dst[1]);
746 }
747 
748 static int tasdev_r0_data_get(struct snd_kcontrol *kcontrol,
749 	struct snd_ctl_elem_value *ucontrol)
750 {
751 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
752 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
753 	struct calidata *cali_data = &tas_priv->cali_data;
754 	struct soc_bytes_ext *bytes_ext =
755 		(struct soc_bytes_ext *) kcontrol->private_value;
756 	unsigned char *dst = ucontrol->value.bytes.data;
757 	unsigned int reg;
758 
759 	guard(mutex)(&tas_priv->codec_lock);
760 
761 	if (tas_priv->chip_id == TAS2563)
762 		reg = TAS2563_PRM_R0_REG;
763 	else if (cali_data->cali_reg_array.r0_reg)
764 		reg = cali_data->cali_reg_array.r0_reg;
765 	else
766 		return -1;
767 	dst[0] = bytes_ext->max;
768 	return calib_data_get(tas_priv, reg, &dst[1]);
769 }
770 
771 static int tasdev_XMA1_data_get(struct snd_kcontrol *kcontrol,
772 	struct snd_ctl_elem_value *ucontrol)
773 {
774 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
775 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
776 	struct tasdevice_fw *tas_fmw = tas_priv->fmw;
777 	struct fct_param_address *p = &(tas_fmw->fct_par_addr);
778 	struct soc_bytes_ext *bytes_ext =
779 		(struct soc_bytes_ext *) kcontrol->private_value;
780 	unsigned char *dst = ucontrol->value.bytes.data;
781 	unsigned int reg = TASDEVICE_XM_A1_REG;
782 
783 	if (tas_priv->dspbin_typ)
784 		reg = TASDEVICE_REG(p->a1_reg[0], p->a1_reg[1], p->a1_reg[2]);
785 
786 	guard(mutex)(&tas_priv->codec_lock);
787 	dst[0] = bytes_ext->max;
788 	return calib_data_get(tas_priv, reg, &dst[1]);
789 }
790 
791 static int tasdev_XMA2_data_get(struct snd_kcontrol *kcontrol,
792 	struct snd_ctl_elem_value *ucontrol)
793 {
794 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
795 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
796 	struct tasdevice_fw *tas_fmw = tas_priv->fmw;
797 	struct fct_param_address *p = &(tas_fmw->fct_par_addr);
798 	struct soc_bytes_ext *bytes_ext =
799 		(struct soc_bytes_ext *) kcontrol->private_value;
800 	unsigned char *dst = ucontrol->value.bytes.data;
801 	unsigned int reg = TASDEVICE_XM_A2_REG;
802 
803 	if (tas_priv->dspbin_typ)
804 		reg = TASDEVICE_REG(p->a2_reg[0], p->a2_reg[1], p->a2_reg[2]);
805 
806 	guard(mutex)(&tas_priv->codec_lock);
807 	dst[0] = bytes_ext->max;
808 	return calib_data_get(tas_priv, reg, &dst[1]);
809 }
810 
811 static int tasdev_nop_get(
812 	struct snd_kcontrol *kcontrol,
813 	struct snd_ctl_elem_value *ucontrol)
814 {
815 	return 0;
816 }
817 
818 static int tasdevice_digital_gain_get(
819 	struct snd_kcontrol *kcontrol,
820 	struct snd_ctl_elem_value *ucontrol)
821 {
822 	struct soc_mixer_control *mc =
823 		(struct soc_mixer_control *)kcontrol->private_value;
824 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
825 	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);
826 	unsigned int l = 0, r = mc->max;
827 	unsigned int target, ar_mid, mid, ar_l, ar_r;
828 	unsigned int reg = mc->reg;
829 	unsigned char data[4];
830 	int ret;
831 
832 	mutex_lock(&tas_dev->codec_lock);
833 	/* Read the primary device */
834 	ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4);
835 	if (ret) {
836 		dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__);
837 		goto out;
838 	}
839 
840 	target = get_unaligned_be32(&data[0]);
841 
842 	while (r > 1 + l) {
843 		mid = (l + r) / 2;
844 		ar_mid = get_unaligned_be32(tas_dev->dvc_tlv_table[mid]);
845 		if (target < ar_mid)
846 			r = mid;
847 		else
848 			l = mid;
849 	}
850 
851 	ar_l = get_unaligned_be32(tas_dev->dvc_tlv_table[l]);
852 	ar_r = get_unaligned_be32(tas_dev->dvc_tlv_table[r]);
853 
854 	/* find out the member same as or closer to the current volume */
855 	ucontrol->value.integer.value[0] =
856 		abs(target - ar_l) <= abs(target - ar_r) ? l : r;
857 out:
858 	mutex_unlock(&tas_dev->codec_lock);
859 	return 0;
860 }
861 
862 static int tasdevice_digital_gain_put(
863 	struct snd_kcontrol *kcontrol,
864 	struct snd_ctl_elem_value *ucontrol)
865 {
866 	struct soc_mixer_control *mc =
867 		(struct soc_mixer_control *)kcontrol->private_value;
868 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
869 	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);
870 	int vol = ucontrol->value.integer.value[0];
871 	int status = 0, max = mc->max, rc = 1;
872 	int i, ret;
873 	unsigned int reg = mc->reg;
874 	unsigned int volrd, volwr;
875 	unsigned char data[4];
876 
877 	vol = clamp(vol, 0, max);
878 	mutex_lock(&tas_dev->codec_lock);
879 	/* Read the primary device */
880 	ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4);
881 	if (ret) {
882 		dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__);
883 		rc = -1;
884 		goto out;
885 	}
886 
887 	volrd = get_unaligned_be32(&data[0]);
888 	volwr = get_unaligned_be32(tas_dev->dvc_tlv_table[vol]);
889 
890 	if (volrd == volwr) {
891 		rc = 0;
892 		goto out;
893 	}
894 
895 	for (i = 0; i < tas_dev->ndev; i++) {
896 		ret = tasdevice_dev_bulk_write(tas_dev, i, reg,
897 			(unsigned char *)tas_dev->dvc_tlv_table[vol], 4);
898 		if (ret) {
899 			dev_err(tas_dev->dev,
900 				"%s, set digital vol error in dev %d\n",
901 				__func__, i);
902 			status |= BIT(i);
903 		}
904 	}
905 
906 	if (status)
907 		rc = -1;
908 out:
909 	mutex_unlock(&tas_dev->codec_lock);
910 	return rc;
911 }
912 
913 static const struct snd_kcontrol_new tasdevice_cali_controls[] = {
914 	SOC_SINGLE_EXT("Calibration Stop", SND_SOC_NOPM, 0, 1, 0,
915 		tasdev_nop_get, tasdev_calib_stop_put),
916 	SND_SOC_BYTES_EXT("Amp TF Data", 6, tasdev_tf_data_get, NULL),
917 	SND_SOC_BYTES_EXT("Amp RE Data", 6, tasdev_re_data_get, NULL),
918 	SND_SOC_BYTES_EXT("Amp R0 Data", 6, tasdev_r0_data_get, NULL),
919 	SND_SOC_BYTES_EXT("Amp XMA1 Data", 6, tasdev_XMA1_data_get, NULL),
920 	SND_SOC_BYTES_EXT("Amp XMA2 Data", 6, tasdev_XMA2_data_get, NULL),
921 };
922 
923 static const struct snd_kcontrol_new tas2x20_snd_controls[] = {
924 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS2X20_AMP_LEVEL,
925 		0, 0, 42, 1, tas2781_amp_getvol,
926 		tas2781_amp_putvol, tas2x20_amp_tlv),
927 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2X20_DVC_LEVEL,
928 		0, 0, ARRAY_SIZE(tas2x20_dvc_table) - 1, 0,
929 		tasdevice_digital_gain_get, tasdevice_digital_gain_put,
930 		tas2x20_dvc_tlv),
931 };
932 
933 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
934 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS2781_AMP_LEVEL,
935 		1, 0, 20, 0, tas2781_amp_getvol,
936 		tas2781_amp_putvol, tas2781_amp_tlv),
937 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2781_DVC_LVL,
938 		0, 0, 200, 1, tas2781_digital_getvol,
939 		tas2781_digital_putvol, tas2781_dvc_tlv),
940 };
941 
942 static const struct snd_kcontrol_new tas5825_snd_controls[] = {
943 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS5825_AMP_LEVEL,
944 		0, 0, 31, 1, tas2781_amp_getvol,
945 		tas2781_amp_putvol, tas5825_amp_tlv),
946 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS5825_DVC_LEVEL,
947 		0, 0, 254, 1, tas2781_amp_getvol,
948 		tas2781_amp_putvol, tas5825_dvc_tlv),
949 };
950 
951 static const struct snd_kcontrol_new tas2781_cali_controls[] = {
952 	SND_SOC_BYTES_EXT("Amp Latch Data", 3, tas2781_latch_reg_get, NULL),
953 };
954 
955 static const struct snd_kcontrol_new tas2563_snd_controls[] = {
956 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2563_DVC_LVL, 0,
957 		0, ARRAY_SIZE(tas2563_dvc_table) - 1, 0,
958 		tasdevice_digital_gain_get, tasdevice_digital_gain_put,
959 		tas2563_dvc_tlv),
960 };
961 
962 static const struct snd_kcontrol_new tas2563_cali_controls[] = {
963 	SOC_SINGLE_EXT("Calibration Start", SND_SOC_NOPM, 0, 1, 0,
964 		tasdev_nop_get, tas2563_calib_start_put),
965 };
966 
967 static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
968 		struct snd_ctl_elem_value *ucontrol)
969 {
970 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
971 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
972 	int ret = 0;
973 
974 	if (tas_priv->rcabin.profile_cfg_id !=
975 		ucontrol->value.integer.value[0]) {
976 		tas_priv->rcabin.profile_cfg_id =
977 			ucontrol->value.integer.value[0];
978 		ret = 1;
979 	}
980 
981 	return ret;
982 }
983 
984 static int tasdevice_info_active_num(struct snd_kcontrol *kcontrol,
985 			struct snd_ctl_elem_info *uinfo)
986 {
987 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
988 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
989 
990 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
991 	uinfo->count = 1;
992 	uinfo->value.integer.min = 0;
993 	uinfo->value.integer.max = tas_priv->ndev - 1;
994 
995 	return 0;
996 }
997 
998 static int tasdevice_info_chip_id(struct snd_kcontrol *kcontrol,
999 			struct snd_ctl_elem_info *uinfo)
1000 {
1001 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1002 	uinfo->count = 1;
1003 	uinfo->value.integer.min = TAS2020;
1004 	uinfo->value.integer.max = TAS_OTHERS;
1005 
1006 	return 0;
1007 }
1008 
1009 static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
1010 			struct snd_ctl_elem_info *uinfo)
1011 {
1012 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1013 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1014 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
1015 
1016 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1017 	uinfo->count = 1;
1018 	uinfo->value.integer.min = 0;
1019 	uinfo->value.integer.max = (int)tas_fw->nr_programs;
1020 
1021 	return 0;
1022 }
1023 
1024 static int tasdevice_info_configurations(
1025 	struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1026 {
1027 	struct snd_soc_component *codec =
1028 		snd_soc_kcontrol_component(kcontrol);
1029 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1030 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
1031 
1032 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1033 	uinfo->count = 1;
1034 	uinfo->value.integer.min = 0;
1035 	uinfo->value.integer.max = (int)tas_fw->nr_configurations - 1;
1036 
1037 	return 0;
1038 }
1039 
1040 static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
1041 			struct snd_ctl_elem_info *uinfo)
1042 {
1043 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1044 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1045 
1046 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1047 	uinfo->count = 1;
1048 	uinfo->value.integer.min = 0;
1049 	uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
1050 
1051 	return 0;
1052 }
1053 
1054 static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
1055 			struct snd_ctl_elem_value *ucontrol)
1056 {
1057 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1058 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1059 
1060 	ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
1061 
1062 	return 0;
1063 }
1064 
1065 static int tasdevice_get_chip_id(struct snd_kcontrol *kcontrol,
1066 			struct snd_ctl_elem_value *ucontrol)
1067 {
1068 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1069 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1070 
1071 	ucontrol->value.integer.value[0] = tas_priv->chip_id;
1072 
1073 	return 0;
1074 }
1075 
1076 static int tasdevice_create_control(struct tasdevice_priv *tas_priv)
1077 {
1078 	struct snd_kcontrol_new *prof_ctrls;
1079 	int nr_controls = 1;
1080 	int mix_index = 0;
1081 	int ret;
1082 	char *name;
1083 
1084 	prof_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
1085 		sizeof(prof_ctrls[0]), GFP_KERNEL);
1086 	if (!prof_ctrls) {
1087 		ret = -ENOMEM;
1088 		goto out;
1089 	}
1090 
1091 	/* Create a mixer item for selecting the active profile */
1092 	name = devm_kstrdup(tas_priv->dev, "Speaker Profile Id", GFP_KERNEL);
1093 	if (!name) {
1094 		ret = -ENOMEM;
1095 		goto out;
1096 	}
1097 	prof_ctrls[mix_index].name = name;
1098 	prof_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1099 	prof_ctrls[mix_index].info = tasdevice_info_profile;
1100 	prof_ctrls[mix_index].get = tasdevice_get_profile_id;
1101 	prof_ctrls[mix_index].put = tasdevice_set_profile_id;
1102 	mix_index++;
1103 
1104 	ret = snd_soc_add_component_controls(tas_priv->codec,
1105 		prof_ctrls, nr_controls < mix_index ? nr_controls : mix_index);
1106 
1107 out:
1108 	return ret;
1109 }
1110 
1111 static int tasdevice_program_get(struct snd_kcontrol *kcontrol,
1112 	struct snd_ctl_elem_value *ucontrol)
1113 {
1114 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1115 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1116 
1117 	ucontrol->value.integer.value[0] = tas_priv->cur_prog;
1118 
1119 	return 0;
1120 }
1121 
1122 static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
1123 	struct snd_ctl_elem_value *ucontrol)
1124 {
1125 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1126 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1127 	unsigned int nr_program = ucontrol->value.integer.value[0];
1128 	int ret = 0;
1129 
1130 	if (tas_priv->cur_prog != nr_program) {
1131 		tas_priv->cur_prog = nr_program;
1132 		ret = 1;
1133 	}
1134 
1135 	return ret;
1136 }
1137 
1138 static int tasdevice_configuration_get(struct snd_kcontrol *kcontrol,
1139 	struct snd_ctl_elem_value *ucontrol)
1140 {
1141 
1142 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1143 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1144 
1145 	ucontrol->value.integer.value[0] = tas_priv->cur_conf;
1146 
1147 	return 0;
1148 }
1149 
1150 static int tasdevice_configuration_put(
1151 	struct snd_kcontrol *kcontrol,
1152 	struct snd_ctl_elem_value *ucontrol)
1153 {
1154 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1155 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1156 	unsigned int nr_configuration = ucontrol->value.integer.value[0];
1157 	int ret = 0;
1158 
1159 	if (tas_priv->cur_conf != nr_configuration) {
1160 		tas_priv->cur_conf = nr_configuration;
1161 		ret = 1;
1162 	}
1163 
1164 	return ret;
1165 }
1166 
1167 static int tasdevice_active_num_get(struct snd_kcontrol *kcontrol,
1168 	struct snd_ctl_elem_value *ucontrol)
1169 {
1170 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1171 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1172 	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;
1173 	struct tasdevice *tasdev = tas_priv->tasdevice;
1174 	int i;
1175 
1176 	for (i = 0; i < tas_priv->ndev; i++) {
1177 		if (clt->addr == tasdev[i].dev_addr) {
1178 			ucontrol->value.integer.value[0] = i;
1179 			return 0;
1180 		}
1181 	}
1182 
1183 	return -1;
1184 }
1185 
1186 static int tasdevice_active_num_put(struct snd_kcontrol *kcontrol,
1187 	struct snd_ctl_elem_value *ucontrol)
1188 {
1189 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1190 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1191 	int dev_id = ucontrol->value.integer.value[0];
1192 	int max = tas_priv->ndev - 1;
1193 
1194 	dev_id = clamp(dev_id, 0, max);
1195 
1196 	guard(mutex)(&tas_priv->codec_lock);
1197 	return tasdev_chn_switch(tas_priv, dev_id);
1198 }
1199 
1200 static int tasdevice_dsp_create_ctrls(struct tasdevice_priv *tas_priv)
1201 {
1202 	struct snd_kcontrol_new *dsp_ctrls;
1203 	char *active_dev_num, *chip_id, *fw_load;
1204 	char *conf_name, *prog_name;
1205 	int nr_controls = 5;
1206 	int mix_index = 0;
1207 
1208 	/* Alloc kcontrol via devm_kzalloc, which don't manually
1209 	 * free the kcontrol
1210 	 */
1211 	dsp_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
1212 		sizeof(dsp_ctrls[0]), GFP_KERNEL);
1213 	if (!dsp_ctrls)
1214 		return -ENOMEM;
1215 
1216 	/* Create mixer items for selecting the active Program and Config */
1217 	prog_name = devm_kstrdup(tas_priv->dev, "Speaker Program Id",
1218 		GFP_KERNEL);
1219 	if (!prog_name)
1220 		return -ENOMEM;
1221 
1222 	dsp_ctrls[mix_index].name = prog_name;
1223 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1224 	dsp_ctrls[mix_index].info = tasdevice_info_programs;
1225 	dsp_ctrls[mix_index].get = tasdevice_program_get;
1226 	dsp_ctrls[mix_index].put = tasdevice_program_put;
1227 	mix_index++;
1228 
1229 	conf_name = devm_kstrdup(tas_priv->dev, "Speaker Config Id",
1230 		GFP_KERNEL);
1231 	if (!conf_name)
1232 		return -ENOMEM;
1233 
1234 	dsp_ctrls[mix_index].name = conf_name;
1235 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1236 	dsp_ctrls[mix_index].info = tasdevice_info_configurations;
1237 	dsp_ctrls[mix_index].get = tasdevice_configuration_get;
1238 	dsp_ctrls[mix_index].put = tasdevice_configuration_put;
1239 	mix_index++;
1240 
1241 	active_dev_num = devm_kstrdup(tas_priv->dev, "Activate Tasdevice Num",
1242 		GFP_KERNEL);
1243 	if (!active_dev_num)
1244 		return -ENOMEM;
1245 
1246 	dsp_ctrls[mix_index].name = active_dev_num;
1247 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1248 	dsp_ctrls[mix_index].info = tasdevice_info_active_num;
1249 	dsp_ctrls[mix_index].get = tasdevice_active_num_get;
1250 	dsp_ctrls[mix_index].put = tasdevice_active_num_put;
1251 	mix_index++;
1252 
1253 	chip_id = devm_kstrdup(tas_priv->dev, "Tasdevice Chip Id", GFP_KERNEL);
1254 	if (!chip_id)
1255 		return -ENOMEM;
1256 
1257 	dsp_ctrls[mix_index].name = chip_id;
1258 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1259 	dsp_ctrls[mix_index].info = tasdevice_info_chip_id;
1260 	dsp_ctrls[mix_index].get = tasdevice_get_chip_id;
1261 	mix_index++;
1262 
1263 	fw_load = devm_kstrdup(tas_priv->dev, "Speaker Force Firmware Load",
1264 		GFP_KERNEL);
1265 	if (!fw_load)
1266 		return -ENOMEM;
1267 
1268 	dsp_ctrls[mix_index].name = fw_load;
1269 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1270 	dsp_ctrls[mix_index].info = snd_soc_info_bool_ext;
1271 	dsp_ctrls[mix_index].put = tasdev_force_fwload_put;
1272 	dsp_ctrls[mix_index].get = tasdev_force_fwload_get;
1273 	dsp_ctrls[mix_index].private_value = 0UL;
1274 	mix_index++;
1275 
1276 	return snd_soc_add_component_controls(tas_priv->codec, dsp_ctrls,
1277 		nr_controls < mix_index ? nr_controls : mix_index);
1278 }
1279 
1280 static void cali_reg_update(struct bulk_reg_val *p,
1281 	struct fct_param_address *t)
1282 {
1283 	const int sum = ARRAY_SIZE(tas2781_cali_start_reg);
1284 	int reg, j;
1285 
1286 	for (j = 0; j < sum; j++) {
1287 		switch (tas2781_cali_start_reg[j].reg) {
1288 		case 0:
1289 			reg = TASDEVICE_REG(t->thr[0], t->thr[1], t->thr[2]);
1290 			break;
1291 		case TAS2781_PRM_PLT_FLAG_REG:
1292 			reg = TASDEVICE_REG(t->plt_flg[0], t->plt_flg[1],
1293 				t->plt_flg[2]);
1294 			break;
1295 		case TAS2781_PRM_SINEGAIN_REG:
1296 			reg = TASDEVICE_REG(t->sin_gn[0], t->sin_gn[1],
1297 				t->sin_gn[2]);
1298 			break;
1299 		case TAS2781_PRM_SINEGAIN2_REG:
1300 			reg = TASDEVICE_REG(t->sin_gn[0], t->sin_gn[1],
1301 				t->sin_gn[2]);
1302 			break;
1303 		default:
1304 			reg = 0;
1305 			break;
1306 		}
1307 		if (reg)
1308 			p[j].reg = reg;
1309 	}
1310 }
1311 
1312 static void alpa_cali_update(struct bulk_reg_val *p,
1313 	struct fct_param_address *t)
1314 {
1315 	p->is_locked = false;
1316 	p->reg = TASDEVICE_REG(t->thr2[0], t->thr2[1], t->thr2[2]);
1317 	p->val_len = 4;
1318 }
1319 
1320 static int tasdevice_create_cali_ctrls(struct tasdevice_priv *priv)
1321 {
1322 	struct calidata *cali_data = &priv->cali_data;
1323 	struct tasdevice *tasdev = priv->tasdevice;
1324 	struct tasdevice_fw *fmw = priv->fmw;
1325 	struct soc_bytes_ext *ext_cali_data;
1326 	struct snd_kcontrol_new *cali_ctrls;
1327 	unsigned int nctrls;
1328 	char *cali_name;
1329 	int rc, i;
1330 
1331 	rc = snd_soc_add_component_controls(priv->codec,
1332 		tasdevice_cali_controls, ARRAY_SIZE(tasdevice_cali_controls));
1333 	if (rc < 0) {
1334 		dev_err(priv->dev, "%s: Add cali controls err rc = %d",
1335 			__func__, rc);
1336 		return rc;
1337 	}
1338 
1339 	if (priv->chip_id == TAS2781) {
1340 		struct fct_param_address *t = &(fmw->fct_par_addr);
1341 
1342 		cali_ctrls = (struct snd_kcontrol_new *)tas2781_cali_controls;
1343 		nctrls = ARRAY_SIZE(tas2781_cali_controls);
1344 		for (i = 0; i < priv->ndev; i++) {
1345 			struct bulk_reg_val *p;
1346 
1347 			p = tasdev[i].cali_data_backup =
1348 				kmemdup(tas2781_cali_start_reg,
1349 				sizeof(tas2781_cali_start_reg), GFP_KERNEL);
1350 			if (!tasdev[i].cali_data_backup)
1351 				return -ENOMEM;
1352 			if (priv->dspbin_typ) {
1353 				cali_reg_update(p, t);
1354 				if (priv->dspbin_typ == TASDEV_ALPHA) {
1355 					p = &tasdev[i].alp_cali_bckp;
1356 					alpa_cali_update(p, t);
1357 				}
1358 			}
1359 		}
1360 	} else {
1361 		cali_ctrls = (struct snd_kcontrol_new *)tas2563_cali_controls;
1362 		nctrls = ARRAY_SIZE(tas2563_cali_controls);
1363 		for (i = 0; i < priv->ndev; i++) {
1364 			tasdev[i].cali_data_backup =
1365 				kmemdup(tas2563_cali_start_reg,
1366 				sizeof(tas2563_cali_start_reg), GFP_KERNEL);
1367 			if (!tasdev[i].cali_data_backup)
1368 				return -ENOMEM;
1369 		}
1370 	}
1371 
1372 	rc = snd_soc_add_component_controls(priv->codec, cali_ctrls, nctrls);
1373 	if (rc < 0) {
1374 		dev_err(priv->dev, "%s: Add chip cali ctrls err rc = %d",
1375 			__func__, rc);
1376 		return rc;
1377 	}
1378 
1379 	/* index for cali_ctrls */
1380 	i = 0;
1381 	if (priv->chip_id == TAS2781)
1382 		nctrls = 2;
1383 	else
1384 		nctrls = 1;
1385 
1386 	/*
1387 	 * Alloc kcontrol via devm_kzalloc(), which don't manually
1388 	 * free the kcontrol。
1389 	 */
1390 	cali_ctrls = devm_kcalloc(priv->dev, nctrls,
1391 		sizeof(cali_ctrls[0]), GFP_KERNEL);
1392 	if (!cali_ctrls)
1393 		return -ENOMEM;
1394 
1395 	ext_cali_data = devm_kzalloc(priv->dev, sizeof(*ext_cali_data),
1396 		GFP_KERNEL);
1397 	if (!ext_cali_data)
1398 		return -ENOMEM;
1399 
1400 	cali_name = devm_kstrdup(priv->dev, "Speaker Calibrated Data",
1401 		GFP_KERNEL);
1402 	if (!cali_name)
1403 		return -ENOMEM;
1404 	/* the number of calibrated data per tas2563/tas2781 */
1405 	cali_data->cali_dat_sz_per_dev = 20;
1406 	/*
1407 	 * Data structure for tas2563/tas2781 calibrated data:
1408 	 *	Pkg len (1 byte)
1409 	 *	Reg id (1 byte, constant 'r')
1410 	 *	book, page, register array for calibrated data (15 bytes)
1411 	 *	for (i = 0; i < Device-Sum; i++) {
1412 	 *		Device #i index_info (1 byte)
1413 	 *		Calibrated data for Device #i (20 bytes)
1414 	 *	}
1415 	 */
1416 	ext_cali_data->max = priv->ndev *
1417 		(cali_data->cali_dat_sz_per_dev + 1) + 1 + 15 + 1;
1418 	priv->cali_data.total_sz = priv->ndev *
1419 		(cali_data->cali_dat_sz_per_dev + 1);
1420 	cali_ctrls[i].name = cali_name;
1421 	cali_ctrls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1422 	cali_ctrls[i].info = snd_soc_bytes_info_ext;
1423 	cali_ctrls[i].get = tasdev_cali_data_get;
1424 	cali_ctrls[i].put = tasdev_cali_data_put;
1425 	cali_ctrls[i].private_value = (unsigned long)ext_cali_data;
1426 	i++;
1427 
1428 	cali_data->data = devm_kzalloc(priv->dev, cali_data->total_sz,
1429 		GFP_KERNEL);
1430 	if (!cali_data->data)
1431 		return -ENOMEM;
1432 
1433 	if (priv->chip_id == TAS2781) {
1434 		struct soc_bytes_ext *ext_cali_start;
1435 		char *cali_start_name;
1436 
1437 		ext_cali_start = devm_kzalloc(priv->dev,
1438 			sizeof(*ext_cali_start), GFP_KERNEL);
1439 		if (!ext_cali_start)
1440 			return -ENOMEM;
1441 
1442 		cali_start_name = devm_kstrdup(priv->dev,
1443 			"Calibration Start", GFP_KERNEL);
1444 		if (!cali_start_name)
1445 			return -ENOMEM;
1446 		/*
1447 		 * package structure for tas2781 ftc start:
1448 		 *	Pkg len (1 byte)
1449 		 *	Reg id (1 byte, constant 'r')
1450 		 *	book, page, register for pilot threshold, pilot tone
1451 		 *		and sine gain (12 bytes)
1452 		 *	for (i = 0; i < Device-Sum; i++) {
1453 		 *		Device #i index_info (1 byte)
1454 		 *		Sine gain for Device #i (8 bytes)
1455 		 *	}
1456 		 */
1457 		ext_cali_start->max = 14 + priv->ndev * 9;
1458 		cali_ctrls[i].name = cali_start_name;
1459 		cali_ctrls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1460 		cali_ctrls[i].info = snd_soc_bytes_info_ext;
1461 		cali_ctrls[i].put = tas2781_calib_start_put;
1462 		cali_ctrls[i].get = tasdev_nop_get;
1463 		cali_ctrls[i].private_value = (unsigned long)ext_cali_start;
1464 		i++;
1465 	}
1466 
1467 	return snd_soc_add_component_controls(priv->codec, cali_ctrls,
1468 		nctrls < i ? nctrls : i);
1469 }
1470 
1471 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C
1472 /*
1473  * This debugfs node is a bridge to the acoustic tuning application
1474  * tool which can tune the chips' acoustic effect.
1475  *
1476  * package structure for PPC3 communications:
1477  *	Pkg len (1 byte)
1478  *	Pkg id (1 byte, 'r' or 'w')
1479  *	Dev id (1 byte, i2c address)
1480  *	Book id (1 byte)
1481  *	Page id (1 byte)
1482  *	Reg id (1 byte)
1483  *	switch (pkg id) {
1484  *	case 'w':
1485  *		1 byte, length of data to read
1486  *	case 'r':
1487  *		data payload (1~128 bytes)
1488  *	}
1489  */
1490 static ssize_t acoustic_ctl_read(struct file *file, char __user *to,
1491 	size_t count, loff_t *ppos)
1492 {
1493 	struct snd_soc_component *comp = file->private_data;
1494 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
1495 	struct acoustic_data *p = &tas_priv->acou_data;
1496 	int ret = -1;
1497 
1498 	if (p->id == 'r' && p->len == count && count <= sizeof(*p))
1499 		ret = simple_read_from_buffer(to, count, ppos, p, p->len);
1500 	else
1501 		dev_err(tas_priv->dev, "Not ready for get.\n");
1502 	return ret;
1503 }
1504 
1505 static ssize_t acoustic_ctl_write(struct file *file,
1506 	const char __user *from, size_t count, loff_t *ppos)
1507 {
1508 	struct snd_soc_component *comp = file->private_data;
1509 	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
1510 	struct acoustic_data *p = &priv->acou_data;
1511 	unsigned int max_pkg_len = sizeof(*p);
1512 	unsigned char *src;
1513 	int j, len, reg, val;
1514 	unsigned short chn;
1515 	int ret = -1;
1516 
1517 	if (count > sizeof(*p)) {
1518 		dev_err(priv->dev, "count(%u) is larger than max(%u).\n",
1519 			(unsigned int)count, max_pkg_len);
1520 		return ret;
1521 	}
1522 
1523 	src = memdup_user(from, count);
1524 	if (IS_ERR(src))
1525 		return PTR_ERR(src);
1526 
1527 	if (src[0] > max_pkg_len && src[0] != count) {
1528 		dev_err(priv->dev, "pkg(%u), max(%u), count(%u) mismatch.\n",
1529 			src[0], max_pkg_len, (unsigned int)count);
1530 		ret = 0;
1531 		goto exit;
1532 	}
1533 
1534 	switch (src[1]) {
1535 	case 'r':
1536 		/* length of data to read */
1537 		len = src[6];
1538 		break;
1539 	case 'w':
1540 		/* Skip 6 bytes for package type and register address */
1541 		len = src[0] - 6;
1542 		break;
1543 	default:
1544 		dev_err(priv->dev, "%s Wrong code %02x.\n", __func__, src[1]);
1545 		ret = 0;
1546 		goto exit;
1547 	}
1548 
1549 	if (len < 1) {
1550 		dev_err(priv->dev, "pkg fmt invalid %02x.\n", len);
1551 		ret = 0;
1552 		goto exit;
1553 	}
1554 
1555 	for (j = 0; j < priv->ndev; j++)
1556 		if (src[2] == priv->tasdevice[j].dev_addr) {
1557 			chn = j;
1558 			break;
1559 		}
1560 	if (j >= priv->ndev) {
1561 		dev_err(priv->dev, "no such device 0x%02x.\n", src[2]);
1562 		ret = 0;
1563 		goto exit;
1564 	}
1565 
1566 	reg = TASDEVICE_REG(src[3], src[4], src[5]);
1567 
1568 	guard(mutex)(&priv->codec_lock);
1569 
1570 	if (src[1] == 'w') {
1571 		if (len > 1)
1572 			ret = tasdevice_dev_bulk_write(priv, chn, reg,
1573 				 &src[6], len);
1574 		else
1575 			ret = tasdevice_dev_write(priv, chn, reg, src[6]);
1576 	} else {
1577 		struct acoustic_data *p = &priv->acou_data;
1578 
1579 		memcpy(p, src, 6);
1580 		if (len > 1) {
1581 			ret = tasdevice_dev_bulk_read(priv, chn, reg,
1582 				p->data, len);
1583 		} else {
1584 			ret = tasdevice_dev_read(priv, chn, reg, &val);
1585 			p->data[0] = val;
1586 		}
1587 		p->len = len + 6;
1588 	}
1589 
1590 	if (ret)
1591 		dev_err(priv->dev, "i2c communication error.\n");
1592 	else
1593 		ret = count;
1594 exit:
1595 	kfree(src);
1596 	return ret;
1597 }
1598 
1599 static const struct file_operations acoustic_ctl_fops = {
1600 	.open = simple_open,
1601 	.read = acoustic_ctl_read,
1602 	.write = acoustic_ctl_write,
1603 };
1604 #endif
1605 
1606 static void tasdevice_fw_ready(const struct firmware *fmw,
1607 	void *context)
1608 {
1609 	struct tasdevice_priv *tas_priv = context;
1610 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C
1611 	struct snd_soc_component *comp = tas_priv->codec;
1612 	struct dentry *debugfs_root = comp->debugfs_root;
1613 	char *acoustic_debugfs_node;
1614 #endif
1615 	int ret = 0;
1616 	int i;
1617 
1618 	mutex_lock(&tas_priv->codec_lock);
1619 
1620 	ret = tasdevice_rca_parser(tas_priv, fmw);
1621 	if (ret) {
1622 		tasdevice_config_info_remove(tas_priv);
1623 		goto out;
1624 	}
1625 	tasdevice_create_control(tas_priv);
1626 
1627 	tasdevice_dsp_remove(tas_priv);
1628 	tasdevice_calbin_remove(tas_priv);
1629 	/*
1630 	 * The baseline is the RCA-only case, and then the code attempts to
1631 	 * load DSP firmware but in case of failures just keep going, i.e.
1632 	 * failing to load DSP firmware is NOT an error.
1633 	 */
1634 	tas_priv->fw_state = TASDEVICE_RCA_FW_OK;
1635 	/* There is no DSP firmware required for TAS2118/2X20/257X. */
1636 	switch (tas_priv->chip_id) {
1637 	case TAS2020:
1638 	case TAS2118:
1639 	case TAS2120:
1640 	case TAS2320:
1641 	case TAS2570:
1642 	case TAS2572:
1643 		goto out;
1644 	}
1645 	if (tas_priv->name_prefix)
1646 		scnprintf(tas_priv->coef_binaryname, 64, "%s-%s_coef.bin",
1647 			tas_priv->name_prefix, tas_priv->dev_name);
1648 	else
1649 		scnprintf(tas_priv->coef_binaryname, 64, "%s_coef.bin",
1650 			tas_priv->dev_name);
1651 	ret = tasdevice_dsp_parser(tas_priv);
1652 	if (ret) {
1653 		dev_err(tas_priv->dev, "dspfw load %s error\n",
1654 			tas_priv->coef_binaryname);
1655 		goto out;
1656 	}
1657 
1658 	/*
1659 	 * If no dsp-related kcontrol created, the dsp resource will be freed.
1660 	 */
1661 	ret = tasdevice_dsp_create_ctrls(tas_priv);
1662 	if (ret) {
1663 		dev_err(tas_priv->dev, "dsp controls error\n");
1664 		goto out;
1665 	}
1666 	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
1667 
1668 	/* There is no calibration required for TAS5825/TAS5827. */
1669 	if (tas_priv->chip_id < TAS5825) {
1670 		ret = tasdevice_create_cali_ctrls(tas_priv);
1671 		if (ret) {
1672 			dev_err(tas_priv->dev, "cali controls error\n");
1673 			goto out;
1674 		}
1675 		/* If calibrated data occurs error, dsp will still works
1676 		 * with default calibrated data inside algo.
1677 		 */
1678 		for (i = 0; i < tas_priv->ndev; i++) {
1679 			if (tas_priv->name_prefix)
1680 				scnprintf(tas_priv->cal_binaryname[i], 64,
1681 					  "%s-%s_cal_0x%02x.bin",
1682 					  tas_priv->name_prefix,
1683 					  tas_priv->dev_name,
1684 					  tas_priv->tasdevice[i].dev_addr);
1685 			else
1686 				scnprintf(tas_priv->cal_binaryname[i], 64,
1687 					  "%s_cal_0x%02x.bin",
1688 					  tas_priv->dev_name,
1689 					  tas_priv->tasdevice[i].dev_addr);
1690 			ret = tas2781_load_calibration(tas_priv,
1691 				tas_priv->cal_binaryname[i], i);
1692 			if (ret != 0)
1693 				dev_err(tas_priv->dev,
1694 					"%s: load %s error, keep default.\n",
1695 					__func__, tas_priv->cal_binaryname[i]);
1696 		}
1697 	}
1698 
1699 	tasdevice_prmg_load(tas_priv, 0);
1700 	tas_priv->cur_prog = 0;
1701 
1702 	/* Init common setting for different audio profiles */
1703 	if (tas_priv->rcabin.init_profile_id >= 0)
1704 		tasdevice_select_cfg_blk(tas_priv,
1705 			tas_priv->rcabin.init_profile_id,
1706 			TASDEVICE_BIN_BLK_PRE_POWER_UP);
1707 
1708 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C
1709 	if (tas_priv->name_prefix)
1710 		acoustic_debugfs_node = devm_kasprintf(tas_priv->dev,
1711 			GFP_KERNEL, "%s_acoustic_ctl", tas_priv->name_prefix);
1712 	else
1713 		acoustic_debugfs_node = devm_kstrdup(tas_priv->dev,
1714 			"acoustic_ctl", GFP_KERNEL);
1715 	debugfs_create_file(acoustic_debugfs_node, 0644, debugfs_root,
1716 		comp, &acoustic_ctl_fops);
1717 #endif
1718 out:
1719 	if (tas_priv->fw_state == TASDEVICE_RCA_FW_OK) {
1720 		switch (tas_priv->chip_id) {
1721 		case TAS2563:
1722 		case TAS2781:
1723 		case TAS5825:
1724 		case TAS5827:
1725 			/* If DSP FW fail, DSP kcontrol won't be created. */
1726 			tasdevice_dsp_remove(tas_priv);
1727 		}
1728 	}
1729 	mutex_unlock(&tas_priv->codec_lock);
1730 	release_firmware(fmw);
1731 }
1732 
1733 static int tasdevice_dapm_event(struct snd_soc_dapm_widget *w,
1734 			struct snd_kcontrol *kcontrol, int event)
1735 {
1736 	struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
1737 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1738 	int state = 0;
1739 
1740 	/* Codec Lock Hold */
1741 	mutex_lock(&tas_priv->codec_lock);
1742 	if (event == SND_SOC_DAPM_PRE_PMD)
1743 		state = 1;
1744 	tasdevice_tuning_switch(tas_priv, state);
1745 	/* Codec Lock Release*/
1746 	mutex_unlock(&tas_priv->codec_lock);
1747 
1748 	return 0;
1749 }
1750 
1751 static const struct snd_soc_dapm_widget tasdevice_dapm_widgets[] = {
1752 	SND_SOC_DAPM_AIF_IN("ASI", "ASI Playback", 0, SND_SOC_NOPM, 0, 0),
1753 	SND_SOC_DAPM_AIF_OUT_E("ASI OUT", "ASI Capture", 0, SND_SOC_NOPM,
1754 		0, 0, tasdevice_dapm_event,
1755 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1756 	SND_SOC_DAPM_SPK("SPK", tasdevice_dapm_event),
1757 	SND_SOC_DAPM_OUTPUT("OUT"),
1758 	SND_SOC_DAPM_INPUT("DMIC"),
1759 };
1760 
1761 static const struct snd_soc_dapm_route tasdevice_audio_map[] = {
1762 	{"SPK", NULL, "ASI"},
1763 	{"OUT", NULL, "SPK"},
1764 	{"ASI OUT", NULL, "DMIC"},
1765 };
1766 
1767 static int tasdevice_startup(struct snd_pcm_substream *substream,
1768 						struct snd_soc_dai *dai)
1769 {
1770 	struct snd_soc_component *codec = dai->component;
1771 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1772 
1773 	switch (tas_priv->fw_state) {
1774 	case TASDEVICE_RCA_FW_OK:
1775 	case TASDEVICE_DSP_FW_ALL_OK:
1776 		return 0;
1777 	default:
1778 		return -EINVAL;
1779 	}
1780 }
1781 
1782 static int tasdevice_hw_params(struct snd_pcm_substream *substream,
1783 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
1784 {
1785 	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(dai);
1786 	unsigned int slot_width;
1787 	unsigned int fsrate;
1788 	int bclk_rate;
1789 
1790 	fsrate = params_rate(params);
1791 	switch (fsrate) {
1792 	case 48000:
1793 	case 44100:
1794 		break;
1795 	default:
1796 		dev_err(tas_priv->dev, "%s: incorrect sample rate = %u\n",
1797 			__func__, fsrate);
1798 		return -EINVAL;
1799 	}
1800 
1801 	slot_width = params_width(params);
1802 	switch (slot_width) {
1803 	case 16:
1804 	case 20:
1805 	case 24:
1806 	case 32:
1807 		break;
1808 	default:
1809 		dev_err(tas_priv->dev, "%s: incorrect slot width = %u\n",
1810 			__func__, slot_width);
1811 		return -EINVAL;
1812 	}
1813 
1814 	bclk_rate = snd_soc_params_to_bclk(params);
1815 	if (bclk_rate < 0) {
1816 		dev_err(tas_priv->dev, "%s: incorrect bclk rate = %d\n",
1817 			__func__, bclk_rate);
1818 		return bclk_rate;
1819 	}
1820 
1821 	return 0;
1822 }
1823 
1824 static int tasdevice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1825 	int clk_id, unsigned int freq, int dir)
1826 {
1827 	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(codec_dai);
1828 
1829 	tas_priv->sysclk = freq;
1830 
1831 	return 0;
1832 }
1833 
1834 static const struct snd_soc_dai_ops tasdevice_dai_ops = {
1835 	.startup = tasdevice_startup,
1836 	.hw_params = tasdevice_hw_params,
1837 	.set_sysclk = tasdevice_set_dai_sysclk,
1838 };
1839 
1840 static struct snd_soc_dai_driver tasdevice_dai_driver[] = {
1841 	{
1842 		.name = "tasdev_codec",
1843 		.id = 0,
1844 		.playback = {
1845 			.stream_name = "Playback",
1846 			.channels_min = 1,
1847 			.channels_max = 4,
1848 			.rates	 = TASDEVICE_RATES,
1849 			.formats	= TASDEVICE_FORMATS,
1850 		},
1851 		.capture = {
1852 			.stream_name = "Capture",
1853 			.channels_min = 1,
1854 			.channels_max = 4,
1855 			.rates	 = TASDEVICE_RATES,
1856 			.formats	= TASDEVICE_FORMATS,
1857 		},
1858 		.ops = &tasdevice_dai_ops,
1859 		.symmetric_rate = 1,
1860 	},
1861 };
1862 
1863 static int tasdevice_codec_probe(struct snd_soc_component *codec)
1864 {
1865 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1866 	struct snd_kcontrol_new *p;
1867 	unsigned int size;
1868 	int rc;
1869 
1870 	switch (tas_priv->chip_id) {
1871 	case TAS2020:
1872 	case TAS2118:
1873 	case TAS2120:
1874 	case TAS2320:
1875 	case TAS2570:
1876 	case TAS2572:
1877 		p = (struct snd_kcontrol_new *)tas2x20_snd_controls;
1878 		size = ARRAY_SIZE(tas2x20_snd_controls);
1879 		tas_priv->dvc_tlv_table = tas2x20_dvc_table;
1880 		break;
1881 	case TAS2781:
1882 		p = (struct snd_kcontrol_new *)tas2781_snd_controls;
1883 		size = ARRAY_SIZE(tas2781_snd_controls);
1884 		break;
1885 	case TAS5825:
1886 	case TAS5827:
1887 		p = (struct snd_kcontrol_new *)tas5825_snd_controls;
1888 		size = ARRAY_SIZE(tas5825_snd_controls);
1889 		break;
1890 	default:
1891 		p = (struct snd_kcontrol_new *)tas2563_snd_controls;
1892 		size = ARRAY_SIZE(tas2563_snd_controls);
1893 		tas_priv->dvc_tlv_table = tas2563_dvc_table;
1894 		break;
1895 	}
1896 
1897 	rc = snd_soc_add_component_controls(codec, p, size);
1898 	if (rc < 0) {
1899 		dev_err(tas_priv->dev, "%s: Add control err rc = %d",
1900 			__func__, rc);
1901 		return rc;
1902 	}
1903 
1904 	tas_priv->name_prefix = codec->name_prefix;
1905 	return tascodec_init(tas_priv, codec, THIS_MODULE, tasdevice_fw_ready);
1906 }
1907 
1908 static void tasdevice_deinit(void *context)
1909 {
1910 	struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;
1911 	struct tasdevice *tasdev = tas_priv->tasdevice;
1912 	int i;
1913 
1914 	for (i = 0; i < tas_priv->ndev; i++)
1915 		kfree(tasdev[i].cali_data_backup);
1916 
1917 	tasdevice_config_info_remove(tas_priv);
1918 	tasdevice_dsp_remove(tas_priv);
1919 	tasdevice_calbin_remove(tas_priv);
1920 	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
1921 }
1922 
1923 static void tasdevice_codec_remove(struct snd_soc_component *codec)
1924 {
1925 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1926 
1927 	tasdevice_deinit(tas_priv);
1928 }
1929 
1930 static const struct snd_soc_component_driver
1931 	soc_codec_driver_tasdevice = {
1932 	.probe			= tasdevice_codec_probe,
1933 	.remove			= tasdevice_codec_remove,
1934 	.dapm_widgets		= tasdevice_dapm_widgets,
1935 	.num_dapm_widgets	= ARRAY_SIZE(tasdevice_dapm_widgets),
1936 	.dapm_routes		= tasdevice_audio_map,
1937 	.num_dapm_routes	= ARRAY_SIZE(tasdevice_audio_map),
1938 	.idle_bias_on		= 1,
1939 	.endianness		= 1,
1940 };
1941 
1942 static void tasdevice_parse_dt(struct tasdevice_priv *tas_priv)
1943 {
1944 	struct i2c_client *client = (struct i2c_client *)tas_priv->client;
1945 	unsigned int dev_addrs[TASDEVICE_MAX_CHANNELS];
1946 	int i, ndev = 0;
1947 
1948 	if (tas_priv->isacpi) {
1949 		ndev = device_property_read_u32_array(&client->dev,
1950 			"ti,audio-slots", NULL, 0);
1951 		if (ndev <= 0) {
1952 			ndev = 1;
1953 			dev_addrs[0] = client->addr;
1954 		} else {
1955 			ndev = (ndev < ARRAY_SIZE(dev_addrs))
1956 				? ndev : ARRAY_SIZE(dev_addrs);
1957 			ndev = device_property_read_u32_array(&client->dev,
1958 				"ti,audio-slots", dev_addrs, ndev);
1959 		}
1960 
1961 		tas_priv->irq =
1962 			acpi_dev_gpio_irq_get(ACPI_COMPANION(&client->dev), 0);
1963 	} else if (IS_ENABLED(CONFIG_OF)) {
1964 		struct device_node *np = tas_priv->dev->of_node;
1965 		u64 addr;
1966 
1967 		for (i = 0; i < TASDEVICE_MAX_CHANNELS; i++) {
1968 			if (of_property_read_reg(np, i, &addr, NULL))
1969 				break;
1970 			dev_addrs[ndev++] = addr;
1971 		}
1972 
1973 		tas_priv->irq = of_irq_get(np, 0);
1974 	} else {
1975 		ndev = 1;
1976 		dev_addrs[0] = client->addr;
1977 	}
1978 	tas_priv->ndev = ndev;
1979 	for (i = 0; i < ndev; i++)
1980 		tas_priv->tasdevice[i].dev_addr = dev_addrs[i];
1981 
1982 	tas_priv->reset = devm_gpiod_get_optional(&client->dev,
1983 			"reset", GPIOD_OUT_HIGH);
1984 	if (IS_ERR(tas_priv->reset))
1985 		dev_err(tas_priv->dev, "%s Can't get reset GPIO\n",
1986 			__func__);
1987 
1988 	strcpy(tas_priv->dev_name, tasdevice_id[tas_priv->chip_id].name);
1989 }
1990 
1991 static int tasdevice_i2c_probe(struct i2c_client *i2c)
1992 {
1993 	const struct acpi_device_id *acpi_id;
1994 	struct tasdevice_priv *tas_priv;
1995 	int ret;
1996 
1997 	tas_priv = tasdevice_kzalloc(i2c);
1998 	if (!tas_priv)
1999 		return -ENOMEM;
2000 
2001 	dev_set_drvdata(&i2c->dev, tas_priv);
2002 
2003 	if (ACPI_HANDLE(&i2c->dev)) {
2004 		acpi_id = acpi_match_device(i2c->dev.driver->acpi_match_table,
2005 				&i2c->dev);
2006 		if (!acpi_id) {
2007 			dev_err(&i2c->dev, "No driver data\n");
2008 			ret = -EINVAL;
2009 			goto err;
2010 		}
2011 		tas_priv->chip_id = acpi_id->driver_data;
2012 		tas_priv->isacpi = true;
2013 	} else {
2014 		tas_priv->chip_id = (uintptr_t)i2c_get_match_data(i2c);
2015 		tas_priv->isacpi = false;
2016 	}
2017 
2018 	tasdevice_parse_dt(tas_priv);
2019 
2020 	ret = tasdevice_init(tas_priv);
2021 	if (ret)
2022 		goto err;
2023 
2024 	tasdevice_reset(tas_priv);
2025 
2026 	ret = devm_snd_soc_register_component(tas_priv->dev,
2027 		&soc_codec_driver_tasdevice,
2028 		tasdevice_dai_driver, ARRAY_SIZE(tasdevice_dai_driver));
2029 	if (ret) {
2030 		dev_err(tas_priv->dev, "%s: codec register error:0x%08x\n",
2031 			__func__, ret);
2032 		goto err;
2033 	}
2034 err:
2035 	if (ret < 0)
2036 		tasdevice_remove(tas_priv);
2037 	return ret;
2038 }
2039 
2040 static void tasdevice_i2c_remove(struct i2c_client *client)
2041 {
2042 	struct tasdevice_priv *tas_priv = i2c_get_clientdata(client);
2043 
2044 	tasdevice_remove(tas_priv);
2045 }
2046 
2047 #ifdef CONFIG_ACPI
2048 static const struct acpi_device_id tasdevice_acpi_match[] = {
2049 	{ "TXNW2020", TAS2020 },
2050 	{ "TXNW2118", TAS2118 },
2051 	{ "TXNW2120", TAS2120 },
2052 	{ "TXNW2320", TAS2320 },
2053 	{ "TXNW2563", TAS2563 },
2054 	{ "TXNW2570", TAS2570 },
2055 	{ "TXNW2572", TAS2572 },
2056 	{ "TXNW2781", TAS2781 },
2057 	{ "TXNW5825", TAS5825 },
2058 	{ "TXNW5827", TAS5827 },
2059 	{},
2060 };
2061 
2062 MODULE_DEVICE_TABLE(acpi, tasdevice_acpi_match);
2063 #endif
2064 
2065 static struct i2c_driver tasdevice_i2c_driver = {
2066 	.driver = {
2067 		.name = "tasdev-codec",
2068 		.of_match_table = of_match_ptr(tasdevice_of_match),
2069 #ifdef CONFIG_ACPI
2070 		.acpi_match_table = ACPI_PTR(tasdevice_acpi_match),
2071 #endif
2072 	},
2073 	.probe	= tasdevice_i2c_probe,
2074 	.remove = tasdevice_i2c_remove,
2075 	.id_table = tasdevice_id,
2076 };
2077 
2078 module_i2c_driver(tasdevice_i2c_driver);
2079 
2080 MODULE_AUTHOR("Shenghao Ding <shenghao-ding@ti.com>");
2081 MODULE_AUTHOR("Kevin Lu <kevin-lu@ti.com>");
2082 MODULE_DESCRIPTION("ASoC TAS2781 Driver");
2083 MODULE_LICENSE("GPL");
2084 MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");
2085