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