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