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