/*******************************************************************************
 * DISCLAIMER
 * This software is supplied by Renesas Electronics Corporation and is only
 * intended for use with Renesas products. No other uses are authorized. This
 * software is owned by Renesas Electronics Corporation and is protected under
 * all applicable laws, including copyright laws.
 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING
 * THIS SOFTWARE, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING BUT NOT
 * LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 * AND NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.
 * TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY LAW, NEITHER RENESAS
 * ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE
 * FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR
 * ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR ITS AFFILIATES HAVE
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * Renesas reserves the right, without notice, to make changes to this software
 * and to discontinue the availability of this software. By using this software,
 * you agree to the additional terms and conditions found by accessing the
 * following link:
 * http://www.renesas.com/disclaimer
 * Copyright 2023 Renesas Electronics Corporation All rights reserved.
 *******************************************************************************/

/*******************************************************************************
 * DESCRIPTION   : AVS driver
 ******************************************************************************/
/******************************************************************************
 * @file          avs.c
 * - Version      : 0.01
 * @brief         AVS driver.
 * .
 *****************************************************************************/
/******************************************************************************
 * History : DD.MM.YYYY Version  Description
 *         : 16.11.2023 0.01     First Release
 *****************************************************************************/

#include <stdint.h>
#include <avs.h>
#include <i2c.h>
#include <log.h>
#include <mem_io.h>
#include <remap_register.h>

#define AVS_BASE                (BASE_AVS_ADDR) /* Physical address:0xE60A0000, Logical address:0xFDAA0000 */
#define AVS_ADVADJP             (AVS_BASE + 0x0080U)
#define ADVADJP_VOLCOND_MASK    (0x000001FFU)

#define VOLCOND_NUM             (5U)    /* Array number */
#define VOLCOND_FLAG_4          (4U)
#define VOLCOND_FLAG_2          (2U)

/* I2C Slave Address */
#define SLAVE_RW_ADDR           (0x000000C8U)

/* PMIC register Address */
#define BUCK1_DVS0CFG1          (0x00000072U)
#define BUCK1_DVS0CFG0          (0x00000073U)
#define DVS_CFG_NUM             (2U)    /* Array number */

/* PMIC register setting value */
#define BUCK1_DVS0CFG1_VOLCOND2 (0x0000009FU)   /* Setting value for 0.7575[V] */
#define BUCK1_DVS0CFG0_VOLCOND2 (0x000000C0U)   /* Setting value for 0.7575[V] */
#define BUCK1_DVS0CFG1_VOLCOND4 (0x0000009AU)   /* Setting value for 0.7325[V] */
#define BUCK1_DVS0CFG0_VOLCOND4 (0x00000080U)   /* Setting value for 0.7325[V] */

void avs_low_power_mode_setting(void)
{
    uint32_t volcond;

    /* Initialize I2C ch3. */
    i2c3_init();

    /* Confirm VOLCOND in ADVADJP register. */
    volcond  = mem_read32(AVS_ADVADJP);
    volcond &= ADVADJP_VOLCOND_MASK;

    NOTICE("Low Power Mode setting(AVS) VOLCOND=%d\n", volcond);
    switch (volcond)
    {
        case VOLCOND_FLAG_2:
        {
            /* In case of VOLCOND=2, set supply voltage to 0.7575[V]. */
            i2c3_write(SLAVE_RW_ADDR, BUCK1_DVS0CFG1, BUCK1_DVS0CFG1_VOLCOND2);
            i2c3_write(SLAVE_RW_ADDR, BUCK1_DVS0CFG0, BUCK1_DVS0CFG0_VOLCOND2);
            INFO("VOLCOND=0x%x SET Slave=0x%x Register=0x%x Value=0x%x\n",
                volcond, SLAVE_RW_ADDR, BUCK1_DVS0CFG1, BUCK1_DVS0CFG1_VOLCOND2);
            INFO("VOLCOND=0x%x SET Slave=0x%x Register=0x%x Value=0x%x\n",
                volcond, SLAVE_RW_ADDR, BUCK1_DVS0CFG0, BUCK1_DVS0CFG0_VOLCOND2);
            break;
        }
        case VOLCOND_FLAG_4:
        {
            /* In case of VOLCOND=4, set supply voltage to 0.7325[V]. */
            i2c3_write(SLAVE_RW_ADDR, BUCK1_DVS0CFG1, BUCK1_DVS0CFG1_VOLCOND4);
            i2c3_write(SLAVE_RW_ADDR, BUCK1_DVS0CFG0, BUCK1_DVS0CFG0_VOLCOND4);
            INFO("VOLCOND=0x%x SET Slave=0x%x Register=0x%x Value=0x%x\n",
                volcond, SLAVE_RW_ADDR, BUCK1_DVS0CFG1, BUCK1_DVS0CFG1_VOLCOND4);
            INFO("VOLCOND=0x%x SET Slave=0x%x Register=0x%x Value=0x%x\n",
                volcond, SLAVE_RW_ADDR, BUCK1_DVS0CFG0, BUCK1_DVS0CFG0_VOLCOND4);
            break;
        }
        default:
        {
            /* Other than VOLCOND = 2 or 4, nothing to do. */
            break;
        }
    }

    /* Release I2C ch3 */
    i2c3_release();
}
/* End of function avs_low_power_mode_setting(void) */


