Merge git://git.infradead.org/battery-2.6
authorLinus Torvalds <torvalds@linux-foundation.org>
Sat, 26 Mar 2011 04:00:29 +0000 (21:00 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Sat, 26 Mar 2011 04:00:29 +0000 (21:00 -0700)
* git://git.infradead.org/battery-2.6: (30 commits)
  bq20z75: Fix time and temp units
  bq20z75: Fix issues with present and suspend
  z2_battery: Fix count of properties
  s3c_adc_battery: Fix method names when PM not set
  z2_battery: Add MODULE_DEVICE_TABLE
  ds2782_battery: Add MODULE_DEVICE_TABLE
  bq20z75: Add MODULE_DEVICE_TABLE
  power_supply: Update power_supply_is_watt_property
  bq20z75: Add i2c retry mechanism
  bq20z75: Add optional battery detect gpio
  twl4030_charger: Make the driver atomic notifier safe
  bq27x00: Use single i2c_transfer call for property read
  bq27x00: Cleanup bq27x00_i2c_read
  bq27x00: Minor cleanups
  bq27x00: Give more specific reports on battery status
  bq27x00: Add MODULE_DEVICE_TABLE
  bq27x00: Add new properties
  bq27x00: Poll battery state
  bq27x00: Cache battery registers
  bq27x00: Add bq27000 support
  ...

15 files changed:
drivers/leds/led-triggers.c
drivers/power/Kconfig
drivers/power/bq20z75.c
drivers/power/bq27x00_battery.c
drivers/power/ds2782_battery.c
drivers/power/power_supply_core.c
drivers/power/power_supply_leds.c
drivers/power/power_supply_sysfs.c
drivers/power/s3c_adc_battery.c
drivers/power/twl4030_charger.c
drivers/power/z2_battery.c
include/linux/leds.h
include/linux/power/bq20z75.h [new file with mode: 0644]
include/linux/power/bq27x00_battery.h [new file with mode: 0644]
include/linux/power_supply.h

index c41eb6180c9c2eb2e88ce2dfbfc64d7ff90daf2a..4bebae733349b90f84230d1b4454942cc9744451 100644 (file)
@@ -231,6 +231,26 @@ void led_trigger_event(struct led_trigger *trigger,
 }
 EXPORT_SYMBOL_GPL(led_trigger_event);
 
+void led_trigger_blink(struct led_trigger *trigger,
+                      unsigned long *delay_on,
+                      unsigned long *delay_off)
+{
+       struct list_head *entry;
+
+       if (!trigger)
+               return;
+
+       read_lock(&trigger->leddev_list_lock);
+       list_for_each(entry, &trigger->led_cdevs) {
+               struct led_classdev *led_cdev;
+
+               led_cdev = list_entry(entry, struct led_classdev, trig_list);
+               led_blink_set(led_cdev, delay_on, delay_off);
+       }
+       read_unlock(&trigger->leddev_list_lock);
+}
+EXPORT_SYMBOL_GPL(led_trigger_blink);
+
 void led_trigger_register_simple(const char *name, struct led_trigger **tp)
 {
        struct led_trigger *trigger;
index 61bf5d724139c80a93fd86350dd7ebb46f9eb8c0..52a462fc6b84467f3aafca8d09cf5630b7d500bd 100644 (file)
@@ -117,10 +117,24 @@ config BATTERY_BQ20Z75
 
 config BATTERY_BQ27x00
        tristate "BQ27x00 battery driver"
+       help
+         Say Y here to enable support for batteries with BQ27x00 (I2C/HDQ) chips.
+
+config BATTERY_BQ27X00_I2C
+       bool "BQ27200/BQ27500 support"
+       depends on BATTERY_BQ27x00
        depends on I2C
+       default y
        help
          Say Y here to enable support for batteries with BQ27x00 (I2C) chips.
 
+config BATTERY_BQ27X00_PLATFORM
+       bool "BQ27000 support"
+       depends on BATTERY_BQ27x00
+       default y
+       help
+         Say Y here to enable support for batteries with BQ27000 (HDQ) chips.
+
 config BATTERY_DA9030
        tristate "DA9030 battery driver"
        depends on PMIC_DA903X
index 492da27e1a47ed8960bd76e7626225e7fe0510f8..506585e31a5bee12ff53e3d7b3604f11d9d70902 100644 (file)
 #include <linux/power_supply.h>
 #include <linux/i2c.h>
 #include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/gpio.h>
+
+#include <linux/power/bq20z75.h>
 
 enum {
        REG_MANUFACTURER_DATA,
@@ -38,11 +42,22 @@ enum {
        REG_CYCLE_COUNT,
        REG_SERIAL_NUMBER,
        REG_REMAINING_CAPACITY,
+       REG_REMAINING_CAPACITY_CHARGE,
        REG_FULL_CHARGE_CAPACITY,
+       REG_FULL_CHARGE_CAPACITY_CHARGE,
        REG_DESIGN_CAPACITY,
+       REG_DESIGN_CAPACITY_CHARGE,
        REG_DESIGN_VOLTAGE,
 };
 
+/* Battery Mode defines */
+#define BATTERY_MODE_OFFSET            0x03
+#define BATTERY_MODE_MASK              0x8000
+enum bq20z75_battery_mode {
+       BATTERY_MODE_AMPS,
+       BATTERY_MODE_WATTS
+};
+
 /* manufacturer access defines */
 #define MANUFACTURER_ACCESS_STATUS     0x0006
 #define MANUFACTURER_ACCESS_SLEEP      0x0011
@@ -78,8 +93,12 @@ static const struct bq20z75_device_data {
                BQ20Z75_DATA(POWER_SUPPLY_PROP_CAPACITY, 0x0E, 0, 100),
        [REG_REMAINING_CAPACITY] =
                BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_NOW, 0x0F, 0, 65535),
+       [REG_REMAINING_CAPACITY_CHARGE] =
+               BQ20Z75_DATA(POWER_SUPPLY_PROP_CHARGE_NOW, 0x0F, 0, 65535),
        [REG_FULL_CHARGE_CAPACITY] =
                BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_FULL, 0x10, 0, 65535),
+       [REG_FULL_CHARGE_CAPACITY_CHARGE] =
+               BQ20Z75_DATA(POWER_SUPPLY_PROP_CHARGE_FULL, 0x10, 0, 65535),
        [REG_TIME_TO_EMPTY] =
                BQ20Z75_DATA(POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, 0x12, 0,
                        65535),
@@ -93,6 +112,9 @@ static const struct bq20z75_device_data {
        [REG_DESIGN_CAPACITY] =
                BQ20Z75_DATA(POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN, 0x18, 0,
                        65535),
+       [REG_DESIGN_CAPACITY_CHARGE] =
+               BQ20Z75_DATA(POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, 0x18, 0,
+                       65535),
        [REG_DESIGN_VOLTAGE] =
                BQ20Z75_DATA(POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN, 0x19, 0,
                        65535),
@@ -117,39 +139,72 @@ static enum power_supply_property bq20z75_properties[] = {
        POWER_SUPPLY_PROP_ENERGY_NOW,
        POWER_SUPPLY_PROP_ENERGY_FULL,
        POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
+       POWER_SUPPLY_PROP_CHARGE_NOW,
+       POWER_SUPPLY_PROP_CHARGE_FULL,
+       POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
 };
 
 struct bq20z75_info {
-       struct i2c_client       *client;
-       struct power_supply     power_supply;
+       struct i2c_client               *client;
+       struct power_supply             power_supply;
+       struct bq20z75_platform_data    *pdata;
+       bool                            is_present;
+       bool                            gpio_detect;
+       bool                            enable_detection;
+       int                             irq;
 };
 
 static int bq20z75_read_word_data(struct i2c_client *client, u8 address)
 {
-       s32 ret;
+       struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client);
+       s32 ret = 0;
+       int retries = 1;
+
+       if (bq20z75_device->pdata)
+               retries = max(bq20z75_device->pdata->i2c_retry_count + 1, 1);
+
+       while (retries > 0) {
+               ret = i2c_smbus_read_word_data(client, address);
+               if (ret >= 0)
+                       break;
+               retries--;
+       }
 
-       ret = i2c_smbus_read_word_data(client, address);
        if (ret < 0) {
-               dev_err(&client->dev,
+               dev_dbg(&client->dev,
                        "%s: i2c read at address 0x%x failed\n",
                        __func__, address);
                return ret;
        }
+
        return le16_to_cpu(ret);
 }
 
 static int bq20z75_write_word_data(struct i2c_client *client, u8 address,
        u16 value)
 {
-       s32 ret;
+       struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client);
+       s32 ret = 0;
+       int retries = 1;
+
+       if (bq20z75_device->pdata)
+               retries = max(bq20z75_device->pdata->i2c_retry_count + 1, 1);
+
+       while (retries > 0) {
+               ret = i2c_smbus_write_word_data(client, address,
+                       le16_to_cpu(value));
+               if (ret >= 0)
+                       break;
+               retries--;
+       }
 
-       ret = i2c_smbus_write_word_data(client, address, le16_to_cpu(value));
        if (ret < 0) {
-               dev_err(&client->dev,
+               dev_dbg(&client->dev,
                        "%s: i2c write to address 0x%x failed\n",
                        __func__, address);
                return ret;
        }
+
        return 0;
 }
 
@@ -158,6 +213,19 @@ static int bq20z75_get_battery_presence_and_health(
        union power_supply_propval *val)
 {
        s32 ret;
+       struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client);
+
+       if (psp == POWER_SUPPLY_PROP_PRESENT &&
+               bq20z75_device->gpio_detect) {
+               ret = gpio_get_value(
+                       bq20z75_device->pdata->battery_detect);
+               if (ret == bq20z75_device->pdata->battery_detect_present)
+                       val->intval = 1;
+               else
+                       val->intval = 0;
+               bq20z75_device->is_present = val->intval;
+               return ret;
+       }
 
        /* Write to ManufacturerAccess with
         * ManufacturerAccess command and then
@@ -165,9 +233,11 @@ static int bq20z75_get_battery_presence_and_health(
        ret = bq20z75_write_word_data(client,
                bq20z75_data[REG_MANUFACTURER_DATA].addr,
                MANUFACTURER_ACCESS_STATUS);
-       if (ret < 0)
+       if (ret < 0) {
+               if (psp == POWER_SUPPLY_PROP_PRESENT)
+                       val->intval = 0; /* battery removed */
                return ret;
-
+       }
 
        ret = bq20z75_read_word_data(client,
                bq20z75_data[REG_MANUFACTURER_DATA].addr);
@@ -248,30 +318,39 @@ static void  bq20z75_unit_adjustment(struct i2c_client *client,
 {
 #define BASE_UNIT_CONVERSION           1000
 #define BATTERY_MODE_CAP_MULT_WATT     (10 * BASE_UNIT_CONVERSION)
-#define TIME_UNIT_CONVERSION           600
-#define TEMP_KELVIN_TO_CELCIUS         2731
+#define TIME_UNIT_CONVERSION           60
+#define TEMP_KELVIN_TO_CELSIUS         2731
        switch (psp) {
        case POWER_SUPPLY_PROP_ENERGY_NOW:
        case POWER_SUPPLY_PROP_ENERGY_FULL:
        case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
+               /* bq20z75 provides energy in units of 10mWh.
+                * Convert to µWh
+                */
                val->intval *= BATTERY_MODE_CAP_MULT_WATT;
                break;
 
        case POWER_SUPPLY_PROP_VOLTAGE_NOW:
        case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
        case POWER_SUPPLY_PROP_CURRENT_NOW:
+       case POWER_SUPPLY_PROP_CHARGE_NOW:
+       case POWER_SUPPLY_PROP_CHARGE_FULL:
+       case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
                val->intval *= BASE_UNIT_CONVERSION;
                break;
 
        case POWER_SUPPLY_PROP_TEMP:
-               /* bq20z75 provides battery tempreture in 0.1°K
-                * so convert it to 0.1°C */
-               val->intval -= TEMP_KELVIN_TO_CELCIUS;
-               val->intval *= 10;
+               /* bq20z75 provides battery temperature in 0.1K
+                * so convert it to 0.1°C
+                */
+               val->intval -= TEMP_KELVIN_TO_CELSIUS;
                break;
 
        case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
        case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
+               /* bq20z75 provides time to empty and time to full in minutes.
+                * Convert to seconds
+                */
                val->intval *= TIME_UNIT_CONVERSION;
                break;
 
@@ -281,11 +360,44 @@ static void  bq20z75_unit_adjustment(struct i2c_client *client,
        }
 }
 
+static enum bq20z75_battery_mode
+bq20z75_set_battery_mode(struct i2c_client *client,
+       enum bq20z75_battery_mode mode)
+{
+       int ret, original_val;
+
+       original_val = bq20z75_read_word_data(client, BATTERY_MODE_OFFSET);
+       if (original_val < 0)
+               return original_val;
+
+       if ((original_val & BATTERY_MODE_MASK) == mode)
+               return mode;
+
+       if (mode == BATTERY_MODE_AMPS)
+               ret = original_val & ~BATTERY_MODE_MASK;
+       else
+               ret = original_val | BATTERY_MODE_MASK;
+
+       ret = bq20z75_write_word_data(client, BATTERY_MODE_OFFSET, ret);
+       if (ret < 0)
+               return ret;
+
+       return original_val & BATTERY_MODE_MASK;
+}
+
 static int bq20z75_get_battery_capacity(struct i2c_client *client,
        int reg_offset, enum power_supply_property psp,
        union power_supply_propval *val)
 {
        s32 ret;
+       enum bq20z75_battery_mode mode = BATTERY_MODE_WATTS;
+
+       if (power_supply_is_amp_property(psp))
+               mode = BATTERY_MODE_AMPS;
+
+       mode = bq20z75_set_battery_mode(client, mode);
+       if (mode < 0)
+               return mode;
 
        ret = bq20z75_read_word_data(client, bq20z75_data[reg_offset].addr);
        if (ret < 0)
@@ -298,6 +410,10 @@ static int bq20z75_get_battery_capacity(struct i2c_client *client,
        } else
                val->intval = ret;
 
+       ret = bq20z75_set_battery_mode(client, mode);
+       if (ret < 0)
+               return ret;
+
        return 0;
 }
 
@@ -318,12 +434,25 @@ static int bq20z75_get_battery_serial_number(struct i2c_client *client,
        return 0;
 }
 
+static int bq20z75_get_property_index(struct i2c_client *client,
+       enum power_supply_property psp)
+{
+       int count;
+       for (count = 0; count < ARRAY_SIZE(bq20z75_data); count++)
+               if (psp == bq20z75_data[count].psp)
+                       return count;
+
+       dev_warn(&client->dev,
+               "%s: Invalid Property - %d\n", __func__, psp);
+
+       return -EINVAL;
+}
+
 static int bq20z75_get_property(struct power_supply *psy,
        enum power_supply_property psp,
        union power_supply_propval *val)
 {
-       int count;
-       int ret;
+       int ret = 0;
        struct bq20z75_info *bq20z75_device = container_of(psy,
                                struct bq20z75_info, power_supply);
        struct i2c_client *client = bq20z75_device->client;
@@ -332,8 +461,8 @@ static int bq20z75_get_property(struct power_supply *psy,
        case POWER_SUPPLY_PROP_PRESENT:
        case POWER_SUPPLY_PROP_HEALTH:
                ret = bq20z75_get_battery_presence_and_health(client, psp, val);
-               if (ret)
-                       return ret;
+               if (psp == POWER_SUPPLY_PROP_PRESENT)
+                       return 0;
                break;
 
        case POWER_SUPPLY_PROP_TECHNOLOGY:
@@ -343,22 +472,19 @@ static int bq20z75_get_property(struct power_supply *psy,
        case POWER_SUPPLY_PROP_ENERGY_NOW:
        case POWER_SUPPLY_PROP_ENERGY_FULL:
        case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
+       case POWER_SUPPLY_PROP_CHARGE_NOW:
+       case POWER_SUPPLY_PROP_CHARGE_FULL:
+       case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
        case POWER_SUPPLY_PROP_CAPACITY:
-               for (count = 0; count < ARRAY_SIZE(bq20z75_data); count++) {
-                       if (psp == bq20z75_data[count].psp)
-                               break;
-               }
-
-               ret = bq20z75_get_battery_capacity(client, count, psp, val);
-               if (ret)
-                       return ret;
+               ret = bq20z75_get_property_index(client, psp);
+               if (ret < 0)
+                       break;
 
+               ret = bq20z75_get_battery_capacity(client, ret, psp, val);
                break;
 
        case POWER_SUPPLY_PROP_SERIAL_NUMBER:
                ret = bq20z75_get_battery_serial_number(client, val);
-               if (ret)
-                       return ret;
                break;
 
        case POWER_SUPPLY_PROP_STATUS:
@@ -369,15 +495,11 @@ static int bq20z75_get_property(struct power_supply *psy,
        case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
        case POWER_SUPPLY_PROP_TIME_TO_FULL_AVG:
        case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
-               for (count = 0; count < ARRAY_SIZE(bq20z75_data); count++) {
-                       if (psp == bq20z75_data[count].psp)
-                               break;
-               }
-
-               ret = bq20z75_get_battery_property(client, count, psp, val);
-               if (ret)
-                       return ret;
+               ret = bq20z75_get_property_index(client, psp);
+               if (ret < 0)
+                       break;
 
+               ret = bq20z75_get_battery_property(client, ret, psp, val);
                break;
 
        default:
@@ -386,26 +508,58 @@ static int bq20z75_get_property(struct power_supply *psy,
                return -EINVAL;
        }
 
-       /* Convert units to match requirements for power supply class */
-       bq20z75_unit_adjustment(client, psp, val);
+       if (!bq20z75_device->enable_detection)
+               goto done;
+
+       if (!bq20z75_device->gpio_detect &&
+               bq20z75_device->is_present != (ret >= 0)) {
+               bq20z75_device->is_present = (ret >= 0);
+               power_supply_changed(&bq20z75_device->power_supply);
+       }
+
+done:
+       if (!ret) {
+               /* Convert units to match requirements for power supply class */
+               bq20z75_unit_adjustment(client, psp, val);
+       }
 
        dev_dbg(&client->dev,
-               "%s: property = %d, value = %d\n", __func__, psp, val->intval);
+               "%s: property = %d, value = %x\n", __func__, psp, val->intval);
+
+       if (ret && bq20z75_device->is_present)
+               return ret;
+
+       /* battery not present, so return NODATA for properties */
+       if (ret)
+               return -ENODATA;
 
        return 0;
 }
 
-static int bq20z75_probe(struct i2c_client *client,
+static irqreturn_t bq20z75_irq(int irq, void *devid)
+{
+       struct power_supply *battery = devid;
+
+       power_supply_changed(battery);
+
+       return IRQ_HANDLED;
+}
+
+static int __devinit bq20z75_probe(struct i2c_client *client,
        const struct i2c_device_id *id)
 {
        struct bq20z75_info *bq20z75_device;
+       struct bq20z75_platform_data *pdata = client->dev.platform_data;
        int rc;
+       int irq;
 
        bq20z75_device = kzalloc(sizeof(struct bq20z75_info), GFP_KERNEL);
        if (!bq20z75_device)
                return -ENOMEM;
 
        bq20z75_device->client = client;
+       bq20z75_device->enable_detection = false;
+       bq20z75_device->gpio_detect = false;
        bq20z75_device->power_supply.name = "battery";
        bq20z75_device->power_supply.type = POWER_SUPPLY_TYPE_BATTERY;
        bq20z75_device->power_supply.properties = bq20z75_properties;
@@ -413,26 +567,86 @@ static int bq20z75_probe(struct i2c_client *client,
                ARRAY_SIZE(bq20z75_properties);
        bq20z75_device->power_supply.get_property = bq20z75_get_property;
 
+       if (pdata) {
+               bq20z75_device->gpio_detect =
+                       gpio_is_valid(pdata->battery_detect);
+               bq20z75_device->pdata = pdata;
+       }
+
        i2c_set_clientdata(client, bq20z75_device);
 
+       if (!bq20z75_device->gpio_detect)
+               goto skip_gpio;
+
+       rc = gpio_request(pdata->battery_detect, dev_name(&client->dev));
+       if (rc) {
+               dev_warn(&client->dev, "Failed to request gpio: %d\n", rc);
+               bq20z75_device->gpio_detect = false;
+               goto skip_gpio;
+       }
+
+       rc = gpio_direction_input(pdata->battery_detect);
+       if (rc) {
+               dev_warn(&client->dev, "Failed to get gpio as input: %d\n", rc);
+               gpio_free(pdata->battery_detect);
+               bq20z75_device->gpio_detect = false;
+               goto skip_gpio;
+       }
+
+       irq = gpio_to_irq(pdata->battery_detect);
+       if (irq <= 0) {
+               dev_warn(&client->dev, "Failed to get gpio as irq: %d\n", irq);
+               gpio_free(pdata->battery_detect);
+               bq20z75_device->gpio_detect = false;
+               goto skip_gpio;
+       }
+
+       rc = request_irq(irq, bq20z75_irq,
+               IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
+               dev_name(&client->dev), &bq20z75_device->power_supply);
+       if (rc) {
+               dev_warn(&client->dev, "Failed to request irq: %d\n", rc);
+               gpio_free(pdata->battery_detect);
+               bq20z75_device->gpio_detect = false;
+               goto skip_gpio;
+       }
+
+       bq20z75_device->irq = irq;
+
+skip_gpio:
+
        rc = power_supply_register(&client->dev, &bq20z75_device->power_supply);
        if (rc) {
                dev_err(&client->dev,
                        "%s: Failed to register power supply\n", __func__);
-               kfree(bq20z75_device);
-               return rc;
+               goto exit_psupply;
        }
 
        dev_info(&client->dev,
                "%s: battery gas gauge device registered\n", client->name);
 
        return 0;
+
+exit_psupply:
+       if (bq20z75_device->irq)
+               free_irq(bq20z75_device->irq, &bq20z75_device->power_supply);
+       if (bq20z75_device->gpio_detect)
+               gpio_free(pdata->battery_detect);
+
+       kfree(bq20z75_device);
+
+       return rc;
 }
 
-static int bq20z75_remove(struct i2c_client *client)
+static int __devexit bq20z75_remove(struct i2c_client *client)
 {
        struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client);
 
+       if (bq20z75_device->irq)
+               free_irq(bq20z75_device->irq, &bq20z75_device->power_supply);
+       if (bq20z75_device->gpio_detect)
+               gpio_free(bq20z75_device->pdata->battery_detect);
+
        power_supply_unregister(&bq20z75_device->power_supply);
        kfree(bq20z75_device);
        bq20z75_device = NULL;
@@ -444,13 +658,14 @@ static int bq20z75_remove(struct i2c_client *client)
 static int bq20z75_suspend(struct i2c_client *client,
        pm_message_t state)
 {
+       struct bq20z75_info *bq20z75_device = i2c_get_clientdata(client);
        s32 ret;
 
        /* write to manufacturer access with sleep command */
        ret = bq20z75_write_word_data(client,
                bq20z75_data[REG_MANUFACTURER_DATA].addr,
                MANUFACTURER_ACCESS_SLEEP);
-       if (ret < 0)
+       if (bq20z75_device->is_present && ret < 0)
                return ret;
 
        return 0;
@@ -465,10 +680,11 @@ static const struct i2c_device_id bq20z75_id[] = {
        { "bq20z75", 0 },
        {}
 };
+MODULE_DEVICE_TABLE(i2c, bq20z75_id);
 
 static struct i2c_driver bq20z75_battery_driver = {
        .probe          = bq20z75_probe,
-       .remove         = bq20z75_remove,
+       .remove         = __devexit_p(bq20z75_remove),
        .suspend        = bq20z75_suspend,
        .resume         = bq20z75_resume,
        .id_table       = bq20z75_id,
index eff0273d4030d71a745f109a25754be52eaa1d8e..59e68dbd028b31e4bf8e17f5e33bc19e0deb7492 100644 (file)
@@ -3,6 +3,7 @@
  *
  * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it>
  * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it>
+ * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de>
  *
  * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc.
  *
  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  *
  */
+
+/*
+ * Datasheets:
+ * http://focus.ti.com/docs/prod/folders/print/bq27000.html
+ * http://focus.ti.com/docs/prod/folders/print/bq27500.html
+ */
+
 #include <linux/module.h>
 #include <linux/param.h>
 #include <linux/jiffies.h>
@@ -27,7 +35,9 @@
 #include <linux/slab.h>
 #include <asm/unaligned.h>
 
-#define DRIVER_VERSION                 "1.1.0"
+#include <linux/power/bq27x00_battery.h>
+
+#define DRIVER_VERSION                 "1.2.0"
 
 #define BQ27x00_REG_TEMP               0x06
 #define BQ27x00_REG_VOLT               0x08
 #define BQ27x00_REG_TTE                        0x16
 #define BQ27x00_REG_TTF                        0x18
 #define BQ27x00_REG_TTECP              0x26
+#define BQ27x00_REG_NAC                        0x0C /* Nominal available capaciy */
+#define BQ27x00_REG_LMD                        0x12 /* Last measured discharge */
+#define BQ27x00_REG_CYCT               0x2A /* Cycle count total */
+#define BQ27x00_REG_AE                 0x22 /* Available enery */
 
 #define BQ27000_REG_RSOC               0x0B /* Relative State-of-Charge */
+#define BQ27000_REG_ILMD               0x76 /* Initial last measured discharge */
 #define BQ27000_FLAG_CHGS              BIT(7)
+#define BQ27000_FLAG_FC                        BIT(5)
 
-#define BQ27500_REG_SOC                        0x2c
+#define BQ27500_REG_SOC                        0x2C
+#define BQ27500_REG_DCAP               0x3C /* Design capacity */
 #define BQ27500_FLAG_DSC               BIT(0)
 #define BQ27500_FLAG_FC                        BIT(9)
 
-/* If the system has several batteries we need a different name for each
- * of them...
- */
-static DEFINE_IDR(battery_id);
-static DEFINE_MUTEX(battery_mutex);
+#define BQ27000_RS                     20 /* Resistor sense */
 
 struct bq27x00_device_info;
 struct bq27x00_access_methods {
-       int (*read)(u8 reg, int *rt_value, int b_single,
-               struct bq27x00_device_info *di);
+       int (*read)(struct bq27x00_device_info *di, u8 reg, bool single);
 };
 
 enum bq27x00_chip { BQ27000, BQ27500 };
 
+struct bq27x00_reg_cache {
+       int temperature;
+       int time_to_empty;
+       int time_to_empty_avg;
+       int time_to_full;
+       int charge_full;
+       int charge_counter;
+       int capacity;
+       int flags;
+
+       int current_now;
+};
+
 struct bq27x00_device_info {
        struct device           *dev;
        int                     id;
-       struct bq27x00_access_methods   *bus;
-       struct power_supply     bat;
        enum bq27x00_chip       chip;
 
-       struct i2c_client       *client;
+       struct bq27x00_reg_cache cache;
+       int charge_design_full;
+
+       unsigned long last_update;
+       struct delayed_work work;
+
+       struct power_supply     bat;
+
+       struct bq27x00_access_methods bus;
+
+       struct mutex lock;
 };
 
 static enum power_supply_property bq27x00_battery_props[] = {
@@ -78,164 +111,328 @@ static enum power_supply_property bq27x00_battery_props[] = {
        POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
        POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG,
        POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
+       POWER_SUPPLY_PROP_TECHNOLOGY,
+       POWER_SUPPLY_PROP_CHARGE_FULL,
+       POWER_SUPPLY_PROP_CHARGE_NOW,
+       POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
+       POWER_SUPPLY_PROP_CHARGE_COUNTER,
+       POWER_SUPPLY_PROP_ENERGY_NOW,
 };
 
+static unsigned int poll_interval = 360;
+module_param(poll_interval, uint, 0644);
+MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \
+                               "0 disables polling");
+
 /*
  * Common code for BQ27x00 devices
  */
 
-static int bq27x00_read(u8 reg, int *rt_value, int b_single,
-                       struct bq27x00_device_info *di)
+static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg,
+               bool single)
 {
-       return di->bus->read(reg, rt_value, b_single, di);
+       return di->bus.read(di, reg, single);
 }
 
 /*
- * Return the battery temperature in tenths of degree Celsius
+ * Return the battery Relative State-of-Charge
  * Or < 0 if something fails.
  */
-static int bq27x00_battery_temperature(struct bq27x00_device_info *di)
+static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di)
 {
-       int ret;
-       int temp = 0;
+       int rsoc;
 
-       ret = bq27x00_read(BQ27x00_REG_TEMP, &temp, 0, di);
-       if (ret) {
-               dev_err(di->dev, "error reading temperature\n");
-               return ret;
+       if (di->chip == BQ27500)
+               rsoc = bq27x00_read(di, BQ27500_REG_SOC, false);
+       else
+               rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true);
+
+       if (rsoc < 0)
+               dev_err(di->dev, "error reading relative State-of-Charge\n");
+
+       return rsoc;
+}
+
+/*
+ * Return a battery charge value in µAh
+ * Or < 0 if something fails.
+ */
+static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg)
+{
+       int charge;
+
+       charge = bq27x00_read(di, reg, false);
+       if (charge < 0) {
+               dev_err(di->dev, "error reading nominal available capacity\n");
+               return charge;
        }
 
        if (di->chip == BQ27500)
-               return temp - 2731;
+               charge *= 1000;
        else
-               return ((temp >> 2) - 273) * 10;
+               charge = charge * 3570 / BQ27000_RS;
+
+       return charge;
 }
 
 /*
- * Return the battery Voltage in milivolts
+ * Return the battery Nominal available capaciy in µAh
  * Or < 0 if something fails.
  */
-static int bq27x00_battery_voltage(struct bq27x00_device_info *di)
+static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di)
 {
-       int ret;
-       int volt = 0;
+       return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC);
+}
 
-       ret = bq27x00_read(BQ27x00_REG_VOLT, &volt, 0, di);
-       if (ret) {
-               dev_err(di->dev, "error reading voltage\n");
-               return ret;
+/*
+ * Return the battery Last measured discharge in µAh
+ * Or < 0 if something fails.
+ */
+static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di)
+{
+       return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD);
+}
+
+/*
+ * Return the battery Initial last measured discharge in µAh
+ * Or < 0 if something fails.
+ */
+static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di)
+{
+       int ilmd;
+
+       if (di->chip == BQ27500)
+               ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false);
+       else
+               ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true);
+
+       if (ilmd < 0) {
+               dev_err(di->dev, "error reading initial last measured discharge\n");
+               return ilmd;
        }
 
-       return volt * 1000;
+       if (di->chip == BQ27500)
+               ilmd *= 1000;
+       else
+               ilmd = ilmd * 256 * 3570 / BQ27000_RS;
+
+       return ilmd;
 }
 
 /*
- * Return the battery average current
- * Note that current can be negative signed as well
- * Or 0 if something fails.
+ * Return the battery Cycle count total
+ * Or < 0 if something fails.
  */
-static int bq27x00_battery_current(struct bq27x00_device_info *di)
+static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di)
 {
-       int ret;
-       int curr = 0;
-       int flags = 0;
+       int cyct;
 
-       ret = bq27x00_read(BQ27x00_REG_AI, &curr, 0, di);
-       if (ret) {
-               dev_err(di->dev, "error reading current\n");
-               return 0;
+       cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false);
+       if (cyct < 0)
+               dev_err(di->dev, "error reading cycle count total\n");
+
+       return cyct;
+}
+
+/*
+ * Read a time register.
+ * Return < 0 if something fails.
+ */
+static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg)
+{
+       int tval;
+
+       tval = bq27x00_read(di, reg, false);
+       if (tval < 0) {
+               dev_err(di->dev, "error reading register %02x: %d\n", reg, tval);
+               return tval;
        }
 
-       if (di->chip == BQ27500) {
-               /* bq27500 returns signed value */
-               curr = (int)(s16)curr;
-       } else {
-               ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
-               if (ret < 0) {
-                       dev_err(di->dev, "error reading flags\n");
-                       return 0;
-               }
-               if (flags & BQ27000_FLAG_CHGS) {
-                       dev_dbg(di->dev, "negative current!\n");
-                       curr = -curr;
-               }
+       if (tval == 65535)
+               return -ENODATA;
+
+       return tval * 60;
+}
+
+static void bq27x00_update(struct bq27x00_device_info *di)
+{
+       struct bq27x00_reg_cache cache = {0, };
+       bool is_bq27500 = di->chip == BQ27500;
+
+       cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500);
+       if (cache.flags >= 0) {
+               cache.capacity = bq27x00_battery_read_rsoc(di);
+               cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false);
+               cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE);
+               cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP);
+               cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF);
+               cache.charge_full = bq27x00_battery_read_lmd(di);
+               cache.charge_counter = bq27x00_battery_read_cyct(di);
+
+               if (!is_bq27500)
+                       cache.current_now = bq27x00_read(di, BQ27x00_REG_AI, false);
+
+               /* We only have to read charge design full once */
+               if (di->charge_design_full <= 0)
+                       di->charge_design_full = bq27x00_battery_read_ilmd(di);
+       }
+
+       /* Ignore current_now which is a snapshot of the current battery state
+        * and is likely to be different even between two consecutive reads */
+       if (memcmp(&di->cache, &cache, sizeof(cache) - sizeof(int)) != 0) {
+               di->cache = cache;
+               power_supply_changed(&di->bat);
        }
 
