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 /* Software Reset, compatble with new device (TAS5825). */ 53 #define TASDEVICE_REG_SWRESET TASDEVICE_REG(0x0, 0x0, 0x01) 54 #define TASDEVICE_REG_SWRESET_RESET BIT(0) 55 56 #define TAS5825_REG_SWRESET_RESET (BIT(0) | BIT(4)) 57 58 /* Checksum */ 59 #define TASDEVICE_CHECKSUM_REG TASDEVICE_REG(0x0, 0x0, 0x7e) 60 61 /* XM_340 */ 62 #define TASDEVICE_XM_A1_REG TASDEVICE_REG(0x64, 0x63, 0x3c) 63 /* XM_341 */ 64 #define TASDEVICE_XM_A2_REG TASDEVICE_REG(0x64, 0x63, 0x38) 65 66 /* Volume control */ 67 #define TAS2563_DVC_LVL TASDEVICE_REG(0x00, 0x02, 0x0c) 68 #define TAS2781_DVC_LVL TASDEVICE_REG(0x0, 0x0, 0x1a) 69 #define TAS2781_AMP_LEVEL TASDEVICE_REG(0x0, 0x0, 0x03) 70 #define TAS2781_AMP_LEVEL_MASK GENMASK(5, 1) 71 72 #define TAS2563_IDLE TASDEVICE_REG(0x00, 0x00, 0x3e) 73 #define TAS2563_PRM_R0_REG TASDEVICE_REG(0x00, 0x0f, 0x34) 74 75 #define TAS2563_RUNTIME_RE_REG_TF TASDEVICE_REG(0x64, 0x02, 0x70) 76 #define TAS2563_RUNTIME_RE_REG TASDEVICE_REG(0x64, 0x02, 0x48) 77 78 #define TAS2563_PRM_ENFF_REG TASDEVICE_REG(0x00, 0x0d, 0x54) 79 #define TAS2563_PRM_DISTCK_REG TASDEVICE_REG(0x00, 0x0d, 0x58) 80 #define TAS2563_PRM_TE_SCTHR_REG TASDEVICE_REG(0x00, 0x0f, 0x60) 81 #define TAS2563_PRM_PLT_FLAG_REG TASDEVICE_REG(0x00, 0x0d, 0x74) 82 #define TAS2563_PRM_SINEGAIN_REG TASDEVICE_REG(0x00, 0x0d, 0x7c) 83 /* prm_Int_B0 */ 84 #define TAS2563_TE_TA1_REG TASDEVICE_REG(0x00, 0x10, 0x0c) 85 /* prm_Int_A1 */ 86 #define TAS2563_TE_TA1_AT_REG TASDEVICE_REG(0x00, 0x10, 0x10) 87 /* prm_TE_Beta */ 88 #define TAS2563_TE_TA2_REG TASDEVICE_REG(0x00, 0x0f, 0x64) 89 /* prm_TE_Beta1 */ 90 #define TAS2563_TE_AT_REG TASDEVICE_REG(0x00, 0x0f, 0x68) 91 /* prm_TE_1_Beta1 */ 92 #define TAS2563_TE_DT_REG TASDEVICE_REG(0x00, 0x0f, 0x70) 93 94 #define TAS2781_PRM_INT_MASK_REG TASDEVICE_REG(0x00, 0x00, 0x3b) 95 #define TAS2781_PRM_CLK_CFG_REG TASDEVICE_REG(0x00, 0x00, 0x5c) 96 #define TAS2781_PRM_RSVD_REG TASDEVICE_REG(0x00, 0x01, 0x19) 97 #define TAS2781_PRM_TEST_57_REG TASDEVICE_REG(0x00, 0xfd, 0x39) 98 #define TAS2781_PRM_TEST_62_REG TASDEVICE_REG(0x00, 0xfd, 0x3e) 99 #define TAS2781_PRM_PVDD_UVLO_REG TASDEVICE_REG(0x00, 0x00, 0x71) 100 #define TAS2781_PRM_CHNL_0_REG TASDEVICE_REG(0x00, 0x00, 0x03) 101 #define TAS2781_PRM_NG_CFG0_REG TASDEVICE_REG(0x00, 0x00, 0x35) 102 #define TAS2781_PRM_IDLE_CH_DET_REG TASDEVICE_REG(0x00, 0x00, 0x66) 103 #define TAS2781_PRM_PLT_FLAG_REG TASDEVICE_REG(0x00, 0x14, 0x38) 104 #define TAS2781_PRM_SINEGAIN_REG TASDEVICE_REG(0x00, 0x14, 0x40) 105 #define TAS2781_PRM_SINEGAIN2_REG TASDEVICE_REG(0x00, 0x14, 0x44) 106 107 #define TAS2781_TEST_UNLOCK_REG TASDEVICE_REG(0x00, 0xfd, 0x0d) 108 #define TAS2781_TEST_PAGE_UNLOCK 0x0d 109 110 #define TAS2781_RUNTIME_LATCH_RE_REG TASDEVICE_REG(0x00, 0x00, 0x49) 111 #define TAS2781_RUNTIME_RE_REG_TF TASDEVICE_REG(0x64, 0x62, 0x48) 112 #define TAS2781_RUNTIME_RE_REG TASDEVICE_REG(0x64, 0x63, 0x44) 113 114 enum audio_device { 115 TAS2020, 116 TAS2118, 117 TAS2120, 118 TAS2320, 119 TAS2563, 120 TAS2568, 121 TAS2570, 122 TAS2572, 123 TAS2573, 124 TAS2574, 125 TAS2781, 126 TAS5802, 127 TAS5806M, 128 TAS5806MD, 129 TAS5815, 130 TAS5822, 131 TAS5825, 132 TAS5827, 133 TAS5828, 134 TAS5830, 135 TAS5832, 136 TAS_OTHERS, 137 }; 138 139 enum dspbin_type { 140 TASDEV_BASIC, 141 TASDEV_ALPHA, 142 TASDEV_BETA, 143 }; 144 145 struct bulk_reg_val { 146 int reg; 147 unsigned char val[4]; 148 unsigned char val_len; 149 bool is_locked; 150 }; 151 152 struct tasdevice { 153 struct bulk_reg_val *cali_data_backup; 154 struct bulk_reg_val alp_cali_bckp; 155 struct tasdevice_fw *cali_data_fmw; 156 void *cali_specific; 157 unsigned int dev_addr; 158 unsigned int err_code; 159 unsigned char cur_book; 160 short cur_prog; 161 short cur_conf; 162 bool is_loading; 163 bool is_loaderr; 164 }; 165 166 struct cali_reg { 167 unsigned int r0_reg; 168 unsigned int r0_low_reg; 169 unsigned int invr0_reg; 170 unsigned int pow_reg; 171 unsigned int tlimit_reg; 172 }; 173 174 struct calidata { 175 unsigned char *data; 176 unsigned long total_sz; 177 struct cali_reg cali_reg_array; 178 unsigned int cali_dat_sz_per_dev; 179 }; 180 181 /* 182 * To enable CONFIG_SND_SOC_TAS2781_ACOUST_I2C will create a bridge to the 183 * acoustic tuning tool which can tune the chips' acoustic effect. Due to the 184 * whole directly exposing the registers, there exist some potential risks. So 185 * this define is invisible in Kconfig, anyone who wants to use acoustic tool 186 * have to edit the source manually. 187 */ 188 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C 189 #define TASDEV_DATA_PAYLOAD_SIZE 128 190 struct acoustic_data { 191 unsigned char len; 192 unsigned char id; 193 unsigned char addr; 194 unsigned char book; 195 unsigned char page; 196 unsigned char reg; 197 unsigned char data[TASDEV_DATA_PAYLOAD_SIZE]; 198 }; 199 #endif 200 201 struct tasdevice_priv { 202 struct tasdevice tasdevice[TASDEVICE_MAX_CHANNELS]; 203 struct tasdevice_rca rcabin; 204 struct calidata cali_data; 205 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C 206 struct acoustic_data acou_data; 207 #endif 208 struct tasdevice_fw *fmw; 209 struct gpio_desc *reset; 210 struct mutex codec_lock; 211 struct regmap *regmap; 212 struct device *dev; 213 214 unsigned char cal_binaryname[TASDEVICE_MAX_CHANNELS][64]; 215 unsigned char crc8_lkp_tbl[CRC8_TABLE_SIZE]; 216 unsigned char coef_binaryname[64]; 217 unsigned char rca_binaryname[64]; 218 unsigned char dev_name[32]; 219 const unsigned char (*dvc_tlv_table)[4]; 220 const char *name_prefix; 221 unsigned char ndev; 222 unsigned int dspbin_typ; 223 unsigned int magic_num; 224 unsigned int chip_id; 225 unsigned int sysclk; 226 int speaker_id; 227 228 int irq; 229 int cur_prog; 230 int cur_conf; 231 int fw_state; 232 int index; 233 void *client; 234 void *codec; 235 bool force_fwload_status; 236 bool playback_started; 237 bool isacpi; 238 bool isspi; 239 unsigned int global_addr; 240 241 int (*fw_parse_variable_header)(struct tasdevice_priv *tas_priv, 242 const struct firmware *fmw, int offset); 243 int (*fw_parse_program_data)(struct tasdevice_priv *tas_priv, 244 struct tasdevice_fw *tas_fmw, 245 const struct firmware *fmw, int offset); 246 int (*fw_parse_configuration_data)(struct tasdevice_priv *tas_priv, 247 struct tasdevice_fw *tas_fmw, 248 const struct firmware *fmw, int offset); 249 int (*fw_parse_fct_param_address)(struct tasdevice_priv *tas_priv, 250 struct tasdevice_fw *tas_fmw, 251 const struct firmware *fmw, int offset); 252 int (*tasdevice_load_block)(struct tasdevice_priv *tas_priv, 253 struct tasdev_blk *block); 254 255 int (*change_chn_book)(struct tasdevice_priv *tas_priv, 256 unsigned short chn, int book); 257 int (*update_bits)(struct tasdevice_priv *tas_priv, 258 unsigned short chn, unsigned int reg, unsigned int mask, 259 unsigned int value); 260 int (*dev_read)(struct tasdevice_priv *tas_priv, 261 unsigned short chn, unsigned int reg, unsigned int *value); 262 int (*dev_bulk_read)(struct tasdevice_priv *tas_priv, 263 unsigned short chn, unsigned int reg, unsigned char *p_data, 264 unsigned int n_length); 265 }; 266 267 int tasdevice_dev_read(struct tasdevice_priv *tas_priv, 268 unsigned short chn, unsigned int reg, unsigned int *value); 269 int tasdevice_dev_bulk_read(struct tasdevice_priv *tas_priv, 270 unsigned short chn, unsigned int reg, unsigned char *p_data, 271 unsigned int n_length); 272 int tasdevice_dev_write(struct tasdevice_priv *tas_priv, 273 unsigned short chn, unsigned int reg, unsigned int value); 274 int tasdevice_dev_bulk_write( 275 struct tasdevice_priv *tas_priv, unsigned short chn, 276 unsigned int reg, unsigned char *p_data, unsigned int n_length); 277 void tasdevice_remove(struct tasdevice_priv *tas_priv); 278 #endif /* __TAS2781_H__ */ 279