static int rk29_adc_battery_get_charge_level(struct rk29_adc_battery_data *bat)
{
	int charge_on = 0;
	struct rk29_adc_battery_platform_data *pdata = bat->pdata;

#if defined(CONFIG_BATTERY_RK29_AC_CHARGE)
	/* AX: In this case the battery manager signals the presence of the
	 * AC supply. It does not signal if it really charges.
	 * It would make more sense to supply the charging information represented
	 * by
	 */
	if (pdata->dc_det_pin != INVALID_GPIO)
	{
		if (gpio_get_value (pdata->dc_det_pin) == pdata->dc_det_level)
		{
			charge_on = 1;
			goto charge_out;
		}
	}
#endif

#if defined(CONFIG_BATTERY_RK29_USB_CHARGE)
	if (charge_on == 0)
	{
		if (suspend_flag) return;

		if (1 == dwc_vbus_status()) //��⵽USB���룬�����޷�ʶ���Ƿ��dz����
		{ //ͨ����ʱ���PCʶ���־�����ʱ��ⲻ����˵���dz��
			if (0 == get_msc_connect_flag())
			{ //��������ʱ�����һ��ʱ��֮�󣬿�ʼ������״̬
				if (++gBatUsbChargeCnt >= NUM_USBCHARGE_IDENTIFY_TIMES)
				{
					gBatUsbChargeCnt = NUM_USBCHARGE_IDENTIFY_TIMES + 1;
					charge_on = 1;
					goto charge_out;
				}
			} //���򣬲�������ģʽ
		}
		else
		{
			gBatUsbChargeCnt = 0;
			if (2 == dwc_vbus_status())
			{
				charge_on = 1;
				goto charge_out;
			}
		}
	}
#endif

	charge_out: return charge_on;
}
static int rk29_adc_battery_get_charge_level(struct rk29_adc_battery_data *bat)
{
    int charge_on = 0;
    struct rk29_adc_battery_platform_data *pdata = bat->pdata;
    
#if defined(CONFIG_BATTERY_RK29_AC_CHARGE)
    if (pdata->dc_det_pin != INVALID_GPIO)
    {
        if (gpio_get_value (pdata->dc_det_pin) == pdata->dc_det_level)
        {
            charge_on = 1;
            wake_lock(&batt_wake_lock);
        }
        else
            wake_unlock(&batt_wake_lock);
    }
#endif
    
#if defined(CONFIG_BATTERY_RK29_USB_CHARGE)
    if (charge_on == 0)
    {
        if (suspend_flag) return;
            
        if (1 == dwc_vbus_status())         //检测到USB插入,但是无法识别是否是充电器
        {                                   //通过延时检测PC识别标志,如果超时检测不到,说明是充电
            if (0 == get_msc_connect_flag())
            {                               //插入充电器时间大于一定时间之后,开始进入充电状态
                if (++gBatUsbChargeCnt >= NUM_USBCHARGE_IDENTIFY_TIMES)
                {
                    gBatUsbChargeCnt = NUM_USBCHARGE_IDENTIFY_TIMES + 1;
                    charge_on = 1;
                }
            }                               //否则,不进入充电模式
        }                   
        else
        {
            gBatUsbChargeCnt = 0;
            if (2 == dwc_vbus_status()) 
            {
                charge_on = 1;
            }
        }
    }
#endif

    return charge_on;
}
static int get_usb_charge_state(struct cw_battery *cw_bat)
{
        int charge_time;
        int time_from_boot;
        struct timespec ts;

        int gadget_status = get_gadget_connect_flag();
        int usb_status = dwc_vbus_status();

        get_monotonic_boottime(&ts);
        time_from_boot = ts.tv_sec;
        
        if (cw_bat->charger_init_mode) {
 
                if (usb_status == 1 || usb_status == 2) {
                        cw_bat->charger_init_mode = 0;
                } else if (time_from_boot < 8) {
                        usb_status = cw_bat->charger_init_mode;
                } else if (strstr(saved_command_line,"charger")) {
                        cw_bat->charger_init_mode = dwc_otg_check_dpdm();
                        usb_status = cw_bat->charger_init_mode;
                }
        }
#ifdef NO_STANDARD_AC_BIG_CHARGE_MODE 
        if (cw_bat->usb_online == 1) {
                
                charge_time = time_from_boot - cw_bat->sleep_time_charge_start - cw_bat->run_time_charge_start;
                if (charge_time > 3) {
                        if (gadget_status == 0 && dwc_vbus_status() == 1) {
                                usb_status = 2;
                        }
                }
        }
#endif
        return usb_status;
 
        dev_dbg(&cw_bat->client->dev, "%s usb_status=[%d],cw_bat->charger_mode=[%d],cw_bat->gadget_status=[%d], cw_bat->charger_init_mode = [%d]\n",__func__,usb_status,cw_bat->charger_mode,gadget_status, cw_bat->charger_init_mode);



}
Esempio n. 4
0
//extern int suspend_flag;
static int rk30_adc_battery_get_charge_level(struct rk30_adc_battery_data *bat)
{
	int charge_on = 0;
	struct rk30_adc_battery_platform_data *pdata = bat->pdata;

#if defined (CONFIG_BATTERY_RK30_AC_CHARGE)
	if (pdata->dc_det_pin != INVALID_GPIO){
		if (gpio_get_value (pdata->dc_det_pin) == pdata->dc_det_level){
			charge_on = 1;
		}
	}
#endif

#if defined  (CONFIG_BATTERY_RK30_USB_CHARGE)
	if (charge_on == 0){
		if (suspend_flag)
			return;
		if (1 == dwc_vbus_status()) {

			if (0 == get_msc_connect_flag()) {
				if (++gBatUsbChargeCnt >= NUM_USBCHARGE_IDENTIFY_TIMES) {
					gBatUsbChargeCnt = NUM_USBCHARGE_IDENTIFY_TIMES + 1;
					charge_on = 1;
				}
			}
		}                   
		else {
			gBatUsbChargeCnt = 0;
			if (2 == dwc_vbus_status()) {
				charge_on = 1;
			}
		}
	}
#endif
	return charge_on;
}
Esempio n. 5
0
static int rk2918_get_bat_capacity_raw(int BatVoltage)
{
	int i = 0;
	int capacity = 0;
	int *p = adc_raw_table_bat;
	int flag = dwc_vbus_status();

	if(gpio_get_value(RK29_PIN0_PA0)==0)//有USB插入。。
	{
	//	if(flag == 2)
		{
			p = adc_raw_table_ac;
			//printk("%s...ac\n",__FUNCTION__);
		}
	//	else
		{
	//		p = adc_raw_table_usb;
			//printk("%s...USB\n",__FUNCTION__);
		}
			
	}

	if(BatVoltage >= p[BAT_ADC_TABLE_LEN - 1])
	{
	    //当电压超过最大值
	    capacity = 100;
	}	
	else if(BatVoltage <= p[0])
	{
	    //当电压低于最小值
	    capacity = 0;
	}
	else
	{
    	//计算容量
    	for(i = 0; i < BAT_ADC_TABLE_LEN - 1; i++)
        {
    		
    		if((p[i] <= BatVoltage) && (BatVoltage < p[i+1]))
    		{
    			capacity = i * 10 + ((BatVoltage - p[i]) * 10) / (p[i+1] - p[i]);
    			break;
    		}
    	}
    }  
    return capacity;
}
Esempio n. 6
0
static void rk2918_get_charge_status(void)
{
    int charge_on = 0;
    
    if (gBatteryData->dc_det_pin != INVALID_GPIO)
    {
        if (gpio_get_value (gBatteryData->dc_det_pin) == gBatteryData->dc_det_level)
        {
            charge_on = 1;
        }
    }
    
#ifdef RK29_USB_CHARGE_SUPPORT
    if (charge_on == 0)
    {
        if (suspend_flag) return;
            
        if (1 == dwc_vbus_status())         //检测到USB插入,但是无法识别是否是充电器
        {                                   //通过延时检测PC识别标志,如果超时检测不到,说明是充电
            if (0 == get_msc_connect_flag())
            {                               //插入充电器时间大于一定时间之后,开始进入充电状态
                if (++gBatUsbChargeCnt >= NUM_USBCHARGE_IDENTIFY_TIMES)
                {
                    gBatUsbChargeCnt = NUM_USBCHARGE_IDENTIFY_TIMES + 1;
                    charge_on = 1;
                }
            }                               //否则,不进入充电模式
            #if defined(CONFIG_MACH_RK29_ACH8)
            charge_on = 1;
            #endif
        }                   
        else
        {
            gBatUsbChargeCnt = 0;
            if (2 == dwc_vbus_status()) 
            {
                charge_on = 1;
            }
        }
    }
#endif
        
    if (charge_on)
    {
        if(gBatChargeStatus !=1) 
        {            
            gBatChargeStatus = 1;
            gBatStatusChangeCnt = 0;        //状态变化开始计数
            rk2918_charge_enable();
        }
    } 
    else 
    {
        if(gBatChargeStatus != 0) 
        {
            gBatChargeStatus = 0;
            gBatStatusChangeCnt = 0;        //状态变化开始计数
            rk2918_charge_disable();
        }
    }
}
Esempio n. 7
0
void rk2918_get_charge_status(void)
{
    int charge_on = 0;
    int tmp = get_msc_connect_flag();
	int flag = dwc_vbus_status();
	static  char  last_flag = 0;


	//printk("%s...........get_msc_connect_flag=%d,dwc_vbus_status=%d\n",__FUNCTION__,tmp,flag);
#ifdef RK29_USB_CHARGE_SUPPORT
    if ((gpio_get_value(RK29_PIN0_PA0) == 0) &&(charge_on == 0) )
    {
        //if (suspend_flag) return;
 #if 1           
        if (1 == flag)         //USB PC
        {   
			charge_on = 1;
			//printk("PC\n");
        } 
 
        else
        {
             if (2 == flag)  //充电器
            {
                charge_on = 1;
				//printk("charger\n");
            }
        }
#endif
#if 0
	if (usb_insert == 2)
	{
		charge_on = 1;
		//printk("%s.......charging  with charger\n",__FUNCTION__);
	}   
#endif 
    }
	
#endif
//printk("%s...dwc_vbus_status=%d,get_msc_connect_flag=%d\n",__FUNCTION__,dwc_vbus_status(),tmp);
   if (charge_on)
    {
        if((gBatChargeStatus !=1) || (last_flag != flag)) 
        {            
            gBatChargeStatus = 1;
			last_flag = flag;
            gBatStatusChangeCnt = 0;        //状态变化开始计数
            wake_lock(&battery_wake_lock);
			if (flag == 2)
			{
				rk2918_charge_enable();
			}		
			
        }

    } 
    else 
    {
        if(gBatChargeStatus != 0) 
        {
            gBatChargeStatus = 0;
            gBatStatusChangeCnt = 0;        //状态变化开始计数
            wake_unlock(&battery_wake_lock);
		    rk2918_charge_disable();
			
        }
    }

}
Esempio n. 8
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;
}
Esempio n. 9
0
static void rk2918_get_charge_status(void)
{
    int charge_on = 0;
    
    if (gBatteryData->dc_det_pin != INVALID_GPIO)
    {
        if (gpio_get_value (gBatteryData->dc_det_pin) == gBatteryData->dc_det_level)
        {
            charge_on = 1;
        }
    }
    
#ifdef RK29_USB_CHARGE_SUPPORT
    if (charge_on == 0)
    {
        if (suspend_flag) return;
            
        if (1 == dwc_vbus_status())         //��⵽USB���룬�����޷�ʶ���Ƿ��dz����
        {                                   //ͨ����ʱ���PCʶ���־�����ʱ��ⲻ����˵���dz��
            if (0 == get_msc_connect_flag())
            {                               //��������ʱ�����һ��ʱ��֮�󣬿�ʼ������״̬
                if (++gBatUsbChargeCnt >= NUM_USBCHARGE_IDENTIFY_TIMES)
                {
                    gBatUsbChargeCnt = NUM_USBCHARGE_IDENTIFY_TIMES + 1;
                    charge_on = 1;
                }
            }                               //���򣬲�������ģʽ
            #if defined(CONFIG_MACH_RK29_ACH8)
            charge_on = 1;
            #endif
        }                   
        else
        {
            gBatUsbChargeCnt = 0;
            if (2 == dwc_vbus_status()) 
            {
                charge_on = 1;
            }
        }
    }
#endif
        
    if (charge_on)
    {
        if(gBatChargeStatus !=1) 
        {            
            gBatChargeStatus = 1;
            gBatStatusChangeCnt = 0;        //״̬�仯��ʼ����
            rk2918_charge_enable();
        }
    } 
    else 
    {
        if(gBatChargeStatus != 0) 
        {
            gBatChargeStatus = 0;
            gBatStatusChangeCnt = 0;        //״̬�仯��ʼ����
            rk2918_charge_disable();
        }
    }
}