Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * BQ27x00 battery driver |
| 3 | * |
| 4 | * Copyright (C) 2008 Rodolfo Giometti <giometti@linux.it> |
| 5 | * Copyright (C) 2008 Eurotech S.p.A. <info@eurotech.it> |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 6 | * Copyright (C) 2010-2011 Lars-Peter Clausen <lars@metafoo.de> |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 7 | * |
| 8 | * Based on a previous work by Copyright (C) 2008 Texas Instruments, Inc. |
| 9 | * |
| 10 | * This package is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | * |
| 14 | * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED |
| 16 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 17 | * |
| 18 | */ |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 19 | |
| 20 | /* |
| 21 | * Datasheets: |
| 22 | * http://focus.ti.com/docs/prod/folders/print/bq27000.html |
| 23 | * http://focus.ti.com/docs/prod/folders/print/bq27500.html |
| 24 | */ |
| 25 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/param.h> |
| 28 | #include <linux/jiffies.h> |
| 29 | #include <linux/workqueue.h> |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/platform_device.h> |
| 32 | #include <linux/power_supply.h> |
| 33 | #include <linux/idr.h> |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 34 | #include <linux/i2c.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 35 | #include <linux/slab.h> |
Harvey Harrison | 8aef7e8 | 2008-09-22 14:53:50 -0700 | [diff] [blame] | 36 | #include <asm/unaligned.h> |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 37 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 38 | #include <linux/power/bq27x00_battery.h> |
| 39 | |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 40 | #define DRIVER_VERSION "1.2.0" |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 41 | |
| 42 | #define BQ27x00_REG_TEMP 0x06 |
| 43 | #define BQ27x00_REG_VOLT 0x08 |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 44 | #define BQ27x00_REG_AI 0x14 |
| 45 | #define BQ27x00_REG_FLAGS 0x0A |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 46 | #define BQ27x00_REG_TTE 0x16 |
| 47 | #define BQ27x00_REG_TTF 0x18 |
| 48 | #define BQ27x00_REG_TTECP 0x26 |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 49 | #define BQ27x00_REG_NAC 0x0C /* Nominal available capaciy */ |
| 50 | #define BQ27x00_REG_LMD 0x12 /* Last measured discharge */ |
| 51 | #define BQ27x00_REG_CYCT 0x2A /* Cycle count total */ |
| 52 | #define BQ27x00_REG_AE 0x22 /* Available enery */ |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 53 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 54 | #define BQ27000_REG_RSOC 0x0B /* Relative State-of-Charge */ |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 55 | #define BQ27000_REG_ILMD 0x76 /* Initial last measured discharge */ |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 56 | #define BQ27000_FLAG_CHGS BIT(7) |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 57 | |
| 58 | #define BQ27500_REG_SOC 0x2c |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 59 | #define BQ27500_REG_DCAP 0x3C /* Design capacity */ |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 60 | #define BQ27500_FLAG_DSC BIT(0) |
| 61 | #define BQ27500_FLAG_FC BIT(9) |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 62 | |
Pali Rohár | a2e5118 | 2010-05-24 20:52:13 +0200 | [diff] [blame] | 63 | #define BQ27000_RS 20 /* Resistor sense */ |
| 64 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 65 | struct bq27x00_device_info; |
| 66 | struct bq27x00_access_methods { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 67 | int (*read)(struct bq27x00_device_info *di, u8 reg, bool single); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 70 | enum bq27x00_chip { BQ27000, BQ27500 }; |
| 71 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 72 | struct bq27x00_reg_cache { |
| 73 | int temperature; |
| 74 | int time_to_empty; |
| 75 | int time_to_empty_avg; |
| 76 | int time_to_full; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 77 | int charge_full; |
| 78 | int charge_counter; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 79 | int capacity; |
| 80 | int flags; |
| 81 | |
| 82 | int current_now; |
| 83 | }; |
| 84 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 85 | struct bq27x00_device_info { |
| 86 | struct device *dev; |
| 87 | int id; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 88 | enum bq27x00_chip chip; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 89 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 90 | struct bq27x00_reg_cache cache; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 91 | int charge_design_full; |
| 92 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 93 | unsigned long last_update; |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 94 | struct delayed_work work; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 95 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 96 | struct power_supply bat; |
| 97 | |
| 98 | struct bq27x00_access_methods bus; |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 99 | |
| 100 | struct mutex lock; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | static enum power_supply_property bq27x00_battery_props[] = { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 104 | POWER_SUPPLY_PROP_STATUS, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 105 | POWER_SUPPLY_PROP_PRESENT, |
| 106 | POWER_SUPPLY_PROP_VOLTAGE_NOW, |
| 107 | POWER_SUPPLY_PROP_CURRENT_NOW, |
| 108 | POWER_SUPPLY_PROP_CAPACITY, |
| 109 | POWER_SUPPLY_PROP_TEMP, |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 110 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW, |
| 111 | POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG, |
| 112 | POWER_SUPPLY_PROP_TIME_TO_FULL_NOW, |
Lars-Peter Clausen | 5661f33 | 2010-05-24 20:36:52 +0200 | [diff] [blame] | 113 | POWER_SUPPLY_PROP_TECHNOLOGY, |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 114 | POWER_SUPPLY_PROP_CHARGE_FULL, |
| 115 | POWER_SUPPLY_PROP_CHARGE_NOW, |
| 116 | POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN, |
| 117 | POWER_SUPPLY_PROP_CHARGE_COUNTER, |
| 118 | POWER_SUPPLY_PROP_ENERGY_NOW, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 121 | static unsigned int poll_interval = 360; |
| 122 | module_param(poll_interval, uint, 0644); |
| 123 | MODULE_PARM_DESC(poll_interval, "battery poll interval in seconds - " \ |
| 124 | "0 disables polling"); |
| 125 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 126 | /* |
| 127 | * Common code for BQ27x00 devices |
| 128 | */ |
| 129 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 130 | static inline int bq27x00_read(struct bq27x00_device_info *di, u8 reg, |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 131 | bool single) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 132 | { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 133 | return di->bus.read(di, reg, single); |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Return the battery Relative State-of-Charge |
| 138 | * Or < 0 if something fails. |
| 139 | */ |
| 140 | static int bq27x00_battery_read_rsoc(struct bq27x00_device_info *di) |
| 141 | { |
| 142 | int rsoc; |
| 143 | |
| 144 | if (di->chip == BQ27500) |
| 145 | rsoc = bq27x00_read(di, BQ27500_REG_SOC, false); |
| 146 | else |
| 147 | rsoc = bq27x00_read(di, BQ27000_REG_RSOC, true); |
| 148 | |
| 149 | if (rsoc < 0) |
| 150 | dev_err(di->dev, "error reading relative State-of-Charge\n"); |
| 151 | |
| 152 | return rsoc; |
| 153 | } |
| 154 | |
| 155 | /* |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 156 | * Return a battery charge value in µAh |
| 157 | * Or < 0 if something fails. |
| 158 | */ |
| 159 | static int bq27x00_battery_read_charge(struct bq27x00_device_info *di, u8 reg) |
| 160 | { |
| 161 | int charge; |
| 162 | |
| 163 | charge = bq27x00_read(di, reg, false); |
| 164 | if (charge < 0) { |
| 165 | dev_err(di->dev, "error reading nominal available capacity\n"); |
| 166 | return charge; |
| 167 | } |
| 168 | |
| 169 | if (di->chip == BQ27500) |
| 170 | charge *= 1000; |
| 171 | else |
| 172 | charge = charge * 3570 / BQ27000_RS; |
| 173 | |
| 174 | return charge; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Return the battery Nominal available capaciy in µAh |
| 179 | * Or < 0 if something fails. |
| 180 | */ |
| 181 | static inline int bq27x00_battery_read_nac(struct bq27x00_device_info *di) |
| 182 | { |
| 183 | return bq27x00_battery_read_charge(di, BQ27x00_REG_NAC); |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | * Return the battery Last measured discharge in µAh |
| 188 | * Or < 0 if something fails. |
| 189 | */ |
| 190 | static inline int bq27x00_battery_read_lmd(struct bq27x00_device_info *di) |
| 191 | { |
| 192 | return bq27x00_battery_read_charge(di, BQ27x00_REG_LMD); |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Return the battery Initial last measured discharge in µAh |
| 197 | * Or < 0 if something fails. |
| 198 | */ |
| 199 | static int bq27x00_battery_read_ilmd(struct bq27x00_device_info *di) |
| 200 | { |
| 201 | int ilmd; |
| 202 | |
| 203 | if (di->chip == BQ27500) |
| 204 | ilmd = bq27x00_read(di, BQ27500_REG_DCAP, false); |
| 205 | else |
| 206 | ilmd = bq27x00_read(di, BQ27000_REG_ILMD, true); |
| 207 | |
| 208 | if (ilmd < 0) { |
| 209 | dev_err(di->dev, "error reading initial last measured discharge\n"); |
| 210 | return ilmd; |
| 211 | } |
| 212 | |
| 213 | if (di->chip == BQ27500) |
| 214 | ilmd *= 1000; |
| 215 | else |
| 216 | ilmd = ilmd * 256 * 3570 / BQ27000_RS; |
| 217 | |
| 218 | return ilmd; |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * Return the battery Cycle count total |
| 223 | * Or < 0 if something fails. |
| 224 | */ |
| 225 | static int bq27x00_battery_read_cyct(struct bq27x00_device_info *di) |
| 226 | { |
| 227 | int cyct; |
| 228 | |
| 229 | cyct = bq27x00_read(di, BQ27x00_REG_CYCT, false); |
| 230 | if (cyct < 0) |
| 231 | dev_err(di->dev, "error reading cycle count total\n"); |
| 232 | |
| 233 | return cyct; |
| 234 | } |
| 235 | |
| 236 | /* |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 237 | * Read a time register. |
| 238 | * Return < 0 if something fails. |
| 239 | */ |
| 240 | static int bq27x00_battery_read_time(struct bq27x00_device_info *di, u8 reg) |
| 241 | { |
| 242 | int tval; |
| 243 | |
| 244 | tval = bq27x00_read(di, reg, false); |
| 245 | if (tval < 0) { |
| 246 | dev_err(di->dev, "error reading register %02x: %d\n", reg, tval); |
| 247 | return tval; |
| 248 | } |
| 249 | |
| 250 | if (tval == 65535) |
| 251 | return -ENODATA; |
| 252 | |
| 253 | return tval * 60; |
| 254 | } |
| 255 | |
| 256 | static void bq27x00_update(struct bq27x00_device_info *di) |
| 257 | { |
| 258 | struct bq27x00_reg_cache cache = {0, }; |
| 259 | bool is_bq27500 = di->chip == BQ27500; |
| 260 | |
| 261 | cache.flags = bq27x00_read(di, BQ27x00_REG_FLAGS, is_bq27500); |
| 262 | if (cache.flags >= 0) { |
| 263 | cache.capacity = bq27x00_battery_read_rsoc(di); |
| 264 | cache.temperature = bq27x00_read(di, BQ27x00_REG_TEMP, false); |
| 265 | cache.time_to_empty = bq27x00_battery_read_time(di, BQ27x00_REG_TTE); |
| 266 | cache.time_to_empty_avg = bq27x00_battery_read_time(di, BQ27x00_REG_TTECP); |
| 267 | cache.time_to_full = bq27x00_battery_read_time(di, BQ27x00_REG_TTF); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 268 | cache.charge_full = bq27x00_battery_read_lmd(di); |
| 269 | cache.charge_counter = bq27x00_battery_read_cyct(di); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 270 | |
| 271 | if (!is_bq27500) |
| 272 | cache.current_now = bq27x00_read(di, BQ27x00_REG_AI, false); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 273 | |
| 274 | /* We only have to read charge design full once */ |
| 275 | if (di->charge_design_full <= 0) |
| 276 | di->charge_design_full = bq27x00_battery_read_ilmd(di); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /* Ignore current_now which is a snapshot of the current battery state |
| 280 | * and is likely to be different even between two consecutive reads */ |
| 281 | if (memcmp(&di->cache, &cache, sizeof(cache) - sizeof(int)) != 0) { |
| 282 | di->cache = cache; |
| 283 | power_supply_changed(&di->bat); |
| 284 | } |
| 285 | |
| 286 | di->last_update = jiffies; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 287 | } |
| 288 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 289 | static void bq27x00_battery_poll(struct work_struct *work) |
| 290 | { |
| 291 | struct bq27x00_device_info *di = |
| 292 | container_of(work, struct bq27x00_device_info, work.work); |
| 293 | |
| 294 | bq27x00_update(di); |
| 295 | |
| 296 | if (poll_interval > 0) { |
| 297 | /* The timer does not have to be accurate. */ |
| 298 | set_timer_slack(&di->work.timer, poll_interval * HZ / 4); |
| 299 | schedule_delayed_work(&di->work, poll_interval * HZ); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 304 | /* |
Grazvydas Ignotas | b4de360 | 2010-02-12 23:57:13 +0200 | [diff] [blame] | 305 | * Return the battery temperature in tenths of degree Celsius |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 306 | * Or < 0 if something fails. |
| 307 | */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 308 | static int bq27x00_battery_temperature(struct bq27x00_device_info *di, |
| 309 | union power_supply_propval *val) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 310 | { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 311 | if (di->cache.temperature < 0) |
| 312 | return di->cache.temperature; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 313 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 314 | if (di->chip == BQ27500) |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 315 | val->intval = di->cache.temperature - 2731; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 316 | else |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 317 | val->intval = ((di->cache.temperature * 5) - 5463) / 2; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 318 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 319 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Return the battery average current |
| 324 | * Note that current can be negative signed as well |
| 325 | * Or 0 if something fails. |
| 326 | */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 327 | static int bq27x00_battery_current(struct bq27x00_device_info *di, |
| 328 | union power_supply_propval *val) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 329 | { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 330 | int curr; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 331 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 332 | if (di->chip == BQ27500) |
| 333 | curr = bq27x00_read(di, BQ27x00_REG_AI, false); |
| 334 | else |
| 335 | curr = di->cache.current_now; |
| 336 | |
| 337 | if (curr < 0) |
| 338 | return curr; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 339 | |
| 340 | if (di->chip == BQ27500) { |
| 341 | /* bq27500 returns signed value */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 342 | val->intval = (int)((s16)curr) * 1000; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 343 | } else { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 344 | if (di->cache.flags & BQ27000_FLAG_CHGS) { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 345 | dev_dbg(di->dev, "negative current!\n"); |
Grazvydas Ignotas | afbc74f | 2010-02-27 17:06:44 +0200 | [diff] [blame] | 346 | curr = -curr; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 347 | } |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 348 | |
| 349 | val->intval = curr * 3570 / BQ27000_RS; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 350 | } |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 351 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 352 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 355 | static int bq27x00_battery_status(struct bq27x00_device_info *di, |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 356 | union power_supply_propval *val) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 357 | { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 358 | int status; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 359 | |
| 360 | if (di->chip == BQ27500) { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 361 | if (di->cache.flags & BQ27500_FLAG_FC) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 362 | status = POWER_SUPPLY_STATUS_FULL; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 363 | else if (di->cache.flags & BQ27500_FLAG_DSC) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 364 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 365 | else |
| 366 | status = POWER_SUPPLY_STATUS_CHARGING; |
| 367 | } else { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 368 | if (di->cache.flags & BQ27000_FLAG_CHGS) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 369 | status = POWER_SUPPLY_STATUS_CHARGING; |
| 370 | else |
| 371 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 372 | } |
| 373 | |
| 374 | val->intval = status; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 375 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | /* |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 380 | * Return the battery Voltage in milivolts |
| 381 | * Or < 0 if something fails. |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 382 | */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 383 | static int bq27x00_battery_voltage(struct bq27x00_device_info *di, |
| 384 | union power_supply_propval *val) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 385 | { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 386 | int volt; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 387 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 388 | volt = bq27x00_read(di, BQ27x00_REG_VOLT, false); |
| 389 | if (volt < 0) |
| 390 | return volt; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 391 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 392 | val->intval = volt * 1000; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 393 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 397 | /* |
| 398 | * Return the battery Available energy in µWh |
| 399 | * Or < 0 if something fails. |
| 400 | */ |
| 401 | static int bq27x00_battery_energy(struct bq27x00_device_info *di, |
| 402 | union power_supply_propval *val) |
| 403 | { |
| 404 | int ae; |
| 405 | |
| 406 | ae = bq27x00_read(di, BQ27x00_REG_AE, false); |
| 407 | if (ae < 0) { |
| 408 | dev_err(di->dev, "error reading available energy\n"); |
| 409 | return ae; |
| 410 | } |
| 411 | |
| 412 | if (di->chip == BQ27500) |
| 413 | ae *= 1000; |
| 414 | else |
| 415 | ae = ae * 29200 / BQ27000_RS; |
| 416 | |
| 417 | val->intval = ae; |
| 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 423 | static int bq27x00_simple_value(int value, |
| 424 | union power_supply_propval *val) |
| 425 | { |
| 426 | if (value < 0) |
| 427 | return value; |
| 428 | |
| 429 | val->intval = value; |
| 430 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 431 | return 0; |
| 432 | } |
| 433 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 434 | #define to_bq27x00_device_info(x) container_of((x), \ |
| 435 | struct bq27x00_device_info, bat); |
| 436 | |
| 437 | static int bq27x00_battery_get_property(struct power_supply *psy, |
| 438 | enum power_supply_property psp, |
| 439 | union power_supply_propval *val) |
| 440 | { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 441 | int ret = 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 442 | struct bq27x00_device_info *di = to_bq27x00_device_info(psy); |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 443 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 444 | mutex_lock(&di->lock); |
| 445 | if (time_is_before_jiffies(di->last_update + 5 * HZ)) { |
| 446 | cancel_delayed_work_sync(&di->work); |
| 447 | bq27x00_battery_poll(&di->work.work); |
| 448 | } |
| 449 | mutex_unlock(&di->lock); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 450 | |
| 451 | if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0) |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 452 | return -ENODEV; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 453 | |
| 454 | switch (psp) { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 455 | case POWER_SUPPLY_PROP_STATUS: |
| 456 | ret = bq27x00_battery_status(di, val); |
| 457 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 458 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 459 | ret = bq27x00_battery_voltage(di, val); |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 460 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 461 | case POWER_SUPPLY_PROP_PRESENT: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 462 | val->intval = di->cache.flags < 0 ? 0 : 1; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 463 | break; |
| 464 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 465 | ret = bq27x00_battery_current(di, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 466 | break; |
| 467 | case POWER_SUPPLY_PROP_CAPACITY: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 468 | ret = bq27x00_simple_value(di->cache.capacity, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 469 | break; |
| 470 | case POWER_SUPPLY_PROP_TEMP: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 471 | ret = bq27x00_battery_temperature(di, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 472 | break; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 473 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 474 | ret = bq27x00_simple_value(di->cache.time_to_empty, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 475 | break; |
| 476 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 477 | ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 478 | break; |
| 479 | case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 480 | ret = bq27x00_simple_value(di->cache.time_to_full, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 481 | break; |
Lars-Peter Clausen | 5661f33 | 2010-05-24 20:36:52 +0200 | [diff] [blame] | 482 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 483 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 484 | break; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 485 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 486 | ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val); |
| 487 | break; |
| 488 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 489 | ret = bq27x00_simple_value(di->cache.charge_full, val); |
| 490 | break; |
| 491 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 492 | ret = bq27x00_simple_value(di->charge_design_full, val); |
| 493 | break; |
| 494 | case POWER_SUPPLY_PROP_CHARGE_COUNTER: |
| 495 | ret = bq27x00_simple_value(di->cache.charge_counter, val); |
| 496 | break; |
| 497 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 498 | ret = bq27x00_battery_energy(di, val); |
| 499 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 500 | default: |
| 501 | return -EINVAL; |
| 502 | } |
| 503 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 504 | return ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 507 | static void bq27x00_external_power_changed(struct power_supply *psy) |
| 508 | { |
| 509 | struct bq27x00_device_info *di = to_bq27x00_device_info(psy); |
| 510 | |
| 511 | cancel_delayed_work_sync(&di->work); |
| 512 | schedule_delayed_work(&di->work, 0); |
| 513 | } |
| 514 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 515 | static int bq27x00_powersupply_init(struct bq27x00_device_info *di) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 516 | { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 517 | int ret; |
| 518 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 519 | di->bat.type = POWER_SUPPLY_TYPE_BATTERY; |
| 520 | di->bat.properties = bq27x00_battery_props; |
| 521 | di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props); |
| 522 | di->bat.get_property = bq27x00_battery_get_property; |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 523 | di->bat.external_power_changed = bq27x00_external_power_changed; |
| 524 | |
| 525 | INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll); |
| 526 | mutex_init(&di->lock); |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 527 | |
| 528 | ret = power_supply_register(di->dev, &di->bat); |
| 529 | if (ret) { |
| 530 | dev_err(di->dev, "failed to register battery: %d\n", ret); |
| 531 | return ret; |
| 532 | } |
| 533 | |
| 534 | dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION); |
| 535 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 536 | bq27x00_update(di); |
| 537 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 538 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 541 | static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di) |
| 542 | { |
| 543 | cancel_delayed_work_sync(&di->work); |
| 544 | |
| 545 | power_supply_unregister(&di->bat); |
| 546 | |
| 547 | mutex_destroy(&di->lock); |
| 548 | } |
| 549 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 550 | |
| 551 | /* i2c specific code */ |
| 552 | #ifdef CONFIG_BATTERY_BQ27X00_I2C |
| 553 | |
| 554 | /* If the system has several batteries we need a different name for each |
| 555 | * of them... |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 556 | */ |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 557 | static DEFINE_IDR(battery_id); |
| 558 | static DEFINE_MUTEX(battery_mutex); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 559 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 560 | static int bq27x00_read_i2c(struct bq27x00_device_info *di, u8 reg, bool single) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 561 | { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 562 | struct i2c_client *client = to_i2c_client(di->dev); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 563 | struct i2c_msg msg[1]; |
| 564 | unsigned char data[2]; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 565 | int ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 566 | |
| 567 | if (!client->adapter) |
| 568 | return -ENODEV; |
| 569 | |
| 570 | msg->addr = client->addr; |
| 571 | msg->flags = 0; |
| 572 | msg->len = 1; |
| 573 | msg->buf = data; |
| 574 | |
| 575 | data[0] = reg; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 576 | ret = i2c_transfer(client->adapter, msg, 1); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 577 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 578 | if (ret >= 0) { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 579 | if (!single) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 580 | msg->len = 2; |
| 581 | else |
| 582 | msg->len = 1; |
| 583 | |
| 584 | msg->flags = I2C_M_RD; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 585 | ret = i2c_transfer(client->adapter, msg, 1); |
| 586 | if (ret >= 0) { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 587 | if (!single) |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 588 | ret = get_unaligned_le16(data); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 589 | else |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 590 | ret = data[0]; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 591 | } |
| 592 | } |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 593 | return ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 594 | } |
| 595 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 596 | static int bq27x00_battery_probe(struct i2c_client *client, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 597 | const struct i2c_device_id *id) |
| 598 | { |
| 599 | char *name; |
| 600 | struct bq27x00_device_info *di; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 601 | int num; |
| 602 | int retval = 0; |
| 603 | |
| 604 | /* Get new ID for the new battery device */ |
| 605 | retval = idr_pre_get(&battery_id, GFP_KERNEL); |
| 606 | if (retval == 0) |
| 607 | return -ENOMEM; |
| 608 | mutex_lock(&battery_mutex); |
| 609 | retval = idr_get_new(&battery_id, client, &num); |
| 610 | mutex_unlock(&battery_mutex); |
| 611 | if (retval < 0) |
| 612 | return retval; |
| 613 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 614 | name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 615 | if (!name) { |
| 616 | dev_err(&client->dev, "failed to allocate device name\n"); |
| 617 | retval = -ENOMEM; |
| 618 | goto batt_failed_1; |
| 619 | } |
| 620 | |
| 621 | di = kzalloc(sizeof(*di), GFP_KERNEL); |
| 622 | if (!di) { |
| 623 | dev_err(&client->dev, "failed to allocate device info data\n"); |
| 624 | retval = -ENOMEM; |
| 625 | goto batt_failed_2; |
| 626 | } |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 627 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 628 | di->id = num; |
| 629 | di->dev = &client->dev; |
| 630 | di->chip = id->driver_data; |
| 631 | di->bat.name = name; |
| 632 | di->bus.read = &bq27x00_read_i2c; |
| 633 | |
| 634 | if (bq27x00_powersupply_init(di)) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 635 | goto batt_failed_3; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 636 | |
| 637 | i2c_set_clientdata(client, di); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 638 | |
| 639 | return 0; |
| 640 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 641 | batt_failed_3: |
| 642 | kfree(di); |
| 643 | batt_failed_2: |
| 644 | kfree(name); |
| 645 | batt_failed_1: |
| 646 | mutex_lock(&battery_mutex); |
| 647 | idr_remove(&battery_id, num); |
| 648 | mutex_unlock(&battery_mutex); |
| 649 | |
| 650 | return retval; |
| 651 | } |
| 652 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 653 | static int bq27x00_battery_remove(struct i2c_client *client) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 654 | { |
| 655 | struct bq27x00_device_info *di = i2c_get_clientdata(client); |
| 656 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 657 | bq27x00_powersupply_unregister(di); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 658 | |
| 659 | kfree(di->bat.name); |
| 660 | |
| 661 | mutex_lock(&battery_mutex); |
| 662 | idr_remove(&battery_id, di->id); |
| 663 | mutex_unlock(&battery_mutex); |
| 664 | |
| 665 | kfree(di); |
| 666 | |
| 667 | return 0; |
| 668 | } |
| 669 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 670 | static const struct i2c_device_id bq27x00_id[] = { |
| 671 | { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */ |
| 672 | { "bq27500", BQ27500 }, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 673 | {}, |
| 674 | }; |
Pali Rohár | fd9b958 | 2011-02-01 00:10:41 +0100 | [diff] [blame^] | 675 | MODULE_DEVICE_TABLE(i2c, bq27x00_id); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 676 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 677 | static struct i2c_driver bq27x00_battery_driver = { |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 678 | .driver = { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 679 | .name = "bq27x00-battery", |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 680 | }, |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 681 | .probe = bq27x00_battery_probe, |
| 682 | .remove = bq27x00_battery_remove, |
| 683 | .id_table = bq27x00_id, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 684 | }; |
| 685 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 686 | static inline int bq27x00_battery_i2c_init(void) |
| 687 | { |
| 688 | int ret = i2c_add_driver(&bq27x00_battery_driver); |
| 689 | if (ret) |
| 690 | printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n"); |
| 691 | |
| 692 | return ret; |
| 693 | } |
| 694 | |
| 695 | static inline void bq27x00_battery_i2c_exit(void) |
| 696 | { |
| 697 | i2c_del_driver(&bq27x00_battery_driver); |
| 698 | } |
| 699 | |
| 700 | #else |
| 701 | |
| 702 | static inline int bq27x00_battery_i2c_init(void) { return 0; } |
| 703 | static inline void bq27x00_battery_i2c_exit(void) {}; |
| 704 | |
| 705 | #endif |
| 706 | |
| 707 | /* platform specific code */ |
| 708 | #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM |
| 709 | |
| 710 | static int bq27000_read_platform(struct bq27x00_device_info *di, u8 reg, |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 711 | bool single) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 712 | { |
| 713 | struct device *dev = di->dev; |
| 714 | struct bq27000_platform_data *pdata = dev->platform_data; |
| 715 | unsigned int timeout = 3; |
| 716 | int upper, lower; |
| 717 | int temp; |
| 718 | |
| 719 | if (!single) { |
| 720 | /* Make sure the value has not changed in between reading the |
| 721 | * lower and the upper part */ |
| 722 | upper = pdata->read(dev, reg + 1); |
| 723 | do { |
| 724 | temp = upper; |
| 725 | if (upper < 0) |
| 726 | return upper; |
| 727 | |
| 728 | lower = pdata->read(dev, reg); |
| 729 | if (lower < 0) |
| 730 | return lower; |
| 731 | |
| 732 | upper = pdata->read(dev, reg + 1); |
| 733 | } while (temp != upper && --timeout); |
| 734 | |
| 735 | if (timeout == 0) |
| 736 | return -EIO; |
| 737 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 738 | return (upper << 8) | lower; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 739 | } |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 740 | |
| 741 | return pdata->read(dev, reg); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | static int __devinit bq27000_battery_probe(struct platform_device *pdev) |
| 745 | { |
| 746 | struct bq27x00_device_info *di; |
| 747 | struct bq27000_platform_data *pdata = pdev->dev.platform_data; |
| 748 | int ret; |
| 749 | |
| 750 | if (!pdata) { |
| 751 | dev_err(&pdev->dev, "no platform_data supplied\n"); |
| 752 | return -EINVAL; |
| 753 | } |
| 754 | |
| 755 | if (!pdata->read) { |
| 756 | dev_err(&pdev->dev, "no hdq read callback supplied\n"); |
| 757 | return -EINVAL; |
| 758 | } |
| 759 | |
| 760 | di = kzalloc(sizeof(*di), GFP_KERNEL); |
| 761 | if (!di) { |
| 762 | dev_err(&pdev->dev, "failed to allocate device info data\n"); |
| 763 | return -ENOMEM; |
| 764 | } |
| 765 | |
| 766 | platform_set_drvdata(pdev, di); |
| 767 | |
| 768 | di->dev = &pdev->dev; |
| 769 | di->chip = BQ27000; |
| 770 | |
| 771 | di->bat.name = pdata->name ?: dev_name(&pdev->dev); |
| 772 | di->bus.read = &bq27000_read_platform; |
| 773 | |
| 774 | ret = bq27x00_powersupply_init(di); |
| 775 | if (ret) |
| 776 | goto err_free; |
| 777 | |
| 778 | return 0; |
| 779 | |
| 780 | err_free: |
| 781 | platform_set_drvdata(pdev, NULL); |
| 782 | kfree(di); |
| 783 | |
| 784 | return ret; |
| 785 | } |
| 786 | |
| 787 | static int __devexit bq27000_battery_remove(struct platform_device *pdev) |
| 788 | { |
| 789 | struct bq27x00_device_info *di = platform_get_drvdata(pdev); |
| 790 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 791 | bq27x00_powersupply_unregister(di); |
| 792 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 793 | platform_set_drvdata(pdev, NULL); |
| 794 | kfree(di); |
| 795 | |
| 796 | return 0; |
| 797 | } |
| 798 | |
| 799 | static struct platform_driver bq27000_battery_driver = { |
| 800 | .probe = bq27000_battery_probe, |
| 801 | .remove = __devexit_p(bq27000_battery_remove), |
| 802 | .driver = { |
| 803 | .name = "bq27000-battery", |
| 804 | .owner = THIS_MODULE, |
| 805 | }, |
| 806 | }; |
| 807 | |
| 808 | static inline int bq27x00_battery_platform_init(void) |
| 809 | { |
| 810 | int ret = platform_driver_register(&bq27000_battery_driver); |
| 811 | if (ret) |
| 812 | printk(KERN_ERR "Unable to register BQ27000 platform driver\n"); |
| 813 | |
| 814 | return ret; |
| 815 | } |
| 816 | |
| 817 | static inline void bq27x00_battery_platform_exit(void) |
| 818 | { |
| 819 | platform_driver_unregister(&bq27000_battery_driver); |
| 820 | } |
| 821 | |
| 822 | #else |
| 823 | |
| 824 | static inline int bq27x00_battery_platform_init(void) { return 0; } |
| 825 | static inline void bq27x00_battery_platform_exit(void) {}; |
| 826 | |
| 827 | #endif |
| 828 | |
| 829 | /* |
| 830 | * Module stuff |
| 831 | */ |
| 832 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 833 | static int __init bq27x00_battery_init(void) |
| 834 | { |
| 835 | int ret; |
| 836 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 837 | ret = bq27x00_battery_i2c_init(); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 838 | if (ret) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 839 | return ret; |
| 840 | |
| 841 | ret = bq27x00_battery_platform_init(); |
| 842 | if (ret) |
| 843 | bq27x00_battery_i2c_exit(); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 844 | |
| 845 | return ret; |
| 846 | } |
| 847 | module_init(bq27x00_battery_init); |
| 848 | |
| 849 | static void __exit bq27x00_battery_exit(void) |
| 850 | { |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 851 | bq27x00_battery_platform_exit(); |
| 852 | bq27x00_battery_i2c_exit(); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 853 | } |
| 854 | module_exit(bq27x00_battery_exit); |
| 855 | |
| 856 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); |
| 857 | MODULE_DESCRIPTION("BQ27x00 battery monitor driver"); |
| 858 | MODULE_LICENSE("GPL"); |