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