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