1 // SPDX-License-Identifier: GPL-2.0
3 // ALSA SoC Texas Instruments TAS2563/TAS2781 Audio Smart Amplifier
5 // Copyright (C) 2022 - 2024 Texas Instruments Incorporated
8 // The TAS2563/TAS2781 driver implements a flexible and configurable
9 // algo coefficient setting for one, two, or even multiple
10 // TAS2563/TAS2781 chips.
12 // Author: Shenghao Ding <shenghao-ding@ti.com>
13 // Author: Kevin Lu <kevin-lu@ti.com>
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>
24 #include <linux/of_address.h>
25 #include <linux/of_irq.h>
26 #include <linux/regmap.h>
27 #include <linux/slab.h>
28 #include <sound/pcm_params.h>
29 #include <sound/soc.h>
30 #include <sound/tas2781.h>
31 #include <sound/tlv.h>
32 #include <sound/tas2563-tlv.h>
33 #include <sound/tas2781-tlv.h>
34 #include <linux/unaligned.h>
36 #define X2563_CL_STT_VAL(xreg, xval) \
41 #define X2563_CL_STT_4BYTS(xreg, byte0, byte1, byte2, byte3) \
43 .val = { byte0, byte1, byte2, byte3 }, \
46 static const struct bulk_reg_val tas2563_cali_start_reg
[] = {
47 X2563_CL_STT_VAL(TAS2563_IDLE
, 0x00),
48 X2563_CL_STT_4BYTS(TAS2563_PRM_ENFF_REG
, 0x40, 0x00, 0x00, 0x00),
49 X2563_CL_STT_4BYTS(TAS2563_PRM_DISTCK_REG
, 0x40, 0x00, 0x00, 0x00),
50 X2563_CL_STT_4BYTS(TAS2563_PRM_TE_SCTHR_REG
, 0x7f, 0xff, 0xff, 0xff),
51 X2563_CL_STT_4BYTS(TAS2563_PRM_PLT_FLAG_REG
, 0x40, 0x00, 0x00, 0x00),
52 X2563_CL_STT_4BYTS(TAS2563_PRM_SINEGAIN_REG
, 0x0a, 0x3d, 0x70, 0xa4),
53 X2563_CL_STT_4BYTS(TAS2563_TE_TA1_REG
, 0x00, 0x36, 0x91, 0x5e),
54 X2563_CL_STT_4BYTS(TAS2563_TE_TA1_AT_REG
, 0x00, 0x36, 0x91, 0x5e),
55 X2563_CL_STT_4BYTS(TAS2563_TE_TA2_REG
, 0x00, 0x06, 0xd3, 0x72),
56 X2563_CL_STT_4BYTS(TAS2563_TE_AT_REG
, 0x00, 0x36, 0x91, 0x5e),
57 X2563_CL_STT_4BYTS(TAS2563_TE_DT_REG
, 0x00, 0x36, 0x91, 0x5e),
60 #define X2781_CL_STT_VAL(xreg, xval, xlocked) \
64 .is_locked = xlocked, }
66 #define X2781_CL_STT_4BYTS_UNLOCKED(xreg, byte0, byte1, byte2, byte3) \
68 .val = { byte0, byte1, byte2, byte3 }, \
72 #define X2781_CL_STT_LEN_UNLOCKED(xreg) \
77 static const struct bulk_reg_val tas2781_cali_start_reg
[] = {
78 X2781_CL_STT_VAL(TAS2781_PRM_INT_MASK_REG
, 0xfe, false),
79 X2781_CL_STT_VAL(TAS2781_PRM_CLK_CFG_REG
, 0xdd, false),
80 X2781_CL_STT_VAL(TAS2781_PRM_RSVD_REG
, 0x20, false),
81 X2781_CL_STT_VAL(TAS2781_PRM_TEST_57_REG
, 0x14, false),
82 X2781_CL_STT_VAL(TAS2781_PRM_TEST_62_REG
, 0x45, true),
83 X2781_CL_STT_VAL(TAS2781_PRM_PVDD_UVLO_REG
, 0x03, false),
84 X2781_CL_STT_VAL(TAS2781_PRM_CHNL_0_REG
, 0xa8, false),
85 X2781_CL_STT_VAL(TAS2781_PRM_NG_CFG0_REG
, 0xb9, false),
86 X2781_CL_STT_VAL(TAS2781_PRM_IDLE_CH_DET_REG
, 0x92, false),
88 * This register is pilot tone threshold, different with the
89 * calibration tool version, it will be updated in
90 * tas2781_calib_start_put(), set to 1mA.
92 X2781_CL_STT_4BYTS_UNLOCKED(0, 0x00, 0x00, 0x00, 0x56),
93 X2781_CL_STT_4BYTS_UNLOCKED(TAS2781_PRM_PLT_FLAG_REG
,
94 0x40, 0x00, 0x00, 0x00),
95 X2781_CL_STT_LEN_UNLOCKED(TAS2781_PRM_SINEGAIN_REG
),
96 X2781_CL_STT_LEN_UNLOCKED(TAS2781_PRM_SINEGAIN2_REG
),
99 static const struct i2c_device_id tasdevice_id
[] = {
100 { "tas2563", TAS2563
},
101 { "tas2781", TAS2781
},
104 MODULE_DEVICE_TABLE(i2c
, tasdevice_id
);
107 static const struct of_device_id tasdevice_of_match
[] = {
108 { .compatible
= "ti,tas2563" },
109 { .compatible
= "ti,tas2781" },
112 MODULE_DEVICE_TABLE(of
, tasdevice_of_match
);
116 * tas2781_digital_getvol - get the volum control
117 * @kcontrol: control pointer
118 * @ucontrol: User data
119 * Customer Kcontrol for tas2781 is primarily for regmap booking, paging
120 * depends on internal regmap mechanism.
121 * tas2781 contains book and page two-level register map, especially
122 * book switching will set the register BXXP00R7F, after switching to the
123 * correct book, then leverage the mechanism for paging to access the
126 static int tas2781_digital_getvol(struct snd_kcontrol
*kcontrol
,
127 struct snd_ctl_elem_value
*ucontrol
)
129 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
130 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
131 struct soc_mixer_control
*mc
=
132 (struct soc_mixer_control
*)kcontrol
->private_value
;
134 return tasdevice_digital_getvol(tas_priv
, ucontrol
, mc
);
137 static int tas2781_digital_putvol(struct snd_kcontrol
*kcontrol
,
138 struct snd_ctl_elem_value
*ucontrol
)
140 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
141 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
142 struct soc_mixer_control
*mc
=
143 (struct soc_mixer_control
*)kcontrol
->private_value
;
145 return tasdevice_digital_putvol(tas_priv
, ucontrol
, mc
);
148 static int tas2781_amp_getvol(struct snd_kcontrol
*kcontrol
,
149 struct snd_ctl_elem_value
*ucontrol
)
151 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
152 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
153 struct soc_mixer_control
*mc
=
154 (struct soc_mixer_control
*)kcontrol
->private_value
;
156 return tasdevice_amp_getvol(tas_priv
, ucontrol
, mc
);
159 static int tas2781_amp_putvol(struct snd_kcontrol
*kcontrol
,
160 struct snd_ctl_elem_value
*ucontrol
)
162 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
163 struct tasdevice_priv
*tas_priv
=
164 snd_soc_component_get_drvdata(codec
);
165 struct soc_mixer_control
*mc
=
166 (struct soc_mixer_control
*)kcontrol
->private_value
;
168 return tasdevice_amp_putvol(tas_priv
, ucontrol
, mc
);
171 static int tasdev_force_fwload_get(struct snd_kcontrol
*kcontrol
,
172 struct snd_ctl_elem_value
*ucontrol
)
174 struct snd_soc_component
*component
=
175 snd_soc_kcontrol_component(kcontrol
);
176 struct tasdevice_priv
*tas_priv
=
177 snd_soc_component_get_drvdata(component
);
179 ucontrol
->value
.integer
.value
[0] = (int)tas_priv
->force_fwload_status
;
180 dev_dbg(tas_priv
->dev
, "%s : Force FWload %s\n", __func__
,
181 tas_priv
->force_fwload_status
? "ON" : "OFF");
186 static int tasdev_force_fwload_put(struct snd_kcontrol
*kcontrol
,
187 struct snd_ctl_elem_value
*ucontrol
)
189 struct snd_soc_component
*component
=
190 snd_soc_kcontrol_component(kcontrol
);
191 struct tasdevice_priv
*tas_priv
=
192 snd_soc_component_get_drvdata(component
);
193 bool change
, val
= (bool)ucontrol
->value
.integer
.value
[0];
195 if (tas_priv
->force_fwload_status
== val
)
199 tas_priv
->force_fwload_status
= val
;
201 dev_dbg(tas_priv
->dev
, "%s : Force FWload %s\n", __func__
,
202 tas_priv
->force_fwload_status
? "ON" : "OFF");
207 static int tasdev_cali_data_get(struct snd_kcontrol
*kcontrol
,
208 struct snd_ctl_elem_value
*ucontrol
)
210 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
211 struct tasdevice_priv
*priv
= snd_soc_component_get_drvdata(comp
);
212 struct soc_bytes_ext
*bytes_ext
=
213 (struct soc_bytes_ext
*) kcontrol
->private_value
;
214 struct calidata
*cali_data
= &priv
->cali_data
;
215 struct cali_reg
*p
= &cali_data
->cali_reg_array
;
216 unsigned char *dst
= ucontrol
->value
.bytes
.data
;
217 unsigned char *data
= cali_data
->data
;
222 guard(mutex
)(&priv
->codec_lock
);
223 if (!priv
->is_user_space_calidata
)
229 dst
[i
++] = bytes_ext
->max
;
232 dst
[i
++] = TASDEVICE_BOOK_ID(p
->r0_reg
);
233 dst
[i
++] = TASDEVICE_PAGE_ID(p
->r0_reg
);
234 dst
[i
++] = TASDEVICE_PAGE_REG(p
->r0_reg
);
236 dst
[i
++] = TASDEVICE_BOOK_ID(p
->r0_low_reg
);
237 dst
[i
++] = TASDEVICE_PAGE_ID(p
->r0_low_reg
);
238 dst
[i
++] = TASDEVICE_PAGE_REG(p
->r0_low_reg
);
240 dst
[i
++] = TASDEVICE_BOOK_ID(p
->invr0_reg
);
241 dst
[i
++] = TASDEVICE_PAGE_ID(p
->invr0_reg
);
242 dst
[i
++] = TASDEVICE_PAGE_REG(p
->invr0_reg
);
244 dst
[i
++] = TASDEVICE_BOOK_ID(p
->pow_reg
);
245 dst
[i
++] = TASDEVICE_PAGE_ID(p
->pow_reg
);
246 dst
[i
++] = TASDEVICE_PAGE_REG(p
->pow_reg
);
248 dst
[i
++] = TASDEVICE_BOOK_ID(p
->tlimit_reg
);
249 dst
[i
++] = TASDEVICE_PAGE_ID(p
->tlimit_reg
);
250 dst
[i
++] = TASDEVICE_PAGE_REG(p
->tlimit_reg
);
252 for (j
= 0, k
= 0; j
< priv
->ndev
; j
++) {
257 dev_err(priv
->dev
, "chn %d device %u not match\n",
262 rc
= tasdevice_dev_bulk_read(priv
, j
, p
->r0_reg
, &dst
[i
], 4);
264 dev_err(priv
->dev
, "chn %d r0_reg bulk_rd err = %d\n",
270 rc
= memcmp(&dst
[i
], &data
[k
], 4);
272 dev_dbg(priv
->dev
, "chn %d r0_data is not same\n", j
);
275 rc
= tasdevice_dev_bulk_read(priv
, j
, p
->r0_low_reg
,
278 dev_err(priv
->dev
, "chn %d r0_low bulk_rd err = %d\n",
284 rc
= memcmp(&dst
[i
], &data
[k
], 4);
286 dev_dbg(priv
->dev
, "chn %d r0_low is not same\n", j
);
289 rc
= tasdevice_dev_bulk_read(priv
, j
, p
->invr0_reg
,
292 dev_err(priv
->dev
, "chn %d invr0 bulk_rd err = %d\n",
298 rc
= memcmp(&dst
[i
], &data
[k
], 4);
300 dev_dbg(priv
->dev
, "chn %d invr0 is not same\n", j
);
303 rc
= tasdevice_dev_bulk_read(priv
, j
, p
->pow_reg
, &dst
[i
], 4);
305 dev_err(priv
->dev
, "chn %d pow_reg bulk_rd err = %d\n",
311 rc
= memcmp(&dst
[i
], &data
[k
], 4);
313 dev_dbg(priv
->dev
, "chn %d pow_reg is not same\n", j
);
316 rc
= tasdevice_dev_bulk_read(priv
, j
, p
->tlimit_reg
,
319 dev_err(priv
->dev
, "chn %d tlimit bulk_rd err = %d\n",
322 rc
= memcmp(&dst
[i
], &data
[k
], 4);
324 dev_dbg(priv
->dev
, "chn %d tlimit is not same\n", j
);
331 static int calib_data_get(struct tasdevice_priv
*tas_priv
, int reg
,
334 struct i2c_client
*clt
= (struct i2c_client
*)tas_priv
->client
;
335 struct tasdevice
*tasdev
= tas_priv
->tasdevice
;
339 for (i
= 0; i
< tas_priv
->ndev
; i
++) {
340 if (clt
->addr
== tasdev
[i
].dev_addr
) {
341 /* First byte is the device index. */
343 rc
= tasdevice_dev_bulk_read(tas_priv
, i
, reg
, &dst
[1],
352 static void sngl_calib_start(struct tasdevice_priv
*tas_priv
, int i
,
353 int *reg
, unsigned char *dat
)
355 struct tasdevice
*tasdev
= tas_priv
->tasdevice
;
356 struct bulk_reg_val
*p
= tasdev
[i
].cali_data_backup
;
357 const int sum
= ARRAY_SIZE(tas2781_cali_start_reg
);
363 /* Store the current setting from the chip */
364 for (j
= 0; j
< sum
; j
++) {
365 if (p
[j
].val_len
== 1) {
367 tasdevice_dev_write(tas_priv
, i
,
368 TAS2781_TEST_UNLOCK_REG
,
369 TAS2781_TEST_PAGE_UNLOCK
);
370 tasdevice_dev_read(tas_priv
, i
, p
[j
].reg
,
371 (int *)&p
[j
].val
[0]);
380 case TAS2781_PRM_PLT_FLAG_REG
:
383 case TAS2781_PRM_SINEGAIN_REG
:
386 case TAS2781_PRM_SINEGAIN2_REG
:
390 tasdevice_dev_bulk_read(tas_priv
, i
, p
[j
].reg
,
395 /* Update the setting for calibration */
396 for (j
= 0; j
< sum
- 2; j
++) {
397 if (p
[j
].val_len
== 1) {
399 tasdevice_dev_write(tas_priv
, i
,
400 TAS2781_TEST_UNLOCK_REG
,
401 TAS2781_TEST_PAGE_UNLOCK
);
402 tasdevice_dev_write(tas_priv
, i
, p
[j
].reg
,
403 tas2781_cali_start_reg
[j
].val
[0]);
407 tasdevice_dev_bulk_write(tas_priv
, i
, p
[j
].reg
,
409 tas2781_cali_start_reg
[j
].val
, 4);
413 tasdevice_dev_bulk_write(tas_priv
, i
, p
[j
].reg
, &dat
[1], 4);
414 tasdevice_dev_bulk_write(tas_priv
, i
, p
[j
+ 1].reg
, &dat
[5], 4);
417 static int tas2781_calib_start_put(struct snd_kcontrol
*kcontrol
,
418 struct snd_ctl_elem_value
*ucontrol
)
420 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
421 struct tasdevice_priv
*priv
= snd_soc_component_get_drvdata(comp
);
422 struct soc_bytes_ext
*bytes_ext
=
423 (struct soc_bytes_ext
*) kcontrol
->private_value
;
424 unsigned char *dat
= ucontrol
->value
.bytes
.data
;
428 guard(mutex
)(&priv
->codec_lock
);
429 if (priv
->chip_id
!= TAS2781
|| bytes_ext
->max
!= dat
[0] ||
431 dev_err(priv
->dev
, "%s: package fmt or chipid incorrect\n",
436 /* refresh pilot tone and SineGain register */
437 for (i
= 0; i
< ARRAY_SIZE(reg
); i
++) {
438 reg
[i
] = TASDEVICE_REG(dat
[j
], dat
[j
+ 1], dat
[j
+ 2]);
442 for (i
= 0; i
< priv
->ndev
; i
++) {
446 dev_err(priv
->dev
, "%s:no cal-setting for dev %d\n",
450 sngl_calib_start(priv
, i
, reg
, dat
+ k
);
455 static void tas2781_calib_stop_put(struct tasdevice_priv
*tas_priv
)
457 const int sum
= ARRAY_SIZE(tas2781_cali_start_reg
);
460 for (i
= 0; i
< tas_priv
->ndev
; i
++) {
461 struct tasdevice
*tasdev
= tas_priv
->tasdevice
;
462 struct bulk_reg_val
*p
= tasdev
[i
].cali_data_backup
;
467 for (j
= 0; j
< sum
; j
++) {
468 if (p
[j
].val_len
== 1) {
470 tasdevice_dev_write(tas_priv
, i
,
471 TAS2781_TEST_UNLOCK_REG
,
472 TAS2781_TEST_PAGE_UNLOCK
);
473 tasdevice_dev_write(tas_priv
, i
, p
[j
].reg
,
478 tasdevice_dev_bulk_write(tas_priv
, i
, p
[j
].reg
,
485 static int tas2563_calib_start_put(struct snd_kcontrol
*kcontrol
,
486 struct snd_ctl_elem_value
*ucontrol
)
488 struct bulk_reg_val
*q
= (struct bulk_reg_val
*)tas2563_cali_start_reg
;
489 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
490 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(comp
);
491 const int sum
= ARRAY_SIZE(tas2563_cali_start_reg
);
495 guard(mutex
)(&tas_priv
->codec_lock
);
496 if (tas_priv
->chip_id
!= TAS2563
) {
501 for (i
= 0; i
< tas_priv
->ndev
; i
++) {
502 struct tasdevice
*tasdev
= tas_priv
->tasdevice
;
503 struct bulk_reg_val
*p
= tasdev
[i
].cali_data_backup
;
507 for (j
= 0; j
< sum
; j
++) {
508 if (p
[j
].val_len
== 1)
509 tasdevice_dev_read(tas_priv
,
511 (unsigned int *)&p
[j
].val
[0]);
513 tasdevice_dev_bulk_read(tas_priv
,
514 i
, p
[j
].reg
, p
[j
].val
, 4);
517 for (j
= 0; j
< sum
; j
++) {
518 if (p
[j
].val_len
== 1)
519 tasdevice_dev_write(tas_priv
, i
, p
[j
].reg
,
522 tasdevice_dev_bulk_write(tas_priv
, i
, p
[j
].reg
,
530 static void tas2563_calib_stop_put(struct tasdevice_priv
*tas_priv
)
532 const int sum
= ARRAY_SIZE(tas2563_cali_start_reg
);
535 for (i
= 0; i
< tas_priv
->ndev
; i
++) {
536 struct tasdevice
*tasdev
= tas_priv
->tasdevice
;
537 struct bulk_reg_val
*p
= tasdev
[i
].cali_data_backup
;
542 for (j
= 0; j
< sum
; j
++) {
543 if (p
[j
].val_len
== 1)
544 tasdevice_dev_write(tas_priv
, i
, p
[j
].reg
,
547 tasdevice_dev_bulk_write(tas_priv
, i
, p
[j
].reg
,
553 static int tasdev_calib_stop_put(struct snd_kcontrol
*kcontrol
,
554 struct snd_ctl_elem_value
*ucontrol
)
556 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
557 struct tasdevice_priv
*priv
= snd_soc_component_get_drvdata(comp
);
559 guard(mutex
)(&priv
->codec_lock
);
560 if (priv
->chip_id
== TAS2563
)
561 tas2563_calib_stop_put(priv
);
563 tas2781_calib_stop_put(priv
);
568 static int tasdev_cali_data_put(struct snd_kcontrol
*kcontrol
,
569 struct snd_ctl_elem_value
*ucontrol
)
571 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
572 struct tasdevice_priv
*priv
= snd_soc_component_get_drvdata(comp
);
573 struct soc_bytes_ext
*bytes_ext
=
574 (struct soc_bytes_ext
*) kcontrol
->private_value
;
575 struct calidata
*cali_data
= &priv
->cali_data
;
576 struct cali_reg
*p
= &cali_data
->cali_reg_array
;
577 unsigned char *src
= ucontrol
->value
.bytes
.data
;
578 unsigned char *dst
= cali_data
->data
;
582 guard(mutex
)(&priv
->codec_lock
);
583 if (src
[0] != bytes_ext
->max
|| src
[1] != 'r') {
584 dev_err(priv
->dev
, "%s: pkg fmt invalid\n", __func__
);
587 for (j
= 0; j
< priv
->ndev
; j
++) {
588 if (src
[17 + j
* 21] != j
) {
589 dev_err(priv
->dev
, "%s: pkg fmt invalid\n", __func__
);
594 priv
->is_user_space_calidata
= true;
596 p
->r0_reg
= TASDEVICE_REG(src
[i
], src
[i
+ 1], src
[i
+ 2]);
598 p
->r0_low_reg
= TASDEVICE_REG(src
[i
], src
[i
+ 1], src
[i
+ 2]);
600 p
->invr0_reg
= TASDEVICE_REG(src
[i
], src
[i
+ 1], src
[i
+ 2]);
602 p
->pow_reg
= TASDEVICE_REG(src
[i
], src
[i
+ 1], src
[i
+ 2]);
604 p
->tlimit_reg
= TASDEVICE_REG(src
[i
], src
[i
+ 1], src
[i
+ 2]);
607 memcpy(dst
, &src
[i
], cali_data
->total_sz
);
611 static int tas2781_latch_reg_get(struct snd_kcontrol
*kcontrol
,
612 struct snd_ctl_elem_value
*ucontrol
)
614 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
615 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(comp
);
616 struct i2c_client
*clt
= (struct i2c_client
*)tas_priv
->client
;
617 struct soc_bytes_ext
*bytes_ext
=
618 (struct soc_bytes_ext
*) kcontrol
->private_value
;
619 struct tasdevice
*tasdev
= tas_priv
->tasdevice
;
620 unsigned char *dst
= ucontrol
->value
.bytes
.data
;
623 dst
[0] = bytes_ext
->max
;
624 guard(mutex
)(&tas_priv
->codec_lock
);
625 for (i
= 0; i
< tas_priv
->ndev
; i
++) {
626 if (clt
->addr
== tasdev
[i
].dev_addr
) {
627 /* First byte is the device index. */
629 rc
= tasdevice_dev_read(tas_priv
, i
,
630 TAS2781_RUNTIME_LATCH_RE_REG
, &val
);
632 dev_err(tas_priv
->dev
, "%s, get value error\n",
644 static int tasdev_tf_data_get(struct snd_kcontrol
*kcontrol
,
645 struct snd_ctl_elem_value
*ucontrol
)
647 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
648 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(comp
);
649 struct soc_bytes_ext
*bytes_ext
=
650 (struct soc_bytes_ext
*) kcontrol
->private_value
;
651 unsigned char *dst
= ucontrol
->value
.bytes
.data
;
654 if (tas_priv
->chip_id
== TAS2781
)
655 reg
= TAS2781_RUNTIME_RE_REG_TF
;
657 reg
= TAS2563_RUNTIME_RE_REG_TF
;
659 guard(mutex
)(&tas_priv
->codec_lock
);
660 dst
[0] = bytes_ext
->max
;
661 return calib_data_get(tas_priv
, reg
, &dst
[1]);
664 static int tasdev_re_data_get(struct snd_kcontrol
*kcontrol
,
665 struct snd_ctl_elem_value
*ucontrol
)
667 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
668 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(comp
);
669 struct soc_bytes_ext
*bytes_ext
=
670 (struct soc_bytes_ext
*) kcontrol
->private_value
;
671 unsigned char *dst
= ucontrol
->value
.bytes
.data
;
674 if (tas_priv
->chip_id
== TAS2781
)
675 reg
= TAS2781_RUNTIME_RE_REG
;
677 reg
= TAS2563_RUNTIME_RE_REG
;
678 guard(mutex
)(&tas_priv
->codec_lock
);
679 dst
[0] = bytes_ext
->max
;
680 return calib_data_get(tas_priv
, reg
, &dst
[1]);
683 static int tasdev_r0_data_get(struct snd_kcontrol
*kcontrol
,
684 struct snd_ctl_elem_value
*ucontrol
)
686 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
687 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(comp
);
688 struct calidata
*cali_data
= &tas_priv
->cali_data
;
689 struct soc_bytes_ext
*bytes_ext
=
690 (struct soc_bytes_ext
*) kcontrol
->private_value
;
691 unsigned char *dst
= ucontrol
->value
.bytes
.data
;
694 guard(mutex
)(&tas_priv
->codec_lock
);
696 if (tas_priv
->chip_id
== TAS2563
)
697 reg
= TAS2563_PRM_R0_REG
;
698 else if (cali_data
->cali_reg_array
.r0_reg
)
699 reg
= cali_data
->cali_reg_array
.r0_reg
;
702 dst
[0] = bytes_ext
->max
;
703 return calib_data_get(tas_priv
, reg
, &dst
[1]);
706 static int tasdev_XMA1_data_get(struct snd_kcontrol
*kcontrol
,
707 struct snd_ctl_elem_value
*ucontrol
)
709 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
710 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(comp
);
711 struct soc_bytes_ext
*bytes_ext
=
712 (struct soc_bytes_ext
*) kcontrol
->private_value
;
713 unsigned char *dst
= ucontrol
->value
.bytes
.data
;
714 unsigned int reg
= TASDEVICE_XM_A1_REG
;
716 guard(mutex
)(&tas_priv
->codec_lock
);
717 dst
[0] = bytes_ext
->max
;
718 return calib_data_get(tas_priv
, reg
, &dst
[1]);
721 static int tasdev_XMA2_data_get(struct snd_kcontrol
*kcontrol
,
722 struct snd_ctl_elem_value
*ucontrol
)
724 struct snd_soc_component
*comp
= snd_soc_kcontrol_component(kcontrol
);
725 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(comp
);
726 struct soc_bytes_ext
*bytes_ext
=
727 (struct soc_bytes_ext
*) kcontrol
->private_value
;
728 unsigned char *dst
= ucontrol
->value
.bytes
.data
;
729 unsigned int reg
= TASDEVICE_XM_A2_REG
;
731 guard(mutex
)(&tas_priv
->codec_lock
);
732 dst
[0] = bytes_ext
->max
;
733 return calib_data_get(tas_priv
, reg
, &dst
[1]);
736 static int tasdev_nop_get(
737 struct snd_kcontrol
*kcontrol
,
738 struct snd_ctl_elem_value
*ucontrol
)
743 static int tas2563_digital_gain_get(
744 struct snd_kcontrol
*kcontrol
,
745 struct snd_ctl_elem_value
*ucontrol
)
747 struct soc_mixer_control
*mc
=
748 (struct soc_mixer_control
*)kcontrol
->private_value
;
749 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
750 struct tasdevice_priv
*tas_dev
= snd_soc_component_get_drvdata(codec
);
751 unsigned int l
= 0, r
= mc
->max
;
752 unsigned int target
, ar_mid
, mid
, ar_l
, ar_r
;
753 unsigned int reg
= mc
->reg
;
754 unsigned char data
[4];
757 mutex_lock(&tas_dev
->codec_lock
);
758 /* Read the primary device */
759 ret
= tasdevice_dev_bulk_read(tas_dev
, 0, reg
, data
, 4);
761 dev_err(tas_dev
->dev
, "%s, get AMP vol error\n", __func__
);
765 target
= get_unaligned_be32(&data
[0]);
769 ar_mid
= get_unaligned_be32(tas2563_dvc_table
[mid
]);
776 ar_l
= get_unaligned_be32(tas2563_dvc_table
[l
]);
777 ar_r
= get_unaligned_be32(tas2563_dvc_table
[r
]);
779 /* find out the member same as or closer to the current volume */
780 ucontrol
->value
.integer
.value
[0] =
781 abs(target
- ar_l
) <= abs(target
- ar_r
) ? l
: r
;
783 mutex_unlock(&tas_dev
->codec_lock
);
787 static int tas2563_digital_gain_put(
788 struct snd_kcontrol
*kcontrol
,
789 struct snd_ctl_elem_value
*ucontrol
)
791 struct soc_mixer_control
*mc
=
792 (struct soc_mixer_control
*)kcontrol
->private_value
;
793 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
794 struct tasdevice_priv
*tas_dev
= snd_soc_component_get_drvdata(codec
);
795 int vol
= ucontrol
->value
.integer
.value
[0];
796 int status
= 0, max
= mc
->max
, rc
= 1;
798 unsigned int reg
= mc
->reg
;
799 unsigned int volrd
, volwr
;
800 unsigned char data
[4];
802 vol
= clamp(vol
, 0, max
);
803 mutex_lock(&tas_dev
->codec_lock
);
804 /* Read the primary device */
805 ret
= tasdevice_dev_bulk_read(tas_dev
, 0, reg
, data
, 4);
807 dev_err(tas_dev
->dev
, "%s, get AMP vol error\n", __func__
);
812 volrd
= get_unaligned_be32(&data
[0]);
813 volwr
= get_unaligned_be32(tas2563_dvc_table
[vol
]);
815 if (volrd
== volwr
) {
820 for (i
= 0; i
< tas_dev
->ndev
; i
++) {
821 ret
= tasdevice_dev_bulk_write(tas_dev
, i
, reg
,
822 (unsigned char *)tas2563_dvc_table
[vol
], 4);
824 dev_err(tas_dev
->dev
,
825 "%s, set digital vol error in dev %d\n",
834 mutex_unlock(&tas_dev
->codec_lock
);
838 static const struct snd_kcontrol_new tasdevice_snd_controls
[] = {
839 SOC_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
840 tasdev_force_fwload_get
, tasdev_force_fwload_put
),
843 static const struct snd_kcontrol_new tasdevice_cali_controls
[] = {
844 SOC_SINGLE_EXT("Calibration Stop", SND_SOC_NOPM
, 0, 1, 0,
845 tasdev_nop_get
, tasdev_calib_stop_put
),
846 SND_SOC_BYTES_EXT("Amp TF Data", 6, tasdev_tf_data_get
, NULL
),
847 SND_SOC_BYTES_EXT("Amp RE Data", 6, tasdev_re_data_get
, NULL
),
848 SND_SOC_BYTES_EXT("Amp R0 Data", 6, tasdev_r0_data_get
, NULL
),
849 SND_SOC_BYTES_EXT("Amp XMA1 Data", 6, tasdev_XMA1_data_get
, NULL
),
850 SND_SOC_BYTES_EXT("Amp XMA2 Data", 6, tasdev_XMA2_data_get
, NULL
),
853 static const struct snd_kcontrol_new tas2781_snd_controls
[] = {
854 SOC_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL
,
855 1, 0, 20, 0, tas2781_amp_getvol
,
856 tas2781_amp_putvol
, amp_vol_tlv
),
857 SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Gain", TAS2781_DVC_LVL
,
858 0, 0, 200, 1, tas2781_digital_getvol
,
859 tas2781_digital_putvol
, dvc_tlv
),
862 static const struct snd_kcontrol_new tas2781_cali_controls
[] = {
863 SND_SOC_BYTES_EXT("Amp Latch Data", 3, tas2781_latch_reg_get
, NULL
),
866 static const struct snd_kcontrol_new tas2563_snd_controls
[] = {
867 SOC_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2563_DVC_LVL
, 0,
868 0, ARRAY_SIZE(tas2563_dvc_table
) - 1, 0,
869 tas2563_digital_gain_get
, tas2563_digital_gain_put
,
873 static const struct snd_kcontrol_new tas2563_cali_controls
[] = {
874 SOC_SINGLE_EXT("Calibration Start", SND_SOC_NOPM
, 0, 1, 0,
875 tasdev_nop_get
, tas2563_calib_start_put
),
878 static int tasdevice_set_profile_id(struct snd_kcontrol
*kcontrol
,
879 struct snd_ctl_elem_value
*ucontrol
)
881 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
882 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
885 if (tas_priv
->rcabin
.profile_cfg_id
!=
886 ucontrol
->value
.integer
.value
[0]) {
887 tas_priv
->rcabin
.profile_cfg_id
=
888 ucontrol
->value
.integer
.value
[0];
895 static int tasdevice_info_active_num(struct snd_kcontrol
*kcontrol
,
896 struct snd_ctl_elem_info
*uinfo
)
898 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
899 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
901 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
903 uinfo
->value
.integer
.min
= 0;
904 uinfo
->value
.integer
.max
= tas_priv
->ndev
- 1;
909 static int tasdevice_info_chip_id(struct snd_kcontrol
*kcontrol
,
910 struct snd_ctl_elem_info
*uinfo
)
912 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
914 uinfo
->value
.integer
.min
= TAS2563
;
915 uinfo
->value
.integer
.max
= TAS2781
;
920 static int tasdevice_info_programs(struct snd_kcontrol
*kcontrol
,
921 struct snd_ctl_elem_info
*uinfo
)
923 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
924 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
925 struct tasdevice_fw
*tas_fw
= tas_priv
->fmw
;
927 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
929 uinfo
->value
.integer
.min
= 0;
930 uinfo
->value
.integer
.max
= (int)tas_fw
->nr_programs
;
935 static int tasdevice_info_configurations(
936 struct snd_kcontrol
*kcontrol
, struct snd_ctl_elem_info
*uinfo
)
938 struct snd_soc_component
*codec
=
939 snd_soc_kcontrol_component(kcontrol
);
940 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
941 struct tasdevice_fw
*tas_fw
= tas_priv
->fmw
;
943 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
945 uinfo
->value
.integer
.min
= 0;
946 uinfo
->value
.integer
.max
= (int)tas_fw
->nr_configurations
- 1;
951 static int tasdevice_info_profile(struct snd_kcontrol
*kcontrol
,
952 struct snd_ctl_elem_info
*uinfo
)
954 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
955 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
957 uinfo
->type
= SNDRV_CTL_ELEM_TYPE_INTEGER
;
959 uinfo
->value
.integer
.min
= 0;
960 uinfo
->value
.integer
.max
= tas_priv
->rcabin
.ncfgs
- 1;
965 static int tasdevice_get_profile_id(struct snd_kcontrol
*kcontrol
,
966 struct snd_ctl_elem_value
*ucontrol
)
968 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
969 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
971 ucontrol
->value
.integer
.value
[0] = tas_priv
->rcabin
.profile_cfg_id
;
976 static int tasdevice_get_chip_id(struct snd_kcontrol
*kcontrol
,
977 struct snd_ctl_elem_value
*ucontrol
)
979 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
980 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
982 ucontrol
->value
.integer
.value
[0] = tas_priv
->chip_id
;
987 static int tasdevice_create_control(struct tasdevice_priv
*tas_priv
)
989 struct snd_kcontrol_new
*prof_ctrls
;
995 prof_ctrls
= devm_kcalloc(tas_priv
->dev
, nr_controls
,
996 sizeof(prof_ctrls
[0]), GFP_KERNEL
);
1002 /* Create a mixer item for selecting the active profile */
1003 name
= devm_kstrdup(tas_priv
->dev
, "Speaker Profile Id", GFP_KERNEL
);
1008 prof_ctrls
[mix_index
].name
= name
;
1009 prof_ctrls
[mix_index
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1010 prof_ctrls
[mix_index
].info
= tasdevice_info_profile
;
1011 prof_ctrls
[mix_index
].get
= tasdevice_get_profile_id
;
1012 prof_ctrls
[mix_index
].put
= tasdevice_set_profile_id
;
1015 ret
= snd_soc_add_component_controls(tas_priv
->codec
,
1016 prof_ctrls
, nr_controls
< mix_index
? nr_controls
: mix_index
);
1022 static int tasdevice_program_get(struct snd_kcontrol
*kcontrol
,
1023 struct snd_ctl_elem_value
*ucontrol
)
1025 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
1026 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
1028 ucontrol
->value
.integer
.value
[0] = tas_priv
->cur_prog
;
1033 static int tasdevice_program_put(struct snd_kcontrol
*kcontrol
,
1034 struct snd_ctl_elem_value
*ucontrol
)
1036 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
1037 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
1038 unsigned int nr_program
= ucontrol
->value
.integer
.value
[0];
1041 if (tas_priv
->cur_prog
!= nr_program
) {
1042 tas_priv
->cur_prog
= nr_program
;
1049 static int tasdevice_configuration_get(struct snd_kcontrol
*kcontrol
,
1050 struct snd_ctl_elem_value
*ucontrol
)
1053 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
1054 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
1056 ucontrol
->value
.integer
.value
[0] = tas_priv
->cur_conf
;
1061 static int tasdevice_configuration_put(
1062 struct snd_kcontrol
*kcontrol
,
1063 struct snd_ctl_elem_value
*ucontrol
)
1065 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
1066 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
1067 unsigned int nr_configuration
= ucontrol
->value
.integer
.value
[0];
1070 if (tas_priv
->cur_conf
!= nr_configuration
) {
1071 tas_priv
->cur_conf
= nr_configuration
;
1078 static int tasdevice_active_num_get(struct snd_kcontrol
*kcontrol
,
1079 struct snd_ctl_elem_value
*ucontrol
)
1081 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
1082 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
1083 struct i2c_client
*clt
= (struct i2c_client
*)tas_priv
->client
;
1084 struct tasdevice
*tasdev
= tas_priv
->tasdevice
;
1087 for (i
= 0; i
< tas_priv
->ndev
; i
++) {
1088 if (clt
->addr
== tasdev
[i
].dev_addr
) {
1089 ucontrol
->value
.integer
.value
[0] = i
;
1097 static int tasdevice_active_num_put(struct snd_kcontrol
*kcontrol
,
1098 struct snd_ctl_elem_value
*ucontrol
)
1100 struct snd_soc_component
*codec
= snd_soc_kcontrol_component(kcontrol
);
1101 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
1102 int dev_id
= ucontrol
->value
.integer
.value
[0];
1103 int max
= tas_priv
->ndev
- 1;
1105 dev_id
= clamp(dev_id
, 0, max
);
1107 guard(mutex
)(&tas_priv
->codec_lock
);
1108 return tasdev_chn_switch(tas_priv
, dev_id
);
1111 static int tasdevice_dsp_create_ctrls(struct tasdevice_priv
*tas_priv
)
1113 struct snd_kcontrol_new
*dsp_ctrls
;
1114 char *active_dev_num
, *chip_id
;
1115 char *conf_name
, *prog_name
;
1116 int nr_controls
= 4;
1120 /* Alloc kcontrol via devm_kzalloc, which don't manually
1123 dsp_ctrls
= devm_kcalloc(tas_priv
->dev
, nr_controls
,
1124 sizeof(dsp_ctrls
[0]), GFP_KERNEL
);
1130 /* Create mixer items for selecting the active Program and Config */
1131 prog_name
= devm_kstrdup(tas_priv
->dev
, "Speaker Program Id",
1137 dsp_ctrls
[mix_index
].name
= prog_name
;
1138 dsp_ctrls
[mix_index
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1139 dsp_ctrls
[mix_index
].info
= tasdevice_info_programs
;
1140 dsp_ctrls
[mix_index
].get
= tasdevice_program_get
;
1141 dsp_ctrls
[mix_index
].put
= tasdevice_program_put
;
1144 conf_name
= devm_kstrdup(tas_priv
->dev
, "Speaker Config Id",
1150 dsp_ctrls
[mix_index
].name
= conf_name
;
1151 dsp_ctrls
[mix_index
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1152 dsp_ctrls
[mix_index
].info
= tasdevice_info_configurations
;
1153 dsp_ctrls
[mix_index
].get
= tasdevice_configuration_get
;
1154 dsp_ctrls
[mix_index
].put
= tasdevice_configuration_put
;
1157 active_dev_num
= devm_kstrdup(tas_priv
->dev
, "Activate Tasdevice Num",
1159 if (!active_dev_num
) {
1163 dsp_ctrls
[mix_index
].name
= active_dev_num
;
1164 dsp_ctrls
[mix_index
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1165 dsp_ctrls
[mix_index
].info
= tasdevice_info_active_num
;
1166 dsp_ctrls
[mix_index
].get
= tasdevice_active_num_get
;
1167 dsp_ctrls
[mix_index
].put
= tasdevice_active_num_put
;
1170 chip_id
= devm_kstrdup(tas_priv
->dev
, "Tasdevice Chip Id", GFP_KERNEL
);
1175 dsp_ctrls
[mix_index
].name
= chip_id
;
1176 dsp_ctrls
[mix_index
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1177 dsp_ctrls
[mix_index
].info
= tasdevice_info_chip_id
;
1178 dsp_ctrls
[mix_index
].get
= tasdevice_get_chip_id
;
1181 ret
= snd_soc_add_component_controls(tas_priv
->codec
, dsp_ctrls
,
1182 nr_controls
< mix_index
? nr_controls
: mix_index
);
1188 static int tasdevice_create_cali_ctrls(struct tasdevice_priv
*priv
)
1190 struct calidata
*cali_data
= &priv
->cali_data
;
1191 struct tasdevice
*tasdev
= priv
->tasdevice
;
1192 struct soc_bytes_ext
*ext_cali_data
;
1193 struct snd_kcontrol_new
*cali_ctrls
;
1194 unsigned int nctrls
;
1198 rc
= snd_soc_add_component_controls(priv
->codec
,
1199 tasdevice_cali_controls
, ARRAY_SIZE(tasdevice_cali_controls
));
1201 dev_err(priv
->dev
, "%s: Add cali controls err rc = %d",
1206 if (priv
->chip_id
== TAS2781
) {
1207 cali_ctrls
= (struct snd_kcontrol_new
*)tas2781_cali_controls
;
1208 nctrls
= ARRAY_SIZE(tas2781_cali_controls
);
1209 for (i
= 0; i
< priv
->ndev
; i
++) {
1210 tasdev
[i
].cali_data_backup
=
1211 kmemdup(tas2781_cali_start_reg
,
1212 sizeof(tas2781_cali_start_reg
), GFP_KERNEL
);
1213 if (!tasdev
[i
].cali_data_backup
)
1217 cali_ctrls
= (struct snd_kcontrol_new
*)tas2563_cali_controls
;
1218 nctrls
= ARRAY_SIZE(tas2563_cali_controls
);
1219 for (i
= 0; i
< priv
->ndev
; i
++) {
1220 tasdev
[i
].cali_data_backup
=
1221 kmemdup(tas2563_cali_start_reg
,
1222 sizeof(tas2563_cali_start_reg
), GFP_KERNEL
);
1223 if (!tasdev
[i
].cali_data_backup
)
1228 rc
= snd_soc_add_component_controls(priv
->codec
, cali_ctrls
, nctrls
);
1230 dev_err(priv
->dev
, "%s: Add chip cali ctrls err rc = %d",
1235 /* index for cali_ctrls */
1237 if (priv
->chip_id
== TAS2781
)
1243 * Alloc kcontrol via devm_kzalloc(), which don't manually
1244 * free the kcontrol。
1246 cali_ctrls
= devm_kcalloc(priv
->dev
, nctrls
,
1247 sizeof(cali_ctrls
[0]), GFP_KERNEL
);
1251 ext_cali_data
= devm_kzalloc(priv
->dev
, sizeof(*ext_cali_data
),
1256 cali_name
= devm_kstrdup(priv
->dev
, "Speaker Calibrated Data",
1260 /* the number of calibrated data per tas2563/tas2781 */
1261 cali_data
->cali_dat_sz_per_dev
= 20;
1263 * Data structure for tas2563/tas2781 calibrated data:
1265 * Reg id (1 byte, constant 'r')
1266 * book, page, register array for calibrated data (15 bytes)
1267 * for (i = 0; i < Device-Sum; i++) {
1268 * Device #i index_info (1 byte)
1269 * Calibrated data for Device #i (20 bytes)
1272 ext_cali_data
->max
= priv
->ndev
*
1273 (cali_data
->cali_dat_sz_per_dev
+ 1) + 1 + 15 + 1;
1274 priv
->cali_data
.total_sz
= priv
->ndev
*
1275 (cali_data
->cali_dat_sz_per_dev
+ 1);
1276 priv
->cali_data
.data
= devm_kzalloc(priv
->dev
,
1277 ext_cali_data
->max
, GFP_KERNEL
);
1278 cali_ctrls
[i
].name
= cali_name
;
1279 cali_ctrls
[i
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1280 cali_ctrls
[i
].info
= snd_soc_bytes_info_ext
;
1281 cali_ctrls
[i
].get
= tasdev_cali_data_get
;
1282 cali_ctrls
[i
].put
= tasdev_cali_data_put
;
1283 cali_ctrls
[i
].private_value
= (unsigned long)ext_cali_data
;
1286 cali_data
->data
= devm_kzalloc(priv
->dev
, cali_data
->total_sz
,
1288 if (!cali_data
->data
)
1291 if (priv
->chip_id
== TAS2781
) {
1292 struct soc_bytes_ext
*ext_cali_start
;
1293 char *cali_start_name
;
1295 ext_cali_start
= devm_kzalloc(priv
->dev
,
1296 sizeof(*ext_cali_start
), GFP_KERNEL
);
1297 if (!ext_cali_start
)
1300 cali_start_name
= devm_kstrdup(priv
->dev
,
1301 "Calibration Start", GFP_KERNEL
);
1302 if (!cali_start_name
)
1305 * package structure for tas2781 ftc start:
1307 * Reg id (1 byte, constant 'r')
1308 * book, page, register for pilot threshold, pilot tone
1309 * and sine gain (12 bytes)
1310 * for (i = 0; i < Device-Sum; i++) {
1311 * Device #i index_info (1 byte)
1312 * Sine gain for Device #i (8 bytes)
1315 ext_cali_start
->max
= 14 + priv
->ndev
* 9;
1316 cali_ctrls
[i
].name
= cali_start_name
;
1317 cali_ctrls
[i
].iface
= SNDRV_CTL_ELEM_IFACE_MIXER
;
1318 cali_ctrls
[i
].info
= snd_soc_bytes_info_ext
;
1319 cali_ctrls
[i
].put
= tas2781_calib_start_put
;
1320 cali_ctrls
[i
].get
= tasdev_nop_get
;
1321 cali_ctrls
[i
].private_value
= (unsigned long)ext_cali_start
;
1325 return snd_soc_add_component_controls(priv
->codec
, cali_ctrls
,
1326 nctrls
< i
? nctrls
: i
);
1329 static void tasdevice_fw_ready(const struct firmware
*fmw
,
1332 struct tasdevice_priv
*tas_priv
= context
;
1336 mutex_lock(&tas_priv
->codec_lock
);
1338 ret
= tasdevice_rca_parser(tas_priv
, fmw
);
1340 tasdevice_config_info_remove(tas_priv
);
1343 tasdevice_create_control(tas_priv
);
1345 tasdevice_dsp_remove(tas_priv
);
1346 tasdevice_calbin_remove(tas_priv
);
1348 * The baseline is the RCA-only case, and then the code attempts to
1349 * load DSP firmware but in case of failures just keep going, i.e.
1350 * failing to load DSP firmware is NOT an error.
1352 tas_priv
->fw_state
= TASDEVICE_RCA_FW_OK
;
1353 if (tas_priv
->name_prefix
)
1354 scnprintf(tas_priv
->coef_binaryname
, 64, "%s-%s_coef.bin",
1355 tas_priv
->name_prefix
, tas_priv
->dev_name
);
1357 scnprintf(tas_priv
->coef_binaryname
, 64, "%s_coef.bin",
1358 tas_priv
->dev_name
);
1359 ret
= tasdevice_dsp_parser(tas_priv
);
1361 dev_err(tas_priv
->dev
, "dspfw load %s error\n",
1362 tas_priv
->coef_binaryname
);
1367 * If no dsp-related kcontrol created, the dsp resource will be freed.
1369 ret
= tasdevice_dsp_create_ctrls(tas_priv
);
1371 dev_err(tas_priv
->dev
, "dsp controls error\n");
1375 ret
= tasdevice_create_cali_ctrls(tas_priv
);
1377 dev_err(tas_priv
->dev
, "cali controls error\n");
1381 tas_priv
->fw_state
= TASDEVICE_DSP_FW_ALL_OK
;
1383 /* If calibrated data occurs error, dsp will still works with default
1384 * calibrated data inside algo.
1386 for (i
= 0; i
< tas_priv
->ndev
; i
++) {
1387 if (tas_priv
->name_prefix
)
1388 scnprintf(tas_priv
->cal_binaryname
[i
], 64,
1389 "%s-%s_cal_0x%02x.bin", tas_priv
->name_prefix
,
1391 tas_priv
->tasdevice
[i
].dev_addr
);
1393 scnprintf(tas_priv
->cal_binaryname
[i
], 64,
1394 "%s_cal_0x%02x.bin", tas_priv
->dev_name
,
1395 tas_priv
->tasdevice
[i
].dev_addr
);
1396 ret
= tas2781_load_calibration(tas_priv
,
1397 tas_priv
->cal_binaryname
[i
], i
);
1399 dev_err(tas_priv
->dev
,
1400 "%s: load %s error, default will effect\n",
1401 __func__
, tas_priv
->cal_binaryname
[i
]);
1404 tasdevice_prmg_load(tas_priv
, 0);
1405 tas_priv
->cur_prog
= 0;
1407 if (tas_priv
->fw_state
== TASDEVICE_RCA_FW_OK
) {
1408 /* If DSP FW fail, DSP kcontrol won't be created. */
1409 tasdevice_dsp_remove(tas_priv
);
1411 mutex_unlock(&tas_priv
->codec_lock
);
1413 release_firmware(fmw
);
1416 static int tasdevice_dapm_event(struct snd_soc_dapm_widget
*w
,
1417 struct snd_kcontrol
*kcontrol
, int event
)
1419 struct snd_soc_component
*codec
= snd_soc_dapm_to_component(w
->dapm
);
1420 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
1423 /* Codec Lock Hold */
1424 mutex_lock(&tas_priv
->codec_lock
);
1425 if (event
== SND_SOC_DAPM_PRE_PMD
)
1427 tasdevice_tuning_switch(tas_priv
, state
);
1428 /* Codec Lock Release*/
1429 mutex_unlock(&tas_priv
->codec_lock
);
1434 static const struct snd_soc_dapm_widget tasdevice_dapm_widgets
[] = {
1435 SND_SOC_DAPM_AIF_IN("ASI", "ASI Playback", 0, SND_SOC_NOPM
, 0, 0),
1436 SND_SOC_DAPM_AIF_OUT_E("ASI OUT", "ASI Capture", 0, SND_SOC_NOPM
,
1437 0, 0, tasdevice_dapm_event
,
1438 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
1439 SND_SOC_DAPM_SPK("SPK", tasdevice_dapm_event
),
1440 SND_SOC_DAPM_OUTPUT("OUT"),
1441 SND_SOC_DAPM_INPUT("DMIC"),
1444 static const struct snd_soc_dapm_route tasdevice_audio_map
[] = {
1445 {"SPK", NULL
, "ASI"},
1446 {"OUT", NULL
, "SPK"},
1447 {"ASI OUT", NULL
, "DMIC"},
1450 static int tasdevice_startup(struct snd_pcm_substream
*substream
,
1451 struct snd_soc_dai
*dai
)
1453 struct snd_soc_component
*codec
= dai
->component
;
1454 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
1456 switch (tas_priv
->fw_state
) {
1457 case TASDEVICE_RCA_FW_OK
:
1458 case TASDEVICE_DSP_FW_ALL_OK
:
1465 static int tasdevice_hw_params(struct snd_pcm_substream
*substream
,
1466 struct snd_pcm_hw_params
*params
, struct snd_soc_dai
*dai
)
1468 struct tasdevice_priv
*tas_priv
= snd_soc_dai_get_drvdata(dai
);
1469 unsigned int slot_width
;
1470 unsigned int fsrate
;
1474 fsrate
= params_rate(params
);
1480 dev_err(tas_priv
->dev
, "%s: incorrect sample rate = %u\n",
1486 slot_width
= params_width(params
);
1487 switch (slot_width
) {
1494 dev_err(tas_priv
->dev
, "%s: incorrect slot width = %u\n",
1495 __func__
, slot_width
);
1500 bclk_rate
= snd_soc_params_to_bclk(params
);
1501 if (bclk_rate
< 0) {
1502 dev_err(tas_priv
->dev
, "%s: incorrect bclk rate = %d\n",
1503 __func__
, bclk_rate
);
1512 static int tasdevice_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
1513 int clk_id
, unsigned int freq
, int dir
)
1515 struct tasdevice_priv
*tas_priv
= snd_soc_dai_get_drvdata(codec_dai
);
1517 tas_priv
->sysclk
= freq
;
1522 static const struct snd_soc_dai_ops tasdevice_dai_ops
= {
1523 .startup
= tasdevice_startup
,
1524 .hw_params
= tasdevice_hw_params
,
1525 .set_sysclk
= tasdevice_set_dai_sysclk
,
1528 static struct snd_soc_dai_driver tasdevice_dai_driver
[] = {
1530 .name
= "tasdev_codec",
1533 .stream_name
= "Playback",
1536 .rates
= TASDEVICE_RATES
,
1537 .formats
= TASDEVICE_FORMATS
,
1540 .stream_name
= "Capture",
1543 .rates
= TASDEVICE_RATES
,
1544 .formats
= TASDEVICE_FORMATS
,
1546 .ops
= &tasdevice_dai_ops
,
1547 .symmetric_rate
= 1,
1551 static int tasdevice_codec_probe(struct snd_soc_component
*codec
)
1553 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
1554 struct snd_kcontrol_new
*p
;
1558 switch (tas_priv
->chip_id
) {
1560 p
= (struct snd_kcontrol_new
*)tas2781_snd_controls
;
1561 size
= ARRAY_SIZE(tas2781_snd_controls
);
1564 p
= (struct snd_kcontrol_new
*)tas2563_snd_controls
;
1565 size
= ARRAY_SIZE(tas2563_snd_controls
);
1568 rc
= snd_soc_add_component_controls(codec
, p
, size
);
1570 dev_err(tas_priv
->dev
, "%s: Add control err rc = %d",
1575 tas_priv
->name_prefix
= codec
->name_prefix
;
1576 return tascodec_init(tas_priv
, codec
, THIS_MODULE
, tasdevice_fw_ready
);
1579 static void tasdevice_deinit(void *context
)
1581 struct tasdevice_priv
*tas_priv
= (struct tasdevice_priv
*) context
;
1582 struct tasdevice
*tasdev
= tas_priv
->tasdevice
;
1585 for (i
= 0; i
< tas_priv
->ndev
; i
++)
1586 kfree(tasdev
[i
].cali_data_backup
);
1588 tasdevice_config_info_remove(tas_priv
);
1589 tasdevice_dsp_remove(tas_priv
);
1590 tasdevice_calbin_remove(tas_priv
);
1591 tas_priv
->fw_state
= TASDEVICE_DSP_FW_PENDING
;
1594 static void tasdevice_codec_remove(struct snd_soc_component
*codec
)
1596 struct tasdevice_priv
*tas_priv
= snd_soc_component_get_drvdata(codec
);
1598 tasdevice_deinit(tas_priv
);
1601 static const struct snd_soc_component_driver
1602 soc_codec_driver_tasdevice
= {
1603 .probe
= tasdevice_codec_probe
,
1604 .remove
= tasdevice_codec_remove
,
1605 .controls
= tasdevice_snd_controls
,
1606 .num_controls
= ARRAY_SIZE(tasdevice_snd_controls
),
1607 .dapm_widgets
= tasdevice_dapm_widgets
,
1608 .num_dapm_widgets
= ARRAY_SIZE(tasdevice_dapm_widgets
),
1609 .dapm_routes
= tasdevice_audio_map
,
1610 .num_dapm_routes
= ARRAY_SIZE(tasdevice_audio_map
),
1615 static void tasdevice_parse_dt(struct tasdevice_priv
*tas_priv
)
1617 struct i2c_client
*client
= (struct i2c_client
*)tas_priv
->client
;
1618 unsigned int dev_addrs
[TASDEVICE_MAX_CHANNELS
];
1621 if (tas_priv
->isacpi
) {
1622 ndev
= device_property_read_u32_array(&client
->dev
,
1623 "ti,audio-slots", NULL
, 0);
1626 dev_addrs
[0] = client
->addr
;
1628 ndev
= (ndev
< ARRAY_SIZE(dev_addrs
))
1629 ? ndev
: ARRAY_SIZE(dev_addrs
);
1630 ndev
= device_property_read_u32_array(&client
->dev
,
1631 "ti,audio-slots", dev_addrs
, ndev
);
1635 acpi_dev_gpio_irq_get(ACPI_COMPANION(&client
->dev
), 0);
1636 } else if (IS_ENABLED(CONFIG_OF
)) {
1637 struct device_node
*np
= tas_priv
->dev
->of_node
;
1640 for (i
= 0; i
< TASDEVICE_MAX_CHANNELS
; i
++) {
1641 if (of_property_read_reg(np
, i
, &addr
, NULL
))
1643 dev_addrs
[ndev
++] = addr
;
1646 tas_priv
->irq
= of_irq_get(np
, 0);
1649 dev_addrs
[0] = client
->addr
;
1651 tas_priv
->ndev
= ndev
;
1652 for (i
= 0; i
< ndev
; i
++)
1653 tas_priv
->tasdevice
[i
].dev_addr
= dev_addrs
[i
];
1655 tas_priv
->reset
= devm_gpiod_get_optional(&client
->dev
,
1656 "reset", GPIOD_OUT_HIGH
);
1657 if (IS_ERR(tas_priv
->reset
))
1658 dev_err(tas_priv
->dev
, "%s Can't get reset GPIO\n",
1661 strcpy(tas_priv
->dev_name
, tasdevice_id
[tas_priv
->chip_id
].name
);
1664 static int tasdevice_i2c_probe(struct i2c_client
*i2c
)
1666 const struct i2c_device_id
*id
= i2c_match_id(tasdevice_id
, i2c
);
1667 const struct acpi_device_id
*acpi_id
;
1668 struct tasdevice_priv
*tas_priv
;
1671 tas_priv
= tasdevice_kzalloc(i2c
);
1675 dev_set_drvdata(&i2c
->dev
, tas_priv
);
1677 if (ACPI_HANDLE(&i2c
->dev
)) {
1678 acpi_id
= acpi_match_device(i2c
->dev
.driver
->acpi_match_table
,
1681 dev_err(&i2c
->dev
, "No driver data\n");
1685 tas_priv
->chip_id
= acpi_id
->driver_data
;
1686 tas_priv
->isacpi
= true;
1688 tas_priv
->chip_id
= id
? id
->driver_data
: 0;
1689 tas_priv
->isacpi
= false;
1692 tasdevice_parse_dt(tas_priv
);
1694 ret
= tasdevice_init(tas_priv
);
1698 tasdevice_reset(tas_priv
);
1700 ret
= devm_snd_soc_register_component(tas_priv
->dev
,
1701 &soc_codec_driver_tasdevice
,
1702 tasdevice_dai_driver
, ARRAY_SIZE(tasdevice_dai_driver
));
1704 dev_err(tas_priv
->dev
, "%s: codec register error:0x%08x\n",
1710 tasdevice_remove(tas_priv
);
1714 static void tasdevice_i2c_remove(struct i2c_client
*client
)
1716 struct tasdevice_priv
*tas_priv
= i2c_get_clientdata(client
);
1718 tasdevice_remove(tas_priv
);
1722 static const struct acpi_device_id tasdevice_acpi_match
[] = {
1723 { "TAS2781", TAS2781
},
1727 MODULE_DEVICE_TABLE(acpi
, tasdevice_acpi_match
);
1730 static struct i2c_driver tasdevice_i2c_driver
= {
1732 .name
= "tasdev-codec",
1733 .of_match_table
= of_match_ptr(tasdevice_of_match
),
1735 .acpi_match_table
= ACPI_PTR(tasdevice_acpi_match
),
1738 .probe
= tasdevice_i2c_probe
,
1739 .remove
= tasdevice_i2c_remove
,
1740 .id_table
= tasdevice_id
,
1743 module_i2c_driver(tasdevice_i2c_driver
);
1745 MODULE_AUTHOR("Shenghao Ding <shenghao-ding@ti.com>");
1746 MODULE_AUTHOR("Kevin Lu <kevin-lu@ti.com>");
1747 MODULE_DESCRIPTION("ASoC TAS2781 Driver");
1748 MODULE_LICENSE("GPL");
1749 MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");