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