static void bq2416x_start_ac_charger(struct bq2416x_device_info *di)
{
   long int  events = BQ2416x_START_AC_CHARGING;
	   
   /*set gpio_174 low level for CD pin to enable bq24161 IC*/
	gpio_set_value(ENABLE_BQ2416x_CHARGER, 0);

	blocking_notifier_call_chain(&notifier_list, events, NULL);

	di->charger_source = POWER_SUPPLY_TYPE_MAINS;
	di->charge_status = POWER_SUPPLY_STATUS_CHARGING;
	di->calling_limit = 0;
    di->factory_flag = 0;

    di->battery_temp_status = -1;

	di->dppm_voltagemV = VINDPM_MIN_4200;
	di->cin_limit = di->max_cin_currentmA;
	di->currentmA = di->max_currentmA ;
    di->voltagemV = di->max_voltagemV;
    di->term_currentmA = ITERM_MIN_50;
	di->enable_ce = ENABLE_CHARGER;     /*enable charger*/
	di->enable_iterm = ENABLE_ITERM;    /*enable charge current termination*/
    di->hz_mode = DIS_HIZ;
    di->safety_timer = TMR_X_9;
    di->enable_low_chg = DISABLE_LOW_CHG;

	bq2416x_config_control_reg(di);
	bq2416x_config_voltage_reg(di);
	bq2416x_config_current_reg(di);
    bq2416x_config_dppm_voltage_reg(di,di->dppm_voltagemV);
    bq2416x_config_safety_reg(di);
    bq2416x_config_watchdog_reg(di);

	schedule_delayed_work(&di->bq2416x_charger_work,
						msecs_to_jiffies(0));

	dev_info(di->dev,"%s, ---->START AC CHARGING, \n"
	                  "battery current = %d mA\n"
	                  "battery voltage = %d mV\n"
	                  , __func__, di->currentmA, di->voltagemV);
    di->battery_present = is_bq27510_battery_exist(g_battery_measure_by_bq27510_device);
	if (!di->battery_present){
		dev_info(di->dev, "BATTERY NOT DETECTED!\n");
		events = BQ2416x_NOT_CHARGING;
		blocking_notifier_call_chain(&notifier_list, events, NULL);
		di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
	}
    di->wakelock_enabled = 1;
    if (di->wakelock_enabled)
        wake_lock(&di->charger_wake_lock);
	return;
}
void bq2416x_open_inner_fet(struct bq2416x_device_info *di)
{
    	u8 en_nobatop = 0;
		
		bq2416x_read_byte(di, &en_nobatop, REG_BATTERY_AND_SUPPLY_STATUS);
       if(g_battery_measure_by_bq27510_device &&
          is_bq27510_battery_exist(g_battery_measure_by_bq27510_device) ){
        	di->enable_iterm = ENABLE_ITERM;
        	en_nobatop = en_nobatop & (~EN_NOBATOP);
        }else {
        	di->enable_iterm = DISABLE_ITERM;
        	en_nobatop = en_nobatop | EN_NOBATOP;
        }
	bq2416x_config_control_reg(di);
	bq2416x_write_byte(di, en_nobatop, REG_BATTERY_AND_SUPPLY_STATUS);
}
static void
bq2416x_charger_update_status(struct bq2416x_device_info *di)
{
	u8 read_reg[8] = {0};

	timer_fault = 0;
	bq2416x_read_block(di, &read_reg[0], 0, 8); 

	if ((read_reg[0] & 0x70) == 0x50)   
		dev_dbg(di->dev, "CHARGE DONE\n");

	if ((read_reg[0] & 0x7) == 0x4) 
		timer_fault = 1;
     
	if (read_reg[0] & 0x7) {
		di->cfg_params = 1;
		dev_err(di->dev, "CHARGER STATUS = %x\n", read_reg[0]);
	}

    if ((read_reg[1] & 0x6) == 0x2) {
        di->hz_mode = 1;
        bq2416x_config_control_reg(di);
        bq2416x_write_byte(di, di->voltage_reg, REG_BATTERY_VOLTAGE);
        dev_err(di->dev, "battery ovp = %x,%x\n", read_reg[1],read_reg[3]);
        msleep(700);
        di->hz_mode = 0;
        bq2416x_config_control_reg(di);
    }

	if (is_bq27510_battery_exist(g_battery_measure_by_bq27510_device)){
		bq2416x__charge_status(di);
	}

	if ((timer_fault == 1) || (di->cfg_params == 1)) {
		bq2416x_write_byte(di, di->control_reg, REG_CONTROL_REGISTER);
		bq2416x_write_byte(di, di->voltage_reg, REG_BATTERY_VOLTAGE);
		bq2416x_write_byte(di, di->current_reg, REG_BATTERY_CURRENT);
		bq2416x_write_byte(di, di->dppm_reg, REG_DPPM_VOLTAGE);
		bq2416x_config_safety_reg(di); 
		di->cfg_params = 0;
	}

	/* reset 32 second timer */
	bq2416x_config_status_reg(di);

	return;
}
static void bq2416x_charger_work(struct work_struct *work)
{
	struct bq2416x_device_info *di = container_of(work,
		struct bq2416x_device_info, bq2416x_charger_work.work);

    di->battery_present = is_bq27510_battery_exist(g_battery_measure_by_bq27510_device);

	bq2416x_open_inner_fet(di);

    bq2416x_monitor_battery_ntc_charging(di);

	bq2416x_charger_update_status(di);

	schedule_delayed_work(&di->bq2416x_charger_work,
						msecs_to_jiffies(BQ2416x_WATCHDOG_TIMEOUT));

}
static void bq2416x_start_ac_charger(struct bq2416x_device_info *di)
{
       long int  events;
	   
      /*set gpio_174 low level for CD pin to enable bq24161 IC*/
	gpio_set_value(BQ2416X_GPIO_174, 0);
	events = BQ2416x_START_AC_CHARGING;
	blocking_notifier_call_chain(&notifier_list, events, NULL);
	di->enable_ce = ENABLE_CE;    /*enable charger*/
	di->enable_iterm = ENABLE_ITERM; /*enable charge current termination*/
	
	if(! g_battery_measure_by_bq27510_device)
        	return ;
	di->charger_source = POWER_SUPPLY_TYPE_MAINS;
	di->charge_status = POWER_SUPPLY_STATUS_CHARGING;

	di->calling_limit = set_zero;
	di->dppm_voltagemV = VOLT_DPPM_ADJUST_AC;
	di->cin_limit = CURRENT_AC_LIMIT_IN;
	di->currentmA = di->max_currentmA ;
	bq2416x_config_control_reg(di);
	bq2416x_config_voltage_reg(di);
	bq2416x_config_current_reg(di);
    bq2416x_config_dppm_voltage_reg(di,di->dppm_voltagemV);
#if BQ2416X_USE_WAKE_LOCK
	wake_lock(&di->charger_wake_lock);
#endif
	schedule_delayed_work(&di->bq2416x_charger_work,
						msecs_to_jiffies(0));

	dev_info(di->dev,"%s, ---->START AC CHARGING, \n"
	                  "battery current = %d mA\n"
	                  "battery voltage = %d mV\n"
	                  , __func__, di->currentmA, di->voltagemV);

	if (!is_bq27510_battery_exist(g_battery_measure_by_bq27510_device)){
		dev_dbg(di->dev, "BATTERY NOT DETECTED!\n");
		events = BQ2416x_NOT_CHARGING;
		blocking_notifier_call_chain(&notifier_list, events, NULL);
		di->charge_status = POWER_SUPPLY_STATUS_NOT_CHARGING;
		di->enable_low_chg = DISABLE_LOW_CHG;
		bq2416x_config_safety_reg(di);
	}
}
/*small current charging(100mA) when low battery voltage or low battery temprature*/
static void
bq2416x_low_current_charge(struct bq2416x_device_info *di)
{
	int battery_voltage = 0;
	int battery_temperature = 0;
	if (!is_bq27510_battery_exist(g_battery_measure_by_bq27510_device))
		return;

		battery_voltage = bq27510_battery_voltage(g_battery_measure_by_bq27510_device);
		battery_temperature = bq27510_battery_temperature(g_battery_measure_by_bq27510_device);

		if (battery_temperature < BQ2416x_COLD_BATTERY_THRESHOLD){
			dev_dbg(di->dev, "battery temp less than -10 degree,disable charging\n");
			di->enable_ce = DISABLE_CE;
			if (di->charger_source == POWER_SUPPLY_TYPE_MAINS){
			   if(di->calling_limit){
					di->cin_limit = CURRENT_AC_LIMIT_IN_800;
			   }
			   else{
					di->cin_limit = CURRENT_AC_LIMIT_IN;
			   }
			}

			else
				di->cin_limit = CURRENT_USB_LIMIT_IN;
		}
		else if ((battery_temperature >= BQ2416x_COLD_BATTERY_THRESHOLD)
			&& (battery_temperature < BQ2416x_COOL_BATTERY_THRESHOLD)){
			/*battery temp is between -10 and 0 degree,or battery voltage is less than 3.0V*/
			di->enable_low_chg = ENABLE_LOW_CHG;/*enable low charge,100mA charging*/
			bq2416x_config_safety_reg(di);
			if (di->charger_source == POWER_SUPPLY_TYPE_MAINS){
			   if(di->calling_limit){
					di->cin_limit = CURRENT_AC_LIMIT_IN_800;
			   }
			   else{
					di->cin_limit = CURRENT_AC_LIMIT_IN;
			   }
			}
			else
				di->cin_limit = CURRENT_USB_LIMIT_IN;
			if(battery_voltage < BQ2416x_LOW_TEMP_TERM_VOLTAGE){
				di->enable_ce = ENABLE_CE;
			}
			else{
				di->enable_ce = DISABLE_CE;
			}
		}
		else if ((battery_temperature >= BQ2416x_COOL_BATTERY_THRESHOLD)
		    && (battery_temperature < (BQ2416x_WARM_BATTERY_THRESHOLD - TEMPERATURE_OFFSET))){
			/*battery temp is between 00 and 50 degree,normal charge*/
			di->enable_low_chg = DISABLE_LOW_CHG;/*normal charge*/
			bq2416x_config_safety_reg(di);
			di->enable_ce = ENABLE_CE;
			if (di->charger_source == POWER_SUPPLY_TYPE_MAINS){
			   if(di->calling_limit){
					di->cin_limit = CURRENT_AC_LIMIT_IN_800;
			   }
			   else{
					di->cin_limit = CURRENT_AC_LIMIT_IN;
				}
			}
			else
				di->cin_limit = CURRENT_USB_LIMIT_IN;
		}
		else if ((battery_temperature >= BQ2416x_WARM_BATTERY_THRESHOLD)
		    && (battery_temperature < BQ2416x_HOT_BATTERY_THRESHOLD )){
			/*battery temp is between 00 and 50 degree,normal charge*/
			di->enable_low_chg = DISABLE_LOW_CHG;/*normal charge*/
			bq2416x_config_safety_reg(di);
			di->enable_ce = ENABLE_CE;
			if (di->charger_source == POWER_SUPPLY_TYPE_MAINS)
				di->cin_limit = CURRENT_AC_LIMIT_IN_800;
			else
				di->cin_limit = CURRENT_USB_LIMIT_IN;
		}
		else if (battery_temperature >= BQ2416x_HOT_BATTERY_THRESHOLD)
		{
			dev_dbg(di->dev, "battery temp more than 50 degree,disable charging\n");
			di->enable_ce = DISABLE_CE;
			if (di->charger_source == POWER_SUPPLY_TYPE_MAINS)
				di->cin_limit = CURRENT_AC_LIMIT_IN_800;
			else
				di->cin_limit = CURRENT_USB_LIMIT_IN;
		}
		else{
			di->cin_limit = di->cin_limit;

		}
		di->enable_ce = (di->enable_ce | di->factory_flag);
		bq2416x_config_control_reg(di);
	return;
}