/*
 * Copyright 2004 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/* 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  
 * 02111-1307, USA
 *
 */
 
/*!
 * @defgroup ATLAS_POWER Atlas Power Driver
 * @ingroup ATLASDRVRS
 */

/*!
 * @file atlas_power.h
 * @brief This is the header of Atlas Power driver. 
 *
 * @ingroup ATLAS_POWER
 */

#ifndef         _ATLAS_POWER_H
#define         _ATLAS_POWER_H

/*
 * Includes
 */
#include <asm/ioctl.h>
                
#define         ATLAS_POWER_INIT                       _IOWR('P',1, int)
#define         ATLAS_POWER_FULL_CHECK                 _IOWR('P',2, int)
#define         ATLAS_POWER_CONTROL_CHECK              _IOWR('P',3, int)
#define         ATLAS_SWITCHER_CHECK                   _IOWR('P',4, int)
#define         ATLAS_REGULATOR_CHECK                  _IOWR('P',5, int)

#ifdef __ALL_TRACES__
#define TRACEMSG_POWER(fmt,args...)  printk(fmt,##args)
#else  /* __ALL_TRACES__ */
#define TRACEMSG_POWER(fmt,args...)
#endif /* __ALL_TRACES__ */

/*!
 * This struct list all state reads in Power Up Sense register
 */
struct t_p_up_sense {
        /*! 
         * power up sense ictest
         */
        bool state_ictest;
        /*! 
         * power up sense clksel
         */
        bool state_clksel;
        /*! 
         * power up mode supply 1
         */
        bool state_pums1;
        /*! 
         * power up mode supply 2
         */
        bool state_pums2;
        /*! 
         * power up mode supply 3
         */
        bool state_pums3;
        /*! 
         * power up sense charge mode 0
         */
        bool state_chrgmode0;
        /*! 
         * power up sense charge mode 1
         */
        bool state_chrgmode1;
        /*! 
         * power up sense USB mode
         */
        bool state_umod;
        /*! 
         * power up sense bootmode enable for USB/RS232
         */
        bool state_usben;
        /*! 
         * power up sense switcher 1a1b joined
         */
        bool state_sw_1a1b_joined;
        /*! 
         * power up sense switcher 1a1b joined
         */
        bool state_sw_2a2b_joined;
};

/*!
 * This struct is used to configure a switcher (SW1A, SW1B ...)
 */
struct t_switcher_conf {
        /*! 
         * switcher setting 
         */
        int sw_setting;
        /*! 
         * switcher setting dvs
         */
        int sw_setting_dvs;
        /*! 
         *  switcher standby setting
         */
        int sw_setting_standby;
        /*! 
         * switcher operation mode 
         */
        int sw_op_mode;
        /*! 
         * switcher standby mode 
         */
        int sw_op_mode_standby;
        /*! 
         * switcher dvs speed 
         */
        int sw_dvs_speed;
        /*! 
         * switcher panic mode
         */
        bool sw_panic_mode;
        /*! 
         * switcher soft start 
         */
        bool sw_softstart;
};

/*!
 * This struct is used to configure power cut mode
 */
struct t_power_cut_conf {
        /*! 
         * power cut counter enable 
         */
        bool pc_counter_en;
        /*! 
         * power cut auto user off
         */
        bool pc_auto_user_off;
        /*! 
         * power cut user off 32k enable 
         */
        bool pc_user_off_32k_en;
        /*! 
         * power cut timer 
         */
        int  pc_timer;
        /*! 
         * power cut counter 
         */
        int  pc_counter;
        /*! 
         * power cut max number pc 
         */
        int  pc_max_nb_pc;
        /*! 
         * power cut ext timer 
         */
        int  pc_ext_timer;
        /*! 
         * power cut timer infini
         */
        bool  pc_ext_timer_inf;
};

struct t_vbkup_conf {
        /*! 
         * Vbackup enable 
         */
        bool vbkup_en;
        /*! 
         * Vbackup automatically enable 
         */
        bool vbkup_auto_en;
        /*! 
         * Vbackup voltage 
         */
        int  vbkup_voltage;
};

struct t_pll_conf {
        /*! 
         * PLL enable 
         */
        bool pll_en;
        /*! 
         * PLL factor 
         */
        int pll_factor;
};

struct t_sw3_conf {
        /*! 
         * Switcher 3 enable 
         */
        bool sw3_en;
        /*! 
         * Switcher 3 setting  
         */
        int  sw3_setting;
        /*! 
         * Switcher 3 control stanby  
         */
        bool  sw3_ctr_stby;
        /*! 
         * Switcher 3 operation mode  
         */
        bool  sw3_op_mode;
};

