static void battery_id_detection(struct battery_type *battery)
{
	INT32 batt_id_index;
	static int batt_id_stable_counter;
	struct poweralg_type* poweralg_ptr = container_of(battery, struct poweralg_type, battery);
	int r2_kOhm = 300;
	if (poweralg_ptr->pdata && poweralg_ptr->pdata->r2_kohm)
		r2_kOhm = poweralg_ptr->pdata->r2_kohm;
	battery->id_ohm = ((1000000*r2_kOhm) / ((1000000*2047)/battery->id_adc - (1000000*1))) * 1000;
	calibrate_id_ohm(battery);
	batt_id_index = get_id_index(battery);
	printk(DRIVER_ZONE "battery.id_ohm=%d, battery.id_index=%d, batt_id_index=%d, st_counter=%d\n",battery->id_ohm, battery->id_index, batt_id_index, batt_id_stable_counter);

	if (is_allow_batt_id_change) {
		
		if (batt_id_stable_counter >= 3 && batt_id_index != battery->id_index) {
			
			batt_id_stable_counter = 0; 
		}
	}

	if (batt_id_stable_counter < 3) {
		if (batt_id_stable_counter == 0) {
			UINT32* fl_25;
			
			battery->id_index = batt_id_index;
			if (NULL != (fl_25 = poweralg_ptr->pdata->batt_param->fl_25))
				battery->charge_full_design_mAh = fl_25[battery->id_index];
			else
				battery->charge_full_design_mAh = DS2746_FULL_CAPACITY_DEFAULT;
			battery->charge_full_real_mAh = battery->charge_full_design_mAh;
			batt_id_stable_counter = 1;
		} else {
			
			if (batt_id_index == battery->id_index)
				batt_id_stable_counter++;
			else
				batt_id_stable_counter = 0;
		}
	}
}
Exemple #2
0
static BOOL __battery_param_udpate(struct battery_type *battery)
{
	static int batt_id_stable_counter = 0;
	INT32 batt_id_index;
	INT32 temp_01c;

	if (support_ds2746_gauge_ic) {
		/* adc register value are read from __ds2746_battery_adc_udpate()*/
		if (!__ds2746_battery_adc_udpate(battery))
			return FALSE;
	}
	else{
		/* adc register value are read from BAHW_get_batt_info_all()
		if ( !BAHW_get_batt_info_all(battery) ) return FALSE;*/
	}

	/*real physical value*/
	battery->voltage_mV = (battery->voltage_adc * voltage_adc_to_mv_coef / voltage_adc_to_mv_resl);
	battery->current_mA = (battery->current_adc * current_adc_to_mv_coef / current_adc_to_mv_resl);
	battery->discharge_mA = (battery->discharge_adc * discharge_adc_to_mv_coef / discharge_adc_to_mv_resl);
	battery->charge_counter_mAh = (battery->charge_counter_adc * acr_adc_to_mv_coef / acr_adc_to_mv_resl) -	charge_counter_zero_base_mAh;
	battery->current_mA = battery->current_mA - battery->discharge_mA;
	/* prevent from adc out of range*/
	if (battery->id_adc >= id_adc_resl) {
		battery->id_adc = id_adc_resl - 1;
	}
	if (battery->id_adc <= 0) {
		battery->id_adc = 1;
	}
	if (battery->temp_adc >= temp_adc_resl) {
		battery->temp_adc = temp_adc_resl - 1;
	}
	if (battery->temp_adc <= 0) {
		battery->temp_adc = 1;
	}

	/* battery ID shall be ready first for temp/kadc calculation*/
	//   if ( id_conversion ) battery->id_ohm = ((float)id_R_kohm / ((float)id_adc_resl/battery->id_adc - 1)) * 1000;     // kohm -> ohm
	//   else   			  battery->id_ohm = battery->id_adc;
	battery->id_ohm = battery->id_adc;
	calibrate_id_ohm(battery);

	batt_id_index = get_id_index(battery);

	if (is_allow_batt_id_change) {
		/*! TODO: batt_id changes immediately; may need to modify in future*/
		if (batt_id_stable_counter >= 3 && batt_id_index != battery->id_index){
			/* if batt_id is stable but is different from previous one*/
			batt_id_stable_counter = 0; /* reset stable counter and set batt_id to new one*/
		}
	}

	if (batt_id_stable_counter < 3) {
		if (batt_id_stable_counter == 0) {
			/* first time to get the batt id*/
			battery->id_index = batt_id_index;
			battery->charge_full_design_mAh = FL_25[battery->id_index];
			battery->charge_full_real_mAh = battery->charge_full_design_mAh;
			batt_id_stable_counter = 1;
		}
		else{
			/* 2nd and further time to get the batt id*/
			if (batt_id_index == battery->id_index)
				batt_id_stable_counter++;
			else
				batt_id_stable_counter = 0;
		}
	}

	/* calculate temperature*/
	//    battery->temp_01c 			  = get_temp_c((float)temp_R_kohm / ((float)temp_adc_resl/battery->temp_adc - 1))*10;
	temp_01c = get_temp_01c(battery);
	if (temp_01c >= TEMP_MIN*10)
		battery->temp_01c = temp_01c;
	else
		printk(DRIVER_ZONE " get temp_01c(%d) failed...\n", temp_01c);
	battery->temp_index = get_temp_index(battery);

	/* calculate KADC and RARC*/
	battery->KADC_01p = CEILING(get_kadc_001p(battery), 10);
	battery->RARC_01p = CEILING(10000 * battery->charge_counter_mAh / battery->charge_full_real_mAh, 10);
	if (!support_ds2746_gauge_ic) {
		__software_acr_update(battery);
	}

	if (battery->voltage_mV <BATTERY_VOLTAGE_MIN ||
		battery->voltage_mV> BATTERY_VOLTAGE_MAX) {
		printk(DRIVER_ZONE " invalid V(%d).\n", battery->voltage_mV);
		return FALSE;
	}

	/*! star_lee 20100426 - minimum RARC is 0%*/
	if (battery->RARC_01p <= 0) {
		battery->RARC_01p = 0;
	}

	printk(DRIVER_ZONE " V=%d(%x) I=%d(%x) C=%d.%d/%d(%x) id=%d(%x) T=%d(%x) KADC=%d\n",
		battery->voltage_mV,
		battery->voltage_adc,
		battery->current_mA,
		battery->current_adc,
		battery->charge_counter_mAh,
		battery->software_charge_counter_mAms,
		battery->charge_full_real_mAh,
		battery->charge_counter_adc,
		battery->id_index,
		battery->id_adc,
		battery->temp_01c,
		battery->temp_adc,
		battery->KADC_01p);

	return TRUE;
}
Exemple #3
0
static void battery_id_detection(struct battery_type *battery)
{
	INT32 batt_id_index;
	/*static int batt_id_stable_counter;*/
	static int batt_id_unknown_stable_count = -1;
	struct poweralg_type* poweralg_ptr = container_of(battery, struct poweralg_type, battery);
	int r2_kOhm = 300;
	if (poweralg_ptr->pdata && poweralg_ptr->pdata->r2_kohm)
		r2_kOhm = poweralg_ptr->pdata->r2_kohm;
	battery->id_ohm = ((1000000*r2_kOhm) / ((1000000*2047)/battery->id_adc - (1000000*1))) * 1000;
	calibrate_id_ohm(battery);
	batt_id_index = get_id_index(battery);
	printk(DRIVER_ZONE "battery.id_ohm=%d, battery.id_index=%d, batt_id_index=%d, st_counter=%d\n",battery->id_ohm, battery->id_index, batt_id_index, batt_id_unknown_stable_count);

	if (-1 == batt_id_unknown_stable_count) {
		/* initial state, no matter what id we detected must set */
		battery_set_batt_id_param(battery, poweralg_ptr,  batt_id_index);
		batt_id_unknown_stable_count = 0;
	}
	else if ((batt_id_index != battery->id_index) && is_allow_batt_id_change) {
		if ((BATTERY_ID_UNKNOWN == batt_id_index) && (batt_id_unknown_stable_count < 2)) {
			batt_id_unknown_stable_count++;
			// do not switch to BATTERY_UNKNOWN state before it stable
			return;
		}
		battery_set_batt_id_param(battery, poweralg_ptr,  batt_id_index);
		batt_id_unknown_stable_count = 0;
	}
	else {
		batt_id_unknown_stable_count = 0;
	}
#if 0
	if (is_allow_batt_id_change) {
		/*! TODO: batt_id changes immediately; may need to modify in future*/
		if (batt_id_stable_counter >= 3 && batt_id_index != battery->id_index) {
			/* if batt_id is stable but is different from previous one*/
			batt_id_stable_counter = 0; /* reset stable counter and set batt_id to new one*/
		}
	}

	if (batt_id_stable_counter < 3) {
		if (batt_id_stable_counter == 0) {
			UINT32* fl_25;
			/* first time to get the batt id*/
			battery->id_index = batt_id_index;
			if (NULL != (fl_25 = poweralg_ptr->pdata->batt_param->fl_25))
				battery->charge_full_design_mAh = fl_25[battery->id_index];
			else
				battery->charge_full_design_mAh = DS2746_FULL_CAPACITY_DEFAULT;
			battery->charge_full_real_mAh = battery->charge_full_design_mAh;

			if (NULL != poweralg_ptr->pdata->batt_param->m_param_tbl) {
				battery->dead_voltage_mV = get_dead_battery_voltage(
					poweralg_ptr->pdata->batt_param->m_param_tbl, batt_id_index);
			}
			else
				battery->dead_voltage_mV = BATTERY_DEAD_VOLTAGE_DEFAULT;
			printk(DRIVER_ZONE " assign dead battery voltage = %d.\n",battery->dead_voltage_mV);

			batt_id_stable_counter = 1;
		} else {
			/* 2nd and further time to get the batt id*/
			if (batt_id_index == battery->id_index)
				batt_id_stable_counter++;
			else
				batt_id_stable_counter = 0;
		}
	}
#endif
}