Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ALSA: hda/tas2781: Remove tas2781_spi_fwlib.c and leverage SND_SOC_TAS2781_FMWLIB

Most codes in tas2781_spi_fwlib.c are same as tas2781-fmwlib.c, mainly for
firmware parsing, only differece is the register reading, bit update and
book switching in i2c and spi. The main purpose of this patch is for code
cleaup and arrange the shared part for i2c and spi.

Signed-off-by: Shenghao Ding <shenghao-ding@ti.com>
Acked-by: Mark Brown <broonie@kernel.org>
Link: https://patch.msgid.link/20250429111055.567-1-shenghao-ding@ti.com
Signed-off-by: Takashi Iwai <tiwai@suse.de>

authored by

Shenghao Ding and committed by
Takashi Iwai
9fa6a693 849c83fe

+779 -2874
+37
include/sound/tas2781-comlib-i2c.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 */ 2 + // 3 + // ALSA SoC Texas Instruments TAS2563/TAS2781 Audio Smart Amplifier 4 + // 5 + // Copyright (C) 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 + // 14 + 15 + #ifndef __TAS2781_COMLIB_I2C_H__ 16 + #define __TAS2781_COMLIB_I2C_H__ 17 + 18 + void tasdevice_reset(struct tasdevice_priv *tas_dev); 19 + int tascodec_init(struct tasdevice_priv *tas_priv, void *codec, 20 + struct module *module, 21 + void (*cont)(const struct firmware *fw, void *context)); 22 + struct tasdevice_priv *tasdevice_kzalloc(struct i2c_client *i2c); 23 + int tasdevice_init(struct tasdevice_priv *tas_priv); 24 + int tasdev_chn_switch(struct tasdevice_priv *tas_priv, 25 + unsigned short chn); 26 + int tasdevice_dev_update_bits( 27 + struct tasdevice_priv *tasdevice, unsigned short chn, 28 + unsigned int reg, unsigned int mask, unsigned int value); 29 + int tasdevice_amp_putvol(struct tasdevice_priv *tas_priv, 30 + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc); 31 + int tasdevice_amp_getvol(struct tasdevice_priv *tas_priv, 32 + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc); 33 + int tasdevice_digital_getvol(struct tasdevice_priv *tas_priv, 34 + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc); 35 + int tasdevice_digital_putvol(struct tasdevice_priv *tas_priv, 36 + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc); 37 + #endif /* __TAS2781_COMLIB_I2C_H__ */
+19 -26
include/sound/tas2781.h
··· 32 32 SNDRV_PCM_FMTBIT_S24_LE | \ 33 33 SNDRV_PCM_FMTBIT_S32_LE) 34 34 35 + #define TASDEVICE_CRC8_POLYNOMIAL 0x4d 36 + 35 37 /* PAGE Control Register (available in page0 of each book) */ 36 38 #define TASDEVICE_PAGE_SELECT 0x00 37 39 #define TASDEVICE_BOOKCTL_PAGE 0x00 ··· 195 193 bool force_fwload_status; 196 194 bool playback_started; 197 195 bool isacpi; 196 + bool isspi; 198 197 bool is_user_space_calidata; 199 198 unsigned int global_addr; 200 199 ··· 213 210 int (*tasdevice_load_block)(struct tasdevice_priv *tas_priv, 214 211 struct tasdev_blk *block); 215 212 213 + int (*change_chn_book)(struct tasdevice_priv *tas_priv, 214 + unsigned short chn, int book); 215 + int (*update_bits)(struct tasdevice_priv *tas_priv, 216 + unsigned short chn, unsigned int reg, unsigned int mask, 217 + unsigned int value); 218 + int (*dev_read)(struct tasdevice_priv *tas_priv, 219 + unsigned short chn, unsigned int reg, unsigned int *value); 220 + int (*dev_bulk_read)(struct tasdevice_priv *tas_priv, 221 + unsigned short chn, unsigned int reg, unsigned char *p_data, 222 + unsigned int n_length); 216 223 int (*save_calibration)(struct tasdevice_priv *tas_priv); 217 224 void (*apply_calibration)(struct tasdevice_priv *tas_priv); 218 225 }; 219 226 220 - void tasdevice_reset(struct tasdevice_priv *tas_dev); 221 - int tascodec_init(struct tasdevice_priv *tas_priv, void *codec, 222 - struct module *module, 223 - void (*cont)(const struct firmware *fw, void *context)); 224 - struct tasdevice_priv *tasdevice_kzalloc(struct i2c_client *i2c); 225 - int tasdevice_init(struct tasdevice_priv *tas_priv); 226 - void tasdevice_remove(struct tasdevice_priv *tas_priv); 227 - int tasdevice_save_calibration(struct tasdevice_priv *tas_priv); 228 - void tasdevice_apply_calibration(struct tasdevice_priv *tas_priv); 229 - int tasdev_chn_switch(struct tasdevice_priv *tas_priv, 230 - unsigned short chn); 231 227 int tasdevice_dev_read(struct tasdevice_priv *tas_priv, 232 228 unsigned short chn, unsigned int reg, unsigned int *value); 229 + int tasdevice_dev_bulk_read(struct tasdevice_priv *tas_priv, 230 + unsigned short chn, unsigned int reg, unsigned char *p_data, 231 + unsigned int n_length); 233 232 int tasdevice_dev_write(struct tasdevice_priv *tas_priv, 234 233 unsigned short chn, unsigned int reg, unsigned int value); 235 234 int tasdevice_dev_bulk_write( 236 235 struct tasdevice_priv *tas_priv, unsigned short chn, 237 236 unsigned int reg, unsigned char *p_data, unsigned int n_length); 238 - int tasdevice_dev_bulk_read(struct tasdevice_priv *tas_priv, 239 - unsigned short chn, unsigned int reg, unsigned char *p_data, 240 - unsigned int n_length); 241 - int tasdevice_dev_update_bits( 242 - struct tasdevice_priv *tasdevice, unsigned short chn, 243 - unsigned int reg, unsigned int mask, unsigned int value); 244 - int tasdevice_amp_putvol(struct tasdevice_priv *tas_priv, 245 - struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc); 246 - int tasdevice_amp_getvol(struct tasdevice_priv *tas_priv, 247 - struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc); 248 - int tasdevice_digital_putvol(struct tasdevice_priv *tas_priv, 249 - struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc); 250 - int tasdevice_digital_getvol(struct tasdevice_priv *tas_priv, 251 - struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc); 252 - 237 + void tasdevice_remove(struct tasdevice_priv *tas_priv); 238 + int tasdevice_save_calibration(struct tasdevice_priv *tas_priv); 239 + void tasdevice_apply_calibration(struct tasdevice_priv *tas_priv); 253 240 #endif /* __TAS2781_H__ */
+3 -1
sound/pci/hda/Kconfig
··· 186 186 depends on ACPI 187 187 depends on EFI 188 188 depends on SND_SOC 189 - select SND_SOC_TAS2781_COMLIB 189 + select SND_SOC_TAS2781_COMLIB_I2C 190 190 select SND_SOC_TAS2781_FMWLIB 191 191 select CRC32 192 192 help ··· 202 202 depends on ACPI 203 203 depends on EFI 204 204 depends on SND_SOC 205 + select SND_SOC_TAS2781_FMWLIB 206 + select CRC8 205 207 select CRC32 206 208 help 207 209 Say Y or M here to include TAS2781 SPI HD-audio side codec support
+1 -1
sound/pci/hda/Makefile
··· 39 39 snd-hda-scodec-cs35l56-spi-y := cs35l56_hda_spi.o 40 40 snd-hda-scodec-component-y := hda_component.o 41 41 snd-hda-scodec-tas2781-i2c-y := tas2781_hda_i2c.o 42 - snd-hda-scodec-tas2781-spi-y := tas2781_hda_spi.o tas2781_spi_fwlib.o 42 + snd-hda-scodec-tas2781-spi-y := tas2781_hda_spi.o 43 43 44 44 # common driver 45 45 obj-$(CONFIG_SND_HDA) := snd-hda-codec.o
-157
sound/pci/hda/tas2781-spi.h
··· 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__ */
+3
sound/pci/hda/tas2781_hda.h
··· 9 9 10 10 #include <sound/asound.h> 11 11 12 + #define TASDEVICE_CALIBRATION_DATA_NAME L"CALI_DATA" 13 + #define TASDEV_CALIB_N 5 14 + 12 15 /* 13 16 * No standard control callbacks for SNDRV_CTL_ELEM_IFACE_CARD 14 17 * Define two controls, one is Volume control callbacks, the other is
+8 -18
sound/pci/hda/tas2781_hda_i2c.c
··· 22 22 #include <sound/hda_codec.h> 23 23 #include <sound/soc.h> 24 24 #include <sound/tas2781.h> 25 + #include <sound/tas2781-comlib-i2c.h> 25 26 #include <sound/tlv.h> 26 27 #include <sound/tas2781-tlv.h> 27 28 ··· 35 34 36 35 #define TASDEVICE_SPEAKER_CALIBRATION_SIZE 20 37 36 38 - enum calib_data { 39 - R0_VAL = 0, 40 - INV_R0, 41 - R0LOW, 42 - POWER, 43 - TLIM, 44 - CALIB_MAX 45 - }; 46 - 47 37 #define TAS2563_MAX_CHANNELS 4 48 38 49 39 #define TAS2563_CAL_POWER TASDEVICE_REG(0, 0x0d, 0x3c) ··· 42 50 #define TAS2563_CAL_INVR0 TASDEVICE_REG(0, 0x0f, 0x40) 43 51 #define TAS2563_CAL_R0_LOW TASDEVICE_REG(0, 0x0f, 0x48) 44 52 #define TAS2563_CAL_TLIM TASDEVICE_REG(0, 0x10, 0x14) 45 - #define TAS2563_CAL_N 5 46 53 #define TAS2563_CAL_DATA_SIZE 4 47 54 #define TAS2563_CAL_CH_SIZE 20 48 55 #define TAS2563_CAL_ARRAY_SIZE 80 49 56 50 - static unsigned int cal_regs[TAS2563_CAL_N] = { 57 + static unsigned int cal_regs[TASDEV_CALIB_N] = { 51 58 TAS2563_CAL_POWER, TAS2563_CAL_R0, TAS2563_CAL_INVR0, 52 59 TAS2563_CAL_R0_LOW, TAS2563_CAL_TLIM, 53 60 }; 54 - 55 61 56 62 struct tas2781_hda { 57 63 struct device *dev; ··· 465 475 int ret; 466 476 467 477 for (int i = 0; i < tas_priv->ndev; i++) { 468 - for (int j = 0; j < TAS2563_CAL_N; ++j) { 478 + for (int j = 0; j < TASDEV_CALIB_N; ++j) { 469 479 data = cpu_to_be32( 470 480 *(uint32_t *)&tas_priv->cali_data.data[offset]); 471 481 ret = tasdevice_dev_bulk_write(tas_priv, i, cal_regs[j], ··· 483 493 static efi_guid_t efi_guid = EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc, 484 494 0x09, 0x43, 0xa3, 0xf4, 0x31, 0x0a, 0x92); 485 495 486 - static efi_char16_t *efi_vars[TAS2563_MAX_CHANNELS][TAS2563_CAL_N] = { 496 + static efi_char16_t *efi_vars[TAS2563_MAX_CHANNELS][TASDEV_CALIB_N] = { 487 497 { L"Power_1", L"R0_1", L"InvR0_1", L"R0_Low_1", L"TLim_1" }, 488 498 { L"Power_2", L"R0_2", L"InvR0_2", L"R0_Low_2", L"TLim_2" }, 489 499 { L"Power_3", L"R0_3", L"InvR0_3", L"R0_Low_3", L"TLim_3" }, ··· 501 511 return -ENOMEM; 502 512 503 513 for (int i = 0; i < tas_priv->ndev; ++i) { 504 - for (int j = 0; j < TAS2563_CAL_N; ++j) { 514 + for (int j = 0; j < TASDEV_CALIB_N; ++j) { 505 515 status = efi.get_variable(efi_vars[i][j], 506 516 &efi_guid, &attr, &max_size, 507 517 &tas_priv->cali_data.data[offset]); ··· 525 535 { 526 536 struct calidata *cali_data = &tas_priv->cali_data; 527 537 struct cali_reg *r = &cali_data->cali_reg_array; 528 - unsigned int cali_reg[CALIB_MAX] = { 538 + unsigned int cali_reg[TASDEV_CALIB_N] = { 529 539 TASDEVICE_REG(0, 0x17, 0x74), 530 540 TASDEVICE_REG(0, 0x18, 0x0c), 531 541 TASDEVICE_REG(0, 0x18, 0x14), ··· 545 555 } 546 556 547 557 for (i = 0; i < tas_priv->ndev; i++) { 548 - for (j = 0; j < CALIB_MAX; j++) { 558 + for (j = 0; j < TASDEV_CALIB_N; j++) { 549 559 data = cpu_to_be32( 550 560 *(uint32_t *)&tas_priv->cali_data.data[oft]); 551 561 rc = tasdevice_dev_bulk_write(tas_priv, i, ··· 568 578 { 569 579 efi_guid_t efi_guid = EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d, 570 580 0x93, 0xfe, 0x5a, 0xa3, 0x5d, 0xb3); 571 - static efi_char16_t efi_name[] = L"CALI_DATA"; 581 + static efi_char16_t efi_name[] = TASDEVICE_CALIBRATION_DATA_NAME; 572 582 unsigned int attr, crc; 573 583 unsigned int *tmp_val; 574 584 efi_status_t status;
+273 -271
sound/pci/hda/tas2781_hda_spi.c
··· 27 27 28 28 #include <sound/hda_codec.h> 29 29 #include <sound/soc.h> 30 - #include <sound/tas2781-dsp.h> 30 + #include <sound/tas2781.h> 31 31 #include <sound/tlv.h> 32 32 #include <sound/tas2781-tlv.h> 33 - 34 - #include "tas2781-spi.h" 35 33 36 34 #include "hda_local.h" 37 35 #include "hda_auto_parser.h" ··· 37 39 #include "hda_jack.h" 38 40 #include "hda_generic.h" 39 41 #include "tas2781_hda.h" 42 + 43 + #define TASDEVICE_RANGE_MAX_SIZE (256 * 128) 44 + #define TASDEVICE_WIN_LEN 128 45 + #define TAS2781_SPI_MAX_FREQ (4 * HZ_PER_MHZ) 46 + /* Flag of calibration registers address. */ 47 + #define TASDEVICE_CALIBRATION_REG_ADDRESS BIT(7) 48 + #define TASDEV_UEFI_CALI_REG_ADDR_FLG BIT(7) 49 + 50 + /* System Reset Check Register */ 51 + #define TAS2781_REG_CLK_CONFIG TASDEVICE_REG(0x0, 0x0, 0x5c) 52 + #define TAS2781_REG_CLK_CONFIG_RESET 0x19 40 53 41 54 struct tas2781_hda { 42 55 struct tasdevice_priv *priv; ··· 61 52 static const struct regmap_range_cfg tasdevice_ranges[] = { 62 53 { 63 54 .range_min = 0, 64 - .range_max = TASDEVICE_MAX_SIZE, 55 + .range_max = TASDEVICE_RANGE_MAX_SIZE, 65 56 .selector_reg = TASDEVICE_PAGE_SELECT, 66 57 .selector_mask = GENMASK(7, 0), 67 58 .selector_shift = 0, 68 59 .window_start = 0, 69 - .window_len = TASDEVICE_MAX_PAGE, 60 + .window_len = TASDEVICE_WIN_LEN, 70 61 }, 71 62 }; 72 63 ··· 74 65 .reg_bits = 8, 75 66 .val_bits = 8, 76 67 .zero_flag_mask = true, 68 + .read_flag_mask = 0x01, 69 + .reg_shift = -1, 77 70 .cache_type = REGCACHE_NONE, 78 71 .ranges = tasdevice_ranges, 79 72 .num_ranges = ARRAY_SIZE(tasdevice_ranges), 80 - .max_register = TASDEVICE_MAX_SIZE, 73 + .max_register = TASDEVICE_RANGE_MAX_SIZE, 81 74 }; 82 75 83 - static int tasdevice_spi_switch_book(struct tasdevice_priv *tas_priv, int reg) 76 + static int tasdevice_spi_dev_read(struct tasdevice_priv *tas_priv, 77 + unsigned short chn, unsigned int reg, unsigned int *val) 84 78 { 85 - struct regmap *map = tas_priv->regmap; 86 - 87 - if (tas_priv->cur_book != TASDEVICE_BOOK_ID(reg)) { 88 - int ret = regmap_write(map, TASDEVICE_BOOKCTL_REG, 89 - TASDEVICE_BOOK_ID(reg)); 90 - if (ret < 0) { 91 - dev_err(tas_priv->dev, "Switch Book E=%d\n", ret); 92 - return ret; 93 - } 94 - tas_priv->cur_book = TASDEVICE_BOOK_ID(reg); 95 - } 96 - return 0; 97 - } 98 - 99 - int tasdevice_spi_dev_read(struct tasdevice_priv *tas_priv, 100 - unsigned int reg, 101 - unsigned int *val) 102 - { 103 - struct regmap *map = tas_priv->regmap; 104 79 int ret; 105 - 106 - ret = tasdevice_spi_switch_book(tas_priv, reg); 107 - if (ret < 0) 108 - return ret; 109 80 110 81 /* 111 82 * In our TAS2781 SPI mode, if read from other book (not book 0), ··· 95 106 if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) { 96 107 unsigned char data[2]; 97 108 98 - ret = regmap_bulk_read(map, TASDEVICE_PAGE_REG(reg) | 1, 109 + ret = tasdevice_dev_bulk_read(tas_priv, chn, reg, 99 110 data, sizeof(data)); 100 111 *val = data[1]; 101 112 } else { 102 - ret = regmap_read(map, TASDEVICE_PAGE_REG(reg) | 1, val); 113 + ret = tasdevice_dev_read(tas_priv, chn, reg, val); 103 114 } 104 115 if (ret < 0) 105 116 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); ··· 107 118 return ret; 108 119 } 109 120 110 - int tasdevice_spi_dev_write(struct tasdevice_priv *tas_priv, 111 - unsigned int reg, 112 - unsigned int value) 121 + static int tasdevice_spi_dev_bulk_read(struct tasdevice_priv *tas_priv, 122 + unsigned short chn, unsigned int reg, unsigned char *data, 123 + unsigned int len) 113 124 { 114 - struct regmap *map = tas_priv->regmap; 115 125 int ret; 116 126 117 - ret = tasdevice_spi_switch_book(tas_priv, reg); 118 - if (ret < 0) 119 - return ret; 120 - 121 - ret = regmap_write(map, TASDEVICE_PAGE_REG(reg), value); 122 - if (ret < 0) 123 - dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 124 - 125 - return ret; 126 - } 127 - 128 - int tasdevice_spi_dev_bulk_write(struct tasdevice_priv *tas_priv, 129 - unsigned int reg, 130 - unsigned char *data, 131 - unsigned int len) 132 - { 133 - struct regmap *map = tas_priv->regmap; 134 - int ret; 135 - 136 - ret = tasdevice_spi_switch_book(tas_priv, reg); 137 - if (ret < 0) 138 - return ret; 139 - 140 - ret = regmap_bulk_write(map, TASDEVICE_PAGE_REG(reg), data, len); 141 - if (ret < 0) 142 - dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 143 - 144 - return ret; 145 - } 146 - 147 - int tasdevice_spi_dev_bulk_read(struct tasdevice_priv *tas_priv, 148 - unsigned int reg, 149 - unsigned char *data, 150 - unsigned int len) 151 - { 152 - struct regmap *map = tas_priv->regmap; 153 - int ret; 154 - 155 - ret = tasdevice_spi_switch_book(tas_priv, reg); 156 - if (ret < 0) 157 - return ret; 158 - 159 - if (len > TASDEVICE_MAX_PAGE) 160 - len = TASDEVICE_MAX_PAGE; 161 127 /* 162 128 * In our TAS2781 SPI mode, if read from other book (not book 0), 163 129 * or read from page number larger than 1 in book 0, one more byte 164 130 * read is needed, and first byte is a dummy byte, need to be ignored. 165 131 */ 166 132 if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) { 167 - unsigned char buf[TASDEVICE_MAX_PAGE+1]; 133 + unsigned char buf[TASDEVICE_WIN_LEN + 1]; 168 134 169 - ret = regmap_bulk_read(map, TASDEVICE_PAGE_REG(reg) | 1, buf, 170 - len + 1); 135 + ret = tasdevice_dev_bulk_read(tas_priv, chn, reg, 136 + buf, len + 1); 171 137 memcpy(data, buf + 1, len); 172 138 } else { 173 - ret = regmap_bulk_read(map, TASDEVICE_PAGE_REG(reg) | 1, data, 174 - len); 139 + ret = tasdevice_dev_bulk_read(tas_priv, chn, reg, data, len); 175 140 } 176 141 if (ret < 0) 177 142 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); ··· 133 190 return ret; 134 191 } 135 192 136 - int tasdevice_spi_dev_update_bits(struct tasdevice_priv *tas_priv, 137 - unsigned int reg, 138 - unsigned int mask, 139 - unsigned int value) 193 + static int tasdevice_spi_dev_update_bits(struct tasdevice_priv *tas_priv, 194 + unsigned short chn, unsigned int reg, unsigned int mask, 195 + unsigned int value) 140 196 { 141 - struct regmap *map = tas_priv->regmap; 142 197 int ret, val; 143 198 144 199 /* 145 200 * In our TAS2781 SPI mode, read/write was masked in last bit of 146 201 * address, it cause regmap_update_bits() not work as expected. 147 202 */ 148 - ret = tasdevice_spi_dev_read(tas_priv, reg, &val); 203 + ret = tasdevice_dev_read(tas_priv, chn, reg, &val); 149 204 if (ret < 0) { 150 205 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 151 206 return ret; 152 207 } 153 - ret = regmap_write(map, TASDEVICE_PAGE_REG(reg), 154 - (val & ~mask) | (mask & value)); 208 + 209 + ret = tasdevice_dev_write(tas_priv, chn, TASDEVICE_PAGE_REG(reg), 210 + (val & ~mask) | (mask & value)); 155 211 if (ret < 0) 156 212 dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 213 + 214 + return ret; 215 + } 216 + 217 + static int tasdevice_spi_change_chn_book(struct tasdevice_priv *p, 218 + unsigned short chn, int book) 219 + { 220 + int ret = 0; 221 + 222 + if (chn == p->index) { 223 + struct tasdevice *tasdev = &p->tasdevice[chn]; 224 + struct regmap *map = p->regmap; 225 + 226 + if (tasdev->cur_book != book) { 227 + ret = regmap_write(map, TASDEVICE_BOOKCTL_REG, book); 228 + if (ret < 0) 229 + dev_err(p->dev, "%s, E=%d\n", __func__, ret); 230 + else 231 + tasdev->cur_book = book; 232 + } 233 + } else { 234 + ret = -EXDEV; 235 + dev_dbg(p->dev, "Not error, %s ignore channel(%d)\n", 236 + __func__, chn); 237 + } 157 238 158 239 return ret; 159 240 } ··· 190 223 gpiod_set_value_cansleep(tas_dev->reset, 0); 191 224 fsleep(800); 192 225 gpiod_set_value_cansleep(tas_dev->reset, 1); 226 + } else { 227 + ret = tasdevice_dev_write(tas_dev, tas_dev->index, 228 + TASDEVICE_REG_SWRESET, TASDEVICE_REG_SWRESET_RESET); 229 + if (ret < 0) 230 + dev_err(tas_dev->dev, "dev sw-reset fail, %d\n", ret); 231 + fsleep(1000); 193 232 } 194 - ret = tasdevice_spi_dev_write(tas_dev, TAS2781_REG_SWRESET, 195 - TAS2781_REG_SWRESET_RESET); 196 - if (ret < 0) 197 - dev_err(tas_dev->dev, "dev sw-reset fail, %d\n", ret); 198 - fsleep(1000); 199 233 } 200 234 201 235 static int tascodec_spi_init(struct tasdevice_priv *tas_priv, ··· 213 245 214 246 scnprintf(tas_priv->rca_binaryname, 215 247 sizeof(tas_priv->rca_binaryname), "%sRCA%d.bin", 216 - tas_priv->dev_name, tas_priv->index); 248 + tas_priv->dev_name, tas_priv->ndev); 217 249 crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL); 218 250 tas_priv->codec = codec; 219 251 ret = request_firmware_nowait(module, FW_ACTION_UEVENT, ··· 228 260 229 261 static void tasdevice_spi_init(struct tasdevice_priv *tas_priv) 230 262 { 231 - tas_priv->cur_prog = -1; 232 - tas_priv->cur_conf = -1; 263 + tas_priv->tasdevice[tas_priv->index].cur_book = -1; 264 + tas_priv->tasdevice[tas_priv->index].cur_conf = -1; 265 + tas_priv->tasdevice[tas_priv->index].cur_prog = -1; 233 266 234 - tas_priv->cur_book = -1; 235 - tas_priv->cur_prog = -1; 236 - tas_priv->cur_conf = -1; 267 + tas_priv->isspi = true; 237 268 238 - /* Store default registers address for calibration data. */ 239 - tas_priv->cali_reg_array[0] = TASDEVICE_REG(0, 0x17, 0x74); 240 - tas_priv->cali_reg_array[1] = TASDEVICE_REG(0, 0x18, 0x0c); 241 - tas_priv->cali_reg_array[2] = TASDEVICE_REG(0, 0x18, 0x14); 242 - tas_priv->cali_reg_array[3] = TASDEVICE_REG(0, 0x13, 0x70); 243 - tas_priv->cali_reg_array[4] = TASDEVICE_REG(0, 0x18, 0x7c); 269 + tas_priv->update_bits = tasdevice_spi_dev_update_bits; 270 + tas_priv->change_chn_book = tasdevice_spi_change_chn_book; 271 + tas_priv->dev_read = tasdevice_spi_dev_read; 272 + tas_priv->dev_bulk_read = tasdevice_spi_dev_bulk_read; 244 273 245 274 mutex_init(&tas_priv->codec_lock); 246 275 } 247 276 248 277 static int tasdevice_spi_amp_putvol(struct tasdevice_priv *tas_priv, 249 - struct snd_ctl_elem_value *ucontrol, 250 - struct soc_mixer_control *mc) 278 + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 251 279 { 252 280 unsigned int invert = mc->invert; 253 281 unsigned char mask; ··· 254 290 mask <<= mc->shift; 255 291 val = clamp(invert ? max - ucontrol->value.integer.value[0] : 256 292 ucontrol->value.integer.value[0], 0, max); 257 - ret = tasdevice_spi_dev_update_bits(tas_priv, 293 + 294 + ret = tasdevice_spi_dev_update_bits(tas_priv, tas_priv->index, 258 295 mc->reg, mask, (unsigned int)(val << mc->shift)); 259 296 if (ret) 260 297 dev_err(tas_priv->dev, "set AMP vol error in dev %d\n", ··· 265 300 } 266 301 267 302 static int tasdevice_spi_amp_getvol(struct tasdevice_priv *tas_priv, 268 - struct snd_ctl_elem_value *ucontrol, 269 - struct soc_mixer_control *mc) 303 + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 270 304 { 271 305 unsigned int invert = mc->invert; 272 306 unsigned char mask = 0; 273 307 int max = mc->max; 274 308 int ret, val; 275 309 276 - /* Read the primary device */ 277 - ret = tasdevice_spi_dev_read(tas_priv, mc->reg, &val); 310 + ret = tasdevice_spi_dev_read(tas_priv, tas_priv->index, mc->reg, &val); 278 311 if (ret) { 279 312 dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__); 280 313 return ret; ··· 287 324 return ret; 288 325 } 289 326 290 - static int tasdevice_spi_digital_putvol(struct tasdevice_priv *tas_priv, 291 - struct snd_ctl_elem_value *ucontrol, 292 - struct soc_mixer_control *mc) 327 + static int tasdevice_spi_digital_putvol(struct tasdevice_priv *p, 328 + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 293 329 { 294 330 unsigned int invert = mc->invert; 295 331 int max = mc->max; ··· 296 334 297 335 val = clamp(invert ? max - ucontrol->value.integer.value[0] : 298 336 ucontrol->value.integer.value[0], 0, max); 299 - ret = tasdevice_spi_dev_write(tas_priv, mc->reg, (unsigned int)val); 337 + ret = tasdevice_dev_write(p, p->index, mc->reg, (unsigned int)val); 300 338 if (ret) 301 - dev_err(tas_priv->dev, "set digital vol err in dev %d\n", 302 - tas_priv->index); 339 + dev_err(p->dev, "set digital vol err in dev %d\n", p->index); 303 340 304 341 return ret; 305 342 } 306 343 307 - static int tasdevice_spi_digital_getvol(struct tasdevice_priv *tas_priv, 308 - struct snd_ctl_elem_value *ucontrol, 309 - struct soc_mixer_control *mc) 344 + static int tasdevice_spi_digital_getvol(struct tasdevice_priv *p, 345 + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 310 346 { 311 347 unsigned int invert = mc->invert; 312 348 int max = mc->max; 313 349 int ret, val; 314 350 315 - /* Read the primary device as the whole */ 316 - ret = tasdevice_spi_dev_read(tas_priv, mc->reg, &val); 351 + ret = tasdevice_spi_dev_read(p, p->index, mc->reg, &val); 317 352 if (ret) { 318 - dev_err(tas_priv->dev, "%s, get digital vol err\n", __func__); 353 + dev_err(p->dev, "%s, get digital vol err\n", __func__); 319 354 return ret; 320 355 } 321 356 ··· 351 392 ret = -EINVAL; 352 393 goto err; 353 394 } 354 - nval = ret; 395 + p->ndev = nval = ret; 355 396 356 397 ret = device_property_read_u32_array(physdev, property, values, nval); 357 398 if (ret) ··· 394 435 static void tas2781_hda_playback_hook(struct device *dev, int action) 395 436 { 396 437 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 438 + struct tasdevice_priv *tas_priv = tas_hda->priv; 397 439 398 440 if (action == HDA_GEN_PCM_ACT_OPEN) { 399 441 pm_runtime_get_sync(dev); 400 - guard(mutex)(&tas_hda->priv->codec_lock); 401 - tasdevice_spi_tuning_switch(tas_hda->priv, 0); 442 + guard(mutex)(&tas_priv->codec_lock); 443 + if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK) 444 + tasdevice_tuning_switch(tas_hda->priv, 0); 402 445 } else if (action == HDA_GEN_PCM_ACT_CLOSE) { 403 - guard(mutex)(&tas_hda->priv->codec_lock); 404 - tasdevice_spi_tuning_switch(tas_hda->priv, 1); 446 + guard(mutex)(&tas_priv->codec_lock); 447 + if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK) 448 + tasdevice_tuning_switch(tas_priv, 1); 405 449 pm_runtime_mark_last_busy(dev); 406 450 pm_runtime_put_autosuspend(dev); 407 451 } ··· 551 589 struct soc_mixer_control *mc = 552 590 (struct soc_mixer_control *)kcontrol->private_value; 553 591 592 + guard(mutex)(&tas_priv->codec_lock); 554 593 return tasdevice_spi_digital_getvol(tas_priv, ucontrol, mc); 555 594 } 556 595 ··· 562 599 struct soc_mixer_control *mc = 563 600 (struct soc_mixer_control *)kcontrol->private_value; 564 601 602 + guard(mutex)(&tas_priv->codec_lock); 565 603 return tasdevice_spi_amp_getvol(tas_priv, ucontrol, mc); 566 604 } 567 605 ··· 573 609 struct soc_mixer_control *mc = 574 610 (struct soc_mixer_control *)kcontrol->private_value; 575 611 576 - /* The check of the given value is in tasdevice_digital_putvol. */ 612 + guard(mutex)(&tas_priv->codec_lock); 577 613 return tasdevice_spi_digital_putvol(tas_priv, ucontrol, mc); 578 614 } 579 615 ··· 584 620 struct soc_mixer_control *mc = 585 621 (struct soc_mixer_control *)kcontrol->private_value; 586 622 587 - /* The check of the given value is in tasdevice_amp_putvol. */ 623 + guard(mutex)(&tas_priv->codec_lock); 588 624 return tasdevice_spi_amp_putvol(tas_priv, ucontrol, mc); 589 625 } 590 626 ··· 689 725 690 726 static void tas2781_apply_calib(struct tasdevice_priv *tas_priv) 691 727 { 692 - int i, rc; 728 + struct calidata *cali_data = &tas_priv->cali_data; 729 + struct cali_reg *r = &cali_data->cali_reg_array; 730 + unsigned char *data = cali_data->data; 731 + unsigned int *tmp_val = (unsigned int *)data; 732 + unsigned int cali_reg[TASDEV_CALIB_N] = { 733 + TASDEVICE_REG(0, 0x17, 0x74), 734 + TASDEVICE_REG(0, 0x18, 0x0c), 735 + TASDEVICE_REG(0, 0x18, 0x14), 736 + TASDEVICE_REG(0, 0x13, 0x70), 737 + TASDEVICE_REG(0, 0x18, 0x7c), 738 + }; 739 + unsigned int crc, oft; 740 + unsigned char *buf; 741 + int i, j, k, l; 693 742 694 - /* 695 - * If no calibration data exist in tasdevice_priv *tas_priv, 696 - * calibration apply will be ignored, and use default values 697 - * in firmware binary, which was loaded during firmware download. 698 - */ 699 - if (tas_priv->cali_data[0] == 0) 700 - return; 701 - /* 702 - * Calibration data was saved in tasdevice_priv *tas_priv as: 703 - * unsigned int cali_data[CALIB_MAX]; 704 - * and every data (in 4 bytes) will be saved in register which in 705 - * book 0, and page number in page_array[], offset was saved in 706 - * rgno_array[]. 707 - */ 708 - for (i = 0; i < CALIB_MAX; i++) { 709 - rc = tasdevice_spi_dev_bulk_write(tas_priv, 710 - tas_priv->cali_reg_array[i], 711 - (unsigned char *)&tas_priv->cali_data[i], 4); 712 - if (rc < 0) 713 - dev_err(tas_priv->dev, 714 - "chn %d calib %d bulk_wr err = %d\n", 715 - tas_priv->index, i, rc); 743 + if (tmp_val[0] == 2781) { 744 + /* 745 + * New features were added in calibrated Data V3: 746 + * 1. Added calibration registers address define in 747 + * a node, marked as Device id == 0x80. 748 + * New features were added in calibrated Data V2: 749 + * 1. Added some the fields to store the link_id and 750 + * uniqie_id for multi-link solutions 751 + * 2. Support flexible number of devices instead of 752 + * fixed one in V1. 753 + * Layout of calibrated data V2 in UEFI(total 256 bytes): 754 + * ChipID (2781, 4 bytes) 755 + * Data-Group-Sum (4 bytes) 756 + * TimeStamp of Calibration (4 bytes) 757 + * for (i = 0; i < Data-Group-Sum; i++) { 758 + * if (Data type != 0x80) (4 bytes) 759 + * Calibrated Data of Device #i (20 bytes) 760 + * else 761 + * Calibration registers address (5*4 = 20 bytes) 762 + * # V2: No reg addr in data grp section. 763 + * # V3: Normally the last grp is the reg addr. 764 + * } 765 + * CRC (4 bytes) 766 + * Reserved (the rest) 767 + */ 768 + crc = crc32(~0, data, (3 + tmp_val[1] * 6) * 4) ^ ~0; 769 + 770 + if (crc != tmp_val[3 + tmp_val[1] * 6]) { 771 + cali_data->total_sz = 0; 772 + dev_err(tas_priv->dev, "%s: CRC error\n", __func__); 773 + return; 774 + } 775 + 776 + for (j = 0, k = 0; j < tmp_val[1]; j++) { 777 + oft = j * 6 + 3; 778 + if (tmp_val[oft] == TASDEV_UEFI_CALI_REG_ADDR_FLG) { 779 + for (i = 0; i < TASDEV_CALIB_N; i++) { 780 + buf = &data[(oft + i + 1) * 4]; 781 + cali_reg[i] = TASDEVICE_REG(buf[1], 782 + buf[2], buf[3]); 783 + } 784 + } else { 785 + l = j * (cali_data->cali_dat_sz_per_dev + 1); 786 + if (k >= tas_priv->ndev || l > oft * 4) { 787 + dev_err(tas_priv->dev, 788 + "%s: dev sum error\n", 789 + __func__); 790 + cali_data->total_sz = 0; 791 + return; 792 + } 793 + 794 + data[l] = k; 795 + for (i = 0; i < TASDEV_CALIB_N * 4; i++) 796 + data[l + i] = data[4 * oft + i]; 797 + k++; 798 + } 799 + } 800 + } else { 801 + /* 802 + * Calibration data is in V1 format. 803 + * struct cali_data { 804 + * char cali_data[20]; 805 + * } 806 + * 807 + * struct { 808 + * struct cali_data cali_data[4]; 809 + * int TimeStamp of Calibration (4 bytes) 810 + * int CRC (4 bytes) 811 + * } ueft; 812 + */ 813 + crc = crc32(~0, data, 84) ^ ~0; 814 + if (crc != tmp_val[21]) { 815 + cali_data->total_sz = 0; 816 + dev_err(tas_priv->dev, "%s: V1 CRC error\n", __func__); 817 + return; 818 + } 819 + 820 + for (j = tas_priv->ndev - 1; j >= 0; j--) { 821 + l = j * (cali_data->cali_dat_sz_per_dev + 1); 822 + for (i = TASDEV_CALIB_N * 4; i > 0 ; i--) 823 + data[l + i] = data[tas_priv->index * 5 + i]; 824 + data[l+i] = j; 825 + } 716 826 } 827 + 828 + if (tas_priv->dspbin_typ == TASDEV_BASIC) { 829 + r->r0_reg = cali_reg[0]; 830 + r->invr0_reg = cali_reg[1]; 831 + r->r0_low_reg = cali_reg[2]; 832 + r->pow_reg = cali_reg[3]; 833 + r->tlimit_reg = cali_reg[4]; 834 + } 835 + 836 + tas_priv->is_user_space_calidata = true; 837 + cali_data->total_sz = 838 + tas_priv->ndev * (cali_data->cali_dat_sz_per_dev + 1); 717 839 } 718 840 719 841 /* ··· 819 769 EFI_GUID(0x02f9af02, 0x7734, 0x4233, 820 770 0xb4, 0x3d, 0x93, 0xfe, 0x5a, 0xa3, 0x5d, 0xb3); 821 771 static efi_char16_t efi_name[] = TASDEVICE_CALIBRATION_DATA_NAME; 822 - unsigned char data[TASDEVICE_CALIBRATION_DATA_SIZE], *buf; 823 - unsigned int attr, crc, offset, *tmp_val; 772 + struct calidata *cali_data = &tas_priv->cali_data; 824 773 unsigned long total_sz = 0; 774 + unsigned int attr, size; 825 775 efi_status_t status; 776 + unsigned char *data; 826 777 827 - tas_priv->cali_data[0] = 0; 828 - status = efi.get_variable(efi_name, &efi_guid, &attr, &total_sz, data); 778 + cali_data->cali_dat_sz_per_dev = 20; 779 + size = tas_priv->ndev * (cali_data->cali_dat_sz_per_dev + 1); 780 + status = efi.get_variable(efi_name, &efi_guid, &attr, &total_sz, NULL); 781 + cali_data->total_sz = total_sz > size ? total_sz : size; 829 782 if (status == EFI_BUFFER_TOO_SMALL) { 830 - if (total_sz > TASDEVICE_CALIBRATION_DATA_SIZE) 783 + /* Allocate data buffer of data_size bytes */ 784 + data = tas_priv->cali_data.data = devm_kzalloc(tas_priv->dev, 785 + tas_priv->cali_data.total_sz, GFP_KERNEL); 786 + if (!data) { 787 + tas_priv->cali_data.total_sz = 0; 831 788 return -ENOMEM; 789 + } 832 790 /* Get variable contents into buffer */ 833 791 status = efi.get_variable(efi_name, &efi_guid, &attr, 834 - &total_sz, data); 792 + &tas_priv->cali_data.total_sz, data); 835 793 } 836 - if (status != EFI_SUCCESS) 794 + if (status != EFI_SUCCESS) { 795 + tas_priv->cali_data.total_sz = 0; 837 796 return status; 838 - 839 - tmp_val = (unsigned int *)data; 840 - if (tmp_val[0] == 2781) { 841 - /* 842 - * New features were added in calibrated Data V3: 843 - * 1. Added calibration registers address define in 844 - * a node, marked as Device id == 0x80. 845 - * New features were added in calibrated Data V2: 846 - * 1. Added some the fields to store the link_id and 847 - * uniqie_id for multi-link solutions 848 - * 2. Support flexible number of devices instead of 849 - * fixed one in V1. 850 - * Layout of calibrated data V2 in UEFI(total 256 bytes): 851 - * ChipID (2781, 4 bytes) 852 - * Device-Sum (4 bytes) 853 - * TimeStamp of Calibration (4 bytes) 854 - * for (i = 0; i < Device-Sum; i++) { 855 - * Device #i index_info () { 856 - * SDW link id (2bytes) 857 - * SDW unique_id (2bytes) 858 - * } // if Device number is 0x80, mean it's 859 - * calibration registers address. 860 - * Calibrated Data of Device #i (20 bytes) 861 - * } 862 - * CRC (4 bytes) 863 - * Reserved (the rest) 864 - */ 865 - crc = crc32(~0, data, (3 + tmp_val[1] * 6) * 4) ^ ~0; 866 - 867 - if (crc != tmp_val[3 + tmp_val[1] * 6]) 868 - return 0; 869 - 870 - for (int j = 0; j < tmp_val[1]; j++) { 871 - offset = j * 6 + 3; 872 - if (tmp_val[offset] == tas_priv->index) { 873 - for (int i = 0; i < CALIB_MAX; i++) 874 - tas_priv->cali_data[i] = 875 - tmp_val[offset + i + 1]; 876 - } else if (tmp_val[offset] == 877 - TASDEVICE_CALIBRATION_REG_ADDRESS) { 878 - for (int i = 0; i < CALIB_MAX; i++) { 879 - buf = &data[(offset + i + 1) * 4]; 880 - tas_priv->cali_reg_array[i] = 881 - TASDEVICE_REG(buf[1], buf[2], 882 - buf[3]); 883 - } 884 - } 885 - tas_priv->apply_calibration(tas_priv); 886 - } 887 - } else { 888 - /* 889 - * Calibration data is in V1 format. 890 - * struct cali_data { 891 - * char cali_data[20]; 892 - * } 893 - * 894 - * struct { 895 - * struct cali_data cali_data[4]; 896 - * int TimeStamp of Calibration (4 bytes) 897 - * int CRC (4 bytes) 898 - * } ueft; 899 - */ 900 - crc = crc32(~0, data, 84) ^ ~0; 901 - if (crc == tmp_val[21]) { 902 - for (int i = 0; i < CALIB_MAX; i++) 903 - tas_priv->cali_data[i] = 904 - tmp_val[tas_priv->index * 5 + i]; 905 - tas_priv->apply_calibration(tas_priv); 906 - } 907 797 } 908 798 799 + tas_priv->apply_calibration(tas_priv); 909 800 return 0; 910 801 } 911 802 ··· 874 883 pm_runtime_get_sync(tas_priv->dev); 875 884 guard(mutex)(&tas_priv->codec_lock); 876 885 877 - ret = tasdevice_spi_rca_parser(tas_priv, fmw); 886 + ret = tasdevice_rca_parser(tas_priv, fmw); 878 887 if (ret) 879 888 goto out; 880 889 ··· 901 910 } 902 911 } 903 912 904 - tasdevice_spi_dsp_remove(tas_priv); 913 + tasdevice_dsp_remove(tas_priv); 905 914 906 915 tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING; 907 916 scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%08X-%01d.bin", 908 917 codec->core.subsystem_id, tas_priv->index); 909 - ret = tasdevice_spi_dsp_parser(tas_priv); 918 + ret = tasdevice_dsp_parser(tas_priv); 910 919 if (ret) { 911 920 dev_err(tas_priv->dev, "dspfw load %s error\n", 912 921 tas_priv->coef_binaryname); ··· 936 945 goto out; 937 946 } 938 947 939 - /* Perform AMP reset before firmware download. */ 940 - tas_priv->rcabin.profile_cfg_id = TAS2781_PRE_POST_RESET_CFG; 941 948 tas2781_spi_reset(tas_priv); 942 949 tas_priv->rcabin.profile_cfg_id = 0; 943 950 944 951 tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK; 945 - ret = tasdevice_spi_dev_read(tas_priv, TAS2781_REG_CLK_CONFIG, &val); 952 + ret = tas_priv->dev_read(tas_priv, tas_priv->index, 953 + TAS2781_REG_CLK_CONFIG, &val); 946 954 if (ret < 0) 947 955 goto out; 948 956 949 - if (val == TAS2781_REG_CLK_CONFIG_RESET) 950 - ret = tasdevice_spi_prmg_load(tas_priv, 0); 951 - if (ret < 0) { 952 - dev_err(tas_priv->dev, "FW download failed = %d\n", ret); 953 - goto out; 957 + if (val == TAS2781_REG_CLK_CONFIG_RESET) { 958 + ret = tasdevice_prmg_load(tas_priv, 0); 959 + if (ret < 0) { 960 + dev_err(tas_priv->dev, "FW download failed = %d\n", 961 + ret); 962 + goto out; 963 + } 954 964 } 955 965 if (tas_priv->fmw->nr_programs > 0) 956 - tas_priv->cur_prog = 0; 966 + tas_priv->tasdevice[tas_priv->index].cur_prog = 0; 957 967 if (tas_priv->fmw->nr_configurations > 0) 958 - tas_priv->cur_conf = 0; 968 + tas_priv->tasdevice[tas_priv->index].cur_conf = 0; 959 969 960 970 /* 961 971 * If calibrated data occurs error, dsp will still works with default ··· 1009 1017 { 1010 1018 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 1011 1019 struct hda_component_parent *parent = master_data; 1020 + struct tasdevice_priv *tas_priv = tas_hda->priv; 1012 1021 struct hda_component *comp; 1013 1022 1014 - comp = hda_component_from_index(parent, tas_hda->priv->index); 1023 + comp = hda_component_from_index(parent, tas_priv->index); 1015 1024 if (comp && (comp->dev == dev)) { 1016 1025 comp->dev = NULL; 1017 1026 memset(comp->name, 0, sizeof(comp->name)); ··· 1021 1028 1022 1029 tas2781_hda_remove_controls(tas_hda); 1023 1030 1024 - tasdevice_spi_config_info_remove(tas_hda->priv); 1025 - tasdevice_spi_dsp_remove(tas_hda->priv); 1031 + tasdevice_config_info_remove(tas_priv); 1032 + tasdevice_dsp_remove(tas_priv); 1026 1033 1027 1034 tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING; 1028 1035 } ··· 1117 1124 static int tas2781_runtime_suspend(struct device *dev) 1118 1125 { 1119 1126 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 1127 + struct tasdevice_priv *tas_priv = tas_hda->priv; 1120 1128 1121 - guard(mutex)(&tas_hda->priv->codec_lock); 1129 + guard(mutex)(&tas_priv->codec_lock); 1122 1130 1123 - if (tas_hda->priv->playback_started) 1124 - tasdevice_spi_tuning_switch(tas_hda->priv, 1); 1131 + if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK 1132 + && tas_priv->playback_started) 1133 + tasdevice_tuning_switch(tas_priv, 1); 1125 1134 1126 - tas_hda->priv->cur_book = -1; 1127 - tas_hda->priv->cur_conf = -1; 1135 + tas_priv->tasdevice[tas_priv->index].cur_book = -1; 1136 + tas_priv->tasdevice[tas_priv->index].cur_conf = -1; 1128 1137 1129 1138 return 0; 1130 1139 } ··· 1134 1139 static int tas2781_runtime_resume(struct device *dev) 1135 1140 { 1136 1141 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 1142 + struct tasdevice_priv *tas_priv = tas_hda->priv; 1137 1143 1138 - guard(mutex)(&tas_hda->priv->codec_lock); 1144 + guard(mutex)(&tas_priv->codec_lock); 1139 1145 1140 - if (tas_hda->priv->playback_started) 1141 - tasdevice_spi_tuning_switch(tas_hda->priv, 0); 1146 + if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK 1147 + && tas_priv->playback_started) 1148 + tasdevice_tuning_switch(tas_priv, 0); 1142 1149 1143 1150 return 0; 1144 1151 } ··· 1148 1151 static int tas2781_system_suspend(struct device *dev) 1149 1152 { 1150 1153 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 1154 + struct tasdevice_priv *tas_priv = tas_hda->priv; 1151 1155 int ret; 1152 1156 1153 1157 ret = pm_runtime_force_suspend(dev); ··· 1156 1158 return ret; 1157 1159 1158 1160 /* Shutdown chip before system suspend */ 1159 - if (tas_hda->priv->playback_started) 1160 - tasdevice_spi_tuning_switch(tas_hda->priv, 1); 1161 + if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK 1162 + && tas_priv->playback_started) 1163 + tasdevice_tuning_switch(tas_priv, 1); 1161 1164 1162 1165 return 0; 1163 1166 } ··· 1166 1167 static int tas2781_system_resume(struct device *dev) 1167 1168 { 1168 1169 struct tas2781_hda *tas_hda = dev_get_drvdata(dev); 1170 + struct tasdevice_priv *tas_priv = tas_hda->priv; 1169 1171 int ret, val; 1170 1172 1171 1173 ret = pm_runtime_force_resume(dev); 1172 1174 if (ret) 1173 1175 return ret; 1174 1176 1175 - guard(mutex)(&tas_hda->priv->codec_lock); 1176 - ret = tasdevice_spi_dev_read(tas_hda->priv, TAS2781_REG_CLK_CONFIG, 1177 - &val); 1177 + guard(mutex)(&tas_priv->codec_lock); 1178 + ret = tas_priv->dev_read(tas_priv, tas_priv->index, 1179 + TAS2781_REG_CLK_CONFIG, &val); 1178 1180 if (ret < 0) 1179 1181 return ret; 1180 1182 1181 1183 if (val == TAS2781_REG_CLK_CONFIG_RESET) { 1182 - tas_hda->priv->cur_book = -1; 1183 - tas_hda->priv->cur_conf = -1; 1184 - tas_hda->priv->cur_prog = -1; 1184 + tas_priv->tasdevice[tas_priv->index].cur_book = -1; 1185 + tas_priv->tasdevice[tas_priv->index].cur_conf = -1; 1186 + tas_priv->tasdevice[tas_priv->index].cur_prog = -1; 1185 1187 1186 - ret = tasdevice_spi_prmg_load(tas_hda->priv, 0); 1188 + ret = tasdevice_prmg_load(tas_priv, 0); 1187 1189 if (ret < 0) { 1188 - dev_err(tas_hda->priv->dev, 1190 + dev_err(tas_priv->dev, 1189 1191 "FW download failed = %d\n", ret); 1190 1192 return ret; 1191 1193 } 1194 + tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK; 1192 1195 1193 - if (tas_hda->priv->playback_started) 1194 - tasdevice_spi_tuning_switch(tas_hda->priv, 0); 1196 + if (tas_priv->playback_started) 1197 + tasdevice_tuning_switch(tas_priv, 0); 1195 1198 } 1196 1199 1197 1200 return ret; ··· 1230 1229 MODULE_DESCRIPTION("TAS2781 HDA SPI Driver"); 1231 1230 MODULE_AUTHOR("Baojun, Xu, <baojun.xug@ti.com>"); 1232 1231 MODULE_LICENSE("GPL"); 1232 + MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");
-2006
sound/pci/hda/tas2781_spi_fwlib.c
··· 1 - // SPDX-License-Identifier: GPL-2.0 2 - // 3 - // TAS2781 HDA SPI driver 4 - // 5 - // Copyright 2024 - 2025 Texas Instruments, Inc. 6 - // 7 - // Author: Baojun Xu <baojun.xu@ti.com> 8 - 9 - #include <linux/crc8.h> 10 - #include <linux/firmware.h> 11 - #include <linux/init.h> 12 - #include <linux/interrupt.h> 13 - #include <linux/module.h> 14 - #include <linux/slab.h> 15 - #include <linux/types.h> 16 - #include <linux/unaligned.h> 17 - #include <sound/pcm_params.h> 18 - #include <sound/soc.h> 19 - #include <sound/tas2781-dsp.h> 20 - #include <sound/tlv.h> 21 - 22 - #include "tas2781-spi.h" 23 - 24 - #define OFFSET_ERROR_BIT BIT(31) 25 - 26 - #define ERROR_PRAM_CRCCHK 0x0000000 27 - #define ERROR_YRAM_CRCCHK 0x0000001 28 - #define PPC_DRIVER_CRCCHK 0x00000200 29 - 30 - #define TAS2781_SA_COEFF_SWAP_REG TASDEVICE_REG(0, 0x35, 0x2c) 31 - #define TAS2781_YRAM_BOOK1 140 32 - #define TAS2781_YRAM1_PAGE 42 33 - #define TAS2781_YRAM1_START_REG 88 34 - 35 - #define TAS2781_YRAM2_START_PAGE 43 36 - #define TAS2781_YRAM2_END_PAGE 49 37 - #define TAS2781_YRAM2_START_REG 8 38 - #define TAS2781_YRAM2_END_REG 127 39 - 40 - #define TAS2781_YRAM3_PAGE 50 41 - #define TAS2781_YRAM3_START_REG 8 42 - #define TAS2781_YRAM3_END_REG 27 43 - 44 - /* should not include B0_P53_R44-R47 */ 45 - #define TAS2781_YRAM_BOOK2 0 46 - #define TAS2781_YRAM4_START_PAGE 50 47 - #define TAS2781_YRAM4_END_PAGE 60 48 - 49 - #define TAS2781_YRAM5_PAGE 61 50 - #define TAS2781_YRAM5_START_REG TAS2781_YRAM3_START_REG 51 - #define TAS2781_YRAM5_END_REG TAS2781_YRAM3_END_REG 52 - 53 - #define TASDEVICE_MAXPROGRAM_NUM_KERNEL 5 54 - #define TASDEVICE_MAXCONFIG_NUM_KERNEL_MULTIPLE_AMPS 64 55 - #define TASDEVICE_MAXCONFIG_NUM_KERNEL 10 56 - #define MAIN_ALL_DEVICES_1X 0x01 57 - #define MAIN_DEVICE_A_1X 0x02 58 - #define MAIN_DEVICE_B_1X 0x03 59 - #define MAIN_DEVICE_C_1X 0x04 60 - #define MAIN_DEVICE_D_1X 0x05 61 - #define COEFF_DEVICE_A_1X 0x12 62 - #define COEFF_DEVICE_B_1X 0x13 63 - #define COEFF_DEVICE_C_1X 0x14 64 - #define COEFF_DEVICE_D_1X 0x15 65 - #define PRE_DEVICE_A_1X 0x22 66 - #define PRE_DEVICE_B_1X 0x23 67 - #define PRE_DEVICE_C_1X 0x24 68 - #define PRE_DEVICE_D_1X 0x25 69 - #define PRE_SOFTWARE_RESET_DEVICE_A 0x41 70 - #define PRE_SOFTWARE_RESET_DEVICE_B 0x42 71 - #define PRE_SOFTWARE_RESET_DEVICE_C 0x43 72 - #define PRE_SOFTWARE_RESET_DEVICE_D 0x44 73 - #define POST_SOFTWARE_RESET_DEVICE_A 0x45 74 - #define POST_SOFTWARE_RESET_DEVICE_B 0x46 75 - #define POST_SOFTWARE_RESET_DEVICE_C 0x47 76 - #define POST_SOFTWARE_RESET_DEVICE_D 0x48 77 - 78 - struct tas_crc { 79 - unsigned char offset; 80 - unsigned char len; 81 - }; 82 - 83 - struct blktyp_devidx_map { 84 - unsigned char blktyp; 85 - unsigned char dev_idx; 86 - }; 87 - 88 - /* fixed m68k compiling issue: mapping table can save code field */ 89 - static const struct blktyp_devidx_map ppc3_tas2781_mapping_table[] = { 90 - { MAIN_ALL_DEVICES_1X, 0x80 }, 91 - { MAIN_DEVICE_A_1X, 0x81 }, 92 - { COEFF_DEVICE_A_1X, 0x81 }, 93 - { PRE_DEVICE_A_1X, 0x81 }, 94 - { PRE_SOFTWARE_RESET_DEVICE_A, 0xC1 }, 95 - { POST_SOFTWARE_RESET_DEVICE_A, 0xC1 }, 96 - { MAIN_DEVICE_B_1X, 0x82 }, 97 - { COEFF_DEVICE_B_1X, 0x82 }, 98 - { PRE_DEVICE_B_1X, 0x82 }, 99 - { PRE_SOFTWARE_RESET_DEVICE_B, 0xC2 }, 100 - { POST_SOFTWARE_RESET_DEVICE_B, 0xC2 }, 101 - { MAIN_DEVICE_C_1X, 0x83 }, 102 - { COEFF_DEVICE_C_1X, 0x83 }, 103 - { PRE_DEVICE_C_1X, 0x83 }, 104 - { PRE_SOFTWARE_RESET_DEVICE_C, 0xC3 }, 105 - { POST_SOFTWARE_RESET_DEVICE_C, 0xC3 }, 106 - { MAIN_DEVICE_D_1X, 0x84 }, 107 - { COEFF_DEVICE_D_1X, 0x84 }, 108 - { PRE_DEVICE_D_1X, 0x84 }, 109 - { PRE_SOFTWARE_RESET_DEVICE_D, 0xC4 }, 110 - { POST_SOFTWARE_RESET_DEVICE_D, 0xC4 }, 111 - }; 112 - 113 - static const struct blktyp_devidx_map ppc3_mapping_table[] = { 114 - { MAIN_ALL_DEVICES_1X, 0x80 }, 115 - { MAIN_DEVICE_A_1X, 0x81 }, 116 - { COEFF_DEVICE_A_1X, 0xC1 }, 117 - { PRE_DEVICE_A_1X, 0xC1 }, 118 - { MAIN_DEVICE_B_1X, 0x82 }, 119 - { COEFF_DEVICE_B_1X, 0xC2 }, 120 - { PRE_DEVICE_B_1X, 0xC2 }, 121 - { MAIN_DEVICE_C_1X, 0x83 }, 122 - { COEFF_DEVICE_C_1X, 0xC3 }, 123 - { PRE_DEVICE_C_1X, 0xC3 }, 124 - { MAIN_DEVICE_D_1X, 0x84 }, 125 - { COEFF_DEVICE_D_1X, 0xC4 }, 126 - { PRE_DEVICE_D_1X, 0xC4 }, 127 - }; 128 - 129 - static const struct blktyp_devidx_map non_ppc3_mapping_table[] = { 130 - { MAIN_ALL_DEVICES, 0x80 }, 131 - { MAIN_DEVICE_A, 0x81 }, 132 - { COEFF_DEVICE_A, 0xC1 }, 133 - { PRE_DEVICE_A, 0xC1 }, 134 - { MAIN_DEVICE_B, 0x82 }, 135 - { COEFF_DEVICE_B, 0xC2 }, 136 - { PRE_DEVICE_B, 0xC2 }, 137 - { MAIN_DEVICE_C, 0x83 }, 138 - { COEFF_DEVICE_C, 0xC3 }, 139 - { PRE_DEVICE_C, 0xC3 }, 140 - { MAIN_DEVICE_D, 0x84 }, 141 - { COEFF_DEVICE_D, 0xC4 }, 142 - { PRE_DEVICE_D, 0xC4 }, 143 - }; 144 - 145 - /* 146 - * Device support different configurations for different scene, 147 - * like voice, music, calibration, was write in regbin file. 148 - * Will be stored into tas_priv after regbin was loaded. 149 - */ 150 - static struct tasdevice_config_info *tasdevice_add_config( 151 - struct tasdevice_priv *tas_priv, unsigned char *config_data, 152 - unsigned int config_size, int *status) 153 - { 154 - struct tasdevice_config_info *cfg_info; 155 - struct tasdev_blk_data **bk_da; 156 - unsigned int config_offset = 0; 157 - unsigned int i; 158 - 159 - /* 160 - * In most projects are many audio cases, such as music, handfree, 161 - * receiver, games, audio-to-haptics, PMIC record, bypass mode, 162 - * portrait, landscape, etc. Even in multiple audios, one or 163 - * two of the chips will work for the special case, such as 164 - * ultrasonic application. In order to support these variable-numbers 165 - * of audio cases, flexible configs have been introduced in the 166 - * DSP firmware. 167 - */ 168 - cfg_info = kzalloc(sizeof(*cfg_info), GFP_KERNEL); 169 - if (!cfg_info) { 170 - *status = -ENOMEM; 171 - return NULL; 172 - } 173 - 174 - if (tas_priv->rcabin.fw_hdr.binary_version_num >= 0x105) { 175 - if ((config_offset + 64) > config_size) { 176 - *status = -EINVAL; 177 - dev_err(tas_priv->dev, "add conf: Out of boundary\n"); 178 - goto config_err; 179 - } 180 - config_offset += 64; 181 - } 182 - 183 - if ((config_offset + 4) > config_size) { 184 - *status = -EINVAL; 185 - dev_err(tas_priv->dev, "add config: Out of boundary\n"); 186 - goto config_err; 187 - } 188 - 189 - /* 190 - * convert data[offset], data[offset + 1], data[offset + 2] and 191 - * data[offset + 3] into host 192 - */ 193 - cfg_info->nblocks = get_unaligned_be32(&config_data[config_offset]); 194 - config_offset += 4; 195 - 196 - /* 197 - * Several kinds of dsp/algorithm firmwares can run on tas2781, 198 - * the number and size of blk are not fixed and different among 199 - * these firmwares. 200 - */ 201 - bk_da = cfg_info->blk_data = kcalloc(cfg_info->nblocks, 202 - sizeof(*bk_da), GFP_KERNEL); 203 - if (!bk_da) { 204 - *status = -ENOMEM; 205 - goto config_err; 206 - } 207 - cfg_info->real_nblocks = 0; 208 - for (i = 0; i < cfg_info->nblocks; i++) { 209 - if (config_offset + 12 > config_size) { 210 - *status = -EINVAL; 211 - dev_err(tas_priv->dev, 212 - "%s: Out of boundary: i = %d nblocks = %u!\n", 213 - __func__, i, cfg_info->nblocks); 214 - goto block_err; 215 - } 216 - bk_da[i] = kzalloc(sizeof(*bk_da[i]), GFP_KERNEL); 217 - if (!bk_da[i]) { 218 - *status = -ENOMEM; 219 - goto block_err; 220 - } 221 - 222 - bk_da[i]->dev_idx = config_data[config_offset]; 223 - config_offset++; 224 - 225 - bk_da[i]->block_type = config_data[config_offset]; 226 - config_offset++; 227 - 228 - bk_da[i]->yram_checksum = 229 - get_unaligned_be16(&config_data[config_offset]); 230 - config_offset += 2; 231 - bk_da[i]->block_size = 232 - get_unaligned_be32(&config_data[config_offset]); 233 - config_offset += 4; 234 - 235 - bk_da[i]->n_subblks = 236 - get_unaligned_be32(&config_data[config_offset]); 237 - 238 - config_offset += 4; 239 - 240 - if (config_offset + bk_da[i]->block_size > config_size) { 241 - *status = -EINVAL; 242 - dev_err(tas_priv->dev, 243 - "%s: Out of boundary: i = %d blks = %u!\n", 244 - __func__, i, cfg_info->nblocks); 245 - goto block_err; 246 - } 247 - /* instead of kzalloc+memcpy */ 248 - bk_da[i]->regdata = kmemdup(&config_data[config_offset], 249 - bk_da[i]->block_size, GFP_KERNEL); 250 - if (!bk_da[i]->regdata) { 251 - *status = -ENOMEM; 252 - i++; 253 - goto block_err; 254 - } 255 - 256 - config_offset += bk_da[i]->block_size; 257 - cfg_info->real_nblocks += 1; 258 - } 259 - 260 - return cfg_info; 261 - block_err: 262 - for (int j = 0; j < i; j++) 263 - kfree(bk_da[j]); 264 - kfree(bk_da); 265 - config_err: 266 - kfree(cfg_info); 267 - return NULL; 268 - } 269 - 270 - /* Regbin file parser function. */ 271 - int tasdevice_spi_rca_parser(void *context, const struct firmware *fmw) 272 - { 273 - struct tasdevice_priv *tas_priv = context; 274 - struct tasdevice_config_info **cfg_info; 275 - struct tasdevice_rca_hdr *fw_hdr; 276 - struct tasdevice_rca *rca; 277 - unsigned int total_config_sz = 0; 278 - int offset = 0, ret = 0, i; 279 - unsigned char *buf; 280 - 281 - rca = &tas_priv->rcabin; 282 - fw_hdr = &rca->fw_hdr; 283 - if (!fmw || !fmw->data) { 284 - dev_err(tas_priv->dev, "Failed to read %s\n", 285 - tas_priv->rca_binaryname); 286 - tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL; 287 - return -EINVAL; 288 - } 289 - buf = (unsigned char *)fmw->data; 290 - fw_hdr->img_sz = get_unaligned_be32(&buf[offset]); 291 - offset += 4; 292 - if (fw_hdr->img_sz != fmw->size) { 293 - dev_err(tas_priv->dev, 294 - "File size not match, %d %u", (int)fmw->size, 295 - fw_hdr->img_sz); 296 - tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL; 297 - return -EINVAL; 298 - } 299 - 300 - fw_hdr->checksum = get_unaligned_be32(&buf[offset]); 301 - offset += 4; 302 - fw_hdr->binary_version_num = get_unaligned_be32(&buf[offset]); 303 - if (fw_hdr->binary_version_num < 0x103) { 304 - dev_err(tas_priv->dev, "File version 0x%04x is too low", 305 - fw_hdr->binary_version_num); 306 - tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL; 307 - return -EINVAL; 308 - } 309 - offset += 4; 310 - fw_hdr->drv_fw_version = get_unaligned_be32(&buf[offset]); 311 - offset += 8; 312 - fw_hdr->plat_type = buf[offset++]; 313 - fw_hdr->dev_family = buf[offset++]; 314 - fw_hdr->reserve = buf[offset++]; 315 - fw_hdr->ndev = buf[offset++]; 316 - if (offset + TASDEVICE_DEVICE_SUM > fw_hdr->img_sz) { 317 - dev_err(tas_priv->dev, "rca_ready: Out of boundary!\n"); 318 - tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL; 319 - return -EINVAL; 320 - } 321 - 322 - for (i = 0; i < TASDEVICE_DEVICE_SUM; i++, offset++) 323 - fw_hdr->devs[i] = buf[offset]; 324 - 325 - fw_hdr->nconfig = get_unaligned_be32(&buf[offset]); 326 - offset += 4; 327 - 328 - for (i = 0; i < TASDEVICE_CONFIG_SUM; i++) { 329 - fw_hdr->config_size[i] = get_unaligned_be32(&buf[offset]); 330 - offset += 4; 331 - total_config_sz += fw_hdr->config_size[i]; 332 - } 333 - 334 - if (fw_hdr->img_sz - total_config_sz != (unsigned int)offset) { 335 - dev_err(tas_priv->dev, "Bin file err %d - %d != %d!\n", 336 - fw_hdr->img_sz, total_config_sz, (int)offset); 337 - tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL; 338 - return -EINVAL; 339 - } 340 - 341 - cfg_info = kcalloc(fw_hdr->nconfig, sizeof(*cfg_info), GFP_KERNEL); 342 - if (!cfg_info) { 343 - tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL; 344 - return -ENOMEM; 345 - } 346 - rca->cfg_info = cfg_info; 347 - rca->ncfgs = 0; 348 - for (i = 0; i < (int)fw_hdr->nconfig; i++) { 349 - rca->ncfgs += 1; 350 - cfg_info[i] = tasdevice_add_config(tas_priv, &buf[offset], 351 - fw_hdr->config_size[i], &ret); 352 - if (ret) { 353 - tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL; 354 - return ret; 355 - } 356 - offset += (int)fw_hdr->config_size[i]; 357 - } 358 - 359 - return ret; 360 - } 361 - 362 - /* fixed m68k compiling issue: mapping table can save code field */ 363 - static unsigned char map_dev_idx(struct tasdevice_fw *tas_fmw, 364 - struct tasdev_blk *block) 365 - { 366 - struct blktyp_devidx_map *p = 367 - (struct blktyp_devidx_map *)non_ppc3_mapping_table; 368 - struct tasdevice_dspfw_hdr *fw_hdr = &tas_fmw->fw_hdr; 369 - struct tasdevice_fw_fixed_hdr *fw_fixed_hdr = &fw_hdr->fixed_hdr; 370 - int i, n = ARRAY_SIZE(non_ppc3_mapping_table); 371 - unsigned char dev_idx = 0; 372 - 373 - if (fw_fixed_hdr->ppcver >= PPC3_VERSION_TAS2781_BASIC_MIN) { 374 - p = (struct blktyp_devidx_map *)ppc3_tas2781_mapping_table; 375 - n = ARRAY_SIZE(ppc3_tas2781_mapping_table); 376 - } else if (fw_fixed_hdr->ppcver >= PPC3_VERSION_BASE) { 377 - p = (struct blktyp_devidx_map *)ppc3_mapping_table; 378 - n = ARRAY_SIZE(ppc3_mapping_table); 379 - } 380 - 381 - for (i = 0; i < n; i++) { 382 - if (block->type == p[i].blktyp) { 383 - dev_idx = p[i].dev_idx; 384 - break; 385 - } 386 - } 387 - 388 - return dev_idx; 389 - } 390 - 391 - /* Block parser function. */ 392 - static int fw_parse_block_data_kernel(struct tasdevice_fw *tas_fmw, 393 - struct tasdev_blk *block, const struct firmware *fmw, int offset) 394 - { 395 - const unsigned char *data = fmw->data; 396 - 397 - if (offset + 16 > fmw->size) { 398 - dev_err(tas_fmw->dev, "%s: File Size error\n", __func__); 399 - return -EINVAL; 400 - } 401 - 402 - /* 403 - * Convert data[offset], data[offset + 1], data[offset + 2] and 404 - * data[offset + 3] into host. 405 - */ 406 - block->type = get_unaligned_be32(&data[offset]); 407 - offset += 4; 408 - 409 - block->is_pchksum_present = data[offset++]; 410 - block->pchksum = data[offset++]; 411 - block->is_ychksum_present = data[offset++]; 412 - block->ychksum = data[offset++]; 413 - block->blk_size = get_unaligned_be32(&data[offset]); 414 - offset += 4; 415 - block->nr_subblocks = get_unaligned_be32(&data[offset]); 416 - offset += 4; 417 - 418 - /* 419 - * Fixed m68k compiling issue: 420 - * 1. mapping table can save code field. 421 - * 2. storing the dev_idx as a member of block can reduce unnecessary 422 - * time and system resource comsumption of dev_idx mapping every 423 - * time the block data writing to the dsp. 424 - */ 425 - block->dev_idx = map_dev_idx(tas_fmw, block); 426 - 427 - if (offset + block->blk_size > fmw->size) { 428 - dev_err(tas_fmw->dev, "%s: nSublocks error\n", __func__); 429 - return -EINVAL; 430 - } 431 - /* instead of kzalloc+memcpy */ 432 - block->data = kmemdup(&data[offset], block->blk_size, GFP_KERNEL); 433 - if (!block->data) 434 - return -ENOMEM; 435 - 436 - offset += block->blk_size; 437 - 438 - return offset; 439 - } 440 - 441 - /* Data of block parser function. */ 442 - static int fw_parse_data_kernel(struct tasdevice_fw *tas_fmw, 443 - struct tasdevice_data *img_data, const struct firmware *fmw, 444 - int offset) 445 - { 446 - const unsigned char *data = fmw->data; 447 - struct tasdev_blk *blk; 448 - unsigned int i; 449 - 450 - if (offset + 4 > fmw->size) { 451 - dev_err(tas_fmw->dev, "%s: File Size error\n", __func__); 452 - return -EINVAL; 453 - } 454 - img_data->nr_blk = get_unaligned_be32(&data[offset]); 455 - offset += 4; 456 - 457 - img_data->dev_blks = kcalloc(img_data->nr_blk, 458 - sizeof(struct tasdev_blk), GFP_KERNEL); 459 - if (!img_data->dev_blks) 460 - return -ENOMEM; 461 - 462 - for (i = 0; i < img_data->nr_blk; i++) { 463 - blk = &img_data->dev_blks[i]; 464 - offset = fw_parse_block_data_kernel( 465 - tas_fmw, blk, fmw, offset); 466 - if (offset < 0) { 467 - kfree(img_data->dev_blks); 468 - return -EINVAL; 469 - } 470 - } 471 - 472 - return offset; 473 - } 474 - 475 - /* Data of DSP program parser function. */ 476 - static int fw_parse_program_data_kernel( 477 - struct tasdevice_priv *tas_priv, struct tasdevice_fw *tas_fmw, 478 - const struct firmware *fmw, int offset) 479 - { 480 - struct tasdevice_prog *program; 481 - unsigned int i; 482 - 483 - for (i = 0; i < tas_fmw->nr_programs; i++) { 484 - program = &tas_fmw->programs[i]; 485 - if (offset + 72 > fmw->size) { 486 - dev_err(tas_priv->dev, "%s: mpName error\n", __func__); 487 - return -EINVAL; 488 - } 489 - /* skip 72 unused byts */ 490 - offset += 72; 491 - 492 - offset = fw_parse_data_kernel(tas_fmw, &program->dev_data, 493 - fmw, offset); 494 - if (offset < 0) 495 - break; 496 - } 497 - 498 - return offset; 499 - } 500 - 501 - /* Data of DSP configurations parser function. */ 502 - static int fw_parse_configuration_data_kernel(struct tasdevice_priv *tas_priv, 503 - struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset) 504 - { 505 - const unsigned char *data = fmw->data; 506 - struct tasdevice_config *config; 507 - unsigned int i; 508 - 509 - for (i = 0; i < tas_fmw->nr_configurations; i++) { 510 - config = &tas_fmw->configs[i]; 511 - if (offset + 80 > fmw->size) { 512 - dev_err(tas_priv->dev, "%s: mpName error\n", __func__); 513 - return -EINVAL; 514 - } 515 - memcpy(config->name, &data[offset], 64); 516 - /* skip extra 16 bytes */ 517 - offset += 80; 518 - 519 - offset = fw_parse_data_kernel(tas_fmw, &config->dev_data, 520 - fmw, offset); 521 - if (offset < 0) 522 - break; 523 - } 524 - 525 - return offset; 526 - } 527 - 528 - /* DSP firmware file header parser function for early PPC3 firmware binary. */ 529 - static int fw_parse_variable_header_kernel(struct tasdevice_priv *tas_priv, 530 - const struct firmware *fmw, int offset) 531 - { 532 - struct tasdevice_fw *tas_fmw = tas_priv->fmw; 533 - struct tasdevice_dspfw_hdr *fw_hdr = &tas_fmw->fw_hdr; 534 - struct tasdevice_config *config; 535 - struct tasdevice_prog *program; 536 - const unsigned char *buf = fmw->data; 537 - unsigned short max_confs; 538 - unsigned int i; 539 - 540 - if (offset + 12 + 4 * TASDEVICE_MAXPROGRAM_NUM_KERNEL > fmw->size) { 541 - dev_err(tas_priv->dev, "%s: File Size error\n", __func__); 542 - return -EINVAL; 543 - } 544 - fw_hdr->device_family = get_unaligned_be16(&buf[offset]); 545 - if (fw_hdr->device_family != 0) { 546 - dev_err(tas_priv->dev, "%s:not TAS device\n", __func__); 547 - return -EINVAL; 548 - } 549 - offset += 2; 550 - fw_hdr->device = get_unaligned_be16(&buf[offset]); 551 - if (fw_hdr->device >= TASDEVICE_DSP_TAS_MAX_DEVICE || 552 - fw_hdr->device == 6) { 553 - dev_err(tas_priv->dev, "Unsupported dev %d\n", fw_hdr->device); 554 - return -EINVAL; 555 - } 556 - offset += 2; 557 - 558 - tas_fmw->nr_programs = get_unaligned_be32(&buf[offset]); 559 - offset += 4; 560 - 561 - if (tas_fmw->nr_programs == 0 || 562 - tas_fmw->nr_programs > TASDEVICE_MAXPROGRAM_NUM_KERNEL) { 563 - dev_err(tas_priv->dev, "mnPrograms is invalid\n"); 564 - return -EINVAL; 565 - } 566 - 567 - tas_fmw->programs = kcalloc(tas_fmw->nr_programs, 568 - sizeof(*tas_fmw->programs), GFP_KERNEL); 569 - if (!tas_fmw->programs) 570 - return -ENOMEM; 571 - 572 - for (i = 0; i < tas_fmw->nr_programs; i++) { 573 - program = &tas_fmw->programs[i]; 574 - program->prog_size = get_unaligned_be32(&buf[offset]); 575 - offset += 4; 576 - } 577 - 578 - /* Skip the unused prog_size */ 579 - offset += 4 * (TASDEVICE_MAXPROGRAM_NUM_KERNEL - tas_fmw->nr_programs); 580 - 581 - tas_fmw->nr_configurations = get_unaligned_be32(&buf[offset]); 582 - offset += 4; 583 - 584 - /* 585 - * The max number of config in firmware greater than 4 pieces of 586 - * tas2781s is different from the one lower than 4 pieces of 587 - * tas2781s. 588 - */ 589 - max_confs = TASDEVICE_MAXCONFIG_NUM_KERNEL; 590 - if (tas_fmw->nr_configurations == 0 || 591 - tas_fmw->nr_configurations > max_confs) { 592 - dev_err(tas_priv->dev, "%s: Conf is invalid\n", __func__); 593 - kfree(tas_fmw->programs); 594 - return -EINVAL; 595 - } 596 - 597 - if (offset + 4 * max_confs > fmw->size) { 598 - dev_err(tas_priv->dev, "%s: mpConfigurations err\n", __func__); 599 - kfree(tas_fmw->programs); 600 - return -EINVAL; 601 - } 602 - 603 - tas_fmw->configs = kcalloc(tas_fmw->nr_configurations, 604 - sizeof(*tas_fmw->configs), GFP_KERNEL); 605 - if (!tas_fmw->configs) { 606 - kfree(tas_fmw->programs); 607 - return -ENOMEM; 608 - } 609 - 610 - for (i = 0; i < tas_fmw->nr_programs; i++) { 611 - config = &tas_fmw->configs[i]; 612 - config->cfg_size = get_unaligned_be32(&buf[offset]); 613 - offset += 4; 614 - } 615 - 616 - /* Skip the unused configs */ 617 - offset += 4 * (max_confs - tas_fmw->nr_programs); 618 - 619 - return offset; 620 - } 621 - 622 - /* 623 - * In sub-block data, have three type sub-block: 624 - * 1. Single byte write. 625 - * 2. Multi-byte write. 626 - * 3. Delay. 627 - * 4. Bits update. 628 - * This function perform single byte write to device. 629 - */ 630 - static int tasdevice_single_byte_wr(void *context, int dev_idx, 631 - unsigned char *data, int sublocksize) 632 - { 633 - struct tasdevice_priv *tas_priv = context; 634 - unsigned short len = get_unaligned_be16(&data[2]); 635 - int i, subblk_offset, rc; 636 - 637 - subblk_offset = 4; 638 - if (subblk_offset + 4 * len > sublocksize) { 639 - dev_err(tas_priv->dev, "process_block: Out of boundary\n"); 640 - return 0; 641 - } 642 - 643 - for (i = 0; i < len; i++) { 644 - if (dev_idx == (tas_priv->index + 1) || dev_idx == 0) { 645 - rc = tasdevice_spi_dev_write(tas_priv, 646 - TASDEVICE_REG(data[subblk_offset], 647 - data[subblk_offset + 1], 648 - data[subblk_offset + 2]), 649 - data[subblk_offset + 3]); 650 - if (rc < 0) { 651 - dev_err(tas_priv->dev, 652 - "process_block: single write error\n"); 653 - subblk_offset |= OFFSET_ERROR_BIT; 654 - } 655 - } 656 - subblk_offset += 4; 657 - } 658 - 659 - return subblk_offset; 660 - } 661 - 662 - /* 663 - * In sub-block data, have three type sub-block: 664 - * 1. Single byte write. 665 - * 2. Multi-byte write. 666 - * 3. Delay. 667 - * 4. Bits update. 668 - * This function perform multi-write to device. 669 - */ 670 - static int tasdevice_burst_wr(void *context, int dev_idx, unsigned char *data, 671 - int sublocksize) 672 - { 673 - struct tasdevice_priv *tas_priv = context; 674 - unsigned short len = get_unaligned_be16(&data[2]); 675 - int subblk_offset, rc; 676 - 677 - subblk_offset = 4; 678 - if (subblk_offset + 4 + len > sublocksize) { 679 - dev_err(tas_priv->dev, "%s: BST Out of boundary\n", __func__); 680 - subblk_offset |= OFFSET_ERROR_BIT; 681 - } 682 - if (len % 4) { 683 - dev_err(tas_priv->dev, "%s:Bst-len(%u)not div by 4\n", 684 - __func__, len); 685 - subblk_offset |= OFFSET_ERROR_BIT; 686 - } 687 - 688 - if (dev_idx == (tas_priv->index + 1) || dev_idx == 0) { 689 - rc = tasdevice_spi_dev_bulk_write(tas_priv, 690 - TASDEVICE_REG(data[subblk_offset], 691 - data[subblk_offset + 1], 692 - data[subblk_offset + 2]), 693 - &data[subblk_offset + 4], len); 694 - if (rc < 0) { 695 - dev_err(tas_priv->dev, "%s: bulk_write error = %d\n", 696 - __func__, rc); 697 - subblk_offset |= OFFSET_ERROR_BIT; 698 - } 699 - } 700 - subblk_offset += (len + 4); 701 - 702 - return subblk_offset; 703 - } 704 - 705 - /* Just delay for ms.*/ 706 - static int tasdevice_delay(void *context, int dev_idx, unsigned char *data, 707 - int sublocksize) 708 - { 709 - struct tasdevice_priv *tas_priv = context; 710 - unsigned int sleep_time, subblk_offset = 2; 711 - 712 - if (subblk_offset + 2 > sublocksize) { 713 - dev_err(tas_priv->dev, "%s: delay Out of boundary\n", 714 - __func__); 715 - subblk_offset |= OFFSET_ERROR_BIT; 716 - } 717 - if (dev_idx == (tas_priv->index + 1) || dev_idx == 0) { 718 - sleep_time = get_unaligned_be16(&data[2]) * 1000; 719 - fsleep(sleep_time); 720 - } 721 - subblk_offset += 2; 722 - 723 - return subblk_offset; 724 - } 725 - 726 - /* 727 - * In sub-block data, have three type sub-block: 728 - * 1. Single byte write. 729 - * 2. Multi-byte write. 730 - * 3. Delay. 731 - * 4. Bits update. 732 - * This function perform bits update. 733 - */ 734 - static int tasdevice_field_wr(void *context, int dev_idx, unsigned char *data, 735 - int sublocksize) 736 - { 737 - struct tasdevice_priv *tas_priv = context; 738 - int rc, subblk_offset = 2; 739 - 740 - if (subblk_offset + 6 > sublocksize) { 741 - dev_err(tas_priv->dev, "%s: bit write Out of boundary\n", 742 - __func__); 743 - subblk_offset |= OFFSET_ERROR_BIT; 744 - } 745 - if (dev_idx == (tas_priv->index + 1) || dev_idx == 0) { 746 - rc = tasdevice_spi_dev_update_bits(tas_priv, 747 - TASDEVICE_REG(data[subblk_offset + 2], 748 - data[subblk_offset + 3], 749 - data[subblk_offset + 4]), 750 - data[subblk_offset + 1], 751 - data[subblk_offset + 5]); 752 - if (rc < 0) { 753 - dev_err(tas_priv->dev, "%s: update_bits error = %d\n", 754 - __func__, rc); 755 - subblk_offset |= OFFSET_ERROR_BIT; 756 - } 757 - } 758 - subblk_offset += 6; 759 - 760 - return subblk_offset; 761 - } 762 - 763 - /* Data block process function. */ 764 - static int tasdevice_process_block(void *context, unsigned char *data, 765 - unsigned char dev_idx, int sublocksize) 766 - { 767 - struct tasdevice_priv *tas_priv = context; 768 - int blktyp = dev_idx & 0xC0, subblk_offset; 769 - unsigned char subblk_typ = data[1]; 770 - 771 - switch (subblk_typ) { 772 - case TASDEVICE_CMD_SING_W: 773 - subblk_offset = tasdevice_single_byte_wr(tas_priv, 774 - dev_idx & 0x3f, data, sublocksize); 775 - break; 776 - case TASDEVICE_CMD_BURST: 777 - subblk_offset = tasdevice_burst_wr(tas_priv, 778 - dev_idx & 0x3f, data, sublocksize); 779 - break; 780 - case TASDEVICE_CMD_DELAY: 781 - subblk_offset = tasdevice_delay(tas_priv, 782 - dev_idx & 0x3f, data, sublocksize); 783 - break; 784 - case TASDEVICE_CMD_FIELD_W: 785 - subblk_offset = tasdevice_field_wr(tas_priv, 786 - dev_idx & 0x3f, data, sublocksize); 787 - break; 788 - default: 789 - subblk_offset = 2; 790 - break; 791 - } 792 - if (((subblk_offset & OFFSET_ERROR_BIT) != 0) && blktyp != 0) { 793 - if (blktyp == 0x80) { 794 - tas_priv->cur_prog = -1; 795 - tas_priv->cur_conf = -1; 796 - } else 797 - tas_priv->cur_conf = -1; 798 - } 799 - subblk_offset &= ~OFFSET_ERROR_BIT; 800 - 801 - return subblk_offset; 802 - } 803 - 804 - /* 805 - * Device support different configurations for different scene, 806 - * this function was used for choose different config. 807 - */ 808 - void tasdevice_spi_select_cfg_blk(void *pContext, int conf_no, 809 - unsigned char block_type) 810 - { 811 - struct tasdevice_priv *tas_priv = pContext; 812 - struct tasdevice_rca *rca = &tas_priv->rcabin; 813 - struct tasdevice_config_info **cfg_info = rca->cfg_info; 814 - struct tasdev_blk_data **blk_data; 815 - unsigned int j, k; 816 - 817 - if (conf_no >= rca->ncfgs || conf_no < 0 || !cfg_info) { 818 - dev_err(tas_priv->dev, "conf_no should be not more than %u\n", 819 - rca->ncfgs); 820 - return; 821 - } 822 - blk_data = cfg_info[conf_no]->blk_data; 823 - 824 - for (j = 0; j < cfg_info[conf_no]->real_nblocks; j++) { 825 - unsigned int length = 0, rc = 0; 826 - 827 - if (block_type > 5 || block_type < 2) { 828 - dev_err(tas_priv->dev, 829 - "block_type should be in range from 2 to 5\n"); 830 - break; 831 - } 832 - if (block_type != blk_data[j]->block_type) 833 - continue; 834 - 835 - for (k = 0; k < blk_data[j]->n_subblks; k++) { 836 - tas_priv->is_loading = true; 837 - 838 - rc = tasdevice_process_block(tas_priv, 839 - blk_data[j]->regdata + length, 840 - blk_data[j]->dev_idx, 841 - blk_data[j]->block_size - length); 842 - length += rc; 843 - if (blk_data[j]->block_size < length) { 844 - dev_err(tas_priv->dev, 845 - "%s: %u %u out of boundary\n", 846 - __func__, length, 847 - blk_data[j]->block_size); 848 - break; 849 - } 850 - } 851 - if (length != blk_data[j]->block_size) 852 - dev_err(tas_priv->dev, "%s: %u %u size is not same\n", 853 - __func__, length, blk_data[j]->block_size); 854 - } 855 - } 856 - 857 - /* Block process function. */ 858 - static int tasdevice_load_block_kernel( 859 - struct tasdevice_priv *tasdevice, struct tasdev_blk *block) 860 - { 861 - const unsigned int blk_size = block->blk_size; 862 - unsigned char *data = block->data; 863 - unsigned int i, length; 864 - 865 - for (i = 0, length = 0; i < block->nr_subblocks; i++) { 866 - int rc = tasdevice_process_block(tasdevice, data + length, 867 - block->dev_idx, blk_size - length); 868 - if (rc < 0) { 869 - dev_err(tasdevice->dev, 870 - "%s: %u %u sublock write error\n", 871 - __func__, length, blk_size); 872 - return rc; 873 - } 874 - length += rc; 875 - if (blk_size < length) { 876 - dev_err(tasdevice->dev, "%s: %u %u out of boundary\n", 877 - __func__, length, blk_size); 878 - rc = -ENOMEM; 879 - return rc; 880 - } 881 - } 882 - 883 - return 0; 884 - } 885 - 886 - /* DSP firmware file header parser function. */ 887 - static int fw_parse_variable_hdr(struct tasdevice_priv *tas_priv, 888 - struct tasdevice_dspfw_hdr *fw_hdr, 889 - const struct firmware *fmw, int offset) 890 - { 891 - const unsigned char *buf = fmw->data; 892 - int len = strlen((char *)&buf[offset]); 893 - 894 - len++; 895 - 896 - if (offset + len + 8 > fmw->size) { 897 - dev_err(tas_priv->dev, "%s: File Size error\n", __func__); 898 - return -EINVAL; 899 - } 900 - 901 - offset += len; 902 - 903 - fw_hdr->device_family = get_unaligned_be32(&buf[offset]); 904 - if (fw_hdr->device_family != 0) { 905 - dev_err(tas_priv->dev, "%s: not TAS device\n", __func__); 906 - return -EINVAL; 907 - } 908 - offset += 4; 909 - 910 - fw_hdr->device = get_unaligned_be32(&buf[offset]); 911 - if (fw_hdr->device >= TASDEVICE_DSP_TAS_MAX_DEVICE || 912 - fw_hdr->device == 6) { 913 - dev_err(tas_priv->dev, "Unsupported dev %d\n", fw_hdr->device); 914 - return -EINVAL; 915 - } 916 - offset += 4; 917 - fw_hdr->ndev = 1; 918 - 919 - return offset; 920 - } 921 - 922 - /* DSP firmware file header parser function for size variabled header. */ 923 - static int fw_parse_variable_header_git(struct tasdevice_priv 924 - *tas_priv, const struct firmware *fmw, int offset) 925 - { 926 - struct tasdevice_fw *tas_fmw = tas_priv->fmw; 927 - struct tasdevice_dspfw_hdr *fw_hdr = &tas_fmw->fw_hdr; 928 - 929 - offset = fw_parse_variable_hdr(tas_priv, fw_hdr, fmw, offset); 930 - 931 - return offset; 932 - } 933 - 934 - /* DSP firmware file block parser function. */ 935 - static int fw_parse_block_data(struct tasdevice_fw *tas_fmw, 936 - struct tasdev_blk *block, const struct firmware *fmw, int offset) 937 - { 938 - unsigned char *data = (unsigned char *)fmw->data; 939 - int n; 940 - 941 - if (offset + 8 > fmw->size) { 942 - dev_err(tas_fmw->dev, "%s: Type error\n", __func__); 943 - return -EINVAL; 944 - } 945 - block->type = get_unaligned_be32(&data[offset]); 946 - offset += 4; 947 - 948 - if (tas_fmw->fw_hdr.fixed_hdr.drv_ver >= PPC_DRIVER_CRCCHK) { 949 - if (offset + 8 > fmw->size) { 950 - dev_err(tas_fmw->dev, "PChkSumPresent error\n"); 951 - return -EINVAL; 952 - } 953 - block->is_pchksum_present = data[offset]; 954 - offset++; 955 - 956 - block->pchksum = data[offset]; 957 - offset++; 958 - 959 - block->is_ychksum_present = data[offset]; 960 - offset++; 961 - 962 - block->ychksum = data[offset]; 963 - offset++; 964 - } else { 965 - block->is_pchksum_present = 0; 966 - block->is_ychksum_present = 0; 967 - } 968 - 969 - block->nr_cmds = get_unaligned_be32(&data[offset]); 970 - offset += 4; 971 - 972 - n = block->nr_cmds * 4; 973 - if (offset + n > fmw->size) { 974 - dev_err(tas_fmw->dev, 975 - "%s: File Size(%lu) error offset = %d n = %d\n", 976 - __func__, (unsigned long)fmw->size, offset, n); 977 - return -EINVAL; 978 - } 979 - /* instead of kzalloc+memcpy */ 980 - block->data = kmemdup(&data[offset], n, GFP_KERNEL); 981 - if (!block->data) 982 - return -ENOMEM; 983 - 984 - offset += n; 985 - 986 - return offset; 987 - } 988 - 989 - /* 990 - * When parsing error occurs, all the memory resource will be released 991 - * in the end of tasdevice_rca_ready. 992 - */ 993 - static int fw_parse_data(struct tasdevice_fw *tas_fmw, 994 - struct tasdevice_data *img_data, const struct firmware *fmw, 995 - int offset) 996 - { 997 - const unsigned char *data = (unsigned char *)fmw->data; 998 - struct tasdev_blk *blk; 999 - unsigned int i, n; 1000 - 1001 - if (offset + 64 > fmw->size) { 1002 - dev_err(tas_fmw->dev, "%s: Name error\n", __func__); 1003 - return -EINVAL; 1004 - } 1005 - memcpy(img_data->name, &data[offset], 64); 1006 - offset += 64; 1007 - 1008 - n = strlen((char *)&data[offset]); 1009 - n++; 1010 - if (offset + n + 2 > fmw->size) { 1011 - dev_err(tas_fmw->dev, "%s: Description error\n", __func__); 1012 - return -EINVAL; 1013 - } 1014 - offset += n; 1015 - img_data->nr_blk = get_unaligned_be16(&data[offset]); 1016 - offset += 2; 1017 - 1018 - img_data->dev_blks = kcalloc(img_data->nr_blk, 1019 - sizeof(*img_data->dev_blks), GFP_KERNEL); 1020 - if (!img_data->dev_blks) 1021 - return -ENOMEM; 1022 - 1023 - for (i = 0; i < img_data->nr_blk; i++) { 1024 - blk = &img_data->dev_blks[i]; 1025 - offset = fw_parse_block_data(tas_fmw, blk, fmw, offset); 1026 - if (offset < 0) 1027 - return -EINVAL; 1028 - } 1029 - 1030 - return offset; 1031 - } 1032 - 1033 - /* 1034 - * When parsing error occurs, all the memory resource will be released 1035 - * in the end of tasdevice_rca_ready. 1036 - */ 1037 - static int fw_parse_program_data(struct tasdevice_priv *tas_priv, 1038 - struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset) 1039 - { 1040 - unsigned char *buf = (unsigned char *)fmw->data; 1041 - struct tasdevice_prog *program; 1042 - int i; 1043 - 1044 - if (offset + 2 > fmw->size) { 1045 - dev_err(tas_priv->dev, "%s: File Size error\n", __func__); 1046 - return -EINVAL; 1047 - } 1048 - tas_fmw->nr_programs = get_unaligned_be16(&buf[offset]); 1049 - offset += 2; 1050 - 1051 - if (tas_fmw->nr_programs == 0) { 1052 - /* Not error in calibration Data file, return directly */ 1053 - dev_dbg(tas_priv->dev, "%s: No Programs data, maybe calbin\n", 1054 - __func__); 1055 - return offset; 1056 - } 1057 - 1058 - tas_fmw->programs = 1059 - kcalloc(tas_fmw->nr_programs, sizeof(*tas_fmw->programs), 1060 - GFP_KERNEL); 1061 - if (!tas_fmw->programs) 1062 - return -ENOMEM; 1063 - 1064 - for (i = 0; i < tas_fmw->nr_programs; i++) { 1065 - int n = 0; 1066 - 1067 - program = &tas_fmw->programs[i]; 1068 - if (offset + 64 > fmw->size) { 1069 - dev_err(tas_priv->dev, "%s: mpName error\n", __func__); 1070 - return -EINVAL; 1071 - } 1072 - offset += 64; 1073 - 1074 - n = strlen((char *)&buf[offset]); 1075 - /* skip '\0' and 5 unused bytes */ 1076 - n += 6; 1077 - if (offset + n > fmw->size) { 1078 - dev_err(tas_priv->dev, "Description err\n"); 1079 - return -EINVAL; 1080 - } 1081 - 1082 - offset += n; 1083 - 1084 - offset = fw_parse_data(tas_fmw, &program->dev_data, fmw, 1085 - offset); 1086 - if (offset < 0) 1087 - return offset; 1088 - } 1089 - 1090 - return offset; 1091 - } 1092 - 1093 - /* 1094 - * When parsing error occurs, all the memory resource will be released 1095 - * in the end of tasdevice_rca_ready. 1096 - */ 1097 - static int fw_parse_configuration_data(struct tasdevice_priv *tas_priv, 1098 - struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset) 1099 - { 1100 - unsigned char *data = (unsigned char *)fmw->data; 1101 - struct tasdevice_config *config; 1102 - unsigned int i, n; 1103 - 1104 - if (offset + 2 > fmw->size) { 1105 - dev_err(tas_priv->dev, "%s: File Size error\n", __func__); 1106 - return -EINVAL; 1107 - } 1108 - tas_fmw->nr_configurations = get_unaligned_be16(&data[offset]); 1109 - offset += 2; 1110 - 1111 - if (tas_fmw->nr_configurations == 0) { 1112 - dev_err(tas_priv->dev, "%s: Conf is zero\n", __func__); 1113 - /* Not error for calibration Data file, return directly */ 1114 - return offset; 1115 - } 1116 - tas_fmw->configs = kcalloc(tas_fmw->nr_configurations, 1117 - sizeof(*tas_fmw->configs), GFP_KERNEL); 1118 - if (!tas_fmw->configs) 1119 - return -ENOMEM; 1120 - for (i = 0; i < tas_fmw->nr_configurations; i++) { 1121 - config = &tas_fmw->configs[i]; 1122 - if (offset + 64 > fmw->size) { 1123 - dev_err(tas_priv->dev, "File Size err\n"); 1124 - return -EINVAL; 1125 - } 1126 - memcpy(config->name, &data[offset], 64); 1127 - offset += 64; 1128 - 1129 - n = strlen((char *)&data[offset]); 1130 - n += 15; 1131 - if (offset + n > fmw->size) { 1132 - dev_err(tas_priv->dev, "Description err\n"); 1133 - return -EINVAL; 1134 - } 1135 - offset += n; 1136 - offset = fw_parse_data(tas_fmw, &config->dev_data, 1137 - fmw, offset); 1138 - if (offset < 0) 1139 - break; 1140 - } 1141 - 1142 - return offset; 1143 - } 1144 - 1145 - /* yram5 page check. */ 1146 - static bool check_inpage_yram_rg(struct tas_crc *cd, 1147 - unsigned char reg, unsigned char len) 1148 - { 1149 - bool in = false; 1150 - 1151 - if (reg <= TAS2781_YRAM5_END_REG && 1152 - reg >= TAS2781_YRAM5_START_REG) { 1153 - if (reg + len > TAS2781_YRAM5_END_REG) 1154 - cd->len = TAS2781_YRAM5_END_REG - reg + 1; 1155 - else 1156 - cd->len = len; 1157 - cd->offset = reg; 1158 - in = true; 1159 - } else if (reg < TAS2781_YRAM5_START_REG) { 1160 - if (reg + len > TAS2781_YRAM5_START_REG) { 1161 - cd->offset = TAS2781_YRAM5_START_REG; 1162 - cd->len = len - TAS2781_YRAM5_START_REG + reg; 1163 - in = true; 1164 - } 1165 - } 1166 - 1167 - return in; 1168 - } 1169 - 1170 - /* DSP firmware yram block check. */ 1171 - static bool check_inpage_yram_bk1(struct tas_crc *cd, 1172 - unsigned char page, unsigned char reg, unsigned char len) 1173 - { 1174 - bool in = false; 1175 - 1176 - if (page == TAS2781_YRAM1_PAGE) { 1177 - if (reg >= TAS2781_YRAM1_START_REG) { 1178 - cd->offset = reg; 1179 - cd->len = len; 1180 - in = true; 1181 - } else if (reg + len > TAS2781_YRAM1_START_REG) { 1182 - cd->offset = TAS2781_YRAM1_START_REG; 1183 - cd->len = len - TAS2781_YRAM1_START_REG + reg; 1184 - in = true; 1185 - } 1186 - } else if (page == TAS2781_YRAM3_PAGE) { 1187 - in = check_inpage_yram_rg(cd, reg, len); 1188 - } 1189 - 1190 - return in; 1191 - } 1192 - 1193 - /* 1194 - * Return Code: 1195 - * true -- the registers are in the inpage yram 1196 - * false -- the registers are NOT in the inpage yram 1197 - */ 1198 - static bool check_inpage_yram(struct tas_crc *cd, unsigned char book, 1199 - unsigned char page, unsigned char reg, unsigned char len) 1200 - { 1201 - bool in = false; 1202 - 1203 - if (book == TAS2781_YRAM_BOOK1) 1204 - in = check_inpage_yram_bk1(cd, page, reg, len); 1205 - else if (book == TAS2781_YRAM_BOOK2 && page == TAS2781_YRAM5_PAGE) 1206 - in = check_inpage_yram_rg(cd, reg, len); 1207 - 1208 - return in; 1209 - } 1210 - 1211 - /* yram4 page check. */ 1212 - static bool check_inblock_yram_bk(struct tas_crc *cd, 1213 - unsigned char page, unsigned char reg, unsigned char len) 1214 - { 1215 - bool in = false; 1216 - 1217 - if ((page >= TAS2781_YRAM4_START_PAGE && 1218 - page <= TAS2781_YRAM4_END_PAGE) || 1219 - (page >= TAS2781_YRAM2_START_PAGE && 1220 - page <= TAS2781_YRAM2_END_PAGE)) { 1221 - if (reg <= TAS2781_YRAM2_END_REG && 1222 - reg >= TAS2781_YRAM2_START_REG) { 1223 - cd->offset = reg; 1224 - cd->len = len; 1225 - in = true; 1226 - } else if (reg < TAS2781_YRAM2_START_REG) { 1227 - if (reg + len - 1 >= TAS2781_YRAM2_START_REG) { 1228 - cd->offset = TAS2781_YRAM2_START_REG; 1229 - cd->len = reg + len - TAS2781_YRAM2_START_REG; 1230 - in = true; 1231 - } 1232 - } 1233 - } 1234 - 1235 - return in; 1236 - } 1237 - 1238 - /* 1239 - * Return Code: 1240 - * true -- the registers are in the inblock yram 1241 - * false -- the registers are NOT in the inblock yram 1242 - */ 1243 - static bool check_inblock_yram(struct tas_crc *cd, unsigned char book, 1244 - unsigned char page, unsigned char reg, unsigned char len) 1245 - { 1246 - bool in = false; 1247 - 1248 - if (book == TAS2781_YRAM_BOOK1 || book == TAS2781_YRAM_BOOK2) 1249 - in = check_inblock_yram_bk(cd, page, reg, len); 1250 - 1251 - return in; 1252 - } 1253 - 1254 - /* yram page check. */ 1255 - static bool check_yram(struct tas_crc *cd, unsigned char book, 1256 - unsigned char page, unsigned char reg, unsigned char len) 1257 - { 1258 - bool in; 1259 - 1260 - in = check_inpage_yram(cd, book, page, reg, len); 1261 - if (!in) 1262 - in = check_inblock_yram(cd, book, page, reg, len); 1263 - 1264 - return in; 1265 - } 1266 - 1267 - /* Checksum for data block. */ 1268 - static int tasdev_multibytes_chksum(struct tasdevice_priv *tasdevice, 1269 - unsigned char book, unsigned char page, 1270 - unsigned char reg, unsigned int len) 1271 - { 1272 - struct tas_crc crc_data; 1273 - unsigned char crc_chksum = 0; 1274 - unsigned char nBuf1[128]; 1275 - int ret = 0, i; 1276 - bool in; 1277 - 1278 - if ((reg + len - 1) > 127) { 1279 - ret = -EINVAL; 1280 - dev_err(tasdevice->dev, "firmware error\n"); 1281 - goto end; 1282 - } 1283 - 1284 - if ((book == TASDEVICE_BOOK_ID(TAS2781_SA_COEFF_SWAP_REG)) && 1285 - (page == TASDEVICE_PAGE_ID(TAS2781_SA_COEFF_SWAP_REG)) && 1286 - (reg == TASDEVICE_REG_ID(TAS2781_SA_COEFF_SWAP_REG)) && 1287 - (len == 4)) { 1288 - /* DSP swap command, pass */ 1289 - ret = 0; 1290 - goto end; 1291 - } 1292 - 1293 - in = check_yram(&crc_data, book, page, reg, len); 1294 - if (!in) 1295 - goto end; 1296 - 1297 - if (len == 1) { 1298 - dev_err(tasdevice->dev, "firmware error\n"); 1299 - ret = -EINVAL; 1300 - goto end; 1301 - } 1302 - 1303 - ret = tasdevice_spi_dev_bulk_read(tasdevice, 1304 - TASDEVICE_REG(book, page, crc_data.offset), 1305 - nBuf1, crc_data.len); 1306 - if (ret < 0) 1307 - goto end; 1308 - 1309 - for (i = 0; i < crc_data.len; i++) { 1310 - if ((book == TASDEVICE_BOOK_ID(TAS2781_SA_COEFF_SWAP_REG)) && 1311 - (page == TASDEVICE_PAGE_ID(TAS2781_SA_COEFF_SWAP_REG)) && 1312 - ((i + crc_data.offset) >= 1313 - TASDEVICE_REG_ID(TAS2781_SA_COEFF_SWAP_REG)) && 1314 - ((i + crc_data.offset) <= 1315 - (TASDEVICE_REG_ID(TAS2781_SA_COEFF_SWAP_REG) + 4))) 1316 - /* DSP swap command, bypass */ 1317 - continue; 1318 - else 1319 - crc_chksum += crc8(tasdevice->crc8_lkp_tbl, &nBuf1[i], 1320 - 1, 0); 1321 - } 1322 - 1323 - ret = crc_chksum; 1324 - 1325 - end: 1326 - return ret; 1327 - } 1328 - 1329 - /* Checksum for single register. */ 1330 - static int do_singlereg_checksum(struct tasdevice_priv *tasdevice, 1331 - unsigned char book, unsigned char page, 1332 - unsigned char reg, unsigned char val) 1333 - { 1334 - struct tas_crc crc_data; 1335 - unsigned int nData1; 1336 - int ret = 0; 1337 - bool in; 1338 - 1339 - /* DSP swap command, pass */ 1340 - if ((book == TASDEVICE_BOOK_ID(TAS2781_SA_COEFF_SWAP_REG)) && 1341 - (page == TASDEVICE_PAGE_ID(TAS2781_SA_COEFF_SWAP_REG)) && 1342 - (reg >= TASDEVICE_REG_ID(TAS2781_SA_COEFF_SWAP_REG)) && 1343 - (reg <= (TASDEVICE_REG_ID(TAS2781_SA_COEFF_SWAP_REG) + 4))) 1344 - return 0; 1345 - 1346 - in = check_yram(&crc_data, book, page, reg, 1); 1347 - if (!in) 1348 - return 0; 1349 - ret = tasdevice_spi_dev_read(tasdevice, 1350 - TASDEVICE_REG(book, page, reg), &nData1); 1351 - if (ret < 0) 1352 - return ret; 1353 - 1354 - if (nData1 != val) { 1355 - dev_err(tasdevice->dev, 1356 - "B[0x%x]P[0x%x]R[0x%x] W[0x%x], R[0x%x]\n", 1357 - book, page, reg, val, nData1); 1358 - tasdevice->err_code |= ERROR_YRAM_CRCCHK; 1359 - return -EAGAIN; 1360 - } 1361 - 1362 - ret = crc8(tasdevice->crc8_lkp_tbl, &val, 1, 0); 1363 - 1364 - return ret; 1365 - } 1366 - 1367 - /* Block type check. */ 1368 - static void set_err_prg_cfg(unsigned int type, struct tasdevice_priv *p) 1369 - { 1370 - if ((type == MAIN_ALL_DEVICES) || (type == MAIN_DEVICE_A) || 1371 - (type == MAIN_DEVICE_B) || (type == MAIN_DEVICE_C) || 1372 - (type == MAIN_DEVICE_D)) 1373 - p->cur_prog = -1; 1374 - else 1375 - p->cur_conf = -1; 1376 - } 1377 - 1378 - /* Checksum for data bytes. */ 1379 - static int tasdev_bytes_chksum(struct tasdevice_priv *tas_priv, 1380 - struct tasdev_blk *block, unsigned char book, 1381 - unsigned char page, unsigned char reg, unsigned int len, 1382 - unsigned char val, unsigned char *crc_chksum) 1383 - { 1384 - int ret; 1385 - 1386 - if (len > 1) 1387 - ret = tasdev_multibytes_chksum(tas_priv, book, page, reg, 1388 - len); 1389 - else 1390 - ret = do_singlereg_checksum(tas_priv, book, page, reg, val); 1391 - 1392 - if (ret > 0) { 1393 - *crc_chksum += ret; 1394 - goto end; 1395 - } 1396 - 1397 - if (ret != -EAGAIN) 1398 - goto end; 1399 - 1400 - block->nr_retry--; 1401 - if (block->nr_retry > 0) 1402 - goto end; 1403 - 1404 - set_err_prg_cfg(block->type, tas_priv); 1405 - 1406 - end: 1407 - return ret; 1408 - } 1409 - 1410 - /* Multi-data byte write. */ 1411 - static int tasdev_multibytes_wr(struct tasdevice_priv *tas_priv, 1412 - struct tasdev_blk *block, unsigned char book, 1413 - unsigned char page, unsigned char reg, unsigned char *data, 1414 - unsigned int len, unsigned int *nr_cmds, 1415 - unsigned char *crc_chksum) 1416 - { 1417 - int ret; 1418 - 1419 - if (len > 1) { 1420 - ret = tasdevice_spi_dev_bulk_write(tas_priv, 1421 - TASDEVICE_REG(book, page, reg), data + 3, len); 1422 - if (ret < 0) 1423 - return ret; 1424 - if (block->is_ychksum_present) 1425 - ret = tasdev_bytes_chksum(tas_priv, block, 1426 - book, page, reg, len, 0, crc_chksum); 1427 - } else { 1428 - ret = tasdevice_spi_dev_write(tas_priv, 1429 - TASDEVICE_REG(book, page, reg), data[3]); 1430 - if (ret < 0) 1431 - return ret; 1432 - if (block->is_ychksum_present) 1433 - ret = tasdev_bytes_chksum(tas_priv, block, book, 1434 - page, reg, 1, data[3], crc_chksum); 1435 - } 1436 - 1437 - if (!block->is_ychksum_present || ret >= 0) { 1438 - *nr_cmds += 1; 1439 - if (len >= 2) 1440 - *nr_cmds += ((len - 2) / 4) + 1; 1441 - } 1442 - 1443 - return ret; 1444 - } 1445 - 1446 - /* Checksum for block. */ 1447 - static int tasdev_block_chksum(struct tasdevice_priv *tas_priv, 1448 - struct tasdev_blk *block) 1449 - { 1450 - unsigned int nr_value; 1451 - int ret; 1452 - 1453 - ret = tasdevice_spi_dev_read(tas_priv, TASDEVICE_CHECKSUM, &nr_value); 1454 - if (ret < 0) { 1455 - dev_err(tas_priv->dev, "%s: read error %d.\n", __func__, ret); 1456 - set_err_prg_cfg(block->type, tas_priv); 1457 - return ret; 1458 - } 1459 - 1460 - if ((nr_value & 0xff) != block->pchksum) { 1461 - dev_err(tas_priv->dev, "%s: PChkSum err %d ", __func__, ret); 1462 - dev_err(tas_priv->dev, "PChkSum = 0x%x, Reg = 0x%x\n", 1463 - block->pchksum, (nr_value & 0xff)); 1464 - tas_priv->err_code |= ERROR_PRAM_CRCCHK; 1465 - ret = -EAGAIN; 1466 - block->nr_retry--; 1467 - 1468 - if (block->nr_retry <= 0) 1469 - set_err_prg_cfg(block->type, tas_priv); 1470 - } else { 1471 - tas_priv->err_code &= ~ERROR_PRAM_CRCCHK; 1472 - } 1473 - 1474 - return ret; 1475 - } 1476 - 1477 - /* Firmware block load function. */ 1478 - static int tasdev_load_blk(struct tasdevice_priv *tas_priv, 1479 - struct tasdev_blk *block) 1480 - { 1481 - unsigned int sleep_time, len, nr_cmds; 1482 - unsigned char offset, book, page, val; 1483 - unsigned char *data = block->data; 1484 - unsigned char crc_chksum = 0; 1485 - int ret = 0; 1486 - 1487 - while (block->nr_retry > 0) { 1488 - if (block->is_pchksum_present) { 1489 - ret = tasdevice_spi_dev_write(tas_priv, 1490 - TASDEVICE_CHECKSUM, 0); 1491 - if (ret < 0) 1492 - break; 1493 - } 1494 - 1495 - if (block->is_ychksum_present) 1496 - crc_chksum = 0; 1497 - 1498 - nr_cmds = 0; 1499 - 1500 - while (nr_cmds < block->nr_cmds) { 1501 - data = block->data + nr_cmds * 4; 1502 - 1503 - book = data[0]; 1504 - page = data[1]; 1505 - offset = data[2]; 1506 - val = data[3]; 1507 - 1508 - nr_cmds++; 1509 - /* Single byte write */ 1510 - if (offset <= 0x7F) { 1511 - ret = tasdevice_spi_dev_write(tas_priv, 1512 - TASDEVICE_REG(book, page, offset), 1513 - val); 1514 - if (ret < 0) 1515 - break; 1516 - if (block->is_ychksum_present) { 1517 - ret = tasdev_bytes_chksum(tas_priv, 1518 - block, book, page, offset, 1519 - 1, val, &crc_chksum); 1520 - if (ret < 0) 1521 - break; 1522 - } 1523 - continue; 1524 - } 1525 - /* sleep command */ 1526 - if (offset == 0x81) { 1527 - /* book -- data[0] page -- data[1] */ 1528 - sleep_time = ((book << 8) + page)*1000; 1529 - fsleep(sleep_time); 1530 - continue; 1531 - } 1532 - /* Multiple bytes write */ 1533 - if (offset == 0x85) { 1534 - data += 4; 1535 - len = (book << 8) + page; 1536 - book = data[0]; 1537 - page = data[1]; 1538 - offset = data[2]; 1539 - ret = tasdev_multibytes_wr(tas_priv, 1540 - block, book, page, offset, data, 1541 - len, &nr_cmds, &crc_chksum); 1542 - if (ret < 0) 1543 - break; 1544 - } 1545 - } 1546 - if (ret == -EAGAIN) { 1547 - if (block->nr_retry > 0) 1548 - continue; 1549 - } else if (ret < 0) { 1550 - /* err in current device, skip it */ 1551 - break; 1552 - } 1553 - 1554 - if (block->is_pchksum_present) { 1555 - ret = tasdev_block_chksum(tas_priv, block); 1556 - if (ret == -EAGAIN) { 1557 - if (block->nr_retry > 0) 1558 - continue; 1559 - } else if (ret < 0) { 1560 - /* err in current device, skip it */ 1561 - break; 1562 - } 1563 - } 1564 - 1565 - if (block->is_ychksum_present) { 1566 - /* TBD, open it when FW ready */ 1567 - dev_err(tas_priv->dev, 1568 - "Blk YChkSum: FW = 0x%x, YCRC = 0x%x\n", 1569 - block->ychksum, crc_chksum); 1570 - 1571 - tas_priv->err_code &= 1572 - ~ERROR_YRAM_CRCCHK; 1573 - ret = 0; 1574 - } 1575 - /* skip current blk */ 1576 - break; 1577 - } 1578 - 1579 - return ret; 1580 - } 1581 - 1582 - /* Firmware block load function. */ 1583 - static int tasdevice_load_block(struct tasdevice_priv *tas_priv, 1584 - struct tasdev_blk *block) 1585 - { 1586 - int ret = 0; 1587 - 1588 - block->nr_retry = 6; 1589 - if (tas_priv->is_loading == false) 1590 - return 0; 1591 - ret = tasdev_load_blk(tas_priv, block); 1592 - if (ret < 0) 1593 - dev_err(tas_priv->dev, "Blk (%d) load error\n", block->type); 1594 - 1595 - return ret; 1596 - } 1597 - 1598 - /* 1599 - * Select firmware binary parser & load callback functions by ppc3 version 1600 - * and firmware binary version. 1601 - */ 1602 - static int dspfw_default_callback(struct tasdevice_priv *tas_priv, 1603 - unsigned int drv_ver, unsigned int ppcver) 1604 - { 1605 - int rc = 0; 1606 - 1607 - if (drv_ver == 0x100) { 1608 - if (ppcver >= PPC3_VERSION_BASE) { 1609 - tas_priv->fw_parse_variable_header = 1610 - fw_parse_variable_header_kernel; 1611 - tas_priv->fw_parse_program_data = 1612 - fw_parse_program_data_kernel; 1613 - tas_priv->fw_parse_configuration_data = 1614 - fw_parse_configuration_data_kernel; 1615 - tas_priv->tasdevice_load_block = 1616 - tasdevice_load_block_kernel; 1617 - } else if (ppcver == 0x00) { 1618 - tas_priv->fw_parse_variable_header = 1619 - fw_parse_variable_header_git; 1620 - tas_priv->fw_parse_program_data = 1621 - fw_parse_program_data; 1622 - tas_priv->fw_parse_configuration_data = 1623 - fw_parse_configuration_data; 1624 - tas_priv->tasdevice_load_block = 1625 - tasdevice_load_block; 1626 - } else { 1627 - dev_err(tas_priv->dev, 1628 - "Wrong PPCVer :0x%08x\n", ppcver); 1629 - rc = -EINVAL; 1630 - } 1631 - } else { 1632 - dev_err(tas_priv->dev, "Wrong DrvVer : 0x%02x\n", drv_ver); 1633 - rc = -EINVAL; 1634 - } 1635 - 1636 - return rc; 1637 - } 1638 - 1639 - /* DSP firmware binary file header parser function. */ 1640 - static int fw_parse_header(struct tasdevice_priv *tas_priv, 1641 - struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset) 1642 - { 1643 - struct tasdevice_dspfw_hdr *fw_hdr = &tas_fmw->fw_hdr; 1644 - struct tasdevice_fw_fixed_hdr *fw_fixed_hdr = &fw_hdr->fixed_hdr; 1645 - static const unsigned char magic_number[] = {0x35, 0x35, 0x35, 0x32, }; 1646 - const unsigned char *buf = (unsigned char *)fmw->data; 1647 - 1648 - if (offset + 92 > fmw->size) { 1649 - dev_err(tas_priv->dev, "%s: File Size error\n", __func__); 1650 - offset = -EINVAL; 1651 - goto out; 1652 - } 1653 - if (memcmp(&buf[offset], magic_number, 4)) { 1654 - dev_err(tas_priv->dev, "%s: Magic num NOT match\n", __func__); 1655 - offset = -EINVAL; 1656 - goto out; 1657 - } 1658 - offset += 4; 1659 - 1660 - /* 1661 - * Convert data[offset], data[offset + 1], data[offset + 2] and 1662 - * data[offset + 3] into host 1663 - */ 1664 - fw_fixed_hdr->fwsize = get_unaligned_be32(&buf[offset]); 1665 - offset += 4; 1666 - if (fw_fixed_hdr->fwsize != fmw->size) { 1667 - dev_err(tas_priv->dev, "File size not match, %lu %u", 1668 - (unsigned long)fmw->size, fw_fixed_hdr->fwsize); 1669 - offset = -EINVAL; 1670 - goto out; 1671 - } 1672 - offset += 4; 1673 - fw_fixed_hdr->ppcver = get_unaligned_be32(&buf[offset]); 1674 - offset += 8; 1675 - fw_fixed_hdr->drv_ver = get_unaligned_be32(&buf[offset]); 1676 - offset += 72; 1677 - 1678 - out: 1679 - return offset; 1680 - } 1681 - 1682 - /* DSP firmware binary file parser function. */ 1683 - static int tasdevice_dspfw_ready(const struct firmware *fmw, void *context) 1684 - { 1685 - struct tasdevice_priv *tas_priv = context; 1686 - struct tasdevice_fw_fixed_hdr *fw_fixed_hdr; 1687 - struct tasdevice_fw *tas_fmw; 1688 - int offset = 0, ret = 0; 1689 - 1690 - if (!fmw || !fmw->data) { 1691 - dev_err(tas_priv->dev, "%s: Failed to read firmware %s\n", 1692 - __func__, tas_priv->coef_binaryname); 1693 - return -EINVAL; 1694 - } 1695 - 1696 - tas_priv->fmw = kzalloc(sizeof(*tas_priv->fmw), GFP_KERNEL); 1697 - if (!tas_priv->fmw) 1698 - return -ENOMEM; 1699 - tas_fmw = tas_priv->fmw; 1700 - tas_fmw->dev = tas_priv->dev; 1701 - offset = fw_parse_header(tas_priv, tas_fmw, fmw, offset); 1702 - 1703 - if (offset == -EINVAL) 1704 - return -EINVAL; 1705 - 1706 - fw_fixed_hdr = &tas_fmw->fw_hdr.fixed_hdr; 1707 - /* Support different versions of firmware */ 1708 - switch (fw_fixed_hdr->drv_ver) { 1709 - case 0x301: 1710 - case 0x302: 1711 - case 0x502: 1712 - case 0x503: 1713 - tas_priv->fw_parse_variable_header = 1714 - fw_parse_variable_header_kernel; 1715 - tas_priv->fw_parse_program_data = 1716 - fw_parse_program_data_kernel; 1717 - tas_priv->fw_parse_configuration_data = 1718 - fw_parse_configuration_data_kernel; 1719 - tas_priv->tasdevice_load_block = 1720 - tasdevice_load_block_kernel; 1721 - break; 1722 - case 0x202: 1723 - case 0x400: 1724 - tas_priv->fw_parse_variable_header = 1725 - fw_parse_variable_header_git; 1726 - tas_priv->fw_parse_program_data = 1727 - fw_parse_program_data; 1728 - tas_priv->fw_parse_configuration_data = 1729 - fw_parse_configuration_data; 1730 - tas_priv->tasdevice_load_block = 1731 - tasdevice_load_block; 1732 - break; 1733 - default: 1734 - ret = dspfw_default_callback(tas_priv, 1735 - fw_fixed_hdr->drv_ver, fw_fixed_hdr->ppcver); 1736 - if (ret) 1737 - return ret; 1738 - break; 1739 - } 1740 - 1741 - offset = tas_priv->fw_parse_variable_header(tas_priv, fmw, offset); 1742 - if (offset < 0) 1743 - return offset; 1744 - 1745 - offset = tas_priv->fw_parse_program_data(tas_priv, tas_fmw, fmw, 1746 - offset); 1747 - if (offset < 0) 1748 - return offset; 1749 - 1750 - offset = tas_priv->fw_parse_configuration_data(tas_priv, 1751 - tas_fmw, fmw, offset); 1752 - if (offset < 0) 1753 - ret = offset; 1754 - 1755 - return ret; 1756 - } 1757 - 1758 - /* DSP firmware binary file parser function. */ 1759 - int tasdevice_spi_dsp_parser(void *context) 1760 - { 1761 - struct tasdevice_priv *tas_priv = context; 1762 - const struct firmware *fw_entry; 1763 - int ret; 1764 - 1765 - ret = request_firmware(&fw_entry, tas_priv->coef_binaryname, 1766 - tas_priv->dev); 1767 - if (ret) { 1768 - dev_err(tas_priv->dev, "%s: load %s error\n", __func__, 1769 - tas_priv->coef_binaryname); 1770 - return ret; 1771 - } 1772 - 1773 - ret = tasdevice_dspfw_ready(fw_entry, tas_priv); 1774 - release_firmware(fw_entry); 1775 - fw_entry = NULL; 1776 - 1777 - return ret; 1778 - } 1779 - 1780 - /* DSP firmware program block data remove function. */ 1781 - static void tasdev_dsp_prog_blk_remove(struct tasdevice_prog *prog) 1782 - { 1783 - struct tasdevice_data *tas_dt; 1784 - struct tasdev_blk *blk; 1785 - unsigned int i; 1786 - 1787 - if (!prog) 1788 - return; 1789 - 1790 - tas_dt = &prog->dev_data; 1791 - 1792 - if (!tas_dt->dev_blks) 1793 - return; 1794 - 1795 - for (i = 0; i < tas_dt->nr_blk; i++) { 1796 - blk = &tas_dt->dev_blks[i]; 1797 - kfree(blk->data); 1798 - } 1799 - kfree(tas_dt->dev_blks); 1800 - } 1801 - 1802 - /* DSP firmware program block data remove function. */ 1803 - static void tasdev_dsp_prog_remove(struct tasdevice_prog *prog, 1804 - unsigned short nr) 1805 - { 1806 - int i; 1807 - 1808 - for (i = 0; i < nr; i++) 1809 - tasdev_dsp_prog_blk_remove(&prog[i]); 1810 - kfree(prog); 1811 - } 1812 - 1813 - /* DSP firmware config block data remove function. */ 1814 - static void tasdev_dsp_cfg_blk_remove(struct tasdevice_config *cfg) 1815 - { 1816 - struct tasdevice_data *tas_dt; 1817 - struct tasdev_blk *blk; 1818 - unsigned int i; 1819 - 1820 - if (cfg) { 1821 - tas_dt = &cfg->dev_data; 1822 - 1823 - if (!tas_dt->dev_blks) 1824 - return; 1825 - 1826 - for (i = 0; i < tas_dt->nr_blk; i++) { 1827 - blk = &tas_dt->dev_blks[i]; 1828 - kfree(blk->data); 1829 - } 1830 - kfree(tas_dt->dev_blks); 1831 - } 1832 - } 1833 - 1834 - /* DSP firmware config remove function. */ 1835 - static void tasdev_dsp_cfg_remove(struct tasdevice_config *config, 1836 - unsigned short nr) 1837 - { 1838 - int i; 1839 - 1840 - for (i = 0; i < nr; i++) 1841 - tasdev_dsp_cfg_blk_remove(&config[i]); 1842 - kfree(config); 1843 - } 1844 - 1845 - /* DSP firmware remove function. */ 1846 - void tasdevice_spi_dsp_remove(void *context) 1847 - { 1848 - struct tasdevice_priv *tas_dev = context; 1849 - 1850 - if (!tas_dev->fmw) 1851 - return; 1852 - 1853 - if (tas_dev->fmw->programs) 1854 - tasdev_dsp_prog_remove(tas_dev->fmw->programs, 1855 - tas_dev->fmw->nr_programs); 1856 - if (tas_dev->fmw->configs) 1857 - tasdev_dsp_cfg_remove(tas_dev->fmw->configs, 1858 - tas_dev->fmw->nr_configurations); 1859 - kfree(tas_dev->fmw); 1860 - tas_dev->fmw = NULL; 1861 - } 1862 - 1863 - /* DSP firmware calibration data remove function. */ 1864 - static void tas2781_clear_calfirmware(struct tasdevice_fw *tas_fmw) 1865 - { 1866 - struct tasdevice_calibration *calibration; 1867 - struct tasdev_blk *block; 1868 - unsigned int blks; 1869 - int i; 1870 - 1871 - if (!tas_fmw->calibrations) 1872 - goto out; 1873 - 1874 - for (i = 0; i < tas_fmw->nr_calibrations; i++) { 1875 - calibration = &tas_fmw->calibrations[i]; 1876 - if (!calibration) 1877 - continue; 1878 - 1879 - if (!calibration->dev_data.dev_blks) 1880 - continue; 1881 - 1882 - for (blks = 0; blks < calibration->dev_data.nr_blk; blks++) { 1883 - block = &calibration->dev_data.dev_blks[blks]; 1884 - if (!block) 1885 - continue; 1886 - kfree(block->data); 1887 - } 1888 - kfree(calibration->dev_data.dev_blks); 1889 - } 1890 - kfree(tas_fmw->calibrations); 1891 - out: 1892 - kfree(tas_fmw); 1893 - } 1894 - 1895 - /* Calibration data from firmware remove function. */ 1896 - void tasdevice_spi_calbin_remove(void *context) 1897 - { 1898 - struct tasdevice_priv *tas_priv = context; 1899 - 1900 - if (tas_priv->cali_data_fmw) { 1901 - tas2781_clear_calfirmware(tas_priv->cali_data_fmw); 1902 - tas_priv->cali_data_fmw = NULL; 1903 - } 1904 - } 1905 - 1906 - /* Configuration remove function. */ 1907 - void tasdevice_spi_config_info_remove(void *context) 1908 - { 1909 - struct tasdevice_priv *tas_priv = context; 1910 - struct tasdevice_rca *rca = &tas_priv->rcabin; 1911 - struct tasdevice_config_info **ci = rca->cfg_info; 1912 - unsigned int i, j; 1913 - 1914 - if (!ci) 1915 - return; 1916 - for (i = 0; i < rca->ncfgs; i++) { 1917 - if (!ci[i]) 1918 - continue; 1919 - if (ci[i]->blk_data) { 1920 - for (j = 0; j < ci[i]->real_nblocks; j++) { 1921 - if (!ci[i]->blk_data[j]) 1922 - continue; 1923 - kfree(ci[i]->blk_data[j]->regdata); 1924 - kfree(ci[i]->blk_data[j]); 1925 - } 1926 - kfree(ci[i]->blk_data); 1927 - } 1928 - kfree(ci[i]); 1929 - } 1930 - kfree(ci); 1931 - } 1932 - 1933 - /* DSP firmware program block data load function. */ 1934 - static int tasdevice_load_data(struct tasdevice_priv *tas_priv, 1935 - struct tasdevice_data *dev_data) 1936 - { 1937 - struct tasdev_blk *block; 1938 - unsigned int i; 1939 - int ret = 0; 1940 - 1941 - for (i = 0; i < dev_data->nr_blk; i++) { 1942 - block = &dev_data->dev_blks[i]; 1943 - ret = tas_priv->tasdevice_load_block(tas_priv, block); 1944 - if (ret < 0) 1945 - break; 1946 - } 1947 - 1948 - return ret; 1949 - } 1950 - 1951 - /* DSP firmware program load interface function. */ 1952 - int tasdevice_spi_prmg_load(void *context, int prm_no) 1953 - { 1954 - struct tasdevice_priv *tas_priv = context; 1955 - struct tasdevice_fw *tas_fmw = tas_priv->fmw; 1956 - struct tasdevice_prog *program; 1957 - struct tasdevice_config *conf; 1958 - int ret = 0; 1959 - 1960 - if (!tas_fmw) { 1961 - dev_err(tas_priv->dev, "%s: Firmware is NULL\n", __func__); 1962 - return -EINVAL; 1963 - } 1964 - if (prm_no >= 0 && prm_no <= tas_fmw->nr_programs) { 1965 - tas_priv->cur_conf = 0; 1966 - tas_priv->is_loading = true; 1967 - program = &tas_fmw->programs[prm_no]; 1968 - ret = tasdevice_load_data(tas_priv, &program->dev_data); 1969 - if (ret < 0) { 1970 - dev_err(tas_priv->dev, "Program failed %d.\n", ret); 1971 - return ret; 1972 - } 1973 - tas_priv->cur_prog = prm_no; 1974 - 1975 - conf = &tas_fmw->configs[tas_priv->cur_conf]; 1976 - ret = tasdevice_load_data(tas_priv, &conf->dev_data); 1977 - if (ret < 0) 1978 - dev_err(tas_priv->dev, "Config failed %d.\n", ret); 1979 - } else { 1980 - dev_err(tas_priv->dev, 1981 - "%s: prm(%d) is not in range of Programs %u\n", 1982 - __func__, prm_no, tas_fmw->nr_programs); 1983 - return -EINVAL; 1984 - } 1985 - 1986 - return ret; 1987 - } 1988 - 1989 - /* RCABIN configuration switch interface function. */ 1990 - void tasdevice_spi_tuning_switch(void *context, int state) 1991 - { 1992 - struct tasdevice_priv *tas_priv = context; 1993 - int profile_cfg_id = tas_priv->rcabin.profile_cfg_id; 1994 - 1995 - if (tas_priv->fw_state == TASDEVICE_DSP_FW_FAIL) { 1996 - dev_err(tas_priv->dev, "DSP bin file not loaded\n"); 1997 - return; 1998 - } 1999 - 2000 - if (state == 0) 2001 - tasdevice_spi_select_cfg_blk(tas_priv, profile_cfg_id, 2002 - TASDEVICE_BIN_BLK_PRE_POWER_UP); 2003 - else 2004 - tasdevice_spi_select_cfg_blk(tas_priv, profile_cfg_id, 2005 - TASDEVICE_BIN_BLK_PRE_SHUTDOWN); 2006 - }
+6 -2
sound/soc/codecs/Kconfig
··· 260 260 imply SND_SOC_TAS2770 261 261 imply SND_SOC_TAS2780 262 262 imply SND_SOC_TAS2781_COMLIB 263 + imply SND_SOC_TAS2781_COMLIB_I2C 263 264 imply SND_SOC_TAS2781_FMWLIB 264 265 imply SND_SOC_TAS2781_I2C 265 266 imply SND_SOC_TAS5086 ··· 1992 1991 digital input mono Class-D audio power amplifiers. 1993 1992 1994 1993 config SND_SOC_TAS2781_COMLIB 1994 + tristate 1995 + 1996 + config SND_SOC_TAS2781_COMLIB_I2C 1995 1997 depends on I2C 1996 1998 select CRC8 1997 1999 select REGMAP_I2C 1998 2000 tristate 1999 2001 2000 2002 config SND_SOC_TAS2781_FMWLIB 2001 - depends on SND_SOC_TAS2781_COMLIB 2003 + select SND_SOC_TAS2781_COMLIB 2002 2004 tristate 2003 2005 default n 2004 2006 2005 2007 config SND_SOC_TAS2781_I2C 2006 2008 tristate "Texas Instruments TAS2781 speaker amplifier based on I2C" 2007 2009 depends on I2C 2008 - select SND_SOC_TAS2781_COMLIB 2010 + select SND_SOC_TAS2781_COMLIB_I2C 2009 2011 select SND_SOC_TAS2781_FMWLIB 2010 2012 help 2011 2013 Enable support for Texas Instruments TAS2781 Smart Amplifier
+2
sound/soc/codecs/Makefile
··· 305 305 snd-soc-tda7419-y := tda7419.o 306 306 snd-soc-tas2770-y := tas2770.o 307 307 snd-soc-tas2781-comlib-y := tas2781-comlib.o 308 + snd-soc-tas2781-comlib-i2c-y := tas2781-comlib-i2c.o 308 309 snd-soc-tas2781-fmwlib-y := tas2781-fmwlib.o 309 310 snd-soc-tas2781-i2c-y := tas2781-i2c.o 310 311 snd-soc-tfa9879-y := tfa9879.o ··· 714 713 obj-$(CONFIG_SND_SOC_TAS2764) += snd-soc-tas2764.o 715 714 obj-$(CONFIG_SND_SOC_TAS2780) += snd-soc-tas2780.o 716 715 obj-$(CONFIG_SND_SOC_TAS2781_COMLIB) += snd-soc-tas2781-comlib.o 716 + obj-$(CONFIG_SND_SOC_TAS2781_COMLIB_I2C) += snd-soc-tas2781-comlib-i2c.o 717 717 obj-$(CONFIG_SND_SOC_TAS2781_FMWLIB) += snd-soc-tas2781-fmwlib.o 718 718 obj-$(CONFIG_SND_SOC_TAS2781_I2C) += snd-soc-tas2781-i2c.o 719 719 obj-$(CONFIG_SND_SOC_TAS5086) += snd-soc-tas5086.o
+375
sound/soc/codecs/tas2781-comlib-i2c.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + // 3 + // TAS2563/TAS2781 Common functions for HDA and ASoC Audio drivers based on I2C 4 + // 5 + // Copyright 2025 Texas Instruments, Inc. 6 + // 7 + // Author: Shenghao Ding <shenghao-ding@ti.com> 8 + 9 + #include <linux/crc8.h> 10 + #include <linux/firmware.h> 11 + #include <linux/gpio/consumer.h> 12 + #include <linux/i2c.h> 13 + #include <linux/init.h> 14 + #include <linux/interrupt.h> 15 + #include <linux/module.h> 16 + #include <linux/of.h> 17 + #include <linux/of_irq.h> 18 + #include <linux/regmap.h> 19 + #include <linux/slab.h> 20 + #include <sound/pcm_params.h> 21 + #include <sound/soc.h> 22 + #include <sound/tas2781.h> 23 + #include <sound/tas2781-comlib-i2c.h> 24 + 25 + static const struct regmap_range_cfg tasdevice_ranges[] = { 26 + { 27 + .range_min = 0, 28 + .range_max = 256 * 128, 29 + .selector_reg = TASDEVICE_PAGE_SELECT, 30 + .selector_mask = 0xff, 31 + .selector_shift = 0, 32 + .window_start = 0, 33 + .window_len = 128, 34 + }, 35 + }; 36 + 37 + static const struct regmap_config tasdevice_regmap = { 38 + .reg_bits = 8, 39 + .val_bits = 8, 40 + .cache_type = REGCACHE_NONE, 41 + .ranges = tasdevice_ranges, 42 + .num_ranges = ARRAY_SIZE(tasdevice_ranges), 43 + .max_register = 256 * 128, 44 + }; 45 + 46 + static int tasdevice_change_chn_book(struct tasdevice_priv *tas_priv, 47 + unsigned short chn, int book) 48 + { 49 + struct i2c_client *client = (struct i2c_client *)tas_priv->client; 50 + int ret = 0; 51 + 52 + if (chn < tas_priv->ndev) { 53 + struct tasdevice *tasdev = &tas_priv->tasdevice[chn]; 54 + struct regmap *map = tas_priv->regmap; 55 + 56 + if (client->addr != tasdev->dev_addr) { 57 + client->addr = tasdev->dev_addr; 58 + /* All tas2781s share the same regmap, clear the page 59 + * inside regmap once switching to another tas2781. 60 + * Register 0 at any pages and any books inside tas2781 61 + * is the same one for page-switching. 62 + */ 63 + ret = regmap_write(map, TASDEVICE_PAGE_SELECT, 0); 64 + if (ret < 0) { 65 + dev_err(tas_priv->dev, "%s, E=%d channel:%d\n", 66 + __func__, ret, chn); 67 + goto out; 68 + } 69 + } 70 + 71 + if (tasdev->cur_book != book) { 72 + ret = regmap_write(map, TASDEVICE_BOOKCTL_REG, book); 73 + if (ret < 0) { 74 + dev_err(tas_priv->dev, "%s, E=%d\n", 75 + __func__, ret); 76 + goto out; 77 + } 78 + tasdev->cur_book = book; 79 + } 80 + } else { 81 + ret = -EINVAL; 82 + dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__, 83 + chn); 84 + } 85 + 86 + out: 87 + return ret; 88 + } 89 + 90 + int tasdev_chn_switch(struct tasdevice_priv *tas_priv, 91 + unsigned short chn) 92 + { 93 + struct i2c_client *client = (struct i2c_client *)tas_priv->client; 94 + struct tasdevice *tasdev = &tas_priv->tasdevice[chn]; 95 + struct regmap *map = tas_priv->regmap; 96 + int ret; 97 + 98 + if (client->addr != tasdev->dev_addr) { 99 + client->addr = tasdev->dev_addr; 100 + /* All devices share the same regmap, clear the page 101 + * inside regmap once switching to another device. 102 + * Register 0 at any pages and any books inside tas2781 103 + * is the same one for page-switching. 104 + */ 105 + ret = regmap_write(map, TASDEVICE_PAGE_SELECT, 0); 106 + if (ret < 0) { 107 + dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 108 + return ret; 109 + } 110 + return 1; 111 + } 112 + return 0; 113 + } 114 + EXPORT_SYMBOL_GPL(tasdev_chn_switch); 115 + 116 + int tasdevice_dev_update_bits( 117 + struct tasdevice_priv *tas_priv, unsigned short chn, 118 + unsigned int reg, unsigned int mask, unsigned int value) 119 + { 120 + int ret = 0; 121 + 122 + if (chn < tas_priv->ndev) { 123 + struct regmap *map = tas_priv->regmap; 124 + 125 + ret = tas_priv->change_chn_book(tas_priv, chn, 126 + TASDEVICE_BOOK_ID(reg)); 127 + if (ret < 0) 128 + goto out; 129 + 130 + ret = regmap_update_bits(map, TASDEVICE_PGRG(reg), 131 + mask, value); 132 + if (ret < 0) 133 + dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 134 + } else { 135 + dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__, 136 + chn); 137 + ret = -EINVAL; 138 + } 139 + 140 + out: 141 + return ret; 142 + } 143 + EXPORT_SYMBOL_GPL(tasdevice_dev_update_bits); 144 + 145 + struct tasdevice_priv *tasdevice_kzalloc(struct i2c_client *i2c) 146 + { 147 + struct tasdevice_priv *tas_priv; 148 + 149 + tas_priv = devm_kzalloc(&i2c->dev, sizeof(*tas_priv), GFP_KERNEL); 150 + if (!tas_priv) 151 + return NULL; 152 + tas_priv->dev = &i2c->dev; 153 + tas_priv->client = (void *)i2c; 154 + 155 + return tas_priv; 156 + } 157 + EXPORT_SYMBOL_GPL(tasdevice_kzalloc); 158 + 159 + int tasdevice_init(struct tasdevice_priv *tas_priv) 160 + { 161 + int ret = 0; 162 + int i; 163 + 164 + tas_priv->regmap = devm_regmap_init_i2c(tas_priv->client, 165 + &tasdevice_regmap); 166 + if (IS_ERR(tas_priv->regmap)) { 167 + ret = PTR_ERR(tas_priv->regmap); 168 + dev_err(tas_priv->dev, "Failed to allocate register map: %d\n", 169 + ret); 170 + goto out; 171 + } 172 + 173 + tas_priv->cur_prog = -1; 174 + tas_priv->cur_conf = -1; 175 + tas_priv->isspi = false; 176 + 177 + for (i = 0; i < tas_priv->ndev; i++) { 178 + tas_priv->tasdevice[i].cur_book = -1; 179 + tas_priv->tasdevice[i].cur_prog = -1; 180 + tas_priv->tasdevice[i].cur_conf = -1; 181 + } 182 + 183 + tas_priv->update_bits = tasdevice_dev_update_bits; 184 + tas_priv->change_chn_book = tasdevice_change_chn_book; 185 + tas_priv->dev_read = tasdevice_dev_read; 186 + tas_priv->dev_bulk_read = tasdevice_dev_bulk_read; 187 + 188 + mutex_init(&tas_priv->codec_lock); 189 + 190 + out: 191 + return ret; 192 + } 193 + EXPORT_SYMBOL_GPL(tasdevice_init); 194 + 195 + static int tasdevice_clamp(int val, int max, unsigned int invert) 196 + { 197 + if (val > max) 198 + val = max; 199 + if (invert) 200 + val = max - val; 201 + if (val < 0) 202 + val = 0; 203 + return val; 204 + } 205 + 206 + int tasdevice_amp_putvol(struct tasdevice_priv *tas_priv, 207 + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 208 + { 209 + unsigned int invert = mc->invert; 210 + unsigned char mask; 211 + int max = mc->max; 212 + int err_cnt = 0; 213 + int val, i, ret; 214 + 215 + mask = (1 << fls(max)) - 1; 216 + mask <<= mc->shift; 217 + val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert); 218 + for (i = 0; i < tas_priv->ndev; i++) { 219 + ret = tasdevice_dev_update_bits(tas_priv, i, 220 + mc->reg, mask, (unsigned int)(val << mc->shift)); 221 + if (!ret) 222 + continue; 223 + err_cnt++; 224 + dev_err(tas_priv->dev, "set AMP vol error in dev %d\n", i); 225 + } 226 + 227 + /* All the devices set error, return 0 */ 228 + return (err_cnt == tas_priv->ndev) ? 0 : 1; 229 + } 230 + EXPORT_SYMBOL_GPL(tasdevice_amp_putvol); 231 + 232 + int tasdevice_amp_getvol(struct tasdevice_priv *tas_priv, 233 + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 234 + { 235 + unsigned int invert = mc->invert; 236 + unsigned char mask = 0; 237 + int max = mc->max; 238 + int ret = 0; 239 + int val; 240 + 241 + /* Read the primary device */ 242 + ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val); 243 + if (ret) { 244 + dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__); 245 + goto out; 246 + } 247 + 248 + mask = (1 << fls(max)) - 1; 249 + mask <<= mc->shift; 250 + val = (val & mask) >> mc->shift; 251 + val = tasdevice_clamp(val, max, invert); 252 + ucontrol->value.integer.value[0] = val; 253 + 254 + out: 255 + return ret; 256 + 257 + } 258 + EXPORT_SYMBOL_GPL(tasdevice_amp_getvol); 259 + 260 + int tasdevice_digital_getvol(struct tasdevice_priv *tas_priv, 261 + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 262 + { 263 + unsigned int invert = mc->invert; 264 + int max = mc->max; 265 + int ret, val; 266 + 267 + /* Read the primary device as the whole */ 268 + ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val); 269 + if (ret) { 270 + dev_err(tas_priv->dev, "%s, get digital vol error\n", 271 + __func__); 272 + goto out; 273 + } 274 + 275 + val = tasdevice_clamp(val, max, invert); 276 + ucontrol->value.integer.value[0] = val; 277 + 278 + out: 279 + return ret; 280 + 281 + } 282 + EXPORT_SYMBOL_GPL(tasdevice_digital_getvol); 283 + 284 + int tasdevice_digital_putvol(struct tasdevice_priv *tas_priv, 285 + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 286 + { 287 + unsigned int invert = mc->invert; 288 + int max = mc->max; 289 + int err_cnt = 0; 290 + int ret; 291 + int val, i; 292 + 293 + val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert); 294 + 295 + for (i = 0; i < tas_priv->ndev; i++) { 296 + ret = tasdevice_dev_write(tas_priv, i, mc->reg, 297 + (unsigned int)val); 298 + if (!ret) 299 + continue; 300 + err_cnt++; 301 + dev_err(tas_priv->dev, 302 + "set digital vol err in dev %d\n", i); 303 + } 304 + 305 + /* All the devices set error, return 0 */ 306 + return (err_cnt == tas_priv->ndev) ? 0 : 1; 307 + 308 + } 309 + EXPORT_SYMBOL_GPL(tasdevice_digital_putvol); 310 + 311 + void tasdevice_reset(struct tasdevice_priv *tas_dev) 312 + { 313 + int ret, i; 314 + 315 + if (tas_dev->reset) { 316 + gpiod_set_value_cansleep(tas_dev->reset, 0); 317 + usleep_range(500, 1000); 318 + gpiod_set_value_cansleep(tas_dev->reset, 1); 319 + } else { 320 + for (i = 0; i < tas_dev->ndev; i++) { 321 + ret = tasdevice_dev_write(tas_dev, i, 322 + TASDEVICE_REG_SWRESET, 323 + TASDEVICE_REG_SWRESET_RESET); 324 + if (ret < 0) 325 + dev_err(tas_dev->dev, 326 + "dev %d swreset fail, %d\n", 327 + i, ret); 328 + } 329 + } 330 + usleep_range(1000, 1050); 331 + } 332 + EXPORT_SYMBOL_GPL(tasdevice_reset); 333 + 334 + int tascodec_init(struct tasdevice_priv *tas_priv, void *codec, 335 + struct module *module, 336 + void (*cont)(const struct firmware *fw, void *context)) 337 + { 338 + int ret = 0; 339 + 340 + /* Codec Lock Hold to ensure that codec_probe and firmware parsing and 341 + * loading do not simultaneously execute. 342 + */ 343 + mutex_lock(&tas_priv->codec_lock); 344 + 345 + if (tas_priv->name_prefix) 346 + scnprintf(tas_priv->rca_binaryname, 64, "%s-%sRCA%d.bin", 347 + tas_priv->name_prefix, tas_priv->dev_name, 348 + tas_priv->ndev); 349 + else 350 + scnprintf(tas_priv->rca_binaryname, 64, "%sRCA%d.bin", 351 + tas_priv->dev_name, tas_priv->ndev); 352 + crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL); 353 + tas_priv->codec = codec; 354 + ret = request_firmware_nowait(module, FW_ACTION_UEVENT, 355 + tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv, 356 + cont); 357 + if (ret) 358 + dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n", 359 + ret); 360 + 361 + /* Codec Lock Release*/ 362 + mutex_unlock(&tas_priv->codec_lock); 363 + return ret; 364 + } 365 + EXPORT_SYMBOL_GPL(tascodec_init); 366 + 367 + void tasdevice_remove(struct tasdevice_priv *tas_priv) 368 + { 369 + mutex_destroy(&tas_priv->codec_lock); 370 + } 371 + EXPORT_SYMBOL_GPL(tasdevice_remove); 372 + 373 + MODULE_DESCRIPTION("TAS2781 common library for I2C"); 374 + MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>"); 375 + MODULE_LICENSE("GPL");
+40 -386
sound/soc/codecs/tas2781-comlib.c
··· 2 2 // 3 3 // TAS2563/TAS2781 Common functions for HDA and ASoC Audio drivers 4 4 // 5 - // Copyright 2023 - 2024 Texas Instruments, Inc. 5 + // Copyright 2023 - 2025 Texas Instruments, Inc. 6 6 // 7 7 // Author: Shenghao Ding <shenghao-ding@ti.com> 8 8 9 9 #include <linux/crc8.h> 10 + #include <linux/dev_printk.h> 10 11 #include <linux/firmware.h> 11 12 #include <linux/gpio/consumer.h> 12 - #include <linux/i2c.h> 13 13 #include <linux/init.h> 14 14 #include <linux/interrupt.h> 15 15 #include <linux/module.h> ··· 17 17 #include <linux/of_irq.h> 18 18 #include <linux/regmap.h> 19 19 #include <linux/slab.h> 20 - #include <sound/pcm_params.h> 21 - #include <sound/soc.h> 22 20 #include <sound/tas2781.h> 23 - 24 - #define TASDEVICE_CRC8_POLYNOMIAL 0x4d 25 - 26 - static const struct regmap_range_cfg tasdevice_ranges[] = { 27 - { 28 - .range_min = 0, 29 - .range_max = 256 * 128, 30 - .selector_reg = TASDEVICE_PAGE_SELECT, 31 - .selector_mask = 0xff, 32 - .selector_shift = 0, 33 - .window_start = 0, 34 - .window_len = 128, 35 - }, 36 - }; 37 - 38 - static const struct regmap_config tasdevice_regmap = { 39 - .reg_bits = 8, 40 - .val_bits = 8, 41 - .cache_type = REGCACHE_NONE, 42 - .ranges = tasdevice_ranges, 43 - .num_ranges = ARRAY_SIZE(tasdevice_ranges), 44 - .max_register = 256 * 128, 45 - }; 46 - 47 - static int tasdevice_change_chn_book(struct tasdevice_priv *tas_priv, 48 - unsigned short chn, int book) 49 - { 50 - struct i2c_client *client = (struct i2c_client *)tas_priv->client; 51 - int ret = 0; 52 - 53 - if (chn < tas_priv->ndev) { 54 - struct tasdevice *tasdev = &tas_priv->tasdevice[chn]; 55 - struct regmap *map = tas_priv->regmap; 56 - 57 - if (client->addr != tasdev->dev_addr) { 58 - client->addr = tasdev->dev_addr; 59 - /* All tas2781s share the same regmap, clear the page 60 - * inside regmap once switching to another tas2781. 61 - * Register 0 at any pages and any books inside tas2781 62 - * is the same one for page-switching. 63 - */ 64 - ret = regmap_write(map, TASDEVICE_PAGE_SELECT, 0); 65 - if (ret < 0) { 66 - dev_err(tas_priv->dev, "%s, E=%d channel:%d\n", 67 - __func__, ret, chn); 68 - goto out; 69 - } 70 - } 71 - 72 - if (tasdev->cur_book != book) { 73 - ret = regmap_write(map, TASDEVICE_BOOKCTL_REG, book); 74 - if (ret < 0) { 75 - dev_err(tas_priv->dev, "%s, E=%d\n", 76 - __func__, ret); 77 - goto out; 78 - } 79 - tasdev->cur_book = book; 80 - } 81 - } else { 82 - ret = -EINVAL; 83 - dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__, 84 - chn); 85 - } 86 - 87 - out: 88 - return ret; 89 - } 90 - 91 - int tasdev_chn_switch(struct tasdevice_priv *tas_priv, 92 - unsigned short chn) 93 - { 94 - struct i2c_client *client = (struct i2c_client *)tas_priv->client; 95 - struct tasdevice *tasdev = &tas_priv->tasdevice[chn]; 96 - struct regmap *map = tas_priv->regmap; 97 - int ret; 98 - 99 - if (client->addr != tasdev->dev_addr) { 100 - client->addr = tasdev->dev_addr; 101 - /* All devices share the same regmap, clear the page 102 - * inside regmap once switching to another device. 103 - * Register 0 at any pages and any books inside tas2781 104 - * is the same one for page-switching. 105 - */ 106 - ret = regmap_write(map, TASDEVICE_PAGE_SELECT, 0); 107 - if (ret < 0) { 108 - dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 109 - return ret; 110 - } 111 - return 1; 112 - } 113 - return 0; 114 - } 115 - EXPORT_SYMBOL_GPL(tasdev_chn_switch); 116 21 117 22 int tasdevice_dev_read(struct tasdevice_priv *tas_priv, 118 23 unsigned short chn, unsigned int reg, unsigned int *val) ··· 27 122 if (chn < tas_priv->ndev) { 28 123 struct regmap *map = tas_priv->regmap; 29 124 30 - ret = tasdevice_change_chn_book(tas_priv, chn, 125 + ret = tas_priv->change_chn_book(tas_priv, chn, 31 126 TASDEVICE_BOOK_ID(reg)); 32 127 if (ret < 0) 33 128 goto out; ··· 46 141 } 47 142 EXPORT_SYMBOL_GPL(tasdevice_dev_read); 48 143 144 + int tasdevice_dev_bulk_read(struct tasdevice_priv *tas_priv, 145 + unsigned short chn, unsigned int reg, unsigned char *data, 146 + unsigned int len) 147 + { 148 + int ret = 0; 149 + 150 + if (chn < tas_priv->ndev) { 151 + struct regmap *map = tas_priv->regmap; 152 + 153 + ret = tas_priv->change_chn_book(tas_priv, chn, 154 + TASDEVICE_BOOK_ID(reg)); 155 + if (ret < 0) 156 + goto out; 157 + 158 + ret = regmap_bulk_read(map, TASDEVICE_PGRG(reg), data, len); 159 + if (ret < 0) 160 + dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 161 + } else 162 + dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__, 163 + chn); 164 + 165 + out: 166 + return ret; 167 + } 168 + EXPORT_SYMBOL_GPL(tasdevice_dev_bulk_read); 169 + 49 170 int tasdevice_dev_write(struct tasdevice_priv *tas_priv, 50 171 unsigned short chn, unsigned int reg, unsigned int value) 51 172 { ··· 80 149 if (chn < tas_priv->ndev) { 81 150 struct regmap *map = tas_priv->regmap; 82 151 83 - ret = tasdevice_change_chn_book(tas_priv, chn, 152 + ret = tas_priv->change_chn_book(tas_priv, chn, 84 153 TASDEVICE_BOOK_ID(reg)); 85 154 if (ret < 0) 86 155 goto out; ··· 110 179 if (chn < tas_priv->ndev) { 111 180 struct regmap *map = tas_priv->regmap; 112 181 113 - ret = tasdevice_change_chn_book(tas_priv, chn, 182 + ret = tas_priv->change_chn_book(tas_priv, chn, 114 183 TASDEVICE_BOOK_ID(reg)); 115 184 if (ret < 0) 116 185 goto out; ··· 130 199 } 131 200 EXPORT_SYMBOL_GPL(tasdevice_dev_bulk_write); 132 201 133 - int tasdevice_dev_bulk_read(struct tasdevice_priv *tas_priv, 134 - unsigned short chn, unsigned int reg, unsigned char *data, 135 - unsigned int len) 202 + int tasdevice_save_calibration(struct tasdevice_priv *tas_priv) 136 203 { 137 - int ret = 0; 138 - 139 - if (chn < tas_priv->ndev) { 140 - struct regmap *map = tas_priv->regmap; 141 - 142 - ret = tasdevice_change_chn_book(tas_priv, chn, 143 - TASDEVICE_BOOK_ID(reg)); 144 - if (ret < 0) 145 - goto out; 146 - 147 - ret = regmap_bulk_read(map, TASDEVICE_PGRG(reg), data, len); 148 - if (ret < 0) 149 - dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 150 - } else 151 - dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__, 152 - chn); 153 - 154 - out: 155 - return ret; 204 + if (tas_priv->save_calibration) 205 + return tas_priv->save_calibration(tas_priv); 206 + return -EINVAL; 156 207 } 157 - EXPORT_SYMBOL_GPL(tasdevice_dev_bulk_read); 208 + EXPORT_SYMBOL_GPL(tasdevice_save_calibration); 158 209 159 - int tasdevice_dev_update_bits( 160 - struct tasdevice_priv *tas_priv, unsigned short chn, 161 - unsigned int reg, unsigned int mask, unsigned int value) 210 + void tasdevice_apply_calibration(struct tasdevice_priv *tas_priv) 162 211 { 163 - int ret = 0; 164 - 165 - if (chn < tas_priv->ndev) { 166 - struct regmap *map = tas_priv->regmap; 167 - 168 - ret = tasdevice_change_chn_book(tas_priv, chn, 169 - TASDEVICE_BOOK_ID(reg)); 170 - if (ret < 0) 171 - goto out; 172 - 173 - ret = regmap_update_bits(map, TASDEVICE_PGRG(reg), 174 - mask, value); 175 - if (ret < 0) 176 - dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret); 177 - } else { 178 - dev_err(tas_priv->dev, "%s, no such channel(%d)\n", __func__, 179 - chn); 180 - ret = -EINVAL; 181 - } 182 - 183 - out: 184 - return ret; 212 + if (tas_priv->apply_calibration && tas_priv->cali_data.total_sz) 213 + tas_priv->apply_calibration(tas_priv); 185 214 } 186 - EXPORT_SYMBOL_GPL(tasdevice_dev_update_bits); 187 - 188 - struct tasdevice_priv *tasdevice_kzalloc(struct i2c_client *i2c) 189 - { 190 - struct tasdevice_priv *tas_priv; 191 - 192 - tas_priv = devm_kzalloc(&i2c->dev, sizeof(*tas_priv), GFP_KERNEL); 193 - if (!tas_priv) 194 - return NULL; 195 - tas_priv->dev = &i2c->dev; 196 - tas_priv->client = (void *)i2c; 197 - 198 - return tas_priv; 199 - } 200 - EXPORT_SYMBOL_GPL(tasdevice_kzalloc); 201 - 202 - void tasdevice_reset(struct tasdevice_priv *tas_dev) 203 - { 204 - int ret, i; 205 - 206 - if (tas_dev->reset) { 207 - gpiod_set_value_cansleep(tas_dev->reset, 0); 208 - usleep_range(500, 1000); 209 - gpiod_set_value_cansleep(tas_dev->reset, 1); 210 - } else { 211 - for (i = 0; i < tas_dev->ndev; i++) { 212 - ret = tasdevice_dev_write(tas_dev, i, 213 - TASDEVICE_REG_SWRESET, 214 - TASDEVICE_REG_SWRESET_RESET); 215 - if (ret < 0) 216 - dev_err(tas_dev->dev, 217 - "dev %d swreset fail, %d\n", 218 - i, ret); 219 - } 220 - } 221 - usleep_range(1000, 1050); 222 - } 223 - EXPORT_SYMBOL_GPL(tasdevice_reset); 224 - 225 - int tascodec_init(struct tasdevice_priv *tas_priv, void *codec, 226 - struct module *module, 227 - void (*cont)(const struct firmware *fw, void *context)) 228 - { 229 - int ret = 0; 230 - 231 - /* Codec Lock Hold to ensure that codec_probe and firmware parsing and 232 - * loading do not simultaneously execute. 233 - */ 234 - mutex_lock(&tas_priv->codec_lock); 235 - 236 - if (tas_priv->name_prefix) 237 - scnprintf(tas_priv->rca_binaryname, 64, "%s-%sRCA%d.bin", 238 - tas_priv->name_prefix, tas_priv->dev_name, 239 - tas_priv->ndev); 240 - else 241 - scnprintf(tas_priv->rca_binaryname, 64, "%sRCA%d.bin", 242 - tas_priv->dev_name, tas_priv->ndev); 243 - crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL); 244 - tas_priv->codec = codec; 245 - ret = request_firmware_nowait(module, FW_ACTION_UEVENT, 246 - tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv, 247 - cont); 248 - if (ret) 249 - dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n", 250 - ret); 251 - 252 - /* Codec Lock Release*/ 253 - mutex_unlock(&tas_priv->codec_lock); 254 - return ret; 255 - } 256 - EXPORT_SYMBOL_GPL(tascodec_init); 257 - 258 - int tasdevice_init(struct tasdevice_priv *tas_priv) 259 - { 260 - int ret = 0; 261 - int i; 262 - 263 - tas_priv->regmap = devm_regmap_init_i2c(tas_priv->client, 264 - &tasdevice_regmap); 265 - if (IS_ERR(tas_priv->regmap)) { 266 - ret = PTR_ERR(tas_priv->regmap); 267 - dev_err(tas_priv->dev, "Failed to allocate register map: %d\n", 268 - ret); 269 - goto out; 270 - } 271 - 272 - tas_priv->cur_prog = -1; 273 - tas_priv->cur_conf = -1; 274 - 275 - for (i = 0; i < tas_priv->ndev; i++) { 276 - tas_priv->tasdevice[i].cur_book = -1; 277 - tas_priv->tasdevice[i].cur_prog = -1; 278 - tas_priv->tasdevice[i].cur_conf = -1; 279 - } 280 - 281 - mutex_init(&tas_priv->codec_lock); 282 - 283 - out: 284 - return ret; 285 - } 286 - EXPORT_SYMBOL_GPL(tasdevice_init); 215 + EXPORT_SYMBOL_GPL(tasdevice_apply_calibration); 287 216 288 217 static void tasdev_dsp_prog_blk_remove(struct tasdevice_prog *prog) 289 218 { ··· 224 433 tas_dev->fmw = NULL; 225 434 } 226 435 EXPORT_SYMBOL_GPL(tasdevice_dsp_remove); 227 - 228 - void tasdevice_remove(struct tasdevice_priv *tas_priv) 229 - { 230 - mutex_destroy(&tas_priv->codec_lock); 231 - } 232 - EXPORT_SYMBOL_GPL(tasdevice_remove); 233 - 234 - int tasdevice_save_calibration(struct tasdevice_priv *tas_priv) 235 - { 236 - if (tas_priv->save_calibration) 237 - return tas_priv->save_calibration(tas_priv); 238 - return -EINVAL; 239 - } 240 - EXPORT_SYMBOL_GPL(tasdevice_save_calibration); 241 - 242 - void tasdevice_apply_calibration(struct tasdevice_priv *tas_priv) 243 - { 244 - if (tas_priv->apply_calibration && tas_priv->cali_data.total_sz) 245 - tas_priv->apply_calibration(tas_priv); 246 - } 247 - EXPORT_SYMBOL_GPL(tasdevice_apply_calibration); 248 - 249 - static int tasdevice_clamp(int val, int max, unsigned int invert) 250 - { 251 - if (val > max) 252 - val = max; 253 - if (invert) 254 - val = max - val; 255 - if (val < 0) 256 - val = 0; 257 - return val; 258 - } 259 - 260 - int tasdevice_amp_putvol(struct tasdevice_priv *tas_priv, 261 - struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 262 - { 263 - unsigned int invert = mc->invert; 264 - unsigned char mask; 265 - int max = mc->max; 266 - int err_cnt = 0; 267 - int val, i, ret; 268 - 269 - mask = (1 << fls(max)) - 1; 270 - mask <<= mc->shift; 271 - val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert); 272 - for (i = 0; i < tas_priv->ndev; i++) { 273 - ret = tasdevice_dev_update_bits(tas_priv, i, 274 - mc->reg, mask, (unsigned int)(val << mc->shift)); 275 - if (!ret) 276 - continue; 277 - err_cnt++; 278 - dev_err(tas_priv->dev, "set AMP vol error in dev %d\n", i); 279 - } 280 - 281 - /* All the devices set error, return 0 */ 282 - return (err_cnt == tas_priv->ndev) ? 0 : 1; 283 - } 284 - EXPORT_SYMBOL_GPL(tasdevice_amp_putvol); 285 - 286 - int tasdevice_amp_getvol(struct tasdevice_priv *tas_priv, 287 - struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 288 - { 289 - unsigned int invert = mc->invert; 290 - unsigned char mask = 0; 291 - int max = mc->max; 292 - int ret = 0; 293 - int val; 294 - 295 - /* Read the primary device */ 296 - ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val); 297 - if (ret) { 298 - dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__); 299 - goto out; 300 - } 301 - 302 - mask = (1 << fls(max)) - 1; 303 - mask <<= mc->shift; 304 - val = (val & mask) >> mc->shift; 305 - val = tasdevice_clamp(val, max, invert); 306 - ucontrol->value.integer.value[0] = val; 307 - 308 - out: 309 - return ret; 310 - 311 - } 312 - EXPORT_SYMBOL_GPL(tasdevice_amp_getvol); 313 - 314 - int tasdevice_digital_putvol(struct tasdevice_priv *tas_priv, 315 - struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 316 - { 317 - unsigned int invert = mc->invert; 318 - int max = mc->max; 319 - int err_cnt = 0; 320 - int ret; 321 - int val, i; 322 - 323 - val = tasdevice_clamp(ucontrol->value.integer.value[0], max, invert); 324 - 325 - for (i = 0; i < tas_priv->ndev; i++) { 326 - ret = tasdevice_dev_write(tas_priv, i, mc->reg, 327 - (unsigned int)val); 328 - if (!ret) 329 - continue; 330 - err_cnt++; 331 - dev_err(tas_priv->dev, 332 - "set digital vol err in dev %d\n", i); 333 - } 334 - 335 - /* All the devices set error, return 0 */ 336 - return (err_cnt == tas_priv->ndev) ? 0 : 1; 337 - 338 - } 339 - EXPORT_SYMBOL_GPL(tasdevice_digital_putvol); 340 - 341 - int tasdevice_digital_getvol(struct tasdevice_priv *tas_priv, 342 - struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) 343 - { 344 - unsigned int invert = mc->invert; 345 - int max = mc->max; 346 - int ret, val; 347 - 348 - /* Read the primary device as the whole */ 349 - ret = tasdevice_dev_read(tas_priv, 0, mc->reg, &val); 350 - if (ret) { 351 - dev_err(tas_priv->dev, "%s, get digital vol error\n", 352 - __func__); 353 - goto out; 354 - } 355 - 356 - val = tasdevice_clamp(val, max, invert); 357 - ucontrol->value.integer.value[0] = val; 358 - 359 - out: 360 - return ret; 361 - 362 - } 363 - EXPORT_SYMBOL_GPL(tasdevice_digital_getvol); 364 436 365 437 MODULE_DESCRIPTION("TAS2781 common library"); 366 438 MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
+11 -6
sound/soc/codecs/tas2781-fmwlib.c
··· 803 803 chn = idx - 1; 804 804 chnend = idx; 805 805 } else { 806 - chn = 0; 807 - chnend = tas_priv->ndev; 806 + if (tas_priv->isspi) { 807 + chn = tas_priv->index; 808 + chnend = chn + 1; 809 + } else { 810 + chn = 0; 811 + chnend = tas_priv->ndev; 812 + } 808 813 } 809 814 810 815 for (; chn < chnend; chn++) { ··· 901 896 is_err = true; 902 897 break; 903 898 } 904 - rc = tasdevice_dev_update_bits(tas_priv, chn, 899 + rc = tas_priv->update_bits(tas_priv, chn, 905 900 TASDEVICE_REG(data[subblk_offset + 2], 906 901 data[subblk_offset + 3], 907 902 data[subblk_offset + 4]), ··· 1466 1461 goto end; 1467 1462 } 1468 1463 1469 - ret = tasdevice_dev_bulk_read(tasdevice, chn, 1464 + ret = tasdevice->dev_bulk_read(tasdevice, chn, 1470 1465 TASDEVICE_REG(book, page, crc_data.offset), 1471 1466 nBuf1, crc_data.len); 1472 1467 if (ret < 0) ··· 1516 1511 in = check_yram(&crc_data, book, page, reg, 1); 1517 1512 if (!in) 1518 1513 goto end; 1519 - ret = tasdevice_dev_read(tasdevice, chl, 1514 + ret = tasdevice->dev_read(tasdevice, chl, 1520 1515 TASDEVICE_REG(book, page, reg), &nData1); 1521 1516 if (ret < 0) 1522 1517 goto end; ··· 1620 1615 unsigned int nr_value; 1621 1616 int ret; 1622 1617 1623 - ret = tasdevice_dev_read(tas_priv, chn, TASDEVICE_CHECKSUM_REG, 1618 + ret = tas_priv->dev_read(tas_priv, chn, TASDEVICE_CHECKSUM_REG, 1624 1619 &nr_value); 1625 1620 if (ret < 0) { 1626 1621 dev_err(tas_priv->dev, "%s: Chn %d\n", __func__, chn);
+1
sound/soc/codecs/tas2781-i2c.c
··· 28 28 #include <sound/pcm_params.h> 29 29 #include <sound/soc.h> 30 30 #include <sound/tas2781.h> 31 + #include <sound/tas2781-comlib-i2c.h> 31 32 #include <sound/tlv.h> 32 33 #include <sound/tas2563-tlv.h> 33 34 #include <sound/tas2781-tlv.h>