/*!
 * This enumeration define all switcher
 */
typedef enum {
        /*! 
         * VBKUP1 
         */
        VBKUP_1=0,
        /*! 
         * VBKUP2 
         */
        VBKUP_2,
} t_vbkup;

/*!
 * This enumeration define all On_OFF button
 */
typedef enum {
        /*! 
         * ON1B 
         */
        BT_ON1B=0,
        /*! 
         * ON2B 
         */
        BT_ON2B,
        /*! 
         * ON3B 
         */
        BT_ON3B,
} t_button;

/*!
 * This enumeration define all switcher
 */
typedef enum {
        /*! 
         * SW1A 
         */
        SW_SW1A=0,
        /*! 
         * SW1B 
         */
        SW_SW1B,
        /*! 
         * SW2A 
         */
        SW_SW2A,
        /*! 
         * SW2B 
         */
        SW_SW2B,
        
} t_switcher;

/*!
 * This enumeration define all regulator enabled by regen
 */
typedef enum {
        /*! 
         * VAudio 
         */
        REGU_VAUDIO=0,
        /*! 
         * VIOHI 
         */
        REGU_VIOHI,
        /*! 
         * VIOLO 
         */
        REGU_VIOLO,
        /*! 
         * VDIG 
         */
        REGU_VDIG,
        /*! 
         * VGEN 
         */
        REGU_VGEN,
        /*! 
         * VRFDIG 
         */
        REGU_VRFDIG, /*5*/
        /*! 
         * VRFREF 
         */
        REGU_VRFREF,
        /*! 
         * VRFCP
         */
        REGU_VRFCP,
        /*! 
         * VSIM
         */
        REGU_VSIM,
        /*! 
         * VESIM
         */
        REGU_VESIM,
        /*! 
         * VCAM
         */
        REGU_VCAM, /*10*/
        /*! 
         * VRFBG
         */
        REGU_VRFBG,
        /*! 
         * VVIB
         */
        REGU_VVIB,
        /*! 
         * VRF1
         */
        REGU_VRF1,
        /*! 
         * VRF2
         */
        REGU_VRF2,
        /*! 
         * VMMC1
         */
        REGU_VMMC1,
        /*! 
         * VMMC2
         */
        REGU_VMMC2,
        /*! 
         * GPO1
         */
        REGU_GPO1,
        /*! 
         * GPP2
         */
        REGU_GPO2,
        /*! 
         * GPO3
         */
        REGU_GPO3,
        /*! 
         * GPO4
         */
        REGU_GPO4,
        /*! 
         * REGU_NUMBER
         */
        REGU_NUMBER,
} t_regulator;

/*!
 * This enumeration define all power interrupts 
 */
typedef enum {
        /*!
         * BP turn on threshold detection  
         */
        PWR_IT_BPONI=0,
        /*! 
         * End of life / low battery detect  
         */
        PWR_IT_LOBATLI,	
        /*!
         * Low battery warning
         */
        PWR_IT_LOBATHI,
        /*!
         * ON1B event
         */
        PWR_IT_ONOFD1I,
        /*!
         * ON2B event
         */
        PWR_IT_ONOFD2I,
        /*!
         * ON3B event
         */
        PWR_IT_ONOFD3I,
        /*!
         * System reset
         */
        PWR_IT_SYSRSTI,
        /*!
         * Power ready 
         */
        PWR_IT_PWRRDYI,
        /*!
         * Power cut event
         */
        PWR_IT_PCI,
        /*!
         * Warm start event 
         */
        PWR_IT_WARMI,
        /*!
         * Memory hold event  
         */
} t_pwr_int;

/*
 * POWER Atlas API
 */

/* EXPORTED FUNCTIONS */