-       return curr * 1000;
+       di->last_update = jiffies;
+}
+
+static void bq27x00_battery_poll(struct work_struct *work)
+{
+       struct bq27x00_device_info *di =
+               container_of(work, struct bq27x00_device_info, work.work);
+
+       bq27x00_update(di);
+
+       if (poll_interval > 0) {
+               /* The timer does not have to be accurate. */
+               set_timer_slack(&di->work.timer, poll_interval * HZ / 4);
+               schedule_delayed_work(&di->work, poll_interval * HZ);
+       }
 }
 
+
 /*
- * Return the battery Relative State-of-Charge
+ * Return the battery temperature in tenths of degree Celsius
  * Or < 0 if something fails.
  */
-static int bq27x00_battery_rsoc(struct bq27x00_device_info *di)
+static int bq27x00_battery_temperature(struct bq27x00_device_info *di,
+       union power_supply_propval *val)
 {
-       int ret;
-       int rsoc = 0;
+       if (di->cache.temperature < 0)
+               return di->cache.temperature;
 
        if (di->chip == BQ27500)
-               ret = bq27x00_read(BQ27500_REG_SOC, &rsoc, 0, di);
+               val->intval = di->cache.temperature - 2731;
        else
-               ret = bq27x00_read(BQ27000_REG_RSOC, &rsoc, 1, di);
-       if (ret) {
-               dev_err(di->dev, "error reading relative State-of-Charge\n");
-               return ret;
+               val->intval = ((di->cache.temperature * 5) - 5463) / 2;
+
+       return 0;
+}
+
+/*
+ * Return the battery average current in µA
+ * Note that current can be negative signed as well
+ * Or 0 if something fails.
+ */
+static int bq27x00_battery_current(struct bq27x00_device_info *di,
+       union power_supply_propval *val)
+{
+       int curr;
+
+       if (di->chip == BQ27500)
+           curr = bq27x00_read(di, BQ27x00_REG_AI, false);
+       else
+           curr = di->cache.current_now;
+
+       if (curr < 0)
+               return curr;
+
+       if (di->chip == BQ27500) {
+               /* bq27500 returns signed value */
+               val->intval = (int)((s16)curr) * 1000;
+       } else {
+               if (di->cache.flags & BQ27000_FLAG_CHGS) {
+                       dev_dbg(di->dev, "negative current!\n");
+                       curr = -curr;
+               }
+
+               val->intval = curr * 3570 / BQ27000_RS;
        }
 
-       return rsoc;
+       return 0;
 }
 
 static int bq27x00_battery_status(struct bq27x00_device_info *di,
-                                 union power_supply_propval *val)
+       union power_supply_propval *val)
 {
-       int flags = 0;
        int status;
-       int ret;
-
-       ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di);
-       if (ret < 0) {
-               dev_err(di->dev, "error reading flags\n");
-               return ret;
-       }
 
        if (di->chip == BQ27500) {
-               if (flags & BQ27500_FLAG_FC)
+               if (di->cache.flags & BQ27500_FLAG_FC)
                        status = POWER_SUPPLY_STATUS_FULL;
-               else if (flags & BQ27500_FLAG_DSC)
+               else if (di->cache.flags & BQ27500_FLAG_DSC)
                        status = POWER_SUPPLY_STATUS_DISCHARGING;
                else
                        status = POWER_SUPPLY_STATUS_CHARGING;
        } else {
-               if (flags & BQ27000_FLAG_CHGS)
+               if (di->cache.flags & BQ27000_FLAG_FC)
+                       status = POWER_SUPPLY_STATUS_FULL;
+               else if (di->cache.flags & BQ27000_FLAG_CHGS)
                        status = POWER_SUPPLY_STATUS_CHARGING;
+               else if (power_supply_am_i_supplied(&di->bat))
+                       status = POWER_SUPPLY_STATUS_NOT_CHARGING;
                else
                        status = POWER_SUPPLY_STATUS_DISCHARGING;
        }
 
        val->intval = status;
