1037 lines · 28.4 KB · C
RawDownload
// 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) 2022 Intel Corporation//// #include "sof-priv.h"#include "sof-audio.h"#include "ipc4-priv.h"#include "ipc4-topology.h" static int sof_ipc4_set_get_kcontrol_data(struct snd_sof_control *scontrol,					  struct sof_ipc4_msg *msg,					  bool set, bool lock){	struct snd_soc_component *scomp = scontrol->scomp;	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);	const struct sof_ipc_ops *iops = sdev->ipc->ops;	struct snd_sof_widget *swidget;	bool widget_found = false;	int ret = 0; 	/* find widget associated with the control */	list_for_each_entry(swidget, &sdev->widget_list, list) {		if (swidget->comp_id == scontrol->comp_id) {			widget_found = true;			break;		}	} 	if (!widget_found) {		dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);		return -ENOENT;	} 	if (lock)		mutex_lock(&swidget->setup_mutex);	else		lockdep_assert_held(&swidget->setup_mutex); 	/*	 * Volatile controls should always be part of static pipelines and the	 * widget use_count would always be > 0 in this case. For the others,	 * just return the cached value if the widget is not set up.	 */	if (!swidget->use_count)		goto unlock; 	msg->primary &= ~SOF_IPC4_MOD_INSTANCE_MASK;	msg->primary |= SOF_IPC4_MOD_INSTANCE(swidget->instance_id); 	ret = iops->set_get_data(sdev, msg, msg->data_size, set);	if (!set)		goto unlock; 	/* It is a set-data operation, and we have a valid backup that we can restore */	if (ret < 0) {		if (!scontrol->old_ipc_control_data)			goto unlock;		/*		 * Current ipc_control_data is not valid, we use the last known good		 * configuration		 */		memcpy(scontrol->ipc_control_data, scontrol->old_ipc_control_data,		       scontrol->size);		kfree(scontrol->old_ipc_control_data);		scontrol->old_ipc_control_data = NULL;		/* Send the last known good configuration to firmware */		ret = iops->set_get_data(sdev, msg, msg->data_size, set);		if (ret < 0)			goto unlock;	} unlock:	if (lock)		mutex_unlock(&swidget->setup_mutex); 	return ret;} static intsof_ipc4_set_volume_data(struct snd_sof_dev *sdev, struct snd_sof_widget *swidget,			 struct snd_sof_control *scontrol, bool lock){	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;	struct sof_ipc4_gain *gain = swidget->private;	struct sof_ipc4_gain_params params;	bool all_channels_equal = true;	struct sof_ipc4_msg msg;	u32 value;	int ret, i; 	/* check if all channel values are equal */	value = cdata->chanv[0].value;	for (i = 1; i < scontrol->num_channels; i++) {		if (cdata->chanv[i].value != value) {			all_channels_equal = false;			break;		}	} 	/*	 * notify DSP with a single IPC message if all channel values are equal. Otherwise send	 * a separate IPC for each channel.	 */	memcpy(&msg, &cdata->msg, sizeof(msg));	for (i = 0; i < scontrol->num_channels; i++) {		if (all_channels_equal) {			params.channels = SOF_IPC4_GAIN_ALL_CHANNELS_MASK;			params.init_val = cdata->chanv[0].value;		} else {			params.channels = cdata->chanv[i].channel;			params.init_val = cdata->chanv[i].value;		} 		/* set curve type and duration from topology */		params.curve_duration_l = gain->data.params.curve_duration_l;		params.curve_duration_h = gain->data.params.curve_duration_h;		params.curve_type = gain->data.params.curve_type; 		msg.data_ptr = &params;		msg.data_size = sizeof(params); 		ret = sof_ipc4_set_get_kcontrol_data(scontrol, &msg, true, lock);		if (ret < 0) {			dev_err(sdev->dev, "Failed to set volume update for %s\n",				scontrol->name);			return ret;		} 		if (all_channels_equal)			break;	} 	return 0;} static bool sof_ipc4_volume_put(struct snd_sof_control *scontrol,				struct snd_ctl_elem_value *ucontrol){	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;	struct snd_soc_component *scomp = scontrol->scomp;	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);	unsigned int channels = scontrol->num_channels;	struct snd_sof_widget *swidget;	bool widget_found = false;	bool change = false;	unsigned int i;	int ret; 	/* update each channel */	for (i = 0; i < channels; i++) {		u32 value = mixer_to_ipc(ucontrol->value.integer.value[i],					 scontrol->volume_table, scontrol->max + 1); 		change = change || (value != cdata->chanv[i].value);		cdata->chanv[i].channel = i;		cdata->chanv[i].value = value;	} 	if (!pm_runtime_active(scomp->dev))		return change; 	/* find widget associated with the control */	list_for_each_entry(swidget, &sdev->widget_list, list) {		if (swidget->comp_id == scontrol->comp_id) {			widget_found = true;			break;		}	} 	if (!widget_found) {		dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);		return false;	} 	ret = sof_ipc4_set_volume_data(sdev, swidget, scontrol, true);	if (ret < 0)		return false; 	return change;} static int sof_ipc4_volume_get(struct snd_sof_control *scontrol,			       struct snd_ctl_elem_value *ucontrol){	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;	unsigned int channels = scontrol->num_channels;	unsigned int i; 	for (i = 0; i < channels; i++)		ucontrol->value.integer.value[i] = ipc_to_mixer(cdata->chanv[i].value,								scontrol->volume_table,								scontrol->max + 1); 	return 0;} static intsof_ipc4_set_generic_control_data(struct snd_sof_dev *sdev,				  struct snd_sof_widget *swidget,				  struct snd_sof_control *scontrol, bool lock){	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;	struct sof_ipc4_control_msg_payload *data;	struct sof_ipc4_msg msg;	size_t data_size;	unsigned int i;	int ret; 	data_size = struct_size(data, chanv, scontrol->num_channels);	data = kzalloc(data_size, GFP_KERNEL);	if (!data)		return -ENOMEM; 	data->id = cdata->index;	data->num_elems = scontrol->num_channels;	for (i = 0; i < scontrol->num_channels; i++) {		data->chanv[i].channel = cdata->chanv[i].channel;		data->chanv[i].value = cdata->chanv[i].value;	} 	memcpy(&msg, &cdata->msg, sizeof(msg));	msg.data_ptr = data;	msg.data_size = data_size; 	ret = sof_ipc4_set_get_kcontrol_data(scontrol, &msg, true, lock);	if (ret < 0)		dev_err(sdev->dev, "Failed to set control update for %s\n",			scontrol->name); 	kfree(data); 	return ret;} static void sof_ipc4_refresh_generic_control(struct snd_sof_control *scontrol){	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;	struct snd_soc_component *scomp = scontrol->scomp;	struct sof_ipc4_control_msg_payload *data;	struct sof_ipc4_msg msg;	size_t data_size;	unsigned int i;	int ret; 	if (!scontrol->comp_data_dirty)		return; 	if (!pm_runtime_active(scomp->dev))		return; 	data_size = struct_size(data, chanv, scontrol->num_channels);	data = kmalloc(data_size, GFP_KERNEL);	if (!data)		return; 	data->id = cdata->index;	data->num_elems = scontrol->num_channels; 	memcpy(&msg, &cdata->msg, sizeof(msg));	msg.data_ptr = data;	msg.data_size = data_size; 	scontrol->comp_data_dirty = false;	ret = sof_ipc4_set_get_kcontrol_data(scontrol, &msg, false, true);	if (!ret) {		for (i = 0; i < scontrol->num_channels; i++) {			cdata->chanv[i].channel = data->chanv[i].channel;			cdata->chanv[i].value = data->chanv[i].value;		}	} else {		dev_err(scomp->dev, "Failed to read control data for %s\n",			scontrol->name);		scontrol->comp_data_dirty = true;	} 	kfree(data);} static intsof_ipc4_set_bytes_control_data(struct snd_sof_control *scontrol, bool lock){	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;	struct snd_soc_component *scomp = scontrol->scomp;	struct sof_ipc4_control_msg_payload *msg_data;	struct sof_abi_hdr *data = cdata->data;	struct sof_ipc4_msg msg;	size_t data_size;	int ret; 	data_size = struct_size(msg_data, data, data->size);	msg_data = kzalloc(data_size, GFP_KERNEL);	if (!msg_data)		return -ENOMEM; 	msg_data->id = cdata->index;	msg_data->num_elems = data->size;	memcpy(msg_data->data, data->data, data->size); 	memcpy(&msg, &cdata->msg, sizeof(msg));	msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(data->type); 	msg.data_ptr = msg_data;	msg.data_size = data_size; 	ret = sof_ipc4_set_get_kcontrol_data(scontrol, &msg, true, lock);	if (ret < 0)		dev_err(scomp->dev, "%s: Failed to set control update for %s\n",			__func__, scontrol->name); 	kfree(msg_data); 	return ret;} static intsof_ipc4_refresh_bytes_control(struct snd_sof_control *scontrol, bool lock){	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;	struct snd_soc_component *scomp = scontrol->scomp;	struct sof_ipc4_control_msg_payload *msg_data;	struct sof_abi_hdr *data = cdata->data;	struct sof_ipc4_msg msg;	size_t data_size;	int ret = 0; 	if (!scontrol->comp_data_dirty)		return 0; 	if (!pm_runtime_active(scomp->dev))		return 0; 	data_size = scontrol->max_size - sizeof(*data);	if (data_size < sizeof(*msg_data))		data_size = sizeof(*msg_data); 	msg_data = kzalloc(data_size, GFP_KERNEL);	if (!msg_data)		return -ENOMEM; 	memcpy(&msg, &cdata->msg, sizeof(msg));	msg.extension = SOF_IPC4_MOD_EXT_MSG_PARAM_ID(data->type); 	msg_data->id = cdata->index;	msg_data->num_elems = 0; /* ignored for bytes */ 	msg.data_ptr = msg_data;	msg.data_size = data_size; 	scontrol->comp_data_dirty = false;	ret = sof_ipc4_set_get_kcontrol_data(scontrol, &msg, false, lock);	if (!ret) {		if (msg.data_size > scontrol->max_size - sizeof(*data)) {			dev_err(scomp->dev,				"%s: no space for data in %s (%zu, %zu)\n",				__func__, scontrol->name, msg.data_size,				scontrol->max_size - sizeof(*data));			ret = -EINVAL;			goto out;		} 		data->size = msg.data_size;		scontrol->size = sizeof(*cdata) + sizeof(*data) + data->size;		memcpy(data->data, msg.data_ptr, data->size);	} else {		dev_err(scomp->dev, "Failed to read control data for %s\n",			scontrol->name);		scontrol->comp_data_dirty = true;	} out:	kfree(msg_data); 	return ret;} static bool sof_ipc4_switch_put(struct snd_sof_control *scontrol,				struct snd_ctl_elem_value *ucontrol){	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;	struct snd_soc_component *scomp = scontrol->scomp;	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);	struct snd_sof_widget *swidget;	bool widget_found = false;	bool change = false;	unsigned int i;	u32 value;	int ret; 	/* update each channel */	for (i = 0; i < scontrol->num_channels; i++) {		value = ucontrol->value.integer.value[i];		change = change || (value != cdata->chanv[i].value);		cdata->chanv[i].channel = i;		cdata->chanv[i].value = value;	} 	if (!pm_runtime_active(scomp->dev))		return change; 	/* find widget associated with the control */	list_for_each_entry(swidget, &sdev->widget_list, list) {		if (swidget->comp_id == scontrol->comp_id) {			widget_found = true;			break;		}	} 	if (!widget_found) {		dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);		return false;	} 	ret = sof_ipc4_set_generic_control_data(sdev, swidget, scontrol, true);	if (ret < 0)		return false; 	return change;} static int sof_ipc4_switch_get(struct snd_sof_control *scontrol,			       struct snd_ctl_elem_value *ucontrol){	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;	unsigned int i; 	sof_ipc4_refresh_generic_control(scontrol); 	/* read back each channel */	for (i = 0; i < scontrol->num_channels; i++)		ucontrol->value.integer.value[i] = cdata->chanv[i].value; 	return 0;} static bool sof_ipc4_enum_put(struct snd_sof_control *scontrol,			      struct snd_ctl_elem_value *ucontrol){	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;	struct snd_soc_component *scomp = scontrol->scomp;	struct snd_sof_dev *sdev = snd_soc_component_get_drvdata(scomp);	struct snd_sof_widget *swidget;	bool widget_found = false;	bool change = false;	unsigned int i;	u32 value;	int ret; 	/* update each channel */	for (i = 0; i < scontrol->num_channels; i++) {		value = ucontrol->value.enumerated.item[i];		change = change || (value != cdata->chanv[i].value);		cdata->chanv[i].channel = i;		cdata->chanv[i].value = value;	} 	if (!pm_runtime_active(scomp->dev))		return change; 	/* find widget associated with the control */	list_for_each_entry(swidget, &sdev->widget_list, list) {		if (swidget->comp_id == scontrol->comp_id) {			widget_found = true;			break;		}	} 	if (!widget_found) {		dev_err(scomp->dev, "Failed to find widget for kcontrol %s\n", scontrol->name);		return false;	} 	ret = sof_ipc4_set_generic_control_data(sdev, swidget, scontrol, true);	if (ret < 0)		return false; 	return change;} static int sof_ipc4_enum_get(struct snd_sof_control *scontrol,			     struct snd_ctl_elem_value *ucontrol){	struct sof_ipc4_control_data *cdata = scontrol->ipc_control_data;	unsigned int i; 	sof_ipc4_refresh_generic_control(scontrol); 	/* read back each channel */	for (i = 0; i < scontrol->num_channels; i++)		ucontrol->value.enumerated.item[i] = cdata->chanv[i].value; 	return 0;} static int sof_ipc4_set_get_bytes_data(struct snd_sof_dev *sdev,				       struct snd_sof_control *scontrol,

Lines 1–500 of 1037.