Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
@@ -0,0 +1,145 @@
# SPDX-License-Identifier: GPL-2.0-only
config SND_HDA_CIRRUS_SCODEC
tristate "Cirrus side-codec library" if KUNIT
config SND_HDA_CIRRUS_SCODEC_KUNIT_TEST
tristate "KUnit test for Cirrus side-codec library" if !KUNIT_ALL_TESTS
depends on SND_HDA_CIRRUS_SCODEC && GPIOLIB && KUNIT
default KUNIT_ALL_TESTS
help
This builds KUnit tests for the cirrus side-codec library.
For more information on KUnit and unit tests in general,
please refer to the KUnit documentation in
Documentation/dev-tools/kunit/.
If in doubt, say "N".
config SND_HDA_SCODEC_CS35L41
tristate
select SND_HDA_GENERIC
select REGMAP_IRQ
select FW_CS_DSP
config SND_HDA_SCODEC_COMPONENT
tristate
config SND_HDA_SCODEC_CS35L41_I2C
tristate "Build CS35L41 HD-audio side codec support for I2C Bus"
depends on I2C
depends on ACPI
depends on EFI
depends on SND_SOC
imply SERIAL_MULTI_INSTANTIATE
select SND_SOC_CS35L41_LIB
select SND_HDA_SCODEC_CS35L41
select SND_SOC_CS_AMP_LIB
help
Say Y or M here to include CS35L41 I2C 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=y && SND_HDA_SCODEC_CS35L41_I2C=m
config SND_HDA_SCODEC_CS35L41_SPI
tristate "Build CS35L41 HD-audio codec support for SPI Bus"
depends on SPI_MASTER
depends on ACPI
depends on EFI
depends on SND_SOC
imply SERIAL_MULTI_INSTANTIATE
select SND_SOC_CS35L41_LIB
select SND_HDA_SCODEC_CS35L41
select SND_SOC_CS_AMP_LIB
help
Say Y or M here to include CS35L41 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=y && SND_HDA_SCODEC_CS35L41_SPI=m
config SND_HDA_SCODEC_CS35L56
tristate
config SND_HDA_SCODEC_CS35L56_I2C
tristate "Build CS35L56 HD-audio side codec support for I2C Bus"
depends on I2C
depends on ACPI
depends on SND_SOC
select FW_CS_DSP
imply SERIAL_MULTI_INSTANTIATE
select SND_HDA_GENERIC
select SND_SOC_CS35L56_SHARED
select SND_HDA_SCODEC_CS35L56
select SND_HDA_CIRRUS_SCODEC
select SND_SOC_CS_AMP_LIB
help
Say Y or M here to include CS35L56 amplifier support with
I2C control.
config SND_HDA_SCODEC_CS35L56_SPI
tristate "Build CS35L56 HD-audio side codec support for SPI Bus"
depends on SPI_MASTER
depends on ACPI
depends on SND_SOC
select FW_CS_DSP
imply SERIAL_MULTI_INSTANTIATE
select SND_HDA_GENERIC
select SND_SOC_CS35L56_SHARED
select SND_HDA_SCODEC_CS35L56
select SND_HDA_CIRRUS_SCODEC
select SND_SOC_CS_AMP_LIB
help
Say Y or M here to include CS35L56 amplifier support with
SPI control.
menu "CS35L56 driver options"
depends on SND_HDA_SCODEC_CS35L56
config SND_HDA_SCODEC_CS35L56_CAL_DEBUGFS
bool "CS35L56 create debugfs for factory calibration"
depends on DEBUG_FS
select SND_SOC_CS35L56_CAL_DEBUGFS_COMMON
help
Create debugfs entries used during factory-line manufacture
for factory calibration.
If unsure select "N".
endmenu
config SND_HDA_SCODEC_TAS2781
tristate
select SND_HDA_GENERIC
config SND_HDA_SCODEC_TAS2781_I2C
tristate "Build TAS2781 HD-audio side codec support for I2C Bus"
depends on I2C
depends on ACPI
depends on EFI
depends on SND_SOC
select SND_HDA_SCODEC_TAS2781
select SND_SOC_TAS2781_COMLIB_I2C
select SND_SOC_TAS2781_FMWLIB
select CRC32
help
Say Y or M here to include TAS2781 I2C 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=y && SND_HDA_SCODEC_TAS2781_I2C=m
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_TAS2781
select SND_SOC_TAS2781_COMLIB
select SND_SOC_TAS2781_FMWLIB
select CRC8
select CRC32
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=y && SND_HDA_SCODEC_TAS2781_SPI=m
@@ -0,0 +1,28 @@
# SPDX-License-Identifier: GPL-2.0
subdir-ccflags-y += -I$(src)/../../common
snd-hda-cirrus-scodec-y := cirrus_scodec.o
snd-hda-cirrus-scodec-test-y := cirrus_scodec_test.o
snd-hda-scodec-cs35l41-y := cs35l41_hda.o cs35l41_hda_property.o
snd-hda-scodec-cs35l41-i2c-y := cs35l41_hda_i2c.o
snd-hda-scodec-cs35l41-spi-y := cs35l41_hda_spi.o
snd-hda-scodec-cs35l56-y := cs35l56_hda.o
snd-hda-scodec-cs35l56-i2c-y := cs35l56_hda_i2c.o
snd-hda-scodec-cs35l56-spi-y := cs35l56_hda_spi.o
snd-hda-scodec-component-y := hda_component.o
snd-hda-scodec-tas2781-y := tas2781_hda.o
snd-hda-scodec-tas2781-i2c-y := tas2781_hda_i2c.o
snd-hda-scodec-tas2781-spi-y := tas2781_hda_spi.o
obj-$(CONFIG_SND_HDA_CIRRUS_SCODEC) += snd-hda-cirrus-scodec.o
obj-$(CONFIG_SND_HDA_CIRRUS_SCODEC_KUNIT_TEST) += snd-hda-cirrus-scodec-test.o
obj-$(CONFIG_SND_HDA_SCODEC_CS35L41) += snd-hda-scodec-cs35l41.o
obj-$(CONFIG_SND_HDA_SCODEC_CS35L41_I2C) += snd-hda-scodec-cs35l41-i2c.o
obj-$(CONFIG_SND_HDA_SCODEC_CS35L41_SPI) += snd-hda-scodec-cs35l41-spi.o
obj-$(CONFIG_SND_HDA_SCODEC_CS35L56) += snd-hda-scodec-cs35l56.o
obj-$(CONFIG_SND_HDA_SCODEC_CS35L56_I2C) += snd-hda-scodec-cs35l56-i2c.o
obj-$(CONFIG_SND_HDA_SCODEC_CS35L56_SPI) += snd-hda-scodec-cs35l56-spi.o
obj-$(CONFIG_SND_HDA_SCODEC_COMPONENT) += snd-hda-scodec-component.o
obj-$(CONFIG_SND_HDA_SCODEC_TAS2781) += snd-hda-scodec-tas2781.o
obj-$(CONFIG_SND_HDA_SCODEC_TAS2781_I2C) += snd-hda-scodec-tas2781-i2c.o
obj-$(CONFIG_SND_HDA_SCODEC_TAS2781_SPI) += snd-hda-scodec-tas2781-spi.o
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: GPL-2.0-only
//
// Common code for Cirrus side-codecs.
//
// Copyright (C) 2021, 2023 Cirrus Logic, Inc. and
// Cirrus Logic International Semiconductor Ltd.
#include <linux/dev_printk.h>
#include <linux/gpio/consumer.h>
#include <linux/module.h>
#include "cirrus_scodec.h"
int cirrus_scodec_get_speaker_id(struct device *dev, int amp_index,
int num_amps, int fixed_gpio_id)
{
struct gpio_desc *speaker_id_desc;
int speaker_id = -ENOENT;
if (fixed_gpio_id >= 0) {
dev_dbg(dev, "Found Fixed Speaker ID GPIO (index = %d)\n", fixed_gpio_id);
speaker_id_desc = gpiod_get_index(dev, NULL, fixed_gpio_id, GPIOD_IN);
if (IS_ERR(speaker_id_desc)) {
speaker_id = PTR_ERR(speaker_id_desc);
return speaker_id;
}
speaker_id = gpiod_get_value_cansleep(speaker_id_desc);
gpiod_put(speaker_id_desc);
} else {
int base_index;
int gpios_per_amp;
int count;
int tmp;
int i;
count = gpiod_count(dev, "spk-id");
if (count > 0) {
speaker_id = 0;
gpios_per_amp = count / num_amps;
base_index = gpios_per_amp * amp_index;
if (count % num_amps)
return -EINVAL;
dev_dbg(dev, "Found %d Speaker ID GPIOs per Amp\n", gpios_per_amp);
for (i = 0; i < gpios_per_amp; i++) {
speaker_id_desc = gpiod_get_index(dev, "spk-id", i + base_index,
GPIOD_IN);
if (IS_ERR(speaker_id_desc)) {
speaker_id = PTR_ERR(speaker_id_desc);
break;
}
tmp = gpiod_get_value_cansleep(speaker_id_desc);
gpiod_put(speaker_id_desc);
if (tmp < 0) {
speaker_id = tmp;
break;
}
speaker_id |= tmp << i;
}
}
}
dev_dbg(dev, "Speaker ID = %d\n", speaker_id);
return speaker_id;
}
EXPORT_SYMBOL_NS_GPL(cirrus_scodec_get_speaker_id, "SND_HDA_CIRRUS_SCODEC");
MODULE_DESCRIPTION("HDA Cirrus side-codec library");
MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
MODULE_LICENSE("GPL");
@@ -0,0 +1,13 @@
/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (C) 2023 Cirrus Logic, Inc. and
* Cirrus Logic International Semiconductor Ltd.
*/
#ifndef CIRRUS_SCODEC_H
#define CIRRUS_SCODEC_H
int cirrus_scodec_get_speaker_id(struct device *dev, int amp_index,
int num_amps, int fixed_gpio_id);
#endif /* CIRRUS_SCODEC_H */
@@ -0,0 +1,330 @@
// SPDX-License-Identifier: GPL-2.0-only
//
// KUnit test for the Cirrus side-codec library.
//
// Copyright (C) 2023 Cirrus Logic, Inc. and
// Cirrus Logic International Semiconductor Ltd.
#include <kunit/resource.h>
#include <kunit/test.h>
#include <linux/device.h>
#include <linux/device/devres.h>
#include <linux/device/faux.h>
#include <linux/gpio/driver.h>
#include <linux/module.h>
#include "cirrus_scodec.h"
KUNIT_DEFINE_ACTION_WRAPPER(faux_device_destroy_wrapper, faux_device_destroy,
struct faux_device *)
KUNIT_DEFINE_ACTION_WRAPPER(device_remove_software_node_wrapper,
device_remove_software_node,
struct device *)
struct cirrus_scodec_test_gpio {
unsigned int pin_state;
struct gpio_chip chip;
};
struct cirrus_scodec_test_priv {
struct faux_device *amp_dev;
struct faux_device *gpio_dev;
struct cirrus_scodec_test_gpio *gpio_priv;
};
static int cirrus_scodec_test_gpio_get_direction(struct gpio_chip *chip,
unsigned int offset)
{
return GPIO_LINE_DIRECTION_IN;
}
static int cirrus_scodec_test_gpio_direction_in(struct gpio_chip *chip,
unsigned int offset)
{
return 0;
}
static int cirrus_scodec_test_gpio_get(struct gpio_chip *chip, unsigned int offset)
{
struct cirrus_scodec_test_gpio *gpio_priv = gpiochip_get_data(chip);
return !!(gpio_priv->pin_state & BIT(offset));
}
static int cirrus_scodec_test_gpio_direction_out(struct gpio_chip *chip,
unsigned int offset, int value)
{
return -EOPNOTSUPP;
}
static int cirrus_scodec_test_gpio_set(struct gpio_chip *chip,
unsigned int offset, int value)
{
return -EOPNOTSUPP;
}
static int cirrus_scodec_test_gpio_set_config(struct gpio_chip *gc,
unsigned int offset,
unsigned long config)
{
switch (pinconf_to_config_param(config)) {
case PIN_CONFIG_LEVEL:
case PIN_CONFIG_OUTPUT_ENABLE:
return -EOPNOTSUPP;
default:
return 0;
}
}
static const struct gpio_chip cirrus_scodec_test_gpio_chip = {
.label = "cirrus_scodec_test_gpio",
.owner = THIS_MODULE,
.request = gpiochip_generic_request,
.free = gpiochip_generic_free,
.get_direction = cirrus_scodec_test_gpio_get_direction,
.direction_input = cirrus_scodec_test_gpio_direction_in,
.get = cirrus_scodec_test_gpio_get,
.direction_output = cirrus_scodec_test_gpio_direction_out,
.set = cirrus_scodec_test_gpio_set,
.set_config = cirrus_scodec_test_gpio_set_config,
.base = -1,
.ngpio = 32,
};
/* software_node referencing the gpio driver */
static const struct software_node cirrus_scodec_test_gpio_swnode = {
.name = "cirrus_scodec_test_gpio",
};
static int cirrus_scodec_test_gpio_probe(struct faux_device *fdev)
{
struct cirrus_scodec_test_gpio *gpio_priv;
int ret;
gpio_priv = devm_kzalloc(&fdev->dev, sizeof(*gpio_priv), GFP_KERNEL);
if (!gpio_priv)
return -ENOMEM;
ret = device_add_software_node(&fdev->dev, &cirrus_scodec_test_gpio_swnode);
if (ret)
return ret;
ret = devm_add_action_or_reset(&fdev->dev, device_remove_software_node_wrapper,
&fdev->dev);
if (ret)
return ret;
/* GPIO core modifies our struct gpio_chip so use a copy */
gpio_priv->chip = cirrus_scodec_test_gpio_chip;
gpio_priv->chip.parent = &fdev->dev;
ret = devm_gpiochip_add_data(&fdev->dev, &gpio_priv->chip, gpio_priv);
if (ret)
return dev_err_probe(&fdev->dev, ret, "Failed to add gpiochip\n");
dev_set_drvdata(&fdev->dev, gpio_priv);
return 0;
}
static const struct faux_device_ops cirrus_scodec_test_gpio_driver_ops = {
.probe = cirrus_scodec_test_gpio_probe,
};
static void cirrus_scodec_test_create_gpio(struct kunit *test)
{
struct cirrus_scodec_test_priv *priv = test->priv;
priv->gpio_dev = faux_device_create("cirrus_scodec_test_gpio_drv", NULL,
&cirrus_scodec_test_gpio_driver_ops);
KUNIT_ASSERT_NOT_NULL(test, priv->gpio_dev);
KUNIT_ASSERT_EQ(test, 0, kunit_add_action_or_reset(test,
faux_device_destroy_wrapper,
priv->gpio_dev));
priv->gpio_priv = dev_get_drvdata(&priv->gpio_dev->dev);
KUNIT_ASSERT_NOT_NULL(test, priv->gpio_priv);
}
static void cirrus_scodec_test_set_gpio_ref_arg(struct software_node_ref_args *arg,
int gpio_num)
{
struct software_node_ref_args template =
SOFTWARE_NODE_REFERENCE(&cirrus_scodec_test_gpio_swnode, gpio_num, 0);
*arg = template;
}
static int cirrus_scodec_test_set_spkid_swnode(struct kunit *test,
struct device *dev,
struct software_node_ref_args *args,
int num_args)
{
const struct property_entry props_template[] = {
PROPERTY_ENTRY_REF_ARRAY_LEN("spk-id-gpios", args, num_args),
{ }
};
struct property_entry *props;
struct software_node *node;
node = kunit_kzalloc(test, sizeof(*node), GFP_KERNEL);
if (!node)
return -ENOMEM;
props = kunit_kzalloc(test, sizeof(props_template), GFP_KERNEL);
if (!props)
return -ENOMEM;
memcpy(props, props_template, sizeof(props_template));
node->properties = props;
return device_add_software_node(dev, node);
}
struct cirrus_scodec_test_spkid_param {
int num_amps;
int gpios_per_amp;
int num_amps_sharing;
};
static void cirrus_scodec_test_spkid_parse(struct kunit *test)
{
struct cirrus_scodec_test_priv *priv = test->priv;
const struct cirrus_scodec_test_spkid_param *param = test->param_value;
int num_spk_id_refs = param->num_amps * param->gpios_per_amp;
struct software_node_ref_args *refs;
struct device *dev = &priv->amp_dev->dev;
unsigned int v;
int i, ret;
refs = kunit_kcalloc(test, num_spk_id_refs, sizeof(*refs), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, refs);
for (i = 0, v = 0; i < num_spk_id_refs; ) {
cirrus_scodec_test_set_gpio_ref_arg(&refs[i++], v++);
/*
* If amps are sharing GPIOs repeat the last set of
* GPIOs until we've done that number of amps.
* We have done all GPIOs for an amp when i is a multiple
* of gpios_per_amp.
* We have done all amps sharing the same GPIOs when i is
* a multiple of (gpios_per_amp * num_amps_sharing).
*/
if (!(i % param->gpios_per_amp) &&
(i % (param->gpios_per_amp * param->num_amps_sharing)))
v -= param->gpios_per_amp;
}
ret = cirrus_scodec_test_set_spkid_swnode(test, dev, refs, num_spk_id_refs);
KUNIT_EXPECT_EQ_MSG(test, ret, 0, "Failed to add swnode\n");
for (i = 0; i < param->num_amps; ++i) {
for (v = 0; v < (1 << param->gpios_per_amp); ++v) {
/* Set only the GPIO bits used by this amp */
priv->gpio_priv->pin_state =
v << (param->gpios_per_amp * (i / param->num_amps_sharing));
ret = cirrus_scodec_get_speaker_id(dev, i, param->num_amps, -1);
KUNIT_EXPECT_EQ_MSG(test, ret, v,
"get_speaker_id failed amp:%d pin_state:%#x\n",
i, priv->gpio_priv->pin_state);
}
}
}
static void cirrus_scodec_test_no_spkid(struct kunit *test)
{
struct cirrus_scodec_test_priv *priv = test->priv;
struct device *dev = &priv->amp_dev->dev;
int ret;
ret = cirrus_scodec_get_speaker_id(dev, 0, 4, -1);
KUNIT_EXPECT_EQ(test, ret, -ENOENT);
}
static int cirrus_scodec_test_case_init(struct kunit *test)
{
struct cirrus_scodec_test_priv *priv;
priv = kunit_kzalloc(test, sizeof(*priv), GFP_KERNEL);
if (!priv)
return -ENOMEM;
test->priv = priv;
/* Create dummy GPIO */
cirrus_scodec_test_create_gpio(test);
/* Create dummy amp driver dev */
priv->amp_dev = faux_device_create("cirrus_scodec_test_amp_drv", NULL, NULL);
KUNIT_ASSERT_NOT_NULL(test, priv->amp_dev);
KUNIT_ASSERT_EQ(test, 0, kunit_add_action_or_reset(test,
faux_device_destroy_wrapper,
priv->amp_dev));
return 0;
}
static const struct cirrus_scodec_test_spkid_param cirrus_scodec_test_spkid_param_cases[] = {
{ .num_amps = 2, .gpios_per_amp = 1, .num_amps_sharing = 1 },
{ .num_amps = 2, .gpios_per_amp = 2, .num_amps_sharing = 1 },
{ .num_amps = 2, .gpios_per_amp = 3, .num_amps_sharing = 1 },
{ .num_amps = 2, .gpios_per_amp = 4, .num_amps_sharing = 1 },
{ .num_amps = 3, .gpios_per_amp = 1, .num_amps_sharing = 1 },
{ .num_amps = 3, .gpios_per_amp = 2, .num_amps_sharing = 1 },
{ .num_amps = 3, .gpios_per_amp = 3, .num_amps_sharing = 1 },
{ .num_amps = 3, .gpios_per_amp = 4, .num_amps_sharing = 1 },
{ .num_amps = 4, .gpios_per_amp = 1, .num_amps_sharing = 1 },
{ .num_amps = 4, .gpios_per_amp = 2, .num_amps_sharing = 1 },
{ .num_amps = 4, .gpios_per_amp = 3, .num_amps_sharing = 1 },
{ .num_amps = 4, .gpios_per_amp = 4, .num_amps_sharing = 1 },
/* Same GPIO shared by all amps */
{ .num_amps = 2, .gpios_per_amp = 1, .num_amps_sharing = 2 },
{ .num_amps = 2, .gpios_per_amp = 2, .num_amps_sharing = 2 },
{ .num_amps = 2, .gpios_per_amp = 3, .num_amps_sharing = 2 },
{ .num_amps = 2, .gpios_per_amp = 4, .num_amps_sharing = 2 },
{ .num_amps = 3, .gpios_per_amp = 1, .num_amps_sharing = 3 },
{ .num_amps = 3, .gpios_per_amp = 2, .num_amps_sharing = 3 },
{ .num_amps = 3, .gpios_per_amp = 3, .num_amps_sharing = 3 },
{ .num_amps = 3, .gpios_per_amp = 4, .num_amps_sharing = 3 },
{ .num_amps = 4, .gpios_per_amp = 1, .num_amps_sharing = 4 },
{ .num_amps = 4, .gpios_per_amp = 2, .num_amps_sharing = 4 },
{ .num_amps = 4, .gpios_per_amp = 3, .num_amps_sharing = 4 },
{ .num_amps = 4, .gpios_per_amp = 4, .num_amps_sharing = 4 },
/* Two sets of shared GPIOs */
{ .num_amps = 4, .gpios_per_amp = 1, .num_amps_sharing = 2 },
{ .num_amps = 4, .gpios_per_amp = 2, .num_amps_sharing = 2 },
{ .num_amps = 4, .gpios_per_amp = 3, .num_amps_sharing = 2 },
{ .num_amps = 4, .gpios_per_amp = 4, .num_amps_sharing = 2 },
};
static void cirrus_scodec_test_spkid_param_desc(const struct cirrus_scodec_test_spkid_param *param,
char *desc)
{
snprintf(desc, KUNIT_PARAM_DESC_SIZE, "amps:%d gpios_per_amp:%d num_amps_sharing:%d",
param->num_amps, param->gpios_per_amp, param->num_amps_sharing);
}
KUNIT_ARRAY_PARAM(cirrus_scodec_test_spkid, cirrus_scodec_test_spkid_param_cases,
cirrus_scodec_test_spkid_param_desc);
static struct kunit_case cirrus_scodec_test_cases[] = {
KUNIT_CASE_PARAM(cirrus_scodec_test_spkid_parse, cirrus_scodec_test_spkid_gen_params),
KUNIT_CASE(cirrus_scodec_test_no_spkid),
{ } /* terminator */
};
static struct kunit_suite cirrus_scodec_test_suite = {
.name = "snd-hda-cirrus-scodec-test",
.init = cirrus_scodec_test_case_init,
.test_cases = cirrus_scodec_test_cases,
};
kunit_test_suite(cirrus_scodec_test_suite);
MODULE_IMPORT_NS("SND_HDA_CIRRUS_SCODEC");
MODULE_DESCRIPTION("KUnit test for the Cirrus side-codec library");
MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
MODULE_LICENSE("GPL");
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,115 @@
/* SPDX-License-Identifier: GPL-2.0
*
* CS35L41 ALSA HDA audio driver
*
* Copyright 2021 Cirrus Logic, Inc.
*
* Author: Lucas Tanure <tanureal@opensource.cirrus.com>
*/
#ifndef __CS35L41_HDA_H__
#define __CS35L41_HDA_H__
#include <linux/acpi.h>
#include <linux/efi.h>
#include <linux/regulator/consumer.h>
#include <linux/gpio/consumer.h>
#include <linux/device.h>
#include <sound/cs35l41.h>
#include <sound/cs-amp-lib.h>
#include <linux/firmware/cirrus/cs_dsp.h>
#include <linux/firmware/cirrus/wmfw.h>
#define CS35L41_MAX_ACCEPTABLE_SPI_SPEED_HZ 1000000
#define DEFAULT_AMP_GAIN_PCM 17 /* 17.5dB Gain */
#define DEFAULT_AMP_GAIN_PDM 19 /* 19.5dB Gain */
struct cs35l41_amp_cal_data {
u32 calTarget[2];
u32 calTime[2];
s8 calAmbient;
u8 calStatus;
u16 calR;
} __packed;
struct cs35l41_amp_efi_data {
u32 size;
u32 count;
struct cs35l41_amp_cal_data data[];
} __packed;
enum cs35l41_hda_spk_pos {
CS35L41_LEFT,
CS35L41_RIGHT,
CS35L41_CENTER,
};
enum cs35l41_hda_gpio_function {
CS35L41_NOT_USED,
CS35l41_VSPK_SWITCH,
CS35L41_INTERRUPT,
CS35l41_SYNC,
};
enum control_bus {
I2C,
SPI
};
struct snd_kcontrol;
struct cs35l41_hda {
struct device *dev;
struct regmap *regmap;
struct gpio_desc *reset_gpio;
struct gpio_desc *cs_gpio;
struct cs35l41_hw_cfg hw_cfg;
struct hda_codec *codec;
int irq;
int index;
int channel_index;
unsigned volatile long irq_errors;
const char *amp_name;
const char *acpi_subsystem_id;
int firmware_type;
int speaker_id;
struct mutex fw_mutex;
struct work_struct fw_load_work;
struct snd_kcontrol *fw_type_ctl;
struct snd_kcontrol *fw_load_ctl;
struct snd_kcontrol *mute_override_ctl;
struct regmap_irq_chip_data *irq_data;
bool firmware_running;
bool request_fw_load;
bool fw_request_ongoing;
bool halo_initialized;
bool playback_started;
struct cs_dsp cs_dsp;
struct acpi_device *dacpi;
bool mute_override;
enum control_bus control_bus;
bool bypass_fw;
unsigned int tuning_gain;
struct cirrus_amp_cal_data cal_data;
bool cal_data_valid;
};
enum halo_state {
HALO_STATE_CODE_INIT_DOWNLOAD = 0,
HALO_STATE_CODE_START,
HALO_STATE_CODE_RUN
};
extern const struct dev_pm_ops cs35l41_hda_pm_ops;
int cs35l41_hda_probe(struct device *dev, const char *device_name, int id, int irq,
struct regmap *regmap, enum control_bus control_bus);
void cs35l41_hda_remove(struct device *dev);
int cs35l41_get_speaker_id(struct device *dev, int amp_index, int num_amps, int fixed_gpio_id);
int cs35l41_hda_parse_acpi(struct cs35l41_hda *cs35l41, struct device *physdev, int id);
#endif /*__CS35L41_HDA_H__*/
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: GPL-2.0
//
// CS35l41 HDA I2C driver
//
// Copyright 2021 Cirrus Logic, Inc.
//
// Author: Lucas Tanure <tanureal@opensource.cirrus.com>
#include <linux/module.h>
#include <linux/i2c.h>
#include "cs35l41_hda.h"
static int cs35l41_hda_i2c_probe(struct i2c_client *clt)
{
const char *device_name;
/*
* Compare against the device name so it works for SPI, normal ACPI
* and for ACPI by serial-multi-instantiate matching cases.
*/
if (strstr(dev_name(&clt->dev), "CLSA0100"))
device_name = "CLSA0100";
else if (strstr(dev_name(&clt->dev), "CLSA0101"))
device_name = "CLSA0101";
else if (strstr(dev_name(&clt->dev), "CSC3551"))
device_name = "CSC3551";
else
return -ENODEV;
return cs35l41_hda_probe(&clt->dev, device_name, clt->addr, clt->irq,
devm_regmap_init_i2c(clt, &cs35l41_regmap_i2c), I2C);
}
static void cs35l41_hda_i2c_remove(struct i2c_client *clt)
{
cs35l41_hda_remove(&clt->dev);
}
static const struct i2c_device_id cs35l41_hda_i2c_id[] = {
{ .name = "cs35l41-hda" },
{ }
};
static const struct acpi_device_id cs35l41_acpi_hda_match[] = {
{"CLSA0100", 0 },
{"CLSA0101", 0 },
{"CSC3551", 0 },
{}
};
MODULE_DEVICE_TABLE(acpi, cs35l41_acpi_hda_match);
static struct i2c_driver cs35l41_i2c_driver = {
.driver = {
.name = "cs35l41-hda",
.acpi_match_table = cs35l41_acpi_hda_match,
.pm = &cs35l41_hda_pm_ops,
},
.id_table = cs35l41_hda_i2c_id,
.probe = cs35l41_hda_i2c_probe,
.remove = cs35l41_hda_i2c_remove,
};
module_i2c_driver(cs35l41_i2c_driver);
MODULE_DESCRIPTION("HDA CS35L41 driver");
MODULE_IMPORT_NS("SND_HDA_SCODEC_CS35L41");
MODULE_AUTHOR("Lucas Tanure <tanureal@opensource.cirrus.com>");
MODULE_LICENSE("GPL");
@@ -0,0 +1,590 @@
// SPDX-License-Identifier: GPL-2.0
//
// CS35L41 ALSA HDA Property driver
//
// Copyright 2023 Cirrus Logic, Inc.
//
// Author: Stefan Binding <sbinding@opensource.cirrus.com>
#include <linux/acpi.h>
#include <linux/gpio/consumer.h>
#include <linux/string.h>
#include "cs35l41_hda_property.h"
#include <linux/spi/spi.h>
#define MAX_AMPS 4
struct cs35l41_config {
const char *ssid;
int num_amps;
enum {
INTERNAL,
EXTERNAL
} boost_type;
u8 channel[MAX_AMPS];
int reset_gpio_index; /* -1 if no reset gpio */
int spkid_gpio_index; /* -1 if no spkid gpio */
int cs_gpio_index; /* -1 if no cs gpio, or cs-gpios already exists, max num amps == 2 */
int boost_ind_nanohenry; /* Required if boost_type == Internal */
int boost_peak_milliamp; /* Required if boost_type == Internal */
int boost_cap_microfarad; /* Required if boost_type == Internal */
};
static const struct cs35l41_config cs35l41_config_table[] = {
{ "10251826", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, -1, -1, 0, 0, 0 },
{ "1025182C", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, -1, -1, 0, 0, 0 },
{ "10251844", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, -1, -1, 0, 0, 0 },
{ "10280B27", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "10280B28", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "10280BEB", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, -1, 0, 0, 0, 0 },
{ "10280C4D", 4, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, CS35L41_LEFT, CS35L41_RIGHT }, 0, 1, -1, 1000, 4500, 24 },
/*
* Device 103C89C6 does have _DSD, however it is setup to use the wrong boost type.
* We can override the _DSD to correct the boost type here.
* Since this laptop has valid ACPI, we do not need to handle cs-gpios, since that already exists
* in the ACPI. The Reset GPIO is also valid, so we can use the Reset defined in _DSD.
*/
{ "103C89C6", 2, INTERNAL, { CS35L41_RIGHT, CS35L41_LEFT, 0, 0 }, -1, -1, -1, 1000, 4500, 24 },
{ "103C8A28", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8A29", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8A2A", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8A2B", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8A2C", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8A2D", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8A2E", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8A30", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8A31", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8A6E", 4, EXTERNAL, { CS35L41_LEFT, CS35L41_LEFT, CS35L41_RIGHT, CS35L41_RIGHT }, 0, -1, -1, 0, 0, 0 },
/*
* Device 103C8B63 has _DSD with valid reset-gpios and cs-gpios, however the
* boost type is incorrectly set to Internal. Override to External Boost.
*/
{ "103C8B63", 4, EXTERNAL, { CS35L41_RIGHT, CS35L41_LEFT, CS35L41_RIGHT, CS35L41_LEFT }, -1, -1, -1, 0, 0, 0 },
{ "103C8BB3", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8BB4", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8BDD", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8BDE", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8BDF", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8BE0", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8BE1", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8BE2", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8BE3", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8BE5", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8BE6", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8BE7", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8BE8", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8BE9", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8B3A", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8C15", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4000, 24 },
{ "103C8C16", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4000, 24 },
{ "103C8C17", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4000, 24 },
{ "103C8C4D", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8C4E", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8C4F", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8C50", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8C51", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8CDD", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4100, 24 },
{ "103C8CDE", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 3900, 24 },
{ "104312AF", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "10431433", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
{ "10431463", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
{ "10431473", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, -1, 0, 1000, 4500, 24 },
{ "10431483", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, -1, 0, 1000, 4500, 24 },
{ "10431493", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "104314D3", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "104314E3", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
{ "10431503", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
{ "10431533", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
{ "10431573", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "10431663", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, -1, 0, 1000, 4500, 24 },
{ "10431683", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
{ "104316A3", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
{ "104316D3", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
{ "104316F3", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
{ "104317F3", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
{ "10431863", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "104318D3", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
{ "10431A83", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
{ "10431B93", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "10431C9F", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "10431CAF", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "10431CCF", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "10431CDF", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "10431CEF", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "10431D1F", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
{ "10431DA2", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
{ "10431E02", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
{ "10431E12", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
{ "10431EE2", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, -1, -1, 0, 0, 0 },
{ "10431F12", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 1000, 4500, 24 },
{ "10431F1F", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, -1, 0, 0, 0, 0 },
{ "10431F62", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 0, 0, 0 },
{ "10433A20", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "10433A30", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "10433A40", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "10433A50", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "10433A60", 2, INTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 1, 2, 0, 1000, 4500, 24 },
{ "17AA3865", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, -1, -1, 0, 0, 0 },
{ "17AA3866", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, -1, -1, 0, 0, 0 },
{ "17AA386E", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 2, -1, 0, 0, 0 },
{ "17AA386F", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, -1, -1, 0, 0, 0 },
{ "17AA3874", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, -1, -1, 0, 0, 0 },
{ "17AA3877", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, -1, -1, 0, 0, 0 },
{ "17AA3878", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, -1, -1, 0, 0, 0 },
{ "17AA38A9", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 2, -1, 0, 0, 0 },
{ "17AA38AB", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 2, -1, 0, 0, 0 },
{ "17AA38B4", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
{ "17AA38B5", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
{ "17AA38B6", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
{ "17AA38B7", 2, EXTERNAL, { CS35L41_LEFT, CS35L41_RIGHT, 0, 0 }, 0, 1, -1, 0, 0, 0 },
{ "17AA38C7", 4, INTERNAL, { CS35L41_RIGHT, CS35L41_LEFT, CS35L41_RIGHT, CS35L41_LEFT }, 0, 2, -1, 1000, 4500, 24 },
{ "17AA38C8", 4, INTERNAL, { CS35L41_RIGHT, CS35L41_LEFT, CS35L41_RIGHT, CS35L41_LEFT }, 0, 2, -1, 1000, 4500, 24 },
{ "17AA38F9", 2, EXTERNAL, { CS35L41_RIGHT, CS35L41_LEFT, 0, 0 }, 0, 2, -1, 0, 0, 0 },
{ "17AA38FA", 2, EXTERNAL, { CS35L41_RIGHT, CS35L41_LEFT, 0, 0 }, 0, 2, -1, 0, 0, 0 },
{ "17AA3929", 4, INTERNAL, { CS35L41_RIGHT, CS35L41_LEFT, CS35L41_RIGHT, CS35L41_LEFT }, 0, 2, -1, 1000, 4500, 24 },
{ "17AA392B", 4, INTERNAL, { CS35L41_RIGHT, CS35L41_LEFT, CS35L41_RIGHT, CS35L41_LEFT }, 0, 2, -1, 1000, 4500, 24 },
{}
};
static int cs35l41_add_gpios(struct cs35l41_hda *cs35l41, struct device *physdev, int reset_gpio,
int spkid_gpio, int cs_gpio_index, int num_amps)
{
struct acpi_gpio_mapping *gpio_mapping = NULL;
struct acpi_gpio_params *reset_gpio_params = NULL;
struct acpi_gpio_params *spkid_gpio_params = NULL;
struct acpi_gpio_params *cs_gpio_params = NULL;
unsigned int num_entries = 0;
unsigned int reset_index, spkid_index, csgpio_index;
int i;
/*
* GPIO Mapping only needs to be done once, since it would be available for subsequent amps
*/
if (cs35l41->dacpi->driver_gpios)
return 0;
if (reset_gpio >= 0) {
reset_index = num_entries;
num_entries++;
}
if (spkid_gpio >= 0) {
spkid_index = num_entries;
num_entries++;
}
if ((cs_gpio_index >= 0) && (num_amps == 2)) {
csgpio_index = num_entries;
num_entries++;
}
if (!num_entries)
return 0;
/* must include termination entry */
num_entries++;
gpio_mapping = devm_kcalloc(physdev, num_entries, sizeof(struct acpi_gpio_mapping),
GFP_KERNEL);
if (!gpio_mapping)
goto err;
if (reset_gpio >= 0) {
gpio_mapping[reset_index].name = "reset-gpios";
reset_gpio_params = devm_kcalloc(physdev, num_amps, sizeof(struct acpi_gpio_params),
GFP_KERNEL);
if (!reset_gpio_params)
goto err;
for (i = 0; i < num_amps; i++)
reset_gpio_params[i].crs_entry_index = reset_gpio;
gpio_mapping[reset_index].data = reset_gpio_params;
gpio_mapping[reset_index].size = num_amps;
}
if (spkid_gpio >= 0) {
gpio_mapping[spkid_index].name = "spk-id-gpios";
spkid_gpio_params = devm_kcalloc(physdev, num_amps, sizeof(struct acpi_gpio_params),
GFP_KERNEL);
if (!spkid_gpio_params)
goto err;
for (i = 0; i < num_amps; i++)
spkid_gpio_params[i].crs_entry_index = spkid_gpio;
gpio_mapping[spkid_index].data = spkid_gpio_params;
gpio_mapping[spkid_index].size = num_amps;
}
if ((cs_gpio_index >= 0) && (num_amps == 2)) {
gpio_mapping[csgpio_index].name = "cs-gpios";
/* only one GPIO CS is supported without using _DSD, obtained using index 0 */
cs_gpio_params = devm_kzalloc(physdev, sizeof(struct acpi_gpio_params), GFP_KERNEL);
if (!cs_gpio_params)
goto err;
cs_gpio_params->crs_entry_index = cs_gpio_index;
gpio_mapping[csgpio_index].data = cs_gpio_params;
gpio_mapping[csgpio_index].size = 1;
}
return devm_acpi_dev_add_driver_gpios(physdev, gpio_mapping);
err:
devm_kfree(physdev, gpio_mapping);
devm_kfree(physdev, reset_gpio_params);
devm_kfree(physdev, spkid_gpio_params);
devm_kfree(physdev, cs_gpio_params);
return -ENOMEM;
}
static int generic_dsd_config(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
const char *hid)
{
struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
const struct cs35l41_config *cfg;
struct gpio_desc *cs_gpiod;
struct spi_device *spi;
bool dsd_found;
int ret;
int i;
for (cfg = cs35l41_config_table; cfg->ssid; cfg++) {
if (!strcasecmp(cfg->ssid, cs35l41->acpi_subsystem_id))
break;
}
if (!cfg->ssid)
return -ENOENT;
if (!cs35l41->dacpi || cs35l41->dacpi != ACPI_COMPANION(physdev)) {
dev_err(cs35l41->dev, "ACPI Device does not match, cannot override _DSD.\n");
return -ENODEV;
}
dev_info(cs35l41->dev, "Adding DSD properties for %s\n", cs35l41->acpi_subsystem_id);
dsd_found = acpi_dev_has_props(cs35l41->dacpi);
if (!dsd_found) {
ret = cs35l41_add_gpios(cs35l41, physdev, cfg->reset_gpio_index,
cfg->spkid_gpio_index, cfg->cs_gpio_index,
cfg->num_amps);
if (ret) {
dev_err(cs35l41->dev, "Error adding GPIO mapping: %d\n", ret);
return ret;
}
} else if (cfg->reset_gpio_index >= 0 || cfg->spkid_gpio_index >= 0) {
dev_warn(cs35l41->dev, "Cannot add Reset/Speaker ID/SPI CS GPIO Mapping, "
"_DSD already exists.\n");
}
if (cs35l41->control_bus == SPI) {
cs35l41->index = id;
/*
* Manually set the Chip Select for the second amp <cs_gpio_index> 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) && cfg->cs_gpio_index >= 0) {
spi = to_spi_device(cs35l41->dev);
if (cfg->num_amps != 2) {
dev_warn(cs35l41->dev,
"Cannot update SPI CS, Number of Amps (%d) != 2\n",
cfg->num_amps);
} else if (dsd_found) {
dev_warn(cs35l41->dev,
"Cannot update SPI CS, _DSD already exists.\n");
} else {
/*
* This is obtained using driver_gpios, since only one GPIO for CS
* exists, this can be obtained using index 0.
*/
cs_gpiod = gpiod_get_index(physdev, "cs", 0, GPIOD_OUT_LOW);
if (IS_ERR(cs_gpiod)) {
dev_err(cs35l41->dev,
"Unable to get Chip Select GPIO descriptor\n");
return PTR_ERR(cs_gpiod);
}
if (id == 1) {
spi_set_csgpiod(spi, 0, cs_gpiod);
cs35l41->cs_gpio = cs_gpiod;
} else {
gpiod_set_value_cansleep(cs_gpiod, true);
gpiod_put(cs_gpiod);
}
spi_setup(spi);
}
}
} else {
if (cfg->num_amps > 2)
/*
* i2c addresses for 3/4 amps are used in order: 0x40, 0x41, 0x42, 0x43,
* subtracting 0x40 would give zero-based index
*/
cs35l41->index = id - 0x40;
else
/* i2c addr 0x40 for first amp (always), 0x41/0x42 for 2nd amp */
cs35l41->index = id == 0x40 ? 0 : 1;
}
cs35l41->reset_gpio = fwnode_gpiod_get_index(acpi_fwnode_handle(cs35l41->dacpi), "reset",
cs35l41->index, GPIOD_OUT_LOW,
"cs35l41-reset");
cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, cs35l41->index, cfg->num_amps, -1);
hw_cfg->spk_pos = cfg->channel[cs35l41->index];
cs35l41->channel_index = 0;
for (i = 0; i < cs35l41->index; i++)
if (cfg->channel[i] == hw_cfg->spk_pos)
cs35l41->channel_index++;
if (cfg->boost_type == INTERNAL) {
hw_cfg->bst_type = CS35L41_INT_BOOST;
hw_cfg->bst_ind = cfg->boost_ind_nanohenry;
hw_cfg->bst_ipk = cfg->boost_peak_milliamp;
hw_cfg->bst_cap = cfg->boost_cap_microfarad;
hw_cfg->gpio1.func = CS35L41_NOT_USED;
hw_cfg->gpio1.valid = true;
} else {
hw_cfg->bst_type = CS35L41_EXT_BOOST;
hw_cfg->bst_ind = -1;
hw_cfg->bst_ipk = -1;
hw_cfg->bst_cap = -1;
hw_cfg->gpio1.func = CS35l41_VSPK_SWITCH;
hw_cfg->gpio1.valid = true;
}
hw_cfg->gpio2.func = CS35L41_INTERRUPT;
hw_cfg->gpio2.valid = true;
hw_cfg->valid = true;
return 0;
}
/*
* Systems 103C8C66, 103C8C67, 103C8C68, 103C8C6A use a dual speaker id system - each speaker has
* its own speaker id.
*/
static int hp_i2c_int_2amp_dual_spkid(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
const char *hid)
{
struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
/* If _DSD exists for this laptop, we cannot support it through here */
if (acpi_dev_has_props(cs35l41->dacpi))
return -ENOENT;
/* check I2C address to assign the index */
cs35l41->index = id == 0x40 ? 0 : 1;
cs35l41->channel_index = 0;
cs35l41->reset_gpio = gpiod_get_index(physdev, NULL, 0, GPIOD_OUT_HIGH);
if (cs35l41->index == 0)
cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, 0, 0, 1);
else
cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, 0, 0, 2);
hw_cfg->spk_pos = cs35l41->index;
hw_cfg->gpio2.func = CS35L41_INTERRUPT;
hw_cfg->gpio2.valid = true;
hw_cfg->valid = true;
hw_cfg->bst_type = CS35L41_INT_BOOST;
hw_cfg->bst_ind = 1000;
hw_cfg->bst_ipk = 4100;
hw_cfg->bst_cap = 24;
hw_cfg->gpio1.func = CS35L41_NOT_USED;
hw_cfg->gpio1.valid = true;
return 0;
}
/*
* Device CLSA010(0/1) doesn't have _DSD so a gpiod_get by the label reset won't work.
* And devices created by serial-multi-instantiate don't have their device struct
* pointing to the correct fwnode, so acpi_dev must be used here.
* And devm functions expect that the device requesting the resource has the correct
* fwnode.
*/
static int lenovo_legion_no_acpi(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
const char *hid)
{
struct cs35l41_hw_cfg *hw_cfg = &cs35l41->hw_cfg;
/* check I2C address to assign the index */
cs35l41->index = id == 0x40 ? 0 : 1;
cs35l41->channel_index = 0;
cs35l41->reset_gpio = gpiod_get_index(physdev, NULL, 0, GPIOD_OUT_HIGH);
cs35l41->speaker_id = cs35l41_get_speaker_id(physdev, 0, 0, 2);
hw_cfg->spk_pos = cs35l41->index;
hw_cfg->gpio2.func = CS35L41_INTERRUPT;
hw_cfg->gpio2.valid = true;
hw_cfg->valid = true;
if (strcmp(hid, "CLSA0100") == 0) {
hw_cfg->bst_type = CS35L41_EXT_BOOST_NO_VSPK_SWITCH;
} else if (strcmp(hid, "CLSA0101") == 0) {
hw_cfg->bst_type = CS35L41_EXT_BOOST;
hw_cfg->gpio1.func = CS35l41_VSPK_SWITCH;
hw_cfg->gpio1.valid = true;
}
return 0;
}
static int missing_speaker_id_gpio2(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
const char *hid)
{
int ret;
ret = cs35l41_add_gpios(cs35l41, physdev, -1, 2, -1, 2);
if (ret) {
dev_err(cs35l41->dev, "Error adding GPIO mapping: %d\n", ret);
return ret;
}
return cs35l41_hda_parse_acpi(cs35l41, physdev, id);
}
struct cs35l41_prop_model {
const char *hid;
const char *ssid;
int (*add_prop)(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
const char *hid);
};
static const struct cs35l41_prop_model cs35l41_prop_model_table[] = {
{ "CLSA0100", NULL, lenovo_legion_no_acpi },
{ "CLSA0101", NULL, lenovo_legion_no_acpi },
{ "CSC3551", "10251826", generic_dsd_config },
{ "CSC3551", "1025182C", generic_dsd_config },
{ "CSC3551", "10251844", generic_dsd_config },
{ "CSC3551", "10280B27", generic_dsd_config },
{ "CSC3551", "10280B28", generic_dsd_config },
{ "CSC3551", "10280BEB", generic_dsd_config },
{ "CSC3551", "10280C4D", generic_dsd_config },
{ "CSC3551", "103C89C6", generic_dsd_config },
{ "CSC3551", "103C8A28", generic_dsd_config },
{ "CSC3551", "103C8A29", generic_dsd_config },
{ "CSC3551", "103C8A2A", generic_dsd_config },
{ "CSC3551", "103C8A2B", generic_dsd_config },
{ "CSC3551", "103C8A2C", generic_dsd_config },
{ "CSC3551", "103C8A2D", generic_dsd_config },
{ "CSC3551", "103C8A2E", generic_dsd_config },
{ "CSC3551", "103C8A30", generic_dsd_config },
{ "CSC3551", "103C8A31", generic_dsd_config },
{ "CSC3551", "103C8A6E", generic_dsd_config },
{ "CSC3551", "103C8B63", generic_dsd_config },
{ "CSC3551", "103C8BB3", generic_dsd_config },
{ "CSC3551", "103C8BB4", generic_dsd_config },
{ "CSC3551", "103C8BDD", generic_dsd_config },
{ "CSC3551", "103C8BDE", generic_dsd_config },
{ "CSC3551", "103C8BDF", generic_dsd_config },
{ "CSC3551", "103C8BE0", generic_dsd_config },
{ "CSC3551", "103C8BE1", generic_dsd_config },
{ "CSC3551", "103C8BE2", generic_dsd_config },
{ "CSC3551", "103C8BE3", generic_dsd_config },
{ "CSC3551", "103C8BE5", generic_dsd_config },
{ "CSC3551", "103C8BE6", generic_dsd_config },
{ "CSC3551", "103C8BE7", generic_dsd_config },
{ "CSC3551", "103C8BE8", generic_dsd_config },
{ "CSC3551", "103C8BE9", generic_dsd_config },
{ "CSC3551", "103C8B3A", generic_dsd_config },
{ "CSC3551", "103C8C15", generic_dsd_config },
{ "CSC3551", "103C8C16", generic_dsd_config },
{ "CSC3551", "103C8C17", generic_dsd_config },
{ "CSC3551", "103C8C4D", generic_dsd_config },
{ "CSC3551", "103C8C4E", generic_dsd_config },
{ "CSC3551", "103C8C4F", generic_dsd_config },
{ "CSC3551", "103C8C50", generic_dsd_config },
{ "CSC3551", "103C8C51", generic_dsd_config },
{ "CSC3551", "103C8C66", hp_i2c_int_2amp_dual_spkid },
{ "CSC3551", "103C8C67", hp_i2c_int_2amp_dual_spkid },
{ "CSC3551", "103C8C68", hp_i2c_int_2amp_dual_spkid },
{ "CSC3551", "103C8C6A", hp_i2c_int_2amp_dual_spkid },
{ "CSC3551", "103C8CDD", generic_dsd_config },
{ "CSC3551", "103C8CDE", generic_dsd_config },
{ "CSC3551", "104312AF", generic_dsd_config },
{ "CSC3551", "10431433", generic_dsd_config },
{ "CSC3551", "10431463", generic_dsd_config },
{ "CSC3551", "10431473", generic_dsd_config },
{ "CSC3551", "10431483", generic_dsd_config },
{ "CSC3551", "10431493", generic_dsd_config },
{ "CSC3551", "104314D3", generic_dsd_config },
{ "CSC3551", "104314E3", generic_dsd_config },
{ "CSC3551", "10431503", generic_dsd_config },
{ "CSC3551", "10431533", generic_dsd_config },
{ "CSC3551", "10431573", generic_dsd_config },
{ "CSC3551", "10431663", generic_dsd_config },
{ "CSC3551", "10431683", generic_dsd_config },
{ "CSC3551", "104316A3", generic_dsd_config },
{ "CSC3551", "104316D3", generic_dsd_config },
{ "CSC3551", "104316F3", generic_dsd_config },
{ "CSC3551", "104317F3", generic_dsd_config },
{ "CSC3551", "10431863", generic_dsd_config },
{ "CSC3551", "104318D3", generic_dsd_config },
{ "CSC3551", "10431A63", missing_speaker_id_gpio2 },
{ "CSC3551", "10431A83", generic_dsd_config },
{ "CSC3551", "10431B93", generic_dsd_config },
{ "CSC3551", "10431C9F", generic_dsd_config },
{ "CSC3551", "10431CAF", generic_dsd_config },
{ "CSC3551", "10431CCF", generic_dsd_config },
{ "CSC3551", "10431CDF", generic_dsd_config },
{ "CSC3551", "10431CEF", generic_dsd_config },
{ "CSC3551", "10431D1F", generic_dsd_config },
{ "CSC3551", "10431DA2", generic_dsd_config },
{ "CSC3551", "10431E02", generic_dsd_config },
{ "CSC3551", "10431E12", generic_dsd_config },
{ "CSC3551", "10431EE2", generic_dsd_config },
{ "CSC3551", "10431F12", generic_dsd_config },
{ "CSC3551", "10431F1F", generic_dsd_config },
{ "CSC3551", "10431F62", generic_dsd_config },
{ "CSC3551", "10433A20", generic_dsd_config },
{ "CSC3551", "10433A30", generic_dsd_config },
{ "CSC3551", "10433A40", generic_dsd_config },
{ "CSC3551", "10433A50", generic_dsd_config },
{ "CSC3551", "10433A60", generic_dsd_config },
{ "CSC3551", "17AA3865", generic_dsd_config },
{ "CSC3551", "17AA3866", generic_dsd_config },
{ "CSC3551", "17AA386E", generic_dsd_config },
{ "CSC3551", "17AA386F", generic_dsd_config },
{ "CSC3551", "17AA3874", generic_dsd_config },
{ "CSC3551", "17AA3877", generic_dsd_config },
{ "CSC3551", "17AA3878", generic_dsd_config },
{ "CSC3551", "17AA38A9", generic_dsd_config },
{ "CSC3551", "17AA38AB", generic_dsd_config },
{ "CSC3551", "17AA38B4", generic_dsd_config },
{ "CSC3551", "17AA38B5", generic_dsd_config },
{ "CSC3551", "17AA38B6", generic_dsd_config },
{ "CSC3551", "17AA38B7", generic_dsd_config },
{ "CSC3551", "17AA38C7", generic_dsd_config },
{ "CSC3551", "17AA38C8", generic_dsd_config },
{ "CSC3551", "17AA38F9", generic_dsd_config },
{ "CSC3551", "17AA38FA", generic_dsd_config },
{ "CSC3551", "17AA3929", generic_dsd_config },
{ "CSC3551", "17AA392B", generic_dsd_config },
{}
};
int cs35l41_add_dsd_properties(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
const char *hid)
{
const struct cs35l41_prop_model *model;
for (model = cs35l41_prop_model_table; model->hid; model++) {
if (!strcmp(model->hid, hid) &&
(!model->ssid ||
(cs35l41->acpi_subsystem_id &&
!strcasecmp(model->ssid, cs35l41->acpi_subsystem_id))))
return model->add_prop(cs35l41, physdev, id, hid);
}
return -ENOENT;
}
@@ -0,0 +1,18 @@
/* SPDX-License-Identifier: GPL-2.0
*
* CS35L41 ALSA HDA Property driver
*
* Copyright 2023 Cirrus Logic, Inc.
*
* Author: Stefan Binding <sbinding@opensource.cirrus.com>
*/
#ifndef CS35L41_HDA_PROP_H
#define CS35L41_HDA_PROP_H
#include <linux/device.h>
#include "cs35l41_hda.h"
int cs35l41_add_dsd_properties(struct cs35l41_hda *cs35l41, struct device *physdev, int id,
const char *hid);
#endif /* CS35L41_HDA_PROP_H */
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: GPL-2.0
//
// CS35l41 HDA SPI driver
//
// Copyright 2021 Cirrus Logic, Inc.
//
// Author: Lucas Tanure <tanureal@opensource.cirrus.com>
#include <linux/module.h>
#include <linux/spi/spi.h>
#include "cs35l41_hda.h"
static int cs35l41_hda_spi_probe(struct spi_device *spi)
{
const char *device_name;
/*
* Compare against the device name so it works for SPI, normal ACPI
* and for ACPI by serial-multi-instantiate matching cases.
*/
if (strstr(dev_name(&spi->dev), "CSC3551"))
device_name = "CSC3551";
else
return -ENODEV;
return cs35l41_hda_probe(&spi->dev, device_name, spi_get_chipselect(spi, 0), spi->irq,
devm_regmap_init_spi(spi, &cs35l41_regmap_spi), SPI);
}
static void cs35l41_hda_spi_remove(struct spi_device *spi)
{
cs35l41_hda_remove(&spi->dev);
}
static const struct spi_device_id cs35l41_hda_spi_id[] = {
{ "cs35l41-hda", 0 },
{}
};
MODULE_DEVICE_TABLE(spi, cs35l41_hda_spi_id);
static const struct acpi_device_id cs35l41_acpi_hda_match[] = {
{ "CSC3551", 0 },
{}
};
MODULE_DEVICE_TABLE(acpi, cs35l41_acpi_hda_match);
static struct spi_driver cs35l41_spi_driver = {
.driver = {
.name = "cs35l41-hda",
.acpi_match_table = cs35l41_acpi_hda_match,
.pm = &cs35l41_hda_pm_ops,
},
.id_table = cs35l41_hda_spi_id,
.probe = cs35l41_hda_spi_probe,
.remove = cs35l41_hda_spi_remove,
};
module_spi_driver(cs35l41_spi_driver);
MODULE_DESCRIPTION("HDA CS35L41 driver");
MODULE_IMPORT_NS("SND_HDA_SCODEC_CS35L41");
MODULE_AUTHOR("Lucas Tanure <tanureal@opensource.cirrus.com>");
MODULE_LICENSE("GPL");
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,57 @@
/* SPDX-License-Identifier: GPL-2.0-only
*
* HDA audio driver for Cirrus Logic CS35L56 smart amp
*
* Copyright (C) 2023 Cirrus Logic, Inc. and
* Cirrus Logic International Semiconductor Ltd.
*/
#ifndef __CS35L56_HDA_H__
#define __CS35L56_HDA_H__
#include <linux/container_of.h>
#include <linux/device.h>
#include <linux/gpio/consumer.h>
#include <linux/firmware/cirrus/cs_dsp.h>
#include <linux/firmware/cirrus/wmfw.h>
#include <linux/regulator/consumer.h>
#include <linux/workqueue.h>
#include <sound/cs35l56.h>
struct dentry;
struct cs35l56_hda {
struct cs35l56_base base;
struct hda_codec *codec;
struct work_struct dsp_work;
int index;
int num_amps;
const char *system_name;
const char *amp_name;
struct cs_dsp cs_dsp;
bool playing;
bool suspended;
u8 asp_tx_mask;
struct snd_kcontrol *posture_ctl;
struct snd_kcontrol *volume_ctl;
struct snd_kcontrol *mixer_ctl[4];
#if IS_ENABLED(CONFIG_SND_DEBUG)
struct dentry *debugfs_root;
#endif
};
static inline struct cs35l56_hda *cs35l56_hda_from_base(struct cs35l56_base *cs35l56_base)
{
return container_of(cs35l56_base, struct cs35l56_hda, base);
}
extern const struct dev_pm_ops cs35l56_hda_pm_ops;
int cs35l56_hda_common_probe(struct cs35l56_hda *cs35l56, int hid, int id);
void cs35l56_hda_remove(struct device *dev);
#endif /*__CS35L56_HDA_H__*/
@@ -0,0 +1,85 @@
// SPDX-License-Identifier: GPL-2.0-only
//
// CS35L56 HDA audio driver I2C binding
//
// Copyright (C) 2023 Cirrus Logic, Inc. and
// Cirrus Logic International Semiconductor Ltd.
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include "cs35l56_hda.h"
static int cs35l56_hda_i2c_probe(struct i2c_client *clt)
{
const struct i2c_device_id *id = i2c_client_get_device_id(clt);
struct cs35l56_hda *cs35l56;
int ret;
cs35l56 = devm_kzalloc(&clt->dev, sizeof(*cs35l56), GFP_KERNEL);
if (!cs35l56)
return -ENOMEM;
cs35l56->base.dev = &clt->dev;
#ifdef CS35L56_WAKE_HOLD_TIME_US
cs35l56->base.can_hibernate = true;
#endif
cs35l56->base.regmap = devm_regmap_init_i2c(clt, &cs35l56_regmap_i2c);
if (IS_ERR(cs35l56->base.regmap)) {
ret = PTR_ERR(cs35l56->base.regmap);
dev_err(cs35l56->base.dev, "Failed to allocate register map: %d\n",
ret);
return ret;
}
ret = cs35l56_hda_common_probe(cs35l56, id->driver_data, clt->addr);
if (ret)
return ret;
ret = cs35l56_irq_request(&cs35l56->base, clt->irq);
if (ret < 0)
cs35l56_hda_remove(cs35l56->base.dev);
return ret;
}
static void cs35l56_hda_i2c_remove(struct i2c_client *clt)
{
cs35l56_hda_remove(&clt->dev);
}
static const struct i2c_device_id cs35l56_hda_i2c_id[] = {
{ .name = "cs35l54-hda", .driver_data = 0x3554 },
{ .name = "cs35l56-hda", .driver_data = 0x3556 },
{ .name = "cs35l57-hda", .driver_data = 0x3557 },
{ }
};
static const struct acpi_device_id cs35l56_acpi_hda_match[] = {
{ "CSC3554", 0 },
{ "CSC3556", 0 },
{ "CSC3557", 0 },
{}
};
MODULE_DEVICE_TABLE(acpi, cs35l56_acpi_hda_match);
static struct i2c_driver cs35l56_hda_i2c_driver = {
.driver = {
.name = "cs35l56-hda",
.acpi_match_table = cs35l56_acpi_hda_match,
.pm = &cs35l56_hda_pm_ops,
},
.id_table = cs35l56_hda_i2c_id,
.probe = cs35l56_hda_i2c_probe,
.remove = cs35l56_hda_i2c_remove,
};
module_i2c_driver(cs35l56_hda_i2c_driver);
MODULE_DESCRIPTION("HDA CS35L56 I2C driver");
MODULE_IMPORT_NS("SND_HDA_SCODEC_CS35L56");
MODULE_IMPORT_NS("SND_SOC_CS35L56_SHARED");
MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>");
MODULE_LICENSE("GPL");
@@ -0,0 +1,88 @@
// SPDX-License-Identifier: GPL-2.0-only
//
// CS35L56 HDA audio driver SPI binding
//
// Copyright (C) 2023 Cirrus Logic, Inc. and
// Cirrus Logic International Semiconductor Ltd.
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/spi/spi.h>
#include "cs35l56_hda.h"
static int cs35l56_hda_spi_probe(struct spi_device *spi)
{
const struct spi_device_id *id = spi_get_device_id(spi);
struct cs35l56_hda *cs35l56;
int ret;
cs35l56 = devm_kzalloc(&spi->dev, sizeof(*cs35l56), GFP_KERNEL);
if (!cs35l56)
return -ENOMEM;
cs35l56->base.dev = &spi->dev;
ret = cs35l56_init_config_for_spi(&cs35l56->base, spi);
if (ret)
return ret;
#ifdef CS35L56_WAKE_HOLD_TIME_US
cs35l56->base.can_hibernate = true;
#endif
cs35l56->base.regmap = devm_regmap_init_spi(spi, &cs35l56_regmap_spi);
if (IS_ERR(cs35l56->base.regmap)) {
ret = PTR_ERR(cs35l56->base.regmap);
dev_err(cs35l56->base.dev, "Failed to allocate register map: %d\n",
ret);
return ret;
}
ret = cs35l56_hda_common_probe(cs35l56, id->driver_data, spi_get_chipselect(spi, 0));
if (ret)
return ret;
ret = cs35l56_irq_request(&cs35l56->base, spi->irq);
if (ret < 0)
cs35l56_hda_remove(cs35l56->base.dev);
return ret;
}
static void cs35l56_hda_spi_remove(struct spi_device *spi)
{
cs35l56_hda_remove(&spi->dev);
}
static const struct spi_device_id cs35l56_hda_spi_id[] = {
{ "cs35l54-hda", 0x3554 },
{ "cs35l56-hda", 0x3556 },
{ "cs35l57-hda", 0x3557 },
{}
};
static const struct acpi_device_id cs35l56_acpi_hda_match[] = {
{ "CSC3554", 0 },
{ "CSC3556", 0 },
{ "CSC3557", 0 },
{}
};
MODULE_DEVICE_TABLE(acpi, cs35l56_acpi_hda_match);
static struct spi_driver cs35l56_hda_spi_driver = {
.driver = {
.name = "cs35l56-hda",
.acpi_match_table = cs35l56_acpi_hda_match,
.pm = &cs35l56_hda_pm_ops,
},
.id_table = cs35l56_hda_spi_id,
.probe = cs35l56_hda_spi_probe,
.remove = cs35l56_hda_spi_remove,
};
module_spi_driver(cs35l56_hda_spi_driver);
MODULE_DESCRIPTION("HDA CS35L56 SPI driver");
MODULE_IMPORT_NS("SND_HDA_SCODEC_CS35L56");
MODULE_IMPORT_NS("SND_SOC_CS35L56_SHARED");
MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
MODULE_AUTHOR("Simon Trimmer <simont@opensource.cirrus.com>");
MODULE_LICENSE("GPL");
@@ -0,0 +1,209 @@
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* HD audio Component Binding Interface
*
* Copyright (C) 2021, 2023 Cirrus Logic, Inc. and
* Cirrus Logic International Semiconductor Ltd.
*/
#include <linux/acpi.h>
#include <linux/component.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <sound/hda_codec.h>
#include "hda_component.h"
#include "hda_local.h"
#ifdef CONFIG_ACPI
void hda_component_acpi_device_notify(struct hda_component_parent *parent,
acpi_handle handle, u32 event, void *data)
{
struct hda_component *comp;
int i;
guard(mutex)(&parent->mutex);
for (i = 0; i < ARRAY_SIZE(parent->comps); i++) {
comp = hda_component_from_index(parent, i);
if (comp->dev && comp->acpi_notify)
comp->acpi_notify(acpi_device_handle(comp->adev), event, comp->dev);
}
}
EXPORT_SYMBOL_NS_GPL(hda_component_acpi_device_notify, "SND_HDA_SCODEC_COMPONENT");
int hda_component_manager_bind_acpi_notifications(struct hda_codec *cdc,
struct hda_component_parent *parent,
acpi_notify_handler handler, void *data)
{
bool support_notifications = false;
struct acpi_device *adev;
struct hda_component *comp;
int ret;
int i;
adev = parent->comps[0].adev;
if (!acpi_device_handle(adev))
return 0;
for (i = 0; i < ARRAY_SIZE(parent->comps); i++) {
comp = hda_component_from_index(parent, i);
support_notifications = support_notifications ||
comp->acpi_notifications_supported;
}
if (support_notifications) {
ret = acpi_install_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY,
handler, data);
if (ret < 0) {
codec_warn(cdc, "Failed to install notify handler: %d\n", ret);
return 0;
}
codec_dbg(cdc, "Notify handler installed\n");
}
return 0;
}
EXPORT_SYMBOL_NS_GPL(hda_component_manager_bind_acpi_notifications, "SND_HDA_SCODEC_COMPONENT");
void hda_component_manager_unbind_acpi_notifications(struct hda_codec *cdc,
struct hda_component_parent *parent,
acpi_notify_handler handler)
{
struct acpi_device *adev;
int ret;
adev = parent->comps[0].adev;
if (!acpi_device_handle(adev))
return;
ret = acpi_remove_notify_handler(adev->handle, ACPI_DEVICE_NOTIFY, handler);
if (ret < 0)
codec_warn(cdc, "Failed to uninstall notify handler: %d\n", ret);
}
EXPORT_SYMBOL_NS_GPL(hda_component_manager_unbind_acpi_notifications, "SND_HDA_SCODEC_COMPONENT");
#endif /* ifdef CONFIG_ACPI */
void hda_component_manager_playback_hook(struct hda_component_parent *parent, int action)
{
struct hda_component *comp;
int i;
guard(mutex)(&parent->mutex);
for (i = 0; i < ARRAY_SIZE(parent->comps); i++) {
comp = hda_component_from_index(parent, i);
if (comp->dev && comp->pre_playback_hook)
comp->pre_playback_hook(comp->dev, action);
}
for (i = 0; i < ARRAY_SIZE(parent->comps); i++) {
comp = hda_component_from_index(parent, i);
if (comp->dev && comp->playback_hook)
comp->playback_hook(comp->dev, action);
}
for (i = 0; i < ARRAY_SIZE(parent->comps); i++) {
comp = hda_component_from_index(parent, i);
if (comp->dev && comp->post_playback_hook)
comp->post_playback_hook(comp->dev, action);
}
}
EXPORT_SYMBOL_NS_GPL(hda_component_manager_playback_hook, "SND_HDA_SCODEC_COMPONENT");
struct hda_scodec_match {
const char *bus;
const char *hid;
const char *match_str;
int index;
};
/* match the device name in a slightly relaxed manner */
static int hda_comp_match_dev_name(struct device *dev, void *data)
{
struct hda_scodec_match *p = data;
const char *d = dev_name(dev);
int n = strlen(p->bus);
char tmp[32];
/* check the bus name */
if (strncmp(d, p->bus, n))
return 0;
/* skip the bus number */
if (isdigit(d[n]))
n++;
/* the rest must be exact matching */
snprintf(tmp, sizeof(tmp), p->match_str, p->hid, p->index);
return !strcmp(d + n, tmp);
}
int hda_component_manager_bind(struct hda_codec *cdc,
struct hda_component_parent *parent)
{
/* Init shared and component specific data */
memset(parent->comps, 0, sizeof(parent->comps));
guard(mutex)(&parent->mutex);
return component_bind_all(hda_codec_dev(cdc), parent);
}
EXPORT_SYMBOL_NS_GPL(hda_component_manager_bind, "SND_HDA_SCODEC_COMPONENT");
int hda_component_manager_init(struct hda_codec *cdc,
struct hda_component_parent *parent, int count,
const char *bus, const char *hid,
const char *match_str,
const struct component_master_ops *ops)
{
struct device *dev = hda_codec_dev(cdc);
struct component_match *match = NULL;
struct hda_scodec_match *sm;
int ret, i;
if (parent->codec) {
codec_err(cdc, "Component binding already created (SSID: %x)\n",
cdc->core.subsystem_id);
return -EINVAL;
}
parent->codec = cdc;
mutex_init(&parent->mutex);
for (i = 0; i < count; i++) {
sm = devm_kmalloc(dev, sizeof(*sm), GFP_KERNEL);
if (!sm)
return -ENOMEM;
sm->bus = bus;
sm->hid = hid;
sm->match_str = match_str;
sm->index = i;
component_match_add(dev, &match, hda_comp_match_dev_name, sm);
if (IS_ERR(match)) {
codec_err(cdc, "Fail to add component %ld\n", PTR_ERR(match));
return PTR_ERR(match);
}
}
ret = component_master_add_with_match(dev, ops, match);
if (ret)
codec_err(cdc, "Fail to register component aggregator %d\n", ret);
return ret;
}
EXPORT_SYMBOL_NS_GPL(hda_component_manager_init, "SND_HDA_SCODEC_COMPONENT");
void hda_component_manager_free(struct hda_component_parent *parent,
const struct component_master_ops *ops)
{
struct device *dev;
if (!parent->codec)
return;
dev = hda_codec_dev(parent->codec);
component_master_del(dev, ops);
parent->codec = NULL;
}
EXPORT_SYMBOL_NS_GPL(hda_component_manager_free, "SND_HDA_SCODEC_COMPONENT");
MODULE_DESCRIPTION("HD Audio component binding library");
MODULE_AUTHOR("Richard Fitzgerald <rf@opensource.cirrus.com>");
MODULE_LICENSE("GPL");
@@ -0,0 +1,102 @@
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* HD audio Component Binding Interface
*
* Copyright (C) 2021 Cirrus Logic, Inc. and
* Cirrus Logic International Semiconductor Ltd.
*/
#ifndef __HDA_COMPONENT_H__
#define __HDA_COMPONENT_H__
#include <linux/acpi.h>
#include <linux/component.h>
#include <linux/mutex.h>
#include <sound/hda_codec.h>
#define HDA_MAX_COMPONENTS 4
#define HDA_MAX_NAME_SIZE 50
struct hda_component {
struct device *dev;
char name[HDA_MAX_NAME_SIZE];
struct acpi_device *adev;
bool acpi_notifications_supported;
void (*acpi_notify)(acpi_handle handle, u32 event, struct device *dev);
void (*pre_playback_hook)(struct device *dev, int action);
void (*playback_hook)(struct device *dev, int action);
void (*post_playback_hook)(struct device *dev, int action);
};
struct hda_component_parent {
struct mutex mutex;
struct hda_codec *codec;
struct hda_component comps[HDA_MAX_COMPONENTS];
};
#ifdef CONFIG_ACPI
void hda_component_acpi_device_notify(struct hda_component_parent *parent,
acpi_handle handle, u32 event, void *data);
int hda_component_manager_bind_acpi_notifications(struct hda_codec *cdc,
struct hda_component_parent *parent,
acpi_notify_handler handler, void *data);
void hda_component_manager_unbind_acpi_notifications(struct hda_codec *cdc,
struct hda_component_parent *parent,
acpi_notify_handler handler);
#else
static inline void hda_component_acpi_device_notify(struct hda_component_parent *parent,
acpi_handle handle,
u32 event,
void *data)
{
}
static inline int hda_component_manager_bind_acpi_notifications(struct hda_codec *cdc,
struct hda_component_parent *parent,
acpi_notify_handler handler,
void *data)
{
return 0;
}
static inline void hda_component_manager_unbind_acpi_notifications(struct hda_codec *cdc,
struct hda_component_parent *parent,
acpi_notify_handler handler)
{
}
#endif /* ifdef CONFIG_ACPI */
void hda_component_manager_playback_hook(struct hda_component_parent *parent, int action);
int hda_component_manager_init(struct hda_codec *cdc,
struct hda_component_parent *parent, int count,
const char *bus, const char *hid,
const char *match_str,
const struct component_master_ops *ops);
void hda_component_manager_free(struct hda_component_parent *parent,
const struct component_master_ops *ops);
int hda_component_manager_bind(struct hda_codec *cdc, struct hda_component_parent *parent);
static inline struct hda_component *hda_component_from_index(struct hda_component_parent *parent,
int index)
{
if (!parent)
return NULL;
if (index < 0 || index >= ARRAY_SIZE(parent->comps))
return NULL;
return &parent->comps[index];
}
static inline void hda_component_manager_unbind(struct hda_codec *cdc,
struct hda_component_parent *parent)
{
guard(mutex)(&parent->mutex);
component_unbind_all(hda_codec_dev(cdc), parent);
}
#endif /* ifndef __HDA_COMPONENT_H__ */
@@ -0,0 +1,421 @@
// SPDX-License-Identifier: GPL-2.0
//
// TAS2781 HDA Shared Lib for I2C&SPI driver
//
// Copyright 2025 - 2026 Texas Instruments, Inc.
//
// Author: Shenghao Ding <shenghao-ding@ti.com>
#include <linux/component.h>
#include <linux/crc8.h>
#include <linux/crc32.h>
#include <linux/efi.h>
#include <linux/firmware.h>
#include <linux/i2c.h>
#include <linux/pm_runtime.h>
#include <sound/soc.h>
#include <sound/tas2781.h>
#include "tas2781_hda.h"
#define CALIBRATION_DATA_AREA_NUM 2
const efi_guid_t tasdev_fct_efi_guid[] = {
/* DELL */
EFI_GUID(0xcc92382d, 0x6337, 0x41cb, 0xa8, 0x8b, 0x8e, 0xce, 0x74,
0x91, 0xea, 0x9f),
/* HP */
EFI_GUID(0x02f9af02, 0x7734, 0x4233, 0xb4, 0x3d, 0x93, 0xfe, 0x5a,
0xa3, 0x5d, 0xb3),
/* LENOVO & OTHERS */
EFI_GUID(0x1f52d2a1, 0xbb3a, 0x457d, 0xbc, 0x09, 0x43, 0xa3, 0xf4,
0x31, 0x0a, 0x92),
};
EXPORT_SYMBOL_NS_GPL(tasdev_fct_efi_guid, "SND_HDA_SCODEC_TAS2781");
/*
* The order of calibrated-data writing function is a bit different from the
* order in UEFI. Here is the conversion to match the order of calibrated-data
* writing function.
*/
static void cali_cnv(unsigned char *data, unsigned int base, int offset)
{
struct cali_reg reg_data;
memcpy(&reg_data, &data[base], sizeof(reg_data));
/* the data order has to be swapped between r0_low_reg and inv0_reg */
swap(reg_data.r0_low_reg, reg_data.invr0_reg);
cpu_to_be32_array((__force __be32 *)(data + offset + 1),
(u32 *)&reg_data, TASDEV_CALIB_N);
}
static void tas2781_apply_calib(struct tasdevice_priv *p)
{
struct calidata *cali_data = &p->cali_data;
struct cali_reg *r = &cali_data->cali_reg_array;
unsigned char *data = cali_data->data;
unsigned int *tmp_val = (unsigned int *)data;
unsigned int cali_reg[TASDEV_CALIB_N] = {
TASDEVICE_REG(0, 0x17, 0x74),
TASDEVICE_REG(0, 0x18, 0x0c),
TASDEVICE_REG(0, 0x18, 0x14),
TASDEVICE_REG(0, 0x13, 0x70),
TASDEVICE_REG(0, 0x18, 0x7c),
};
unsigned int crc, oft, node_num;
unsigned char *buf;
int i, j, k, l;
if (tmp_val[0] == 2781) {
/*
* New features were added in calibrated Data V3:
* 1. Added calibration registers address define in
* a node, marked as Device id == 0x80.
* 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 (2781, 4 bytes)
* Data-Group-Sum (4 bytes)
* TimeStamp of Calibration (4 bytes)
* for (i = 0; i < Data-Group-Sum; i++) {
* if (Data type != 0x80) (4 bytes)
* Calibrated Data of Device #i (20 bytes)
* else
* Calibration registers address (5*4 = 20 bytes)
* # V2: No reg addr in data grp section.
* # V3: Normally the last grp is the reg addr.
* }
* CRC (4 bytes)
* Reserved (the rest)
*/
crc = crc32(~0, data, (3 + tmp_val[1] * 6) * 4) ^ ~0;
if (crc != tmp_val[3 + tmp_val[1] * 6]) {
cali_data->total_sz = 0;
dev_err(p->dev, "%s: CRC error\n", __func__);
return;
}
node_num = tmp_val[1];
for (j = 0, k = 0; j < node_num; j++) {
oft = j * 6 + 3;
if (tmp_val[oft] == TASDEV_UEFI_CALI_REG_ADDR_FLG) {
for (i = 0; i < TASDEV_CALIB_N; i++) {
buf = &data[(oft + i + 1) * 4];
cali_reg[i] = TASDEVICE_REG(buf[1],
buf[2], buf[3]);
}
} else {
l = j * (cali_data->cali_dat_sz_per_dev + 1);
if (k >= p->ndev || l > oft * 4) {
dev_err(p->dev, "%s: dev sum error\n",
__func__);
cali_data->total_sz = 0;
return;
}
data[l] = k;
oft++;
cali_cnv(data, 4 * oft, l);
k++;
}
}
} 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 = crc32(~0, data, 84) ^ ~0;
if (crc != tmp_val[21]) {
cali_data->total_sz = 0;
dev_err(p->dev, "%s: V1 CRC error\n", __func__);
return;
}
for (j = p->ndev - 1; j >= 0; j--) {
l = j * (cali_data->cali_dat_sz_per_dev + 1);
cali_cnv(data, cali_data->cali_dat_sz_per_dev * j, l);
data[l] = j;
}
}
if (p->dspbin_typ == TASDEV_BASIC) {
r->r0_reg = cali_reg[0];
r->invr0_reg = cali_reg[1];
r->r0_low_reg = cali_reg[2];
r->pow_reg = cali_reg[3];
r->tlimit_reg = cali_reg[4];
}
cali_data->total_sz = p->ndev * (cali_data->cali_dat_sz_per_dev + 1);
}
/*
* Update the calibration data, including speaker impedance, f0, etc,
* into algo. Calibrate data is done by manufacturer in the factory.
* The data is 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.
*/
int tas2781_save_calibration(struct tas2781_hda *hda)
{
/*
* GUID was used for data access in BIOS, it was provided by board
* manufactory.
*/
efi_guid_t efi_guid = tasdev_fct_efi_guid[LENOVO];
/*
* Some devices save the calibrated data into L"CALI_DATA",
* and others into L"SmartAmpCalibrationData".
*/
static efi_char16_t *efi_name[CALIBRATION_DATA_AREA_NUM] = {
L"CALI_DATA",
L"SmartAmpCalibrationData",
};
struct tasdevice_priv *p = hda->priv;
struct calidata *cali_data = &p->cali_data;
unsigned long total_sz = 0;
unsigned int attr, size;
unsigned char *data;
efi_status_t status;
int i;
if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) {
dev_err(p->dev, "%s: NO EFI FOUND!\n", __func__);
return -EINVAL;
}
if (hda->catlog_id < LENOVO)
efi_guid = tasdev_fct_efi_guid[hda->catlog_id];
cali_data->cali_dat_sz_per_dev = 20;
size = p->ndev * (cali_data->cali_dat_sz_per_dev + 1);
for (i = 0; i < CALIBRATION_DATA_AREA_NUM; i++) {
/* Get real size of UEFI variable */
status = efi.get_variable(efi_name[i], &efi_guid, &attr,
&total_sz, NULL);
cali_data->total_sz = total_sz > size ? total_sz : size;
if (status == EFI_BUFFER_TOO_SMALL) {
/* Allocate data buffer of data_size bytes */
data = cali_data->data = devm_kzalloc(p->dev,
cali_data->total_sz, GFP_KERNEL);
if (!data) {
status = -ENOMEM;
continue;
}
/*
* Set to an invalid value before the calibrated data
* is stored into it, for the default value is 0, which
* means the first device.
*/
data[0] = 0xff;
/* Get variable contents into buffer */
status = efi.get_variable(efi_name[i], &efi_guid,
&attr, &cali_data->total_sz, data);
}
/* Check whether get the calibrated data */
if (status == EFI_SUCCESS)
break;
}
if (status != EFI_SUCCESS) {
cali_data->total_sz = 0;
return status;
}
tas2781_apply_calib(p);
return 0;
}
EXPORT_SYMBOL_NS_GPL(tas2781_save_calibration, "SND_HDA_SCODEC_TAS2781");
void tas2781_hda_remove(struct device *dev,
const struct component_ops *ops)
{
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
component_del(tas_hda->dev, ops);
pm_runtime_get_sync(tas_hda->dev);
pm_runtime_disable(tas_hda->dev);
pm_runtime_put_noidle(tas_hda->dev);
tasdevice_remove(tas_hda->priv);
}
EXPORT_SYMBOL_NS_GPL(tas2781_hda_remove, "SND_HDA_SCODEC_TAS2781");
int tasdevice_info_profile(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *uinfo)
{
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
uinfo->count = 1;
uinfo->value.integer.min = 0;
uinfo->value.integer.max = tas_priv->rcabin.ncfgs - 1;
return 0;
}
EXPORT_SYMBOL_NS_GPL(tasdevice_info_profile, "SND_HDA_SCODEC_TAS2781");
int tasdevice_info_programs(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *uinfo)
{
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
uinfo->count = 1;
uinfo->value.integer.min = 0;
uinfo->value.integer.max = tas_priv->fmw->nr_programs - 1;
return 0;
}
EXPORT_SYMBOL_NS_GPL(tasdevice_info_programs, "SND_HDA_SCODEC_TAS2781");
int tasdevice_info_config(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_info *uinfo)
{
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
struct tasdevice_fw *tas_fw = tas_priv->fmw;
uinfo->type = SNDRV_CTL_ELEM_TYPE_INTEGER;
uinfo->count = 1;
uinfo->value.integer.min = 0;
uinfo->value.integer.max = tas_fw->nr_configurations - 1;
return 0;
}
EXPORT_SYMBOL_NS_GPL(tasdevice_info_config, "SND_HDA_SCODEC_TAS2781");
int tasdevice_get_profile_id(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
ucontrol->value.integer.value[0] = tas_priv->rcabin.profile_cfg_id;
dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n", __func__,
kcontrol->id.name, tas_priv->rcabin.profile_cfg_id);
return 0;
}
EXPORT_SYMBOL_NS_GPL(tasdevice_get_profile_id, "SND_HDA_SCODEC_TAS2781");
int tasdevice_set_profile_id(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
int profile_id = ucontrol->value.integer.value[0];
int max = tas_priv->rcabin.ncfgs - 1;
int val, ret = 0;
val = clamp(profile_id, 0, max);
guard(mutex)(&tas_priv->codec_lock);
dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n", __func__,
kcontrol->id.name, tas_priv->rcabin.profile_cfg_id, val);
if (tas_priv->rcabin.profile_cfg_id != val) {
tas_priv->rcabin.profile_cfg_id = val;
ret = 1;
}
return ret;
}
EXPORT_SYMBOL_NS_GPL(tasdevice_set_profile_id, "SND_HDA_SCODEC_TAS2781");
int tasdevice_program_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
ucontrol->value.integer.value[0] = tas_priv->cur_prog;
dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n", __func__,
kcontrol->id.name, tas_priv->cur_prog);
return 0;
}
EXPORT_SYMBOL_NS_GPL(tasdevice_program_get, "SND_HDA_SCODEC_TAS2781");
int tasdevice_program_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
struct tasdevice_fw *tas_fw = tas_priv->fmw;
int nr_program = ucontrol->value.integer.value[0];
int max = tas_fw->nr_programs - 1;
int val, ret = 0;
val = clamp(nr_program, 0, max);
guard(mutex)(&tas_priv->codec_lock);
dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n", __func__,
kcontrol->id.name, tas_priv->cur_prog, val);
if (tas_priv->cur_prog != val) {
tas_priv->cur_prog = val;
ret = 1;
}
return ret;
}
EXPORT_SYMBOL_NS_GPL(tasdevice_program_put, "SND_HDA_SCODEC_TAS2781");
int tasdevice_config_get(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
ucontrol->value.integer.value[0] = tas_priv->cur_conf;
dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n", __func__,
kcontrol->id.name, tas_priv->cur_conf);
return 0;
}
EXPORT_SYMBOL_NS_GPL(tasdevice_config_get, "SND_HDA_SCODEC_TAS2781");
int tasdevice_config_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
struct tasdevice_fw *tas_fw = tas_priv->fmw;
int nr_config = ucontrol->value.integer.value[0];
int max = tas_fw->nr_configurations - 1;
int val, ret = 0;
val = clamp(nr_config, 0, max);
guard(mutex)(&tas_priv->codec_lock);
dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n", __func__,
kcontrol->id.name, tas_priv->cur_conf, val);
if (tas_priv->cur_conf != val) {
tas_priv->cur_conf = val;
ret = 1;
}
return ret;
}
EXPORT_SYMBOL_NS_GPL(tasdevice_config_put, "SND_HDA_SCODEC_TAS2781");
MODULE_DESCRIPTION("TAS2781 HDA Driver");
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
@@ -0,0 +1,90 @@
/* SPDX-License-Identifier: GPL-2.0-only
*
* HDA audio driver for Texas Instruments TAS2781 smart amp
*
* Copyright (C) 2025 Texas Instruments, Inc.
*/
#ifndef __TAS2781_HDA_H__
#define __TAS2781_HDA_H__
#include <sound/asound.h>
/* Flag of calibration registers address. */
#define TASDEV_UEFI_CALI_REG_ADDR_FLG BIT(7)
#define TASDEV_CALIB_N 5
/*
* 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, xinvert, \
xhandler_get, xhandler_put, tlv_array) { \
.iface = SNDRV_CTL_ELEM_IFACE_CARD, .name = (xname), \
.access = SNDRV_CTL_ELEM_ACCESS_TLV_READ | \
SNDRV_CTL_ELEM_ACCESS_READWRITE, \
.tlv.p = (tlv_array), \
.info = snd_soc_info_volsw, \
.get = xhandler_get, .put = xhandler_put, \
.private_value = (unsigned long)&(struct soc_mixer_control) { \
.reg = xreg, .rreg = xreg, \
.shift = xshift, .rshift = xshift,\
.min = xmin, .max = xmax, .invert = xinvert, \
} \
}
/* Flag control callbacks for tas2781 */
#define ACARD_SINGLE_BOOL_EXT(xname, xdata, xhandler_get, xhandler_put) { \
.iface = SNDRV_CTL_ELEM_IFACE_CARD, \
.name = xname, \
.info = snd_ctl_boolean_mono_info, \
.get = xhandler_get, \
.put = xhandler_put, \
.private_value = xdata, \
}
enum device_catlog_id {
DELL = 0,
HP,
LENOVO,
OTHERS
};
struct tas2781_hda {
struct device *dev;
struct tasdevice_priv *priv;
struct snd_kcontrol *dsp_prog_ctl;
struct snd_kcontrol *dsp_conf_ctl;
struct snd_kcontrol *prof_ctl;
enum device_catlog_id catlog_id;
void *hda_priv;
};
extern const efi_guid_t tasdev_fct_efi_guid[];
int tas2781_save_calibration(struct tas2781_hda *p);
void tas2781_hda_remove(struct device *dev,
const struct component_ops *ops);
int tasdevice_info_profile(struct snd_kcontrol *kctl,
struct snd_ctl_elem_info *uctl);
int tasdevice_info_programs(struct snd_kcontrol *kctl,
struct snd_ctl_elem_info *uctl);
int tasdevice_info_config(struct snd_kcontrol *kctl,
struct snd_ctl_elem_info *uctl);
int tasdevice_set_profile_id(struct snd_kcontrol *kctl,
struct snd_ctl_elem_value *uctl);
int tasdevice_get_profile_id(struct snd_kcontrol *kctl,
struct snd_ctl_elem_value *uctl);
int tasdevice_program_get(struct snd_kcontrol *kctl,
struct snd_ctl_elem_value *uctl);
int tasdevice_program_put(struct snd_kcontrol *kctl,
struct snd_ctl_elem_value *uctl);
int tasdevice_config_put(struct snd_kcontrol *kctl,
struct snd_ctl_elem_value *uctl);
int tasdevice_config_get(struct snd_kcontrol *kctl,
struct snd_ctl_elem_value *uctl);
#endif
@@ -0,0 +1,829 @@
// SPDX-License-Identifier: GPL-2.0
//
// TAS2781 HDA I2C driver
//
// Copyright 2023 - 2026 Texas Instruments, Inc.
//
// Author: Shenghao Ding <shenghao-ding@ti.com>
// Current maintainer: Baojun Xu <baojun.xu@ti.com>
#include <linux/unaligned.h>
#include <linux/acpi.h>
#include <linux/crc8.h>
#include <linux/crc32.h>
#include <linux/efi.h>
#include <linux/firmware.h>
#include <linux/i2c.h>
#include <linux/module.h>
#include <linux/pci_ids.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <sound/hda_codec.h>
#include <sound/soc.h>
#include <sound/tas2781.h>
#include <sound/tas2781-comlib-i2c.h>
#include <sound/tlv.h>
#include <sound/tas2770-tlv.h>
#include <sound/tas2781-tlv.h>
#include <sound/tas5825-tlv.h>
#include "hda_local.h"
#include "hda_auto_parser.h"
#include "hda_component.h"
#include "hda_jack.h"
#include "../generic.h"
#include "tas2781_hda.h"
#define TAS2563_CAL_VAR_NAME_MAX 16
#define TAS2563_CAL_ARRAY_SIZE 80
#define TAS2563_CAL_DATA_SIZE 4
#define TAS2563_MAX_CHANNELS 4
#define TAS2563_CAL_CH_SIZE 20
#define TAS2563_CAL_R0_LOW TASDEVICE_REG(0, 0x0f, 0x48)
#define TAS2563_CAL_POWER TASDEVICE_REG(0, 0x0d, 0x3c)
#define TAS2563_CAL_INVR0 TASDEVICE_REG(0, 0x0f, 0x40)
#define TAS2563_CAL_TLIM TASDEVICE_REG(0, 0x10, 0x14)
#define TAS2563_CAL_R0 TASDEVICE_REG(0, 0x0f, 0x34)
enum device_chip_id {
HDA_TAS2563,
HDA_TAS2770,
HDA_TAS2781,
HDA_TAS5825,
HDA_OTHERS
};
struct tas2781_hda_i2c_priv {
struct snd_kcontrol *snd_ctls[2];
int (*save_calibration)(struct tas2781_hda *h);
int hda_chip_id;
};
static int tas2781_get_i2c_res(struct acpi_resource *ares, void *data)
{
struct tasdevice_priv *tas_priv = data;
struct acpi_resource_i2c_serialbus *sb;
if (i2c_acpi_get_i2c_resource(ares, &sb)) {
if (tas_priv->ndev < TASDEVICE_MAX_CHANNELS &&
sb->slave_address != tas_priv->global_addr) {
tas_priv->tasdevice[tas_priv->ndev].dev_addr =
(unsigned int)sb->slave_address;
tas_priv->ndev++;
}
}
return 1;
}
static const struct acpi_gpio_params speakerid_gpios = { 0, 0, false };
static const struct acpi_gpio_mapping tas2781_speaker_id_gpios[] = {
{ "speakerid-gpios", &speakerid_gpios, 1 },
{ }
};
static int tas2781_read_acpi(struct tasdevice_priv *p, const char *hid)
{
struct gpio_desc *speaker_id;
struct acpi_device *adev;
struct device *physdev;
LIST_HEAD(resources);
const char *sub;
uint32_t subid;
int ret;
adev = acpi_dev_get_first_match_dev(hid, NULL, -1);
if (!adev) {
dev_err(p->dev,
"Failed to find an ACPI device for %s\n", hid);
return -ENODEV;
}
physdev = get_device(acpi_get_first_physical_node(adev));
ret = acpi_dev_get_resources(adev, &resources, tas2781_get_i2c_res, p);
if (ret < 0) {
dev_err(p->dev, "Failed to get ACPI resource.\n");
goto err;
}
sub = acpi_get_subsystem_id(ACPI_HANDLE(physdev));
if (IS_ERR(sub)) {
/* No subsys id in older tas2563 projects. */
if (!strncmp(hid, "INT8866", sizeof("INT8866"))) {
p->speaker_id = -1;
goto end_2563;
}
dev_err(p->dev, "Failed to get SUBSYS ID.\n");
ret = PTR_ERR(sub);
goto err;
}
/* Speaker id was needed for ASUS projects. */
ret = kstrtou32(sub, 16, &subid);
if (!ret && upper_16_bits(subid) == PCI_VENDOR_ID_ASUSTEK) {
ret = acpi_dev_add_driver_gpios(adev, tas2781_speaker_id_gpios);
if (ret < 0) {
dev_err(p->dev, "Failed to add driver gpio %d.\n",
ret);
p->speaker_id = -1;
goto end_2563;
}
speaker_id = fwnode_gpiod_get_index(acpi_fwnode_handle(adev),
"speakerid", 0, GPIOD_IN, NULL);
if (!IS_ERR(speaker_id)) {
p->speaker_id = gpiod_get_value_cansleep(speaker_id);
dev_dbg(p->dev, "Got speaker id gpio from ACPI: %d.\n",
p->speaker_id);
gpiod_put(speaker_id);
} else {
p->speaker_id = -1;
ret = PTR_ERR(speaker_id);
dev_err(p->dev, "Get speaker id gpio failed %d.\n",
ret);
}
acpi_dev_remove_driver_gpios(adev);
} else {
p->speaker_id = -1;
}
end_2563:
acpi_dev_free_resource_list(&resources);
strscpy(p->dev_name, hid, sizeof(p->dev_name));
put_device(physdev);
acpi_dev_put(adev);
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 = dev_get_drvdata(dev);
dev_dbg(tas_hda->dev, "%s: action = %d\n", __func__, action);
switch (action) {
case HDA_GEN_PCM_ACT_OPEN:
pm_runtime_get_sync(dev);
scoped_guard(mutex, &tas_hda->priv->codec_lock) {
tasdevice_tuning_switch(tas_hda->priv, 0);
tas_hda->priv->playback_started = true;
}
break;
case HDA_GEN_PCM_ACT_CLOSE:
scoped_guard(mutex, &tas_hda->priv->codec_lock) {
tasdevice_tuning_switch(tas_hda->priv, 1);
tas_hda->priv->playback_started = false;
}
pm_runtime_put_autosuspend(dev);
break;
default:
break;
}
}
static int tas2781_amp_getvol(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
struct soc_mixer_control *mc =
(struct soc_mixer_control *)kcontrol->private_value;
int ret;
guard(mutex)(&tas_priv->codec_lock);
ret = tasdevice_amp_getvol(tas_priv, ucontrol, mc);
dev_dbg(tas_priv->dev, "%s: kcontrol %s: %ld\n",
__func__, kcontrol->id.name, ucontrol->value.integer.value[0]);
return ret;
}
static int tas2781_amp_putvol(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
struct soc_mixer_control *mc =
(struct soc_mixer_control *)kcontrol->private_value;
guard(mutex)(&tas_priv->codec_lock);
dev_dbg(tas_priv->dev, "%s: kcontrol %s: -> %ld\n",
__func__, kcontrol->id.name, ucontrol->value.integer.value[0]);
/* The check of the given value is in tasdevice_amp_putvol. */
return tasdevice_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 = snd_kcontrol_chip(kcontrol);
guard(mutex)(&tas_priv->codec_lock);
ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d\n",
__func__, kcontrol->id.name, tas_priv->force_fwload_status);
return 0;
}
static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
bool change, val = (bool)ucontrol->value.integer.value[0];
guard(mutex)(&tas_priv->codec_lock);
dev_dbg(tas_priv->dev, "%s: kcontrol %s: %d -> %d\n",
__func__, kcontrol->id.name,
tas_priv->force_fwload_status, val);
if (tas_priv->force_fwload_status == val)
change = false;
else {
change = true;
tas_priv->force_fwload_status = val;
}
return change;
}
static const struct snd_kcontrol_new tas2770_snd_controls[] = {
ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS2770_AMP_LEVEL,
0, 0, 20, 0, tas2781_amp_getvol,
tas2781_amp_putvol, tas2770_amp_tlv),
ACARD_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS2770_DVC_LEVEL,
0, 0, 200, 1, tas2781_amp_getvol,
tas2781_amp_putvol, tas2770_dvc_tlv),
};
static const struct snd_kcontrol_new tas2781_snd_controls[] = {
ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS2781_AMP_LEVEL,
1, 0, 20, 0, tas2781_amp_getvol,
tas2781_amp_putvol, tas2781_amp_tlv),
ACARD_SINGLE_BOOL_EXT("Speaker Force Firmware Load", 0,
tas2781_force_fwload_get, tas2781_force_fwload_put),
};
static const struct snd_kcontrol_new tas5825_snd_controls[] = {
ACARD_SINGLE_RANGE_EXT_TLV("Speaker Analog Volume", TAS5825_AMP_LEVEL,
0, 0, 31, 1, tas2781_amp_getvol,
tas2781_amp_putvol, tas5825_amp_tlv),
ACARD_SINGLE_RANGE_EXT_TLV("Speaker Digital Volume", TAS5825_DVC_LEVEL,
0, 0, 254, 1, tas2781_amp_getvol,
tas2781_amp_putvol, tas5825_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 tasdevice_prof_ctrl = {
.name = "Speaker Profile Id",
.iface = SNDRV_CTL_ELEM_IFACE_CARD,
.info = tasdevice_info_profile,
.get = tasdevice_get_profile_id,
.put = tasdevice_set_profile_id,
};
static const struct snd_kcontrol_new tasdevice_dsp_prog_ctrl = {
.name = "Speaker Program Id",
.iface = SNDRV_CTL_ELEM_IFACE_CARD,
.info = tasdevice_info_programs,
.get = tasdevice_program_get,
.put = tasdevice_program_put,
};
static const struct snd_kcontrol_new tasdevice_dsp_conf_ctrl = {
.name = "Speaker Config Id",
.iface = SNDRV_CTL_ELEM_IFACE_CARD,
.info = tasdevice_info_config,
.get = tasdevice_config_get,
.put = tasdevice_config_put,
};
static int tas2563_save_calibration(struct tas2781_hda *h)
{
efi_guid_t efi_guid = tasdev_fct_efi_guid[LENOVO];
char *vars[TASDEV_CALIB_N] = {
"R0_%d", "R0_Low_%d", "InvR0_%d", "Power_%d", "TLim_%d"
};
efi_char16_t efi_name[TAS2563_CAL_VAR_NAME_MAX];
unsigned long max_size = TAS2563_CAL_DATA_SIZE;
unsigned char var8[TAS2563_CAL_VAR_NAME_MAX];
struct tasdevice_priv *p = h->priv;
struct calidata *cd = &p->cali_data;
struct cali_reg *r = &cd->cali_reg_array;
unsigned int offset = 0;
unsigned char *data;
__be32 bedata;
efi_status_t status;
unsigned int attr;
int ret, i, j, k;
if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) {
dev_err(p->dev, "%s: NO EFI FOUND!\n", __func__);
return -EINVAL;
}
cd->cali_dat_sz_per_dev = TAS2563_CAL_DATA_SIZE * TASDEV_CALIB_N;
/* extra byte for each device is the device number */
cd->total_sz = (cd->cali_dat_sz_per_dev + 1) * p->ndev;
data = cd->data = devm_kzalloc(p->dev, cd->total_sz,
GFP_KERNEL);
if (!data)
return -ENOMEM;
for (i = 0; i < p->ndev; ++i) {
data[offset] = i;
offset++;
for (j = 0; j < TASDEV_CALIB_N; ++j) {
/* EFI name for calibration started with 1, not 0 */
ret = snprintf(var8, sizeof(var8), vars[j], i + 1);
if (ret < 0 || ret >= sizeof(var8) - 1) {
dev_err(p->dev, "%s: Read %s failed\n",
__func__, var8);
cd->total_sz = 0;
return -EINVAL;
}
/*
* Our variable names are ASCII by construction, but
* EFI names are wide chars. Convert and zero-pad.
*/
memset(efi_name, 0, sizeof(efi_name));
for (k = 0; k < sizeof(var8) && var8[k]; k++)
efi_name[k] = var8[k];
status = efi.get_variable(efi_name,
&efi_guid, &attr, &max_size,
&data[offset]);
if (status != EFI_SUCCESS ||
max_size != TAS2563_CAL_DATA_SIZE) {
dev_warn(p->dev,
"Dev %d: Caldat[%d] read failed %ld\n",
i, j, status);
cd->total_sz = 0;
return -EINVAL;
}
bedata = cpu_to_be32(*(uint32_t *)&data[offset]);
memcpy(&data[offset], &bedata, sizeof(bedata));
offset += TAS2563_CAL_DATA_SIZE;
}
}
if (cd->total_sz != offset) {
dev_err(p->dev, "%s: tot_size(%lu) and offset(%u) mismatch\n",
__func__, cd->total_sz, offset);
cd->total_sz = 0;
return -EINVAL;
}
r->r0_reg = TAS2563_CAL_R0;
r->invr0_reg = TAS2563_CAL_INVR0;
r->r0_low_reg = TAS2563_CAL_R0_LOW;
r->pow_reg = TAS2563_CAL_POWER;
r->tlimit_reg = TAS2563_CAL_TLIM;
return 0;
}
static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
{
struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
struct hda_codec *codec = tas_hda->priv->codec;
snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
for (int i = ARRAY_SIZE(hda_priv->snd_ctls) - 1; i >= 0; i--)
snd_ctl_remove(codec->card, hda_priv->snd_ctls[i]);
snd_ctl_remove(codec->card, tas_hda->prof_ctl);
}
static void tasdev_add_kcontrols(struct tasdevice_priv *tas_priv,
struct snd_kcontrol **ctls, struct hda_codec *codec,
const struct snd_kcontrol_new *tas_snd_ctrls, int num_ctls)
{
int i, ret;
for (i = 0; i < num_ctls; i++) {
ctls[i] = snd_ctl_new1(
&tas_snd_ctrls[i], tas_priv);
ret = snd_ctl_add(codec->card, ctls[i]);
if (ret) {
dev_err(tas_priv->dev,
"Failed to add KControl %s = %d\n",
tas_snd_ctrls[i].name, ret);
break;
}
}
}
static void tasdevice_dspfw_init(void *context)
{
struct tasdevice_priv *tas_priv = context;
struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
struct hda_codec *codec = tas_priv->codec;
int ret;
tasdevice_dsp_remove(tas_priv);
tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
if (tas_priv->speaker_id >= 0) {
snprintf(tas_priv->coef_binaryname,
sizeof(tas_priv->coef_binaryname),
"TAS2XXX%04X%d.bin",
lower_16_bits(codec->core.subsystem_id),
tas_priv->speaker_id);
} else {
snprintf(tas_priv->coef_binaryname,
sizeof(tas_priv->coef_binaryname),
"TAS2XXX%04X.bin",
lower_16_bits(codec->core.subsystem_id));
}
ret = tasdevice_dsp_parser(tas_priv);
if (ret) {
dev_err(tas_priv->dev, "dspfw load %s error\n",
tas_priv->coef_binaryname);
tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
return;
}
tasdev_add_kcontrols(tas_priv, &tas_hda->dsp_prog_ctl, codec,
&tasdevice_dsp_prog_ctrl, 1);
tasdev_add_kcontrols(tas_priv, &tas_hda->dsp_conf_ctl, codec,
&tasdevice_dsp_conf_ctrl, 1);
tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
tasdevice_prmg_load(tas_priv, 0);
if (tas_priv->fmw->nr_programs > 0)
tas_priv->cur_prog = 0;
if (tas_priv->fmw->nr_configurations > 0)
tas_priv->cur_conf = 0;
/* Init common setting for different audio profiles */
if (tas_priv->rcabin.init_profile_id >= 0)
tasdevice_select_cfg_blk(tas_priv,
tas_priv->rcabin.init_profile_id,
TASDEVICE_BIN_BLK_PRE_POWER_UP);
/* If calibrated data occurs error, dsp will still works with default
* calibrated data inside algo.
*/
hda_priv->save_calibration(tas_hda);
}
static void tasdev_fw_ready(const struct firmware *fmw, void *context)
{
struct tasdevice_priv *tas_priv = context;
struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
struct tas2781_hda_i2c_priv *hda_priv = tas_hda->hda_priv;
struct hda_codec *codec = tas_priv->codec;
int ret;
guard(pm_runtime_active_auto)(tas_priv->dev);
guard(mutex)(&tas_priv->codec_lock);
ret = tasdevice_rca_parser(tas_priv, fmw);
if (ret)
goto out;
tas_priv->fw_state = TASDEVICE_RCA_FW_OK;
tasdev_add_kcontrols(tas_priv, &tas_hda->prof_ctl, codec,
&tasdevice_prof_ctrl, 1);
switch (hda_priv->hda_chip_id) {
case HDA_TAS2770:
tasdev_add_kcontrols(tas_priv, hda_priv->snd_ctls, codec,
&tas2770_snd_controls[0],
ARRAY_SIZE(tas2770_snd_controls));
break;
case HDA_TAS2781:
tasdev_add_kcontrols(tas_priv, hda_priv->snd_ctls, codec,
&tas2781_snd_controls[0],
ARRAY_SIZE(tas2781_snd_controls));
tasdevice_dspfw_init(context);
break;
case HDA_TAS5825:
tasdev_add_kcontrols(tas_priv, hda_priv->snd_ctls, codec,
&tas5825_snd_controls[0],
ARRAY_SIZE(tas5825_snd_controls));
tasdevice_dspfw_init(context);
break;
case HDA_TAS2563:
tasdevice_dspfw_init(context);
break;
default:
break;
}
out:
release_firmware(fmw);
}
static int tas2781_hda_bind(struct device *dev, struct device *master,
void *master_data)
{
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
struct hda_component_parent *parent = master_data;
struct hda_component *comp;
struct hda_codec *codec;
unsigned int subid;
int ret;
comp = hda_component_from_index(parent, tas_hda->priv->index);
if (!comp)
return -EINVAL;
if (comp->dev)
return -EBUSY;
codec = parent->codec;
subid = codec->core.subsystem_id >> 16;
switch (subid) {
case 0x1028:
tas_hda->catlog_id = DELL;
break;
case 0x103C:
tas_hda->catlog_id = HP;
break;
default:
tas_hda->catlog_id = LENOVO;
break;
}
guard(pm_runtime_active_auto)(dev);
comp->dev = dev;
strscpy(comp->name, dev_name(dev), sizeof(comp->name));
ret = tascodec_init(tas_hda->priv, codec, THIS_MODULE, tasdev_fw_ready);
if (!ret)
comp->playback_hook = tas2781_hda_playback_hook;
return ret;
}
static void tas2781_hda_unbind(struct device *dev,
struct device *master, void *master_data)
{
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
struct hda_component_parent *parent = master_data;
struct hda_component *comp;
comp = hda_component_from_index(parent, tas_hda->priv->index);
if (comp && (comp->dev == dev)) {
comp->dev = NULL;
memset(comp->name, 0, sizeof(comp->name));
comp->playback_hook = NULL;
}
request_firmware_nowait_cancel(tas_hda->priv->dev, tas_hda->priv,
tasdev_fw_ready);
tas2781_hda_remove_controls(tas_hda);
tasdevice_config_info_remove(tas_hda->priv);
tasdevice_dsp_remove(tas_hda->priv);
tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
}
static const struct component_ops tas2781_hda_comp_ops = {
.bind = tas2781_hda_bind,
.unbind = tas2781_hda_unbind,
};
static int tas2781_hda_i2c_probe(struct i2c_client *clt)
{
struct tas2781_hda_i2c_priv *hda_priv;
struct tas2781_hda *tas_hda;
const char *device_name;
int ret;
tas_hda = devm_kzalloc(&clt->dev, sizeof(*tas_hda), GFP_KERNEL);
if (!tas_hda)
return -ENOMEM;
hda_priv = devm_kzalloc(&clt->dev, sizeof(*hda_priv), GFP_KERNEL);
if (!hda_priv)
return -ENOMEM;
tas_hda->hda_priv = hda_priv;
dev_set_drvdata(&clt->dev, tas_hda);
tas_hda->dev = &clt->dev;
tas_hda->priv = tasdevice_kzalloc(clt);
if (!tas_hda->priv)
return -ENOMEM;
if (strstr(dev_name(&clt->dev), "TIAS2781")) {
/*
* TAS2781, integrated on-chip DSP with
* global I2C address supported.
*/
device_name = "TIAS2781";
hda_priv->hda_chip_id = HDA_TAS2781;
tas_hda->priv->chip_id = TAS2781;
hda_priv->save_calibration = tas2781_save_calibration;
tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
} else if (strstarts(dev_name(&clt->dev), "i2c-TXNW2770")) {
/*
* TAS2770, has no on-chip DSP, so no calibration data
* required; has no global I2C address supported.
*/
device_name = "TXNW2770";
hda_priv->hda_chip_id = HDA_TAS2770;
} else if (strstarts(dev_name(&clt->dev),
"i2c-TXNW2781:00-tas2781-hda.0")) {
device_name = "TXNW2781";
hda_priv->hda_chip_id = HDA_TAS2781;
tas_hda->priv->chip_id = TAS2781;
hda_priv->save_calibration = tas2781_save_calibration;
tas_hda->priv->global_addr = TAS2781_GLOBAL_ADDR;
} else if (strstr(dev_name(&clt->dev), "INT8866")) {
/*
* TAS2563, integrated on-chip DSP with
* global I2C address supported.
*/
device_name = "INT8866";
hda_priv->hda_chip_id = HDA_TAS2563;
hda_priv->save_calibration = tas2563_save_calibration;
tas_hda->priv->global_addr = TAS2563_GLOBAL_ADDR;
} else if (strstarts(dev_name(&clt->dev), "i2c-TXNW5825")) {
/*
* TAS5825, integrated on-chip DSP without
* global I2C address and calibration supported.
*/
device_name = "TXNW5825";
hda_priv->hda_chip_id = HDA_TAS5825;
tas_hda->priv->chip_id = TAS5825;
} else {
return -ENODEV;
}
tas_hda->priv->irq = clt->irq;
ret = tas2781_read_acpi(tas_hda->priv, device_name);
if (ret)
return dev_err_probe(tas_hda->dev, ret,
"Platform not supported\n");
ret = tasdevice_init(tas_hda->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_enable(tas_hda->dev);
tasdevice_reset(tas_hda->priv);
ret = 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(&clt->dev, &tas2781_hda_comp_ops);
return ret;
}
static void tas2781_hda_i2c_remove(struct i2c_client *clt)
{
tas2781_hda_remove(&clt->dev, &tas2781_hda_comp_ops);
}
static int tas2781_runtime_suspend(struct device *dev)
{
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
dev_dbg(tas_hda->dev, "Runtime Suspend\n");
guard(mutex)(&tas_hda->priv->codec_lock);
/* The driver powers up the amplifiers at module load time.
* Stop the playback if it's unused.
*/
if (tas_hda->priv->playback_started) {
tasdevice_tuning_switch(tas_hda->priv, 1);
tas_hda->priv->playback_started = false;
}
return 0;
}
static int tas2781_runtime_resume(struct device *dev)
{
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
dev_dbg(tas_hda->dev, "Runtime Resume\n");
guard(mutex)(&tas_hda->priv->codec_lock);
tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
return 0;
}
static int tas2781_system_suspend(struct device *dev)
{
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
dev_dbg(tas_hda->priv->dev, "System Suspend\n");
guard(mutex)(&tas_hda->priv->codec_lock);
/* Shutdown chip before system suspend */
if (tas_hda->priv->playback_started)
tasdevice_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 = dev_get_drvdata(dev);
int i;
dev_dbg(tas_hda->priv->dev, "System Resume\n");
guard(mutex)(&tas_hda->priv->codec_lock);
for (i = 0; i < tas_hda->priv->ndev; i++) {
tas_hda->priv->tasdevice[i].cur_book = -1;
tas_hda->priv->tasdevice[i].cur_prog = -1;
tas_hda->priv->tasdevice[i].cur_conf = -1;
}
tasdevice_reset(tas_hda->priv);
tasdevice_prmg_load(tas_hda->priv, tas_hda->priv->cur_prog);
/* Init common setting for different audio profiles */
if (tas_hda->priv->rcabin.init_profile_id >= 0)
tasdevice_select_cfg_blk(tas_hda->priv,
tas_hda->priv->rcabin.init_profile_id,
TASDEVICE_BIN_BLK_PRE_POWER_UP);
if (tas_hda->priv->playback_started)
tasdevice_tuning_switch(tas_hda->priv, 0);
return 0;
}
static const struct dev_pm_ops tas2781_hda_pm_ops = {
RUNTIME_PM_OPS(tas2781_runtime_suspend, tas2781_runtime_resume, NULL)
SYSTEM_SLEEP_PM_OPS(tas2781_system_suspend, tas2781_system_resume)
};
static const struct i2c_device_id tas2781_hda_i2c_id[] = {
{ .name = "tas2781-hda" },
{ }
};
static const struct acpi_device_id tas2781_acpi_hda_match[] = {
{"INT8866", 0 },
{"TIAS2781", 0 },
{"TXNW2770", 0 },
{"TXNW2781", 0 },
{"TXNW5825", 0 },
{}
};
MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
static struct i2c_driver tas2781_hda_i2c_driver = {
.driver = {
.name = "tas2781-hda",
.acpi_match_table = tas2781_acpi_hda_match,
.pm = &tas2781_hda_pm_ops,
},
.id_table = tas2781_hda_i2c_id,
.probe = tas2781_hda_i2c_probe,
.remove = tas2781_hda_i2c_remove,
};
module_i2c_driver(tas2781_hda_i2c_driver);
MODULE_DESCRIPTION("TAS2781 HDA Driver");
MODULE_AUTHOR("Shenghao Ding, TI, <shenghao-ding@ti.com>");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");
MODULE_IMPORT_NS("SND_HDA_SCODEC_TAS2781");
@@ -0,0 +1,959 @@
// SPDX-License-Identifier: GPL-2.0
//
// TAS2781 HDA SPI driver
//
// Copyright 2024 - 2026 Texas Instruments, Inc.
//
// Author: Baojun Xu <baojun.xu@ti.com>
#include <linux/acpi.h>
#include <linux/array_size.h>
#include <linux/bits.h>
#include <linux/cleanup.h>
#include <linux/crc8.h>
#include <linux/crc32.h>
#include <linux/efi.h>
#include <linux/firmware.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/pm_runtime.h>
#include <linux/property.h>
#include <linux/regmap.h>
#include <linux/spi/spi.h>
#include <linux/time.h>
#include <linux/types.h>
#include <linux/units.h>
#include <sound/hda_codec.h>
#include <sound/soc.h>
#include <sound/tas2781.h>
#include <sound/tlv.h>
#include <sound/tas2781-tlv.h>
#include "hda_local.h"
#include "hda_auto_parser.h"
#include "hda_component.h"
#include "hda_jack.h"
#include "../generic.h"
#include "tas2781_hda.h"
#define TASDEVICE_RANGE_MAX_SIZE (256 * 128)
#define TASDEVICE_WIN_LEN 128
#define TAS2781_SPI_MAX_FREQ (4 * HZ_PER_MHZ)
/* System Reset Check Register */
#define TAS2781_REG_CLK_CONFIG TASDEVICE_REG(0x0, 0x0, 0x5c)
#define TAS2781_REG_CLK_CONFIG_RESET 0x19
struct tas2781_hda_spi_priv {
struct snd_kcontrol *snd_ctls[3];
};
static const struct regmap_range_cfg tasdevice_ranges[] = {
{
.range_min = 0,
.range_max = TASDEVICE_RANGE_MAX_SIZE,
.selector_reg = TASDEVICE_PAGE_SELECT,
.selector_mask = GENMASK(7, 0),
.selector_shift = 0,
.window_start = 0,
.window_len = TASDEVICE_WIN_LEN,
},
};
static const struct regmap_config tasdevice_regmap = {
.reg_bits = 8,
.val_bits = 8,
.zero_flag_mask = true,
.read_flag_mask = 0x01,
.reg_shift = -1,
.cache_type = REGCACHE_NONE,
.ranges = tasdevice_ranges,
.num_ranges = ARRAY_SIZE(tasdevice_ranges),
.max_register = TASDEVICE_RANGE_MAX_SIZE,
};
static int tasdevice_spi_dev_read(struct tasdevice_priv *tas_priv,
unsigned short chn, unsigned int reg, unsigned int *val)
{
int 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 more byte
* 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 = tasdevice_dev_bulk_read(tas_priv, chn, reg,
data, sizeof(data));
*val = data[1];
} else {
ret = tasdevice_dev_read(tas_priv, chn, reg, val);
}
if (ret < 0)
dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
return ret;
}
static int tasdevice_spi_dev_bulk_read(struct tasdevice_priv *tas_priv,
unsigned short chn, unsigned int reg, unsigned char *data,
unsigned int len)
{
int 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 more byte
* 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[TASDEVICE_WIN_LEN + 1];
ret = tasdevice_dev_bulk_read(tas_priv, chn, reg,
buf, len + 1);
memcpy(data, buf + 1, len);
} else {
ret = tasdevice_dev_bulk_read(tas_priv, chn, reg, data, len);
}
if (ret < 0)
dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
return ret;
}
static int tasdevice_spi_dev_update_bits(struct tasdevice_priv *tas_priv,
unsigned short chn, unsigned int reg, unsigned int mask,
unsigned int value)
{
int ret, val;
/*
* In TAS2781 SPI mode, when accessing non-book-zero or page numbers
* greater than 1 in book 0, an additional byte must be read. The
* first byte in such cases is a dummy byte and should be ignored.
*/
if ((TASDEVICE_BOOK_ID(reg) > 0) || (TASDEVICE_PAGE_ID(reg) > 1)) {
unsigned char buf[2];
ret = tasdevice_dev_bulk_read(tas_priv, chn, reg, buf, 2);
val = buf[1];
} else {
ret = tasdevice_dev_read(tas_priv, chn, reg, &val);
}
if (ret < 0) {
dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
return ret;
}
ret = tasdevice_dev_write(tas_priv, chn, TASDEVICE_PAGE_REG(reg),
(val & ~mask) | (mask & value));
if (ret < 0)
dev_err(tas_priv->dev, "%s, E=%d\n", __func__, ret);
return ret;
}
static int tasdevice_spi_change_chn_book(struct tasdevice_priv *p,
unsigned short chn, int book)
{
int ret = 0;
if (chn == p->index) {
struct tasdevice *tasdev = &p->tasdevice[chn];
struct regmap *map = p->regmap;
if (tasdev->cur_book != book) {
ret = regmap_write(map, TASDEVICE_BOOKCTL_REG, book);
if (ret < 0)
dev_err(p->dev, "%s, E=%d\n", __func__, ret);
else
tasdev->cur_book = book;
}
} else {
ret = -EXDEV;
dev_dbg(p->dev, "Not error, %s ignore channel(%d)\n",
__func__, chn);
}
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);
fsleep(800);
gpiod_set_value_cansleep(tas_dev->reset, 1);
}
ret = tasdevice_dev_write(tas_dev, tas_dev->index,
TASDEVICE_REG_SWRESET, TASDEVICE_REG_SWRESET_RESET);
if (ret < 0) {
dev_err(tas_dev->dev, "dev sw-reset fail, %d\n", ret);
return;
}
fsleep(1000);
}
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;
/*
* Codec Lock Hold to ensure that codec_probe and firmware parsing and
* loading do not simultaneously execute.
*/
guard(mutex)(&tas_priv->codec_lock);
scnprintf(tas_priv->rca_binaryname,
sizeof(tas_priv->rca_binaryname), "%sRCA%d.bin",
tas_priv->dev_name, tas_priv->ndev);
crc8_populate_msb(tas_priv->crc8_lkp_tbl, TASDEVICE_CRC8_POLYNOMIAL);
tas_priv->codec = codec;
ret = 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);
return ret;
}
static void tasdevice_spi_init(struct tasdevice_priv *tas_priv)
{
tas_priv->tasdevice[tas_priv->index].cur_book = -1;
tas_priv->tasdevice[tas_priv->index].cur_conf = -1;
tas_priv->tasdevice[tas_priv->index].cur_prog = -1;
tas_priv->isspi = true;
tas_priv->update_bits = tasdevice_spi_dev_update_bits;
tas_priv->change_chn_book = tasdevice_spi_change_chn_book;
tas_priv->dev_read = tasdevice_spi_dev_read;
tas_priv->dev_bulk_read = tasdevice_spi_dev_bulk_read;
mutex_init(&tas_priv->codec_lock);
}
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 = mc->invert;
unsigned char mask;
int max = mc->max;
int val, ret;
mask = rounddown_pow_of_two(max);
mask <<= mc->shift;
val = clamp(invert ? max - ucontrol->value.integer.value[0] :
ucontrol->value.integer.value[0], 0, max);
ret = tasdevice_spi_dev_update_bits(tas_priv, tas_priv->index,
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 = mc->invert;
unsigned char mask = 0;
int max = mc->max;
int ret, val;
ret = tasdevice_spi_dev_read(tas_priv, tas_priv->index, mc->reg, &val);
if (ret) {
dev_err(tas_priv->dev, "%s, get AMP vol error\n", __func__);
return ret;
}
mask = rounddown_pow_of_two(max);
mask <<= mc->shift;
val = (val & mask) >> mc->shift;
val = clamp(invert ? max - val : val, 0, max);
ucontrol->value.integer.value[0] = val;
return ret;
}
static int tasdevice_spi_digital_putvol(struct tasdevice_priv *p,
struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
{
unsigned int invert = mc->invert;
int max = mc->max;
int val, ret;
val = clamp(invert ? max - ucontrol->value.integer.value[0] :
ucontrol->value.integer.value[0], 0, max);
ret = tasdevice_dev_write(p, p->index, mc->reg, (unsigned int)val);
if (ret)
dev_err(p->dev, "set digital vol err in dev %d\n", p->index);
return ret;
}
static int tasdevice_spi_digital_getvol(struct tasdevice_priv *p,
struct snd_ctl_elem_value *ucontrol, struct soc_mixer_control *mc)
{
unsigned int invert = mc->invert;
int max = mc->max;
int ret, val;
ret = tasdevice_spi_dev_read(p, p->index, mc->reg, &val);
if (ret) {
dev_err(p->dev, "%s, get digital vol err\n", __func__);
return ret;
}
val = clamp(invert ? max - val : val, 0, max);
ucontrol->value.integer.value[0] = val;
return ret;
}
static int tas2781_read_acpi(struct tas2781_hda *tas_hda,
const char *hid, int id)
{
struct tasdevice_priv *p = tas_hda->priv;
struct acpi_device *adev;
struct device *physdev;
u32 values[HDA_MAX_COMPONENTS];
const char *property;
size_t nval;
int ret, i;
adev = 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));
physdev = get_device(acpi_get_first_physical_node(adev));
acpi_dev_put(adev);
if (!physdev)
return -ENODEV;
property = "ti,dev-index";
ret = device_property_count_u32(physdev, property);
if (ret <= 0 || ret > ARRAY_SIZE(values)) {
ret = -EINVAL;
goto err;
}
p->ndev = nval = ret;
ret = device_property_read_u32_array(physdev, property, values, nval);
if (ret)
goto err;
p->index = U8_MAX;
for (i = 0; i < nval; i++) {
if (values[i] == id) {
p->index = i;
break;
}
}
if (p->index == U8_MAX) {
dev_dbg(p->dev, "No index found in %s\n", property);
ret = -ENODEV;
goto err;
}
if (p->index == 0) {
/* All of amps share same RESET pin. */
p->reset = devm_gpiod_get_index_optional(physdev, "reset",
p->index, GPIOD_OUT_LOW);
if (IS_ERR(p->reset)) {
ret = PTR_ERR(p->reset);
dev_err_probe(p->dev, ret, "Failed on reset GPIO\n");
goto err;
}
}
put_device(physdev);
return 0;
err:
dev_err(p->dev, "read acpi error, ret: %d\n", ret);
put_device(physdev);
return ret;
}
static void tas2781_hda_playback_hook(struct device *dev, int action)
{
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
struct tasdevice_priv *tas_priv = tas_hda->priv;
if (action == HDA_GEN_PCM_ACT_OPEN) {
pm_runtime_get_sync(dev);
guard(mutex)(&tas_priv->codec_lock);
if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK)
tasdevice_tuning_switch(tas_hda->priv, 0);
} else if (action == HDA_GEN_PCM_ACT_CLOSE) {
guard(mutex)(&tas_priv->codec_lock);
if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK)
tasdevice_tuning_switch(tas_priv, 1);
pm_runtime_put_autosuspend(dev);
}
}
/*
* 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.
*
* Return 0 if succeeded.
*/
static int tas2781_digital_getvol(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
struct soc_mixer_control *mc =
(struct soc_mixer_control *)kcontrol->private_value;
guard(mutex)(&tas_priv->codec_lock);
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 = snd_kcontrol_chip(kcontrol);
struct soc_mixer_control *mc =
(struct soc_mixer_control *)kcontrol->private_value;
guard(mutex)(&tas_priv->codec_lock);
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 = snd_kcontrol_chip(kcontrol);
struct soc_mixer_control *mc =
(struct soc_mixer_control *)kcontrol->private_value;
guard(mutex)(&tas_priv->codec_lock);
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 = snd_kcontrol_chip(kcontrol);
struct soc_mixer_control *mc =
(struct soc_mixer_control *)kcontrol->private_value;
guard(mutex)(&tas_priv->codec_lock);
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 = snd_kcontrol_chip(kcontrol);
ucontrol->value.integer.value[0] = (int)tas_priv->force_fwload_status;
dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
str_on_off(tas_priv->force_fwload_status));
return 0;
}
static int tas2781_force_fwload_put(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol)
{
struct tasdevice_priv *tas_priv = snd_kcontrol_chip(kcontrol);
bool change, val = (bool)ucontrol->value.integer.value[0];
if (tas_priv->force_fwload_status == val) {
change = false;
} else {
change = true;
tas_priv->force_fwload_status = val;
}
dev_dbg(tas_priv->dev, "%s : Force FWload %s\n", __func__,
str_on_off(tas_priv->force_fwload_status));
return change;
}
static struct snd_kcontrol_new tas2781_snd_ctls[] = {
ACARD_SINGLE_RANGE_EXT_TLV(NULL, TAS2781_AMP_LEVEL, 1, 0, 20, 0,
tas2781_amp_getvol, tas2781_amp_putvol,
tas2781_amp_tlv),
ACARD_SINGLE_RANGE_EXT_TLV(NULL, TAS2781_DVC_LVL, 0, 0, 200, 1,
tas2781_digital_getvol, tas2781_digital_putvol,
tas2781_dvc_tlv),
ACARD_SINGLE_BOOL_EXT(NULL, 0, tas2781_force_fwload_get,
tas2781_force_fwload_put),
};
static struct snd_kcontrol_new tas2781_prof_ctl = {
.iface = SNDRV_CTL_ELEM_IFACE_CARD,
.info = tasdevice_info_profile,
.get = tasdevice_get_profile_id,
.put = tasdevice_set_profile_id,
};
static struct snd_kcontrol_new tas2781_dsp_ctls[] = {
/* Speaker Program */
{
.iface = SNDRV_CTL_ELEM_IFACE_CARD,
.info = tasdevice_info_programs,
.get = tasdevice_program_get,
.put = tasdevice_program_put,
},
/* Speaker Config */
{
.iface = SNDRV_CTL_ELEM_IFACE_CARD,
.info = tasdevice_info_config,
.get = tasdevice_config_get,
.put = tasdevice_config_put,
},
};
static void tas2781_hda_remove_controls(struct tas2781_hda *tas_hda)
{
struct hda_codec *codec = tas_hda->priv->codec;
struct tas2781_hda_spi_priv *h_priv = tas_hda->hda_priv;
snd_ctl_remove(codec->card, tas_hda->dsp_prog_ctl);
snd_ctl_remove(codec->card, tas_hda->dsp_conf_ctl);
for (int i = ARRAY_SIZE(h_priv->snd_ctls) - 1; i >= 0; i--)
snd_ctl_remove(codec->card, h_priv->snd_ctls[i]);
snd_ctl_remove(codec->card, tas_hda->prof_ctl);
}
static int tas2781_hda_spi_prf_ctl(struct tas2781_hda *h)
{
struct tasdevice_priv *p = h->priv;
struct hda_codec *c = p->codec;
char name[64];
int rc;
snprintf(name, sizeof(name), "Speaker-%d Profile Id", p->index);
tas2781_prof_ctl.name = name;
h->prof_ctl = snd_ctl_new1(&tas2781_prof_ctl, p);
rc = snd_ctl_add(c->card, h->prof_ctl);
if (rc)
dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
tas2781_prof_ctl.name, rc);
return rc;
}
static int tas2781_hda_spi_snd_ctls(struct tas2781_hda *h)
{
struct tas2781_hda_spi_priv *h_priv = h->hda_priv;
struct tasdevice_priv *p = h->priv;
struct hda_codec *c = p->codec;
char name[64];
int i = 0;
int rc;
snprintf(name, sizeof(name), "Speaker-%d Analog Volume", p->index);
tas2781_snd_ctls[i].name = name;
h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);
rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);
if (rc) {
dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
tas2781_snd_ctls[i].name, rc);
return rc;
}
i++;
snprintf(name, sizeof(name), "Speaker-%d Digital Volume", p->index);
tas2781_snd_ctls[i].name = name;
h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);
rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);
if (rc) {
dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
tas2781_snd_ctls[i].name, rc);
return rc;
}
i++;
snprintf(name, sizeof(name), "Force Speaker-%d FW Load", p->index);
tas2781_snd_ctls[i].name = name;
h_priv->snd_ctls[i] = snd_ctl_new1(&tas2781_snd_ctls[i], p);
rc = snd_ctl_add(c->card, h_priv->snd_ctls[i]);
if (rc) {
dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
tas2781_snd_ctls[i].name, rc);
}
return rc;
}
static int tas2781_hda_spi_dsp_ctls(struct tas2781_hda *h)
{
struct tasdevice_priv *p = h->priv;
struct hda_codec *c = p->codec;
char name[64];
int i = 0;
int rc;
snprintf(name, sizeof(name), "Speaker-%d Program Id", p->index);
tas2781_dsp_ctls[i].name = name;
h->dsp_prog_ctl = snd_ctl_new1(&tas2781_dsp_ctls[i], p);
rc = snd_ctl_add(c->card, h->dsp_prog_ctl);
if (rc) {
dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
tas2781_dsp_ctls[i].name, rc);
return rc;
}
i++;
snprintf(name, sizeof(name), "Speaker-%d Config Id", p->index);
tas2781_dsp_ctls[i].name = name;
h->dsp_conf_ctl = snd_ctl_new1(&tas2781_dsp_ctls[i], p);
rc = snd_ctl_add(c->card, h->dsp_conf_ctl);
if (rc) {
dev_err(p->dev, "Failed to add KControl: %s, rc = %d\n",
tas2781_dsp_ctls[i].name, rc);
}
return rc;
}
static void tasdev_fw_ready(const struct firmware *fmw, void *context)
{
struct tasdevice_priv *tas_priv = context;
struct tas2781_hda *tas_hda = dev_get_drvdata(tas_priv->dev);
struct hda_codec *codec = tas_priv->codec;
int ret;
guard(pm_runtime_active_auto)(tas_priv->dev);
guard(mutex)(&tas_priv->codec_lock);
ret = tasdevice_rca_parser(tas_priv, fmw);
if (ret)
goto out;
/* Add control one time only. */
ret = tas2781_hda_spi_prf_ctl(tas_hda);
if (ret)
goto out;
ret = tas2781_hda_spi_snd_ctls(tas_hda);
if (ret)
goto out;
tasdevice_dsp_remove(tas_priv);
tas_priv->fw_state = TASDEVICE_DSP_FW_PENDING;
scnprintf(tas_priv->coef_binaryname, 64, "TAS2XXX%04X-%01d.bin",
lower_16_bits(codec->core.subsystem_id), tas_priv->index);
ret = tasdevice_dsp_parser(tas_priv);
if (ret) {
dev_err(tas_priv->dev, "dspfw load %s error\n",
tas_priv->coef_binaryname);
tas_priv->fw_state = TASDEVICE_DSP_FW_FAIL;
goto out;
}
ret = tas2781_hda_spi_dsp_ctls(tas_hda);
if (ret)
goto out;
/* Perform AMP reset before firmware download. */
tas2781_spi_reset(tas_priv);
tas_priv->rcabin.profile_cfg_id = 0;
tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
ret = tasdevice_prmg_load(tas_priv, 0);
if (ret < 0) {
dev_err(tas_priv->dev, "FW download failed = %d\n", ret);
goto out;
}
tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
if (tas_priv->fmw->nr_programs > 0)
tas_priv->tasdevice[tas_priv->index].cur_prog = 0;
if (tas_priv->fmw->nr_configurations > 0)
tas_priv->tasdevice[tas_priv->index].cur_conf = 0;
/*
* If calibrated data occurs error, dsp will still works with default
* calibrated data inside algo.
*/
tas2781_save_calibration(tas_hda);
out:
release_firmware(fmw);
}
static int tas2781_hda_bind(struct device *dev, struct device *master,
void *master_data)
{
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
struct hda_component_parent *parent = master_data;
struct hda_component *comp;
struct hda_codec *codec;
int ret;
comp = hda_component_from_index(parent, tas_hda->priv->index);
if (!comp)
return -EINVAL;
if (comp->dev)
return -EBUSY;
codec = parent->codec;
guard(pm_runtime_active_auto)(dev);
comp->dev = dev;
strscpy(comp->name, dev_name(dev), sizeof(comp->name));
ret = tascodec_spi_init(tas_hda->priv, codec, THIS_MODULE,
tasdev_fw_ready);
if (!ret)
comp->playback_hook = tas2781_hda_playback_hook;
/* Only HP Laptop support SPI-based TAS2781 */
tas_hda->catlog_id = HP;
return ret;
}
static void tas2781_hda_unbind(struct device *dev, struct device *master,
void *master_data)
{
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
struct hda_component_parent *parent = master_data;
struct tasdevice_priv *tas_priv = tas_hda->priv;
struct hda_component *comp;
comp = hda_component_from_index(parent, tas_priv->index);
if (comp && (comp->dev == dev)) {
comp->dev = NULL;
memset(comp->name, 0, sizeof(comp->name));
comp->playback_hook = NULL;
}
request_firmware_nowait_cancel(tas_priv->dev, tas_priv,
tasdev_fw_ready);
tas2781_hda_remove_controls(tas_hda);
tasdevice_config_info_remove(tas_priv);
tasdevice_dsp_remove(tas_priv);
tas_hda->priv->fw_state = TASDEVICE_DSP_FW_PENDING;
}
static const struct component_ops tas2781_hda_comp_ops = {
.bind = tas2781_hda_bind,
.unbind = tas2781_hda_unbind,
};
static int tas2781_hda_spi_probe(struct spi_device *spi)
{
struct tas2781_hda_spi_priv *hda_priv;
struct tasdevice_priv *tas_priv;
struct tas2781_hda *tas_hda;
const char *device_name;
int ret = 0;
tas_hda = devm_kzalloc(&spi->dev, sizeof(*tas_hda), GFP_KERNEL);
if (!tas_hda)
return -ENOMEM;
hda_priv = devm_kzalloc(&spi->dev, sizeof(*hda_priv), GFP_KERNEL);
if (!hda_priv)
return -ENOMEM;
tas_hda->hda_priv = hda_priv;
spi->max_speed_hz = TAS2781_SPI_MAX_FREQ;
tas_priv = devm_kzalloc(&spi->dev, sizeof(*tas_priv), GFP_KERNEL);
if (!tas_priv)
return -ENOMEM;
tas_priv->dev = &spi->dev;
tas_hda->priv = tas_priv;
tas_priv->regmap = devm_regmap_init_spi(spi, &tasdevice_regmap);
if (IS_ERR(tas_priv->regmap)) {
ret = PTR_ERR(tas_priv->regmap);
dev_err(tas_priv->dev, "Failed to allocate regmap: %d\n",
ret);
return ret;
}
if (strstr(dev_name(&spi->dev), "TXNW2781")) {
device_name = "TXNW2781";
tas_hda->priv->chip_id = TAS2781;
} else {
dev_err(tas_priv->dev, "Unmatched spi dev %s\n",
dev_name(&spi->dev));
return -ENODEV;
}
tas_priv->irq = spi->irq;
dev_set_drvdata(&spi->dev, tas_hda);
ret = tas2781_read_acpi(tas_hda, device_name,
spi_get_chipselect(spi, 0));
if (ret)
return dev_err_probe(tas_priv->dev, ret,
"Platform not supported\n");
tasdevice_spi_init(tas_priv);
pm_runtime_set_autosuspend_delay(tas_priv->dev, 3000);
pm_runtime_use_autosuspend(tas_priv->dev);
pm_runtime_set_active(tas_priv->dev);
pm_runtime_get_noresume(tas_priv->dev);
pm_runtime_enable(tas_priv->dev);
pm_runtime_put_autosuspend(tas_priv->dev);
ret = component_add(tas_priv->dev, &tas2781_hda_comp_ops);
if (ret) {
dev_err(tas_priv->dev, "Register component fail: %d\n", ret);
pm_runtime_disable(tas_priv->dev);
tas2781_hda_remove(&spi->dev, &tas2781_hda_comp_ops);
}
return ret;
}
static void tas2781_hda_spi_remove(struct spi_device *spi)
{
tas2781_hda_remove(&spi->dev, &tas2781_hda_comp_ops);
}
static int tas2781_runtime_suspend(struct device *dev)
{
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
struct tasdevice_priv *tas_priv = tas_hda->priv;
guard(mutex)(&tas_priv->codec_lock);
if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK
&& tas_priv->playback_started)
tasdevice_tuning_switch(tas_priv, 1);
tas_priv->tasdevice[tas_priv->index].cur_book = -1;
tas_priv->tasdevice[tas_priv->index].cur_conf = -1;
return 0;
}
static int tas2781_runtime_resume(struct device *dev)
{
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
struct tasdevice_priv *tas_priv = tas_hda->priv;
guard(mutex)(&tas_priv->codec_lock);
if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK
&& tas_priv->playback_started)
tasdevice_tuning_switch(tas_priv, 0);
return 0;
}
static int tas2781_system_suspend(struct device *dev)
{
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
struct tasdevice_priv *tas_priv = tas_hda->priv;
int ret;
ret = pm_runtime_force_suspend(dev);
if (ret)
return ret;
/* Shutdown chip before system suspend */
if (tas_priv->fw_state == TASDEVICE_DSP_FW_ALL_OK
&& tas_priv->playback_started)
tasdevice_tuning_switch(tas_priv, 1);
return 0;
}
static int tas2781_system_resume(struct device *dev)
{
struct tas2781_hda *tas_hda = dev_get_drvdata(dev);
struct tasdevice_priv *tas_priv = tas_hda->priv;
int ret, val;
ret = pm_runtime_force_resume(dev);
if (ret)
return ret;
guard(mutex)(&tas_priv->codec_lock);
ret = tas_priv->dev_read(tas_priv, tas_priv->index,
TAS2781_REG_CLK_CONFIG, &val);
if (ret < 0)
return ret;
if (val == TAS2781_REG_CLK_CONFIG_RESET) {
tas_priv->tasdevice[tas_priv->index].cur_book = -1;
tas_priv->tasdevice[tas_priv->index].cur_conf = -1;
tas_priv->tasdevice[tas_priv->index].cur_prog = -1;
ret = tasdevice_prmg_load(tas_priv, 0);
if (ret < 0) {
dev_err(tas_priv->dev,
"FW download failed = %d\n", ret);
return ret;
}
tas_priv->fw_state = TASDEVICE_DSP_FW_ALL_OK;
if (tas_priv->playback_started)
tasdevice_tuning_switch(tas_priv, 0);
}
return ret;
}
static const struct dev_pm_ops tas2781_hda_pm_ops = {
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[] = {
{ "tas2781-hda", },
{}
};
static const struct acpi_device_id tas2781_acpi_hda_match[] = {
{"TXNW2781", },
{}
};
MODULE_DEVICE_TABLE(acpi, tas2781_acpi_hda_match);
static struct spi_driver tas2781_hda_spi_driver = {
.driver = {
.name = "tas2781-hda",
.acpi_match_table = tas2781_acpi_hda_match,
.pm = &tas2781_hda_pm_ops,
},
.id_table = tas2781_hda_spi_id,
.probe = tas2781_hda_spi_probe,
.remove = tas2781_hda_spi_remove,
};
module_spi_driver(tas2781_hda_spi_driver);
MODULE_DESCRIPTION("TAS2781 HDA SPI Driver");
MODULE_AUTHOR("Baojun, Xu, <baojun.xug@ti.com>");
MODULE_LICENSE("GPL");
MODULE_IMPORT_NS("SND_SOC_TAS2781_FMWLIB");
MODULE_IMPORT_NS("SND_HDA_SCODEC_TAS2781");