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