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; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 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, |
Pali Rohár | 73c244a | 2011-04-30 23:59:35 +0200 | [diff] [blame] | 117 | POWER_SUPPLY_PROP_CYCLE_COUNT, |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 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); |
Pali Rohár | 73c244a | 2011-04-30 23:59:35 +0200 | [diff] [blame] | 269 | cache.cycle_count = bq27x00_battery_read_cyct(di); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 270 | |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 271 | /* We only have to read charge design full once */ |
| 272 | if (di->charge_design_full <= 0) |
| 273 | di->charge_design_full = bq27x00_battery_read_ilmd(di); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame^] | 276 | if (memcmp(&di->cache, &cache, sizeof(cache)) != 0) { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 277 | di->cache = cache; |
| 278 | power_supply_changed(&di->bat); |
| 279 | } |
| 280 | |
| 281 | di->last_update = jiffies; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 284 | static void bq27x00_battery_poll(struct work_struct *work) |
| 285 | { |
| 286 | struct bq27x00_device_info *di = |
| 287 | container_of(work, struct bq27x00_device_info, work.work); |
| 288 | |
| 289 | bq27x00_update(di); |
| 290 | |
| 291 | if (poll_interval > 0) { |
| 292 | /* The timer does not have to be accurate. */ |
| 293 | set_timer_slack(&di->work.timer, poll_interval * HZ / 4); |
| 294 | schedule_delayed_work(&di->work, poll_interval * HZ); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 299 | /* |
Grazvydas Ignotas | b4de360 | 2010-02-12 23:57:13 +0200 | [diff] [blame] | 300 | * Return the battery temperature in tenths of degree Celsius |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 301 | * Or < 0 if something fails. |
| 302 | */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 303 | static int bq27x00_battery_temperature(struct bq27x00_device_info *di, |
| 304 | union power_supply_propval *val) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 305 | { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 306 | if (di->cache.temperature < 0) |
| 307 | return di->cache.temperature; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 308 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 309 | if (di->chip == BQ27500) |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 310 | val->intval = di->cache.temperature - 2731; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 311 | else |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 312 | val->intval = ((di->cache.temperature * 5) - 5463) / 2; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 313 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 314 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /* |
Pali Rohár | bf7d414 | 2011-02-01 00:23:13 +0100 | [diff] [blame] | 318 | * Return the battery average current in µA |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 319 | * Note that current can be negative signed as well |
| 320 | * Or 0 if something fails. |
| 321 | */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 322 | static int bq27x00_battery_current(struct bq27x00_device_info *di, |
| 323 | union power_supply_propval *val) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 324 | { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 325 | int curr; |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame^] | 326 | int flags; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 327 | |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame^] | 328 | curr = bq27x00_read(di, BQ27x00_REG_AI, false); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 329 | if (curr < 0) |
| 330 | return curr; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 331 | |
| 332 | if (di->chip == BQ27500) { |
| 333 | /* bq27500 returns signed value */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 334 | val->intval = (int)((s16)curr) * 1000; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 335 | } else { |
Pali Rohár | b68f621 | 2011-11-01 01:43:03 +0100 | [diff] [blame^] | 336 | flags = bq27x00_read(di, BQ27x00_REG_FLAGS, false); |
| 337 | if (flags & BQ27000_FLAG_CHGS) { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 338 | dev_dbg(di->dev, "negative current!\n"); |
Grazvydas Ignotas | afbc74f | 2010-02-27 17:06:44 +0200 | [diff] [blame] | 339 | curr = -curr; |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 340 | } |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 341 | |
| 342 | val->intval = curr * 3570 / BQ27000_RS; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 343 | } |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 344 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 345 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 348 | static int bq27x00_battery_status(struct bq27x00_device_info *di, |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 349 | union power_supply_propval *val) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 350 | { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 351 | int status; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 352 | |
| 353 | if (di->chip == BQ27500) { |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 354 | if (di->cache.flags & BQ27500_FLAG_FC) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 355 | status = POWER_SUPPLY_STATUS_FULL; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 356 | else if (di->cache.flags & BQ27500_FLAG_DSC) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 357 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 358 | else |
| 359 | status = POWER_SUPPLY_STATUS_CHARGING; |
| 360 | } else { |
Lars-Peter Clausen | c1b9ab6 | 2010-09-18 17:31:16 +0200 | [diff] [blame] | 361 | if (di->cache.flags & BQ27000_FLAG_FC) |
| 362 | status = POWER_SUPPLY_STATUS_FULL; |
| 363 | else if (di->cache.flags & BQ27000_FLAG_CHGS) |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 364 | status = POWER_SUPPLY_STATUS_CHARGING; |
Lars-Peter Clausen | c1b9ab6 | 2010-09-18 17:31:16 +0200 | [diff] [blame] | 365 | else if (power_supply_am_i_supplied(&di->bat)) |
| 366 | status = POWER_SUPPLY_STATUS_NOT_CHARGING; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 367 | else |
| 368 | status = POWER_SUPPLY_STATUS_DISCHARGING; |
| 369 | } |
| 370 | |
| 371 | val->intval = status; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 372 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | /* |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 377 | * Return the battery Voltage in milivolts |
| 378 | * Or < 0 if something fails. |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 379 | */ |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 380 | static int bq27x00_battery_voltage(struct bq27x00_device_info *di, |
| 381 | union power_supply_propval *val) |
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 | int volt; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 384 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 385 | volt = bq27x00_read(di, BQ27x00_REG_VOLT, false); |
| 386 | if (volt < 0) |
| 387 | return volt; |
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 | val->intval = volt * 1000; |
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 | return 0; |
| 392 | } |
| 393 | |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 394 | /* |
| 395 | * Return the battery Available energy in µWh |
| 396 | * Or < 0 if something fails. |
| 397 | */ |
| 398 | static int bq27x00_battery_energy(struct bq27x00_device_info *di, |
| 399 | union power_supply_propval *val) |
| 400 | { |
| 401 | int ae; |
| 402 | |
| 403 | ae = bq27x00_read(di, BQ27x00_REG_AE, false); |
| 404 | if (ae < 0) { |
| 405 | dev_err(di->dev, "error reading available energy\n"); |
| 406 | return ae; |
| 407 | } |
| 408 | |
| 409 | if (di->chip == BQ27500) |
| 410 | ae *= 1000; |
| 411 | else |
| 412 | ae = ae * 29200 / BQ27000_RS; |
| 413 | |
| 414 | val->intval = ae; |
| 415 | |
| 416 | return 0; |
| 417 | } |
| 418 | |
| 419 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 420 | static int bq27x00_simple_value(int value, |
| 421 | union power_supply_propval *val) |
| 422 | { |
| 423 | if (value < 0) |
| 424 | return value; |
| 425 | |
| 426 | val->intval = value; |
| 427 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 431 | #define to_bq27x00_device_info(x) container_of((x), \ |
| 432 | struct bq27x00_device_info, bat); |
| 433 | |
| 434 | static int bq27x00_battery_get_property(struct power_supply *psy, |
| 435 | enum power_supply_property psp, |
| 436 | union power_supply_propval *val) |
| 437 | { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 438 | int ret = 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 439 | struct bq27x00_device_info *di = to_bq27x00_device_info(psy); |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 440 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 441 | mutex_lock(&di->lock); |
| 442 | if (time_is_before_jiffies(di->last_update + 5 * HZ)) { |
| 443 | cancel_delayed_work_sync(&di->work); |
| 444 | bq27x00_battery_poll(&di->work.work); |
| 445 | } |
| 446 | mutex_unlock(&di->lock); |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 447 | |
| 448 | if (psp != POWER_SUPPLY_PROP_PRESENT && di->cache.flags < 0) |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 449 | return -ENODEV; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 450 | |
| 451 | switch (psp) { |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 452 | case POWER_SUPPLY_PROP_STATUS: |
| 453 | ret = bq27x00_battery_status(di, val); |
| 454 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 455 | case POWER_SUPPLY_PROP_VOLTAGE_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 456 | ret = bq27x00_battery_voltage(di, val); |
Lars-Peter Clausen | 3413b4e | 2010-05-24 21:57:33 +0200 | [diff] [blame] | 457 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 458 | case POWER_SUPPLY_PROP_PRESENT: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 459 | val->intval = di->cache.flags < 0 ? 0 : 1; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 460 | break; |
| 461 | case POWER_SUPPLY_PROP_CURRENT_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 462 | ret = bq27x00_battery_current(di, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 463 | break; |
| 464 | case POWER_SUPPLY_PROP_CAPACITY: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 465 | ret = bq27x00_simple_value(di->cache.capacity, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 466 | break; |
| 467 | case POWER_SUPPLY_PROP_TEMP: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 468 | ret = bq27x00_battery_temperature(di, val); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 469 | break; |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 470 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 471 | ret = bq27x00_simple_value(di->cache.time_to_empty, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 472 | break; |
| 473 | case POWER_SUPPLY_PROP_TIME_TO_EMPTY_AVG: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 474 | ret = bq27x00_simple_value(di->cache.time_to_empty_avg, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 475 | break; |
| 476 | case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW: |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 477 | ret = bq27x00_simple_value(di->cache.time_to_full, val); |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 478 | break; |
Lars-Peter Clausen | 5661f33 | 2010-05-24 20:36:52 +0200 | [diff] [blame] | 479 | case POWER_SUPPLY_PROP_TECHNOLOGY: |
| 480 | val->intval = POWER_SUPPLY_TECHNOLOGY_LION; |
| 481 | break; |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 482 | case POWER_SUPPLY_PROP_CHARGE_NOW: |
| 483 | ret = bq27x00_simple_value(bq27x00_battery_read_nac(di), val); |
| 484 | break; |
| 485 | case POWER_SUPPLY_PROP_CHARGE_FULL: |
| 486 | ret = bq27x00_simple_value(di->cache.charge_full, val); |
| 487 | break; |
| 488 | case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN: |
| 489 | ret = bq27x00_simple_value(di->charge_design_full, val); |
| 490 | break; |
Pali Rohár | 73c244a | 2011-04-30 23:59:35 +0200 | [diff] [blame] | 491 | case POWER_SUPPLY_PROP_CYCLE_COUNT: |
| 492 | ret = bq27x00_simple_value(di->cache.cycle_count, val); |
Pali Rohár | 631c17e | 2011-02-01 00:08:02 +0100 | [diff] [blame] | 493 | break; |
| 494 | case POWER_SUPPLY_PROP_ENERGY_NOW: |
| 495 | ret = bq27x00_battery_energy(di, val); |
| 496 | break; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 497 | default: |
| 498 | return -EINVAL; |
| 499 | } |
| 500 | |
Grazvydas Ignotas | 4e924a8 | 2010-02-27 17:06:09 +0200 | [diff] [blame] | 501 | return ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 504 | static void bq27x00_external_power_changed(struct power_supply *psy) |
| 505 | { |
| 506 | struct bq27x00_device_info *di = to_bq27x00_device_info(psy); |
| 507 | |
| 508 | cancel_delayed_work_sync(&di->work); |
| 509 | schedule_delayed_work(&di->work, 0); |
| 510 | } |
| 511 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 512 | static int bq27x00_powersupply_init(struct bq27x00_device_info *di) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 513 | { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 514 | int ret; |
| 515 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 516 | di->bat.type = POWER_SUPPLY_TYPE_BATTERY; |
| 517 | di->bat.properties = bq27x00_battery_props; |
| 518 | di->bat.num_properties = ARRAY_SIZE(bq27x00_battery_props); |
| 519 | di->bat.get_property = bq27x00_battery_get_property; |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 520 | di->bat.external_power_changed = bq27x00_external_power_changed; |
| 521 | |
| 522 | INIT_DELAYED_WORK(&di->work, bq27x00_battery_poll); |
| 523 | mutex_init(&di->lock); |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 524 | |
| 525 | ret = power_supply_register(di->dev, &di->bat); |
| 526 | if (ret) { |
| 527 | dev_err(di->dev, "failed to register battery: %d\n", ret); |
| 528 | return ret; |
| 529 | } |
| 530 | |
| 531 | dev_info(di->dev, "support ver. %s enabled\n", DRIVER_VERSION); |
| 532 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 533 | bq27x00_update(di); |
| 534 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 535 | return 0; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 538 | static void bq27x00_powersupply_unregister(struct bq27x00_device_info *di) |
| 539 | { |
| 540 | cancel_delayed_work_sync(&di->work); |
| 541 | |
| 542 | power_supply_unregister(&di->bat); |
| 543 | |
| 544 | mutex_destroy(&di->lock); |
| 545 | } |
| 546 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 547 | |
| 548 | /* i2c specific code */ |
| 549 | #ifdef CONFIG_BATTERY_BQ27X00_I2C |
| 550 | |
| 551 | /* If the system has several batteries we need a different name for each |
| 552 | * of them... |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 553 | */ |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 554 | static DEFINE_IDR(battery_id); |
| 555 | static DEFINE_MUTEX(battery_mutex); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 556 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 557 | 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] | 558 | { |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 559 | struct i2c_client *client = to_i2c_client(di->dev); |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 560 | struct i2c_msg msg[2]; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 561 | unsigned char data[2]; |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 562 | int ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 563 | |
| 564 | if (!client->adapter) |
| 565 | return -ENODEV; |
| 566 | |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 567 | msg[0].addr = client->addr; |
| 568 | msg[0].flags = 0; |
| 569 | msg[0].buf = ® |
| 570 | msg[0].len = sizeof(reg); |
| 571 | msg[1].addr = client->addr; |
| 572 | msg[1].flags = I2C_M_RD; |
| 573 | msg[1].buf = data; |
Lars-Peter Clausen | 2ec523a | 2011-02-02 19:35:07 +0100 | [diff] [blame] | 574 | if (single) |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 575 | msg[1].len = 1; |
Lars-Peter Clausen | 2ec523a | 2011-02-02 19:35:07 +0100 | [diff] [blame] | 576 | else |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 577 | msg[1].len = 2; |
Lars-Peter Clausen | 2ec523a | 2011-02-02 19:35:07 +0100 | [diff] [blame] | 578 | |
Grazvydas Ignotas | 9e912f4 | 2011-02-15 23:27:35 +0200 | [diff] [blame] | 579 | ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); |
Lars-Peter Clausen | 2ec523a | 2011-02-02 19:35:07 +0100 | [diff] [blame] | 580 | if (ret < 0) |
| 581 | return ret; |
| 582 | |
| 583 | if (!single) |
| 584 | ret = get_unaligned_le16(data); |
| 585 | else |
| 586 | ret = data[0]; |
| 587 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 588 | return ret; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 591 | static int bq27x00_battery_probe(struct i2c_client *client, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 592 | const struct i2c_device_id *id) |
| 593 | { |
| 594 | char *name; |
| 595 | struct bq27x00_device_info *di; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 596 | int num; |
| 597 | int retval = 0; |
| 598 | |
| 599 | /* Get new ID for the new battery device */ |
| 600 | retval = idr_pre_get(&battery_id, GFP_KERNEL); |
| 601 | if (retval == 0) |
| 602 | return -ENOMEM; |
| 603 | mutex_lock(&battery_mutex); |
| 604 | retval = idr_get_new(&battery_id, client, &num); |
| 605 | mutex_unlock(&battery_mutex); |
| 606 | if (retval < 0) |
| 607 | return retval; |
| 608 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 609 | name = kasprintf(GFP_KERNEL, "%s-%d", id->name, num); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 610 | if (!name) { |
| 611 | dev_err(&client->dev, "failed to allocate device name\n"); |
| 612 | retval = -ENOMEM; |
| 613 | goto batt_failed_1; |
| 614 | } |
| 615 | |
| 616 | di = kzalloc(sizeof(*di), GFP_KERNEL); |
| 617 | if (!di) { |
| 618 | dev_err(&client->dev, "failed to allocate device info data\n"); |
| 619 | retval = -ENOMEM; |
| 620 | goto batt_failed_2; |
| 621 | } |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 622 | |
Lars-Peter Clausen | a40402e | 2010-05-24 19:37:58 +0200 | [diff] [blame] | 623 | di->id = num; |
| 624 | di->dev = &client->dev; |
| 625 | di->chip = id->driver_data; |
| 626 | di->bat.name = name; |
| 627 | di->bus.read = &bq27x00_read_i2c; |
| 628 | |
| 629 | if (bq27x00_powersupply_init(di)) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 630 | goto batt_failed_3; |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 631 | |
| 632 | i2c_set_clientdata(client, di); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 633 | |
| 634 | return 0; |
| 635 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 636 | batt_failed_3: |
| 637 | kfree(di); |
| 638 | batt_failed_2: |
| 639 | kfree(name); |
| 640 | batt_failed_1: |
| 641 | mutex_lock(&battery_mutex); |
| 642 | idr_remove(&battery_id, num); |
| 643 | mutex_unlock(&battery_mutex); |
| 644 | |
| 645 | return retval; |
| 646 | } |
| 647 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 648 | static int bq27x00_battery_remove(struct i2c_client *client) |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 649 | { |
| 650 | struct bq27x00_device_info *di = i2c_get_clientdata(client); |
| 651 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 652 | bq27x00_powersupply_unregister(di); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 653 | |
| 654 | kfree(di->bat.name); |
| 655 | |
| 656 | mutex_lock(&battery_mutex); |
| 657 | idr_remove(&battery_id, di->id); |
| 658 | mutex_unlock(&battery_mutex); |
| 659 | |
| 660 | kfree(di); |
| 661 | |
| 662 | return 0; |
| 663 | } |
| 664 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 665 | static const struct i2c_device_id bq27x00_id[] = { |
| 666 | { "bq27200", BQ27000 }, /* bq27200 is same as bq27000, but with i2c */ |
| 667 | { "bq27500", BQ27500 }, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 668 | {}, |
| 669 | }; |
Pali Rohár | fd9b958 | 2011-02-01 00:10:41 +0100 | [diff] [blame] | 670 | MODULE_DEVICE_TABLE(i2c, bq27x00_id); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 671 | |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 672 | static struct i2c_driver bq27x00_battery_driver = { |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 673 | .driver = { |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 674 | .name = "bq27x00-battery", |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 675 | }, |
Grazvydas Ignotas | e20908d | 2010-02-12 23:57:23 +0200 | [diff] [blame] | 676 | .probe = bq27x00_battery_probe, |
| 677 | .remove = bq27x00_battery_remove, |
| 678 | .id_table = bq27x00_id, |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 679 | }; |
| 680 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 681 | static inline int bq27x00_battery_i2c_init(void) |
| 682 | { |
| 683 | int ret = i2c_add_driver(&bq27x00_battery_driver); |
| 684 | if (ret) |
| 685 | printk(KERN_ERR "Unable to register BQ27x00 i2c driver\n"); |
| 686 | |
| 687 | return ret; |
| 688 | } |
| 689 | |
| 690 | static inline void bq27x00_battery_i2c_exit(void) |
| 691 | { |
| 692 | i2c_del_driver(&bq27x00_battery_driver); |
| 693 | } |
| 694 | |
| 695 | #else |
| 696 | |
| 697 | static inline int bq27x00_battery_i2c_init(void) { return 0; } |
| 698 | static inline void bq27x00_battery_i2c_exit(void) {}; |
| 699 | |
| 700 | #endif |
| 701 | |
| 702 | /* platform specific code */ |
| 703 | #ifdef CONFIG_BATTERY_BQ27X00_PLATFORM |
| 704 | |
| 705 | 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] | 706 | bool single) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 707 | { |
| 708 | struct device *dev = di->dev; |
| 709 | struct bq27000_platform_data *pdata = dev->platform_data; |
| 710 | unsigned int timeout = 3; |
| 711 | int upper, lower; |
| 712 | int temp; |
| 713 | |
| 714 | if (!single) { |
| 715 | /* Make sure the value has not changed in between reading the |
| 716 | * lower and the upper part */ |
| 717 | upper = pdata->read(dev, reg + 1); |
| 718 | do { |
| 719 | temp = upper; |
| 720 | if (upper < 0) |
| 721 | return upper; |
| 722 | |
| 723 | lower = pdata->read(dev, reg); |
| 724 | if (lower < 0) |
| 725 | return lower; |
| 726 | |
| 727 | upper = pdata->read(dev, reg + 1); |
| 728 | } while (temp != upper && --timeout); |
| 729 | |
| 730 | if (timeout == 0) |
| 731 | return -EIO; |
| 732 | |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 733 | return (upper << 8) | lower; |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 734 | } |
Lars-Peter Clausen | 297a533 | 2011-01-07 20:12:47 +0100 | [diff] [blame] | 735 | |
| 736 | return pdata->read(dev, reg); |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | static int __devinit bq27000_battery_probe(struct platform_device *pdev) |
| 740 | { |
| 741 | struct bq27x00_device_info *di; |
| 742 | struct bq27000_platform_data *pdata = pdev->dev.platform_data; |
| 743 | int ret; |
| 744 | |
| 745 | if (!pdata) { |
| 746 | dev_err(&pdev->dev, "no platform_data supplied\n"); |
| 747 | return -EINVAL; |
| 748 | } |
| 749 | |
| 750 | if (!pdata->read) { |
| 751 | dev_err(&pdev->dev, "no hdq read callback supplied\n"); |
| 752 | return -EINVAL; |
| 753 | } |
| 754 | |
| 755 | di = kzalloc(sizeof(*di), GFP_KERNEL); |
| 756 | if (!di) { |
| 757 | dev_err(&pdev->dev, "failed to allocate device info data\n"); |
| 758 | return -ENOMEM; |
| 759 | } |
| 760 | |
| 761 | platform_set_drvdata(pdev, di); |
| 762 | |
| 763 | di->dev = &pdev->dev; |
| 764 | di->chip = BQ27000; |
| 765 | |
| 766 | di->bat.name = pdata->name ?: dev_name(&pdev->dev); |
| 767 | di->bus.read = &bq27000_read_platform; |
| 768 | |
| 769 | ret = bq27x00_powersupply_init(di); |
| 770 | if (ret) |
| 771 | goto err_free; |
| 772 | |
| 773 | return 0; |
| 774 | |
| 775 | err_free: |
| 776 | platform_set_drvdata(pdev, NULL); |
| 777 | kfree(di); |
| 778 | |
| 779 | return ret; |
| 780 | } |
| 781 | |
| 782 | static int __devexit bq27000_battery_remove(struct platform_device *pdev) |
| 783 | { |
| 784 | struct bq27x00_device_info *di = platform_get_drvdata(pdev); |
| 785 | |
Lars-Peter Clausen | 740b755 | 2011-01-07 20:14:53 +0100 | [diff] [blame] | 786 | bq27x00_powersupply_unregister(di); |
| 787 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 788 | platform_set_drvdata(pdev, NULL); |
| 789 | kfree(di); |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | static struct platform_driver bq27000_battery_driver = { |
| 795 | .probe = bq27000_battery_probe, |
| 796 | .remove = __devexit_p(bq27000_battery_remove), |
| 797 | .driver = { |
| 798 | .name = "bq27000-battery", |
| 799 | .owner = THIS_MODULE, |
| 800 | }, |
| 801 | }; |
| 802 | |
| 803 | static inline int bq27x00_battery_platform_init(void) |
| 804 | { |
| 805 | int ret = platform_driver_register(&bq27000_battery_driver); |
| 806 | if (ret) |
| 807 | printk(KERN_ERR "Unable to register BQ27000 platform driver\n"); |
| 808 | |
| 809 | return ret; |
| 810 | } |
| 811 | |
| 812 | static inline void bq27x00_battery_platform_exit(void) |
| 813 | { |
| 814 | platform_driver_unregister(&bq27000_battery_driver); |
| 815 | } |
| 816 | |
| 817 | #else |
| 818 | |
| 819 | static inline int bq27x00_battery_platform_init(void) { return 0; } |
| 820 | static inline void bq27x00_battery_platform_exit(void) {}; |
| 821 | |
| 822 | #endif |
| 823 | |
| 824 | /* |
| 825 | * Module stuff |
| 826 | */ |
| 827 | |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 828 | static int __init bq27x00_battery_init(void) |
| 829 | { |
| 830 | int ret; |
| 831 | |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 832 | ret = bq27x00_battery_i2c_init(); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 833 | if (ret) |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 834 | return ret; |
| 835 | |
| 836 | ret = bq27x00_battery_platform_init(); |
| 837 | if (ret) |
| 838 | bq27x00_battery_i2c_exit(); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 839 | |
| 840 | return ret; |
| 841 | } |
| 842 | module_init(bq27x00_battery_init); |
| 843 | |
| 844 | static void __exit bq27x00_battery_exit(void) |
| 845 | { |
Lars-Peter Clausen | 7fb7ba5 | 2010-05-24 19:55:27 +0200 | [diff] [blame] | 846 | bq27x00_battery_platform_exit(); |
| 847 | bq27x00_battery_i2c_exit(); |
Rodolfo Giometti | b996ad0 | 2008-08-20 16:52:58 -0700 | [diff] [blame] | 848 | } |
| 849 | module_exit(bq27x00_battery_exit); |
| 850 | |
| 851 | MODULE_AUTHOR("Rodolfo Giometti <giometti@linux.it>"); |
| 852 | MODULE_DESCRIPTION("BQ27x00 battery monitor driver"); |
| 853 | MODULE_LICENSE("GPL"); |