xref: /linux/include/sound/tas2781.h (revision 5367243a37f7f74d5f595ceac780508138f1e3b0)
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 // Author: Baojun Xu <baojun.xu@ti.com>
15 //
16 
17 #ifndef __TAS2781_H__
18 #define __TAS2781_H__
19 
20 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C
21 #include <linux/debugfs.h>
22 #endif
23 
24 #include "tas2781-dsp.h"
25 
26 /* version number */
27 #define TAS2781_DRV_VER			1
28 #define SMARTAMP_MODULE_NAME		"tas2781"
29 #define TAS2781_GLOBAL_ADDR	0x40
30 #define TAS2563_GLOBAL_ADDR	0x48
31 #define TASDEVICE_RATES			(SNDRV_PCM_RATE_44100 |\
32 	SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |\
33 	SNDRV_PCM_RATE_88200)
34 
35 #define TASDEVICE_FORMATS		(SNDRV_PCM_FMTBIT_S16_LE | \
36 	SNDRV_PCM_FMTBIT_S24_LE | \
37 	SNDRV_PCM_FMTBIT_S32_LE)
38 
39 #define TASDEVICE_CRC8_POLYNOMIAL		0x4d
40 
41 /* PAGE Control Register (available in page0 of each book) */
42 #define TASDEVICE_PAGE_SELECT		0x00
43 #define TASDEVICE_BOOKCTL_PAGE		0x00
44 #define TASDEVICE_BOOKCTL_REG		127
45 #define TASDEVICE_BOOK_ID(reg)		(reg / (256 * 128))
46 #define TASDEVICE_PAGE_ID(reg)		((reg % (256 * 128)) / 128)
47 #define TASDEVICE_PAGE_REG(reg)		((reg % (256 * 128)) % 128)
48 #define TASDEVICE_PGRG(reg)		(reg % (256 * 128))
49 #define TASDEVICE_REG(book, page, reg)	(((book * 256 * 128) + \
50 					(page * 128)) + reg)
51 
52 #define TAS_REG_ERR_LOG(dev, chn, reg, ret) \
53 	dev_err((dev), "%s, C%uBx%02xPx%02xRx%02x E=%d\n", \
54 		__func__, chn, TASDEVICE_BOOK_ID(reg), \
55 		TASDEVICE_PAGE_ID(reg), \
56 		TASDEVICE_PAGE_REG(reg), \
57 		(ret))
58 
59 /* Software Reset, compatble with new device (TAS5825). */
60 #define TASDEVICE_REG_SWRESET		TASDEVICE_REG(0x0, 0x0, 0x01)
61 #define TASDEVICE_REG_SWRESET_RESET	BIT(0)
62 
63 #define TAS5825_REG_SWRESET_RESET	(BIT(0) | BIT(4))
64 
65 /* Checksum */
66 #define TASDEVICE_CHECKSUM_REG		TASDEVICE_REG(0x0, 0x0, 0x7e)
67 
68 /* XM_340 */
69 #define	TASDEVICE_XM_A1_REG	TASDEVICE_REG(0x64, 0x02, 0x4c)
70 /* XM_341 */
71 #define	TASDEVICE_XM_A2_REG	TASDEVICE_REG(0x64, 0x02, 0x64)
72 
73 /* Volume control */
74 #define TAS2563_DVC_LVL			TASDEVICE_REG(0x00, 0x02, 0x0c)
75 #define TAS2781_DVC_LVL			TASDEVICE_REG(0x0, 0x0, 0x1a)
76 #define TAS2781_AMP_LEVEL		TASDEVICE_REG(0x0, 0x0, 0x03)
77 #define TAS2781_AMP_LEVEL_MASK		GENMASK(5, 1)
78 
79 #define TAS2563_IDLE		TASDEVICE_REG(0x00, 0x00, 0x3e)
80 #define TAS2563_PRM_R0_REG		TASDEVICE_REG(0x00, 0x0f, 0x34)
81 
82 #define TAS2563_RUNTIME_RE_REG_TF	TASDEVICE_REG(0x64, 0x02, 0x70)
83 #define TAS2563_RUNTIME_RE_REG		TASDEVICE_REG(0x64, 0x02, 0x48)
84 
85 #define TAS2563_PRM_ENFF_REG		TASDEVICE_REG(0x00, 0x0d, 0x54)
86 #define TAS2563_PRM_DISTCK_REG		TASDEVICE_REG(0x00, 0x0d, 0x58)
87 #define TAS2563_PRM_TE_SCTHR_REG	TASDEVICE_REG(0x00, 0x0f, 0x60)
88 #define TAS2563_PRM_PLT_FLAG_REG	TASDEVICE_REG(0x00, 0x0d, 0x74)
89 #define TAS2563_PRM_SINEGAIN_REG	TASDEVICE_REG(0x00, 0x0d, 0x7c)
90 /* prm_Int_B0 */
91 #define TAS2563_TE_TA1_REG		TASDEVICE_REG(0x00, 0x10, 0x0c)
92 /* prm_Int_A1 */
93 #define TAS2563_TE_TA1_AT_REG		TASDEVICE_REG(0x00, 0x10, 0x10)
94 /* prm_TE_Beta */
95 #define TAS2563_TE_TA2_REG		TASDEVICE_REG(0x00, 0x0f, 0x64)
96 /* prm_TE_Beta1 */
97 #define TAS2563_TE_AT_REG		TASDEVICE_REG(0x00, 0x0f, 0x68)
98 /* prm_TE_1_Beta1 */
99 #define TAS2563_TE_DT_REG		TASDEVICE_REG(0x00, 0x0f, 0x70)
100 
101 #define TAS2573_FCT_INT_LATCH		TASDEVICE_REG(0x00, 0x00, 0x67)
102 /* YM57 */
103 #define TAS2573_FCT_OUTPUT_R0		TASDEVICE_REG(0x00, 0x65, 0x74)
104 /* YM58 */
105 #define TAS2573_FCT_OUTPUT_R0_LOW	TASDEVICE_REG(0x00, 0x65, 0x78)
106 /* YM59 */
107 #define TAS2573_FCT_OUTPUT_INV_R0	TASDEVICE_REG(0x00, 0x65, 0x7C)
108 /* YM61 */
109 #define TAS2573_FCT_OUTPUT_POWERTOT	TASDEVICE_REG(0x00, 0x66, 0x0C)
110 /* YM64 */
111 #define TAS2573_FCT_OUTPUT_F0		TASDEVICE_REG(0x00, 0x65, 0x18)
112 /* YM147 */
113 #define TAS2573_FCT_STATUS_CTRL		TASDEVICE_REG(0x00, 0x68, 0x74)
114 /* YM148 */
115 #define TAS2573_FCT_STATUS_BINNING	TASDEVICE_REG(0x00, 0x68, 0x78)
116 /* YM664 */
117 #define TAS2573_RE_OUT			TASDEVICE_REG(0x00, 0x7A, 0x18)
118 /* YM952 */
119 #define TAS2573_SILENCE_DETECTED	TASDEVICE_REG(0x00, 0x83, 0x60)
120 /* YM954 */
121 #define TAS2573_OPEN_CIRCUIT		TASDEVICE_REG(0x00, 0x83, 0x68)
122 /* YM955 */
123 #define TAS2573_SHORTCKT		TASDEVICE_REG(0x00, 0x83, 0x6C)
124 
125 #define TAS2781_PRM_INT_MASK_REG	TASDEVICE_REG(0x00, 0x00, 0x3b)
126 #define TAS2781_PRM_CLK_CFG_REG		TASDEVICE_REG(0x00, 0x00, 0x5c)
127 #define TAS2781_PRM_RSVD_REG		TASDEVICE_REG(0x00, 0x01, 0x19)
128 #define TAS2781_PRM_TEST_57_REG		TASDEVICE_REG(0x00, 0xfd, 0x39)
129 #define TAS2781_PRM_TEST_62_REG		TASDEVICE_REG(0x00, 0xfd, 0x3e)
130 #define TAS2781_PRM_PVDD_UVLO_REG	TASDEVICE_REG(0x00, 0x00, 0x71)
131 #define TAS2781_PRM_CHNL_0_REG		TASDEVICE_REG(0x00, 0x00, 0x03)
132 #define TAS2781_PRM_NG_CFG0_REG		TASDEVICE_REG(0x00, 0x00, 0x35)
133 #define TAS2781_PRM_IDLE_CH_DET_REG	TASDEVICE_REG(0x00, 0x00, 0x66)
134 #define TAS2781_PRM_PLT_FLAG_REG	TASDEVICE_REG(0x00, 0x14, 0x38)
135 #define TAS2781_PRM_SINEGAIN_REG	TASDEVICE_REG(0x00, 0x14, 0x40)
136 #define TAS2781_PRM_SINEGAIN2_REG	TASDEVICE_REG(0x00, 0x14, 0x44)
137 
138 #define TAS2781_TEST_UNLOCK_REG		TASDEVICE_REG(0x00, 0xfd, 0x0d)
139 #define TAS2781_TEST_PAGE_UNLOCK	0x0d
140 
141 #define TAS2781_RUNTIME_LATCH_RE_REG	TASDEVICE_REG(0x00, 0x00, 0x49)
142 #define TAS2781_RUNTIME_RE_REG_TF	TASDEVICE_REG(0x64, 0x62, 0x48)
143 #define TAS2781_RUNTIME_RE_REG		TASDEVICE_REG(0x64, 0x63, 0x44)
144 
145 enum audio_device {
146 	TAS2020,
147 	TAS2118,
148 	TAS2120,
149 	TAS2320,
150 	TAS2563,
151 	TAS2568,
152 	TAS2570,
153 	TAS2572,
154 	TAS2573,
155 	TAS2574,
156 	TAS2781,
157 	TAS5802,
158 	TAS5806M,
159 	TAS5806MD,
160 	TAS5815,
161 	TAS5822,
162 	TAS5825,
163 	TAS5827,
164 	TAS5828,
165 	TAS5830,
166 	TAS5832,
167 	TAS_OTHERS,
168 };
169 
170 enum dspbin_type {
171 	TASDEV_BASIC,
172 	TASDEV_ALPHA,
173 	TASDEV_BETA,
174 };
175 
176 struct bulk_reg_val {
177 	int reg;
178 	unsigned char val[4];
179 	unsigned char val_len;
180 	bool is_locked;
181 };
182 
183 struct tasdevice {
184 	struct bulk_reg_val *cali_data_backup;
185 	struct bulk_reg_val alp_cali_bckp;
186 	struct tasdevice_fw *cali_data_fmw;
187 	void *cali_specific;
188 	unsigned int dev_addr;
189 	unsigned int err_code;
190 	unsigned char cur_book;
191 	short cur_prog;
192 	short cur_conf;
193 	bool is_loading;
194 	bool is_loaderr;
195 };
196 
197 struct cali_reg {
198 	unsigned int r0_reg;
199 	unsigned int r0_low_reg;
200 	unsigned int invr0_reg;
201 	unsigned int pow_reg;
202 	unsigned int tlimit_reg;
203 };
204 
205 struct calidata {
206 	unsigned char *data;
207 	unsigned long total_sz;
208 	struct cali_reg cali_reg_array;
209 	unsigned int cali_dat_sz_per_dev;
210 };
211 
212 /*
213  * To enable CONFIG_SND_SOC_TAS2781_ACOUST_I2C will create a bridge to the
214  * acoustic tuning tool which can tune the chips' acoustic effect. Due to the
215  * whole directly exposing the registers, there exist some potential risks. So
216  * this define is invisible in Kconfig, anyone who wants to use acoustic tool
217  * have to edit the source manually.
218  */
219 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C
220 #define TASDEV_DATA_PAYLOAD_SIZE	128
221 struct acoustic_data {
222 	unsigned char len;
223 	unsigned char id;
224 	unsigned char addr;
225 	unsigned char book;
226 	unsigned char page;
227 	unsigned char reg;
228 	unsigned char data[TASDEV_DATA_PAYLOAD_SIZE];
229 };
230 #endif
231 
232 struct tasdevice_priv {
233 	struct tasdevice tasdevice[TASDEVICE_MAX_CHANNELS];
234 	struct tasdevice_rca rcabin;
235 	struct calidata cali_data;
236 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C
237 	struct acoustic_data acou_data;
238 #endif
239 	struct tasdevice_fw *fmw;
240 	struct gpio_desc *reset;
241 	struct mutex codec_lock;
242 	struct regmap *regmap;
243 	struct device *dev;
244 
245 	unsigned char cal_binaryname[TASDEVICE_MAX_CHANNELS][64];
246 	unsigned char crc8_lkp_tbl[CRC8_TABLE_SIZE];
247 	unsigned char coef_binaryname[64];
248 	unsigned char rca_binaryname[64];
249 	unsigned char dev_name[32];
250 	const unsigned char (*dvc_tlv_table)[4];
251 	const char *name_prefix;
252 	unsigned char ndev;
253 	unsigned int dspbin_typ;
254 	unsigned int magic_num;
255 	unsigned int chip_id;
256 	unsigned int sysclk;
257 	int speaker_id;
258 
259 	int irq;
260 	int cur_prog;
261 	int cur_conf;
262 	int fw_state;
263 	int index;
264 	void *client;
265 	void *codec;
266 	bool force_fwload_status;
267 	bool playback_started;
268 	bool isacpi;
269 	bool isspi;
270 	unsigned int global_addr;
271 
272 	int (*fw_parse_variable_header)(struct tasdevice_priv *tas_priv,
273 		const struct firmware *fmw, int offset);
274 	int (*fw_parse_program_data)(struct tasdevice_priv *tas_priv,
275 		struct tasdevice_fw *tas_fmw,
276 		const struct firmware *fmw, int offset);
277 	int (*fw_parse_configuration_data)(struct tasdevice_priv *tas_priv,
278 		struct tasdevice_fw *tas_fmw,
279 		const struct firmware *fmw, int offset);
280 	int (*fw_parse_fct_param_address)(struct tasdevice_priv *tas_priv,
281 		struct tasdevice_fw *tas_fmw,
282 		const struct firmware *fmw, int offset);
283 	int (*tasdevice_load_block)(struct tasdevice_priv *tas_priv,
284 		struct tasdev_blk *block);
285 
286 	int (*change_chn_book)(struct tasdevice_priv *tas_priv,
287 		unsigned short chn, int book);
288 	int (*update_bits)(struct tasdevice_priv *tas_priv,
289 		unsigned short chn, unsigned int reg, unsigned int mask,
290 		unsigned int value);
291 	int (*dev_read)(struct tasdevice_priv *tas_priv,
292 		unsigned short chn, unsigned int reg, unsigned int *value);
293 	int (*dev_bulk_read)(struct tasdevice_priv *tas_priv,
294 		unsigned short chn, unsigned int reg, unsigned char *p_data,
295 		unsigned int n_length);
296 };
297 
298 int tasdevice_dev_read(struct tasdevice_priv *tas_priv,
299 	unsigned short chn, unsigned int reg, unsigned int *value);
300 int tasdevice_dev_bulk_read(struct tasdevice_priv *tas_priv,
301 	unsigned short chn, unsigned int reg, unsigned char *p_data,
302 	unsigned int n_length);
303 int tasdevice_dev_write(struct tasdevice_priv *tas_priv,
304 	unsigned short chn, unsigned int reg, unsigned int value);
305 int tasdevice_dev_bulk_write(
306 	struct tasdevice_priv *tas_priv, unsigned short chn,
307 	unsigned int reg, unsigned char *p_data, unsigned int n_length);
308 void tasdevice_remove(struct tasdevice_priv *tas_priv);
309 #endif /* __TAS2781_H__ */
310