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 #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 TAS2574, 124 TAS2781, 125 TAS5802, 126 TAS5806M, 127 TAS5806MD, 128 TAS5815, 129 TAS5822, 130 TAS5825, 131 TAS5827, 132 TAS5828, 133 TAS5830, 134 TAS_OTHERS, 135 }; 136 137 enum dspbin_type { 138 TASDEV_BASIC, 139 TASDEV_ALPHA, 140 TASDEV_BETA, 141 }; 142 143 struct bulk_reg_val { 144 int reg; 145 unsigned char val[4]; 146 unsigned char val_len; 147 bool is_locked; 148 }; 149 150 struct tasdevice { 151 struct bulk_reg_val *cali_data_backup; 152 struct bulk_reg_val alp_cali_bckp; 153 struct tasdevice_fw *cali_data_fmw; 154 unsigned int dev_addr; 155 unsigned int err_code; 156 unsigned char cur_book; 157 short cur_prog; 158 short cur_conf; 159 bool is_loading; 160 bool is_loaderr; 161 }; 162 163 struct cali_reg { 164 unsigned int r0_reg; 165 unsigned int r0_low_reg; 166 unsigned int invr0_reg; 167 unsigned int pow_reg; 168 unsigned int tlimit_reg; 169 }; 170 171 struct calidata { 172 unsigned char *data; 173 unsigned long total_sz; 174 struct cali_reg cali_reg_array; 175 unsigned int cali_dat_sz_per_dev; 176 }; 177 178 /* 179 * To enable CONFIG_SND_SOC_TAS2781_ACOUST_I2C will create a bridge to the 180 * acoustic tuning tool which can tune the chips' acoustic effect. Due to the 181 * whole directly exposing the registers, there exist some potential risks. So 182 * this define is invisible in Kconfig, anyone who wants to use acoustic tool 183 * have to edit the source manually. 184 */ 185 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C 186 #define TASDEV_DATA_PAYLOAD_SIZE 128 187 struct acoustic_data { 188 unsigned char len; 189 unsigned char id; 190 unsigned char addr; 191 unsigned char book; 192 unsigned char page; 193 unsigned char reg; 194 unsigned char data[TASDEV_DATA_PAYLOAD_SIZE]; 195 }; 196 #endif 197 198 struct tasdevice_priv { 199 struct tasdevice tasdevice[TASDEVICE_MAX_CHANNELS]; 200 struct tasdevice_rca rcabin; 201 struct calidata cali_data; 202 #ifdef CONFIG_SND_SOC_TAS2781_ACOUST_I2C 203 struct acoustic_data acou_data; 204 #endif 205 struct tasdevice_fw *fmw; 206 struct gpio_desc *reset; 207 struct mutex codec_lock; 208 struct regmap *regmap; 209 struct device *dev; 210 211 unsigned char cal_binaryname[TASDEVICE_MAX_CHANNELS][64]; 212 unsigned char crc8_lkp_tbl[CRC8_TABLE_SIZE]; 213 unsigned char coef_binaryname[64]; 214 unsigned char rca_binaryname[64]; 215 unsigned char dev_name[32]; 216 const unsigned char (*dvc_tlv_table)[4]; 217 const char *name_prefix; 218 unsigned char ndev; 219 unsigned int dspbin_typ; 220 unsigned int magic_num; 221 unsigned int chip_id; 222 unsigned int sysclk; 223 int speaker_id; 224 225 int irq; 226 int cur_prog; 227 int cur_conf; 228 int fw_state; 229 int index; 230 void *client; 231 void *codec; 232 bool force_fwload_status; 233 bool playback_started; 234 bool isacpi; 235 bool isspi; 236 bool is_user_space_calidata; 237 unsigned int global_addr; 238 239 int (*fw_parse_variable_header)(struct tasdevice_priv *tas_priv, 240 const struct firmware *fmw, int offset); 241 int (*fw_parse_program_data)(struct tasdevice_priv *tas_priv, 242 struct tasdevice_fw *tas_fmw, 243 const struct firmware *fmw, int offset); 244 int (*fw_parse_configuration_data)(struct tasdevice_priv *tas_priv, 245 struct tasdevice_fw *tas_fmw, 246 const struct firmware *fmw, int offset); 247 int (*fw_parse_fct_param_address)(struct tasdevice_priv *tas_priv, 248 struct tasdevice_fw *tas_fmw, 249 const struct firmware *fmw, int offset); 250 int (*tasdevice_load_block)(struct tasdevice_priv *tas_priv, 251 struct tasdev_blk *block); 252 253 int (*change_chn_book)(struct tasdevice_priv *tas_priv, 254 unsigned short chn, int book); 255 int (*update_bits)(struct tasdevice_priv *tas_priv, 256 unsigned short chn, unsigned int reg, unsigned int mask, 257 unsigned int value); 258 int (*dev_read)(struct tasdevice_priv *tas_priv, 259 unsigned short chn, unsigned int reg, unsigned int *value); 260 int (*dev_bulk_read)(struct tasdevice_priv *tas_priv, 261 unsigned short chn, unsigned int reg, unsigned char *p_data, 262 unsigned int n_length); 263 }; 264 265 int tasdevice_dev_read(struct tasdevice_priv *tas_priv, 266 unsigned short chn, unsigned int reg, unsigned int *value); 267 int tasdevice_dev_bulk_read(struct tasdevice_priv *tas_priv, 268 unsigned short chn, unsigned int reg, unsigned char *p_data, 269 unsigned int n_length); 270 int tasdevice_dev_write(struct tasdevice_priv *tas_priv, 271 unsigned short chn, unsigned int reg, unsigned int value); 272 int tasdevice_dev_bulk_write( 273 struct tasdevice_priv *tas_priv, unsigned short chn, 274 unsigned int reg, unsigned char *p_data, unsigned int n_length); 275 void tasdevice_remove(struct tasdevice_priv *tas_priv); 276 #endif /* __TAS2781_H__ */ 277