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