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