static int rk30_adc_battery_voltage_to_capacity(struct rk30_adc_battery_data *bat, int BatVoltage) { int i = 0; int capacity = 0; struct batt_vol_cal *p; p = batt_table; if (rk30_adc_battery_get_charge_level(bat)) { /* calculate with charging mode values */ if(BatVoltage >= (p[BATT_NUM - 1].charge_vol)) { capacity = 100; } else{ if(BatVoltage <= (p[0].charge_vol)){ capacity = 0; } else{ for(i = 0; i < BATT_NUM - 1; i++){ if(((p[i].charge_vol) <= BatVoltage) && (BatVoltage < (p[i+1].charge_vol))){ capacity = p[i].disp_cal + ((BatVoltage - p[i].charge_vol) * (p[i+1].disp_cal -p[i].disp_cal ))/ (p[i+1].charge_vol- p[i].charge_vol); break; } } } } } else { /* Calculate with lower discharging values */ if(BatVoltage >= (p[BATT_NUM - 1].dis_charge_vol)){ capacity = 100; } else{ if(BatVoltage <= (p[0].dis_charge_vol)){ capacity = 0; } else{ for(i = 0; i < BATT_NUM - 1; i++){ if(((p[i].dis_charge_vol) <= BatVoltage) && (BatVoltage < (p[i+1].dis_charge_vol))){ capacity = p[i].disp_cal+ ((BatVoltage - p[i].dis_charge_vol) * (p[i+1].disp_cal -p[i].disp_cal ) )/ (p[i+1].dis_charge_vol- p[i].dis_charge_vol) ; break; } } } } } return capacity; }
static int rk30_adc_battery_voltage_to_capacity(struct rk30_adc_battery_data *bat, int BatVoltage) { int i = 0; int capacity = 0; int *p; p = batt_table; if (rk30_adc_battery_get_charge_level(bat)){ //charge if(BatVoltage >= (p[2*BATT_NUM +5])){ capacity = 100; } else{ if(BatVoltage <= (p[BATT_NUM +6])){ capacity = 0; } else{ for(i = BATT_NUM +6; i <2*BATT_NUM +6; i++){ if(((p[i]) <= BatVoltage) && (BatVoltage < (p[i+1]))){ capacity = (i-BATT_NUM +6)*10 + ((BatVoltage - p[i]) * 10)/ (p[i+1]- p[i]); break; } } } } } else{ //discharge if(BatVoltage >= (p[BATT_NUM +5])){ capacity = 100; } else{ if(BatVoltage <= (p[6])){ capacity = 0; } else{ for(i = 6; i < BATT_NUM +6; i++){ if(((p[i]) <= BatVoltage) && (BatVoltage < (p[i+1]))){ capacity = (i-6)*10+ ((BatVoltage - p[i]) *10 )/ (p[i+1]- p[i]) ; break; } } } } } return capacity; }
static int rk30_adc_battery_voltage_to_capacity(struct rk30_adc_battery_data *bat, int BatVoltage) { int i = 0; int capacity = 0; struct batt_vol_cal *p; p = batt_table; if (rk30_adc_battery_get_charge_level(bat)){ //charge if(BatVoltage >= (p[BATT_NUM - 1].charge_vol)){ capacity = 100; } else{ if(BatVoltage <= (p[0].charge_vol)){ capacity = 0; } else{ for(i = 0; i < BATT_NUM - 1; i++){ if(((p[i].charge_vol) <= BatVoltage) && (BatVoltage < (p[i+1].charge_vol))){ capacity = p[i].disp_cal ; break; } } } } } else{ //discharge if(BatVoltage >= (p[BATT_NUM - 1].dis_charge_vol)){ capacity = 100; } else{ if(BatVoltage <= (p[0].dis_charge_vol)){ capacity = 0; } else{ for(i = 0; i < BATT_NUM - 1; i++){ if(((p[i].dis_charge_vol) <= BatVoltage) && (BatVoltage < (p[i+1].dis_charge_vol))){ capacity = p[i].disp_cal ; break; } } } } } return capacity; }
static void rk30_adc_battery_voltage_samples(struct rk30_adc_battery_data *bat) { int value; int i,*pStart = bat->adc_samples, num = 0; int level = rk30_adc_battery_get_charge_level(bat); value = bat->adc_val; adc_async_read(bat->client); *pSamples++ = adc_to_voltage(value); bat->bat_status_cnt++; if (bat->bat_status_cnt > NUM_VOLTAGE_SAMPLE) bat->bat_status_cnt = NUM_VOLTAGE_SAMPLE + 1; num = pSamples - pStart; if (num >= NUM_VOLTAGE_SAMPLE){ pSamples = pStart; num = NUM_VOLTAGE_SAMPLE; } value = 0; for (i = 0; i < num; i++){ value += bat->adc_samples[i]; } bat->bat_voltage = value / num; /* Check limits */ if(1 == level){ if(bat->bat_voltage >= batt_table[BATT_NUM-1].charge_vol + 10) bat->bat_voltage = batt_table[BATT_NUM-1].charge_vol + 10; else if(bat->bat_voltage <= batt_table[0].charge_vol - 10) bat->bat_voltage = batt_table[0].charge_vol - 10; } else{ if(bat->bat_voltage >= batt_table[BATT_NUM-1].dis_charge_vol + 10) bat->bat_voltage = batt_table[BATT_NUM-1].dis_charge_vol + 10; else if(bat->bat_voltage <= batt_table[0].dis_charge_vol - 10) bat->bat_voltage = batt_table[0].dis_charge_vol - 10; } }
static void rk30_adc_battery_voltage_samples(struct rk30_adc_battery_data *bat) { int value; int i,*pStart = bat->adc_samples, num = 0; int level = rk30_adc_battery_get_charge_level(bat); value = bat->adc_val; adc_async_read(bat->client); *pSamples++ = adc_to_voltage(value); bat->bat_status_cnt++; if (bat->bat_status_cnt > NUM_VOLTAGE_SAMPLE) bat->bat_status_cnt = NUM_VOLTAGE_SAMPLE + 1; num = pSamples - pStart; if (num >= NUM_VOLTAGE_SAMPLE){ pSamples = pStart; num = NUM_VOLTAGE_SAMPLE; } value = 0; for (i = 0; i < num; i++){ value += bat->adc_samples[i]; } bat->bat_voltage = value / num; /*消除毛刺电压*/ if(battery_test_flag == 0) { if(1 == level){ if(bat->bat_voltage >= batt_table[2*BATT_NUM +5]+ 10) bat->bat_voltage = batt_table[2*BATT_NUM +5] + 10; else if(bat->bat_voltage <= batt_table[BATT_NUM +6] - 10) bat->bat_voltage = batt_table[BATT_NUM +6] - 10; } else{ if(bat->bat_voltage >= batt_table[BATT_NUM +5]+ 10) bat->bat_voltage = batt_table[BATT_NUM +5] + 10; else if(bat->bat_voltage <= batt_table[6] - 10) bat->bat_voltage = batt_table[6] - 10; } }else if(battery_test_flag == 2) /**************************************************/ { if(batt_table[3] == 0) { if(bat->bat_voltage < 3400) { //printk("gSecondsCnt=%ld,get_seconds()=%ld,(get_seconds() - gSecondsCnt)=%ld-------------------1\n",gSecondsCnt,get_seconds(),(get_seconds() - gSecondsCnt)); if((get_seconds() - gSecondsCnt) > 30) { gSecondsCnt = get_seconds(); //printk("gSecondsCnt=%ld,gVoltageCnt=%d,(gVoltageCnt - bat->bat_voltage)=%d,bat->bat_voltage=%d-------------------2\n",gSecondsCnt,gVoltageCnt,(gVoltageCnt - bat->bat_voltage),bat->bat_voltage); if((gVoltageCnt - bat->bat_voltage) > 15) { //gVoltageCnt = bat->bat_voltage; //printk("gVoltageCnt=%d-------------------3\n",gVoltageCnt); strncpy(gDischargeFlag, "off" ,3); } gVoltageCnt = bat->bat_voltage; } } if(bat->bat_voltage < 3400) { bat->bat_voltage = 3400; } } else { if(bat->bat_voltage < 6800) { //printk("gSecondsCnt=%ld,get_seconds()=%ld,(get_seconds() - gSecondsCnt)=%ld-------------------1\n",gSecondsCnt,get_seconds(),(get_seconds() - gSecondsCnt)); if((get_seconds() - gSecondsCnt) > 30) { gSecondsCnt = get_seconds(); //printk("gSecondsCnt=%ld,gVoltageCnt=%d,(gVoltageCnt - bat->bat_voltage)=%d,bat->bat_voltage=%d-------------------2\n",gSecondsCnt,gVoltageCnt,(gVoltageCnt - bat->bat_voltage),bat->bat_voltage); if((gDoubleVoltageCnt - bat->bat_voltage) > 30) { //gVoltageCnt = bat->bat_voltage; //printk("gVoltageCnt=%d-------------------3\n",gVoltageCnt); strncpy(gDischargeFlag, "off" ,3); } gDoubleVoltageCnt =bat->bat_voltage; } } if(bat->bat_voltage < 6800) { bat->bat_voltage = 6800; } } } /****************************************************/ }
//int old_charge_level; static int rk30_adc_battery_status_samples(struct rk30_adc_battery_data *bat) { int charge_level; struct rk30_adc_battery_platform_data *pdata = bat->pdata; charge_level = rk30_adc_battery_get_charge_level(bat); //检测充电状态变化情况 if (charge_level != bat->old_charge_level){ bat->old_charge_level = charge_level; bat->bat_change = 1; if(charge_level) { rk30_adc_battery_charge_enable(bat); } else{ rk30_adc_battery_charge_disable(bat); } bat->bat_status_cnt = 0; //状态变化开始计数 } if(charge_level == 0){ //discharge bat->full_times = 0; bat->bat_status = POWER_SUPPLY_STATUS_NOT_CHARGING; } else{ //CHARGE if (pdata->charge_ok_pin == INVALID_GPIO){ //no charge_ok_pin if (bat->bat_capacity == 100){ if (bat->bat_status != POWER_SUPPLY_STATUS_FULL){ bat->bat_status = POWER_SUPPLY_STATUS_FULL; bat->bat_change = 1; } } else{ bat->bat_status = POWER_SUPPLY_STATUS_CHARGING; } } else{ // pin of charge_ok_pin if (gpio_get_value(pdata->charge_ok_pin) != pdata->charge_ok_level){ bat->full_times = 0; bat->bat_status = POWER_SUPPLY_STATUS_CHARGING; } else{ //检测到充电满电平标志 bat->full_times++; if (bat->full_times >= NUM_CHARGE_FULL_DELAY_TIMES) { bat->full_times = NUM_CHARGE_FULL_DELAY_TIMES + 1; } if ((bat->full_times >= NUM_CHARGE_FULL_DELAY_TIMES) && (bat->bat_capacity >= 99)){ if (bat->bat_status != POWER_SUPPLY_STATUS_FULL){ bat->bat_status = POWER_SUPPLY_STATUS_FULL; bat->bat_capacity = 100; bat->bat_change = 1; } } else{ bat->bat_status = POWER_SUPPLY_STATUS_CHARGING; } } } } return charge_level; }