예제 #1
0
static void rk2918_low_battery_check(void)
{
    int i;
    int tmp = 0;
    
    for (i = 0; i < 100; i++)
    {
        tmp += adc_sync_read(gBatteryData->client);
        mdelay(1);
    }
    tmp = tmp / 100;
    //gBatteryData->adc_val = tmp;
    //gBatVoltage = (tmp * BAT_2V5_VALUE * (BAT_PULL_UP_R + BAT_PULL_DOWN_R)) / (1024 * BAT_PULL_DOWN_R);
    //rk2918_get_charge_status();
    //gBatCapacity = rk2918_get_bat_capacity_raw(gBatVoltage);
    rk2918_get_charge_status();
    tmp = (tmp * BAT_2V5_VALUE * (BAT_PULL_UP_R + BAT_PULL_DOWN_R)) / (1024 * BAT_PULL_DOWN_R);
	if(RK29_ADC_CLI_ADD)
		tmp += RK29_ADC_CLI_VALUE;
	else
		tmp -= RK29_ADC_CLI_VALUE;
    gBatCapacity = rk2918_get_bat_capacity_raw(tmp);
    if (gBatCapacity == 0) gBatCapacity = 1;
    printk("rk2918_low_battery_check: gBatVoltage = %d, gBatCapacity = %d\n", gBatVoltage, gBatCapacity);
    
    if (gBatVoltage <= BATT_ZERO_VOL_VALUE + 50)
    {
        printk("low battery: powerdown\n");
        gpio_direction_output(POWER_ON_PIN, GPIO_LOW);
        tmp = 0;
        while(1)
        {
            if(gpio_get_value(POWER_ON_PIN) == GPIO_HIGH)
		    {
			    gpio_set_value(POWER_ON_PIN,GPIO_LOW);
		    }
		    mdelay(5);
		    if (++tmp > 50) break;
		}
    }
    gpio_direction_output(POWER_ON_PIN, GPIO_HIGH);
}
예제 #2
0
static int rk2918_get_bat_capacity_ext(int BatVoltage)
{
    int i = 0;
	int capacity = 0;
	static int lostcount = 0;
	
    capacity = rk2918_get_bat_capacity_raw(BatVoltage);
	capacitytmp = capacity;
	
	//充放电状态变化后,Buffer填满之前,不更新
    if (gBatStatusChangeCnt < NUM_VOLTAGE_SAMPLE)
    {
        capacity = gBatCapacity;
    }
        
    if (gBatChargeStatus == 1)
    {
        if ((capacity > gBatCapacity)  && (gBatCapacity < 100))
        {
            //实际采样到的电压比显示的电压大,逐级上升
            gBatHighCapacityChargeCnt = 0;
            if (gBatCapacityDownCnt == 0)
            {
                capacity = gBatCapacity + 1;
                gBatCapacityDownCnt = NUM_CHARGE_MIN_SAMPLE;
            }
            else
            {
                capacity = gBatCapacity;
            }
        }
        else if ((capacity <= gBatCapacity) && (gBatCapacity < 100))
        {
            capacity = gBatCapacity;
            
            //长时间内充电电压无变化,开始启动计时充电
            if ((gBatCapacity >= 80) && (++gBatHighCapacityChargeCnt > NUM_CHARGE_MAX_SAMPLE))
            {
                capacity = gBatCapacity + 1;
                gBatHighCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
            }
        }
		else
        {
            capacity = gBatCapacity;
        }
        
		if (capacity == 0)
		{
		    capacity = 1;
		}
		
		if (capacity >= 100)
		{
		    capacity = 99;
		}

		if (gBatStatus == POWER_SUPPLY_STATUS_FULL)
		{
		    capacity = 100;
		}
    }    
    else
    {   
        //放电时,只允许电压下降
        if (capacity > gBatCapacity)
        {
            capacity = gBatCapacity;
        }
        
        if ((capacity < gBatCapacity) && (gBatCapacityDownCnt == 0))
        {
            capacity = gBatCapacity - 1;
            gBatCapacityDownCnt = NUM_DISCHARGE_MIN_SAMPLE;
        }
        else
        {
            capacity = gBatCapacity;
        }
		
	#if defined (CONFIG_MACH_M1005HN) || defined (CONFIG_MACH_M1005HNW) || defined (CONFIG_MACH_M1005HD)
		if (lostcount++ > NUM_SPEEDLOSE_SAMPLE)
		{
			lostcount = 0;
			if (((lastlost-gBatVoltage)>16)&&(gBatVoltage >= 6900))// start play game
			{
				shutdownvoltage = 6700; 
				printk("%s...lastlost=%d.enter game\n",__func__,gBatVoltage);
			}
			else
			{
				if ((gBatVoltage-lastlost)>16) //exit game
				{
					shutdownvoltage = 6900;
					printk("%s.. lastlost=%d..exit game\n",__func__,gBatVoltage);
				}
			}
			lastlost = gBatVoltage;
			
	
		}
	
		// <8%capacity enter game will make mistake
		if (((gBatVoltage<=shutdownvoltage) && (lastlost-gBatVoltage)<=6) || (gBatVoltage <= 6700))
		{	
			shutdownflag++;
			if(shutdownflag >= NUM_SHUTD0WN_SAMPLE)
			{
				capacity = 0;
				printk("%s.........%d.........%d\n",__func__,__LINE__,shutdownflag);
				shutdownflag = NUM_SHUTD0WN_SAMPLE+1;
			}
		}
	#else
		if (lostcount++ > NUM_SPEEDLOSE_SAMPLE)
		{
			lostcount = 0;
			if (((lastlost-gBatVoltage)>8)&&(gBatVoltage >= 3400))// start play game
			{
				shutdownvoltage = 3300; 
				printk("%s...lastlost=%d.enter game\n",__func__,gBatVoltage);
			}
			else
			{
				if ((gBatVoltage-lastlost)>8) //exit game
				{
					shutdownvoltage = 3400;
					printk("%s.. lastlost=%d..exit game\n",__func__,gBatVoltage);
				}
			}
			lastlost = gBatVoltage;
			
	
		}
	
		// <8%capacity enter game will make mistake
		if (((gBatVoltage<=shutdownvoltage) && (lastlost-gBatVoltage)<=3) || (gBatVoltage <= 3300))
		{	
			shutdownflag++;
			if(shutdownflag >= NUM_SHUTD0WN_SAMPLE)
			{
				capacity = 0;
				printk("%s.........%d.........%d\n",__func__,__LINE__,shutdownflag);
				shutdownflag = NUM_SHUTD0WN_SAMPLE+1;
			}
		}
	#endif
    }
    if (gBatCapacityDownCnt > 0)
    {
        gBatCapacityDownCnt --;
    }
    
	return capacity;
}
예제 #3
0
static int rk2918_battery_resume_get_Capacity(int deltatime)
{
	int i;
    int tmp = 0;
    int capacity = 0;

	for (i = 0; i < 20; i++)
    {
        tmp += adc_sync_read(gBatteryData->client);
        mdelay(1);
    }
    tmp = tmp / 20;
    //tmp = (tmp * BAT_2V5_VALUE * (BAT_PULL_UP_R + BAT_PULL_DOWN_R)) / (1024 * BAT_PULL_DOWN_R);
    tmp = adc_to_voltage(tmp);
    capacity = rk2918_get_bat_capacity_raw(tmp);
    //if last status is charging FULL, will return 100
	if(last_BatChargeStatus == POWER_SUPPLY_STATUS_FULL)
		return 100;
	//if last status is charging , and now still charging
    if ((gBatChargeStatus == 1) && (last_BatChargeStatus == 1))
    {
    	/*
        if (deltatime > (100 - gBatCapacity) * CHARGE_MIN_SECOND)
            deltatime = (100 - gBatCapacity) * CHARGE_MIN_SECOND;
        if (capacity > gBatCapacity + (deltatime / CHARGE_MIN_SECOND))       //采样电池容量偏差较大,将容量拉回
        {
            capacity = gBatCapacity + (deltatime / CHARGE_MIN_SECOND);
        }
		*/
		//
		if((gBatCapacity > 80))
		{
			capacity = gBatCapacity + deltatime/CHARGE_MID_SECOND;
		}
		else
		{
			/*some battery cannot arrive to 4.1V when charging full*/
			if((deltatime > (100 - gBatCapacity) * CHARGE_MID_SECOND))
			{
				capacity = 99;
				if (rk29_battery_dbg_level)
					printk("0000rk2918_battery_resume: last_BatChargeStatus: %d, gBatChargeStatus: %d, gBatVoltage = %d, gBatCapacity = %d, deltatime = %d, capacity = %d\n", 
	           			last_BatChargeStatus, gBatChargeStatus, gBatVoltage, gBatCapacity, deltatime, capacity);
				if (gBatStatus == POWER_SUPPLY_STATUS_FULL)
				{
					capacity = 100;
				}
   				return capacity;
			}
			/*if battery is not full after long charging*/
			if((capacity > 80))
			{
				if(capacity < gBatCapacity + deltatime/CHARGE_MID_SECOND)
					capacity = gBatCapacity + deltatime/CHARGE_MID_SECOND;
				if(capacity >= 100)
					capacity = 99;
				if (gBatStatus == POWER_SUPPLY_STATUS_FULL)
				{
					capacity = 100;
				}
				if (rk29_battery_dbg_level)
					printk("11111rk2918_battery_resume: last_BatChargeStatus: %d, gBatChargeStatus: %d, gBatVoltage = %d, gBatCapacity = %d, deltatime = %d, capacity = %d\n", 
		       			last_BatChargeStatus, gBatChargeStatus, gBatVoltage, gBatCapacity, deltatime, capacity);
				return capacity;
			}
			
			if(capacity > gBatCapacity)
	        {
	        	if((capacity-gBatCapacity)>15)
					gBatCapacity = capacity;
				else
				{
					if(gBatCapacity > 20)
					 	capacity=gBatCapacity;
					else
						gBatCapacity =(gBatCapacity + capacity)/2;
				}
	        }
	        else if (capacity < gBatCapacity)
	        {
	        	if((gBatCapacity - capacity)>10)
					gBatCapacity = capacity;
				else
					 capacity=gBatCapacity ;
	        }
		}
		if (capacity >= 100) 
			capacity = 99;
    }
    else
    {
    	/*
        if (deltatime > gBatCapacity * DISCHARGE_MIN_SECOND)
            deltatime = gBatCapacity * DISCHARGE_MIN_SECOND;            
        if (capacity < gBatCapacity - (deltatime / DISCHARGE_MIN_SECOND))    //采样电池容量偏差较大,将容量拉回
        {
            capacity = gBatCapacity - (deltatime / DISCHARGE_MIN_SECOND);
        }
		*/
		if (rk29_battery_dbg_level)
			printk("333rk2918_battery_resume: last_BatChargeStatus: %d, gBatChargeStatus: %d, gBatVoltage = %d, gBatCapacity = %d, deltatime = %d, capacity = %d\n", 
	           			last_BatChargeStatus, gBatChargeStatus, gBatVoltage, gBatCapacity, deltatime, capacity);
		/*to ensure battery show 100% after long charging during sleep and then pug out charger*/
		if((deltatime > (100 - gBatCapacity) * CHARGE_MID_SECOND) && (last_BatChargeStatus == 1))
		{
			if(capacity < 99)
				capacity = 99;
			if(capacity >= 100)
				capacity = 100;
			return capacity;
		}
		
        if (capacity < gBatCapacity)
        {
			if((gBatCapacity - capacity)>10)
				gBatCapacity = capacity;
			else
				 capacity=gBatCapacity ;
        }
        else if (capacity > gBatCapacity)
        {
        	if((capacity-gBatCapacity)>15)
				gBatCapacity = capacity;
			else
				 capacity=gBatCapacity ;
        }
    }
	
    if (capacity == 0) 
		capacity = 1;
    if (capacity >= 100) 
		capacity = 100;
    
    if (gBatStatus == POWER_SUPPLY_STATUS_FULL)
	{
	    capacity = 100;
	}
	if (rk29_battery_dbg_level)	
	    printk("rk2918_battery_resume: last_BatChargeStatus: %d, gBatChargeStatus: %d, gBatVoltage = %d, gBatCapacity = %d, deltatime = %d, ktmietmp.tv.sec = %lu, capacity = %d\n", 
	           last_BatChargeStatus, gBatChargeStatus, gBatVoltage, gBatCapacity, deltatime, batteryspendcnt, capacity);
    
    return capacity;
}
예제 #4
0
static int rk2918_battery_load_capacity(void)
{
    int i;
    int tmp = 0;
	int  loadcapacity = 0;
	int  truecapacity = 0;
    char value[11];
    static char lastthree[6]= {0};
	char* p = value;
    struct file* fp = filp_open(BATT_FILENAME,O_RDONLY,0);
    
    //get true capacity
    for (i = 0; i < 20; i++)
    {
        tmp += adc_to_voltage(adc_sync_read(gBatteryData->client));
        mdelay(1);
    }
    tmp = tmp / 20;

	lastlost = tmp;
    truecapacity = rk2918_get_bat_capacity_raw(tmp);
    
	if(IS_ERR(fp))
    {
		printk("bryan---->open file /data/bat_last_capacity.dat failed\n");
		printk("truecapacity = %d\n", truecapacity);
		if(truecapacity>=100)
			truecapacity = 100;
	 	if(truecapacity==0)
			truecapacity=1;
		openfailflag = 1;

		if (openfailcount <= 5)
		{
			lastthree[openfailcount-1] = truecapacity;

			if (openfailcount == 1)
			{
				tmp = 0;
				for (i=0;i<5;i++)
				{
					tmp += lastthree[4-i];	
					printk("%s...............%d\n",__func__,tmp);
				}

				truecapacity = tmp/5;
				printk("%s...............%d\n",__func__,tmp);

			}
		}
		return truecapacity;
	}
	else
	{
		openfailflag = 0;
		openfailcount = 0;
	}
	kernel_read(fp,0,value,10);
    filp_close(fp,NULL);

	value[10]=0;
	while(*p)
	{
	    if(*p==0x0d)
	    {
			*p=0;
			break;
		}
		p++;
	}	
	sscanf(value,"%d",&loadcapacity);
	printk("bryan---->loadcapacity = %d, truecapacity = %d\n",loadcapacity, truecapacity);
	
    if ((loadcapacity <= 0) || (loadcapacity > 100))
	{
	    loadcapacity = truecapacity;
	}
	//如果从文件中读取的电压比实际的高很多的话,说明是长时间放置导致放电
	if (loadcapacity > truecapacity)
	{
	    if (loadcapacity - truecapacity > 20)
	    {
	        loadcapacity = truecapacity;
	    }
	}
	else
	{
		if ( ((truecapacity-loadcapacity) >= 20))
		{

			if (truecapacity < 30)
			{
				if (loadcapacity < 10)
				{
					loadcapacity = truecapacity/2;
				}
			}
			else
			{

				loadcapacity = truecapacity;	
				
			}

		}
	}
	    
	
	if (loadcapacity <= 0)
	{
		loadcapacity = 1;
	}
	if (loadcapacity >= 100)
	{
		loadcapacity = 100;
	}
	return loadcapacity;
}
예제 #5
0
static int rk2918_get_bat_capacity_ext(int BatVoltage)
{
    int i = 0;
	int capacity = 0;
	static int lostcount = 0;
	uint8_t speedlose = 1;
	
    capacity = rk2918_get_bat_capacity_raw(BatVoltage);
	capacitytmp = capacity;
	
	//充放电状态变化后,Buffer填满之前,不更新
    if (gBatStatusChangeCnt < NUM_VOLTAGE_SAMPLE)
    {
        capacity = gBatCapacity;
    }
        
    if (gBatChargeStatus == 1)
    {
		if ((capacity > gBatCapacity) && (gBatCapacity < 100) && (gBatCapacityDownCnt == 0))
        {
            capacity = gBatCapacity + 1;
			if(usb_charging_on)
            	gBatCapacityDownCnt = NUM_CHARGE_MIN_SAMPLE + NUM_CHARGE_MIN_SAMPLE;
			else
				gBatCapacityDownCnt = NUM_CHARGE_MIN_SAMPLE;
            gBatHighCapacityChargeCnt = 0;
        }
        else if (((capacity <= gBatCapacity) && (gBatCapacity < 100)) || (gBatCapacity >= 90))
        {
        	if(usb_charging_on)
        	{
	            if (++gBatHighCapacityChargeCnt > NUM_CHARGE_MAX_SAMPLE + NUM_CHARGE_MID_SAMPLE)
	            {
	                //长时间内充电电压无变化,开始计时充电
	                capacity = gBatCapacity + 1;
	                gBatHighCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
	            }
	            else
	            {
	                capacity = gBatCapacity;
	            }
        	}
			else
			{
	            if (++gBatHighCapacityChargeCnt > NUM_CHARGE_MAX_SAMPLE)
	            {
	                //长时间内充电电压无变化,开始计时充电
	                capacity = gBatCapacity + 1;
	                gBatHighCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
	            }
	            else
	            {
	                capacity = gBatCapacity;
	            }
        	}
				
        }
        else
        {
            capacity = gBatCapacity;
        }
        
		if (capacity == 0)
		{
		    capacity = 1;
		}
		
		if (capacity >= 100)
		{
		    capacity = 99;
		}
		
		if (gBatStatus == POWER_SUPPLY_STATUS_FULL)
		{
		    capacity = 100;
		}
    }    
    else
    {   
        //放电时,只允许电压下降
        if (capacity > gBatCapacity)
        {
            capacity = gBatCapacity;
        }
        
        if ((capacity < gBatCapacity) && (gBatCapacityDownCnt == 0))
        {
            capacity = gBatCapacity - 1;
            gBatCapacityDownCnt = NUM_DISCHARGE_MIN_SAMPLE;
        }
        else
        {
            capacity = gBatCapacity;
        }
		
		if (lostcount++ > NUM_SPEEDLOSE_SAMPLE)
 		{
			lostcount = 0;
			if (((lastlost-gBatVoltage)>8)&&(gBatVoltage >= 3400))// start play game
			{
				shutdownvoltage = 3300;	
				printk("%s...lastlost=%d.enter game\n",__func__,gBatVoltage);
			}
			else
			{
				if ((gBatVoltage-lastlost)>8) //exit game
				{
					shutdownvoltage = 3400;
					printk("%s.. lastlost=%d..exit game\n",__func__,gBatVoltage);
				}
			}
			lastlost = gBatVoltage;
			

 		}

// <8%capacity enter game will make mistake
 		if (((gBatVoltage<=shutdownvoltage) && (lastlost-gBatVoltage)<=3) || (gBatVoltage <= 3300))
 		{	
 			shutdownflag++;
 			if(shutdownflag >= NUM_SHUTD0WN_SAMPLE)
			{
				capacity = 0;
				printk("%s.........%d.........%d\n",__func__,__LINE__,shutdownflag);
				shutdownflag = NUM_SHUTD0WN_SAMPLE+1;
			}
 		}	
    }
    
    if (gBatCapacityDownCnt > 0)
    {
        gBatCapacityDownCnt --;
    }
    
	return capacity;
}
예제 #6
0
static int rk2918_battery_resume_get_Capacity(int deltatime)
{
	int i;
    int tmp = 0;
    int capacity = 0;
	gFlagLoop = 0;
	pSamples = &gBatVoltageSamples[0];
    for (i = 0; i < 80; i++)
    {
        tmp += adc_sync_read(gBatteryData->client);
        mdelay(1);
    }
    tmp = tmp / 80;
    tmp = (tmp * BAT_2V5_VALUE * (BAT_PULL_UP_R + BAT_PULL_DOWN_R)) / (1024 * BAT_PULL_DOWN_R);
	if(RK29_ADC_CLI_ADD)
		tmp += RK29_ADC_CLI_VALUE;
	else
		tmp -= RK29_ADC_CLI_VALUE;
    capacity = rk2918_get_bat_capacity_raw(tmp);
	
    if (gBatChargeStatus == 1)
    {
    	/*
        if (deltatime > (100 - gBatCapacity) * CHARGE_MIN_SECOND)
            deltatime = (100 - gBatCapacity) * CHARGE_MIN_SECOND;
        if (capacity > gBatCapacity + (deltatime / CHARGE_MIN_SECOND))       //采样电池容量偏差较大,将容量拉回
        {
            capacity = gBatCapacity + (deltatime / CHARGE_MIN_SECOND);
        }
		*/
		//
		if(gBatCapacity > 80)
		{
			if(gBatChargeStatus == last_BatChargeStatus)
				capacity = gBatCapacity + deltatime/CHARGE_MID_SECOND;
		}
		else
		{
			/*some battery cannot arrive to 4.1V when charging full*/
			if((deltatime > (100 - gBatCapacity) * CHARGE_MID_SECOND) && (gBatChargeStatus == last_BatChargeStatus))
			{
				capacity = 99;
				printk("0000rk2918_battery_resume: last_BatChargeStatus: %d, gBatChargeStatus: %d, gBatVoltage = %d, gBatCapacity = %d, deltatime = %d, capacity = %d\n", 
           			last_BatChargeStatus, gBatChargeStatus, gBatVoltage, gBatCapacity, deltatime, capacity);
				if (gBatStatus == POWER_SUPPLY_STATUS_FULL)
				{
					capacity = 100;
				}
   				return capacity;
			}
			/*if battery is not full after long charging*/
			if((capacity > 80)&& (gBatChargeStatus == last_BatChargeStatus))
			{
				if(capacity < gBatCapacity + deltatime/CHARGE_MID_SECOND)
					capacity = gBatCapacity + deltatime/CHARGE_MID_SECOND;
				if(capacity >= 100)
					capacity = 99;
				if (gBatStatus == POWER_SUPPLY_STATUS_FULL)
				{
					capacity = 100;
				}
				printk("11111rk2918_battery_resume: last_BatChargeStatus: %d, gBatChargeStatus: %d, gBatVoltage = %d, gBatCapacity = %d, deltatime = %d, capacity = %d\n", 
           			last_BatChargeStatus, gBatChargeStatus, gBatVoltage, gBatCapacity, deltatime, capacity);
				return capacity;
			}
			
			if(capacity > gBatCapacity)
	        {
	        	if((capacity-gBatCapacity)>15)
					gBatCapacity = capacity;
				else
					 capacity=gBatCapacity ;
	        }
	        else if (capacity < gBatCapacity)
	        {
	        	if((gBatCapacity - capacity)>10)
					gBatCapacity = capacity;
				else
					 capacity=gBatCapacity ;
	        }
		}
		if (capacity >= 100) 
			capacity = 99;
    }
    else
    {
    	/*
        if (deltatime > gBatCapacity * DISCHARGE_MIN_SECOND)
            deltatime = gBatCapacity * DISCHARGE_MIN_SECOND;            
        if (capacity < gBatCapacity - (deltatime / DISCHARGE_MIN_SECOND))    //采样电池容量偏差较大,将容量拉回
        {
            capacity = gBatCapacity - (deltatime / DISCHARGE_MIN_SECOND);
        }
		*/
		
        if (capacity < gBatCapacity)
        {
			if((gBatCapacity - capacity)>10)
				gBatCapacity = capacity;
			else
				 capacity=gBatCapacity ;
        }
        else if (capacity > gBatCapacity)
        {
        	if((capacity-gBatCapacity)>15)
				gBatCapacity = capacity;
			else
				 capacity=gBatCapacity ;
        }
    }
	
    if (capacity == 0) 
		capacity = 1;
    if (capacity >= 100) 
		capacity = 100;
    
    if (gBatStatus == POWER_SUPPLY_STATUS_FULL)
	{
	    capacity = 100;
	}
		
    printk("rk2918_battery_resume: last_BatChargeStatus: %d, gBatChargeStatus: %d, gBatVoltage = %d, gBatCapacity = %d, deltatime = %d, ktmietmp.tv.sec = %lu, capacity = %d\n", 
           last_BatChargeStatus, gBatChargeStatus, gBatVoltage, gBatCapacity, deltatime, batteryspendcnt, capacity);
    
    return capacity;
}
예제 #7
0
static int rk2918_battery_load_capacity(void)
{
    int i;
    int tmp = 0;
	int  loadcapacity = 0;
	int  truecapacity = 0;
    char value[11];
    static char lastthree[6]= {0};
	char* p = value;
	unsigned long spendcnt_intervals = 0;
	ktime_t ktimetmp; 
	
	ktimetmp = ktime_get_real();
    struct file* fp = filp_open(BATT_FILENAME,O_RDONLY,0);
    
    //get true capacity
    for (i = 0; i < 50; i++)
    {
        tmp += adc_sync_read(gBatteryData->client);
        mdelay(1);
    }
    tmp = tmp / 50;
    tmp = (tmp * BAT_2V5_VALUE * (BAT_PULL_UP_R + BAT_PULL_DOWN_R)) / (1024 * BAT_PULL_DOWN_R);
	
	if(RK29_ADC_CLI_ADD)
		tmp += RK29_ADC_CLI_VALUE;
	else
		tmp -= RK29_ADC_CLI_VALUE;
	
    truecapacity = rk2918_get_bat_capacity_raw(tmp);
    
	if(IS_ERR(fp))
    {
		printk("hj---->open file /data/bat_last_capacity.dat failed\n");
		printk("truecapacity = %d\n", truecapacity);
		
		if(truecapacity>=100)
			truecapacity = 100;
	 	if(truecapacity==0)
			truecapacity=1;
			
		openfailflag = 1;

		if (openfailcount <= 5)
		{
			lastthree[openfailcount-1] = truecapacity;

			if (openfailcount == 1)
			{
				tmp = 0;
				for (i=0;i<5;i++)
				{
					tmp += lastthree[4-i];	
					printk("%s...............%d\n",__func__,tmp);
				}

				truecapacity = tmp/5;
				printk("%s...............%d\n",__func__,tmp);

			}
		}
		return truecapacity;
	}
	else
	{
		openfailflag = 0;
		openfailcount = 0;
	}
	kernel_read(fp,0,value,10);
    filp_close(fp,NULL);

	value[10]=0;
	while(*p)
	{
	    if(*p==0x0d)
	    {
			*p=0;
			break;
		}
		p++;
	}	

	//spendcnt_intervals = ktimetmp.tv.sec - get_spendcnt_from_datefile();
	sscanf(value,"%d",&loadcapacity);
	printk("hj---->loadcapacity = %d, truecapacity = %d\n",loadcapacity, truecapacity);
	if((loadcapacity < 0) || (loadcapacity > 100))
		loadcapacity = truecapacity;
	// will show capacity in last poweroff if not more than 6 houre in off state
#if !(defined (CONFIG_MACH_M900) || defined (CONFIG_MACH_M900HD) || defined (CONFIG_MACH_M900HDW))
	if(( spendcnt_intervals < SPENDCNT_IN_OFF_STATE) && ((truecapacity-loadcapacity) < 25))
	{			
		if (loadcapacity == 0)
		{
			loadcapacity = 1;
		}
		else
		{
			if (loadcapacity >= 100)
			{
				loadcapacity = 100;
			}
		}	
		return loadcapacity;
	}
	printk("off state more than 1 housr\n");
#endif
	//如果从文件中读取的电压比实际的高很多的话,说明是长时间放置导致放电
	if (loadcapacity > truecapacity)
	{
	    if (loadcapacity - truecapacity > 20)
	    {
	        loadcapacity = truecapacity;
	    }
	}
	else
	{
		if ( ((truecapacity-loadcapacity) >= 20))
		{

			if (truecapacity < 30)
			{
				if (loadcapacity < 10)
				{
					loadcapacity = truecapacity/2;
				}
			}
			else
			{

				loadcapacity = truecapacity;	
				
			}

		}


	}
	    
	if (loadcapacity == 0)
	{
		loadcapacity = 1;
	}
	else
	{
		if (loadcapacity >= 100)
		{
			loadcapacity = 100;
		}
	}
	
	lastlost = loadcapacity;
	return loadcapacity;
}
예제 #8
0
static int rk2918_get_bat_capacity_ext(int BatVoltage)
{
	int capacity = 0;
	int num = 0;
	int i = 0;
	static int lostcount = 0;
	uint8_t speedlose = 1;

	
    	capacity = rk2918_get_bat_capacity_raw(BatVoltage);
	capacitytmp = capacity;
	
	//充放电状态变化后,Buffer填满之前,不更新
    if (gBatStatusChangeCnt < NUM_VOLTAGE_SAMPLE)
    {
        capacity = gBatCapacity;
    }
        
    if (gBatChargeStatus == 1)
    {
 			
        if ((capacity > gBatCapacity) && (gBatCapacity < 99) && ((gBatCapacityUpCnt++) >= NUM_CHARGE_MIN_SAMPLE))
        {

            	capacity = gBatCapacity + 1; 
		     
        	 gBatCapacityUpCnt = 0;
             gBatHighCapacityChargeCnt = 0;
	      }
    	  else if ((capacity < gBatCapacity) && (gBatCapacity < 90))
        {    	
			  if (++gBatHighCapacityChargeCnt > NUM_CHARGE_MAX_SAMPLE*3)
			  {
				  //长时间内充电电压无变化,开始计时充电
				  capacity = gBatCapacity + 1;
				  gBatHighCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
			  }
			  else
			  {
				  capacity = gBatCapacity;
			  }

        }
        else
        {
                 capacity = gBatCapacity;
        }

		if (capacity == 0)
		{
			capacity = 1;
		}


	if (gBatStatusBack == POWER_SUPPLY_STATUS_FULL)
	{	
		capacity = 100;
	}	
	else if((gBatStatusBack==POWER_SUPPLY_STATUS_UNKNOWN)&&(gBatCapacity>=100))
	{
		capacity = 99;
	}

	//shutdownvoltage = 3500;
	
    }    
    else
    {   
        //放电时,只允许电压下降
        if (capacity > gBatCapacity)
        {     
            	capacity = gBatCapacity;				
        }
   
        if ((capacity < gBatCapacity) && ((gBatCapacityDownCnt++) >= NUM_DISCHARGE_MIN_SAMPLE))
        {  
 /*
 [ 6377.140657] gBatStatus = 3, adc_val = 775, TrueBatVol = 3784,gBatVol = 3764, gBatCap = 53, captmp = 56, sec = 1293972374,full_times=0,chargeok=1,flag =1,dcdet=0
 [ 6378.263192] gBatStatus = 3, adc_val = 775, TrueBatVol = 3784,gBatVol = 3269, gBatCap = 27, captmp = 0, sec = 1293973200,full_times=0,chargeok=1,flag =1,dcdet=0
 [ 6397.263182] gBatStatus = 3, adc_val = 780, TrueBatVol = 3808,gBatVol = 3806, gBatCap = 27, captmp = 62, sec = 1293973219,full_times=0,chargeok=1,flag =1,dcdet=0
		if(capacity==0)
		{
			if((gBatCapacity-capacity)>3)
				capacity = gBatCapacity -(gBatCapacity - capacity)/2;
			else
				capacity =0;
		}		
		else		
*/
			capacity = gBatCapacity - 1;       					
			gBatCapacityDownCnt = 0;	  
        }
        else
        {
            capacity = gBatCapacity;	
        }

	if(capacity>=100)
		capacity = 99;
	
	gBatStatusBack = POWER_SUPPLY_STATUS_UNKNOWN;

#if 0
 		if (lostcount++ > NUM_SPEEDLOSE_SAMPLE)
 		{
			lostcount = 0;
			if (((lastlost-gBatVoltage)>20)&&(gBatVoltage >= 3400))// start play game
			{
				shutdownvoltage = 3400;	
				entercount++;
				printk("%s...lastlost=%d. entercount=%d enter game\n",__func__,gBatVoltage,entercount);
			}
			else
			{
				if (((gBatVoltage-lastlost)>20)&& (entercount>0))//exit game
				{
					shutdownvoltage = 3500;
					entercount--;
					printk("%s.. lastlost=%d..entercount=%d exit game\n",__func__,gBatVoltage,entercount);
				}
			}
			lastlost = gBatVoltage;	
 		}
#endif


// <8%capacity enter game will make mistake
// 		if (((gBatVoltage<=shutdownvoltage) && (lastlost-gBatVoltage)<=3) || (gBatVoltage <= 3300))
		if ((gBatVoltage<shutdownvoltage) && (gBatCapacity < 8)) 
 		{	
 			shutdownflag++;
 			if(shutdownflag >= NUM_SHUTD0WN_SAMPLE)
			{
				capacity = 0;
				printk("%s.........%d...voltage is too small....%d\n",__func__,__LINE__,shutdownflag);
				shutdownflag = NUM_SHUTD0WN_SAMPLE+1;
			}
 		}	

 
    }

	return capacity;
}
예제 #9
0
static int rk2918_battery_resume_get_Capacity(int deltatime)
{
	int i;
	int tmp = 0;
	int capacity = 0;
	int num = 0;
	int voltagetmp = 0;
	pSamples = &gBatVoltageSamples[0];
    
    for (i = 0; i < 50; i++)
    {
         voltagetmp = adc_sync_read(gBatteryData->client);
         *pSamples = (voltagetmp * BAT_2V5_VALUE * (BAT_PULL_UP_R + BAT_PULL_DOWN_R)) / (1024 * BAT_PULL_DOWN_R);
	 	 tmp = tmp + *pSamples++;	
          mdelay(1);
    }

    tmp = tmp / 50;
    //tmp = (tmp * BAT_2V5_VALUE * (BAT_PULL_UP_R + BAT_PULL_DOWN_R)) / (1024 * BAT_PULL_DOWN_R);
    capacity = rk2918_get_bat_capacity_raw(tmp);
    printk("rk2918_battery_resume:%d,%d,%d,%d\n",capacity,gBatCapacity,tmp,gBatChargeStatus);
    if (gBatChargeStatus == 1)
    {
        //if (deltatime > (100 - gBatCapacity) * CHARGE_MIN_SECOND)
        //    deltatime = (100 - gBatCapacity) * CHARGE_MIN_SECOND;
       // if (capacity > gBatCapacity + (deltatime / CHARGE_MIN_SECOND))       //采样电池容量偏差较大,将容量拉回
        //{
       //     capacity = gBatCapacity + (deltatime / CHARGE_MIN_SECOND);
      //  }
        if(capacity > gBatCapacity)
        {
        	if((capacity-gBatCapacity)>5)
				gBatCapacity = capacity;
			else
			   capacity=gBatCapacity ;
        }
        else 
        {            	
			capacity = gBatCapacity;		
        }

		if ((gpio_get_value(gBatteryData->charge_ok_pin) ==  gBatteryData->charge_ok_level))
			{
				capacity = 100;
				gBatStatus = POWER_SUPPLY_STATUS_FULL;
				 gBatteryData->full_times = NUM_CHARGE_FULL_DELAY_TIMES + 1;
			}
    }
    else
    {
        //if (deltatime > gBatCapacity * DISCHARGE_MIN_SECOND)
           // deltatime = gBatCapacity * DISCHARGE_MIN_SECOND;            
        //if (capacity < gBatCapacity - (deltatime / DISCHARGE_MIN_SECOND))    //采样电池容量偏差较大,将容量拉回
        //{
          //  capacity = gBatCapacity - (deltatime / DISCHARGE_MIN_SECOND);
        //}
        if (capacity < gBatCapacity)
        {
			if((capacity-gBatCapacity)>5)
				gBatCapacity = capacity;
			else
			    capacity=gBatCapacity ;
        }
        else 
        {        	
			 capacity=gBatCapacity ;
        }
    }
    if (capacity == 0) capacity = 1;
    if (capacity >= 100) capacity = 100;
    
    if (gBatStatus == POWER_SUPPLY_STATUS_FULL)
   {
	    capacity = 100;
    }
		
    printk("rk2918_battery_resume: gBatVoltage = %d, gBatCapacity = %d, deltatime = %d, ktmietmp.tv.sec = %ul, capacity = %d,gFlagLoop=%d\n", 
           gBatVoltage, gBatCapacity, deltatime, batteryspendcnt, capacity,gFlagLoop);
    
    return capacity;
}
예제 #10
0
static int rk2918_battery_load_capacity(void)
{
    int i;
    int tmp = 0;
	int  loadcapacity = 0;
	int  truecapacity = 0;
    char value[11];
    static char lastthree[6]= {0};
	char* p = value;
	int flag = dwc_vbus_status();
    struct file* fp = filp_open(BATT_FILENAME,O_RDONLY,0);
  
    
    //get true capacity
    for (i = 0; i < 30; i++)
    {
        tmp += adc_sync_read(gBatteryData->client);
        mdelay(1);
    }
    tmp = tmp / 30;
    tmp = (tmp * BAT_2V5_VALUE * (BAT_PULL_UP_R + BAT_PULL_DOWN_R)) / (1024 * BAT_PULL_DOWN_R);
    truecapacity = rk2918_get_bat_capacity_raw(tmp);
 	
	
	if(IS_ERR(fp))
    {
		printk("hj---->open file /data/bat_last_capacity.dat failed\n");
		printk("truecapacity = %d\n", truecapacity);
		
		if(truecapacity>=100)
			truecapacity = 100;
	 	if(truecapacity==0)
			truecapacity=1;
#if 0
	if (flag == 1)
	{
		if (truecapacity > 60)
			truecapacity -= 35;
	}
#endif
		openfailflag = 1;

		if (openfailcount <= 5)
		{
			lastthree[openfailcount-1] = truecapacity;

			if (openfailcount == 1)
			{
				tmp = 0;
				for (i=0;i<5;i++)
				{
					tmp += lastthree[4-i];	
					printk("%s...............%d\n",__func__,tmp);
				}

				truecapacity = tmp/5;
				printk("%s...............%d\n",__func__,tmp);

			}
		}
		return truecapacity;
	}
	else
	{
		openfailflag = 0;
		openfailcount = 0;
	}


	kernel_read(fp,0,value,10);
    filp_close(fp,NULL);

	value[10]=0;
	while(*p)
	{
	    if(*p==0x0d)
	    {
			*p=0;
			break;
		}
		p++;
	}	
	sscanf(value,"%d",&loadcapacity);
	printk("hj---->loadcapacity = %d, truecapacity = %d\n",loadcapacity, truecapacity);
	
	//如果从文件中读取的电压比实际的高很多的话,说明是长时间放置导致放电
#if 0
	if (loadcapacity > truecapacity)
	{
	    if ((loadcapacity - truecapacity) > 15)
	    {
	        loadcapacity = truecapacity;
	    }
	}
	else
#endif

	{

		if (((truecapacity-loadcapacity) >= 25))
		{

			if (truecapacity < 30)
			{
				if (loadcapacity < 10)
				{
					loadcapacity = truecapacity/4;
				}
			}
			else
			{
				if (truecapacity < 80)
					loadcapacity = truecapacity - 8;
				else
					loadcapacity = truecapacity;
				
			}

		}
	}
	    
	if (loadcapacity == 0)
	{
		loadcapacity = 1;
	}
	else
	{
		if (loadcapacity >= 100)
		{
			loadcapacity = 99;
		}
	}
/*
	if (gpio_get_value(gBatteryData->charge_ok_pin) ==  gBatteryData->charge_ok_level)
	{
		loadcapacity = 99;
	}

	if (gBatStatus == POWER_SUPPLY_STATUS_FULL)
	{	
		loadcapacity = 100;
	}

*/
	lastlost = loadcapacity;
	return loadcapacity;
}
예제 #11
0
static int rk2918_get_bat_capacity_ext(int BatVoltage)
{
    int i = 0;
	int capacity = 0;
	static int lostcount = 0;
	
    capacity = rk2918_get_bat_capacity_raw(BatVoltage);
	capacitytmp = capacity;
	
	//��ŵ�״̬�仯��Buffer����֮ǰ��������
    if (gBatStatusChangeCnt < NUM_VOLTAGE_SAMPLE)
    {
        capacity = gBatCapacity;
    }
        
    if (gBatChargeStatus == 1)
    {
        if ((capacity > gBatCapacity)  && (gBatCapacity < 100))
        {
            //ʵ�ʲ���ĵ�ѹ����ʾ�ĵ�ѹ��������
            gBatHighCapacityChargeCnt = 0;
            if (gBatCapacityDownCnt == 0)
            {
                capacity = gBatCapacity + 1;
                gBatCapacityDownCnt = NUM_CHARGE_MIN_SAMPLE;
            }
            else
            {
                capacity = gBatCapacity;
            }
        }
        else if ((capacity <= gBatCapacity) && (gBatCapacity < 100))
        {
            capacity = gBatCapacity;
            
            //��ʱ���ڳ���ѹ�ޱ仯����ʼ������ʱ���
            if ((gBatCapacity >= 80) && (++gBatHighCapacityChargeCnt > NUM_CHARGE_MAX_SAMPLE))
            {
                capacity = gBatCapacity + 1;
                gBatHighCapacityChargeCnt = (NUM_CHARGE_MAX_SAMPLE - NUM_CHARGE_MID_SAMPLE);
            }
        }
		else
        {
            capacity = gBatCapacity;
        }
        
		if (capacity == 0)
		{
		    capacity = 1;
		}
		
		if (capacity >= 100)
		{
		    capacity = 99;
		}

		if (gBatStatus == POWER_SUPPLY_STATUS_FULL)
		{
		    capacity = 100;
		}
    }    
    else
    {   
        //�ŵ�ʱ,ֻ�����ѹ�½�
        if (capacity > gBatCapacity)
        {
            capacity = gBatCapacity;
        }
        
        if ((capacity < gBatCapacity) && (gBatCapacityDownCnt == 0))
        {
            capacity = gBatCapacity - 1;
            gBatCapacityDownCnt = NUM_DISCHARGE_MIN_SAMPLE;
        }
        else
        {
            capacity = gBatCapacity;
        }
		
	#if defined (CONFIG_MACH_M1005HN) || defined (CONFIG_MACH_M1005HNW) || defined (CONFIG_MACH_M1005HD) || \
		defined (CONFIG_ODYS_XPRESS)
		if (lostcount++ > NUM_SPEEDLOSE_SAMPLE)
		{
			lostcount = 0;
			if (((lastlost-gBatVoltage)>16)&&(gBatVoltage >= 6900))// start play game
			{
				shutdownvoltage = 6700; 
				printk("%s...lastlost=%d.enter game\n",__func__,gBatVoltage);
			}
			else
			{
				if ((gBatVoltage-lastlost)>16) //exit game
				{
					shutdownvoltage = 6900;
					printk("%s.. lastlost=%d..exit game\n",__func__,gBatVoltage);
				}
			}
			lastlost = gBatVoltage;
			
	
		}
	
		// <8%capacity enter game will make mistake
		if (((gBatVoltage<=shutdownvoltage) && (lastlost-gBatVoltage)<=6) || (gBatVoltage <= 6700))
		{	
			shutdownflag++;
			if(shutdownflag >= NUM_SHUTD0WN_SAMPLE)
			{
				capacity = 0;
				printk("%s.........%d.........%d\n",__func__,__LINE__,shutdownflag);
				shutdownflag = NUM_SHUTD0WN_SAMPLE+1;
			}
		}
	#else
		if (lostcount++ > NUM_SPEEDLOSE_SAMPLE)
		{
			lostcount = 0;
			if (((lastlost-gBatVoltage)>8)&&(gBatVoltage >= 3400))// start play game
			{
				shutdownvoltage = 3300; 
				printk("%s...lastlost=%d.enter game\n",__func__,gBatVoltage);
			}
			else
			{
				if ((gBatVoltage-lastlost)>8) //exit game
				{
					shutdownvoltage = 3400;
					printk("%s.. lastlost=%d..exit game\n",__func__,gBatVoltage);
				}
			}
			lastlost = gBatVoltage;
			
	
		}
	
		// <8%capacity enter game will make mistake
		if (((gBatVoltage<=shutdownvoltage) && (lastlost-gBatVoltage)<=3) || (gBatVoltage <= 3300))
		{	
			shutdownflag++;
			if(shutdownflag >= NUM_SHUTD0WN_SAMPLE)
			{
				capacity = 0;
				printk("%s.........%d.........%d\n",__func__,__LINE__,shutdownflag);
				shutdownflag = NUM_SHUTD0WN_SAMPLE+1;
			}
		}
	#endif
    }
    if (gBatCapacityDownCnt > 0)
    {
        gBatCapacityDownCnt --;
    }
    
	return capacity;
}