/*
 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of The Linux Foundation nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Changes from Qualcomm Innovation Center are provided under the following license:
 * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
 * SPDX-License-Identifier: BSD-3-Clause-Clear
 */

#define LOG_TAG "AHAL: Gain"
#include "PalApi.h"
#include "PalDefs.h"
#include "audio_extn.h"
#include "AudioCommon.h"

extern "C" {

#define CHECK_SERVICES_REGISTERED()                       \
    ({                                                    \
        if(!AudioExtn::isServiceRegistered()) {           \
            AHAL_ERR("service not registered");           \
            return 0;                                     \
        }                                                 \
    })

__attribute__ ((visibility ("default")))
int audio_hw_get_gain_level_mapping(struct pal_amp_db_and_gain_table *mapping_tbl,
                                      int table_size) {
    CHECK_SERVICES_REGISTERED();

    int ret = 0;
    size_t payload_size = 0;
    pal_param_gain_lvl_map_t gain_lvl_map;
    gain_lvl_map.mapping_tbl = mapping_tbl;
    gain_lvl_map.table_size  = table_size;
    gain_lvl_map.filled_size = 0;

    ret = pal_get_param(PAL_PARAM_ID_GAIN_LVL_MAP,
            (void **)&gain_lvl_map,
            &payload_size, nullptr);

    if (ret != 0) {
        AHAL_ERR("fail to get PAL_PARAM_ID_GAIN_LVL_MAP %d", ret);
        gain_lvl_map.filled_size = 0;
    }

    return gain_lvl_map.filled_size;
}

__attribute__ ((visibility ("default")))
bool audio_hw_send_gain_dep_calibration(int level) {
    CHECK_SERVICES_REGISTERED();

    int32_t ret = 0;
    pal_param_gain_lvl_cal_t gain_lvl_cal;
    gain_lvl_cal.level = level;

    ret = pal_set_param(PAL_PARAM_ID_GAIN_LVL_CAL, (void*)&gain_lvl_cal, sizeof(pal_param_gain_lvl_cal_t));
    if (ret != 0) {
        AHAL_ERR("fail to set PAL_PARAM_ID_GAIN_LVL_CAL %d",ret);
    }

    return (ret != 0) ? false: true;
}

__attribute__ ((visibility ("default")))
bool audio_hw_send_linear_gain(int32_t gain) {
    CHECK_SERVICES_REGISTERED();

    int32_t ret = 0;
    pal_param_mspp_linear_gain_t linear_gain;
    linear_gain.gain = gain;

    ret = pal_set_param(PAL_PARAM_ID_MSPP_LINEAR_GAIN, (void*)&linear_gain, sizeof(pal_param_mspp_linear_gain_t));
    if (ret != 0) {
        AHAL_ERR("fail to set PAL_PARAM_ID_MSPP_LINEAR_GAIN %d",ret);
    }

    return (ret != 0) ? false: true;
}

} /* extern "C" */
