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