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