Debian Codegraph @

sound/soc/codecs/tas2781-i2c.c

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