1 /* SPDX-License-Identifier: GPL-2.0 */ 2 // 3 // ALSA SoC Texas Instruments TAS2781 Audio Smart Amplifier 4 // 5 // Copyright (C) 2024 Texas Instruments Incorporated 6 // https://www.ti.com 7 // 8 // The TAS2781 driver implements a flexible and configurable 9 // algo coefficient setting for TAS2781 chips. 10 // 11 // Author: Baojun Xu <baojun.xu@ti.com> 12 // 13 14 #ifndef __TAS2781_SPI_H__ 15 #define __TAS2781_SPI_H__ 16 17 #define TASDEVICE_RATES \ 18 (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \ 19 SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_88200) 20 21 #define TASDEVICE_FORMATS \ 22 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | \ 23 SNDRV_PCM_FMTBIT_S32_LE) 24 25 #define TASDEVICE_MAX_BOOK_NUM 256 26 #define TASDEVICE_MAX_PAGE 256 27 28 #define TASDEVICE_MAX_SIZE (TASDEVICE_MAX_BOOK_NUM * TASDEVICE_MAX_PAGE) 29 30 /* PAGE Control Register (available in page0 of each book) */ 31 #define TASDEVICE_PAGE_SELECT 0x00 32 #define TASDEVICE_BOOKCTL_PAGE 0x00 33 #define TASDEVICE_BOOKCTL_REG GENMASK(7, 1) 34 #define TASDEVICE_BOOK_ID(reg) (((reg) & GENMASK(24, 16)) >> 16) 35 #define TASDEVICE_PAGE_ID(reg) (((reg) & GENMASK(15, 8)) >> 8) 36 #define TASDEVICE_REG_ID(reg) (((reg) & GENMASK(7, 1)) >> 1) 37 #define TASDEVICE_PAGE_REG(reg) ((reg) & GENMASK(15, 1)) 38 #define TASDEVICE_REG(book, page, reg) \ 39 (((book) << 16) | ((page) << 8) | ((reg) << 1)) 40 41 /* Software Reset */ 42 #define TAS2781_REG_SWRESET TASDEVICE_REG(0x0, 0x0, 0x01) 43 #define TAS2781_REG_SWRESET_RESET BIT(0) 44 45 /* System Reset Check Register */ 46 #define TAS2781_REG_CLK_CONFIG TASDEVICE_REG(0x0, 0x0, 0x5c) 47 #define TAS2781_REG_CLK_CONFIG_RESET (0x19) 48 #define TAS2781_PRE_POST_RESET_CFG 3 49 50 /* Block Checksum */ 51 #define TASDEVICE_CHECKSUM TASDEVICE_REG(0x0, 0x0, 0x7e) 52 53 /* Volume control */ 54 #define TAS2781_DVC_LVL TASDEVICE_REG(0x0, 0x0, 0x1a) 55 #define TAS2781_AMP_LEVEL TASDEVICE_REG(0x0, 0x0, 0x03) 56 #define TAS2781_AMP_LEVEL_MASK GENMASK(5, 1) 57 58 #define TASDEVICE_CMD_SING_W 0x1 59 #define TASDEVICE_CMD_BURST 0x2 60 #define TASDEVICE_CMD_DELAY 0x3 61 #define TASDEVICE_CMD_FIELD_W 0x4 62 63 #define TAS2781_SPI_MAX_FREQ (4 * HZ_PER_MHZ) 64 65 #define TASDEVICE_CRC8_POLYNOMIAL 0x4d 66 #define TASDEVICE_SPEAKER_CALIBRATION_SIZE 20 67 68 /* Flag of calibration registers address. */ 69 #define TASDEVICE_CALIBRATION_REG_ADDRESS BIT(7) 70 71 #define TASDEVICE_CALIBRATION_DATA_NAME L"CALI_DATA" 72 #define TASDEVICE_CALIBRATION_DATA_SIZE 256 73 74 enum calib_data { 75 R0_VAL = 0, 76 INV_R0, 77 R0LOW, 78 POWER, 79 TLIM, 80 CALIB_MAX 81 }; 82 83 struct tasdevice_priv { 84 struct tasdevice_fw *cali_data_fmw; 85 struct tasdevice_rca rcabin; 86 struct tasdevice_fw *fmw; 87 struct gpio_desc *reset; 88 struct mutex codec_lock; 89 struct regmap *regmap; 90 struct device *dev; 91 92 unsigned char crc8_lkp_tbl[CRC8_TABLE_SIZE]; 93 unsigned char coef_binaryname[64]; 94 unsigned char rca_binaryname[64]; 95 unsigned char dev_name[32]; 96 97 bool force_fwload_status; 98 bool playback_started; 99 bool is_loading; 100 bool is_loaderr; 101 unsigned int cali_reg_array[CALIB_MAX]; 102 unsigned int cali_data[CALIB_MAX]; 103 unsigned int err_code; 104 void *codec; 105 int cur_book; 106 int cur_prog; 107 int cur_conf; 108 int fw_state; 109 int index; 110 int irq; 111 112 int (*fw_parse_variable_header)(struct tasdevice_priv *tas_priv, 113 const struct firmware *fmw, 114 int offset); 115 int (*fw_parse_program_data)(struct tasdevice_priv *tas_priv, 116 struct tasdevice_fw *tas_fmw, 117 const struct firmware *fmw, int offset); 118 int (*fw_parse_configuration_data)(struct tasdevice_priv *tas_priv, 119 struct tasdevice_fw *tas_fmw, 120 const struct firmware *fmw, 121 int offset); 122 int (*tasdevice_load_block)(struct tasdevice_priv *tas_priv, 123 struct tasdev_blk *block); 124 125 int (*save_calibration)(struct tasdevice_priv *tas_priv); 126 void (*apply_calibration)(struct tasdevice_priv *tas_priv); 127 }; 128 129 int tasdevice_spi_dev_read(struct tasdevice_priv *tas_priv, 130 unsigned int reg, unsigned int *value); 131 int tasdevice_spi_dev_write(struct tasdevice_priv *tas_priv, 132 unsigned int reg, unsigned int value); 133 int tasdevice_spi_dev_bulk_write(struct tasdevice_priv *tas_priv, 134 unsigned int reg, unsigned char *p_data, 135 unsigned int n_length); 136 int tasdevice_spi_dev_bulk_read(struct tasdevice_priv *tas_priv, 137 unsigned int reg, unsigned char *p_data, 138 unsigned int n_length); 139 int tasdevice_spi_dev_update_bits(struct tasdevice_priv *tasdevice, 140 unsigned int reg, unsigned int mask, 141 unsigned int value); 142 143 void tasdevice_spi_select_cfg_blk(void *context, int conf_no, 144 unsigned char block_type); 145 void tasdevice_spi_config_info_remove(void *context); 146 int tasdevice_spi_dsp_parser(void *context); 147 int tasdevice_spi_rca_parser(void *context, const struct firmware *fmw); 148 void tasdevice_spi_dsp_remove(void *context); 149 void tasdevice_spi_calbin_remove(void *context); 150 int tasdevice_spi_select_tuningprm_cfg(void *context, int prm, int cfg_no, 151 int rca_conf_no); 152 int tasdevice_spi_prmg_load(void *context, int prm_no); 153 int tasdevice_spi_prmg_calibdata_load(void *context, int prm_no); 154 void tasdevice_spi_tuning_switch(void *context, int state); 155 int tas2781_spi_load_calibration(void *context, char *file_name, 156 unsigned short i); 157 #endif /* __TAS2781_SPI_H__ */ 158