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