Path: blob/master/sound/hda/codecs/side-codecs/tas2781_hda_spi.c
52819 views
// SPDX-License-Identifier: GPL-2.01//2// TAS2781 HDA SPI driver3//4// Copyright 2024 - 2026 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)4344/* System Reset Check Register */45#define TAS2781_REG_CLK_CONFIG TASDEVICE_REG(0x0, 0x0, 0x5c)46#define TAS2781_REG_CLK_CONFIG_RESET 0x194748struct tas2781_hda_spi_priv {49struct snd_kcontrol *snd_ctls[3];50};5152static const struct regmap_range_cfg tasdevice_ranges[] = {53{54.range_min = 0,55.range_max = TASDEVICE_RANGE_MAX_SIZE,56.selector_reg = TASDEVICE_PAGE_SELECT,57.selector_mask = GENMASK(7, 0),58.selector_shift = 0,59.window_start = 0,60.window_len = TASDEVICE_WIN_LEN,61},62};6364static const struct regmap_config tasdevice_regmap = {65.reg_bits = 8,66.val_bits = 8,67.zero_flag_mask = true,68.read_flag_mask = 0x01,69.reg_shift = -1,70.cache_type = REGCACHE_NONE,71.ranges = tasdevice_ranges,72.num_ranges = ARRAY_SIZE(tasdevice_ranges),73.max_register = TASDEVICE_RANGE_MAX_SIZE,74};7576static int tasdevice_spi_dev_read(struct tasdevice_priv *tas_priv,77unsigned short chn, unsigned int reg, unsigned int *val)78{79int ret;8081/*82* In our TAS2781 SPI mode, if read from other book (not book 0),83* or read from page number larger than 1 in book 0, one more byte84* read is needed, and first byte is a dummy byte, need to be ignored.85*/86if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) {87unsigned char data[2];8889ret = tasdevice_dev_bulk_read(tas_priv, chn, reg,90data, sizeof(data));91*val = data[1];92} else {93ret = tasdevice_dev_read(tas_priv, chn, reg, val);94}95if (ret < 0)96dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);9798return ret;99}100101static int tasdevice_spi_dev_bulk_read(struct tasdevice_priv *tas_priv,102unsigned short chn, unsigned int reg, unsigned char *data,103unsigned int len)104{105int ret;106107/*108* In our TAS2781 SPI mode, if read from other book (not book 0),109* or read from page number larger than 1 in book 0, one more byte110* read is needed, and first byte is a dummy byte, need to be ignored.111*/112if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) {113unsigned char buf[TASDEVICE_WIN_LEN + 1];114115ret = tasdevice_dev_bulk_read(tas_priv, chn, reg,116buf, len + 1);117memcpy(data, buf + 1, len);118} else {119ret = tasdevice_dev_bulk_read(tas_priv, chn, reg, data, len);120}121if (ret < 0)122dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);123124return ret;125}126127static int tasdevice_spi_dev_update_bits(struct tasdevice_priv *tas_priv,128unsigned short chn, unsigned int reg, unsigned int mask,129unsigned int value)130{131int ret, val;132133/*134* In our TAS2781 SPI mode, read/write was masked in last bit of135* address, it cause regmap_update_bits() not work as expected.136*/137ret = tasdevice_dev_read(tas_priv, chn, reg, &val);138if (ret < 0) {139dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);140return ret;141}142143ret = tasdevice_dev_write(tas_priv, chn, TASDEVICE_PAGE_REG(reg),144(val & ~mask) | (mask & value));145if (ret < 0)146dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);147148return ret;149}150151static int tasdevice_spi_change_chn_book(struct tasdevice_priv *p,152unsigned short chn, int book)153{154int ret = 0;155156if (chn == p->index) {157struct tasdevice *tasdev = &p->tasdevice[chn];158struct regmap *map = p->regmap;159160if (tasdev->cur_book != book) {161ret = regmap_write(map, TASDEVICE_BOOKCTL_REG, book);162if (ret < 0)163dev_err(p->dev, "%s, E=%d\n", __func__, ret);164else165tasdev->cur_book = book;166}167} else {168ret = -EXDEV;169dev_dbg(p->dev, "Not error, %s ignore channel(%d)\n",170__func__, chn);171}172173return ret;174}175176static void tas2781_spi_reset(struct tasdevice_priv *tas_dev)177{178int ret;179180if (tas_dev->reset) {181gpiod_set_value_cansleep(tas_dev->reset, 0);182fsleep(800);183gpiod_set_value_cansleep(tas_dev->reset, 1);184} else {185ret = tasdevice_dev_write(tas_dev, tas_dev->index,186TASDEVICE_REG_SWRESET, TASDEVICE_REG_SWRESET_RESET);187if (ret < 0) {188dev_err(tas_dev->dev, "dev sw-reset fail, %d\n", ret);189return;190}191fsleep(1000);192}193}194195static int tascodec_spi_init(struct tasdevice_priv *tas_priv,196void *codec, struct module *module,197void (*cont)(const struct firmware *fw, void *context))198{199int ret;200201/*202* Codec Lock Hold to ensure that codec_probe and firmware parsing and203* loading do not simultaneously execute.204*/205guard(mutex)(&tas_priv->codec_lock);206207scnprintf(tas_priv->rca_binaryname,208sizeof(tas_priv->rca_binaryname), "%sRCA%d.bin",209tas_priv->dev_name, tas_priv->ndev);210crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL);211tas_priv->codec = codec;212ret = request_firmware_nowait(module, FW_ACTION_UEVENT,213tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv,214cont);215if (ret)216dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n",217ret);218219return ret;220}221222static void tasdevice_spi_init(struct tasdevice_priv *tas_priv)223{224tas_priv->tasdevice[tas_priv->index].cur_book = -1;225tas_priv->tasdevice[tas_priv->index].cur_conf = -1;226tas_priv->tasdevice[tas_priv->index].cur_prog = -1;227228tas_priv->isspi = true;229230tas_priv->update_bits = tasdevice_spi_dev_update_bits;231tas_priv->change_chn_book = tasdevice_spi_change_chn_book;232tas_priv->dev_read = tasdevice_spi_dev_read;233tas_priv->dev_bulk_read = tasdevice_spi_dev_bulk_read;234235mutex_init(&tas_priv->codec_lock);236}237238static int tasdevice_spi_amp_putvol(struct tasdevice_priv *tas_priv,239struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)240{241unsigned int invert = mc->invert;242unsigned char mask;243int max = mc->max;244int val, ret;245246mask = rounddown_pow_of_two(max);247mask <<= mc->shift;248val = clamp(invert ? max - ucontrol->value.integer.value[0] :249ucontrol->value.integer.value[0], 0, max);250251ret = tasdevice_spi_dev_update_bits(tas_priv, tas_priv->index,252mc->reg, mask, (unsigned int)(val << mc->shift));253if (ret)254dev_err(tas_priv->dev, "set AMP vol error in dev %d\n",255tas_priv->index);256257return ret;258}259260static int tasdevice_spi_amp_getvol(struct tasdevice_priv *tas_priv,261struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)262{263unsigned int invert = mc->invert;264unsigned char mask = 0;265int max = mc->max;266int ret, val;267268ret = tasdevice_spi_dev_read(tas_priv, tas_priv->index, mc->reg, &val);269if (ret) {270dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__);271return ret;272}273274mask = rounddown_pow_of_two(max);275mask <<= mc->shift;276val = (val & mask) >> mc->shift;277val = clamp(invert ? max - val : val, 0, max);278ucontrol->value.integer.value[0] = val;279280return ret;281}282283static int tasdevice_spi_digital_putvol(struct tasdevice_priv *p,284struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)285{286unsigned int invert = mc->invert;287int max = mc->max;288int val, ret;289290val = clamp(invert ? max - ucontrol->value.integer.value[0] :291ucontrol->value.integer.value[0], 0, max);292ret = tasdevice_dev_write(p, p->index, mc->reg, (unsigned int)val);293if (ret)294dev_err(p->dev, "set digital vol err in dev %d\n", p->index);295296return ret;297}298299static int tasdevice_spi_digital_getvol(struct tasdevice_priv *p,300struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)301{302unsigned int invert = mc->invert;303int max = mc->max;304int ret, val;305306ret = tasdevice_spi_dev_read(p, p->index, mc->reg, &val);307if (ret) {308dev_err(p->dev, "%s, get digital vol err\n", __func__);309return ret;310}311312val = clamp(invert ? max - val : val, 0, max);313ucontrol->value.integer.value[0] = val;314315return ret;316}317318static int tas2781_read_acpi(struct tas2781_hda *tas_hda,319const char *hid, int id)320{321struct tasdevice_priv *p = tas_hda->priv;322struct acpi_device *adev;323struct device *physdev;324u32 values[HDA_MAX_COMPONENTS];325const char *property;326size_t nval;327int ret, i;328329adev = acpi_dev_get_first_match_dev(hid, NULL, -1);330if (!adev) {331dev_err(p->dev, "Failed to find ACPI device: %s\n", hid);332return -ENODEV;333}334335strscpy(p->dev_name, hid, sizeof(p->dev_name));336physdev = get_device(acpi_get_first_physical_node(adev));337acpi_dev_put(adev);338339property = "ti,dev-index";340ret = device_property_count_u32(physdev, property);341if (ret <= 0 || ret > ARRAY_SIZE(values)) {342ret = -EINVAL;343goto err;344}345p->ndev = nval = ret;346347ret = device_property_read_u32_array(physdev, property, values, nval);348if (ret)349goto err;350351p->index = U8_MAX;352for (i = 0; i < nval; i++) {353if (values[i] == id) {354p->index = i;355break;356}357}358if (p->index == U8_MAX) {359dev_dbg(p->dev, "No index found in %s\n", property);360ret = -ENODEV;361goto err;362}363364if (p->index == 0) {365/* All of amps share same RESET pin. */366p->reset = devm_gpiod_get_index_optional(physdev, "reset",367p->index, GPIOD_OUT_LOW);368if (IS_ERR(p->reset)) {369ret = PTR_ERR(p->reset);370dev_err_probe(p->dev, ret, "Failed on reset GPIO\n");371goto err;372}373}374put_device(physdev);375376return 0;377err:378dev_err(p->dev, "read acpi error, ret: %d\n", ret);379put_device(physdev);380acpi_dev_put(adev);381382return ret;383}384385static void tas2781_hda_playback_hook(struct device *dev, int action)386{387struct tas2781_hda *tas_hda = dev_get_drvdata(dev);388struct tasdevice_priv *tas_priv = tas_hda->priv;389390if (action == HDA_GEN_PCM_ACT_OPEN) {391pm_runtime_get_sync(dev);392guard(mutex)(&tas_priv->codec_lock);393if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK)394tasdevice_tuning_switch(tas_hda->priv, 0);395} else if (action == HDA_GEN_PCM_ACT_CLOSE) {396guard(mutex)(&tas_priv->codec_lock);397if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK)398tasdevice_tuning_switch(tas_priv, 1);399pm_runtime_put_autosuspend(dev);400}401}402403/*404* tas2781_digital_getvol - get the volum control405* @kcontrol: control pointer406* @ucontrol: User data407*408* Customer Kcontrol for tas2781 is primarily for regmap booking, paging409* depends on internal regmap mechanism.410* tas2781 contains book and page two-level register map, especially411* book switching will set the register BXXP00R7F, after switching to the412* correct book, then leverage the mechanism for paging to access the413* register.414*415* Return 0 if succeeded.416*/417static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,418struct snd_ctl_elem_value *ucontrol)419{420struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);421struct soc_mixer_control *mc =422(struct soc_mixer_control *)kcontrol->private_value;423424guard(mutex)(&tas_priv->codec_lock);425return tasdevice_spi_digital_getvol(tas_priv, ucontrol, mc);426}427428static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,429struct snd_ctl_elem_value *ucontrol)430{431struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);432struct soc_mixer_control *mc =433(struct soc_mixer_control *)kcontrol->private_value;434435guard(mutex)(&tas_priv->codec_lock);436return tasdevice_spi_amp_getvol(tas_priv, ucontrol, mc);437}438439static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol,440struct snd_ctl_elem_value *ucontrol)441{442struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);443struct soc_mixer_control *mc =444(struct soc_mixer_control *)kcontrol->private_value;445446guard(mutex)(&tas_priv->codec_lock);447return tasdevice_spi_digital_putvol(tas_priv, ucontrol, mc);448}449450static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,451struct snd_ctl_elem_value *ucontrol)452{453struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);454struct soc_mixer_control *mc =455(struct soc_mixer_control *)kcontrol->private_value;456457guard(mutex)(&tas_priv->codec_lock);458return tasdevice_spi_amp_putvol(tas_priv, ucontrol, mc);459}460461static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol,462struct snd_ctl_elem_value *ucontrol)463{464struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);465466ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;467dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,468str_on_off(tas_priv->force_fwload_status));469470return 0;471}472473static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,474struct snd_ctl_elem_value *ucontrol)475{476struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);477bool change, val = (bool)ucontrol->value.integer.value[0];478479if (tas_priv->force_fwload_status == val) {480change = false;481} else {482change = true;483tas_priv->force_fwload_status = val;484}485dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,486str_on_off(tas_priv->force_fwload_status));487488return change;489}490491static struct snd_kcontrol_new tas2781_snd_ctls[] = {492ACARD_SINGLE_RANGE_EXT_TLV(NULL, TAS2781_AMP_LEVEL, 1, 0, 20, 0,493tas2781_amp_getvol, tas2781_amp_putvol,494tas2781_amp_tlv),495ACARD_SINGLE_RANGE_EXT_TLV(NULL, TAS2781_DVC_LVL, 0, 0, 200, 1,496tas2781_digital_getvol, tas2781_digital_putvol,497tas2781_dvc_tlv),498ACARD_SINGLE_BOOL_EXT(NULL, 0, tas2781_force_fwload_get,499tas2781_force_fwload_put),500};501502static struct snd_kcontrol_new tas2781_prof_ctl = {503.iface = SNDRV_CTL_ELEM_IFACE_CARD,504.info = tasdevice_info_profile,505.get = tasdevice_get_profile_id,506.put = tasdevice_set_profile_id,507};508509static struct snd_kcontrol_new tas2781_dsp_ctls[] = {510/* Speaker Program */511{512.iface = SNDRV_CTL_ELEM_IFACE_CARD,513.info = tasdevice_info_programs,514.get = tasdevice_program_get,515.put = tasdevice_program_put,516},517/* Speaker Config */518{519.iface = SNDRV_CTL_ELEM_IFACE_CARD,520.info = tasdevice_info_config,521.get = tasdevice_config_get,522.put = tasdevice_config_put,523},524};525526static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)527{528struct hda_codec *codec = tas_hda->priv->codec;529struct tas2781_hda_spi_priv *h_priv = tas_hda->hda_priv;530531snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);532533snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);534535for (int i = ARRAY_SIZE(h_priv->snd_ctls) - 1; i >= 0; i--)536snd_ctl_remove(codec->card, h_priv->snd_ctls[i]);537538snd_ctl_remove(codec->card, tas_hda->prof_ctl);539}540541static int tas2781_hda_spi_prf_ctl(struct tas2781_hda *h)542{543struct tasdevice_priv *p = h->priv;544struct hda_codec *c = p->codec;545char name[64];546int rc;547548snprintf(name, sizeof(name), "Speaker-%d Profile Id", p->index);549tas2781_prof_ctl.name = name;550h->prof_ctl = snd_ctl_new1(&tas2781_prof_ctl, p);551rc = snd_ctl_add(c->card, h->prof_ctl);552if (rc)553dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",554tas2781_prof_ctl.name, rc);555return rc;556}557558static int tas2781_hda_spi_snd_ctls(struct tas2781_hda *h)559{560struct tas2781_hda_spi_priv *h_priv = h->hda_priv;561struct tasdevice_priv *p = h->priv;562struct hda_codec *c = p->codec;563char name[64];564int i = 0;565int rc;566567snprintf(name, sizeof(name), "Speaker-%d Analog Volume", p->index);568tas2781_snd_ctls[i].name = name;569h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);570rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);571if (rc) {572dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",573tas2781_snd_ctls[i].name, rc);574return rc;575}576i++;577snprintf(name, sizeof(name), "Speaker-%d Digital Volume", p->index);578tas2781_snd_ctls[i].name = name;579h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);580rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);581if (rc) {582dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",583tas2781_snd_ctls[i].name, rc);584return rc;585}586i++;587snprintf(name, sizeof(name), "Froce Speaker-%d FW Load", p->index);588tas2781_snd_ctls[i].name = name;589h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);590rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);591if (rc) {592dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",593tas2781_snd_ctls[i].name, rc);594}595return rc;596}597598static int tas2781_hda_spi_dsp_ctls(struct tas2781_hda *h)599{600struct tasdevice_priv *p = h->priv;601struct hda_codec *c = p->codec;602char name[64];603int i = 0;604int rc;605606snprintf(name, sizeof(name), "Speaker-%d Program Id", p->index);607tas2781_dsp_ctls[i].name = name;608h->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_ctls[i], p);609rc = snd_ctl_add(c->card, h->dsp_prog_ctl);610if (rc) {611dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",612tas2781_dsp_ctls[i].name, rc);613return rc;614}615i++;616snprintf(name, sizeof(name), "Speaker-%d Config Id", p->index);617tas2781_dsp_ctls[i].name = name;618h->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_ctls[i], p);619rc = snd_ctl_add(c->card, h->dsp_conf_ctl);620if (rc) {621dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",622tas2781_dsp_ctls[i].name, rc);623}624625return rc;626}627628static void tasdev_fw_ready(const struct firmware *fmw, void *context)629{630struct tasdevice_priv *tas_priv = context;631struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);632struct hda_codec *codec = tas_priv->codec;633int ret, val;634635guard(pm_runtime_active_auto)(tas_priv->dev);636guard(mutex)(&tas_priv->codec_lock);637638ret = tasdevice_rca_parser(tas_priv, fmw);639if (ret)640goto out;641642/* Add control one time only. */643ret = tas2781_hda_spi_prf_ctl(tas_hda);644if (ret)645goto out;646647ret = tas2781_hda_spi_snd_ctls(tas_hda);648if (ret)649goto out;650651tasdevice_dsp_remove(tas_priv);652653tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;654scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%04X-%01d.bin",655lower_16_bits(codec->core.subsystem_id), tas_priv->index);656ret = tasdevice_dsp_parser(tas_priv);657if (ret) {658dev_err(tas_priv->dev, "dspfw load %s error\n",659tas_priv->coef_binaryname);660tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;661goto out;662}663664ret = tas2781_hda_spi_dsp_ctls(tas_hda);665if (ret)666goto out;667/* Perform AMP reset before firmware download. */668tas2781_spi_reset(tas_priv);669tas_priv->rcabin.profile_cfg_id = 0;670671tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;672ret = tasdevice_spi_dev_read(tas_priv, tas_priv->index,673TAS2781_REG_CLK_CONFIG, &val);674if (ret < 0)675goto out;676677if (val == TAS2781_REG_CLK_CONFIG_RESET) {678ret = tasdevice_prmg_load(tas_priv, 0);679if (ret < 0) {680dev_err(tas_priv->dev, "FW download failed = %d\n",681ret);682goto out;683}684tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;685}686if (tas_priv->fmw->nr_programs > 0)687tas_priv->tasdevice[tas_priv->index].cur_prog = 0;688if (tas_priv->fmw->nr_configurations > 0)689tas_priv->tasdevice[tas_priv->index].cur_conf = 0;690691/*692* If calibrated data occurs error, dsp will still works with default693* calibrated data inside algo.694*/695tas2781_save_calibration(tas_hda);696out:697release_firmware(fmw);698}699700static int tas2781_hda_bind(struct device *dev, struct device *master,701void *master_data)702{703struct tas2781_hda *tas_hda = dev_get_drvdata(dev);704struct hda_component_parent *parent = master_data;705struct hda_component *comp;706struct hda_codec *codec;707int ret;708709comp = hda_component_from_index(parent, tas_hda->priv->index);710if (!comp)711return -EINVAL;712713if (comp->dev)714return -EBUSY;715716codec = parent->codec;717718guard(pm_runtime_active_auto)(dev);719720comp->dev = dev;721722strscpy(comp->name, dev_name(dev), sizeof(comp->name));723724ret = tascodec_spi_init(tas_hda->priv, codec, THIS_MODULE,725tasdev_fw_ready);726if (!ret)727comp->playback_hook = tas2781_hda_playback_hook;728729/* Only HP Laptop support SPI-based TAS2781 */730tas_hda->catlog_id = HP;731732return ret;733}734735static void tas2781_hda_unbind(struct device *dev, struct device *master,736void *master_data)737{738struct tas2781_hda *tas_hda = dev_get_drvdata(dev);739struct hda_component_parent *parent = master_data;740struct tasdevice_priv *tas_priv = tas_hda->priv;741struct hda_component *comp;742743comp = hda_component_from_index(parent, tas_priv->index);744if (comp && (comp->dev == dev)) {745comp->dev = NULL;746memset(comp->name, 0, sizeof(comp->name));747comp->playback_hook = NULL;748}749750tas2781_hda_remove_controls(tas_hda);751752tasdevice_config_info_remove(tas_priv);753tasdevice_dsp_remove(tas_priv);754755tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;756}757758static const struct component_ops tas2781_hda_comp_ops = {759.bind = tas2781_hda_bind,760.unbind = tas2781_hda_unbind,761};762763static int tas2781_hda_spi_probe(struct spi_device *spi)764{765struct tas2781_hda_spi_priv *hda_priv;766struct tasdevice_priv *tas_priv;767struct tas2781_hda *tas_hda;768const char *device_name;769int ret = 0;770771tas_hda = devm_kzalloc(&spi->dev, sizeof(*tas_hda), GFP_KERNEL);772if (!tas_hda)773return -ENOMEM;774775hda_priv = devm_kzalloc(&spi->dev, sizeof(*hda_priv), GFP_KERNEL);776if (!hda_priv)777return -ENOMEM;778779tas_hda->hda_priv = hda_priv;780spi->max_speed_hz = TAS2781_SPI_MAX_FREQ;781782tas_priv = devm_kzalloc(&spi->dev, sizeof(*tas_priv), GFP_KERNEL);783if (!tas_priv)784return -ENOMEM;785tas_priv->dev = &spi->dev;786tas_hda->priv = tas_priv;787tas_priv->regmap = devm_regmap_init_spi(spi, &tasdevice_regmap);788if (IS_ERR(tas_priv->regmap)) {789ret = PTR_ERR(tas_priv->regmap);790dev_err(tas_priv->dev, "Failed to allocate regmap: %d\n",791ret);792return ret;793}794if (strstr(dev_name(&spi->dev), "TXNW2781")) {795device_name = "TXNW2781";796} else {797dev_err(tas_priv->dev, "Unmatched spi dev %s\n",798dev_name(&spi->dev));799return -ENODEV;800}801802tas_priv->irq = spi->irq;803dev_set_drvdata(&spi->dev, tas_hda);804ret = tas2781_read_acpi(tas_hda, device_name,805spi_get_chipselect(spi, 0));806if (ret)807return dev_err_probe(tas_priv->dev, ret,808"Platform not supported\n");809810tasdevice_spi_init(tas_priv);811812pm_runtime_set_autosuspend_delay(tas_priv->dev, 3000);813pm_runtime_use_autosuspend(tas_priv->dev);814pm_runtime_set_active(tas_priv->dev);815pm_runtime_get_noresume(tas_priv->dev);816pm_runtime_enable(tas_priv->dev);817818pm_runtime_put_autosuspend(tas_priv->dev);819820ret = component_add(tas_priv->dev, &tas2781_hda_comp_ops);821if (ret) {822dev_err(tas_priv->dev, "Register component fail: %d\n", ret);823pm_runtime_disable(tas_priv->dev);824tas2781_hda_remove(&spi->dev, &tas2781_hda_comp_ops);825}826827return ret;828}829830static void tas2781_hda_spi_remove(struct spi_device *spi)831{832tas2781_hda_remove(&spi->dev, &tas2781_hda_comp_ops);833}834835static int tas2781_runtime_suspend(struct device *dev)836{837struct tas2781_hda *tas_hda = dev_get_drvdata(dev);838struct tasdevice_priv *tas_priv = tas_hda->priv;839840guard(mutex)(&tas_priv->codec_lock);841842if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK843&& tas_priv->playback_started)844tasdevice_tuning_switch(tas_priv, 1);845846tas_priv->tasdevice[tas_priv->index].cur_book = -1;847tas_priv->tasdevice[tas_priv->index].cur_conf = -1;848849return 0;850}851852static int tas2781_runtime_resume(struct device *dev)853{854struct tas2781_hda *tas_hda = dev_get_drvdata(dev);855struct tasdevice_priv *tas_priv = tas_hda->priv;856857guard(mutex)(&tas_priv->codec_lock);858859if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK860&& tas_priv->playback_started)861tasdevice_tuning_switch(tas_priv, 0);862863return 0;864}865866static int tas2781_system_suspend(struct device *dev)867{868struct tas2781_hda *tas_hda = dev_get_drvdata(dev);869struct tasdevice_priv *tas_priv = tas_hda->priv;870int ret;871872ret = pm_runtime_force_suspend(dev);873if (ret)874return ret;875876/* Shutdown chip before system suspend */877if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK878&& tas_priv->playback_started)879tasdevice_tuning_switch(tas_priv, 1);880881return 0;882}883884static int tas2781_system_resume(struct device *dev)885{886struct tas2781_hda *tas_hda = dev_get_drvdata(dev);887struct tasdevice_priv *tas_priv = tas_hda->priv;888int ret, val;889890ret = pm_runtime_force_resume(dev);891if (ret)892return ret;893894guard(mutex)(&tas_priv->codec_lock);895ret = tas_priv->dev_read(tas_priv, tas_priv->index,896TAS2781_REG_CLK_CONFIG, &val);897if (ret < 0)898return ret;899900if (val == TAS2781_REG_CLK_CONFIG_RESET) {901tas_priv->tasdevice[tas_priv->index].cur_book = -1;902tas_priv->tasdevice[tas_priv->index].cur_conf = -1;903tas_priv->tasdevice[tas_priv->index].cur_prog = -1;904905ret = tasdevice_prmg_load(tas_priv, 0);906if (ret < 0) {907dev_err(tas_priv->dev,908"FW download failed = %d\n", ret);909return ret;910}911tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;912913if (tas_priv->playback_started)914tasdevice_tuning_switch(tas_priv, 0);915}916917return ret;918}919920static const struct dev_pm_ops tas2781_hda_pm_ops = {921RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)922SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)923};924925static const struct spi_device_id tas2781_hda_spi_id[] = {926{ "tas2781-hda", },927{}928};929930static const struct acpi_device_id tas2781_acpi_hda_match[] = {931{"TXNW2781", },932{}933};934MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);935936static struct spi_driver tas2781_hda_spi_driver = {937.driver = {938.name = "tas2781-hda",939.acpi_match_table = tas2781_acpi_hda_match,940.pm = &tas2781_hda_pm_ops,941},942.id_table = tas2781_hda_spi_id,943.probe = tas2781_hda_spi_probe,944.remove = tas2781_hda_spi_remove,945};946module_spi_driver(tas2781_hda_spi_driver);947948MODULE_DESCRIPTION("TAS2781 HDA SPI Driver");949MODULE_AUTHOR("Baojun, Xu, <[email protected]>");950MODULE_LICENSE("GPL");951MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");952MODULE_IMPORT_NS("SND_HDA_SCODEC_TAS2781");953954955