+
        return 0;
 }
 
 /*
- * Read a time register.
- * Return < 0 if something fails.
+ * Return the battery Voltage in milivolts
+ * Or < 0 if something fails.
  */
-static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg,
-                               union power_supply_propval *val)
+static int bq27x00_battery_voltage(struct bq27x00_device_info *di,
+       union power_supply_propval *val)
 {
-       int tval = 0;
-       int ret;
+       int volt;
 
-       ret = bq27x00_read(reg, &tval, 0, di);
-       if (ret) {
-               dev_err(di->dev, "error reading register %02x\n", reg);
-               return ret;
+       volt = bq27x00_read(di, BQ27x00_REG_VOLT, false);
+       if (volt < 0)
+               return volt;
+
+       val->intval = volt * 1000;
+
+       return 0;
+}
+
+/*
+ * Return the battery Available energy in µWh
+ * Or < 0 if something fails.
+ */
+static int bq27x00_battery_energy(struct bq27x00_device_info *di,
+       union power_supply_propval *val)
+{
+       int ae;
+
+       ae = bq27x00_read(di, BQ27x00_REG_AE, false);
+       if (ae < 0) {
+               dev_err(di->dev, "error reading available energy\n");
+               return ae;
        }
 
-       if (tval == 65535)
-               return -ENODATA;
+       if (di->chip == BQ27500)
+               ae *= 1000;
+       else
+               ae = ae * 29200 / BQ27000_RS;
+
+       val->intval = ae;
+
+       return 0;
+}
+
+
+static int bq27x00_simple_value(int value,
+       union power_supply_propval *val)
+{
+       if (value < 0)
+               return value;
+
+       val->intval = value;
 
-       val->intval = tval * 60;
        return 0;
 }
 
@@ -249,33 +446,61 @@ static int bq27x00_battery_get_property(struct power_supply *psy,
        int ret = 0;
        struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
 
+       mutex_lock(&di->lock);
+       if (time_is_before_jiffies(di->last_update + 5 * HZ)) {
+               cancel_delayed_work_sync(&di->work);
+               bq27x00_battery_poll(&di->work.work);
+       }
+       mutex_unlock(&di->lock);
+
+       if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0)
+               return -ENODEV;
+
        switch (psp) {
        case POWER_SUPPLY_PROP_STATUS:
                ret = bq27x00_battery_status(di, val);
                break;
        case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+               ret = bq27x00_battery_voltage(di, val);
+               break;
        case POWER_SUPPLY_PROP_PRESENT:
-               val->intval = bq27x00_battery_voltage(di);
-               if (psp == POWER_SUPPLY_PROP_PRESENT)
-                       val->intval = val->intval <= 0 ? 0 : 1;
+               val->intval = di->cache.flags < 0 ? 0 : 1;
                break;
        case POWER_SUPPLY_PROP_CURRENT_NOW:
-               val->intval = bq27x00_battery_current(di);
+               ret = bq27x00_battery_current(di, val);
                break;
        case POWER_SUPPLY_PROP_CAPACITY:
-               val->intval = bq27x00_battery_rsoc(di);
+               ret = bq27x00_simple_value(di->cache.capacity, val);
                break;
        case POWER_SUPPLY_PROP_TEMP:
-               val->intval = bq27x00_battery_temperature(di);
+               ret = bq27x00_battery_temperature(di, val);
                break;
        case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
-               ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val);
+               ret = bq27x00_simple_value(di->cache.time_to_empty, val);
                break;
        case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG:
-               ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val);
+               ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val);
                break;
        case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
