rk818-battery: fix Division by zero in kernel, and update regs definition
author许盛飞 <xsf@rock-chips.com>
Mon, 12 Jan 2015 03:40:45 +0000 (11:40 +0800)
committer许盛飞 <xsf@rock-chips.com>
Mon, 12 Jan 2015 03:40:45 +0000 (11:40 +0800)
Signed-off-by: 许盛飞 <xsf@rock-chips.com>
drivers/power/rk818_battery.c
include/linux/power/rk818_battery.h

index b72e2344e92c96fa8ae32055256bc4bbbe962ce3..68d66e38be6635371dfe16d7c2c186b139d372fd 100644 (file)
@@ -158,7 +158,6 @@ struct battery_info {
        int     debug_finish_temp_soc;
 };
 
-struct battery_info *data;
 struct battery_info *g_battery;
 u32 support_uboot_chrg;
 
@@ -169,7 +168,7 @@ extern void kernel_power_off(void);
 extern int rk818_set_bits(struct rk818 *rk818, u8 reg, u8 mask, u8 val);
 extern unsigned int irq_create_mapping(struct irq_domain *domain,
                                                                                        irq_hw_number_t hwirq);
-
+extern void rk_send_wakeup_key(void);
 static void update_battery_info(struct battery_info *di);
 
 #define        SUPPORT_USB_CHARGE