/*!
 * This function returns power up sense value
 *
 * @param        p_up_sense  	value of power up sense
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_power_mode_sense(struct t_p_up_sense *p_up_sense);

/*!
 * This function configures the Regen assignment for all regulator
 *
 * @param        regu     	type of regulator
 * @param        en_dis  	if true, the regulator is enabled by regen.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_set_regen_assig(t_regulator regu, bool en_dis);

/*!
 * This function gets the Regen assignment for all regulator
 *
 * @param        regu     	type of regulator
 * @param        en_dis  	return value, if true :
 *                              the regulator is enabled by regen.
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_regen_assig(t_regulator regu, bool *en_dis);

/*!
 * This function sets the Regen polarity.
 *
 * @param        en_dis  	If true regen is inverted.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_set_regen_inv(bool en_dis);

/*!
 * This function gets the Regen polarity.
 *
 * @param        en_dis  	If true regen is inverted.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_regen_inv(bool *en_dis);

/*!
 * This function configures switcher.
 *
 * @param        sw  	define type od switcher.
 * @param        sw_conf define new configuration.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_conf_switcher(t_switcher sw, struct t_switcher_conf *sw_conf);

/*!
 * This function returns configuration of one switcher.
 *
 * @param        sw  	define type od switcher.
 * @param        sw_conf return value of configuration.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_switcher(t_switcher sw, struct t_switcher_conf *sw_conf);

/*!
 * This function enables or disables a regulator.
 *
 * @param        regu           define the regulator.
 * @param        en_dis  	If true, enable the regulator.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_regu_en(t_regulator regu, bool en_dis);

/*!
 * This function returns state of regulator.
 *
 * @param        regu           define the regulator.
 * @param        en_dis  	If true, regulator is enabled.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_regu_state(t_regulator regu, bool *en_dis);

/*!
 * This function configures a regulator.
 *
 * @param        regu           define the regulator.
 * @param        stby  	        If true, the regulator is controlled by standby.
 * @param        mode  	        configure the regulator operating mode.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_conf_regu(t_regulator regu, bool stby, bool mode);

/*!
 * This function gets configuration of one regulator.
 *
 * @param        regu           define the regulator.
 * @param        stby  	        If true, the regulator is controlled by standby.
 * @param        mode  	        return the regulator operating mode.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_regu_conf(t_regulator regu, bool *stby, bool *mode);

/*!
 * This function sets value of regulator setting.
 *
 * @param        regu           define the regulator.
 * @param        setting        define the regulator setting.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_set_regu(t_regulator regu, int setting);

/*!
 * This function gets value of regulator setting.
 *
 * @param        regu           define the regulator.
 * @param        setting        return the regulator setting.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_regu(t_regulator regu, int *setting);

/*!
 * This function enables power cut mode.
 *
 * @param        en           if true, power cut enable.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_cut_en(bool en);

/*!
 * This function gets power cut mode.
 *
 * @param        en           if true, power cut is enabled.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_power_cut(bool *en);

/*!
 * This function enables warm start mode.
 *
 * @param        en           if true, warm start enable.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_warm_start_en(bool en);

/*!
 * This function gets warm start mode.
 *
 * @param        en           if true, warm start is enabled.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_warm_start(bool *en);

/*!
 * This function send SPI command for entering user off modes.
 *
 * @param        en           if true, entering user off modes
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_user_off_en(bool en);

/*!
 * This function returns value of user off modes SPI command.
 *
 * @param        en           if true, user off modes is started
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_user_off_en(bool *en);

/*!
 * This function enables CLK32KMCU clock.
 *
 * @param        en           if true, enable CLK32K
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_32k_en(bool en);

/*!
 * This function gets state of CLK32KMCU clock.
 *
 * @param        en           if true, CLK32K is enabled
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_32k_state(bool *en);

#ifdef __ATLAS_2_0_

/*!
 * This function enables automatically VBKUP2 in the memory hold modes.
 * Only on Atlas 2.0 or higher
 *
 * @param        en           if true, enable VBKUP2AUTOMH
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_vbkup2_auto_en(bool en);

/*!
 * This function gets state of automatically VBKUP2AUTOMH.
 * Only on Atlas 2.0 or higher
 *
 * @param        en           if true, CLK32K is enabled
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_vbkup2_auto_state(bool *en);

/*!
 * This function enables battery detect function.
 * Only on Atlas 2.0 or higher
 *
 * @param        en           if true, enable BATTDETEN
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_bat_det_en(bool en);

/*!
 * This function gets state of battery detect function.
 * Only on Atlas 2.0 or higher
 *
 * @param        en           if true, BATTDETEN is enabled
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_bat_det_state(bool *en);

/*!
 * This function enables esim control voltage.
 * Only on Atlas 2.0 or higher
 *
 * @param        vesim          if true, enable VESIMESIMEN
 * @param        vmmc1          if true, enable VMMC1ESIMEN
 * @param        vmmc2          if true, enable VMMC2ESIMEN
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_esim_v_en(bool vesim, bool vmmc1, bool vmmc2);

/*!
 * This function gets esim control voltage values.
 * Only on Atlas 2.0 or higher
 *
 * @param        vesim          if true, enable VESIMESIMEN
 * @param        vmmc1          if true, enable VMMC1ESIMEN
 * @param        vmmc2          if true, enable VMMC2ESIMEN
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_gets_esim_v_state(bool* vesim, bool* vmmc1, bool* vmmc2);

/*!
 * This function enables control of VVIB by VIBEN pin.
 * Only on Atlas 2.0 or higher
 *
 * @param        en           if true, enable VIBPINCTRL
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_vib_pin_en(bool en);

/*!
 * This function gets state of control of VVIB by VIBEN pin.
 * Only on Atlas 2.0 or higher
 * @param        en           if true, VIBPINCTRL is enabled
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_gets_vib_pin_state(bool *en);

#endif
 
/*!
 * This function configures power cut mode.
 *
 * @param        pc  	define configuration of power cut mode.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_cut_conf(struct t_power_cut_conf *pc);

/*!
 * This function gets configuration of power cut mode.
 *
 * @param        pc  	return configuration of power cut mode.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_cut_get_conf(struct t_power_cut_conf *pc);

/*!
 * This function configures v-backup during power cut.
 *
 * @param        vbkup  	type of VBLUP (1 or2).
 * @param        vbkup_conf  	define configuration of VBLUP.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_vbkup_conf(t_vbkup vbkup, struct t_vbkup_conf *vbkup_conf);

/*!
 * This function gets configuration of v-backup.
 *
 * @param        vbkup  	type of VBLUP (1 or2).
 * @param        vbkup_conf  	return configuration of VBLUP.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_vbkup_conf(t_vbkup vbkup, struct t_vbkup_conf *vbkup_conf);

/*!
 * This function sets BP detection threshold.
 *
 * @param        threshold           define threshold
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_set_bp_threshold(int threshold);

/*!
 * This function gets BP detection threshold.
 *
 * @param        threshold           return threshold
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_bp_threshold(int *threshold);

/*!
 * This function selects EOL function instead of LOBAT.
 *
 * @param        eol          If true, selects EOL function
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_set_eol_function(bool eol);

/*!
 * This function gets EOL function instead of LOBAT.
 *
 * @param        eol          If true, selects EOL function
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_eol_function(bool *eol);

/*!
 * This function sets coincell charger voltage setting.
 *
 * @param        voltage         value of voltage setting
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_set_coincell_voltage(int voltage);

/*!
 * This function gets coincell charger voltage setting.
 *
 * @param        voltage         return value of voltage setting
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_coincell_voltage(int *voltage);

/*!
 * This function enables coincell charger.
 *
 * @param        en         if true, enable the coincell charger
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_coincell_charger_en(bool en);

/*!
 * This function gets state of coincell charger.
 *
 * @param        en         if true, the coincell charger is enabled
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_coincell_charger_en(bool *en);

/*!
 * This function enables auto reset after a system reset.
 *
 * @param        en         if true, the auto reset is enabled
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_set_auto_reset_en(bool en);

/*!
 * This function gets auto reset configuration.
 *
 * @param        en         if true, the auto reset is enabled
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_auto_reset_en(bool *en);

/*!
 * This function configures a system reset on a button.
 *
 * @param       bt         type of button.
 * @param       sys_rst    if true, enable the system reset on this button
 * @param       deb_time   sets the debounce time on this button pin
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_set_conf_button(t_button bt, bool sys_rst, int deb_time);

/*!
 * This function gets configuration of a button.
 *
 * @param       bt         type of button.
 * @param       sys_rst    if true, the system reset is enabled on this button
 * @param       deb_time   gets the debounce time on this button pin
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_conf_button(t_button bt, bool *sys_rst, int *deb_time);

/*!
 * This function configures switcher PLL.
 *
 * @param       pll_conf   configuration of PLL.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_set_conf_pll(struct t_pll_conf *pll_conf);

/*!
 * This function gets configuration of switcher PLL.
 *
 * @param       pll_conf   configuration of PLL.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_conf_pll(struct  t_pll_conf *pll_conf);

/*!
 * This function configures SW3 switcher.
 *
 * @param       sw3_conf   configuration of sw3 switcher.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_set_sw3(struct  t_sw3_conf *sw3_conf);

/*!
 * This function gets configuration of SW3 switcher.
 *
 * @param       sw3_conf   configuration of sw3 switcher.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_get_sw3(struct  t_sw3_conf *sw3_conf);

/*!
 * This function is used to subscribe on power event IT.
 *
 * @param        event  	type of event.
 * @param        callback  	event callback function.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_event_sub(t_pwr_int event, void* callback);

/*!
 * This function is used to un subscribe on power event IT.
 *
 * @param        event  	type of event.
 * @param        callback  	event callback function.
 *
 * @return       This function returns 0 if successful.
 */
int atlas_power_event_unsub(t_pwr_int event, void* callback);

#endif /* _ATLAS_POWER_H */
