Skip to content

Commit 77b696f

Browse files
rgantoisbroonie
authored andcommitted
ASoC: tlv320aic31xx: Add support for loading filter coefficients
The TLV320DAC3100 Audio DAC has 25 built-in digital audio processing blocks (PRBs). Each of these PRBs has a static filter structure with programmable coefficients. Once a PRB is selected for use by the DAC, its filter coefficients can be configured via a dedicated set of registers. Define a new optional firmware which can be loaded by the TLV320DAC driver. This firmware describes a full set of filter coefficients for all blocks used by the various PRBs. The firmware's binary format is heavily inspired by the one used in the peb2466 driver. It includes a version marker to allow for potential evolutions of the format. Note that adaptive filtering is not supported i.e. filter coefficients are loaded once before power-on and then cannot be changed while the DAC is powered. This is why only page A coefficients are modified. Page B coefficients are only used for adaptive filtering. Signed-off-by: Romain Gantois <romain.gantois@bootlin.com> Link: https://patch.msgid.link/20240906-tlv320-filter-v1-1-6955f53ff435@bootlin.com Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 241c044 commit 77b696f

1 file changed

Lines changed: 100 additions & 0 deletions

File tree

sound/soc/codecs/tlv320aic31xx.c

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* and mono/stereo Class-D speaker driver.
1313
*/
1414

15+
#include <asm/unaligned.h>
1516
#include <linux/module.h>
1617
#include <linux/moduleparam.h>
1718
#include <linux/init.h>
@@ -22,6 +23,7 @@
2223
#include <linux/gpio/consumer.h>
2324
#include <linux/regulator/consumer.h>
2425
#include <linux/acpi.h>
26+
#include <linux/firmware.h>
2527
#include <linux/of.h>
2628
#include <linux/slab.h>
2729
#include <sound/core.h>
@@ -1638,6 +1640,98 @@ static const struct i2c_device_id aic31xx_i2c_id[] = {
16381640
};
16391641
MODULE_DEVICE_TABLE(i2c, aic31xx_i2c_id);
16401642

1643+
static int tlv320dac3100_fw_load(struct aic31xx_priv *aic31xx,
1644+
const u8 *data, size_t size)
1645+
{
1646+
int ret, reg;
1647+
u16 val16;
1648+
1649+
/*
1650+
* Coefficients firmware binary structure. Multi-byte values are big-endian.
1651+
*
1652+
* @0, 16bits: Magic (0xB30C)
1653+
* @2, 16bits: Version (0x0100 for version 1.0)
1654+
* @4, 8bits: DAC Processing Block Selection
1655+
* @5, 62 16-bit values: Page 8 buffer A DAC programmable filter coefficients
1656+
* @129, 12 16-bit values: Page 9 Buffer A DAC programmable filter coefficients
1657+
*
1658+
* Filter coefficients are interpreted as two's complement values
1659+
* ranging from -32 768 to 32 767. For more details on filter coefficients,
1660+
* please refer to the TLV320DAC3100 datasheet, tables 6-120 and 6-123.
1661+
*/
1662+
1663+
if (size != 153) {
1664+
dev_err(aic31xx->dev, "firmware size is %zu, expected 153 bytes\n", size);
1665+
return -EINVAL;
1666+
}
1667+
1668+
/* Check magic */
1669+
val16 = get_unaligned_be16(data);
1670+
if (val16 != 0xb30c) {
1671+
dev_err(aic31xx->dev, "fw magic is 0x%04x expected 0xb30c\n", val16);
1672+
return -EINVAL;
1673+
}
1674+
data += 2;
1675+
1676+
/* Check version */
1677+
val16 = get_unaligned_be16(data);
1678+
if (val16 != 0x0100) {
1679+
dev_err(aic31xx->dev, "invalid firmware version 0x%04x! expected 1", val16);
1680+
return -EINVAL;
1681+
}
1682+
data += 2;
1683+
1684+
ret = regmap_write(aic31xx->regmap, AIC31XX_DACPRB, *data);
1685+
if (ret) {
1686+
dev_err(aic31xx->dev, "failed to write PRB index: err %d\n", ret);
1687+
return ret;
1688+
}
1689+
data += 1;
1690+
1691+
/* Page 8 Buffer A coefficients */
1692+
for (reg = 2; reg < 126; reg++) {
1693+
ret = regmap_write(aic31xx->regmap, AIC31XX_REG(8, reg), *data);
1694+
if (ret) {
1695+
dev_err(aic31xx->dev,
1696+
"failed to write page 8 filter coefficient %d: err %d\n", reg, ret);
1697+
return ret;
1698+
}
1699+
data++;
1700+
}
1701+
1702+
/* Page 9 Buffer A coefficients */
1703+
for (reg = 2; reg < 26; reg++) {
1704+
ret = regmap_write(aic31xx->regmap, AIC31XX_REG(9, reg), *data);
1705+
if (ret) {
1706+
dev_err(aic31xx->dev,
1707+
"failed to write page 9 filter coefficient %d: err %d\n", reg, ret);
1708+
return ret;
1709+
}
1710+
data++;
1711+
}
1712+
1713+
dev_info(aic31xx->dev, "done loading DAC filter coefficients\n");
1714+
1715+
return ret;
1716+
}
1717+
1718+
static int tlv320dac3100_load_coeffs(struct aic31xx_priv *aic31xx,
1719+
const char *fw_name)
1720+
{
1721+
const struct firmware *fw;
1722+
int ret;
1723+
1724+
ret = request_firmware(&fw, fw_name, aic31xx->dev);
1725+
if (ret)
1726+
return ret;
1727+
1728+
ret = tlv320dac3100_fw_load(aic31xx, fw->data, fw->size);
1729+
1730+
release_firmware(fw);
1731+
1732+
return ret;
1733+
}
1734+
16411735
static int aic31xx_i2c_probe(struct i2c_client *i2c)
16421736
{
16431737
struct aic31xx_priv *aic31xx;
@@ -1727,6 +1821,12 @@ static int aic31xx_i2c_probe(struct i2c_client *i2c)
17271821
}
17281822
}
17291823

1824+
if (aic31xx->codec_type == DAC3100) {
1825+
ret = tlv320dac3100_load_coeffs(aic31xx, "tlv320dac3100-coeffs.bin");
1826+
if (ret)
1827+
dev_warn(aic31xx->dev, "Did not load any filter coefficients\n");
1828+
}
1829+
17301830
if (aic31xx->codec_type & DAC31XX_BIT)
17311831
return devm_snd_soc_register_component(&i2c->dev,
17321832
&soc_codec_driver_aic31xx,

0 commit comments

Comments
 (0)