@@ -409,36 +408,12 @@ static struct device_attribute rk818_bat_attr[] = {
 
 #endif
 
-#define BATT_NUM  11
-
-static int batt_table[22];
-
-static ssize_t bat_param_read(struct device *dev, struct device_attribute *attr, char *buf)
-{
-       int i;
-
-       for (i = 0; i < BATT_NUM; i++)
-               printk(KERN_INFO"i = %d batt_table = %d\n", i, batt_table[i]);
-
-       for (i = 0; i < BATT_NUM; i++)
-               printk(KERN_INFO"i = %d batt_table = %d\n", i + BATT_NUM , batt_table[i+BATT_NUM]);
-       return 0;
-}
-
-static ssize_t bat_param_write(struct device *dev,
-                               struct device_attribute *attr, const char *buf, size_t size)
-{
-       return size;
-}
-
-
-DEVICE_ATTR(rk818batparam, 0664, bat_param_read, bat_param_write);
 static uint16_t get_relax_voltage(struct battery_info *di);
 
 static ssize_t show_state_attrs(struct device *dev,
                                struct device_attribute *attr, char *buf)
 {
-       printk(KERN_INFO"get_relax_voltage relax voltage = %d\n", get_relax_voltage(data));
+       struct battery_info *data = g_battery;
 
        if (0 == get_relax_voltage(data)) {
                return sprintf(buf,
@@ -1163,24 +1138,6 @@ batt_failed:
        return ret;
 }
 
-#if 1
-/*calc battery resister*/
-static void res_mode_init(struct battery_info *di)
-{
-       u8 ggcon;/*  chrg_ctrl_reg2; */
-       u8 ggsts;
-
-       battery_read(di->rk818, GGCON, &ggcon, 1);
-       battery_read(di->rk818, GGSTS, &ggsts, 1);
-
-       ggcon |= 0x01;
-       ggsts  &= ~0x01;
-       ggsts |= 0x60;
-       battery_write(di->rk818, GGCON, &ggcon, 1);
-       battery_write(di->rk818, GGSTS, &ggsts, 1);
-}
-#endif
-
 static void  _capacity_init(struct battery_info *di, u32 capacity)
 {
        u8 buf;
@@ -1555,9 +1512,9 @@ static void rk_battery_charger_init(struct  battery_info *di)
        DBG("old usb_ctrl_reg = 0x%2x, CHRG_CTRL_REG1 = 0x%2x\n ", usb_ctrl_reg, chrg_ctrl_reg1);
        usb_ctrl_reg &= (~0x0f);
 #ifdef SUPPORT_USB_CHARGE
-       usb_ctrl_reg |= (VLIM_4400MV | ILIM_45MA | CHRG_CT_EN);
+       usb_ctrl_reg |= (ILIM_450MA | CHRG_CT_EN);
 #else
-       usb_ctrl_reg |= (VLIM_4400MV | ILIM_3000MA) | CHRG_CT_EN);
+       usb_ctrl_reg |= (ILIM_3000MA | CHRG_CT_EN);
 #endif
        chrg_ctrl_reg1 &= (0x00);
        chrg_ctrl_reg1 |= (CHRG_EN) | (CHRG_VOL4200 | CHRG_CUR1400mA);
@@ -1628,7 +1585,7 @@ static void  fg_init(struct battery_info *di)
        _set_cal_offset(di, di->current_offset+42);
        _rsoc_init(di);
        _capacity_init(di, di->nac);
-       res_mode_init(di);
+
        di->remain_capacity = _get_realtime_capacity(di);
        di->current_avg = _get_average_current(di);
 
@@ -1799,6 +1756,8 @@ static void voltage_to_soc_discharge_smooth(struct battery_info *di)
 
        voltage = di->voltage;
        now_current = di->current_avg;
+       if (now_current == 0)
+               now_current = 1;
        soc_time = di->fcc*3600/100/(abs_int(now_current));
        _voltage_to_capacity(di, 3800);
        volt_to_soc = di->temp_soc;
@@ -1952,6 +1911,8 @@ static void voltage_to_soc_charge_smooth(struct battery_info *di)
        int now_current, soc_time;
 
        now_current = _get_average_current(di);
+       if (now_current == 0)
+               now_current = 1;
        soc_time = di->fcc*3600/100/(abs_int(now_current));   /* 1%  time; */
        di->temp_soc = _get_soc(di);
 
@@ -2082,7 +2043,7 @@ static int estimate_battery_resister(struct battery_info *di)
        if (delta_time >= 20) {/*20s*/
 
                /*first sample*/
-               set_charge_current(di, ILIM_45MA);/*450mA*/
+               set_charge_current(di, ILIM_450MA);/*450mA*/
                msleep(1000);
                for (i = 0; i < 10 ; i++) {
                        msleep(100);
@@ -2339,7 +2300,7 @@ static int  get_charging_status_type(struct battery_info *di)
        if (di->ac_online == 1)
                set_charge_current(di, ILIM_3000MA);
        else
-               set_charge_current(di, ILIM_45MA);
+               set_charge_current(di, ILIM_450MA);
        return otg_status;
 }
 
@@ -2359,9 +2320,9 @@ static void battery_poweron_status_init(struct battery_info *di)
        if (otg_status == 1) {
                di->usb_online = 1;
                di->ac_online = 0;
-               set_charge_current(di, ILIM_45MA);
+               set_charge_current(di, ILIM_450MA);
                di->status = POWER_SUPPLY_STATUS_CHARGING;
-               DBG("++++++++ILIM_45MA++++++\n");
+               DBG("++++++++ILIM_450MA++++++\n");
 
        } else if (otg_status == 2) {
                di->usb_online = 0;
@@ -2648,13 +2609,6 @@ static int rk818_battery_sysfs_init(struct battery_info *di, struct device *dev)
        int i;
        struct kobject *rk818_fg_kobj;
 
-       ret = device_create_file(dev, &dev_attr_rk818batparam);
-       if (ret) {
-               ret = -EINVAL;
-               dev_err(dev, "failed to create bat param file\n");
-               goto err_sysfs;
-       }
-
        ret = create_sysfs_interfaces(dev);
        if (ret < 0) {
                ret = -EINVAL;
@@ -2796,7 +2750,6 @@ static int rk_battery_parse_dt(struct rk818 *rk818, struct device *dev)
                dev_err(dev, "kzalloc for battery_platform_data failed!\n");
                return -ENOMEM;
        }
-       memset(data, 0, sizeof(*data));
 
        cell_cfg = devm_kzalloc(rk818->dev, sizeof(*cell_cfg), GFP_KERNEL);
        if (!cell_cfg) {
@@ -2980,7 +2933,8 @@ static int battery_resume(struct platform_device *dev)
        queue_delayed_work(di->wq, &di->battery_monitor_work,
                                        msecs_to_jiffies(TIMER_MS_COUNTS/2));
 
-       if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING || di->real_soc <= 5)
+       if (di->sleep_status == POWER_SUPPLY_STATUS_CHARGING ||
+                       di->real_soc <= 5)
                wake_lock_timeout(&di->resume_wake_lock, 5*HZ);
 
        return 0;
index 5ac820b304ca79e5288ac4a1268039f2200ddbab..7519c05c52cbc05b2bc50cacb2eb37c46670db6e 100644 (file)
@@ -168,6 +168,7 @@ bit  0: disable 1: enable
 /* auto sleep mode 0:disable 1:enable*/
 #define FRAME_SMP_INTERV_TIME          0x1F
 
+/*VB_MOD_REG*/
 #define PLUG_IN_STS                                    (1<<6)
 
 /*SUP_STS_REG*/
@@ -188,6 +189,7 @@ bit  0: disable 1: enable
 /*USB_CTRL_REG*/
 #define CHRG_CT_EN                                     (1<<7)
 /* USB_VLIM_SEL*/
+/*
 #define VLIM_4000MV                                    (0x00<<4)
 #define VLIM_4100MV                                    (0x01<<4)
 #define VLIM_4200MV                                    (0x02<<4)
@@ -196,22 +198,22 @@ bit  0: disable 1: enable
 #define VLIM_4500MV                                    (0x05<<4)
 #define VLIM_4600MV                                    (0x06<<4)
 #define VLIM_4700MV                                    (0x07<<4)
+*/
+
 /*USB_ILIM_SEL*/
-#define ILIM_45MA                                      (0x00)
-#define ILIM_300MA                                     (0x01)
-#define ILIM_80MA                                      (0x02)
-#define ILIM_820MA                                     (0x03)
-#define ILIM_1000MA                                    (0x04)
-#define ILIM_1200MA                                    (0x05)
-#define ILIM_1400MA                                    (0x06)
-#define ILIM_1600MA                                    (0x07)
-#define ILIM_1800MA                                    (0x08)
-#define ILIM_2000MA                                    (0x09)
-#define ILIM_2200MA                                    (0x0A)
-#define ILIM_2400MA                                    (0x0B)
-#define ILIM_2600MA                                    (0x0C)
-#define ILIM_2800MA                                    (0x0D)
-#define ILIM_3000MA                                    (0x0E)
+#define ILIM_450MA                                     (0x00)
+#define ILIM_800MA                                     (0x01)
+#define ILIM_850MA                                     (0x02)
+#define ILIM_1000MA                                    (0x03)
+#define ILIM_1250MA                                    (0x04)
+#define ILIM_1500MA                                    (0x05)
+#define ILIM_1750MA                                    (0x06)
+#define ILIM_2000MA                                    (0x07)
+#define ILIM_2250MA                                    (0x08)
+#define ILIM_2500MA                                    (0x09)
+#define ILIM_2750MA                                    (0x0A)
+#define ILIM_3000MA                                    (0x0B)
+
 
 /*CHRG_CTRL_REG*/
 #define CHRG_EN                                                (0x01<<7)
@@ -238,10 +240,10 @@ bit  0: disable 1: enable
 #define CHRG_CUR3000mA                 (0x0A)
 
 /*CHRG_CTRL_REG2*/
-#define FINISH_100MA   (0X00 << 6)
-#define FINISH_150MA   (0X01 << 6)
-#define FINISH_200MA   (0X10 << 6)
-#define FINISH_250MA   (0X11 << 6)
+#define FINISH_100MA                           (0x00<<6)
+#define FINISH_150MA                           (0x01<<6)
+#define FINISH_200MA                           (0x02<<6)
+#define FINISH_250MA                           (0x03<<6)
 
 /* CHRG_CTRL_REG2*/
 #define CHRG_TERM_ANA_SIGNAL (0 << 5)
@@ -602,6 +604,6 @@ struct battery_platform_data {
        struct cell_config *cell_cfg;
 };
 
-extern void rk_send_wakeup_key(void);
+
 
 #endif