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/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, true),
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  */
tas2781_digital_getvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tas2781_digital_putvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tas2781_amp_getvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tas2781_amp_putvol(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tasdev_force_fwload_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tasdev_force_fwload_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
tasdev_cali_data_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)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 
calib_data_get(struct tasdevice_priv * tas_priv,int reg,unsigned char * dst)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 
partial_cali_data_update(int * reg,int j)352 static int partial_cali_data_update(int *reg, int j)
353 {
354 	switch (tas2781_cali_start_reg[j].reg) {
355 	case 0:
356 		return reg[0];
357 	case TAS2781_PRM_PLT_FLAG_REG:
358 		return reg[1];
359 	case TAS2781_PRM_SINEGAIN_REG:
360 		return reg[2];
361 	case TAS2781_PRM_SINEGAIN2_REG:
362 		return reg[3];
363 	default:
364 		return 0;
365 	}
366 }
367 
sngl_calib_start(struct tasdevice_priv * tas_priv,int i,int * reg,unsigned char * dat)368 static void sngl_calib_start(struct tasdevice_priv *tas_priv, int i,
369 	int *reg, unsigned char *dat)
370 {
371 	struct tasdevice *tasdev = tas_priv->tasdevice;
372 	struct bulk_reg_val *p = tasdev[i].cali_data_backup;
373 	struct bulk_reg_val *t = &tasdev[i].alp_cali_bckp;
374 	const int sum = ARRAY_SIZE(tas2781_cali_start_reg);
375 	unsigned char val[4];
376 	int j, r;
377 
378 	if (p == NULL)
379 		return;
380 
381 	/* Store the current setting from the chip */
382 	for (j = 0; j < sum; j++) {
383 		if (p[j].val_len == 1) {
384 			if (p[j].is_locked)
385 				tasdevice_dev_write(tas_priv, i,
386 					TAS2781_TEST_UNLOCK_REG,
387 					TAS2781_TEST_PAGE_UNLOCK);
388 			tasdevice_dev_read(tas_priv, i, p[j].reg,
389 				(int *)&p[j].val[0]);
390 		} else {
391 			if (!tas_priv->dspbin_typ) {
392 				r = partial_cali_data_update(reg, j);
393 				if (r)
394 					p[j].reg = r;
395 			}
396 
397 			if (p[j].reg)
398 				tasdevice_dev_bulk_read(tas_priv, i, p[j].reg,
399 					p[j].val, 4);
400 		}
401 	}
402 
403 	if (tas_priv->dspbin_typ == TASDEV_ALPHA)
404 		tasdevice_dev_bulk_read(tas_priv, i, t->reg, t->val, 4);
405 
406 	/* Update the setting for calibration */
407 	for (j = 0; j < sum - 4; j++) {
408 		if (p[j].val_len == 1) {
409 			if (p[j].is_locked)
410 				tasdevice_dev_write(tas_priv, i,
411 					TAS2781_TEST_UNLOCK_REG,
412 					TAS2781_TEST_PAGE_UNLOCK);
413 			tasdevice_dev_write(tas_priv, i, p[j].reg,
414 				tas2781_cali_start_reg[j].val[0]);
415 		}
416 	}
417 
418 	if (tas_priv->dspbin_typ == TASDEV_ALPHA) {
419 		val[0] = 0x00;
420 		val[1] = 0x00;
421 		val[2] = 0x21;
422 		val[3] = 0x8e;
423 	} else {
424 		val[0] = tas2781_cali_start_reg[j].val[0];
425 		val[1] = tas2781_cali_start_reg[j].val[1];
426 		val[2] = tas2781_cali_start_reg[j].val[2];
427 		val[3] = tas2781_cali_start_reg[j].val[3];
428 	}
429 	tasdevice_dev_bulk_write(tas_priv, i, p[j].reg, val, 4);
430 	tasdevice_dev_bulk_write(tas_priv, i, p[j + 1].reg,
431 		(unsigned char *)tas2781_cali_start_reg[j + 1].val, 4);
432 	tasdevice_dev_bulk_write(tas_priv, i, p[j + 2].reg, &dat[1], 4);
433 	tasdevice_dev_bulk_write(tas_priv, i, p[j + 3].reg, &dat[5], 4);
434 	if (tas_priv->dspbin_typ == TASDEV_ALPHA) {
435 		val[0] = 0x00;
436 		val[1] = 0x00;
437 		val[2] = 0x2a;
438 		val[3] = 0x0b;
439 
440 		tasdevice_dev_bulk_read(tas_priv, i, t->reg, val, 4);
441 	}
442 }
443 
tas2781_calib_start_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)444 static int tas2781_calib_start_put(struct snd_kcontrol *kcontrol,
445 	struct snd_ctl_elem_value *ucontrol)
446 {
447 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
448 	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
449 	struct soc_bytes_ext *bytes_ext =
450 		(struct soc_bytes_ext *) kcontrol->private_value;
451 	unsigned char *dat = ucontrol->value.bytes.data;
452 	int i, reg[4];
453 	int j = 0;
454 
455 	guard(mutex)(&priv->codec_lock);
456 	if (priv->chip_id != TAS2781 || bytes_ext->max != dat[0] ||
457 		dat[1] != 'r') {
458 		dev_err(priv->dev, "%s: package fmt or chipid incorrect\n",
459 			__func__);
460 		return 0;
461 	}
462 	j += 2;
463 	/* refresh pilot tone and SineGain register */
464 	for (i = 0; i < ARRAY_SIZE(reg); i++) {
465 		reg[i] = TASDEVICE_REG(dat[j], dat[j + 1], dat[j + 2]);
466 		j += 3;
467 	}
468 
469 	for (i = 0; i < priv->ndev; i++) {
470 		int k = i * 9 + j;
471 
472 		if (dat[k] != i) {
473 			dev_err(priv->dev, "%s:no cal-setting for dev %d\n",
474 				__func__, i);
475 			continue;
476 		}
477 		sngl_calib_start(priv, i, reg, dat + k);
478 	}
479 	return 1;
480 }
481 
tas2781_calib_stop_put(struct tasdevice_priv * priv)482 static void tas2781_calib_stop_put(struct tasdevice_priv *priv)
483 {
484 	const int sum = ARRAY_SIZE(tas2781_cali_start_reg);
485 	int i, j;
486 
487 	for (i = 0; i < priv->ndev; i++) {
488 		struct tasdevice *tasdev = priv->tasdevice;
489 		struct bulk_reg_val *p = tasdev[i].cali_data_backup;
490 		struct bulk_reg_val *t = &tasdev[i].alp_cali_bckp;
491 
492 		if (p == NULL)
493 			continue;
494 
495 		for (j = 0; j < sum; j++) {
496 			if (p[j].val_len == 1) {
497 				if (p[j].is_locked)
498 					tasdevice_dev_write(priv, i,
499 						TAS2781_TEST_UNLOCK_REG,
500 						TAS2781_TEST_PAGE_UNLOCK);
501 				tasdevice_dev_write(priv, i, p[j].reg,
502 					p[j].val[0]);
503 			} else {
504 				if (!p[j].reg)
505 					continue;
506 				tasdevice_dev_bulk_write(priv, i, p[j].reg,
507 					p[j].val, 4);
508 			}
509 		}
510 
511 		if (priv->dspbin_typ == TASDEV_ALPHA)
512 			tasdevice_dev_bulk_write(priv, i, t->reg, t->val, 4);
513 	}
514 }
515 
tas2563_calib_start_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)516 static int tas2563_calib_start_put(struct snd_kcontrol *kcontrol,
517 	struct snd_ctl_elem_value *ucontrol)
518 {
519 	struct bulk_reg_val *q = (struct bulk_reg_val *)tas2563_cali_start_reg;
520 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
521 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
522 	const int sum = ARRAY_SIZE(tas2563_cali_start_reg);
523 	int i, j;
524 
525 	guard(mutex)(&tas_priv->codec_lock);
526 	if (tas_priv->chip_id != TAS2563)
527 		return -1;
528 
529 	for (i = 0; i < tas_priv->ndev; i++) {
530 		struct tasdevice *tasdev = tas_priv->tasdevice;
531 		struct bulk_reg_val *p = tasdev[i].cali_data_backup;
532 
533 		if (p == NULL)
534 			continue;
535 		for (j = 0; j < sum; j++) {
536 			if (p[j].val_len == 1)
537 				tasdevice_dev_read(tas_priv,
538 					i, p[j].reg,
539 					(unsigned int *)&p[j].val[0]);
540 			else
541 				tasdevice_dev_bulk_read(tas_priv,
542 					i, p[j].reg, p[j].val, 4);
543 		}
544 
545 		for (j = 0; j < sum; j++) {
546 			if (p[j].val_len == 1)
547 				tasdevice_dev_write(tas_priv, i, p[j].reg,
548 					q[j].val[0]);
549 			else
550 				tasdevice_dev_bulk_write(tas_priv, i, p[j].reg,
551 					q[j].val, 4);
552 		}
553 	}
554 
555 	return 1;
556 }
557 
tas2563_calib_stop_put(struct tasdevice_priv * tas_priv)558 static void tas2563_calib_stop_put(struct tasdevice_priv *tas_priv)
559 {
560 	const int sum = ARRAY_SIZE(tas2563_cali_start_reg);
561 	int i, j;
562 
563 	for (i = 0; i < tas_priv->ndev; i++) {
564 		struct tasdevice *tasdev = tas_priv->tasdevice;
565 		struct bulk_reg_val *p = tasdev[i].cali_data_backup;
566 
567 		if (p == NULL)
568 			continue;
569 
570 		for (j = 0; j < sum; j++) {
571 			if (p[j].val_len == 1)
572 				tasdevice_dev_write(tas_priv, i, p[j].reg,
573 					p[j].val[0]);
574 			else
575 				tasdevice_dev_bulk_write(tas_priv, i, p[j].reg,
576 					p[j].val, 4);
577 		}
578 	}
579 }
580 
tasdev_calib_stop_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)581 static int tasdev_calib_stop_put(struct snd_kcontrol *kcontrol,
582 	struct snd_ctl_elem_value *ucontrol)
583 {
584 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
585 	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
586 
587 	guard(mutex)(&priv->codec_lock);
588 	if (priv->chip_id == TAS2563)
589 		tas2563_calib_stop_put(priv);
590 	else
591 		tas2781_calib_stop_put(priv);
592 
593 	return 1;
594 }
595 
tasdev_cali_data_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)596 static int tasdev_cali_data_put(struct snd_kcontrol *kcontrol,
597 	struct snd_ctl_elem_value *ucontrol)
598 {
599 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
600 	struct tasdevice_priv *priv = snd_soc_component_get_drvdata(comp);
601 	struct soc_bytes_ext *bytes_ext =
602 		(struct soc_bytes_ext *) kcontrol->private_value;
603 	struct calidata *cali_data = &priv->cali_data;
604 	struct cali_reg *p = &cali_data->cali_reg_array;
605 	unsigned char *src = ucontrol->value.bytes.data;
606 	unsigned char *dst = cali_data->data;
607 	int i = 0;
608 	int j;
609 
610 	guard(mutex)(&priv->codec_lock);
611 	if (src[0] != bytes_ext->max || src[1] != 'r') {
612 		dev_err(priv->dev, "%s: pkg fmt invalid\n", __func__);
613 		return 0;
614 	}
615 	for (j = 0; j < priv->ndev; j++) {
616 		if (src[17 + j * 21] != j) {
617 			dev_err(priv->dev, "%s: pkg fmt invalid\n", __func__);
618 			return 0;
619 		}
620 	}
621 	i += 2;
622 	priv->is_user_space_calidata = true;
623 
624 	if (priv->dspbin_typ == TASDEV_BASIC) {
625 		p->r0_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
626 		i += 3;
627 		p->r0_low_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
628 		i += 3;
629 		p->invr0_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
630 		i += 3;
631 		p->pow_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
632 		i += 3;
633 		p->tlimit_reg = TASDEVICE_REG(src[i], src[i + 1], src[i + 2]);
634 		i += 3;
635 	} else {
636 		i += 15;
637 	}
638 
639 	memcpy(dst, &src[i], cali_data->total_sz);
640 	return 1;
641 }
642 
tas2781_latch_reg_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)643 static int tas2781_latch_reg_get(struct snd_kcontrol *kcontrol,
644 	struct snd_ctl_elem_value *ucontrol)
645 {
646 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
647 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
648 	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;
649 	struct soc_bytes_ext *bytes_ext =
650 		(struct soc_bytes_ext *) kcontrol->private_value;
651 	struct tasdevice *tasdev = tas_priv->tasdevice;
652 	unsigned char *dst = ucontrol->value.bytes.data;
653 	int i, val, rc = -1;
654 
655 	dst[0] = bytes_ext->max;
656 	guard(mutex)(&tas_priv->codec_lock);
657 	for (i = 0; i < tas_priv->ndev; i++) {
658 		if (clt->addr == tasdev[i].dev_addr) {
659 			/* First byte is the device index. */
660 			dst[1] = i;
661 			rc = tasdevice_dev_read(tas_priv, i,
662 				TAS2781_RUNTIME_LATCH_RE_REG, &val);
663 			if (rc < 0)
664 				dev_err(tas_priv->dev, "%s, get value error\n",
665 					__func__);
666 			else
667 				dst[2] = val;
668 
669 			break;
670 		}
671 	}
672 
673 	return rc;
674 }
675 
tasdev_tf_data_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)676 static int tasdev_tf_data_get(struct snd_kcontrol *kcontrol,
677 	struct snd_ctl_elem_value *ucontrol)
678 {
679 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
680 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
681 	struct soc_bytes_ext *bytes_ext =
682 		(struct soc_bytes_ext *) kcontrol->private_value;
683 	unsigned char *dst = ucontrol->value.bytes.data;
684 	unsigned int reg = TAS2781_RUNTIME_RE_REG_TF;
685 
686 	if (tas_priv->chip_id == TAS2781) {
687 		struct tasdevice_fw *tas_fmw = tas_priv->fmw;
688 		struct fct_param_address *p = &(tas_fmw->fct_par_addr);
689 
690 		reg = TAS2781_RUNTIME_RE_REG_TF;
691 		if (tas_priv->dspbin_typ)
692 			reg = TASDEVICE_REG(p->tf_reg[0], p->tf_reg[1],
693 				p->tf_reg[2]);
694 	} else {
695 		reg = TAS2563_RUNTIME_RE_REG_TF;
696 	}
697 
698 	guard(mutex)(&tas_priv->codec_lock);
699 	dst[0] = bytes_ext->max;
700 	return calib_data_get(tas_priv, reg, &dst[1]);
701 }
702 
tasdev_re_data_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)703 static int tasdev_re_data_get(struct snd_kcontrol *kcontrol,
704 	struct snd_ctl_elem_value *ucontrol)
705 {
706 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
707 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
708 	struct soc_bytes_ext *bytes_ext =
709 		(struct soc_bytes_ext *) kcontrol->private_value;
710 	unsigned char *dst = ucontrol->value.bytes.data;
711 	unsigned int reg = TAS2781_RUNTIME_RE_REG;
712 
713 	if (tas_priv->chip_id == TAS2781) {
714 		struct tasdevice_fw *tas_fmw = tas_priv->fmw;
715 		struct fct_param_address *p = &(tas_fmw->fct_par_addr);
716 
717 		if (tas_priv->dspbin_typ)
718 			reg = TASDEVICE_REG(p->r0_reg[0], p->r0_reg[1],
719 				p->r0_reg[2]);
720 	} else {
721 		reg = TAS2563_RUNTIME_RE_REG;
722 	}
723 
724 	guard(mutex)(&tas_priv->codec_lock);
725 	dst[0] = bytes_ext->max;
726 	return calib_data_get(tas_priv, reg, &dst[1]);
727 }
728 
tasdev_r0_data_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)729 static int tasdev_r0_data_get(struct snd_kcontrol *kcontrol,
730 	struct snd_ctl_elem_value *ucontrol)
731 {
732 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
733 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
734 	struct calidata *cali_data = &tas_priv->cali_data;
735 	struct soc_bytes_ext *bytes_ext =
736 		(struct soc_bytes_ext *) kcontrol->private_value;
737 	unsigned char *dst = ucontrol->value.bytes.data;
738 	unsigned int reg;
739 
740 	guard(mutex)(&tas_priv->codec_lock);
741 
742 	if (tas_priv->chip_id == TAS2563)
743 		reg = TAS2563_PRM_R0_REG;
744 	else if (cali_data->cali_reg_array.r0_reg)
745 		reg = cali_data->cali_reg_array.r0_reg;
746 	else
747 		return -1;
748 	dst[0] = bytes_ext->max;
749 	return calib_data_get(tas_priv, reg, &dst[1]);
750 }
751 
tasdev_XMA1_data_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)752 static int tasdev_XMA1_data_get(struct snd_kcontrol *kcontrol,
753 	struct snd_ctl_elem_value *ucontrol)
754 {
755 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
756 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
757 	struct tasdevice_fw *tas_fmw = tas_priv->fmw;
758 	struct fct_param_address *p = &(tas_fmw->fct_par_addr);
759 	struct soc_bytes_ext *bytes_ext =
760 		(struct soc_bytes_ext *) kcontrol->private_value;
761 	unsigned char *dst = ucontrol->value.bytes.data;
762 	unsigned int reg = TASDEVICE_XM_A1_REG;
763 
764 	if (tas_priv->dspbin_typ)
765 		reg = TASDEVICE_REG(p->a1_reg[0], p->a1_reg[1], p->a1_reg[2]);
766 
767 	guard(mutex)(&tas_priv->codec_lock);
768 	dst[0] = bytes_ext->max;
769 	return calib_data_get(tas_priv, reg, &dst[1]);
770 }
771 
tasdev_XMA2_data_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)772 static int tasdev_XMA2_data_get(struct snd_kcontrol *kcontrol,
773 	struct snd_ctl_elem_value *ucontrol)
774 {
775 	struct snd_soc_component *comp = snd_soc_kcontrol_component(kcontrol);
776 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(comp);
777 	struct tasdevice_fw *tas_fmw = tas_priv->fmw;
778 	struct fct_param_address *p = &(tas_fmw->fct_par_addr);
779 	struct soc_bytes_ext *bytes_ext =
780 		(struct soc_bytes_ext *) kcontrol->private_value;
781 	unsigned char *dst = ucontrol->value.bytes.data;
782 	unsigned int reg = TASDEVICE_XM_A2_REG;
783 
784 	if (tas_priv->dspbin_typ)
785 		reg = TASDEVICE_REG(p->a2_reg[0], p->a2_reg[1], p->a2_reg[2]);
786 
787 	guard(mutex)(&tas_priv->codec_lock);
788 	dst[0] = bytes_ext->max;
789 	return calib_data_get(tas_priv, reg, &dst[1]);
790 }
791 
tasdev_nop_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)792 static int tasdev_nop_get(
793 	struct snd_kcontrol *kcontrol,
794 	struct snd_ctl_elem_value *ucontrol)
795 {
796 	return 0;
797 }
798 
tas2563_digital_gain_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)799 static int tas2563_digital_gain_get(
800 	struct snd_kcontrol *kcontrol,
801 	struct snd_ctl_elem_value *ucontrol)
802 {
803 	struct soc_mixer_control *mc =
804 		(struct soc_mixer_control *)kcontrol->private_value;
805 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
806 	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);
807 	unsigned int l = 0, r = mc->max;
808 	unsigned int target, ar_mid, mid, ar_l, ar_r;
809 	unsigned int reg = mc->reg;
810 	unsigned char data[4];
811 	int ret;
812 
813 	mutex_lock(&tas_dev->codec_lock);
814 	/* Read the primary device */
815 	ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4);
816 	if (ret) {
817 		dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__);
818 		goto out;
819 	}
820 
821 	target = get_unaligned_be32(&data[0]);
822 
823 	while (r > 1 + l) {
824 		mid = (l + r) / 2;
825 		ar_mid = get_unaligned_be32(tas2563_dvc_table[mid]);
826 		if (target < ar_mid)
827 			r = mid;
828 		else
829 			l = mid;
830 	}
831 
832 	ar_l = get_unaligned_be32(tas2563_dvc_table[l]);
833 	ar_r = get_unaligned_be32(tas2563_dvc_table[r]);
834 
835 	/* find out the member same as or closer to the current volume */
836 	ucontrol->value.integer.value[0] =
837 		abs(target - ar_l) <= abs(target - ar_r) ? l : r;
838 out:
839 	mutex_unlock(&tas_dev->codec_lock);
840 	return 0;
841 }
842 
tas2563_digital_gain_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)843 static int tas2563_digital_gain_put(
844 	struct snd_kcontrol *kcontrol,
845 	struct snd_ctl_elem_value *ucontrol)
846 {
847 	struct soc_mixer_control *mc =
848 		(struct soc_mixer_control *)kcontrol->private_value;
849 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
850 	struct tasdevice_priv *tas_dev = snd_soc_component_get_drvdata(codec);
851 	int vol = ucontrol->value.integer.value[0];
852 	int status = 0, max = mc->max, rc = 1;
853 	int i, ret;
854 	unsigned int reg = mc->reg;
855 	unsigned int volrd, volwr;
856 	unsigned char data[4];
857 
858 	vol = clamp(vol, 0, max);
859 	mutex_lock(&tas_dev->codec_lock);
860 	/* Read the primary device */
861 	ret = tasdevice_dev_bulk_read(tas_dev, 0, reg, data, 4);
862 	if (ret) {
863 		dev_err(tas_dev->dev, "%s, get AMP vol error\n", __func__);
864 		rc = -1;
865 		goto out;
866 	}
867 
868 	volrd = get_unaligned_be32(&data[0]);
869 	volwr = get_unaligned_be32(tas2563_dvc_table[vol]);
870 
871 	if (volrd == volwr) {
872 		rc = 0;
873 		goto out;
874 	}
875 
876 	for (i = 0; i < tas_dev->ndev; i++) {
877 		ret = tasdevice_dev_bulk_write(tas_dev, i, reg,
878 			(unsigned char *)tas2563_dvc_table[vol], 4);
879 		if (ret) {
880 			dev_err(tas_dev->dev,
881 				"%s, set digital vol error in dev %d\n",
882 				__func__, i);
883 			status |= BIT(i);
884 		}
885 	}
886 
887 	if (status)
888 		rc = -1;
889 out:
890 	mutex_unlock(&tas_dev->codec_lock);
891 	return rc;
892 }
893 
894 static const struct snd_kcontrol_new tasdevice_snd_controls[] = {
895 	SOC_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
896 		tasdev_force_fwload_get, tasdev_force_fwload_put),
897 };
898 
899 static const struct snd_kcontrol_new tasdevice_cali_controls[] = {
900 	SOC_SINGLE_EXT("Calibration Stop", SND_SOC_NOPM, 0, 1, 0,
901 		tasdev_nop_get, tasdev_calib_stop_put),
902 	SND_SOC_BYTES_EXT("Amp TF Data", 6, tasdev_tf_data_get, NULL),
903 	SND_SOC_BYTES_EXT("Amp RE Data", 6, tasdev_re_data_get, NULL),
904 	SND_SOC_BYTES_EXT("Amp R0 Data", 6, tasdev_r0_data_get, NULL),
905 	SND_SOC_BYTES_EXT("Amp XMA1 Data", 6, tasdev_XMA1_data_get, NULL),
906 	SND_SOC_BYTES_EXT("Amp XMA2 Data", 6, tasdev_XMA2_data_get, NULL),
907 };
908 
909 static const struct snd_kcontrol_new tas2781_snd_controls[] = {
910 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL,
911 		1, 0, 20, 0, tas2781_amp_getvol,
912 		tas2781_amp_putvol, amp_vol_tlv),
913 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Gain", TAS2781_DVC_LVL,
914 		0, 0, 200, 1, tas2781_digital_getvol,
915 		tas2781_digital_putvol, dvc_tlv),
916 };
917 
918 static const struct snd_kcontrol_new tas2781_cali_controls[] = {
919 	SND_SOC_BYTES_EXT("Amp Latch Data", 3, tas2781_latch_reg_get, NULL),
920 };
921 
922 static const struct snd_kcontrol_new tas2563_snd_controls[] = {
923 	SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2563_DVC_LVL, 0,
924 		0, ARRAY_SIZE(tas2563_dvc_table) - 1, 0,
925 		tas2563_digital_gain_get, tas2563_digital_gain_put,
926 		tas2563_dvc_tlv),
927 };
928 
929 static const struct snd_kcontrol_new tas2563_cali_controls[] = {
930 	SOC_SINGLE_EXT("Calibration Start", SND_SOC_NOPM, 0, 1, 0,
931 		tasdev_nop_get, tas2563_calib_start_put),
932 };
933 
tasdevice_set_profile_id(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)934 static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
935 		struct snd_ctl_elem_value *ucontrol)
936 {
937 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
938 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
939 	int ret = 0;
940 
941 	if (tas_priv->rcabin.profile_cfg_id !=
942 		ucontrol->value.integer.value[0]) {
943 		tas_priv->rcabin.profile_cfg_id =
944 			ucontrol->value.integer.value[0];
945 		ret = 1;
946 	}
947 
948 	return ret;
949 }
950 
tasdevice_info_active_num(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)951 static int tasdevice_info_active_num(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->ndev - 1;
961 
962 	return 0;
963 }
964 
tasdevice_info_chip_id(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)965 static int tasdevice_info_chip_id(struct snd_kcontrol *kcontrol,
966 			struct snd_ctl_elem_info *uinfo)
967 {
968 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
969 	uinfo->count = 1;
970 	uinfo->value.integer.min = TAS2563;
971 	uinfo->value.integer.max = TAS2781;
972 
973 	return 0;
974 }
975 
tasdevice_info_programs(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)976 static int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
977 			struct snd_ctl_elem_info *uinfo)
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 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
982 
983 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
984 	uinfo->count = 1;
985 	uinfo->value.integer.min = 0;
986 	uinfo->value.integer.max = (int)tas_fw->nr_programs;
987 
988 	return 0;
989 }
990 
tasdevice_info_configurations(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)991 static int tasdevice_info_configurations(
992 	struct snd_kcontrol *kcontrol, struct snd_ctl_elem_info *uinfo)
993 {
994 	struct snd_soc_component *codec =
995 		snd_soc_kcontrol_component(kcontrol);
996 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
997 	struct tasdevice_fw *tas_fw = tas_priv->fmw;
998 
999 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1000 	uinfo->count = 1;
1001 	uinfo->value.integer.min = 0;
1002 	uinfo->value.integer.max = (int)tas_fw->nr_configurations - 1;
1003 
1004 	return 0;
1005 }
1006 
tasdevice_info_profile(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_info * uinfo)1007 static int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
1008 			struct snd_ctl_elem_info *uinfo)
1009 {
1010 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1011 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1012 
1013 	uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
1014 	uinfo->count = 1;
1015 	uinfo->value.integer.min = 0;
1016 	uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
1017 
1018 	return 0;
1019 }
1020 
tasdevice_get_profile_id(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1021 static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
1022 			struct snd_ctl_elem_value *ucontrol)
1023 {
1024 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1025 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1026 
1027 	ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
1028 
1029 	return 0;
1030 }
1031 
tasdevice_get_chip_id(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1032 static int tasdevice_get_chip_id(struct snd_kcontrol *kcontrol,
1033 			struct snd_ctl_elem_value *ucontrol)
1034 {
1035 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1036 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1037 
1038 	ucontrol->value.integer.value[0] = tas_priv->chip_id;
1039 
1040 	return 0;
1041 }
1042 
tasdevice_create_control(struct tasdevice_priv * tas_priv)1043 static int tasdevice_create_control(struct tasdevice_priv *tas_priv)
1044 {
1045 	struct snd_kcontrol_new *prof_ctrls;
1046 	int nr_controls = 1;
1047 	int mix_index = 0;
1048 	int ret;
1049 	char *name;
1050 
1051 	prof_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
1052 		sizeof(prof_ctrls[0]), GFP_KERNEL);
1053 	if (!prof_ctrls) {
1054 		ret = -ENOMEM;
1055 		goto out;
1056 	}
1057 
1058 	/* Create a mixer item for selecting the active profile */
1059 	name = devm_kstrdup(tas_priv->dev, "Speaker Profile Id", GFP_KERNEL);
1060 	if (!name) {
1061 		ret = -ENOMEM;
1062 		goto out;
1063 	}
1064 	prof_ctrls[mix_index].name = name;
1065 	prof_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1066 	prof_ctrls[mix_index].info = tasdevice_info_profile;
1067 	prof_ctrls[mix_index].get = tasdevice_get_profile_id;
1068 	prof_ctrls[mix_index].put = tasdevice_set_profile_id;
1069 	mix_index++;
1070 
1071 	ret = snd_soc_add_component_controls(tas_priv->codec,
1072 		prof_ctrls, nr_controls < mix_index ? nr_controls : mix_index);
1073 
1074 out:
1075 	return ret;
1076 }
1077 
tasdevice_program_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1078 static int tasdevice_program_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 
1084 	ucontrol->value.integer.value[0] = tas_priv->cur_prog;
1085 
1086 	return 0;
1087 }
1088 
tasdevice_program_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1089 static int tasdevice_program_put(struct snd_kcontrol *kcontrol,
1090 	struct snd_ctl_elem_value *ucontrol)
1091 {
1092 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1093 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1094 	unsigned int nr_program = ucontrol->value.integer.value[0];
1095 	int ret = 0;
1096 
1097 	if (tas_priv->cur_prog != nr_program) {
1098 		tas_priv->cur_prog = nr_program;
1099 		ret = 1;
1100 	}
1101 
1102 	return ret;
1103 }
1104 
tasdevice_configuration_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1105 static int tasdevice_configuration_get(struct snd_kcontrol *kcontrol,
1106 	struct snd_ctl_elem_value *ucontrol)
1107 {
1108 
1109 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1110 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1111 
1112 	ucontrol->value.integer.value[0] = tas_priv->cur_conf;
1113 
1114 	return 0;
1115 }
1116 
tasdevice_configuration_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1117 static int tasdevice_configuration_put(
1118 	struct snd_kcontrol *kcontrol,
1119 	struct snd_ctl_elem_value *ucontrol)
1120 {
1121 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1122 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1123 	unsigned int nr_configuration = ucontrol->value.integer.value[0];
1124 	int ret = 0;
1125 
1126 	if (tas_priv->cur_conf != nr_configuration) {
1127 		tas_priv->cur_conf = nr_configuration;
1128 		ret = 1;
1129 	}
1130 
1131 	return ret;
1132 }
1133 
tasdevice_active_num_get(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1134 static int tasdevice_active_num_get(struct snd_kcontrol *kcontrol,
1135 	struct snd_ctl_elem_value *ucontrol)
1136 {
1137 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1138 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1139 	struct i2c_client *clt = (struct i2c_client *)tas_priv->client;
1140 	struct tasdevice *tasdev = tas_priv->tasdevice;
1141 	int i;
1142 
1143 	for (i = 0; i < tas_priv->ndev; i++) {
1144 		if (clt->addr == tasdev[i].dev_addr) {
1145 			ucontrol->value.integer.value[0] = i;
1146 			return 0;
1147 		}
1148 	}
1149 
1150 	return -1;
1151 }
1152 
tasdevice_active_num_put(struct snd_kcontrol * kcontrol,struct snd_ctl_elem_value * ucontrol)1153 static int tasdevice_active_num_put(struct snd_kcontrol *kcontrol,
1154 	struct snd_ctl_elem_value *ucontrol)
1155 {
1156 	struct snd_soc_component *codec = snd_soc_kcontrol_component(kcontrol);
1157 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1158 	int dev_id = ucontrol->value.integer.value[0];
1159 	int max = tas_priv->ndev - 1;
1160 
1161 	dev_id = clamp(dev_id, 0, max);
1162 
1163 	guard(mutex)(&tas_priv->codec_lock);
1164 	return tasdev_chn_switch(tas_priv, dev_id);
1165 }
1166 
tasdevice_dsp_create_ctrls(struct tasdevice_priv * tas_priv)1167 static int tasdevice_dsp_create_ctrls(struct tasdevice_priv *tas_priv)
1168 {
1169 	struct snd_kcontrol_new *dsp_ctrls;
1170 	char *active_dev_num, *chip_id;
1171 	char *conf_name, *prog_name;
1172 	int nr_controls = 4;
1173 	int mix_index = 0;
1174 
1175 	/* Alloc kcontrol via devm_kzalloc, which don't manually
1176 	 * free the kcontrol
1177 	 */
1178 	dsp_ctrls = devm_kcalloc(tas_priv->dev, nr_controls,
1179 		sizeof(dsp_ctrls[0]), GFP_KERNEL);
1180 	if (!dsp_ctrls)
1181 		return -ENOMEM;
1182 
1183 	/* Create mixer items for selecting the active Program and Config */
1184 	prog_name = devm_kstrdup(tas_priv->dev, "Speaker Program Id",
1185 		GFP_KERNEL);
1186 	if (!prog_name)
1187 		return -ENOMEM;
1188 
1189 	dsp_ctrls[mix_index].name = prog_name;
1190 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1191 	dsp_ctrls[mix_index].info = tasdevice_info_programs;
1192 	dsp_ctrls[mix_index].get = tasdevice_program_get;
1193 	dsp_ctrls[mix_index].put = tasdevice_program_put;
1194 	mix_index++;
1195 
1196 	conf_name = devm_kstrdup(tas_priv->dev, "Speaker Config Id",
1197 		GFP_KERNEL);
1198 	if (!conf_name)
1199 		return -ENOMEM;
1200 
1201 	dsp_ctrls[mix_index].name = conf_name;
1202 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1203 	dsp_ctrls[mix_index].info = tasdevice_info_configurations;
1204 	dsp_ctrls[mix_index].get = tasdevice_configuration_get;
1205 	dsp_ctrls[mix_index].put = tasdevice_configuration_put;
1206 	mix_index++;
1207 
1208 	active_dev_num = devm_kstrdup(tas_priv->dev, "Activate Tasdevice Num",
1209 		GFP_KERNEL);
1210 	if (!active_dev_num)
1211 		return -ENOMEM;
1212 
1213 	dsp_ctrls[mix_index].name = active_dev_num;
1214 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1215 	dsp_ctrls[mix_index].info = tasdevice_info_active_num;
1216 	dsp_ctrls[mix_index].get = tasdevice_active_num_get;
1217 	dsp_ctrls[mix_index].put = tasdevice_active_num_put;
1218 	mix_index++;
1219 
1220 	chip_id = devm_kstrdup(tas_priv->dev, "Tasdevice Chip Id", GFP_KERNEL);
1221 	if (!chip_id)
1222 		return -ENOMEM;
1223 
1224 	dsp_ctrls[mix_index].name = chip_id;
1225 	dsp_ctrls[mix_index].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1226 	dsp_ctrls[mix_index].info = tasdevice_info_chip_id;
1227 	dsp_ctrls[mix_index].get = tasdevice_get_chip_id;
1228 	mix_index++;
1229 
1230 	return snd_soc_add_component_controls(tas_priv->codec, dsp_ctrls,
1231 		nr_controls < mix_index ? nr_controls : mix_index);
1232 }
1233 
cali_reg_update(struct bulk_reg_val * p,struct fct_param_address * t)1234 static void cali_reg_update(struct bulk_reg_val *p,
1235 	struct fct_param_address *t)
1236 {
1237 	const int sum = ARRAY_SIZE(tas2781_cali_start_reg);
1238 	int reg, j;
1239 
1240 	for (j = 0; j < sum; j++) {
1241 		switch (tas2781_cali_start_reg[j].reg) {
1242 		case 0:
1243 			reg = TASDEVICE_REG(t->thr[0], t->thr[1], t->thr[2]);
1244 			break;
1245 		case TAS2781_PRM_PLT_FLAG_REG:
1246 			reg = TASDEVICE_REG(t->plt_flg[0], t->plt_flg[1],
1247 				t->plt_flg[2]);
1248 			break;
1249 		case TAS2781_PRM_SINEGAIN_REG:
1250 			reg = TASDEVICE_REG(t->sin_gn[0], t->sin_gn[1],
1251 				t->sin_gn[2]);
1252 			break;
1253 		case TAS2781_PRM_SINEGAIN2_REG:
1254 			reg = TASDEVICE_REG(t->sin_gn[0], t->sin_gn[1],
1255 				t->sin_gn[2]);
1256 			break;
1257 		default:
1258 			reg = 0;
1259 			break;
1260 		}
1261 		if (reg)
1262 			p[j].reg = reg;
1263 	}
1264 }
1265 
alpa_cali_update(struct bulk_reg_val * p,struct fct_param_address * t)1266 static void alpa_cali_update(struct bulk_reg_val *p,
1267 	struct fct_param_address *t)
1268 {
1269 	p->is_locked = false;
1270 	p->reg = TASDEVICE_REG(t->thr2[0], t->thr2[1], t->thr2[2]);
1271 	p->val_len = 4;
1272 }
1273 
tasdevice_create_cali_ctrls(struct tasdevice_priv * priv)1274 static int tasdevice_create_cali_ctrls(struct tasdevice_priv *priv)
1275 {
1276 	struct calidata *cali_data = &priv->cali_data;
1277 	struct tasdevice *tasdev = priv->tasdevice;
1278 	struct tasdevice_fw *fmw = priv->fmw;
1279 	struct soc_bytes_ext *ext_cali_data;
1280 	struct snd_kcontrol_new *cali_ctrls;
1281 	unsigned int nctrls;
1282 	char *cali_name;
1283 	int rc, i;
1284 
1285 	rc = snd_soc_add_component_controls(priv->codec,
1286 		tasdevice_cali_controls, ARRAY_SIZE(tasdevice_cali_controls));
1287 	if (rc < 0) {
1288 		dev_err(priv->dev, "%s: Add cali controls err rc = %d",
1289 			__func__, rc);
1290 		return rc;
1291 	}
1292 
1293 	if (priv->chip_id == TAS2781) {
1294 		struct fct_param_address *t = &(fmw->fct_par_addr);
1295 
1296 		cali_ctrls = (struct snd_kcontrol_new *)tas2781_cali_controls;
1297 		nctrls = ARRAY_SIZE(tas2781_cali_controls);
1298 		for (i = 0; i < priv->ndev; i++) {
1299 			struct bulk_reg_val *p;
1300 
1301 			p = tasdev[i].cali_data_backup =
1302 				kmemdup(tas2781_cali_start_reg,
1303 				sizeof(tas2781_cali_start_reg), GFP_KERNEL);
1304 			if (!tasdev[i].cali_data_backup)
1305 				return -ENOMEM;
1306 			if (priv->dspbin_typ) {
1307 				cali_reg_update(p, t);
1308 				if (priv->dspbin_typ == TASDEV_ALPHA) {
1309 					p = &tasdev[i].alp_cali_bckp;
1310 					alpa_cali_update(p, t);
1311 				}
1312 			}
1313 		}
1314 	} else {
1315 		cali_ctrls = (struct snd_kcontrol_new *)tas2563_cali_controls;
1316 		nctrls = ARRAY_SIZE(tas2563_cali_controls);
1317 		for (i = 0; i < priv->ndev; i++) {
1318 			tasdev[i].cali_data_backup =
1319 				kmemdup(tas2563_cali_start_reg,
1320 				sizeof(tas2563_cali_start_reg), GFP_KERNEL);
1321 			if (!tasdev[i].cali_data_backup)
1322 				return -ENOMEM;
1323 		}
1324 	}
1325 
1326 	rc = snd_soc_add_component_controls(priv->codec, cali_ctrls, nctrls);
1327 	if (rc < 0) {
1328 		dev_err(priv->dev, "%s: Add chip cali ctrls err rc = %d",
1329 			__func__, rc);
1330 		return rc;
1331 	}
1332 
1333 	/* index for cali_ctrls */
1334 	i = 0;
1335 	if (priv->chip_id == TAS2781)
1336 		nctrls = 2;
1337 	else
1338 		nctrls = 1;
1339 
1340 	/*
1341 	 * Alloc kcontrol via devm_kzalloc(), which don't manually
1342 	 * free the kcontrol。
1343 	 */
1344 	cali_ctrls = devm_kcalloc(priv->dev, nctrls,
1345 		sizeof(cali_ctrls[0]), GFP_KERNEL);
1346 	if (!cali_ctrls)
1347 		return -ENOMEM;
1348 
1349 	ext_cali_data = devm_kzalloc(priv->dev, sizeof(*ext_cali_data),
1350 		GFP_KERNEL);
1351 	if (!ext_cali_data)
1352 		return -ENOMEM;
1353 
1354 	cali_name = devm_kstrdup(priv->dev, "Speaker Calibrated Data",
1355 		GFP_KERNEL);
1356 	if (!cali_name)
1357 		return -ENOMEM;
1358 	/* the number of calibrated data per tas2563/tas2781 */
1359 	cali_data->cali_dat_sz_per_dev = 20;
1360 	/*
1361 	 * Data structure for tas2563/tas2781 calibrated data:
1362 	 *	Pkg len (1 byte)
1363 	 *	Reg id (1 byte, constant 'r')
1364 	 *	book, page, register array for calibrated data (15 bytes)
1365 	 *	for (i = 0; i < Device-Sum; i++) {
1366 	 *		Device #i index_info (1 byte)
1367 	 *		Calibrated data for Device #i (20 bytes)
1368 	 *	}
1369 	 */
1370 	ext_cali_data->max = priv->ndev *
1371 		(cali_data->cali_dat_sz_per_dev + 1) + 1 + 15 + 1;
1372 	priv->cali_data.total_sz = priv->ndev *
1373 		(cali_data->cali_dat_sz_per_dev + 1);
1374 	cali_ctrls[i].name = cali_name;
1375 	cali_ctrls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1376 	cali_ctrls[i].info = snd_soc_bytes_info_ext;
1377 	cali_ctrls[i].get = tasdev_cali_data_get;
1378 	cali_ctrls[i].put = tasdev_cali_data_put;
1379 	cali_ctrls[i].private_value = (unsigned long)ext_cali_data;
1380 	i++;
1381 
1382 	cali_data->data = devm_kzalloc(priv->dev, cali_data->total_sz,
1383 		GFP_KERNEL);
1384 	if (!cali_data->data)
1385 		return -ENOMEM;
1386 
1387 	if (priv->chip_id == TAS2781) {
1388 		struct soc_bytes_ext *ext_cali_start;
1389 		char *cali_start_name;
1390 
1391 		ext_cali_start = devm_kzalloc(priv->dev,
1392 			sizeof(*ext_cali_start), GFP_KERNEL);
1393 		if (!ext_cali_start)
1394 			return -ENOMEM;
1395 
1396 		cali_start_name = devm_kstrdup(priv->dev,
1397 			"Calibration Start", GFP_KERNEL);
1398 		if (!cali_start_name)
1399 			return -ENOMEM;
1400 		/*
1401 		 * package structure for tas2781 ftc start:
1402 		 *	Pkg len (1 byte)
1403 		 *	Reg id (1 byte, constant 'r')
1404 		 *	book, page, register for pilot threshold, pilot tone
1405 		 *		and sine gain (12 bytes)
1406 		 *	for (i = 0; i < Device-Sum; i++) {
1407 		 *		Device #i index_info (1 byte)
1408 		 *		Sine gain for Device #i (8 bytes)
1409 		 *	}
1410 		 */
1411 		ext_cali_start->max = 14 + priv->ndev * 9;
1412 		cali_ctrls[i].name = cali_start_name;
1413 		cali_ctrls[i].iface = SNDRV_CTL_ELEM_IFACE_MIXER;
1414 		cali_ctrls[i].info = snd_soc_bytes_info_ext;
1415 		cali_ctrls[i].put = tas2781_calib_start_put;
1416 		cali_ctrls[i].get = tasdev_nop_get;
1417 		cali_ctrls[i].private_value = (unsigned long)ext_cali_start;
1418 		i++;
1419 	}
1420 
1421 	return snd_soc_add_component_controls(priv->codec, cali_ctrls,
1422 		nctrls < i ? nctrls : i);
1423 }
1424 
tasdevice_fw_ready(const struct firmware * fmw,void * context)1425 static void tasdevice_fw_ready(const struct firmware *fmw,
1426 	void *context)
1427 {
1428 	struct tasdevice_priv *tas_priv = context;
1429 	int ret = 0;
1430 	int i;
1431 
1432 	mutex_lock(&tas_priv->codec_lock);
1433 
1434 	ret = tasdevice_rca_parser(tas_priv, fmw);
1435 	if (ret) {
1436 		tasdevice_config_info_remove(tas_priv);
1437 		goto out;
1438 	}
1439 	tasdevice_create_control(tas_priv);
1440 
1441 	tasdevice_dsp_remove(tas_priv);
1442 	tasdevice_calbin_remove(tas_priv);
1443 	/*
1444 	 * The baseline is the RCA-only case, and then the code attempts to
1445 	 * load DSP firmware but in case of failures just keep going, i.e.
1446 	 * failing to load DSP firmware is NOT an error.
1447 	 */
1448 	tas_priv->fw_state = TASDEVICE_RCA_FW_OK;
1449 	if (tas_priv->name_prefix)
1450 		scnprintf(tas_priv->coef_binaryname, 64, "%s-%s_coef.bin",
1451 			tas_priv->name_prefix, tas_priv->dev_name);
1452 	else
1453 		scnprintf(tas_priv->coef_binaryname, 64, "%s_coef.bin",
1454 			tas_priv->dev_name);
1455 	ret = tasdevice_dsp_parser(tas_priv);
1456 	if (ret) {
1457 		dev_err(tas_priv->dev, "dspfw load %s error\n",
1458 			tas_priv->coef_binaryname);
1459 		goto out;
1460 	}
1461 
1462 	/*
1463 	 * If no dsp-related kcontrol created, the dsp resource will be freed.
1464 	 */
1465 	ret = tasdevice_dsp_create_ctrls(tas_priv);
1466 	if (ret) {
1467 		dev_err(tas_priv->dev, "dsp controls error\n");
1468 		goto out;
1469 	}
1470 
1471 	ret = tasdevice_create_cali_ctrls(tas_priv);
1472 	if (ret) {
1473 		dev_err(tas_priv->dev, "cali controls error\n");
1474 		goto out;
1475 	}
1476 
1477 	tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
1478 
1479 	/* If calibrated data occurs error, dsp will still works with default
1480 	 * calibrated data inside algo.
1481 	 */
1482 	for (i = 0; i < tas_priv->ndev; i++) {
1483 		if (tas_priv->name_prefix)
1484 			scnprintf(tas_priv->cal_binaryname[i], 64,
1485 				"%s-%s_cal_0x%02x.bin", tas_priv->name_prefix,
1486 				tas_priv->dev_name,
1487 				tas_priv->tasdevice[i].dev_addr);
1488 		else
1489 			scnprintf(tas_priv->cal_binaryname[i], 64,
1490 				"%s_cal_0x%02x.bin", tas_priv->dev_name,
1491 				tas_priv->tasdevice[i].dev_addr);
1492 		ret = tas2781_load_calibration(tas_priv,
1493 			tas_priv->cal_binaryname[i], i);
1494 		if (ret != 0)
1495 			dev_err(tas_priv->dev,
1496 				"%s: load %s error, default will effect\n",
1497 				__func__, tas_priv->cal_binaryname[i]);
1498 	}
1499 
1500 	tasdevice_prmg_load(tas_priv, 0);
1501 	tas_priv->cur_prog = 0;
1502 out:
1503 	if (tas_priv->fw_state == TASDEVICE_RCA_FW_OK) {
1504 		/* If DSP FW fail, DSP kcontrol won't be created. */
1505 		tasdevice_dsp_remove(tas_priv);
1506 	}
1507 	mutex_unlock(&tas_priv->codec_lock);
1508 	if (fmw)
1509 		release_firmware(fmw);
1510 }
1511 
tasdevice_dapm_event(struct snd_soc_dapm_widget * w,struct snd_kcontrol * kcontrol,int event)1512 static int tasdevice_dapm_event(struct snd_soc_dapm_widget *w,
1513 			struct snd_kcontrol *kcontrol, int event)
1514 {
1515 	struct snd_soc_component *codec = snd_soc_dapm_to_component(w->dapm);
1516 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1517 	int state = 0;
1518 
1519 	/* Codec Lock Hold */
1520 	mutex_lock(&tas_priv->codec_lock);
1521 	if (event == SND_SOC_DAPM_PRE_PMD)
1522 		state = 1;
1523 	tasdevice_tuning_switch(tas_priv, state);
1524 	/* Codec Lock Release*/
1525 	mutex_unlock(&tas_priv->codec_lock);
1526 
1527 	return 0;
1528 }
1529 
1530 static const struct snd_soc_dapm_widget tasdevice_dapm_widgets[] = {
1531 	SND_SOC_DAPM_AIF_IN("ASI", "ASI Playback", 0, SND_SOC_NOPM, 0, 0),
1532 	SND_SOC_DAPM_AIF_OUT_E("ASI OUT", "ASI Capture", 0, SND_SOC_NOPM,
1533 		0, 0, tasdevice_dapm_event,
1534 		SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1535 	SND_SOC_DAPM_SPK("SPK", tasdevice_dapm_event),
1536 	SND_SOC_DAPM_OUTPUT("OUT"),
1537 	SND_SOC_DAPM_INPUT("DMIC"),
1538 };
1539 
1540 static const struct snd_soc_dapm_route tasdevice_audio_map[] = {
1541 	{"SPK", NULL, "ASI"},
1542 	{"OUT", NULL, "SPK"},
1543 	{"ASI OUT", NULL, "DMIC"},
1544 };
1545 
tasdevice_startup(struct snd_pcm_substream * substream,struct snd_soc_dai * dai)1546 static int tasdevice_startup(struct snd_pcm_substream *substream,
1547 						struct snd_soc_dai *dai)
1548 {
1549 	struct snd_soc_component *codec = dai->component;
1550 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1551 
1552 	switch (tas_priv->fw_state) {
1553 	case TASDEVICE_RCA_FW_OK:
1554 	case TASDEVICE_DSP_FW_ALL_OK:
1555 		return 0;
1556 	default:
1557 		return -EINVAL;
1558 	}
1559 }
1560 
tasdevice_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,struct snd_soc_dai * dai)1561 static int tasdevice_hw_params(struct snd_pcm_substream *substream,
1562 	struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
1563 {
1564 	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(dai);
1565 	unsigned int slot_width;
1566 	unsigned int fsrate;
1567 	int bclk_rate;
1568 
1569 	fsrate = params_rate(params);
1570 	switch (fsrate) {
1571 	case 48000:
1572 	case 44100:
1573 		break;
1574 	default:
1575 		dev_err(tas_priv->dev, "%s: incorrect sample rate = %u\n",
1576 			__func__, fsrate);
1577 		return -EINVAL;
1578 	}
1579 
1580 	slot_width = params_width(params);
1581 	switch (slot_width) {
1582 	case 16:
1583 	case 20:
1584 	case 24:
1585 	case 32:
1586 		break;
1587 	default:
1588 		dev_err(tas_priv->dev, "%s: incorrect slot width = %u\n",
1589 			__func__, slot_width);
1590 		return -EINVAL;
1591 	}
1592 
1593 	bclk_rate = snd_soc_params_to_bclk(params);
1594 	if (bclk_rate < 0) {
1595 		dev_err(tas_priv->dev, "%s: incorrect bclk rate = %d\n",
1596 			__func__, bclk_rate);
1597 		return bclk_rate;
1598 	}
1599 
1600 	return 0;
1601 }
1602 
tasdevice_set_dai_sysclk(struct snd_soc_dai * codec_dai,int clk_id,unsigned int freq,int dir)1603 static int tasdevice_set_dai_sysclk(struct snd_soc_dai *codec_dai,
1604 	int clk_id, unsigned int freq, int dir)
1605 {
1606 	struct tasdevice_priv *tas_priv = snd_soc_dai_get_drvdata(codec_dai);
1607 
1608 	tas_priv->sysclk = freq;
1609 
1610 	return 0;
1611 }
1612 
1613 static const struct snd_soc_dai_ops tasdevice_dai_ops = {
1614 	.startup = tasdevice_startup,
1615 	.hw_params = tasdevice_hw_params,
1616 	.set_sysclk = tasdevice_set_dai_sysclk,
1617 };
1618 
1619 static struct snd_soc_dai_driver tasdevice_dai_driver[] = {
1620 	{
1621 		.name = "tasdev_codec",
1622 		.id = 0,
1623 		.playback = {
1624 			.stream_name = "Playback",
1625 			.channels_min = 1,
1626 			.channels_max = 4,
1627 			.rates	 = TASDEVICE_RATES,
1628 			.formats	= TASDEVICE_FORMATS,
1629 		},
1630 		.capture = {
1631 			.stream_name = "Capture",
1632 			.channels_min = 1,
1633 			.channels_max = 4,
1634 			.rates	 = TASDEVICE_RATES,
1635 			.formats	= TASDEVICE_FORMATS,
1636 		},
1637 		.ops = &tasdevice_dai_ops,
1638 		.symmetric_rate = 1,
1639 	},
1640 };
1641 
tasdevice_codec_probe(struct snd_soc_component * codec)1642 static int tasdevice_codec_probe(struct snd_soc_component *codec)
1643 {
1644 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1645 	struct snd_kcontrol_new *p;
1646 	unsigned int size;
1647 	int rc;
1648 
1649 	switch (tas_priv->chip_id) {
1650 	case TAS2781:
1651 		p = (struct snd_kcontrol_new *)tas2781_snd_controls;
1652 		size = ARRAY_SIZE(tas2781_snd_controls);
1653 		break;
1654 	default:
1655 		p = (struct snd_kcontrol_new *)tas2563_snd_controls;
1656 		size = ARRAY_SIZE(tas2563_snd_controls);
1657 	}
1658 
1659 	rc = snd_soc_add_component_controls(codec, p, size);
1660 	if (rc < 0) {
1661 		dev_err(tas_priv->dev, "%s: Add control err rc = %d",
1662 			__func__, rc);
1663 		return rc;
1664 	}
1665 
1666 	tas_priv->name_prefix = codec->name_prefix;
1667 	return tascodec_init(tas_priv, codec, THIS_MODULE, tasdevice_fw_ready);
1668 }
1669 
tasdevice_deinit(void * context)1670 static void tasdevice_deinit(void *context)
1671 {
1672 	struct tasdevice_priv *tas_priv = (struct tasdevice_priv *) context;
1673 	struct tasdevice *tasdev = tas_priv->tasdevice;
1674 	int i;
1675 
1676 	for (i = 0; i < tas_priv->ndev; i++)
1677 		kfree(tasdev[i].cali_data_backup);
1678 
1679 	tasdevice_config_info_remove(tas_priv);
1680 	tasdevice_dsp_remove(tas_priv);
1681 	tasdevice_calbin_remove(tas_priv);
1682 	tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
1683 }
1684 
tasdevice_codec_remove(struct snd_soc_component * codec)1685 static void tasdevice_codec_remove(struct snd_soc_component *codec)
1686 {
1687 	struct tasdevice_priv *tas_priv = snd_soc_component_get_drvdata(codec);
1688 
1689 	tasdevice_deinit(tas_priv);
1690 }
1691 
1692 static const struct snd_soc_component_driver
1693 	soc_codec_driver_tasdevice = {
1694 	.probe			= tasdevice_codec_probe,
1695 	.remove			= tasdevice_codec_remove,
1696 	.controls		= tasdevice_snd_controls,
1697 	.num_controls		= ARRAY_SIZE(tasdevice_snd_controls),
1698 	.dapm_widgets		= tasdevice_dapm_widgets,
1699 	.num_dapm_widgets	= ARRAY_SIZE(tasdevice_dapm_widgets),
1700 	.dapm_routes		= tasdevice_audio_map,
1701 	.num_dapm_routes	= ARRAY_SIZE(tasdevice_audio_map),
1702 	.idle_bias_on		= 1,
1703 	.endianness		= 1,
1704 };
1705 
tasdevice_parse_dt(struct tasdevice_priv * tas_priv)1706 static void tasdevice_parse_dt(struct tasdevice_priv *tas_priv)
1707 {
1708 	struct i2c_client *client = (struct i2c_client *)tas_priv->client;
1709 	unsigned int dev_addrs[TASDEVICE_MAX_CHANNELS];
1710 	int i, ndev = 0;
1711 
1712 	if (tas_priv->isacpi) {
1713 		ndev = device_property_read_u32_array(&client->dev,
1714 			"ti,audio-slots", NULL, 0);
1715 		if (ndev <= 0) {
1716 			ndev = 1;
1717 			dev_addrs[0] = client->addr;
1718 		} else {
1719 			ndev = (ndev < ARRAY_SIZE(dev_addrs))
1720 				? ndev : ARRAY_SIZE(dev_addrs);
1721 			ndev = device_property_read_u32_array(&client->dev,
1722 				"ti,audio-slots", dev_addrs, ndev);
1723 		}
1724 
1725 		tas_priv->irq =
1726 			acpi_dev_gpio_irq_get(ACPI_COMPANION(&client->dev), 0);
1727 	} else if (IS_ENABLED(CONFIG_OF)) {
1728 		struct device_node *np = tas_priv->dev->of_node;
1729 		u64 addr;
1730 
1731 		for (i = 0; i < TASDEVICE_MAX_CHANNELS; i++) {
1732 			if (of_property_read_reg(np, i, &addr, NULL))
1733 				break;
1734 			dev_addrs[ndev++] = addr;
1735 		}
1736 
1737 		tas_priv->irq = of_irq_get(np, 0);
1738 	} else {
1739 		ndev = 1;
1740 		dev_addrs[0] = client->addr;
1741 	}
1742 	tas_priv->ndev = ndev;
1743 	for (i = 0; i < ndev; i++)
1744 		tas_priv->tasdevice[i].dev_addr = dev_addrs[i];
1745 
1746 	tas_priv->reset = devm_gpiod_get_optional(&client->dev,
1747 			"reset", GPIOD_OUT_HIGH);
1748 	if (IS_ERR(tas_priv->reset))
1749 		dev_err(tas_priv->dev, "%s Can't get reset GPIO\n",
1750 			__func__);
1751 
1752 	strcpy(tas_priv->dev_name, tasdevice_id[tas_priv->chip_id].name);
1753 }
1754 
tasdevice_i2c_probe(struct i2c_client * i2c)1755 static int tasdevice_i2c_probe(struct i2c_client *i2c)
1756 {
1757 	const struct acpi_device_id *acpi_id;
1758 	struct tasdevice_priv *tas_priv;
1759 	int ret;
1760 
1761 	tas_priv = tasdevice_kzalloc(i2c);
1762 	if (!tas_priv)
1763 		return -ENOMEM;
1764 
1765 	dev_set_drvdata(&i2c->dev, tas_priv);
1766 
1767 	if (ACPI_HANDLE(&i2c->dev)) {
1768 		acpi_id = acpi_match_device(i2c->dev.driver->acpi_match_table,
1769 				&i2c->dev);
1770 		if (!acpi_id) {
1771 			dev_err(&i2c->dev, "No driver data\n");
1772 			ret = -EINVAL;
1773 			goto err;
1774 		}
1775 		tas_priv->chip_id = acpi_id->driver_data;
1776 		tas_priv->isacpi = true;
1777 	} else {
1778 		tas_priv->chip_id = (uintptr_t)i2c_get_match_data(i2c);
1779 		tas_priv->isacpi = false;
1780 	}
1781 
1782 	tasdevice_parse_dt(tas_priv);
1783 
1784 	ret = tasdevice_init(tas_priv);
1785 	if (ret)
1786 		goto err;
1787 
1788 	tasdevice_reset(tas_priv);
1789 
1790 	ret = devm_snd_soc_register_component(tas_priv->dev,
1791 		&soc_codec_driver_tasdevice,
1792 		tasdevice_dai_driver, ARRAY_SIZE(tasdevice_dai_driver));
1793 	if (ret) {
1794 		dev_err(tas_priv->dev, "%s: codec register error:0x%08x\n",
1795 			__func__, ret);
1796 		goto err;
1797 	}
1798 err:
1799 	if (ret < 0)
1800 		tasdevice_remove(tas_priv);
1801 	return ret;
1802 }
1803 
tasdevice_i2c_remove(struct i2c_client * client)1804 static void tasdevice_i2c_remove(struct i2c_client *client)
1805 {
1806 	struct tasdevice_priv *tas_priv = i2c_get_clientdata(client);
1807 
1808 	tasdevice_remove(tas_priv);
1809 }
1810 
1811 #ifdef CONFIG_ACPI
1812 static const struct acpi_device_id tasdevice_acpi_match[] = {
1813 	{ "TAS2781", TAS2781 },
1814 	{},
1815 };
1816 
1817 MODULE_DEVICE_TABLE(acpi, tasdevice_acpi_match);
1818 #endif
1819 
1820 static struct i2c_driver tasdevice_i2c_driver = {
1821 	.driver = {
1822 		.name = "tasdev-codec",
1823 		.of_match_table = of_match_ptr(tasdevice_of_match),
1824 #ifdef CONFIG_ACPI
1825 		.acpi_match_table = ACPI_PTR(tasdevice_acpi_match),
1826 #endif
1827 	},
1828 	.probe	= tasdevice_i2c_probe,
1829 	.remove = tasdevice_i2c_remove,
1830 	.id_table = tasdevice_id,
1831 };
1832 
1833 module_i2c_driver(tasdevice_i2c_driver);
1834 
1835 MODULE_AUTHOR("Shenghao Ding <shenghao-ding@ti.com>");
1836 MODULE_AUTHOR("Kevin Lu <kevin-lu@ti.com>");
1837 MODULE_DESCRIPTION("ASoC TAS2781 Driver");
1838 MODULE_LICENSE("GPL");
1839 MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");
1840