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