1 /* SPDX-License-Identifier: GPL-2.0 */
2 //
3 // ALSA SoC Texas Instruments TAS2563/TAS2781 Audio Smart Amplifier
4 //
5 // Copyright (C) 2022 - 2025 Texas Instruments Incorporated
6 // https://www.ti.com
7 //
8 // The TAS2563/TAS2781 driver implements a flexible and configurable
9 // algo coefficient setting for one, two, or even multiple
10 // TAS2563/TAS2781 chips.
11 //
12 // Author: Shenghao Ding <shenghao-ding@ti.com>
13 // Author: Kevin Lu <kevin-lu@ti.com>
14 // Author: Baojun Xu <baojun.xu@ti.com>
15 //
16 
17 #ifndef __TAS2781_H__
18 #define __TAS2781_H__
19 
20 #include "tas2781-dsp.h"
21 
22 /* version number */
23 #define TAS2781_DRV_VER			1
24 #define SMARTAMP_MODULE_NAME		"tas2781"
25 #define TAS2781_GLOBAL_ADDR	0x40
26 #define TAS2563_GLOBAL_ADDR	0x48
27 #define TASDEVICE_RATES			(SNDRV_PCM_RATE_44100 |\
28 	SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_96000 |\
29 	SNDRV_PCM_RATE_88200)
30 
31 #define TASDEVICE_FORMATS		(SNDRV_PCM_FMTBIT_S16_LE | \
32 	SNDRV_PCM_FMTBIT_S24_LE | \
33 	SNDRV_PCM_FMTBIT_S32_LE)
34 
35 /* PAGE Control Register (available in page0 of each book) */
36 #define TASDEVICE_PAGE_SELECT		0x00
37 #define TASDEVICE_BOOKCTL_PAGE		0x00
38 #define TASDEVICE_BOOKCTL_REG		127
39 #define TASDEVICE_BOOK_ID(reg)		(reg / (256 * 128))
40 #define TASDEVICE_PAGE_ID(reg)		((reg % (256 * 128)) / 128)
41 #define TASDEVICE_PAGE_REG(reg)		((reg % (256 * 128)) % 128)
42 #define TASDEVICE_PGRG(reg)		(reg % (256 * 128))
43 #define TASDEVICE_REG(book, page, reg)	(((book * 256 * 128) + \
44 					(page * 128)) + reg)
45 
46 /* Software Reset */
47 #define TASDEVICE_REG_SWRESET		TASDEVICE_REG(0x0, 0x0, 0x01)
48 #define TASDEVICE_REG_SWRESET_RESET	BIT(0)
49 
50 /* I2C Checksum */
51 #define TASDEVICE_CHECKSUM_REG		TASDEVICE_REG(0x0, 0x0, 0x7e)
52 
53 /* XM_340 */
54 #define	TASDEVICE_XM_A1_REG	TASDEVICE_REG(0x64, 0x63, 0x3c)
55 /* XM_341 */
56 #define	TASDEVICE_XM_A2_REG	TASDEVICE_REG(0x64, 0x63, 0x38)
57 
58 /* Volume control */
59 #define TAS2563_DVC_LVL			TASDEVICE_REG(0x00, 0x02, 0x0c)
60 #define TAS2781_DVC_LVL			TASDEVICE_REG(0x0, 0x0, 0x1a)
61 #define TAS2781_AMP_LEVEL		TASDEVICE_REG(0x0, 0x0, 0x03)
62 #define TAS2781_AMP_LEVEL_MASK		GENMASK(5, 1)
63 
64 #define TAS2563_IDLE		TASDEVICE_REG(0x00, 0x00, 0x3e)
65 #define TAS2563_PRM_R0_REG		TASDEVICE_REG(0x00, 0x0f, 0x34)
66 
67 #define TAS2563_RUNTIME_RE_REG_TF	TASDEVICE_REG(0x64, 0x02, 0x70)
68 #define TAS2563_RUNTIME_RE_REG		TASDEVICE_REG(0x64, 0x02, 0x48)
69 
70 #define TAS2563_PRM_ENFF_REG		TASDEVICE_REG(0x00, 0x0d, 0x54)
71 #define TAS2563_PRM_DISTCK_REG		TASDEVICE_REG(0x00, 0x0d, 0x58)
72 #define TAS2563_PRM_TE_SCTHR_REG	TASDEVICE_REG(0x00, 0x0f, 0x60)
73 #define TAS2563_PRM_PLT_FLAG_REG	TASDEVICE_REG(0x00, 0x0d, 0x74)
74 #define TAS2563_PRM_SINEGAIN_REG	TASDEVICE_REG(0x00, 0x0d, 0x7c)
75 /* prm_Int_B0 */
76 #define TAS2563_TE_TA1_REG		TASDEVICE_REG(0x00, 0x10, 0x0c)
77 /* prm_Int_A1 */
78 #define TAS2563_TE_TA1_AT_REG		TASDEVICE_REG(0x00, 0x10, 0x10)
79 /* prm_TE_Beta */
80 #define TAS2563_TE_TA2_REG		TASDEVICE_REG(0x00, 0x0f, 0x64)
81 /* prm_TE_Beta1 */
82 #define TAS2563_TE_AT_REG		TASDEVICE_REG(0x00, 0x0f, 0x68)
83 /* prm_TE_1_Beta1 */
84 #define TAS2563_TE_DT_REG		TASDEVICE_REG(0x00, 0x0f, 0x70)
85 
86 #define TAS2781_PRM_INT_MASK_REG	TASDEVICE_REG(0x00, 0x00, 0x3b)
87 #define TAS2781_PRM_CLK_CFG_REG		TASDEVICE_REG(0x00, 0x00, 0x5c)
88 #define TAS2781_PRM_RSVD_REG		TASDEVICE_REG(0x00, 0x01, 0x19)
89 #define TAS2781_PRM_TEST_57_REG		TASDEVICE_REG(0x00, 0xfd, 0x39)
90 #define TAS2781_PRM_TEST_62_REG		TASDEVICE_REG(0x00, 0xfd, 0x3e)
91 #define TAS2781_PRM_PVDD_UVLO_REG	TASDEVICE_REG(0x00, 0x00, 0x71)
92 #define TAS2781_PRM_CHNL_0_REG		TASDEVICE_REG(0x00, 0x00, 0x03)
93 #define TAS2781_PRM_NG_CFG0_REG		TASDEVICE_REG(0x00, 0x00, 0x35)
94 #define TAS2781_PRM_IDLE_CH_DET_REG	TASDEVICE_REG(0x00, 0x00, 0x66)
95 #define TAS2781_PRM_PLT_FLAG_REG	TASDEVICE_REG(0x00, 0x14, 0x38)
96 #define TAS2781_PRM_SINEGAIN_REG	TASDEVICE_REG(0x00, 0x14, 0x40)
97 #define TAS2781_PRM_SINEGAIN2_REG	TASDEVICE_REG(0x00, 0x14, 0x44)
98 
99 #define TAS2781_TEST_UNLOCK_REG		TASDEVICE_REG(0x00, 0xfd, 0x0d)
100 #define TAS2781_TEST_PAGE_UNLOCK	0x0d
101 
102 #define TAS2781_RUNTIME_LATCH_RE_REG	TASDEVICE_REG(0x00, 0x00, 0x49)
103 #define TAS2781_RUNTIME_RE_REG_TF	TASDEVICE_REG(0x64, 0x62, 0x48)
104 #define TAS2781_RUNTIME_RE_REG		TASDEVICE_REG(0x64, 0x63, 0x44)
105 
106 #define TASDEVICE_CMD_SING_W		0x1
107 #define TASDEVICE_CMD_BURST		0x2
108 #define TASDEVICE_CMD_DELAY		0x3
109 #define TASDEVICE_CMD_FIELD_W		0x4
110 
111 enum audio_device {
112 	TAS2563,
113 	TAS2781,
114 };
115 
116 enum dspbin_type {
117 	TASDEV_BASIC,
118 	TASDEV_ALPHA,
119 	TASDEV_BETA,
120 };
121 
122 enum device_catlog_id {
123 	LENOVO = 0,
124 	OTHERS
125 };
126 
127 struct bulk_reg_val {
128 	int reg;
129 	unsigned char val[4];
130 	unsigned char val_len;
131 	bool is_locked;
132 };
133 
134 struct tasdevice {
135 	struct bulk_reg_val *cali_data_backup;
136 	struct bulk_reg_val alp_cali_bckp;
137 	struct tasdevice_fw *cali_data_fmw;
138 	unsigned int dev_addr;
139 	unsigned int err_code;
140 	unsigned char cur_book;
141 	short cur_prog;
142 	short cur_conf;
143 	bool is_loading;
144 	bool is_loaderr;
145 };
146 
147 struct cali_reg {
148 	unsigned int r0_reg;
149 	unsigned int r0_low_reg;
150 	unsigned int invr0_reg;
151 	unsigned int pow_reg;
152 	unsigned int tlimit_reg;
153 };
154 
155 struct calidata {
156 	unsigned char *data;
157 	unsigned long total_sz;
158 	struct cali_reg cali_reg_array;
159 	unsigned int cali_dat_sz_per_dev;
160 };
161 
162 struct tasdevice_priv {
163 	struct tasdevice tasdevice[TASDEVICE_MAX_CHANNELS];
164 	struct tasdevice_rca rcabin;
165 	struct calidata cali_data;
166 	struct tasdevice_fw *fmw;
167 	struct gpio_desc *speaker_id;
168 	struct gpio_desc *reset;
169 	struct mutex codec_lock;
170 	struct regmap *regmap;
171 	struct device *dev;
172 
173 	enum device_catlog_id catlog_id;
174 	unsigned char cal_binaryname[TASDEVICE_MAX_CHANNELS][64];
175 	unsigned char crc8_lkp_tbl[CRC8_TABLE_SIZE];
176 	unsigned char coef_binaryname[64];
177 	unsigned char rca_binaryname[64];
178 	unsigned char dev_name[32];
179 	const char *name_prefix;
180 	unsigned char ndev;
181 	unsigned int dspbin_typ;
182 	unsigned int magic_num;
183 	unsigned int chip_id;
184 	unsigned int sysclk;
185 
186 	int irq;
187 	int cur_prog;
188 	int cur_conf;
189 	int fw_state;
190 	int index;
191 	void *client;
192 	void *codec;
193 	bool force_fwload_status;
194 	bool playback_started;
195 	bool isacpi;
196 	bool is_user_space_calidata;
197 	unsigned int global_addr;
198 
199 	int (*fw_parse_variable_header)(struct tasdevice_priv *tas_priv,
200 		const struct firmware *fmw, int offset);
201 	int (*fw_parse_program_data)(struct tasdevice_priv *tas_priv,
202 		struct tasdevice_fw *tas_fmw,
203 		const struct firmware *fmw, int offset);
204 	int (*fw_parse_configuration_data)(struct tasdevice_priv *tas_priv,
205 		struct tasdevice_fw *tas_fmw,
206 		const struct firmware *fmw, int offset);
207 	int (*fw_parse_fct_param_address)(struct tasdevice_priv *tas_priv,
208 		struct tasdevice_fw *tas_fmw,
209 		const struct firmware *fmw, int offset);
210 	int (*tasdevice_load_block)(struct tasdevice_priv *tas_priv,
211 		struct tasdev_blk *block);
212 
213 	int (*save_calibration)(struct tasdevice_priv *tas_priv);
214 	void (*apply_calibration)(struct tasdevice_priv *tas_priv);
215 };
216 
217 void tasdevice_reset(struct tasdevice_priv *tas_dev);
218 int tascodec_init(struct tasdevice_priv *tas_priv, void *codec,
219 	struct module *module,
220 	void (*cont)(const struct firmware *fw, void *context));
221 struct tasdevice_priv *tasdevice_kzalloc(struct i2c_client *i2c);
222 int tasdevice_init(struct tasdevice_priv *tas_priv);
223 void tasdevice_remove(struct tasdevice_priv *tas_priv);
224 int tasdevice_save_calibration(struct tasdevice_priv *tas_priv);
225 void tasdevice_apply_calibration(struct tasdevice_priv *tas_priv);
226 int tasdev_chn_switch(struct tasdevice_priv *tas_priv,
227 	unsigned short chn);
228 int tasdevice_dev_read(struct tasdevice_priv *tas_priv,
229 	unsigned short chn, unsigned int reg, unsigned int *value);
230 int tasdevice_dev_write(struct tasdevice_priv *tas_priv,
231 	unsigned short chn, unsigned int reg, unsigned int value);
232 int tasdevice_dev_bulk_write(
233 	struct tasdevice_priv *tas_priv, unsigned short chn,
234 	unsigned int reg, unsigned char *p_data, unsigned int n_length);
235 int tasdevice_dev_bulk_read(struct tasdevice_priv *tas_priv,
236 	unsigned short chn, unsigned int reg, unsigned char *p_data,
237 	unsigned int n_length);
238 int tasdevice_dev_update_bits(
239 	struct tasdevice_priv *tasdevice, unsigned short chn,
240 	unsigned int reg, unsigned int mask, unsigned int value);
241 int tasdevice_amp_putvol(struct tasdevice_priv *tas_priv,
242 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc);
243 int tasdevice_amp_getvol(struct tasdevice_priv *tas_priv,
244 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc);
245 int tasdevice_digital_putvol(struct tasdevice_priv *tas_priv,
246 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc);
247 int tasdevice_digital_getvol(struct tasdevice_priv *tas_priv,
248 	struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc);
249 
250 #endif /* __TAS2781_H__ */
251