/*******************************************************************************
 *   @file   adc_core.c
 *   @brief  Implementation of ADC Core Driver.
 *   @author DBogdan (dragos.bogdan@analog.com)
 *******************************************************************************
 * Copyright 2013(c) Analog Devices, Inc.
 *
 * 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 Analog Devices, Inc. nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *  - The use of this software may or may not infringe the patent rights
 *    of one or more patent holders.  This license does not release you
 *    from the requirement that you obtain separate licenses from these
 *    patent holders to use this software.
 *  - Use of the software either in source or binary form, must be run
 *    on or directly connected to an Analog Devices Inc. component.
 *
 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, 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.
 ******************************************************************************/

/******************************************************************************/
/***************************** Include Files **********************************/
/******************************************************************************/

#include <stdint.h>

#include "adc_core.h"
#include "platform.h"
#include "util.h"

/*******************************************************************************
 * @brief adc_read
 ******************************************************************************/
int adc_read(struct ad9361_rf_phy *phy, uint32_t regAddr, uint32_t *data)
{
    return axiadc_read(phy->adc_state, regAddr, data);
}

/*******************************************************************************
 * @brief adc_write
 ******************************************************************************/
int adc_write(struct ad9361_rf_phy *phy, uint32_t regAddr, uint32_t data)
{
    return axiadc_write(phy->adc_state, regAddr, data);
}

/*******************************************************************************
 * @brief adc_init
 ******************************************************************************/
int adc_init(struct ad9361_rf_phy *phy)
{
    int ret;

    ret = adc_write(phy, ADC_REG_RSTN, 0);
    if (ret < 0) {
        return ret;
    }

    ret = adc_write(phy, ADC_REG_RSTN, ADC_RSTN);
    if (ret < 0) {
        return ret;
    }

    ret = adc_write(phy, ADC_REG_CHAN_CNTRL(0),
                    ADC_IQCOR_ENB | ADC_FORMAT_SIGNEXT | ADC_FORMAT_ENABLE |
                        ADC_ENABLE);
    if (ret < 0) {
        return ret;
    }

    ret = adc_write(phy, ADC_REG_CHAN_CNTRL(1),
                    ADC_IQCOR_ENB | ADC_FORMAT_SIGNEXT | ADC_FORMAT_ENABLE |
                        ADC_ENABLE);
    if (ret < 0) {
        return ret;
    }

    if (phy->pdata->rx2tx2) {
        ret = adc_write(phy, ADC_REG_CHAN_CNTRL(2),
                        ADC_IQCOR_ENB | ADC_FORMAT_SIGNEXT | ADC_FORMAT_ENABLE |
                            ADC_ENABLE);
        if (ret < 0) {
            return ret;
        }

        ret = adc_write(phy, ADC_REG_CHAN_CNTRL(3),
                        ADC_IQCOR_ENB | ADC_FORMAT_SIGNEXT | ADC_FORMAT_ENABLE |
                            ADC_ENABLE);
        if (ret < 0) {
            return ret;
        }
    }

    return 0;
}

/*******************************************************************************
 * @brief adc_set_calib_scale_phase
 ******************************************************************************/
int32_t adc_set_calib_scale_phase(struct ad9361_rf_phy *phy,
                                  uint32_t phase,
                                  uint32_t chan,
                                  int32_t val,
                                  int32_t val2)
{
    int ret;
    uint32_t fract;
    uint64_t llval;
    uint32_t tmp;

    switch (val) {
        case 1:
            fract = 0x4000;
            break;
        case -1:
            fract = 0xC000;
            break;
        case 0:
            fract = 0;
            if (val2 < 0) {
                fract = 0x8000;
                val2 *= -1;
            }
            break;
        default:
            return -1;
    }

    llval = (uint64_t)val2 * 0x4000UL + (1000000UL / 2);
    do_div(&llval, 1000000UL);
    fract |= llval;

    ret = adc_read(phy, ADC_REG_CHAN_CNTRL_2(chan), &tmp);
    if (ret < 0) {
        return ret;
    }

    if (!((chan + phase) % 2)) {
        tmp &= ~ADC_IQCOR_COEFF_1(~0);
        tmp |= ADC_IQCOR_COEFF_1(fract);
    } else {
        tmp &= ~ADC_IQCOR_COEFF_2(~0);
        tmp |= ADC_IQCOR_COEFF_2(fract);
    }

    ret = adc_write(phy, ADC_REG_CHAN_CNTRL_2(chan), tmp);
    if (ret < 0) {
        return ret;
    }

    return 0;
}

/*******************************************************************************
 * @brief adc_get_calib_scale_phase
 ******************************************************************************/
int32_t adc_get_calib_scale_phase(struct ad9361_rf_phy *phy,
                                  uint32_t phase,
                                  uint32_t chan,
                                  int32_t *val,
                                  int32_t *val2)
{
    int ret;
    uint32_t tmp;
    int32_t sign;
    uint64_t llval;

    ret = adc_read(phy, ADC_REG_CHAN_CNTRL_2(chan), &tmp);
    if (ret < 0) {
        return ret;
    }

    /* format is 1.1.14 (sign, integer and fractional bits) */

    if (!((phase + chan) % 2)) {
        tmp = ADC_TO_IQCOR_COEFF_1(tmp);
    } else {
        tmp = ADC_TO_IQCOR_COEFF_2(tmp);
    }

    if (tmp & 0x8000)
        sign = -1;
    else
        sign = 1;

    if (tmp & 0x4000)
        *val = 1 * sign;
    else
        *val = 0;

    tmp &= ~0xC000;

    llval = tmp * 1000000ULL + (0x4000 / 2);
    do_div(&llval, 0x4000);
    if (*val == 0)
        *val2 = (int32_t)llval * sign;
    else
        *val2 = (int32_t)llval;

    return 0;
}

/*******************************************************************************
 * @brief adc_set_calib_scale
 ******************************************************************************/
int32_t adc_set_calib_scale(struct ad9361_rf_phy *phy,
                            uint32_t chan,
                            int32_t val,
                            int32_t val2)
{
    return adc_set_calib_scale_phase(phy, 0, chan, val, val2);
}

/*******************************************************************************
 * @brief adc_get_calib_scale
 ******************************************************************************/
int32_t adc_get_calib_scale(struct ad9361_rf_phy *phy,
                            uint32_t chan,
                            int32_t *val,
                            int32_t *val2)
{
    return adc_get_calib_scale_phase(phy, 0, chan, val, val2);
}

/*******************************************************************************
 * @brief adc_set_calib_phase
 ******************************************************************************/
int32_t adc_set_calib_phase(struct ad9361_rf_phy *phy,
                            uint32_t chan,
                            int32_t val,
                            int32_t val2)
{
    return adc_set_calib_scale_phase(phy, 1, chan, val, val2);
}

/*******************************************************************************
 * @brief adc_get_calib_phase
 ******************************************************************************/
int32_t adc_get_calib_phase(struct ad9361_rf_phy *phy,
                            uint32_t chan,
                            int32_t *val,
                            int32_t *val2)
{
    return adc_get_calib_scale_phase(phy, 1, chan, val, val2);
}
