From nobody Tue May 21 09:58:09 2024 Received: from lelv0143.ext.ti.com (lelv0143.ext.ti.com [198.47.23.248]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BC3353AC for ; Thu, 18 Apr 2024 04:04:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.47.23.248 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713413070; cv=none; b=OfNXSoTV638VQ7UrWeYWJITk8LGxnDz8o2T7lnH3PYQwsV5tyUCNnPmiRZp939geiuZFXXEAsF7CFDDNgFl9aI+BYvRo+gKgIBnVsZG/0qHacb1sZs0oHexvmqTr5J/jPsUQahoztWafREeNIeWp7jRAmWbA6xso8DNUKqhN9EA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713413070; c=relaxed/simple; bh=0H/hY/3/OFXlOeFs7WDfWj82kmVBxqPisc+eHrNcraM=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=slfyu4W/xk8REK52a0u23m7X0c94SCEw1cF1vJWmB3oVYqVd9peIZNppR/LjO57EuM5K3Dk1I1sVVgRjwWMJuji+sR/YJHMCoHrctfqV1wmaww0KWEu2+ezbvAX0FDfAOqyuX6hkAn/54YKaUPSKyW3HjVVQYMxlQKEC5+rqoiY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com; spf=pass smtp.mailfrom=ti.com; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b=NK0xGSlD; arc=none smtp.client-ip=198.47.23.248 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ti.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b="NK0xGSlD" Received: from fllv0035.itg.ti.com ([10.64.41.0]) by lelv0143.ext.ti.com (8.15.2/8.15.2) with ESMTP id 43I43MDs128724; Wed, 17 Apr 2024 23:03:22 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1713413002; bh=S5KMrmk3XdgunGHKlkLZgGl0ODGZNDpEVrJblFUqXuY=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=NK0xGSlDyvcU0pVL2gtkvQgnByM6sWRHxAqGgXykjb7OrUQrBlU19t869gKkB/JmJ UJV1gBJLw1GbR74ZjiVVWEfNeSt6ywpVs1Ul0SnfkufKEPNEo9FLunkrANIWtSOFkH lbCreSYqfGcE9aOF0R93AASvrC6aYunIHSrZAjU4= Received: from DFLE102.ent.ti.com (dfle102.ent.ti.com [10.64.6.23]) by fllv0035.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 43I43MYp016173 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 17 Apr 2024 23:03:22 -0500 Received: from DFLE109.ent.ti.com (10.64.6.30) by DFLE102.ent.ti.com (10.64.6.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23; Wed, 17 Apr 2024 23:03:21 -0500 Received: from lelvsmtp5.itg.ti.com (10.180.75.250) by DFLE109.ent.ti.com (10.64.6.30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23 via Frontend Transport; Wed, 17 Apr 2024 23:03:21 -0500 Received: from lelvsmtp6.itg.ti.com ([10.250.165.138]) by lelvsmtp5.itg.ti.com (8.15.2/8.15.2) with ESMTP id 43I439il070557; Wed, 17 Apr 2024 23:03:16 -0500 From: Baojun Xu To: CC: , , , , , , , , <13916275206@139.com>, , , , , , , , , Subject: [PATCH v3 1/3] ALSA: hda/tas2781: Modification for add tas2781 driver for SPI Date: Thu, 18 Apr 2024 12:02:37 +0800 Message-ID: <20240418040240.2337-2-baojun.xu@ti.com> X-Mailer: git-send-email 2.43.0.windows.1 In-Reply-To: <20240418040240.2337-1-baojun.xu@ti.com> References: <20240418040240.2337-1-baojun.xu@ti.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Content-Type: text/plain; charset="utf-8" Integrate tas2781 configs for HP Laptops. Every tas2781 in the laptop will work as a single speaker on SPI bus. The code support realtek as the primary codec. Signed-off-by: Baojun Xu --- v3: - Move HID up to above /* Non-conforming _HID ... */ in scan.c, for avoid misunderstanding. - Move HID up to above /* Non-conforming _HID ... */ in serial-multi-instantiate.c, for avoid misunderstanding. - Change objs to y for snd-hda-scodec-tas2781-spi- in Makefile. --- drivers/acpi/scan.c | 1 + drivers/platform/x86/serial-multi-instantiate.c | 10 ++++++++++ sound/pci/hda/Kconfig | 15 +++++++++++++++ sound/pci/hda/Makefile | 2 ++ sound/pci/hda/patch_realtek.c | 13 +++++++++++++ 5 files changed, 41 insertions(+) diff --git a/drivers/acpi/scan.c b/drivers/acpi/scan.c index d1464324de95..371a42a13fc8 100644 --- a/drivers/acpi/scan.c +++ b/drivers/acpi/scan.c @@ -1765,6 +1765,7 @@ static bool acpi_device_enumeration_by_parent(struct = acpi_device *device) {"CSC3557", }, {"INT33FE", }, {"INT3515", }, + {"TXNW2781", }, /* Non-conforming _HID for Cirrus Logic already released */ {"CLSA0100", }, {"CLSA0101", }, diff --git a/drivers/platform/x86/serial-multi-instantiate.c b/drivers/plat= form/x86/serial-multi-instantiate.c index 97b9c6392230..2dcf66bb6216 100644 --- a/drivers/platform/x86/serial-multi-instantiate.c +++ b/drivers/platform/x86/serial-multi-instantiate.c @@ -368,6 +368,15 @@ static const struct smi_node cs35l57_hda =3D { .bus_type =3D SMI_AUTO_DETECT, }; =20 +static const struct smi_node tas2781_hda =3D { + .instances =3D { + { "tas2781-hda", IRQ_RESOURCE_AUTO, 0 }, + { "tas2781-hda", IRQ_RESOURCE_AUTO, 0 }, + {} + }, + .bus_type =3D SMI_AUTO_DETECT, +}; + /* * Note new device-ids must also be added to ignore_serial_bus_ids in * drivers/acpi/scan.c: acpi_device_enumeration_by_parent(). @@ -380,6 +389,7 @@ static const struct acpi_device_id smi_acpi_ids[] =3D { { "CSC3556", (unsigned long)&cs35l56_hda }, { "CSC3557", (unsigned long)&cs35l57_hda }, { "INT3515", (unsigned long)&int3515_data }, + { "TXNW2781", (unsigned long)&tas2781_hda }, /* Non-conforming _HID for Cirrus Logic already released */ { "CLSA0100", (unsigned long)&cs35l41_hda }, { "CLSA0101", (unsigned long)&cs35l41_hda }, diff --git a/sound/pci/hda/Kconfig b/sound/pci/hda/Kconfig index f806636242ee..f25ad9ea51d9 100644 --- a/sound/pci/hda/Kconfig +++ b/sound/pci/hda/Kconfig @@ -202,6 +202,21 @@ config SND_HDA_SCODEC_TAS2781_I2C comment "Set to Y if you want auto-loading the side codec driver" depends on SND_HDA=3Dy && SND_HDA_SCODEC_TAS2781_I2C=3Dm =20 +config SND_HDA_SCODEC_TAS2781_SPI + tristate "Build TAS2781 HD-audio side codec support for SPI Bus" + depends on SPI_MASTER + depends on ACPI + depends on EFI + depends on SND_SOC + select SND_HDA_SCODEC_COMPONENT + select CRC32_SARWATE + help + Say Y or M here to include TAS2781 SPI HD-audio side codec support + in snd-hda-intel driver, such as ALC287. + +comment "Set to Y if you want auto-loading the side codec driver" + depends on SND_HDA=3Dy && SND_HDA_SCODEC_TAS2781_SPI=3Dm + config SND_HDA_CODEC_REALTEK tristate "Build Realtek HD-audio codec support" select SND_HDA_GENERIC diff --git a/sound/pci/hda/Makefile b/sound/pci/hda/Makefile index 13e04e1f65de..ce5da317578f 100644 --- a/sound/pci/hda/Makefile +++ b/sound/pci/hda/Makefile @@ -39,6 +39,7 @@ snd-hda-scodec-cs35l56-spi-objs :=3D cs35l56_hda_spi.o snd-hda-cs-dsp-ctls-objs :=3D hda_cs_dsp_ctl.o snd-hda-scodec-component-objs :=3D hda_component.o snd-hda-scodec-tas2781-i2c-objs :=3D tas2781_hda_i2c.o +snd-hda-scodec-tas2781-spi-y :=3D tas2781_hda_spi.o tas2781_spi_fwlib.o =20 # common driver obj-$(CONFIG_SND_HDA) :=3D snd-hda-codec.o @@ -70,6 +71,7 @@ obj-$(CONFIG_SND_HDA_SCODEC_CS35L56_SPI) +=3D snd-hda-sco= dec-cs35l56-spi.o obj-$(CONFIG_SND_HDA_CS_DSP_CONTROLS) +=3D snd-hda-cs-dsp-ctls.o obj-$(CONFIG_SND_HDA_SCODEC_COMPONENT) +=3D snd-hda-scodec-component.o obj-$(CONFIG_SND_HDA_SCODEC_TAS2781_I2C) +=3D snd-hda-scodec-tas2781-i2c.o +obj-$(CONFIG_SND_HDA_SCODEC_TAS2781_SPI) +=3D snd-hda-scodec-tas2781-spi.o =20 # this must be the last entry after codec drivers; # otherwise the codec patches won't be hooked before the PCI probe diff --git a/sound/pci/hda/patch_realtek.c b/sound/pci/hda/patch_realtek.c index cdcb28aa9d7b..558c48cc53a1 100644 --- a/sound/pci/hda/patch_realtek.c +++ b/sound/pci/hda/patch_realtek.c @@ -6895,6 +6895,11 @@ static void cs35l56_fixup_spi_four(struct hda_codec = *cdc, const struct hda_fixup comp_generic_fixup(cdc, action, "spi", "CSC3556", "-%s:00-cs35l56-hda.%d"= , 4); } =20 +static void tas2781_fixup_spi_two(struct hda_codec *cdc, const struct hda_= fixup *fix, int action) +{ + comp_generic_fixup(cdc, action, "spi", "TXNW2781", "-%s:00-tas2781-hda.%d= ", 2); +} + static void alc285_fixup_asus_ga403u(struct hda_codec *cdc, const struct h= da_fixup *fix, int action) { /* @@ -7451,6 +7456,7 @@ enum { ALC236_FIXUP_DELL_DUAL_CODECS, ALC287_FIXUP_CS35L41_I2C_2_THINKPAD_ACPI, ALC287_FIXUP_TAS2781_I2C, + ALC245_FIXUP_TAS2781_SPI_2_HP_GPIO_LED, ALC287_FIXUP_YOGA7_14ARB7_I2C, ALC245_FIXUP_HP_MUTE_LED_COEFBIT, ALC245_FIXUP_HP_X360_MUTE_LEDS, @@ -9614,6 +9620,12 @@ static const struct hda_fixup alc269_fixups[] =3D { .chained =3D true, .chain_id =3D ALC285_FIXUP_THINKPAD_HEADSET_JACK, }, + [ALC245_FIXUP_TAS2781_SPI_2_HP_GPIO_LED] =3D { + .type =3D HDA_FIXUP_FUNC, + .v.func =3D tas2781_fixup_spi_two, + .chained =3D true, + .chain_id =3D ALC285_FIXUP_HP_GPIO_LED, + }, [ALC287_FIXUP_YOGA7_14ARB7_I2C] =3D { .type =3D HDA_FIXUP_FUNC, .v.func =3D yoga7_14arb7_fixup_i2c, @@ -10038,6 +10050,7 @@ static const struct snd_pci_quirk alc269_fixup_tbl[= ] =3D { SND_PCI_QUIRK(0x103c, 0x8b8d, "HP", ALC236_FIXUP_HP_GPIO_LED), SND_PCI_QUIRK(0x103c, 0x8b8f, "HP", ALC245_FIXUP_CS35L41_SPI_4_HP_GPIO_LE= D), SND_PCI_QUIRK(0x103c, 0x8b92, "HP", ALC245_FIXUP_CS35L41_SPI_2_HP_GPIO_LE= D), + SND_PCI_QUIRK(0x103c, 0x8b93, "HP", ALC245_FIXUP_TAS2781_SPI_2_HP_GPIO_LE= D), SND_PCI_QUIRK(0x103c, 0x8b96, "HP", ALC236_FIXUP_HP_MUTE_LED_MICMUTE_VREF= ), SND_PCI_QUIRK(0x103c, 0x8b97, "HP", ALC236_FIXUP_HP_MUTE_LED_MICMUTE_VREF= ), SND_PCI_QUIRK(0x103c, 0x8bdd, "HP Envy 17", ALC287_FIXUP_CS35L41_I2C_2), --=20 2.40.1 From nobody Tue May 21 09:58:09 2024 Received: from lelv0143.ext.ti.com (lelv0143.ext.ti.com [198.47.23.248]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1BBF72570 for ; Thu, 18 Apr 2024 04:04:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.47.23.248 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713413072; cv=none; b=RgoeCjOFroXHiKzhAJ35iC2F8sKegnTdV3DXs8e0FtgojwpnSC1bys+N4tNM2Wb11ztu8OdWjuDqadVGACnQI6rOzOs2ajR2KRFVnq5T1gVS4WlvtIpNNZ6vkS5KESB5CgPT8V+L9RwQmaF28HTsPbz4+ffTXgfvHKIktStitM0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713413072; c=relaxed/simple; bh=JFGM6C4Eytt2MX9XDuY6MRHPnAVaXFAkPCU8eQk2e3U=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=jqqmPZBp7UyxrZ5NP6hdc6/RWm6nxkUvDFeUda6UV0N9iAimoszXNN1drl3tKMsX1/u4LDuU3dgpuVDt4aYnkpwbWUMxYDbZ8+bBupm2ID5NaiCifrxs4ClSJ4UxMbAhhHI1uaOEiSJ5xbQvlGLJCzFz4LuQMfFoSGVOkw+quHA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com; spf=pass smtp.mailfrom=ti.com; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b=DUM7aL+v; arc=none smtp.client-ip=198.47.23.248 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ti.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b="DUM7aL+v" Received: from fllv0034.itg.ti.com ([10.64.40.246]) by lelv0143.ext.ti.com (8.15.2/8.15.2) with ESMTP id 43I43TCk128742; Wed, 17 Apr 2024 23:03:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1713413009; bh=yBEdHEQtmE2mTguI4Uf86VpeX1ZAHlTtkRrjDBx/zB0=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=DUM7aL+v0Y9b2Gw+dxq/EGP9d7fGzpak740k/6vGXxubiP4RGHE8LxrG88VfEXJU5 /RY9CXLJaJggCEA/qkgz4xH+RqMmAfaaxydALd3R/XDEVPyvEHG1h1wdIh5V8ve+/a pEbkv7swjoX+REYLEUP5Adjop2KV7n+SdvPcTtzE= Received: from DFLE100.ent.ti.com (dfle100.ent.ti.com [10.64.6.21]) by fllv0034.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 43I43Trn125991 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 17 Apr 2024 23:03:29 -0500 Received: from DFLE104.ent.ti.com (10.64.6.25) by DFLE100.ent.ti.com (10.64.6.21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23; Wed, 17 Apr 2024 23:03:29 -0500 Received: from lelvsmtp5.itg.ti.com (10.180.75.250) by DFLE104.ent.ti.com (10.64.6.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23 via Frontend Transport; Wed, 17 Apr 2024 23:03:29 -0500 Received: from lelvsmtp6.itg.ti.com ([10.250.165.138]) by lelvsmtp5.itg.ti.com (8.15.2/8.15.2) with ESMTP id 43I439im070557; Wed, 17 Apr 2024 23:03:22 -0500 From: Baojun Xu To: CC: , , , , , , , , <13916275206@139.com>, , , , , , , , , Subject: [PATCH v3 2/3] ALSA: hda/tas2781: Main code of tas2781 driver for SPI Date: Thu, 18 Apr 2024 12:02:38 +0800 Message-ID: <20240418040240.2337-3-baojun.xu@ti.com> X-Mailer: git-send-email 2.43.0.windows.1 In-Reply-To: <20240418040240.2337-1-baojun.xu@ti.com> References: <20240418040240.2337-1-baojun.xu@ti.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Content-Type: text/plain; charset="utf-8" Main source code for tas2781 driver for SPI. Signed-off-by: Baojun Xu --- v3: - Change format for some define in tas2781-spi.h. - Add define for TASDEVICE_MAX_BOOK_NUM, TASDEVICE_MAX_PAGE. - Move declare of "enum calib_data {" to header file from source file. - Remove "enum device_catlog_id {" as only one platform was supported now. - Remove "struct calidata", as we will save pure calibration data only. - Remove "struct calidata", "enum device_catlog_id" in tasdevice_priv. - Add calibration_data in tasdevice_priv to save pure calibration data. - Remove declare of tasdevice_save_calibration() and tasdevice_apply_calibration(), as it will be used within same c file. - Add below header files included in tas2781_hda_spi.c: bits.h, mutex.h, property.h, time.h, types.h. - Move two micro define to header file tas2781-spi.h. - Change format of some micro define. - Change format of some structure. - Remove irq in tas2781_hda, as have it already in tasdevice_priv. - Remove some local functions declare as not necessarily. - Return error if regmap_write() failed. - Change fixed value 2 to sizeof(data). - Remove all of EXPORT_SYMBOL_GPL, as all of function was called within same module. - Remove empty line after last return in some functions. - Remove some variable initialyzation. - Remove variable sub, store acpi_subsystem_id directly. - Remove wrong comments for device index, it's must, can't be NULL. - Remove some local variables, use elements in structure directly. - Append commas in last element of an array. - Change calibration data process, didn't save all of calibration data in = EFI, get in local, and save pure calibration data in tasdevice_priv. - Call tasdevice_save_calibration() from function pointer in tasdevice_pri= v; - Remove subid as one platform was supported only, needn't check. - Add initialyzation for local variable. - Move regmap initialyzation to before acpi read. - Call tasdevice_apply_calibration() from function pointer in tasdevice_pr= iv; - Remove MODULE_IMPORT_NS(SND_SOC_TAS2781_FMWLIB), as all functions was performed within same module. --- sound/pci/hda/tas2781-spi.h | 165 ++++ sound/pci/hda/tas2781_hda_spi.c | 1305 +++++++++++++++++++++++++++++++ 2 files changed, 1470 insertions(+) create mode 100644 sound/pci/hda/tas2781-spi.h create mode 100644 sound/pci/hda/tas2781_hda_spi.c diff --git a/sound/pci/hda/tas2781-spi.h b/sound/pci/hda/tas2781-spi.h new file mode 100644 index 000000000000..cf258bf30ad2 --- /dev/null +++ b/sound/pci/hda/tas2781-spi.h @@ -0,0 +1,165 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +// +// ALSA SoC Texas Instruments TAS2781 Audio Smart Amplifier +// +// Copyright (C) 2024 Texas Instruments Incorporated +// https://www.ti.com +// +// The TAS2781 driver implements a flexible and configurable +// algo coefficient setting for TAS2781 chips. +// +// Author: Baojun Xu +// + +#ifndef __TAS2781_SPI_H__ +#define __TAS2781_SPI_H__ + +#include + +#define TASDEVICE_RATES \ + (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000 | \ + SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_88200) + +#define TASDEVICE_FORMATS \ + (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S24_LE | \ + SNDRV_PCM_FMTBIT_S32_LE) + +/*PAGE Control Register (available in page0 of each book) */ +#define TASDEVICE_PAGE_SELECT 0x00 +#define TASDEVICE_BOOKCTL_PAGE 0x00 +#define TASDEVICE_BOOKCTL_REG 127 +#define TASDEVICE_BOOK_ID(reg) (reg / (256 * 128)) +#define TASDEVICE_PAGE_ID(reg) ((reg % (256 * 128)) / 128) +#define TASDEVICE_PAGE_REG(reg) ((reg % (256 * 128)) % 128) +#define TASDEVICE_PGRG(reg) (reg % (256 * 128)) +#define TASDEVICE_REG(book, page, reg) (((book * 256 * 128) + \ + (page * 128)) + reg) + +#define TASDEVICE_MAX_BOOK_NUM 256 +#define TASDEVICE_MAX_PAGE 128 + +/*Software Reset */ +#define TAS2781_REG_SWRESET TASDEVICE_REG(0x0, 0X0, 0x01) +#define TAS2781_REG_SWRESET_RESET BIT(0) + +/*I2C Checksum */ +#define TASDEVICE_Checksum TASDEVICE_REG(0x0, 0x0, 0x7E) + +/* Volume control */ +#define TAS2781_DVC_LVL TASDEVICE_REG(0x0, 0x0, 0x1A) +#define TAS2781_AMP_LEVEL TASDEVICE_REG(0x0, 0x0, 0x03) +#define TAS2781_AMP_LEVEL_MASK GENMASK(5, 1) + +#define TASDEVICE_CMD_SING_W 0x1 +#define TASDEVICE_CMD_BURST 0x2 +#define TASDEVICE_CMD_DELAY 0x3 +#define TASDEVICE_CMD_FIELD_W 0x4 + +#define TAS2781_SPI_MAX_FREQ 4000000 + +#define TASDEVICE_CRC8_POLYNOMIAL 0x4d +#define TASDEVICE_SPEAKER_CALIBRATION_SIZE 20 + +enum calib_data { + R0_VAL =3D 0, + INV_R0, + R0LOW, + POWER, + TLIM, + CALIB_MAX +}; + +struct tasdevice { + struct tasdevice_fw *cali_data_fmw; + unsigned int dev_addr; + unsigned int err_code; + unsigned char cur_book; + short cur_prog; + short cur_conf; + bool is_loading; + bool is_loaderr; +}; + +struct tasdevice_irqinfo { + int irq_gpio; + int irq; +}; + +struct tasdevice_priv { + struct tasdevice tasdevice; + struct tasdevice_irqinfo irq_info; + struct tasdevice_rca rcabin; + struct tasdevice_fw *fmw; + struct gpio_desc *reset; + struct gpio_desc *chipselect; + struct mutex codec_lock; + struct regmap *regmap; + struct device *dev; + struct tm tm; + + const char *acpi_subsystem_id; + unsigned char crc8_lkp_tbl[CRC8_TABLE_SIZE]; + unsigned char coef_binaryname[64]; + unsigned char rca_binaryname[64]; + unsigned char dev_name[32]; + unsigned int magic_num; + unsigned int chip_id; + unsigned int sysclk; + + int cur_prog; + int cur_conf; + int fw_state; + int index; + void *client; + void *codec; + bool force_fwload_status; + bool playback_started; + unsigned int global_addr; + unsigned int calibration_data[CALIB_MAX]; + + int (*fw_parse_variable_header)(struct tasdevice_priv *tas_priv, + const struct firmware *fmw, int offset); + int (*fw_parse_program_data)(struct tasdevice_priv *tas_priv, + struct tasdevice_fw *tas_fmw, + const struct firmware *fmw, int offset); + int (*fw_parse_configuration_data)(struct tasdevice_priv *tas_priv, + struct tasdevice_fw *tas_fmw, + const struct firmware *fmw, int offset); + int (*tasdevice_load_block)(struct tasdevice_priv *tas_priv, + struct tasdev_blk *block); + + int (*save_calibration)(struct tasdevice_priv *tas_priv); + void (*apply_calibration)(struct tasdevice_priv *tas_priv); +}; + +int tascodec_init(struct tasdevice_priv *tas_priv, void *codec, + struct module *module, + void (*cont)(const struct firmware *fw, void *context)); +void tasdevice_remove(struct tasdevice_priv *tas_priv); +int tasdevice_spi_dev_read(struct tasdevice_priv *tas_priv, + unsigned int reg, unsigned int *value); +int tasdevice_spi_dev_write(struct tasdevice_priv *tas_priv, + unsigned int reg, unsigned int value); +int tasdevice_spi_dev_bulk_write( + struct tasdevice_priv *tas_priv, unsigned int reg, + unsigned char *p_data, unsigned int n_length); +int tasdevice_spi_dev_bulk_read(struct tasdevice_priv *tas_priv, + unsigned int reg, unsigned char *p_data, unsigned int n_length); +int tasdevice_spi_dev_update_bits(struct tasdevice_priv *tasdevice, + unsigned int reg, unsigned int mask, unsigned int value); + +void tasdevice_spi_select_cfg_blk(void *context, int conf_no, + unsigned char block_type); +void tasdevice_spi_config_info_remove(void *context); +int tasdevice_spi_dsp_parser(void *context); +int tasdevice_spi_rca_parser(void *context, const struct firmware *fmw); +void tasdevice_spi_dsp_remove(void *context); +void tasdevice_spi_calbin_remove(void *context); +int tasdevice_spi_select_tuningprm_cfg(void *context, int prm, + int cfg_no, int rca_conf_no); +int tasdevice_spi_prmg_load(void *context, int prm_no); +int tasdevice_spi_prmg_calibdata_load(void *context, int prm_no); +void tasdevice_spi_tuning_switch(void *context, int state); +int tas2781_spi_load_calibration(void *context, char *file_name, + unsigned short i); +#endif /* __TAS2781_SPI_H__ */ diff --git a/sound/pci/hda/tas2781_hda_spi.c b/sound/pci/hda/tas2781_hda_sp= i.c new file mode 100644 index 000000000000..a25db61e055e --- /dev/null +++ b/sound/pci/hda/tas2781_hda_spi.c @@ -0,0 +1,1305 @@ +// SPDX-License-Identifier: GPL-2.0 +// +// TAS2781 HDA SPI driver +// +// Copyright 2024 Texas Instruments, Inc. +// +// Author: Baojun Xu + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#include "tas2781-spi.h" + +#include "hda_local.h" +#include "hda_auto_parser.h" +#include "hda_component.h" +#include "hda_jack.h" +#include "hda_generic.h" + +/* + * No standard control callbacks for SNDRV_CTL_ELEM_IFACE_CARD + * Define two controls, one is Volume control callbacks, the other is + * flag setting control callbacks. + */ + +/* Volume control callbacks for tas2781 */ +#define ACARD_SINGLE_RANGE_EXT_TLV(xname, xreg, xshift, xmin, xmax, xinver= t, \ + xhandler_get, xhandler_put, tlv_array) { \ + .iface =3D SNDRV_CTL_ELEM_IFACE_CARD, .name =3D (xname), \ + .access =3D SNDRV_CTL_ELEM_ACCESS_TLV_READ | \ + SNDRV_CTL_ELEM_ACCESS_READWRITE, \ + .tlv.p =3D (tlv_array), \ + .info =3D snd_soc_info_volsw_range, \ + .get =3D xhandler_get, .put =3D xhandler_put, \ + .private_value =3D (unsigned long)&(struct soc_mixer_control) { \ + .reg =3D xreg, .rreg =3D xreg, \ + .shift =3D xshift, .rshift =3D xshift,\ + .min =3D xmin, .max =3D xmax, .invert =3D xinvert, \ + } \ +} + +/* Flag control callbacks for tas2781 */ +#define ACARD_SINGLE_BOOL_EXT(xname, xdata, xhandler_get, xhandler_put) { \ + .iface =3D SNDRV_CTL_ELEM_IFACE_CARD, \ + .name =3D xname, \ + .info =3D snd_ctl_boolean_mono_info, \ + .get =3D xhandler_get, \ + .put =3D xhandler_put, \ + .private_value =3D xdata, \ +} + +struct tas2781_hda { + struct gpio_desc *reset_gpio; + struct tasdevice_priv *priv; + struct gpio_desc *cs_gpio; + struct acpi_device *dacpi; + struct device *dev; + struct snd_kcontrol *dsp_prog_ctl; + struct snd_kcontrol *dsp_conf_ctl; + struct snd_kcontrol *snd_ctls[3]; + struct snd_kcontrol *prof_ctl; +}; + +static const struct regmap_range_cfg tasdevice_ranges[] =3D { + { + .range_min =3D 0, + .range_max =3D TASDEVICE_MAX_BOOK_NUM * TASDEVICE_MAX_PAGE, + .selector_reg =3D TASDEVICE_PAGE_SELECT, + .selector_mask =3D 0xff, + .selector_shift =3D 0, + .window_start =3D 0, + .window_len =3D 128, + }, +}; + +static const struct regmap_config tasdevice_regmap =3D { + .reg_bits =3D 8, + .val_bits =3D 8, + .cache_type =3D REGCACHE_NONE, + .ranges =3D tasdevice_ranges, + .num_ranges =3D ARRAY_SIZE(tasdevice_ranges), + .max_register =3D TASDEVICE_MAX_BOOK_NUM * TASDEVICE_MAX_PAGE, +}; + +static int tasdevice_spi_switch_book(struct tasdevice_priv *tas_priv, int = book) +{ + struct tasdevice *tasdev =3D &tas_priv->tasdevice; + struct regmap *map =3D tas_priv->regmap; + int ret =3D 0; + + if (tasdev->cur_book !=3D book) { + /* Change to page 0 before book change. */ + ret =3D regmap_write(map, TASDEVICE_PAGE_SELECT, 0); + if (ret < 0) { + dev_err(tas_priv->dev, "%s, E=3D%d\n", __func__, ret); + return ret; + } + ret =3D regmap_write(map, TASDEVICE_BOOKCTL_REG, book); + if (ret < 0) { + dev_err(tas_priv->dev, "%s, E=3D%d\n", __func__, ret); + return ret; + } + tasdev->cur_book =3D book; + } + + return ret; +} + +int tasdevice_spi_dev_read(struct tasdevice_priv *tas_priv, + unsigned int reg, unsigned int *val) +{ + struct regmap *map =3D tas_priv->regmap; + int ret; + + ret =3D tasdevice_spi_switch_book(tas_priv, TASDEVICE_BOOK_ID(reg)); + if (ret < 0) + return ret; + + /* + * In our TAS2781 SPI mode, if read from other book (not book 0), + * or read from page number larger than 1 in book 0, one byte more + * read is needed, and first byte is a dummy byte, need to be ignored. + */ + if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) { + unsigned char data[2]; + + ret =3D regmap_bulk_read(map, TASDEVICE_PGRG(reg), data, + sizeof(data)); + *val =3D data[1]; + } else { + ret =3D regmap_read(map, TASDEVICE_PGRG(reg), val); + } + if (ret < 0) + dev_err(tas_priv->dev, "%s, E=3D%d\n", __func__, ret); + + return ret; +} + +int tasdevice_spi_dev_write(struct tasdevice_priv *tas_priv, + unsigned int reg, unsigned int value) +{ + struct regmap *map =3D tas_priv->regmap; + int ret; + + ret =3D tasdevice_spi_switch_book(tas_priv, TASDEVICE_BOOK_ID(reg)); + if (ret < 0) + return ret; + + ret =3D regmap_write(map, TASDEVICE_PGRG(reg), value); + if (ret < 0) + dev_err(tas_priv->dev, "%s, E=3D%d\n", __func__, ret); + + return ret; +} + +int tasdevice_spi_dev_bulk_write(struct tasdevice_priv *tas_priv, + unsigned int reg, unsigned char *data, unsigned int len) +{ + struct regmap *map =3D tas_priv->regmap; + int ret; + + ret =3D tasdevice_spi_switch_book(tas_priv, TASDEVICE_BOOK_ID(reg)); + if (ret < 0) + return ret; + + ret =3D regmap_bulk_write(map, TASDEVICE_PGRG(reg), data, len); + if (ret < 0) + dev_err(tas_priv->dev, "%s, E=3D%d\n", __func__, ret); + + return ret; +} + +int tasdevice_spi_dev_bulk_read(struct tasdevice_priv *tas_priv, + unsigned int reg, unsigned char *data, unsigned int len) +{ + struct regmap *map =3D tas_priv->regmap; + int ret; + + ret =3D tasdevice_spi_switch_book(tas_priv, TASDEVICE_BOOK_ID(reg)); + if (ret < 0) + return ret; + + /* + * In our TAS2781 SPI mode, if read from other book (not book 0), + * or read from page number larger than 1 in book 0, one byte more + * read is needed, and first byte is a dummy byte, need to be ignored. + */ + if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) { + unsigned char buf[129]; + + ret =3D regmap_bulk_read(map, TASDEVICE_PGRG(reg), + buf, len + 1); + memcpy(data, buf + 1, len); + } else { + ret =3D regmap_bulk_read(map, TASDEVICE_PGRG(reg), data, len); + } + if (ret < 0) + dev_err(tas_priv->dev, "%s, E=3D%d\n", __func__, ret); + + return ret; +} + +int tasdevice_spi_dev_update_bits(struct tasdevice_priv *tas_priv, + unsigned int reg, unsigned int mask, unsigned int value) +{ + struct regmap *map =3D tas_priv->regmap; + int ret; + + ret =3D tasdevice_spi_switch_book(tas_priv, TASDEVICE_BOOK_ID(reg)); + if (ret < 0) + return ret; + + ret =3D regmap_update_bits(map, TASDEVICE_PGRG(reg), mask, value); + if (ret < 0) + dev_err(tas_priv->dev, "%s, E=3D%d\n", __func__, ret); + + return ret; +} + +static void tas2781_spi_reset(struct tasdevice_priv *tas_dev) +{ + int ret; + + if (tas_dev->reset) { + gpiod_set_value_cansleep(tas_dev->reset, 0); + usleep_range(500, 1000); + gpiod_set_value_cansleep(tas_dev->reset, 1); + } else { + ret =3D tasdevice_spi_dev_write(tas_dev, + TAS2781_REG_SWRESET, TAS2781_REG_SWRESET_RESET); + if (ret < 0) + dev_err(tas_dev->dev, "dev sw-reset fail, %d\n", ret); + } + usleep_range(1000, 1050); +} + +static int tascodec_spi_init(struct tasdevice_priv *tas_priv, void *codec, + struct module *module, + void (*cont)(const struct firmware *fw, void *context)) +{ + int ret =3D 0; + + /* + * Codec Lock Hold to ensure that codec_probe and firmware parsing and + * loading do not simultaneously execute. + */ + mutex_lock(&tas_priv->codec_lock); + + scnprintf(tas_priv->rca_binaryname, 64, "%sRCA%d.bin", + tas_priv->dev_name, tas_priv->index); + crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL); + tas_priv->codec =3D codec; + ret =3D request_firmware_nowait(module, FW_ACTION_UEVENT, + tas_priv->rca_binaryname, tas_priv->dev, GFP_KERNEL, tas_priv, + cont); + if (ret) + dev_err(tas_priv->dev, "request_firmware_nowait err:0x%08x\n", + ret); + + /* Codec Lock Release*/ + mutex_unlock(&tas_priv->codec_lock); + + return ret; +} + +static int tasdevice_spi_init(struct tasdevice_priv *tas_priv) +{ + tas_priv->cur_prog =3D -1; + tas_priv->cur_conf =3D -1; + + tas_priv->tasdevice.cur_book =3D -1; + tas_priv->tasdevice.cur_prog =3D -1; + tas_priv->tasdevice.cur_conf =3D -1; + + mutex_init(&tas_priv->codec_lock); + + return 0; +} + +static int tasdevice_clamp(int val, int max, unsigned int invert) +{ + if (val > max) + val =3D max; + if (invert) + val =3D max - val; + if (val < 0) + val =3D 0; + + return val; +} + +static int tasdevice_spi_amp_putvol(struct tasdevice_priv *tas_priv, + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) +{ + unsigned int invert =3D mc->invert; + unsigned char mask; + int max =3D mc->max; + int val, ret; + + mask =3D (1 << fls(max)) - 1; + mask <<=3D mc->shift; + val =3D tasdevice_clamp(ucontrol->value.integer.value[0], max, invert); + ret =3D tasdevice_spi_dev_update_bits(tas_priv, + mc->reg, mask, (unsigned int)(val << mc->shift)); + if (ret) + dev_err(tas_priv->dev, "set AMP vol error in dev %d\n", + tas_priv->index); + + return ret; +} + +static int tasdevice_spi_amp_getvol(struct tasdevice_priv *tas_priv, + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) +{ + unsigned int invert =3D mc->invert; + unsigned char mask =3D 0; + int max =3D mc->max; + int ret, val; + + /* Read the primary device */ + ret =3D tasdevice_spi_dev_read(tas_priv, mc->reg, &val); + if (ret) { + dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__); + return ret; + } + + mask =3D (1 << fls(max)) - 1; + mask <<=3D mc->shift; + val =3D (val & mask) >> mc->shift; + val =3D tasdevice_clamp(val, max, invert); + ucontrol->value.integer.value[0] =3D val; + + return ret; +} + +static int tasdevice_spi_digital_putvol(struct tasdevice_priv *tas_priv, + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) +{ + unsigned int invert =3D mc->invert; + int max =3D mc->max; + int val, ret; + + val =3D tasdevice_clamp(ucontrol->value.integer.value[0], max, invert); + ret =3D tasdevice_spi_dev_write(tas_priv, mc->reg, (unsigned int)val); + if (ret) + dev_err(tas_priv->dev, "set digital vol err in dev %d\n", + tas_priv->index); + + return ret; + +} + +static int tasdevice_spi_digital_getvol(struct tasdevice_priv *tas_priv, + struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc) +{ + unsigned int invert =3D mc->invert; + int max =3D mc->max; + int ret, val; + + /* Read the primary device as the whole */ + ret =3D tasdevice_spi_dev_read(tas_priv, mc->reg, &val); + if (ret) { + dev_err(tas_priv->dev, "%s, get digital vol err\n", __func__); + return ret; + } + + val =3D tasdevice_clamp(val, max, invert); + ucontrol->value.integer.value[0] =3D val; + + return ret; +} + +static int tas2781_read_acpi(struct tas2781_hda *tas_hda, const char *hid, + int id) +{ + struct tasdevice_priv *p =3D tas_hda->priv; + struct gpio_desc *cs_gpiod; + struct acpi_device *adev; + struct device *physdev; + struct spi_device *spi; + u32 values[HDA_MAX_COMPONENTS]; + const char *property; + size_t nval; + int ret, i; + + adev =3D acpi_dev_get_first_match_dev(hid, NULL, -1); + if (!adev) { + dev_err(p->dev, "Failed to find ACPI device: %s\n", hid); + return -ENODEV; + } + + strscpy(p->dev_name, hid, sizeof(p->dev_name)); + tas_hda->dacpi =3D adev; + physdev =3D get_device(acpi_get_first_physical_node(adev)); + acpi_dev_put(adev); + + /* No side-effect to the playback even if subsystem_id is NULL*/ + p->acpi_subsystem_id =3D acpi_get_subsystem_id(ACPI_HANDLE(physdev)); + + property =3D "ti,dev-index"; + ret =3D device_property_count_u32(physdev, property); + if (ret <=3D 0) + goto err; + + if (ret > ARRAY_SIZE(values)) { + ret =3D -EINVAL; + goto err; + } + nval =3D ret; + + ret =3D device_property_read_u32_array(physdev, property, values, nval); + if (ret) + goto err; + + p->index =3D 0xFF; + for (i =3D 0; i < nval; i++) { + if (values[i] =3D=3D id) { + p->index =3D i; + break; + } + } + if (p->index =3D=3D 0xFF) { + dev_dbg(p->dev, "No index found in %s\n", property); + ret =3D -ENODEV; + goto err; + } + + p->reset =3D devm_gpiod_get_index_optional(physdev, + "reset", p->index, GPIOD_OUT_LOW); + if (IS_ERR(p->reset)) { + ret =3D PTR_ERR(p->reset); + + /* + * If RESET is shared the first amp to probe will grab the + * reset line and reset all the amps + */ + if (ret !=3D -EBUSY) { + dev_err_probe(p->dev, ret, + "Failed to get reset GPIO\n"); + goto err; + } + dev_info(p->dev, "Reset GPIO busy, assume shared reset\n"); + p->reset =3D NULL; + } + + /* + * Manually set the Chip Select for the second amp + * in the node. + * This is only supported for systems with 2 amps, since we cannot + * expand the default number of chip selects without using cs-gpios + * The CS GPIO must be set high prior to communicating with the + * first amp (which uses a native chip select), to ensure the second + * amp does not clash with the first. + */ + if (IS_ENABLED(CONFIG_SPI)) { + spi =3D to_spi_device(p->dev); + + /* + * This is obtained using driver_gpios, since only one GPIO + * for CS exists, this can be obtained using index 0. + */ + cs_gpiod =3D devm_gpiod_get_index_optional(physdev, "cs", + p->index, GPIOD_OUT_LOW); + if (IS_ERR(cs_gpiod)) { + dev_err(p->dev, "Unable to get CS GPIO descriptor\n"); + ret =3D PTR_ERR(cs_gpiod); + goto err; + } + if (id =3D=3D 1) { + spi_set_csgpiod(spi, 0, cs_gpiod); + p->chipselect =3D cs_gpiod; + } else { + gpiod_set_value_cansleep(cs_gpiod, true); + gpiod_put(cs_gpiod); + } + spi_setup(spi); + } + put_device(physdev); + + return 0; + +err: + dev_err(p->dev, "read acpi error, ret: %d\n", ret); + put_device(physdev); + acpi_dev_put(adev); + + return ret; +} + +static void tas2781_hda_playback_hook(struct device *dev, int action) +{ + struct tas2781_hda *tas_hda =3D dev_get_drvdata(dev); + + switch (action) { + case HDA_GEN_PCM_ACT_OPEN: + pm_runtime_get_sync(dev); + mutex_lock(&tas_hda->priv->codec_lock); + tasdevice_spi_tuning_switch(tas_hda->priv, 0); + mutex_unlock(&tas_hda->priv->codec_lock); + break; + case HDA_GEN_PCM_ACT_CLOSE: + mutex_lock(&tas_hda->priv->codec_lock); + tasdevice_spi_tuning_switch(tas_hda->priv, 1); + mutex_unlock(&tas_hda->priv->codec_lock); + + pm_runtime_mark_last_busy(dev); + pm_runtime_put_autosuspend(dev); + break; + default: + dev_dbg(tas_hda->dev, "Playback action not supported: %d\n", + action); + break; + } +} + +static int tasdevice_info_profile(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_info *uinfo) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + + uinfo->type =3D SNDRV_CTL_ELEM_TYPE_INTEGER; + uinfo->count =3D 1; + uinfo->value.integer.min =3D 0; + uinfo->value.integer.max =3D tas_priv->rcabin.ncfgs - 1; + + return 0; +} + +static int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + + ucontrol->value.integer.value[0] =3D tas_priv->rcabin.profile_cfg_id; + + return 0; +} + +static int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + int max =3D tas_priv->rcabin.ncfgs - 1; + int val, ret =3D 0; + + val =3D clamp(ucontrol->value.integer.value[0], 0, max); + + if (tas_priv->rcabin.profile_cfg_id !=3D val) { + tas_priv->rcabin.profile_cfg_id =3D val; + ret =3D 1; + } + + return ret; +} + +static int tasdevice_info_programs(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_info *uinfo) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + + uinfo->type =3D SNDRV_CTL_ELEM_TYPE_INTEGER; + uinfo->count =3D 1; + uinfo->value.integer.min =3D 0; + uinfo->value.integer.max =3D tas_priv->fmw->nr_programs - 1; + + return 0; +} + +static int tasdevice_info_config(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_info *uinfo) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + + uinfo->type =3D SNDRV_CTL_ELEM_TYPE_INTEGER; + uinfo->count =3D 1; + uinfo->value.integer.min =3D 0; + uinfo->value.integer.max =3D tas_priv->fmw->nr_configurations - 1; + + return 0; +} + +static int tasdevice_program_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + + ucontrol->value.integer.value[0] =3D tas_priv->cur_prog; + + return 0; +} + +static int tasdevice_program_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + int nr_program =3D ucontrol->value.integer.value[0]; + int max =3D tas_priv->fmw->nr_programs - 1; + int val, ret =3D 0; + + val =3D clamp(nr_program, 0, max); + + if (tas_priv->cur_prog !=3D val) { + tas_priv->cur_prog =3D val; + ret =3D 1; + } + + return ret; +} + +static int tasdevice_config_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + + ucontrol->value.integer.value[0] =3D tas_priv->cur_conf; + + return 0; +} + +static int tasdevice_config_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + int max =3D tas_priv->fmw->nr_configurations - 1; + int val, ret =3D 0; + + val =3D clamp(ucontrol->value.integer.value[0], 0, max); + + if (tas_priv->cur_conf !=3D val) { + tas_priv->cur_conf =3D val; + ret =3D 1; + } + + return ret; +} + +/* + * tas2781_digital_getvol - get the volum control + * @kcontrol: control pointer + * @ucontrol: User data + * Customer Kcontrol for tas2781 is primarily for regmap booking, paging + * depends on internal regmap mechanism. + * tas2781 contains book and page two-level register map, especially + * book switching will set the register BXXP00R7F, after switching to the + * correct book, then leverage the mechanism for paging to access the + * register. + */ +static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + struct soc_mixer_control *mc =3D + (struct soc_mixer_control *)kcontrol->private_value; + + return tasdevice_spi_digital_getvol(tas_priv, ucontrol, mc); +} + +static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + struct soc_mixer_control *mc =3D + (struct soc_mixer_control *)kcontrol->private_value; + + return tasdevice_spi_amp_getvol(tas_priv, ucontrol, mc); +} + +static int tas2781_digital_putvol(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + struct soc_mixer_control *mc =3D + (struct soc_mixer_control *)kcontrol->private_value; + + /* The check of the given value is in tasdevice_digital_putvol. */ + return tasdevice_spi_digital_putvol(tas_priv, ucontrol, mc); +} + +static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + struct soc_mixer_control *mc =3D + (struct soc_mixer_control *)kcontrol->private_value; + + /* The check of the given value is in tasdevice_amp_putvol. */ + return tasdevice_spi_amp_putvol(tas_priv, ucontrol, mc); +} + +static int tas2781_force_fwload_get(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + + ucontrol->value.integer.value[0] =3D (int)tas_priv->force_fwload_status; + dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__, + tas_priv->force_fwload_status ? "ON" : "OFF"); + + return 0; +} + +static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol, + struct snd_ctl_elem_value *ucontrol) +{ + struct tasdevice_priv *tas_priv =3D snd_kcontrol_chip(kcontrol); + bool change, val =3D (bool)ucontrol->value.integer.value[0]; + + if (tas_priv->force_fwload_status =3D=3D val) { + change =3D false; + } else { + change =3D true; + tas_priv->force_fwload_status =3D val; + } + dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__, + tas_priv->force_fwload_status ? "ON" : "OFF"); + + return change; +} + +static const struct snd_kcontrol_new tas2781_snd_controls[] =3D { + ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Gain", TAS2781_AMP_LEVEL, + 1, 0, 20, 0, tas2781_amp_getvol, + tas2781_amp_putvol, amp_vol_tlv), + ACARD_SINGLE_RANGE_EXT_TLV("Speaker Digital Gain", TAS2781_DVC_LVL, + 0, 0, 200, 1, tas2781_digital_getvol, + tas2781_digital_putvol, dvc_tlv), + ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0, + tas2781_force_fwload_get, tas2781_force_fwload_put), +}; + +static const struct snd_kcontrol_new tas2781_prof_ctrl =3D { + .name =3D "Speaker Profile Id", + .iface =3D SNDRV_CTL_ELEM_IFACE_CARD, + .info =3D tasdevice_info_profile, + .get =3D tasdevice_get_profile_id, + .put =3D tasdevice_set_profile_id, +}; + +static const struct snd_kcontrol_new tas2781_dsp_prog_ctrl =3D { + .name =3D "Speaker Program Id", + .iface =3D SNDRV_CTL_ELEM_IFACE_CARD, + .info =3D tasdevice_info_programs, + .get =3D tasdevice_program_get, + .put =3D tasdevice_program_put, +}; + +static const struct snd_kcontrol_new tas2781_dsp_conf_ctrl =3D { + .name =3D "Speaker Config Id", + .iface =3D SNDRV_CTL_ELEM_IFACE_CARD, + .info =3D tasdevice_info_config, + .get =3D tasdevice_config_get, + .put =3D tasdevice_config_put, +}; + +static void tas2781_apply_calib(struct tasdevice_priv *tas_priv) +{ + static const unsigned char page_array[CALIB_MAX] =3D { + 0x17, 0x18, 0x18, 0x13, 0x18, + }; + static const unsigned char rgno_array[CALIB_MAX] =3D { + 0x74, 0x0c, 0x14, 0x70, 0x7c, + }; + int j, rc; + + /* + * If no calibration data exist in tasdevice_priv *tas_priv, + * calibration apply will be ignored, and use default values + * in firmware binary, which was loaded during firmware download. + */ + if (tas_priv->calibration_data[0] =3D=3D 0) + return; + /* + * Calibration data was saved in tasdevice_priv *tas_priv as: + * unsigned int calibration_data[CALIB_MAX]; + * and every data (in 4 bytes) will be saved in register which in + * book 0,and page number in page_array[], offset was saved in + * rgno_array[]. + */ + for (j =3D 0; j < CALIB_MAX; j++) { + rc =3D tasdevice_spi_dev_bulk_write(tas_priv, + TASDEVICE_REG(0, page_array[j], rgno_array[j]), + (unsigned char *)&tas_priv->calibration_data[j], 4); + if (rc < 0) + dev_err(tas_priv->dev, + "chn %d calib %d bulk_wr err =3D %d\n", + tas_priv->index, j, rc); + } +} + +/* + * Update the calibration data, including speaker impedance, f0, etc, + * into algo. Calibrate data is done by manufacturer in the factory. + * These data are used by Algo for calculating the speaker temperature, + * speaker membrane excursion and f0 in real time during playback. + * Calibration data format in EFI is V2, since 2024. + */ +static int tas2781_save_calibration(struct tasdevice_priv *tas_priv) +{ + efi_guid_t efi_guid =3D EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d, + 0x93, 0xfe, 0x5a, 0xa3, 0x5d, 0xb3); + static efi_char16_t efi_name[] =3D L"CALI_DATA"; + struct tm *tm =3D &tas_priv->tm; + unsigned long total_sz =3D 0; + unsigned int attr, crc; + unsigned int *tmp_val; + efi_status_t status; + unsigned char *data; + + tas_priv->calibration_data[0] =3D 0; + /* Get real size of UEFI variable */ + status =3D efi.get_variable(efi_name, &efi_guid, &attr, &total_sz, + tas_priv->calibration_data); + if (status =3D=3D EFI_BUFFER_TOO_SMALL) { + /* Allocate data buffer of data_size bytes */ + data =3D devm_kzalloc(tas_priv->dev, total_sz, GFP_KERNEL); + if (!data) + return -ENOMEM; + /* Get variable contents into buffer */ + status =3D efi.get_variable(efi_name, &efi_guid, &attr, + &total_sz, data); + } + if (status !=3D EFI_SUCCESS) { + kfree(data); + return -EINVAL; + } + + tmp_val =3D (unsigned int *)data; + if (tmp_val[0] =3D=3D 2781) { + /* + * New features were added in calibrated Data V2: + * 1. Added some the fields to store the link_id and + * uniqie_id for multi-link solutions + * 2. Support flexible number of devices instead of + * fixed one in V1. + * Layout of calibrated data V2 in UEFI(total 256 bytes): + * ChipID (2783, 4 bytes) + * Device-Sum (4 bytes) + * TimeStamp of Calibration (4 bytes) + * for (i =3D 0; i < Device-Sum; i++) { + * Device #i index_info () { + * SDW link id (2bytes) + * SDW unique_id (2bytes) + * } + * Calibrated Data of Device #i (20 bytes) + * } + * CRC (4 bytes) + * Reserved (the rest) + */ + crc =3D crc32(~0, data, 3 + tmp_val[0] * 6) ^ ~0; + dev_dbg(tas_priv->dev, "cali crc 0x%08x PK tmp_val 0x%08x\n", + crc, tmp_val[3 + tmp_val[0] * 6]); + + if (crc =3D=3D tmp_val[3+tmp_val[0]*6]) { + time64_to_tm(tmp_val[2], 0, tm); + dev_dbg(tas_priv->dev, "%4ld-%2d-%2d, %2d:%2d:%2d\n", + tm->tm_year, tm->tm_mon, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec); + for (int i =3D 0; i < CALIB_MAX; i++) + tas_priv->calibration_data[i] =3D + tmp_val[3 + tas_priv->index * 6 + i]; + tas_priv->apply_calibration(tas_priv); + } else { + total_sz =3D 0; + } + } else { + /* + * Calibration data is in V1 format. + * struct cali_data { + * char cali_data[20]; + * } + * + * struct { + * struct cali_data cali_data[4]; + * int TimeStamp of Calibration (4 bytes) + * int CRC (4 bytes) + * } ueft; + */ + crc =3D crc32(~0, data, 84) ^ ~0; + dev_dbg(tas_priv->dev, "cali crc 0x%08x PK tmp_val 0x%08x\n", + crc, tmp_val[21]); + + if (crc =3D=3D tmp_val[21]) { + time64_to_tm(tmp_val[20], 0, tm); + dev_dbg(tas_priv->dev, "%4ld-%2d-%2d, %2d:%2d:%2d\n", + tm->tm_year, tm->tm_mon, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec); + for (int i =3D 0; i < CALIB_MAX; i++) + tas_priv->calibration_data[i] =3D + tmp_val[tas_priv->index * 5 + i]; + tas_priv->apply_calibration(tas_priv); + } + } + kfree(data); + + return 0; +} + +static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda) +{ + struct hda_codec *codec =3D tas_hda->priv->codec; + + if (tas_hda->dsp_prog_ctl) + snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl); + + if (tas_hda->dsp_conf_ctl) + snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl); + + for (int i =3D ARRAY_SIZE(tas_hda->snd_ctls) - 1; i >=3D 0; i--) + if (tas_hda->snd_ctls[i]) + snd_ctl_remove(codec->card, tas_hda->snd_ctls[i]); + + if (tas_hda->prof_ctl) + snd_ctl_remove(codec->card, tas_hda->prof_ctl); +} + +static void tasdev_fw_ready(const struct firmware *fmw, void *context) +{ + struct tasdevice_priv *tas_priv =3D context; + struct tas2781_hda *tas_hda =3D dev_get_drvdata(tas_priv->dev); + struct hda_codec *codec =3D tas_priv->codec; + int i, ret; + + pm_runtime_get_sync(tas_priv->dev); + mutex_lock(&tas_priv->codec_lock); + + ret =3D tasdevice_spi_rca_parser(tas_priv, fmw); + if (ret) + goto out; + + /* Add control one time only. */ + if (tas_priv->index =3D=3D 0) { + tas_hda->prof_ctl =3D snd_ctl_new1(&tas2781_prof_ctrl, tas_priv); + ret =3D snd_ctl_add(codec->card, tas_hda->prof_ctl); + if (ret) { + dev_err(tas_priv->dev, + "Failed to add KControl %s =3D %d\n", + tas2781_prof_ctrl.name, ret); + goto out; + } + + for (i =3D 0; i < ARRAY_SIZE(tas2781_snd_controls); i++) { + tas_hda->snd_ctls[i] =3D + snd_ctl_new1(&tas2781_snd_controls[i], + tas_priv); + ret =3D snd_ctl_add(codec->card, tas_hda->snd_ctls[i]); + if (ret) { + dev_err(tas_priv->dev, + "Failed to add KControl %s =3D %d\n", + tas2781_snd_controls[i].name, ret); + goto out; + } + } + } + + tasdevice_spi_dsp_remove(tas_priv); + + tas_priv->fw_state =3D TASDEVICE_DSP_FW_PENDING; + scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%08X-%01d.bin", + codec->core.subsystem_id, tas_priv->index); + ret =3D tasdevice_spi_dsp_parser(tas_priv); + if (ret) { + dev_err(tas_priv->dev, "dspfw load %s error\n", + tas_priv->coef_binaryname); + tas_priv->fw_state =3D TASDEVICE_DSP_FW_FAIL; + goto out; + } + + /* Add control one time only. */ + if (tas_priv->index =3D=3D 0) { + tas_hda->dsp_prog_ctl =3D snd_ctl_new1(&tas2781_dsp_prog_ctrl, + tas_priv); + ret =3D snd_ctl_add(codec->card, tas_hda->dsp_prog_ctl); + if (ret) { + dev_err(tas_priv->dev, + "Failed to add KControl %s =3D %d\n", + tas2781_dsp_prog_ctrl.name, ret); + goto out; + } + + tas_hda->dsp_conf_ctl =3D snd_ctl_new1(&tas2781_dsp_conf_ctrl, + tas_priv); + ret =3D snd_ctl_add(codec->card, tas_hda->dsp_conf_ctl); + if (ret) { + dev_err(tas_priv->dev, + "Failed to add KControl %s =3D %d\n", + tas2781_dsp_conf_ctrl.name, ret); + goto out; + } + } + + tas_priv->fw_state =3D TASDEVICE_DSP_FW_ALL_OK; + tasdevice_spi_prmg_load(tas_priv, 0); + if (tas_priv->fmw->nr_programs > 0) + tas_priv->cur_prog =3D 0; + if (tas_priv->fmw->nr_configurations > 0) + tas_priv->cur_conf =3D 0; + + /* + * If calibrated data occurs error, dsp will still works with default + * calibrated data inside algo. + */ + tas_priv->save_calibration(tas_priv); + + tasdevice_spi_tuning_switch(tas_hda->priv, 0); + +out: + mutex_unlock(&tas_hda->priv->codec_lock); + if (fmw) + release_firmware(fmw); + pm_runtime_mark_last_busy(tas_hda->dev); + pm_runtime_put_autosuspend(tas_hda->dev); +} + +static int tas2781_hda_bind(struct device *dev, struct device *master, + void *master_data) +{ + struct tas2781_hda *tas_hda =3D dev_get_drvdata(dev); + struct hda_component *comps =3D master_data; + struct hda_codec *codec; + int ret; + + if (!comps || tas_hda->priv->index < 0 || + tas_hda->priv->index >=3D HDA_MAX_COMPONENTS) + return -EINVAL; + + comps =3D &comps[tas_hda->priv->index]; + if (comps->dev) + return -EBUSY; + + codec =3D comps->codec; + + pm_runtime_get_sync(dev); + + comps->dev =3D dev; + + strscpy(comps->name, dev_name(dev), sizeof(comps->name)); + + ret =3D tascodec_spi_init(tas_hda->priv, codec, THIS_MODULE, + tasdev_fw_ready); + if (!ret) + comps->playback_hook =3D tas2781_hda_playback_hook; + + pm_runtime_mark_last_busy(dev); + pm_runtime_put_autosuspend(dev); + + return ret; +} + +static void tas2781_hda_unbind(struct device *dev, + struct device *master, void *master_data) +{ + struct tas2781_hda *tas_hda =3D dev_get_drvdata(dev); + struct hda_component *comps =3D master_data; + + comps =3D &comps[tas_hda->priv->index]; + + if (comps->dev =3D=3D dev) { + comps->dev =3D NULL; + memset(comps->name, 0, sizeof(comps->name)); + comps->playback_hook =3D NULL; + } + + tas2781_hda_remove_controls(tas_hda); + + tasdevice_spi_config_info_remove(tas_hda->priv); + tasdevice_spi_dsp_remove(tas_hda->priv); + + tas_hda->priv->fw_state =3D TASDEVICE_DSP_FW_PENDING; +} + +static const struct component_ops tas2781_hda_comp_ops =3D { + .bind =3D tas2781_hda_bind, + .unbind =3D tas2781_hda_unbind, +}; + +static void tas2781_hda_remove(struct device *dev) +{ + struct tas2781_hda *tas_hda =3D dev_get_drvdata(dev); + + pm_runtime_get_sync(tas_hda->dev); + pm_runtime_disable(tas_hda->dev); + + component_del(tas_hda->dev, &tas2781_hda_comp_ops); + + pm_runtime_put_noidle(tas_hda->dev); + + tasdevice_remove(tas_hda->priv); +} + +static int tas2781_hda_spi_probe(struct spi_device *spi) +{ + struct tasdevice_priv *tas_priv; + struct tas2781_hda *tas_hda; + const char *device_name; + int ret =3D 0; + + tas_hda =3D devm_kzalloc(&spi->dev, sizeof(*tas_hda), GFP_KERNEL); + if (!tas_hda) + return -ENOMEM; + + tas_hda->dev =3D &spi->dev; + spi->max_speed_hz =3D TAS2781_SPI_MAX_FREQ; + + tas_priv =3D devm_kzalloc(&spi->dev, sizeof(*tas_priv), GFP_KERNEL); + if (!tas_priv) + goto err; + tas_priv->regmap =3D devm_regmap_init_spi(spi, &tasdevice_regmap); + if (IS_ERR(tas_priv->regmap)) { + ret =3D PTR_ERR(tas_priv->regmap); + dev_err(tas_priv->dev, "Failed to allocate regmap: %d\n", + ret); + goto err; + } + tas_priv->dev =3D &spi->dev; + tas_priv->client =3D spi; + tas_hda->priv =3D tas_priv; + + if (strstr(dev_name(&spi->dev), "TXNW2781")) { + device_name =3D "TXNW2781"; + tas_priv->save_calibration =3D tas2781_save_calibration; + tas_priv->apply_calibration =3D tas2781_apply_calib; + } else + goto err; + + tas_priv->irq_info.irq =3D spi->irq; + dev_set_drvdata(&spi->dev, tas_hda); + ret =3D tas2781_read_acpi(tas_hda, device_name, + spi_get_chipselect(spi, 0)); + if (ret) + return dev_err_probe(tas_hda->dev, ret, + "Platform not supported\n"); + + ret =3D tasdevice_spi_init(tas_priv); + if (ret) + goto err; + + pm_runtime_set_autosuspend_delay(tas_hda->dev, 3000); + pm_runtime_use_autosuspend(tas_hda->dev); + pm_runtime_mark_last_busy(tas_hda->dev); + pm_runtime_set_active(tas_hda->dev); + pm_runtime_get_noresume(tas_hda->dev); + pm_runtime_enable(tas_hda->dev); + + pm_runtime_put_autosuspend(tas_hda->dev); + + tas2781_spi_reset(tas_priv); + + ret =3D component_add(tas_hda->dev, &tas2781_hda_comp_ops); + if (ret) { + dev_err(tas_hda->dev, "Register component failed: %d\n", ret); + pm_runtime_disable(tas_hda->dev); + } + +err: + if (ret) + tas2781_hda_remove(&spi->dev); + return ret; +} + +static void tas2781_hda_spi_remove(struct spi_device *spi) +{ + tas2781_hda_remove(&spi->dev); +} + +static int tas2781_runtime_suspend(struct device *dev) +{ + struct tas2781_hda *tas_hda =3D dev_get_drvdata(dev); + + dev_dbg(tas_hda->dev, "Runtime Suspend\n"); + + mutex_lock(&tas_hda->priv->codec_lock); + + if (tas_hda->priv->playback_started) { + tasdevice_spi_tuning_switch(tas_hda->priv, 1); + tas_hda->priv->playback_started =3D false; + } + + tas_hda->priv->tasdevice.cur_book =3D -1; + tas_hda->priv->tasdevice.cur_prog =3D -1; + tas_hda->priv->tasdevice.cur_conf =3D -1; + + mutex_unlock(&tas_hda->priv->codec_lock); + + return 0; +} + +static int tas2781_runtime_resume(struct device *dev) +{ + struct tas2781_hda *tas_hda =3D dev_get_drvdata(dev); + + dev_dbg(tas_hda->dev, "Runtime Resume\n"); + + mutex_lock(&tas_hda->priv->codec_lock); + + tasdevice_spi_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog); + + /* + * If calibrated data occurs error, dsp will still works with default + * calibrated data inside algo. + */ + tas_hda->priv->apply_calibration(tas_hda->priv); + + mutex_unlock(&tas_hda->priv->codec_lock); + + return 0; +} + +static int tas2781_system_suspend(struct device *dev) +{ + struct tas2781_hda *tas_hda =3D dev_get_drvdata(dev); + int ret; + + dev_dbg(tas_hda->priv->dev, "System Suspend\n"); + + ret =3D pm_runtime_force_suspend(dev); + if (ret) + return ret; + + /* Shutdown chip before system suspend */ + tasdevice_spi_tuning_switch(tas_hda->priv, 1); + + /* + * Reset GPIO may be shared, so cannot reset here. + * However beyond this point, amps may be powered down. + */ + return 0; +} + +static int tas2781_system_resume(struct device *dev) +{ + struct tas2781_hda *tas_hda =3D dev_get_drvdata(dev); + int ret; + + ret =3D pm_runtime_force_resume(dev); + if (ret) + return ret; + + mutex_lock(&tas_hda->priv->codec_lock); + + tas_hda->priv->tasdevice.cur_book =3D -1; + tas_hda->priv->tasdevice.cur_prog =3D -1; + tas_hda->priv->tasdevice.cur_conf =3D -1; + + tas2781_spi_reset(tas_hda->priv); + tasdevice_spi_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog); + + /* + * If calibrated data occurs error, dsp will still work with default + * calibrated data inside algo. + */ + tas_hda->priv->apply_calibration(tas_hda->priv); + mutex_unlock(&tas_hda->priv->codec_lock); + + return 0; +} + +static const struct dev_pm_ops tas2781_hda_pm_ops =3D { + RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL) + SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume) +}; + +static const struct spi_device_id tas2781_hda_spi_id[] =3D { + { "tas2781-hda", 0 }, + {} +}; + +static const struct acpi_device_id tas2781_acpi_hda_match[] =3D { + {"TXNW2781", 0 }, + {} +}; +MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match); + +static struct spi_driver tas2781_hda_spi_driver =3D { + .driver =3D { + .name =3D "tas2781-hda", + .acpi_match_table =3D tas2781_acpi_hda_match, + .pm =3D &tas2781_hda_pm_ops, + }, + .id_table =3D tas2781_hda_spi_id, + .probe =3D tas2781_hda_spi_probe, + .remove =3D tas2781_hda_spi_remove, +}; +module_spi_driver(tas2781_hda_spi_driver); + +MODULE_DESCRIPTION("TAS2781 HDA SPI Driver"); +MODULE_AUTHOR("Baojun, Xu, "); +MODULE_LICENSE("GPL"); --=20 2.40.1 From nobody Tue May 21 09:58:09 2024 Received: from fllv0015.ext.ti.com (fllv0015.ext.ti.com [198.47.19.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1381854745 for ; Thu, 18 Apr 2024 04:05:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.47.19.141 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713413108; cv=none; b=hPyGMsbl2w6GZEU4D3SpiaD6GFXCszRLEfIr+lS0Cb6OyUx+PDmsMPMkWJdq0mQannV/KgtYQy14J4rNtY2Zct4R4fvWnvPOJ6TZql8eIoblNtQsCiNR5ciYm+KIIQJiOtbdZ/D0w22FdbHMoIw8jjvZ4CsX8dkCOBuh4io3fo8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1713413108; c=relaxed/simple; bh=RnWouCkBXol2OuZV+9kBDsYaxqMxJH0zP+Xa1lDH2IA=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=CSD1NyDuJrm1DwfhFjkyd2LRhuinNaJnwi7wtf+LSd3NVo+jyNzayjmWbsvMa4zfxoOhLLCcg6w+jmjFn9IHyzlonTm1Mg/h32HzBLttlxeraJtDttJkIblCX3K7tP/LTBJqjthd1uzi3xauOtv5C8x//qXGtvFxHZp9ffO1kYI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com; spf=pass smtp.mailfrom=ti.com; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b=OYejVwlN; arc=none smtp.client-ip=198.47.19.141 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ti.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ti.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=ti.com header.i=@ti.com header.b="OYejVwlN" Received: from fllv0034.itg.ti.com ([10.64.40.246]) by fllv0015.ext.ti.com (8.15.2/8.15.2) with ESMTP id 43I43cod089595; Wed, 17 Apr 2024 23:03:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ti.com; s=ti-com-17Q1; t=1713413018; bh=kBqTDlTrYKcOXrP2LJ67b8yh3EVsZWqYyHs53c4M3kk=; h=From:To:CC:Subject:Date:In-Reply-To:References; b=OYejVwlNve+cdVN8cgpHaxLWIKy2O2tHA8H/uKtpUebdNcaTqi1pTQjv2G5luXE/y 8XYMzD/+sIXF8kdsqb8gXrxeOBvDCTZr6QCIu1zou6ez7U4Y8uqEfZ0bzSPoPSwaZx ZFyMAULXTQYKCatuTa7dxCFapXm61QXsGFrnL9Cc= Received: from DFLE104.ent.ti.com (dfle104.ent.ti.com [10.64.6.25]) by fllv0034.itg.ti.com (8.15.2/8.15.2) with ESMTPS id 43I43cNG126064 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 17 Apr 2024 23:03:38 -0500 Received: from DFLE108.ent.ti.com (10.64.6.29) by DFLE104.ent.ti.com (10.64.6.25) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23; Wed, 17 Apr 2024 23:03:38 -0500 Received: from lelvsmtp5.itg.ti.com (10.180.75.250) by DFLE108.ent.ti.com (10.64.6.29) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.2507.23 via Frontend Transport; Wed, 17 Apr 2024 23:03:38 -0500 Received: from lelvsmtp6.itg.ti.com ([10.250.165.138]) by lelvsmtp5.itg.ti.com (8.15.2/8.15.2) with ESMTP id 43I439in070557; Wed, 17 Apr 2024 23:03:30 -0500 From: Baojun Xu To: CC: , , , , , , , , <13916275206@139.com>, , , , , , , , , Subject: [PATCH v3 3/3] ALSA: hda/tas2781: Firmware load for tas2781 driver for SPI Date: Thu, 18 Apr 2024 12:02:39 +0800 Message-ID: <20240418040240.2337-4-baojun.xu@ti.com> X-Mailer: git-send-email 2.43.0.windows.1 In-Reply-To: <20240418040240.2337-1-baojun.xu@ti.com> References: <20240418040240.2337-1-baojun.xu@ti.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-EXCLAIMER-MD-CONFIG: e1e8a2fd-e40a-4ac6-ac9b-f7e9cc9ee180 Content-Type: text/plain; charset="utf-8" Firmware download and parser lib for tas2781, it work for spi device with a single firmware binary file. Signed-off-by: Baojun Xu --- v3: - Update format and variables declare order. - Change format of multi conditions for if. - Remove casting which is not needed. - Change variables type to avoid casting. - Remove Unneeded parentheses. - Change if check to avoid goto. - Add {} for keep same style. - Remove some local variables, use elements in structure directly. --- sound/pci/hda/tas2781_spi_fwlib.c | 2272 +++++++++++++++++++++++++++++ 1 file changed, 2272 insertions(+) create mode 100644 sound/pci/hda/tas2781_spi_fwlib.c diff --git a/sound/pci/hda/tas2781_spi_fwlib.c b/sound/pci/hda/tas2781_spi_= fwlib.c new file mode 100644 index 000000000000..93f016e669b5 --- /dev/null +++ b/sound/pci/hda/tas2781_spi_fwlib.c @@ -0,0 +1,2272 @@ +// SPDX-License-Identifier: GPL-2.0 +// +// TAS2781 HDA SPI driver +// +// Copyright 2024 Texas Instruments, Inc. +// +// Author: Baojun Xu + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "tas2781-spi.h" + +#define OFFSET_ERROR_BIT 0x40000000 + +#define ERROR_PRAM_CRCCHK 0x0000000 +#define ERROR_YRAM_CRCCHK 0x0000001 +#define PPC_DRIVER_CRCCHK 0x00000200 + +#define TAS2781_SA_COEFF_SWAP_REG TASDEVICE_REG(0, 0x35, 0x2c) +#define TAS2781_YRAM_BOOK1 140 +#define TAS2781_YRAM1_PAGE 42 +#define TAS2781_YRAM1_START_REG 88 + +#define TAS2781_YRAM2_START_PAGE 43 +#define TAS2781_YRAM2_END_PAGE 49 +#define TAS2781_YRAM2_START_REG 8 +#define TAS2781_YRAM2_END_REG 127 + +#define TAS2781_YRAM3_PAGE 50 +#define TAS2781_YRAM3_START_REG 8 +#define TAS2781_YRAM3_END_REG 27 + +/*should not include B0_P53_R44-R47 */ +#define TAS2781_YRAM_BOOK2 0 +#define TAS2781_YRAM4_START_PAGE 50 +#define TAS2781_YRAM4_END_PAGE 60 + +#define TAS2781_YRAM5_PAGE 61 +#define TAS2781_YRAM5_START_REG TAS2781_YRAM3_START_REG +#define TAS2781_YRAM5_END_REG TAS2781_YRAM3_END_REG + +#define TASDEVICE_MAXPROGRAM_NUM_KERNEL 5 +#define TASDEVICE_MAXCONFIG_NUM_KERNEL_MULTIPLE_AMPS 64 +#define TASDEVICE_MAXCONFIG_NUM_KERNEL 10 +#define MAIN_ALL_DEVICES_1X 0x01 +#define MAIN_DEVICE_A_1X 0x02 +#define MAIN_DEVICE_B_1X 0x03 +#define MAIN_DEVICE_C_1X 0x04 +#define MAIN_DEVICE_D_1X 0x05 +#define COEFF_DEVICE_A_1X 0x12 +#define COEFF_DEVICE_B_1X 0x13 +#define COEFF_DEVICE_C_1X 0x14 +#define COEFF_DEVICE_D_1X 0x15 +#define PRE_DEVICE_A_1X 0x22 +#define PRE_DEVICE_B_1X 0x23 +#define PRE_DEVICE_C_1X 0x24 +#define PRE_DEVICE_D_1X 0x25 +#define PRE_SOFTWARE_RESET_DEVICE_A 0x41 +#define PRE_SOFTWARE_RESET_DEVICE_B 0x42 +#define PRE_SOFTWARE_RESET_DEVICE_C 0x43 +#define PRE_SOFTWARE_RESET_DEVICE_D 0x44 +#define POST_SOFTWARE_RESET_DEVICE_A 0x45 +#define POST_SOFTWARE_RESET_DEVICE_B 0x46 +#define POST_SOFTWARE_RESET_DEVICE_C 0x47 +#define POST_SOFTWARE_RESET_DEVICE_D 0x48 + +struct tas_crc { + unsigned char offset; + unsigned char len; +}; + +struct blktyp_devidx_map { + unsigned char blktyp; + unsigned char dev_idx; +}; + +/* fixed m68k compiling issue: mapping table can save code field */ +static const struct blktyp_devidx_map ppc3_tas2781_mapping_table[] =3D { + { MAIN_ALL_DEVICES_1X, 0x80 }, + { MAIN_DEVICE_A_1X, 0x81 }, + { COEFF_DEVICE_A_1X, 0xC1 }, + { PRE_DEVICE_A_1X, 0xC1 }, + { PRE_SOFTWARE_RESET_DEVICE_A, 0xC1 }, + { POST_SOFTWARE_RESET_DEVICE_A, 0xC1 }, + { MAIN_DEVICE_B_1X, 0x82 }, + { COEFF_DEVICE_B_1X, 0xC2 }, + { PRE_DEVICE_B_1X, 0xC2 }, + { PRE_SOFTWARE_RESET_DEVICE_B, 0xC2 }, + { POST_SOFTWARE_RESET_DEVICE_B, 0xC2 }, + { MAIN_DEVICE_C_1X, 0x83 }, + { COEFF_DEVICE_C_1X, 0xC3 }, + { PRE_DEVICE_C_1X, 0xC3 }, + { PRE_SOFTWARE_RESET_DEVICE_C, 0xC3 }, + { POST_SOFTWARE_RESET_DEVICE_C, 0xC3 }, + { MAIN_DEVICE_D_1X, 0x84 }, + { COEFF_DEVICE_D_1X, 0xC4 }, + { PRE_DEVICE_D_1X, 0xC4 }, + { PRE_SOFTWARE_RESET_DEVICE_D, 0xC4 }, + { POST_SOFTWARE_RESET_DEVICE_D, 0xC4 }, +}; + +static const struct blktyp_devidx_map ppc3_mapping_table[] =3D { + { MAIN_ALL_DEVICES_1X, 0x80 }, + { MAIN_DEVICE_A_1X, 0x81 }, + { COEFF_DEVICE_A_1X, 0xC1 }, + { PRE_DEVICE_A_1X, 0xC1 }, + { MAIN_DEVICE_B_1X, 0x82 }, + { COEFF_DEVICE_B_1X, 0xC2 }, + { PRE_DEVICE_B_1X, 0xC2 }, + { MAIN_DEVICE_C_1X, 0x83 }, + { COEFF_DEVICE_C_1X, 0xC3 }, + { PRE_DEVICE_C_1X, 0xC3 }, + { MAIN_DEVICE_D_1X, 0x84 }, + { COEFF_DEVICE_D_1X, 0xC4 }, + { PRE_DEVICE_D_1X, 0xC4 }, +}; + +static const struct blktyp_devidx_map non_ppc3_mapping_table[] =3D { + { MAIN_ALL_DEVICES, 0x80 }, + { MAIN_DEVICE_A, 0x81 }, + { COEFF_DEVICE_A, 0xC1 }, + { PRE_DEVICE_A, 0xC1 }, + { MAIN_DEVICE_B, 0x82 }, + { COEFF_DEVICE_B, 0xC2 }, + { PRE_DEVICE_B, 0xC2 }, + { MAIN_DEVICE_C, 0x83 }, + { COEFF_DEVICE_C, 0xC3 }, + { PRE_DEVICE_C, 0xC3 }, + { MAIN_DEVICE_D, 0x84 }, + { COEFF_DEVICE_D, 0xC4 }, + { PRE_DEVICE_D, 0xC4 }, +}; + +static struct tasdevice_config_info *tasdevice_add_config( + struct tasdevice_priv *tas_priv, unsigned char *config_data, + unsigned int config_size, int *status) +{ + struct tasdevice_config_info *cfg_info; + struct tasdev_blk_data **bk_da; + unsigned int config_offset =3D 0; + unsigned int i; + + /* + * In most projects are many audio cases, such as music, handfree, + * receiver, games, audio-to-haptics, PMIC record, bypass mode, + * portrait, landscape, etc. Even in multiple audios, one or + * two of the chips will work for the special case, such as + * ultrasonic application. In order to support these variable-numbers + * of audio cases, flexible configs have been introduced in the + * DSP firmware. + */ + cfg_info =3D kzalloc(sizeof(*cfg_info), GFP_KERNEL); + if (!cfg_info) { + *status =3D -ENOMEM; + return NULL; + } + + if (tas_priv->rcabin.fw_hdr.binary_version_num >=3D 0x105) { + if ((config_offset + 64) > config_size) { + *status =3D -EINVAL; + dev_err(tas_priv->dev, "add conf: Out of boundary\n"); + goto out; + } + config_offset +=3D 64; + } + + if ((config_offset + 4) > config_size) { + *status =3D -EINVAL; + dev_err(tas_priv->dev, "add config: Out of boundary\n"); + goto out; + } + + /* + * convert data[offset], data[offset + 1], data[offset + 2] and + * data[offset + 3] into host + */ + cfg_info->nblocks =3D get_unaligned_be32(&config_data[config_offset]); + config_offset +=3D 4; + + /* + * Several kinds of dsp/algorithm firmwares can run on tas2781, + * the number and size of blk are not fixed and different among + * these firmwares. + */ + bk_da =3D cfg_info->blk_data =3D kcalloc(cfg_info->nblocks, + sizeof(*bk_da), GFP_KERNEL); + if (!bk_da) { + *status =3D -ENOMEM; + goto out; + } + cfg_info->real_nblocks =3D 0; + for (i =3D 0; i < cfg_info->nblocks; i++) { + if (config_offset + 12 > config_size) { + *status =3D -EINVAL; + dev_err(tas_priv->dev, + "%s: Out of boundary: i =3D %d nblocks =3D %u!\n", + __func__, i, cfg_info->nblocks); + goto out1; + } + bk_da[i] =3D kzalloc(sizeof(*bk_da[i]), GFP_KERNEL); + if (!bk_da[i]) { + *status =3D -ENOMEM; + goto out1; + } + + bk_da[i]->dev_idx =3D config_data[config_offset]; + config_offset++; + + bk_da[i]->block_type =3D config_data[config_offset]; + config_offset++; + + if (bk_da[i]->block_type =3D=3D TASDEVICE_BIN_BLK_PRE_POWER_UP) { + if (bk_da[i]->dev_idx =3D=3D 0) + cfg_info->active_dev =3D + BIT(tas_priv->index) - 1; + else + cfg_info->active_dev |=3D + (BIT(bk_da[i]->dev_idx) - 1); + + } + bk_da[i]->yram_checksum =3D + get_unaligned_be16(&config_data[config_offset]); + config_offset +=3D 2; + bk_da[i]->block_size =3D + get_unaligned_be32(&config_data[config_offset]); + config_offset +=3D 4; + + bk_da[i]->n_subblks =3D + get_unaligned_be32(&config_data[config_offset]); + + config_offset +=3D 4; + + if (config_offset + bk_da[i]->block_size > config_size) { + *status =3D -EINVAL; + dev_err(tas_priv->dev, + "%s: Out of boundary: i =3D %d blks =3D %u!\n", + __func__, i, cfg_info->nblocks); + goto out1; + } + /* instead of kzalloc+memcpy */ + bk_da[i]->regdata =3D kmemdup(&config_data[config_offset], + bk_da[i]->block_size, GFP_KERNEL); + if (!bk_da[i]->regdata) { + *status =3D -ENOMEM; + i++; + goto out1; + } + + config_offset +=3D bk_da[i]->block_size; + cfg_info->real_nblocks +=3D 1; + } + + return cfg_info; +out1: + for (int j =3D 0; j < i; j++) + kfree(bk_da[j]); + kfree(bk_da); +out: + kfree(cfg_info); + return NULL; +} + +int tasdevice_spi_rca_parser(void *context, const struct firmware *fmw) +{ + struct tasdevice_priv *tas_priv =3D context; + struct tasdevice_config_info **cfg_info; + struct tasdevice_rca_hdr *fw_hdr; + struct tasdevice_rca *rca; + unsigned int total_config_sz =3D 0; + int offset =3D 0, ret =3D 0, i; + unsigned char *buf; + + rca =3D &(tas_priv->rcabin); + fw_hdr =3D &(rca->fw_hdr); + if (!fmw || !fmw->data) { + dev_err(tas_priv->dev, "Failed to read %s\n", + tas_priv->rca_binaryname); + tas_priv->fw_state =3D TASDEVICE_DSP_FW_FAIL; + return -EINVAL; + } + buf =3D (unsigned char *)fmw->data; + fw_hdr->img_sz =3D get_unaligned_be32(&buf[offset]); + offset +=3D 4; + if (fw_hdr->img_sz !=3D fmw->size) { + dev_err(tas_priv->dev, + "File size not match, %d %u", (int)fmw->size, + fw_hdr->img_sz); + tas_priv->fw_state =3D TASDEVICE_DSP_FW_FAIL; + return -EINVAL; + } + + fw_hdr->checksum =3D get_unaligned_be32(&buf[offset]); + offset +=3D 4; + fw_hdr->binary_version_num =3D get_unaligned_be32(&buf[offset]); + if (fw_hdr->binary_version_num < 0x103) { + dev_err(tas_priv->dev, "File version 0x%04x is too low", + fw_hdr->binary_version_num); + tas_priv->fw_state =3D TASDEVICE_DSP_FW_FAIL; + return -EINVAL; + } + offset +=3D 4; + fw_hdr->drv_fw_version =3D get_unaligned_be32(&buf[offset]); + offset +=3D 8; + fw_hdr->plat_type =3D buf[offset++]; + fw_hdr->dev_family =3D buf[offset++]; + fw_hdr->reserve =3D buf[offset++]; + fw_hdr->ndev =3D buf[offset++]; + if (offset + TASDEVICE_DEVICE_SUM > fw_hdr->img_sz) { + dev_err(tas_priv->dev, "rca_ready: Out of boundary!\n"); + tas_priv->fw_state =3D TASDEVICE_DSP_FW_FAIL; + return -EINVAL; + } + + for (i =3D 0; i < TASDEVICE_DEVICE_SUM; i++, offset++) + fw_hdr->devs[i] =3D buf[offset]; + + fw_hdr->nconfig =3D get_unaligned_be32(&buf[offset]); + offset +=3D 4; + + for (i =3D 0; i < TASDEVICE_CONFIG_SUM; i++) { + fw_hdr->config_size[i] =3D get_unaligned_be32(&buf[offset]); + offset +=3D 4; + total_config_sz +=3D fw_hdr->config_size[i]; + } + + if (fw_hdr->img_sz - total_config_sz !=3D (unsigned int)offset) { + dev_err(tas_priv->dev, "Bin file err %d - %d !=3D %d!\n", + fw_hdr->img_sz, total_config_sz, (int)offset); + tas_priv->fw_state =3D TASDEVICE_DSP_FW_FAIL; + return -EINVAL; + } + + cfg_info =3D kcalloc(fw_hdr->nconfig, sizeof(*cfg_info), GFP_KERNEL); + if (!cfg_info) { + tas_priv->fw_state =3D TASDEVICE_DSP_FW_FAIL; + return -ENOMEM; + } + rca->cfg_info =3D cfg_info; + rca->ncfgs =3D 0; + for (i =3D 0; i < (int)fw_hdr->nconfig; i++) { + rca->ncfgs +=3D 1; + cfg_info[i] =3D tasdevice_add_config(tas_priv, &buf[offset], + fw_hdr->config_size[i], &ret); + if (ret) { + tas_priv->fw_state =3D TASDEVICE_DSP_FW_FAIL; + return ret; + } + offset +=3D (int)fw_hdr->config_size[i]; + } + + return ret; +} + +/* fixed m68k compiling issue: mapping table can save code field */ +static unsigned char map_dev_idx(struct tasdevice_fw *tas_fmw, + struct tasdev_blk *block) +{ + struct blktyp_devidx_map *p =3D + (struct blktyp_devidx_map *)non_ppc3_mapping_table; + struct tasdevice_dspfw_hdr *fw_hdr =3D &(tas_fmw->fw_hdr); + struct tasdevice_fw_fixed_hdr *fw_fixed_hdr =3D &(fw_hdr->fixed_hdr); + int i, n =3D ARRAY_SIZE(non_ppc3_mapping_table); + unsigned char dev_idx =3D 0; + + if (fw_fixed_hdr->ppcver >=3D PPC3_VERSION_TAS2781) { + p =3D (struct blktyp_devidx_map *)ppc3_tas2781_mapping_table; + n =3D ARRAY_SIZE(ppc3_tas2781_mapping_table); + } else if (fw_fixed_hdr->ppcver >=3D PPC3_VERSION) { + p =3D (struct blktyp_devidx_map *)ppc3_mapping_table; + n =3D ARRAY_SIZE(ppc3_mapping_table); + } + + for (i =3D 0; i < n; i++) { + if (block->type =3D=3D p[i].blktyp) { + dev_idx =3D p[i].dev_idx; + break; + } + } + + return dev_idx; +} + +static int fw_parse_block_data_kernel(struct tasdevice_fw *tas_fmw, + struct tasdev_blk *block, const struct firmware *fmw, int offset) +{ + const unsigned char *data =3D fmw->data; + + if (offset + 16 > fmw->size) { + dev_err(tas_fmw->dev, "%s: File Size error\n", __func__); + return -EINVAL; + } + + /* + * Convert data[offset], data[offset + 1], data[offset + 2] and + * data[offset + 3] into host. + */ + block->type =3D get_unaligned_be32(&data[offset]); + offset +=3D 4; + + block->is_pchksum_present =3D data[offset++]; + block->pchksum =3D data[offset++]; + block->is_ychksum_present =3D data[offset++]; + block->ychksum =3D data[offset++]; + block->blk_size =3D get_unaligned_be32(&data[offset]); + offset +=3D 4; + block->nr_subblocks =3D get_unaligned_be32(&data[offset]); + offset +=3D 4; + + /* + * Fixed m68k compiling issue: + * 1. mapping table can save code field. + * 2. storing the dev_idx as a member of block can reduce unnecessary + * time and system resource comsumption of dev_idx mapping every + * time the block data writing to the dsp. + */ + block->dev_idx =3D map_dev_idx(tas_fmw, block); + + if (offset + block->blk_size > fmw->size) { + dev_err(tas_fmw->dev, "%s: nSublocks error\n", __func__); + return -EINVAL; + } + /* instead of kzalloc+memcpy */ + block->data =3D kmemdup(&data[offset], block->blk_size, GFP_KERNEL); + if (!block->data) + return -ENOMEM; + + offset +=3D block->blk_size; + + return offset; +} + +static int fw_parse_data_kernel(struct tasdevice_fw *tas_fmw, + struct tasdevice_data *img_data, const struct firmware *fmw, + int offset) +{ + const unsigned char *data =3D fmw->data; + struct tasdev_blk *blk; + unsigned int i; + + if (offset + 4 > fmw->size) { + dev_err(tas_fmw->dev, "%s: File Size error\n", __func__); + return -EINVAL; + } + img_data->nr_blk =3D get_unaligned_be32(&data[offset]); + offset +=3D 4; + + img_data->dev_blks =3D kcalloc(img_data->nr_blk, + sizeof(struct tasdev_blk), GFP_KERNEL); + if (!img_data->dev_blks) + return -ENOMEM; + + for (i =3D 0; i < img_data->nr_blk; i++) { + blk =3D &(img_data->dev_blks[i]); + offset =3D fw_parse_block_data_kernel( + tas_fmw, blk, fmw, offset); + if (offset < 0) { + kfree(img_data->dev_blks); + return -EINVAL; + } + } + + return offset; +} + +static int fw_parse_program_data_kernel( + struct tasdevice_priv *tas_priv, struct tasdevice_fw *tas_fmw, + const struct firmware *fmw, int offset) +{ + struct tasdevice_prog *program; + unsigned int i; + + for (i =3D 0; i < tas_fmw->nr_programs; i++) { + program =3D &(tas_fmw->programs[i]); + if (offset + 72 > fmw->size) { + dev_err(tas_priv->dev, "%s: mpName error\n", __func__); + return -EINVAL; + } + /*skip 72 unused byts*/ + offset +=3D 72; + + offset =3D fw_parse_data_kernel(tas_fmw, &(program->dev_data), + fmw, offset); + if (offset < 0) + break; + } + + return offset; +} + +static int fw_parse_configuration_data_kernel(struct tasdevice_priv *tas_p= riv, + struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset) +{ + const unsigned char *data =3D fmw->data; + struct tasdevice_config *config; + unsigned int i; + + for (i =3D 0; i < tas_fmw->nr_configurations; i++) { + config =3D &(tas_fmw->configs[i]); + if (offset + 80 > fmw->size) { + dev_err(tas_priv->dev, "%s: mpName error\n", __func__); + return -EINVAL; + } + memcpy(config->name, &data[offset], 64); + /*skip extra 16 bytes*/ + offset +=3D 80; + + offset =3D fw_parse_data_kernel(tas_fmw, &(config->dev_data), + fmw, offset); + if (offset < 0) + break; + } + + return offset; +} + +static int fw_parse_variable_header_kernel(struct tasdevice_priv *tas_priv, + const struct firmware *fmw, int offset) +{ + struct tasdevice_fw *tas_fmw =3D tas_priv->fmw; + struct tasdevice_dspfw_hdr *fw_hdr =3D &(tas_fmw->fw_hdr); + struct tasdevice_config *config; + struct tasdevice_prog *program; + const unsigned char *buf =3D fmw->data; + unsigned short max_confs; + unsigned int i; + + if (offset + 12 + 4 * TASDEVICE_MAXPROGRAM_NUM_KERNEL > fmw->size) { + dev_err(tas_priv->dev, "%s: File Size error\n", __func__); + return -EINVAL; + } + fw_hdr->device_family =3D get_unaligned_be16(&buf[offset]); + if (fw_hdr->device_family !=3D 0) { + dev_err(tas_priv->dev, "%s:not TAS device\n", __func__); + return -EINVAL; + } + offset +=3D 2; + fw_hdr->device =3D get_unaligned_be16(&buf[offset]); + if (fw_hdr->device >=3D TASDEVICE_DSP_TAS_MAX_DEVICE || + fw_hdr->device =3D=3D 6) { + dev_err(tas_priv->dev, "Unsupported dev %d\n", fw_hdr->device); + return -EINVAL; + } + offset +=3D 2; + + tas_fmw->nr_programs =3D get_unaligned_be32(&buf[offset]); + offset +=3D 4; + + if (tas_fmw->nr_programs =3D=3D 0 || + tas_fmw->nr_programs > TASDEVICE_MAXPROGRAM_NUM_KERNEL) { + dev_err(tas_priv->dev, "mnPrograms is invalid\n"); + return -EINVAL; + } + + tas_fmw->programs =3D kcalloc(tas_fmw->nr_programs, + sizeof(*tas_fmw->programs), GFP_KERNEL); + if (!tas_fmw->programs) + return -ENOMEM; + + for (i =3D 0; i < tas_fmw->nr_programs; i++) { + program =3D &(tas_fmw->programs[i]); + program->prog_size =3D get_unaligned_be32(&buf[offset]); + offset +=3D 4; + } + + /* Skip the unused prog_size */ + offset +=3D 4 * (TASDEVICE_MAXPROGRAM_NUM_KERNEL - tas_fmw->nr_programs); + + tas_fmw->nr_configurations =3D get_unaligned_be32(&buf[offset]); + offset +=3D 4; + + /* + * The max number of config in firmware greater than 4 pieces of + * tas2781s is different from the one lower than 4 pieces of + * tas2781s. + */ + max_confs =3D TASDEVICE_MAXCONFIG_NUM_KERNEL; + if (tas_fmw->nr_configurations =3D=3D 0 || + tas_fmw->nr_configurations > max_confs) { + dev_err(tas_priv->dev, "%s: Conf is invalid\n", __func__); + kfree(tas_fmw->programs); + return -EINVAL; + } + + if (offset + 4 * max_confs > fmw->size) { + dev_err(tas_priv->dev, "%s: mpConfigurations err\n", __func__); + kfree(tas_fmw->programs); + return -EINVAL; + } + + tas_fmw->configs =3D kcalloc(tas_fmw->nr_configurations, + sizeof(*tas_fmw->configs), GFP_KERNEL); + if (!tas_fmw->configs) { + kfree(tas_fmw->programs); + return -ENOMEM; + } + + for (i =3D 0; i < tas_fmw->nr_programs; i++) { + config =3D &(tas_fmw->configs[i]); + config->cfg_size =3D get_unaligned_be32(&buf[offset]); + offset +=3D 4; + } + + /* Skip the unused configs */ + offset +=3D 4 * (max_confs - tas_fmw->nr_programs); + + return offset; +} + +static int tasdevice_single_byte_wr(void *context, unsigned char *data, + int sublocksize) +{ + struct tasdevice_priv *tas_priv =3D context; + unsigned short len =3D get_unaligned_be16(&data[2]); + int i, subblk_offset, rc; + + subblk_offset =3D 4; + if (subblk_offset + 4 * len > sublocksize) { + dev_err(tas_priv->dev, "process_block: Out of boundary\n"); + return 0; + } + + for (i =3D 0; i < len; i++) { + rc =3D tasdevice_spi_dev_write(tas_priv, + TASDEVICE_REG(data[subblk_offset], + data[subblk_offset + 1], data[subblk_offset + 2]), + data[subblk_offset + 3]); + if (rc < 0) { + dev_err(tas_priv->dev, + "process_block: single write error\n"); + subblk_offset |=3D OFFSET_ERROR_BIT; + } + subblk_offset +=3D 4; + } + + return subblk_offset; +} + +static int tasdevice_burst_wr(void *context, unsigned char *data, + int sublocksize) +{ + struct tasdevice_priv *tas_priv =3D context; + unsigned short len =3D get_unaligned_be16(&data[2]); + int subblk_offset, rc; + + subblk_offset =3D 4; + if (subblk_offset + 4 + len > sublocksize) { + dev_err(tas_priv->dev, "%s: BST Out of boundary\n", __func__); + subblk_offset |=3D OFFSET_ERROR_BIT; + } + if (len % 4) { + dev_err(tas_priv->dev, "%s:Bst-len(%u)not div by 4\n", + __func__, len); + subblk_offset |=3D OFFSET_ERROR_BIT; + } + + rc =3D tasdevice_spi_dev_bulk_write(tas_priv, + TASDEVICE_REG(data[subblk_offset], data[subblk_offset + 1], + data[subblk_offset + 2]), &(data[subblk_offset + 4]), len); + if (rc < 0) { + dev_err(tas_priv->dev, "%s: bulk_write error =3D %d\n", + __func__, rc); + subblk_offset |=3D OFFSET_ERROR_BIT; + } + subblk_offset +=3D (len + 4); + + return subblk_offset; +} + +static int tasdevice_delay(void *context, unsigned char *data, + int sublocksize) +{ + struct tasdevice_priv *tas_priv =3D context; + unsigned int sleep_time, subblk_offset =3D 2; + + if (subblk_offset + 2 > sublocksize) { + dev_err(tas_priv->dev, "%s: delay Out of boundary\n", + __func__); + subblk_offset |=3D OFFSET_ERROR_BIT; + } + sleep_time =3D get_unaligned_be16(&data[2]) * 1000; + usleep_range(sleep_time, sleep_time + 50); + subblk_offset +=3D 2; + + return subblk_offset; +} + +static int tasdevice_field_wr(void *context, unsigned char *data, + int sublocksize) +{ + struct tasdevice_priv *tas_priv =3D context; + int rc, subblk_offset =3D 2; + + if (subblk_offset + 6 > sublocksize) { + dev_err(tas_priv->dev, "%s: bit write Out of boundary\n", + __func__); + subblk_offset |=3D OFFSET_ERROR_BIT; + } + rc =3D tasdevice_spi_dev_update_bits(tas_priv, + TASDEVICE_REG(data[subblk_offset + 2], + data[subblk_offset + 3], + data[subblk_offset + 4]), + data[subblk_offset + 1], data[subblk_offset + 5]); + if (rc < 0) { + dev_err(tas_priv->dev, "%s: update_bits error =3D %d\n", + __func__, rc); + subblk_offset |=3D OFFSET_ERROR_BIT; + } + subblk_offset +=3D 6; + + return subblk_offset; +} + +static int tasdevice_process_block(void *context, unsigned char *data, + unsigned char dev_idx, int sublocksize) +{ + struct tasdevice_priv *tas_priv =3D context; + int blktyp =3D dev_idx & 0xC0, subblk_offset; + unsigned char subblk_typ =3D data[1]; + + if (tas_priv->tasdevice.is_loading =3D=3D false) + return sublocksize; + + subblk_offset =3D 2; + switch (subblk_typ) { + case TASDEVICE_CMD_SING_W: + subblk_offset =3D tasdevice_single_byte_wr(tas_priv, data, + sublocksize); + break; + case TASDEVICE_CMD_BURST: + subblk_offset =3D tasdevice_burst_wr(tas_priv, data, + sublocksize); + break; + case TASDEVICE_CMD_DELAY: + subblk_offset =3D tasdevice_delay(tas_priv, data, sublocksize); + break; + case TASDEVICE_CMD_FIELD_W: + subblk_offset =3D tasdevice_field_wr(tas_priv, data, + sublocksize); + break; + default: + break; + } + if (((subblk_offset & OFFSET_ERROR_BIT) !=3D 0) && blktyp !=3D 0) { + if (blktyp =3D=3D 0x80) { + tas_priv->tasdevice.cur_prog =3D -1; + tas_priv->tasdevice.cur_conf =3D -1; + } else + tas_priv->tasdevice.cur_conf =3D -1; + } + subblk_offset &=3D ~OFFSET_ERROR_BIT; + + return subblk_offset; +} + +void tasdevice_spi_select_cfg_blk(void *pContext, int conf_no, + unsigned char block_type) +{ + struct tasdevice_priv *tas_priv =3D pContext; + struct tasdevice_rca *rca =3D &(tas_priv->rcabin); + struct tasdevice_config_info **cfg_info =3D rca->cfg_info; + struct tasdev_blk_data **blk_data; + unsigned int j, k; + + if (conf_no >=3D rca->ncfgs || conf_no < 0 || !cfg_info) { + dev_err(tas_priv->dev, "conf_no should be not more than %u\n", + rca->ncfgs); + return; + } + blk_data =3D cfg_info[conf_no]->blk_data; + + for (j =3D 0; j < cfg_info[conf_no]->real_nblocks; j++) { + unsigned int length =3D 0, rc =3D 0; + + if (block_type > 5 || block_type < 2) { + dev_err(tas_priv->dev, + "block_type should be in range from 2 to 5\n"); + break; + } + if (block_type !=3D blk_data[j]->block_type) + continue; + + for (k =3D 0; k < blk_data[j]->n_subblks; k++) { + tas_priv->tasdevice.is_loading =3D true; + + rc =3D tasdevice_process_block(tas_priv, + blk_data[j]->regdata + length, + blk_data[j]->dev_idx, + blk_data[j]->block_size - length); + length +=3D rc; + if (blk_data[j]->block_size < length) { + dev_err(tas_priv->dev, + "%s: %u %u out of boundary\n", + __func__, length, + blk_data[j]->block_size); + break; + } + } + if (length !=3D blk_data[j]->block_size) + dev_err(tas_priv->dev, "%s: %u %u size is not same\n", + __func__, length, blk_data[j]->block_size); + } +} + +static int tasdevice_load_block_kernel( + struct tasdevice_priv *tasdevice, struct tasdev_blk *block) +{ + const unsigned int blk_size =3D block->blk_size; + unsigned char *data =3D block->data; + unsigned int i, length; + + for (i =3D 0, length =3D 0; i < block->nr_subblocks; i++) { + int rc =3D tasdevice_process_block(tasdevice, data + length, + block->dev_idx, blk_size - length); + if (rc < 0) { + dev_err(tasdevice->dev, + "%s: %u %u sublock write error\n", + __func__, length, blk_size); + break; + } + length +=3D rc; + if (blk_size < length) { + dev_err(tasdevice->dev, "%s: %u %u out of boundary\n", + __func__, length, blk_size); + break; + } + } + + return 0; +} + +static int fw_parse_variable_hdr(struct tasdevice_priv *tas_priv, + struct tasdevice_dspfw_hdr *fw_hdr, + const struct firmware *fmw, int offset) +{ + const unsigned char *buf =3D fmw->data; + int len =3D strlen((char *)&buf[offset]); + + len++; + + if (offset + len + 8 > fmw->size) { + dev_err(tas_priv->dev, "%s: File Size error\n", __func__); + return -EINVAL; + } + + offset +=3D len; + + fw_hdr->device_family =3D get_unaligned_be32(&buf[offset]); + if (fw_hdr->device_family !=3D 0) { + dev_err(tas_priv->dev, "%s: not TAS device\n", __func__); + return -EINVAL; + } + offset +=3D 4; + + fw_hdr->device =3D get_unaligned_be32(&buf[offset]); + if (fw_hdr->device >=3D TASDEVICE_DSP_TAS_MAX_DEVICE || + fw_hdr->device =3D=3D 6) { + dev_err(tas_priv->dev, "Unsupported dev %d\n", fw_hdr->device); + return -EINVAL; + } + offset +=3D 4; + fw_hdr->ndev =3D 1; + + return offset; +} + +static int fw_parse_variable_header_git(struct tasdevice_priv + *tas_priv, const struct firmware *fmw, int offset) +{ + struct tasdevice_fw *tas_fmw =3D tas_priv->fmw; + struct tasdevice_dspfw_hdr *fw_hdr =3D &(tas_fmw->fw_hdr); + + offset =3D fw_parse_variable_hdr(tas_priv, fw_hdr, fmw, offset); + + return offset; +} + +static int fw_parse_block_data(struct tasdevice_fw *tas_fmw, + struct tasdev_blk *block, const struct firmware *fmw, int offset) +{ + unsigned char *data =3D (unsigned char *)fmw->data; + int n; + + if (offset + 8 > fmw->size) { + dev_err(tas_fmw->dev, "%s: Type error\n", __func__); + return -EINVAL; + } + block->type =3D get_unaligned_be32(&data[offset]); + offset +=3D 4; + + if (tas_fmw->fw_hdr.fixed_hdr.drv_ver >=3D PPC_DRIVER_CRCCHK) { + if (offset + 8 > fmw->size) { + dev_err(tas_fmw->dev, "PChkSumPresent error\n"); + return -EINVAL; + } + block->is_pchksum_present =3D data[offset]; + offset++; + + block->pchksum =3D data[offset]; + offset++; + + block->is_ychksum_present =3D data[offset]; + offset++; + + block->ychksum =3D data[offset]; + offset++; + } else { + block->is_pchksum_present =3D 0; + block->is_ychksum_present =3D 0; + } + + block->nr_cmds =3D get_unaligned_be32(&data[offset]); + offset +=3D 4; + + n =3D block->nr_cmds * 4; + if (offset + n > fmw->size) { + dev_err(tas_fmw->dev, + "%s: File Size(%lu) error offset =3D %d n =3D %d\n", + __func__, (unsigned long)fmw->size, offset, n); + return -EINVAL; + } + /* instead of kzalloc+memcpy */ + block->data =3D kmemdup(&data[offset], n, GFP_KERNEL); + if (!block->data) + return -ENOMEM; + + offset +=3D n; + + return offset; +} + +/* + * When parsing error occurs, all the memory resource will be released + * in the end of tasdevice_rca_ready. + */ +static int fw_parse_data(struct tasdevice_fw *tas_fmw, + struct tasdevice_data *img_data, const struct firmware *fmw, + int offset) +{ + const unsigned char *data =3D (unsigned char *)fmw->data; + struct tasdev_blk *blk; + unsigned int i, n; + + if (offset + 64 > fmw->size) { + dev_err(tas_fmw->dev, "%s: Name error\n", __func__); + return -EINVAL; + } + memcpy(img_data->name, &data[offset], 64); + offset +=3D 64; + + n =3D strlen((char *)&data[offset]); + n++; + if (offset + n + 2 > fmw->size) { + dev_err(tas_fmw->dev, "%s: Description error\n", __func__); + return -EINVAL; + } + offset +=3D n; + img_data->nr_blk =3D get_unaligned_be16(&data[offset]); + offset +=3D 2; + + img_data->dev_blks =3D kcalloc(img_data->nr_blk, + sizeof(*img_data->dev_blks), GFP_KERNEL); + if (!img_data->dev_blks) + return -ENOMEM; + + for (i =3D 0; i < img_data->nr_blk; i++) { + blk =3D &(img_data->dev_blks[i]); + offset =3D fw_parse_block_data(tas_fmw, blk, fmw, offset); + if (offset < 0) + return -EINVAL; + } + + return offset; +} + +/* + * When parsing error occurs, all the memory resource will be released + * in the end of tasdevice_rca_ready. + */ +static int fw_parse_program_data(struct tasdevice_priv *tas_priv, + struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset) +{ + unsigned char *buf =3D (unsigned char *)fmw->data; + struct tasdevice_prog *program; + int i; + + if (offset + 2 > fmw->size) { + dev_err(tas_priv->dev, "%s: File Size error\n", __func__); + return -EINVAL; + } + tas_fmw->nr_programs =3D get_unaligned_be16(&buf[offset]); + offset +=3D 2; + + if (tas_fmw->nr_programs =3D=3D 0) { + /*Not error in calibration Data file, return directly*/ + dev_info(tas_priv->dev, "%s: No Programs data, maybe calbin\n", + __func__); + return offset; + } + + tas_fmw->programs =3D + kcalloc(tas_fmw->nr_programs, sizeof(*tas_fmw->programs), + GFP_KERNEL); + if (!tas_fmw->programs) + return -ENOMEM; + + for (i =3D 0; i < tas_fmw->nr_programs; i++) { + int n =3D 0; + + program =3D &(tas_fmw->programs[i]); + if (offset + 64 > fmw->size) { + dev_err(tas_priv->dev, "%s: mpName error\n", __func__); + return -EINVAL; + } + offset +=3D 64; + + n =3D strlen((char *)&buf[offset]); + /* skip '\0' and 5 unused bytes */ + n +=3D 6; + if (offset + n > fmw->size) { + dev_err(tas_priv->dev, "Description err\n"); + return -EINVAL; + } + + offset +=3D n; + + offset =3D fw_parse_data(tas_fmw, &(program->dev_data), fmw, + offset); + if (offset < 0) + return offset; + } + + return offset; +} + +/* + * When parsing error occurs, all the memory resource will be released + * in the end of tasdevice_rca_ready. + */ +static int fw_parse_configuration_data(struct tasdevice_priv *tas_priv, + struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset) +{ + unsigned char *data =3D (unsigned char *)fmw->data; + struct tasdevice_config *config; + unsigned int i, n; + + if (offset + 2 > fmw->size) { + dev_err(tas_priv->dev, "%s: File Size error\n", __func__); + return -EINVAL; + } + tas_fmw->nr_configurations =3D get_unaligned_be16(&data[offset]); + offset +=3D 2; + + if (tas_fmw->nr_configurations =3D=3D 0) { + dev_err(tas_priv->dev, "%s: Conf is zero\n", __func__); + /*Not error for calibration Data file, return directly*/ + return offset; + } + tas_fmw->configs =3D kcalloc(tas_fmw->nr_configurations, + sizeof(*tas_fmw->configs), GFP_KERNEL); + if (!tas_fmw->configs) + return -ENOMEM; + for (i =3D 0; i < tas_fmw->nr_configurations; i++) { + config =3D &(tas_fmw->configs[i]); + if (offset + 64 > fmw->size) { + dev_err(tas_priv->dev, "File Size err\n"); + return -EINVAL; + } + memcpy(config->name, &data[offset], 64); + offset +=3D 64; + + n =3D strlen((char *)&data[offset]); + n +=3D 15; + if (offset + n > fmw->size) { + dev_err(tas_priv->dev, "Description err\n"); + return -EINVAL; + } + + offset +=3D n; + + offset =3D fw_parse_data(tas_fmw, &(config->dev_data), + fmw, offset); + if (offset < 0) + break; + } + + return offset; +} + +static bool check_inpage_yram_rg(struct tas_crc *cd, + unsigned char reg, unsigned char len) +{ + bool in =3D false; + + if (reg <=3D TAS2781_YRAM5_END_REG && + reg >=3D TAS2781_YRAM5_START_REG) { + if (reg + len > TAS2781_YRAM5_END_REG) + cd->len =3D TAS2781_YRAM5_END_REG - reg + 1; + else + cd->len =3D len; + cd->offset =3D reg; + in =3D true; + } else if (reg < TAS2781_YRAM5_START_REG) { + if (reg + len > TAS2781_YRAM5_START_REG) { + cd->offset =3D TAS2781_YRAM5_START_REG; + cd->len =3D len - TAS2781_YRAM5_START_REG + reg; + in =3D true; + } + } + + return in; +} + +static bool check_inpage_yram_bk1(struct tas_crc *cd, + unsigned char page, unsigned char reg, unsigned char len) +{ + bool in =3D false; + + if (page =3D=3D TAS2781_YRAM1_PAGE) { + if (reg >=3D TAS2781_YRAM1_START_REG) { + cd->offset =3D reg; + cd->len =3D len; + in =3D true; + } else if (reg + len > TAS2781_YRAM1_START_REG) { + cd->offset =3D TAS2781_YRAM1_START_REG; + cd->len =3D len - TAS2781_YRAM1_START_REG + reg; + in =3D true; + } + } else if (page =3D=3D TAS2781_YRAM3_PAGE) { + in =3D check_inpage_yram_rg(cd, reg, len); + } + + return in; +} + +/* + * Return Code: + * true -- the registers are in the inpage yram + * false -- the registers are NOT in the inpage yram + */ +static bool check_inpage_yram(struct tas_crc *cd, unsigned char book, + unsigned char page, unsigned char reg, unsigned char len) +{ + bool in =3D false; + + if (book =3D=3D TAS2781_YRAM_BOOK1) + in =3D check_inpage_yram_bk1(cd, page, reg, len); + else if (book =3D=3D TAS2781_YRAM_BOOK2 && page =3D=3D TAS2781_YRAM5_PAGE) + in =3D check_inpage_yram_rg(cd, reg, len); + + return in; +} + +static bool check_inblock_yram_bk(struct tas_crc *cd, + unsigned char page, unsigned char reg, unsigned char len) +{ + bool in =3D false; + + if ((page >=3D TAS2781_YRAM4_START_PAGE && + page <=3D TAS2781_YRAM4_END_PAGE) || + (page >=3D TAS2781_YRAM2_START_PAGE && + page <=3D TAS2781_YRAM2_END_PAGE)) { + if (reg <=3D TAS2781_YRAM2_END_REG && + reg >=3D TAS2781_YRAM2_START_REG) { + cd->offset =3D reg; + cd->len =3D len; + in =3D true; + } else if (reg < TAS2781_YRAM2_START_REG) { + if (reg + len - 1 >=3D TAS2781_YRAM2_START_REG) { + cd->offset =3D TAS2781_YRAM2_START_REG; + cd->len =3D reg + len - TAS2781_YRAM2_START_REG; + in =3D true; + } + } + } + + return in; +} + +/* + * Return Code: + * true -- the registers are in the inblock yram + * false -- the registers are NOT in the inblock yram + */ +static bool check_inblock_yram(struct tas_crc *cd, unsigned char book, + unsigned char page, unsigned char reg, unsigned char len) +{ + bool in =3D false; + + if (book =3D=3D TAS2781_YRAM_BOOK1 || book =3D=3D TAS2781_YRAM_BOOK2) + in =3D check_inblock_yram_bk(cd, page, reg, len); + + return in; +} + +static bool check_yram(struct tas_crc *cd, unsigned char book, + unsigned char page, unsigned char reg, unsigned char len) +{ + bool in; + + in =3D check_inpage_yram(cd, book, page, reg, len); + if (!in) + in =3D check_inblock_yram(cd, book, page, reg, len); + + return in; +} + +static int tasdev_multibytes_chksum(struct tasdevice_priv *tasdevice, + unsigned char book, unsigned char page, + unsigned char reg, unsigned int len) +{ + struct tas_crc crc_data; + unsigned char crc_chksum =3D 0; + unsigned char nBuf1[128]; + int ret =3D 0, i; + bool in; + + if ((reg + len - 1) > 127) { + ret =3D -EINVAL; + dev_err(tasdevice->dev, "firmware error\n"); + goto end; + } + + if ((book =3D=3D TASDEVICE_BOOK_ID(TAS2781_SA_COEFF_SWAP_REG)) && + (page =3D=3D TASDEVICE_PAGE_ID(TAS2781_SA_COEFF_SWAP_REG)) && + (reg =3D=3D TASDEVICE_PAGE_REG(TAS2781_SA_COEFF_SWAP_REG)) && + (len =3D=3D 4)) { + /*DSP swap command, pass */ + ret =3D 0; + goto end; + } + + in =3D check_yram(&crc_data, book, page, reg, len); + if (!in) + goto end; + + if (len =3D=3D 1) { + dev_err(tasdevice->dev, "firmware error\n"); + ret =3D -EINVAL; + goto end; + } + + ret =3D tasdevice_spi_dev_bulk_read(tasdevice, + TASDEVICE_REG(book, page, crc_data.offset), + nBuf1, crc_data.len); + if (ret < 0) + goto end; + + for (i =3D 0; i < crc_data.len; i++) { + if ((book =3D=3D TASDEVICE_BOOK_ID(TAS2781_SA_COEFF_SWAP_REG)) && + (page =3D=3D TASDEVICE_PAGE_ID(TAS2781_SA_COEFF_SWAP_REG)) && + ((i + crc_data.offset) >=3D + TASDEVICE_PAGE_REG(TAS2781_SA_COEFF_SWAP_REG)) && + ((i + crc_data.offset) <=3D + (TASDEVICE_PAGE_REG(TAS2781_SA_COEFF_SWAP_REG) + 4))) + /*DSP swap command, bypass */ + continue; + else + crc_chksum +=3D crc8(tasdevice->crc8_lkp_tbl, &nBuf1[i], + 1, 0); + } + + ret =3D crc_chksum; + +end: + return ret; +} + +static int do_singlereg_checksum(struct tasdevice_priv *tasdevice, + unsigned char book, unsigned char page, + unsigned char reg, unsigned char val) +{ + struct tas_crc crc_data; + unsigned int nData1; + int ret =3D 0; + bool in; + + /*DSP swap command, pass */ + if ((book =3D=3D TASDEVICE_BOOK_ID(TAS2781_SA_COEFF_SWAP_REG)) && + (page =3D=3D TASDEVICE_PAGE_ID(TAS2781_SA_COEFF_SWAP_REG)) && + (reg >=3D TASDEVICE_PAGE_REG(TAS2781_SA_COEFF_SWAP_REG)) && + (reg <=3D (TASDEVICE_PAGE_REG(TAS2781_SA_COEFF_SWAP_REG) + 4))) + return 0; + + in =3D check_yram(&crc_data, book, page, reg, 1); + if (!in) + return 0; + ret =3D tasdevice_spi_dev_read(tasdevice, + TASDEVICE_REG(book, page, reg), &nData1); + if (ret < 0) + return ret; + + if (nData1 !=3D val) { + dev_err(tasdevice->dev, + "B[0x%x]P[0x%x]R[0x%x] W[0x%x], R[0x%x]\n", + book, page, reg, val, nData1); + tasdevice->tasdevice.err_code |=3D ERROR_YRAM_CRCCHK; + return -EAGAIN; + } + + ret =3D crc8(tasdevice->crc8_lkp_tbl, &val, 1, 0); + + return ret; +} + +static void set_err_prg_cfg(unsigned int type, struct tasdevice *dev) +{ + if ((type =3D=3D MAIN_ALL_DEVICES) || (type =3D=3D MAIN_DEVICE_A) || + (type =3D=3D MAIN_DEVICE_B) || (type =3D=3D MAIN_DEVICE_C) || + (type =3D=3D MAIN_DEVICE_D)) + dev->cur_prog =3D -1; + else + dev->cur_conf =3D -1; +} + +static int tasdev_bytes_chksum(struct tasdevice_priv *tas_priv, + struct tasdev_blk *block, unsigned char book, + unsigned char page, unsigned char reg, unsigned int len, + unsigned char val, unsigned char *crc_chksum) +{ + int ret; + + if (len > 1) + ret =3D tasdev_multibytes_chksum(tas_priv, book, page, reg, + len); + else + ret =3D do_singlereg_checksum(tas_priv, book, page, reg, val); + + if (ret > 0) { + *crc_chksum +=3D ret; + goto end; + } + + if (ret !=3D -EAGAIN) + goto end; + + block->nr_retry--; + if (block->nr_retry > 0) + goto end; + + set_err_prg_cfg(block->type, &tas_priv->tasdevice); + +end: + return ret; +} + +static int tasdev_multibytes_wr(struct tasdevice_priv *tas_priv, + struct tasdev_blk *block, unsigned char book, + unsigned char page, unsigned char reg, unsigned char *data, + unsigned int len, unsigned int *nr_cmds, + unsigned char *crc_chksum) +{ + int ret; + + if (len > 1) { + ret =3D tasdevice_spi_dev_bulk_write(tas_priv, + TASDEVICE_REG(book, page, reg), data + 3, len); + if (ret < 0) + return ret; + if (block->is_ychksum_present) + ret =3D tasdev_bytes_chksum(tas_priv, block, + book, page, reg, len, 0, crc_chksum); + } else { + ret =3D tasdevice_spi_dev_write(tas_priv, + TASDEVICE_REG(book, page, reg), data[3]); + if (ret < 0) + return ret; + if (block->is_ychksum_present) + ret =3D tasdev_bytes_chksum(tas_priv, block, book, + page, reg, 1, data[3], crc_chksum); + } + + if (!block->is_ychksum_present || ret >=3D 0) { + *nr_cmds +=3D 1; + if (len >=3D 2) + *nr_cmds +=3D ((len - 2) / 4) + 1; + } + + return ret; +} + +static int tasdev_block_chksum(struct tasdevice_priv *tas_priv, + struct tasdev_blk *block) +{ + unsigned int nr_value; + int ret; + + ret =3D tasdevice_spi_dev_read(tas_priv, TASDEVICE_Checksum, &nr_value); + if (ret < 0) { + dev_err(tas_priv->dev, "%s: read error %d.\n", __func__, ret); + set_err_prg_cfg(block->type, &tas_priv->tasdevice); + return ret; + } + + if ((nr_value & 0xff) !=3D block->pchksum) { + dev_err(tas_priv->dev, "%s: PChkSum err %d ", __func__, ret); + dev_err(tas_priv->dev, "PChkSum =3D 0x%x, Reg =3D 0x%x\n", + block->pchksum, (nr_value & 0xff)); + tas_priv->tasdevice.err_code |=3D ERROR_PRAM_CRCCHK; + ret =3D -EAGAIN; + block->nr_retry--; + + if (block->nr_retry <=3D 0) + set_err_prg_cfg(block->type, + &tas_priv->tasdevice); + } else { + tas_priv->tasdevice.err_code &=3D ~ERROR_PRAM_CRCCHK; + } + + return ret; +} + +static int tasdev_load_blk(struct tasdevice_priv *tas_priv, + struct tasdev_blk *block) +{ + unsigned int sleep_time, len, nr_cmds; + unsigned char offset, book, page, val; + unsigned char *data =3D block->data; + unsigned char crc_chksum =3D 0; + int ret =3D 0; + + while (block->nr_retry > 0) { + if (block->is_pchksum_present) { + ret =3D tasdevice_spi_dev_write(tas_priv, + TASDEVICE_Checksum, 0); + if (ret < 0) + break; + } + + if (block->is_ychksum_present) + crc_chksum =3D 0; + + nr_cmds =3D 0; + + while (nr_cmds < block->nr_cmds) { + data =3D block->data + nr_cmds * 4; + + book =3D data[0]; + page =3D data[1]; + offset =3D data[2]; + val =3D data[3]; + + nr_cmds++; + /*Single byte write*/ + if (offset <=3D 0x7F) { + ret =3D tasdevice_spi_dev_write(tas_priv, + TASDEVICE_REG(book, page, offset), + val); + if (ret < 0) + break; + if (block->is_ychksum_present) { + ret =3D tasdev_bytes_chksum(tas_priv, + block, book, page, offset, + 1, val, &crc_chksum); + if (ret < 0) + break; + } + continue; + } + /*sleep command*/ + if (offset =3D=3D 0x81) { + /*book -- data[0] page -- data[1]*/ + sleep_time =3D ((book << 8) + page)*1000; + usleep_range(sleep_time, sleep_time + 50); + continue; + } + /*Multiple bytes write*/ + if (offset =3D=3D 0x85) { + data +=3D 4; + len =3D (book << 8) + page; + book =3D data[0]; + page =3D data[1]; + offset =3D data[2]; + ret =3D tasdev_multibytes_wr(tas_priv, + block, book, page, offset, data, + len, &nr_cmds, &crc_chksum); + if (ret < 0) + break; + } + } + if (ret =3D=3D -EAGAIN) { + if (block->nr_retry > 0) + continue; + } else if (ret < 0) { + /*err in current device, skip it*/ + break; + } + + if (block->is_pchksum_present) { + ret =3D tasdev_block_chksum(tas_priv, block); + if (ret =3D=3D -EAGAIN) { + if (block->nr_retry > 0) + continue; + } else if (ret < 0) { + /*err in current device, skip it*/ + break; + } + } + + if (block->is_ychksum_present) { + /* TBD, open it when FW ready */ + dev_err(tas_priv->dev, + "Blk YChkSum: FW =3D 0x%x, YCRC =3D 0x%x\n", + block->ychksum, crc_chksum); + + tas_priv->tasdevice.err_code &=3D + ~ERROR_YRAM_CRCCHK; + ret =3D 0; + } + /*skip current blk*/ + break; + } + + return ret; +} + +static int tasdevice_load_block(struct tasdevice_priv *tas_priv, + struct tasdev_blk *block) +{ + int ret =3D 0; + + block->nr_retry =3D 6; + if (tas_priv->tasdevice.is_loading =3D=3D false) + return 0; + ret =3D tasdev_load_blk(tas_priv, block); + if (ret < 0) + dev_err(tas_priv->dev, "Blk (%d) load error\n", block->type); + + return ret; +} + +static int dspfw_default_callback(struct tasdevice_priv *tas_priv, + unsigned int drv_ver, unsigned int ppcver) +{ + int rc =3D 0; + + if (drv_ver =3D=3D 0x100) { + if (ppcver >=3D PPC3_VERSION) { + tas_priv->fw_parse_variable_header =3D + fw_parse_variable_header_kernel; + tas_priv->fw_parse_program_data =3D + fw_parse_program_data_kernel; + tas_priv->fw_parse_configuration_data =3D + fw_parse_configuration_data_kernel; + tas_priv->tasdevice_load_block =3D + tasdevice_load_block_kernel; + } else { + switch (ppcver) { + case 0x00: + tas_priv->fw_parse_variable_header =3D + fw_parse_variable_header_git; + tas_priv->fw_parse_program_data =3D + fw_parse_program_data; + tas_priv->fw_parse_configuration_data =3D + fw_parse_configuration_data; + tas_priv->tasdevice_load_block =3D + tasdevice_load_block; + break; + default: + dev_err(tas_priv->dev, + "%s: PPCVer must be 0x0 or 0x%02x", + __func__, PPC3_VERSION); + dev_err(tas_priv->dev, " Current:0x%02x\n", + ppcver); + rc =3D -EINVAL; + break; + } + } + } else { + dev_err(tas_priv->dev, + "DrvVer must be 0x0, 0x230 or above 0x230 "); + dev_err(tas_priv->dev, "current is 0x%02x\n", drv_ver); + rc =3D -EINVAL; + } + + return rc; +} + +static int load_calib_data(struct tasdevice_priv *tas_priv, + struct tasdevice_data *dev_data) +{ + struct tasdev_blk *block; + unsigned int i; + int ret =3D 0; + + for (i =3D 0; i < dev_data->nr_blk; i++) { + block =3D &(dev_data->dev_blks[i]); + ret =3D tasdevice_load_block(tas_priv, block); + if (ret < 0) + break; + } + + return ret; +} + +static int fw_parse_header(struct tasdevice_priv *tas_priv, + struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset) +{ + struct tasdevice_dspfw_hdr *fw_hdr =3D &(tas_fmw->fw_hdr); + struct tasdevice_fw_fixed_hdr *fw_fixed_hdr =3D &(fw_hdr->fixed_hdr); + static const unsigned char magic_number[] =3D {0x35, 0x35, 0x35, 0x32, }; + const unsigned char *buf =3D (unsigned char *)fmw->data; + + if (offset + 92 > fmw->size) { + dev_err(tas_priv->dev, "%s: File Size error\n", __func__); + offset =3D -EINVAL; + goto out; + } + if (memcmp(&buf[offset], magic_number, 4)) { + dev_err(tas_priv->dev, "%s: Magic num NOT match\n", __func__); + offset =3D -EINVAL; + goto out; + } + offset +=3D 4; + + /* + * Convert data[offset], data[offset + 1], data[offset + 2] and + * data[offset + 3] into host + */ + fw_fixed_hdr->fwsize =3D get_unaligned_be32(&buf[offset]); + offset +=3D 4; + if (fw_fixed_hdr->fwsize !=3D fmw->size) { + dev_err(tas_priv->dev, "File size not match, %lu %u", + (unsigned long)fmw->size, fw_fixed_hdr->fwsize); + offset =3D -EINVAL; + goto out; + } + offset +=3D 4; + fw_fixed_hdr->ppcver =3D get_unaligned_be32(&buf[offset]); + offset +=3D 8; + fw_fixed_hdr->drv_ver =3D get_unaligned_be32(&buf[offset]); + offset +=3D 72; + +out: + return offset; +} + +static int fw_parse_variable_hdr_cal(struct tasdevice_priv *tas_priv, + struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset) +{ + struct tasdevice_dspfw_hdr *fw_hdr =3D &(tas_fmw->fw_hdr); + + offset =3D fw_parse_variable_hdr(tas_priv, fw_hdr, fmw, offset); + if (offset < 0) + return offset; + if (fw_hdr->ndev !=3D 1) { + dev_err(tas_priv->dev, + "%s: calbin must be 1, but currently ndev(%u)\n", + __func__, fw_hdr->ndev); + offset =3D -EINVAL; + } + + return offset; +} + +/* + * When calibrated data parsing error occurs, DSP can still work with defa= ult + * calibrated data, memory resource related to calibrated data will be + * released in the tasdevice_codec_remove. + */ +static int fw_parse_calibration_data(struct tasdevice_priv *tas_priv, + struct tasdevice_fw *tas_fmw, const struct firmware *fmw, int offset) +{ + struct tasdevice_calibration *calibration; + unsigned char *data =3D (unsigned char *)fmw->data; + unsigned int i, n; + + if (offset + 2 > fmw->size) { + dev_err(tas_priv->dev, "%s: Calibrations error\n", __func__); + return -EINVAL; + } + tas_fmw->nr_calibrations =3D get_unaligned_be16(&data[offset]); + offset +=3D 2; + + if (tas_fmw->nr_calibrations !=3D 1) { + dev_err(tas_priv->dev, + "%s: only supports one calibration (%d)!\n", + __func__, tas_fmw->nr_calibrations); + return offset; + } + + tas_fmw->calibrations =3D kcalloc(tas_fmw->nr_calibrations, + sizeof(*tas_fmw->calibrations), GFP_KERNEL); + if (!tas_fmw->calibrations) + return -ENOMEM; + for (i =3D 0; i < tas_fmw->nr_calibrations; i++) { + if (offset + 64 > fmw->size) { + dev_err(tas_priv->dev, "Calibrations error\n"); + return -EINVAL; + } + calibration =3D &(tas_fmw->calibrations[i]); + offset +=3D 64; + + n =3D strlen((char *)&data[offset]); + /* skip '\0' and 2 unused bytes */ + n +=3D 3; + if (offset + n > fmw->size) { + dev_err(tas_priv->dev, "Description err\n"); + return -EINVAL; + } + offset +=3D n; + + offset =3D fw_parse_data(tas_fmw, &(calibration->dev_data), fmw, + offset); + if (offset < 0) + return offset; + } + + return offset; +} + +int tas2781_spi_load_calibration(void *context, char *file_name, + unsigned short i) +{ + struct tasdevice_priv *tas_priv =3D context; + struct tasdevice *tasdev =3D &(tas_priv->tasdevice); + const struct firmware *fw_entry; + struct tasdevice_fw *tas_fmw; + struct firmware fmw; + int offset =3D 0; + int ret; + + ret =3D request_firmware(&fw_entry, file_name, tas_priv->dev); + if (ret) { + dev_err(tas_priv->dev, "%s: Request firmware %s failed\n", + __func__, file_name); + goto out; + } + + if (!fw_entry->size) { + dev_err(tas_priv->dev, "%s: file read error: size =3D %lu\n", + __func__, (unsigned long)fw_entry->size); + ret =3D -EINVAL; + goto out; + } + fmw.size =3D fw_entry->size; + fmw.data =3D fw_entry->data; + + tas_fmw =3D tasdev->cali_data_fmw =3D kzalloc(sizeof(*tas_fmw), + GFP_KERNEL); + if (!tasdev->cali_data_fmw) { + ret =3D -ENOMEM; + goto out; + } + tas_fmw->dev =3D tas_priv->dev; + offset =3D fw_parse_header(tas_priv, tas_fmw, &fmw, offset); + if (offset =3D=3D -EINVAL) { + dev_err(tas_priv->dev, "fw_parse_header EXIT!\n"); + ret =3D offset; + goto out; + } + offset =3D fw_parse_variable_hdr_cal(tas_priv, tas_fmw, &fmw, offset); + if (offset =3D=3D -EINVAL) { + dev_err(tas_priv->dev, + "%s: fw_parse_variable_header_cal EXIT!\n", __func__); + ret =3D offset; + goto out; + } + offset =3D fw_parse_program_data(tas_priv, tas_fmw, &fmw, offset); + if (offset < 0) { + dev_err(tas_priv->dev, "fw_parse_program_data EXIT!\n"); + ret =3D offset; + goto out; + } + offset =3D fw_parse_configuration_data(tas_priv, tas_fmw, &fmw, offset); + if (offset < 0) { + dev_err(tas_priv->dev, "fw_parse_configuration_data EXIT!\n"); + ret =3D offset; + goto out; + } + offset =3D fw_parse_calibration_data(tas_priv, tas_fmw, &fmw, offset); + if (offset < 0) { + dev_err(tas_priv->dev, "fw_parse_calibration_data EXIT!\n"); + ret =3D offset; + goto out; + } + +out: + if (fw_entry) + release_firmware(fw_entry); + + return ret; +} + +static int tasdevice_dspfw_ready(const struct firmware *fmw, void *context) +{ + struct tasdevice_priv *tas_priv =3D context; + struct tasdevice_fw_fixed_hdr *fw_fixed_hdr; + struct tasdevice_fw *tas_fmw; + int offset =3D 0, ret =3D 0; + + if (!fmw || !fmw->data) { + dev_err(tas_priv->dev, "%s: Failed to read firmware %s\n", + __func__, tas_priv->coef_binaryname); + return -EINVAL; + } + + tas_priv->fmw =3D kzalloc(sizeof(*tas_priv->fmw), GFP_KERNEL); + if (!tas_priv->fmw) + return -ENOMEM; + tas_fmw =3D tas_priv->fmw; + tas_fmw->dev =3D tas_priv->dev; + offset =3D fw_parse_header(tas_priv, tas_fmw, fmw, offset); + + if (offset =3D=3D -EINVAL) + return -EINVAL; + + fw_fixed_hdr =3D &(tas_fmw->fw_hdr.fixed_hdr); + /* Support different versions of firmware */ + switch (fw_fixed_hdr->drv_ver) { + case 0x301: + case 0x302: + case 0x502: + case 0x503: + tas_priv->fw_parse_variable_header =3D + fw_parse_variable_header_kernel; + tas_priv->fw_parse_program_data =3D + fw_parse_program_data_kernel; + tas_priv->fw_parse_configuration_data =3D + fw_parse_configuration_data_kernel; + tas_priv->tasdevice_load_block =3D + tasdevice_load_block_kernel; + break; + case 0x202: + case 0x400: + tas_priv->fw_parse_variable_header =3D + fw_parse_variable_header_git; + tas_priv->fw_parse_program_data =3D + fw_parse_program_data; + tas_priv->fw_parse_configuration_data =3D + fw_parse_configuration_data; + tas_priv->tasdevice_load_block =3D + tasdevice_load_block; + break; + default: + ret =3D dspfw_default_callback(tas_priv, + fw_fixed_hdr->drv_ver, fw_fixed_hdr->ppcver); + if (ret) + return ret; + break; + } + + offset =3D tas_priv->fw_parse_variable_header(tas_priv, fmw, offset); + if (offset < 0) + return offset; + + offset =3D tas_priv->fw_parse_program_data(tas_priv, tas_fmw, fmw, + offset); + if (offset < 0) + return offset; + + offset =3D tas_priv->fw_parse_configuration_data(tas_priv, + tas_fmw, fmw, offset); + if (offset < 0) + ret =3D offset; + + return ret; +} + +int tasdevice_spi_dsp_parser(void *context) +{ + struct tasdevice_priv *tas_priv =3D context; + const struct firmware *fw_entry; + int ret; + + ret =3D request_firmware(&fw_entry, tas_priv->coef_binaryname, + tas_priv->dev); + if (ret) { + dev_err(tas_priv->dev, "%s: load %s error\n", __func__, + tas_priv->coef_binaryname); + return ret; + } + + ret =3D tasdevice_dspfw_ready(fw_entry, tas_priv); + release_firmware(fw_entry); + fw_entry =3D NULL; + + return ret; +} + +static void tasdev_dsp_prog_blk_remove(struct tasdevice_prog *prog) +{ + struct tasdevice_data *tas_dt; + struct tasdev_blk *blk; + unsigned int i; + + if (!prog) + return; + + tas_dt =3D &(prog->dev_data); + + if (!tas_dt->dev_blks) + return; + + for (i =3D 0; i < tas_dt->nr_blk; i++) { + blk =3D &(tas_dt->dev_blks[i]); + kfree(blk->data); + } + kfree(tas_dt->dev_blks); +} + +static void tasdev_dsp_prog_remove(struct tasdevice_prog *prog, + unsigned short nr) +{ + int i; + + for (i =3D 0; i < nr; i++) + tasdev_dsp_prog_blk_remove(&prog[i]); + kfree(prog); +} + +static void tasdev_dsp_cfg_blk_remove(struct tasdevice_config *cfg) +{ + struct tasdevice_data *tas_dt; + struct tasdev_blk *blk; + unsigned int i; + + if (cfg) { + tas_dt =3D &(cfg->dev_data); + + if (!tas_dt->dev_blks) + return; + + for (i =3D 0; i < tas_dt->nr_blk; i++) { + blk =3D &(tas_dt->dev_blks[i]); + kfree(blk->data); + } + kfree(tas_dt->dev_blks); + } +} + +static void tasdev_dsp_cfg_remove(struct tasdevice_config *config, + unsigned short nr) +{ + int i; + + for (i =3D 0; i < nr; i++) + tasdev_dsp_cfg_blk_remove(&config[i]); + kfree(config); +} + +void tasdevice_spi_dsp_remove(void *context) +{ + struct tasdevice_priv *tas_dev =3D context; + + if (!tas_dev->fmw) + return; + + if (tas_dev->fmw->programs) + tasdev_dsp_prog_remove(tas_dev->fmw->programs, + tas_dev->fmw->nr_programs); + if (tas_dev->fmw->configs) + tasdev_dsp_cfg_remove(tas_dev->fmw->configs, + tas_dev->fmw->nr_configurations); + kfree(tas_dev->fmw); + tas_dev->fmw =3D NULL; +} + +static void tas2781_clear_calfirmware(struct tasdevice_fw *tas_fmw) +{ + struct tasdevice_calibration *calibration; + struct tasdev_blk *block; + unsigned int blks; + int i; + + if (!tas_fmw->calibrations) + goto out; + + for (i =3D 0; i < tas_fmw->nr_calibrations; i++) { + calibration =3D &(tas_fmw->calibrations[i]); + if (!calibration) + continue; + + if (!calibration->dev_data.dev_blks) + continue; + + for (blks =3D 0; blks < calibration->dev_data.nr_blk; blks++) { + block =3D &(calibration->dev_data.dev_blks[blks]); + if (!block) + continue; + kfree(block->data); + } + kfree(calibration->dev_data.dev_blks); + } + kfree(tas_fmw->calibrations); +out: + kfree(tas_fmw); +} + +void tasdevice_spi_calbin_remove(void *context) +{ + struct tasdevice_priv *tas_priv =3D context; + struct tasdevice *tasdev; + + tasdev =3D &(tas_priv->tasdevice); + if (tasdev->cali_data_fmw) { + tas2781_clear_calfirmware(tasdev->cali_data_fmw); + tasdev->cali_data_fmw =3D NULL; + } +} + +void tasdevice_spi_config_info_remove(void *context) +{ + struct tasdevice_priv *tas_priv =3D context; + struct tasdevice_rca *rca =3D &(tas_priv->rcabin); + struct tasdevice_config_info **ci =3D rca->cfg_info; + unsigned int i, j; + + if (!ci) + return; + for (i =3D 0; i < rca->ncfgs; i++) { + if (!ci[i]) + continue; + if (ci[i]->blk_data) { + for (j =3D 0; j < ci[i]->real_nblocks; j++) { + if (!ci[i]->blk_data[j]) + continue; + kfree(ci[i]->blk_data[j]->regdata); + kfree(ci[i]->blk_data[j]); + } + kfree(ci[i]->blk_data); + } + kfree(ci[i]); + } + kfree(ci); +} + +static int tasdevice_load_data(struct tasdevice_priv *tas_priv, + struct tasdevice_data *dev_data) +{ + struct tasdev_blk *block; + unsigned int i; + int ret =3D 0; + + for (i =3D 0; i < dev_data->nr_blk; i++) { + block =3D &(dev_data->dev_blks[i]); + ret =3D tas_priv->tasdevice_load_block(tas_priv, block); + if (ret < 0) + break; + } + + return ret; +} + +int tasdevice_spi_select_tuningprm_cfg(void *context, int prm_no, + int cfg_no, int rca_conf_no) +{ + struct tasdevice_priv *tas_priv =3D context; + struct tasdevice_rca *rca =3D &(tas_priv->rcabin); + struct tasdevice_config_info **cfg_info =3D rca->cfg_info; + struct tasdevice_fw *tas_fmw =3D tas_priv->fmw; + struct tasdevice_prog *program; + struct tasdevice_config *conf; + int prog_status =3D 0; + int status; + + if (!tas_fmw) { + dev_err(tas_priv->dev, "%s: Firmware is NULL\n", __func__); + return 0; + } + + if (cfg_no >=3D tas_fmw->nr_configurations) { + dev_err(tas_priv->dev, + "%s: cfg(%d) is not in range of conf %u\n", + __func__, cfg_no, tas_fmw->nr_configurations); + return 0; + } + + if (prm_no >=3D tas_fmw->nr_programs) { + dev_err(tas_priv->dev, + "%s: prm(%d) is not in range of Programs %u\n", + __func__, prm_no, tas_fmw->nr_programs); + return 0; + } + + if (rca_conf_no >=3D rca->ncfgs || + rca_conf_no < 0 || + !cfg_info) { + dev_err(tas_priv->dev, + "conf_no:%d should be in range from 0 to %u\n", + rca_conf_no, rca->ncfgs-1); + return 0; + } + + if (cfg_info[rca_conf_no]->active_dev & 1) { + if (prm_no >=3D 0 && + (tas_priv->tasdevice.cur_prog !=3D prm_no || + tas_priv->force_fwload_status)) { + tas_priv->tasdevice.cur_conf =3D -1; + tas_priv->tasdevice.is_loading =3D true; + prog_status++; + } + } else { + tas_priv->tasdevice.is_loading =3D false; + } + + if (prog_status) { + program =3D &(tas_fmw->programs[prm_no]); + tasdevice_load_data(tas_priv, &(program->dev_data)); + if (tas_priv->tasdevice.is_loaderr =3D=3D false && + tas_priv->tasdevice.is_loading =3D=3D true) { + struct tasdevice_fw *cal_fmw =3D + tas_priv->tasdevice.cali_data_fmw; + + if (cal_fmw) { + struct tasdevice_calibration + *cal =3D cal_fmw->calibrations; + + if (cal) + load_calib_data(tas_priv, + &(cal->dev_data)); + } + tas_priv->tasdevice.cur_prog =3D prm_no; + } + + } + + if (cfg_no && + (cfg_no !=3D tas_priv->tasdevice.cur_conf) && + (cfg_info[rca_conf_no]->active_dev & 1) && + (tas_priv->tasdevice.is_loaderr =3D=3D false)) { + status++; + tas_priv->tasdevice.is_loading =3D true; + } else { + tas_priv->tasdevice.is_loading =3D false; + } + + if (status) { + conf =3D &(tas_fmw->configs[cfg_no]); + status =3D 0; + tasdevice_load_data(tas_priv, &(conf->dev_data)); + if (tas_priv->tasdevice.is_loaderr =3D=3D true) { + status |=3D 1 << 4; + } else if (tas_priv->tasdevice.is_loaderr =3D=3D false && + tas_priv->tasdevice.is_loading =3D=3D true) { + tas_priv->tasdevice.cur_conf =3D cfg_no; + } + } else { + dev_dbg(tas_priv->dev, "%s: Unneeded loading dsp conf %d\n", + __func__, cfg_no); + } + + status |=3D cfg_info[rca_conf_no]->active_dev; + + return prog_status; +} + +int tasdevice_spi_prmg_load(void *context, int prm_no) +{ + struct tasdevice_priv *tas_priv =3D context; + struct tasdevice_fw *tas_fmw =3D tas_priv->fmw; + struct tasdevice_prog *program; + int prog_status =3D 0; + + if (!tas_fmw) { + dev_err(tas_priv->dev, "%s: Firmware is NULL\n", __func__); + return 0; + } + + if (prm_no >=3D tas_fmw->nr_programs) { + dev_err(tas_priv->dev, + "%s: prm(%d) is not in range of Programs %u\n", + __func__, prm_no, tas_fmw->nr_programs); + return 0; + } + + if (prm_no >=3D 0 && tas_priv->tasdevice.cur_prog !=3D prm_no) { + tas_priv->tasdevice.cur_conf =3D -1; + tas_priv->tasdevice.is_loading =3D true; + prog_status++; + } + + if (prog_status) { + program =3D &(tas_fmw->programs[prm_no]); + tasdevice_load_data(tas_priv, &(program->dev_data)); + if ((tas_priv->tasdevice.is_loaderr =3D=3D false) && + (tas_priv->tasdevice.is_loading =3D=3D true)) + tas_priv->tasdevice.cur_prog =3D prm_no; + } + + return prog_status; +} + +int tasdevice_spi_prmg_calibdata_load(void *context, int prm_no) +{ + struct tasdevice_priv *tas_priv =3D context; + struct tasdevice_fw *tas_fmw =3D tas_priv->fmw; + struct tasdevice_prog *program; + int prog_status =3D 0; + + if (!tas_fmw) { + dev_err(tas_priv->dev, "%s: Firmware is NULL\n", __func__); + return 0; + } + + if (prm_no >=3D tas_fmw->nr_programs) { + dev_err(tas_priv->dev, + "%s: prm(%d) is not in range of Programs %u\n", + __func__, prm_no, tas_fmw->nr_programs); + return 0; + } + + if (prm_no >=3D 0 && tas_priv->tasdevice.cur_prog !=3D prm_no) { + tas_priv->tasdevice.cur_conf =3D -1; + tas_priv->tasdevice.is_loading =3D true; + prog_status++; + } + tas_priv->tasdevice.is_loaderr =3D false; + + if (prog_status) { + program =3D &(tas_fmw->programs[prm_no]); + tasdevice_load_data(tas_priv, &(program->dev_data)); + if (tas_priv->tasdevice.is_loaderr =3D=3D false && + tas_priv->tasdevice.is_loading =3D=3D true) { + struct tasdevice_fw *cal_fmw =3D + tas_priv->tasdevice.cali_data_fmw; + + if (cal_fmw) { + struct tasdevice_calibration *cal =3D + cal_fmw->calibrations; + + if (cal) + load_calib_data(tas_priv, + &(cal->dev_data)); + } + tas_priv->tasdevice.cur_prog =3D prm_no; + } + + } + + return prog_status; +} + +void tasdevice_spi_tuning_switch(void *context, int state) +{ + struct tasdevice_priv *tas_priv =3D context; + struct tasdevice_fw *tas_fmw =3D tas_priv->fmw; + int profile_cfg_id =3D tas_priv->rcabin.profile_cfg_id; + + if (tas_priv->fw_state =3D=3D TASDEVICE_DSP_FW_FAIL) { + dev_err(tas_priv->dev, "DSP bin file not loaded\n"); + return; + } + + if (state =3D=3D 0) { + if (tas_priv->cur_prog < tas_fmw->nr_programs) { + /*dsp mode or tuning mode*/ + profile_cfg_id =3D tas_priv->rcabin.profile_cfg_id; + tasdevice_spi_select_tuningprm_cfg(tas_priv, + tas_priv->cur_prog, tas_priv->cur_conf, + profile_cfg_id); + } + + tasdevice_spi_select_cfg_blk(tas_priv, profile_cfg_id, + TASDEVICE_BIN_BLK_PRE_POWER_UP); + } else + tasdevice_spi_select_cfg_blk(tas_priv, profile_cfg_id, + TASDEVICE_BIN_BLK_PRE_SHUTDOWN); +} + +MODULE_DESCRIPTION("Texas Firmware Support"); +MODULE_AUTHOR("Baojun Xu, TI, "); +MODULE_LICENSE("GPL"); --=20 2.40.1