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