Path: blob/master/sound/hda/codecs/side-codecs/tas2781_hda.c
29268 views
// SPDX-License-Identifier: GPL-2.01//2// TAS2781 HDA Shared Lib for I2C&SPI driver3//4// Copyright 2025 Texas Instruments, Inc.5//6// Author: Shenghao Ding <[email protected]>78#include <linux/component.h>9#include <linux/crc8.h>10#include <linux/crc32.h>11#include <linux/efi.h>12#include <linux/firmware.h>13#include <linux/i2c.h>14#include <linux/pm_runtime.h>15#include <sound/soc.h>16#include <sound/tas2781.h>1718#include "tas2781_hda.h"1920#define CALIBRATION_DATA_AREA_NUM 22122const efi_guid_t tasdev_fct_efi_guid[] = {23/* DELL */24EFI_GUID(0xcc92382d, 0x6337, 0x41cb, 0xa8, 0x8b, 0x8e, 0xce, 0x74,250x91, 0xea, 0x9f),26/* HP */27EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d, 0x93, 0xfe, 0x5a,280xa3, 0x5d, 0xb3),29/* LENOVO & OTHERS */30EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc, 0x09, 0x43, 0xa3, 0xf4,310x31, 0x0a, 0x92),32};33EXPORT_SYMBOL_NS_GPL(tasdev_fct_efi_guid, "SND_HDA_SCODEC_TAS2781");3435/*36* The order of calibrated-data writing function is a bit different from the37* order in UEFI. Here is the conversion to match the order of calibrated-data38* writing function.39*/40static void cali_cnv(unsigned char *data, unsigned int base, int offset)41{42struct cali_reg reg_data;4344memcpy(®_data, &data[base], sizeof(reg_data));45/* the data order has to be swapped between r0_low_reg and inv0_reg */46swap(reg_data.r0_low_reg, reg_data.invr0_reg);4748cpu_to_be32_array((__force __be32 *)(data + offset + 1),49(u32 *)®_data, TASDEV_CALIB_N);50}5152static void tas2781_apply_calib(struct tasdevice_priv *p)53{54struct calidata *cali_data = &p->cali_data;55struct cali_reg *r = &cali_data->cali_reg_array;56unsigned char *data = cali_data->data;57unsigned int *tmp_val = (unsigned int *)data;58unsigned int cali_reg[TASDEV_CALIB_N] = {59TASDEVICE_REG(0, 0x17, 0x74),60TASDEVICE_REG(0, 0x18, 0x0c),61TASDEVICE_REG(0, 0x18, 0x14),62TASDEVICE_REG(0, 0x13, 0x70),63TASDEVICE_REG(0, 0x18, 0x7c),64};65unsigned int crc, oft, node_num;66unsigned char *buf;67int i, j, k, l;6869if (tmp_val[0] == 2781) {70/*71* New features were added in calibrated Data V3:72* 1. Added calibration registers address define in73* a node, marked as Device id == 0x80.74* New features were added in calibrated Data V2:75* 1. Added some the fields to store the link_id and76* uniqie_id for multi-link solutions77* 2. Support flexible number of devices instead of78* fixed one in V1.79* Layout of calibrated data V2 in UEFI(total 256 bytes):80* ChipID (2781, 4 bytes)81* Data-Group-Sum (4 bytes)82* TimeStamp of Calibration (4 bytes)83* for (i = 0; i < Data-Group-Sum; i++) {84* if (Data type != 0x80) (4 bytes)85* Calibrated Data of Device #i (20 bytes)86* else87* Calibration registers address (5*4 = 20 bytes)88* # V2: No reg addr in data grp section.89* # V3: Normally the last grp is the reg addr.90* }91* CRC (4 bytes)92* Reserved (the rest)93*/94crc = crc32(~0, data, (3 + tmp_val[1] * 6) * 4) ^ ~0;9596if (crc != tmp_val[3 + tmp_val[1] * 6]) {97cali_data->total_sz = 0;98dev_err(p->dev, "%s: CRC error\n", __func__);99return;100}101node_num = tmp_val[1];102103for (j = 0, k = 0; j < node_num; j++) {104oft = j * 6 + 3;105if (tmp_val[oft] == TASDEV_UEFI_CALI_REG_ADDR_FLG) {106for (i = 0; i < TASDEV_CALIB_N; i++) {107buf = &data[(oft + i + 1) * 4];108cali_reg[i] = TASDEVICE_REG(buf[1],109buf[2], buf[3]);110}111} else {112l = j * (cali_data->cali_dat_sz_per_dev + 1);113if (k >= p->ndev || l > oft * 4) {114dev_err(p->dev, "%s: dev sum error\n",115__func__);116cali_data->total_sz = 0;117return;118}119120data[l] = k;121oft++;122cali_cnv(data, 4 * oft, l);123k++;124}125}126} else {127/*128* Calibration data is in V1 format.129* struct cali_data {130* char cali_data[20];131* }132*133* struct {134* struct cali_data cali_data[4];135* int TimeStamp of Calibration (4 bytes)136* int CRC (4 bytes)137* } ueft;138*/139crc = crc32(~0, data, 84) ^ ~0;140if (crc != tmp_val[21]) {141cali_data->total_sz = 0;142dev_err(p->dev, "%s: V1 CRC error\n", __func__);143return;144}145146for (j = p->ndev - 1; j >= 0; j--) {147l = j * (cali_data->cali_dat_sz_per_dev + 1);148cali_cnv(data, cali_data->cali_dat_sz_per_dev * j, l);149data[l] = j;150}151}152153if (p->dspbin_typ == TASDEV_BASIC) {154r->r0_reg = cali_reg[0];155r->invr0_reg = cali_reg[1];156r->r0_low_reg = cali_reg[2];157r->pow_reg = cali_reg[3];158r->tlimit_reg = cali_reg[4];159}160161p->is_user_space_calidata = true;162cali_data->total_sz = p->ndev * (cali_data->cali_dat_sz_per_dev + 1);163}164165/*166* Update the calibration data, including speaker impedance, f0, etc,167* into algo. Calibrate data is done by manufacturer in the factory.168* The data is used by Algo for calculating the speaker temperature,169* speaker membrane excursion and f0 in real time during playback.170* Calibration data format in EFI is V2, since 2024.171*/172int tas2781_save_calibration(struct tas2781_hda *hda)173{174/*175* GUID was used for data access in BIOS, it was provided by board176* manufactory.177*/178efi_guid_t efi_guid = tasdev_fct_efi_guid[LENOVO];179/*180* Some devices save the calibrated data into L"CALI_DATA",181* and others into L"SmartAmpCalibrationData".182*/183static efi_char16_t *efi_name[CALIBRATION_DATA_AREA_NUM] = {184L"CALI_DATA",185L"SmartAmpCalibrationData",186};187struct tasdevice_priv *p = hda->priv;188struct calidata *cali_data = &p->cali_data;189unsigned long total_sz = 0;190unsigned int attr, size;191unsigned char *data;192efi_status_t status;193int i;194195if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) {196dev_err(p->dev, "%s: NO EFI FOUND!\n", __func__);197return -EINVAL;198}199200if (hda->catlog_id < LENOVO)201efi_guid = tasdev_fct_efi_guid[hda->catlog_id];202203cali_data->cali_dat_sz_per_dev = 20;204size = p->ndev * (cali_data->cali_dat_sz_per_dev + 1);205for (i = 0; i < CALIBRATION_DATA_AREA_NUM; i++) {206/* Get real size of UEFI variable */207status = efi.get_variable(efi_name[i], &efi_guid, &attr,208&total_sz, NULL);209cali_data->total_sz = total_sz > size ? total_sz : size;210if (status == EFI_BUFFER_TOO_SMALL) {211/* Allocate data buffer of data_size bytes */212data = cali_data->data = devm_kzalloc(p->dev,213cali_data->total_sz, GFP_KERNEL);214if (!data) {215status = -ENOMEM;216continue;217}218/* Get variable contents into buffer */219status = efi.get_variable(efi_name[i], &efi_guid,220&attr, &cali_data->total_sz, data);221}222/* Check whether get the calibrated data */223if (status == EFI_SUCCESS)224break;225}226227if (status != EFI_SUCCESS) {228cali_data->total_sz = 0;229return status;230}231232tas2781_apply_calib(p);233234return 0;235}236EXPORT_SYMBOL_NS_GPL(tas2781_save_calibration, "SND_HDA_SCODEC_TAS2781");237238void tas2781_hda_remove(struct device *dev,239const struct component_ops *ops)240{241struct tas2781_hda *tas_hda = dev_get_drvdata(dev);242243component_del(tas_hda->dev, ops);244245pm_runtime_get_sync(tas_hda->dev);246pm_runtime_disable(tas_hda->dev);247248pm_runtime_put_noidle(tas_hda->dev);249250tasdevice_remove(tas_hda->priv);251}252EXPORT_SYMBOL_NS_GPL(tas2781_hda_remove, "SND_HDA_SCODEC_TAS2781");253254int tasdevice_info_profile(struct snd_kcontrol *kcontrol,255struct snd_ctl_elem_info *uinfo)256{257struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);258259uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;260uinfo->count = 1;261uinfo->value.integer.min = 0;262uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;263264return 0;265}266EXPORT_SYMBOL_NS_GPL(tasdevice_info_profile, "SND_HDA_SCODEC_TAS2781");267268int tasdevice_info_programs(struct snd_kcontrol *kcontrol,269struct snd_ctl_elem_info *uinfo)270{271struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);272273uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;274uinfo->count = 1;275uinfo->value.integer.min = 0;276uinfo->value.integer.max = tas_priv->fmw->nr_programs - 1;277278return 0;279}280EXPORT_SYMBOL_NS_GPL(tasdevice_info_programs, "SND_HDA_SCODEC_TAS2781");281282int tasdevice_info_config(struct snd_kcontrol *kcontrol,283struct snd_ctl_elem_info *uinfo)284{285struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);286struct tasdevice_fw *tas_fw = tas_priv->fmw;287288uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;289uinfo->count = 1;290uinfo->value.integer.min = 0;291uinfo->value.integer.max = tas_fw->nr_configurations - 1;292293return 0;294}295EXPORT_SYMBOL_NS_GPL(tasdevice_info_config, "SND_HDA_SCODEC_TAS2781");296297int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,298struct snd_ctl_elem_value *ucontrol)299{300struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);301302ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;303304dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n", __func__,305kcontrol->id.name, tas_priv->rcabin.profile_cfg_id);306307return 0;308}309EXPORT_SYMBOL_NS_GPL(tasdevice_get_profile_id, "SND_HDA_SCODEC_TAS2781");310311int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,312struct snd_ctl_elem_value *ucontrol)313{314struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);315int profile_id = ucontrol->value.integer.value[0];316int max = tas_priv->rcabin.ncfgs - 1;317int val, ret = 0;318319val = clamp(profile_id, 0, max);320321guard(mutex)(&tas_priv->codec_lock);322323dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n", __func__,324kcontrol->id.name, tas_priv->rcabin.profile_cfg_id, val);325326if (tas_priv->rcabin.profile_cfg_id != val) {327tas_priv->rcabin.profile_cfg_id = val;328ret = 1;329}330331return ret;332}333EXPORT_SYMBOL_NS_GPL(tasdevice_set_profile_id, "SND_HDA_SCODEC_TAS2781");334335int tasdevice_program_get(struct snd_kcontrol *kcontrol,336struct snd_ctl_elem_value *ucontrol)337{338struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);339340ucontrol->value.integer.value[0] = tas_priv->cur_prog;341342dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n", __func__,343kcontrol->id.name, tas_priv->cur_prog);344345return 0;346}347EXPORT_SYMBOL_NS_GPL(tasdevice_program_get, "SND_HDA_SCODEC_TAS2781");348349int tasdevice_program_put(struct snd_kcontrol *kcontrol,350struct snd_ctl_elem_value *ucontrol)351{352struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);353struct tasdevice_fw *tas_fw = tas_priv->fmw;354int nr_program = ucontrol->value.integer.value[0];355int max = tas_fw->nr_programs - 1;356int val, ret = 0;357358val = clamp(nr_program, 0, max);359360guard(mutex)(&tas_priv->codec_lock);361362dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n", __func__,363kcontrol->id.name, tas_priv->cur_prog, val);364365if (tas_priv->cur_prog != val) {366tas_priv->cur_prog = val;367ret = 1;368}369370return ret;371}372EXPORT_SYMBOL_NS_GPL(tasdevice_program_put, "SND_HDA_SCODEC_TAS2781");373374int tasdevice_config_get(struct snd_kcontrol *kcontrol,375struct snd_ctl_elem_value *ucontrol)376{377struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);378379ucontrol->value.integer.value[0] = tas_priv->cur_conf;380381dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n", __func__,382kcontrol->id.name, tas_priv->cur_conf);383384return 0;385}386EXPORT_SYMBOL_NS_GPL(tasdevice_config_get, "SND_HDA_SCODEC_TAS2781");387388int tasdevice_config_put(struct snd_kcontrol *kcontrol,389struct snd_ctl_elem_value *ucontrol)390{391struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);392struct tasdevice_fw *tas_fw = tas_priv->fmw;393int nr_config = ucontrol->value.integer.value[0];394int max = tas_fw->nr_configurations - 1;395int val, ret = 0;396397val = clamp(nr_config, 0, max);398399guard(mutex)(&tas_priv->codec_lock);400401dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n", __func__,402kcontrol->id.name, tas_priv->cur_conf, val);403404if (tas_priv->cur_conf != val) {405tas_priv->cur_conf = val;406ret = 1;407}408409return ret;410}411EXPORT_SYMBOL_NS_GPL(tasdevice_config_put, "SND_HDA_SCODEC_TAS2781");412413MODULE_DESCRIPTION("TAS2781 HDA Driver");414MODULE_LICENSE("GPL");415MODULE_AUTHOR("Shenghao Ding, TI, <[email protected]>");416417418