Manually restart charger periodically
authorBalaji Rao <balajirrao@openmoko.org>
Mon, 29 Dec 2008 12:11:33 +0000 (12:11 +0000)
committerAndy Green <agreen@pads.home.warmcat.com>
Mon, 29 Dec 2008 12:11:33 +0000 (12:11 +0000)
This avoids excessive discharges seen in case of Li-ion batteries even
when charger is connected.

Signed-off-by: Balaji Rao <balajirrao@openmoko.org>

drivers/power/pcf50633-charger.c
include/linux/mfd/pcf50633/mbc.h

index 5cfdb27..b06a2ed 100644 (file)
@@ -143,6 +143,42 @@ static struct attribute_group mbc_attr_group = {
        .attrs  = mbc_sysfs_entries,
 };
 
+/* MBC state machine switches into charging mode when the battery voltage
+ * falls below 96% of a battery float voltage. But the voltage drop in Li-ion
+ * batteries is marginal(1~2 %) till about 80% of its capacity - which means,
+ * after a BATFULL, charging won't be restarted until 80%.
+ *
+ * This work_struct function restarts charging every
+ * CHARGING_RESTART_TIMEOUT seconds and makes sure we don't discharge too much
+ */
+
+#define CHARGING_RESTART_TIMEOUT       (900 * HZ)  /* 15 minutes */
+
+static void pcf50633_mbc_charging_restart(struct work_struct *work)
+{
+       struct pcf50633_mbc *mbc;
+       struct pcf50633 *pcf;
+       u8 mbcs2, chgmod;
+
+       mbc = container_of(work, struct pcf50633_mbc,
+                               charging_restart_work.work);
+       pcf = container_of(mbc, struct pcf50633, mbc);
+
+       mbcs2 = pcf50633_reg_read(pcf, PCF50633_REG_MBCS2);
+       chgmod = (mbcs2 & PCF50633_MBCS2_MBC_MASK);
+
+       if (chgmod != PCF50633_MBCS2_MBC_BAT_FULL)
+               return;
+
+       /* Restart charging */
+       pcf50633_reg_set_bit_mask(pcf, PCF50633_REG_MBCC1, PCF50633_MBCC1_RESUME,
+                                               PCF50633_MBCC1_RESUME);
+       mbc->usb_active = 1;
+       power_supply_changed(&mbc->usb);
+
+       dev_info(pcf->dev, "Charging restarted..\n");
+}
+
 static void pcf50633_mbc_irq_handler(struct pcf50633 *pcf, int irq, void *data)
 {
        struct pcf50633_mbc *mbc;
@@ -156,6 +192,7 @@ static void pcf50633_mbc_irq_handler(struct pcf50633 *pcf, int irq, void *data)
                mbc->usb_online = 0;
                mbc->usb_active = 0;
                pcf50633_mbc_usb_curlim_set(pcf, 0);
+               cancel_delayed_work_sync(&mbc->charging_restart_work);
        }
 
        /* Adapter */
@@ -170,6 +207,8 @@ static void pcf50633_mbc_irq_handler(struct pcf50633 *pcf, int irq, void *data)
        if (irq == PCF50633_IRQ_BATFULL) {
                mbc->usb_active = 0;
                mbc->adapter_active = 0;
+               schedule_delayed_work(&mbc->charging_restart_work,
+                                               CHARGING_RESTART_TIMEOUT);
        } else if (irq == PCF50633_IRQ_USBLIMON)
                mbc->usb_active = 0;
        else if (irq == PCF50633_IRQ_USBLIMOFF)
@@ -310,6 +349,9 @@ int __init pcf50633_mbc_probe(struct platform_device *pdev)
        mbc->ac.supplied_to             = pcf->pdata->batteries;
        mbc->ac.num_supplicants         = pcf->pdata->num_batteries;
 
+       INIT_DELAYED_WORK(&mbc->charging_restart_work,
+                               pcf50633_mbc_charging_restart);
+
        ret = power_supply_register(&pdev->dev, &mbc->adapter);
        if (ret)
                dev_err(pcf->dev, "failed to register adapter\n");
index 9a7938a..5ba4aee 100644 (file)
@@ -132,6 +132,8 @@ struct pcf50633_mbc {
        struct power_supply usb;
        struct power_supply adapter;
 
+       struct delayed_work charging_restart_work;
+
        struct platform_device *pdev;
 };
 #endif