Path: blob/master/sound/hda/codecs/side-codecs/tas2781_hda_spi.c
26481 views
// SPDX-License-Identifier: GPL-2.01//2// TAS2781 HDA SPI driver3//4// Copyright 2024 - 2025 Texas Instruments, Inc.5//6// Author: Baojun Xu <[email protected]>78#include <linux/acpi.h>9#include <linux/array_size.h>10#include <linux/bits.h>11#include <linux/cleanup.h>12#include <linux/crc8.h>13#include <linux/crc32.h>14#include <linux/efi.h>15#include <linux/firmware.h>16#include <linux/mod_devicetable.h>17#include <linux/module.h>18#include <linux/mutex.h>19#include <linux/pm_runtime.h>20#include <linux/property.h>21#include <linux/regmap.h>22#include <linux/spi/spi.h>23#include <linux/time.h>24#include <linux/types.h>25#include <linux/units.h>2627#include <sound/hda_codec.h>28#include <sound/soc.h>29#include <sound/tas2781.h>30#include <sound/tlv.h>31#include <sound/tas2781-tlv.h>3233#include "hda_local.h"34#include "hda_auto_parser.h"35#include "hda_component.h"36#include "hda_jack.h"37#include "../generic.h"38#include "tas2781_hda.h"3940#define TASDEVICE_RANGE_MAX_SIZE (256 * 128)41#define TASDEVICE_WIN_LEN 12842#define TAS2781_SPI_MAX_FREQ (4 * HZ_PER_MHZ)43/* Flag of calibration registers address. */44#define TASDEVICE_CALIBRATION_REG_ADDRESS BIT(7)45#define TASDEV_UEFI_CALI_REG_ADDR_FLG BIT(7)4647/* System Reset Check Register */48#define TAS2781_REG_CLK_CONFIG TASDEVICE_REG(0x0, 0x0, 0x5c)49#define TAS2781_REG_CLK_CONFIG_RESET 0x195051struct tas2781_hda_spi_priv {52struct snd_kcontrol *snd_ctls[3];53};5455static const struct regmap_range_cfg tasdevice_ranges[] = {56{57.range_min = 0,58.range_max = TASDEVICE_RANGE_MAX_SIZE,59.selector_reg = TASDEVICE_PAGE_SELECT,60.selector_mask = GENMASK(7, 0),61.selector_shift = 0,62.window_start = 0,63.window_len = TASDEVICE_WIN_LEN,64},65};6667static const struct regmap_config tasdevice_regmap = {68.reg_bits = 8,69.val_bits = 8,70.zero_flag_mask = true,71.read_flag_mask = 0x01,72.reg_shift = -1,73.cache_type = REGCACHE_NONE,74.ranges = tasdevice_ranges,75.num_ranges = ARRAY_SIZE(tasdevice_ranges),76.max_register = TASDEVICE_RANGE_MAX_SIZE,77};7879static int tasdevice_spi_dev_read(struct tasdevice_priv *tas_priv,80unsigned short chn, unsigned int reg, unsigned int *val)81{82int ret;8384/*85* In our TAS2781 SPI mode, if read from other book (not book 0),86* or read from page number larger than 1 in book 0, one more byte87* read is needed, and first byte is a dummy byte, need to be ignored.88*/89if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) {90unsigned char data[2];9192ret = tasdevice_dev_bulk_read(tas_priv, chn, reg,93data, sizeof(data));94*val = data[1];95} else {96ret = tasdevice_dev_read(tas_priv, chn, reg, val);97}98if (ret < 0)99dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);100101return ret;102}103104static int tasdevice_spi_dev_bulk_read(struct tasdevice_priv *tas_priv,105unsigned short chn, unsigned int reg, unsigned char *data,106unsigned int len)107{108int ret;109110/*111* In our TAS2781 SPI mode, if read from other book (not book 0),112* or read from page number larger than 1 in book 0, one more byte113* read is needed, and first byte is a dummy byte, need to be ignored.114*/115if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) {116unsigned char buf[TASDEVICE_WIN_LEN + 1];117118ret = tasdevice_dev_bulk_read(tas_priv, chn, reg,119buf, len + 1);120memcpy(data, buf + 1, len);121} else {122ret = tasdevice_dev_bulk_read(tas_priv, chn, reg, data, len);123}124if (ret < 0)125dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);126127return ret;128}129130static int tasdevice_spi_dev_update_bits(struct tasdevice_priv *tas_priv,131unsigned short chn, unsigned int reg, unsigned int mask,132unsigned int value)133{134int ret, val;135136/*137* In our TAS2781 SPI mode, read/write was masked in last bit of138* address, it cause regmap_update_bits() not work as expected.139*/140ret = tasdevice_dev_read(tas_priv, chn, reg, &val);141if (ret < 0) {142dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);143return ret;144}145146ret = tasdevice_dev_write(tas_priv, chn, TASDEVICE_PAGE_REG(reg),147(val & ~mask) | (mask & value));148if (ret < 0)149dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);150151return ret;152}153154static int tasdevice_spi_change_chn_book(struct tasdevice_priv *p,155unsigned short chn, int book)156{157int ret = 0;158159if (chn == p->index) {160struct tasdevice *tasdev = &p->tasdevice[chn];161struct regmap *map = p->regmap;162163if (tasdev->cur_book != book) {164ret = regmap_write(map, TASDEVICE_BOOKCTL_REG, book);165if (ret < 0)166dev_err(p->dev, "%s, E=%d\n", __func__, ret);167else168tasdev->cur_book = book;169}170} else {171ret = -EXDEV;172dev_dbg(p->dev, "Not error, %s ignore channel(%d)\n",173__func__, chn);174}175176return ret;177}178179static void tas2781_spi_reset(struct tasdevice_priv *tas_dev)180{181int ret;182183if (tas_dev->reset) {184gpiod_set_value_cansleep(tas_dev->reset, 0);185fsleep(800);186gpiod_set_value_cansleep(tas_dev->reset, 1);187} else {188ret = tasdevice_dev_write(tas_dev, tas_dev->index,189TASDEVICE_REG_SWRESET, TASDEVICE_REG_SWRESET_RESET);190if (ret < 0) {191dev_err(tas_dev->dev, "dev sw-reset fail, %d\n", ret);192return;193}194fsleep(1000);195}196}197198static int tascodec_spi_init(struct tasdevice_priv *tas_priv,199void *codec, struct module *module,200void (*cont)(const struct firmware *fw, void *context))201{202int ret;203204/*205* Codec Lock Hold to ensure that codec_probe and firmware parsing and206* loading do not simultaneously execute.207*/208guard(mutex)(&tas_priv->codec_lock);209210scnprintf(tas_priv->rca_binaryname,211sizeof(tas_priv->rca_binaryname), "%sRCA%d.bin",212tas_priv->dev_name, tas_priv->ndev);213crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL);214tas_priv->codec = codec;215ret = request_firmware_nowait(module, FW_ACTION_UEVENT,216tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv,217cont);218if (ret)219dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n",220ret);221222return ret;223}224225static void tasdevice_spi_init(struct tasdevice_priv *tas_priv)226{227tas_priv->tasdevice[tas_priv->index].cur_book = -1;228tas_priv->tasdevice[tas_priv->index].cur_conf = -1;229tas_priv->tasdevice[tas_priv->index].cur_prog = -1;230231tas_priv->isspi = true;232233tas_priv->update_bits = tasdevice_spi_dev_update_bits;234tas_priv->change_chn_book = tasdevice_spi_change_chn_book;235tas_priv->dev_read = tasdevice_spi_dev_read;236tas_priv->dev_bulk_read = tasdevice_spi_dev_bulk_read;237238mutex_init(&tas_priv->codec_lock);239}240241static int tasdevice_spi_amp_putvol(struct tasdevice_priv *tas_priv,242struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)243{244unsigned int invert = mc->invert;245unsigned char mask;246int max = mc->max;247int val, ret;248249mask = rounddown_pow_of_two(max);250mask <<= mc->shift;251val = clamp(invert ? max - ucontrol->value.integer.value[0] :252ucontrol->value.integer.value[0], 0, max);253254ret = tasdevice_spi_dev_update_bits(tas_priv, tas_priv->index,255mc->reg, mask, (unsigned int)(val << mc->shift));256if (ret)257dev_err(tas_priv->dev, "set AMP vol error in dev %d\n",258tas_priv->index);259260return ret;261}262263static int tasdevice_spi_amp_getvol(struct tasdevice_priv *tas_priv,264struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)265{266unsigned int invert = mc->invert;267unsigned char mask = 0;268int max = mc->max;269int ret, val;270271ret = tasdevice_spi_dev_read(tas_priv, tas_priv->index, mc->reg, &val);272if (ret) {273dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__);274return ret;275}276277mask = rounddown_pow_of_two(max);278mask <<= mc->shift;279val = (val & mask) >> mc->shift;280val = clamp(invert ? max - val : val, 0, max);281ucontrol->value.integer.value[0] = val;282283return ret;284}285286static int tasdevice_spi_digital_putvol(struct tasdevice_priv *p,287struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)288{289unsigned int invert = mc->invert;290int max = mc->max;291int val, ret;292293val = clamp(invert ? max - ucontrol->value.integer.value[0] :294ucontrol->value.integer.value[0], 0, max);295ret = tasdevice_dev_write(p, p->index, mc->reg, (unsigned int)val);296if (ret)297dev_err(p->dev, "set digital vol err in dev %d\n", p->index);298299return ret;300}301302static int tasdevice_spi_digital_getvol(struct tasdevice_priv *p,303struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)304{305unsigned int invert = mc->invert;306int max = mc->max;307int ret, val;308309ret = tasdevice_spi_dev_read(p, p->index, mc->reg, &val);310if (ret) {311dev_err(p->dev, "%s, get digital vol err\n", __func__);312return ret;313}314315val = clamp(invert ? max - val : val, 0, max);316ucontrol->value.integer.value[0] = val;317318return ret;319}320321static int tas2781_read_acpi(struct tas2781_hda *tas_hda,322const char *hid, int id)323{324struct tasdevice_priv *p = tas_hda->priv;325struct acpi_device *adev;326struct device *physdev;327u32 values[HDA_MAX_COMPONENTS];328const char *property;329size_t nval;330int ret, i;331332adev = acpi_dev_get_first_match_dev(hid, NULL, -1);333if (!adev) {334dev_err(p->dev, "Failed to find ACPI device: %s\n", hid);335return -ENODEV;336}337338strscpy(p->dev_name, hid, sizeof(p->dev_name));339physdev = get_device(acpi_get_first_physical_node(adev));340acpi_dev_put(adev);341342property = "ti,dev-index";343ret = device_property_count_u32(physdev, property);344if (ret <= 0 || ret > ARRAY_SIZE(values)) {345ret = -EINVAL;346goto err;347}348p->ndev = nval = ret;349350ret = device_property_read_u32_array(physdev, property, values, nval);351if (ret)352goto err;353354p->index = U8_MAX;355for (i = 0; i < nval; i++) {356if (values[i] == id) {357p->index = i;358break;359}360}361if (p->index == U8_MAX) {362dev_dbg(p->dev, "No index found in %s\n", property);363ret = -ENODEV;364goto err;365}366367if (p->index == 0) {368/* All of amps share same RESET pin. */369p->reset = devm_gpiod_get_index_optional(physdev, "reset",370p->index, GPIOD_OUT_LOW);371if (IS_ERR(p->reset)) {372ret = PTR_ERR(p->reset);373dev_err_probe(p->dev, ret, "Failed on reset GPIO\n");374goto err;375}376}377put_device(physdev);378379return 0;380err:381dev_err(p->dev, "read acpi error, ret: %d\n", ret);382put_device(physdev);383acpi_dev_put(adev);384385return ret;386}387388static void tas2781_hda_playback_hook(struct device *dev, int action)389{390struct tas2781_hda *tas_hda = dev_get_drvdata(dev);391struct tasdevice_priv *tas_priv = tas_hda->priv;392393if (action == HDA_GEN_PCM_ACT_OPEN) {394pm_runtime_get_sync(dev);395guard(mutex)(&tas_priv->codec_lock);396if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK)397tasdevice_tuning_switch(tas_hda->priv, 0);398} else if (action == HDA_GEN_PCM_ACT_CLOSE) {399guard(mutex)(&tas_priv->codec_lock);400if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK)401tasdevice_tuning_switch(tas_priv, 1);402pm_runtime_put_autosuspend(dev);403}404}405406/*407* tas2781_digital_getvol - get the volum control408* @kcontrol: control pointer409* @ucontrol: User data410*411* Customer Kcontrol for tas2781 is primarily for regmap booking, paging412* depends on internal regmap mechanism.413* tas2781 contains book and page two-level register map, especially414* book switching will set the register BXXP00R7F, after switching to the415* correct book, then leverage the mechanism for paging to access the416* register.417*418* Return 0 if succeeded.419*/420static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,421struct snd_ctl_elem_value *ucontrol)422{423struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);424struct soc_mixer_control *mc =425(struct soc_mixer_control *)kcontrol->private_value;426427guard(mutex)(&tas_priv->codec_lock);428return tasdevice_spi_digital_getvol(tas_priv, ucontrol, mc);429}430431static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,432struct snd_ctl_elem_value *ucontrol)433{434struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);435struct soc_mixer_control *mc =436(struct soc_mixer_control *)kcontrol->private_value;437438guard(mutex)(&tas_priv->codec_lock);439return tasdevice_spi_amp_getvol(tas_priv, ucontrol, mc);440}441442static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,443struct snd_ctl_elem_value *ucontrol)444{445struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);446struct soc_mixer_control *mc =447(struct soc_mixer_control *)kcontrol->private_value;448449guard(mutex)(&tas_priv->codec_lock);450return tasdevice_spi_digital_putvol(tas_priv, ucontrol, mc);451}452453static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,454struct snd_ctl_elem_value *ucontrol)455{456struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);457struct soc_mixer_control *mc =458(struct soc_mixer_control *)kcontrol->private_value;459460guard(mutex)(&tas_priv->codec_lock);461return tasdevice_spi_amp_putvol(tas_priv, ucontrol, mc);462}463464static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,465struct snd_ctl_elem_value *ucontrol)466{467struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);468469ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;470dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,471str_on_off(tas_priv->force_fwload_status));472473return 0;474}475476static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,477struct snd_ctl_elem_value *ucontrol)478{479struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);480bool change, val = (bool)ucontrol->value.integer.value[0];481482if (tas_priv->force_fwload_status == val) {483change = false;484} else {485change = true;486tas_priv->force_fwload_status = val;487}488dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,489str_on_off(tas_priv->force_fwload_status));490491return change;492}493494static struct snd_kcontrol_new tas2781_snd_ctls[] = {495ACARD_SINGLE_RANGE_EXT_TLV(NULL, TAS2781_AMP_LEVEL, 1, 0, 20, 0,496tas2781_amp_getvol, tas2781_amp_putvol,497tas2781_amp_tlv),498ACARD_SINGLE_RANGE_EXT_TLV(NULL, TAS2781_DVC_LVL, 0, 0, 200, 1,499tas2781_digital_getvol, tas2781_digital_putvol,500tas2781_dvc_tlv),501ACARD_SINGLE_BOOL_EXT(NULL, 0, tas2781_force_fwload_get,502tas2781_force_fwload_put),503};504505static struct snd_kcontrol_new tas2781_prof_ctl = {506.iface = SNDRV_CTL_ELEM_IFACE_CARD,507.info = tasdevice_info_profile,508.get = tasdevice_get_profile_id,509.put = tasdevice_set_profile_id,510};511512static struct snd_kcontrol_new tas2781_dsp_ctls[] = {513/* Speaker Program */514{515.iface = SNDRV_CTL_ELEM_IFACE_CARD,516.info = tasdevice_info_programs,517.get = tasdevice_program_get,518.put = tasdevice_program_put,519},520/* Speaker Config */521{522.iface = SNDRV_CTL_ELEM_IFACE_CARD,523.info = tasdevice_info_config,524.get = tasdevice_config_get,525.put = tasdevice_config_put,526},527};528529static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)530{531struct hda_codec *codec = tas_hda->priv->codec;532struct tas2781_hda_spi_priv *h_priv = tas_hda->hda_priv;533534snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);535536snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);537538for (int i = ARRAY_SIZE(h_priv->snd_ctls) - 1; i >= 0; i--)539snd_ctl_remove(codec->card, h_priv->snd_ctls[i]);540541snd_ctl_remove(codec->card, tas_hda->prof_ctl);542}543544static int tas2781_hda_spi_prf_ctl(struct tas2781_hda *h)545{546struct tasdevice_priv *p = h->priv;547struct hda_codec *c = p->codec;548char name[64];549int rc;550551snprintf(name, sizeof(name), "Speaker-%d Profile Id", p->index);552tas2781_prof_ctl.name = name;553h->prof_ctl = snd_ctl_new1(&tas2781_prof_ctl, p);554rc = snd_ctl_add(c->card, h->prof_ctl);555if (rc)556dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",557tas2781_prof_ctl.name, rc);558return rc;559}560561static int tas2781_hda_spi_snd_ctls(struct tas2781_hda *h)562{563struct tas2781_hda_spi_priv *h_priv = h->hda_priv;564struct tasdevice_priv *p = h->priv;565struct hda_codec *c = p->codec;566char name[64];567int i = 0;568int rc;569570snprintf(name, sizeof(name), "Speaker-%d Analog Volume", p->index);571tas2781_snd_ctls[i].name = name;572h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);573rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);574if (rc) {575dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",576tas2781_snd_ctls[i].name, rc);577return rc;578}579i++;580snprintf(name, sizeof(name), "Speaker-%d Digital Volume", p->index);581tas2781_snd_ctls[i].name = name;582h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);583rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);584if (rc) {585dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",586tas2781_snd_ctls[i].name, rc);587return rc;588}589i++;590snprintf(name, sizeof(name), "Froce Speaker-%d FW Load", p->index);591tas2781_snd_ctls[i].name = name;592h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);593rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);594if (rc) {595dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",596tas2781_snd_ctls[i].name, rc);597}598return rc;599}600601static int tas2781_hda_spi_dsp_ctls(struct tas2781_hda *h)602{603struct tasdevice_priv *p = h->priv;604struct hda_codec *c = p->codec;605char name[64];606int i = 0;607int rc;608609snprintf(name, sizeof(name), "Speaker-%d Program Id", p->index);610tas2781_dsp_ctls[i].name = name;611h->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_ctls[i], p);612rc = snd_ctl_add(c->card, h->dsp_prog_ctl);613if (rc) {614dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",615tas2781_dsp_ctls[i].name, rc);616return rc;617}618i++;619snprintf(name, sizeof(name), "Speaker-%d Config Id", p->index);620tas2781_dsp_ctls[i].name = name;621h->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_ctls[i], p);622rc = snd_ctl_add(c->card, h->dsp_conf_ctl);623if (rc) {624dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",625tas2781_dsp_ctls[i].name, rc);626}627628return rc;629}630631static void tasdev_fw_ready(const struct firmware *fmw, void *context)632{633struct tasdevice_priv *tas_priv = context;634struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);635struct hda_codec *codec = tas_priv->codec;636int ret, val;637638pm_runtime_get_sync(tas_priv->dev);639guard(mutex)(&tas_priv->codec_lock);640641ret = tasdevice_rca_parser(tas_priv, fmw);642if (ret)643goto out;644645/* Add control one time only. */646ret = tas2781_hda_spi_prf_ctl(tas_hda);647if (ret)648goto out;649650ret = tas2781_hda_spi_snd_ctls(tas_hda);651if (ret)652goto out;653654tasdevice_dsp_remove(tas_priv);655656tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;657scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%04X-%01d.bin",658lower_16_bits(codec->core.subsystem_id), tas_priv->index);659ret = tasdevice_dsp_parser(tas_priv);660if (ret) {661dev_err(tas_priv->dev, "dspfw load %s error\n",662tas_priv->coef_binaryname);663tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;664goto out;665}666667ret = tas2781_hda_spi_dsp_ctls(tas_hda);668if (ret)669goto out;670/* Perform AMP reset before firmware download. */671tas2781_spi_reset(tas_priv);672tas_priv->rcabin.profile_cfg_id = 0;673674tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;675ret = tasdevice_spi_dev_read(tas_priv, tas_priv->index,676TAS2781_REG_CLK_CONFIG, &val);677if (ret < 0)678goto out;679680if (val == TAS2781_REG_CLK_CONFIG_RESET) {681ret = tasdevice_prmg_load(tas_priv, 0);682if (ret < 0) {683dev_err(tas_priv->dev, "FW download failed = %d\n",684ret);685goto out;686}687tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;688}689if (tas_priv->fmw->nr_programs > 0)690tas_priv->tasdevice[tas_priv->index].cur_prog = 0;691if (tas_priv->fmw->nr_configurations > 0)692tas_priv->tasdevice[tas_priv->index].cur_conf = 0;693694/*695* If calibrated data occurs error, dsp will still works with default696* calibrated data inside algo.697*/698tas2781_save_calibration(tas_hda);699out:700release_firmware(fmw);701pm_runtime_put_autosuspend(tas_hda->priv->dev);702}703704static int tas2781_hda_bind(struct device *dev, struct device *master,705void *master_data)706{707struct tas2781_hda *tas_hda = dev_get_drvdata(dev);708struct hda_component_parent *parent = master_data;709struct hda_component *comp;710struct hda_codec *codec;711int ret;712713comp = hda_component_from_index(parent, tas_hda->priv->index);714if (!comp)715return -EINVAL;716717if (comp->dev)718return -EBUSY;719720codec = parent->codec;721722pm_runtime_get_sync(dev);723724comp->dev = dev;725726strscpy(comp->name, dev_name(dev), sizeof(comp->name));727728ret = tascodec_spi_init(tas_hda->priv, codec, THIS_MODULE,729tasdev_fw_ready);730if (!ret)731comp->playback_hook = tas2781_hda_playback_hook;732733pm_runtime_put_autosuspend(dev);734735return ret;736}737738static void tas2781_hda_unbind(struct device *dev, struct device *master,739void *master_data)740{741struct tas2781_hda *tas_hda = dev_get_drvdata(dev);742struct hda_component_parent *parent = master_data;743struct tasdevice_priv *tas_priv = tas_hda->priv;744struct hda_component *comp;745746comp = hda_component_from_index(parent, tas_priv->index);747if (comp && (comp->dev == dev)) {748comp->dev = NULL;749memset(comp->name, 0, sizeof(comp->name));750comp->playback_hook = NULL;751}752753tas2781_hda_remove_controls(tas_hda);754755tasdevice_config_info_remove(tas_priv);756tasdevice_dsp_remove(tas_priv);757758tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;759}760761static const struct component_ops tas2781_hda_comp_ops = {762.bind = tas2781_hda_bind,763.unbind = tas2781_hda_unbind,764};765766static int tas2781_hda_spi_probe(struct spi_device *spi)767{768struct tas2781_hda_spi_priv *hda_priv;769struct tasdevice_priv *tas_priv;770struct tas2781_hda *tas_hda;771const char *device_name;772int ret = 0;773774tas_hda = devm_kzalloc(&spi->dev, sizeof(*tas_hda), GFP_KERNEL);775if (!tas_hda)776return -ENOMEM;777778hda_priv = devm_kzalloc(&spi->dev, sizeof(*hda_priv), GFP_KERNEL);779if (!hda_priv)780return -ENOMEM;781782tas_hda->hda_priv = hda_priv;783spi->max_speed_hz = TAS2781_SPI_MAX_FREQ;784785tas_priv = devm_kzalloc(&spi->dev, sizeof(*tas_priv), GFP_KERNEL);786if (!tas_priv)787return -ENOMEM;788tas_priv->dev = &spi->dev;789tas_hda->priv = tas_priv;790tas_priv->regmap = devm_regmap_init_spi(spi, &tasdevice_regmap);791if (IS_ERR(tas_priv->regmap)) {792ret = PTR_ERR(tas_priv->regmap);793dev_err(tas_priv->dev, "Failed to allocate regmap: %d\n",794ret);795return ret;796}797if (strstr(dev_name(&spi->dev), "TXNW2781")) {798device_name = "TXNW2781";799} else {800dev_err(tas_priv->dev, "Unmatched spi dev %s\n",801dev_name(&spi->dev));802return -ENODEV;803}804805tas_priv->irq = spi->irq;806dev_set_drvdata(&spi->dev, tas_hda);807ret = tas2781_read_acpi(tas_hda, device_name,808spi_get_chipselect(spi, 0));809if (ret)810return dev_err_probe(tas_priv->dev, ret,811"Platform not supported\n");812813tasdevice_spi_init(tas_priv);814815pm_runtime_set_autosuspend_delay(tas_priv->dev, 3000);816pm_runtime_use_autosuspend(tas_priv->dev);817pm_runtime_set_active(tas_priv->dev);818pm_runtime_get_noresume(tas_priv->dev);819pm_runtime_enable(tas_priv->dev);820821pm_runtime_put_autosuspend(tas_priv->dev);822823ret = component_add(tas_priv->dev, &tas2781_hda_comp_ops);824if (ret) {825dev_err(tas_priv->dev, "Register component fail: %d\n", ret);826pm_runtime_disable(tas_priv->dev);827tas2781_hda_remove(&spi->dev, &tas2781_hda_comp_ops);828}829830return ret;831}832833static void tas2781_hda_spi_remove(struct spi_device *spi)834{835tas2781_hda_remove(&spi->dev, &tas2781_hda_comp_ops);836}837838static int tas2781_runtime_suspend(struct device *dev)839{840struct tas2781_hda *tas_hda = dev_get_drvdata(dev);841struct tasdevice_priv *tas_priv = tas_hda->priv;842843guard(mutex)(&tas_priv->codec_lock);844845if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK846&& tas_priv->playback_started)847tasdevice_tuning_switch(tas_priv, 1);848849tas_priv->tasdevice[tas_priv->index].cur_book = -1;850tas_priv->tasdevice[tas_priv->index].cur_conf = -1;851852return 0;853}854855static int tas2781_runtime_resume(struct device *dev)856{857struct tas2781_hda *tas_hda = dev_get_drvdata(dev);858struct tasdevice_priv *tas_priv = tas_hda->priv;859860guard(mutex)(&tas_priv->codec_lock);861862if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK863&& tas_priv->playback_started)864tasdevice_tuning_switch(tas_priv, 0);865866return 0;867}868869static int tas2781_system_suspend(struct device *dev)870{871struct tas2781_hda *tas_hda = dev_get_drvdata(dev);872struct tasdevice_priv *tas_priv = tas_hda->priv;873int ret;874875ret = pm_runtime_force_suspend(dev);876if (ret)877return ret;878879/* Shutdown chip before system suspend */880if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK881&& tas_priv->playback_started)882tasdevice_tuning_switch(tas_priv, 1);883884return 0;885}886887static int tas2781_system_resume(struct device *dev)888{889struct tas2781_hda *tas_hda = dev_get_drvdata(dev);890struct tasdevice_priv *tas_priv = tas_hda->priv;891int ret, val;892893ret = pm_runtime_force_resume(dev);894if (ret)895return ret;896897guard(mutex)(&tas_priv->codec_lock);898ret = tas_priv->dev_read(tas_priv, tas_priv->index,899TAS2781_REG_CLK_CONFIG, &val);900if (ret < 0)901return ret;902903if (val == TAS2781_REG_CLK_CONFIG_RESET) {904tas_priv->tasdevice[tas_priv->index].cur_book = -1;905tas_priv->tasdevice[tas_priv->index].cur_conf = -1;906tas_priv->tasdevice[tas_priv->index].cur_prog = -1;907908ret = tasdevice_prmg_load(tas_priv, 0);909if (ret < 0) {910dev_err(tas_priv->dev,911"FW download failed = %d\n", ret);912return ret;913}914tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;915916if (tas_priv->playback_started)917tasdevice_tuning_switch(tas_priv, 0);918}919920return ret;921}922923static const struct dev_pm_ops tas2781_hda_pm_ops = {924RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)925SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)926};927928static const struct spi_device_id tas2781_hda_spi_id[] = {929{ "tas2781-hda", },930{}931};932933static const struct acpi_device_id tas2781_acpi_hda_match[] = {934{"TXNW2781", },935{}936};937MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);938939static struct spi_driver tas2781_hda_spi_driver = {940.driver = {941.name = "tas2781-hda",942.acpi_match_table = tas2781_acpi_hda_match,943.pm = &tas2781_hda_pm_ops,944},945.id_table = tas2781_hda_spi_id,946.probe = tas2781_hda_spi_probe,947.remove = tas2781_hda_spi_remove,948};949module_spi_driver(tas2781_hda_spi_driver);950951MODULE_DESCRIPTION("TAS2781 HDA SPI Driver");952MODULE_AUTHOR("Baojun, Xu, <[email protected]>");953MODULE_LICENSE("GPL");954MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");955MODULE_IMPORT_NS("SND_HDA_SCODEC_TAS2781");956957958