-               ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val);
+               ret = bq27x00_simple_value(di->cache.time_to_full, val);
+               break;
+       case POWER_SUPPLY_PROP_TECHNOLOGY:
+               val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
+               break;
+       case POWER_SUPPLY_PROP_CHARGE_NOW:
+               ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val);
+               break;
+       case POWER_SUPPLY_PROP_CHARGE_FULL:
+               ret = bq27x00_simple_value(di->cache.charge_full, val);
+               break;
+       case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
+               ret = bq27x00_simple_value(di->charge_design_full, val);
+               break;
+       case POWER_SUPPLY_PROP_CHARGE_COUNTER:
+               ret = bq27x00_simple_value(di->cache.charge_counter, val);
+               break;
+       case POWER_SUPPLY_PROP_ENERGY_NOW:
+               ret = bq27x00_battery_energy(di, val);
                break;
        default:
                return -EINVAL;
@@ -284,56 +509,91 @@ static int bq27x00_battery_get_property(struct power_supply *psy,
        return ret;
 }
 
-static void bq27x00_powersupply_init(struct bq27x00_device_info *di)
+static void bq27x00_external_power_changed(struct power_supply *psy)
 {
+       struct bq27x00_device_info *di = to_bq27x00_device_info(psy);
+
+       cancel_delayed_work_sync(&di->work);
+       schedule_delayed_work(&di->work, 0);
+}
+
+static int bq27x00_powersupply_init(struct bq27x00_device_info *di)
+{
+       int ret;
+
        di->bat.type = POWER_SUPPLY_TYPE_BATTERY;
        di->bat.properties = bq27x00_battery_props;
        di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props);
        di->bat.get_property = bq27x00_battery_get_property;
