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
+61
View File
@@ -0,0 +1,61 @@
# SPDX-License-Identifier: GPL-2.0-only
menu "SoundWire (SDCA)"
config SND_SOC_SDCA
tristate "SDCA core support"
depends on ACPI
select AUXILIARY_BUS
help
This option enables support for the MIPI SoundWire Device
Class for Audio (SDCA).
config SND_SOC_SDCA_HID
bool "SDCA HID support"
depends on SND_SOC_SDCA
depends on HID=y || HID=SND_SOC_SDCA
default y
help
This option enables support for audio jack button reporting using HID.
config SND_SOC_SDCA_IRQ
bool "SDCA IRQ support"
select REGMAP
select REGMAP_IRQ
depends on SND_SOC_SDCA
default y
help
This option enables support for SDCA IRQs.
config SND_SOC_SDCA_FDL
bool "SDCA FDL (File DownLoad) support"
depends on SND_SOC_SDCA
default y
help
This option enables support for the File Download using UMP,
typically used for downloading firmware to devices.
config SND_SOC_SDCA_OPTIONAL
def_tristate SND_SOC_SDCA || !SND_SOC_SDCA
config SND_SOC_SDCA_CLASS
tristate "SDCA Class Driver"
depends on SOUNDWIRE
depends on HID=y || HID=SND_SOC_SDCA
depends on SND_SOC_SDCA
select SND_SOC_SDCA_CLASS_FUNCTION
select SND_SOC_SDCA_FDL
select SND_SOC_SDCA_HID
select SND_SOC_SDCA_IRQ
select REGMAP_SOUNDWIRE
help
This option enables support for the SDCA Class driver which should
support any class compliant SDCA part.
config SND_SOC_SDCA_CLASS_FUNCTION
tristate
select REGMAP_SOUNDWIRE_MBQ
help
This option enables support for the SDCA Class Function drivers,
these implement the individual functions of the SDCA Class driver.
endmenu
+15
View File
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: GPL-2.0-only
snd-soc-sdca-y := sdca_functions.o sdca_device.o sdca_function_device.o \
sdca_regmap.o sdca_asoc.o sdca_ump.o
snd-soc-sdca-$(CONFIG_SND_SOC_SDCA_HID) += sdca_hid.o
snd-soc-sdca-$(CONFIG_SND_SOC_SDCA_IRQ) += sdca_interrupts.o sdca_jack.o
snd-soc-sdca-$(CONFIG_SND_SOC_SDCA_FDL) += sdca_fdl.o
snd-soc-sdca-class-y := sdca_class.o
snd-soc-sdca-class-function-y := sdca_class_function.o
obj-$(CONFIG_SND_SOC_SDCA) += snd-soc-sdca.o
obj-$(CONFIG_SND_SOC_SDCA_CLASS) += snd-soc-sdca-class.o
obj-$(CONFIG_SND_SOC_SDCA_CLASS_FUNCTION) += snd-soc-sdca-class-function.o
File diff suppressed because it is too large Load Diff
+297
View File
@@ -0,0 +1,297 @@
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2025 Cirrus Logic, Inc. and
// Cirrus Logic International Semiconductor Ltd.
/*
* The MIPI SDCA specification is available for public downloads at
* https://www.mipi.org/mipi-sdca-v1-0-download
*/
#include <linux/device.h>
#include <linux/err.h>
#include <linux/module.h>
#include <linux/pm.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/soundwire/sdw.h>
#include <linux/soundwire/sdw_registers.h>
#include <linux/soundwire/sdw_type.h>
#include <sound/sdca.h>
#include <sound/sdca_function.h>
#include <sound/sdca_interrupts.h>
#include <sound/sdca_regmap.h>
#include "sdca_class.h"
#define CLASS_SDW_ATTACH_TIMEOUT_MS 5000
static int class_read_prop(struct sdw_slave *sdw)
{
struct sdw_slave_prop *prop = &sdw->prop;
sdw_slave_read_prop(sdw);
prop->use_domain_irq = true;
prop->scp_int1_mask = SDW_SCP_INT1_BUS_CLASH | SDW_SCP_INT1_PARITY |
SDW_SCP_INT1_IMPL_DEF;
return 0;
}
static const struct sdw_slave_ops class_sdw_ops = {
.read_prop = class_read_prop,
};
static void class_regmap_lock(void *data)
{
struct mutex *lock = data;
mutex_lock(lock);
}
static void class_regmap_unlock(void *data)
{
struct mutex *lock = data;
mutex_unlock(lock);
}
static bool class_dev_regmap_volatile(struct device *dev, unsigned int reg)
{
switch (reg) {
case SDW_SCP_SDCA_INTMASK1 ... SDW_SCP_SDCA_INTMASK4:
return false;
default:
return true;
}
}
static bool class_dev_regmap_precious(struct device *dev, unsigned int reg)
{
switch (reg) {
case SDW_SCP_SDCA_INT1 ... SDW_SCP_SDCA_INT4:
case SDW_SCP_SDCA_INTMASK1 ... SDW_SCP_SDCA_INTMASK4:
return false;
default:
return true;
}
}
static const struct regmap_config class_dev_regmap_config = {
.name = "sdca-device",
.reg_bits = 32,
.val_bits = 8,
.max_register = SDW_SDCA_MAX_REGISTER,
.volatile_reg = class_dev_regmap_volatile,
.precious_reg = class_dev_regmap_precious,
.cache_type = REGCACHE_MAPLE,
.lock = class_regmap_lock,
.unlock = class_regmap_unlock,
};
static void class_remove_functions(void *data)
{
struct sdca_class_drv *drv = data;
sdca_dev_unregister_functions(drv->sdw);
}
static void class_boot_work(struct work_struct *work)
{
struct sdca_class_drv *drv = container_of(work,
struct sdca_class_drv,
boot_work);
int ret;
ret = sdw_slave_wait_for_init(drv->sdw, CLASS_SDW_ATTACH_TIMEOUT_MS);
if (ret)
goto err;
regcache_cache_only(drv->dev_regmap, false);
drv->irq_info = devm_sdca_irq_allocate(drv->dev, drv->dev_regmap,
drv->sdw->irq);
if (IS_ERR(drv->irq_info))
goto err;
ret = sdca_dev_register_functions(drv->sdw);
if (ret)
goto err;
/* Ensure function drivers are removed before the IRQ is destroyed */
ret = devm_add_action_or_reset(drv->dev, class_remove_functions, drv);
if (ret)
goto err;
dev_dbg(drv->dev, "boot work complete\n");
pm_runtime_mark_last_busy(drv->dev);
pm_runtime_put_autosuspend(drv->dev);
return;
err:
pm_runtime_put_sync(drv->dev);
}
static int class_sdw_probe(struct sdw_slave *sdw, const struct sdw_device_id *id)
{
struct device *dev = &sdw->dev;
struct regmap_config *dev_config;
struct sdca_class_drv *drv;
int ret;
sdca_lookup_swft(sdw);
drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
if (!drv)
return -ENOMEM;
dev_config = devm_kmemdup(dev, &class_dev_regmap_config,
sizeof(*dev_config), GFP_KERNEL);
if (!dev_config)
return -ENOMEM;
drv->dev = dev;
drv->sdw = sdw;
mutex_init(&drv->regmap_lock);
mutex_init(&drv->init_lock);
dev_set_drvdata(drv->dev, drv);
INIT_WORK(&drv->boot_work, class_boot_work);
dev_config->lock_arg = &drv->regmap_lock;
drv->dev_regmap = devm_regmap_init_sdw(sdw, dev_config);
if (IS_ERR(drv->dev_regmap))
return dev_err_probe(drv->dev, PTR_ERR(drv->dev_regmap),
"failed to create device regmap\n");
regcache_cache_only(drv->dev_regmap, true);
pm_runtime_set_autosuspend_delay(dev, 250);
pm_runtime_use_autosuspend(dev);
pm_runtime_set_active(dev);
pm_runtime_get_noresume(dev);
ret = devm_pm_runtime_enable(dev);
if (ret)
return ret;
queue_work(system_long_wq, &drv->boot_work);
return 0;
}
static void class_sdw_remove(struct sdw_slave *sdw)
{
struct device *dev = &sdw->dev;
struct sdca_class_drv *drv = dev_get_drvdata(dev);
cancel_work_sync(&drv->boot_work);
}
static int class_suspend(struct device *dev)
{
struct sdca_class_drv *drv = dev_get_drvdata(dev);
int ret;
disable_irq(drv->sdw->irq);
ret = pm_runtime_force_suspend(dev);
if (ret) {
dev_err(dev, "failed to force suspend: %d\n", ret);
return ret;
}
return 0;
}
static int class_resume(struct device *dev)
{
struct sdca_class_drv *drv = dev_get_drvdata(dev);
int ret;
ret = pm_runtime_force_resume(dev);
if (ret) {
dev_err(dev, "failed to force resume: %d\n", ret);
return ret;
}
enable_irq(drv->sdw->irq);
return 0;
}
static int class_runtime_suspend(struct device *dev)
{
struct sdca_class_drv *drv = dev_get_drvdata(dev);
/*
* Whilst the driver doesn't power the chip down here, going into runtime
* suspend lets the SoundWire bus power down, which means the driver
* can't communicate with the device any more.
*/
regcache_cache_only(drv->dev_regmap, true);
return 0;
}
static int class_runtime_resume(struct device *dev)
{
struct sdca_class_drv *drv = dev_get_drvdata(dev);
int ret;
ret = sdw_slave_wait_for_init(drv->sdw, CLASS_SDW_ATTACH_TIMEOUT_MS);
if (ret)
goto err;
regcache_cache_only(drv->dev_regmap, false);
regcache_mark_dirty(drv->dev_regmap);
ret = regcache_sync(drv->dev_regmap);
if (ret) {
dev_err(drv->dev, "failed to restore cache: %d\n", ret);
goto err;
}
return 0;
err:
regcache_cache_only(drv->dev_regmap, true);
return ret;
}
static const struct dev_pm_ops class_pm_ops = {
SYSTEM_SLEEP_PM_OPS(class_suspend, class_resume)
RUNTIME_PM_OPS(class_runtime_suspend, class_runtime_resume, NULL)
};
static const struct sdw_device_id class_sdw_id[] = {
SDW_SLAVE_ENTRY(0x01FA, 0x4245, 0),
SDW_SLAVE_ENTRY(0x01FA, 0x4249, 0),
SDW_SLAVE_ENTRY(0x01FA, 0x4747, 0),
{}
};
MODULE_DEVICE_TABLE(sdw, class_sdw_id);
static struct sdw_driver class_sdw_driver = {
.driver = {
.name = "sdca_class",
.pm = pm_ptr(&class_pm_ops),
},
.probe = class_sdw_probe,
.remove = class_sdw_remove,
.id_table = class_sdw_id,
.ops = &class_sdw_ops,
};
module_sdw_driver(class_sdw_driver);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("SDCA Class Driver");
MODULE_IMPORT_NS("SND_SOC_SDCA");
+35
View File
@@ -0,0 +1,35 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* The MIPI SDCA specification is available for public downloads at
* https://www.mipi.org/mipi-sdca-v1-0-download
*
* Copyright (C) 2025 Cirrus Logic, Inc. and
* Cirrus Logic International Semiconductor Ltd.
*/
#ifndef __SDCA_CLASS_H__
#define __SDCA_CLASS_H__
#include <linux/completion.h>
#include <linux/mutex.h>
#include <linux/workqueue.h>
struct device;
struct regmap;
struct sdw_slave;
struct sdca_function_data;
struct sdca_class_drv {
struct device *dev;
struct regmap *dev_regmap;
struct sdw_slave *sdw;
struct sdca_interrupt_info *irq_info;
struct mutex regmap_lock;
/* Serialise function initialisations */
struct mutex init_lock;
struct work_struct boot_work;
};
#endif /* __SDCA_CLASS_H__ */
@@ -0,0 +1,573 @@
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2025 Cirrus Logic, Inc. and
// Cirrus Logic International Semiconductor Ltd.
/*
* The MIPI SDCA specification is available for public downloads at
* https://www.mipi.org/mipi-sdca-v1-0-download
*/
#include <linux/auxiliary_bus.h>
#include <linux/cleanup.h>
#include <linux/minmax.h>
#include <linux/module.h>
#include <linux/pm.h>
#include <linux/pm_runtime.h>
#include <linux/soundwire/sdw.h>
#include <linux/soundwire/sdw_registers.h>
#include <sound/pcm.h>
#include <sound/sdca_asoc.h>
#include <sound/sdca_fdl.h>
#include <sound/sdca_function.h>
#include <sound/sdca_interrupts.h>
#include <sound/sdca_jack.h>
#include <sound/sdca_regmap.h>
#include <sound/sdw.h>
#include <sound/soc-component.h>
#include <sound/soc-dai.h>
#include <sound/soc.h>
#include "sdca_class.h"
#include "sdca_function_device.h"
struct class_function_drv {
struct device *dev;
struct regmap *regmap;
struct sdca_class_drv *core;
struct sdca_function_data *function;
bool suspended;
};
static void class_function_regmap_lock(void *data)
{
struct mutex *lock = data;
mutex_lock(lock);
}
static void class_function_regmap_unlock(void *data)
{
struct mutex *lock = data;
mutex_unlock(lock);
}
static bool class_function_regmap_writeable(struct device *dev, unsigned int reg)
{
struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
return sdca_regmap_writeable(drv->function, reg);
}
static bool class_function_regmap_readable(struct device *dev, unsigned int reg)
{
struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
return sdca_regmap_readable(drv->function, reg);
}
static bool class_function_regmap_volatile(struct device *dev, unsigned int reg)
{
struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
return sdca_regmap_volatile(drv->function, reg);
}
static const struct regmap_config class_function_regmap_config = {
.name = "sdca",
.reg_bits = 32,
.val_bits = 32,
.reg_format_endian = REGMAP_ENDIAN_LITTLE,
.val_format_endian = REGMAP_ENDIAN_LITTLE,
.max_register = SDW_SDCA_MAX_REGISTER,
.readable_reg = class_function_regmap_readable,
.writeable_reg = class_function_regmap_writeable,
.volatile_reg = class_function_regmap_volatile,
.cache_type = REGCACHE_MAPLE,
.lock = class_function_regmap_lock,
.unlock = class_function_regmap_unlock,
};
static int class_function_regmap_mbq_size(struct device *dev, unsigned int reg)
{
struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
return sdca_regmap_mbq_size(drv->function, reg);
}
static bool class_function_regmap_deferrable(struct device *dev, unsigned int reg)
{
struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
return sdca_regmap_deferrable(drv->function, reg);
}
static const struct regmap_sdw_mbq_cfg class_function_mbq_config = {
.mbq_size = class_function_regmap_mbq_size,
.deferrable = class_function_regmap_deferrable,
.retry_us = 1000,
.timeout_us = 10000,
};
static int class_function_startup(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
struct class_function_drv *drv = snd_soc_component_get_drvdata(dai->component);
return sdca_asoc_set_constraints(drv->dev, drv->regmap, drv->function,
substream, dai);
}
static int class_function_sdw_add_peripheral(struct snd_pcm_substream *substream,
struct snd_pcm_hw_params *params,
struct snd_soc_dai *dai)
{
struct class_function_drv *drv = snd_soc_component_get_drvdata(dai->component);
struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
struct sdw_slave *sdw = dev_to_sdw_dev(drv->dev->parent);
struct sdw_stream_config sconfig = {0};
struct sdw_port_config pconfig = {0};
int ret;
if (!sdw_stream)
return -EINVAL;
snd_sdw_params_to_config(substream, params, &sconfig, &pconfig);
/*
* FIXME: As also noted in sdca_asoc_get_port(), currently only
* a single unshared port is supported for each DAI.
*/
ret = sdca_asoc_get_port(drv->dev, drv->regmap, drv->function, dai);
if (ret < 0)
return ret;
pconfig.num = ret;
ret = sdw_stream_add_slave(sdw, &sconfig, &pconfig, 1, sdw_stream);
if (ret) {
dev_err(drv->dev, "failed to add sdw stream: %d\n", ret);
return ret;
}
return sdca_asoc_hw_params(drv->dev, drv->regmap, drv->function,
substream, params, dai);
}
static int class_function_sdw_remove_peripheral(struct snd_pcm_substream *substream,
struct snd_soc_dai *dai)
{
struct class_function_drv *drv = snd_soc_component_get_drvdata(dai->component);
struct sdw_stream_runtime *sdw_stream = snd_soc_dai_get_dma_data(dai, substream);
struct sdw_slave *sdw = dev_to_sdw_dev(drv->dev->parent);
if (!sdw_stream)
return -EINVAL;
return sdw_stream_remove_slave(sdw, sdw_stream);
}
static int class_function_sdw_set_stream(struct snd_soc_dai *dai, void *sdw_stream,
int direction)
{
snd_soc_dai_dma_data_set(dai, direction, sdw_stream);
return 0;
}
static const struct snd_soc_dai_ops class_function_sdw_ops = {
.startup = class_function_startup,
.shutdown = sdca_asoc_free_constraints,
.set_stream = class_function_sdw_set_stream,
.hw_params = class_function_sdw_add_peripheral,
.hw_free = class_function_sdw_remove_peripheral,
};
static int class_function_component_fixup_controls(struct snd_soc_component *component)
{
struct class_function_drv *drv = snd_soc_component_get_drvdata(component);
struct sdca_class_drv *core = drv->core;
return sdca_irq_populate(drv->function, component, core->irq_info);
}
static void class_function_component_remove(struct snd_soc_component *component)
{
struct class_function_drv *drv = snd_soc_component_get_drvdata(component);
struct sdca_class_drv *core = drv->core;
sdca_irq_cleanup(component->dev, drv->function, core->irq_info);
}
static int class_function_set_jack(struct snd_soc_component *component,
struct snd_soc_jack *jack, void *d)
{
struct class_function_drv *drv = snd_soc_component_get_drvdata(component);
struct sdca_class_drv *core = drv->core;
return sdca_jack_set_jack(core->irq_info, jack);
}
static const struct snd_soc_component_driver class_function_component_drv = {
.fixup_controls = class_function_component_fixup_controls,
.remove = class_function_component_remove,
.endianness = 1,
};
static int class_function_init_device(struct class_function_drv *drv,
unsigned int status)
{
int ret;
if (!(status & SDCA_CTL_ENTITY_0_FUNCTION_HAS_BEEN_RESET)) {
dev_dbg(drv->dev, "reset function device\n");
ret = sdca_reset_function(drv->dev, drv->function, drv->regmap);
if (ret)
return ret;
}
if (status & SDCA_CTL_ENTITY_0_FUNCTION_NEEDS_INITIALIZATION) {
dev_dbg(drv->dev, "write initialisation\n");
ret = sdca_regmap_write_init(drv->dev, drv->core->dev_regmap,
drv->function);
if (ret)
return ret;
}
return 0;
}
static int class_function_boot(struct class_function_drv *drv)
{
unsigned int reg = SDW_SDCA_CTL(drv->function->desc->adr,
SDCA_ENTITY_TYPE_ENTITY_0,
SDCA_CTL_ENTITY_0_FUNCTION_STATUS, 0);
unsigned int val;
int ret;
guard(mutex)(&drv->core->init_lock);
ret = regmap_read(drv->regmap, reg, &val);
if (ret < 0) {
dev_err(drv->dev, "failed to read function status: %d\n", ret);
return ret;
}
ret = class_function_init_device(drv, val);
if (ret)
return ret;
/* Start FDL process */
ret = sdca_irq_populate_early(drv->dev, drv->regmap, drv->function,
drv->core->irq_info);
if (ret)
return ret;
ret = sdca_fdl_sync(drv->dev, drv->function, drv->core->irq_info);
if (ret)
return ret;
ret = sdca_regmap_write_defaults(drv->dev, drv->regmap, drv->function);
if (ret)
return ret;
ret = regmap_write(drv->regmap, reg, 0xFF);
if (ret < 0) {
dev_err(drv->dev, "failed to clear function status: %d\n", ret);
return ret;
}
return 0;
}
static int class_function_probe(struct auxiliary_device *auxdev,
const struct auxiliary_device_id *aux_dev_id)
{
struct device *dev = &auxdev->dev;
struct sdca_class_drv *core = dev_get_drvdata(dev->parent);
struct sdca_dev *sdev = auxiliary_dev_to_sdca_dev(auxdev);
struct snd_soc_component_driver *cmp_drv;
struct snd_soc_dai_driver *dais;
struct class_function_drv *drv;
struct regmap_sdw_mbq_cfg *mbq_config;
struct regmap_config *config;
struct reg_default *defaults;
int ndefaults;
int num_dais;
int ret;
drv = devm_kzalloc(dev, sizeof(*drv), GFP_KERNEL);
if (!drv)
return -ENOMEM;
cmp_drv = devm_kmemdup(dev, &class_function_component_drv, sizeof(*cmp_drv),
GFP_KERNEL);
if (!cmp_drv)
return -ENOMEM;
config = devm_kmemdup(dev, &class_function_regmap_config, sizeof(*config),
GFP_KERNEL);
if (!config)
return -ENOMEM;
mbq_config = devm_kmemdup(dev, &class_function_mbq_config, sizeof(*mbq_config),
GFP_KERNEL);
if (!mbq_config)
return -ENOMEM;
drv->dev = dev;
drv->core = core;
drv->function = &sdev->function;
ret = sdca_parse_function(dev, core->sdw, drv->function);
if (ret)
return ret;
ndefaults = sdca_regmap_count_constants(dev, drv->function);
if (ndefaults < 0)
return ndefaults;
defaults = devm_kcalloc(dev, ndefaults, sizeof(*defaults), GFP_KERNEL);
if (!defaults)
return -ENOMEM;
ret = sdca_regmap_populate_constants(dev, drv->function, defaults);
if (ret < 0)
return ret;
regcache_sort_defaults(defaults, ndefaults);
auxiliary_set_drvdata(auxdev, drv);
config->reg_defaults = defaults;
config->num_reg_defaults = ndefaults;
config->lock_arg = &core->regmap_lock;
if (drv->function->busy_max_delay) {
mbq_config->timeout_us = drv->function->busy_max_delay;
mbq_config->retry_us = umax(drv->function->busy_max_delay / 10,
mbq_config->retry_us);
}
drv->regmap = devm_regmap_init_sdw_mbq_cfg(dev, core->sdw, config, mbq_config);
if (IS_ERR(drv->regmap))
return dev_err_probe(dev, PTR_ERR(drv->regmap),
"failed to create regmap");
switch (drv->function->desc->type) {
case SDCA_FUNCTION_TYPE_UAJ:
case SDCA_FUNCTION_TYPE_RJ:
cmp_drv->set_jack = class_function_set_jack;
break;
default:
break;
}
ret = sdca_asoc_populate_component(dev, drv->function, cmp_drv,
&dais, &num_dais,
&class_function_sdw_ops);
if (ret)
return ret;
dev_pm_set_driver_flags(dev, DPM_FLAG_NO_DIRECT_COMPLETE);
pm_runtime_set_autosuspend_delay(dev, 200);
pm_runtime_use_autosuspend(dev);
pm_runtime_set_active(dev);
pm_runtime_get_noresume(dev);
ret = devm_pm_runtime_enable(dev);
if (ret)
return ret;
ret = class_function_boot(drv);
if (ret)
return ret;
ret = devm_snd_soc_register_component(dev, cmp_drv, dais, num_dais);
if (ret)
return dev_err_probe(dev, ret, "failed to register component\n");
pm_runtime_mark_last_busy(dev);
pm_runtime_put_autosuspend(dev);
return 0;
}
static void class_function_remove(struct auxiliary_device *auxdev)
{
struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
sdca_irq_cleanup_late(drv->dev, drv->function, drv->core->irq_info);
}
static int class_function_runtime_suspend(struct device *dev)
{
struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
/*
* Whilst the driver doesn't power the chip down here, going into
* runtime suspend means the driver can't be sure the bus won't
* power down which would prevent communication with the device.
*/
regcache_cache_only(drv->regmap, true);
return 0;
}
static int class_function_runtime_resume(struct device *dev)
{
struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
int ret;
guard(mutex)(&drv->core->init_lock);
regcache_mark_dirty(drv->regmap);
regcache_cache_only(drv->regmap, false);
if (drv->suspended) {
unsigned int reg = SDW_SDCA_CTL(drv->function->desc->adr,
SDCA_ENTITY_TYPE_ENTITY_0,
SDCA_CTL_ENTITY_0_FUNCTION_STATUS, 0);
unsigned int val;
ret = regmap_read(drv->regmap, reg, &val);
if (ret < 0) {
dev_err(drv->dev, "failed to read function status: %d\n", ret);
goto err;
}
ret = class_function_init_device(drv, val);
if (ret)
goto err;
sdca_irq_enable_early(drv->function, drv->core->irq_info);
ret = sdca_fdl_sync(drv->dev, drv->function, drv->core->irq_info);
if (ret)
goto err;
sdca_irq_enable(drv->function, drv->core->irq_info);
ret = regmap_write(drv->regmap, reg, 0xFF);
if (ret < 0) {
dev_err(drv->dev, "failed to clear function status: %d\n", ret);
goto err;
}
drv->suspended = false;
}
ret = regcache_sync(drv->regmap);
if (ret) {
dev_err(drv->dev, "failed to restore register cache: %d\n", ret);
goto err;
}
return 0;
err:
regcache_cache_only(drv->regmap, true);
return ret;
}
static int class_function_suspend(struct device *dev)
{
struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
struct class_function_drv *drv = auxiliary_get_drvdata(auxdev);
int ret;
drv->suspended = true;
/* Ensure runtime resume runs on resume */
ret = pm_runtime_resume_and_get(dev);
if (ret) {
dev_err(dev, "failed to resume for suspend: %d\n", ret);
return ret;
}
sdca_irq_disable(drv->function, drv->core->irq_info);
ret = pm_runtime_force_suspend(dev);
if (ret) {
dev_err(dev, "failed to force suspend: %d\n", ret);
return ret;
}
pm_runtime_put_noidle(dev);
return 0;
}
static int class_function_resume(struct device *dev)
{
int ret;
ret = pm_runtime_force_resume(dev);
if (ret) {
dev_err(dev, "failed to force resume: %d\n", ret);
return ret;
}
return 0;
}
static const struct dev_pm_ops class_function_pm_ops = {
SYSTEM_SLEEP_PM_OPS(class_function_suspend, class_function_resume)
RUNTIME_PM_OPS(class_function_runtime_suspend,
class_function_runtime_resume, NULL)
};
static const struct auxiliary_device_id class_function_id_table[] = {
{
.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_SMART_AMP_NAME,
.driver_data = SDCA_FUNCTION_TYPE_SMART_AMP,
},
{
.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_SMART_MIC_NAME,
.driver_data = SDCA_FUNCTION_TYPE_SMART_MIC,
},
{
.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_UAJ_NAME,
.driver_data = SDCA_FUNCTION_TYPE_UAJ,
},
{
.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_HID_NAME,
.driver_data = SDCA_FUNCTION_TYPE_HID,
},
{
.name = "snd_soc_sdca." SDCA_FUNCTION_TYPE_RJ_NAME,
.driver_data = SDCA_FUNCTION_TYPE_RJ,
},
{},
};
MODULE_DEVICE_TABLE(auxiliary, class_function_id_table);
static struct auxiliary_driver class_function_drv = {
.driver = {
.name = "sdca_function",
.pm = pm_ptr(&class_function_pm_ops),
},
.probe = class_function_probe,
.remove = class_function_remove,
.id_table = class_function_id_table
};
module_auxiliary_driver(class_function_drv);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("SDCA Class Function Driver");
MODULE_IMPORT_NS("SND_SOC_SDCA");
+108
View File
@@ -0,0 +1,108 @@
// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
// Copyright(c) 2024 Intel Corporation
/*
* The MIPI SDCA specification is available for public downloads at
* https://www.mipi.org/mipi-sdca-v1-0-download
*/
#include <linux/acpi.h>
#include <linux/device.h>
#include <linux/dmi.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/soundwire/sdw.h>
#include <sound/sdca.h>
#include <sound/sdca_function.h>
void sdca_lookup_interface_revision(struct sdw_slave *slave)
{
struct fwnode_handle *fwnode = slave->dev.fwnode;
/*
* if this property is not present, then the sdca_interface_revision will
* remain zero, which will be considered as 'not defined' or 'invalid'.
*/
fwnode_property_read_u32(fwnode, "mipi-sdw-sdca-interface-revision",
&slave->sdca_data.interface_revision);
}
EXPORT_SYMBOL_NS(sdca_lookup_interface_revision, "SND_SOC_SDCA");
static void devm_acpi_table_put(void *ptr)
{
acpi_put_table((struct acpi_table_header *)ptr);
}
void sdca_lookup_swft(struct sdw_slave *slave)
{
acpi_status status;
status = acpi_get_table(ACPI_SIG_SWFT, 0,
(struct acpi_table_header **)&slave->sdca_data.swft);
if (ACPI_FAILURE(status))
dev_info(&slave->dev, "SWFT not available\n");
else
devm_add_action_or_reset(&slave->dev, devm_acpi_table_put,
slave->sdca_data.swft);
}
EXPORT_SYMBOL_NS(sdca_lookup_swft, "SND_SOC_SDCA");
static bool sdca_device_quirk_rt712_vb(struct sdw_slave *slave)
{
struct sdw_slave_id *id = &slave->id;
int i;
/*
* The RT712_VA relies on the v06r04 draft, and the
* RT712_VB on a more recent v08r01 draft.
*/
if (slave->sdca_data.interface_revision < 0x0801)
return false;
if (id->mfg_id != 0x025d)
return false;
if (id->part_id != 0x712 &&
id->part_id != 0x713 &&
id->part_id != 0x716 &&
id->part_id != 0x717)
return false;
for (i = 0; i < slave->sdca_data.num_functions; i++) {
if (slave->sdca_data.function[i].type == SDCA_FUNCTION_TYPE_SMART_MIC)
return true;
}
return false;
}
static bool sdca_device_quirk_skip_func_type_patching(struct sdw_slave *slave)
{
const char *vendor, *sku;
vendor = dmi_get_system_info(DMI_SYS_VENDOR);
sku = dmi_get_system_info(DMI_PRODUCT_SKU);
if (vendor && sku &&
!strcmp(vendor, "Dell Inc.") &&
(!strcmp(sku, "0C62") || !strcmp(sku, "0C63") || !strcmp(sku, "0C6B")) &&
slave->sdca_data.interface_revision == 0x061c &&
slave->id.mfg_id == 0x01fa && slave->id.part_id == 0x4243)
return true;
return false;
}
bool sdca_device_quirk_match(struct sdw_slave *slave, enum sdca_quirk quirk)
{
switch (quirk) {
case SDCA_QUIRKS_RT712_VB:
return sdca_device_quirk_rt712_vb(slave);
case SDCA_QUIRKS_SKIP_FUNC_TYPE_PATCHING:
return sdca_device_quirk_skip_func_type_patching(slave);
default:
break;
}
return false;
}
EXPORT_SYMBOL_NS(sdca_device_quirk_match, "SND_SOC_SDCA");
+513
View File
@@ -0,0 +1,513 @@
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2025 Cirrus Logic, Inc. and
// Cirrus Logic International Semiconductor Ltd.
/*
* The MIPI SDCA specification is available for public downloads at
* https://www.mipi.org/mipi-sdca-v1-0-download
*/
#include <linux/acpi.h>
#include <linux/device.h>
#include <linux/dev_printk.h>
#include <linux/dmi.h>
#include <linux/firmware.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/sprintf.h>
#include <linux/soundwire/sdw.h>
#include <linux/soundwire/sdw_registers.h>
#include <sound/sdca.h>
#include <sound/sdca_fdl.h>
#include <sound/sdca_function.h>
#include <sound/sdca_interrupts.h>
#include <sound/sdca_ump.h>
/**
* sdca_reset_function - send an SDCA function reset
* @dev: Device pointer for error messages.
* @function: Pointer to the SDCA Function.
* @regmap: Pointer to the SDCA Function regmap.
*
* Return: Zero on success or a negative error code.
*/
int sdca_reset_function(struct device *dev, struct sdca_function_data *function,
struct regmap *regmap)
{
unsigned int reg = SDW_SDCA_CTL(function->desc->adr,
SDCA_ENTITY_TYPE_ENTITY_0,
SDCA_CTL_ENTITY_0_FUNCTION_ACTION, 0);
unsigned int val, poll_us;
int ret;
ret = regmap_write(regmap, reg, SDCA_CTL_ENTITY_0_RESET_FUNCTION_NOW);
if (ret) // Allowed for function reset to not be implemented
return 0;
/*
* Poll up to 16 times but no more than once per ms, these are just
* arbitrarily selected values, so may be fine tuned in future.
*/
poll_us = umin(function->reset_max_delay >> 4, 1000);
ret = regmap_read_poll_timeout(regmap, reg, val, !val, poll_us,
function->reset_max_delay);
if (ret) {
dev_err(dev, "Failed waiting for function reset: %d\n", ret);
return ret;
}
return 0;
}
EXPORT_SYMBOL_NS(sdca_reset_function, "SND_SOC_SDCA");
/**
* sdca_fdl_sync - wait for a function to finish FDL
* @dev: Device pointer for error messages.
* @function: Pointer to the SDCA Function.
* @info: Pointer to the SDCA interrupt info for this device.
*
* Return: Zero on success or a negative error code.
*/
int sdca_fdl_sync(struct device *dev, struct sdca_function_data *function,
struct sdca_interrupt_info *info)
{
static const int fdl_retries = 6;
unsigned long begin_timeout = msecs_to_jiffies(100);
unsigned long done_timeout = msecs_to_jiffies(4000);
int nfdl;
int i, j;
for (i = 0; i < fdl_retries; i++) {
nfdl = 0;
for (j = 0; j < SDCA_MAX_INTERRUPTS; j++) {
struct sdca_interrupt *interrupt = &info->irqs[j];
struct fdl_state *fdl_state;
unsigned long time;
if (interrupt->function != function ||
!interrupt->entity || !interrupt->control ||
interrupt->entity->type != SDCA_ENTITY_TYPE_XU ||
interrupt->control->sel != SDCA_CTL_XU_FDL_CURRENTOWNER)
continue;
fdl_state = interrupt->priv;
nfdl++;
/*
* Looking for timeout without any new FDL requests
* to imply the device has completed initial
* firmware setup. Alas the specification doesn't
* have any mechanism to detect this.
*/
time = wait_for_completion_timeout(&fdl_state->begin,
begin_timeout);
if (!time) {
dev_dbg(dev, "no new FDL starts\n");
nfdl--;
continue;
}
time = wait_for_completion_timeout(&fdl_state->done,
done_timeout);
if (!time) {
dev_err(dev, "timed out waiting for FDL to complete\n");
goto error;
}
}
if (!nfdl)
return 0;
}
dev_err(dev, "too many FDL requests\n");
error:
for (j = 0; j < SDCA_MAX_INTERRUPTS; j++) {
struct sdca_interrupt *interrupt = &info->irqs[j];
struct fdl_state *fdl_state;
if (interrupt->function != function ||
!interrupt->entity || !interrupt->control ||
interrupt->entity->type != SDCA_ENTITY_TYPE_XU ||
interrupt->control->sel != SDCA_CTL_XU_FDL_CURRENTOWNER)
continue;
disable_irq(interrupt->irq);
fdl_state = interrupt->priv;
sdca_ump_cancel_timeout(&fdl_state->timeout);
}
return -ETIMEDOUT;
}
EXPORT_SYMBOL_NS_GPL(sdca_fdl_sync, "SND_SOC_SDCA");
static char *fdl_get_sku_filename(struct device *dev,
struct sdca_fdl_file *fdl_file)
{
struct device *parent = dev;
const char *product_vendor;
const char *product_sku;
/*
* Try to find pci_dev manually because the card may not be ready to be
* used for snd_soc_card_get_pci_ssid yet
*/
while (parent) {
if (dev_is_pci(parent)) {
struct pci_dev *pci_dev = to_pci_dev(parent);
return kasprintf(GFP_KERNEL, "sdca/%x/%x/%x/%x.bin",
fdl_file->vendor_id,
pci_dev->subsystem_vendor,
pci_dev->subsystem_device,
fdl_file->file_id);
} else {
parent = parent->parent;
}
}
product_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
if (!product_vendor || !strcmp(product_vendor, "Default string"))
product_vendor = dmi_get_system_info(DMI_BOARD_VENDOR);
if (!product_vendor || !strcmp(product_vendor, "Default string"))
product_vendor = dmi_get_system_info(DMI_CHASSIS_VENDOR);
if (!product_vendor)
product_vendor = "unknown";
product_sku = dmi_get_system_info(DMI_PRODUCT_SKU);
if (!product_sku || !strcmp(product_sku, "Default string"))
product_sku = dmi_get_system_info(DMI_PRODUCT_NAME);
if (!product_sku)
product_sku = "unknown";
return kasprintf(GFP_KERNEL, "sdca/%x/%s/%s/%x.bin", fdl_file->vendor_id,
product_vendor, product_sku, fdl_file->file_id);
}
static int fdl_load_file(struct sdca_interrupt *interrupt,
struct sdca_fdl_set *set, int file_index)
{
struct device *dev = interrupt->dev;
struct sdca_fdl_data *fdl_data = &interrupt->function->fdl_data;
const struct firmware *firmware = NULL;
struct acpi_sw_file *swf = NULL, *tmp;
struct sdca_fdl_file *fdl_file;
char *disk_filename;
int ret;
int i;
if (!set) {
dev_err(dev, "request to load SWF with no set\n");
return -EINVAL;
}
fdl_file = &set->files[file_index];
if (fdl_data->swft) {
tmp = fdl_data->swft->files;
for (i = 0; i < fdl_data->swft->header.length; i += tmp->file_length,
tmp = ACPI_ADD_PTR(struct acpi_sw_file, tmp, tmp->file_length)) {
if (tmp->vendor_id == fdl_file->vendor_id &&
tmp->file_id == fdl_file->file_id) {
dev_dbg(dev, "located SWF in ACPI: %x-%x-%x\n",
tmp->vendor_id, tmp->file_id,
tmp->file_version);
swf = tmp;
break;
}
}
}
disk_filename = fdl_get_sku_filename(dev, fdl_file);
if (!disk_filename)
return -ENOMEM;
dev_dbg(dev, "FDL disk filename: %s\n", disk_filename);
ret = firmware_request_nowarn(&firmware, disk_filename, dev);
kfree(disk_filename);
if (ret) {
disk_filename = kasprintf(GFP_KERNEL, "sdca/%x/%x.bin",
fdl_file->vendor_id, fdl_file->file_id);
if (!disk_filename)
return -ENOMEM;
dev_dbg(dev, "FDL disk filename: %s\n", disk_filename);
ret = firmware_request_nowarn(&firmware, disk_filename, dev);
kfree(disk_filename);
}
if (!ret) {
tmp = (struct acpi_sw_file *)&firmware->data[0];
if (firmware->size < sizeof(*tmp) ||
tmp->file_length != firmware->size) {
dev_err(dev, "bad disk SWF size\n");
} else if (!swf || swf->file_version <= tmp->file_version) {
dev_dbg(dev, "using SWF from disk\n");
swf = tmp;
}
}
if (!swf) {
dev_err(dev, "failed to locate SWF\n");
ret = -ENOENT;
goto error;
}
dev_info(dev, "loading SWF: %x-%x-%x\n",
swf->vendor_id, swf->file_id, swf->file_version);
ret = sdca_ump_write_message(dev, interrupt->device_regmap,
interrupt->function_regmap,
interrupt->function, interrupt->entity,
SDCA_CTL_XU_FDL_MESSAGEOFFSET, fdl_file->fdl_offset,
SDCA_CTL_XU_FDL_MESSAGELENGTH, swf->data,
swf->file_length - offsetof(struct acpi_sw_file, data));
error:
release_firmware(firmware);
return ret;
}
static struct sdca_fdl_set *fdl_get_set(struct sdca_interrupt *interrupt)
{
struct device *dev = interrupt->dev;
struct sdca_fdl_data *fdl_data = &interrupt->function->fdl_data;
struct sdca_entity *xu = interrupt->entity;
struct sdca_control_range *range;
unsigned int val;
int i, ret;
ret = regmap_read(interrupt->function_regmap,
SDW_SDCA_CTL(interrupt->function->desc->adr, xu->id,
SDCA_CTL_XU_FDL_SET_INDEX, 0),
&val);
if (ret < 0) {
dev_err(dev, "failed to read FDL set index: %d\n", ret);
return NULL;
}
range = sdca_selector_find_range(dev, xu, SDCA_CTL_XU_FDL_SET_INDEX,
SDCA_FDL_SET_INDEX_NCOLS, 0);
val = sdca_range_search(range, SDCA_FDL_SET_INDEX_SET_NUMBER,
val, SDCA_FDL_SET_INDEX_FILE_SET_ID);
for (i = 0; i < fdl_data->num_sets; i++) {
if (fdl_data->sets[i].id == val)
return &fdl_data->sets[i];
}
dev_err(dev, "invalid fileset id: %d\n", val);
return NULL;
}
static void fdl_end(struct sdca_interrupt *interrupt)
{
struct fdl_state *fdl_state = interrupt->priv;
if (!fdl_state->set)
return;
fdl_state->set = NULL;
pm_runtime_put(interrupt->dev);
complete(&fdl_state->done);
dev_dbg(interrupt->dev, "completed FDL process\n");
}
static void sdca_fdl_timeout_work(struct work_struct *work)
{
struct fdl_state *fdl_state = container_of(work, struct fdl_state,
timeout.work);
struct sdca_interrupt *interrupt = fdl_state->interrupt;
struct device *dev = interrupt->dev;
dev_err(dev, "FDL transaction timed out\n");
guard(mutex)(&fdl_state->lock);
fdl_end(interrupt);
sdca_reset_function(dev, interrupt->function, interrupt->function_regmap);
}
static int fdl_status_process(struct sdca_interrupt *interrupt, unsigned int status)
{
struct fdl_state *fdl_state = interrupt->priv;
int ret;
switch (status) {
case SDCA_CTL_XU_FDLD_NEEDS_SET:
dev_dbg(interrupt->dev, "starting FDL process...\n");
pm_runtime_get(interrupt->dev);
complete(&fdl_state->begin);
fdl_state->file_index = 0;
fdl_state->set = fdl_get_set(interrupt);
fallthrough;
case SDCA_CTL_XU_FDLD_MORE_FILES_OK:
ret = fdl_load_file(interrupt, fdl_state->set, fdl_state->file_index);
if (ret) {
fdl_end(interrupt);
return SDCA_CTL_XU_FDLH_REQ_ABORT;
}
return SDCA_CTL_XU_FDLH_FILE_AVAILABLE;
case SDCA_CTL_XU_FDLD_FILE_OK:
if (!fdl_state->set) {
fdl_end(interrupt);
return SDCA_CTL_XU_FDLH_REQ_ABORT;
}
fdl_state->file_index++;
if (fdl_state->file_index < fdl_state->set->num_files)
return SDCA_CTL_XU_FDLH_MORE_FILES;
fallthrough;
case SDCA_CTL_XU_FDLD_COMPLETE:
fdl_end(interrupt);
return SDCA_CTL_XU_FDLH_COMPLETE;
default:
fdl_end(interrupt);
if (status & SDCA_CTL_XU_FDLD_REQ_RESET)
return SDCA_CTL_XU_FDLH_RESET_ACK;
else if (status & SDCA_CTL_XU_FDLD_REQ_ABORT)
return SDCA_CTL_XU_FDLH_COMPLETE;
dev_err(interrupt->dev, "invalid FDL status: %x\n", status);
return -EINVAL;
}
}
/**
* sdca_fdl_process - Process the FDL state machine
* @interrupt: SDCA interrupt structure
*
* Based on section 13.2.5 Flow Diagram for File Download, Host side.
*
* Return: Zero on success or a negative error code.
*/
int sdca_fdl_process(struct sdca_interrupt *interrupt)
{
struct device *dev = interrupt->dev;
struct sdca_entity_xu *xu = &interrupt->entity->xu;
struct fdl_state *fdl_state = interrupt->priv;
unsigned int reg, status;
int response, ret;
ret = sdca_ump_get_owner_host(dev, interrupt->function_regmap,
interrupt->function, interrupt->entity,
interrupt->control);
if (ret)
goto reset_function;
sdca_ump_cancel_timeout(&fdl_state->timeout);
scoped_guard(mutex, &fdl_state->lock) {
reg = SDW_SDCA_CTL(interrupt->function->desc->adr,
interrupt->entity->id, SDCA_CTL_XU_FDL_STATUS, 0);
ret = regmap_read(interrupt->function_regmap, reg, &status);
if (ret < 0) {
dev_err(dev, "failed to read FDL status: %d\n", ret);
return ret;
}
dev_dbg(dev, "FDL status: %#x\n", status);
ret = fdl_status_process(interrupt, status);
if (ret < 0)
goto reset_function;
response = ret;
dev_dbg(dev, "FDL response: %#x\n", response);
ret = regmap_write(interrupt->function_regmap, reg,
response | (status & ~SDCA_CTL_XU_FDLH_MASK));
if (ret < 0) {
dev_err(dev, "failed to set FDL status signal: %d\n", ret);
return ret;
}
ret = sdca_ump_set_owner_device(dev, interrupt->function_regmap,
interrupt->function,
interrupt->entity,
interrupt->control);
if (ret)
return ret;
switch (response) {
case SDCA_CTL_XU_FDLH_RESET_ACK:
dev_dbg(dev, "FDL request reset\n");
switch (xu->reset_mechanism) {
default:
dev_warn(dev, "Requested reset mechanism not implemented\n");
fallthrough;
case SDCA_XU_RESET_FUNCTION:
goto reset_function;
}
case SDCA_CTL_XU_FDLH_COMPLETE:
if (status & SDCA_CTL_XU_FDLD_REQ_ABORT ||
status == SDCA_CTL_XU_FDLD_COMPLETE)
return 0;
fallthrough;
default:
sdca_ump_schedule_timeout(&fdl_state->timeout, xu->max_delay);
return 0;
}
}
reset_function:
sdca_reset_function(dev, interrupt->function, interrupt->function_regmap);
return ret;
}
EXPORT_SYMBOL_NS_GPL(sdca_fdl_process, "SND_SOC_SDCA");
/**
* sdca_fdl_alloc_state - allocate state for an FDL interrupt
* @interrupt: SDCA interrupt structure.
*
* Return: Zero on success or a negative error code.
*/
int sdca_fdl_alloc_state(struct sdca_interrupt *interrupt)
{
struct fdl_state *fdl_state;
fdl_state = kzalloc_obj(*fdl_state);
if (!fdl_state)
return -ENOMEM;
INIT_DELAYED_WORK(&fdl_state->timeout, sdca_fdl_timeout_work);
init_completion(&fdl_state->begin);
init_completion(&fdl_state->done);
mutex_init(&fdl_state->lock);
fdl_state->interrupt = interrupt;
interrupt->priv = fdl_state;
return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_fdl_alloc_state, "SND_SOC_SDCA");
/**
* sdca_fdl_free_state - free state for an FDL interrupt
* @interrupt: SDCA interrupt structure.
*/
void sdca_fdl_free_state(struct sdca_interrupt *interrupt)
{
kfree(interrupt->priv);
}
EXPORT_SYMBOL_NS_GPL(sdca_fdl_free_state, "SND_SOC_SDCA");
@@ -0,0 +1,135 @@
// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
// Copyright(c) 2024 Intel Corporation.
/*
* SDCA Function Device management
*/
#include <linux/acpi.h>
#include <linux/module.h>
#include <linux/auxiliary_bus.h>
#include <linux/soundwire/sdw.h>
#include <sound/sdca.h>
#include <sound/sdca_function.h>
#include "sdca_function_device.h"
/*
* A SoundWire device can have multiple SDCA functions identified by
* their type and ADR. there can be multiple SoundWire devices per
* link, or multiple devices spread across multiple links. An IDA is
* required to identify each instance.
*/
static DEFINE_IDA(sdca_function_ida);
static void sdca_dev_release(struct device *dev)
{
struct auxiliary_device *auxdev = to_auxiliary_dev(dev);
struct sdca_dev *sdev = auxiliary_dev_to_sdca_dev(auxdev);
ida_free(&sdca_function_ida, auxdev->id);
kfree(sdev);
}
/* alloc, init and add link devices */
static struct sdca_dev *sdca_dev_register(struct device *parent,
struct sdca_function_desc *function_desc)
{
struct sdca_dev *sdev;
struct auxiliary_device *auxdev;
int ret;
int rc;
sdev = kzalloc_obj(*sdev);
if (!sdev)
return ERR_PTR(-ENOMEM);
auxdev = &sdev->auxdev;
auxdev->name = function_desc->name;
auxdev->dev.parent = parent;
auxdev->dev.fwnode = function_desc->node;
auxdev->dev.release = sdca_dev_release;
sdev->function.desc = function_desc;
rc = ida_alloc(&sdca_function_ida, GFP_KERNEL);
if (rc < 0) {
kfree(sdev);
return ERR_PTR(rc);
}
auxdev->id = rc;
/* now follow the two-step init/add sequence */
ret = auxiliary_device_init(auxdev);
if (ret < 0) {
dev_err(parent, "failed to initialize SDCA function dev %s\n",
function_desc->name);
ida_free(&sdca_function_ida, auxdev->id);
kfree(sdev);
return ERR_PTR(ret);
}
ret = auxiliary_device_add(auxdev);
if (ret < 0) {
dev_err(parent, "failed to add SDCA function dev %s\n",
sdev->auxdev.name);
/* sdev will be freed with the put_device() and .release sequence */
auxiliary_device_uninit(&sdev->auxdev);
return ERR_PTR(ret);
}
return sdev;
}
static void sdca_dev_unregister(struct sdca_dev *sdev)
{
if (!sdev)
return;
auxiliary_device_delete(&sdev->auxdev);
auxiliary_device_uninit(&sdev->auxdev);
}
int sdca_dev_register_functions(struct sdw_slave *slave)
{
struct sdca_device_data *sdca_data = &slave->sdca_data;
int i;
int ret;
for (i = 0; i < sdca_data->num_functions; i++) {
struct sdca_dev *func_dev;
func_dev = sdca_dev_register(&slave->dev,
&sdca_data->function[i]);
if (IS_ERR(func_dev)) {
ret = PTR_ERR(func_dev);
/*
* Unregister functions that were successfully
* registered before this failure. This also
* sets func_dev to NULL so the caller will not
* try to unregister them again.
*/
sdca_dev_unregister_functions(slave);
return ret;
}
sdca_data->function[i].func_dev = func_dev;
}
return 0;
}
EXPORT_SYMBOL_NS(sdca_dev_register_functions, "SND_SOC_SDCA");
void sdca_dev_unregister_functions(struct sdw_slave *slave)
{
struct sdca_device_data *sdca_data = &slave->sdca_data;
int i;
for (i = 0; i < sdca_data->num_functions; i++) {
if (!sdca_data->function[i].func_dev)
continue;
sdca_dev_unregister(sdca_data->function[i].func_dev);
sdca_data->function[i].func_dev = NULL;
}
}
EXPORT_SYMBOL_NS(sdca_dev_unregister_functions, "SND_SOC_SDCA");
@@ -0,0 +1,15 @@
/* SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) */
/* Copyright(c) 2024 Intel Corporation. */
#ifndef __SDCA_FUNCTION_DEVICE_H
#define __SDCA_FUNCTION_DEVICE_H
struct sdca_dev {
struct auxiliary_device auxdev;
struct sdca_function_data function;
};
#define auxiliary_dev_to_sdca_dev(auxiliary_dev) \
container_of(auxiliary_dev, struct sdca_dev, auxdev)
#endif
File diff suppressed because it is too large Load Diff
+168
View File
@@ -0,0 +1,168 @@
// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
/*
* The MIPI SDCA specification is available for public downloads at
* https://www.mipi.org/mipi-sdca-v1-0-download
*/
#include <linux/acpi.h>
#include <linux/byteorder/generic.h>
#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/dev_printk.h>
#include <linux/hid.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/soundwire/sdw.h>
#include <linux/types.h>
#include <sound/sdca.h>
#include <sound/sdca_function.h>
#include <sound/sdca_hid.h>
#include <sound/sdca_interrupts.h>
#include <sound/sdca_ump.h>
static int sdwhid_parse(struct hid_device *hid)
{
struct sdca_entity *entity = hid->driver_data;
unsigned int rsize;
int ret;
rsize = le16_to_cpu(entity->hide.hid_desc.rpt_desc.wDescriptorLength);
if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
dev_err(&hid->dev, "invalid size of report descriptor (%u)\n", rsize);
return -EINVAL;
}
ret = hid_parse_report(hid, entity->hide.hid_report_desc, rsize);
if (!ret)
return 0;
dev_err(&hid->dev, "parsing report descriptor failed\n");
return ret;
}
static int sdwhid_start(struct hid_device *hid)
{
return 0;
}
static void sdwhid_stop(struct hid_device *hid)
{
}
static int sdwhid_raw_request(struct hid_device *hid, unsigned char reportnum,
__u8 *buf, size_t len, unsigned char rtype, int reqtype)
{
switch (reqtype) {
case HID_REQ_GET_REPORT:
/* not implemented yet */
return 0;
case HID_REQ_SET_REPORT:
/* not implemented yet */
return 0;
default:
return -EIO;
}
}
static int sdwhid_open(struct hid_device *hid)
{
return 0;
}
static void sdwhid_close(struct hid_device *hid)
{
}
static const struct hid_ll_driver sdw_hid_driver = {
.parse = sdwhid_parse,
.start = sdwhid_start,
.stop = sdwhid_stop,
.open = sdwhid_open,
.close = sdwhid_close,
.raw_request = sdwhid_raw_request,
};
int sdca_add_hid_device(struct device *dev, struct sdw_slave *sdw,
struct sdca_entity *entity)
{
struct sdw_bus *bus = sdw->bus;
struct hid_device *hid;
int ret;
hid = hid_allocate_device();
if (IS_ERR(hid))
return PTR_ERR(hid);
hid->ll_driver = &sdw_hid_driver;
hid->dev.parent = dev;
hid->bus = BUS_SDW;
hid->version = le16_to_cpu(entity->hide.hid_desc.bcdHID);
snprintf(hid->name, sizeof(hid->name),
"HID sdw:%01x:%01x:%04x:%04x:%02x",
bus->controller_id, bus->link_id, sdw->id.mfg_id,
sdw->id.part_id, sdw->id.class_id);
snprintf(hid->phys, sizeof(hid->phys), "%s", dev->bus->name);
hid->driver_data = entity;
ret = hid_add_device(hid);
if (ret && ret != -ENODEV) {
dev_err(dev, "can't add hid device: %d\n", ret);
hid_destroy_device(hid);
return ret;
}
entity->hide.hid = hid;
return 0;
}
EXPORT_SYMBOL_NS(sdca_add_hid_device, "SND_SOC_SDCA");
/**
* sdca_hid_process_report - read a HID event from the device and report
* @interrupt: Pointer to the SDCA interrupt information structure.
*
* Return: Zero on success, and a negative error code on failure.
*/
int sdca_hid_process_report(struct sdca_interrupt *interrupt)
{
struct device *dev = interrupt->dev;
struct hid_device *hid = interrupt->entity->hide.hid;
void *val __free(kfree) = NULL;
int len, ret;
ret = sdca_ump_get_owner_host(dev, interrupt->function_regmap,
interrupt->function, interrupt->entity,
interrupt->control);
if (ret)
return ret;
len = sdca_ump_read_message(dev, interrupt->device_regmap,
interrupt->function_regmap,
interrupt->function, interrupt->entity,
SDCA_CTL_HIDE_HIDTX_MESSAGEOFFSET,
SDCA_CTL_HIDE_HIDTX_MESSAGELENGTH, &val);
if (len < 0)
return len;
ret = sdca_ump_set_owner_device(dev, interrupt->function_regmap,
interrupt->function, interrupt->entity,
interrupt->control);
if (ret)
return ret;
ret = hid_input_report(hid, HID_INPUT_REPORT, val, len, true);
if (ret < 0) {
dev_err(dev, "failed to report hid event: %d\n", ret);
return ret;
}
return 0;
}
EXPORT_SYMBOL_NS(sdca_hid_process_report, "SND_SOC_SDCA");
@@ -0,0 +1,737 @@
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2025 Cirrus Logic, Inc. and
// Cirrus Logic International Semiconductor Ltd.
/*
* The MIPI SDCA specification is available for public downloads at
* https://www.mipi.org/mipi-sdca-v1-0-download
*/
#include <linux/bitmap.h>
#include <linux/bits.h>
#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/dev_printk.h>
#include <linux/interrupt.h>
#include <linux/pm_runtime.h>
#include <linux/regmap.h>
#include <linux/soundwire/sdw.h>
#include <linux/soundwire/sdw_registers.h>
#include <sound/sdca.h>
#include <sound/sdca_fdl.h>
#include <sound/sdca_function.h>
#include <sound/sdca_hid.h>
#include <sound/sdca_interrupts.h>
#include <sound/sdca_jack.h>
#include <sound/sdca_ump.h>
#include <sound/soc-component.h>
#include <sound/soc.h>
#define IRQ_SDCA(number) REGMAP_IRQ_REG(number, ((number) / BITS_PER_BYTE), \
SDW_SCP_SDCA_INTMASK_SDCA_##number)
static const struct regmap_irq regmap_irqs[SDCA_MAX_INTERRUPTS] = {
IRQ_SDCA(0),
IRQ_SDCA(1),
IRQ_SDCA(2),
IRQ_SDCA(3),
IRQ_SDCA(4),
IRQ_SDCA(5),
IRQ_SDCA(6),
IRQ_SDCA(7),
IRQ_SDCA(8),
IRQ_SDCA(9),
IRQ_SDCA(10),
IRQ_SDCA(11),
IRQ_SDCA(12),
IRQ_SDCA(13),
IRQ_SDCA(14),
IRQ_SDCA(15),
IRQ_SDCA(16),
IRQ_SDCA(17),
IRQ_SDCA(18),
IRQ_SDCA(19),
IRQ_SDCA(20),
IRQ_SDCA(21),
IRQ_SDCA(22),
IRQ_SDCA(23),
IRQ_SDCA(24),
IRQ_SDCA(25),
IRQ_SDCA(26),
IRQ_SDCA(27),
IRQ_SDCA(28),
IRQ_SDCA(29),
IRQ_SDCA(30),
};
static const struct regmap_irq_chip sdca_irq_chip = {
.name = "sdca_irq",
.status_base = SDW_SCP_SDCA_INT1,
.unmask_base = SDW_SCP_SDCA_INTMASK1,
.ack_base = SDW_SCP_SDCA_INT1,
.num_regs = 4,
.irqs = regmap_irqs,
.num_irqs = SDCA_MAX_INTERRUPTS,
.runtime_pm = true,
};
static irqreturn_t base_handler(int irq, void *data)
{
struct sdca_interrupt *interrupt = data;
struct device *dev = interrupt->dev;
dev_info(dev, "%s irq without full handling\n", interrupt->name);
return IRQ_HANDLED;
}
static irqreturn_t function_status_handler(int irq, void *data)
{
struct sdca_interrupt *interrupt = data;
struct device *dev = interrupt->dev;
irqreturn_t irqret = IRQ_NONE;
unsigned int reg, val;
unsigned long status;
unsigned int mask;
int ret;
ret = pm_runtime_get_sync(dev);
if (ret < 0) {
dev_err(dev, "failed to resume for function status: %d\n", ret);
goto error;
}
reg = SDW_SDCA_CTL(interrupt->function->desc->adr, interrupt->entity->id,
interrupt->control->sel, 0);
ret = regmap_read(interrupt->function_regmap, reg, &val);
if (ret < 0) {
dev_err(dev, "failed to read function status: %d\n", ret);
goto error;
}
dev_dbg(dev, "function status: %#x\n", val);
status = val;
for_each_set_bit(mask, &status, BITS_PER_BYTE) {
switch (BIT(mask)) {
case SDCA_CTL_ENTITY_0_FUNCTION_NEEDS_INITIALIZATION:
/*
* FIXME: Should this do init writes?
*
* Currently init writes/cache sync are done from the suspend/resume
* infrastructure. It is unclear in what situations one would receive this
* IRQ outside of that flow. Presumably it would be something like the chip
* crashing. In that case however doing the init writes and a cache sync might
* not be sufficient, for example if the failure was during audio playback
* there could be ordering constraints on the register writes to restore the
* state that are not handled by a simple cache sync.
*/
break;
case SDCA_CTL_ENTITY_0_FUNCTION_FAULT:
dev_err(dev, "function fault\n");
break;
case SDCA_CTL_ENTITY_0_UMP_SEQUENCE_FAULT:
dev_err(dev, "ump sequence fault\n");
break;
case SDCA_CTL_ENTITY_0_FUNCTION_BUSY:
dev_info(dev, "unexpected function busy\n");
break;
case SDCA_CTL_ENTITY_0_DEVICE_NEWLY_ATTACHED:
case SDCA_CTL_ENTITY_0_INTS_DISABLED_ABNORMALLY:
case SDCA_CTL_ENTITY_0_STREAMING_STOPPED_ABNORMALLY:
case SDCA_CTL_ENTITY_0_FUNCTION_HAS_BEEN_RESET:
break;
}
}
ret = regmap_write(interrupt->function_regmap, reg, val & 0x7F);
if (ret < 0) {
dev_err(dev, "failed to clear function status: %d\n", ret);
goto error;
}
irqret = IRQ_HANDLED;
error:
pm_runtime_put(dev);
return irqret;
}
static irqreturn_t detected_mode_handler(int irq, void *data)
{
struct sdca_interrupt *interrupt = data;
struct device *dev = interrupt->dev;
irqreturn_t irqret = IRQ_NONE;
int ret;
ret = pm_runtime_get_sync(dev);
if (ret < 0) {
dev_err(dev, "failed to resume for detected mode: %d\n", ret);
goto error;
}
ret = sdca_jack_process(interrupt);
if (ret)
goto error;
irqret = IRQ_HANDLED;
error:
pm_runtime_put(dev);
return irqret;
}
static irqreturn_t hid_handler(int irq, void *data)
{
struct sdca_interrupt *interrupt = data;
struct device *dev = interrupt->dev;
irqreturn_t irqret = IRQ_NONE;
int ret;
ret = pm_runtime_get_sync(dev);
if (ret < 0) {
dev_err(dev, "failed to resume for hid: %d\n", ret);
goto error;
}
ret = sdca_hid_process_report(interrupt);
if (ret)
goto error;
irqret = IRQ_HANDLED;
error:
pm_runtime_put(dev);
return irqret;
}
#ifdef CONFIG_PM_SLEEP
static bool no_pm_in_progress(struct device *dev)
{
return completion_done(&dev->power.completion);
}
#else
static bool no_pm_in_progress(struct device *dev)
{
return true;
}
#endif
static irqreturn_t fdl_owner_handler(int irq, void *data)
{
struct sdca_interrupt *interrupt = data;
struct device *dev = interrupt->dev;
irqreturn_t irqret = IRQ_NONE;
int ret;
/*
* FDL has to run from the system resume handler, at which point
* we can't wait for the pm runtime.
*/
if (no_pm_in_progress(dev)) {
ret = pm_runtime_get_sync(dev);
if (ret < 0) {
dev_err(dev, "failed to resume for fdl: %d\n", ret);
goto error;
}
}
ret = sdca_fdl_process(interrupt);
if (ret)
goto error;
irqret = IRQ_HANDLED;
error:
if (no_pm_in_progress(dev))
pm_runtime_put(dev);
return irqret;
}
static int sdca_irq_request_locked(struct device *dev,
struct sdca_interrupt_info *info,
int sdca_irq, const char *name,
irq_handler_t handler, void *data)
{
int irq;
int ret;
irq = regmap_irq_get_virq(info->irq_data, sdca_irq);
if (irq < 0)
return irq;
ret = request_threaded_irq(irq, NULL, handler, IRQF_ONESHOT, name, data);
if (ret)
return ret;
info->irqs[sdca_irq].irq = irq;
dev_dbg(dev, "requested irq %d for %s\n", irq, name);
return 0;
}
static void sdca_irq_free_locked(struct device *dev, struct sdca_interrupt_info *info,
int sdca_irq, const char *name, void *data)
{
int irq;
irq = regmap_irq_get_virq(info->irq_data, sdca_irq);
if (irq < 0)
return;
free_irq(irq, data);
info->irqs[sdca_irq].irq = 0;
dev_dbg(dev, "freed irq %d for %s\n", irq, name);
}
/**
* sdca_irq_request - request an individual SDCA interrupt
* @dev: Pointer to the struct device against which things should be allocated.
* @info: Pointer to the interrupt information structure.
* @sdca_irq: SDCA interrupt position.
* @name: Name to be given to the IRQ.
* @handler: A callback thread function to be called for the IRQ.
* @data: Private data pointer that will be passed to the handler.
*
* Typically this is handled internally by sdca_irq_populate, however if
* a device requires custom IRQ handling this can be called manually before
* calling sdca_irq_populate, which will then skip that IRQ whilst processing.
*
* Return: Zero on success, and a negative error code on failure.
*/
int sdca_irq_request(struct device *dev, struct sdca_interrupt_info *info,
int sdca_irq, const char *name, irq_handler_t handler,
void *data)
{
int ret;
if (sdca_irq < 0 || sdca_irq >= SDCA_MAX_INTERRUPTS) {
dev_err(dev, "bad irq request: %d\n", sdca_irq);
return -EINVAL;
}
guard(mutex)(&info->irq_lock);
ret = sdca_irq_request_locked(dev, info, sdca_irq, name, handler, data);
if (ret) {
dev_err(dev, "failed to request irq %s: %d\n", name, ret);
return ret;
}
return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_irq_request, "SND_SOC_SDCA");
/**
* sdca_irq_free - free an individual SDCA interrupt
* @dev: Pointer to the struct device.
* @info: Pointer to the interrupt information structure.
* @sdca_irq: SDCA interrupt position.
* @name: Name to be given to the IRQ.
* @data: Private data pointer that will be passed to the handler.
*
* Typically this is handled internally by sdca_irq_cleanup, however if
* a device requires custom IRQ handling this can be called manually before
* calling sdca_irq_cleanup, which will then skip that IRQ whilst processing.
*/
void sdca_irq_free(struct device *dev, struct sdca_interrupt_info *info,
int sdca_irq, const char *name, void *data)
{
if (sdca_irq < 0 || sdca_irq >= SDCA_MAX_INTERRUPTS)
return;
guard(mutex)(&info->irq_lock);
sdca_irq_free_locked(dev, info, sdca_irq, name, data);
}
EXPORT_SYMBOL_NS_GPL(sdca_irq_free, "SND_SOC_SDCA");
/**
* sdca_irq_data_populate - Populate common interrupt data
* @dev: Pointer to the Function device.
* @regmap: Pointer to the Function regmap.
* @component: Pointer to the ASoC component for the Function.
* @function: Pointer to the SDCA Function.
* @entity: Pointer to the SDCA Entity.
* @control: Pointer to the SDCA Control.
* @interrupt: Pointer to the SDCA interrupt for this IRQ.
*
* Return: Zero on success, and a negative error code on failure.
*/
int sdca_irq_data_populate(struct device *dev, struct regmap *regmap,
struct snd_soc_component *component,
struct sdca_function_data *function,
struct sdca_entity *entity,
struct sdca_control *control,
struct sdca_interrupt *interrupt)
{
const char *name;
if (!dev && component)
dev = component->dev;
if (!dev)
return -ENODEV;
name = kasprintf(GFP_KERNEL, "%s %s", entity->label, control->label);
if (!name)
return -ENOMEM;
interrupt->name = name;
interrupt->dev = dev;
if (!regmap && component)
interrupt->function_regmap = component->regmap;
else
interrupt->function_regmap = regmap;
interrupt->component = component;
interrupt->function = function;
interrupt->entity = entity;
interrupt->control = control;
return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_irq_data_populate, "SND_SOC_SDCA");
static struct sdca_interrupt *get_interrupt_data(struct device *dev, int irq,
struct sdca_interrupt_info *info)
{
if (irq == SDCA_NO_INTERRUPT) {
return NULL;
} else if (irq < 0 || irq >= SDCA_MAX_INTERRUPTS) {
dev_err(dev, "bad irq position: %d\n", irq);
return ERR_PTR(-EINVAL);
}
if (info->irqs[irq].irq) {
dev_dbg(dev, "skipping irq %d, already requested\n", irq);
return NULL;
}
return &info->irqs[irq];
}
/**
* sdca_irq_populate_early - process pre-audio card IRQ registrations
* @dev: Device pointer for SDCA Function.
* @regmap: Regmap pointer for the SDCA Function.
* @function: Pointer to the SDCA Function.
* @info: Pointer to the SDCA interrupt info for this device.
*
* This is intended to be used as part of the Function boot process. It
* can be called before the soundcard is registered (ie. doesn't depend
* on component) and will populate all the required IRQ data, as well as
* registering the FDL interrupts to start booting the device.
*
* Return: Zero on success, and a negative error code on failure.
*/
int sdca_irq_populate_early(struct device *dev, struct regmap *regmap,
struct sdca_function_data *function,
struct sdca_interrupt_info *info)
{
int i, j;
guard(mutex)(&info->irq_lock);
for (i = 0; i < function->num_entities; i++) {
struct sdca_entity *entity = &function->entities[i];
for (j = 0; j < entity->num_controls; j++) {
struct sdca_control *control = &entity->controls[j];
int irq = control->interrupt_position;
struct sdca_interrupt *interrupt;
int ret;
interrupt = get_interrupt_data(dev, irq, info);
if (IS_ERR(interrupt))
return PTR_ERR(interrupt);
else if (!interrupt)
continue;
ret = sdca_irq_data_populate(dev, regmap, NULL, function,
entity, control, interrupt);
if (ret)
return ret;
switch (SDCA_CTL_TYPE(entity->type, control->sel)) {
case SDCA_CTL_TYPE_S(ENTITY_0, FUNCTION_STATUS):
interrupt->handler = function_status_handler;
break;
case SDCA_CTL_TYPE_S(GE, DETECTED_MODE):
interrupt->handler = detected_mode_handler;
interrupt->free_priv = sdca_jack_free_state;
ret = sdca_jack_alloc_state(interrupt);
if (ret)
return ret;
break;
case SDCA_CTL_TYPE_S(XU, FDL_CURRENTOWNER):
interrupt->handler = fdl_owner_handler;
interrupt->free_priv = sdca_fdl_free_state;
ret = sdca_fdl_alloc_state(interrupt);
if (ret)
return ret;
interrupt->early_request = true;
ret = sdca_irq_request_locked(dev, info, irq,
interrupt->name,
interrupt->handler,
interrupt);
if (ret) {
dev_err(dev, "failed to request irq %s: %d\n",
interrupt->name, ret);
return ret;
}
break;
case SDCA_CTL_TYPE_S(HIDE, HIDTX_CURRENTOWNER):
interrupt->handler = hid_handler;
break;
default:
interrupt->handler = base_handler;
break;
}
}
}
return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_irq_populate_early, "SND_SOC_SDCA");
/**
* sdca_irq_populate - Request all the individual IRQs for an SDCA Function
* @function: Pointer to the SDCA Function.
* @component: Pointer to the ASoC component for the Function.
* @info: Pointer to the SDCA interrupt info for this device.
*
* Typically this would be called from the driver for a single SDCA Function.
*
* Return: Zero on success, and a negative error code on failure.
*/
int sdca_irq_populate(struct sdca_function_data *function,
struct snd_soc_component *component,
struct sdca_interrupt_info *info)
{
struct device *dev = component->dev;
int i, ret;
guard(mutex)(&info->irq_lock);
for (i = 0; i < SDCA_MAX_INTERRUPTS; i++) {
struct sdca_interrupt *interrupt = &info->irqs[i];
struct sdca_control *control = interrupt->control;
struct sdca_entity *entity = interrupt->entity;
int irq;
if (interrupt->function != function || interrupt->irq)
continue;
interrupt->component = component;
switch (SDCA_CTL_TYPE(entity->type, control->sel)) {
case SDCA_CTL_TYPE_S(GE, DETECTED_MODE):
ret = sdca_jack_init_state(interrupt);
if (ret)
return ret;
break;
default:
break;
}
irq = interrupt->control->interrupt_position;
ret = sdca_irq_request_locked(dev, info, irq, interrupt->name,
interrupt->handler, interrupt);
if (ret) {
dev_err(dev, "failed to request irq %s: %d\n",
interrupt->name, ret);
return ret;
}
}
return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_irq_populate, "SND_SOC_SDCA");
static void sdca_irq_cleanup_flags(struct device *dev,
struct sdca_function_data *function,
struct sdca_interrupt_info *info,
bool late_cleanup)
{
int i;
guard(mutex)(&info->irq_lock);
for (i = 0; i < SDCA_MAX_INTERRUPTS; i++) {
struct sdca_interrupt *interrupt = &info->irqs[i];
if (interrupt->function != function ||
(interrupt->early_request && !late_cleanup))
continue;
if (interrupt->irq)
sdca_irq_free_locked(dev, info, i, interrupt->name, interrupt);
if (!late_cleanup)
continue;
if (interrupt->free_priv)
interrupt->free_priv(interrupt);
kfree(interrupt->name);
}
}
/**
* sdca_irq_cleanup - Free the regular IRQs for an SDCA Function
* @dev: Device pointer against which the sdca_interrupt_info was allocated.
* @function: Pointer to the SDCA Function.
* @info: Pointer to the SDCA interrupt info for this device.
*
* Typically this would be called from the driver for a single SDCA Function
* from component remove.
*/
void sdca_irq_cleanup(struct device *dev,
struct sdca_function_data *function,
struct sdca_interrupt_info *info)
{
sdca_irq_cleanup_flags(dev, function, info, false);
}
EXPORT_SYMBOL_NS_GPL(sdca_irq_cleanup, "SND_SOC_SDCA");
/**
* sdca_irq_cleanup_late - Free the early IRQs for an SDCA Function
* @dev: Device pointer against which the sdca_interrupt_info was allocated.
* @function: Pointer to the SDCA Function.
* @info: Pointer to the SDCA interrupt info for this device.
*
* Typically this would be called from the driver for a single SDCA Function
* from bus remove.
*/
void sdca_irq_cleanup_late(struct device *dev,
struct sdca_function_data *function,
struct sdca_interrupt_info *info)
{
sdca_irq_cleanup_flags(dev, function, info, true);
}
EXPORT_SYMBOL_NS_GPL(sdca_irq_cleanup_late, "SND_SOC_SDCA");
/**
* devm_sdca_irq_allocate - allocate an SDCA interrupt structure for a device
* @sdev: Device pointer against which things should be allocated.
* @regmap: regmap to be used for accessing the SDCA IRQ registers.
* @irq: The interrupt number.
*
* Typically this would be called from the top level driver for the whole
* SDCA device, as only a single instance is required across all Functions
* on the device.
*
* Return: A pointer to the allocated sdca_interrupt_info struct, or an
* error code.
*/
struct sdca_interrupt_info *devm_sdca_irq_allocate(struct device *sdev,
struct regmap *regmap, int irq)
{
struct sdca_interrupt_info *info;
int ret, i;
info = devm_kzalloc(sdev, sizeof(*info), GFP_KERNEL);
if (!info)
return ERR_PTR(-ENOMEM);
info->irq_chip = sdca_irq_chip;
for (i = 0; i < ARRAY_SIZE(info->irqs); i++)
info->irqs[i].device_regmap = regmap;
ret = devm_mutex_init(sdev, &info->irq_lock);
if (ret)
return ERR_PTR(ret);
ret = devm_regmap_add_irq_chip(sdev, regmap, irq, IRQF_ONESHOT, 0,
&info->irq_chip, &info->irq_data);
if (ret) {
dev_err(sdev, "failed to register irq chip: %d\n", ret);
return ERR_PTR(ret);
}
dev_dbg(sdev, "registered on irq %d\n", irq);
return info;
}
EXPORT_SYMBOL_NS_GPL(devm_sdca_irq_allocate, "SND_SOC_SDCA");
static void irq_enable_flags(struct sdca_function_data *function,
struct sdca_interrupt_info *info, bool early)
{
int i;
for (i = 0; i < SDCA_MAX_INTERRUPTS; i++) {
struct sdca_interrupt *interrupt = &info->irqs[i];
if (!interrupt->irq || interrupt->function != function)
continue;
switch (SDCA_CTL_TYPE(interrupt->entity->type,
interrupt->control->sel)) {
case SDCA_CTL_TYPE_S(XU, FDL_CURRENTOWNER):
if (early)
enable_irq(interrupt->irq);
break;
default:
if (!early)
enable_irq(interrupt->irq);
break;
}
}
}
/**
* sdca_irq_enable_early - Re-enable early SDCA IRQs for a given function
* @function: Pointer to the SDCA Function.
* @info: Pointer to the SDCA interrupt info for this device.
*
* The early version of the IRQ enable allows enabling IRQs which may be
* necessary to bootstrap functionality for other IRQs, such as the FDL
* process.
*/
void sdca_irq_enable_early(struct sdca_function_data *function,
struct sdca_interrupt_info *info)
{
irq_enable_flags(function, info, true);
}
EXPORT_SYMBOL_NS_GPL(sdca_irq_enable_early, "SND_SOC_SDCA");
/**
* sdca_irq_enable - Re-enable SDCA IRQs for a given function
* @function: Pointer to the SDCA Function.
* @info: Pointer to the SDCA interrupt info for this device.
*/
void sdca_irq_enable(struct sdca_function_data *function,
struct sdca_interrupt_info *info)
{
irq_enable_flags(function, info, false);
}
EXPORT_SYMBOL_NS_GPL(sdca_irq_enable, "SND_SOC_SDCA");
/**
* sdca_irq_disable - Disable SDCA IRQs for a given function
* @function: Pointer to the SDCA Function.
* @info: Pointer to the SDCA interrupt info for this device.
*/
void sdca_irq_disable(struct sdca_function_data *function,
struct sdca_interrupt_info *info)
{
int i;
for (i = 0; i < SDCA_MAX_INTERRUPTS; i++) {
struct sdca_interrupt *interrupt = &info->irqs[i];
if (!interrupt->irq || interrupt->function != function)
continue;
disable_irq(interrupt->irq);
}
}
EXPORT_SYMBOL_NS_GPL(sdca_irq_disable, "SND_SOC_SDCA");
+274
View File
@@ -0,0 +1,274 @@
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2025 Cirrus Logic, Inc. and
// Cirrus Logic International Semiconductor Ltd.
/*
* The MIPI SDCA specification is available for public downloads at
* https://www.mipi.org/mipi-sdca-v1-0-download
*/
#include <linux/cleanup.h>
#include <linux/device.h>
#include <linux/dev_printk.h>
#include <linux/soundwire/sdw.h>
#include <linux/soundwire/sdw_registers.h>
#include <linux/sprintf.h>
#include <linux/regmap.h>
#include <linux/rwsem.h>
#include <sound/asound.h>
#include <sound/control.h>
#include <sound/jack.h>
#include <sound/sdca.h>
#include <sound/sdca_function.h>
#include <sound/sdca_interrupts.h>
#include <sound/sdca_jack.h>
#include <sound/soc-component.h>
#include <sound/soc-jack.h>
#include <sound/soc.h>
/**
* sdca_jack_process - Process an SDCA jack event
* @interrupt: SDCA interrupt structure
*
* Return: Zero on success or a negative error code.
*/
int sdca_jack_process(struct sdca_interrupt *interrupt)
{
struct device *dev = interrupt->dev;
struct snd_soc_component *component = interrupt->component;
struct snd_soc_card *card = component->card;
struct rw_semaphore *rwsem = &card->snd_card->controls_rwsem;
struct jack_state *state = interrupt->priv;
struct snd_kcontrol *kctl = state->kctl;
struct snd_ctl_elem_value *ucontrol __free(kfree) = NULL;
struct soc_enum *soc_enum;
unsigned int reg, val;
int ret;
guard(rwsem_write)(rwsem);
reg = SDW_SDCA_CTL(interrupt->function->desc->adr, interrupt->entity->id,
interrupt->control->sel, 0);
ret = regmap_read(interrupt->function_regmap, reg, &val);
if (ret < 0) {
dev_err(dev, "failed to read detected mode: %d\n", ret);
return ret;
}
reg = SDW_SDCA_CTL(interrupt->function->desc->adr, interrupt->entity->id,
SDCA_CTL_GE_SELECTED_MODE, 0);
switch (val) {
case SDCA_DETECTED_MODE_DETECTION_IN_PROGRESS:
case SDCA_DETECTED_MODE_JACK_UNKNOWN:
/*
* Selected mode is not normally marked as volatile register
* (RW), but here force a read from the hardware. If the
* detected mode is unknown we need to see what the device
* selected as a "safe" option.
*/
regcache_drop_region(interrupt->function_regmap, reg, reg);
ret = regmap_read(interrupt->function_regmap, reg, &val);
if (ret) {
dev_err(dev, "failed to re-check selected mode: %d\n", ret);
return ret;
}
break;
default:
break;
}
dev_dbg(dev, "%s: %#x\n", interrupt->name, val);
ucontrol = kzalloc_obj(*ucontrol);
if (!ucontrol)
return -ENOMEM;
soc_enum = (struct soc_enum *)kctl->private_value;
ucontrol->value.enumerated.item[0] = snd_soc_enum_val_to_item(soc_enum, val);
ret = snd_soc_dapm_put_enum_double(kctl, ucontrol);
if (ret < 0) {
dev_err(dev, "failed to update selected mode: %d\n", ret);
return ret;
}
snd_ctl_notify(card->snd_card, SNDRV_CTL_EVENT_MASK_VALUE, &kctl->id);
return sdca_jack_report(interrupt);
}
EXPORT_SYMBOL_NS_GPL(sdca_jack_process, "SND_SOC_SDCA");
/**
* sdca_jack_alloc_state - allocate state for a jack interrupt
* @interrupt: SDCA interrupt structure.
*
* Return: Zero on success or a negative error code.
*/
int sdca_jack_alloc_state(struct sdca_interrupt *interrupt)
{
struct jack_state *jack_state;
jack_state = kzalloc_obj(*jack_state);
if (!jack_state)
return -ENOMEM;
interrupt->priv = jack_state;
return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_jack_alloc_state, "SND_SOC_SDCA");
/**
* sdca_jack_free_state - free state for a jack interrupt
* @interrupt: SDCA interrupt structure.
*/
void sdca_jack_free_state(struct sdca_interrupt *interrupt)
{
kfree(interrupt->priv);
}
EXPORT_SYMBOL_NS_GPL(sdca_jack_free_state, "SND_SOC_SDCA");
/**
* sdca_jack_init_state - Initialise transient state for a jack interrupt
* @interrupt: SDCA interrupt structure.
*
* Return: Zero on success or a negative error code.
*/
int sdca_jack_init_state(struct sdca_interrupt *interrupt)
{
struct jack_state *jack_state = interrupt->priv;
const char *name __free(kfree) = kasprintf(GFP_KERNEL, "%s %s",
interrupt->entity->label,
SDCA_CTL_SELECTED_MODE_NAME);
if (!name)
return -ENOMEM;
jack_state->kctl = snd_soc_component_get_kcontrol(interrupt->component, name);
if (!jack_state->kctl) {
dev_err(interrupt->dev, "control not found: %s\n", name);
return -ENODEV;
}
return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_jack_init_state, "SND_SOC_SDCA");
static int type_get_mask(enum sdca_terminal_type type)
{
switch (type) {
case SDCA_TERM_TYPE_LINEIN_STEREO:
case SDCA_TERM_TYPE_LINEIN_FRONT_LR:
case SDCA_TERM_TYPE_LINEIN_CENTER_LFE:
case SDCA_TERM_TYPE_LINEIN_SURROUND_LR:
case SDCA_TERM_TYPE_LINEIN_REAR_LR:
return SND_JACK_LINEIN;
case SDCA_TERM_TYPE_LINEOUT_STEREO:
case SDCA_TERM_TYPE_LINEOUT_FRONT_LR:
case SDCA_TERM_TYPE_LINEOUT_CENTER_LFE:
case SDCA_TERM_TYPE_LINEOUT_SURROUND_LR:
case SDCA_TERM_TYPE_LINEOUT_REAR_LR:
return SND_JACK_LINEOUT;
case SDCA_TERM_TYPE_MIC_JACK:
return SND_JACK_MICROPHONE;
case SDCA_TERM_TYPE_HEADPHONE_JACK:
return SND_JACK_HEADPHONE;
case SDCA_TERM_TYPE_HEADSET_JACK:
return SND_JACK_HEADSET;
default:
return 0;
}
}
/**
* sdca_jack_set_jack - attach an ASoC jack to SDCA
* @info: SDCA interrupt information.
* @jack: ASoC jack to be attached.
*
* Return: Zero on success or a negative error code.
*/
int sdca_jack_set_jack(struct sdca_interrupt_info *info, struct snd_soc_jack *jack)
{
int i, j;
int ret;
guard(mutex)(&info->irq_lock);
for (i = 0; i < SDCA_MAX_INTERRUPTS; i++) {
struct sdca_interrupt *interrupt = &info->irqs[i];
struct sdca_control *control = interrupt->control;
struct sdca_entity *entity = interrupt->entity;
struct sdca_control_range *range;
struct jack_state *jack_state;
if (!interrupt->dev)
continue;
switch (SDCA_CTL_TYPE(entity->type, control->sel)) {
case SDCA_CTL_TYPE_S(GE, DETECTED_MODE):
range = sdca_selector_find_range(interrupt->dev, entity,
SDCA_CTL_GE_SELECTED_MODE,
SDCA_SELECTED_MODE_NCOLS, 0);
if (!range)
return -EINVAL;
jack_state = interrupt->priv;
for (j = 0; j < range->rows; j++) {
enum sdca_terminal_type type;
type = sdca_range(range, SDCA_SELECTED_MODE_TERM_TYPE, j);
jack_state->mask |= type_get_mask(type);
}
jack_state->jack = jack;
/* Report initial state in case IRQ was already handled */
ret = sdca_jack_report(interrupt);
if (ret)
return ret;
break;
default:
break;
}
}
return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_jack_set_jack, "SND_SOC_SDCA");
int sdca_jack_report(struct sdca_interrupt *interrupt)
{
struct jack_state *jack_state = interrupt->priv;
struct sdca_control_range *range;
enum sdca_terminal_type type;
unsigned int reg, val;
int ret;
reg = SDW_SDCA_CTL(interrupt->function->desc->adr, interrupt->entity->id,
SDCA_CTL_GE_SELECTED_MODE, 0);
ret = regmap_read(interrupt->function_regmap, reg, &val);
if (ret) {
dev_err(interrupt->dev, "failed to read selected mode: %d\n", ret);
return ret;
}
range = sdca_selector_find_range(interrupt->dev, interrupt->entity,
SDCA_CTL_GE_SELECTED_MODE,
SDCA_SELECTED_MODE_NCOLS, 0);
if (!range)
return -EINVAL;
type = sdca_range_search(range, SDCA_SELECTED_MODE_INDEX,
val, SDCA_SELECTED_MODE_TERM_TYPE);
snd_soc_jack_report(jack_state->jack, type_get_mask(type), jack_state->mask);
return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_jack_report, "SND_SOC_SDCA");
+381
View File
@@ -0,0 +1,381 @@
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2025 Cirrus Logic, Inc. and
// Cirrus Logic International Semiconductor Ltd.
/*
* The MIPI SDCA specification is available for public downloads at
* https://www.mipi.org/mipi-sdca-v1-0-download
*/
#include <linux/bitops.h>
#include <linux/minmax.h>
#include <linux/module.h>
#include <linux/regmap.h>
#include <linux/soundwire/sdw_registers.h>
#include <linux/types.h>
#include <sound/sdca.h>
#include <sound/sdca_function.h>
#include <sound/sdca_regmap.h>
static struct sdca_entity *
function_find_entity(struct sdca_function_data *function, unsigned int reg)
{
int i;
for (i = 0; i < function->num_entities; i++)
if (SDW_SDCA_CTL_ENT(reg) == function->entities[i].id)
return &function->entities[i];
return NULL;
}
static struct sdca_control *
entity_find_control(struct sdca_entity *entity, unsigned int reg)
{
int i;
for (i = 0; i < entity->num_controls; i++) {
if (SDW_SDCA_CTL_CSEL(reg) == entity->controls[i].sel)
return &entity->controls[i];
}
return NULL;
}
static struct sdca_control *
function_find_control(struct sdca_function_data *function, unsigned int reg)
{
struct sdca_entity *entity;
entity = function_find_entity(function, reg);
if (!entity)
return NULL;
return entity_find_control(entity, reg);
}
/**
* sdca_regmap_readable - return if a given SDCA Control is readable
* @function: Pointer to the Function information.
* @reg: Register address/Control to be processed.
*
* Return: Returns true if the register is readable.
*/
bool sdca_regmap_readable(struct sdca_function_data *function, unsigned int reg)
{
struct sdca_control *control;
if (!SDW_SDCA_VALID_CTL(reg))
return false;
control = function_find_control(function, reg);
if (!control)
return false;
if (!(BIT(SDW_SDCA_CTL_CNUM(reg)) & control->cn_list))
return false;
switch (control->mode) {
case SDCA_ACCESS_MODE_RW:
case SDCA_ACCESS_MODE_RO:
case SDCA_ACCESS_MODE_RW1S:
case SDCA_ACCESS_MODE_RW1C:
if (SDW_SDCA_NEXT_CTL(0) & reg)
return false;
fallthrough;
case SDCA_ACCESS_MODE_DUAL:
/* No access to registers marked solely for device use */
return control->layers & ~SDCA_ACCESS_LAYER_DEVICE;
default:
return false;
}
}
EXPORT_SYMBOL_NS(sdca_regmap_readable, "SND_SOC_SDCA");
/**
* sdca_regmap_writeable - return if a given SDCA Control is writeable
* @function: Pointer to the Function information.
* @reg: Register address/Control to be processed.
*
* Return: Returns true if the register is writeable.
*/
bool sdca_regmap_writeable(struct sdca_function_data *function, unsigned int reg)
{
struct sdca_control *control;
if (!SDW_SDCA_VALID_CTL(reg))
return false;
control = function_find_control(function, reg);
if (!control)
return false;
if (!(BIT(SDW_SDCA_CTL_CNUM(reg)) & control->cn_list))
return false;
switch (control->mode) {
case SDCA_ACCESS_MODE_RW:
case SDCA_ACCESS_MODE_RW1S:
case SDCA_ACCESS_MODE_RW1C:
if (SDW_SDCA_NEXT_CTL(0) & reg)
return false;
fallthrough;
case SDCA_ACCESS_MODE_DUAL:
/* No access to registers marked solely for device use */
return control->layers & ~SDCA_ACCESS_LAYER_DEVICE;
default:
return false;
}
}
EXPORT_SYMBOL_NS(sdca_regmap_writeable, "SND_SOC_SDCA");
/**
* sdca_regmap_volatile - return if a given SDCA Control is volatile
* @function: Pointer to the Function information.
* @reg: Register address/Control to be processed.
*
* Return: Returns true if the register is volatile.
*/
bool sdca_regmap_volatile(struct sdca_function_data *function, unsigned int reg)
{
struct sdca_control *control;
if (!SDW_SDCA_VALID_CTL(reg))
return false;
control = function_find_control(function, reg);
if (!control)
return false;
return control->is_volatile;
}
EXPORT_SYMBOL_NS(sdca_regmap_volatile, "SND_SOC_SDCA");
/**
* sdca_regmap_deferrable - return if a given SDCA Control is deferrable
* @function: Pointer to the Function information.
* @reg: Register address/Control to be processed.
*
* Return: Returns true if the register is deferrable.
*/
bool sdca_regmap_deferrable(struct sdca_function_data *function, unsigned int reg)
{
struct sdca_control *control;
if (!SDW_SDCA_VALID_CTL(reg))
return false;
control = function_find_control(function, reg);
if (!control)
return false;
return control->deferrable;
}
EXPORT_SYMBOL_NS(sdca_regmap_deferrable, "SND_SOC_SDCA");
/**
* sdca_regmap_mbq_size - return size in bytes of a given SDCA Control
* @function: Pointer to the Function information.
* @reg: Register address/Control to be processed.
*
* Return: Returns the size in bytes of the Control.
*/
int sdca_regmap_mbq_size(struct sdca_function_data *function, unsigned int reg)
{
struct sdca_control *control;
if (!SDW_SDCA_VALID_CTL(reg))
return -EINVAL;
control = function_find_control(function, reg);
if (!control)
return -EINVAL;
return clamp_val(control->nbits / BITS_PER_BYTE, sizeof(u8), sizeof(u32));
}
EXPORT_SYMBOL_NS(sdca_regmap_mbq_size, "SND_SOC_SDCA");
/**
* sdca_regmap_count_constants - count the number of DisCo constant Controls
* @dev: Pointer to the device.
* @function: Pointer to the Function information, to be parsed.
*
* This function returns the number of DisCo constant Controls present
* in a function. Typically this information will be used to populate
* the regmap defaults array, allowing drivers to access the values of
* DisCo constants as any other physical register.
*
* Return: Returns number of DisCo constant controls, or a negative error
* code on failure.
*/
int sdca_regmap_count_constants(struct device *dev,
struct sdca_function_data *function)
{
int nconsts = 0;
int i, j;
for (i = 0; i < function->num_entities; i++) {
struct sdca_entity *entity = &function->entities[i];
for (j = 0; j < entity->num_controls; j++) {
if (entity->controls[j].mode == SDCA_ACCESS_MODE_DC ||
entity->controls[j].has_reset)
nconsts += hweight64(entity->controls[j].cn_list);
}
}
return nconsts;
}
EXPORT_SYMBOL_NS(sdca_regmap_count_constants, "SND_SOC_SDCA");
/**
* sdca_regmap_populate_constants - fill an array with DisCo constant values
* @dev: Pointer to the device.
* @function: Pointer to the Function information, to be parsed.
* @consts: Pointer to the array which should be filled with the DisCo
* constant values.
*
* This function will populate a regmap struct reg_default array with
* the values of the DisCo constants for a given Function. This
* allows to access the values of DisCo constants the same as any
* other physical register.
*
* Return: Returns the number of constants populated on success, a negative
* error code on failure.
*/
int sdca_regmap_populate_constants(struct device *dev,
struct sdca_function_data *function,
struct reg_default *consts)
{
int i, j, k, l;
for (i = 0, k = 0; i < function->num_entities; i++) {
struct sdca_entity *entity = &function->entities[i];
for (j = 0; j < entity->num_controls; j++) {
struct sdca_control *control = &entity->controls[j];
int cn;
if (control->mode != SDCA_ACCESS_MODE_DC &&
!control->has_reset)
continue;
l = 0;
for_each_set_bit(cn, (unsigned long *)&control->cn_list,
BITS_PER_TYPE(control->cn_list)) {
consts[k].reg = SDW_SDCA_CTL(function->desc->adr,
entity->id,
control->sel, cn);
if (control->mode == SDCA_ACCESS_MODE_DC)
consts[k].def = control->values[l];
else
consts[k].def = control->reset;
k++;
l++;
}
}
}
return k;
}
EXPORT_SYMBOL_NS(sdca_regmap_populate_constants, "SND_SOC_SDCA");
static int populate_control_defaults(struct device *dev, struct regmap *regmap,
struct sdca_function_data *function,
struct sdca_entity *entity,
struct sdca_control *control)
{
int i, ret;
int cn;
if (control->mode == SDCA_ACCESS_MODE_DC)
return 0;
if (control->layers & SDCA_ACCESS_LAYER_DEVICE)
return 0;
i = 0;
for_each_set_bit(cn, (unsigned long *)&control->cn_list,
BITS_PER_TYPE(control->cn_list)) {
unsigned int reg, val;
reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, cn);
if (control->has_default || control->has_fixed) {
ret = regmap_write(regmap, reg, control->values[i]);
if (ret) {
dev_err(dev, "Failed to write default %#x: %d\n",
reg, ret);
return ret;
}
i++;
} else if (!control->is_volatile) {
if (control->has_reset)
regcache_drop_region(regmap, reg, reg);
ret = regmap_read(regmap, reg, &val);
if (ret) {
dev_err(dev, "Failed to read initial %#x: %d\n",
reg, ret);
return ret;
}
}
}
return 0;
}
/**
* sdca_regmap_write_defaults - write out DisCo defaults to device
* @dev: Pointer to the device.
* @regmap: Pointer to the Function register map.
* @function: Pointer to the Function information, to be parsed.
*
* This function will write out to the hardware all the DisCo default and
* fixed value controls. This will cause them to be populated into the cache,
* and subsequent handling can be done through a cache sync. It will also
* read any non-volatile registers that don't have defaults/fixed values to
* populate those into the cache, this ensures they are available for reads
* even when the device is runtime suspended.
*
* Return: Returns zero on success, and a negative error code on failure.
*/
int sdca_regmap_write_defaults(struct device *dev, struct regmap *regmap,
struct sdca_function_data *function)
{
int i, j;
int ret;
for (i = 0; i < function->num_entities; i++) {
struct sdca_entity *entity = &function->entities[i];
for (j = 0; j < entity->num_controls; j++) {
struct sdca_control *control = &entity->controls[j];
ret = populate_control_defaults(dev, regmap, function,
entity, control);
if (ret)
return ret;
}
}
return 0;
}
EXPORT_SYMBOL_NS(sdca_regmap_write_defaults, "SND_SOC_SDCA");
int sdca_regmap_write_init(struct device *dev, struct regmap *regmap,
struct sdca_function_data *function)
{
struct sdca_init_write *init = function->init_table;
int ret, i;
for (i = 0; i < function->num_init_table; i++) {
ret = regmap_write(regmap, init[i].addr, init[i].val);
if (ret)
return ret;
}
return 0;
}
EXPORT_SYMBOL_NS(sdca_regmap_write_init, "SND_SOC_SDCA");
+262
View File
@@ -0,0 +1,262 @@
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2025 Cirrus Logic, Inc. and
// Cirrus Logic International Semiconductor Ltd.
/*
* The MIPI SDCA specification is available for public downloads at
* https://www.mipi.org/mipi-sdca-v1-0-download
*/
#include <linux/dev_printk.h>
#include <linux/device.h>
#include <linux/regmap.h>
#include <sound/sdca.h>
#include <sound/sdca_function.h>
#include <sound/sdca_ump.h>
#include <sound/soc-component.h>
#include <linux/soundwire/sdw_registers.h>
/**
* sdca_ump_get_owner_host - check a UMP buffer is owned by the host
* @dev: Pointer to the struct device used for error messages.
* @function_regmap: Pointer to the regmap for the SDCA Function.
* @function: Pointer to the Function information.
* @entity: Pointer to the SDCA Entity.
* @control: Pointer to the SDCA Control for the UMP Owner.
*
* Return: Returns zero on success, and a negative error code on failure.
*/
int sdca_ump_get_owner_host(struct device *dev,
struct regmap *function_regmap,
struct sdca_function_data *function,
struct sdca_entity *entity,
struct sdca_control *control)
{
unsigned int reg, owner;
int ret;
reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, 0);
ret = regmap_read(function_regmap, reg, &owner);
if (ret < 0) {
dev_err(dev, "%s: failed to read UMP owner: %d\n",
entity->label, ret);
return ret;
}
if (owner != SDCA_UMP_OWNER_HOST) {
dev_err(dev, "%s: host is not the UMP owner\n", entity->label);
return -EINVAL;
}
return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_ump_get_owner_host, "SND_SOC_SDCA");
/**
* sdca_ump_set_owner_device - set a UMP buffer's ownership back to the device
* @dev: Pointer to the struct device used for error messages.
* @function_regmap: Pointer to the regmap for the SDCA Function.
* @function: Pointer to the Function information.
* @entity: Pointer to the SDCA Entity.
* @control: Pointer to the SDCA Control for the UMP Owner.
*
* Return: Returns zero on success, and a negative error code on failure.
*/
int sdca_ump_set_owner_device(struct device *dev,
struct regmap *function_regmap,
struct sdca_function_data *function,
struct sdca_entity *entity,
struct sdca_control *control)
{
unsigned int reg;
int ret;
reg = SDW_SDCA_CTL(function->desc->adr, entity->id, control->sel, 0);
ret = regmap_write(function_regmap, reg, SDCA_UMP_OWNER_DEVICE);
if (ret < 0)
dev_err(dev, "%s: failed to write UMP owner: %d\n",
entity->label, ret);
return ret;
}
EXPORT_SYMBOL_NS_GPL(sdca_ump_set_owner_device, "SND_SOC_SDCA");
/**
* sdca_ump_read_message - read a UMP message from the device
* @dev: Pointer to the struct device used for error messages.
* @device_regmap: Pointer to the Device register map.
* @function_regmap: Pointer to the regmap for the SDCA Function.
* @function: Pointer to the Function information.
* @entity: Pointer to the SDCA Entity.
* @offset_sel: Control Selector for the UMP Offset Control.
* @length_sel: Control Selector for the UMP Length Control.
* @msg: Pointer that will be populated with an dynamically buffer
* containing the UMP message. Note this needs to be freed by the
* caller.
*
* The caller should first call sdca_ump_get_owner_host() to ensure the host
* currently owns the UMP buffer, and then this function can be used to
* retrieve a message. It is the callers responsibility to free the
* message once it is finished with it. Finally sdca_ump_set_owner_device()
* should be called to return the buffer to the device.
*
* Return: Returns the message length on success, and a negative error
* code on failure.
*/
int sdca_ump_read_message(struct device *dev,
struct regmap *device_regmap,
struct regmap *function_regmap,
struct sdca_function_data *function,
struct sdca_entity *entity,
unsigned int offset_sel, unsigned int length_sel,
void **msg)
{
struct sdca_control_range *range;
unsigned int msg_offset, msg_len;
unsigned int buf_addr, buf_len;
unsigned int reg;
int ret;
reg = SDW_SDCA_CTL(function->desc->adr, entity->id, offset_sel, 0);
ret = regmap_read(function_regmap, reg, &msg_offset);
if (ret < 0) {
dev_err(dev, "%s: failed to read UMP offset: %d\n",
entity->label, ret);
return ret;
}
range = sdca_selector_find_range(dev, entity, offset_sel,
SDCA_MESSAGEOFFSET_NCOLS, 1);
if (!range)
return -ENOENT;
buf_addr = sdca_range(range, SDCA_MESSAGEOFFSET_BUFFER_START_ADDRESS, 0);
buf_len = sdca_range(range, SDCA_MESSAGEOFFSET_BUFFER_LENGTH, 0);
reg = SDW_SDCA_CTL(function->desc->adr, entity->id, length_sel, 0);
ret = regmap_read(function_regmap, reg, &msg_len);
if (ret < 0) {
dev_err(dev, "%s: failed to read UMP length: %d\n",
entity->label, ret);
return ret;
}
if (msg_offset + msg_len > buf_len) {
dev_err(dev, "%s: message too big for UMP buffer: %d\n",
entity->label, msg_len);
return -EINVAL;
}
*msg = kmalloc(msg_len, GFP_KERNEL);
if (!*msg)
return -ENOMEM;
ret = regmap_raw_read(device_regmap, buf_addr + msg_offset, *msg, msg_len);
if (ret < 0) {
dev_err(dev, "%s: failed to read UMP message: %d\n",
entity->label, ret);
return ret;
}
return msg_len;
}
EXPORT_SYMBOL_NS_GPL(sdca_ump_read_message, "SND_SOC_SDCA");
/**
* sdca_ump_write_message - write a UMP message to the device
* @dev: Pointer to the struct device used for error messages.
* @device_regmap: Pointer to the Device register map.
* @function_regmap: Pointer to the regmap for the SDCA Function.
* @function: Pointer to the Function information.
* @entity: Pointer to the SDCA Entity.
* @offset_sel: Control Selector for the UMP Offset Control.
* @msg_offset: Offset within the UMP buffer at which the message should
* be written.
* @length_sel: Control Selector for the UMP Length Control.
* @msg: Pointer to the data that should be written to the UMP buffer.
* @msg_len: Length of the message data in bytes.
*
* The caller should first call sdca_ump_get_owner_host() to ensure the host
* currently owns the UMP buffer, and then this function can be used to
* write a message. Finally sdca_ump_set_owner_device() should be called to
* return the buffer to the device, allowing the device to access the
* message.
*
* Return: Returns zero on success, and a negative error code on failure.
*/
int sdca_ump_write_message(struct device *dev,
struct regmap *device_regmap,
struct regmap *function_regmap,
struct sdca_function_data *function,
struct sdca_entity *entity,
unsigned int offset_sel, unsigned int msg_offset,
unsigned int length_sel,
void *msg, int msg_len)
{
struct sdca_control_range *range;
unsigned int buf_addr, buf_len, ump_mode;
unsigned int reg;
int ret;
range = sdca_selector_find_range(dev, entity, offset_sel,
SDCA_MESSAGEOFFSET_NCOLS, 1);
if (!range)
return -ENOENT;
buf_addr = sdca_range(range, SDCA_MESSAGEOFFSET_BUFFER_START_ADDRESS, 0);
buf_len = sdca_range(range, SDCA_MESSAGEOFFSET_BUFFER_LENGTH, 0);
ump_mode = sdca_range(range, SDCA_MESSAGEOFFSET_UMP_MODE, 0);
if (msg_offset + msg_len > buf_len) {
dev_err(dev, "%s: message too big for UMP buffer: %d\n",
entity->label, msg_len);
return -EINVAL;
}
if (ump_mode != SDCA_UMP_MODE_DIRECT) {
dev_err(dev, "%s: only direct mode currently supported\n",
entity->label);
return -EINVAL;
}
ret = regmap_raw_write(device_regmap, buf_addr + msg_offset, msg, msg_len);
if (ret) {
dev_err(dev, "%s: failed to write UMP message: %d\n",
entity->label, ret);
return ret;
}
reg = SDW_SDCA_CTL(function->desc->adr, entity->id, offset_sel, 0);
ret = regmap_write(function_regmap, reg, msg_offset);
if (ret < 0) {
dev_err(dev, "%s: failed to write UMP offset: %d\n",
entity->label, ret);
return ret;
}
reg = SDW_SDCA_CTL(function->desc->adr, entity->id, length_sel, 0);
ret = regmap_write(function_regmap, reg, msg_len);
if (ret < 0) {
dev_err(dev, "%s: failed to write UMP length: %d\n",
entity->label, ret);
return ret;
}
return 0;
}
EXPORT_SYMBOL_NS_GPL(sdca_ump_write_message, "SND_SOC_SDCA");
void sdca_ump_cancel_timeout(struct delayed_work *work)
{
cancel_delayed_work_sync(work);
}
EXPORT_SYMBOL_NS_GPL(sdca_ump_cancel_timeout, "SND_SOC_SDCA");
void sdca_ump_schedule_timeout(struct delayed_work *work, unsigned int timeout_us)
{
if (!timeout_us)
return;
queue_delayed_work(system_dfl_wq, work, usecs_to_jiffies(timeout_us));
}
EXPORT_SYMBOL_NS_GPL(sdca_ump_schedule_timeout, "SND_SOC_SDCA");