// SPDX-License-Identifier: (GPL-2.0-only OR BSD-3-Clause)
//
// This file is provided under a dual BSD/GPLv2 license. When using or
// redistributing this file, you may do so under either license.
//
// Copyright(c) 2019 Intel Corporation. All rights reserved.
//
// Author: Ranjani Sridharan <ranjani.sridharan@linux.intel.com>
//
#include <linux/bitfield.h>
#include "sof-audio.h"
#include "ops.h"
static int sof_kcontrol_setup(struct snd_sof_dev *sdev, struct snd_sof_control *scontrol)
{
int ipc_cmd, ctrl_type;
int ret;
/* reset readback offset for scontrol */
scontrol->readback_offset = 0;
/* notify DSP of kcontrol values */
switch (scontrol->cmd) {
case SOF_CTRL_CMD_VOLUME:
case SOF_CTRL_CMD_ENUM:
case SOF_CTRL_CMD_SWITCH:
ipc_cmd = SOF_IPC_COMP_SET_VALUE;
ctrl_type = SOF_CTRL_TYPE_VALUE_CHAN_SET;
break;
case SOF_CTRL_CMD_BINARY:
ipc_cmd = SOF_IPC_COMP_SET_DATA;
ctrl_type = SOF_CTRL_TYPE_DATA_SET;
break;
default:
return 0;
}
ret = snd_sof_ipc_set_get_comp_data(scontrol, ipc_cmd, ctrl_type, scontrol->cmd, true);
if (ret < 0)
dev_err(sdev->dev, "error: failed kcontrol value set for widget: %d\n",
scontrol->comp_id);
return ret;
}
static int sof_dai_config_setup(struct snd_sof_dev *sdev, struct snd_sof_dai *dai)
{
struct sof_ipc_dai_config *config;
struct sof_ipc_reply reply;
int ret;
config = &dai->dai_config[dai->current_config];
if (!config) {
dev_err(sdev->dev, "error: no config for DAI %s\n", dai->name);
return -EINVAL;
}
/* set NONE flag to clear all previous settings */
config->flags = FIELD_PREP(SOF_DAI_CONFIG_FLAGS_MASK, SOF_DAI_CONFIG_FLAGS_NONE);
ret = sof_ipc_tx_message(sdev->ipc, config->hdr.cmd, config, config->hdr.size,
&reply, sizeof(reply));
if (ret < 0)
dev_err(sdev->dev, "error: failed to set dai config for %s\n", dai->name);
return ret;
}
static int sof_widget_kcontrol_setup(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
{
struct snd_sof_control *scontrol;
int ret;
/* set up all controls for the widget */
list_for_each_entry(scontrol, &sdev->kcontrol_list, list)
if (scontrol->comp_id == swidget->comp_id) {
ret = sof_kcontrol_setup(sdev, scontrol);
if (ret < 0) {
dev_err(sdev->dev, "error: fail to set up kcontrols for widget %s\n",
swidget->widget->name);
return ret;
}
}
return 0;
}
static void sof_reset_route_setup_status(struct snd_sof_dev *sdev, struct snd_sof_widget *widget)
{
struct snd_sof_route *sroute;
list_for_each_entry(sroute, &sdev->route_list, list)
if (sroute->src_widget == widget || sroute->sink_widget == widget)
sroute->setup = false;
}
int sof_widget_free(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget)
{
struct sof_ipc_free ipc_free = {
.hdr = {
.size = sizeof(ipc_free),
.cmd = SOF_IPC_GLB_TPLG_MSG,
},
.id = swidget->comp_id,
};
struct sof_ipc_reply reply;
int ret;
if (!swidget->private)
return 0;
/* only free when use_count is 0 */
if (--swidget->use_count)
return 0;
switch (swidget->id) {
case snd_soc_dapm_scheduler:
ipc_free.hdr.cmd |= SOF_IPC_TPLG_PIPE_FREE;
break;
case snd_soc_dapm_buffer:
ipc_free.hdr.cmd |= SOF_IPC_TPLG_BUFFER_FREE;
break;
default:
ipc_free.hdr.cmd |= SOF_IPC_TPLG_COMP_FREE;
break;
}
ret = sof_ipc_tx_message(sdev->ipc, ipc_free.hdr.cmd, &ipc_free, sizeof(ipc_free),
&reply, sizeof(reply));
if (ret < 0) {
dev_err(sdev->dev, "error: failed to free widget %s\n", swidget->widget->name);
swidget->use_count++;
return ret;
}
/* reset route setup status for all routes that contain this widget */
sof_reset_route_setup_status(sdev, swidget);
swidget->complete = 0;
dev_dbg(sdev->dev, "widget %s freed\n", swidget->widget->name);
return 0;
}
EXPORT_SYMBOL(sof_widget_free);
int