-       di->bat.external_power_changed = NULL;
+       di->bat.external_power_changed = bq27x00_external_power_changed;
+
+       INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll);
+       mutex_init(&di->lock);
+
+       ret = power_supply_register(di->dev, &di->bat);
+       if (ret) {
+               dev_err(di->dev, "failed to register battery: %d\n", ret);
+               return ret;
+       }
+
+       dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION);
+
+       bq27x00_update(di);
+
+       return 0;
 }
 
-/*
- * i2c specific code
+static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di)
+{
+       cancel_delayed_work_sync(&di->work);
+
+       power_supply_unregister(&di->bat);
+
+       mutex_destroy(&di->lock);
+}
+
+
+/* i2c specific code */
+#ifdef CONFIG_BATTERY_BQ27X00_I2C
+
+/* If the system has several batteries we need a different name for each
+ * of them...
  */
+static DEFINE_IDR(battery_id);
+static DEFINE_MUTEX(battery_mutex);
 
-static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single,
-                       struct bq27x00_device_info *di)
+static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single)
 {
-       struct i2c_client *client = di->client;
-       struct i2c_msg msg[1];
+       struct i2c_client *client = to_i2c_client(di->dev);
+       struct i2c_msg msg[2];
        unsigned char data[2];
-       int err;
+       int ret;
 
        if (!client->adapter)
                return -ENODEV;
 
-       msg->addr = client->addr;
-       msg->flags = 0;
-       msg->len = 1;
-       msg->buf = data;
-
-       data[0] = reg;
-       err = i2c_transfer(client->adapter, msg, 1);
+       msg[0].addr = client->addr;
+       msg[0].flags = 0;
+       msg[0].buf = &reg;
+       msg[0].len = sizeof(reg);
+       msg[1].addr = client->addr;
+       msg[1].flags = I2C_M_RD;
+       msg[1].buf = data;
+       if (single)
+               msg[1].len = 1;
+       else
+               msg[1].len = 2;
 
-       if (err >= 0) {
-               if (!b_single)
-                       msg->len = 2;
-               else
-                       msg->len = 1;
+       ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
+       if (ret < 0)
+               return ret;
 
-               msg->flags = I2C_M_RD;
-               err = i2c_transfer(client->adapter, msg, 1);
-               if (err >= 0) {
-                       if (!b_single)
-                               *rt_value = get_unaligned_le16(data);
-                       else
-                               *rt_value = data[0];
+       if (!single)
+               ret = get_unaligned_le16(data);
+       else
+               ret = data[0];
 
-                       return 0;
-               }
-       }
-       return err;
+       return ret;
 }
 
 static int bq27x00_battery_probe(struct i2c_client *client,
@@ -341,7 +601,6 @@ static int bq27x00_battery_probe(struct i2c_client *client,
 {
        char *name;
        struct bq27x00_device_info *di;
-       struct bq27x00_access_methods *bus;
        int num;
        int retval = 0;
 
@@ -368,38 +627,20 @@ static int bq27x00_battery_probe(struct i2c_client *client,
                retval = -ENOMEM;
                goto batt_failed_2;
        }
+
        di->id = num;
+       di->dev = &client->dev;
        di->chip = id->driver_data;
+       di->bat.name = name;
+       di->bus.read = &bq27x00_read_i2c;
 
-       bus = kzalloc(sizeof(*bus), GFP_KERNEL);
-       if (!bus) {
-               dev_err(&client->dev, "failed to allocate access method "
-                                       "data\n");
-               retval = -ENOMEM;
+       if (bq27x00_powersupply_init(di))
                goto batt_failed_3;
-       }
 
        i2c_set_clientdata(client, di);
-       di->dev = &client->dev;
-       di->bat.name = name;
-       bus->read = &bq27x00_read_i2c;
-       di->bus = bus;
-       di->client = client;
-
-       bq27x00_powersupply_init(di);
-
-       retval = power_supply_register(&client->dev, &di->bat);
-       if (retval) {
-               dev_err(&client->dev, "failed to register battery\n");
-               goto batt_failed_4;
-       }
-
-       dev_info(&client->dev, "support ver. %s enabled\n", DRIVER_VERSION);
 
        return 0;
 
-batt_failed_4:
-       kfree(bus);
 batt_failed_3:
        kfree(di);
 batt_failed_2:
@@ -416,9 +657,8 @@ static int bq27x00_battery_remove(struct i2c_client *client)
 {
        struct bq27x00_device_info *di = i2c_get_clientdata(client);
 
-       power_supply_unregister(&di->bat);
+       bq27x00_powersupply_unregister(di);
 
-       kfree(di->bus);
        kfree(di->bat.name);
 
        mutex_lock(&battery_mutex);
@@ -430,15 +670,12 @@ static int bq27x00_battery_remove(struct i2c_client *client)
        return 0;
 }
 
-/*
- * Module stuff
- */
-
 static const struct i2c_device_id bq27x00_id[] = {
        { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */
        { "bq27500", BQ27500 },
        {},
 };
+MODULE_DEVICE_TABLE(i2c, bq27x00_id);
 
 static struct i2c_driver bq27x00_battery_driver = {
        .driver = {
@@ -449,13 +686,164 @@ static struct i2c_driver bq27x00_battery_driver = {
        .id_table = bq27x00_id,
 };
 
+static inline int bq27x00_battery_i2c_init(void)
+{
+       int ret = i2c_add_driver(&bq27x00_battery_driver);
+       if (ret)
+               printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n");
+
+       return ret;
+}
+
+static inline void bq27x00_battery_i2c_exit(void)
+{
+       i2c_del_driver(&bq27x00_battery_driver);
+}
+
+#else
+
+static inline int bq27x00_battery_i2c_init(void) { return 0; }
+static inline void bq27x00_battery_i2c_exit(void) {};
+
+#endif
+
+/* platform specific code */
+#ifdef CONFIG_BATTERY_BQ27X00_PLATFORM
+
+static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg,
+                       bool single)
+{
+       struct device *dev = di->dev;
+       struct bq27000_platform_data *pdata = dev->platform_data;
+       unsigned int timeout = 3;
+       int upper, lower;
+       int temp;
+
+       if (!single) {
+               /* Make sure the value has not changed in between reading the
+                * lower and the upper part */
+               upper = pdata->read(dev, reg + 1);
+               do {
+                       temp = upper;
+                       if (upper < 0)
+                               return upper;
+
+                       lower = pdata->read(dev, reg);
+                       if (lower < 0)
+                               return lower;
+
+                       upper = pdata->read(dev, reg + 1);
+               } while (temp != upper && --timeout);
+
+               if (timeout == 0)
+                       return -EIO;
+
+               return (upper << 8) | lower;
+       }
+
+       return pdata->read(dev, reg);
+}
+
+static int __devinit bq27000_battery_probe(struct platform_device *pdev)
+{
+       struct bq27x00_device_info *di;
+       struct bq27000_platform_data *pdata = pdev->dev.platform_data;
+       int ret;
+
+       if (!pdata) {
+               dev_err(&pdev->dev, "no platform_data supplied\n");
+               return -EINVAL;
+       }
+
+       if (!pdata->read) {
+               dev_err(&pdev->dev, "no hdq read callback supplied\n");
+               return -EINVAL;
+       }
+
+       di = kzalloc(sizeof(*di), GFP_KERNEL);
+       if (!di) {
+               dev_err(&pdev->dev, "failed to allocate device info data\n");
+               return -ENOMEM;
+       }
+
+       platform_set_drvdata(pdev, di);
+
+       di->dev = &pdev->dev;
+       di->chip = BQ27000;
+
+       di->bat.name = pdata->name ?: dev_name(&pdev->dev);
+       di->bus.read = &bq27000_read_platform;
+
+       ret = bq27x00_powersupply_init(di);
+       if (ret)
+               goto err_free;
+
+       return 0;
+
+err_free:
+       platform_set_drvdata(pdev, NULL);
+       kfree(di);
+
+       return ret;
+}
+
+static int __devexit bq27000_battery_remove(struct platform_device *pdev)
+{
+       struct bq27x00_device_info *di = platform_get_drvdata(pdev);
+
+       bq27x00_powersupply_unregister(di);
+
+       platform_set_drvdata(pdev, NULL);
+       kfree(di);
+
+       return 0;
+}
+
+static struct platform_driver bq27000_battery_driver = {
+       .probe  = bq27000_battery_probe,
+       .remove = __devexit_p(bq27000_battery_remove),
+       .driver = {
+               .name = "bq27000-battery",
+               .owner = THIS_MODULE,
+       },
+};
+
+static inline int bq27x00_battery_platform_init(void)
+{
+       int ret = platform_driver_register(&bq27000_battery_driver);
+       if (ret)
+               printk(KERN_ERR "Unable to register BQ27000 platform driver\n");
+
+       return ret;
+}
+
+static inline void bq27x00_battery_platform_exit(void)
+{
+       platform_driver_unregister(&bq27000_battery_driver);
+}
+
+#else
+
+static inline int bq27x00_battery_platform_init(void) { return 0; }
+static inline void bq27x00_battery_platform_exit(void) {};
+
+#endif
+
+/*
+ * Module stuff
+ */
+
 static int __init bq27x00_battery_init(void)
 {
        int ret;
 
-       ret = i2c_add_driver(&bq27x00_battery_driver);
+       ret = bq27x00_battery_i2c_init();
        if (ret)
-               printk(KERN_ERR "Unable to register BQ27x00 driver\n");
+               return ret;
+
+       ret = bq27x00_battery_platform_init();
+       if (ret)
+               bq27x00_battery_i2c_exit();
 
        return ret;
 }
@@ -463,7 +851,8 @@ module_init(bq27x00_battery_init);
 
 static void __exit bq27x00_battery_exit(void)
 {
-       i2c_del_driver(&bq27x00_battery_driver);
+       bq27x00_battery_platform_exit();
+       bq27x00_battery_i2c_exit();
 }
 module_exit(bq27x00_battery_exit);
 
index 6957e8af64490d006d383b7ce253bd7faddca0f4..4d2dc4fa2888fe2953b4b7c10a1aa53648b1f298 100644 (file)
@@ -393,6 +393,7 @@ static const struct i2c_device_id ds278x_id[] = {
        {"ds2786", DS2786},
        {},
 };
+MODULE_DEVICE_TABLE(i2c, ds278x_id);
 
 static struct i2c_driver ds278x_battery_driver = {
        .driver         = {
index 970f7335d3a7f67bade76d6fad08eb954427a949..329b46b2327d4110eaea4438f87f2570f2587e88 100644 (file)
@@ -171,6 +171,8 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
        dev_set_drvdata(dev, psy);
        psy->dev = dev;
 
+       INIT_WORK(&psy->changed_work, power_supply_changed_work);
+
        rc = kobject_set_name(&dev->kobj, "%s", psy->name);
        if (rc)
                goto kobject_set_name_failed;
@@ -179,8 +181,6 @@ int power_supply_register(struct device *parent, struct power_supply *psy)
        if (rc)
                goto device_add_failed;
 
-       INIT_WORK(&psy->changed_work, power_supply_changed_work);
-
        rc = power_supply_create_triggers(psy);
        if (rc)
                goto create_triggers_failed;
index 031a554837f78d2958b2c3368268c27466251837..da25eb94e5c6487907fc4fecb175b73059a8677a 100644 (file)
@@ -21,6 +21,8 @@
 static void power_supply_update_bat_leds(struct power_supply *psy)
 {
        union power_supply_propval status;
+       unsigned long delay_on = 0;
+       unsigned long delay_off = 0;
 
        if (psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status))
                return;
@@ -32,16 +34,22 @@ static void power_supply_update_bat_leds(struct power_supply *psy)
                led_trigger_event(psy->charging_full_trig, LED_FULL);
                led_trigger_event(psy->charging_trig, LED_OFF);
                led_trigger_event(psy->full_trig, LED_FULL);
+               led_trigger_event(psy->charging_blink_full_solid_trig,
+                       LED_FULL);
                break;
        case POWER_SUPPLY_STATUS_CHARGING:
                led_trigger_event(psy->charging_full_trig, LED_FULL);
                led_trigger_event(psy->charging_trig, LED_FULL);
                led_trigger_event(psy->full_trig, LED_OFF);
+               led_trigger_blink(psy->charging_blink_full_solid_trig,
+                       &delay_on, &delay_off);
                break;
        default:
                led_trigger_event(psy->charging_full_trig, LED_OFF);
                led_trigger_event(psy->charging_trig, LED_OFF);
                led_trigger_event(psy->full_trig, LED_OFF);
+               led_trigger_event(psy->charging_blink_full_solid_trig,
+                       LED_OFF);
                break;
        }
 }
@@ -64,15 +72,24 @@ static int power_supply_create_bat_triggers(struct power_supply *psy)
        if (!psy->full_trig_name)
                goto full_failed;
 
+       psy->charging_blink_full_solid_trig_name = kasprintf(GFP_KERNEL,
+               "%s-charging-blink-full-solid", psy->name);
+       if (!psy->charging_blink_full_solid_trig_name)
+               goto charging_blink_full_solid_failed;
+
        led_trigger_register_simple(psy->charging_full_trig_name,
                                    &psy->charging_full_trig);
        led_trigger_register_simple(psy->charging_trig_name,
                                    &psy->charging_trig);
        led_trigger_register_simple(psy->full_trig_name,
                                    &psy->full_trig);
+       led_trigger_register_simple(psy->charging_blink_full_solid_trig_name,
+                                   &psy->charging_blink_full_solid_trig);
 
        goto success;
 
+charging_blink_full_solid_failed:
+       kfree(psy->full_trig_name);
 full_failed:
        kfree(psy->charging_trig_name);
 charging_failed:
@@ -88,6 +105,8 @@ static void power_supply_remove_bat_triggers(struct power_supply *psy)
        led_trigger_unregister_simple(psy->charging_full_trig);
        led_trigger_unregister_simple(psy->charging_trig);
        led_trigger_unregister_simple(psy->full_trig);
+       led_trigger_unregister_simple(psy->charging_blink_full_solid_trig);
+       kfree(psy->charging_blink_full_solid_trig_name);
        kfree(psy->full_trig_name);
        kfree(psy->charging_trig_name);
        kfree(psy->charging_full_trig_name);
index cd1f90754a3a301ebdbdd71da8bac4ad8265a580..605514afc29f20029032ef2ab94cc1b006e8ffa3 100644 (file)
@@ -270,7 +270,7 @@ int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
                attr = &power_supply_attrs[psy->properties[j]];
 
                ret = power_supply_show_property(dev, attr, prop_buf);
-               if (ret == -ENODEV) {
+               if (ret == -ENODEV || ret == -ENODATA) {
                        /* When a battery is absent, we expect -ENODEV. Don't abort;
                           send the uevent with at least the the PRESENT=0 property */
                        ret = 0;
index 4255f2358b138beb96305e832d778bef9ab15285..d36c289aaef52e1246029dd97f07f8f9d061f2f8 100644 (file)
@@ -406,8 +406,8 @@ static int s3c_adc_bat_resume(struct platform_device *pdev)
        return 0;
 }
 #else
-#define s3c_adc_battery_suspend NULL
-#define s3c_adc_battery_resume NULL
+#define s3c_adc_bat_suspend NULL
+#define s3c_adc_bat_resume NULL
 #endif
 
 static struct platform_driver s3c_adc_bat_driver = {
index ff1f42398a2edf42509152deae3cb721079ba221..92c16e1677bd7db5d796a34d64e5bd3b29428c7b 100644 (file)
@@ -71,8 +71,11 @@ struct twl4030_bci {
        struct power_supply     usb;
        struct otg_transceiver  *transceiver;
        struct notifier_block   otg_nb;
+       struct work_struct      work;
        int                     irq_chg;
        int                     irq_bci;
+
+       unsigned long           event;
 };
 
 /*
@@ -258,14 +261,11 @@ static irqreturn_t twl4030_bci_interrupt(int irq, void *arg)
        return IRQ_HANDLED;
 }
 
-static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val,
-                              void *priv)
+static void twl4030_bci_usb_work(struct work_struct *data)
 {
-       struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, otg_nb);
+       struct twl4030_bci *bci = container_of(data, struct twl4030_bci, work);
 
-       dev_dbg(bci->dev, "OTG notify %lu\n", val);
-
-       switch (val) {
+       switch (bci->event) {
        case USB_EVENT_VBUS:
        case USB_EVENT_CHARGER:
                twl4030_charger_enable_usb(bci, true);
@@ -274,6 +274,17 @@ static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val,
                twl4030_charger_enable_usb(bci, false);
                break;
        }
+}
+
+static int twl4030_bci_usb_ncb(struct notifier_block *nb, unsigned long val,
+                              void *priv)
+{
+       struct twl4030_bci *bci = container_of(nb, struct twl4030_bci, otg_nb);
+
+       dev_dbg(bci->dev, "OTG notify %lu\n", val);
+
+       bci->event = val;
+       schedule_work(&bci->work);
 
        return NOTIFY_OK;
 }
@@ -466,6 +477,8 @@ static int __init twl4030_bci_probe(struct platform_device *pdev)
                goto fail_bci_irq;
        }
 
+       INIT_WORK(&bci->work, twl4030_bci_usb_work);
+
        bci->transceiver = otg_get_transceiver();
        if (bci->transceiver != NULL) {
                bci->otg_nb.notifier_call = twl4030_bci_usb_ncb;
index e5ed52d719376e41c35de6c1368c8723af532f89..2a9ab89f83b8af99264495663516694fc2afee25 100644 (file)
@@ -134,6 +134,8 @@ static int z2_batt_ps_init(struct z2_charger *charger, int props)
        enum power_supply_property *prop;
        struct z2_battery_info *info = charger->info;
 
+       if (info->charge_gpio >= 0)
+               props++;        /* POWER_SUPPLY_PROP_STATUS */
        if (info->batt_tech >= 0)
                props++;        /* POWER_SUPPLY_PROP_TECHNOLOGY */
        if (info->batt_I2C_reg >= 0)
@@ -293,6 +295,7 @@ static const struct i2c_device_id z2_batt_id[] = {
        { "aer915", 0 },
        { }
 };
+MODULE_DEVICE_TABLE(i2c, z2_batt_id);
 
 static struct i2c_driver z2_batt_driver = {
        .driver = {
index 383811d9af83fdfd45210e83c326cf1abd25b026..61e0340a4b770c3675787130afdc8174eb902262 100644 (file)
@@ -145,6 +145,9 @@ extern void led_trigger_register_simple(const char *name,
 extern void led_trigger_unregister_simple(struct led_trigger *trigger);
 extern void led_trigger_event(struct led_trigger *trigger,
                                enum led_brightness event);
+extern void led_trigger_blink(struct led_trigger *trigger,
+                             unsigned long *delay_on,
+                             unsigned long *delay_off);
 
 #else
 
diff --git a/include/linux/power/bq20z75.h b/include/linux/power/bq20z75.h
new file mode 100644 (file)
index 0000000..b0843b6
--- /dev/null
@@ -0,0 +1,39 @@
+/*
+ * Gas Gauge driver for TI's BQ20Z75
+ *
+ * Copyright (c) 2010, NVIDIA Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#ifndef __LINUX_POWER_BQ20Z75_H_
+#define __LINUX_POWER_BQ20Z75_H_
+
+#include <linux/power_supply.h>
+#include <linux/types.h>
+
+/**
+ * struct bq20z75_platform_data - platform data for bq20z75 devices
+ * @battery_detect:            GPIO which is used to detect battery presence
+ * @battery_detect_present:    gpio state when battery is present (0 / 1)
+ * @i2c_retry_count:           # of times to retry on i2c IO failure
+ */
+struct bq20z75_platform_data {
+       int battery_detect;
+       int battery_detect_present;
+       int i2c_retry_count;
+};
+
+#endif
diff --git a/include/linux/power/bq27x00_battery.h b/include/linux/power/bq27x00_battery.h
new file mode 100644 (file)
index 0000000..a857f71
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef __LINUX_BQ27X00_BATTERY_H__
+#define __LINUX_BQ27X00_BATTERY_H__
+
+/**
+ * struct bq27000_plaform_data - Platform data for bq27000 devices
+ * @name: Name of the battery. If NULL the driver will fallback to "bq27000".
+ * @read: HDQ read callback.
+ *     This function should provide access to the HDQ bus the battery is
+ *     connected to.
+ *     The first parameter is a pointer to the battery device, the second the
+ *     register to be read. The return value should either be the content of
+ *     the passed register or an error value.
+ */
+struct bq27000_platform_data {
+       const char *name;
+       int (*read)(struct device *dev, unsigned int);
+};
+
+#endif
index 7d7325685c421a8b7da8746ea47583396bb61b8c..204c18dfdc9e839a378cfd4200342784c22be5b7 100644 (file)
@@ -173,6 +173,8 @@ struct power_supply {
        char *full_trig_name;
        struct led_trigger *online_trig;
        char *online_trig_name;
+       struct led_trigger *charging_blink_full_solid_trig;
+       char *charging_blink_full_solid_trig_name;
 #endif
 };
 
@@ -213,4 +215,49 @@ extern void power_supply_unregister(struct power_supply *psy);
 /* For APM emulation, think legacy userspace. */
 extern struct class *power_supply_class;
 
+static inline bool power_supply_is_amp_property(enum power_supply_property psp)
+{
+       switch (psp) {
+       case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
+       case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
+       case POWER_SUPPLY_PROP_CHARGE_FULL:
+       case POWER_SUPPLY_PROP_CHARGE_EMPTY:
+       case POWER_SUPPLY_PROP_CHARGE_NOW:
+       case POWER_SUPPLY_PROP_CHARGE_AVG:
+       case POWER_SUPPLY_PROP_CHARGE_COUNTER:
+       case POWER_SUPPLY_PROP_CURRENT_MAX:
+       case POWER_SUPPLY_PROP_CURRENT_NOW:
+       case POWER_SUPPLY_PROP_CURRENT_AVG:
+               return 1;
+       default:
+               break;
+       }
+
+       return 0;
+}
+
+static inline bool power_supply_is_watt_property(enum power_supply_property psp)
+{
+       switch (psp) {
+       case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
+       case POWER_SUPPLY_PROP_ENERGY_EMPTY_DESIGN:
+       case POWER_SUPPLY_PROP_ENERGY_FULL:
+       case POWER_SUPPLY_PROP_ENERGY_EMPTY:
+       case POWER_SUPPLY_PROP_ENERGY_NOW:
+       case POWER_SUPPLY_PROP_ENERGY_AVG:
+       case POWER_SUPPLY_PROP_VOLTAGE_MAX:
+       case POWER_SUPPLY_PROP_VOLTAGE_MIN:
+       case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+       case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+       case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+       case POWER_SUPPLY_PROP_VOLTAGE_AVG:
+       case POWER_SUPPLY_PROP_POWER_NOW:
+               return 1;
+       default:
+               break;
+       }
+
+       return 0;
+}
+
 #endif /* __LINUX_POWER_SUPPLY_H__ */