// SPDX-License-Identifier: GPL-2.0
/*
 * pp_control_impl.c  --  Mediatek Soundbar kcontrol
 *
 * Copyright (c) 2020 MediaTek Inc.
 * Author: Wen Cai <wen.cai@mediatek.com>
 *
 */
#include "pp_control_impl.h"
#include "kcontrol_uevent.h"

static struct notify_dev kcontrol_data;
#define UEVENT_DEV_NAME "pp"

struct pp_control_data stPpCtrlData;
struct pp_control_data *pstPpCtrlData;

bool _is_dts_vx_config_changed(
		struct dts_vx_config_t *dts_vx_config)
{
	int i = 0;

	for (i = 0; i < MAX_PP_DTS_VIRTUALX_CONFIG; i++) {
		if (pstPpCtrlData->dts_vx_config.config[i]
				!= dts_vx_config->config[i])
			return true;
	}
	return false;
}

int impl_dts_vx_config_put(
		struct dts_vx_config_t *dts_vx_config)
{
	if (pstPpCtrlData && dts_vx_config
			&& _is_dts_vx_config_changed(
			dts_vx_config)) {
		memcpy(&pstPpCtrlData->dts_vx_config,
			dts_vx_config,
			sizeof(struct dts_vx_config_t));
		kcontrol_notify_uevent_integer_array(&kcontrol_data,
			KCONTROL_NOTIFY_PP_DTS_VIRTUALX_CONFIG,
			dts_vx_config->config,
			MAX_PP_DTS_VIRTUALX_CONFIG);
		return true;
	}
	return false;
}

int impl_dts_vx_config_get(
		struct dts_vx_config_t *dts_vx_config)
{
	if (pstPpCtrlData && dts_vx_config) {
		memcpy(dts_vx_config,
			&pstPpCtrlData->dts_vx_config,
			sizeof(struct dts_vx_config_t));
		return true;
	}
	return false;
}

static bool _pp_ctrl_data_init(void)
{
	int i = 0;

	if (pstPpCtrlData)
		return true;

	if (pstPpCtrlData == NULL) {
		pstPpCtrlData = &stPpCtrlData;
		pstPpCtrlData->dts_vx_config.config[0] = 1;
		pstPpCtrlData->dts_vx_config.config[1] = 5;
		pstPpCtrlData->dts_vx_config.config[2] = 1073741823;
		pstPpCtrlData->dts_vx_config.config[3] = 850833021;
		pstPpCtrlData->dts_vx_config.config[4] = 759135469;
		pstPpCtrlData->dts_vx_config.config[5] = 1;
		pstPpCtrlData->dts_vx_config.config[6] = 1;
		pstPpCtrlData->dts_vx_config.config[7] = 1;
		pstPpCtrlData->dts_vx_config.config[8] = 1;
		pstPpCtrlData->dts_vx_config.config[9] = 1;
		pstPpCtrlData->dts_vx_config.config[10] = 0;
		pstPpCtrlData->dts_vx_config.config[11] = 1;
		pstPpCtrlData->dts_vx_config.config[12] = 5;
		pstPpCtrlData->dts_vx_config.config[13] = 65536;
		pstPpCtrlData->dts_vx_config.config[14] = 759135469;
		pstPpCtrlData->dts_vx_config.config[15] = 268435456;
		pstPpCtrlData->dts_vx_config.config[16] = 268435456;
		pstPpCtrlData->dts_vx_config.config[17] = 0;
		pstPpCtrlData->dts_vx_config.config[18] = 0;
		pstPpCtrlData->dts_vx_config.config[19] = 10;
		pstPpCtrlData->dts_vx_config.config[20] = 0;
		pstPpCtrlData->dts_vx_config.config[21] = 10;
		pstPpCtrlData->dts_vx_config.config[22] = 12;
		pstPpCtrlData->dts_vx_config.config[23] = 708669603;
		pstPpCtrlData->dts_vx_config.config[24] = 12;
		pstPpCtrlData->dts_vx_config.config[25] = 708669603;
		pstPpCtrlData->dts_vx_config.config[26] = 12;
		pstPpCtrlData->dts_vx_config.config[27] = 708669603;
		pstPpCtrlData->dts_vx_config.config[28] = 708669603;
		pstPpCtrlData->dts_vx_config.config[29] = 12;
		pstPpCtrlData->dts_vx_config.config[30] = 0;
		pstPpCtrlData->dts_vx_config.config[31] = 9830;
		pstPpCtrlData->dts_vx_config.config[32] = 0;
		pstPpCtrlData->dts_vx_config.config[33] = 0;
		pstPpCtrlData->dts_vx_config.config[34] = 0;
		pstPpCtrlData->dts_vx_config.config[35] = 0;
		pstPpCtrlData->dts_vx_config.config[36] = 0;
		pstPpCtrlData->dts_vx_config.config[37] = 0;
		pstPpCtrlData->dts_vx_config.config[38] = 4194304;
		pstPpCtrlData->dts_vx_config.config[39] = 262766;
		pstPpCtrlData->dts_vx_config.config[40] = 262766;
		pstPpCtrlData->dts_vx_config.config[41] = 262766;
		pstPpCtrlData->dts_vx_config.config[42] = 4194304;
		pstPpCtrlData->dts_vx_config.config[43] = 0;
		pstPpCtrlData->dts_vx_config.config[44] = 0;
		pstPpCtrlData->dts_vx_config.config[45] = 0;
		pstPpCtrlData->dts_vx_config.config[46] = 0;
		pstPpCtrlData->dts_vx_config.config[47] = 0;
		pstPpCtrlData->dts_vx_config.config[48] = 0;
		pstPpCtrlData->dts_vx_config.config[49] = 0;
		pstPpCtrlData->dts_vx_config.config[50] = 0;
		pstPpCtrlData->dts_vx_config.config[51] = 0;
	}

	return true;
}

int impl_pp_initialize(void)
{
	int ret = 0;

	kcontrol_data.name = UEVENT_DEV_NAME;
	kcontrol_data.index = 0;
	ret = kcontrol_uevent_dev_register(&kcontrol_data);
	_pp_ctrl_data_init();
	return 0;
}
