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