ds2760_battery: add rated_capacity module parameter
[linux-2.6.git] / drivers / power / ds2760_battery.c
index 1d76892..ed0ea5e 100644 (file)
@@ -62,6 +62,14 @@ static unsigned int cache_time = 1000;
 module_param(cache_time, uint, 0644);
 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
 
+static unsigned int pmod_enabled;
+module_param(pmod_enabled, bool, 0644);
+MODULE_PARM_DESC(pmod_enabled, "PMOD enable bit");
+
+static unsigned int rated_capacity;
+module_param(rated_capacity, uint, 0644);
+MODULE_PARM_DESC(rated_capacity, "rated battery capacity, 10*mAh or index");
+
 /* Some batteries have their rated capacity stored a N * 10 mAh, while
  * others use an index into this table. */
 static int rated_capacities[] = {
@@ -180,10 +188,13 @@ static int ds2760_battery_read_status(struct ds2760_device_info *di)
        di->empty_uAh = battery_interpolate(scale, di->temp_C / 10);
        di->empty_uAh *= 1000; /* convert to µAh */
 
-       /* From Maxim Application Note 131: remaining capacity =
-        * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
-       di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
-                           (di->full_active_uAh - di->empty_uAh);
+       if (di->full_active_uAh == di->empty_uAh)
+               di->rem_capacity = 0;
+       else
+               /* From Maxim Application Note 131: remaining capacity =
+                * ((ICA - Empty Value) / (Full Value - Empty Value)) x 100% */
+               di->rem_capacity = ((di->accum_current_uAh - di->empty_uAh) * 100L) /
+                                   (di->full_active_uAh - di->empty_uAh);
 
        if (di->rem_capacity < 0)
                di->rem_capacity = 0;
@@ -256,6 +267,28 @@ static void ds2760_battery_update_status(struct ds2760_device_info *di)
                power_supply_changed(&di->bat);
 }
 
+static void ds2760_battery_write_status(struct ds2760_device_info *di,
+                                       char status)
+{
+       if (status == di->raw[DS2760_STATUS_REG])
+               return;
+
+       w1_ds2760_write(di->w1_dev, &status, DS2760_STATUS_WRITE_REG, 1);
+       w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
+       w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
+}
+
+static void ds2760_battery_write_rated_capacity(struct ds2760_device_info *di,
+                                               unsigned char rated_capacity)
+{
+       if (rated_capacity == di->raw[DS2760_RATED_CAPACITY])
+               return;
+
+       w1_ds2760_write(di->w1_dev, &rated_capacity, DS2760_RATED_CAPACITY, 1);
+       w1_ds2760_store_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
+       w1_ds2760_recall_eeprom(di->w1_dev, DS2760_EEPROM_BLOCK1);
+}
+
 static void ds2760_battery_work(struct work_struct *work)
 {
        struct ds2760_device_info *di = container_of(work,
@@ -319,6 +352,12 @@ static int ds2760_battery_get_property(struct power_supply *psy,
        case POWER_SUPPLY_PROP_TEMP:
                val->intval = di->temp_C;
                break;
+       case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
+               val->intval = di->life_sec;
+               break;
+       case POWER_SUPPLY_PROP_CAPACITY:
+               val->intval = di->rem_capacity;
+               break;
        default:
                return -EINVAL;
        }
@@ -335,13 +374,15 @@ static enum power_supply_property ds2760_battery_props[] = {
        POWER_SUPPLY_PROP_CHARGE_EMPTY,
        POWER_SUPPLY_PROP_CHARGE_NOW,
        POWER_SUPPLY_PROP_TEMP,
+       POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
+       POWER_SUPPLY_PROP_CAPACITY,
 };
 
 static int ds2760_battery_probe(struct platform_device *pdev)
 {
+       char status;
        int retval = 0;
        struct ds2760_device_info *di;
-       struct ds2760_platform_data *pdata;
 
        di = kzalloc(sizeof(*di), GFP_KERNEL);
        if (!di) {
@@ -351,19 +392,32 @@ static int ds2760_battery_probe(struct platform_device *pdev)
 
        platform_set_drvdata(pdev, di);
 
-       pdata = pdev->dev.platform_data;
-       di->dev         = &pdev->dev;
-       di->w1_dev           = pdev->dev.parent;
-       di->bat.name       = dev_name(&pdev->dev);
-       di->bat.type       = POWER_SUPPLY_TYPE_BATTERY;
-       di->bat.properties     = ds2760_battery_props;
-       di->bat.num_properties = ARRAY_SIZE(ds2760_battery_props);
-       di->bat.get_property   = ds2760_battery_get_property;
+       di->dev                 = &pdev->dev;
+       di->w1_dev              = pdev->dev.parent;
+       di->bat.name            = dev_name(&pdev->dev);
+       di->bat.type            = POWER_SUPPLY_TYPE_BATTERY;
+       di->bat.properties      = ds2760_battery_props;
+       di->bat.num_properties  = ARRAY_SIZE(ds2760_battery_props);
+       di->bat.get_property    = ds2760_battery_get_property;
        di->bat.external_power_changed =
                                  ds2760_battery_external_power_changed;
 
        di->charge_status = POWER_SUPPLY_STATUS_UNKNOWN;
 
+       /* enable sleep mode feature */
+       ds2760_battery_read_status(di);
+       status = di->raw[DS2760_STATUS_REG];
+       if (pmod_enabled)
+               status |= DS2760_STATUS_PMOD;
+       else
+               status &= ~DS2760_STATUS_PMOD;
+
+       ds2760_battery_write_status(di, status);
+
+       /* set rated capacity from module param */
+       if (rated_capacity)
+               ds2760_battery_write_rated_capacity(di, rated_capacity);
+
        retval = power_supply_register(&pdev->dev, &di->bat);
        if (retval) {
                dev_err(di->dev, "failed to register battery\n");