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