From f8d94eb77e4ef6d80c6b7e5e02b92180d9a6f37b Mon Sep 17 00:00:00 2001 From: Mark Brown Date: Wed, 27 Jan 2010 20:45:35 +0000 Subject: wm97xx_battery: Clean up some warnings Staticise work_lock (nothing outside the driver has any reason to see it) and specify dev when requesting the charger IRQ (since that's what we pass in when we free it). Signed-off-by: Mark Brown Acked-by: Marek Vasut Signed-off-by: Anton Vorontsov diff --git a/drivers/power/wm97xx_battery.c b/drivers/power/wm97xx_battery.c index 6ea3cb5..23eed35 100644 --- a/drivers/power/wm97xx_battery.c +++ b/drivers/power/wm97xx_battery.c @@ -26,7 +26,7 @@ static DEFINE_MUTEX(bat_lock); static struct work_struct bat_work; -struct mutex work_lock; +static struct mutex work_lock; static int bat_status = POWER_SUPPLY_STATUS_UNKNOWN; static struct wm97xx_batt_info *gpdata; static enum power_supply_property *prop; @@ -203,7 +203,7 @@ static int __devinit wm97xx_bat_probe(struct platform_device *dev) goto err2; ret = request_irq(gpio_to_irq(pdata->charge_gpio), wm97xx_chrg_irq, IRQF_DISABLED, - "AC Detect", 0); + "AC Detect", dev); if (ret) goto err2; props++; /* POWER_SUPPLY_PROP_STATUS */ -- cgit v0.10.2 From 5324dc0e3872324ed0bf372bce7bc437436910b6 Mon Sep 17 00:00:00 2001 From: Stefan Weil Date: Tue, 2 Feb 2010 14:45:04 -0800 Subject: da9030_battery: fix spelling in comment platfrom -> platform monotor -> monitor Signed-off-by: Stefan Weil Cc: Anton Vorontsov Acked-by: Mike Rapoport Signed-off-by: Andrew Morton Signed-off-by: Anton Vorontsov diff --git a/drivers/power/da9030_battery.c b/drivers/power/da9030_battery.c index 3364198..a2e71f7 100644 --- a/drivers/power/da9030_battery.c +++ b/drivers/power/da9030_battery.c @@ -509,7 +509,7 @@ static int da9030_battery_probe(struct platform_device *pdev) charger->master = pdev->dev.parent; - /* 10 seconds between monotor runs unless platfrom defines other + /* 10 seconds between monitor runs unless platform defines other interval */ charger->interval = msecs_to_jiffies( (pdata->batmon_interval ? : 10) * 1000); -- cgit v0.10.2 From 97f70c23a1fa1bffe79187563dc5c6f2c72420a2 Mon Sep 17 00:00:00 2001 From: Grazvydas Ignotas Date: Fri, 12 Feb 2010 23:56:46 +0200 Subject: power_supply: bq27x00: remove double endian swap The bq27x00 registers are little endian, so just read them as such and don't do double endian swap on LE machines. This also gets rid of strange looking shift that was done after reading 8bit register because endian swap in bq27x00_read() was messing it up. Signed-off-by: Grazvydas Ignotas Acked-by: Rodolfo Giometti Signed-off-by: Anton Vorontsov diff --git a/drivers/power/bq27x00_battery.c b/drivers/power/bq27x00_battery.c index 62bb981..537915c 100644 --- a/drivers/power/bq27x00_battery.c +++ b/drivers/power/bq27x00_battery.c @@ -74,12 +74,7 @@ static enum power_supply_property bq27x00_battery_props[] = { static int bq27x00_read(u8 reg, int *rt_value, int b_single, struct bq27x00_device_info *di) { - int ret; - - ret = di->bus->read(reg, rt_value, b_single, di); - *rt_value = be16_to_cpu(*rt_value); - - return ret; + return di->bus->read(reg, rt_value, b_single, di); } /* @@ -161,7 +156,7 @@ static int bq27x00_battery_rsoc(struct bq27x00_device_info *di) return ret; } - return rsoc >> 8; + return rsoc; } #define to_bq27x00_device_info(x) container_of((x), \ @@ -238,7 +233,7 @@ static int bq27200_read(u8 reg, int *rt_value, int b_single, err = i2c_transfer(client->adapter, msg, 1); if (err >= 0) { if (!b_single) - *rt_value = get_unaligned_be16(data); + *rt_value = get_unaligned_le16(data); else *rt_value = data[0]; -- cgit v0.10.2 From ba96f8717e77f1ef68c160f6e9a454b3ce2c045b Mon Sep 17 00:00:00 2001 From: Grazvydas Ignotas Date: Fri, 12 Feb 2010 23:57:02 +0200 Subject: power_supply: bq27x00: remove unused struct fields These are probably leftover from old TI code. Signed-off-by: Grazvydas Ignotas Acked-by: Rodolfo Giometti Signed-off-by: Anton Vorontsov diff --git a/drivers/power/bq27x00_battery.c b/drivers/power/bq27x00_battery.c index 537915c..3ae3e08 100644 --- a/drivers/power/bq27x00_battery.c +++ b/drivers/power/bq27x00_battery.c @@ -49,10 +49,6 @@ struct bq27x00_access_methods { struct bq27x00_device_info { struct device *dev; int id; - int voltage_uV; - int current_uA; - int temp_C; - int charge_rsoc; struct bq27x00_access_methods *bus; struct power_supply bat; -- cgit v0.10.2 From b4de3608156311b615da4bcc4c095913abc44825 Mon Sep 17 00:00:00 2001 From: Grazvydas Ignotas Date: Fri, 12 Feb 2010 23:57:13 +0200 Subject: power_supply: bq27x00: fix temperature conversion The power supply class requires tenths of degree Celsius. Signed-off-by: Grazvydas Ignotas Acked-by: Rodolfo Giometti Signed-off-by: Anton Vorontsov diff --git a/drivers/power/bq27x00_battery.c b/drivers/power/bq27x00_battery.c index 3ae3e08..3da9e0a 100644 --- a/drivers/power/bq27x00_battery.c +++ b/drivers/power/bq27x00_battery.c @@ -74,7 +74,7 @@ static int bq27x00_read(u8 reg, int *rt_value, int b_single, } /* - * Return the battery temperature in Celsius degrees + * Return the battery temperature in tenths of degree Celsius * Or < 0 if something fails. */ static int bq27x00_battery_temperature(struct bq27x00_device_info *di) @@ -88,7 +88,7 @@ static int bq27x00_battery_temperature(struct bq27x00_device_info *di) return ret; } - return (temp >> 2) - 273; + return ((temp >> 2) - 273) * 10; } /* -- cgit v0.10.2 From e20908d95e89d04b9537aefecc0734d2e4f7faed Mon Sep 17 00:00:00 2001 From: Grazvydas Ignotas Date: Fri, 12 Feb 2010 23:57:23 +0200 Subject: power_supply: bq27x00: add BQ27500 support BQ27500 is newer fuel gauge chip from TI with slightly changed register layout and some different register semantics. Add new i2c ID for it and handle differences where needed. Also rename bq27200 functions to bq27x00, as they are no longer bq27200 specific. Signed-off-by: Grazvydas Ignotas Acked-by: Rodolfo Giometti Signed-off-by: Anton Vorontsov diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig index d4b3d67..bf14672 100644 --- a/drivers/power/Kconfig +++ b/drivers/power/Kconfig @@ -98,10 +98,10 @@ config BATTERY_WM97XX Say Y to enable support for battery measured by WM97xx aux port. config BATTERY_BQ27x00 - tristate "BQ27200 battery driver" + tristate "BQ27x00 battery driver" depends on I2C help - Say Y here to enable support for batteries with BQ27200(I2C) chip. + Say Y here to enable support for batteries with BQ27x00 (I2C) chips. config BATTERY_DA9030 tristate "DA9030 battery driver" diff --git a/drivers/power/bq27x00_battery.c b/drivers/power/bq27x00_battery.c index 3da9e0a..1526d02 100644 --- a/drivers/power/bq27x00_battery.c +++ b/drivers/power/bq27x00_battery.c @@ -26,14 +26,17 @@ #include #include -#define DRIVER_VERSION "1.0.0" +#define DRIVER_VERSION "1.1.0" #define BQ27x00_REG_TEMP 0x06 #define BQ27x00_REG_VOLT 0x08 -#define BQ27x00_REG_RSOC 0x0B /* Relative State-of-Charge */ #define BQ27x00_REG_AI 0x14 #define BQ27x00_REG_FLAGS 0x0A +#define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */ + +#define BQ27500_REG_SOC 0x2c + /* If the system has several batteries we need a different name for each * of them... */ @@ -46,11 +49,14 @@ struct bq27x00_access_methods { struct bq27x00_device_info *di); }; +enum bq27x00_chip { BQ27000, BQ27500 }; + 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; }; @@ -88,7 +94,10 @@ static int bq27x00_battery_temperature(struct bq27x00_device_info *di) return ret; } - return ((temp >> 2) - 273) * 10; + if (di->chip == BQ27500) + return temp - 2731; + else + return ((temp >> 2) - 273) * 10; } /* @@ -125,15 +134,22 @@ static int bq27x00_battery_current(struct bq27x00_device_info *di) dev_err(di->dev, "error reading current\n"); return 0; } - ret = bq27x00_read(BQ27x00_REG_FLAGS, &flags, 0, di); - if (ret < 0) { - dev_err(di->dev, "error reading flags\n"); - return 0; - } - if ((flags & (1 << 7)) != 0) { - dev_dbg(di->dev, "negative current!\n"); - return -curr; + + 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 & (1 << 7)) != 0) { + dev_dbg(di->dev, "negative current!\n"); + return -curr; + } } + return curr; } @@ -146,7 +162,10 @@ static int bq27x00_battery_rsoc(struct bq27x00_device_info *di) int ret; int rsoc = 0; - ret = bq27x00_read(BQ27x00_REG_RSOC, &rsoc, 1, di); + if (di->chip == BQ27500) + ret = bq27x00_read(BQ27500_REG_SOC, &rsoc, 0, di); + 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; @@ -197,10 +216,10 @@ static void bq27x00_powersupply_init(struct bq27x00_device_info *di) } /* - * BQ27200 specific code + * i2c specific code */ -static int bq27200_read(u8 reg, int *rt_value, int b_single, +static int bq27x00_read_i2c(u8 reg, int *rt_value, int b_single, struct bq27x00_device_info *di) { struct i2c_client *client = di->client; @@ -239,7 +258,7 @@ static int bq27200_read(u8 reg, int *rt_value, int b_single, return err; } -static int bq27200_battery_probe(struct i2c_client *client, +static int bq27x00_battery_probe(struct i2c_client *client, const struct i2c_device_id *id) { char *name; @@ -258,7 +277,7 @@ static int bq27200_battery_probe(struct i2c_client *client, if (retval < 0) return retval; - name = kasprintf(GFP_KERNEL, "bq27200-%d", num); + name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num); if (!name) { dev_err(&client->dev, "failed to allocate device name\n"); retval = -ENOMEM; @@ -272,6 +291,7 @@ static int bq27200_battery_probe(struct i2c_client *client, goto batt_failed_2; } di->id = num; + di->chip = id->driver_data; bus = kzalloc(sizeof(*bus), GFP_KERNEL); if (!bus) { @@ -284,7 +304,7 @@ static int bq27200_battery_probe(struct i2c_client *client, i2c_set_clientdata(client, di); di->dev = &client->dev; di->bat.name = name; - bus->read = &bq27200_read; + bus->read = &bq27x00_read_i2c; di->bus = bus; di->client = client; @@ -314,7 +334,7 @@ batt_failed_1: return retval; } -static int bq27200_battery_remove(struct i2c_client *client) +static int bq27x00_battery_remove(struct i2c_client *client) { struct bq27x00_device_info *di = i2c_get_clientdata(client); @@ -335,27 +355,28 @@ static int bq27200_battery_remove(struct i2c_client *client) * Module stuff */ -static const struct i2c_device_id bq27200_id[] = { - { "bq27200", 0 }, +static const struct i2c_device_id bq27x00_id[] = { + { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */ + { "bq27500", BQ27500 }, {}, }; -static struct i2c_driver bq27200_battery_driver = { +static struct i2c_driver bq27x00_battery_driver = { .driver = { - .name = "bq27200-battery", + .name = "bq27x00-battery", }, - .probe = bq27200_battery_probe, - .remove = bq27200_battery_remove, - .id_table = bq27200_id, + .probe = bq27x00_battery_probe, + .remove = bq27x00_battery_remove, + .id_table = bq27x00_id, }; static int __init bq27x00_battery_init(void) { int ret; - ret = i2c_add_driver(&bq27200_battery_driver); + ret = i2c_add_driver(&bq27x00_battery_driver); if (ret) - printk(KERN_ERR "Unable to register BQ27200 driver\n"); + printk(KERN_ERR "Unable to register BQ27x00 driver\n"); return ret; } @@ -363,7 +384,7 @@ module_init(bq27x00_battery_init); static void __exit bq27x00_battery_exit(void) { - i2c_del_driver(&bq27200_battery_driver); + i2c_del_driver(&bq27x00_battery_driver); } module_exit(bq27x00_battery_exit); -- cgit v0.10.2 From 4e924a814ac78e325653840190e9e4762e6f4b7c Mon Sep 17 00:00:00 2001 From: Grazvydas Ignotas Date: Sat, 27 Feb 2010 17:06:09 +0200 Subject: power_supply: bq27x00: add status and time properties The BQ27x00 series of chips can report time-to-empty and time-to-full, so let's add corresponding properties. Also report charge status based on status flag register. Signed-off-by: Grazvydas Ignotas Signed-off-by: Anton Vorontsov diff --git a/drivers/power/bq27x00_battery.c b/drivers/power/bq27x00_battery.c index 1526d02..5d940fa 100644 --- a/drivers/power/bq27x00_battery.c +++ b/drivers/power/bq27x00_battery.c @@ -32,10 +32,16 @@ #define BQ27x00_REG_VOLT 0x08 #define BQ27x00_REG_AI 0x14 #define BQ27x00_REG_FLAGS 0x0A +#define BQ27x00_REG_TTE 0x16 +#define BQ27x00_REG_TTF 0x18 +#define BQ27x00_REG_TTECP 0x26 #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */ +#define BQ27000_FLAG_CHGS BIT(7) #define BQ27500_REG_SOC 0x2c +#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... @@ -62,11 +68,15 @@ struct bq27x00_device_info { }; static enum power_supply_property bq27x00_battery_props[] = { + POWER_SUPPLY_PROP_STATUS, POWER_SUPPLY_PROP_PRESENT, POWER_SUPPLY_PROP_VOLTAGE_NOW, POWER_SUPPLY_PROP_CURRENT_NOW, POWER_SUPPLY_PROP_CAPACITY, POWER_SUPPLY_PROP_TEMP, + POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, + POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, + POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, }; /* @@ -144,7 +154,7 @@ static int bq27x00_battery_current(struct bq27x00_device_info *di) dev_err(di->dev, "error reading flags\n"); return 0; } - if ((flags & (1 << 7)) != 0) { + if (flags & BQ27000_FLAG_CHGS) { dev_dbg(di->dev, "negative current!\n"); return -curr; } @@ -174,6 +184,60 @@ static int bq27x00_battery_rsoc(struct bq27x00_device_info *di) return rsoc; } +static int bq27x00_battery_status(struct bq27x00_device_info *di, + 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) + status = POWER_SUPPLY_STATUS_FULL; + else if (flags & BQ27500_FLAG_DSC) + status = POWER_SUPPLY_STATUS_DISCHARGING; + else + status = POWER_SUPPLY_STATUS_CHARGING; + } else { + if (flags & BQ27000_FLAG_CHGS) + status = POWER_SUPPLY_STATUS_CHARGING; + else + status = POWER_SUPPLY_STATUS_DISCHARGING; + } + + val->intval = status; + return 0; +} + +/* + * Read a time register. + * Return < 0 if something fails. + */ +static int bq27x00_battery_time(struct bq27x00_device_info *di, int reg, + union power_supply_propval *val) +{ + int tval = 0; + int ret; + + ret = bq27x00_read(reg, &tval, 0, di); + if (ret) { + dev_err(di->dev, "error reading register %02x\n", reg); + return ret; + } + + if (tval == 65535) + return -ENODATA; + + val->intval = tval * 60; + return 0; +} + #define to_bq27x00_device_info(x) container_of((x), \ struct bq27x00_device_info, bat); @@ -181,9 +245,13 @@ static int bq27x00_battery_get_property(struct power_supply *psy, enum power_supply_property psp, union power_supply_propval *val) { + int ret = 0; struct bq27x00_device_info *di = to_bq27x00_device_info(psy); switch (psp) { + case POWER_SUPPLY_PROP_STATUS: + ret = bq27x00_battery_status(di, val); + break; case POWER_SUPPLY_PROP_VOLTAGE_NOW: case POWER_SUPPLY_PROP_PRESENT: val->intval = bq27x00_battery_voltage(di); @@ -199,11 +267,20 @@ static int bq27x00_battery_get_property(struct power_supply *psy, case POWER_SUPPLY_PROP_TEMP: val->intval = bq27x00_battery_temperature(di); break; + case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: + ret = bq27x00_battery_time(di, BQ27x00_REG_TTE, val); + break; + case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: + ret = bq27x00_battery_time(di, BQ27x00_REG_TTECP, val); + break; + case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: + ret = bq27x00_battery_time(di, BQ27x00_REG_TTF, val); + break; default: return -EINVAL; } - return 0; + return ret; } static void bq27x00_powersupply_init(struct bq27x00_device_info *di) -- cgit v0.10.2 From afbc74fdc5b96552c384e57119b3cc13c840bf06 Mon Sep 17 00:00:00 2001 From: Grazvydas Ignotas Date: Sat, 27 Feb 2010 17:06:44 +0200 Subject: power_supply: bq27x00: fix voltage and current units The chip returns voltage and current in mV and mA, but power supply class uses uV and uA, so add missing conversion. Signed-off-by: Grazvydas Ignotas Signed-off-by: Anton Vorontsov diff --git a/drivers/power/bq27x00_battery.c b/drivers/power/bq27x00_battery.c index 5d940fa..bece33e 100644 --- a/drivers/power/bq27x00_battery.c +++ b/drivers/power/bq27x00_battery.c @@ -125,7 +125,7 @@ static int bq27x00_battery_voltage(struct bq27x00_device_info *di) return ret; } - return volt; + return volt * 1000; } /* @@ -156,11 +156,11 @@ static int bq27x00_battery_current(struct bq27x00_device_info *di) } if (flags & BQ27000_FLAG_CHGS) { dev_dbg(di->dev, "negative current!\n"); - return -curr; + curr = -curr; } } - return curr; + return curr * 1000; } /* -- cgit v0.10.2