xref: /linux/sound/soc/codecs/tas2781-i2c.c (revision 0bebef817d4816a95a5d13a00210908f96596552)
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 void tas2573_calib_stop_put(struct tasdevice_priv *tas_priv)
617 {
618 	int cal_prof_id = tas_priv->rcabin.calibration_profile_id;
619 
620 	if (cal_prof_id >= 0)
621 		tasdevice_select_cfg_blk(tas_priv, cal_prof_id,
622 			TASDEVICE_BIN_BLK_PRE_SHUTDOWN);
623 }
624 
625 static int tas2573_calib_start_put(struct snd_kcontrol *kcontrol,
626 	struct snd_ctl_elem_value *ucontrol)
627 {
628 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
629 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
630 	int cal_prof_id = tas_priv->rcabin.calibration_profile_id;
631 	int cal_conf_id = tas_priv->fmw->calibration_config_id;
632 
633 	guard(mutex)(&tas_priv->codec_lock);
634 	if (tas_priv->chip_id != TAS2573) {
635 		WARN_ON_ONCE(1);
636 		return -ENODEV;
637 	}
638 
639 	if (cal_prof_id >= 0)
640 		tasdevice_select_cfg_blk(tas_priv, cal_prof_id,
641 			TASDEVICE_BIN_BLK_PRE_POWER_UP);
642 
643 	tasdevice_select_tuningprm_cfg(tas_priv, tas_priv->cur_prog,
644 		cal_conf_id, cal_prof_id);
645 
646 	return 1;
647 }
648 
649 static int tasdev_calib_stop_put(struct snd_kcontrol *kcontrol,
650 	struct snd_ctl_elem_value *ucontrol)
651 {
652 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
653 	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
654 	int i;
655 
656 	guard(mutex)(&priv->codec_lock);
657 
658 	switch (priv->chip_id) {
659 	case TAS2563:
660 		tas2563_calib_stop_put(priv);
661 		break;
662 	case TAS2573:
663 		tas2573_calib_stop_put(priv);
664 		break;
665 	case TAS2781:
666 		tas2781_calib_stop_put(priv);
667 		break;
668 	default:
669 		dev_err(priv->dev, "%s: Chip(%d) unsupports calibration\n",
670 			__func__, priv->chip_id);
671 		return -1;
672 	}
673 
674 	/*
675 	 * Set reloading-firmware flag after calibration, the flag will work
676 	 * during next playback, then set to the program id after reloading
677 	 * firmware.
678 	 */
679 	for (i = 0; i < priv->ndev; i++)
680 		priv->tasdevice[i].cur_prog = -1;
681 
682 	return 1;
683 }
684 
685 static int tasdev_cali_data_put(struct snd_kcontrol *kcontrol,
686 	struct snd_ctl_elem_value *ucontrol)
687 {
688 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
689 	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
690 	struct soc_bytes_ext *bytes_ext =
691 		(struct soc_bytes_ext *) kcontrol->private_value;
692 	struct calidata *cali_data = &priv->cali_data;
693 	struct cali_reg *p = &cali_data->cali_reg_array;
694 	unsigned char *src = ucontrol->value.bytes.data;
695 	unsigned char *dst = cali_data->data;
696 	int i = 0;
697 	int j;
698 
699 	guard(mutex)(&priv->codec_lock);
700 	if (src[0] != bytes_ext->max || src[1] != 'r') {
701 		dev_err(priv->dev, "%s: pkg fmt invalid\n", __func__);
702 		return 0;
703 	}
704 	for (j = 0; j < priv->ndev; j++) {
705 		if (src[17 + j * 21] != j) {
706 			dev_err(priv->dev, "%s: pkg fmt invalid\n", __func__);
707 			return 0;
708 		}
709 	}
710 	i += 2;
711 
712 	if (priv->dspbin_typ == TASDEV_BASIC) {
713 		p->r0_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
714 		i += 3;
715 		p->r0_low_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
716 		i += 3;
717 		p->invr0_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
718 		i += 3;
719 		p->pow_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
720 		i += 3;
721 		p->tlimit_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
722 		i += 3;
723 	} else {
724 		i += 15;
725 	}
726 
727 	memcpy(dst, &src[i], cali_data->total_sz);
728 	return 1;
729 }
730 
731 static int tas2781_latch_reg_get(struct snd_kcontrol *kcontrol,
732 	struct snd_ctl_elem_value *ucontrol)
733 {
734 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
735 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
736 	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;
737 	struct soc_bytes_ext *bytes_ext =
738 		(struct soc_bytes_ext *) kcontrol->private_value;
739 	struct tasdevice *tasdev = tas_priv->tasdevice;
740 	unsigned char *dst = ucontrol->value.bytes.data;
741 	int i, val, rc = -1;
742 
743 	dst[0] = bytes_ext->max;
744 	guard(mutex)(&tas_priv->codec_lock);
745 	for (i = 0; i < tas_priv->ndev; i++) {
746 		if (clt->addr == tasdev[i].dev_addr) {
747 			/* First byte is the device index. */
748 			dst[1] = i;
749 			rc = tasdevice_dev_read(tas_priv, i,
750 				TAS2781_RUNTIME_LATCH_RE_REG, &val);
751 			if (rc < 0)
752 				dev_err(tas_priv->dev, "%s, get value error\n",
753 					__func__);
754 			else
755 				dst[2] = val;
756 
757 			break;
758 		}
759 	}
760 
761 	return rc;
762 }
763 
764 static int tasdev_tf_data_get(struct snd_kcontrol *kcontrol,
765 	struct snd_ctl_elem_value *ucontrol)
766 {
767 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
768 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
769 	struct soc_bytes_ext *bytes_ext =
770 		(struct soc_bytes_ext *) kcontrol->private_value;
771 	unsigned char *dst = ucontrol->value.bytes.data;
772 	unsigned int reg = TAS2781_RUNTIME_RE_REG_TF;
773 
774 	if (tas_priv->chip_id == TAS2781) {
775 		struct tasdevice_fw *tas_fmw = tas_priv->fmw;
776 		struct fct_param_address *p = &(tas_fmw->fct_par_addr);
777 
778 		reg = TAS2781_RUNTIME_RE_REG_TF;
779 		if (tas_priv->dspbin_typ)
780 			reg = TASDEVICE_REG(p->tf_reg[0], p->tf_reg[1],
781 				p->tf_reg[2]);
782 	} else {
783 		reg = TAS2563_RUNTIME_RE_REG_TF;
784 	}
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_re_data_get(struct snd_kcontrol *kcontrol,
792 	struct snd_ctl_elem_value *ucontrol)
793 {
794 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
795 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
796 	struct soc_bytes_ext *bytes_ext =
797 		(struct soc_bytes_ext *) kcontrol->private_value;
798 	unsigned char *dst = ucontrol->value.bytes.data;
799 	unsigned int reg = TAS2781_RUNTIME_RE_REG;
800 
801 	if (tas_priv->chip_id == TAS2781) {
802 		struct tasdevice_fw *tas_fmw = tas_priv->fmw;
803 		struct fct_param_address *p = &(tas_fmw->fct_par_addr);
804 
805 		if (tas_priv->dspbin_typ)
806 			reg = TASDEVICE_REG(p->r0_reg[0], p->r0_reg[1],
807 				p->r0_reg[2]);
808 	} else {
809 		reg = TAS2563_RUNTIME_RE_REG;
810 	}
811 
812 	guard(mutex)(&tas_priv->codec_lock);
813 	dst[0] = bytes_ext->max;
814 	return calib_data_get(tas_priv, reg, &dst[1]);
815 }
816 
817 static int tasdev_r0_data_get(struct snd_kcontrol *kcontrol,
818 	struct snd_ctl_elem_value *ucontrol)
819 {
820 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
821 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
822 	struct calidata *cali_data = &tas_priv->cali_data;
823 	struct soc_bytes_ext *bytes_ext =
824 		(struct soc_bytes_ext *) kcontrol->private_value;
825 	unsigned char *dst = ucontrol->value.bytes.data;
826 	unsigned int reg;
827 
828 	guard(mutex)(&tas_priv->codec_lock);
829 
830 	if (tas_priv->chip_id == TAS2563)
831 		reg = TAS2563_PRM_R0_REG;
832 	else if (cali_data->cali_reg_array.r0_reg)
833 		reg = cali_data->cali_reg_array.r0_reg;
834 	else
835 		return -1;
836 	dst[0] = bytes_ext->max;
837 	return calib_data_get(tas_priv, reg, &dst[1]);
838 }
839 
840 static int tasdev_XMA1_data_get(struct snd_kcontrol *kcontrol,
841 	struct snd_ctl_elem_value *ucontrol)
842 {
843 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
844 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
845 	struct tasdevice_fw *tas_fmw = tas_priv->fmw;
846 	struct fct_param_address *p = &(tas_fmw->fct_par_addr);
847 	struct soc_bytes_ext *bytes_ext =
848 		(struct soc_bytes_ext *) kcontrol->private_value;
849 	unsigned char *dst = ucontrol->value.bytes.data;
850 	unsigned int reg = TASDEVICE_XM_A1_REG;
851 
852 	if (tas_priv->dspbin_typ)
853 		reg = TASDEVICE_REG(p->a1_reg[0], p->a1_reg[1], p->a1_reg[2]);
854 
855 	guard(mutex)(&tas_priv->codec_lock);
856 	dst[0] = bytes_ext->max;
857 	return calib_data_get(tas_priv, reg, &dst[1]);
858 }
859 
860 static int tasdev_XMA2_data_get(struct snd_kcontrol *kcontrol,
861 	struct snd_ctl_elem_value *ucontrol)
862 {
863 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
864 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
865 	struct tasdevice_fw *tas_fmw = tas_priv->fmw;
866 	struct fct_param_address *p = &(tas_fmw->fct_par_addr);
867 	struct soc_bytes_ext *bytes_ext =
868 		(struct soc_bytes_ext *) kcontrol->private_value;
869 	unsigned char *dst = ucontrol->value.bytes.data;
870 	unsigned int reg = TASDEVICE_XM_A2_REG;
871 
872 	if (tas_priv->dspbin_typ)
873 		reg = TASDEVICE_REG(p->a2_reg[0], p->a2_reg[1], p->a2_reg[2]);
874 
875 	guard(mutex)(&tas_priv->codec_lock);
876 	dst[0] = bytes_ext->max;
877 	return calib_data_get(tas_priv, reg, &dst[1]);
878 }
879 
880 static int tasdev_nop_get(
881 	struct snd_kcontrol *kcontrol,
882 	struct snd_ctl_elem_value *ucontrol)
883 {
884 	return 0;
885 }
886 
887 static int tasdevice_digital_gain_get(
888 	struct snd_kcontrol *kcontrol,
889 	struct snd_ctl_elem_value *ucontrol)
890 {
891 	struct soc_mixer_control *mc =
892 		(struct soc_mixer_control *)kcontrol->private_value;
893 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
894 	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);
895 	unsigned int l = 0, r = mc->max;
896 	unsigned int target, ar_mid, mid, ar_l, ar_r;
897 	unsigned int reg = mc->reg;
898 	unsigned char data[4];
899 	int ret;
900 
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 ret;
907 	}
908 
909 	target = get_unaligned_be32(&data[0]);
910 
911 	while (r > 1 + l) {
912 		mid = (l + r) / 2;
913 		ar_mid = get_unaligned_be32(tas_dev->dvc_tlv_table[mid]);
914 		if (target < ar_mid)
915 			r = mid;
916 		else
917 			l = mid;
918 	}
919 
920 	ar_l = get_unaligned_be32(tas_dev->dvc_tlv_table[l]);
921 	ar_r = get_unaligned_be32(tas_dev->dvc_tlv_table[r]);
922 
923 	/* find out the member same as or closer to the current volume */
924 	ucontrol->value.integer.value[0] =
925 		abs(target - ar_l) <= abs(target - ar_r) ? l : r;
926 
927 	return 0;
928 }
929 
930 static int tasdevice_digital_gain_put(
931 	struct snd_kcontrol *kcontrol,
932 	struct snd_ctl_elem_value *ucontrol)
933 {
934 	struct soc_mixer_control *mc =
935 		(struct soc_mixer_control *)kcontrol->private_value;
936 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
937 	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);
938 	int vol = ucontrol->value.integer.value[0];
939 	int status = 0, max = mc->max;
940 	int i, ret;
941 	unsigned int reg = mc->reg;
942 	unsigned int volrd, volwr;
943 	unsigned char data[4];
944 
945 	vol = clamp(vol, 0, max);
946 	guard(mutex)(&tas_dev->codec_lock);
947 	/* Read the primary device */
948 	ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4);
949 	if (ret) {
950 		dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__);
951 		return -1;
952 	}
953 
954 	volrd = get_unaligned_be32(&data[0]);
955 	volwr = get_unaligned_be32(tas_dev->dvc_tlv_table[vol]);
956 
957 	if (volrd == volwr)
958 		return 0;
959 
960 	for (i = 0; i < tas_dev->ndev; i++) {
961 		ret = tasdevice_dev_bulk_write(tas_dev, i, reg,
962 			(unsigned char *)tas_dev->dvc_tlv_table[vol], 4);
963 		if (ret) {
964 			dev_err(tas_dev->dev,
965 				"%s, set digital vol error in dev %d\n",
966 				__func__, i);
967 			status |= BIT(i);
968 		}
969 	}
970 
971 	if (status)
972 		return -1;
973 
974 	return 1;
975 }
976 
977 static const struct snd_kcontrol_new tasdevice_cali_controls[] = {
978 	SOC_SINGLE_EXT("Calibration Stop", SND_SOC_NOPM, 0, 1, 0,
979 		tasdev_nop_get, tasdev_calib_stop_put),
980 	SND_SOC_BYTES_EXT("Amp TF Data", 6, tasdev_tf_data_get, NULL),
981 	SND_SOC_BYTES_EXT("Amp RE Data", 6, tasdev_re_data_get, NULL),
982 	SND_SOC_BYTES_EXT("Amp R0 Data", 6, tasdev_r0_data_get, NULL),
983 	SND_SOC_BYTES_EXT("Amp XMA1 Data", 6, tasdev_XMA1_data_get, NULL),
984 	SND_SOC_BYTES_EXT("Amp XMA2 Data", 6, tasdev_XMA2_data_get, NULL),
985 };
986 
987 static const struct snd_kcontrol_new tas2x20_snd_controls[] = {
988 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS2X20_AMP_LEVEL,
989 		0, 0, 42, 1, tas2781_amp_getvol,
990 		tas2781_amp_putvol, tas2x20_amp_tlv),
991 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2X20_DVC_LEVEL,
992 		0, 0, ARRAY_SIZE(tas2x20_dvc_table) - 1, 0,
993 		tasdevice_digital_gain_get, tasdevice_digital_gain_put,
994 		tas2x20_dvc_tlv),
995 };
996 
997 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
998 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS2781_AMP_LEVEL,
999 		1, 0, 20, 0, tas2781_amp_getvol,
1000 		tas2781_amp_putvol, tas2781_amp_tlv),
1001 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2781_DVC_LVL,
1002 		0, 0, 200, 1, tas2781_digital_getvol,
1003 		tas2781_digital_putvol, tas2781_dvc_tlv),
1004 };
1005 
1006 static const struct snd_kcontrol_new tas5825_snd_controls[] = {
1007 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS5825_AMP_LEVEL,
1008 		0, 0, 31, 1, tas2781_amp_getvol,
1009 		tas2781_amp_putvol, tas5825_amp_tlv),
1010 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS5825_DVC_LEVEL,
1011 		0, 0, 254, 1, tas2781_amp_getvol,
1012 		tas2781_amp_putvol, tas5825_dvc_tlv),
1013 };
1014 
1015 static const struct snd_kcontrol_new tas2781_cali_controls[] = {
1016 	SND_SOC_BYTES_EXT("Amp Latch Data", 3, tas2781_latch_reg_get, NULL),
1017 };
1018 
1019 static const struct snd_kcontrol_new tas2563_snd_controls[] = {
1020 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2563_DVC_LVL, 0,
1021 		0, ARRAY_SIZE(tas2563_dvc_table) - 1, 0,
1022 		tasdevice_digital_gain_get, tasdevice_digital_gain_put,
1023 		tas2563_dvc_tlv),
1024 };
1025 
1026 static const struct snd_kcontrol_new tas2563_cali_controls[] = {
1027 	SOC_SINGLE_EXT("Calibration Start", SND_SOC_NOPM, 0, 1, 0,
1028 		tasdev_nop_get, tas2563_calib_start_put),
1029 };
1030 
1031 static const struct snd_kcontrol_new tas2573_cali_controls[] = {
1032 	SOC_SINGLE_EXT("Calibration Start", SND_SOC_NOPM, 0, 1, 0,
1033 		tasdev_nop_get, tas2573_calib_start_put),
1034 	SOC_SINGLE_EXT("Calibration Stop", SND_SOC_NOPM, 0, 1, 0,
1035 		tasdev_nop_get, tasdev_calib_stop_put),
1036 };
1037 
1038 static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
1039 		struct snd_ctl_elem_value *ucontrol)
1040 {
1041 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1042 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1043 	int ret = 0;
1044 
1045 	if (tas_priv->rcabin.profile_cfg_id !=
1046 		ucontrol->value.integer.value[0]) {
1047 		tas_priv->rcabin.profile_cfg_id =
1048 			ucontrol->value.integer.value[0];
1049 		ret = 1;
1050 	}
1051 
1052 	return ret;
1053 }
1054 
1055 /**
1056  * tasdevice_set_capture_profile_id - Set runtime capture profile index via
1057  * ALSA control
1058  * @kcontrol: ALSA kcontrol handle that triggers this operation
1059  * @ucontrol: User space control value carrying the new profile index
1060  *
1061  * This mixer control handler validates the user-provided capture profile ID
1062  * against the maximum valid index parsed from the loaded DSP firmware,
1063  * then updates the runtime stored capture profile ID only if the new value
1064  * differs from the current active one. It will immediately return -EINVAL
1065  * if the submitted profile ID falls outside the valid range, including the
1066  * edge case that no valid configuration blocks are detected in firmware.
1067  *
1068  * No actual DSP register write is performed in this handler. The updated
1069  * profile ID will be applied to the hardware when the next ALSA capture
1070  * stream starts up. Caller does not need to take extra codec lock here,
1071  * as the ALSA control core already guarantees serialized execution.
1072  *
1073  * Return: 1 if profile ID value was changed, 0 if no modification needed,
1074  *	   -EINVAL if the input profile ID is out of valid range
1075  */
1076 static int tasdevice_set_capture_profile_id(struct snd_kcontrol *kcontrol,
1077 		struct snd_ctl_elem_value *ucontrol)
1078 {
1079 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1080 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1081 	unsigned int user_prof_id = ucontrol->value.integer.value[0];
1082 	unsigned int max_valid_id;
1083 	int ret = 0;
1084 
1085 	/*
1086 	 * Align valid range with the bound defined in
1087 	 * tasdevice_info_profile()
1088 	 */
1089 	max_valid_id = tas_priv->rcabin.ncfgs - 1;
1090 
1091 	/*
1092 	 * Reject invalid input including zero total configuration edge
1093 	 * case
1094 	 */
1095 	if (tas_priv->rcabin.ncfgs == 0 || user_prof_id > max_valid_id)
1096 		return -EINVAL;
1097 
1098 	if (tas_priv->rcabin.capture_profile_id != user_prof_id) {
1099 		tas_priv->rcabin.capture_profile_id = user_prof_id;
1100 		ret = 1;
1101 	}
1102 
1103 	return ret;
1104 }
1105 
1106 static int tasdevice_info_active_num(struct snd_kcontrol *kcontrol,
1107 			struct snd_ctl_elem_info *uinfo)
1108 {
1109 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1110 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1111 
1112 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1113 	uinfo->count = 1;
1114 	uinfo->value.integer.min = 0;
1115 	uinfo->value.integer.max = tas_priv->ndev - 1;
1116 
1117 	return 0;
1118 }
1119 
1120 static int tasdevice_info_chip_id(struct snd_kcontrol *kcontrol,
1121 			struct snd_ctl_elem_info *uinfo)
1122 {
1123 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1124 	uinfo->count = 1;
1125 	uinfo->value.integer.min = TAS2020;
1126 	uinfo->value.integer.max = TAS_OTHERS;
1127 
1128 	return 0;
1129 }
1130 
1131 static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
1132 			struct snd_ctl_elem_info *uinfo)
1133 {
1134 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1135 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1136 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
1137 
1138 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1139 	uinfo->count = 1;
1140 	uinfo->value.integer.min = 0;
1141 	uinfo->value.integer.max = (int)tas_fw->nr_programs;
1142 
1143 	return 0;
1144 }
1145 
1146 static int tasdevice_info_configurations(
1147 	struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
1148 {
1149 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1150 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1151 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
1152 
1153 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1154 	uinfo->count = 1;
1155 	uinfo->value.integer.min = 0;
1156 	uinfo->value.integer.max = (int)tas_fw->nr_configurations - 1;
1157 
1158 	return 0;
1159 }
1160 
1161 static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
1162 			struct snd_ctl_elem_info *uinfo)
1163 {
1164 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1165 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1166 
1167 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1168 	uinfo->count = 1;
1169 	uinfo->value.integer.min = 0;
1170 	uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
1171 
1172 	return 0;
1173 }
1174 
1175 static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
1176 			struct snd_ctl_elem_value *ucontrol)
1177 {
1178 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1179 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1180 
1181 	ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
1182 
1183 	return 0;
1184 }
1185 
1186 /**
1187  * tasdevice_get_capture_profile_id - Report current active capture profile
1188  * ID to user space
1189  * @kcontrol: ALSA kcontrol structure passed from ALSA core
1190  * @ucontrol: User-space control element value buffer to write the result back
1191  *
1192  * This function ensures the returned profile ID is always clamped inside the
1193  * valid range advertised by the info callback, preventing accidental invalid
1194  * values from being exposed to applications even if internal driver state is
1195  * temporarily inconsistent.
1196  *
1197  * Returns 0 on successful fill of the control value, no error conditions
1198  * are defined for this getter callback.
1199  */
1200 static int tasdevice_get_capture_profile_id(struct snd_kcontrol *kcontrol,
1201 			struct snd_ctl_elem_value *ucontrol)
1202 {
1203 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1204 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1205 	unsigned int max_valid_id, current_prof_id;
1206 
1207 	max_valid_id = tas_priv->rcabin.ncfgs > 0 ?
1208 		(tas_priv->rcabin.ncfgs - 1U) : 0;
1209 
1210 	/*
1211 	 * Cast current profile id to unsigned to match type with max_valid_id,
1212 	 * avoid signedness mismatch;
1213 	 */
1214 	current_prof_id = (unsigned int)tas_priv->rcabin.capture_profile_id;
1215 	/* Prevent underflow when there are no loaded capture profiles. */
1216 	ucontrol->value.integer.value[0] = min(current_prof_id, max_valid_id);
1217 
1218 	return 0;
1219 }
1220 
1221 static int tasdevice_get_chip_id(struct snd_kcontrol *kcontrol,
1222 			struct snd_ctl_elem_value *ucontrol)
1223 {
1224 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1225 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1226 
1227 	ucontrol->value.integer.value[0] = tas_priv->chip_id;
1228 
1229 	return 0;
1230 }
1231 
1232 static int tasdevice_create_control(struct tasdevice_priv *tas_priv)
1233 {
1234 	struct snd_kcontrol_new *prof_ctrls;
1235 	int nr_controls = 1;
1236 	int mix_index = 0;
1237 	int ret;
1238 	char *name;
1239 
1240 	prof_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
1241 		sizeof(prof_ctrls[0]), GFP_KERNEL);
1242 	if (!prof_ctrls) {
1243 		ret = -ENOMEM;
1244 		goto out;
1245 	}
1246 
1247 	/* Create a mixer item for selecting the active profile */
1248 	name = devm_kstrdup(tas_priv->dev, "Speaker Profile Id", GFP_KERNEL);
1249 	if (!name) {
1250 		ret = -ENOMEM;
1251 		goto out;
1252 	}
1253 	prof_ctrls[mix_index].name = name;
1254 	prof_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1255 	prof_ctrls[mix_index].info = tasdevice_info_profile;
1256 	prof_ctrls[mix_index].get = tasdevice_get_profile_id;
1257 	prof_ctrls[mix_index].put = tasdevice_set_profile_id;
1258 	mix_index++;
1259 
1260 	ret = snd_soc_add_component_controls(tas_priv->codec,
1261 		prof_ctrls, nr_controls < mix_index ? nr_controls : mix_index);
1262 
1263 	mix_index = 0;
1264 	switch (tas_priv->chip_id) {
1265 	case TAS2563:
1266 	case TAS2568:
1267 	case TAS2570:
1268 	case TAS2572:
1269 	case TAS2573:
1270 	case TAS2574:
1271 	case TAS2781:
1272 	prof_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
1273 		sizeof(prof_ctrls[0]), GFP_KERNEL);
1274 	if (!prof_ctrls) {
1275 		ret = -ENOMEM;
1276 		goto out;
1277 	}
1278 
1279 	/* Create a mixer item for selecting the capture profile */
1280 	name = devm_kstrdup(tas_priv->dev, "Speaker Capture Profile Id",
1281 		GFP_KERNEL);
1282 	if (!name) {
1283 		ret = -ENOMEM;
1284 		goto out;
1285 	}
1286 	prof_ctrls[mix_index].name = name;
1287 	prof_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1288 	prof_ctrls[mix_index].info = tasdevice_info_profile;
1289 	prof_ctrls[mix_index].get = tasdevice_get_capture_profile_id;
1290 	prof_ctrls[mix_index].put = tasdevice_set_capture_profile_id;
1291 	mix_index++;
1292 
1293 	ret = snd_soc_add_component_controls(tas_priv->codec,
1294 		prof_ctrls, nr_controls < mix_index ? nr_controls : mix_index);
1295 		break;
1296 	}
1297 
1298 out:
1299 	return ret;
1300 }
1301 
1302 static int tasdevice_program_get(struct snd_kcontrol *kcontrol,
1303 	struct snd_ctl_elem_value *ucontrol)
1304 {
1305 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1306 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1307 
1308 	ucontrol->value.integer.value[0] = tas_priv->cur_prog;
1309 
1310 	return 0;
1311 }
1312 
1313 static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
1314 	struct snd_ctl_elem_value *ucontrol)
1315 {
1316 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1317 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1318 	unsigned int nr_program = ucontrol->value.integer.value[0];
1319 	int ret = 0;
1320 
1321 	if (tas_priv->cur_prog != nr_program) {
1322 		tas_priv->cur_prog = nr_program;
1323 		ret = 1;
1324 	}
1325 
1326 	return ret;
1327 }
1328 
1329 static int tasdevice_configuration_get(struct snd_kcontrol *kcontrol,
1330 	struct snd_ctl_elem_value *ucontrol)
1331 {
1332 
1333 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1334 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1335 
1336 	ucontrol->value.integer.value[0] = tas_priv->cur_conf;
1337 
1338 	return 0;
1339 }
1340 
1341 static int tasdevice_configuration_put(
1342 	struct snd_kcontrol *kcontrol,
1343 	struct snd_ctl_elem_value *ucontrol)
1344 {
1345 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1346 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1347 	unsigned int nr_configuration = ucontrol->value.integer.value[0];
1348 	int ret = 0;
1349 
1350 	if (tas_priv->cur_conf != nr_configuration) {
1351 		tas_priv->cur_conf = nr_configuration;
1352 		ret = 1;
1353 	}
1354 
1355 	return ret;
1356 }
1357 
1358 static int tasdevice_active_num_get(struct snd_kcontrol *kcontrol,
1359 	struct snd_ctl_elem_value *ucontrol)
1360 {
1361 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1362 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1363 	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;
1364 	struct tasdevice *tasdev = tas_priv->tasdevice;
1365 	int i;
1366 
1367 	for (i = 0; i < tas_priv->ndev; i++) {
1368 		if (clt->addr == tasdev[i].dev_addr) {
1369 			ucontrol->value.integer.value[0] = i;
1370 			return 0;
1371 		}
1372 	}
1373 
1374 	return -1;
1375 }
1376 
1377 static int tasdevice_active_num_put(struct snd_kcontrol *kcontrol,
1378 	struct snd_ctl_elem_value *ucontrol)
1379 {
1380 	struct snd_soc_component *codec = snd_kcontrol_chip(kcontrol);
1381 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1382 	int dev_id = ucontrol->value.integer.value[0];
1383 	int max = tas_priv->ndev - 1;
1384 
1385 	dev_id = clamp(dev_id, 0, max);
1386 
1387 	guard(mutex)(&tas_priv->codec_lock);
1388 	return tasdev_chn_switch(tas_priv, dev_id);
1389 }
1390 
1391 static int tasdevice_dsp_create_ctrls(struct tasdevice_priv *tas_priv)
1392 {
1393 	struct snd_kcontrol_new *dsp_ctrls;
1394 	char *active_dev_num, *chip_id, *fw_load;
1395 	char *conf_name, *prog_name;
1396 	int nr_controls = 5;
1397 	int mix_index = 0;
1398 
1399 	/* Alloc kcontrol via devm_kzalloc, which don't manually
1400 	 * free the kcontrol
1401 	 */
1402 	dsp_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
1403 		sizeof(dsp_ctrls[0]), GFP_KERNEL);
1404 	if (!dsp_ctrls)
1405 		return -ENOMEM;
1406 
1407 	/* Create mixer items for selecting the active Program and Config */
1408 	prog_name = devm_kstrdup(tas_priv->dev, "Speaker Program Id",
1409 		GFP_KERNEL);
1410 	if (!prog_name)
1411 		return -ENOMEM;
1412 
1413 	dsp_ctrls[mix_index].name = prog_name;
1414 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1415 	dsp_ctrls[mix_index].info = tasdevice_info_programs;
1416 	dsp_ctrls[mix_index].get = tasdevice_program_get;
1417 	dsp_ctrls[mix_index].put = tasdevice_program_put;
1418 	mix_index++;
1419 
1420 	conf_name = devm_kstrdup(tas_priv->dev, "Speaker Config Id",
1421 		GFP_KERNEL);
1422 	if (!conf_name)
1423 		return -ENOMEM;
1424 
1425 	dsp_ctrls[mix_index].name = conf_name;
1426 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1427 	dsp_ctrls[mix_index].info = tasdevice_info_configurations;
1428 	dsp_ctrls[mix_index].get = tasdevice_configuration_get;
1429 	dsp_ctrls[mix_index].put = tasdevice_configuration_put;
1430 	mix_index++;
1431 
1432 	active_dev_num = devm_kstrdup(tas_priv->dev, "Activate Tasdevice Num",
1433 		GFP_KERNEL);
1434 	if (!active_dev_num)
1435 		return -ENOMEM;
1436 
1437 	dsp_ctrls[mix_index].name = active_dev_num;
1438 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1439 	dsp_ctrls[mix_index].info = tasdevice_info_active_num;
1440 	dsp_ctrls[mix_index].get = tasdevice_active_num_get;
1441 	dsp_ctrls[mix_index].put = tasdevice_active_num_put;
1442 	mix_index++;
1443 
1444 	chip_id = devm_kstrdup(tas_priv->dev, "Tasdevice Chip Id", GFP_KERNEL);
1445 	if (!chip_id)
1446 		return -ENOMEM;
1447 
1448 	dsp_ctrls[mix_index].name = chip_id;
1449 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1450 	dsp_ctrls[mix_index].info = tasdevice_info_chip_id;
1451 	dsp_ctrls[mix_index].get = tasdevice_get_chip_id;
1452 	mix_index++;
1453 
1454 	fw_load = devm_kstrdup(tas_priv->dev, "Speaker Force Firmware Load",
1455 		GFP_KERNEL);
1456 	if (!fw_load)
1457 		return -ENOMEM;
1458 
1459 	dsp_ctrls[mix_index].name = fw_load;
1460 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1461 	dsp_ctrls[mix_index].info = snd_soc_info_bool_ext;
1462 	dsp_ctrls[mix_index].put = tasdev_force_fwload_put;
1463 	dsp_ctrls[mix_index].get = tasdev_force_fwload_get;
1464 	dsp_ctrls[mix_index].private_value = 0UL;
1465 	mix_index++;
1466 
1467 	return snd_soc_add_component_controls(tas_priv->codec, dsp_ctrls,
1468 		nr_controls < mix_index ? nr_controls : mix_index);
1469 }
1470 
1471 static void cali_reg_update(struct bulk_reg_val *p,
1472 	struct fct_param_address *t)
1473 {
1474 	const int sum = ARRAY_SIZE(tas2781_cali_start_reg);
1475 	int reg, j;
1476 
1477 	for (j = 0; j < sum; j++) {
1478 		switch (tas2781_cali_start_reg[j].reg) {
1479 		case 0:
1480 			reg = TASDEVICE_REG(t->thr[0], t->thr[1], t->thr[2]);
1481 			break;
1482 		case TAS2781_PRM_PLT_FLAG_REG:
1483 			reg = TASDEVICE_REG(t->plt_flg[0], t->plt_flg[1],
1484 				t->plt_flg[2]);
1485 			break;
1486 		case TAS2781_PRM_SINEGAIN_REG:
1487 			reg = TASDEVICE_REG(t->sin_gn[0], t->sin_gn[1],
1488 				t->sin_gn[2]);
1489 			break;
1490 		case TAS2781_PRM_SINEGAIN2_REG:
1491 			reg = TASDEVICE_REG(t->sin_gn2[0], t->sin_gn2[1],
1492 				t->sin_gn2[2]);
1493 			break;
1494 		default:
1495 			reg = 0;
1496 			break;
1497 		}
1498 		if (reg)
1499 			p[j].reg = reg;
1500 	}
1501 }
1502 
1503 static void alpa_cali_update(struct bulk_reg_val *p,
1504 	struct fct_param_address *t)
1505 {
1506 	p->is_locked = false;
1507 	p->reg = TASDEVICE_REG(t->thr2[0], t->thr2[1], t->thr2[2]);
1508 	p->val_len = 4;
1509 }
1510 
1511 static int create_tas2781_cali_start_ktrl(struct tasdevice_priv
1512 	*priv, struct snd_kcontrol_new *cali_ctrl)
1513 {
1514 	struct soc_bytes_ext *ext_cali_start;
1515 	char *cali_start_name;
1516 
1517 	ext_cali_start = devm_kzalloc(priv->dev,
1518 		sizeof(*ext_cali_start), GFP_KERNEL);
1519 	if (!ext_cali_start)
1520 		return -ENOMEM;
1521 
1522 	cali_start_name = devm_kstrdup(priv->dev,
1523 		"Calibration Start", GFP_KERNEL);
1524 	if (!cali_start_name)
1525 		return -ENOMEM;
1526 	/*
1527 	 * package structure for tas2781 ftc start:
1528 	 *	Pkg len (1 byte)
1529 	 *	Reg id (1 byte, constant 'r')
1530 	 *	book, page, register for pilot threshold, pilot tone
1531 	 *		and sine gain (12 bytes)
1532 	 *	for (i = 0; i < Device-Sum; i++) {
1533 	 *		Device #i index_info (1 byte)
1534 	 *		Sine gain for Device #i (8 bytes)
1535 	 *	}
1536 	 */
1537 	ext_cali_start->max = 14 + priv->ndev * 9;
1538 	cali_ctrl->name = cali_start_name;
1539 	cali_ctrl->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1540 	cali_ctrl->info = snd_soc_bytes_info_ext;
1541 	cali_ctrl->put = tas2781_calib_start_put;
1542 	cali_ctrl->get = tasdev_nop_get;
1543 	cali_ctrl->private_value = (unsigned long)ext_cali_start;
1544 
1545 	return 0;
1546 }
1547 
1548 static int tas2573_re_get(struct snd_kcontrol *kcontrol,
1549 	struct snd_ctl_elem_value *ucontrol)
1550 {
1551 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
1552 	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
1553 	unsigned char *dst = ucontrol->value.bytes.data;
1554 	struct soc_bytes_ext *bytes_ext =
1555 		(struct soc_bytes_ext *) kcontrol->private_value;
1556 	unsigned int i = 0;
1557 	unsigned int j;
1558 	int rc;
1559 
1560 	guard(mutex)(&priv->codec_lock);
1561 	dst[i++] = bytes_ext->max;
1562 	dst[i++] = 'r';
1563 
1564 	dst[i++] = TASDEVICE_BOOK_ID(TAS2573_RE_OUT);
1565 	dst[i++] = TASDEVICE_PAGE_ID(TAS2573_RE_OUT);
1566 	dst[i++] = TASDEVICE_PAGE_REG(TAS2573_RE_OUT);
1567 
1568 	dst[i++] = 'D';
1569 
1570 	for (j = 0; j < priv->ndev; j++) {
1571 		dst[i++] = j;
1572 
1573 		rc = tasdevice_dev_bulk_read(priv, j, TAS2573_RE_OUT,
1574 			&dst[i], 4);
1575 		if (rc < 0)
1576 			dev_err(priv->dev, "chn %d bulk_rd err = %d\n", j, rc);
1577 
1578 		i += 4;
1579 	}
1580 
1581 	return 0;
1582 }
1583 
1584 static int tas2573_calib_status_get(struct snd_kcontrol *kcontrol,
1585 	struct snd_ctl_elem_value *ucontrol)
1586 {
1587 	struct snd_soc_component *comp = snd_kcontrol_chip(kcontrol);
1588 	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
1589 	unsigned char *dst = ucontrol->value.bytes.data;
1590 	struct soc_bytes_ext *bytes_ext =
1591 		(struct soc_bytes_ext *) kcontrol->private_value;
1592 	unsigned int fct_status_regs[] = {
1593 		TAS2573_FCT_INT_LATCH,
1594 		TAS2573_FCT_STATUS_CTRL,
1595 		TAS2573_FCT_STATUS_BINNING,
1596 		TAS2573_SILENCE_DETECTED,
1597 		TAS2573_OPEN_CIRCUIT,
1598 		TAS2573_SHORTCKT,
1599 		TAS2573_FCT_OUTPUT_R0,
1600 		TAS2573_FCT_OUTPUT_R0_LOW,
1601 		TAS2573_FCT_OUTPUT_INV_R0,
1602 		TAS2573_FCT_OUTPUT_POWERTOT,
1603 		TAS2573_FCT_OUTPUT_F0,
1604 		TAS2573_RE_OUT,
1605 	};
1606 	unsigned int j, k, val;
1607 	unsigned int i = 0;
1608 	int rc;
1609 
1610 	guard(mutex)(&priv->codec_lock);
1611 	dst[i++] = bytes_ext->max;
1612 	dst[i++] = 'r';
1613 
1614 	for (j = 0; j < ARRAY_SIZE(fct_status_regs); j++) {
1615 		dst[i++] = TASDEVICE_BOOK_ID(fct_status_regs[j]);
1616 		dst[i++] = TASDEVICE_PAGE_ID(fct_status_regs[j]);
1617 		dst[i++] = TASDEVICE_PAGE_REG(fct_status_regs[j]);
1618 	}
1619 	dst[i++] = 'D';
1620 
1621 	for (j = 0; j < priv->ndev; j++) {
1622 		dst[i++] = j;
1623 		dst[i++] = 0;
1624 		dst[i++] = 0;
1625 		dst[i++] = 0;
1626 
1627 		rc = tasdevice_dev_read(priv, j, fct_status_regs[0], &val);
1628 		if (rc < 0)
1629 			dev_err(priv->dev,
1630 				"chn %d fct_status_regs[0] rd err = %d\n",
1631 				j, rc);
1632 		else
1633 			dst[i++] = val;
1634 
1635 		for (k = 1; k < ARRAY_SIZE(fct_status_regs); k++, i += 4) {
1636 			rc = tasdevice_dev_bulk_read(priv, j,
1637 				fct_status_regs[k], &dst[i], 4);
1638 			if (rc < 0) {
1639 				dev_err(priv->dev,
1640 					"chn %d regs[%u] bulk_rd err = %d\n",
1641 					j, k, rc);
1642 			}
1643 		}
1644 
1645 	}
1646 
1647 	return 0;
1648 }
1649 
1650 static int create_tas2573_cali_status_ktrl(struct tasdevice_priv
1651 	*priv, struct snd_kcontrol_new *cali_ctrl)
1652 {
1653 	struct soc_bytes_ext *ext_cali_start;
1654 	char *cali_start_name;
1655 
1656 	ext_cali_start = devm_kzalloc(priv->dev,
1657 		sizeof(*ext_cali_start), GFP_KERNEL);
1658 	if (!ext_cali_start)
1659 		return -ENOMEM;
1660 
1661 	cali_start_name = devm_kstrdup(priv->dev,
1662 		"Calibration Status", GFP_KERNEL);
1663 	if (!cali_start_name)
1664 		return -ENOMEM;
1665 	/*
1666 	 * package structure for tas2573 fct status:
1667 	 *	Pkg len (1 byte)
1668 	 *	Reg id (1 byte, constant 'r')
1669 	 *	book, page, register for fct status (total 36 bytes)
1670 	 *	Data Start Flag (1 byte, constant 'D')
1671 	 *	for (i = 0; i < Device-Sum; i++) {
1672 	 *		Device #i index_info (1 byte)
1673 	 *		Sine gain for Device #i (48 bytes)
1674 	 *	}
1675 	 */
1676 	ext_cali_start->max = 39 + priv->ndev * 49;
1677 	cali_ctrl->name = cali_start_name;
1678 	cali_ctrl->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1679 	cali_ctrl->info = snd_soc_bytes_info_ext;
1680 	cali_ctrl->put = NULL;
1681 	cali_ctrl->get = tas2573_calib_status_get;
1682 	cali_ctrl->private_value = (unsigned long)ext_cali_start;
1683 
1684 	return 0;
1685 }
1686 
1687 static int create_tas2573_cali_re_ktrl(struct tasdevice_priv
1688 	*priv, struct snd_kcontrol_new *cali_ctrl)
1689 {
1690 	struct soc_bytes_ext *ext_cali_start;
1691 	char *cali_start_name;
1692 
1693 	ext_cali_start = devm_kzalloc(priv->dev,
1694 		sizeof(*ext_cali_start), GFP_KERNEL);
1695 	if (!ext_cali_start)
1696 		return -ENOMEM;
1697 
1698 	cali_start_name = devm_kstrdup(priv->dev,
1699 		"Real-time Speaker Impedance", GFP_KERNEL);
1700 	if (!cali_start_name)
1701 		return -ENOMEM;
1702 	/*
1703 	 * package structure for tas2573 real-time spk impedance:
1704 	 *	Pkg len (1 byte)
1705 	 *	Reg id (1 byte, constant 'r')
1706 	 *	book, page, register for fct status (total 3 bytes)
1707 	 *	Data Start Flag (1 byte, constant 'D')
1708 	 *	for (i = 0; i < Device-Sum; i++) {
1709 	 *		Device #i index_info (1 byte)
1710 	 *		Sine gain for Device #i (4 bytes)
1711 	 *	}
1712 	 */
1713 	ext_cali_start->max = 6 + priv->ndev * 5;
1714 	cali_ctrl->name = cali_start_name;
1715 	cali_ctrl->iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1716 	cali_ctrl->info = snd_soc_bytes_info_ext;
1717 	cali_ctrl->put = NULL;
1718 	cali_ctrl->get = tas2573_re_get;
1719 	cali_ctrl->private_value = (unsigned long)ext_cali_start;
1720 
1721 	return 0;
1722 }
1723 
1724 static int tasdevice_create_cali_ctrls(struct tasdevice_priv *priv)
1725 {
1726 	struct calidata *cali_data = &priv->cali_data;
1727 	struct tasdevice *tasdev = priv->tasdevice;
1728 	struct tasdevice_fw *fmw = priv->fmw;
1729 	struct soc_bytes_ext *ext_cali_data;
1730 	struct snd_kcontrol_new *cali_ctrls;
1731 	unsigned int nctrls;
1732 	char *cali_name;
1733 	int rc, i;
1734 
1735 	switch (priv->chip_id) {
1736 	case TAS2563: {
1737 		rc = snd_soc_add_component_controls(priv->codec,
1738 			tasdevice_cali_controls,
1739 			ARRAY_SIZE(tasdevice_cali_controls));
1740 		if (rc < 0) {
1741 			dev_err(priv->dev, "%s: Add cali controls err rc = %d",
1742 				__func__, rc);
1743 			return rc;
1744 		}
1745 		cali_ctrls = (struct snd_kcontrol_new *)tas2563_cali_controls;
1746 		nctrls = ARRAY_SIZE(tas2563_cali_controls);
1747 		for (i = 0; i < priv->ndev; i++) {
1748 			tasdev[i].cali_data_backup =
1749 				kmemdup(tas2563_cali_start_reg,
1750 				sizeof(tas2563_cali_start_reg), GFP_KERNEL);
1751 			if (!tasdev[i].cali_data_backup)
1752 				return -ENOMEM;
1753 		}
1754 	}
1755 		break;
1756 	case TAS2573:
1757 		cali_ctrls = (struct snd_kcontrol_new *)tas2573_cali_controls;
1758 		nctrls = ARRAY_SIZE(tas2573_cali_controls);
1759 		break;
1760 	case TAS2781: {
1761 		struct fct_param_address *t = &(fmw->fct_par_addr);
1762 
1763 		rc = snd_soc_add_component_controls(priv->codec,
1764 			tasdevice_cali_controls,
1765 			ARRAY_SIZE(tasdevice_cali_controls));
1766 		if (rc < 0) {
1767 			dev_err(priv->dev, "%s: Add cali controls err rc = %d",
1768 				__func__, rc);
1769 			return rc;
1770 		}
1771 
1772 		cali_ctrls = (struct snd_kcontrol_new *)tas2781_cali_controls;
1773 		nctrls = ARRAY_SIZE(tas2781_cali_controls);
1774 		for (i = 0; i < priv->ndev; i++) {
1775 			struct bulk_reg_val *p;
1776 
1777 			p = tasdev[i].cali_data_backup =
1778 				kmemdup(tas2781_cali_start_reg,
1779 				sizeof(tas2781_cali_start_reg), GFP_KERNEL);
1780 			if (!tasdev[i].cali_data_backup)
1781 				return -ENOMEM;
1782 			if (priv->dspbin_typ) {
1783 				cali_reg_update(p, t);
1784 				if (priv->dspbin_typ == TASDEV_ALPHA) {
1785 					p = &tasdev[i].alp_cali_bckp;
1786 					alpa_cali_update(p, t);
1787 				}
1788 			}
1789 		}
1790 	}
1791 		break;
1792 	default:
1793 		dev_err(priv->dev, "%s: Wrong chip id = %d", __func__,
1794 			priv->chip_id);
1795 		return -EINVAL;
1796 	}
1797 
1798 	rc = snd_soc_add_component_controls(priv->codec, cali_ctrls, nctrls);
1799 	if (rc < 0) {
1800 		dev_err(priv->dev, "%s: Add chip cali ctrls err rc = %d",
1801 			__func__, rc);
1802 		return rc;
1803 	}
1804 
1805 	/* index for cali_ctrls */
1806 	i = 0;
1807 	switch (priv->chip_id) {
1808 	case TAS2573:
1809 		nctrls = 3;
1810 		break;
1811 	case TAS2781:
1812 		nctrls = 2;
1813 		break;
1814 	default:
1815 		nctrls = 1;
1816 		break;
1817 	}
1818 
1819 	/*
1820 	 * Alloc kcontrol via devm_kzalloc(), which don't manually
1821 	 * free the kcontrol.
1822 	 */
1823 	cali_ctrls = devm_kcalloc(priv->dev, nctrls,
1824 		sizeof(cali_ctrls[0]), GFP_KERNEL);
1825 	if (!cali_ctrls)
1826 		return -ENOMEM;
1827 
1828 	ext_cali_data = devm_kzalloc(priv->dev, sizeof(*ext_cali_data),
1829 		GFP_KERNEL);
1830 	if (!ext_cali_data)
1831 		return -ENOMEM;
1832 
1833 	cali_name = devm_kstrdup(priv->dev, "Speaker Calibrated Data",
1834 		GFP_KERNEL);
1835 	if (!cali_name)
1836 		return -ENOMEM;
1837 	/* the number of calibrated data per tas2563/tas2781 */
1838 	cali_data->cali_dat_sz_per_dev = 20;
1839 	/*
1840 	 * Data structure for tas2563/tas2781 calibrated data:
1841 	 *	Pkg len (1 byte)
1842 	 *	Reg id (1 byte, constant 'r')
1843 	 *	book, page, register array for calibrated data (15 bytes)
1844 	 *	for (i = 0; i < Device-Sum; i++) {
1845 	 *		Device #i index_info (1 byte)
1846 	 *		Calibrated data for Device #i (20 bytes)
1847 	 *	}
1848 	 */
1849 	ext_cali_data->max = priv->ndev *
1850 		(cali_data->cali_dat_sz_per_dev + 1) + 1 + 15 + 1;
1851 	priv->cali_data.total_sz = priv->ndev *
1852 		(cali_data->cali_dat_sz_per_dev + 1);
1853 	cali_ctrls[i].name = cali_name;
1854 	cali_ctrls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1855 	cali_ctrls[i].info = snd_soc_bytes_info_ext;
1856 	cali_ctrls[i].get = tasdev_cali_data_get;
1857 	cali_ctrls[i].put = tasdev_cali_data_put;
1858 	cali_ctrls[i].private_value = (unsigned long)ext_cali_data;
1859 	i++;
1860 
1861 	cali_data->data = devm_kzalloc(priv->dev, cali_data->total_sz,
1862 		GFP_KERNEL);
1863 	if (!cali_data->data)
1864 		return -ENOMEM;
1865 	/*
1866 	 * Set to an invalid value before the calibrated data is stored into
1867 	 * it, for the default value is 0, which means the first device.
1868 	 */
1869 	cali_data->data[0] = 0xff;
1870 
1871 	switch (priv->chip_id) {
1872 	case TAS2573:
1873 		rc = create_tas2573_cali_status_ktrl(priv, &cali_ctrls[i]);
1874 		if (rc != 0)
1875 			return rc;
1876 		i++;
1877 		rc = create_tas2573_cali_re_ktrl(priv, &cali_ctrls[i]);
1878 		if (rc != 0)
1879 			return rc;
1880 		i++;
1881 		break;
1882 	case TAS2781:
1883 		rc = create_tas2781_cali_start_ktrl(priv, &cali_ctrls[i]);
1884 		if (rc != 0)
1885 			return rc;
1886 		i++;
1887 		break;
1888 	}
1889 
1890 	return snd_soc_add_component_controls(priv->codec, cali_ctrls,
1891 		nctrls < i ? nctrls : i);
1892 }
1893 
1894 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C
1895 /*
1896  * This debugfs node is a bridge to the acoustic tuning application
1897  * tool which can tune the chips' acoustic effect.
1898  *
1899  * package structure for PPC3 communications:
1900  *	Pkg len (1 byte)
1901  *	Pkg id (1 byte, 'r' or 'w')
1902  *	Dev id (1 byte, i2c address)
1903  *	Book id (1 byte)
1904  *	Page id (1 byte)
1905  *	Reg id (1 byte)
1906  *	switch (pkg id) {
1907  *	case 'w':
1908  *		1 byte, length of data to read
1909  *	case 'r':
1910  *		data payload (1~128 bytes)
1911  *	}
1912  */
1913 static ssize_t acoustic_ctl_read(struct file *file, char __user *to,
1914 	size_t count, loff_t *ppos)
1915 {
1916 	struct snd_soc_component *comp = file->private_data;
1917 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
1918 	struct acoustic_data *p = &tas_priv->acou_data;
1919 	int ret = -1;
1920 
1921 	if (p->id == 'r' && p->len == count && count <= sizeof(*p))
1922 		ret = simple_read_from_buffer(to, count, ppos, p, p->len);
1923 	else
1924 		dev_err(tas_priv->dev, "Not ready for get.\n");
1925 	return ret;
1926 }
1927 
1928 static ssize_t acoustic_ctl_write(struct file *file,
1929 	const char __user *from, size_t count, loff_t *ppos)
1930 {
1931 	struct snd_soc_component *comp = file->private_data;
1932 	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
1933 	struct acoustic_data *p = &priv->acou_data;
1934 	unsigned int max_pkg_len = sizeof(*p);
1935 	unsigned char *src;
1936 	int j, len, reg, val;
1937 	unsigned short chn;
1938 	int ret = -1;
1939 
1940 	if (count > sizeof(*p)) {
1941 		dev_err(priv->dev, "count(%u) is larger than max(%u).\n",
1942 			(unsigned int)count, max_pkg_len);
1943 		return ret;
1944 	}
1945 
1946 	src = memdup_user(from, count);
1947 	if (IS_ERR(src))
1948 		return PTR_ERR(src);
1949 
1950 	if (src[0] > max_pkg_len && src[0] != count) {
1951 		dev_err(priv->dev, "pkg(%u), max(%u), count(%u) mismatch.\n",
1952 			src[0], max_pkg_len, (unsigned int)count);
1953 		kfree(src);
1954 		return 0;
1955 	}
1956 
1957 	switch (src[1]) {
1958 	case 'r':
1959 		/* length of data to read */
1960 		len = src[6];
1961 		break;
1962 	case 'w':
1963 		/* Skip 6 bytes for package type and register address */
1964 		len = src[0] - 6;
1965 		break;
1966 	default:
1967 		dev_err(priv->dev, "%s Wrong code %02x.\n", __func__, src[1]);
1968 		kfree(src);
1969 		return 0;
1970 	}
1971 
1972 	if (len < 1) {
1973 		dev_err(priv->dev, "pkg fmt invalid %02x.\n", len);
1974 		kfree(src);
1975 		return 0;
1976 	}
1977 
1978 	for (j = 0; j < priv->ndev; j++)
1979 		if (src[2] == priv->tasdevice[j].dev_addr) {
1980 			chn = j;
1981 			break;
1982 		}
1983 	if (j >= priv->ndev) {
1984 		dev_err(priv->dev, "no such device 0x%02x.\n", src[2]);
1985 		kfree(src);
1986 		return 0;
1987 	}
1988 
1989 	reg = TASDEVICE_REG(src[3], src[4], src[5]);
1990 
1991 	guard(mutex)(&priv->codec_lock);
1992 
1993 	if (src[1] == 'w') {
1994 		if (len > 1)
1995 			ret = tasdevice_dev_bulk_write(priv, chn, reg,
1996 				 &src[6], len);
1997 		else
1998 			ret = tasdevice_dev_write(priv, chn, reg, src[6]);
1999 	} else {
2000 		struct acoustic_data *p = &priv->acou_data;
2001 
2002 		memcpy(p, src, 6);
2003 		if (len > 1) {
2004 			ret = tasdevice_dev_bulk_read(priv, chn, reg,
2005 				p->data, len);
2006 		} else {
2007 			ret = tasdevice_dev_read(priv, chn, reg, &val);
2008 			p->data[0] = val;
2009 		}
2010 		p->len = len + 6;
2011 	}
2012 
2013 	if (ret)
2014 		dev_err(priv->dev, "i2c communication error.\n");
2015 	else
2016 		ret = count;
2017 
2018 	kfree(src);
2019 	return ret;
2020 }
2021 
2022 static const struct file_operations acoustic_ctl_fops = {
2023 	.open = simple_open,
2024 	.read = acoustic_ctl_read,
2025 	.write = acoustic_ctl_write,
2026 };
2027 #endif
2028 
2029 static void tasdevice_fw_ready(const struct firmware *fmw,
2030 	void *context)
2031 {
2032 	struct tasdevice_priv *tas_priv = context;
2033 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C
2034 	struct snd_soc_component *comp = tas_priv->codec;
2035 	struct dentry *debugfs_root = comp->debugfs_root;
2036 	char *acoustic_debugfs_node;
2037 #endif
2038 	int ret = 0;
2039 	int i;
2040 
2041 	mutex_lock(&tas_priv->codec_lock);
2042 
2043 	ret = tasdevice_rca_parser(tas_priv, fmw);
2044 	if (ret) {
2045 		tasdevice_config_info_remove(tas_priv);
2046 		goto out;
2047 	}
2048 	tasdevice_create_control(tas_priv);
2049 
2050 	tasdevice_dsp_remove(tas_priv);
2051 	tasdevice_calbin_remove(tas_priv);
2052 	/*
2053 	 * The baseline is the RCA-only case, and then the code attempts to
2054 	 * load DSP firmware but in case of failures just keep going, i.e.
2055 	 * failing to load DSP firmware is NOT an error.
2056 	 */
2057 	tas_priv->fw_state = TASDEVICE_RCA_FW_OK;
2058 	/* There is no DSP firmware required for TAS2118/2X20/257X. */
2059 	switch (tas_priv->chip_id) {
2060 	case TAS2020:
2061 	case TAS2118:
2062 	case TAS2120:
2063 	case TAS2320:
2064 	case TAS2568:
2065 	case TAS2570:
2066 	case TAS2572:
2067 	case TAS2574:
2068 		goto out;
2069 	}
2070 	if (tas_priv->name_prefix)
2071 		scnprintf(tas_priv->coef_binaryname, 64, "%s-%s_coef.bin",
2072 			tas_priv->name_prefix, tas_priv->dev_name);
2073 	else
2074 		scnprintf(tas_priv->coef_binaryname, 64, "%s_coef.bin",
2075 			tas_priv->dev_name);
2076 	ret = tasdevice_dsp_parser(tas_priv);
2077 	if (ret) {
2078 		dev_err(tas_priv->dev, "dspfw load %s error\n",
2079 			tas_priv->coef_binaryname);
2080 		goto out;
2081 	}
2082 
2083 	/*
2084 	 * If no dsp-related kcontrol created, the dsp resource will be freed.
2085 	 */
2086 	ret = tasdevice_dsp_create_ctrls(tas_priv);
2087 	if (ret) {
2088 		dev_err(tas_priv->dev, "dsp controls error\n");
2089 		goto out;
2090 	}
2091 	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
2092 
2093 	/* There is no calibration required for TAS58XX. */
2094 	if (tas_priv->chip_id == TAS2563 || tas_priv->chip_id == TAS2573 ||
2095 		tas_priv->chip_id == TAS2781) {
2096 		ret = tasdevice_create_cali_ctrls(tas_priv);
2097 		if (ret) {
2098 			dev_err(tas_priv->dev, "cali controls error\n");
2099 			goto out;
2100 		}
2101 		/* If calibrated data occurs error, dsp will still works
2102 		 * with default calibrated data inside algo.
2103 		 */
2104 		for (i = 0; i < tas_priv->ndev; i++) {
2105 			if (tas_priv->name_prefix)
2106 				scnprintf(tas_priv->cal_binaryname[i], 64,
2107 					  "%s-%s_cal_0x%02x.bin",
2108 					  tas_priv->name_prefix,
2109 					  tas_priv->dev_name,
2110 					  tas_priv->tasdevice[i].dev_addr);
2111 			else
2112 				scnprintf(tas_priv->cal_binaryname[i], 64,
2113 					  "%s_cal_0x%02x.bin",
2114 					  tas_priv->dev_name,
2115 					  tas_priv->tasdevice[i].dev_addr);
2116 			ret = tas2781_load_calibration(tas_priv,
2117 				tas_priv->cal_binaryname[i], i);
2118 			if (ret != 0)
2119 				dev_err(tas_priv->dev,
2120 					"%s: load %s error, keep default.\n",
2121 					__func__, tas_priv->cal_binaryname[i]);
2122 		}
2123 	}
2124 
2125 	tasdevice_prmg_load(tas_priv, 0);
2126 	tas_priv->cur_prog = 0;
2127 
2128 	/* Init common setting for different audio profiles */
2129 	if (tas_priv->rcabin.init_profile_id >= 0)
2130 		tasdevice_select_cfg_blk(tas_priv,
2131 			tas_priv->rcabin.init_profile_id,
2132 			TASDEVICE_BIN_BLK_PRE_POWER_UP);
2133 
2134 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C
2135 	if (tas_priv->name_prefix)
2136 		acoustic_debugfs_node = devm_kasprintf(tas_priv->dev,
2137 			GFP_KERNEL, "%s_acoustic_ctl", tas_priv->name_prefix);
2138 	else
2139 		acoustic_debugfs_node = devm_kstrdup(tas_priv->dev,
2140 			"acoustic_ctl", GFP_KERNEL);
2141 	debugfs_create_file(acoustic_debugfs_node, 0644, debugfs_root,
2142 		comp, &acoustic_ctl_fops);
2143 #endif
2144 out:
2145 	if (tas_priv->fw_state == TASDEVICE_RCA_FW_OK) {
2146 		switch (tas_priv->chip_id) {
2147 		case TAS2563:
2148 		case TAS2573:
2149 		case TAS2781:
2150 		case TAS5802:
2151 		case TAS5806M:
2152 		case TAS5806MD:
2153 		case TAS5815:
2154 		case TAS5822:
2155 		case TAS5825:
2156 		case TAS5827:
2157 		case TAS5828:
2158 		case TAS5830:
2159 		case TAS5832:
2160 			/* If DSP FW fail, DSP kcontrol won't be created. */
2161 			tasdevice_dsp_remove(tas_priv);
2162 		}
2163 	}
2164 	mutex_unlock(&tas_priv->codec_lock);
2165 	release_firmware(fmw);
2166 }
2167 
2168 static int tasdevice_dapm_event(struct snd_soc_dapm_widget *w,
2169 			struct snd_kcontrol *kcontrol, int event)
2170 {
2171 	struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
2172 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
2173 	int state = 0;
2174 
2175 	guard(mutex)(&tas_priv->codec_lock);
2176 	if (event == SND_SOC_DAPM_PRE_PMD)
2177 		state = 1;
2178 	tasdevice_tuning_switch(tas_priv, state, false);
2179 
2180 	return 0;
2181 }
2182 
2183 static int tasdevice_capture_dapm_event(struct snd_soc_dapm_widget *w,
2184 			struct snd_kcontrol *kcontrol, int event)
2185 {
2186 	struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
2187 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
2188 	int state = 0;
2189 
2190 	guard(mutex)(&tas_priv->codec_lock);
2191 	if (event == SND_SOC_DAPM_PRE_PMD)
2192 		state = 1;
2193 	tasdevice_tuning_switch(tas_priv, state, true);
2194 
2195 	return 0;
2196 }
2197 
2198 static const struct snd_soc_dapm_widget tasdevice_dapm_widgets[] = {
2199 	SND_SOC_DAPM_AIF_IN("ASI", "ASI Playback", 0, SND_SOC_NOPM, 0, 0),
2200 	SND_SOC_DAPM_AIF_OUT_E("ASI OUT", "ASI Capture", 0, SND_SOC_NOPM,
2201 		0, 0, tasdevice_capture_dapm_event,
2202 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
2203 	SND_SOC_DAPM_SPK("SPK", tasdevice_dapm_event),
2204 	SND_SOC_DAPM_OUTPUT("OUT"),
2205 	SND_SOC_DAPM_INPUT("DMIC"),
2206 };
2207 
2208 static const struct snd_soc_dapm_route tasdevice_audio_map[] = {
2209 	{"SPK", NULL, "ASI"},
2210 	{"OUT", NULL, "SPK"},
2211 	{"ASI OUT", NULL, "DMIC"},
2212 };
2213 
2214 static int tasdevice_startup(struct snd_pcm_substream *substream,
2215 						struct snd_soc_dai *dai)
2216 {
2217 	struct snd_soc_component *codec = dai->component;
2218 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
2219 
2220 	switch (tas_priv->fw_state) {
2221 	case TASDEVICE_RCA_FW_OK:
2222 	case TASDEVICE_DSP_FW_ALL_OK:
2223 		return 0;
2224 	default:
2225 		return -EINVAL;
2226 	}
2227 }
2228 
2229 static int tasdevice_hw_params(struct snd_pcm_substream *substream,
2230 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
2231 {
2232 	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(dai);
2233 	unsigned int slot_width;
2234 	unsigned int fsrate;
2235 	int bclk_rate;
2236 
2237 	fsrate = params_rate(params);
2238 	switch (fsrate) {
2239 	case 48000:
2240 	case 44100:
2241 		break;
2242 	default:
2243 		dev_err(tas_priv->dev, "%s: incorrect sample rate = %u\n",
2244 			__func__, fsrate);
2245 		return -EINVAL;
2246 	}
2247 
2248 	slot_width = params_width(params);
2249 	switch (slot_width) {
2250 	case 16:
2251 	case 20:
2252 	case 24:
2253 	case 32:
2254 		break;
2255 	default:
2256 		dev_err(tas_priv->dev, "%s: incorrect slot width = %u\n",
2257 			__func__, slot_width);
2258 		return -EINVAL;
2259 	}
2260 
2261 	bclk_rate = snd_soc_params_to_bclk(params);
2262 	if (bclk_rate < 0) {
2263 		dev_err(tas_priv->dev, "%s: incorrect bclk rate = %d\n",
2264 			__func__, bclk_rate);
2265 		return bclk_rate;
2266 	}
2267 
2268 	return 0;
2269 }
2270 
2271 static int tasdevice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
2272 	int clk_id, unsigned int freq, int dir)
2273 {
2274 	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(codec_dai);
2275 
2276 	tas_priv->sysclk = freq;
2277 
2278 	return 0;
2279 }
2280 
2281 static const struct snd_soc_dai_ops tasdevice_dai_ops = {
2282 	.startup = tasdevice_startup,
2283 	.hw_params = tasdevice_hw_params,
2284 	.set_sysclk = tasdevice_set_dai_sysclk,
2285 };
2286 
2287 static struct snd_soc_dai_driver tasdevice_dai_driver[] = {
2288 	{
2289 		.name = "tasdev_codec",
2290 		.id = 0,
2291 		.playback = {
2292 			.stream_name = "Playback",
2293 			.channels_min = 1,
2294 			.channels_max = 4,
2295 			.rates	 = TASDEVICE_RATES,
2296 			.formats	= TASDEVICE_FORMATS,
2297 		},
2298 		.capture = {
2299 			.stream_name = "Capture",
2300 			.channels_min = 1,
2301 			.channels_max = 4,
2302 			.rates	 = TASDEVICE_RATES,
2303 			.formats	= TASDEVICE_FORMATS,
2304 		},
2305 		.ops = &tasdevice_dai_ops,
2306 		.symmetric_rate = 1,
2307 	},
2308 };
2309 
2310 static int tasdevice_codec_probe(struct snd_soc_component *codec)
2311 {
2312 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
2313 	struct snd_kcontrol_new *p;
2314 	unsigned int size;
2315 	int rc;
2316 
2317 	switch (tas_priv->chip_id) {
2318 	case TAS2020:
2319 	case TAS2118:
2320 	case TAS2120:
2321 	case TAS2320:
2322 	case TAS2568:
2323 	case TAS2570:
2324 	case TAS2572:
2325 	case TAS2573:
2326 	case TAS2574:
2327 		p = (struct snd_kcontrol_new *)tas2x20_snd_controls;
2328 		size = ARRAY_SIZE(tas2x20_snd_controls);
2329 		tas_priv->dvc_tlv_table = tas2x20_dvc_table;
2330 		break;
2331 	case TAS2781:
2332 		p = (struct snd_kcontrol_new *)tas2781_snd_controls;
2333 		size = ARRAY_SIZE(tas2781_snd_controls);
2334 		break;
2335 	case TAS5802:
2336 	case TAS5806M:
2337 	case TAS5806MD:
2338 	case TAS5815:
2339 	case TAS5822:
2340 	case TAS5825:
2341 	case TAS5827:
2342 	case TAS5828:
2343 	case TAS5830:
2344 	case TAS5832:
2345 		p = (struct snd_kcontrol_new *)tas5825_snd_controls;
2346 		size = ARRAY_SIZE(tas5825_snd_controls);
2347 		break;
2348 	default:
2349 		p = (struct snd_kcontrol_new *)tas2563_snd_controls;
2350 		size = ARRAY_SIZE(tas2563_snd_controls);
2351 		tas_priv->dvc_tlv_table = tas2563_dvc_table;
2352 		break;
2353 	}
2354 
2355 	rc = snd_soc_add_component_controls(codec, p, size);
2356 	if (rc < 0) {
2357 		dev_err(tas_priv->dev, "%s: Add control err rc = %d",
2358 			__func__, rc);
2359 		return rc;
2360 	}
2361 
2362 	tas_priv->name_prefix = codec->name_prefix;
2363 	return tascodec_init(tas_priv, codec, THIS_MODULE, tasdevice_fw_ready);
2364 }
2365 
2366 static void tasdevice_deinit(void *context)
2367 {
2368 	struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;
2369 	struct tasdevice *tasdev = tas_priv->tasdevice;
2370 	int i;
2371 
2372 	for (i = 0; i < tas_priv->ndev; i++)
2373 		kfree(tasdev[i].cali_data_backup);
2374 
2375 	tasdevice_config_info_remove(tas_priv);
2376 	tasdevice_dsp_remove(tas_priv);
2377 	tasdevice_calbin_remove(tas_priv);
2378 	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
2379 }
2380 
2381 static void tasdevice_codec_remove(struct snd_soc_component *codec)
2382 {
2383 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
2384 
2385 	tasdevice_deinit(tas_priv);
2386 }
2387 
2388 static const struct snd_soc_component_driver
2389 	soc_codec_driver_tasdevice = {
2390 	.probe			= tasdevice_codec_probe,
2391 	.remove			= tasdevice_codec_remove,
2392 	.dapm_widgets		= tasdevice_dapm_widgets,
2393 	.num_dapm_widgets	= ARRAY_SIZE(tasdevice_dapm_widgets),
2394 	.dapm_routes		= tasdevice_audio_map,
2395 	.num_dapm_routes	= ARRAY_SIZE(tasdevice_audio_map),
2396 	.idle_bias_on		= 1,
2397 	.endianness		= 1,
2398 };
2399 
2400 static void tasdevice_parse_dt(struct tasdevice_priv *tas_priv)
2401 {
2402 	struct i2c_client *client = (struct i2c_client *)tas_priv->client;
2403 	unsigned int dev_addrs[TASDEVICE_MAX_CHANNELS];
2404 	int ndev = 0;
2405 	int i, rc;
2406 
2407 	if (tas_priv->isacpi) {
2408 		ndev = device_property_read_u32_array(&client->dev,
2409 			"ti,audio-slots", NULL, 0);
2410 		if (ndev <= 0) {
2411 			ndev = 1;
2412 			dev_addrs[0] = client->addr;
2413 		} else {
2414 			ndev = (ndev < ARRAY_SIZE(dev_addrs))
2415 				? ndev : ARRAY_SIZE(dev_addrs);
2416 			rc = device_property_read_u32_array(&client->dev,
2417 				"ti,audio-slots", dev_addrs, ndev);
2418 			if (rc != 0) {
2419 				ndev = 1;
2420 				dev_addrs[0] = client->addr;
2421 			}
2422 		}
2423 
2424 		tas_priv->irq =
2425 			acpi_dev_gpio_irq_get(ACPI_COMPANION(&client->dev), 0);
2426 	} else if (IS_ENABLED(CONFIG_OF)) {
2427 		struct device_node *np = tas_priv->dev->of_node;
2428 		u64 addr;
2429 
2430 		for (i = 0; i < TASDEVICE_MAX_CHANNELS; i++) {
2431 			if (of_property_read_reg(np, i, &addr, NULL))
2432 				break;
2433 			dev_addrs[ndev++] = addr;
2434 		}
2435 
2436 		tas_priv->irq = of_irq_get(np, 0);
2437 	} else {
2438 		ndev = 1;
2439 		dev_addrs[0] = client->addr;
2440 	}
2441 	tas_priv->ndev = ndev;
2442 	for (i = 0; i < ndev; i++)
2443 		tas_priv->tasdevice[i].dev_addr = dev_addrs[i];
2444 
2445 	tas_priv->reset = devm_gpiod_get_optional(&client->dev,
2446 			"reset", GPIOD_OUT_HIGH);
2447 	if (IS_ERR(tas_priv->reset))
2448 		dev_err(tas_priv->dev, "%s Can't get reset GPIO\n",
2449 			__func__);
2450 }
2451 
2452 static int tasdevice_i2c_probe(struct i2c_client *i2c)
2453 {
2454 	struct tasdevice_priv *tas_priv;
2455 	struct i2c_device_id *id_data;
2456 	int ret;
2457 
2458 	tas_priv = tasdevice_kzalloc(i2c);
2459 	if (!tas_priv)
2460 		return -ENOMEM;
2461 
2462 	dev_set_drvdata(&i2c->dev, tas_priv);
2463 
2464 	if (ACPI_HANDLE(&i2c->dev)) {
2465 		id_data = (struct i2c_device_id *)
2466 			acpi_device_get_match_data(&i2c->dev);
2467 		tas_priv->isacpi = true;
2468 	} else {
2469 		id_data = (struct i2c_device_id *)i2c_get_match_data(i2c);
2470 		tas_priv->isacpi = false;
2471 	}
2472 
2473 	if (!id_data) {
2474 		dev_err(&i2c->dev, "No driver data\n");
2475 		ret = -EINVAL;
2476 		goto err;
2477 	}
2478 
2479 	tas_priv->chip_id = (uintptr_t)id_data->driver_data;
2480 	strscpy(tas_priv->dev_name, id_data->name, sizeof(tas_priv->dev_name));
2481 
2482 	tasdevice_parse_dt(tas_priv);
2483 
2484 	ret = tasdevice_init(tas_priv);
2485 	if (ret)
2486 		goto err;
2487 
2488 	tasdevice_reset(tas_priv);
2489 
2490 	ret = devm_snd_soc_register_component(tas_priv->dev,
2491 		&soc_codec_driver_tasdevice,
2492 		tasdevice_dai_driver, ARRAY_SIZE(tasdevice_dai_driver));
2493 	if (ret) {
2494 		dev_err(tas_priv->dev, "%s: codec register error:0x%08x\n",
2495 			__func__, ret);
2496 		goto err;
2497 	}
2498 err:
2499 	if (ret < 0)
2500 		tasdevice_remove(tas_priv);
2501 	return ret;
2502 }
2503 
2504 static void tasdevice_i2c_remove(struct i2c_client *client)
2505 {
2506 	struct tasdevice_priv *tas_priv = i2c_get_clientdata(client);
2507 
2508 	tasdevice_remove(tas_priv);
2509 }
2510 
2511 static const struct acpi_device_id tasdevice_acpi_match[] = {
2512 	{ "TXNW2020", (kernel_ulong_t)&tasdevice_id[TAS2020] },
2513 	{ "TXNW2118", (kernel_ulong_t)&tasdevice_id[TAS2118] },
2514 	{ "TXNW2120", (kernel_ulong_t)&tasdevice_id[TAS2120] },
2515 	{ "TXNW2320", (kernel_ulong_t)&tasdevice_id[TAS2320] },
2516 	{ "TXNW2563", (kernel_ulong_t)&tasdevice_id[TAS2563] },
2517 	{ "TXNW2568", (kernel_ulong_t)&tasdevice_id[TAS2568] },
2518 	{ "TXNW2570", (kernel_ulong_t)&tasdevice_id[TAS2570] },
2519 	{ "TXNW2572", (kernel_ulong_t)&tasdevice_id[TAS2572] },
2520 	{ "TXNW2573", (kernel_ulong_t)&tasdevice_id[TAS2573] },
2521 	{ "TXNW2574", (kernel_ulong_t)&tasdevice_id[TAS2574] },
2522 	{ "TXNW2781", (kernel_ulong_t)&tasdevice_id[TAS2781] },
2523 	{ "TXNW5802", (kernel_ulong_t)&tasdevice_id[TAS5802] },
2524 	{ "TXNW806M", (kernel_ulong_t)&tasdevice_id[TAS5806M] },
2525 	{ "TXNW806D", (kernel_ulong_t)&tasdevice_id[TAS5806MD] },
2526 	{ "TXNW5815", (kernel_ulong_t)&tasdevice_id[TAS5815] },
2527 	{ "TXNW5822", (kernel_ulong_t)&tasdevice_id[TAS5822] },
2528 	{ "TXNW5825", (kernel_ulong_t)&tasdevice_id[TAS5825] },
2529 	{ "TXNW5827", (kernel_ulong_t)&tasdevice_id[TAS5827] },
2530 	{ "TXNW5828", (kernel_ulong_t)&tasdevice_id[TAS5828] },
2531 	{ "TXNW5830", (kernel_ulong_t)&tasdevice_id[TAS5830] },
2532 	{ "TXNW5832", (kernel_ulong_t)&tasdevice_id[TAS5832] },
2533 	{},
2534 };
2535 
2536 MODULE_DEVICE_TABLE(acpi, tasdevice_acpi_match);
2537 
2538 static struct i2c_driver tasdevice_i2c_driver = {
2539 	.driver = {
2540 		.name = "tasdev-codec",
2541 		.of_match_table = tasdevice_of_match,
2542 		.acpi_match_table = tasdevice_acpi_match,
2543 	},
2544 	.probe	= tasdevice_i2c_probe,
2545 	.remove = tasdevice_i2c_remove,
2546 };
2547 
2548 module_i2c_driver(tasdevice_i2c_driver);
2549 
2550 MODULE_AUTHOR("Shenghao Ding <shenghao-ding@ti.com>");
2551 MODULE_AUTHOR("Kevin Lu <kevin-lu@ti.com>");
2552 MODULE_DESCRIPTION("ASoC TAS2781 Driver");
2553 MODULE_LICENSE("GPL");
2554 MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");
2555